def modify_doc(doc):
     source = ColumnDataSource(dict(x=[1, 2], y=[1, 1], val=["a", "b"]))
     plot = Plot(plot_height=400, plot_width=400, x_range=Range1d(0, 1), y_range=Range1d(0, 1), min_border=0)
     plot.add_glyph(source, Circle(x='x', y='y', size=20))
     plot.add_tools(CustomAction(callback=CustomJS(args=dict(s=source), code=RECORD("data", "s.data"))))
     group = RadioButtonGroup(labels=LABELS, css_classes=["foo"])
     def cb(active):
         source.data['val'] = [active, "b"]
     group.on_click(cb)
     doc.add_root(column(group, plot))
    def test_js_on_change_executes(self, bokeh_model_page):
        group = RadioButtonGroup(labels=LABELS, css_classes=["foo"])
        group.js_on_click(CustomJS(code=RECORD("active", "cb_obj.active")))

        page = bokeh_model_page(group)

        el = page.driver.find_element_by_css_selector('.foo .bk-btn:nth-child(3)')
        el.click()

        results = page.results
        assert results['active'] == 2

        el = page.driver.find_element_by_css_selector('.foo .bk-btn:nth-child(1)')
        el.click()

        results = page.results
        assert results['active'] == 0

        assert page.has_no_console_errors()
Beispiel #3
0
def home():
    #Open file and create sources
    dictionary = open_file_into_dictionary('SampleCSV2.csv')
    keys = list(key.title() for key in dictionary.keys())
    values = [value for value in dictionary.values()]
    xy_source = ColumnDataSource(data=dict(xs=[values[0]], ys=[values[1]], labels = [keys[1]],
    colors = ['red', 'green', 'blue', 'purple', 'brown', 'aqua']))
    variables_source = ColumnDataSource(data = dict(keys = keys, values = values))




    #Create general plot
    plot = figure(plot_width=800, plot_height=600, toolbar_location = 'left')
    plot.title.text_font= 'helvetica'
    plot.title.text_font_size = '24pt'
    plot.title.align = 'center'
    plot.title.text_font_style = 'normal'
    plot.multi_line(xs = 'xs', ys = 'ys', legend = 'labels', line_color = 'colors', source = xy_source)


    #Define callbacks
    x_axis_callback = CustomJS(args=dict(xy_source = xy_source, variables_source = variables_source,
     axis = plot.xaxis[0]), code="""
        var xy_data = xy_source.data;
        var variables_data = variables_source.data;
        var index = cb_obj.active;
        var values = variables_data.values;

        var y_length = xy_data['ys'].length;
        if (y_length == 0){
            y_length = 1}
        var new_list = [];
        for (i = 0; i < y_length; i++) {
        new_list.push(values[index])}
        xy_data['xs'] = new_list;

        xy_source.change.emit();

        var keys = variables_data.keys;
        var label = keys[index];
        axis.axis_label = label;
    """)

    y_axis_callback = CustomJS(args=dict(xy_source = xy_source, variables_source = variables_source,
     axis = plot.yaxis[0]), code="""

        var xy_data = xy_source.data;
        var variables_data = variables_source.data;
        var index_list = cb_obj.active;

        var values = variables_data.values;
        var index_length = index_list.length;
        var keys = variables_data.keys;

        var new_ys = [];
        var new_labels = [];
        for (i = 0; i < index_length; i++) {
            new_ys.push(values[index_list[i]]);
            new_labels.push(keys[index_list[i]])}
        xy_data['labels'] = new_labels;
        xy_data['ys'] = new_ys;

        if (index_length > 0){
            var x_variable = xy_data['xs'][0];
            var new_x = [];
            for (i = 0; i < index_length; i++) {
                new_x.push(x_variable)}
            xy_data['xs'] = new_x;}

        xy_source.change.emit();

        var y_axis_name = keys[[index_list[0]]];
        for (i = 1; i < index_length; i++) {
            y_axis_name += ", " + keys[[index_list[i]]];}
        axis.axis_label = y_axis_name;
    """)

    title_callback = CustomJS(args= dict(title = plot.title), code="""
        var title_text = cb_obj.value;
        title.text = title_text;

    """)
    x_name_callback = CustomJS(args=dict(axis = plot.xaxis[0]), code="""
        var label_text = cb_obj.value;
        axis.axis_label = label_text;
         """)

    y_name_callback = CustomJS(args=dict(axis = plot.yaxis[0]), code="""
        var label_text = cb_obj.value;
        axis.axis_label = label_text;
         """)


    #Create toolbox
    label_x = Div(text="""X-Axis""", width=200)
    x_axis = RadioButtonGroup(labels=keys, active=0, callback = x_axis_callback)
    label_y = Div(text="""Y-Axis""", width=200)
    y_axis = CheckboxButtonGroup(labels=keys, active=[1], callback = y_axis_callback)
    label_axes = Div(text="""<br />Modify Labels""", width=200)

    title_name = TextInput(title="Title", value='Default Title')
    plot.title.text = title_name.value
    title_name.js_on_change('value', title_callback)

    x_name = TextInput(title="X-Axis", value='Default X Label')
    plot.xaxis.axis_label = keys[0]
    x_name.js_on_change('value', x_name_callback)
    y_name = TextInput(title="Y-Axis", value='Default Y Label')
    plot.yaxis.axis_label = keys[1]
    y_name.js_on_change('value', y_name_callback)

    toolbox = widgetbox(label_x, x_axis, label_y, y_axis, label_axes, title_name, x_name, y_name)





    #Integrate with html
    parts = dict(toolbox = toolbox, plot = plot)
    script, div = components(parts, INLINE)
    return render_template('plotpage.html',
                           script=script,
                           toolbox_div=div['toolbox'],
                           plot_div=div['plot'],
                           js_resources=INLINE.render_js(),
                           css_resources=INLINE.render_css())
from bokeh.io import show
from bokeh.models import CustomJS, RadioButtonGroup

LABELS = ["Option 1", "Option 2", "Option 3"]

radio_button_group = RadioButtonGroup(labels=LABELS, active=0)
radio_button_group.js_on_event(
    "button_click",
    CustomJS(args=dict(btn=radio_button_group),
             code="""
    console.log('radio_button_group: active=' + radio_button_group.active, this.toString())
"""))

show(radio_button_group)
Beispiel #5
0
 def button_group(level):
     choice = RadioButtonGroup(labels=['None'],
                               visible=False,
                               active=None)
     set_callback(choice, level)
     return choice
Beispiel #6
0
    def __init__(self, n_nodes):
        self.i = 0
        kps = [crypto_sign_keypair() for _ in range(n_nodes)]
        stake = {kp[0]: 1 for kp in kps}

        network = {}
        self.nodes = [Node(kp, network, n_nodes, stake) for kp in kps]
        for n in self.nodes:
            network[n.pk] = n.ask_sync
        self.ids = {kp[0]: i for i, kp in enumerate(kps)}

        self.main_its = [n.main() for n in self.nodes]
        for m in self.main_its:
            next(m)

        def toggle():
            if play.label == '► Play':
                play.label = '❚❚ Pause'
                curdoc().add_periodic_callback(self.animate, 50)
            else:
                play.label = '► Play'
                curdoc().remove_periodic_callback(self.animate)

        play = Button(label='► Play', width=60)
        play.on_click(toggle)

        def sel_node(new):
            self.active = new
            node = self.nodes[new]
            self.tbd = {}
            self.tr_src.data, self.links_src.data = self.extract_data(
                node, bfs((node.head, ), lambda u: node.hg[u].p), 0)
            for u, j in tuple(self.tbd.items()):
                self.tr_src.data['line_alpha'][j] = 1 if node.famous.get(
                    u) else 0
                if u in node.idx:
                    self.tr_src.data['round_color'][j] = idx_color(node.idx[u])
                self.tr_src.data['idx'][j] = node.idx.get(u)
                if u in node.idx and u in node.famous:
                    del self.tbd[u]
                    print('updated')
            self.tr_src.trigger('data', None, self.tr_src.data)

        selector = RadioButtonGroup(
            labels=['Node %i' % i for i in range(n_nodes)],
            active=0,
            name='Node to inspect')
        selector.on_click(sel_node)

        plot = figure(
            plot_height=700,
            plot_width=900,
            y_range=(0, 30),
            tools=[
                PanTool(dimensions=['height']),
                HoverTool(tooltips=[('round', '@round'), (
                    'hash',
                    '@hash'), ('timestamp',
                               '@time'), ('payload',
                                          '@payload'), ('number', '@idx')])
            ])
        plot.xgrid.grid_line_color = None
        plot.xaxis.minor_tick_line_color = None
        plot.ygrid.grid_line_color = None
        plot.yaxis.minor_tick_line_color = None

        self.links_src = ColumnDataSource(data={
            'x0': [],
            'y0': [],
            'x1': [],
            'y1': [],
            'width': []
        })
        #self.links_rend = plot.add_layout(
        #        Arrow(end=NormalHead(fill_color='black'), x_start='x0', y_start='y0', x_end='x1',
        #        y_end='y1', source=self.links_src))
        self.links_rend = plot.segment(color='#777777',
                                       x0='x0',
                                       y0='y0',
                                       x1='x1',
                                       y1='y1',
                                       source=self.links_src,
                                       line_width='width')

        self.tr_src = ColumnDataSource(
            data={
                'x': [],
                'y': [],
                'round_color': [],
                'idx': [],
                'line_alpha': [],
                'round': [],
                'hash': [],
                'payload': [],
                'time': []
            })

        self.tr_rend = plot.circle(x='x',
                                   y='y',
                                   size=20,
                                   color='round_color',
                                   line_alpha='line_alpha',
                                   source=self.tr_src,
                                   line_width=5)

        sel_node(0)
        curdoc().add_root(
            row([widgetbox(play, selector, width=300), plot],
                sizing_mode='fixed'))
Beispiel #7
0
def wohngeld(plot_dict, data):
    def make_dataset(sel_year, hh_size, wg_dict):
        dataset = wg_dict[sel_year][hh_size]

        heatmap_source = pd.DataFrame(dataset.stack(),
                                      columns=["Wohngeld"]).reset_index()
        heatmap_source.columns = ["Miete", "Einkommen", "Wohngeld"]

        return ColumnDataSource(heatmap_source)

    def update_plot(attr, old, new):
        sel_year = [1992, 2001, 2009, 2016, 2020, 2021][year_selection.active]
        hh_size = hh_size_selection.value
        new_src = make_dataset(sel_year, hh_size, wg_dict)

        src.data.update(new_src.data)

    def setup_plot(src):
        """
        Create the heatmap plot.

        src: ColumnDataSource
        """

        # Prepare a color pallete and color mapper
        mapper = LinearColorMapper(
            # force 0 to be mapped with white color
            palette=tuple(
                itertools.chain(["#FFFFFF"], tuple(reversed(Viridis256[1:])))),
            low=0,
            high=1000,
        )

        # Actual figure setup
        p = figure(
            plot_width=800,
            plot_height=400,
            x_range=(src.data["Miete"].min(), src.data["Miete"].max()),
            y_range=(src.data["Einkommen"].min(), src.data["Einkommen"].max()),
            tools="hover",
            tooltips="Housing Benefit: @Wohngeld{0.0f}€",
        )

        p.rect(
            x="Miete",
            y="Einkommen",
            width=25,
            height=src.data["Einkommen"][1] - src.data["Einkommen"][0],
            source=src,
            line_color=transform("Wohngeld", mapper),
            fill_color=transform("Wohngeld", mapper),
        )

        color_bar = ColorBar(
            color_mapper=mapper,
            location=(0, 0),
            ticker=BasicTicker(desired_num_ticks=20),
            formatter=NumeralTickFormatter(format="0€"),
            label_standoff=12,
        )
        p.add_layout(color_bar, "right")

        plot = plotstyle(p, plot_dict)

        return plot

    wg_dict = data

    year_selection = RadioButtonGroup(
        labels=[str(i) for i in [1992, 2001, 2009, 2016, 2020, 2021]],
        active=5)
    year_selection.on_change("active", update_plot)

    hh_size_selection = Slider(start=1,
                               end=12,
                               value=4,
                               step=1,
                               title="Household Size")
    hh_size_selection.on_change("value", update_plot)

    src = make_dataset(2021, 4, wg_dict)

    p = setup_plot(src)

    description = Div(
        text=plot_dict["description"],
        width=1000,
    )

    year_label = Div(text="Year")

    layout = column(description, year_label, year_selection, hh_size_selection,
                    p)

    tab = Panel(child=layout, title="Housing benefits")

    return tab
Beispiel #8
0
def change_range(attrname, old, new):
    low, high = rangeslider.value

    for i, lead in enumerate(StandardHeader):
        leads[i].x_range.start = low * 100
        leads[i].x_range.end = (high - rs_width) * 100


# Set widgets
rangeslider = RangeSlider(start=0,
                          end=10000,
                          step=1,
                          value=(0, rs_width + 30),
                          title="X range")
file_selector = Select(value=None, options=nix(None, files))
waveselector = RadioButtonGroup(labels=["P wave", "QRS wave", "T wave"],
                                active=0)
textboxnew = PreText(text="New points:      \t[]")
retrievebutton = Button(label='Retrieve Segmentation')
storebutton = Button(label='Store Segmentation')
writebutton = Button(label='Write to File')

# Set callbacks
file_selector.on_change('value', file_change)
source.selected.on_change('indices', selection_change)
retrievebutton.on_click(retrieve_segmentation)
storebutton.on_click(save_segmentation)
writebutton.on_click(write_segmentation)
rangeslider.on_change('value', change_range)
waveselector.on_change('active', wave_change)

# set up layout
data_counties = pd.concat([data_counties, raw_df], axis=1)

# getting quantile values for categorising counties
top_qtl = raw_df.quantile(0.66)
middle_qtl = raw_df.quantile(0.33)

session = Session()
document = Document()
session.use_doc('python_project')
session.load_document(document)

source = ColumnDataSource(data=dict(x=[], y=[], c=[], i=[]))
inf_source = ColumnDataSource(data=dict(facs=[]))

# radio buttons intialization
radio = RadioButtonGroup(labels=['Cancer Deaths', 'Heart Disease Deaths', 'Respiratory Disease Deaths', 'Diabetes Deaths'], active=0)
radio.on_click(update)

# calling update function to intialize the plot 
update(0)

        

