<commit_before>import json

import h5py
import numpy as np

from .. import options
from . import BrainData, VolumeData, VertexData

default_cmap = options.config.get("basic", "default_cmap")

class View(object):
    def __init__(self, cmap=None, vmin=None, vmax=None, state=None, **kwargs):
        self.cmap = cmap if cmap is not None else default_cmap
        self.vmin = vmin
        self.vmax = vmax
        self.state = state
        self.attrs = kwargs
        if 'priority' not in self.attrs:
            self.attrs['priority'] = 1

    @property
    def priority(self):
        return self.attrs['priority']

    @priority.setter
    def priority(self, value):
        self.attrs['priority'] = value

    def __call__(self, data, description=""):
        return DataView(data, description, cmap=self.cmap, vmin=self.vmin, vmax=self.vmax, state=self.state)

class DataView(View):
    def __init__(self, data, description="", **kwargs):
        super(DataView, self).__init__(**kwargs)
        if isinstance(data, list):
            #validate if the input is of a recognizable form
            if len(data) != 1 or len(data[0]) != 2:
                raise TypeError("Sorry, multi views are currently not supported...")
            xdim, ydim = map(DataView.normalize, data[0])
            if xdim.subject != ydim.subject:
                raise TypeError("2D data views require the same subject")
            if xdim.raw or ydim.raw:
                raise TypeError("2D data views cannot be raw")
            #this awful bit of logic checks validity of movies
            if xdim.movie ^ ydim.movie or (
                xdim.movie and 
                len(xdim.data) != len(ydim.data)):
                raise TypeError('2D movies must be same length')

            no_nan_x = xdim.data[~np.isnan(xdim.data)]
            no_nan_y = ydim.data[~np.isnan(ydim.data)]
            if self.vmin is None:
                self.vmin = [(
                    float(np.percentile(no_nan_x, 1)), 
                    float(np.percentile(no_nan_y, 1)))]
            if self.vmax is None:
                self.vmax = [(
                    float(np.percentile(no_nan_x, 99)), 
                    float(np.percentile(no_nan_y, 99)))]
            self.data = [(xdim, ydim)]
        else:
            self.data = DataView.normalize(data)

        self.description = description
        
        no_nan = self.data.data[~np.isnan(self.data.data)]
        if self.vmin is None:
            self.vmin = float(np.percentile(no_nan, 1))
        if self.vmax is None:
            self.vmax = float(np.percentile(no_nan, 99))

    @staticmethod
    def normalize(data):
        if isinstance(data, tuple):
            if len(data) == 3:
                return VolumeData(*data)
            elif len(data) == 2:
                return VertexData(*data)
            else:
                raise TypeError("Invalid input for DataView")
        elif isinstance(data, BrainData):
            return data
        elif isinstance(data, DataView) and isinstance(data.data, BrainData):
            return data.data
        else:
            raise TypeError("Invalid input for DataView")

    @classmethod
    def from_hdf(cls, ds, node):
        data = []
        for name in json.loads(node[0]):
            if isinstance(name, list):
                d = []
                for n in name:
                    bd = BrainData.from_hdf(ds, node.file.get(n))
                    d.append(bd)
                data.append(d)
            else:
                bd = BrainData.from_hdf(ds, node.file.get(name))
                data.append(bd)
        if len(data) < 2 and isinstance(data[0], BrainData):
            data = data[0]
        desc = node[1]
        cmap = node[2]
        vmin = json.loads(node[3])
        vmax = json.loads(node[4])
        state = json.loads(node[5])
        attrs = json.loads(node[6])
        return cls(data, cmap=cmap, vmin=vmin, vmax=vmax, description=desc)

    def to_json(self):
        dnames = []
        if isinstance(self.data, BrainData):
            dnames.append(self.data.name)<commit_msg>Fix bug with passing dataview directly to webshow<commit_after>import json

import h5py
import numpy as np

from .. import options
from . import BrainData, VolumeData, VertexData

default_cmap = options.config.get("basic", "default_cmap")

