Example #1
0
class Attribute:
    def __init__(self):
        self.name = TextInput(value='Attribute name')

        self.sourcefile_options = ['', 'Tx data', 'Customer Master', 'Product master', 'Input manually']
        self.sourcefile = Select(value='', options=self.sourcefile_options)
        self.sourcefile.on_change('value', partial(self.update_sourcecolumns_options, cpq=None))
        self.sourcecolumns = Select(value='', options=[''])
        self.include_in_filters = CheckboxGroup(labels=[None], active=[0])

        self.configurations = [Configuration()]
        self.num_configs = Spinner(title="Number of configurations", low=1, high=200, step=1, value=1, width=150)
        self.num_configs.on_change('value', self.update_configs)

    def update_sourcefile(self, value='', cpq=None):
        self.sourcefile.value = value
        self.sourcefile.on_change('value', partial(self.update_sourcecolumns_options, cpq=cpq))
        self.set_sourcecolumns_options(cpq=cpq)

    def update_sourcecolumns_options(self, attr, old, new, cpq=None):
        """
        on_change event for changes in the sourcefile Select widget

        :param attr:
        :param old:
        :param new:
        :param cpq: Expects an instance of the PriceView class
        :return:
        """

        self.set_sourcecolumns_options(cpq=cpq)

    def set_sourcecolumns_options(self, cpq=None):
        col_options = None
        try:
            if self.sourcefile.value == 'Tx data':
                col_options = cpq.tx.df.columns.tolist()
            elif self.sourcefile.value == 'Customer Master':
                col_options = cpq.cust_master.df.columns.tolist()
            elif self.sourcefile.value == 'Product master':
                col_options = cpq.prod_master.df.columns.tolist()
        except AttributeError:
            pass

        self.sourcecolumns.options = col_options

    def update_configs(self, attr, old, new):
        """
        on_change event for changes in the num_configs Spinner
        """
        for i in range(new):
            if i + 1 > len(self.configurations):
                self.configurations.append(Configuration())
Example #2
0
class _DataSource:
    """
        Abstract base class
    """

    def __init__(self):
        self.df = None
        self.df_col_headers = None
        self.filepath = TextInput()
        self.filepath.on_change('value', self.update_data)
        self.cols = None
        self.num_columns = Spinner(title="Number of columns", low=0, high=100, step=1, value=1, width=150)
        self.num_columns.on_change('value', self.update_num_columns)
        self.col_type_selectors = [Select(options=None)]
        self.col_selectors = [Select(options=self.df_col_headers)]

    def import_data(self):
        infile = Path(self.filepath.value)
        self.df = pd.read_csv(infile, parse_dates=True, infer_datetime_format=True, encoding='unicode_escape')
        self.df_col_headers = self.df.columns.tolist()

        print(self.df)
        print(self.df_col_headers)

    def update_data(self, attr, old, new):
        self.import_data()

    def update_num_columns(self, attr, old, new):
        for i in range(self.num_columns.value):
            if i + 1 > len(self.col_type_selectors):
                self.col_type_selectors.append(Select(options=[*self.cols]))
                self.col_selectors.append(Select(options=self.df_col_headers))
            else:
                self.col_type_selectors[i].options = [*self.cols]
                self.col_selectors[i].options = self.df_col_headers

    def update_col_options(self):
        self.col_type_selectors[0].options = [*self.cols]
