Пример #1
0
class PivotTable(PlotObject):
    title = String("Pivot Table")
    description = String("")
    source = Instance(has_ref=True)
    data = Dict()
    fields = List()  # List[{name: String, dtype: String}]
    rows = List()
    columns = List()
    values = List()
    filters = List()
    manual_update = Bool(True)

    def setup_events(self):
        self.on_change('rows', self, 'get_data')
        self.on_change('columns', self, 'get_data')
        self.on_change('values', self, 'get_data')
        self.on_change('filters', self, 'get_data')

        if not self.fields:
            self.fields = self.source.fields()

        if not self.data:
            self.get_data()

    def get_data(self, obj=None, attrname=None, old=None, new=None):
        self.data = self.source.pivot(
            dict(
                rows=self.rows,
                columns=self.columns,
                values=self.values,
                filters=self.filters,
            ))
Пример #2
0
    def test_Bool(self):
        prop = Bool()

        self.assertTrue(prop.is_valid(None))
        self.assertTrue(prop.is_valid(False))
        self.assertTrue(prop.is_valid(True))
        self.assertFalse(prop.is_valid(0))
        self.assertFalse(prop.is_valid(1))
        self.assertFalse(prop.is_valid(0.0))
        self.assertFalse(prop.is_valid(1.0))
        self.assertFalse(prop.is_valid(1.0+1.0j))
        self.assertFalse(prop.is_valid(""))
        self.assertFalse(prop.is_valid(()))
        self.assertFalse(prop.is_valid([]))
        self.assertFalse(prop.is_valid({}))
        self.assertFalse(prop.is_valid(Foo()))
Пример #3
0
class DataTable(PlotObject):
    source = Instance(has_ref=True)
    sort = List()
    group = List()
    offset = Int(default=0)
    length = Int(default=100)
    maxlength = Int()
    totallength = Int()
    tabledata = Dict()
    filterselected = Bool(default=False)

    def setup_events(self):
        self.on_change('sort', self, 'get_data')
        self.on_change('group', self, 'get_data')
        self.on_change('length', self, 'get_data')
        self.on_change('offset', self, 'get_data')
        self.on_change('filterselected', self, 'get_data')
        self.source.on_change('selected', self, 'get_data')
        self.source.on_change('data', self, 'get_data')
        self.source.on_change('computed_columns', self, 'get_data')
        if not self.tabledata:
            self.get_data()

    def transform(self):
        return dict(
            sort=self.sort,
            group=self.group,
            offset=self.offset,
            length=self.length,
            filterselected=self.filterselected,
        )

    def setselect(self, select):
        self.source.setselect(select, self.transform())
        self.get_data()

    def select(self, select):
        self.source.select(select, self.transform())
        self.get_data()

    def deselect(self, deselect):
        self.source.deselect(deselect, self.transform())
        self.get_data()

    def get_data(self, obj=None, attrname=None, old=None, new=None):
        data = self.source.get_data(self.transform())
        self.maxlength = data.pop('maxlength')
        self.totallength = data.pop('totallength')
        self.tabledata = data
Пример #4
0
    def test_Bool(self):
        prop = Bool()

        self.assertTrue(prop.is_valid(None))
        self.assertTrue(prop.is_valid(False))
        self.assertTrue(prop.is_valid(True))
        self.assertFalse(prop.is_valid(0))
        self.assertFalse(prop.is_valid(1))
        self.assertFalse(prop.is_valid(0.0))
        self.assertFalse(prop.is_valid(1.0))
        self.assertFalse(prop.is_valid(1.0 + 1.0j))
        self.assertFalse(prop.is_valid(""))
        self.assertFalse(prop.is_valid(()))
        self.assertFalse(prop.is_valid([]))
        self.assertFalse(prop.is_valid({}))
        self.assertFalse(prop.is_valid(Foo()))
