def _ui(self) -> List[W.Widget]: cb = W.Checkbox(description="Treat Port Labels as Group") T.link((self, "treat_as_group"), (cb, "value")) return [cb]
def get_widgets(): layout = widgets.Layout(width='500px', height='auto') style = {'description_width': 'initial'} output_directory_chooser = widgets.interactive(g, i=widgets.Text( description="Output storage name: (country/location name, e.g. \"Guyana\")", placeholder = "output", style = style, layout =layout,)) k_chooser = widgets.interactive(g, i=widgets.Dropdown( options=[3,4,5,6,7,8,9,10], value=3, description='k, harmonic terms', style = style, layout= layout,)) freq_chooser = widgets.interactive(g, i=widgets.IntSlider( value = 365,min=1,max=365,step=1, description='freq, frequency of seasonal model (days)', style = style, layout=layout,)) trend_chooser = widgets.interactive(g, i=widgets.Checkbox( value = False, description= "add trend", style = style, layout=layout,)) hfrac_chooser = widgets.interactive(g, i=widgets.FloatSlider( value = 0.25,min=0,max=1,step=0.01, description= "Bandwith relative to sample size", style = style, layout=layout,)) level_chooser = widgets.interactive(g, i=widgets.SelectionSlider( description= "Significance level of the monitoring", options = [0.95 , 0.951, 0.952, 0.953, 0.954, 0.955, 0.956, 0.957, 0.958, 0.959, 0.96 , 0.961, 0.962, 0.963, 0.964, 0.965, 0.966, 0.967, 0.968, 0.969, 0.97 , 0.971, 0.972, 0.973, 0.974, 0.975, 0.976, 0.977, 0.978, 0.979, 0.98 , 0.981, 0.982, 0.983, 0.984, 0.985, 0.986, 0.987, 0.988, 0.989, 0.99 , 0.991, 0.992, 0.993, 0.994, 0.995, 0.996, 0.997, 0.998, 0.999], style = style, layout = layout, )) backend_chooser = widgets.interactive(g, i=widgets.Dropdown( options=['opencl','python'], value='opencl', description='backend', style = style, layout = layout,)) load_chooser = widgets.interactive(g, i=widgets.Checkbox( value = False, description= "load tiles", style = style, layout=layout,)) block_size_chooser = widgets.interactive(g, i=widgets.Dropdown( options=[128,256,512,1024], value=512, description='block size, bigger is generally faster, but may result in memory issues', style = style, layout = layout,)) plot_display_data_chooser = widgets.interactive(g, i=widgets.Dropdown( options=['magnitudes','magnitudes_negative'], value='magnitudes', description='data to plot', style = style, layout = layout,)) return(output_directory_chooser,k_chooser,freq_chooser,trend_chooser,hfrac_chooser,level_chooser,backend_chooser, load_chooser, block_size_chooser, plot_display_data_chooser)
def ts_widget( df_traffic, aggregation_columns=None, time_column="TIME", value_column="BW", top_flows_to_show=5, align_vertically=True, ): """ Returns a Box widget containing various widgets used to depict and explore a time series data frame. the widget contains: - A HTML and a figure widget used to depict the data of the aggregated data frame. - A set of check boxes to allow users to select the characteristics shown that are depicted in the figure and table. - An update button used to refresh the table and graph widgets when the user changes the checkboxes. - A Text box which contains information on the state of the widget (E.g. Processing, Updated, etc.) The graph and the table are placed horizontally if the align_vertically is False. If selected, a horizontal widget is more compact, but cannot show that much information. The update function of the update button is a nested function and uses non-local variables. """ # Get the list of aggregation columns. It defaults to any non-time, non-value column in the df. if aggregation_columns is None: aggregation_columns = list( set(df_traffic.columns) - {time_column, value_column} ) aggregation_columns = sorted(aggregation_columns) else: aggregation_columns = list(aggregation_columns) # Defines the layout objects that we will use depending on the widget layout (horizontal or vertical). # The values here defined were obtained with trial and error :) # The only fancy thing here is the use of a variable traffic_traph_box_widget to define whether the # graph and table box is vertical or horizontal if align_vertically: all_widget_height = "1050px" all_widget_width = "1000px" table_height = "400px" table_width = all_widget_width graph_table_height = "950px" traffic_traph_box_widget = widgets.VBox figure_size = [9.1, 4.8] else: all_widget_height = "700px" all_widget_width = "1000px" graph_table_height = "650px" table_height = "600px" table_width = "500px" traffic_traph_box_widget = widgets.HBox figure_size = [4.8, 4.8] # define the main widget. ts_main_widget = widgets.VBox( layout=widgets.Layout(height=all_widget_height, width=all_widget_width) ) # the main widget is formed by a control box and the graph_table_box. # The control box which contains the check boxes, update butoon and information box. # the graph_and table box is self-described. control_information_box = widgets.VBox() graph_table_box = traffic_traph_box_widget( layout=widgets.Layout(height=graph_table_height, width=all_widget_width) ) ts_main_widget.children = (control_information_box, graph_table_box) # Control box # the control box is itself formed by: # * another box holding the checkboxes # * the update button # * The information text # The first two elements are horitzontally alligned using a box called cbx_update_box cbx_update_box = widgets.HBox() information_widget = widgets.Text(disabled=True, description="State:") control_information_box.children = (cbx_update_box, information_widget) # the cbx is itself divided into the check_box box and the update button. # I place the check boxes into their own box to let them have a box space. check_boxes = {} check_boxes_box = widgets.HBox( layout=widgets.Layout(overflow_x="scroll", height="50px", width="850px") ) for level in aggregation_columns: # Create check boxes for each TS characteristic column. this_checkbox = widgets.Checkbox(description=level) check_boxes_box.children = check_boxes_box.children + (this_checkbox,) this_checkbox.value = False check_boxes[level] = this_checkbox # Update button refresh_button = widgets.Button(description="Update") cbx_update_box.children = (check_boxes_box, refresh_button) # Now, let us finish with the graph and table box. fig_prefix_distribution, ax_prefix_distribution = plt.subplots() this_canvas = fig_prefix_distribution.canvas # this_canvas = Canvas(fig_prefix_distribution) fig_prefix_distribution.set_size_inches(figure_size) this_canvas.figure.set_label("{}".format("Figure")) table_box_layout = widgets.Layout( overflow_x="scroll", overflow_y="scroll", # border='3px solid black', width=table_width, height=table_height, flex_direction="row", display="flex", ) table_widget = widgets.HTML() table_box_widget = widgets.VBox(children=(table_widget,), layout=table_box_layout) graph_table_box.children = (this_canvas, table_box_widget) # Finally, define the update function and assign it to the butotn def update_compound_widget(caller=None): """ The update function checks the aggrupation characteristics, calculates the resulting df, and updates the table and graph. """ information_widget.value = "Updating..." # find the aggregation level using the check boxes aggregation_level = [] for level in check_boxes: check_box = check_boxes[level] if check_box.value: aggregation_level.append(level) table_df, graph_df = process_df_for_widget( df_traffic, aggregation_columns=aggregation_level, value_column=value_column, time_column=time_column, top_flows_to_show=top_flows_to_show, ) ax_prefix_distribution.clear() aggregation_column_name = next( iter(set(graph_df.columns) - {time_column, value_column}) ) graph_df.plot.area(ax=ax_prefix_distribution) ax_prefix_distribution.legend_.remove() ax_prefix_distribution.legend( bbox_to_anchor=(0.1, 0.85, 0.9, 0.105), loc=3, ncol=2, mode=None, borderaxespad=0.1, fontsize=8, ) ax_prefix_distribution.set_ylabel(value_column) table_widget.value = ( table_df.style.set_table_attributes('class="table"') .set_table_styles([hover()]) .render() ) information_widget.value = "Redrawing..." plt.draw_all() information_widget.value = "Done" refresh_button.on_click(update_compound_widget) return ts_main_widget
] tabs = widgets.Tab(children=[ about_tab.tab, config_tab.tab, microenv_tab.tab, user_tab.tab, sub.tab, animate_tab.tab ], _titles={i: t for i, t in enumerate(titles)}, layout=tab_layout) homedir = os.getcwd() tool_title = widgets.Label(r'\(\textbf{pc4nanobio}\)') if nanoHUB_flag or hublib_flag: # define this, but don't use (yet) remote_cb = widgets.Checkbox( indent=False, value=False, description='Submit as Batch Job to Clusters/Grid') top_row = widgets.HBox(children=[read_config, tool_title]) gui = widgets.VBox(children=[top_row, tabs, run_button.w]) fill_gui_params(read_config.options['DEFAULT']) else: top_row = widgets.HBox(children=[tool_title]) gui = widgets.VBox(children=[top_row, tabs, run_button]) fill_gui_params("data/PhysiCell_settings.xml") # pass in (relative) directory where output data is located output_dir = "tmpdir" # svg.update(output_dir) sub.update_dropdown_fields(
def init_metrics_ui(self): """构建基础env widget ui return widgets.VBox""" # 回测时间模式 self.metrics_mode = widgets.RadioButtons( options={u'考虑初始资金+标尺大盘对比': 0, u'不考虑初始资金+不对比标尺': 1}, value=0, description=u'度量模式:', disabled=False ) self.metrics_out_put = widgets.RadioButtons( options={u'只输出交易单:orders_pd': 0, u'只输出行为单:action_pd': 1, u'只输出资金单:capital_pd': 2, u'输出交易单,行为单,资金单': 3}, value=0, description=u'输出对象:', disabled=False ) out_put_display_max_label1 = widgets.Label(u'输出显示最大行列数,最大100行,100列', layout=widgets.Layout(width='300px', align_items='stretch')) out_put_display_max_label2 = widgets.Label(u'如需查看更多输出表单,请选择保存输出至文件', layout=widgets.Layout(width='300px', align_items='stretch')) self.out_put_display_max_rows = widgets.IntSlider( value=50, min=1, max=100, step=1, description=u'行数', disabled=False, orientation='horizontal', readout=True, readout_format='d' ) self.out_put_display_max_columns = widgets.IntSlider( value=50, min=1, max=100, step=1, description=u'列数', disabled=False, orientation='horizontal', readout=True, readout_format='d' ) out_put_display = widgets.VBox([out_put_display_max_label1, out_put_display_max_label2, self.out_put_display_max_rows, self.out_put_display_max_columns]) save_out_put_lable = widgets.Label(u'是否保存交易单,行为单,资金单到文件', layout=widgets.Layout(width='300px', align_items='stretch')) save_out_put_lable2 = widgets.Label(u'路径:{}'.format(os.path.join(ABuEnv.g_project_data_dir, 'out_put')), layout=widgets.Layout(width='300px', align_items='stretch')) self.save_out_put = widgets.Checkbox( value=False, description=u'保存输出', disabled=False, ) save_out_put = widgets.VBox([save_out_put_lable, save_out_put_lable2, self.save_out_put]) accordion = widgets.Accordion() accordion.children = [widgets.VBox([self.metrics_mode, self.metrics_out_put, out_put_display, save_out_put])] accordion.set_title(0, u'回测度量结果设置') accordion_shut(accordion) return accordion
def preProcessingInterface(video_dropdown, json_dropdown, data_dropdown, frame_n): persons = wg.RadioButtons(options=['Biggest', 'Unsorted', 'All'], value='Biggest', rows=3, description='Choose:', disabled=False) custom = wg.BoundedIntText(value=0, min=0, max=10, step=1, description='Person:', disabled=False, layout=wg.Layout(display='flex', flex_flow='line', align_items='flex-start', justify_content='flex-start', width='90%')) joint_pose = wg.RadioButtons( options=['Sagittal Left', 'Sagittal Right', 'Whole Body'], value='Sagittal Left', rows=4, description='Pose: ', disabled=False, layout=wg.Layout(display='flex', flex_flow='line', width='90%')) frame_slider = wg.IntSlider() preprocess_vid = wg.Button(description='Pre Process Video') paf_score_th = wg.FloatText(value=0.1, step=0.1, description='PAF Thres:', layout=wg.Layout(display='flex', flex_flow='line', align_items='flex-start', justify_content='flex-start', width='90%')) conf_th = wg.FloatText(value=0.7, step=0.1, description='Conf. Thres:', layout=wg.Layout(display='flex', flex_flow='line', align_items='flex-start', justify_content='flex-start', width='90%')) n_interp_samples = wg.IntText(value=10, description='Samples Interpolated:', layout=wg.Layout( display='flex', flex_flow='line', align_items='flex-start', justify_content='flex-start', width='90%')) joint_n = wg.Dropdown(options=keypoints_mapping, value='Nose', description='Joint:', disabled=False) threshold = wg.FloatText(value=0.1, step=0.1, description='Threshold:', layout=wg.Layout(display='flex', flex_flow='line', align_items='flex-start', justify_content='flex-start', width='80%')) alpha = wg.FloatText(value=0.6, step=0.1, description='Transparency:', layout=wg.Layout(display='flex', flex_flow='line', align_items='flex-start', justify_content='flex-start', width='80%')) show_point = wg.Checkbox(value=False, description='Show Point', disabled=False, layout=wg.Layout(display='flex', flex_flow='line', align_items='flex-start', justify_content='flex-start', width='80%')) binary = wg.Checkbox(value=False, description='Binary', disabled=False, layout=wg.Layout(display='flex', flex_flow='line', align_items='flex-start', justify_content='flex-start', width='80%')) wg.jslink((frame_n, 'value'), (frame_slider, 'value')) show_heatmap = wg.ToggleButtons( options=['Keypoints', 'Heatmap'], value='Keypoints', disabled=False, button_style='', # 'success', 'info', 'warning', 'danger' or '' tooltips=['Show keypoints', 'Show heatmap'], # icons=['check'] * 3 ) summary = wg.Textarea(value='', placeholder='description', description='Summary:', disabled=False) output_name = wg.Text(value='', placeholder='File output name', description='Output:', disabled=False) def onPreProcessClicked(b): if (video_dropdown.value == "None"): print("Choose a video") return video_path = videos_dir + video_dropdown.value video_name = (video_dropdown.value).split(sep='.')[0] file_dir = data_dir + video_name + '/' if not os.path.exists(file_dir): os.makedirs(file_dir) files_list = os.listdir(file_dir) json_list = ["None"] for names in files_list: if names.endswith(".json"): json_list.append(names) mp4_list = ["None"] for names in files_list: if names.endswith(".mp4"): mp4_list.append(names) data_list = ["None"] for names in files_list: if names.endswith(".data"): data_list.append(names) json_dropdown.options = json_list mp4_dropdown.options = mp4_list data_dropdown.options = data_list saveJointFile(video_dropdown.value, json_dropdown.value, output_name.value, threshold.value, n_interp_samples.value, paf_score_th.value, conf_th.value, summary.value) preprocess_vid.on_click(onPreProcessClicked) ht_vbox_1 = wg.VBox([joint_n, show_point, alpha, binary, threshold], layout=wg.Layout(display='flex', flex_flow='line', align_items='flex-start', justify_content='flex-start')) hbox_play = wg.HBox([video_dropdown, json_dropdown, frame_n, frame_slider]) vbox_params = wg.VBox([paf_score_th, conf_th, n_interp_samples]) vbox_per = wg.VBox([persons, custom, joint_pose], layout=wg.Layout(display='flex', flex_flow='column', align_items='flex-start', justify_content='flex-start')) def preprocessView(show_heatmap, video_name, file_name, persons, custom, joint_pose, joint_n, alpha, binary, threshold, n_interp_samples, paf_score_th, conf_th, frame_n, show_point): if (show_heatmap == 'Keypoints'): keypointsFromJSON(video_name, file_name, persons, custom, joint_pose, threshold, n_interp_samples, paf_score_th, conf_th, frame_n) else: heatmapFromJSON(video_name, file_name, keypoints_mapping.index(joint_n), threshold, alpha, binary, n_interp_samples, paf_score_th, conf_th, frame_n, show_point) out_res = wg.interactive_output( preprocessView, { "show_heatmap": show_heatmap, "video_name": video_dropdown, "file_name": json_dropdown, "persons": persons, "custom": custom, "joint_pose": joint_pose, "joint_n": joint_n, "alpha": alpha, "binary": binary, "threshold": threshold, "n_interp_samples": n_interp_samples, "paf_score_th": paf_score_th, "conf_th": conf_th, "frame_n": frame_n, "show_point": show_point }) tabs = ['Choose Person', 'Heatmap', 'PAF Parameters'] children = [] children.append(vbox_per) children.append(ht_vbox_1) children.append(vbox_params) tab = wg.Tab() tab.children = children for i in range(len(children)): tab.set_title(i, tabs[i]) hbox_out = wg.HBox([output_name, summary, preprocess_vid]) vbox_res = wg.VBox([show_heatmap, tab]) hbox_res = wg.HBox([out_res, vbox_res]) vbox_vid = wg.VBox([hbox_play, hbox_res, hbox_out]) return vbox_vid
def dashboard_one(): style = {'description_width': 'initial'} print('Folder path is choosen in the info tab') dashboard_one.datain = widgets.ToggleButtons( options=['Folder'], description='Data In:', disabled=False, button_style='success', # 'success', 'info', 'warning', 'danger' or '' tooltips=[ 'Data in folder', 'Data in csv format - NOT ACTIVE', 'Data in dataframe - NOT ACTIVE' ], ) dashboard_one.norma = widgets.ToggleButtons( options=['Imagenet', 'Custom', 'Cifar', 'Mnist'], description='Normalization:', disabled=False, button_style='info', # 'success', 'info', 'warning', 'danger' or '' tooltips=[ 'Imagenet stats', 'Create your own', 'Cifar stats', 'Mnist stats' ], style=style) dashboard_one.archi = widgets.ToggleButtons( options=[ 'alexnet', 'BasicBlock', 'densenet121', 'densenet161', 'densenet169', 'densenet201', 'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'squeezenet1_0', 'squeezenet1_1', 'vgg16_bn', 'vgg19_bn', 'xresnet18', 'xresnet34', 'xresnet50', 'xresnet101', 'xresnet152' ], description='Architecture:', disabled=False, button_style='', # 'success', 'info', 'warning', 'danger' or '' tooltips=[], ) layout = widgets.Layout(width='auto', height='40px') #set width and height xres_text = widgets.Button( description= 'FOR Xresnet models: Are not pretrained so have to UNCHECK Pretrain box to avoid errors.', disabled=True, display='flex', flex_flow='column', align_items='stretch', layout=layout) dashboard_one.pretrain_check = widgets.Checkbox( options=['Yes', "No"], description='Pretrained:', disabled=False, value=True, box_style='success', button_style= 'lightgreen', # 'success', 'info', 'warning', 'danger' or '' tooltips=[ 'Default: Checked = use pretrained weights, Unchecked = No pretrained weights' ], ) layout = { 'width': '90%', 'height': '50px', 'border': 'solid', 'fontcolor': 'lightgreen' } layout_two = { 'width': '100%', 'height': '200px', 'border': 'solid', 'fontcolor': 'lightgreen' } style_green = { 'handle_color': 'green', 'readout_color': 'red', 'slider_color': 'blue' } style_blue = { 'handle_color': 'blue', 'readout_color': 'red', 'slider_color': 'blue' } dashboard_one.f = widgets.FloatSlider(min=8, max=64, step=8, value=32, continuous_update=False, layout=layout, style=style_green, description="Batch size") dashboard_one.m = widgets.FloatSlider(min=0, max=360, step=16, value=128, continuous_update=False, layout=layout, style=style_green, description='Image size') display(dashboard_one.datain, dashboard_one.norma, dashboard_one.archi, xres_text, dashboard_one.pretrain_check, dashboard_one.f, dashboard_one.m)
def fitUI(out, DEFAULTS): # Fit the data layout = inputBox() layout.add_child( "model", widgets.Dropdown( description="Tapqir model", value="cosmos", options=avail_models, style={"description_width": "initial"}, ), ) layout.add_child( "cuda", widgets.Checkbox( value=DEFAULTS["cuda"], description="Run computations on GPU?", style={"description_width": "initial"}, ), ) layout.add_child( "nbatch_size", widgets.IntText( value=DEFAULTS["nbatch-size"], description="AOI batch size", style={"description_width": "initial"}, ), ) layout.add_child( "fbatch_size", widgets.IntText( value=DEFAULTS["fbatch-size"], description="Frame batch size", style={"description_width": "initial"}, ), ) layout.add_child( "learning_rate", widgets.FloatText( value=DEFAULTS["learning-rate"], description="Learning rate", style={"description_width": "initial"}, ), ) layout.add_child( "num_iter", widgets.IntText( description="Number of iterations", style={"description_width": "initial"}, ), ) layout.add_child( "matlab", widgets.Checkbox( value=DEFAULTS["matlab"], description="Save parameters in matlab format?", style={"description_width": "initial"}, ), ) priors = inputBox() for name, value in DEFAULTS["priors"].items(): priors.add_child( name, widgets.FloatText( value=value, description=name, style={"description_width": "initial"} ), ) layout.add_child("priors", widgets.Accordion(children=[priors])) layout["priors"].set_title(0, "Priors") # Fit the data layout.add_child("fit", widgets.Button(description="Fit the data")) # Callbacks layout["fit"].on_click(partial(fitCmd, layout=layout, out=out, DEFAULTS=DEFAULTS)) return layout
def showCmd(b, layout, out): layout.toggle_hide(names=("show",)) params, summary = layout["view"].children controls, view, fov_controls = params.children with out: logger.info("Loading results ...") show_ret = show( **layout.kwargs, n=0, f1=None, f2=None, gui=True, ) if show_ret == 1: out.clear_output(wait=True) return model, fig, item, ax, fov = show_ret with view: plt.show() with summary: display(model.summary) n = widgets.BoundedIntText( value=0, min=0, max=model.data.Nt - 1, description=f"AOI (0-{model.data.Nt-1})", style={"description_width": "initial"}, layout={"width": "150px"}, ) n_counter = widgets.BoundedIntText( min=0, max=model.data.Nt - 1, ) f1_text = widgets.BoundedIntText( value=0, min=0, max=model.data.F - 15, step=1, description=f"Frame (0-{model.data.F-1})", style={"description_width": "initial"}, layout={"width": "300px"}, ) f1_counter = widgets.BoundedIntText( min=0, max=model.data.F - 15, ) f1_slider = widgets.IntSlider( value=0, min=0, max=model.data.F - 15, step=1, continuous_update=True, readout=False, readout_format="d", layout={"width": "210px"}, ) f1_box = widgets.VBox(layout=widgets.Layout(width="310px")) f1_incr = widgets.Button(description="+15", layout=widgets.Layout(width="45px")) f1_decr = widgets.Button(description="-15", layout=widgets.Layout(width="45px")) f1_controls = widgets.HBox( children=[f1_decr, f1_slider, f1_incr], layout=widgets.Layout(width="305px"), ) f1_box.children = [f1_text, f1_controls] widgets.jslink((f1_slider, "value"), (f1_text, "value")) widgets.jslink((f1_text, "value"), (f1_counter, "value")) widgets.jslink((n, "value"), (n_counter, "value")) zoom = widgets.Checkbox( value=False, description="Zoom out frames ['z']", indent=False, layout={"width": "240px"}, ) labels = widgets.Checkbox( value=False, description="Show labels", indent=False, layout={"width": "240px"}, ) targets = widgets.Checkbox( value=False, description="Show target location ['o']", indent=False, layout={"width": "240px"}, ) nonspecific = widgets.Checkbox( value=True, description="Show non-specific spots ['n']", indent=False, layout={"width": "240px"}, ) exclude_aoi = widgets.Checkbox( value=not model.data.mask[0], description="Exclude AOI from analysis ['e']", indent=False, layout={"width": "240px"}, ) checkboxes = widgets.VBox(layout=widgets.Layout(width="250px")) if model.data.labels is None: checkboxes.children = [zoom, targets, nonspecific, exclude_aoi] else: checkboxes.children = [zoom, targets, nonspecific, exclude_aoi, labels] controls.children = [n, f1_box, checkboxes] # fov controls if fov is not None: for dtype in fov.dtypes: fov_controls.add_child( dtype, widgets.Checkbox(value=True, description=f"Show {dtype} AOIs") ) fov_controls[dtype].observe( partial( showAOIs, fov=fov, n=n_counter, item=item, fig=fig, ), names="value", ) fov_controls.add_child("save_data", widgets.Button(description="Save data")) # callbacks n.observe( partial( updateParams, f1=f1_slider, model=model, fig=fig, item=item, ax=ax, targets=targets, nonspecific=nonspecific, labels=labels, fov_controls=fov_controls, exclude_aoi=exclude_aoi, show_fov=layout["show_fov"], ), names="value", ) f1_slider.observe( partial( updateRange, n=n, model=model, fig=fig, item=item, ax=ax, zoom=zoom, targets=targets, fov=fov, ), names="value", ) f1_incr.on_click(partial(incrementRange, x=15, counter=f1_counter)) f1_decr.on_click(partial(incrementRange, x=-15, counter=f1_counter)) zoom.observe( partial(zoomOut, f1=f1_slider, model=model, fig=fig, item=item, ax=ax), names="value", ) labels.observe( partial(showLabels, n=n_counter, model=model, item=item, ax=ax), names="value", ) targets.observe( partial( showTargets, n=n_counter, f1=f1_slider, model=model, item=item, ax=ax, ), names="value", ) nonspecific.observe( partial(showNonspecific, n=n_counter, model=model, item=item, ax=ax), names="value", ) exclude_aoi.observe( partial( excludeAOI, n=n_counter, model=model, item=item, show_fov=layout["show_fov"] ), names="value", ) fov_controls["save_data"].on_click( partial(saveData, data=model.data, path=model.path, out=out) ) # mouse click UI fig.canvas.mpl_connect( "button_release_event", partial(onFrameClick, counter=f1_counter), ) # key press UI d = Event(source=layout, watched_events=["keyup"], prevent_default_action=True) d.on_dom_event( partial( onKeyPress, n=n_counter, f1=f1_counter, zoom=zoom, targets=targets, nonspecific=nonspecific, exclude_aoi=exclude_aoi, ) ) with out: logger.info("Loading results: Done") out.clear_output(wait=True) b.disabled = True
options=[ '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23' ], value='12', layout={'width': '150px'}, description='Hour:', disabled=False, ) # In[6]: #Not Logged In Users Checkbox global aLogins aLogins = widgets.Checkbox(value=False, description='Include Unverified Users', disabled=False) # In[7]: #Review users according to date and hour selected def on_users(b): global aDates global aHours global data2 global data3 global data4 global df1 global df2 global df3
def glimpseUI(out, DEFAULTS): """ Glimpse command layout. """ layout = inputBox() layout.add_child( "dataset", widgets.Text( value=DEFAULTS["dataset"], description="Dataset name", style={"description_width": "initial"}, ), ) layout.add_child( "P", widgets.BoundedIntText( value=DEFAULTS["P"], min=5, max=50, description="AOI image size", style={"description_width": "initial"}, ), ) layout.add_child( "offset_x", widgets.IntText( value=DEFAULTS["offset-x"], description="Offset region top-left corner (x-axis)", style={"description_width": "initial"}, ), ) layout.add_child( "offset_y", widgets.IntText( value=DEFAULTS["offset-y"], description="Offset region top-left corner (y-axis)", style={"description_width": "initial"}, ), ) layout.add_child( "offset_P", widgets.IntText( value=DEFAULTS["offset-P"], description="Offset region size", style={"description_width": "initial"}, ), ) layout.add_child( "bin_size", widgets.BoundedIntText( value=DEFAULTS["bin-size"], min=1, max=21, step=2, description="Offset histogram bin size (odd number)", style={"description_width": "initial"}, ), ) layout.add_child( "frame_range", widgets.Checkbox( value=DEFAULTS["frame-range"], description="Specify frame range?", indent=False, ), ) layout.add_child( "frame_start", widgets.IntText( value=DEFAULTS["frame-start"], description="Starting frame", style={"description_width": "initial"}, ), hide=not DEFAULTS["frame-range"], ) layout.add_child( "frame_end", widgets.IntText( value=DEFAULTS["frame-end"], description="Ending frame", style={"description_width": "initial"}, ), hide=not DEFAULTS["frame-range"], ) layout.add_child( "use_offtarget", widgets.Checkbox( value=DEFAULTS["use-offtarget"], description="Use off-target AOI locations?", indent=False, ), ) layout.add_child( "num_channels", widgets.BoundedIntText( value=DEFAULTS["num-channels"], min=1, max=4, step=1, description="Number of color channels", style={"description_width": "initial"}, ), ) layout.add_child("channels", widgets.Tab()) channelUI( layout["num_channels"].value, layout["channels"], layout["use_offtarget"], DEFAULTS, ) layout.add_child("extract_aois", widgets.Button(description="Extract AOIs")) # Callbacks layout["num_channels"].observe( partial( channelUI, channelTabs=layout["channels"], useOfftarget=layout["use_offtarget"], DEFAULTS=DEFAULTS, ), names="value", ) layout["use_offtarget"].observe( partial(toggleUseOffTarget, channelTabs=layout["channels"]), names="value", ) layout["extract_aois"].on_click(partial(glimpseCmd, layout=layout, out=out)) layout["frame_range"].observe( partial(layout.toggle_hide, names=("frame_start", "frame_end")), names="value", ) return layout
def interactive_scatterplot(simdata, table, scores=None, kws_scatterplot=None, kws_distplot=None, kws_hexbin=None): """Interactive scatterplot of a score vs. another score. Scores for each axis are selectable using dropdown menus. Options are provided to draw a 2D histogram and lines indicating the score thresholds. From a jupyter notebook it may be necessary to use the magic command '%matplotlib notebook' for a correct visualization. Args: simdata: simdata.SimData object containing the data. table: Table to use. scores: List of scores to plot. If None, all of the scores in simdata are used. kws_scatterplot: Keywords passed to the seaborn.scatterplot function. kws_distplot: Keywords passed to the seaborn.distplot function, which draws the marginal distributions. kws_hexbin: Keywords pased to the matplotlib.pyplot.hexbin function, which draws the 2D histogram. """ if scores is None: score_list = simdata.get_score_list() else: # Validate the scores. score_list = [simdata.get_score_info(sc)['name'] for sc in scores] if kws_scatterplot is None: kws_scatterplot = {} if kws_distplot is None: kws_distplot = {'kde': False, 'bins': 50} else: if 'kde' not in kws_distplot: kws_distplot['kde'] = False if 'bins' not in kws_distplot: kws_distplot['bins'] = 50 if kws_hexbin is None: kws_hexbin = {'gridsize': (20, 20), 'cmap': 'Purples'} else: if 'gridsize' not in kws_hexbin: kws_hexbin['gridsize'] = (20, 20) if 'cmap' not in kws_hexbin: kws_hexbin['cmap'] = 'Purples' hist_2d_cb = widgets.Checkbox(value=False, description='2D histogram', disabled=False) score_thr_cb = widgets.Checkbox(value=False, description='Score threshold', disabled=False) horiz = widgets.Dropdown(options=score_list, value=score_list[0], description='X axis score:', disabled=False, layout=widgets.Layout(width='6in')) vert = widgets.Dropdown(options=score_list, value=score_list[0], description='Y axis score:', disabled=False, layout=widgets.Layout(width='6in')) display(widgets.HBox([hist_2d_cb, score_thr_cb])) display(vert) x0_var_data = simdata.get_score_data(score_list[0], table, verbose=False).data y0_var_data = simdata.get_score_data(score_list[0], table, verbose=False).data g = sns.jointplot(x0_var_data, y0_var_data, space=0, ratio=7) g.ax_joint.set_xlabel(score_list[0]) g.ax_joint.set_ylabel(score_list[0]) plt.tight_layout() display(horiz) def handle_change(): """Handles changes in the interface.""" score_x = horiz.value score_y = vert.value x_var_data = simdata.get_score_data(score_x, table, verbose=False).data y_var_data = simdata.get_score_data(score_y, table, verbose=False).data g.ax_joint.clear() g.ax_marg_x.clear() g.ax_marg_y.clear() g.x = x_var_data g.y = y_var_data if hist_2d_cb.value: g.plot_joint(plt.hexbin, **kws_hexbin) else: g.plot_joint(sns.scatterplot, **kws_scatterplot) g.plot_marginals(sns.distplot, **kws_distplot) g.ax_joint.set_xlabel(score_x) g.ax_joint.set_ylabel(score_y) g.ax_marg_x.set_yticks([]) g.ax_marg_y.set_xticks([]) g.ax_marg_x.grid('off') g.ax_marg_y.grid('off') if score_thr_cb.value: g.ax_joint.axhline(_SCORE_THR, linewidth=2, color='k', linestyle='--') g.ax_joint.axvline(_SCORE_THR, linewidth=2, color='k', linestyle='--') g.ax_joint.grid(True) g.fig.show() horiz.on_trait_change(handle_change, name='value') vert.on_trait_change(handle_change, name='value') hist_2d_cb.on_trait_change(handle_change, name='value') score_thr_cb.on_trait_change(handle_change, name='value') handle_change()
def interactive_cdf(simdata): """Plots score CDFs. Allows selection of score and table combinations and dsisplays the CDFs of the selected pairs. Args: simdata: simdata.SimData object containing the data. """ score_list = [ '{0}: {1}'.format(idx, sn) for (idx, sn) in enumerate(simdata.get_score_list()) ] table_list = [ '{0}: {1}'.format(idx, tn) for (idx, tn) in enumerate(simdata.get_table_list()) ] score_select = widgets.Select(options=score_list, value=None, rows=10, description='Scores:', layout=widgets.Layout(width='45%'), disabled=False) table_select = widgets.Select(options=table_list, value=None, rows=10, description='Tables:', layout=widgets.Layout(width='45%'), disabled=False) score_table_select = widgets.Select(options=[], rows=5, description='Score+table:', layout=widgets.Layout(width='90%'), disabled=False) add_button = widgets.Button(description='Add', disabled=False, button_style='success', layout=widgets.Layout(width='20%'), tooltip='Add score/table combination', icon='fa-arrow-down') del_button = widgets.Button(description='Remove', disabled=False, button_style='warning', layout=widgets.Layout(width='20%'), tooltip='Remove score/table combination', icon='fa-arrow-up') reset_button = widgets.Button(description='Reset', disabled=False, button_style='danger', layout=widgets.Layout(width='10%'), tooltip='Reset', icon='fa-home') score_thr_cb = widgets.Checkbox(value=False, description='Score threshold', disabled=False) enable_readouts_cb = widgets.Checkbox(value=False, description='Enable CDF reading', disabled=False) cdf_slider = widgets.FloatSlider(min=0, max=1, step=0.001, value=0, orientation='vertical', continuous_update=False, readout_format='.3f', layout=widgets.Layout( height='3.5in', align_self='center')) cdf_readout = widgets.Select(options=[], value=None, rows=10, description='', layout=widgets.Layout(width='1in', align_self='center'), disabled=True) out = widgets.Output(layout={'width': '7in', 'height': '5in'}) # Display the widgets. display( widgets.VBox([ widgets.HBox([score_select, table_select]), widgets.HBox([add_button, del_button, reset_button], layout=widgets.Layout(justify_content='center')), score_table_select, widgets.HBox([score_thr_cb, enable_readouts_cb]) ])) display(widgets.HBox([out, cdf_slider, cdf_readout])) # Create empty plot. with out: fig, _ = plt.subplots() fig.set_size_inches(6, 4) plt.xlabel('Score values') plt.ylabel('CDF') plt.tight_layout() plt.show() var_list = [] def plot_ecdfs(): """Plot ecdfs.""" with out: clear_output(wait=True) fig, ax = plt.subplots() fig.set_size_inches(6, 4) plt.xlabel('Score values') plt.ylabel('CDF') cdf_readout.options = [] cdf_readout.value = None if not var_list: return with out: cdf_list = [] for i, var in enumerate(var_list): vardata = var[2] xs, ecdf_mean_bounds = vardata.ecdf_data() ax.plot(xs, ecdf_mean_bounds[0, :], label=str(i + 1)) if enable_readouts_cb.value: cdf_list.append('{0} : {1:.3f}'.format( i + 1, vardata.percentile(cdf_slider.value * 100.).mean)) if score_thr_cb.value: ax.axvline(_SCORE_THR, linewidth=2, color='k', linestyle='--') if enable_readouts_cb.value: ax.axhline(cdf_slider.value, linewidth=2, color='k', linestyle='--') cdf_readout.options = cdf_list cdf_readout.value = None plt.xlabel(vardata.variable_info['name']) plt.ylabel('CDF') plt.title(vardata.table_info['title']) plt.yticks(np.linspace(0, 1, 11)) plt.ylim(0, 1) plt.legend() ax.grid(True) plt.show() def refresh_score_table(): """Refresh data in score table select.""" var_str_list = [] for i, var in enumerate(var_list): score_name = simdata.get_score_info(var[0], verbose=False)['name'] table_name = simdata.get_table_info(var[1], verbose=False)['title'] var_name = '{0} - "{1}" : "{2}"'.format(i + 1, score_name, table_name) var_str_list.append(var_name) score_table_select.options = var_str_list score_table_select.value = None plot_ecdfs() def handle_add(_): """Handle pressing the Add button.""" if score_select.value is None or table_select.value is None: return score_idx = simdata.get_score_info(score_select.value.split(': ', 1)[-1], verbose=False)['index'] table_idx = simdata.get_table_info(table_select.value.split(': ', 1)[-1], verbose=False)['index'] score_vardata = simdata.get_score_data(score_idx, table_idx, verbose=False) var_list.append((score_idx, table_idx, score_vardata)) refresh_score_table() def handle_remove(_): """Handle pressing the Remove button.""" if score_table_select.value is None: return var_list.pop(score_table_select.index) refresh_score_table() def handle_reset(_): """Handle pressing the Reset button.""" var_list[:] = [] refresh_score_table() add_button.on_click(handle_add) del_button.on_click(handle_remove) reset_button.on_click(handle_reset) score_thr_cb.on_trait_change(plot_ecdfs, name='value') enable_readouts_cb.on_trait_change(plot_ecdfs, name='value') cdf_slider.on_trait_change(plot_ecdfs, name='value')
def _ui(self) -> List[W.Widget]: cb = W.Checkbox(description="Allow Non-Flow Ports To Switch Sides") T.link((self, "allow_switch"), (cb, "value")) return [cb]
def __init__(self, name, **kwargs): value = kwargs.get('value', False) self.dd = widgets.Checkbox(value=value) FormValue.__init__(self, name, **kwargs)
def __init__(self, inputType, dataType, graph, options, title): """ :param inputType: on of these values: "radio", "checkboxGroup" :param dataType: can either be "discrimination", "corpusNames" or "analysisFunction" this will be used by the models to know what kind of control is handled by this input :param graph: graph to update when this input is updated :param options: can either be a list or a dictionary - a list if you want the data displayed for the user to be directly sent to the model - a dict if you want to your own words on something and translate it before sending it to the model. {click here to select SwitchBoard: SWBD, click here to select mTx: MTX } :param title: title to be given to the input """ # checking the types try: if not isinstance(inputType, str): message = "expected string as inputType, got " + str( type(inputType)) raise TypeError(message) if not (isinstance(options, list) or isinstance(options, dict)): message = "expected array or dict as options, got " + str( type(options)) raise TypeError(message) for item in options: if not isinstance(item, str): message = "expected strings inside options, got " + str( type(item)) raise TypeError(message) except TypeError: traceback.print_exc() self.__optionToProgramMeaning = None inputOptions = None if isinstance(options, list): inputOptions = options else: inputOptions = [command for command in options] self.__optionToProgramMeaning = options if inputType == "radio": self.__widget = widgets.RadioButtons(options=inputOptions, value=inputOptions[0], description=title, disabled=False) elif inputType == "checkboxGroup": inputs = [] for option in inputOptions: temp = widgets.Checkbox(value=True, description=option) inputs.append(temp) verticalBox = widgets.VBox() verticalBox.children = inputs self.__widget = verticalBox elif inputType == "text": textInput = widgets.IntText(description=options, continuous_update=False) def onclick(): return textInput.value button = widgets.IntText(description="conversation id", continuous_update=False) button.on_click(onclick) if inputType == "checkboxGroup": for widget in self.__widget.children: widget.observe(self._callOnChange) else: self.__widget.observe(self._callOnChange) self.__onChanges = None self.__dataType = dataType self.graph = graph
def Create_form(): '''Display the widget for creation and printing of the experiment form.''' short_layout = widgets.Layout(width='200px', height='40px') medium_layout = widgets.Layout(width='300px', height='40px') long_layout = widgets.Layout(width='800px', height='40px') style = {'description_width': 'initial'} title = widgets.Text( placeholder='Title', description='Experiment title:', layout=long_layout, style=style) number = widgets.Text( placeholder='Number', description='Experiment number:', layout=medium_layout, style=style) ttype = widgets.Text( placeholder='Proposal, Commissioning, ...', description='Type:', layout=medium_layout, style=style) safety = widgets.Dropdown( options=['Green', 'Yellow', 'Red'], value='Yellow', description='Safety:', layout=widgets.Layout(width='150px'), style=style) date = widgets.Text( placeholder='DD/MM/YYYY - DD/MM/YYYY', description='Date:', layout=medium_layout, style=style) proposer = widgets.Text( placeholder='Name', description='Main proposer:', layout=short_layout, style=style) contact = widgets.Text( placeholder='Name', description='Local contact:', layout=medium_layout, style=style) users = widgets.Text( placeholder='Names', description='Users (on site):', layout=long_layout, style=style) recording = widgets.Text( placeholder='Full path to the recording directory', description='Recording directory:', layout=long_layout, style=style) current = widgets.Text( placeholder='450 mA, 500 mA, ...', description='Current:', layout=medium_layout, style=style) mode = widgets.Text( placeholder='Hybrid, Top-up, ...', description='Mode:', layout=medium_layout, style=style) dcm = widgets.Dropdown( options=['Si111', 'InSb', 'Not Used'], value='Si111', description='DCM:', layout=widgets.Layout(width='150px'), style=style) mgm = widgets.Dropdown( options=['In use', 'Not used'], value='Not used', description='MGM:', layout=widgets.Layout(width='150px'), style=style) energy = widgets.Text( placeholder='Value(s)', description='Energy (keV):', layout=medium_layout, style=style) energy_type = widgets.Dropdown( options=['Fixed', 'Variable'], value='Fixed', description='Fixed/Variable energy:', layout=widgets.Layout(width='300px'), style=style) wavelength = widgets.Text( placeholder='Value(s)', description='Wavelength (nm):', layout=medium_layout, style=style) harmonic = widgets.Text( placeholder='Value(s)', description='Harmonic:', layout=short_layout, style=style) polarisation = widgets.Text( placeholder='Value(s)', value='LH', description='Polarisation:', layout=short_layout, style=style) phase = widgets.Text( placeholder='Value(s)', value='0', description='Phase (deg):', layout=short_layout, style=style) m1 = widgets.Dropdown( options=['M1-A Pt Track', 'M1-A B4C Track', 'M1-B (B4C)', 'No M1'], value='M1-A Pt Track', description='M1:', layout=widgets.Layout(width='200px'), style=style) m2 = widgets.Dropdown( options=['M2 Pt Track', 'M2 B4C Track', 'No M2'], value='M2 Pt Track', description='M2:', layout=widgets.Layout(width='200px'), style=style) m3 = widgets.Dropdown( options=['M3 Pt Track', 'M3 B4C Track', 'No M3'], value='No M3', description='M3:', layout=widgets.Layout(width='200px'), style=style) m4 = widgets.Dropdown( options=['M4 Pt Track', 'M4 Si Track', 'No M4'], value='M4 Pt Track', description='M4:', layout=widgets.Layout(width='200px'), style=style) horizontal_foc = widgets.Checkbox( value=True, description='Horizontal focalisation:', layout=long_layout, style=style) vertical_foc = widgets.Checkbox( value=True, description='Vertical focalisation:', layout=long_layout, style=style) horizontal_size = widgets.Text( placeholder='Value(s)', description='Horizontal beamsize (mm):', layout=medium_layout, style=style) vertical_size = widgets.Text( placeholder='Value(s)', description='Vertical beamsize (mm):', layout=medium_layout, style=style) mon1 = widgets.Text( placeholder='Empty or type of mon', description='mon1:', layout=short_layout, style=style) mon2 = widgets.Text( placeholder='Empty or type of mon', description='mon2:', layout=short_layout, style=style) mon3 = widgets.Text( placeholder='Empty or type of mon', description='mon3:', layout=short_layout, style=style) mon4 = widgets.Text( placeholder='Empty or type of mon', description='mon4:', layout=short_layout, style=style) detectors = widgets.Textarea( placeholder='Type of detectors', description='Detectors:', layout=long_layout, style=style) remarks = widgets.Textarea( placeholder='Remarks', description='Remarks:', layout=long_layout, style=style) display(title) display(widgets.HBox([date, number])) display(widgets.HBox([ttype, safety])) print('-'*100) display(widgets.HBox([proposer, contact])) display(users) print('-'*100) display(recording) print('\033[1m'+'Machine:') display(widgets.HBox([current, mode])) print('\033[1m'+'Optics:') display(widgets.HBox([dcm, mgm])) display(widgets.HBox([m1, m2, m3, m4])) print('\033[1m'+'Beam:') display(widgets.HBox([energy_type, energy, wavelength])) display(widgets.HBox([harmonic, polarisation, phase])) display(widgets.HBox([horizontal_foc, vertical_foc])) display(widgets.HBox([horizontal_size, vertical_size])) print('\033[1m'+'Monitors and XBPM:') display(widgets.HBox([mon1, mon2, mon3, mon4])) display(detectors) print('\033[1m'+'Remarks:') display(remarks) def on_button_clicked(b): txt = [] txt.append('$\LARGE \\textbf{SIRIUS Beamline}:\\textbf{Experiment %s}$'%number.value) ######################################## # Prepare the title in several parts # Avoid having a line larger than the page # Max number of characters allowed by line max_length = 70 title_split = title.value.split(' ') title_blocks = [] j=0 for k in range(0,len(title_split)): title_part = '' for i in range(j,len(title_split)): if len(title_part)<max_length: title_part += title_split[i]+' ' j=j+1 else: break title_blocks.append(title_part) for title_block in title_blocks: if title_block != '': txt.append('$\Large \\color{red}{\\bf %s}$'%('\ '.join(title_block.split(' ')))) # Former (simpler version), but allows line longer than the page width #ptitle = ('\ '.join(title.value.split(' '))) #txt.append('$\Large \\color{red}{\\bf %s}$'%ptitle) ######################################## txt.append('* %s %s'%(ttype.description,ttype.value)+'\n' +'* %s %s'%(safety.description,safety.value)+'\n' +'* %s %s'%(date.description,date.value)) txt.append('* %s %s'%(proposer.description,proposer.value)+'\n' +'* %s %s'%(contact.description,contact.value)+'\n' +'* %s %s'%(users.description,users.value)+'\n' +'* %s %s'%(recording.description,recording.value)) txt.append('* Machine:'+'\n' +'\t * %s %s'%(current.description,current.value)+'\n' +'\t * %s %s'%(mode.description,mode.value)) txt.append('* Optics:'+'\n' +'\t * %s %s'%(dcm.description,dcm.value)+'\n' +'\t * %s %s'%(mgm.description,mgm.value)+'\n' +'\t * %s %s'%(m1.description,m1.value)+'\n' +'\t * %s %s'%(m2.description,m2.value)+'\n' +'\t * %s %s'%(m3.description,m3.value)+'\n' +'\t * %s %s'%(m4.description,m4.value)) txt.append('* Beam:'+'\n' +'\t * %s %s'%(energy_type.description,energy_type.value)+'\n' +'\t * %s %s'%(energy.description,energy.value)+'\n' +'\t * %s %s'%(wavelength.description,wavelength.value)+'\n' +'\t * %s %s'%(harmonic.description,harmonic.value)+'\n' +'\t * %s %s'%(polarisation.description,polarisation.value)+'\n' +'\t * %s %s'%(phase.description,phase.value)+'\n' +'\t * %s %s'%(horizontal_foc.description,horizontal_foc.value)+'\n' +'\t * %s %s'%(vertical_foc.description,vertical_foc.value)+'\n' +'\t * %s %s'%(horizontal_size.description,horizontal_size.value)+'\n' +'\t * %s %s'%(vertical_size.description,vertical_size.value)) txt.append('* Monitors and XBPM:'+'\n' +'\t * %s %s'%(mon1.description,mon1.value)+'\n' +'\t * %s %s'%(mon2.description,mon2.value)+'\n' +'\t * %s %s'%(mon3.description,mon3.value)+'\n' +'\t * %s %s'%(mon4.description,mon4.value)+'\n' +'\t * %s %s'%(detectors.description,detectors.value)) txt.append('* %s %s'%(remarks.description,remarks.value)) txt.reverse() for elem in txt: Utils.Create_cell(code=elem, position ='below', celltype='markdown', is_print=True) Utils.Create_cell(code='# Experimental setup', position ='below', celltype='markdown', is_print=True) # Remove the widget when done Utils.Delete_current_cell() button = widgets.Button(description="Print form") out = widgets.Output() display(button,out) button.on_click(on_button_clicked)
description='$Y_{delta}$', layout = Layout(width = constWidth), ) zdelta = widgets.BoundedFloatText( min = 1., description='$Z_{delta}$', layout = Layout(width = constWidth), ) tdelta = widgets.BoundedFloatText( min = 0.01, description='$Time_{delta}$', layout = Layout(width = constWidth), ) toggle2D = widgets.Checkbox( description='2-D', layout = Layout(width = constWidth), ) def toggle2D_cb(b): if (toggle2D.value): #zmin.disabled = zmax.disabled = zdelta.disabled = True zmin.disabled = True zmax.disabled = True zdelta.disabled = True else: zmin.disabled = False zmax.disabled = False zdelta.disabled = False toggle2D.observe(toggle2D_cb) x_row = widgets.HBox([xmin,xmax,xdelta])
def bool2checkbox(trait, label): tooltip = trait.desc if trait.desc else "" widget = ipywidgets.Checkbox(tooltip=tooltip, ) return labelme(widget=widget, label=label)
def gpt_plot_gui(gpt_data_input): gpt_data = convert_gpt_data(gpt_data_input) p = make_default_plot(None, plot_width=500, plot_height=400) p_table = DataTable() screen_z_list = gpt_data.stat('mean_z', 'screen').tolist() tab_panel = widgets.Tab() # Layouts layout_200px = widgets.Layout(width='200px', height='30px') layout_150px = widgets.Layout(width='150px', height='30px') layout_100px = widgets.Layout(width='100px', height='30px') layout_20px = widgets.Layout(width='20px', height='30px') label_layout = layout_150px # Make widgets plottype_list = ['Trends', '1D Distribution', '2D Distribution'] plottype_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(plottype_list) ], value=0) dist2d_type_dropdown = widgets.Dropdown(options=[('Scatter', 'scatter'), ('Histogram', 'histogram') ], value='histogram', layout=layout_150px) scatter_color = ['density', 't', 'x', 'y', 'r', 'px', 'py', 'pz', 'pr'] scatter_color_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(scatter_color) ], value=0, layout=layout_150px) trend_x_list = ['z', 't'] trend_y_list = [ 'Beam Size', 'Bunch Length', 'Emittance (x,y)', 'Emittance (4D)', 'Slice emit. (x,y)', 'Slice emit. (4D)', 'Charge', 'Energy', 'Trajectory' ] trend_x_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(trend_x_list) ], value=0, layout=layout_150px) trend_y_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(trend_y_list) ], value=0, layout=layout_150px) dist_list = ['t', 'x', 'y', 'r', 'px', 'py', 'pz', 'pr'] trend_slice_var_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(dist_list) ], value=0, layout=layout_150px) trend_slice_nslices_text = widgets.BoundedIntText(value=50, min=5, max=500, step=1, layout=layout_150px) dist_x_1d_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(dist_list) ], value=0, layout=layout_150px) dist_x_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(dist_list) ], value=1, layout=layout_150px) dist_y_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(dist_list) ], value=2, layout=layout_150px) dist_type_1d_list = [ 'Charge Density', 'Emittance X', 'Emittance Y', 'Emittance 4D', 'Sigma X', 'Sigma Y' ] dist_type_1d_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(dist_type_1d_list) ], value=0, layout=layout_150px) axis_equal_checkbox = widgets.Checkbox(value=False, description='Enabled', disabled=False, indent=False, layout=layout_100px) screen_z_dropdown = widgets.Dropdown(options=[ (f'{z:.3f}', i) for (i, z) in enumerate(screen_z_list) ], layout=layout_150px) nbin_1d_text = widgets.BoundedIntText(value=50, min=5, max=500, step=1, layout=layout_150px) nbin_x_text = widgets.BoundedIntText(value=50, min=5, max=500, step=1, layout=layout_150px) nbin_y_text = widgets.BoundedIntText(value=50, min=5, max=500, step=1, layout=layout_150px) cyl_copies_checkbox = widgets.Checkbox(value=False, description='Enabled', disabled=False, indent=False, layout=layout_100px) cyl_copies_text = widgets.BoundedIntText(value=50, min=10, max=500, step=1, layout=layout_150px) remove_correlation_checkbox = widgets.Checkbox(value=False, description='Enabled', disabled=False, indent=False, layout=layout_100px) remove_correlation_n_text = widgets.BoundedIntText(value=1, min=0, max=10, step=1, layout=layout_150px) remove_correlation_var1_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(dist_list) ], value=0, layout=layout_150px) remove_correlation_var2_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(dist_list) ], value=6, layout=layout_150px) take_slice_checkbox = widgets.Checkbox(value=False, description='Enabled', disabled=False, indent=False, layout=layout_100px) take_slice_var_dropdown = widgets.Dropdown(options=[ (a, i) for (i, a) in enumerate(dist_list) ], value=0, layout=layout_150px) take_slice_nslices_text = widgets.BoundedIntText(value=50, min=5, max=500, step=1, layout=layout_150px) take_slice_index_text = widgets.BoundedIntText( value=0, min=0, max=take_slice_nslices_text.value - 1, step=1, layout=layout_150px) trends_tab = widgets.VBox([ widgets.HBox( [widgets.Label('X axis', layout=label_layout), trend_x_dropdown]), widgets.HBox( [widgets.Label('Y axis', layout=label_layout), trend_y_dropdown]), widgets.HBox([ widgets.Label('Slice variable', layout=label_layout), trend_slice_var_dropdown ]), widgets.HBox([ widgets.Label('Number of slices', layout=label_layout), trend_slice_nslices_text ]) ]) dist_1d_tab = widgets.VBox([ widgets.HBox([ widgets.Label('Screen z (m)', layout=label_layout), screen_z_dropdown ]), widgets.HBox( [widgets.Label('X axis', layout=label_layout), dist_x_1d_dropdown]), widgets.HBox([ widgets.Label('Y axis', layout=label_layout), dist_type_1d_dropdown ]), widgets.HBox([ widgets.Label('Histogram bins', layout=label_layout), nbin_1d_text ]) ]) dist_2d_tab = widgets.VBox([ widgets.HBox([ widgets.Label('Screen z (m)', layout=label_layout), screen_z_dropdown ]), widgets.HBox([ widgets.Label('Plot method', layout=label_layout), dist2d_type_dropdown ]), widgets.HBox([ widgets.Label('Scatter color variable', layout=label_layout), scatter_color_dropdown ]), widgets.HBox( [widgets.Label('X axis', layout=label_layout), dist_x_dropdown]), widgets.HBox( [widgets.Label('Y axis', layout=label_layout), dist_y_dropdown]), widgets.HBox([ widgets.Label('Equal scale axes', layout=label_layout), axis_equal_checkbox ]), widgets.HBox([ widgets.Label('Histogram bins, X', layout=label_layout), nbin_x_text ]), widgets.HBox([ widgets.Label('Histogram bins, Y', layout=label_layout), nbin_y_text ]) ]) postprocess_tab = widgets.VBox([ widgets.HBox([ widgets.Label('Cylindrical copies', layout=label_layout), cyl_copies_checkbox ]), widgets.HBox([ widgets.Label('Number of copies', layout=label_layout), cyl_copies_text ]), widgets.HBox([ widgets.Label('Remove Correlation', layout=label_layout), remove_correlation_checkbox ]), widgets.HBox([ widgets.Label('Max polynomial power', layout=label_layout), remove_correlation_n_text ]), widgets.HBox([ widgets.Label('Independent var (x)', layout=label_layout), remove_correlation_var1_dropdown ]), widgets.HBox([ widgets.Label('Dependent var (y)', layout=label_layout), remove_correlation_var2_dropdown ]), widgets.HBox([ widgets.Label('Take slice of data', layout=label_layout), take_slice_checkbox ]), widgets.HBox([ widgets.Label('Slice variable', layout=label_layout), take_slice_var_dropdown ]), widgets.HBox([ widgets.Label('Slice index', layout=label_layout), take_slice_index_text ]), widgets.HBox([ widgets.Label('Number of slices', layout=label_layout), take_slice_nslices_text ]) ], description='Postprocessing') tab_list = [trends_tab, dist_1d_tab, dist_2d_tab, postprocess_tab] tab_label_list = ['Trends', '1D Dist.', '2D Dist.', 'Postprocess'] tab_panel.children = tab_list for i, t in enumerate(tab_label_list): tab_panel.set_title(i, t) tools_panel = widgets.VBox([ widgets.HBox([ widgets.Label('Plot Type', layout=label_layout), plottype_dropdown ]), tab_panel ]) def make_plot(): plottype = plottype_dropdown.label.lower() trend_x = trend_x_dropdown.label trend_y = trend_y_dropdown.label dist_x_1d = dist_x_1d_dropdown.label dist_y_1d = dist_type_1d_dropdown.label dist_x = dist_x_dropdown.label dist_y = dist_y_dropdown.label screen_z = screen_z_list[screen_z_dropdown.value] nbins_1d = nbin_1d_text.value nbins = [nbin_x_text.value, nbin_y_text.value] cyl_copies = cyl_copies_text.value cyl_copies_on = cyl_copies_checkbox.value and (plottype != 'trends') cyl_copies_text.disabled = not cyl_copies_on ptype = dist2d_type_dropdown.value.lower() scatter_color_var = scatter_color_dropdown.label.lower() axis_equal = axis_equal_checkbox.value remove_correlation = remove_correlation_checkbox.value and (plottype != 'trends') remove_correlation_n = remove_correlation_n_text.value remove_correlation_var1 = remove_correlation_var1_dropdown.label remove_correlation_var2 = remove_correlation_var2_dropdown.label take_slice = take_slice_checkbox.value and (plottype != 'trends') take_slice_var = take_slice_var_dropdown.label take_slice_index = take_slice_index_text.value take_slice_nslices = take_slice_nslices_text.value take_slice_index_text.max = take_slice_nslices - 1 trend_slice_var = trend_slice_var_dropdown.label trend_slice_nslices = trend_slice_nslices_text.value is_trend = (plottype == 'trends') is_dist1d = (plottype == '1d distribution') is_dist2d = (plottype == '2d distribution') is_slice_trend = ('slice' in trend_y.lower()) trend_x_dropdown.disabled = not is_trend trend_y_dropdown.disabled = not is_trend dist_x_1d_dropdown.disabled = not is_dist1d dist_type_1d_dropdown.disabled = not is_dist1d dist_x_dropdown.disabled = not is_dist2d dist_y_dropdown.disabled = not is_dist2d nbin_1d_text.disabled = not is_dist1d nbin_x_text.disabled = not is_dist2d nbin_y_text.disabled = not is_dist2d screen_z_dropdown.disabled = not (is_dist1d or is_dist2d) cyl_copies_checkbox.disabled = not (is_dist1d or is_dist2d) dist2d_type_dropdown.disabled = not is_dist2d axis_equal_checkbox.disabled = not is_dist2d scatter_color_dropdown.disabled = not (is_dist2d and ptype == 'scatter') trend_slice_var_dropdown.disabled = not is_slice_trend trend_slice_nslices_text.disabled = not is_slice_trend if (is_trend): if (tab_panel.selected_index < 3): tab_panel.selected_index = 0 var1 = 'mean_' + trend_x var2 = get_trend_vars(trend_y) params = {} if (is_slice_trend): params['slice_key'] = trend_slice_var params['n_slices'] = trend_slice_nslices gpt_plot(gpt_data, var1, var2, p=p, show_plot=False, format_input_data=False, **params) #figure_hbox.children += (gpt_plot(gpt_data, var1, var2, **params), ) if (is_dist1d): if (tab_panel.selected_index < 3): tab_panel.selected_index = 1 ptype_1d = get_dist_plot_type(dist_y_1d) params = {} if (cyl_copies_on): params['cylindrical_copies'] = cyl_copies if (remove_correlation): params['remove_correlation'] = (remove_correlation_var1, remove_correlation_var2, remove_correlation_n) if (take_slice): params['take_slice'] = (take_slice_var, take_slice_index, take_slice_nslices) gpt_plot_dist1d(gpt_data, dist_x_1d, p=p, p_table=p_table, screen_z=screen_z, plot_type=ptype_1d, nbins=nbins_1d, show_plot=False, format_input_data=False, **params) if (is_dist2d): if (tab_panel.selected_index < 3): tab_panel.selected_index = 2 params = {} params['color_var'] = scatter_color_var if (axis_equal): params['axis'] = 'equal' if (cyl_copies_on): params['cylindrical_copies'] = cyl_copies if (remove_correlation): params['remove_correlation'] = (remove_correlation_var1, remove_correlation_var2, remove_correlation_n) if (take_slice): params['take_slice'] = (take_slice_var, take_slice_index, take_slice_nslices) gpt_plot_dist2d(gpt_data, dist_x, dist_y, p=p, p_table=p_table, screen_z=screen_z, plot_type=ptype, nbins=nbins, show_plot=False, format_input_data=False, **params) # Callback functions def remake_on_value_change(change): p.renderers = [] make_plot() push_notebook() # Register callbacks plottype_dropdown.observe(remake_on_value_change, names='value') trend_x_dropdown.observe(remake_on_value_change, names='value') trend_y_dropdown.observe(remake_on_value_change, names='value') dist_x_1d_dropdown.observe(remake_on_value_change, names='value') dist_x_dropdown.observe(remake_on_value_change, names='value') dist_y_dropdown.observe(remake_on_value_change, names='value') screen_z_dropdown.observe(remake_on_value_change, names='value') nbin_1d_text.observe(remake_on_value_change, names='value') nbin_x_text.observe(remake_on_value_change, names='value') nbin_y_text.observe(remake_on_value_change, names='value') cyl_copies_checkbox.observe(remake_on_value_change, names='value') cyl_copies_text.observe(remake_on_value_change, names='value') dist2d_type_dropdown.observe(remake_on_value_change, names='value') scatter_color_dropdown.observe(remake_on_value_change, names='value') axis_equal_checkbox.observe(remake_on_value_change, names='value') dist_type_1d_dropdown.observe(remake_on_value_change, names='value') remove_correlation_checkbox.observe(remake_on_value_change, names='value') remove_correlation_n_text.observe(remake_on_value_change, names='value') remove_correlation_var1_dropdown.observe(remake_on_value_change, names='value') remove_correlation_var2_dropdown.observe(remake_on_value_change, names='value') take_slice_checkbox.observe(remake_on_value_change, names='value') take_slice_var_dropdown.observe(remake_on_value_change, names='value') take_slice_index_text.observe(remake_on_value_change, names='value') take_slice_nslices_text.observe(remake_on_value_change, names='value') trend_slice_var_dropdown.observe(remake_on_value_change, names='value') trend_slice_nslices_text.observe(remake_on_value_change, names='value') make_plot() gui = tools_panel return (p, p_table, gui)
def task_timeline(): path_input = widgets.Button(description="View task timeline") breakdown_basic = "Basic" breakdown_task = "Task Breakdowns" breakdown_opt = widgets.Dropdown( options=["Basic", "Task Breakdowns"], value="Basic", disabled=False, ) obj_dep = widgets.Checkbox( value=True, disabled=False, layout=widgets.Layout(width='20px') ) task_dep = widgets.Checkbox( value=True, disabled=False, layout=widgets.Layout(width='20px') ) # Labels to bypass width limitation for descriptions. label_tasks = widgets.Label(value='Task submissions', layout=widgets.Layout(width='110px')) label_objects = widgets.Label(value='Object dependencies', layout=widgets.Layout(width='130px')) label_options = widgets.Label(value='View options:', layout=widgets.Layout(width='100px')) start_box, end_box, range_slider, time_opt = get_sliders(False) display(widgets.HBox([task_dep, label_tasks, obj_dep, label_objects])) display(widgets.HBox([label_options, breakdown_opt])) display(path_input) # Check that the trace viewer renderer file is present, and copy it to the # current working directory if it is not present. if not os.path.exists("trace_viewer_full.html"): shutil.copy( os.path.join(os.path.dirname(os.path.abspath(__file__)), "../core/src/catapult_files/trace_viewer_full.html"), "trace_viewer_full.html") def handle_submit(sender): json_tmp = tempfile.mktemp() + ".json" # Determine whether task components should be displayed or not. if breakdown_opt.value == breakdown_basic: breakdown = False elif breakdown_opt.value == breakdown_task: breakdown = True else: raise ValueError( "Unexpected breakdown value '{}'".format(breakdown_opt.value)) low, high = map(lambda x: x / 100., range_slider.value) smallest, largest, num_tasks = ray.global_state._job_length() diff = largest - smallest if time_opt.value == total_time_value: tasks = _truncated_task_profiles(start=smallest + diff * low, end=smallest + diff * high) elif time_opt.value == total_tasks_value: if range_slider.value[0] == 0: tasks = _truncated_task_profiles(num_tasks=int( num_tasks * high), fwd=True) else: tasks = _truncated_task_profiles(num_tasks=int( num_tasks * (high - low)), fwd=False) else: raise ValueError("Unexpected time value '{}'".format( time_opt.value)) # Write trace to a JSON file print("Collected profiles for {} tasks.".format(len(tasks))) print( "Dumping task profile data to {}, " "this might take a while...".format(json_tmp)) ray.global_state.dump_catapult_trace(json_tmp, tasks, breakdowns=breakdown, obj_dep=obj_dep.value, task_dep=task_dep.value) print("Opening html file in browser...") trace_viewer_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "../core/src/catapult_files/index.html") html_file_path = _get_temp_file_path(suffix=".html") json_file_path = _get_temp_file_path(suffix=".json") print("Pointing to {} named {}".format(json_tmp, json_file_path)) shutil.copy(json_tmp, json_file_path) with open(trace_viewer_path) as f: data = f.read() # Replace the demo data path with our own # https://github.com/catapult-project/catapult/blob/ # 33a9271eb3cf5caf925293ec6a4b47c94f1ac968/tracing/bin/index.html#L107 data = data.replace("../test_data/big_trace.json", json_file_path) with open(html_file_path, "w+") as f: f.write(data) # Display the task trace within the Jupyter notebook clear_output(wait=True) print( "To view fullscreen, open chrome://tracing in Google Chrome " "and load `{}`".format(json_tmp)) display(IFrame(html_file_path, 900, 800)) path_input.on_click(handle_submit)
def _init_widget(self): """构建AbuDoubleMaBuy策略参数界面""" self.description = widgets.Textarea( value=u'动态自适应双均线买入策略:\n' u'双均线策略是量化策略中经典的策略之一,其属于趋势跟踪策略: \n' u'1. 预设两条均线:如一个ma=5,一个ma=60, 5的均线被称作快线,60的均线被称作慢线\n' u'2. 择时买入策略中当快线上穿慢线(ma5上穿ma60)称为形成金叉买点信号,买入股票\n' u'3. 自适应动态慢线,不需要输入慢线值,根据走势震荡套利空间,寻找合适的ma慢线\n' u'4. 自适应动态快线,不需要输入快线值,根据慢线以及大盘走势,寻找合适的ma快线', description=u'双均线买', disabled=False, layout=self.description_layout ) self.slow_label = widgets.Label(u'默认使用动态慢线,可手动固定慢线值', layout=self.label_layout) self.slow_int = widgets.IntSlider( value=60, min=10, max=120, step=1, description=u'手动', disabled=True, orientation='horizontal', readout=True, readout_format='d' ) self.auto_slow = widgets.Checkbox( value=True, description=u'动态慢线', disabled=False ) def slow_change(change): self.slow_int.disabled = change['new'] self.auto_slow.observe(slow_change, names='value') self.slow = widgets.VBox([self.auto_slow, self.slow_int]) self.slow_box = widgets.VBox([self.slow_label, self.slow]) self.fast_label = widgets.Label(u'默认使用动态快线,可手动固定快线值', layout=self.label_layout) self.fast_int = widgets.IntSlider( value=5, min=1, max=90, step=1, description=u'手动', disabled=True, orientation='horizontal', readout=True, readout_format='d' ) self.auto_fast = widgets.Checkbox( value=True, description=u'动态快线', disabled=False, ) def fast_change(change): self.fast_int.disabled = change['new'] self.auto_fast.observe(fast_change, names='value') self.fast = widgets.VBox([self.auto_fast, self.fast_int]) self.fast_box = widgets.VBox([self.fast_label, self.fast]) self.widget = widgets.VBox([self.description, self.slow_box, self.fast_box, self.add], # border='solid 1px', layout=self.widget_layout)
def __init__(self, idno, livefig, title='None', ntraces=4, **kwargs): """ Data Aquistion Instance (a run). :param idno : id number you wish to use to keep track :param livefig: plotly FigureWidget to use for live display :param title: optional name :param ntraces: number of traces (default = 4) more than 4 easily overwhelms a pi4. :param kwargs: :ignore_skew: bool (default: True) if True only a single average collection time will be recorded for each time in a multichannel data collection. If False a separate set of time will be recorded for each channel. """ self.ignore_skew = kwargs.pop('ignore_skew', True) self.idno = idno self.livefig = livefig self.title = str(title) self.averaging_time = 0.1 # seconds adjusted based on collection rate self.gain = [1] * ntraces self.data = [] self.timestamp = [] self.stdev = [] self.pandadf = None self.ntraces = ntraces self.separate_plots = True self.traces = [] # index map from returned data to trace, self.tracemap = [] self.tracelbls = [] self.units = [] for i in range(self.ntraces): self.traces.append(ChannelSettings(i, availboards)) self.ratemax = 20.0 # Hz self.rate = 1.0 # Hz self.deltamin = 1 / self.ratemax self.delta = 1.0 / self.rate self.setupbtn = widgets.Button( description='Set Parameters', disabled=False, button_style='info', # 'success', 'info', 'warning', 'danger' or '' tooltip='Click to set collection parameters to displayed values.', icon='') self.collectbtn = widgets.Button( description='Start Collecting', disabled=False, button_style='success', # 'success', 'info', 'warning', 'danger' or '' tooltip='Start collecting data and plotting it. ' 'Will make new graph.', icon='') self.separate_traces_checkbox = widgets.Checkbox( value = self.separate_plots, description = 'Display multiple traces as stacked graphs. ' \ 'Uncheck to display all on the same graph.', layout = widgets.Layout(width='80%'), disabled = False) self.rateinp = widgets.BoundedFloatText(value=self.rate, min=0, max=self.ratemax, step=self.ratemax / 1000.0, description='Rate (Hz):', disabled=False) self.timelbl = widgets.Text(value='Time(s)', placeholder='Type something', description='X-axis label (time):', disabled=False) self.runtitle = widgets.Text(value=self.title, placeholder='Type title/description', description='Run title', disabled=False) self.defaultparamtxt = '' self.defaultcollecttxt = '<span style="color:blue;"> To accurately ' self.defaultcollecttxt += 'read point location on graph you can zoom ' self.defaultcollecttxt += 'in. Drag to zoom. Additional controls show ' self.defaultcollecttxt += 'above plot when you hover over it.</span>' self.collecttxt = widgets.HTML(value=self.defaultcollecttxt, placeholder='', description='') self.setup_layout_bottom = widgets.HBox( [self.rateinp, self.timelbl, self.setupbtn]) self.setup_layout = widgets.VBox( [self.separate_traces_checkbox, self.setup_layout_bottom]) self.collect_layout = widgets.HBox([self.collectbtn, self.collecttxt])
def __init__(self, filename, figsize=(5, 5), colormap=None, orient_radiology=None, guidelines=None, animation_speed=300): """ Turn .nii files into interactive plots using ipywidgets. Args ---- filename : str The path to your ``.nii`` file. Can be a string, or a ``PosixPath`` from python3's pathlib. figsize : tuple The figure size for each individual view. colormaps : tuple, list A list of colormaps, or single colormap. If None, a selection of maps is used to populate the dropdown widget. orient_radiology : bool Whether to display the images in the "radiological" style, i.e. with lef and right reversed. If None, a checkbox is offered. guidelines : bool Whether to display guide lines. If None, a checkbox is offered. animation_speed : int The speed used for the animation, in milliseconds between frames (the lower, the faster, up to a limit). """ if hasattr(filename, 'get_data'): self.data = filename else: filename = str(filename) if not os.path.isfile(filename): raise OSError('File ' + filename + ' not found.') # load data to ensures that the file readable by nibabel self.data = nib.load(str(filename)) self.displays = [widgets.Output() for _ in range(3)] self._generate_axes(figsize=figsize) # set how many dimensions this file has self.ndim = len(self.data.shape) # initialise the control components of this widget self.dims = ['x', 'y', 'z', 't'][:self.ndim] self.controls = {} for i, dim in enumerate(self.dims): maxval = self.data.shape[i] - 1 self.controls[dim] = PlaySlider(min=0, max=maxval, value=maxval // 2, interval=animation_speed, label=dim.upper(), continuous_update=False) widgets.link((self.controls[dim], 'value'), (self, dim)) if not isinstance(colormap, str): self.color_picker = get_cmap_dropdown(colormap) widgets.link((self.color_picker, 'value'), (self, 'colormap')) self.color_reverser = widgets.Checkbox( description='Reverse colormap', indent=True) widgets.link((self.color_reverser, 'value'), (self, 'reverse_colors')) else: self.color_picker = widgets.HBox([]) self.color_reverser = widgets.HBox([]) self.colormap = colormap if guidelines is None: self.guideline_picker = widgets.Checkbox(value=True, description='Show guides', indent=False) widgets.link((self.guideline_picker, 'value'), (self, 'guidelines')) else: self.guideline_picker = widgets.Box([]) self.guidelines = guidelines if orient_radiology is None: self.orientation_switcher = widgets.Checkbox( value=self.orient_radiology, indent=False, description='Radiological Orientation') widgets.link((self.orientation_switcher, 'value'), (self, 'orient_radiology')) else: self.orientation_switcher = widgets.Box([]) self.orient_radiology = orient_radiology self._update_orientation(True)
def visualize(self): """Create widgets for visualize tab content""" content = [] # Note about data self.plot_note_html = ui.HTML(self.PLOT_STATUS_TEMPLATE % (NONE_ITEM, 0)) content.append(section(self.PLOT_NOTE_TITLE, [self.plot_note_html])) # Plotting widgets = [] label = ui.Label(value=self.PLOT_PLOT_LABEL, layout=ui.Layout(display="flex", justify_content="flex-start", width="5%")) self.viz_ddn_plot_set = ui.Dropdown(options=PLOT_SET_OPTIONS, layout=self.LO25) spacer = ui.Label(value=' ', layout=self.LO10) self.viz_btn_plot_generate = ui.Button( description=self.PLOT_GENERATE_LABEL, icon='line-chart', disabled=True, layout=ui.Layout(width='auto')) widgets.append( ui.HBox([ label, self.viz_ddn_plot_set, spacer, self.viz_btn_plot_generate ])) # Settings grid w1 = ui.Label(value=self.PLOT_TYPE_LABEL, layout=ui.Layout(width='auto', grid_area='w1')) w2 = self.viz_ddn_plot_type = ui.Dropdown(options=self.PLOT_TYPES, layout=ui.Layout( width='auto', grid_area='w2')) w3 = ui.Label(value=self.PLOT_X_LABEL, layout=ui.Layout(width='auto', grid_area='w3')) w4 = self.viz_ddn_plot_xaxis = ui.Dropdown(options=FIELDS, layout=ui.Layout( width='auto', grid_area='w4')) w5 = ui.Label(value=self.PLOT_Y_LABEL, layout=ui.Layout(width='auto', grid_area='w5')) w6 = self.viz_ddn_plot_yaxis = ui.Dropdown(options=FIELDS, layout=ui.Layout( width='auto', grid_area='w6')) w7 = ui.Label(value=self.PLOT_PIVOT_LABEL, layout=ui.Layout(width='auto', grid_area='w7')) w8 = self.viz_ddn_plot_pivot = ui.Dropdown(options=FIELDS, layout=ui.Layout( width='auto', grid_area='w8')) w9 = ui.Label(value=self.PLOT_AGGFUNC_LABEL, layout=ui.Layout(width='auto', grid_area='w9')) w10 = self.viz_ddn_plot_aggfunc = ui.Dropdown( options=[NONE_ITEM] + self.PLOT_AGGFUNC_OPTIONS, layout=ui.Layout(width='auto', grid_area='w10')) w11 = ui.Label(value=self.PLOT_FILL_LABEL, layout=ui.Layout(width='auto', grid_area='w11')) w12 = self.viz_ddn_plot_fill = ui.Dropdown( options=self.PLOT_FILL_OPTIONS, layout=ui.Layout(width='auto', grid_area='w12')) w13 = ui.Label(value=self.PLOT_INDEX_LABEL, layout=ui.Layout(width='auto', grid_area='w13')) w14 = self.viz_ckb_plot_index = ui.Checkbox(indent=False, layout=ui.Layout( width='auto', grid_area='w14')) w15 = ui.Label(value=self.PLOT_HARM_ROW_LABEL, layout=ui.Layout(width='auto', grid_area='w15')) w16 = self.viz_ddn_plot_harm_row = ui.Dropdown(options=[NONE_ITEM], disabled=True, layout=ui.Layout( width='auto', grid_area='w16')) w17 = ui.Label(value=self.PLOT_HARM_COL_LABEL, layout=ui.Layout(width='auto', grid_area='w17')) w18 = self.viz_ddn_plot_harm_col = ui.Dropdown(options=[NONE_ITEM], disabled=True, layout=ui.Layout( width='auto', grid_area='w18')) widgets.append( ui.GridBox( children=[ w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15, w16, w17, w18 ], layout=ui.Layout( width='100%', grid_template_rows='auto auto auto', grid_template_columns='auto auto auto auto auto auto', grid_template_areas='''"w3 w4 w7 w8 w15 w16" "w5 w6 w9 w10 w17 w18" "w1 w2 w11 w12 w13 w14"'''))) self.viz_out_plot_output = ui.Output() widgets.append(self.viz_out_plot_output) content.append(section(self.PLOT_TITLE, widgets)) # Pivot output self.viz_out_plot_data = ui.Output() sec = section(self.PIVOT_TITLE, [self.viz_out_plot_data]) sec.selected_index = None content.append(sec) # Plot download self.viz_ddn_plot_format = ui.Dropdown( value=self.DOWNLOAD_PLOT_FORMAT_OPTIONS[0][1], options=self.DOWNLOAD_PLOT_FORMAT_OPTIONS, layout=self.LO25) self.viz_btn_plot_refexp = ui.Button(description=self.EXPORT_BUTTON, icon='download', layout=self.LO20) self.viz_out_plot_export = ui.Output( layout={'border': '1px solid black'}) spacer = ui.Label(value=' ', layout=self.LO10) row = ui.HBox([ ui.Label(value=self.DOWNLOAD_PLOT_FORMAT_LABEL, layout=self.LO10), self.viz_ddn_plot_format, spacer, self.viz_btn_plot_refexp ]) widgets = [ui.VBox([row, spacer, self.viz_out_plot_export])] content.append(section(self.EXPORT_PLOT_TITLE, widgets)) return ui.VBox(content)
) w_startdate = widgets.Text( value='2017-04-01', placeholder=' ', description='Start date:', disabled=False ) w_enddate = widgets.Text( value='2017-10-01', placeholder=' ', description='End date:', disabled=False ) w_median = widgets.Checkbox( value=True, description='3x3 Median filter', disabled=False ) w_significance = widgets.BoundedFloatText( value=0.0001, min=0, max=0.05, step=0.0001, description='Significance:', disabled=False ) w_opacity = widgets.BoundedFloatText( value=0.5, min=0.0, max=1.0, step=0.1,
def __init__(self, thelenota, figsize=(800, 600)): # DIMRED widgets self.thelenota = thelenota self.figsize = figsize self.counts = None #widgets self.w = DUMMY() current_cluster_labels = None wstyle = widgets.Layout(width='200px') #, max_width='120px', # min_width='120px') self.w.drfilter_lower_perc_switch = ccwidget( self.thelenota, 'drfilter_lower_perc_switch', 'bool', self.dr_name_simple, False, lwidth='40px') self.w.drfilter_log_switch = ccwidget(self.thelenota, 'drfilter_log_switch', 'bool', self.dr_name_simple, False, lwidth='40px') self.w.drfilter_lower_percentage = ccwidget( self.thelenota, 'drfilter_lower_percentage', 'float', self.dr_name_simple, 0.2, min=0.01, max=1, value=0.2) self.w.drmethod = ccwidget( self.thelenota, 'dimred_method', 'dropdown', self.dr_name_simple, 'tsne', options='pca ica scorpius KernelPCA tsne nmf'.split()) self.w.drperplex = cache_widget_value(widgets.IntSlider(value=67, min=2, max=99, step=5), 67, self.thelenota, 'dimred_perplexity', self.dr_name_simple, fmt='int') self.w.drangle = cache_widget_value(widgets.FloatSlider(value=0.5, min=0.05, max=0.95, step=0.05), 0.5, self.thelenota, 'dimred_angle', self.dr_name_simple, fmt='float') self.w.drlrate = cache_widget_value(widgets.IntSlider(value=920, min=20, max=10000, step=50), 920, self.thelenota, 'dimred_learning_rate', self.dr_name_simple, fmt='int') self.w.drearly = cache_widget_value(widgets.FloatSlider(value=3.5, min=1, max=20, step=0.5), 3.5, self.thelenota, 'dimred_early_exag', self.dr_name_simple, fmt='float') self.w.dr_tsne_pcavar_cutoff = ccwidget(self.thelenota, 'tsne_pca_var_cutoff', 'float', self.dr_name_simple, 0.05, min=0.01, max=0.2, step=0.01) self.w.drrun = widgets.Button(description='GO!') self.w.drforce = widgets.Checkbox(description='force', layout=widgets.Layout(width='300px')) @dcache(self.thelenota, 'dimred_correlating_genes', 'tsv', self.dr_name) def get_dr_correlating(*_): stats, trans = run_dr_2() c1 = self.thelenota.counttable.apply( lambda x: pearsonr(x, trans.iloc[:, 0])[0], axis=1) c2 = self.thelenota.counttable.apply( lambda x: pearsonr(x, trans.iloc[:, 1])[0], axis=1) return pd.DataFrame({0: c1, 1: c2}) def get_top_correlating(*_): d = get_dr_correlating().abs().sum(1).sort_values(ascending=False) d = d.head(40) d = d.reset_index() d = d.apply(lambda x: '{} ({:.3g})'.format(x.iloc[0], x.iloc[1]), axis=1) return list(d) tcolormap = cmapper.CMAPPER(self.thelenota, extra_intrinsic_methods={ "top DR correlate": (get_top_correlating, cmapper.intrinsic_gene_score) }) self.w.sl_group_name = cache_widget_value(widgets.Text(layout=wstyle), 'self.thelenota', self.thelenota, 'group_define_name', self.dr_name_simple) self.w.sl_group_set = widgets.Text(layout=wstyle) self.w.sl_group_set_go = widgets.Button(description='set', layout=wstyle) self.w.sl_groupextractname = widgets.Text(layout=wstyle) self.w.sl_group_extract_go = widgets.Button(description='extract') self.w.clu_method = ccwidget(self.thelenota, 'cluster_method', 'dropdown', self.dr_name_simple, 'dbscan', options=['dbscan']) self.w.clu_dbscan_eps = ccwidget(self.thelenota, 'clu_dbscan_eps_w', 'float', self.dr_name_simple, 2.5, min=0.1, max=10.0, step=0.1) self.w.clu_go = widgets.Button(description='Cluster!') self.w.clu_name = ccwidget(self.thelenota, "cluster_name", "text", self.dr_name_simple, 'cluster') self.w.clu_store_go = widgets.Button(description='save') plotinfo = {} self.w.html = widgets.HTML() # data! samples = self.thelenota.counttable.columns nosamples = len(samples) color_mapper = LinearColorMapper(palette="Inferno256", low=-0.3, high=2.5) topgene = self.thelenota.counttable.std(1).sort_values().tail( 1).index[0] colv = list(self.thelenota.counttable.loc[topgene]) pdata = ColumnDataSource( dict(x=[random.uniform(-10, 10) for x in range(nosamples)], y=[random.uniform(-10, 10) for x in range(nosamples)], desc=list(samples), size=[0.3] * nosamples, score=colv)) self.thelenota._pdata = pdata # Clustering def run_clustering(*args): method = self.w.clu_method.value stats, trans = run_dr_2() if method == 'dbscan': labels = run_dbscan(trans, self.w.clu_dbscan_eps.value) else: lg.warning('Not implemented cluster method: {}'.format(method)) return self.thelenota._CLUSTER_LABELS[self.dr_name()] = labels colv = labels color_mapper = CategoricalColorMapper( palette=bokeh.palettes.Category20[20], factors=list(colv.value_counts().index)) colorbar_mapper = LinearColorMapper(palette='Inferno256', low=0, high=0) bcolorbar.color_mapper = colorbar_mapper if not bfigure.legend[0].items: bfigure.legend[0].items.append(blegend) bplot.data_source.data['score'] = colv bplot.glyph.fill_color['transform'] = color_mapper bplot.glyph.line_width = 0 bplot.glyph.line_alpha = 0 bokeh_io.push_notebook(handle=bhandle) def store_current_cluster(*args): labels = self.thelenota._CLUSTER_LABELS.get(self.dr_name()) if labels is None: self.w.html.value = "no cluster defined" return cname = self.w.clu_name.value labels = labels.apply(lambda x: '{}_{}'.format(cname, x)) outfile = self.thelenota.metadata_dir / '{}.tsv'.format(cname) moutfile = self.thelenota.metadata_dir / '{}.meta.tsv'.format( cname) tmeta = pd.DataFrame({cname: labels}) tmeta.to_csv(outfile, sep="\t") with open(moutfile, 'w') as F: F.write("{}\tcategorical\n".format(cname)) self.w.html.value = 'saved cluster to {}'.format(outfile) self.w.clu_store_go.on_click(store_current_cluster) self.w.clu_go.on_click(run_clustering) # GROUP SET def define_group(*args): groupset = self.w.sl_group_name.value groupname = self.w.sl_group_set.value self.thelenota.selected = list( self.thelenota.counttable.columns.to_series()\ .iloc[list(self.thelenota._DR_INDICI_SEL_)]) if groupset in self.thelenota.metadata_info.index: tmeta = self.thelenota.get_metadata(groupset) else: tmeta = pd.Series('na', index=self.thelenota.counttable.columns) tmeta.loc[self.thelenota.selected] = groupname self.thelenota.metadata_dir.makedirs_p() outfile = self.thelenota.metadata_dir / '{}.tsv'.format(groupset) moutfile = self.thelenota.metadata_dir / '{}.meta.tsv'.format( groupset) tmeta = pd.DataFrame({groupset: tmeta}) tmeta.to_csv(outfile, sep="\t") with open(moutfile, 'w') as F: F.write("{}\tcategorical\n".format(groupset)) run_dr_color() self.w.sl_group_set_go.on_click(define_group) # GROUP EXTRACT #sl_groupextractname_w def extract_group(*args): groupname = self.w.sl_groupextractname.value self.thelenota.selected = list( self.thelenota.counttable.columns.to_series()\ .iloc[list(self.thelenota._DR_INDICI_SEL_)]) outfile = self.thelenota.counttable_file.dirname() \ / '{}__{}.tsv'.format(self.thelenota.counttable_name, groupname) newcounts = self.thelenota.counttable[self.thelenota.selected] newcounts.to_csv(outfile, sep="\t") self.w.html.value = "save new count table to {}".format(outfile) self.w.sl_group_extract_go.on_click(extract_group) def force_refresh(): "Force to refresh calculations? Or can we use the cache??" return self.w.drforce.value @dcache(self.thelenota, 'filtered', 'pickle', self.dr_name_filter, force_refresh) def filter_counts(*_): counts = self.thelenota.counttable if self.w.drfilter_log_switch.value: minval = 0.5 * counts[counts > 0].min().min() lg.warning('log tranform log10(x+{:.4g})'.format(minval)) counts = np.log10(counts + minval) if self.w.drfilter_lower_perc_switch.value: perc = self.w.drfilter_lower_percentage.value csum = counts.sum(1) counts = counts[csum >= csum.quantile(perc)] return counts @dcache(self.thelenota, 'dimred_table', 'mtsv', self.dr_name, force_refresh) def run_dr_2(*_): param = self.get_dr_param() method = param['method'] del param['method'] if method == 'pca': stats, trans = qrtask.pca.sync(self.counts) elif method == 'ica': stats, trans = rqtask.ica.sync(self.counts) elif method == 'KernelPCA': stats, trans = rqtask.kernelPCA.sync(self.counts) elif method == 'scorpius': stats, trans = rqtask.scorpius_dr.sync(self.counts) elif method == 'nmf': stats, trans = rqtask.nmf.sync(self.counts) elif method == 'tsne': stats, trans = rqtask.tsne.sync(self.counts, self.get_dr_param()) else: raise NotImplemented return stats, trans def set_color_scale(): score = tcolormap.score method = tcolormap.method self.warn('set color scale {}/{}'.format(method, tcolormap.value)) color_mapper = tcolormap.colormapper bplot.glyph.fill_color['transform'] = color_mapper bplot.data_source.data['score'] = score if not tcolormap.discrete: bcolorbar.color_mapper = color_mapper if tcolormap.discrete: if not bfigure.legend[0].items: bfigure.legend[0].items.append(blegend) else: if bfigure.legend[0].items: bfigure.legend[0].items.pop() bplot.glyph.line_width = 0 bplot.glyph.line_alpha = 0 def _create_title(): cmethod = '{}/{}'.format(tcolormap.method, tcolormap.value) if self.w.drfilter_lower_perc_switch.value: cmethod += '/low%{}'.format( self.w.drfilter_lower_percentage.value) if self.w.drfilter_log_switch.value: cmethod += '/log' cmethod += '/{}/{}'.format(*self.counts.shape) return '{}/{}'.format(self.thelenota.counttable_name, cmethod) def _create_scatter_plot(only_color=False): if (self.w.drfilter_lower_perc_switch.value) or ( self.w.drfilter_log_switch.value): self.counts = filter_counts() else: self.counts = self.thelenota.counttable self.warn("count table: {}".format(str(self.counts.shape))) if not only_color: meta, trans = run_dr_2() self.thelenota.dr = trans self.thelenota._dr_meta = meta self.thelenota._dr_trans = trans col1, col2 = trans.columns[:2] d1 = trans[col1] d2 = trans[col2] title = self.dr_name().replace('_', ' ') m = max(d2.max() - d2.min(), d1.max() - d1.min()) bplot.data_source.data['size'] = [0.008 * m] * nosamples bplot.data_source.data['x'] = d1 bplot.data_source.data['y'] = d2 bfigure.title.text = _create_title() set_color_scale() bokeh_io.push_notebook(handle=bhandle) def run_dr_color(*_): """ refresh dr scatter plot - color only """ self.w.drrun.button_style = 'info' try: _create_scatter_plot(only_color=True) except: self.w.drrun.button_style = 'danger' raise self.w.drrun.button_style = '' def run_dr(*_): self.w.drrun.button_style = 'info' try: _create_scatter_plot() except: self.w.drrun.button_style = 'danger' raise self.w.drrun.button_style = '' tcolormap.on_change = run_dr_color self.w.drrun.on_click(run_dr) # clgenesetchoice_w.observe(run_dr_color, 'value') # clmetadata_w.observe(run_dr_color, 'value') # set up bokeh select_callback = CustomJS(args={'dsource': pdata}, code=""" var indici = dsource.selected['1d'].indices; console.log(indici); IPython.notebook.kernel.execute( 'T._DR_INDICI_SEL_ = ' + indici); """) bokeh_tools = [ bmodels.HoverTool(tooltips=[ ("(x,y)", "($x, $y)"), ("score", "$score"), ("desc", "@desc"), ]), bmodels.BoxSelectTool(callback=select_callback), bmodels.PanTool(), bmodels.WheelZoomTool(), bmodels.BoxZoomTool(), bmodels.LassoSelectTool(callback=select_callback), bmodels.SaveTool(), bmodels.ResetTool(), bmodels.HelpTool(), ] bfigure = bokeh_figure(plot_width=figsize[0], plot_height=figsize[1], tools=bokeh_tools, toolbar_sticky=False, toolbar_location='left', title='dimredplot') bfigure.title.text_color = 'darkgrey' bfigure.title.text_font_style = 'normal' bfigure.title.text_font_size = "12px" self.thelenota._bfigure = bfigure bplot = bfigure.circle(x='x', y='y', radius='size', source=pdata, legend='score', color=dict(field='score', transform=color_mapper)) self.thelenota._bplot = bplot bcolorbar = ColorBar(color_mapper=tcolormap.colormapper, ticker=BasicTicker(), formatter=BasicTickFormatter(precision=1), label_standoff=10, border_line_color=None, location=(0, 0)) bfigure.add_layout(bcolorbar, 'right') blegend = bfigure.legend[0].items[0] bhandle = bokeh_io.show(bfigure, notebook_handle=True) tab_children = [] tab_children.append( widgets.VBox([ ilabel('filter sum%', self.w.drfilter_lower_perc_switch, self.w.drfilter_lower_percentage), ilabel('log tranform', self.w.drfilter_log_switch), ilabel('method', self.w.drmethod), ilabel('perplexity', self.w.drperplex), ilabel('angle', self.w.drangle), ilabel('learning rate', self.w.drlrate), ilabel('early exagg.', self.w.drearly), ilabel('PCA var. cutoff', self.w.dr_tsne_pcavar_cutoff), widgets.HBox([self.w.drrun, self.w.drforce]), ])) tab_children.append(widgets.VBox([tcolormap.prepare_display()])) tab_children.append( widgets.VBox([ ilabel('method', self.w.clu_method), ilabel('dbscan:eps', self.w.clu_dbscan_eps), ilabel('store', self.w.clu_name, self.w.clu_store_go), self.w.clu_go ])) tab_children.append( widgets.VBox([ ilabel('group define', self.w.sl_group_name, self.w.sl_group_set, self.w.sl_group_set_go), ilabel('count extract', self.w.sl_groupextractname, self.w.sl_group_extract_go), ])) tabs = widgets.Tab(children=tab_children) tabs.set_title(0, 'DimRed') tabs.set_title(1, 'Color') tabs.set_title(2, 'Cluster') tabs.set_title(3, 'Select') tabs.selected_index = 0 display(tabs) display(self.w.html) #run a few on_change functions so that all is in sync #on_colormethod_change() run_dr()
def get_image_widget(): w_out = widgets.Output(layout=widgets.Layout(width='50%')) w_layers = widgets.Accordion() fullres_box = widgets.Checkbox( value=False, description='Display full resolution (slow)') figwidth_box = widgets.BoundedFloatText(value=10, min=1, max=1000, description='Width (cm)') def wrapper(object_name): if object_name.startswith('*'): print('Downloading fits files, this may take a few minutes.') object_name = object_name.strip('*') obj = Image(object_name, catalog=catalog) obj.widget_setup_complete = False def plot_function(change=None): if not obj.widget_setup_complete: return w_out.clear_output() with w_out: obj.plot(fullres=fullres_box.value, figsize=(figwidth_box.value, figwidth_box.value)) fullres_box.observe(plot_function, names='value') figwidth_box.observe(plot_function, names='value') w_clr = [] extra_colors = [ 'magenta', 'cyan', 'yellow', 'orange', 'purple', 'pink', 'turquoise', 'lavender' ] if 'optical_red' not in obj.filters: extra_colors = obj.default_colors + extra_colors for x in obj.filters: if x.split('optical_')[-1] in obj.default_colors: clr = x.split('optical_')[-1] else: clr = extra_colors.pop(0) new_layer = ImageLayer(object_name=obj.object, filter_name=x, color=clr, catalog=obj.catalog) obj.append_layer(new_layer) w_clr.append( get_layer_widget(new_layer, plot_function=plot_function)) w_layers.children = w_clr for i, x in enumerate(obj.filters): w_layers.set_title(i, x) obj.widget_setup_complete = True plot_function() object_list = catalog.local_objects + [ '*' + x for x in catalog.remote_objects ] w = interactive(wrapper, object_name=widgets.Dropdown(options=object_list, value='kepler', description='Object', disabled=False)) w_control = widgets.VBox([w, figwidth_box, fullres_box, w_layers], layout=widgets.Layout(width='50%')) w_all = widgets.HBox([w_control, w_out]) return w_all
y_untag = widgets.BoundedFloatText(value=0.2, min=0, max=10, step=0.1, continuous_update=False, description=r'$y_{untag}$ [a.u.]') y_mix = widgets.BoundedFloatText(value=1, min=0, max=10, step=0.1, continuous_update=False, description=r'$y_{mix}$ [a.u.]') name = widgets.Text(value='plots/tagging.eps', continuous_update=False) save = widgets.ToggleButton(value=False, description='Save') # select b_f = widgets.Checkbox(value=True, description=r'$B$$\to$$f$', disabled=False) bbar_f = widgets.Checkbox(value=True, description=r'$\overline{B}$$\to$$f$', disabled=False) bbar_fbar = widgets.Checkbox(value=True, description=r'$\overline{B}$$\to$$\overline{f}$', disabled=False) b_fbar = widgets.Checkbox(value=True, description=r'$B$$\to$$\overline{f}$', disabled=False) fold_amix = widgets.Checkbox(value=True, description=r'fold $A_{mix}$', disabled=False) k_acc = widgets.Checkbox(value=True, description=r'Acceptance', disabled=False) k_res = widgets.Checkbox(value=True, description=r'Resolution', disabled=False)
def __init__(self, arc, ws, line_list=None, wavelength_range=None, flatten_order=None): """Preform identification of a spectrum of an arc lamp and return the wavelength solution for that spectrum. Parameters ---------- arc: Spectrum1D An uncalibrated Spectrum1D of an observation of an arc lamp ws: WavelengthSolution An initial wavelength solution for the arc line_list: Astropy.Table A list of lines in the observed arc wavelength_range: list An initial guess for the wavelength range of the observed arc to be used in creating the artificial spectrum of the arc. flatten_order: int, None If not none, the continuum of the arc observations will be removed. A polynomial of this order will be fit to the continuum and subtracted. Returns ------- ws: WavelengthSolution A calibrated wavelength solution for the observed arc """ #super().__init__() #output = widgets.Output() self.arc = arc self.xarr = self.arc.spectral_axis.value if flatten_order: self.farr = flatspectrum(self.arc.spectral_axis.value, self.arc.flux, order=flatten_order) else: self.farr = self.arc.spectral_axis.value self.ws = ws self.orig_ws = copy.deepcopy(ws) self.arc_color = '#2d34ff' self.line_list = line_list self.xp = [] self.wp = [] self.xpd = [] # saved deleted points self.wpd = [] # TODO: Need to add these values into the options self.textcolor = 'black' self.cdiff = 20 self.sections = 6 self.mdiff = 20 self.wdiff = 20 self.sigma = 5 self.res = 2 self.niter = 5 self.ndstep = 20 # make an artificial version of the line list for plotting self.line_spec = make_artificial_spectra(self.line_list, wavelength_range, wavelength_unit=u.angstrom, flux_unit=u.electron, sigma=self.sigma) # set up the information and control panel color_picker = widgets.ColorPicker(value=self.arc_color, description='Color for Arc') color_picker.observe(self.line_color, 'value') self.pixel_value = widgets.BoundedFloatText(min=0, max=len(self.arc.data), description='pixel', disable=True) self.wavelength_value = widgets.FloatText(description='wavelength', disable=True) self.add_point_button = widgets.Button( description='Add point', disabled=False, button_style='', # 'success', 'info', 'warning', 'danger' or '' tooltip= 'Click button to add a point at this x value and wavelength value', ) self.add_point_button.on_click(self.add_point) self.dispersion_unit = widgets.RadioButtons( options=['pixel', 'wavelength'], value='pixel', description='Dispersion:', disabled=False) self.dispersion_unit.observe(self.set_dispersion, 'value') self.display_artificial = widgets.Checkbox( value=True, description='Display line list', disabled=False, indent=False) self.display_artificial.observe(self.set_display_artifical, 'value') self.display_features = widgets.Checkbox( value=True, description='Display features', disabled=False, indent=False) self.display_features.observe(self.set_display_features, 'value') controls = widgets.VBox([ color_picker, self.pixel_value, self.wavelength_value, self.add_point_button, self.dispersion_unit, self.display_artificial, self.display_features ]) # Set up the initial plot self.fig, self.ax = plt.subplots(constrained_layout=True, figsize=(8, 5)) self.fig.canvas.mpl_connect('button_press_event', self.on_click) self.fig.canvas.mpl_connect('key_press_event', self.on_key_press) self.draw_plot() display(controls)