Example #3
0
class PriceGroup:
    """
    Each price group (typically one for customers and one for each of a handful of product groups) is going to have
    several things
        - a column data source for the coefficients
        - a table associated with the cds (and table columns to go along with it)
        - a spinner to set the number of attributes
        - an attribute list, with associated file sources and column names
    """

    def __init__(self, name='', pg_type='Product'):

        self.name = TextInput(value=name, width=250)
        self.pg_type = pg_type

        self.num_attributes = Spinner(title="Number of attributes", low=1, high=100, step=1, value=1, width=150)
        self.num_attributes.on_change('value', self.update_attributes, self.update_attribute_selector)

        self.attributes = [Attribute()]
        self.attribute_selector = RadioButtonGroup(labels=['Attribute #1'], active=0)

    def update_attributes(self, attr, old, new):
        """
        on_change event for changes in the num_attributes Spinner
        """

        for i in range(new):
            if i + 1 > len(self.attributes):
                self.attributes.append(Attribute())
            self.attributes[i].name.on_change('value', self.update_attribute_selector)

    def update_attribute_selector(self, attr, old, new):
        attributes = self.attributes[:self.num_attributes.value]

        self.attribute_selector.labels = [attr.name.value for attr in attributes]
        self.attribute_selector.active = 0
Example #4
0
class Coins():
    """Coin tossing simulator class."""

    # %%
    def __init__(self):

        title = Div(text="""<h1>Coin tossing simulation</h1>"""
                    """<p>Simulates the effects of tossing a coin """
                    """many times. Heads are scored 1 and tails 0. """
                    """The chart shows the cumlative mean, which """
                    """should go to 0.5 in 'the long run'. To use """
                    """the software, select the number of coin """
                    """tosses or press go to generate a fresh """
                    """data set.</p>""",
                    sizing_mode="""stretch_width""")

        self.tosses = 2000

        self.toss_count = Spinner(step=1,
                                  value=self.tosses,
                                  title="""Number of tosses""",
                                  sizing_mode="""stretch_width""")
        self.go = Button(label="Button",
                         button_type="""success""",
                         sizing_mode="""stretch_width""")

        chart = Figure(title='Cumulative mean of coin tosses',
                       x_axis_label='Number of tosses',
                       y_axis_label='Cumulative mean')
        self.cds = ColumnDataSource()
        self.make_means()
        chart.line(source=self.cds,
                   x='tosses',
                   y='means',
                   line_color='red',
                   line_width=4,
                   line_alpha=0.5)

        chart.title.text_font_size = '20px'
        chart.xaxis.axis_label_text_font_size = '15px'
        chart.xaxis.major_label_text_font_size = '15px'
        chart.yaxis.axis_label_text_font_size = '15px'
        chart.yaxis.major_label_text_font_size = '15px'

        title_bar = column(children=[title], sizing_mode="stretch_both")
        widget_bar = column(children=[self.toss_count, self.go],
                            sizing_mode='fixed',
                            width=250,
                            height=500)
        chart_row = row(children=[widget_bar, chart],
                        sizing_mode='stretch_both')
        self.layout = column(children=[title_bar, chart_row],
                             sizing_mode='stretch_both')

    # %%
    def setup(self):
        """Setup the callbacks"""
        self.toss_count.on_change('value', self.callback_toss_count)
        self.go.on_change('clicks', self.callback_go)

    # %%
    def callback_toss_count(self, attrname, old, new):
        """Callback method for mean count"""

        self.tosses = self.toss_count.value
        self.make_means()

    # %%
    def callback_go(self, attrname, old, new):
        """Callback method for mean count"""
        self.make_means()

    # %%
    def make_means(self):
        """Create thje means be generating a random number serioes."""
        heads = np.random.randint(2, size=self.toss_count.value)
        means = np.cumsum(heads) / np.arange(1, self.toss_count.value + 1)

        self.cds.data = {'tosses': range(1, self.tosses + 1), 'means': means}
Example #5
0
# Slider to select frame.
idx_selection_slider = Slider(start=model.idxs[0],
                              end=model.idxs[-1],
                              step=1.0,
                              value=model.idxs[0],
                              title="Select frame id")
idx_selection_slider.on_change('value', idx_slider_update)

# Spinner to select frame.
idx_selection_spinner = Spinner(low=model.idxs[0],
                                high=model.idxs[-1],
                                step=1.0,
                                value=model.idxs[0],
                                title="Select frame id")
idx_selection_spinner.on_change('value', idx_spinner_update)