Пример #5
0
    def test_Bool(self):
        prop = Bool()

        self.assertTrue(prop.is_valid(None))
        self.assertTrue(prop.is_valid(False))
        self.assertTrue(prop.is_valid(True))
        self.assertFalse(prop.is_valid(0))
        self.assertFalse(prop.is_valid(1))
        self.assertFalse(prop.is_valid(0.0))
        self.assertFalse(prop.is_valid(1.0))
        self.assertFalse(prop.is_valid(1.0+1.0j))
        self.assertFalse(prop.is_valid(""))
        self.assertFalse(prop.is_valid(()))
        self.assertFalse(prop.is_valid([]))
        self.assertFalse(prop.is_valid({}))
        self.assertFalse(prop.is_valid(Foo()))

        try:
            import numpy as np
            self.assertTrue(prop.is_valid(np.bool8(False)))
            self.assertTrue(prop.is_valid(np.bool8(True)))
            self.assertFalse(prop.is_valid(np.int8(0)))
            self.assertFalse(prop.is_valid(np.int8(1)))
            self.assertFalse(prop.is_valid(np.int16(0)))
            self.assertFalse(prop.is_valid(np.int16(1)))
            self.assertFalse(prop.is_valid(np.int32(0)))
            self.assertFalse(prop.is_valid(np.int32(1)))
            self.assertFalse(prop.is_valid(np.int64(0)))
            self.assertFalse(prop.is_valid(np.int64(1)))
            self.assertFalse(prop.is_valid(np.uint8(0)))
            self.assertFalse(prop.is_valid(np.uint8(1)))
            self.assertFalse(prop.is_valid(np.uint16(0)))
            self.assertFalse(prop.is_valid(np.uint16(1)))
            self.assertFalse(prop.is_valid(np.uint32(0)))
            self.assertFalse(prop.is_valid(np.uint32(1)))
            self.assertFalse(prop.is_valid(np.uint64(0)))
            self.assertFalse(prop.is_valid(np.uint64(1)))
            self.assertFalse(prop.is_valid(np.float16(0)))
            self.assertFalse(prop.is_valid(np.float16(1)))
            self.assertFalse(prop.is_valid(np.float32(0)))
            self.assertFalse(prop.is_valid(np.float32(1)))
            self.assertFalse(prop.is_valid(np.float64(0)))
            self.assertFalse(prop.is_valid(np.float64(1)))
            self.assertFalse(prop.is_valid(np.complex64(1.0+1.0j)))
            self.assertFalse(prop.is_valid(np.complex128(1.0+1.0j)))
            self.assertFalse(prop.is_valid(np.complex256(1.0+1.0j)))
        except ImportError:
            pass
Пример #6
0
class PandasPivotTable(PlotObject):
    source = Instance(has_ref=True)
    sort = List()
    group = List()
    offset = Int(default=0)
    length = Int(default=100)
    maxlength = Int()
    totallength = Int()
    precision = Dict()
    tabledata = Dict()
    filterselected = Bool(default=False)

    def setup_events(self):
        self.on_change('sort', self, 'get_data')
        self.on_change('group', self, 'get_data')
        self.on_change('length', self, 'get_data')
        self.on_change('offset', self, 'get_data')
        self.on_change('precision', self, 'get_data')
        self.on_change('filterselected', self, 'get_data')
        self.source.on_change('selected', self, 'get_data')
        self.source.on_change('data', self, 'get_data')
        self.source.on_change('computed_columns', self, 'get_data')
        if not self.tabledata:
            self.get_data()

    def format_data(self, jsondata):
        """inplace manipulation of jsondata
        """
        precision = self.precision
        for colname, data in jsondata.iteritems():
            if colname == '_selected' or colname == '_counts':
                continue
            if self.source.metadata.get(colname, {}).get('date'):
                isdate = True
            else:
                isdate = False
            for idx, val in enumerate(data):
                if isdate:
                    timeobj = time.localtime(val / 1000.0)
                    data[idx] = time.strftime("%Y-%m-%d %H:%M:%S", timeobj)
                if isinstance(val, float):
                    data[idx] = "%%.%df" % precision.get(colname,
                                                         2) % data[idx]

    def transform(self):
        return dict(
            sort=self.sort,
            group=self.group,
            offset=self.offset,
            length=self.length,
            filterselected=self.filterselected,
        )

    def setselect(self, select):
        self.source.setselect(select, self.transform())
        self.get_data()

    def select(self, select):
        self.source.select(select, self.transform())
        self.get_data()

    def deselect(self, deselect):
        self.source.deselect(deselect, self.transform())
        self.get_data()

    def get_data(self, obj=None, attrname=None, old=None, new=None):
        data = self.source.get_data(self.transform())
        print data['data']['_selected']
        self.maxlength = data.pop('maxlength')
        self.totallength = data.pop('totallength')
        self.format_data(data['data'])
        self.tabledata = data