if __name__ == '__main__':
    link = session.object_link(document.context)
    print("Please visit %s to see the plots" % link)
    view(link)
    session.poll_document(document)
    

    def add_controllers(self):
        titles = {'npeople': 'Number of people',
                  'sensorinterval': 'Interval',
                  'sensortpr': 'True positive rate',
                  'sensorexfp': 'Expected num of FP',
                  'sensorrange': 'Sensor range',
                  'sensorsnum': 'Fleet size',
                  'sensorspeed': 'Fleet speed'}
        idx0 = 1

        stacked = []

        #Create a controller for the scaling
        def on_scaling_changed(_, _old, _new):
            self.scaling = _new
            self.update_plot()

        self.scalingwidget = Select(title='Scaling', value='linear',
                              options=['linear', 'log'])
        self.scalingwidget.on_change('value', on_scaling_changed)
        stacked.append(self.scalingwidget)
        # Create a controller for each param
        for k in titles.keys():
            def on_radio_changed(attr, old, new, kk):
                if self.blocked: return
                newvalue = self.config[kk][new-1] if new != FIXEDIDX else -1
                print('Changed ' + str(kk) + ' to ' + str(newvalue))
                self.currparams[kk] = newvalue

                if new == FIXEDIDX:
                    self.blocked = True
                    for param in self.contr.keys():
                        if param == kk or self.contr[param].active != FIXEDIDX:
                            continue

                        self.contr[param].active = 1
                        self.currparams[param] = self.config[param][0]
                    self.var = kk

                varyingparam = False
                for kkk, vvv in self.currparams.items():
                    if vvv == -1:
                        varyingparam = True
                        break
                if not varyingparam: self.var = None
                self.update_plot()
                if self.blocked: self.blocked = False

            my_radio_changed = partial(on_radio_changed, kk=k)
            params = ['varying'] + list(map(str, self.config[k]))

            buttonisactive = FIXEDIDX if self.var == k else idx0
            self.contr[k] = RadioButtonGroup(labels=params, active=buttonisactive)

            self.contr[k].on_change('active', my_radio_changed)
            self.currparams[k] = params[idx0]

            r = Row(widgetbox(Div(text='{}:'.format(titles[k]))),
                    self.contr[k])
            stacked.append(r)


        self.currparams[self.var] = -1
        adjwidget = Column(*stacked)
        self.guirow.children[0] = adjwidget
    source1.change.emit(); // update the arrays
    source2.change.emit(); // update phis
""")

# define widgets
modes_slider = Slider(
    start=2,
    end=maxMode,
    value=100,
    step=1,
    title="Number of Modes, $N$",
    callback=callback1,
    #                     callback_policy="mouseup"
)
lock_toggle = RadioButtonGroup(labels=["Modelocking", "No Modelocking"],
                               active=0,
                               callback=callback2)

callback1.args["modes"] = modes_slider
callback1.args["locking"] = lock_toggle
callback2.args["modes"] = modes_slider
callback2.args["locking"] = lock_toggle

maxI = np.amax(y)
meanI = np.mean(y)
print(meanI)

div1 = Div(
    text="""
    <div> Peak intensity of the current plot: </div> <div id="maxInfo" class="valueDiv"></div>
