def __init__(self, *args, **kwargs): """Create a Vega Bar Chart""" super(Bar, self).__init__(*args, **kwargs) #Scales self.scales['x'] = Scale(name='x', type='ordinal', range='width', domain=DataRef(data='table', field="data.idx")) self.scales['y'] = Scale(name='y', range='height', nice=True, domain=DataRef(data='table', field="data.val")) self.axes.extend([Axis(type='x', scale='x'), Axis(type='y', scale='y')]) #Marks enter_props = PropertySet(x=ValueRef(scale='x', field="data.idx"), y=ValueRef(scale='y', field="data.val"), width=ValueRef(scale='x', band=True, offset=-1), y2=ValueRef(scale='y', value=0)) update_props = PropertySet(fill=ValueRef(value='steelblue')) mark = Mark(type='rect', from_=MarkRef(data='table'), properties=MarkProperties(enter=enter_props, update=update_props)) self.marks.append(mark)
def test_scale_from_data(self): data = [[1, 2, 3], [2, -1, -4]] expected_scale = np.array([1.0 / 2.0, 1.0 / 2.0, 1.0 / 4.0]) calculated_scale = Scale.scale_from_data(data) testing.assert_array_equal(expected_scale, calculated_scale.factors)
def __init__(self, *args, **kwargs): """Create a Vega Line Chart""" super(Line, self).__init__(*args, **kwargs) #Line Updates self.scales['x'].type = 'linear' self.scales['y'].type = 'linear' if self._is_datetime: self.scales['x'].type = 'time' self.scales['color'] = Scale(name='color', type='ordinal', domain=DataRef(data='table', field='data.col'), range='category20') del self.marks[0] transform = MarkRef(data='table', transform=[Transform(type='facet', keys=['data.col'])]) enter_props = PropertySet(x=ValueRef(scale='x', field="data.idx"), y=ValueRef(scale='y', field="data.val"), stroke=ValueRef(scale="color", field='data.col'), stroke_width=ValueRef(value=2)) new_mark = Mark(type='group', from_=transform, marks=[Mark(type='line', properties=MarkProperties(enter=enter_props))]) self.marks.append(new_mark)
def __init__(self, *args, **kwargs): """Create a Vega Grouped Bar Chart""" if 'grouped' not in kwargs: kwargs['grouped'] = True super(GroupedBar, self).__init__(*args, **kwargs) del self.data['stats'] del self.scales['y'].type self.scales['y'].domain.field = 'data.val' self.scales['y'].domain.data = 'table' self.scales['x'].padding = 0.2 del self.marks[0].from_.transform[1] self.marks[0].from_.transform[0].keys[0] = 'data.idx' enter_props = PropertySet(x=ValueRef(scale='x', field="key"), width=ValueRef(scale='x', band=True)) self.marks[0].properties = MarkProperties(enter=enter_props) self.marks[0].scales = KeyedList() self.marks[0].scales['pos'] = Scale(name='pos', type='ordinal', range='width', domain=DataRef(field='data.group')) self.marks[0].marks[0].properties.enter.width.scale = 'pos' self.marks[0].marks[0].properties.enter.y.field = 'data.val' self.marks[0].marks[0].properties.enter.x.field = 'data.group' self.marks[0].marks[0].properties.enter.x.scale = 'pos' del self.marks[0].marks[0].properties.enter.y2.field self.marks[0].marks[0].properties.enter.y2.value = 0
def test_gradient_descent(self): scale = Scale.scale_from_data(self.test_dataset) # self.logreg = LogisticRegressor(self.train_dataset, self.train_labels, scale=scale) self.logreg = LogisticRegressor(self.train_dataset, self.train_labels) gr = GradientDescentRunner(self.logreg.get_gradient(), len(self.samples[0]) + 1, self.logreg.get_error_function(), alpha=1e-8, max_iter=300) weights = gr.run() self.logreg.weights = weights mismatches = 0 predictions = [self.logreg.predict(test_data) for test_data in self.test_dataset] print "All predictions: ", predictions for test_data, label in zip(self.test_dataset, self.test_labels): prediction = self.logreg.predict(test_data) if prediction != label: mismatches +=1 # print "Mismatch! Predicted ", prediction, ", True ", label total = len(self.test_dataset) print "total data: ", total print "total mismatch: ", mismatches print "percentage success: ", (100 - float(mismatches) / float(total) * 100), "%"
def test_gradient_descent(self): scale = Scale.scale_from_data(self.test_dataset) # self.logreg = LogisticRegressor(self.train_dataset, self.train_labels, scale=scale) self.logreg = LogisticRegressor(self.train_dataset, self.train_labels) gr = GradientDescentRunner(self.logreg.get_gradient(), len(self.samples[0]) + 1, self.logreg.get_error_function(), alpha=1e-8, max_iter=300) weights = gr.run() self.logreg.weights = weights mismatches = 0 predictions = [ self.logreg.predict(test_data) for test_data in self.test_dataset ] print "All predictions: ", predictions for test_data, label in zip(self.test_dataset, self.test_labels): prediction = self.logreg.predict(test_data) if prediction != label: mismatches += 1 # print "Mismatch! Predicted ", prediction, ", True ", label total = len(self.test_dataset) print "total data: ", total print "total mismatch: ", mismatches print "percentage success: ", ( 100 - float(mismatches) / float(total) * 100), "%"
def __init__(self, x_scale=None, y_scale=None, mark=None, width=None, height=None): if x_scale: self.x_scale = x_scale else: self.x_scale = Scale(name='x', range='width', type='ordinal', domain=DataRef(data='table', field='data.x')) if y_scale: self.y_scale = y_scale else: self.y_scale = Scale(name='y', range='height', type='linear', nice=True, domain=DataRef(data='table', field='data.y')) if mark: self.mark = mark else: self.mark = Mark( type='rect', from_=MarkRef(data='table'), properties=MarkProperties( enter=PropertySet(x=ValueRef(scale='x', field='data.x'), y=ValueRef(scale='y', field='data.y'), width=ValueRef(scale='x', band=True, offset=-1), y2=ValueRef(scale='y', value=0)), update=PropertySet(fill=ValueRef(value='steelblue')))) self.width = width or 400 self.height = height or 200 self.padding = {'top': 10, 'left': 30, 'bottom': 20, 'right': 10} self.x_axis = Axis(type='x', scale='x') self.y_axis = Axis(type='y', scale='y')
def __init__(self, samples, labels, weights=None, scale=None): # list_X = [r + [1] for r in samples] list_X = [r for r in samples] self.X = array(list_X) self.Y = array(labels) self.weights = weights if scale is None: self.scale = Scale.unit_scale(len(list_X[0])) else: # self.scale = scale.add_unit_elements(1) self.scale = scale
def __init__(self, train_data, train_labels, weights=None, scale=None): # Adding 1 at the end for the constant term list_X = [r + [1] for r in train_data] if scale is None: self.scale = Scale.unit_scale(len(list_X[0])) else: self.scale = scale.add_unit_elements(1) self.weights = weights self.X = self.scale(np.array(list_X)) self.Y = np.array(train_labels)
# SPDX-FileCopyrightText: 2021 Jose David M # # SPDX-License-Identifier: MIT ############################# """ This is a basic demonstration of a Scale Class. """ import time import board from scales import Scale display = board.DISPLAY my_scale = Scale( x=50, y=180, direction="vertical", divisions=3, limits=(0, 80), ) display.show(my_scale) values = [56, 58, 60, 65, 63, 60, 56, 54, 53, 42, 43, 44, 45, 52, 54] for val in values: my_scale.animate_pointer(val) time.sleep(2)
def test_mul(self): scale = Scale.unit_scale(3) two_scale = scale * 2 a = np.array([1, 2, 3]) scaled_a = two_scale.scale(a) testing.assert_array_equal(a * 2, scaled_a)
def test_basic(self): factors = [2, 3, 4] scale = Scale(np.array(factors)) ones = np.ones(3) scaled_a = scale.scale(ones) testing.assert_array_equal(factors, scaled_a)
def test_less_that_one(self): factors = [0.1, 0.2, 0.3] scale = Scale(np.array(factors)) ones = np.ones(3) scaled_a = scale.scale(ones) testing.assert_array_equal(factors, scaled_a)
def test_scale_on_2D_arrays(self): a = np.array([[1, 2, 3], [2, 3, 4]]) scale = Scale(np.array([0.5, 2, 0])) scaled_a = scale(a) testing.assert_array_equal(scale(a[0]), scaled_a[0]) testing.assert_array_equal(scale(a[1]), scaled_a[1])
def build_chart(): # LAYOUT layout = Layout() layout.configure(plot_width=1200) # TIME WINDOW today = date.toordinal(date.today()) duration = 21 end = today + duration # PLOT plot = Plot() plot.x = layout.plot.x plot.y = layout.plot.y plot.width = layout.plot.width plot.height = layout.plot.height plot.start = today plot.finish = end plot.clean_dates() plot.calculate_resolution() # SCALES scale = Scale() scale.x = layout.scales_top.x scale.y = layout.scales_top.y scale.width = layout.scales_top.width scale.height = layout.scales_top.height / 4 scale.start = plot.start scale.finish = plot.finish scale.resolution = plot.resolution scale.interval_type = 'days' scale.week_start = 0 scale.min_label_width = 20 scale.box_fill = 'pink' scale.ends = 'yellow' scale.label_type = 'd' scale.date_format = 'a w' scale.separator = '-' scale.font_size = 10 scale.text_x = 10 scale.text_y = scale.height * 0.65 # GRID grid = Grid() grid.interval_type = 'WEEKS' grid.week_start = 0 grid.x = layout.plot.x grid.y = layout.plot.y grid.height = plot.height grid.start = plot.start grid.finish = plot.finish grid.resolution = plot.resolution grid.line_width = 0.5 # VIEWPORT viewport = ViewPort() viewport.width = layout.chart.width viewport.height = layout.chart.height viewport.child_elements = [layout, scale, grid] viewport.order_child_elements() viewport.render_child_elements() return viewport.svg
def test_unit_scale(self): scale = Scale.unit_scale(3) a = np.array([1, 2, 3]) scaled_a = scale.scale(a) testing.assert_array_equal(a, scaled_a)