Пример #7
0
class AttrSpec(HasProps):
    """A container for assigning attributes to values and retrieving them as needed.

    A special function this provides is automatically handling cases where the provided
    iterator is too short compared to the distinct values provided.

    Once created as attr_spec, you can do attr_spec[data_label], where data_label must
    be a one dimensional tuple of values, representing the unique group in the data.

    See the :meth:`AttrSpec.setup` method for the primary way to provide an existing
    AttrSpec with data and column values and update all derived property values.
    """

    id = Any()
    data = Instance(ColumnDataSource)
    name = String(help='Name of the attribute the spec provides.')

    columns = Either(ColumnLabel,
                     List(ColumnLabel),
                     help="""
        The label or list of column labels that correspond to the columns that will be
        used to find all distinct values (single column) or combination of values (
        multiple columns) to then assign a unique attribute to. If not enough unique
        attribute values are found, then the attribute values will be cycled.
        """)

    default = Any(default=None,
                  help="""
        The default value for the attribute, which is used if no column is assigned to
        the attribute for plotting. If the default value is not provided, the first
        value in the `iterable` property is used.
        """)

    attr_map = Dict(Any,
                    Any,
                    help="""
        Created by the attribute specification when `iterable` and `data` are
        available. The `attr_map` will include a mapping between the distinct value(s)
        found in `columns` and the attribute value that has been assigned.
        """)

    iterable = List(Any,
                    default=None,
                    help="""
        The iterable of attribute values to assign to the distinct values found in
        `columns` of `data`.
        """)

    items = List(Any,
                 default=None,
                 help="""
        The attribute specification calculates this list of distinct values that are
        found in `columns` of `data`.
        """)

    sort = Bool(default=True,
                help="""
        A boolean flag to tell the attribute specification to sort `items`, when it is
        calculated. This affects which value of `iterable` is assigned to each distinct
        value in `items`.
        """)

    ascending = Bool(default=True,
                     help="""
        A boolean flag to tell the attribute specification how to sort `items` if the
        `sort` property is set to `True`. The default setting for `ascending` is `True`.
        """)

    def __init__(self,
                 columns=None,
                 df=None,
                 iterable=None,
                 default=None,
                 items=None,
                 **properties):
        """Create a lazy evaluated attribute specification.

        Args:
            columns: a list of column labels
            df(:class:`~pandas.DataFrame`): the data source for the attribute spec.
            iterable: an iterable of distinct attribute values
            default: a value to use as the default attribute when no columns are passed
            items: the distinct values in columns. If items is provided as input,
                then the values provided are used instead of being calculated. This can
                be used to force a specific order for assignment.
            **properties: other properties to pass to parent :class:`HasProps`
        """
        properties['columns'] = self._ensure_list(columns)

        if df is not None:
            properties['data'] = ColumnDataSource(df)

        if default is None and iterable is not None:
            default_iter = copy(iterable)
            properties['default'] = next(iter(default_iter))
        elif default is not None:
            properties['default'] = default

        if iterable is not None:
            properties['iterable'] = iterable

        if items is not None:
            properties['items'] = items

        super(AttrSpec, self).__init__(**properties)

    @staticmethod
    def _ensure_list(attr):
        """Always returns a list with the provided value. Returns the value if a list."""
        if isinstance(attr, str):
            return [attr]
        elif isinstance(attr, tuple):
            return list(attr)
        else:
            return attr

    @staticmethod
    def _ensure_tuple(attr):
        """Return tuple with the provided value. Returns the value if a tuple."""
        if not isinstance(attr, tuple):
            return (attr, )
        else:
            return attr

    def _setup_default(self):
        """Stores the first value of iterable into `default` property."""
        self.default = next(self._setup_iterable())

    def _setup_iterable(self):
        """Default behavior is to copy and cycle the provided iterable."""
        return cycle(copy(self.iterable))

    def _generate_items(self, df, columns):
        """Produce list of unique tuples that identify each item."""
        if self.items is None or len(self.items) == 0:
            if self.sort:
                df = df.sort(columns=columns, ascending=self.ascending)
            items = df[columns].drop_duplicates()
            self.items = [tuple(x) for x in items.to_records(index=False)]

    def _create_attr_map(self, df, columns):
        """Creates map between unique values and available attributes."""

        self._generate_items(df, columns)
        iterable = self._setup_iterable()

        iter_map = {}
        for item in self.items:
            item = self._ensure_tuple(item)
            iter_map[item] = next(iterable)
        return iter_map

    def set_columns(self, columns):
        """Set columns property and update derived properties as needed."""
        columns = self._ensure_list(columns)
        if all([col in self.data.column_names for col in columns]):
            self.columns = columns
        else:
            # we have input values other than columns
            # assume this is now the iterable at this point
            self.iterable = columns
            self._setup_default()

    def setup(self, data=None, columns=None):
        """Set the data and update derived properties as needed."""
        if data is not None:
            self.data = data

            if columns is not None:
                self.set_columns(columns)

        if self.columns is not None and self.data is not None:
            self.attr_map = self._create_attr_map(self.data.to_df(),
                                                  self.columns)

    def __getitem__(self, item):
        """Lookup the attribute to use for the given unique group label."""

        if not self.columns or not self.data or item is None:
            return self.default
        elif self._ensure_tuple(item) not in self.attr_map.keys():

            # make sure we have attr map
            self.setup()

        return self.attr_map[self._ensure_tuple(item)]