"""
Beispiel #12
0
def show_cells_on_stack(data,
                        stack,
                        X='X',
                        Y='Y',
                        channelNames=None,
                        group='group',
                        hue='hue',
                        palette='Spectral11',
                        default=0,
                        alpha=.5,
                        plot_width=500,
                        plot_height=500,
                        vmin=0,
                        vmax=3,
                        znorm=True):
    '''
    stack:  np. array of shape (nchannels,height,width)
    '''
    from bokeh.models import CheckboxGroup, RadioButtonGroup, Legend, LegendItem
    if not channelNames:
        channelNames = ['Channel ' + str(i) for i in range(len(stack))]
        print(channelNames)

    s1 = ColumnDataSource(data=data)
    p1 = figure(plot_width=plot_width,
                plot_height=plot_height,
                tools="pan,wheel_zoom,reset")

    channels = {}
    for i, channel in enumerate(channelNames):
        img = stack[i]
        if znorm:
            img = (img - img.mean()) / img.std()

        channels[i] = p1.image(image=[img],
                               x=[0],
                               y=[0],
                               dw=[plot_width],
                               dh=[plot_height],
                               color_mapper=LogColorMapper(palette=palette,
                                                           low=vmin,
                                                           high=vmax),
                               global_alpha=alpha,
                               visible=(i == default))

    plots = {}
    #scaled_coordinates to fit in stack_dw,stack_dh
    dh_ratio = plot_height / img.shape[0]
    dw_ratio = plot_width / img.shape[1]

    data['warped_X'] = data[X] * dw_ratio
    data['warped_Y'] = data[Y] * dh_ratio

    groups = list(data[group].unique())
    for g_id in groups:
        s2 = ColumnDataSource(data=data[data[group] == g_id])
        scatter = p1.circle('warped_X',
                            'warped_Y',
                            source=s2,
                            alpha=1,
                            color='hue')  #,legend_label = str(g_id))
        scatter.visible = False
        plots[g_id] = scatter

    select_celltype = CheckboxGroup(labels=groups, active=[], width=100)
    select_channel = RadioButtonGroup(labels=channelNames,
                                      active=default,
                                      width=100,
                                      orientation='horizontal')

    select_celltype.callback = CustomJS(args={
        'plots': plots,
        'groups': groups,
        'msel': select_celltype,
        'fig': p1
    },
                                        code="""
            //fig.title.text = 'new Title'
            for (var i =0; i<groups.length;i++){
               plots[groups[i]].visible = msel.active.indexOf(i)>-1
            }
               """)

    select_channel.callback = CustomJS(args={
        'channels': channels,
        'sel': select_channel,
        'fig': p1
    },
                                       code="""

        //fig.title.text = Object.keys(channels).length.toString()
        for (var i =0; i<Object.keys(channels).length;i++){
            channels[i].visible = false
        }
        var val = sel.active
        channels[val].visible = true


        /**
        if (val==0){
            fig.title.text = 'confirm upper'
            chan1.visible = true
            chan2.visible = false
            //bg_channel = [z[0]]

        }else if (val==1){
            fig.title.text = 'confirm lower'
            chan1.visible = false
            chan2.visible = true
            //bg_channel = [z[1]]

        }
        **/



    """)

    layout = column(p1, select_celltype, select_channel)
    show(layout)
Beispiel #13
0
                       step=1,
                       title="% Female")
slider62.on_change('value', update)

# 7) Etnicity
slider7 = RangeSlider(start=0,
                      end=100,
                      value=(0, 100),
                      step=1,
                      title="Diversity Index")
slider7.on_change('value', update)

# 8)
#Rent
div81 = Div(text="<b> Housing Rent per Sqft </b>")
radio_button81 = RadioButtonGroup(
    labels=["No preference", "< 20 $", "20-30$", ">30 $"], active=0)
radio_button81.on_change('active', update)

#Crime
div82 = Div(text="<b>Felonies per 100.000 </b>")
radio_button82 = RadioButtonGroup(
    labels=["No preference", "< 30", "30-60", "> 60"], active=0)
radio_button82.on_change('active', update)

table, m, div0 = create_plot()

#Combine all controls to get in column

col1 = row(table, m, height=200, width=1180)
col2 = column(div1, select1, slider21, slider22, slider3, slider4, width=210)
col3 = column(div5,
Beispiel #14
0
click_button = Button(label="Button still has click event", button_type="success")

disabled_button = Button(label="Button (disabled) - still has click event", button_type="primary", disabled=True)

toggle = Toggle(label="Toggle button", button_type="success")

menu = [("Item 1", "item_1_value"), ("Item 2", "item_2_value"), None, ("Item 3", "item_3_value")]

dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=menu)
dropdown_split = Dropdown(label="Split button", button_type="danger", menu=menu, split=True)

checkbox_group = CheckboxGroup(labels=["Option 1", "Option 2", "Option 3"], active=[0, 1])
radio_group = RadioGroup(labels=["Option 1", "Option 2", "Option 3"], active=0)

checkbox_button_group = CheckboxButtonGroup(labels=["Option 1", "Option 2", "Option 3"], active=[0, 1])
radio_button_group = RadioButtonGroup(labels=["Option 1", "Option 2", "Option 3"], active=0)

text_input = TextInput(placeholder="Enter value ...")

completions = ["aaa", "aab", "aac", "baa", "caa"]
autocomplete_input = AutocompleteInput(placeholder="Enter value (auto-complete) ...", completions=completions)

select = Select(options=["Option 1", "Option 2", "Option 3"])

multi_select = MultiSelect(options=["Option %d" % (i+1) for i in range(16)], size=6)

slider = Slider(value=10, start=0, end=100, step=0.5)

range_slider = RangeSlider(value=[10, 90], start=0, end=100, step=0.5)

date_slider = DateSlider(value=date(2016, 1, 1), start=date(2015, 1, 1), end=date(2017, 12, 31))
Beispiel #15
0
def load_data():
    curdoc().clear()
	
    button_1 = Button(label="Load data")
    button_1.on_click(load_data)
    button_2 = Button(label="Advanced options")
    button_2.on_click(Advanced_options)




    curdoc().add_root(button_1)
    curdoc().add_root(button_2)
	
    
    datapath=filedialog.askopenfilename() 
    

	
    global Dsource, Csource, Isource, Ssource, data_new_masked, data3, CX2, CY2, data, YY, XX,SelectedIon, mzlabs
    
    
    mz=pd.read_csv(datapath,sep='\t',skiprows=(0,1,2),header=None, nrows=1)
    
	
    mz=mz.drop(columns=[0,1,2])
    #lastmz=mz.columns[-1]
    #mz=mz.drop(columns=[lastmz-1,lastmz])

        
    data = pd.read_csv(datapath,sep='\t',skiprows=(0,1,2,3),header=None)
    Xpixels=data[1].tolist()
    Ypixels=data[2].tolist()

    last=data.columns[-1]
    data = data.drop(data.columns[[0, 1, 2,last-1,last]], axis=1)

    ScanNum=data.index.tolist()

    TotalScan=len(ScanNum)

    
    mzlabs=mz.loc[0].values.tolist()
    
    data.columns=mzlabs

    

    data = data.reindex(sorted(data.columns), axis=1)
    mzlabs.sort()

    peakNum=len(mzlabs)
    


# Work out pixel dimensions- need to do try/ catch here 

    a=Xpixels[1]
    Ypix=Xpixels.count(a)

    Xpix=np.round(TotalScan/Ypix)

    print(Ypix)
    print(Xpix)

# Make sure Ypix * Xpix = total pix 

# Do sum normalisation.. this will have multiple options 

# This is not the real sum normalisation 
    
    

  #  data_new = data.div(data.sum(axis=1), axis=0)

# This is a crude method for clearing background pixels based on lipid vs non-lipid 

    #low1=int(peakNum/2)
    #low2=int(peakNum/2)
    #high2=int(peakNum)
    
    low1=min(range(len(mzlabs)), key=lambda x:abs(mzlabs[x]-50))
    high1=min(range(len(mzlabs)), key=lambda x:abs(mzlabs[x]-200))
    low2=min(range(len(mzlabs)), key=lambda x:abs(mzlabs[x]-750))
    high2=min(range(len(mzlabs)), key=lambda x:abs(mzlabs[x]-900))
    
    
    D1=data.iloc[:,low1:high1]
    D2=data.iloc[:,low2:high2]
    D1s = D1.sum(axis=1)
    D1s=D1s+1
    D2s = D2.sum(axis=1)

    Ratio=D2s/D1s

    Ratio.tolist()
    
    del D1,D1s,D2,D2s

# This may be possible to do with only one copy of the data 


    data2=data
    data2.loc[Ratio<2,:]=0

    data3=data2.loc[~(data2==0).all(axis=1)]
    del data2
    data_new_masked = data3.div(data3.sum(axis=1), axis=0)
    data_new_masked=data_new_masked.fillna(0)

# Do PCA data reduction 
    
   



#Data_reduced=PCA(n_components=10).fit_transform(data_new)
    Data_reduced=PCA(n_components=10).fit_transform(data_new_masked)

# Perform the UMAP - these paramaters will be adjustable


    reducer = umap.UMAP(n_neighbors=10,min_dist=0.1,n_components=2,metric='euclidean')

    embedding = reducer.fit_transform(Data_reduced)

# This can be replaced using the Xpix Ypix from above 


    YY=int(Ypix)
    XX=int(Xpix)

    CX=[]
    for y in range(YY):
        for x in range(XX):
            CX.append(x)
        
    CY=[]

    for y in range(YY):
        for x in range(XX):
            CY.append(y)
		
		
		
    idx=data3.index

    CX2 = [CX[i] for i in idx]
    CY2 = [CY[i] for i in idx]
	
#    CX2=reverse(CX2)
#    CY2=reverse(CY2)

    #CX2=CX2[::-1]
    #CY2=CY2[::-1]
	
	
# This defines the UMAP output as columns for the plotting tools 

    x2=embedding[:, 0]
    y2=embedding[:, 1]    


    x3=x2-np.min(x2)
    y3=y2-np.min(y2)

    scannum= np.arange(0,TotalScan).tolist()
    spectra=scannum

    spectra2 = [spectra[i] for i in idx]

    ColX=(x3/np.max(x3))*255
    ColY=(y3/np.max(y3))*255
    CV1 = ["#%02x%02x%02x" % (int(r), int(g), 0) for r, g in zip(ColX, ColY)]
    CV2 = ["#%02x%02x%02x" % (0, int(r), int(g)) for r, g in zip(ColX, ColY)]
    CV3 = ["#%02x%02x%02x" % (int(r), 0, int(g)) for r, g in zip(ColX, ColY)]


# Create the data sources required 

    Mean1=np.mean(data3) #.iloc[1,:] 

    Blank=[0]*len(CX2)
    BlankMap = ["#%02x%02x%02x" % (0, 0, 0) for r in(ColX)]

    CompData=Mean1/Mean1
    Ssource = ColumnDataSource(data=dict(x=mzlabs,y=Mean1))
    Dsource = ColumnDataSource(data=dict(x=x2, y=y2, cordsX=CX2,cordsY=CY2,CV=CV1,spectra=spectra2))
    Csource = ColumnDataSource(data=dict(x=mzlabs,Region1=Mean1,Region2=Mean1,y=CompData))
    Isource = ColumnDataSource(data=dict(cordsX=CX2,cordsY=CY2,Region1=Blank,Region2=Blank,Map=Blank))



# Set up the plot region (need to define min and max for right plot) 
    TOOLS="lasso_select, box_select,pan,wheel_zoom,box_zoom,reset"


    Right=figure(title="UMAP output",plot_width=500,plot_height=500,x_range=[-15,15], y_range=[-15,15],tools=TOOLS)

    Left=figure(plot_width=500,plot_height=500,title=None,x_range=[0,XX], y_range=[0,YY],tools=TOOLS)
    Left.axis.visible = False
    Results=figure(plot_width=500,plot_height=400,title=None,x_range=[0,1200],tools="pan,wheel_zoom,box_zoom,reset,tap",x_axis_label='m/z',y_axis_label='log2 fold change')
    Spectrum=figure(plot_width=500,plot_height=400,title=None,x_range=[0,1200],x_axis_label='m/z',y_axis_label='mean intensity')
    SelectedIon=figure(plot_width=300,plot_height=300,title="Selected ion image",title_location = "below",x_range=[0,XX], y_range=[0,YY])
    SelectedIon.axis.visible = False
    Regions=figure(plot_width=200,plot_height=200,title=None,x_range=[0,Xpix], y_range=[0,Ypix],align="center")
    Regions.axis.visible = False


    Results.add_tools(HoverTool(
        tooltips = [
            ("m/z", "@x"),
            ("fold change", "@y"),
        ],
        mode='mouse',
        point_policy='snap_to_data'
    ))

# Populate the initial plots 
    r=Right.scatter(x='x',y='y',fill_color='CV',line_color=None,source=Dsource,radius=0.1)
    Left.square(x='cordsX',y='cordsY',fill_color='CV',line_color=None,alpha=1,size=5, source=Dsource)
#Spectrum.line(x='x',y='y',source=Ssource)
#Results.line(x='x',y='y',source=Csource)
    Spectrum.vbar(x='x',top='y',source=Ssource,width=0.5)
    Results.vbar(x='x',top='y',source=Csource,width=0.5)
    Regions.square(x='cordsX',y='cordsY',fill_color='Map',line_color=None,alpha=1,size=5, source=Isource)

    callback = CustomJS(args=dict(renderer=r), code="""
        renderer.glyph.radius = cb_obj.value;
    """)


    slider1 = Slider(start=0.01, end=1, step=0.01, value=0.1,title='circle size')
    slider1.js_on_change('value', callback)

    #text = TextInput(title="title", value='Insert experiment name')

    button_group = RadioButtonGroup(labels=["Red Green", "Red Blue", "Blue Green"], active=0)
    select = Select(title="Option:", value="foo", options=["Sum normalisation", "No normalisation", "Mean normalisation", "Median normalisation"])
    button_1 = Button(label="Load data")
    button_2 = Button(label="Reset data")
    button_3 = Button(label="Select region 1")
    button_4 = Button(label="Select region 2")
    button_5 = Button(label="Compare")
    button_6 = Button(label="Output report")
	# These are the list of actions possible 

    #text.on_change('value', update_title) 
    button_1.on_click(load_data)
    button_3.on_click(Region_1)
    button_4.on_click(Region_2)
    button_5.on_click(Compare_data)
    button_6.on_click(Output)
    Dsource.selected.on_change('indices', update_data)
#taptool = Results.select(type=TapTool)
    Csource.selected.on_change('indices',create_ion_map)


    
    p = gridplot([[Left,Right,column(slider1,select, button_3,button_4,Regions,button_5,button_6)],[Spectrum,Results,SelectedIon]])
    

    curdoc().add_root(p)
Beispiel #16
0
    def __init__(self, n_nodes):
        self.i = 0
        kps = [crypto_sign_keypair() for _ in range(n_nodes)]
        stake = {kp[0]: 1 for kp in kps}

        network = {}
        self.nodes = [Node(kp, network, n_nodes, stake) for kp in kps]
        for n in self.nodes:
            network[n.pk] = n.ask_sync
        self.ids = {kp[0]: i for i, kp in enumerate(kps)}

        self.main_its = [n.main() for n in self.nodes]
        for m in self.main_its:
            next(m)

        def toggle():
            if play.label == '► Play':
                play.label = '❚❚ Pause'
                curdoc().add_periodic_callback(self.animate, 50)
            else:
                play.label = '► Play'
                curdoc().remove_periodic_callback(self.animate)

        play = Button(label='► Play', width=60)
        play.on_click(toggle)

        def sel_node(new):
            self.active = new
            node = self.nodes[new]
            self.tbd = {}
            self.tr_src.data, self.links_src.data = self.extract_data(
                    node, bfs((node.head,), lambda u: node.hg[u].p), 0)
            for u, j in tuple(self.tbd.items()):
                self.tr_src.data['line_alpha'][j] = 1 if node.famous.get(u) else 0
                if u in node.idx:
                    self.tr_src.data['round_color'][j] = idx_color(node.idx[u])
                self.tr_src.data['idx'][j] = node.idx.get(u)
                if u in node.idx and u in node.famous:
                    del self.tbd[u]
                    print('updated')
            self.tr_src.trigger('data', None, self.tr_src.data)

        selector = RadioButtonGroup(
                labels=['Node %i' % i for i in range(n_nodes)], active=0,
                name='Node to inspect')
        selector.on_click(sel_node)

        plot = figure(
                plot_height=700, plot_width=900, y_range=(0, 30),
                tools=[PanTool(dimensions=['height']),
                       HoverTool(tooltips=[
                           ('round', '@round'), ('hash', '@hash'),
                           ('timestamp', '@time'), ('payload', '@payload'),
                           ('number', '@idx')])])
        plot.xgrid.grid_line_color = None
        plot.xaxis.minor_tick_line_color = None
        plot.ygrid.grid_line_color = None
        plot.yaxis.minor_tick_line_color = None

        self.links_src = ColumnDataSource(data={'x0': [], 'y0': [], 'x1': [],
                                                'y1': [], 'width': []})
        #self.links_rend = plot.add_layout(
        #        Arrow(end=NormalHead(fill_color='black'), x_start='x0', y_start='y0', x_end='x1',
        #        y_end='y1', source=self.links_src))
        self.links_rend = plot.segment(color='#777777',
                x0='x0', y0='y0', x1='x1',
                y1='y1', source=self.links_src, line_width='width')

        self.tr_src = ColumnDataSource(
                data={'x': [], 'y': [], 'round_color': [], 'idx': [],
                    'line_alpha': [], 'round': [], 'hash': [], 'payload': [],
                    'time': []})

        self.tr_rend = plot.circle(x='x', y='y', size=20, color='round_color',
                                   line_alpha='line_alpha', source=self.tr_src, line_width=5)

        sel_node(0)
        curdoc().add_root(row([widgetbox(play, selector, width=300), plot], sizing_mode='fixed'))
    def setup_widgets(self):

        # Generation of selectable items

        # Dict contains all inspectable runs (maps display strings to timestamps)
        # The dict structure allows to get the timestamp from the display string
        # in O(1)
        self.runs_dict = helper.gen_runs_selection(self.metadata)

        # Dict maps display strings to column names for the different factors
        # (var, backend, test)
        self.factors_dict = {
            "Variables": "variable",
            "Backends": "vfc_backend",
            "Tests": "test"
        }

        # Run selection

        # Contains all options strings
        runs_display = list(self.runs_dict.keys())
        # Will be used when updating plots (contains actual number)
        self.current_run = self.runs_dict[runs_display[-1]]
        # Contains the selected option string, used to update current_n_runs
        current_run_display = runs_display[-1]
        # This contains only entries matching the run
        self.run_data = self.data[self.data["timestamp"] == self.current_run]

        change_run_callback_js = "updateRunMetadata(cb_obj.value, \"\");"

        self.widgets["select_run"] = Select(name="select_run",
                                            title="Run :",
                                            value=current_run_display,
                                            options=runs_display)
        self.doc.add_root(self.widgets["select_run"])
        self.widgets["select_run"].on_change("value", self.update_run)
        self.widgets["select_run"].js_on_change(
            "value",
            CustomJS(
                code=change_run_callback_js,
                args=(dict(metadata=helper.metadata_to_dict(
                    helper.get_metadata(self.metadata, self.current_run))))))

        # Factors selection

        # "Group by" radio
        self.widgets["groupby_radio"] = RadioButtonGroup(
            name="groupby_radio",
            labels=list(self.factors_dict.keys()),
            active=0)
        self.doc.add_root(self.widgets["groupby_radio"])
        # The functions are defined inside the template to avoid writing too
        # much JS server side
        self.widgets["groupby_radio"].on_change("active", self.update_groupby)

        # "Filter by" radio
        # Get all possible factors, and remove the one selected in "Group by"
        filterby_list = list(self.factors_dict.keys())
        del filterby_list[self.widgets["groupby_radio"].active]

        self.widgets["filterby_radio"] = RadioButtonGroup(
            name="filterby_radio", labels=filterby_list, active=0)
        self.doc.add_root(self.widgets["filterby_radio"])
        # The functions are defined inside the template to avoid writing too
        # much JS server side
        self.widgets["filterby_radio"].on_change("active",
                                                 self.update_filterby)

        # Filter selector

        filterby = self.widgets["filterby_radio"].labels[
            self.widgets["filterby_radio"].active]
        filterby = self.factors_dict[filterby]

        if not self.run_data.empty:
            options = self.run_data.index\
                .get_level_values(filterby).drop_duplicates().tolist()
        else:
            options = ["None"]

        self.widgets["select_filter"] = Select(
            # We need a different name to avoid collision in the template with
            # the runs comparison's widget
            name="select_filter",
            title="Select a filter :",
            value=options[0],
            options=options)
        self.doc.add_root(self.widgets["select_filter"])
        self.widgets["select_filter"]\
            .on_change("value", self.update_filter)

        # Toggle for outliers filtering

        self.widgets["outliers_filtering_inspect"] = CheckboxGroup(
            name="outliers_filtering_inspect",
            labels=["Filter outliers"],
            active=[])
        self.doc.add_root(self.widgets["outliers_filtering_inspect"])
        self.widgets["outliers_filtering_inspect"]\
            .on_change("active", self.update_outliers_filtering)
    'Orange',
]

df = pd.DataFrame({'x': x, 'y': y, 'label': label})

source = ColumnDataSource(data=dict(x=df.x, y=df.y, label=df.label))

plot_figure = figure(title='Radio Button Group',
                     plot_height=450,
                     plot_width=600,
                     tools="save,reset",
                     toolbar_location="below")

plot_figure.scatter('x', 'y', color='label', source=source, size=10)

radio_button_group = RadioButtonGroup(labels=["Red", "Orange"])


def radiogroup_click(attr, old, new):
    active_radio = radio_button_group.active  ##Getting radio button value

    # filter the dataframe with value in radio-button
    if active_radio == 0:
        selected_df = df[df['label'] == 'Red']
    elif active_radio == 1:
        selected_df = df[df['label'] == "Orange"]

    source.data = dict(x=selected_df.x,
                       y=selected_df.y,
                       label=selected_df.label)
                 value=selected_dataset)

measure_1 = Select(title="Measure",
                   options=[
                       ('cosine_similarity', 'Cosine Similarity'),
                       ('cosine', 'Cosine Distance'),
                       ('euclidean', 'Euclidean'),
                       ('dot_product', 'Dot product'),
                       ('correlation', 'Correlation'),
                   ],
                   value='cosine_similarity')
x_axis = TextInput(title="X Axis Formula", placeholder="formula")
y_axis = TextInput(title="Y Axis Formula", placeholder="formula")
explicit_tab = Panel(child=column(measure_1, x_axis, y_axis), title="Explicit")

filtering_before_after_2 = RadioButtonGroup(
    labels=["Filter before projection", "Filter after projection"], active=0)
pca_tab = Panel(child=filtering_before_after_2, title="PCA")

measure_3 = Select(title="Measure",
                   options=[
                       ('cosine', 'Cosine Distance'),
                       ('euclidean', 'Euclidean'),
                       ('dot_product', 'Dot product'),
                       ('correlation', 'Correlation'),
                   ],
                   value='cosine')
filtering_before_after_3 = RadioButtonGroup(
    labels=["Filter before projection", "Filter after projection"], active=0)
tsne_tab = Panel(child=column(measure_3, filtering_before_after_3),
                 title="t-SNE")
def build_lockdown_tab():

    # Get data for lockdown tab
    lockdown_data = get_lockdown_data()
    lockdown_data = prep_lockdown_data(lockdown_data)

    source_gantt = ColumnDataSource(lockdown_data.dropna())
    source_points = ColumnDataSource(lockdown_data)

    # Create lockdown figure
    lockdown_fig = figure(
        y_range=FactorRange(factors=lockdown_data.Country.unique().tolist()),
        x_axis_type='datetime',
        title="Lockdown Status by Nation",
        x_range=(lockdown_data['start_date'].min() - timedelta(days=3),
                 lockdown_data['end_date'].max() + timedelta(days=3)),
        outline_line_color=None,
        width=1000,
        height=650)

    # Adding hbar glyph of lockdown dates
    gantt_plot = lockdown_fig.hbar(y="Country",
                                   left='start_date',
                                   right='end_date',
                                   height=0.4,
                                   source=source_gantt,
                                   color='color')

    # Adding start point circle glyph
    start_point = lockdown_fig.circle(x='start_date',
                                      y='Country',
                                      source=source_points,
                                      size=5,
                                      line_color='blue',
                                      fill_color='white')

    # Adding end point circle glyph
    end_point = lockdown_fig.circle(x='end_date',
                                    y='Country',
                                    source=source_points,
                                    size=5,
                                    line_color='blue',
                                    fill_color='white')

    # Formatting x-axis
    lockdown_fig.xaxis.axis_label = "Date"
    lockdown_fig.xaxis.formatter = DatetimeTickFormatter(days='%d/%m/%Y')
    lockdown_fig.xaxis.ticker = DaysTicker(days=np.arange(5, 365, 7))
    lockdown_fig.xaxis.major_label_orientation = math.pi / 6

    # Formatting y-axis
    lockdown_fig.yaxis.axis_label = "Country"
    lockdown_fig.yaxis.major_label_orientation = math.pi / 12
    lockdown_fig.yaxis.major_label_text_font_size = "7pt"
    lockdown_fig.yaxis.major_label_text_font_style = "bold"
    lockdown_fig.ygrid.grid_line_color = None
    lockdown_fig.y_range.range_padding = 0.05

    # Align grid and axis tickers
    lockdown_fig.xgrid[0].ticker = lockdown_fig.xaxis[0].ticker

    # Adding hover tools
    gantt_hover = HoverTool(renderers=[gantt_plot],
                            tooltips=[('Country', '@Country'),
                                      ('Start Date', '@start_date{%d/%m/%Y}'),
                                      ('End Date', '@end_date{%d/%m/%Y}'),
                                      ('Length', '@length{%d days}')],
                            formatters={
                                '@start_date': 'datetime',
                                '@end_date': 'datetime',
                                '@length': 'printf'
                            })

    start_hover = HoverTool(renderers=[start_point],
                            tooltips=[('Country', '@Country'),
                                      ('Start Date', '@start_date{%d/%m/%Y}')],
                            formatters={'@start_date': 'datetime'})

    end_hover = HoverTool(renderers=[end_point],
                          tooltips=[('Country', '@Country'),
                                    ('End Date', '@end_date{%d/%m/%Y}')],
                          formatters={'@end_date': 'datetime'})

    lockdown_fig.add_tools(gantt_hover, start_hover, end_hover)

    # Adding vertical span for today's date
    today_date_span = Span(location=datetime.today(),
                           dimension='height',
                           line_color='blue',
                           line_width=3,
                           line_dash=[6, 6])

    lockdown_fig.add_layout(today_date_span)

    # Labelling span
    span_label = Label(x=datetime.today() + timedelta(hours=12),
                       y=-1.2,
                       y_units='screen',
                       text='Current Date',
                       text_font_size='12pt')

    lockdown_fig.add_layout(span_label)

    # Adding CheckboxButtonGroup for continents
    continents = lockdown_data['continent'].unique().tolist()

    continent_rbg = RadioButtonGroup(labels=continents, active=None, width=500)

    def continent_rbg_callback(attr, old, new):
        if continent_rbg.active != None:
            selected_continent = continent_rbg.labels[continent_rbg.active]

            filtered_df = lockdown_data.loc[lockdown_data.continent ==
                                            selected_continent]

            gantt_cds = ColumnDataSource(filtered_df.dropna())

            points_cds = ColumnDataSource(filtered_df)

            source_gantt.data.update(gantt_cds.data)
            source_points.data.update(points_cds.data)

            lockdown_fig.y_range.factors = filtered_df.Country.unique().tolist(
            )

            # Synchronise country filter
            country_multiselect.options = filtered_df['Country'].unique(
            ).tolist()
            country_multiselect.value = filtered_df['Country'].unique().tolist(
            )

    continent_rbg.on_change('active', continent_rbg_callback)

    # Adding MultiSelect Widget for filtering by country
    countries = lockdown_data['Country'].unique().tolist()

    country_multiselect = MultiSelect(title='Countries:',
                                      options=countries,
                                      value=countries,
                                      height=500)

    def country_multiselect_callback(attr, old, new):

        selected_countries = country_multiselect.value

        filter_condition = lockdown_data.Country.isin(selected_countries)
        filtered_df = lockdown_data.loc[filter_condition]

        gantt_cds = ColumnDataSource(filtered_df.dropna())

        points_cds = ColumnDataSource(filtered_df)

        source_gantt.data.update(gantt_cds.data)
        source_points.data.update(points_cds.data)

        lockdown_fig.y_range.factors = filtered_df.Country.unique().tolist()

        # Synchronise continent filter
        continent_rbg.active = None

    country_multiselect.on_change('value', country_multiselect_callback)

    # Creating Clear All, Select All Buttons

    def clear_button_callback(event):

        country_multiselect.options = countries
        country_multiselect.value = []
        continent_rbg.active = None

        lockdown_fig.y_range.factors = lockdown_data.Country.unique().tolist()

    def select_all_button_callback(event):

        country_multiselect.options = countries
        country_multiselect.value = countries
        continent_rbg.active = None

        lockdown_fig.y_range.factors = lockdown_data.Country.unique().tolist()

    clear_button = Button(label="Clear Selection", button_type="success")
    clear_button.on_click(clear_button_callback)

    select_all_button = Button(label="Select All", button_type="success")
    select_all_button.on_click(select_all_button_callback)

    # Add the plot to the current document and add a title

    layout = row(
        widgetbox(clear_button, select_all_button, continent_rbg,
                  country_multiselect), lockdown_fig)
    layout.sizing_mode = 'scale_width'

    # Creating lockdown tab
    lockdown_tab = Panel(child=layout, title='Lockdown')

    return (lockdown_tab)
Beispiel #21
0
def create():
    config = pyzebra.AnatricConfig()

    def _load_config_file(file):
        config.load_from_file(file)

        logfile_textinput.value = config.logfile
        logfile_verbosity_select.value = config.logfile_verbosity

        filelist_type.value = config.filelist_type
        filelist_format_textinput.value = config.filelist_format
        filelist_datapath_textinput.value = config.filelist_datapath
        filelist_ranges_textareainput.value = "\n".join(
            map(str, config.filelist_ranges))

        crystal_sample_textinput.value = config.crystal_sample
        lambda_textinput.value = config.crystal_lambda
        zeroOM_textinput.value = config.crystal_zeroOM
        zeroSTT_textinput.value = config.crystal_zeroSTT
        zeroCHI_textinput.value = config.crystal_zeroCHI
        ub_textareainput.value = config.crystal_UB

        dataFactory_implementation_select.value = config.dataFactory_implementation
        dataFactory_dist1_textinput.value = config.dataFactory_dist1
        reflectionPrinter_format_select.value = config.reflectionPrinter_format

        set_active_widgets(config.algorithm)
        if config.algorithm == "adaptivemaxcog":
            threshold_textinput.value = config.threshold
            shell_textinput.value = config.shell
            steepness_textinput.value = config.steepness
            duplicateDistance_textinput.value = config.duplicateDistance
            maxequal_textinput.value = config.maxequal
            aps_window_textinput.value = str(
                tuple(map(int, config.aps_window.values())))

        elif config.algorithm == "adaptivedynamic":
            adm_window_textinput.value = str(
                tuple(map(int, config.adm_window.values())))
            border_textinput.value = str(
                tuple(map(int, config.border.values())))
            minWindow_textinput.value = str(
                tuple(map(int, config.minWindow.values())))
            reflectionFile_textinput.value = config.reflectionFile
            targetMonitor_textinput.value = config.targetMonitor
            smoothSize_textinput.value = config.smoothSize
            loop_textinput.value = config.loop
            minPeakCount_textinput.value = config.minPeakCount
            displacementCurve_textinput.value = "\n".join(
                map(str, config.displacementCurve))
        else:
            raise ValueError("Unknown processing mode.")

    def set_active_widgets(implementation):
        if implementation == "adaptivemaxcog":
            mode_radio_button_group.active = 0
            disable_adaptivemaxcog = False
            disable_adaptivedynamic = True

        elif implementation == "adaptivedynamic":
            mode_radio_button_group.active = 1
            disable_adaptivemaxcog = True
            disable_adaptivedynamic = False
        else:
            raise ValueError(
                "Implementation can be either 'adaptivemaxcog' or 'adaptivedynamic'"
            )

        threshold_textinput.disabled = disable_adaptivemaxcog
        shell_textinput.disabled = disable_adaptivemaxcog
        steepness_textinput.disabled = disable_adaptivemaxcog
        duplicateDistance_textinput.disabled = disable_adaptivemaxcog
        maxequal_textinput.disabled = disable_adaptivemaxcog
        aps_window_textinput.disabled = disable_adaptivemaxcog

        adm_window_textinput.disabled = disable_adaptivedynamic
        border_textinput.disabled = disable_adaptivedynamic
        minWindow_textinput.disabled = disable_adaptivedynamic
        reflectionFile_textinput.disabled = disable_adaptivedynamic
        targetMonitor_textinput.disabled = disable_adaptivedynamic
        smoothSize_textinput.disabled = disable_adaptivedynamic
        loop_textinput.disabled = disable_adaptivedynamic
        minPeakCount_textinput.disabled = disable_adaptivedynamic
        displacementCurve_textinput.disabled = disable_adaptivedynamic

    upload_div = Div(text="Open XML configuration file:")

    def upload_button_callback(_attr, _old, new):
        with io.BytesIO(base64.b64decode(new)) as file:
            _load_config_file(file)

    upload_button = FileInput(accept=".xml")
    upload_button.on_change("value", upload_button_callback)

    # General parameters
    # ---- logfile
    def logfile_textinput_callback(_attr, _old, new):
        config.logfile = new

    logfile_textinput = TextInput(title="Logfile:",
                                  value="logfile.log",
                                  width=520)
    logfile_textinput.on_change("value", logfile_textinput_callback)

    def logfile_verbosity_select_callback(_attr, _old, new):
        config.logfile_verbosity = new

    logfile_verbosity_select = Select(title="verbosity:",
                                      options=["0", "5", "10", "15", "30"],
                                      width=70)
    logfile_verbosity_select.on_change("value",
                                       logfile_verbosity_select_callback)

    # ---- FileList
    def filelist_type_callback(_attr, _old, new):
        config.filelist_type = new

    filelist_type = Select(title="File List:",
                           options=["TRICS", "SINQ"],
                           width=100)
    filelist_type.on_change("value", filelist_type_callback)

    def filelist_format_textinput_callback(_attr, _old, new):
        config.filelist_format = new

    filelist_format_textinput = TextInput(title="format:", width=490)
    filelist_format_textinput.on_change("value",
                                        filelist_format_textinput_callback)

    def filelist_datapath_textinput_callback(_attr, _old, new):
        config.filelist_datapath = new

    filelist_datapath_textinput = TextInput(title="datapath:")
    filelist_datapath_textinput.on_change(
        "value", filelist_datapath_textinput_callback)

    def filelist_ranges_textareainput_callback(_attr, _old, new):
        ranges = []
        for line in new.splitlines():
            ranges.append(re.findall(r"\b\d+\b", line))
        config.filelist_ranges = ranges

    filelist_ranges_textareainput = TextAreaInput(title="ranges:", height=100)
    filelist_ranges_textareainput.on_change(
        "value", filelist_ranges_textareainput_callback)

    # ---- crystal
    def crystal_sample_textinput_callback(_attr, _old, new):
        config.crystal_sample = new

    crystal_sample_textinput = TextInput(title="Sample Name:")
    crystal_sample_textinput.on_change("value",
                                       crystal_sample_textinput_callback)

    def lambda_textinput_callback(_attr, _old, new):
        config.crystal_lambda = new

    lambda_textinput = TextInput(title="lambda:", width=140)
    lambda_textinput.on_change("value", lambda_textinput_callback)

    def ub_textareainput_callback(_attr, _old, new):
        config.crystal_UB = new

    ub_textareainput = TextAreaInput(title="UB matrix:", height=100)
    ub_textareainput.on_change("value", ub_textareainput_callback)

    def zeroOM_textinput_callback(_attr, _old, new):
        config.crystal_zeroOM = new

    zeroOM_textinput = TextInput(title="zeroOM:", width=140)
    zeroOM_textinput.on_change("value", zeroOM_textinput_callback)

    def zeroSTT_textinput_callback(_attr, _old, new):
        config.crystal_zeroSTT = new

    zeroSTT_textinput = TextInput(title="zeroSTT:", width=140)
    zeroSTT_textinput.on_change("value", zeroSTT_textinput_callback)

    def zeroCHI_textinput_callback(_attr, _old, new):
        config.crystal_zeroCHI = new

    zeroCHI_textinput = TextInput(title="zeroCHI:", width=140)
    zeroCHI_textinput.on_change("value", zeroCHI_textinput_callback)

    # ---- DataFactory
    def dataFactory_implementation_select_callback(_attr, _old, new):
        config.dataFactory_implementation = new

    dataFactory_implementation_select = Select(
        title="DataFactory implementation:",
        options=DATA_FACTORY_IMPLEMENTATION,
        width=300,
    )
    dataFactory_implementation_select.on_change(
        "value", dataFactory_implementation_select_callback)

    def dataFactory_dist1_textinput_callback(_attr, _old, new):
        config.dataFactory_dist1 = new

    dataFactory_dist1_textinput = TextInput(title="dist1:", width=290)
    dataFactory_dist1_textinput.on_change(
        "value", dataFactory_dist1_textinput_callback)

    # ---- BackgroundProcessor

    # ---- DetectorEfficency

    # ---- ReflectionPrinter
    def reflectionPrinter_format_select_callback(_attr, _old, new):
        config.reflectionPrinter_format = new

    reflectionPrinter_format_select = Select(
        title="ReflectionPrinter format:",
        options=REFLECTION_PRINTER_FORMATS,
        width=300,
    )
    reflectionPrinter_format_select.on_change(
        "value", reflectionPrinter_format_select_callback)

    # Adaptive Peak Detection (adaptivemaxcog)
    # ---- threshold
    def threshold_textinput_callback(_attr, _old, new):
        config.threshold = new

    threshold_textinput = TextInput(title="Threshold:")
    threshold_textinput.on_change("value", threshold_textinput_callback)

    # ---- shell
    def shell_textinput_callback(_attr, _old, new):
        config.shell = new

    shell_textinput = TextInput(title="Shell:")
    shell_textinput.on_change("value", shell_textinput_callback)

    # ---- steepness
    def steepness_textinput_callback(_attr, _old, new):
        config.steepness = new

    steepness_textinput = TextInput(title="Steepness:")
    steepness_textinput.on_change("value", steepness_textinput_callback)

    # ---- duplicateDistance
    def duplicateDistance_textinput_callback(_attr, _old, new):
        config.duplicateDistance = new

    duplicateDistance_textinput = TextInput(title="Duplicate Distance:")
    duplicateDistance_textinput.on_change(
        "value", duplicateDistance_textinput_callback)

    # ---- maxequal
    def maxequal_textinput_callback(_attr, _old, new):
        config.maxequal = new

    maxequal_textinput = TextInput(title="Max Equal:")
    maxequal_textinput.on_change("value", maxequal_textinput_callback)

    # ---- window
    def aps_window_textinput_callback(_attr, _old, new):
        config.aps_window = dict(
            zip(("x", "y", "z"), re.findall(r"\b\d+\b", new)))

    aps_window_textinput = TextInput(title="Window (x, y, z):")
    aps_window_textinput.on_change("value", aps_window_textinput_callback)

    # Adaptive Dynamic Mask Integration (adaptivedynamic)
    # ---- window
    def adm_window_textinput_callback(_attr, _old, new):
        config.adm_window = dict(
            zip(("x", "y", "z"), re.findall(r"\b\d+\b", new)))

    adm_window_textinput = TextInput(title="Window (x, y, z):")
    adm_window_textinput.on_change("value", adm_window_textinput_callback)

    # ---- border
    def border_textinput_callback(_attr, _old, new):
        config.border = dict(zip(("x", "y", "z"), re.findall(r"\b\d+\b", new)))

    border_textinput = TextInput(title="Border (x, y, z):")
    border_textinput.on_change("value", border_textinput_callback)

    # ---- minWindow
    def minWindow_textinput_callback(_attr, _old, new):
        config.minWindow = dict(
            zip(("x", "y", "z"), re.findall(r"\b\d+\b", new)))

    minWindow_textinput = TextInput(title="Min Window (x, y, z):")
    minWindow_textinput.on_change("value", minWindow_textinput_callback)

    # ---- reflectionFile
    def reflectionFile_textinput_callback(_attr, _old, new):
        config.reflectionFile = new

    reflectionFile_textinput = TextInput(title="Reflection File:")
    reflectionFile_textinput.on_change("value",
                                       reflectionFile_textinput_callback)

    # ---- targetMonitor
    def targetMonitor_textinput_callback(_attr, _old, new):
        config.targetMonitor = new

    targetMonitor_textinput = TextInput(title="Target Monitor:")
    targetMonitor_textinput.on_change("value",
                                      targetMonitor_textinput_callback)

    # ---- smoothSize
    def smoothSize_textinput_callback(_attr, _old, new):
        config.smoothSize = new

    smoothSize_textinput = TextInput(title="Smooth Size:")
    smoothSize_textinput.on_change("value", smoothSize_textinput_callback)

    # ---- loop
    def loop_textinput_callback(_attr, _old, new):
        config.loop = new

    loop_textinput = TextInput(title="Loop:")
    loop_textinput.on_change("value", loop_textinput_callback)

    # ---- minPeakCount
    def minPeakCount_textinput_callback(_attr, _old, new):
        config.minPeakCount = new

    minPeakCount_textinput = TextInput(title="Min Peak Count:")
    minPeakCount_textinput.on_change("value", minPeakCount_textinput_callback)

    # ---- displacementCurve
    def displacementCurve_textinput_callback(_attr, _old, new):
        maps = []
        for line in new.splitlines():
            maps.append(re.findall(r"\d+(?:\.\d+)?", line))
        config.displacementCurve = maps

    displacementCurve_textinput = TextAreaInput(
        title="Displacement Curve (twotheta, x, y):", height=100)
    displacementCurve_textinput.on_change(
        "value", displacementCurve_textinput_callback)

    def mode_radio_button_group_callback(active):
        if active == 0:
            config.algorithm = "adaptivemaxcog"
            set_active_widgets("adaptivemaxcog")
        else:
            config.algorithm = "adaptivedynamic"
            set_active_widgets("adaptivedynamic")

    mode_radio_button_group = RadioButtonGroup(
        labels=["Adaptive Peak Detection", "Adaptive Dynamic Integration"],
        active=0)
    mode_radio_button_group.on_click(mode_radio_button_group_callback)
    set_active_widgets("adaptivemaxcog")

    def process_button_callback():
        with tempfile.TemporaryDirectory() as temp_dir:
            temp_file = temp_dir + "/temp.xml"
            config.save_as(temp_file)
            pyzebra.anatric(temp_file)

            with open(config.logfile) as f_log:
                output_log.value = f_log.read()

    process_button = Button(label="Process", button_type="primary")
    process_button.on_click(process_button_callback)

    output_log = TextAreaInput(title="Logfile output:",
                               height=700,
                               disabled=True)
    output_config = TextAreaInput(title="Current config:",
                                  height=700,
                                  width=400,
                                  disabled=True)

    tab_layout = row(
        column(
            upload_div,
            upload_button,
            row(logfile_textinput, logfile_verbosity_select),
            row(filelist_type, filelist_format_textinput),
            filelist_datapath_textinput,
            filelist_ranges_textareainput,
            crystal_sample_textinput,
            row(lambda_textinput, zeroOM_textinput, zeroSTT_textinput,
                zeroCHI_textinput),
            ub_textareainput,
            row(dataFactory_implementation_select,
                dataFactory_dist1_textinput),
            reflectionPrinter_format_select,
            process_button,
        ),
        column(
            mode_radio_button_group,
            row(
                column(
                    threshold_textinput,
                    shell_textinput,
                    steepness_textinput,
                    duplicateDistance_textinput,
                    maxequal_textinput,
                    aps_window_textinput,
                ),
                column(
                    adm_window_textinput,
                    border_textinput,
                    minWindow_textinput,
                    reflectionFile_textinput,
                    targetMonitor_textinput,
                    smoothSize_textinput,
                    loop_textinput,
                    minPeakCount_textinput,
                    displacementCurve_textinput,
                ),
            ),
        ),
        output_config,
        output_log,
    )

    async def update_config():
        config.save_as("debug.xml")
        with open("debug.xml") as f_config:
            output_config.value = f_config.read()

    curdoc().add_periodic_callback(update_config, 1000)

    return Panel(child=tab_layout, title="Anatric")
    Button(label="Primary Button 2", button_type="primary"),
    Button(label="Success Button 3", button_type="success"),

    Toggle(label="Default Toggle 1", button_type="default"),
    Toggle(label="Primary Toggle 2", button_type="primary"),
    Toggle(label="Success Toggle 3", button_type="success"),

    Dropdown(label="Default Dropdown 1", button_type="default", menu=menu),
    Dropdown(label="Primary Dropdown 2", button_type="primary", menu=menu),
    Dropdown(label="Success Dropdown 3", button_type="success", menu=menu),

    CheckboxButtonGroup(labels=["Checkbox Option 1", "Checkbox Option 2", "Checkbox Option 3"], button_type="default", active=[0, 1]),
    CheckboxButtonGroup(labels=["Checkbox Option 4", "Checkbox Option 5", "Checkbox Option 6"], button_type="primary", active=[1, 2]),
    CheckboxButtonGroup(labels=["Checkbox Option 7", "Checkbox Option 8", "Checkbox Option 9"], button_type="success", active=[0, 2]),

    RadioButtonGroup(labels=["Radio Option 1", "Radio Option 2", "Radio Option 3"], button_type="default", active=0),
    RadioButtonGroup(labels=["Radio Option 4", "Radio Option 5", "Radio Option 6"], button_type="primary", active=1),
    RadioButtonGroup(labels=["Radio Option 7", "Radio Option 8", "Radio Option 9"], button_type="success", active=2),

    TextInput(placeholder="TextInput 1"),
    TextInput(placeholder="TextInput 2"),
    TextInput(placeholder="TextInput 3"),

    AutocompleteInput(placeholder="AutocompleteInput 1 ...", completions=["aaa", "aab", "aac", "baa", "caa"]),
    AutocompleteInput(placeholder="AutocompleteInput 2 ...", completions=["AAA", "AAB", "AAC", "BAA", "CAA"]),
    AutocompleteInput(placeholder="AutocompleteInput 3 ...", completions=["000", "001", "002", "100", "200"]),

    DatePicker(value=date(2018, 9, 1)),
    DatePicker(value=date(2018, 9, 2)),
    DatePicker(value=date(2018, 9, 3)),
)
                map_plot_glyphs[i].glyph.line_color = "#8f8f8f"
                elevation_graph_glyphs[i].glyph.line_alpha = 0.2
                gradient_graph_glyphs[i].glyph.line_alpha = 0.2
                map_plot_glyphs[i].glyph.line_alpha = 0.3
                gradient_histogram_glyphs[i].visible = False


###############################
# Route selection pane (left) #
###############################

origin_input = TextInput(width=400, title="Origin")
destination_input = TextInput(width=400, title="Destination")
type_input = RadioButtonGroup(
    labels=["Bicycle", "Driving", "Walking"],
    width=400,
    active=0
)
go_button = Button(width=400, label="Search", button_type="success")
go_button.on_click(find_routes)

alert_holder = PreText(text="", css_classes=['hidden'], visible=False)
alert = CustomJS(args={}, code='alert(cb_obj.text);')
alert_holder.js_on_change("text", alert)

result_picker = RadioGroup(labels=[], width=400)
result_picker.on_change("active", display_results)

route_selection_pane = column(
    Div(text="<h2>Search</h2>"),
    origin_input,
Beispiel #24
0
         width=5,
         height=1,
         color=DARK_GRAY)

# create components --------------
title_div = Div(
    text=
    'Revenue Impact of a 4% Surtax on Taxpayers with Adjusted Gross Income over $5 Million',
    height=30)

radio_group_text = Div(text="Include Additional Capital Gains Behavior.")
radio_group = RadioGroup(labels=["Without", "With"], active=0)
sources['radio_group'] = radio_group

elasticity_text = Div(text="Elasticity of Taxable Income")
elasticity_option = RadioButtonGroup(
    labels=sources['capital_gains_no'].data['labels'], active=0)
sources['elasticity_option'] = elasticity_option

# create callbacks ---------------
radio_group.callback = CustomJS(args=sources,
                                code="""
  var capitalGainsType = radio_group.active === 0 ? capital_gains_no : capital_gains_yes;
  elasticity_option.set('labels', capitalGainsType.data.labels);

  while (elasticity_option.active > capitalGainsType.data.source_ids.length - 1) {
    elasticity_option.active = elasticity_option.active - 1;
  }

  ref.data = eval(capitalGainsType.data.source_ids[elasticity_option.active]).data;
  ref.trigger('change');