class View(object):
    def __init__(self, cmap=None, vmin=None, vmax=None, state=None, **kwargs):
        self.cmap = cmap if cmap is not None else default_cmap
        self.vmin = vmin
        self.vmax = vmax
        self.state = state
        self.attrs = kwargs
        if 'priority' not in self.attrs:
            self.attrs['priority'] = 1

    @property
    def priority(self):
        return self.attrs['priority']

    @priority.setter
    def priority(self, value):
        self.attrs['priority'] = value

    def __call__(self, data, description=""):
        return DataView(data, description, cmap=self.cmap, vmin=self.vmin, vmax=self.vmax, state=self.state)

class DataView(View):
    def __init__(self, data, description="", **kwargs):
        super(DataView, self).__init__(**kwargs)
        if isinstance(data, list):
            #validate if the input is of a recognizable form
            if len(data) != 1 or len(data[0]) != 2:
                raise TypeError("Sorry, multi views are currently not supported...")
            xdim, ydim = map(DataView.normalize, data[0])
            if xdim.subject != ydim.subject:
                raise TypeError("2D data views require the same subject")
            if xdim.raw or ydim.raw:
                raise TypeError("2D data views cannot be raw")
            #this awful bit of logic checks validity of movies
            if xdim.movie ^ ydim.movie or (
                xdim.movie and 
                len(xdim.data) != len(ydim.data)):
                raise TypeError('2D movies must be same length')

            no_nan_x = xdim.data[~np.isnan(xdim.data)]
            no_nan_y = ydim.data[~np.isnan(ydim.data)]
            if self.vmin is None:
                self.vmin = [(
                    float(np.percentile(no_nan_x, 1)), 
                    float(np.percentile(no_nan_y, 1)))]
            if self.vmax is None:
                self.vmax = [(
                    float(np.percentile(no_nan_x, 99)), 
                    float(np.percentile(no_nan_y, 99)))]
            self.data = [(xdim, ydim)]
        else:
            self.data = DataView.normalize(data)

        self.description = description

        no_nan = self.data.data[~np.isnan(self.data.data)]
        if self.vmin is None:
            self.vmin = float(np.percentile(no_nan, 1))
        if self.vmax is None:
            self.vmax = float(np.percentile(no_nan, 99))

    @staticmethod
    def normalize(data):
        if isinstance(data, tuple):
            if len(data) == 3:
                return VolumeData(*data)
            elif len(data) == 2:
                return VertexData(*data)
            else:
                raise TypeError("Invalid input for DataView")
        elif isinstance(data, BrainData):
            return data
        elif isinstance(data, DataView) and isinstance(data.data, BrainData):
            return data.data
        else:
            raise TypeError("Invalid input for DataView")

    @classmethod
    def from_hdf(cls, ds, node):
        data = []
        for name in json.loads(node[0]):
            if isinstance(name, list):
                d = []
                for n in name:
                    bd = BrainData.from_hdf(ds, node.file.get(n))
                    d.append(bd)
                data.append(d)
            else:
                bd = BrainData.from_hdf(ds, node.file.get(name))
                data.append(bd)
        if len(data) < 2 and isinstance(data[0], BrainData):
            data = data[0]
        desc = node[1]
        cmap = node[2]
        vmin = json.loads(node[3])
        vmax = json.loads(node[4])
        state = json.loads(node[5])
        attrs = json.loads(node[6])
        return cls(data, cmap=cmap, vmin=vmin, vmax=vmax, description=desc)

    @property
    def uniques(self):
        """Return the set of unique BrainData objects contained by this dataview"""
        uniques = set()
        for data in self:
            uniques.add(data)

        return uniques

    def to_json(self):
        dnames = []
        if isinstance(self.data, BrainData):
            dnames.append(self.data.name)
{'fraction_non_alphanumeric': 0.11107168204400283, 'fraction_numerical': 0.007097232079488999, 'mean_word_length': 1.8343949044585988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4904748', 'n_tokens_mistral': 2617, 'n_tokens_neox': 2491, 'n_words': 645}