Пример #8
0
    def test_Bool(self):
        prop = Bool()

        self.assertTrue(prop.is_valid(None))
        self.assertTrue(prop.is_valid(False))
        self.assertTrue(prop.is_valid(True))
        self.assertFalse(prop.is_valid(0))
        self.assertFalse(prop.is_valid(1))
        self.assertFalse(prop.is_valid(0.0))
        self.assertFalse(prop.is_valid(1.0))
        self.assertFalse(prop.is_valid(1.0+1.0j))
        self.assertFalse(prop.is_valid(""))
        self.assertFalse(prop.is_valid(()))
        self.assertFalse(prop.is_valid([]))
        self.assertFalse(prop.is_valid({}))
        self.assertFalse(prop.is_valid(Foo()))

        try:
            import numpy as np
            self.assertTrue(prop.is_valid(np.bool8(False)))
            self.assertTrue(prop.is_valid(np.bool8(True)))
            self.assertFalse(prop.is_valid(np.int8(0)))
            self.assertFalse(prop.is_valid(np.int8(1)))
            self.assertFalse(prop.is_valid(np.int16(0)))
            self.assertFalse(prop.is_valid(np.int16(1)))
            self.assertFalse(prop.is_valid(np.int32(0)))
            self.assertFalse(prop.is_valid(np.int32(1)))
            self.assertFalse(prop.is_valid(np.int64(0)))
            self.assertFalse(prop.is_valid(np.int64(1)))
            self.assertFalse(prop.is_valid(np.uint8(0)))
            self.assertFalse(prop.is_valid(np.uint8(1)))
            self.assertFalse(prop.is_valid(np.uint16(0)))
            self.assertFalse(prop.is_valid(np.uint16(1)))
            self.assertFalse(prop.is_valid(np.uint32(0)))
            self.assertFalse(prop.is_valid(np.uint32(1)))
            self.assertFalse(prop.is_valid(np.uint64(0)))
            self.assertFalse(prop.is_valid(np.uint64(1)))
            self.assertFalse(prop.is_valid(np.float16(0)))
            self.assertFalse(prop.is_valid(np.float16(1)))
            self.assertFalse(prop.is_valid(np.float32(0)))
            self.assertFalse(prop.is_valid(np.float32(1)))
            self.assertFalse(prop.is_valid(np.float64(0)))
            self.assertFalse(prop.is_valid(np.float64(1)))
            self.assertFalse(prop.is_valid(np.complex64(1.0+1.0j)))
            self.assertFalse(prop.is_valid(np.complex128(1.0+1.0j)))
            self.assertFalse(prop.is_valid(np.complex256(1.0+1.0j)))
        except ImportError:
            pass