def test_dataset_cumulative_histogram_dask(self): import dask.array as da ds = Dataset((da.from_array(np.array(range(10), dtype='f'), chunks=(3)),), ['x'], datatype=['dask']) op_hist = histogram(ds, num_bins=3, cumulative=True, normed=True) hist = Histogram(([0, 3, 6, 9], [0.3, 0.6, 1]), vdims=('x_frequency', 'Frequency')) self.assertIsInstance(op_hist.data['x_frequency'], da.Array) self.assertEqual(op_hist, hist)
def setUp(self): self.store_copy = OptionTree(sorted(Store.options().items()), groups=['style', 'plot', 'norm']) self.backend = 'matplotlib' Store.current_backend = self.backend Store.options(val=OptionTree(groups=['plot', 'style'])) options = Store.options() self.default_plot = dict(plot1='plot1', plot2='plot2') options.Histogram = Options('plot', **self.default_plot) self.default_style = dict(style1='style1', style2='style2') options.Histogram = Options('style', **self.default_style) data = [np.random.normal() for i in range(10000)] frequencies, edges = np.histogram(data, 20) self.hist = Histogram(frequencies, edges) super(TestStoreInheritance, self).setUp()
def test_dataset_weighted_histogram_dask(self): import dask.array as da ds = Dataset((da.from_array(np.array(range(10), dtype='f'), chunks=3), da.from_array([i/10. for i in range(10)], chunks=3)), ['x', 'y'], datatype=['dask']) op_hist = histogram(ds, weight_dimension='y', num_bins=3, normed=True) hist = Histogram(([0, 3, 6, 9], [0.022222, 0.088889, 0.222222]), vdims='y') self.assertIsInstance(op_hist.data['y'], da.Array) self.assertEqual(op_hist, hist)
def setUp(self): self.xs = np.linspace(0, 2 * np.pi, 11) self.hxs = np.arange(len(self.xs)) self.sin = np.sin(self.xs) self.cos = np.cos(self.xs) sine_data = np.column_stack((self.xs, self.sin)) cos_data = np.column_stack((self.xs, self.cos)) self.curve = Curve(sine_data) self.path = Path([sine_data, cos_data]) self.histogram = Histogram((self.hxs, self.sin)) super(ElementConstructorTest, self).setUp()
def test_points_histogram_not_normed(self): points = Points([float(i) for i in range(10)]) op_hist = histogram(points, num_bins=3, normed=False) # Make sure that the name and label are as desired op_freq_dim = op_hist.get_dimension('x_frequency') self.assertEqual(op_freq_dim.label, 'x Frequency') # Because the operation labels are now different from the # default Element label, change back before comparing. op_hist = op_hist.redim(x_frequency='Frequency') hist = Histogram(([3, 3, 4], [0, 3, 6, 9])) self.assertEqual(op_hist, hist)
def test_histogram_operation_pd_period(self): dates = pd.date_range('2017-01-01', '2017-01-04', freq='D').to_period('D') op_hist = histogram(Dataset(dates, 'Date'), num_bins=4, normed=True) hist_data = { 'Date': np.array([ '2017-01-01T00:00:00.000000', '2017-01-01T18:00:00.000000', '2017-01-02T12:00:00.000000', '2017-01-03T06:00:00.000000', '2017-01-04T00:00:00.000000'], dtype='datetime64[us]'), 'Date_frequency': np.array([ 3.85802469e-18, 3.85802469e-18, 3.85802469e-18, 3.85802469e-18]) } hist = Histogram(hist_data, kdims='Date', vdims=('Date_frequency', 'Frequency')) self.assertEqual(op_hist, hist)
def test_points_histogram_cumulative(self): arr = np.arange(4) points = Points(arr) op_hist = histogram(points, cumulative=True, num_bins=3, normed=False) # Make sure that the name and label are as desired op_freq_dim = op_hist.get_dimension('x_frequency') self.assertEqual(op_freq_dim.label, 'x Frequency') # Because the operation labels are now different from the # default Element label, change back before comparing. op_hist = op_hist.redim(x_frequency='Frequency') hist = Histogram(([0, 1, 2, 3], [1, 2, 4])) self.assertEqual(op_hist, hist)
def test_histogram_operation_datetime64(self): dates = np.array([dt.datetime(2017, 1, i) for i in range(1, 5)]).astype('M') op_hist = histogram(Dataset(dates, 'Date'), num_bins=4, normed=True) hist_data = { 'Date': np.array([ '2017-01-01T00:00:00.000000', '2017-01-01T18:00:00.000000', '2017-01-02T12:00:00.000000', '2017-01-03T06:00:00.000000', '2017-01-04T00:00:00.000000'], dtype='datetime64[us]'), 'Date_frequency': np.array([ 3.85802469e-18, 3.85802469e-18, 3.85802469e-18, 3.85802469e-18]) } hist = Histogram(hist_data, kdims='Date', vdims=('Date_frequency', 'Frequency')) self.assertEqual(op_hist, hist)
def _process(self, tree, key=None): preferences = tree[self.p.feature + 'Preference'] layout = Layout() for s, p in self.p.projections: if s not in preferences: continue featurepref = preferences[s] if isinstance(featurepref, HoloMap): featurepref = featurepref.last feature = featurepref.value_dimensions[0] feature_arr = featurepref.data.flat cfs = tree.CFs[p] deltas, weights = [], [] for k, cf in cfs.items(): preferred = featurepref[k] weight_arr = cf.situated.data.flat feature_slice = feature_arr[weight_arr > 0] weight_slice = weight_arr[weight_arr > 0] if feature.cyclic: feature_delta = circular_dist(preferred, feature_slice, feature.range[1]) else: feature_delta = np.abs(feature_slice - preferred) deltas.append(feature_delta) weights.append(weight_slice) deltas = np.concatenate(deltas) weights = np.concatenate(weights) bin_range = (0, feature.range[1] / 2.) if feature.cyclic else None bins, edges = np.histogram(deltas, range=bin_range, bins=self.p.num_bins, weights=weights, normed=self.p.normalized) # Construct Elements label = ' '.join([s, p]) group = '%s Weight Distribution' % self.p.feature histogram = Histogram( bins, edges, group=group, label=label, kdims=[' '.join([self.p.feature, 'Difference'])], vdims=['Weight']) layout.WeightDistribution['_'.join([s, p])] = histogram return layout
def setUp(self): self.store_copy = OptionTree(sorted(Store.options().items()), groups=['style', 'plot', 'norm']) self.backend = 'matplotlib' Store.current_backend = self.backend Store.options(val=OptionTree(groups=['plot', 'style'])) options = Store.options() self.default_plot = dict(plot1='plot1', plot2='plot2') options.Histogram = Options('plot', **self.default_plot) self.default_style = dict(style1='style1', style2='style2') options.Histogram = Options('style', **self.default_style) data = [np.random.normal() for i in range(10000)] frequencies, edges = np.histogram(data, 20) self.hist = Histogram(frequencies, edges) super(TestStoreInheritance, self).setUp()
def test_histogram_operation_kwargs(self): points = Points([float(j) for i in range(10) for j in [i] * (2 * i)]) op_hist = histogram( points, dimension='y', normed=False, num_bins=10, bin_range=[0, 10], ) hist = Histogram( ([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 ], [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]), vdims=('y_count', 'Count'), kdims='y') # Check histogram self.assertEqual(op_hist, hist) # Check operation kwargs for histogram generated with operation self.assertEqual( op_hist._operation_kwargs, { 'dimension': 'y', 'normed': False, 'dynamic': False, 'bins': [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] }) # Test that operation_kwargs is preserved through clone self.assertEqual( op_hist.clone()._operation_kwargs, { 'dimension': 'y', 'normed': False, 'dynamic': False, 'bins': [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] }) # Check that operation kwargs is None for histogram generated directly # from the Histogram constructor self.assertIsNone(hist._operation_kwargs)
def test_histogram_range_y_explicit_upper(self): r = Histogram([1,2,3], [0, 1, 2, 3], vdims=[Dimension('y', range=(None, 4.))]).range(1) self.assertEqual(r, (1., 4.))
def test_histogram_range_y(self): r = Histogram([1,2,3], [0, 1, 2, 3]).range(1) self.assertEqual(r, (1., 3.0))
def test_histogram_range_x_explicit_lower(self): r = Histogram([1,2,3], [0, 1, 2, 3], kdims=[Dimension('x', range=(-1, None))]).range(0) self.assertEqual(r, (-1., 3.))
def test_histogram_range_x_explicit_upper(self): r = Histogram([1,2,3], [0, 1, 2, 3], kdims=[Dimension('x', range=(None, 4.))]).range(0) self.assertEqual(r, (0, 4.))
def test_histogram_range_y_explicit(self): r = Histogram(([0, 1, 2, 3], [1, 2, 3]), vdims=[Dimension('y', range=(0, 4.))]).range(1) self.assertEqual(r, (0., 4.))
def test_histogram_range_x_explicit(self): r = Histogram(([0, 1, 2, 3], [1, 2, 3]), kdims=[Dimension('x', range=(-1, 4.))]).range(0) self.assertEqual(r, (-1., 4.))
def test_points_histogram(self): points = Points([float(i) for i in range(10)]) op_hist = histogram(points, num_bins=3) hist = Histogram(([0.1, 0.1, 0.133333], [0, 3, 6, 9])) self.assertEqual(op_hist, hist)
def setUp(self): self.values = [i for i in range(10)] self.edges = [i for i in range(11)] self.hist=Histogram(self.values, self.edges)
def test_hist_curve_int_edges_construct(self): hist = Histogram(Curve(range(3))) values = hist.dimension_values(1) edges = hist.edges self.assertEqual(values, np.arange(3)) self.assertEqual(edges, np.array([-.5, .5, 1.5, 2.5]))
def test_hist_yvalues_construct(self): self.assertEqual(Histogram(self.sin), self.histogram)
def test_hist_zip_construct(self): self.assertEqual(Histogram(list(zip(self.hxs, self.sin))), self.histogram)
def test_points_histogram_not_normed(self): points = Points([float(i) for i in range(10)]) op_hist = histogram(points, num_bins=3, normed=False) hist = Histogram(([3, 3, 4], [0, 3, 6, 9])) self.assertEqual(op_hist, hist)
def test_points_histogram_bin_range(self): points = Points([float(i) for i in range(10)]) op_hist = histogram(points, num_bins=3, bin_range=(0, 3)) hist = Histogram(([0.25, 0.25, 0.5], [0., 1., 2., 3.])) self.assertEqual(op_hist, hist)
def test_histogram_range_y_explicit_lower(self): r = Histogram([1,2,3], [0, 1, 2, 3], vdims=[Dimension('y', range=(0., None))]).range(1) self.assertEqual(r, (0., 3.))
def test_histogram_range_x(self): r = Histogram(([0, 1, 2, 3], [1, 2, 3])).range(0) self.assertEqual(r, (0., 3.0))
def test_hist_curve_int_edges_construct(self): hist = Histogram(Curve(range(3))) values = hist.dimension_values(1) edges = hist.edges self.assertEqual(values, np.arange(3)) self.assertEqual(edges, np.array([-.5, .5, 1.5, 2.5]))
def test_hist_curve_int_edges_construct(self): hist = Histogram(Curve(range(3))) self.assertEqual(hist.data[0], np.arange(3)) self.assertEqual(hist.data[1], np.array([-.5, .5, 1.5, 2.5]))
def test_hist_array_construct(self): self.assertEqual(Histogram(np.column_stack((self.hxs, self.sin))), self.histogram)
def test_points_histogram_weighted(self): points = Points([float(i) for i in range(10)]) op_hist = histogram(points, num_bins=3, weight_dimension='y') hist = Histogram(([0.022222, 0.088889, 0.222222], [0, 3, 6, 9]), vdims=['y']) self.assertEqual(op_hist, hist)
def test_hist_curve_construct(self): hist = Histogram(Curve(([0.1, 0.3, 0.5], [2.1, 2.2, 3.3]))) values = hist.dimension_values(1) edges = hist.edges self.assertEqual(values, np.array([2.1, 2.2, 3.3])) self.assertEqual(edges, np.array([0, 0.2, 0.4, 0.6]))
class TestStoreInheritance(ComparisonTestCase): """ Tests to prevent regression after fix in 71c1f3a that resolves issue #43 """ def setUp(self): self.store_copy = OptionTree(sorted(Store.options().items()), groups=['style', 'plot', 'norm']) self.backend = 'matplotlib' Store.current_backend = self.backend Store.options(val=OptionTree(groups=['plot', 'style'])) options = Store.options() self.default_plot = dict(plot1='plot1', plot2='plot2') options.Histogram = Options('plot', **self.default_plot) self.default_style = dict(style1='style1', style2='style2') options.Histogram = Options('style', **self.default_style) data = [np.random.normal() for i in range(10000)] frequencies, edges = np.histogram(data, 20) self.hist = Histogram(frequencies, edges) super(TestStoreInheritance, self).setUp() def lookup_options(self, obj, group): return Store.lookup_options(self.backend, obj, group) def tearDown(self): Store.options(val=self.store_copy) Store._custom_options = {k:{} for k in Store._custom_options.keys()} super(TestStoreInheritance, self).tearDown() def test_original_style_options(self): self.assertEqual(self.lookup_options(self.hist, 'style').options, self.default_style) def test_original_plot_options(self): self.assertEqual(self.lookup_options(self.hist, 'plot').options, self.default_plot) def test_plot_inheritance_addition(self): "Adding an element" hist2 = self.hist(plot={'plot3':'plot3'}) self.assertEqual(self.lookup_options(hist2, 'plot').options, dict(plot1='plot1', plot2='plot2', plot3='plot3')) # Check style works as expected self.assertEqual(self.lookup_options(hist2, 'style').options, self.default_style) def test_plot_inheritance_override(self): "Overriding an element" hist2 = self.hist(plot={'plot1':'plot_child'}) self.assertEqual(self.lookup_options(hist2, 'plot').options, dict(plot1='plot_child', plot2='plot2')) # Check style works as expected self.assertEqual(self.lookup_options(hist2, 'style').options, self.default_style) def test_style_inheritance_addition(self): "Adding an element" hist2 = self.hist(style={'style3':'style3'}) self.assertEqual(self.lookup_options(hist2, 'style').options, dict(style1='style1', style2='style2', style3='style3')) # Check plot options works as expected self.assertEqual(self.lookup_options(hist2, 'plot').options, self.default_plot) def test_style_inheritance_override(self): "Overriding an element" hist2 = self.hist(style={'style1':'style_child'}) self.assertEqual(self.lookup_options(hist2, 'style').options, dict(style1='style_child', style2='style2')) # Check plot options works as expected self.assertEqual(self.lookup_options(hist2, 'plot').options, self.default_plot) def test_style_transfer(self): hist = self.hist.opts(style={'style1':'style_child'}) hist2 = self.hist.opts() opts = Store.lookup_options('matplotlib', hist2, 'style').kwargs self.assertEqual(opts, {'style1': 'style1', 'style2': 'style2'}) Store.transfer_options(hist, hist2, 'matplotlib') opts = Store.lookup_options('matplotlib', hist2, 'style').kwargs self.assertEqual(opts, {'style1': 'style_child', 'style2': 'style2'})
class TestStoreInheritance(ComparisonTestCase): """ Tests to prevent regression after fix in 71c1f3a that resolves issue #43 """ def setUp(self): if 'matplotlib' not in Store.renderers: raise SkipTest('Matplotlib backend not available.') self.store_copy = OptionTree(sorted(Store.options().items()), groups=Options._option_groups) self.backend = 'matplotlib' Store.current_backend = self.backend Store.options(val=OptionTree(groups=['plot', 'style'])) options = Store.options() self.default_plot = dict(plot1='plot1', plot2='plot2') options.Histogram = Options('plot', **self.default_plot) self.default_style = dict(style1='style1', style2='style2') options.Histogram = Options('style', **self.default_style) data = [np.random.normal() for i in range(10000)] frequencies, edges = np.histogram(data, 20) self.hist = Histogram(frequencies, edges) super(TestStoreInheritance, self).setUp() def lookup_options(self, obj, group): return Store.lookup_options(self.backend, obj, group) def tearDown(self): Store.options(val=self.store_copy) Store._custom_options = {k: {} for k in Store._custom_options.keys()} super(TestStoreInheritance, self).tearDown() def test_original_style_options(self): self.assertEqual( self.lookup_options(self.hist, 'style').options, self.default_style) def test_original_plot_options(self): self.assertEqual( self.lookup_options(self.hist, 'plot').options, self.default_plot) def test_plot_inheritance_addition(self): "Adding an element" hist2 = self.hist.opts(plot={'plot3': 'plot3'}) self.assertEqual( self.lookup_options(hist2, 'plot').options, dict(plot1='plot1', plot2='plot2', plot3='plot3')) # Check style works as expected self.assertEqual( self.lookup_options(hist2, 'style').options, self.default_style) def test_plot_inheritance_override(self): "Overriding an element" hist2 = self.hist.opts(plot={'plot1': 'plot_child'}) self.assertEqual( self.lookup_options(hist2, 'plot').options, dict(plot1='plot_child', plot2='plot2')) # Check style works as expected self.assertEqual( self.lookup_options(hist2, 'style').options, self.default_style) def test_style_inheritance_addition(self): "Adding an element" hist2 = self.hist.opts(style={'style3': 'style3'}) self.assertEqual( self.lookup_options(hist2, 'style').options, dict(style1='style1', style2='style2', style3='style3')) # Check plot options works as expected self.assertEqual( self.lookup_options(hist2, 'plot').options, self.default_plot) def test_style_inheritance_override(self): "Overriding an element" hist2 = self.hist.opts(style={'style1': 'style_child'}) self.assertEqual( self.lookup_options(hist2, 'style').options, dict(style1='style_child', style2='style2')) # Check plot options works as expected self.assertEqual( self.lookup_options(hist2, 'plot').options, self.default_plot) def test_style_transfer(self): if 'matplotlib' not in Store.renderers: raise SkipTest("test_style_transfer requires matplotlib") hist = self.hist.opts(style={'style1': 'style_child'}) hist2 = self.hist.opts() opts = Store.lookup_options('matplotlib', hist2, 'style').kwargs self.assertEqual(opts, {'style1': 'style1', 'style2': 'style2'}) Store.transfer_options(hist, hist2, 'matplotlib') opts = Store.lookup_options('matplotlib', hist2, 'style').kwargs self.assertEqual(opts, {'style1': 'style_child', 'style2': 'style2'})
def test_hist_curve_construct(self): hist = Histogram(Curve(([0.1, 0.3, 0.5], [2.1, 2.2, 3.3]))) values = hist.dimension_values(1) edges = hist.edges self.assertEqual(values, np.array([2.1, 2.2, 3.3])) self.assertEqual(edges, np.array([0, 0.2, 0.4, 0.6]))