# Selection for column to yield symbol color.
color_column_selection = Select(options=model.frames[int(
    idx_selection_spinner.value)].columns.to_list(),
                                title="Select symbol color from column",
                                value="Volume (µm^3)")
color_column_selection.on_change('value', color_update)

# Selection for column to yield symbol size.
size_column_selection = Select(options=model.frames[int(
    idx_selection_spinner.value)].columns.to_list(),
                               title="Select symbol size from column",
                               value="Volume (µm^3)")
size_column_selection.on_change('value', size_update)
Example #6
0
def get_a_callback(attrs, old, new):
    global U

    a = int(new)
    U = PolyaUrn(a, U._b, U._n)


def get_b_callback(attrs, old, new):
    global U

    b = int(new)
    U = PolyaUrn(U._a, b, U._n)


a_field.on_change("value", get_a_callback)
b_field.on_change("value", get_b_callback)
stop_button.on_click(stop_callback)
start_button.on_click(start_callback)

#B = row(start_button, stop_button,name='B')
T = row(a_field, b_field, name="T")
S = column([DensityPanel, WalkPanel], name="S")
E = Div(text=explanation_text, name="E")
E2 = layout([Div(text=explanation_text_2), [start_button, stop_button]],
            name="E2")

doc.add_root(E)
doc.add_root(E2)
doc.add_root(S)
doc.add_root(T)
Example #7
0
class PriceModel:
    """
    fundamentally, this class contains a list of PriceGroup objects and related widgets and methods
    """

    def __init__(self):
        self.num_prod_groups = Spinner(title="Number of product groups", low=0, high=100, step=1, value=0, width=150)
        self.num_prod_groups.on_change('value', self.update_num_prod_groups, self.update_price_group_selector)
        self.prod_groups = [PriceGroup(name='Product group #1', pg_type='Product')]

        self.num_customer_groups = Spinner(title="Number of customer groups", low=0, high=100, step=1, value=0,
                                           width=150)
        self.num_customer_groups.on_change('value', self.update_num_cust_groups, self.update_price_group_selector)
        self.cust_groups = []

        # initialize with the number of product groups plus the number of customer groups
        self.price_groups = self.prod_groups + self.cust_groups

        self.num_price_groups = 1

        self.price_group_selector = RadioButtonGroup(labels=['Prod group #1'], active=0)

    def update_num_price_groups(self):
        self.num_price_groups = self.num_prod_groups.value + self.num_customer_groups.value

    def update_price_group_selector(self, attr, old, new):

        prod_groups = self.prod_groups[:self.num_prod_groups.value]
        cust_groups = self.cust_groups[:self.num_customer_groups.value]
        price_groups = prod_groups + cust_groups

        self.price_group_selector.labels = [_.name.value for _ in price_groups]
        self.price_group_selector.active = 0

    def update_num_prod_groups(self, attr, old, new):
        """
        on_change event for changes in the num_prod_groups spinner

        :param attr:
        :param old:
        :param new:
        :return:
        """
        self.update_num_price_groups()

        for i in range(new):
            if i + 1 > len(self.prod_groups):
                self.prod_groups.append(PriceGroup(name='Product group #{}'.format(i + 1), pg_type='Product'))
            self.prod_groups[i].name.on_change('value', self.update_price_group_selector)

        self.price_groups = self.prod_groups + self.cust_groups

    def update_num_cust_groups(self, attr, old, new):
        """
        on_change event for changes in the num_prod_groups spinner

        :param attr:
        :param old:
        :param new:
        :return:
        """
        self.update_num_price_groups()

        for i in range(new):
            if i + 1 > len(self.cust_groups):
                self.cust_groups.append(PriceGroup(name='Customer Group #{}'.format(i + 1), pg_type='Customer'))
            self.cust_groups[i].name.on_change('value', self.update_price_group_selector)
        self.price_groups = self.prod_groups + self.cust_groups