""")
Beispiel #25
0
def barchart(df):
    def make_dataset(df, usageType):

        df_verwerkt = df[df['attributeName'] == usageType]
        df_verwerkt = df_verwerkt.sort_values('time')
        df_verwerkt = df_verwerkt.replace(0, np.NaN)
        df_verwerkt = df_verwerkt.fillna(method='ffill')
        df_verwerkt = df_verwerkt.fillna(
            method='bfill')  # nodig om eerste rij te fixen.
        df_verwerkt['time'] = pd.to_datetime(df_verwerkt['time'],
                                             unit='ms',
                                             utc=True)
        df_verwerkt['time'] = df_verwerkt['time'].apply(
            lambda x: x.astimezone(timezone('Europe/Amsterdam')))
        df_verwerkt['hour'] = df_verwerkt['time'].dt.hour
        df_verwerkt['minutes'] = (df_verwerkt['time'].dt.hour *
                                  60) + df_verwerkt['time'].dt.minute
        df_verwerkt['dayofweek'] = df_verwerkt['time'].dt.dayofweek
        df_verwerkt['difference'] = df_verwerkt['value'].diff()
        df_verwerkt['difference'] = df_verwerkt['difference'].fillna(0)
        df_verwerkt[df_verwerkt['difference'] >
                    1000] = 0  # sprong naar Nederhoven uitfilteren
        df_verwerkt = df_verwerkt[df_verwerkt['difference'] > 0]

        df_verwerkt = remove_outlier(df_verwerkt, 'difference')

        groupby = df_verwerkt.groupby('minutes').sum()

        return ColumnDataSource(data=groupby)

    def make_plot(src):

        # Deze regel maakt een dict met de vorm {0: '00:00', 1: '00:01', ... t/m 1440 voor alle minuten van de dag.
        # In feite zet deze regel alle minuten van 0 t/m 1440 om in een string met tijd voor de x-as.
        d = {
            i: f"{floor(i/60):02d}" + ":" + f"{int(i%60):02d}"
            for i in range(1440)
        }

        p = figure(title='Bar chart',
                   x_range=(0, 1440),
                   tools=TOOLS,
                   background_fill_color="#fafafa")
        p.sizing_mode = 'stretch_both'  # https://docs.bokeh.org/en/latest/docs/user_guide/layout.html
        p.vbar(x='minutes',
               bottom=0,
               top='difference',
               source=src,
               width=0.9,
               color={
                   'field': 'difference',
                   'transform': color_mapper
               })

        p.y_range.start = 0
        p.xaxis.axis_label = 'Tijd'
        p.xaxis.major_label_overrides = d
        p.yaxis.axis_label = 'Verbruik'
        p.grid.grid_line_color = "white"
        return p

    def update(attr, old, new):

        # Get the selected items for the graph
        # ...
        selected = radio_button_group.active

        usageType = 'gastotalusage'  # standaardwaarde

        # Koppel de selectie aan de juiste gegevens uit het DataFrame
        if selected == 0:
            usageType = 'gastotalusage'
            p.title.text = 'Gasverbruik per minuut'
        elif selected == 1:
            usageType = 'tariff1totalusage'
            p.title.text = 'Stroomtarief 1 verbruik'
        elif selected == 2:
            usageType = 'tariff2totalusage'
            p.title.text = 'Stroomtarief 2 verbruik'

        print('Update usageType: ' + str(usageType))

        # update data
        new_src = make_dataset(df, usageType)
        src.data.update(new_src.data)

    radio_button_group = RadioButtonGroup(
        labels=["Gas", "Tarief 1", "Tarief 2"], active=0)

    radio_button_group.on_change('active', update)

    # initial execution
    src = make_dataset(df, 'gastotalusage')
    p = make_plot(src)

    # make a grid
    grid = gridplot([[p], [radio_button_group]])
    grid.sizing_mode = 'scale_width'

    tab = Panel(child=grid, title='Bar chart')

    print("barchart() uitgevoerd")

    return tab
Beispiel #26
0
    def __init__(self):

        ### Methods
        self.args = Settings()
        self.index = None
        self.data_getter = None
        self.filter = None
        self._data = None
        self._model_type = None
        self._model_dir = self.args.models_path + 'unique_object/'
        self.controls = {}
        self.scene_plotter = ScenePlotter(self._set_head_selection)

        ### initialization of the interface

        ## Model type selector

        def update_select_net():
            if self._model_dir is not None:
                file_list = [fn for fn in os.listdir(self._model_dir) if os.path.isfile(os.path.join(self._model_dir, fn))]
                file_list.sort(key=lambda fn: os.stat(os.path.join(self._model_dir, fn)).st_mtime, reverse=True)
                file_list = [os.path.splitext(fn)[0] for fn in file_list]
                self.controls['net'].options = file_list
                # print(self._model_dir)
                # print('file_list')
                # print(file_list)
                if len(file_list) > 0:
                    self.controls['net'].value = file_list[0]
                else:
                    self.controls['net'].value = None

        def update_model_type():
            if self.controls['multi_mono_object'].active == 0:
                self._model_type = 'mono'
                self._model_dir = self.args.models_path + 'unique_object/'
            elif self.controls['multi_mono_object'].active == 1:
                self._model_type = 'multi_obj'
                self._model_dir = self.args.models_path + 'multi_objects/'
            elif self.controls['multi_mono_object'].active == 2:
                self._model_type = 'multi_pred'
                self._model_dir = self.args.models_path + 'multi_pred/'
            model_types = ['CV', 'CA', 'Bicycle', 'CV_LSTM', 'CA_LSTM', 'Bicycle_LSTM',  'nn_attention']
            existing_types = [type for type in model_types if os.path.isdir(self._model_dir + type)]
            self.controls['model_sub_type'].options = existing_types
            print('existing types')
            print(existing_types)
            if len(existing_types) > 0 and not self.controls['model_sub_type'].value in existing_types:
                self.controls['model_sub_type'].value = existing_types[0]
                return
            set_model_sub_type()
            update_select_net()

        def set_model_sub_type():
            if self.controls['model_sub_type'].value is not None:
                self._model_dir = self._model_dir + self.controls['model_sub_type'].value + '/'
                self.args.model_type = self.controls['model_sub_type'].value
            else:
                self._model_dir = None

        def update_multi_mono_object(attr, old, new):
            update_model_type()
            print(self._model_dir)
            self._set_data_getter()
            print('___')

        multi_mono_object = RadioButtonGroup(labels=["Mono-object", "Multi-objects", "Multi-pred"], active=1)
        self.controls['multi_mono_object'] = multi_mono_object
        multi_mono_object.on_change('active', update_multi_mono_object)

        ## Model sub type selector
        model_types = ['CV', 'CA', 'Bicycle', 'CV_LSTM', 'CA_LSTM', 'Bicycle_LSTM',  'nn_attention']
        model_sub_type = Select(title='Select model type:', value=model_types[3], options=model_types)
        self.controls['model_sub_type'] = model_sub_type
        model_sub_type.on_change('value', lambda att, old, new: update_model_type())

        ## Model selector
        select = Select(title="Select parameter file:", value=None, options=[])
        self.controls['net'] = select
        select.on_change('value', lambda att, old, new: self._set_data_getter())

        ## Select dataset to use
        dataset_list = ['Argoverse', 'Fusion', 'NGSIM']
        select = Select(title='Dataset:', value=dataset_list[0], options=dataset_list)
        self.controls['dataset'] = select
        select.on_change('value', lambda att, old, new: self._set_data_getter(change_index=True))

        ## Set what to draw
        checkbox_group = CheckboxGroup(
            labels=['Draw lanes', 'Draw history', 'Draw true future', 'Draw forecast', 'Draw forecast covariance'],
            active=[0, 1, 2, 3, 4])
        self.controls['check_box'] = checkbox_group
        checkbox_group.on_change('active',
                                 lambda att, old, new: (self._update_cov(), self._update_lanes(), self._update_path()))

        ## Set the number of pred
        n_pred = Slider(start=1, end=6, step=1, value=1, title='Number of prediction hypothesis')
        self.controls['n_pred'] = n_pred
        n_pred.on_change('value', lambda att, old, new: (self._update_cov(), self._update_path()))

        ## Sequence ID input
        text_input = TextInput(title="Sequence ID to plot:", value="Random")
        self.controls['sequence_id'] = text_input

        ## Head selection input
        multi_select_head = MultiSelect(title='Attention head multiple selection:',
                                             value=[], options=[])
        self.controls['Head_selection'] = multi_select_head
        multi_select_head.on_change('value', self.scene_plotter.set_active_heads)

        ## Refresh all sample
        button = Button(label="Refresh", button_type="success")
        self.controls['refresh'] = button
        button.on_click(
            lambda event: (self._set_index(), self._set_data()))
        # button.js_on_click(CustomJS(args=dict(p=self.image), code="""p.reset.emit()"""))

        update_multi_mono_object(None, None, None)

        ## Set the interface layout
        inputs = column(*(self.controls.values()), width=320, height=1000)
        inputs.sizing_mode = "fixed"
        lay = layout([[inputs, self.scene_plotter.get_image()]], sizing_mode="scale_both")
        curdoc().add_root(lay)

        self.scene_plotter._tap_on_veh('selected', [], [0])
Beispiel #27
0
data = data.cpu().detach().numpy()

p = figure(plot_height=int(880*0.8), plot_width=int(1680*0.8))

p.image(image='image', x=0, y=0, dw=168, dh=88, palette="Viridis256", source = source)

energy_slider = Slider(title="Energy", value = data[0][8], start = 0., end = 2000., step = 10.)
phi_slider = Slider(title="Azimuthal angle", value = np.arctan2(data[0][7], data[0][5]), start = -np.pi, end = np.pi, step = np.pi/10.)
coseta_slider = Slider(title="Cosine of zenith angle", value = data[0][6], start = -0.999, end = 0.999, step = 1/20.)

posx_slider = Slider(title="Position x", value = data[0][2], start = -250., end = 250., step = 500./20)
posy_slider = Slider(title="Position y", value = data[0][3], start = -250., end = 250., step = 500./20)
posz_slider = Slider(title="Position z", value = data[0][4], start = -250., end = 250., step = 500./20)

PID_button = RadioButtonGroup(labels=["gamma", "electron", "muon"], active=0)

inputs = row(column(PID_button,energy_slider), column(phi_slider,coseta_slider), column(posx_slider, posy_slider, posz_slider))

def update_data(attrname, old, new) :

    energy = energy_slider.value
    phi = phi_slider.value
    coseta = coseta_slider.value

    dx = np.cos(phi)
    dy = coseta
    dz = np.sin(phi)

    r = (1-dy**2)**0.5
    if r :
Beispiel #28
0
menu = [("Item 1", "item_1_value"), ("Item 2", "item_2_value"), None,
        ("Item 3", "item_3_value")]

dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=menu)
dropdown_split = Dropdown(label="Split button",
                          button_type="danger",
                          menu=menu,
                          split=True)

checkbox_group = CheckboxGroup(labels=["Option 1", "Option 2", "Option 3"],
                               active=[0, 1])
radio_group = RadioGroup(labels=["Option 1", "Option 2", "Option 3"], active=0)

checkbox_button_group = CheckboxButtonGroup(
    labels=["Option 1", "Option 2", "Option 3"], active=[0, 1])
radio_button_group = RadioButtonGroup(
    labels=["Option 1", "Option 2", "Option 3"], active=0)

checkbox_button_group_vertical = CheckboxButtonGroup(
    labels=["Option 1", "Option 2", "Option 3"],
    active=[0, 1],
    orientation="vertical")
radio_button_group_vertical = RadioButtonGroup(
    labels=["Option 1", "Option 2", "Option 3"],
    active=0,
    orientation="vertical")

text_input = TextInput(placeholder="Enter value ...")

completions = ["aaa", "aab", "aac", "baa", "caa"]
autocomplete_input = AutocompleteInput(
    placeholder="Enter value (auto-complete) ...", completions=completions)
Beispiel #29
0
def main():
    #path variables
    #in_path = '/mnt/data04/Conduit/afib/new_files/1hour/'
    #af_outpath = '/mnt/data04/Conduit/afib/AF_annotations/af_ann4/'
    in_path = join(dirname(__file__), "data_in/")
    af_outpath = join(dirname(__file__), "data_out/")
    #Miscellaneous variables
    colours = {
        '': 'black',
        'O': 'blue',
        'N': 'green',
        '~': 'purple',
        'A': 'red'
    }
    #wf_classes = ['AF','Normal','Other','Noise','No Signal']
    wf_classes = ['AF', 'Not AF']
    #global variables remove if you can
    newECG = pd.DataFrame()
    df_ann = pd.DataFrame(columns=[0])

    #widgets to be used as necessary
    rdo_btn_wave_lbls = RadioButtonGroup(labels=wf_classes, active=2)
    date_range_slider = Slider(title="Date Range", step=10)
    table_source, table_columns = load_file_source(in_path, af_outpath)
    file_table = DataTable(source=table_source,
                           columns=table_columns,
                           width=800,
                           height=600)
    pgph_file_loaded = Div(text="Select file from table.",
                           width=1000,
                           height=50)
    pgph_file_loaded.style = {
        "font-size": '1.2em',
        'font-weight': 'bold',
        'color': 'SteelBlue'
    }
    txt_processing = Div(
        text=
        "Use slider to select segments, label by selecting the wave type and pressing 'Label'. Use the save to save when finished all annotations.",
        width=1000,
        height=30)
    txt_processing.style = {
        "font-size": '1.2em',
        'font-weight': 'bold',
        'color': 'SteelBlue',
        'white-space': 'pre'
    }

    btn_lbl = Button(label='Label', button_type='success')
    btn_save = Button(label='Save', button_type='danger')
    btn_load_annotated_graph = Button(label='Load Annotated Graph',
                                      button_type='warning')

    #----------------------------- Functions ---------------------------------#
    ''' Callback function for the data table ColumnDataSource (table_source).
        Load the hd5 file based on the selected row in the data_table.
        Generate the output file path.
        Update the date range slider according to the new file (date_range_slider).
        Generate a plot of the hd5 ECG lead II data (wave_graph).
        Then combine all into a layout (graph_layout)
        Update the Tab Pane with this new layout.
        Returns: Nothing'''
    def callback_select_file_table(attr, old, new):
        global newECG, out_file
        #clear tabs graphs to reduce overhead
        output_tab.child.children = []
        sel_id = new
        txt_processing.text = "Use slider to select segments, label by selecting the wave type and pressing 'Label'. Use the save button to when done."
        txt_processing.style = {"font-size": '1.2em', 'color': 'SteelBlue'}
        wave_file_path = load_selected_file(table_source, sel_id)
        out_file = load_annotation_file(
            table_source, sel_id, af_outpath)  #to be used for saving the file
        pgph_file_loaded.text = "Processing..."
        wave_hdfs, hdfs_keys = load_hd5_file(wave_file_path)
        print(hdfs_keys)
        newECG = (wave_hdfs.select(
            key='Waveforms').II).to_frame()  #get lead II from waveforms)
        newECG.reset_index(
            inplace=True)  #move datetime index to column and reset the index
        newECG.columns = ['date', 'II']
        wave_hdfs.close()
        #enable the buttons if disabled
        btn_save.disabled = False
        btn_lbl.disabled = False
        btn_load_annotated_graph.disabled = True
        #check if the file has already been annotated
        annotated = check_if_annotated(table_source, sel_id)
        if annotated == 'Yes':
            txt_processing.text = 'This file has already been annotated. If you save this file again you will overwrite previous data.'
            txt_processing.style = {"font-size": '1.2em', 'color': 'Red'}
            btn_load_annotated_graph.disabled = False
        #create figure
        print("*********************Creating Figure (in Callback File Select)")
        get_next_graph(0, newECG)
        pgph_file_loaded.text = "File loaded, navigate Label Data Tab to annotate lead II."

    ''' Using the AF annotation file, create a Bokeh figure with annotated data.
        Update the output_tab to show this figure.
        Disable some buttons to keep users on track. '''

    def load_output_graph():
        global newECG
        btn_save.disabled = True
        btn_lbl.disabled = True
        btn_load_annotated_graph.disabled = True
        txt_processing.text = 'Loading Plot...'
        df_AF = pd.read_hdf(out_file)
        noise, normal, other, af, nosig, notAF = load_annotations(
            0, df_AF.shape[0] - 1, newECG, df_AF)
        output_graph = get_graph_annotated(noise, normal, other, af, nosig,
                                           notAF)
        output_tab.child.children = [output_graph]
        txt_processing.text = 'Plot loaded, navigate to "Final Annotated Graph" tab to view. &#10 If you save this file again you will overwrite previous data.'
        btn_save.disabled = False
        btn_lbl.disabled = False

    def get_next_graph(sind, df):
        length = 20000  #7200
        eind = sind + length
        if eind <= list(df.index)[-1] and sind < list(
                df.index
        )[-1]:  #as long as the end and beginning are less than the end of the dataframe
            sub_df = df.iloc[sind:eind]
            sub_df = sub_df.set_index('date')
            del sub_df.index.name
            source = ColumnDataSource(sub_df)
            wave_graph = get_graph(source)
            start_span, end_span = add_span(source)
            wave_graph.add_layout(start_span)
            wave_graph.add_layout(end_span)
            start_rng = start_span.location
            end_rng = end_span.location
        elif eind < list(df.index)[-1] and sind < list(
                df.index
        )[-1]:  #if the start is before but the end is after then just use the end of the dataframe
            sub_df = df.iloc[sind:list(df.index)[-1]]
            sub_df = sub_df.set_index('date')
            del sub_df.index.name
            source = ColumnDataSource(sub_df)
            wave_graph = get_graph(source)
            start_span, end_span = add_span(source)
            wave_graph.add_layout(start_span)
            wave_graph.add_layout(end_span)
            start_rng = start_span.location
            end_rng = end_span.location
        else:
            txt_processing.text = 'You have finished annotating this file.'
            return
        end_span.location = start_span.location
        #slider to change date range
        date_range_slider.start = start_rng
        date_range_slider.end = end_rng
        date_range_slider.value = start_rng
        date_range_slider.on_change('value', callback_date_time_slider)
        graph_layout = column(
            widgetbox([txt_processing, btn_load_annotated_graph, btn_save],
                      width=250),
            widgetbox(Div(text="""<hr/>""",
                          style={
                              'display': 'block',
                              'height': '1px',
                              'border': '0',
                              'border-top': '1px solid #css',
                              'margin': '1em 0',
                              'padding': '0'
                          }),
                      width=1400),
            widgetbox([rdo_btn_wave_lbls, btn_lbl], width=300),
            widgetbox(date_range_slider, width=1350),
            widgetbox(Div(text="""<hr/>""",
                          style={
                              'display': 'block',
                              'height': '1px',
                              'border': '0',
                              'border-top': '1px solid #css',
                              'margin': '1em 0',
                              'padding': '0'
                          }),
                      width=1400), wave_graph)
        wf_tab.child.children = [graph_layout]

    ''' Use a ColumnDataSource to plot the ECG lead II waveform data in a line plot.
        Parameters: source a ColumnDataSource
        Returns: p a Bokeh figure '''

    def get_graph(source):
        p = figure(plot_width=1400,
                   plot_height=500,
                   x_axis_type='datetime',
                   tools=['zoom_in', 'zoom_out', 'xpan', 'ypan'])
        date_range = source.data['II'][0:20000]
        p.y_range = Range1d(start=min(date_range) - 1, end=max(date_range) + 1)
        dt_axis_format = ["%d-%m-%Y %H:%M"]
        wf_x_axis = DatetimeTickFormatter(
            hours=dt_axis_format,
            days=dt_axis_format,
            months=dt_axis_format,
            years=dt_axis_format,
        )
        p.xaxis.formatter = wf_x_axis
        p.line(x='index',
               y='II',
               source=source,
               line_color='black',
               line_width=1)
        return p

    ''' Get the first and last time points from the ColumnDataSource (source).
        Utlizes tzlocal to add an offset which modifies the data.
        Returns integer timetuple value for the dates found: start, end '''

    def get_time(source):
        start = pd.to_datetime(min(source.data['index'])).timestamp() * 1000
        end = pd.to_datetime(max(source.data['index'])).timestamp() * 1000
        return start, end

    ''' Generate two Bokeh Spans based on the ColumnDataSource given (source).
        Spans are at the first and last datetimes in the source data.
        Parameters: source a ColumnDataSource
        Returns: Span, Span '''

    def add_span(source):
        start, end = get_time(source)
        # Start span represents the start of the area of interest
        start_span = Span(location=start,
                          dimension='height',
                          line_color='green',
                          line_dash='dashed',
                          line_width=3)
        # End span represents the end of the area of interest
        end_span = Span(location=end,
                        dimension='height',
                        line_color='red',
                        line_dash='dashed',
                        line_width=3)
        return start_span, end_span

    ''' Callback function for Bokeh Slider, move the Spans on the specified graph (generated in callback_file_table) to the location specified by the Span.
        Returns: Nothing '''

    def callback_date_time_slider(attr, old, new):
        inds = get_spans()
        wf_tab.child.children[0].children[5].renderers[inds[1]].location = new

    ''' Navigate through the widgets on the wave_graph to find the spans
        Returns the widget indexes of the spans'''

    def get_spans():
        inds = []
        for x in range(len(wf_tab.child.children[0].children[5].renderers)):
            if isinstance(wf_tab.child.children[0].children[5].renderers[x],
                          Span):
                inds.append(x)
        return inds

    ''' Callback function for btn_lbl.
        Get the location of the spans from the wave_graph, then call segment_and_label
        Return: Nothing '''

    def callback_btn_lbl():
        inds = get_spans()
        active = rdo_btn_wave_lbls.labels[rdo_btn_wave_lbls.active]
        start_span = wf_tab.child.children[0].children[5].renderers[inds[0]]
        end_span = wf_tab.child.children[0].children[5].renderers[inds[1]]
        segment_and_label(active, start_span.location, end_span.location)

    ''' Function to get ECG data between two Spans (after modifying the timetuple to timestamp).
        Call apply_annotations using start and end indexes found.
        Modify the global df_ann variable.
        Update slider position (start to end), (end to start).
        Parameters: label a string (AF, Normal, Noise, Other), start a timpletuple integer, end a timetuple integer
        Return: nothing '''

    def segment_and_label(label, start, end):
        global newECG
        print("*********************Segmenting and Labelling")
        try:
            txt_processing.text = "Use slider to select segments, label by selecting the wave type and pressing 'Label'. Use the save button to when done."
            start_dt = pd.Timestamp(start / 1000, unit='s')
            end_dt = pd.Timestamp(end / 1000, unit='s')
            mask = (newECG['date'] > start_dt) & (newECG['date'] <= end_dt)
            df_sub = newECG.loc[mask]  #apply mask
            indexes = list(df_sub.index)  #get indexes
            s_ind = indexes[0]  #get first index
            e_ind = indexes[-1]  #get last index
            apply_annotations(label, s_ind, e_ind,
                              df_ann)  #concatenate dataframe of annotations
            get_next_graph(e_ind, newECG)
        except IndexError:
            txt_processing.text = 'Indexing error. Advance slider.'

    ''' Function to apply annotations to a dataframe structured like that of Computing in Cardiology AF algorithm.
        Parameters: label a string (AF, Normal, Noise, Other), s_ind the index of the start datetime,
                    e_ind the index of the end datetime, df a pandas database to be appended to, columns= 'AF'
        Return: Nothing '''

    def apply_annotations(label, s_ind, e_ind, df):
        #data frame structure like this
        if label == 'AF':
            df.loc[s_ind, 0] = 'A'
        elif label == 'Not AF':
            df.loc[s_ind, 0] = 'nAF'
        elif label == 'Noise':
            df.loc[s_ind, 0] = '~'
        elif label == 'Normal':
            df.loc[s_ind, 0] = 'N'
        elif label == 'Other':
            df.loc[s_ind, 0] = 'O'
        elif label == 'No Signal':
            df.loc[s_ind, 0] = '-'

    ''' Stream update to ColumnDataSource that the file has been annotated'''

    def mark_as_done(file_path):
        print("*********************Marking as Done")
        table_source = file_table.source
        name = os.path.splitext(os.path.basename(file_path))[0]  #get file name
        ind = list(table_source.data['name']).index(
            name)  #get index within the source
        patches = {
            'annotated': [(ind, 'Yes')]
        }  #new data to update ColumnDataSource with
        table_source.patch(patches)  #update - ***** THROWING ERROR - CHECK!!

    ''' Callback function for btn_save.
        Utilizes the out_file global variable for the path.
        Appends dataframe to output file. '''

    def callback_save_annotations():
        print("*********************Saving Annotations")
        txt_processing.style = {"font-size": '1.2em', 'color': 'Red'}
        print("Writting: ", out_file)
        txt_processing.text = 'Saving Annotations...'
        df_ann.to_hdf(out_file, key='AF', format='t')
        print("success!")
        btn_save.disabled = True
        btn_lbl.disabled = True
        mark_as_done(out_file)
        nrows = df_ann.shape[0]
        df_ann.drop(df_ann.index[:nrows],
                    inplace=True)  #clear dataframe for a new file to be loaded
        btn_load_annotated_graph.disabled = False
        txt_processing.style = {"font-size": '1.2em', 'color': 'SteelBlue'}
        txt_processing.text = '''Done. Click 'Load Annotated Graph' to view annotations or to "File Management" to select new file to anntoate. You will need to reload this file to make changes.'''

    ''' Load annotations from AF formatted hdf file. '''

    def load_annotations(start, end, df, df_AF):
        # Initialize empty dataframes
        noise = pd.DataFrame()
        normal = pd.DataFrame()
        other = pd.DataFrame()
        af = pd.DataFrame()
        nosig = pd.DataFrame()
        notAF = pd.DataFrame()
        # Read annotations
        for n in range(start, end):
            df_temp = df.iloc[df_AF.index[n]:df_AF.index[n + 1]]
            df_temp.index = list(df_temp['date'])
            df_temp.drop(columns='date')
            value = df_AF.iloc[n, 0]
            if value == '~':
                noise = noise.append(df_temp)
            elif value == 'N':
                normal = normal.append(df_temp)
            elif value == 'O':
                other = other.append(df_temp)
            elif value == 'A':
                af = af.append(df_temp)
            elif value == '-':
                nosig = nosig.append(df_temp)
            elif value == 'nAF':
                notAF = notAF.append(df_temp)
        #add the last labelled section (end+1) to end of df
        df_temp = df.iloc[df_AF.index[end]:df.index[-1]]
        df_temp.index = list(df_temp['date'])
        df_temp.drop(columns='date')
        value = df_AF.iloc[end, 0]
        if value == '~':
            noise = noise.append(df_temp)
        elif value == 'N':
            normal = normal.append(df_temp)
        elif value == 'O':
            other = other.append(df_temp)
        elif value == 'A':
            af = af.append(df_temp)
        elif value == '-':
            nosig = nosig.append(df_temp)
        elif value == 'nAF':
            notAF = notAF.append(df_temp)
        return noise, normal, other, af, nosig, notAF

    ''' Create Bokeh figure from dataframes af, normal, other and noise. '''

    def get_graph_annotated(noise, normal, other, af, nosig, notAF):
        p = figure(
            plot_width=1400,
            plot_height=500,
            x_axis_type='datetime',
            tools=['box_zoom', 'wheel_zoom', 'pan', 'reset', 'crosshair'])
        # plot color coded waves (if they exist)
        if noise.empty is False:
            p.line(x='index',
                   y='II',
                   source=noise,
                   color='blue',
                   legend='Noise')
        if normal.empty is False:
            p.line(x='index',
                   y='II',
                   source=normal,
                   color='green',
                   legend='Normal')
        if other.empty is False:
            p.line(x='index',
                   y='II',
                   source=other,
                   color='purple',
                   legend='Other')
        if af.empty is False:
            p.line(x='index', y='II', source=af, color='red', legend='AF')
        if nosig.empty is False:
            p.line(x='index',
                   y='II',
                   source=nosig,
                   color='black',
                   legend='No Signal')
        if notAF.empty is False:
            p.line(x='index',
                   y='II',
                   source=notAF,
                   color='grey',
                   legend='No Signal')
        dt_axis_format = ["%d-%m-%Y %H:%M"]
        wf_x_axis = DatetimeTickFormatter(
            hours=dt_axis_format,
            days=dt_axis_format,
            months=dt_axis_format,
            years=dt_axis_format,
        )
        p.xaxis.formatter = wf_x_axis
        return p

    ############################## Assign Callbacks ##########################################
    #table_source.on_change('selected', callback_select_file_table) #assign callback
    table_source.selected.on_change('indices', callback_select_file_table)
    btn_lbl.on_click(callback_btn_lbl)
    btn_save.on_click(callback_save_annotations)
    btn_load_annotated_graph.on_click(load_output_graph)

    ################################## Load Document ##########################################
    ####layouts####
    file_layout = column(widgetbox(pgph_file_loaded, file_table, width=1000))
    col_waveforms = column(name="figures", sizing_mode='scale_width')
    col_output = column(name="output", sizing_mode='scale_width')

    ###tabs###
    wf_tab = Panel(child=col_waveforms, title='Label Data')
    file_tab = Panel(child=file_layout, title='File Management')
    output_tab = Panel(child=col_output, title='Final Annotated Graph')
    tab_pane = Tabs(tabs=[file_tab, wf_tab, output_tab], width=1000)

    ###combine into document####
    curdoc().add_root(column(tab_pane))
    curdoc().title = "AF Annotator 4"
Beispiel #30
0
    ("Daily New Cases per Capita", "@dnc"),
],
                  renderers=[cantons])

p1.add_tools(hover)

# T2.3 Add circles at the locations of capital cities for each canton, and the sizes are proportional to daily new cases per capita
sites = p1.circle('long',
                  'lat',
                  source=geosource,
                  color='violet',
                  size='size',
                  alpha=0.33)

# T2.4 Create a radio button group with labels 'Density', and 'BedsPerCapita'
buttons = RadioButtonGroup(labels=['Density', 'BedPerCapita'], active=0)


# Define a function to update color mapper used in both patches and colorbar
def update_bar(new):
    for i, d in enumerate(labels):
        if i == new:
            color_bar.color_mapper = mappers[d]["transform"]
            color_bar.title = d
            cantons.glyph.fill_color = mappers[d]


buttons.on_click(update_bar)

# T2.5 Add a dateslider to control which per capita daily new cases information to display
Beispiel #31
0
def prepare_server(doc,
                   input_data,
                   cell_stack,
                   cell_markers=None,
                   default_cell_marker=None):
    @lru_cache()
    def image_markers(lower=False, mapping=False):
        if mapping:
            return {
                y: j
                for j, y in sorted(
                    ((i, x) for i, x in enumerate(
                        image_markers(lower=lower, mapping=False))),
                    key=lambda x: x[1].lower(),
                )
            }
        if lower:
            return [
                x.lower() for x in image_markers(lower=False, mapping=False)
            ]
        return (cell_markers if cell_markers is not None else
                [f"Marker {i + 1}" for i in range(cell_stack.shape[1])])

    # Data sources
    ###########################################################################

    def prepare_data(input_data):
        data = input_data.copy()
        if "contour" in data and not all(x in data
                                         for x in ["contour_x", "contour_y"]):
            contour = parse_contour(data["contour"])
            data["contour_x"] = contour[0]
            data["contour_y"] = contour[1]
        if "marked" not in data:
            data["marked"] = np.full(data.shape[0], "")
        source.data = data

    source = ColumnDataSource(data={})
    prepare_data(input_data)
    image_source = ColumnDataSource(
        data=dict(image=[], dw=[], dh=[], contour_x=[], contour_y=[]))

    # Cell picture plot
    ###########################################################################

    def add_outline():
        data = source.data
        if all(x in data for x in ["contour_x", "contour_y"]):
            cell_outline = cell_figure.patches(
                xs="contour_x",
                ys="contour_y",
                fill_color=None,
                line_color="red",
                name="cell_outline",
                source=image_source,
            )
            cell_outline.level = "overlay"
        else:
            cell_outline = cell_figure.select(name="cell_outline")
            for x in cell_outline:
                cell_figure.renderers.remove(x)

    default_cell_marker = (0 if default_cell_marker is None else image_markers(
        mapping=True)[default_cell_marker])
    cell_markers_select = Select(
        value=str(default_cell_marker),
        options=list(
            (str(i), x) for x, i in image_markers(mapping=True).items()),
        title="Marker cell image",
    )
    cell_marker_input = AutocompleteInput(
        completions=list(image_markers()) + list(image_markers(lower=True)),
        min_characters=1,
        placeholder="Search for marker",
    )
    cell_slider = RangeSlider(start=0,
                              end=1,
                              value=(0, 1),
                              orientation="vertical",
                              direction="rtl")
    metric_select = RadioButtonGroup(active=0, labels=CELL_IMAGE_METRICS[0])
    stats = PreText(text="", width=100)

    cell_mapper = bokeh.models.mappers.LinearColorMapper(viridis(20),
                                                         low=0,
                                                         high=1000,
                                                         high_color=None)
    cell_color_bar = ColorBar(color_mapper=cell_mapper,
                              width=12,
                              location=(0, 0))
    cell_figure = figure(
        plot_width=450,
        plot_height=350,
        tools="pan,wheel_zoom,reset",
        toolbar_location="left",
    )
    cell_image = cell_figure.image(
        image="image",
        color_mapper=cell_mapper,
        x=0,
        y=0,
        dw="dw",
        dh="dh",
        source=image_source,
    )
    add_outline()
    cell_figure.add_layout(cell_color_bar, "right")

    # Edit data of selected cells
    ###########################################################################

    marker_edit_container = column()
    marker_edit_instances = []

    def add_marker_edit_callback():
        editor = ColumnEditor(
            source,
            marker_edit_container,
            log_widget=edit_selecton_log,
            editor_delete_callback=delete_marker_edit_callback,
            external_edit_callback=edit_selection_callback,
        )
        marker_edit_instances.append(editor)

    def delete_marker_edit_callback(editor):
        idx = next(i for i, x in enumerate(marker_edit_instances)
                   if x is editor)
        del marker_edit_instances[idx]

    file_name_text = Div()

    add_marker_edit_button = Button(label="+",
                                    button_type="success",
                                    align=("start", "end"),
                                    width=50)
    add_marker_edit_button.on_click(add_marker_edit_callback)

    edit_selection_submit = Button(label="Submit change",
                                   button_type="primary",
                                   align=("start", "end"))
    download_button = Button(label="Download edited data",
                             button_type="success",
                             align=("start", "end"))
    download_button.js_on_click(
        CustomJS(args=dict(source=source), code=DOWNLOAD_JS))

    edit_selecton_log = TextAreaInput(value="",
                                      disabled=True,
                                      css_classes=["edit_log"],
                                      cols=30,
                                      rows=10)

    upload_file_input = FileInput(accept="text/csv", align=("end", "end"))

    # Cell table
    ###########################################################################

    default_data_table_cols = [
        TableColumn(field="marked", title="Seen", width=20)
    ]

    data_table = DataTable(source=source,
                           columns=default_data_table_cols,
                           width=800)

    # Callbacks for buttons and widgets
    ###########################################################################

    def cell_slider_change(attrname, old, new):
        cell_mapper.low = new[0]
        cell_mapper.high = new[1]

    def selection_change(attrname, old, new):
        selected = source.selected.indices
        data = source.data
        if not selected:
            return
        mean_image = CELL_IMAGE_METRICS[1][metric_select.active](
            cell_stack[selected,
                       int(cell_markers_select.value), :, :], axis=0)
        image_data = {
            "image": [mean_image],
            "dw": [cell_stack.shape[2]],
            "dh": [cell_stack.shape[3]],
        }
        for coord in ["contour_x", "contour_y"]:
            try:
                image_data[coord] = list(data[coord][selected])
            except KeyError:
                pass
        image_source.data = image_data
        image_extr = round_signif(mean_image.min()), round_signif(
            mean_image.max())
        cell_slider.start = image_extr[0]
        cell_slider.end = image_extr[1]
        cell_slider.step = round_signif((image_extr[1] - image_extr[0]) / 50)
        cell_slider.value = image_extr
        stats.text = "n cells: " + str(len(selected))

    def autocomplete_cell_change(attrname, old, new):
        try:
            idx = image_markers(mapping=True)[new]
        except KeyError:
            try:
                idx = image_markers(lower=True, mapping=True)[new]
            except KeyError:
                return
        cell_markers_select.value = str(idx)
        cell_marker_input.value = None

    def data_change(attrname, old, new):
        new_keys = [n for n in new.keys() if n not in set(old.keys())]
        for n in new_keys:
            data_table.columns.append(TableColumn(field=n, title=n))

    def edit_selection_submit_click():
        for x in marker_edit_instances:
            x.edit_callback()

    def edit_selection_callback():
        idx = source.selected.indices
        try:
            if len(idx) == 1 and all(
                    source.data[x.widgets["input_col"].value][idx] != "NA"
                    for x in marker_edit_instances):
                source.selected.indices = [idx[0] + 1]
        except KeyError:
            pass

    def upload_file_callback(attrname, old, new):
        try:
            data_text = b64decode(new)
            data = pd.read_csv(BytesIO(data_text))
        except Exception:
            file_name_text.text = f"Error loading file {upload_file_input.filename}"
            return
        file_name_text.text = f"Editing file {upload_file_input.filename}"
        # Have to regenerate contours
        try:
            del data["contour_x"]
            del data["contour_y"]
        except KeyError:
            pass
        data_table.columns = default_data_table_cols
        prepare_data(data)
        add_outline()

    source.selected.on_change("indices", selection_change)
    source.on_change("data", data_change)
    cell_slider.on_change("value", cell_slider_change)
    metric_select.on_change("active", selection_change)
    cell_markers_select.on_change("value", selection_change)
    cell_marker_input.on_change("value", autocomplete_cell_change)
    edit_selection_submit.on_click(edit_selection_submit_click)
    upload_file_input.on_change("value", upload_file_callback)

    style_div = Div(text=CUSTOM_CSS)

    # set up layout
    layout = column(
        row(
            column(data_table),
            column(
                cell_markers_select,
                cell_marker_input,
                metric_select,
                row(cell_figure, cell_slider),
                stats,
            ),
        ),
        file_name_text,
        marker_edit_container,
        add_marker_edit_button,
        row(edit_selection_submit, download_button, upload_file_input),
        edit_selecton_log,
        style_div,
    )

    doc.add_root(layout)
    doc.title = "Cell classifier"
Beispiel #32
0
# get all osm files
select_file = Select(title="Select OSM File:",  value='', options=osm_list)

def on_file_change(obj, attr, old, new):
    print "OSM File:", new
    global osmfile, osm_list
    osmfile = new
    #osm_list.append(osmfile)

select_file.on_change('value', on_file_change)
##############

##############
# radio group gmap
radio_group_gmap = RadioButtonGroup(labels=["GMap", "Blank"],
                                    #title='Background Map',
                                active=0, name='Select Plot Options')

def radio_group_handler_gmap(active):
    # need to update skipnodes too
    global gmap_background
    # detault to empty datasources
    if active == 0:
        gmap_background = True
    elif active == 1:
        gmap_background = False

radio_group_gmap.on_click(radio_group_handler_gmap)
##############

#############
def plot(location=None):

    (
        source_df, labels, colors,
        location_labels, xs_df, ys_df, scales_dict
        ) = load_database(location=location)
    print(location_labels)
    if location is None:
        location = location_labels[0]
    print(location)
    source = ColumnDataSource(source_df)
    xs = ColumnDataSource(xs_df)
    ys = ColumnDataSource(ys_df)
    scales = ColumnDataSource(scales_dict)

    bfig = bokeh_plot(source)

    labels_group = RadioGroup(
        labels=labels, active=None)

    active = location_labels.index(location)
    locations_group = RadioButtonGroup(
        labels=location_labels, active=active, name='locations_group'
        )

    x_scale, y_scale = scales_dict[location]

    x_slider = Slider(
        start=0.05, end=20, value=x_scale, step=0.05,
        title='x scale'
        )
    y_slider = Slider(
        start=0.001, end=0.2, value=y_scale, step=0.001,
        title='y scale'
        )

    with open('bokeh_js/bokeh_plot_onclick.js', 'r') as f:
        code = f.read()
    source.selected.js_on_change(
        'indices',
        CustomJS(
                args=dict(
                    s=source,
                    ls=labels,
                    r=labels_group,
                    ),
                code=code
                )
        )
    with open('bokeh_js/bokeh_label_onchange.js', 'r') as f:
        code = f.read()
    labels_group.js_on_change(
        'active',
        CustomJS(
            args=dict(
                s=source,
                r=labels_group,
                ls=labels,
                cs=colors),
            code=code
            )
        )

    with open('bokeh_js/bokeh_locations_onchange.js', 'r') as f:
        code = f.read()
    locations_group.js_on_change(
        'active',
        CustomJS(
            args=dict(
                s=source,
                r=locations_group,
                location_labels=location_labels,
                xs=xs,
                ys=ys,
                scales=scales,
                x_slider=x_slider,
                y_slider=y_slider,
                ),
            code=code
            )
        )

    with open('bokeh_js/bokeh_x_slider_onchange.js', 'r') as f:
        code = f.read()
    x_slider.js_on_change(
        'value',
        CustomJS(
            args=dict(
                s=source,
                r=locations_group,
                location_labels=location_labels,
                xs=xs,
                ys=ys,
                scales=scales,
                x_slider=x_slider,
                y_slider=y_slider,
                ),
            code=code
            )
        )
    with open('bokeh_js/bokeh_y_slider_onchange.js', 'r') as f:
        code = f.read()
    y_slider.js_on_change(
        'value',
        CustomJS(
            args=dict(
                s=source,
                r=locations_group,
                location_labels=location_labels,
                xs=xs,
                ys=ys,
                scales=scales,
                x_slider=x_slider,
                y_slider=y_slider,
                ),
            code=code
            )
        )

    return components(
        column(
            row(
                locations_group,
                ),
            row(
                x_slider,
                y_slider,
                ),
            row(
                bfig,
                column(
                    labels_group,
                    )
                )
            )
        )
Beispiel #34
0
# initialize controls
# slider for going though time
time_slider = Slider(title="time", name='time', value=pde_settings.t_init, start=pde_settings.t_min, end=pde_settings.t_max,
                     step=pde_settings.t_step)
time_slider.on_change('value', time_change)
# slider controlling spatial stepsize of the solver
h_slider = Slider(title="spatial meshwidth", name='spatial meshwidth', value=pde_settings.h_init, start=pde_settings.h_min,
                  end=pde_settings.h_max, step=pde_settings.h_step)
h_slider.on_change('value', mesh_change)
# slider controlling spatial stepsize of the solver
k_slider = Slider(title="temporal meshwidth", name='temporal meshwidth', value=pde_settings.k_init, start=pde_settings.k_min,
                  end=pde_settings.k_max, step=pde_settings.k_step)
k_slider.on_change('value', mesh_change)
# radiobuttons controlling pde type
pde_type = RadioButtonGroup(labels=['Heat', 'Wave'], active=0)
pde_type.on_change('active', pde_type_change)
# radiobuttons controlling solver type
solver_type = RadioButtonGroup(labels=['Explicit', 'Implicit'], active=0)
solver_type.on_change('active', mesh_change)
# text input for IC
initial_condition = TextInput(value=pde_settings.IC_init, title="initial condition")
initial_condition.on_change('value', initial_condition_change)

# initialize plot
toolset = "crosshair,pan,reset,resize,wheel_zoom,box_zoom"
# Generate a figure container
plot = Figure(plot_height=400,
              plot_width=400,
              tools=toolset,
              title="Time dependent PDEs",
Beispiel #35
0
class BokehGainMatching(Tool):
    name = "BokehGainMatching"
    description = "Interactively explore the steps in obtaining charge vs hv"

    input_path = Unicode('',
                         help='Path to the numpy array containing the '
                         'gain and hv').tag(config=True)

    aliases = Dict(dict(f='BokehGainMatching.input_path'))

    classes = List([])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self._active_pixel = None

        self.dead = Dead()
        self.charge = None
        self.charge_error = None
        self.hv = None

        self.n_hv = None
        self.n_pixels = None
        self.n_tmpix = 64
        self.modules = None
        self.tmpix = None
        self.n_tm = None

        self.m_pix = None
        self.c_pix = None
        self.m_tm = None
        self.c_tm = None
        self.m_tm2048 = None
        self.c_tm2048 = None

        self.p_camera_pix = None
        self.p_plotter_pix = None
        self.p_camera_tm = None
        self.p_plotter_tm = None

        self.w_view_radio = None

        self.layout = None

    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        arrays = np.load(self.input_path)
        self.charge = self.dead.mask2d(arrays['charge'])
        self.charge = np.ma.masked_where(self.charge <= 0, self.charge)
        self.charge_error = np.ma.array(arrays['charge_error'],
                                        mask=self.charge.mask)
        self.hv = arrays['rundesc']

        self.n_hv, self.n_pixels = self.charge.shape
        assert (self.n_hv == self.hv.size)

        geom = CameraGeometry.guess(*checm_pixel_pos * u.m,
                                    optical_foclen * u.m)
        self.modules = np.arange(self.n_pixels) // self.n_tmpix
        self.tmpix = np.arange(self.n_pixels) % self.n_tmpix
        self.n_tm = np.unique(self.modules).size

        # Init Plots
        self.p_camera_pix = Camera(self, "Gain Matching Pixels", geom)
        self.p_camera_tm = Camera(self, "Gain Matching TMs", geom)
        self.p_plotter_pix = Plotter(**kwargs)
        self.p_plotter_tm = Plotter(**kwargs)

    def start(self):
        # Overcomplicated method instead of just reshaping...
        # gain_modules = np.zeros((self.n_hv, self.n_tm, self.n_tmpix))
        # hv_r = np.arange(self.n_hv, dtype=np.int)[:, None]
        # hv_z = np.zeros(self.n_hv, dtype=np.int)[:, None]
        # tm_r = np.arange(self.n_tm, dtype=np.int)[None, :]
        # tm_z = np.zeros(self.n_tm, dtype=np.int)[None, :]
        # tmpix_r = np.arange(self.n_tmpix, dtype=np.int)[None, :]
        # tmpix_z = np.zeros(self.n_tmpix, dtype=np.int)[None, :]
        # hv_i = (hv_r + tm_z)[..., None] + tmpix_z
        # tm_i = (hv_z + tm_r)[..., None] + tmpix_z
        # tmpix_i = (hv_z + tm_z)[..., None] + tmpix_r
        # gain_rs = np.reshape(self.charge, (self.n_hv, self.n_tm, self.n_tmpix))
        # modules_rs = np.reshape(self.modules, (self.n_tm, self.n_tmpix))
        # tmpix_rs = np.reshape(self.tmpix, (self.n_tm, self.n_tmpix))
        # tm_j = hv_z[..., None] + modules_rs[None, ...]
        # tmpix_j = hv_z[..., None] + tmpix_rs[None, ...]
        # gain_modules[hv_i, tm_i, tmpix_i] = gain_rs[hv_i, tm_j, tmpix_j]
        # gain_modules_mean = np.mean(gain_modules, axis=2)

        shape = (self.n_hv, self.n_tm, self.n_tmpix)
        gain_tm = np.reshape(self.charge, shape)
        gain_error_tm = np.reshape(self.charge_error, shape)
        gain_tm_mean = np.mean(gain_tm, axis=2)
        gain_error_tm_mean = np.sqrt(np.sum(gain_error_tm**2, axis=2))

        self.m_pix = np.ma.zeros(self.n_pixels, fill_value=0)
        self.c_pix = np.ma.zeros(self.n_pixels, fill_value=0)
        self.m_tm = np.ma.zeros(self.n_tm, fill_value=0)
        self.c_tm = np.ma.zeros(self.n_tm, fill_value=0)
        p0 = [0, 5]
        bounds = (-np.inf, np.inf)  # ([-2000, -10], [2000, 10])
        for pix in range(self.n_pixels):
            x = self.hv[~self.charge.mask[:, pix]]
            y = self.charge[:, pix][~self.charge.mask[:, pix]]
            if x.size == 0:
                continue
            try:
                coeff, _ = curve_fit(
                    gain_func,
                    x,
                    y,
                    p0=p0,
                    bounds=bounds,
                    # sigma=y_err[:, pix],
                    # absolute_sigma=True
                )
                self.c_pix[pix], self.m_pix[pix] = coeff
            except RuntimeError:
                self.log.warning("Unable to fit pixel: {}".format(pix))
        for tm in range(self.n_tm):
            x = self.hv
            y = gain_tm_mean[:, tm]
            try:
                coeff, _ = curve_fit(
                    gain_func,
                    x,
                    y,
                    p0=p0,
                    bounds=bounds,
                    # sigma=y_err_tm[:, tm],
                    # absolute_sigma=True
                )
                self.c_tm[tm], self.m_tm[tm] = coeff
            except RuntimeError:
                self.log.warning("Unable to fit tm: {}".format(tm))

        self.m_tm2048 = self.m_tm[:, None] * np.ones((self.n_tm, self.n_tmpix))
        self.c_tm2048 = self.c_tm[:, None] * np.ones((self.n_tm, self.n_tmpix))

        self.m_pix = self.dead.mask1d(self.m_pix)
        self.c_pix = self.dead.mask1d(self.c_pix)
        self.m_tm2048 = self.dead.mask1d(self.m_tm2048.ravel())
        self.c_tm2048 = self.dead.mask1d(self.c_tm2048.ravel())

        # Setup Plots
        self.p_camera_pix.enable_pixel_picker()
        self.p_camera_pix.add_colorbar()
        self.p_camera_tm.enable_pixel_picker()
        self.p_camera_tm.add_colorbar()
        self.p_plotter_pix.create(self.hv, self.charge, self.charge_error,
                                  self.m_pix, self.c_pix)
        self.p_plotter_tm.create(self.hv, gain_tm_mean, gain_error_tm_mean,
                                 self.m_tm, self.c_tm)

        # Setup widgets
        self.create_view_radio_widget()
        self.set_camera_image()
        self.active_pixel = 0

        # Get bokeh layouts
        l_camera_pix = self.p_camera_pix.layout
        l_camera_tm = self.p_camera_tm.layout
        l_plotter_pix = self.p_plotter_pix.layout
        l_plotter_tm = self.p_plotter_tm.layout

        # Setup layout
        self.layout = layout([[self.w_view_radio],
                              [l_camera_pix, l_plotter_pix],
                              [l_camera_tm, l_plotter_tm]])

    def finish(self):
        curdoc().add_root(self.layout)
        curdoc().title = "Charge Vs HV"

        output_dir = dirname(self.input_path)
        output_path = join(output_dir, 'gain_matching_coeff.npz')
        np.savez(output_path,
                 alpha_pix=np.ma.filled(self.m_pix),
                 C_pix=np.ma.filled(self.c_pix),
                 alpha_tm=np.ma.filled(self.m_tm),
                 C_tm=np.ma.filled(self.c_tm))
        self.log.info("Numpy array saved to: {}".format(output_path))

    @property
    def active_pixel(self):
        return self._active_pixel

    @active_pixel.setter
    def active_pixel(self, val):
        if not self._active_pixel == val:
            self._active_pixel = val
            self.p_camera_pix.active_pixel = val
            self.p_camera_tm.active_pixel = val
            self.p_plotter_pix.active_pixel = val
            self.p_plotter_pix.fig.title.text = 'Pixel {}'.format(val)
            module = self.modules[val]
            self.p_plotter_tm.active_pixel = module
            self.p_plotter_tm.fig.title.text = 'TM {}'.format(module)

    def set_camera_image(self):
        if self.w_view_radio.active == 0:
            self.p_camera_pix.image = self.m_pix
            self.p_camera_tm.image = self.m_tm2048
            self.p_camera_pix.fig.title.text = 'Gain Matching Pixels (gradient)'
            self.p_camera_tm.fig.title.text = 'Gain Matching TMs (gradient)'
        elif self.w_view_radio.active == 1:
            self.p_camera_pix.image = self.c_pix
            self.p_camera_tm.image = self.c_tm2048
            self.p_camera_pix.fig.title.text = 'Gain Matching Pixels (intercept)'
            self.p_camera_tm.fig.title.text = 'Gain Matching TMs (intercept)'

    def create_view_radio_widget(self):
        self.w_view_radio = RadioButtonGroup(labels=["gradient", "intercept"],
                                             active=0)
        self.w_view_radio.on_click(self.on_view_radio_widget_click)

    def on_view_radio_widget_click(self, active):
        self.set_camera_image()
Beispiel #36
0
 def create_view_radio_widget(self):
     self.w_view_radio = RadioButtonGroup(labels=["gradient", "intercept"],
                                          active=0)
     self.w_view_radio.on_click(self.on_view_radio_widget_click)
Beispiel #37
0
         source=sources['static'],
         angle=pi/2,
         width=5,
         height=1,
         color=DARK_GRAY)


# create components --------------
title_div = Div(text='Revenue Impact of a 4% Surtax on Taxpayers with Adjusted Gross Income over $5 Million', height=30)

radio_group_text = Div(text="Include Additional Capital Gains Behavior.")
radio_group = RadioGroup(labels=["Without", "With"], active=0)
sources['radio_group'] = radio_group

elasticity_text = Div(text="Elasticity of Taxable Income")
elasticity_option = RadioButtonGroup(labels=sources['capital_gains_no'].data['labels'], active=0)
sources['elasticity_option'] = elasticity_option


# create callbacks ---------------
radio_group.callback = CustomJS(args=sources, code="""
  var capitalGainsType = radio_group.active === 0 ? capital_gains_no : capital_gains_yes;
  elasticity_option.set('labels', capitalGainsType.data.labels);

  while (elasticity_option.active > capitalGainsType.data.source_ids.length - 1) {
    elasticity_option.active = elasticity_option.active - 1;
  }

  ref.data = eval(capitalGainsType.data.source_ids[elasticity_option.active]).data;
  ref.trigger('change');
""")