def slider(self, figsize=(8, 8), exclude_particle_records=['charge', 'mass'], **kw): """ Navigate the simulation using a slider Parameters: ----------- figsize: tuple Size of the figures exclude_particle_records: list of strings List of particle quantities that should not be displayed in the slider (typically because they are less interesting) kw: dict Extra arguments to pass to matplotlib's imshow """ # ----------------------- # Define useful functions # ----------------------- def refresh_field(change=None, force=False): """ Refresh the current field figure Parameters : ------------ change: dictionary Dictionary passed by the widget to a callback functions whenever a change of a widget happens (see docstring of ipywidgets.Widget.observe) This is mainline a place holder ; not used in this function force: bool Whether to force the update """ # Determine whether to do the refresh do_refresh = False if (self.avail_fields is not None): if force or fld_refresh_toggle.value: do_refresh = True # Do the refresh if do_refresh: plt.figure(fld_figure_button.value, figsize=figsize) plt.clf() # When working in inline mode, in an ipython notebook, # clear the output (prevents the images from stacking # in the notebook) if 'inline' in matplotlib.get_backend(): clear_output() if fld_use_button.value: i_power = fld_magnitude_button.value vmin = fld_range_button.value[0] * 10**i_power vmax = fld_range_button.value[1] * 10**i_power else: vmin = None vmax = None self.get_field(t=self.current_t, output=False, plot=True, field=fieldtype_button.value, coord=coord_button.value, m=convert_to_int(mode_button.value), slicing=slicing_button.value, theta=theta_button.value, slicing_dir=slicing_dir_button.value, vmin=vmin, vmax=vmax, cmap=fld_color_button.value) def refresh_ptcl(change=None, force=False): """ Refresh the current particle figure Parameters : ------------ change: dictionary Dictionary passed by the widget to a callback functions whenever a change of a widget happens (see docstring of ipywidgets.Widget.observe) This is mainline a place holder ; not used in this function force: bool Whether to force the update """ # Determine whether to do the refresh do_refresh = False if self.avail_species is not None: if force or ptcl_refresh_toggle.value: do_refresh = True # Do the refresh if do_refresh: plt.figure(ptcl_figure_button.value, figsize=figsize) plt.clf() # When working in inline mode, in an ipython notebook, # clear the output (prevents the images from stacking # in the notebook) if 'inline' in matplotlib.get_backend(): clear_output() if ptcl_use_button.value: i_power = ptcl_magnitude_button.value vmin = ptcl_range_button.value[0] * 10**i_power vmax = ptcl_range_button.value[1] * 10**i_power else: vmin = None vmax = None if ptcl_yaxis_button.value == 'None': # 1D histogram self.get_particle(t=self.current_t, output=False, var_list=[ptcl_xaxis_button.value], select=ptcl_select_widget.to_dict(), species=ptcl_species_button.value, plot=True, vmin=vmin, vmax=vmax, cmap=ptcl_color_button.value, nbins=ptcl_bins_button.value) else: # 2D histogram self.get_particle(t=self.current_t, output=False, var_list=[ ptcl_xaxis_button.value, ptcl_yaxis_button.value ], select=ptcl_select_widget.to_dict(), species=ptcl_species_button.value, plot=True, vmin=vmin, vmax=vmax, cmap=ptcl_color_button.value, nbins=ptcl_bins_button.value) def refresh_field_type(change): """ Refresh the field type and disable the coordinates buttons if the field is scalar. Parameter --------- change: dictionary Dictionary passed by the widget to a callback functions whenever a change of a widget happens (see docstring of ipywidgets.Widget.observe) """ if self.avail_fields[change['new']] == 'scalar': coord_button.disabled = True elif self.avail_fields[change['new']] == 'vector': coord_button.disabled = False refresh_field() def refresh_species(change=None): """ Refresh the particle species buttons by populating them with the available records for the current species Parameter --------- change: dictionary Dictionary passed by the widget to a callback functions whenever a change of a widget happens (see docstring of ipywidgets.Widget.observe) """ # Deactivate the particle refreshing to avoid callback # while modifying the widgets saved_refresh_value = ptcl_refresh_toggle.value ptcl_refresh_toggle.value = False # Get available records for this species avail_records = [ q for q in self.avail_record_components[ ptcl_species_button.value] if q not in exclude_particle_records ] # Update the plotting buttons ptcl_xaxis_button.options = avail_records ptcl_yaxis_button.options = avail_records + ['None'] if ptcl_xaxis_button.value not in ptcl_xaxis_button.options: ptcl_xaxis_button.value = avail_records[0] if ptcl_yaxis_button.value not in ptcl_yaxis_button.options: ptcl_yaxis_button.value = 'None' # Update the selection widgets for dropdown_button in ptcl_select_widget.quantity: dropdown_button.options = avail_records # Put back the previous value of the refreshing button ptcl_refresh_toggle.value = saved_refresh_value def change_t(change): "Plot the result at the required time" self.current_t = 1.e-15 * change['new'] refresh_field() refresh_ptcl() def step_fw(b): "Plot the result one iteration further" if self.current_i < len(self.t) - 1: self.current_t = self.t[self.current_i + 1] else: self.current_t = self.t[self.current_i] slider.value = self.current_t * 1.e15 def step_bw(b): "Plot the result one iteration before" if self.current_t > 0: self.current_t = self.t[self.current_i - 1] else: self.current_t = self.t[self.current_i] slider.value = self.current_t * 1.e15 # --------------- # Define widgets # --------------- # Slider slider = widgets.FloatSlider( min=math.ceil(1.e15 * self.tmin), max=math.ceil(1.e15 * self.tmax), step=math.ceil(1.e15 * (self.tmax - self.tmin)) / 20., description="t (fs)") slider.observe(change_t, names='value', type='change') # Forward button button_p = widgets.Button(description="+") button_p.on_click(step_fw) # Backward button button_m = widgets.Button(description="-") button_m.on_click(step_bw) # Display the time widgets container = widgets.HBox(children=[button_m, button_p, slider]) display(container) # Field widgets # ------------- if (self.avail_fields is not None): # Field type # ---------- # Field button fieldtype_button = widgets.ToggleButtons( description='Field:', options=sorted(self.avail_fields.keys())) fieldtype_button.observe(refresh_field_type, 'value', 'change') # Coord button if self.geometry == "thetaMode": coord_button = widgets.ToggleButtons( description='Coord:', options=['x', 'y', 'z', 'r', 't']) elif self.geometry in ["2dcartesian", "3dcartesian"]: coord_button = widgets.ToggleButtons(description='Coord:', options=['x', 'y', 'z']) coord_button.observe(refresh_field, 'value', 'change') # Mode and theta button (for thetaMode) mode_button = widgets.ToggleButtons(description='Mode:', options=self.avail_circ_modes) mode_button.observe(refresh_field, 'value', 'change') theta_button = widgets.FloatSlider(width=140, value=0., description=r'Theta:', min=-math.pi / 2, max=math.pi / 2) theta_button.observe(refresh_field, 'value', 'change') # Slicing buttons (for 3D) slicing_dir_button = widgets.ToggleButtons( value=self.axis_labels[1], options=self.axis_labels, description='Slicing direction:') slicing_dir_button.observe(refresh_field, 'value', 'change') slicing_button = widgets.FloatSlider(width=150, description='Slicing:', min=-1., max=1., value=0.) slicing_button.observe(refresh_field, 'value', 'change') # Plotting options # ---------------- # Figure number fld_figure_button = widgets.IntText(description='Figure ', value=0, width=50) # Range of values fld_range_button = widgets.FloatRangeSlider(min=-10, max=10, width=220) fld_range_button.observe(refresh_field, 'value', 'change') # Order of magnitude fld_magnitude_button = widgets.IntText(description='x 10^', value=9, width=50) fld_magnitude_button.observe(refresh_field, 'value', 'change') # Use button fld_use_button = widgets.Checkbox(description=' Use this range', value=False) fld_use_button.observe(refresh_field, 'value', 'change') # Colormap button fld_color_button = widgets.Select(options=sorted( plt.cm.datad.keys()), height=50, width=200, value='jet') fld_color_button.observe(refresh_field, 'value', 'change') # Resfresh buttons fld_refresh_toggle = widgets.ToggleButton( description='Always refresh', value=True) fld_refresh_button = widgets.Button(description='Refresh now!') fld_refresh_button.on_click(partial(refresh_field, force=True)) # Containers # ---------- # Field type container if self.geometry == "thetaMode": container_fields = widgets.VBox(width=260, children=[ fieldtype_button, coord_button, mode_button, theta_button ]) elif self.geometry == "2dcartesian": container_fields = widgets.VBox( width=260, children=[fieldtype_button, coord_button]) elif self.geometry == "3dcartesian": container_fields = widgets.VBox(width=260, children=[ fieldtype_button, coord_button, slicing_dir_button, slicing_button ]) # Plotting options container container_fld_plots = widgets.VBox(width=260, children=[ fld_figure_button, fld_range_button, widgets.HBox(children=[ fld_magnitude_button, fld_use_button ], height=50), fld_color_button ]) # Accordion for the field widgets accord1 = widgets.Accordion( children=[container_fields, container_fld_plots]) accord1.set_title(0, 'Field type') accord1.set_title(1, 'Plotting options') # Complete field container container_fld = widgets.VBox( width=300, children=[ accord1, widgets.HBox( children=[fld_refresh_toggle, fld_refresh_button]) ]) # Particle widgets # ---------------- if (self.avail_species is not None): # Particle quantities # ------------------- # Species selection ptcl_species_button = widgets.Dropdown(width=250, options=self.avail_species) ptcl_species_button.observe(refresh_species, 'value', 'change') # Get available records for this species avail_records = [ q for q in self.avail_record_components[ ptcl_species_button.value] if q not in exclude_particle_records ] # Particle quantity on the x axis ptcl_xaxis_button = widgets.ToggleButtons(options=avail_records) ptcl_xaxis_button.observe(refresh_ptcl, 'value', 'change') # Particle quantity on the y axis ptcl_yaxis_button = widgets.ToggleButtons(options=avail_records + ['None'], value='None') ptcl_yaxis_button.observe(refresh_ptcl, 'value', 'change') # Particle selection # ------------------ # 3 selection rules at maximum ptcl_select_widget = ParticleSelectWidget(3, avail_records, refresh_ptcl) # Plotting options # ---------------- # Figure number ptcl_figure_button = widgets.IntText(description='Figure ', value=1, width=50) # Number of bins ptcl_bins_button = widgets.IntText(description='nbins:', value=100, width=100) ptcl_bins_button.observe(refresh_ptcl, 'value', 'change') # Colormap button ptcl_color_button = widgets.Select(options=sorted( plt.cm.datad.keys()), height=50, width=200, value='Blues') ptcl_color_button.observe(refresh_ptcl, 'value', 'change') # Range of values ptcl_range_button = widgets.FloatRangeSlider(min=0, max=10, width=220, value=(0, 5)) ptcl_range_button.observe(refresh_ptcl, 'value', 'change') # Order of magnitude ptcl_magnitude_button = widgets.IntText(description='x 10^', value=9, width=50) ptcl_magnitude_button.observe(refresh_ptcl, 'value', 'change') # Use button ptcl_use_button = widgets.Checkbox(description=' Use this range', value=False) ptcl_use_button.observe(refresh_ptcl, 'value', 'change') # Resfresh buttons ptcl_refresh_toggle = widgets.ToggleButton( description='Always refresh', value=True) ptcl_refresh_button = widgets.Button(description='Refresh now!') ptcl_refresh_button.on_click(partial(refresh_ptcl, force=True)) # Containers # ---------- # Particle quantity container container_ptcl_quantities = widgets.VBox(width=310, children=[ ptcl_species_button, ptcl_xaxis_button, ptcl_yaxis_button ]) # Particle selection container container_ptcl_select = ptcl_select_widget.to_container() # Plotting options container container_ptcl_plots = widgets.VBox(width=310, children=[ ptcl_figure_button, ptcl_bins_button, ptcl_range_button, widgets.HBox(children=[ ptcl_magnitude_button, ptcl_use_button ], height=50), ptcl_color_button ]) # Accordion for the field widgets accord2 = widgets.Accordion(children=[ container_ptcl_quantities, container_ptcl_select, container_ptcl_plots ]) accord2.set_title(0, 'Particle quantities') accord2.set_title(1, 'Particle selection') accord2.set_title(2, 'Plotting options') # Complete particle container container_ptcl = widgets.VBox( width=370, children=[ accord2, widgets.HBox( children=[ptcl_refresh_toggle, ptcl_refresh_button]) ]) # Global container if (self.avail_fields is not None) and \ (self.avail_species is not None): global_container = widgets.HBox( children=[container_fld, container_ptcl]) display(global_container) elif self.avail_species is None: display(container_fld) elif self.avail_fields is None: display(container_ptcl)
def __init__(self, initialize, success_condition, allowed_gates, vi, qubit_names, eps=0.1, backend=Aer.get_backend('qasm_simulator'), shots=1024, mode='circle', verbose=False): """ initialize List of gates applied to the initial 00 state to get the starting state of the puzzle. Supported single qubit gates (applied to qubit '0' or '1') are 'x', 'y', 'z', 'h', 'ry(pi/4)'. Supported two qubit gates are 'cz' and 'cx'. Specify only the target qubit. success_condition Values for pauli observables that must be obtained for the puzzle to declare success. allowed_gates For each qubit, specify which operations are allowed in this puzzle. 'both' should be used only for operations that don't need a qubit to be specified ('cz' and 'unbloch'). Gates are expressed as a dict with an int as value. If this is non-zero, it specifies the number of times the gate is must be used (no more or less) for the puzzle to be successfully solved. If the value is zero, the player can use the gate any number of times. vi Some visualization information as a three element list. These specify: * which qubits are hidden (empty list if both shown). * whether both circles shown for each qubit (use True for qubit puzzles and False for bit puzzles). * whether the correlation circles (the four in the middle) are shown. qubit_names The two qubits are always called '0' and '1' from the programming side. But for the player, we can display different names. eps=0.1 How close the expectation values need to be to the targets for success to be declared. backend=Aer.get_backend('qasm_simulator') Backend to be used by Qiskit to calculate expectation values (defaults to local simulator). shots=1024 Number of shots used to to calculate expectation values. mode='circle' Either the standard 'Hello Quantum' visualization can be used (with mode='circle') or the alternative line based one (mode='line'). verbose=False """ def get_total_gate_list(): # Get a text block describing allowed gates. total_gate_list = "" for qubit in allowed_gates: gate_list = "" for gate in allowed_gates[qubit]: if required_gates[qubit][gate] > 0: gate_list += ' ' + gate + " (use " + str( required_gates[qubit][gate]) + " time" + "s" * ( required_gates[qubit][gate] > 1) + ")" elif allowed_gates[qubit][gate] == 0: gate_list += ' ' + gate + ' ' if gate_list != "": if qubit == "both": gate_list = "\nAllowed symmetric operations:" + gate_list else: gate_list = "\nAllowed operations for " + qubit_names[ qubit] + ":\n" + " " * 10 + gate_list total_gate_list += gate_list + "\n" return total_gate_list def get_success(required_gates): # Determine whether the success conditions are satisfied, both for expectation values, and the number of gates to be used. success = True grid.get_rho() if verbose: print(grid.rho) for pauli in success_condition: success = success and ( abs(success_condition[pauli] - grid.rho[pauli]) < eps) for qubit in required_gates: for gate in required_gates[qubit]: success = success and (required_gates[qubit][gate] == 0) return success def get_command(gate, qubit): # For a given gate and qubit, return the string describing the corresoinding Qiskit string. if qubit == 'both': qubit = '1' qubit_name = qubit_names[qubit] for name in qubit_names.values(): if name != qubit_name: other_name = name # then make the command (both for the grid, and for printing to screen) if gate in ['x', 'y', 'z', 'h']: real_command = 'grid.qc.' + gate + '(grid.qr[' + qubit + '])' clean_command = 'qc.' + gate + '(' + qubit_name + ')' elif gate in ['ry(pi/4)', 'ry(-pi/4)']: real_command = 'grid.qc.ry(' + '-' * ( gate == 'ry(-pi/4)') + 'np.pi/4,grid.qr[' + qubit + '])' clean_command = 'qc.ry(' + '-' * ( gate == 'ry(-pi/4)') + 'np.pi/4,' + qubit_name + ')' elif gate in ['cz', 'cx', 'swap']: real_command = 'grid.qc.' + gate + '(grid.qr[' + '0' * ( qubit == '1') + '1' * ( qubit == '0') + '],grid.qr[' + qubit + '])' clean_command = 'qc.' + gate + '(' + other_name + ',' + qubit_name + ')' return [real_command, clean_command] clear_output() bloch = [None] # set up initial state and figure grid = pauli_grid(backend=backend, shots=shots, mode=mode) for gate in initialize: eval(get_command(gate[0], gate[1])[0]) required_gates = copy.deepcopy(allowed_gates) # determine which qubits to show in figure if allowed_gates[ '0'] == {}: # if no gates are allowed for qubit 0, we know to only show qubit 1 shown_qubit = 1 elif allowed_gates['1'] == {}: # and vice versa shown_qubit = 0 else: shown_qubit = 2 # show figure grid.update_grid(bloch=bloch[0], hidden=vi[0], qubit=vi[1], corr=vi[2], message=get_total_gate_list()) description = { 'gate': ['Choose gate'], 'qubit': ['Choose ' + 'qu' * vi[1] + 'bit'], 'action': ['Make it happen!'] } all_allowed_gates_raw = [] for q in ['0', '1', 'both']: all_allowed_gates_raw += list(allowed_gates[q]) all_allowed_gates_raw = list(set(all_allowed_gates_raw)) all_allowed_gates = [] for g in ['bloch', 'unbloch']: if g in all_allowed_gates_raw: all_allowed_gates.append(g) for g in ['x', 'y', 'z', 'h', 'cz', 'cx']: if g in all_allowed_gates_raw: all_allowed_gates.append(g) for g in all_allowed_gates_raw: if g not in all_allowed_gates: all_allowed_gates.append(g) gate = widgets.ToggleButtons(options=description['gate'] + all_allowed_gates) qubit = widgets.ToggleButtons(options=['']) action = widgets.ToggleButtons(options=['']) boxes = widgets.VBox([gate, qubit, action]) display(boxes) if vi[1]: print('\nYour quantum program so far\n') self.program = [] def given_gate(a): # Action to be taken when gate is chosen. This sets up the system to choose a qubit. if gate.value: if gate.value in allowed_gates['both']: qubit.options = description['qubit'] + ["not required"] qubit.value = "not required" else: allowed_qubits = [] for q in ['0', '1']: if (gate.value in allowed_gates[q]) or ( gate.value in allowed_gates['both']): allowed_qubits.append(q) allowed_qubit_names = [] for q in allowed_qubits: allowed_qubit_names += [qubit_names[q]] qubit.options = description['qubit'] + allowed_qubit_names def given_qubit(b): # Action to be taken when qubit is chosen. This sets up the system to choose an action. if qubit.value not in ['', description['qubit'][0], 'Success!']: action.options = description['action'] + ['Apply operation'] def given_action(c): # Action to be taken when user confirms their choice of gate and qubit. # This applied the command, updates the visualization and checks whether the puzzle is solved. if action.value not in ['', description['action'][0]]: # apply operation if action.value == 'Apply operation': if qubit.value not in [ '', description['qubit'][0], 'Success!' ]: # translate bit gates to qubit gates if gate.value == 'NOT': q_gate = 'x' elif gate.value == 'CNOT': q_gate = 'cx' else: q_gate = gate.value if qubit.value == "not required": q = qubit_names['1'] else: q = qubit.value q01 = '0' * (qubit.value == qubit_names['0']) + '1' * ( qubit.value == qubit_names['1']) + 'both' * ( qubit.value == "not required") if q_gate in ['bloch', 'unbloch']: if q_gate == 'bloch': bloch[0] = q01 else: bloch[0] = None else: command = get_command(q_gate, q01) eval(command[0]) if vi[1]: print(command[1]) self.program.append(command[1]) if required_gates[q01][gate.value] > 0: required_gates[q01][gate.value] -= 1 grid.update_grid(bloch=bloch[0], hidden=vi[0], qubit=vi[1], corr=vi[2], message=get_total_gate_list()) success = get_success(required_gates) if success: gate.options = ['Success!'] qubit.options = ['Success!'] action.options = ['Success!'] plt.close(grid.fig) else: gate.value = description['gate'][0] qubit.options = [''] action.options = [''] gate.observe(given_gate) qubit.observe(given_qubit) action.observe(given_action)
def PairwiseDensityPlot(df, X=None, Y=None, scatter=True, width=800, height=800): if not X: X = df.select_dtypes( include=['bool', 'float', 'int', 'category']).columns.values if not Y: Y = df.select_dtypes( include=['bool', 'float', 'int', 'category']).columns.values assert Basic.isCollection( X ), "X needs to be a collection type. If inputting only a single value, add it as a list." assert Basic.isCollection( Y ), "Y needs to be a collection type. If inputting only a single value, add it as a list." x = df[X[0]].values y = df[Y[0]].values x_dropdown = widgets.Dropdown( options=X, value=X[0], description='X Axis Value:', ) y_dropdown = widgets.Dropdown( options=Y, value=Y[0], description='Y Axis Value:', ) container = widgets.HBox([x_dropdown, y_dropdown]) #start of figure traces = [] traces.append( go.Histogram2dContour(x=x, y=y, colorscale='Blues', reversescale=True, xaxis='x', yaxis='y', name='Contour', showscale=False)) if scatter: #with enough datapoints, this will slow down the graph build or make it look fugly. traces.append( go.Scatter(x=x, y=y, xaxis='x', yaxis='y', mode='markers', marker=dict(color='rgba(0,0,0,0.2)', size=2), name='Data', hoverinfo='skip')) traces.append( go.Histogram(y=y, xaxis='x2', marker=dict(color='rgba(0,0,0,1)'), name='Histogram', orientation='h')) traces.append( go.Histogram(x=x, yaxis='y2', marker=dict(color='rgba(0,0,0,1)'), name='Histogram')) layout = go.Layout(autosize=True, width=width, height=height, xaxis=dict(zeroline=False, domain=[0, 0.85], showgrid=False), yaxis=dict(zeroline=False, domain=[0, 0.85], showgrid=False), xaxis2=dict(zeroline=False, domain=[0.85, 1], showgrid=False), yaxis2=dict(zeroline=False, domain=[0.85, 1], showgrid=False), bargap=0, showlegend=False, margin=dict(l=10, r=10, t=10, b=10)) g = go.FigureWidget(data=traces, layout=layout) def response(change): x = df[x_dropdown.value].values y = df[y_dropdown.value].values update_range = 4 if scatter else 3 yhist = 1 if scatter else 2 with g.batch_update(): for i in range(0, update_range): g.data[i].x = x g.data[i].y = y g.layout.xaxis.title = x_dropdown.value g.layout.yaxis.title = y_dropdown.value x_dropdown.observe(response, names="value") y_dropdown.observe(response, names="value") Title( title="Interactive Density Plot", description="Select values for X and Y axis to view varying densities." ) display(widgets.VBox([container, g])) return
def plots_tab(self, output): db = self llegend_format = widgets.Text(value=str(self.vars.get('legend_format', '')), description='', disabled=False) ltitle_format = widgets.Text(value=str(self.vars.get('title_format', '')), description='', disabled=False) lcmap = widgets.Text(value=str(self.vars.get('cmap', 'jet')), description='cmap:', layout=self.layout_dropdown, disabled=False) llog_metric_list = widgets.Text(value=str( self.vars.get('log_metric_list', '[train_loss]')), description='log_metric_list:', disabled=False) bdownload = widgets.Button(description="Download Plots") bdownload_out = widgets.Output(layout=self.layout_button) def on_download_clicked(b): fname = 'plots.pdf' from matplotlib.backends.backend_pdf import PdfPages import matplotlib.pyplot as plt pp = PdfPages(fname) for fig in self.rm_original.fig_list: fig.savefig(pp, format='pdf') pp.close() bdownload_out.clear_output() with bdownload_out: display(FileLink(fname, result_html_prefix="Download: ")) bdownload.on_click(on_download_clicked) h22 = widgets.Label( value="Format:", layout=widgets.Layout(width='340px'), ) h33 = widgets.Label( value="Format:", layout=widgets.Layout(width='340px'), ) h44 = widgets.Label( value="", layout=widgets.Layout(width='340px'), ) space = widgets.Label( value="", layout=widgets.Layout(width='300px'), ) brefresh = widgets.Button(description="Display Plot") d_avg_across_txt = widgets.Label(value="avg_across:", ) w_y_metrics = wdg.SelectMultiple(header="Y-axis Metrics:", options=self.rm_original.score_keys, db_vars=db.vars, var='y_metrics') if db.vars.get('legend_list') is None: db.vars['legend_list'] = hu.get_diff_hparam(db.rm.exp_list) w_legend = wdg.SelectMultiple(header="Legend:", options=['exp_id'] + db.rm.exp_params, db_vars=db.vars, var='legend_list', select_all=True) w_title = wdg.SelectMultiple(header="Title:", options=db.rm.exp_params, db_vars=db.vars, var='title_list') w_groupby = wdg.SelectMultiple(header="GroupBy:", options=['None'] + db.rm.exp_params, db_vars=db.vars, var='groupby_list') w_x_metric = wdg.Dropdown(header='X-axis Metric', options=self.rm_original.score_keys, db_vars=db.vars, var='x_metric') w_mode = wdg.Dropdown(header='Plot Mode', options=['line', 'bar'], db_vars=db.vars, var='mode') w_bar_agg = wdg.Dropdown(header='Plot Agg (bar plot only) ', options=['last', 'max', 'mean', 'min'], db_vars=db.vars, var='bar_agg') w_avg_across = wdg.Dropdown(header='Avg Across', options=['None'] + db.rm.exp_params, db_vars=db.vars, var='avg_across') button = widgets.VBox([ widgets.HBox([ w_y_metrics.get_widget(), w_legend.get_widget(), w_title.get_widget(), w_groupby.get_widget(), ]), widgets.HBox([ w_x_metric.get_widget(), w_mode.get_widget(), w_bar_agg.get_widget(), w_avg_across.get_widget(), ]), # widgets.HBox([ d_avg_across_txt, d_avg_across_columns, ]), widgets.HBox([ brefresh, bdownload, bdownload_out, ]), ]) # button = widgets.VBox([widgets.HBox([brefresh, bdownload, bdownload_out]), # widgets.HBox([t_y_metric, d_x_metric_columns]), # widgets.HBox([t_title_list, d_style]), # widgets.HBox([t_groupby_list, llegend_list, ]), # widgets.HBox([t_mode, t_bar_agg]), # widgets.HBox([ltitle_format, llegend_format]), # widgets.HBox([d_avg_across_columns]), # ]) output_plot = widgets.Output() def on_clicked(b): # if d_style.value == 'True': # from IPython import get_ipython # ipython = get_ipython() # ipython.magic("matplotlib widget") output_plot.clear_output() with output_plot: self.update_rm() w, h = 10, 5 if len(w_y_metrics.update()) == 0: display('No results saved yet.') return elif len(w_y_metrics.update()) > 1: figsize = (2 * int(w), int(h)) self.vars['figsize'] = figsize else: self.vars['figsize'] = (int(w), int(h)) self.vars['legend_format'] = llegend_format.value self.vars['log_metric_list'] = hu.get_list_from_str( llog_metric_list.value) self.vars['title_format'] = ltitle_format.value self.vars['cmap'] = lcmap.value self.rm_original.fig_list = self.rm.get_plot_all( y_metric_list=w_y_metrics.update(), x_metric=w_x_metric.update(), groupby_list=w_groupby.update(), legend_list=w_legend.update(), log_metric_list=self.vars['log_metric_list'], mode=w_mode.update(), bar_agg=w_bar_agg.update(), figsize=self.vars['figsize'], title_list=w_title.update(), legend_format=self.vars['legend_format'], title_format=self.vars['title_format'], cmap=self.vars['cmap'], avg_across=w_avg_across.update()) show_inline_matplotlib_plots() # d_style.observe(on_clicked) brefresh.on_click(on_clicked) with output: display(button) display(output_plot)
from IPython.display import display, clear_output # In[30]: get_ipython().run_cell_magic('capture', '', "%matplotlib inline\n\nfig, axes = plt.subplots(1, 2, figsize=(12, 9))\nfor ax in axes:\n ax.axis('off')\n\nout = widgets.Output()\ndef show_channel(c):\n channel = y[0, ordered_idxs[c]]\n axes[0].imshow(channel)\n axes[0].title.set_text(f'Y | min: {channel.min():.2f} | max: {channel.max():.2f}')\n \n channel = y_hat[0, ordered_idxs[c]]\n axes[1].imshow(channel)\n axes[1].title.set_text(f'Yhat | min: {channel.min():.2f} | max: {channel.max():.2f}')\n with out:\n clear_output(wait=True)\n display(fig)") # In[31]: slider = widgets.IntSlider(min=0, max=y.size(1)-1, step=1,continuous_update=False, description='Channel idx') slider.observe(lambda ev: show_channel(slider.value)) show_channel(0) display(widgets.VBox([out, slider])) # ## Quantized vs continuous latent # In[32]: with torch.no_grad(): x_hat_y = net.g_s(y).clamp_(0, 1) x_hat_y_hat = net.g_s(y_hat).clamp_(0, 1) # In[33]:
def images_tab(self, output): db = self w_legend = wdg.SelectMultiple(header="Legend:", options=db.rm.exp_params, db_vars=db.vars, var='legend_list') w_n_exps = wdg.Text('n_exps:', default='3', type='int', db_vars=db.vars, var='n_exps') w_n_images = wdg.Text('n_images:', default='5', type='int', db_vars=db.vars, var='n_images') w_figsize = wdg.Text('figsize:', default='(10,5)', type='tuple', db_vars=db.vars, var='figsize') w_dirname = wdg.Text('dirname:', default='images', type='str', db_vars=db.vars, var='dirname') bdownload = widgets.Button(description="Download Images", layout=self.layout_button) bdownload_out = widgets.Output(layout=self.layout_button) brefresh = widgets.Button(description="Display Images") button = widgets.VBox([ widgets.HBox([ w_legend.get_widget(), w_n_exps.get_widget(), w_n_images.get_widget(), w_figsize.get_widget(), w_dirname.get_widget() ]), widgets.HBox([brefresh, bdownload, bdownload_out]), ]) output_plot = widgets.Output() with output: display(button) display(output_plot) def on_clicked(b): output_plot.clear_output() with output_plot: self.update_rm() self.rm_original.fig_image_list = self.rm.get_images( legend_list=w_legend.update(), n_images=w_n_images.update(), n_exps=w_n_exps.update(), figsize=w_figsize.update(), dirname=w_dirname.update()) show_inline_matplotlib_plots() brefresh.on_click(on_clicked) def on_download_clicked(b): fname = 'images' from matplotlib.backends.backend_pdf import PdfPages import matplotlib.pyplot as plt pp = PdfPages(fname) for fig in self.rm_original.fig_image_list: fig.savefig(pp, format='pdf') pp.close() bdownload_out.clear_output() with bdownload_out: display(FileLink(fname, result_html_prefix="Download: ")) bdownload.on_click(on_download_clicked)
def display_widgets(self): self._create_widgets() self.out = widgets.Output( ) # this is the output widget in which the df is displayed display(widgets.VBox([self.sel_file, self.button, self.out]))
def createUI(self): # ------------ # Callbacks + logic # ------------ # Return if image should be shown def image_passes_filters(imageIndex): boPredCorrect = self.dataset.images[ imageIndex].label == self.lutId2Label[ self.predLabels[imageIndex]] if (boPredCorrect and self.wFilterCorrect.value) or ( not boPredCorrect and self.wFilterWrong.value): return True return False # Next / previous image button callback def button_pressed(obj): step = int(obj.value) self.visImageIndex += step self.visImageIndex = min(max(0, self.visImageIndex), int(len(self.predLabels)) - 1) while not image_passes_filters(self.visImageIndex): self.visImageIndex += step if self.visImageIndex <= 0 or self.visImageIndex >= int( len(self.predLabels)) - 1: break self.visImageIndex = min(max(0, self.visImageIndex), int(len(self.predLabels)) - 1) self.wImageSlider.value = self.visImageIndex self.updateUI() # Image slider callback. Need to wrap in try statement to avoid errors when slider value is not a number. def slider_changed(obj): try: self.visImageIndex = int(obj['new']['value']) self.updateUI() except Exception as e: pass # ------------ # UI - image + controls (left side) # ------------ wNextImageButton = widgets.Button(description="Image +1") wNextImageButton.value = "1" wNextImageButton.layout = Layout(width='80px') wNextImageButton.on_click(button_pressed) wPreviousImageButton = widgets.Button(description="Image -1") wPreviousImageButton.value = "-1" wPreviousImageButton.layout = Layout(width='80px') wPreviousImageButton.on_click(button_pressed) self.wImageSlider = IntSlider(min=0, max=len(self.predLabels) - 1, step=1, value=self.visImageIndex, continuous_update=False) self.wImageSlider.observe(slider_changed) self.wImageHeader = widgets.Text("", layout=Layout(width="130px")) self.wImg = widgets.Image() imgWidth = 400 self.wImg.layout.width = str(imgWidth) + 'px' # '500px' wImageWithHeader = widgets.VBox(children=[ widgets.HBox(children=[ wPreviousImageButton, wNextImageButton, self.wImageSlider ]), self.wImg ], width=imgWidth + 20) # ------------ # UI - info (right side) # ------------ wFilterHeader = widgets.HTML( value="Filters (use Image +1/-1 buttons for navigation):") self.wFilterCorrect = widgets.Checkbox( value=True, description='Correct classifications') self.wFilterWrong = widgets.Checkbox( value=True, description='Incorrect classifications') wGtHeader = widgets.HTML(value="Ground truth:") self.wGtLabel = widgets.Text(value="", description="Label:") wPredHeader = widgets.HTML(value="Prediction:") self.wPredLabel = widgets.Text(value="", description="Label:") self.wPredScore = widgets.Text(value="", description="Score:") wInfoHeader = widgets.HTML(value="Image info:") self.wIndex = widgets.Text(value="", description="Index:") self.wFilename = widgets.Text(value="", description="Name:") wScoresHeader = widgets.HTML(value="Classification scores:") self.wScores = bqPyplot.figure() self.wScores.layout.height = '250px' self.wScores.layout.width = '370px' self.wScores.fig_margin = { "top": 5, "bottom": 80, "left": 30, "right": 5 } # Combine UIs into tab widget wInfoHBox = widgets.VBox(children=[ wFilterHeader, self.wFilterCorrect, self.wFilterWrong, wGtHeader, self.wGtLabel, wPredHeader, self.wPredLabel, self.wPredScore, wInfoHeader, self.wIndex, self.wFilename, wScoresHeader, self.wScores ]) wInfoHBox.layout.padding = '20px' visTabsUI = widgets.Tab( children=[widgets.HBox(children=[wImageWithHeader, wInfoHBox])]) visTabsUI.set_title(0, 'Results viewer') # Fill UI with content self.updateUI() return (visTabsUI)
def render(self) -> widgets.VBox: items = [self.content["top"].render()] if self.content["bottom"] is not None: items.append(self.content["bottom"].render()) return widgets.VBox(items)
def get_widget(self): return widgets.VBox([self.header, self.select_multiple])
def initiate(self): tab_children = [] ########################### # data 1 box d1_vbox_childs = [] ## ### d1_button_next = widgets.Button(description='next measurement') d1_button_prev = widgets.Button(description='prev measurement') d1_button_next.on_click(self.on_d1_botton_next) d1_button_prev.on_click(self.on_d1_botton_prev) d1_dropdown_fnames_options = [ i.name for i in self.controller.data.dataset1.path2data_list ] d1_dropdown_fnames_value = self.controller.data.dataset1.path2active.name self.d1_dropdown_fnames = widgets.Dropdown( options=d1_dropdown_fnames_options, value=d1_dropdown_fnames_value, # description='N', # disabled=disable_data_2, ) self.d1_dropdown_fnames.observe(self.on_change_d1_dropdown_fnames) d1_box_h_1 = widgets.HBox( [d1_button_prev, d1_button_next, self.d1_dropdown_fnames]) ### d1_vbox_childs.append(d1_box_h_1) ## ### d1_text_path = widgets.Text(placeholder='path name', disabled=False) self.d1_text_path = d1_text_path d1_vbox_childs.append(d1_text_path) ## d1_vbox = widgets.VBox(d1_vbox_childs) tab_children.append({'element': d1_vbox, 'title': 'iMet'}) ############################ # data 2 box if isinstance(self.controller.data.dataset2, type(None)): disable_data_2 = True d2_dropdown_fnames_options = [] d2_dropdown_fnames_value = None else: disable_data_2 = False d2_dropdown_fnames_options = [ i.name for i in self.controller.data.dataset2.path2data_list ] d2_dropdown_fnames_value = self.controller.data.dataset2.path2active.name d2_vbox_childs = [] ## ### d2_button_next = widgets.Button(description='next measurement', disabled=disable_data_2) d2_button_prev = widgets.Button(description='prev measurement', disabled=disable_data_2) self.d2_dropdown_fnames = widgets.Dropdown( options=d2_dropdown_fnames_options, value=d2_dropdown_fnames_value, # description='N', disabled=disable_data_2, ) d2_button_next.on_click(self.on_d2_botton_next) d2_button_prev.on_click(self.on_d2_botton_prev) self.d2_dropdown_fnames.observe(self.on_change_d2_dropdown_fnames) d2_box_h_1 = widgets.HBox( [d2_button_prev, d2_button_next, self.d2_dropdown_fnames]) ### d2_vbox_childs.append(d2_box_h_1) ## ### # text field showing the path d2_text_path = widgets.Text(placeholder='path name', disabled=False) self.d2_text_path = d2_text_path d2_vbox_childs.append(d2_text_path) ## d2_vbox = widgets.VBox(d2_vbox_childs) tab_children.append({'element': d2_vbox, 'title': 'POPS'}) # others box # Tab tab = widgets.Tab([child['element'] for child in tab_children]) for e, child in enumerate(tab_children): tab.set_title(e, child['title']) # accordeon self.accordeon_start = widgets.Text(value='', placeholder='hit z key', description='start:', disabled=False) self.accordeon_end = widgets.Text(value='', placeholder='hit x key', description='end:', disabled=False) self.accordeon_alt = widgets.Text(value='', placeholder='hit a key', description='altitude:', disabled=False) hbox_accordeon_start_stop = widgets.HBox( [self.accordeon_start, self.accordeon_end]) self.dropdown_gps_bar_bad = widgets.Dropdown( options=[ 'gps', 'baro', 'bad', 'bad_but_usable_gps', 'bad_but_usable_baro' ], value='gps', description='which alt to use:', disabled=False, ) self.button_save_unsave_flight = widgets.Button( description='save/unsave flight') # button_bind_measurements.on_click(self.deprecated_on_button_bind_measurements) self.button_save_unsave_flight.on_click(self.on_button_save_flight) hbox_accordeon_alt_source = widgets.HBox( [self.dropdown_gps_bar_bad, self.accordeon_alt]) # self.accordeon_assigned = widgets.Valid(value=False, # description='bound?', # ) # # # self.inttext_deltat = widgets.IntText(value=0, # description='deltat', # disabled=False # ) # self.inttext_deltat.observe(self.on_inttext_deltat) # # self.button_bind_measurements = widgets.ToggleButton(description = 'bind/unbind measurements') # # button_bind_measurements.on_click(self.deprecated_on_button_bind_measurements) # self.button_bind_measurements.observe(self.on_button_bind_measurements) # # # accordon_box = widgets.VBox( [ hbox_accordeon_start_stop, hbox_accordeon_alt_source, self.button_save_unsave_flight ] ) #[self.accordeon_assigned, self.dropdown_popssn, self.inttext_deltat, self.button_bind_measurements]) accordion_children = [accordon_box] accordion = widgets.Accordion(children=accordion_children) accordion.set_title(0, 'do_stuff') # messages self.messages = widgets.Textarea('\n'.join(self.controller._message), layout={'width': '100%'}) # message_box = widgets.HBox([self.messages]) # OverVbox overVbox = widgets.VBox([tab, accordion, self.messages]) display(overVbox) #################### self.update_d1() self.update_d2() self.update_accordeon()
def get_widget(self): return widgets.VBox([self.header, self.text])
def get_widget(self): return widgets.VBox([self.header, self.dropdown])
def ask(qid): def isfloat(value): try: float(value) return True except ValueError: return False question = questions_map[qid] if 'type' not in question['properties']: question_type = 'oneliner' else: question_type = question['properties']['type'] if question_type == 'oneliner': field = create_input(qid) elif question_type == 'long': field = create_input(qid, textarea=True) elif question_type == 'markdown': field = widgets.HTML( '<strong style="color: #666;">Jouw antwoord:</strong>') elif question_type == 'code': field = widgets.HTML( '<strong style="color: #666;">Jouw code:</strong>') # and not ('type' in question['properties'] and question['properties']['type'] == 'open') if 'answer-spec' in question and 'auto-score' in question[ 'properties'] and question['properties']['auto-score'].lower( ) == 'true': X = answers[qid]['answer'] r = False try: r = eval(question['answer-spec']) except: pass if r: color = 'green' else: color = 'red' spec_label = 'Test' spec_html = '''<div style="margin-top: -3px;"><span class="spec-label">''' + spec_label + '''</span>: <pre style="display: inline; color: ''' + color + '''">''' + question[ 'answer-spec'] + '''</pre></div>''' answer_spec = widgets.HTML(value=spec_html) if 'points' in question['properties']: score_description = 'Score (maximaal ' + str( question['properties']['points']) + '):' else: score_description = 'Score (maximaal 1):' else: color = 'black' answer_spec = None if 'points' in question['properties']: score_description = 'Score (maximaal ' + str( question['properties']['points']) + '):' else: score_description = 'Score (maximaal 1):' if role == 'teacher': if len(str(answers[qid]['score'])): if int(answers[qid]['score']) != float(answers[qid]['score']): score = str(float(answers[qid]['score'])) else: score = str(int(answers[qid]['score'])) else: score = '' score_field = widgets.Text(description=score_description, placeholder='0', value=score) score_field.question = qid score_field.observe(score_changed) score_field.layout.width = '235px' score_flex = widgets.HBox([score_field]) score_flex.layout.justify_content = 'flex-end' if answer_spec: answer_flex = widgets.HBox([answer_spec]) answer_flex.layout.justify_content = 'flex-end' answer_spec_score = widgets.VBox([answer_flex, score_flex]) else: answer_spec_score = score_flex else: if 'points' in question['properties']: points = question['properties']['points'] else: points = '1' answer_spec_score = widgets.HTML( value= '<span style="color: #666;"><strong>%s</strong> / %d punten</span>' % (points, total_points)) if 'answer-spec' in question: if 'auto-score' not in question['properties'] \ or ('auto-score' in question['properties'] and question['properties']['auto-score'].lower() == 'false'): spec_label = 'Richtlijn' spec_html = '''<div style="margin-top: 6px;"><span class="spec-label">''' + spec_label + '''</span>: <pre style="display: inline;">''' + question[ 'answer-spec'] + '''</pre></div>''' widget = widgets.HBox([ field, widgets.VBox([widgets.HTML(value=spec_html), score_flex]) ]) widget.layout.justify_content = 'space-between' else: widget = widgets.HBox([field, answer_spec_score]) widget.layout.justify_content = 'space-between' else: widget = widgets.HBox([field, answer_spec_score]) widget.layout.justify_content = 'space-between' display(widget)
def simple_optimazation_app(): population_cnt = 20 itter_time = 50 crossover_rate = 0.1 drop_rate = 0.5 mutation_rate = 0.1 i = 0 best_score = 0 best_ind = [] best_ind_ready = [] population = [] ''' dynamic figure ''' X = np.linspace(0, 1, 1000) y = np.array([target_function(x) for x in X]) x_sc = LinearScale() y_sc = LinearScale() ref = Lines(x=X, y=y, scales={'x': x_sc, 'y': y_sc}) # scatter = Scatter(x=[population], y=np.array([target_function(ind) for ind in population]), # scales={'x': x_sc, 'y': y_sc}, # colors=['DarkOrange'], stroke='red', # stroke_width=0.4, default_size=20) scatter = Scatter(x=[], y=[], scales={ 'x': x_sc, 'y': y_sc }, colors=['DarkOrange'], stroke='red', stroke_width=0.4, default_size=20) x_ax = Axis(label='X', scale=x_sc) y_ax = Axis(label='Y', scale=y_sc, orientation='vertical') x_ax.min = 0 x_ax.max = 1 x_ax.num_ticks = 7 x_ax.grid_color = 'orangered' fig = Figure(marks=[ref, scatter], title='A Figure', axes=[x_ax, y_ax], animation_duration=1000) # display(fig) # %% run_itter_slider = population_slider = widgets.IntSlider( value=50, description='#Iteration', min=1, max=100, step=1) run_btn = widgets.Button(description='Run', icon='play', disabled=True) population_cnt_slider = widgets.IntSlider(value=30, description='#Population', min=0, max=100, step=10) init_population_btn = widgets.Button(description='Initialize Population') descriptor1 = widgets.Label('crossover_rate') crossover_rate_slider = widgets.FloatSlider(value=0.1, description='', min=0, max=1.0, step=0.1) descriptor2 = widgets.Label('drop_rate') drop_rate_slider = widgets.FloatSlider(value=0.5, description='', min=0, max=1.0, step=0.1) descriptor3 = widgets.Label('mutation_rate') mutation_rate_slider = widgets.FloatSlider(value=0.3, description='', min=0, max=1.0, step=0.1) patch1 = widgets.HBox([descriptor1, crossover_rate_slider]) patch2 = widgets.HBox([descriptor2, drop_rate_slider]) patch3 = widgets.HBox([descriptor3, mutation_rate_slider]) blank = widgets.Label('') run_out = widgets.Output(layout={ 'border': '1px solid black', 'height': '50px' }) row1 = widgets.VBox([population_cnt_slider, init_population_btn]) row2 = widgets.HBox([patch1, patch2, patch3]) row_n = widgets.VBox([run_itter_slider, run_btn]) app = widgets.VBox([row1, blank, row2, blank, row_n, run_out, fig]) # %% def initialize(): nonlocal population, i population = np.random.rand(population_cnt_slider.value) scatter.x = population scatter.y = get_scores(scatter.x) i = 0 fig.title = f'迭代{i}次\n' @run_out.capture() def update(itter_time=itter_time, crossover_rate=crossover_rate, drop_rate=drop_rate, mutation_rate=mutation_rate): nonlocal scatter, fig, best_score, best_ind_ready, best_ind, i for j in range(itter_time): new_population = select_and_crossover( population, crossover_rate=crossover_rate, drop_rate=drop_rate) new_population_ready = encode_all(new_population) new_population_ready = mutatie_all(new_population_ready, mutation_rate=mutation_rate) new_population = decode_all(new_population_ready) ind, score = get_best(new_population) if score > best_score: best_ind = ind best_score = score best_ind_ready = encode(best_ind) i += 1 scatter.x = new_population scatter.y = get_scores(new_population) fig.title = f'迭代{i}次' # + f'最优个体为: {best_ind_ready}; 函数值为:{best_score}' clear_output(wait=True) display(f'最优个体为: {best_ind_ready}; 函数值为:{best_score}') # %% # update() # %% def on_click_init(change): initialize() run_btn.disabled = False def on_click_run(change): update(itter_time=run_itter_slider.value, crossover_rate=crossover_rate_slider.value, drop_rate=drop_rate_slider.value, mutation_rate=mutation_rate_slider.value) init_population_btn.on_click(on_click_init) run_btn.on_click(on_click_run) return app
def updateBox(self): rows = [self.display_description] + self.addSeparator(top='5px') + \ [self.selections] + self.addSeparator(top='10px') + self.addConditionsWidget() vbox = widgets.VBox(rows, layout=widgets.Layout(width='100%', magins='10px')) return vbox
def plots_tab(self, output): llegend_list = widgets.SelectMultiple(options=self.rm.exp_params, value=self.vars.get('legend_list', [self.rm.exp_params[0]])) # widgets.Text( # value=str(self.vars.get('legend_list', '[model]')), # description='legend_list:', # disabled=False # ) llegend_format = widgets.Text( value=str(self.vars.get('legend_format', '')), description='', disabled=False ) ltitle_format = widgets.Text( value=str(self.vars.get('title_format', '')), description='', disabled=False ) lcmap = widgets.Text( value=str(self.vars.get('cmap', 'jet')), description='cmap:', layout=self.layout_dropdown, disabled=False ) llog_metric_list = widgets.Text( value=str(self.vars.get('log_metric_list', '[train_loss]')), description='log_metric_list:', disabled=False ) metrics_list = [k for k in self.rm_original.score_keys if k is not 'None'] if len(metrics_list) == 0: metrics_list = ['None'] metrics_txt = widgets.Label(value="Metrics:", layout=self.layout_label,) metric_example = [metrics_list[0]] t_y_metric = widgets.SelectMultiple(options=metrics_list, value=[y for y in self.vars.get('y_metrics', metric_example) if y in metrics_list]) # t_y_metric # t_y_metric = widgets.Text( # value=str(self.vars.get('y_metrics', 'train_loss')), # description='y_metrics:', # disabled=False # ) d_x_metric_columns = widgets.Dropdown( options=metrics_list, value=self.vars.get('x_metric', metric_example[0]), layout=widgets.Layout(width='300px'), disabled=False, ) t_groupby_list = widgets.SelectMultiple(options=['None'] + self.rm.exp_params, value=self.vars.get('groupby_list', ['None']),) # widgets.Text( # value=str(self.vars.get('groupby_list')), # description='groupby_list:', # disabled=False # ) t_mode = widgets.Dropdown( options=['line', 'bar'], value='line', layout=widgets.Layout(width='300px'), disabled=False, ) t_bar_agg = widgets.Dropdown( options=['last', 'max', 'mean'], value='last', layout=widgets.Layout(width='300px'), disabled=False, ) t_title_list = widgets.SelectMultiple(options=self.rm.exp_params, value=self.vars.get('title_list', [self.rm.exp_params[0]]),) # widgets.Text( # value=str(self.vars.get('title_list', 'dataset')), # description='title_list:', # disabled=False # ) d_style = widgets.Dropdown( options=['False', 'True'], value='False', description='Interactive:', layout=self.layout_dropdown, disabled=False, ) d_avg_across_columns = widgets.Dropdown( options=['None'] + self.rm.exp_params, value='None', layout=widgets.Layout(width='300px'), disabled=False, ) bdownload = widgets.Button(description="Download Plots", layout=self.layout_button) bdownload_out = widgets.Output(layout=self.layout_button) def on_download_clicked(b): fname = 'plots.pdf' from matplotlib.backends.backend_pdf import PdfPages import matplotlib.pyplot as plt pp = PdfPages(fname) for fig in self.rm_original.fig_list: fig.savefig(pp, format='pdf') pp.close() bdownload_out.clear_output() with bdownload_out: display(FileLink(fname, result_html_prefix="Download: ")) bdownload.on_click(on_download_clicked) h1 = widgets.Label(value="Y-axis Metrics:", layout=widgets.Layout(width='340px'),) h11 = widgets.Label(value="X-axis Metric", layout=widgets.Layout(width='340px'),) h2 = widgets.Label(value="Legend:", layout=widgets.Layout(width='340px'),) h22 = widgets.Label(value="Format:", layout=widgets.Layout(width='340px'),) h3 = widgets.Label(value="Title:", layout=widgets.Layout(width='340px'),) h33 = widgets.Label(value="Format:", layout=widgets.Layout(width='340px'),) h4 = widgets.Label(value="GroupBy:", layout=widgets.Layout(width='340px'),) h44 = widgets.Label(value="", layout=widgets.Layout(width='340px'),) h5 = widgets.Label(value="Plot Mode:", layout=widgets.Layout(width='320px'),) h55 = widgets.Label(value="Plot Agg", layout=widgets.Layout(width='320px'),) space = widgets.Label(value="", layout=widgets.Layout(width='300px'),) brefresh = widgets.Button(description="Display Plot") d_avg_across_txt = widgets.Label(value="avg_across:",) button = widgets.VBox([ widgets.HBox([h1, h2, h3, h4, ]), widgets.HBox([t_y_metric, llegend_list, t_title_list, t_groupby_list,]), widgets.HBox([h11, h5, h55, d_avg_across_txt, ]), widgets.HBox([d_x_metric_columns, t_mode, t_bar_agg, d_avg_across_columns, ]), # widgets.HBox([ d_avg_across_txt, d_avg_across_columns, ]), widgets.HBox([brefresh, bdownload, bdownload_out,]),]) # button = widgets.VBox([widgets.HBox([brefresh, bdownload, bdownload_out]), # widgets.HBox([t_y_metric, d_x_metric_columns]), # widgets.HBox([t_title_list, d_style]), # widgets.HBox([t_groupby_list, llegend_list, ]), # widgets.HBox([t_mode, t_bar_agg]), # widgets.HBox([ltitle_format, llegend_format]), # widgets.HBox([d_avg_across_columns]), # ]) output_plot = widgets.Output() def on_clicked(b): if d_style.value == 'True': from IPython import get_ipython ipython = get_ipython() ipython.magic("matplotlib widget") output_plot.clear_output() with output_plot: self.update_rm() self.vars['y_metrics'] = list(t_y_metric.value) self.vars['x_metric'] = d_x_metric_columns.value w, h = 10, 5 if len(self.vars['y_metrics']) > 1: figsize = (2*int(w), int(h)) self.vars['figsize'] = figsize else: self.vars['figsize'] = (int(w), int(h)) self.vars['legend_list'] = list(llegend_list.value) self.vars['legend_format'] = llegend_format.value self.vars['log_metric_list'] = hu.get_list_from_str( llog_metric_list.value) self.vars['groupby_list'] = list( t_groupby_list.value) self.vars['mode'] = t_mode.value self.vars['title_list'] = list(t_title_list.value) self.vars['bar_agg'] = t_bar_agg.value self.vars['title_format'] = ltitle_format.value self.vars['cmap'] = lcmap.value self.vars['avg_across'] = d_avg_across_columns.value avg_across_value = self.vars['avg_across'] if avg_across_value == "None": avg_across_value = None self.rm_original.fig_list = self.rm.get_plot_all(y_metric_list=self.vars['y_metrics'], x_metric=self.vars['x_metric'], groupby_list=self.vars['groupby_list'], legend_list=self.vars['legend_list'], log_metric_list=self.vars['log_metric_list'], mode=self.vars['mode'], bar_agg=self.vars['bar_agg'], figsize=self.vars['figsize'], title_list=self.vars['title_list'], legend_format=self.vars['legend_format'], title_format=self.vars['title_format'], cmap=self.vars['cmap'], avg_across=avg_across_value) show_inline_matplotlib_plots() d_style.observe(on_clicked) brefresh.on_click(on_clicked) with output: display(button) display(output_plot)
def make_vertical_box(cls, children, layout=Layout(), duplicates=False): if not duplicates: return widgets.VBox(children, layout=layout) else: return widgets.VBox([children[0], children[2]], layout=layout)
def make_vertical_box(cls, children, layout=Layout(), duplicates=False): "Make a vertical box with `children` and `layout`." if not duplicates: return widgets.VBox(children, layout=layout) else: return widgets.VBox([children[0], children[2]], layout=layout)
def create_textinputquiz_widget(description, text_description, correct_answer, a2, hint): ##grid for option table correct_answer = correct_answer ##float ##str alternativ = widgets.Text(value='', placeholder='', description='', disabled=False, layout=(Layout(width='auto'))) ##question description description_out = widgets.Output(layout=Layout(width='auto')) with description_out: print(description) ##description before text widget text_description_out = widgets.Output(layout=Layout(width='auto')) with text_description_out: print(text_description) ##description after text widget e.g. units a2_out = widgets.Output(layout=Layout(width='auto')) with a2_out: print(a2) ## feedback_out = widgets.Output() def check_selection(b): a = alternativ.value if a == correct_answer: s = "Correct!" else: s = "Wrong answer unfortunately" with feedback_out: feedback_out.clear_output() print(s) return check = widgets.Button(description="check") check.on_click(check_selection) ## hint_out = widgets.Output() def hint_selection(b): with hint_out: print(hint) with feedback_out: feedback_out.clear_output() print(hint) hintbutton = widgets.Button(description="hint") hintbutton.on_click(hint_selection) return widgets.VBox([ description_out, widgets.HBox([text_description_out, alternativ, a2_out]), widgets.HBox([hintbutton, check]), feedback_out ], layout=Layout(display='flex', flex_flow='column', align_items='stretch', width='auto'))
def get_list(items): return widgets.VBox([item.render() for item in items])
def automator_gui(filename): """ :parameter project_filename: The name of a puckle file containing an OQtProject """ global w_model, w_sources, w_nb_name, w_nb_type, w_repo, w_progress global project_filename, model global project_dir wdg_list = [] margin = 5 project_filename = filename oqmbtp = OQtProject.load_from_file(project_filename) models = oqmbtp.models.keys() project_dir = oqmbtp.directory w_title = widgets.HTML(value="<h3>Automator<h3>") tmp_str = "Name : %s <br>" % (oqmbtp.name) tmp_str += "Stored in: %s <br><br>" % (project_dir) w_text = widgets.HTML(value=tmp_str) wdg_list.append(w_title) wdg_list.append(w_text) tmp_str = "Warning: the model does not contain sources" w_warn = widgets.HTML(value=tmp_str, visible=False) if len(models): model_id = models[0] model = oqmbtp.models[model_id] w_model = widgets.Dropdown(options=models, description='Model', value=model_id, width=400, margin=margin) if len(model.sources.keys()): # Sources drop down menu tmp_list = sorted(model.sources.keys()) tmp_list.insert(0, 'All') tmp_str = 'Sources' w_sources = widgets.SelectMultiple(options=tmp_list, description=tmp_str, width=200, margin=margin) else: w_sources = widgets.Dropdown(options=[], description='Source') wdg_list.append(w_model) wdg_list.append(w_sources) else: w_warn.visible = True # Notebook type w_nb_type = widgets.Dropdown(options=NB_TYPES, description='Notebook type', width=400, margin=margin) wdg_list.append(w_nb_type) # Notebook name w_nb_name = widgets.Dropdown(options=[], description='Notebook name', width=400, margin=margin) wdg_list.append(w_nb_name) # Report checkbox w_repo = widgets.Checkbox(description='Generate report', value=False) wdg_list.append(w_repo) # Warning wdg_list.append(w_warn) # Button w_butt = widgets.Button(description='Run', width=100, border_color='red') wdg_list.append(w_butt) # Progress bar w_progress = widgets.FloatProgress(value=0.0, min=0.0, step=1, visible=False, description='Processing:') wdg_list.append(w_progress) w_model.on_trait_change(handle_change_model) w_nb_type.on_trait_change(handle_change_nb_type, 'value') w_butt.on_click(handle_run) # Clean variables del oqmbtp return widgets.VBox(children=wdg_list)
def notebook_login(): """ Displays a widget to login to the HF website and store the token. """ try: import ipywidgets.widgets as widgets from IPython.display import clear_output, display except ImportError: raise ImportError( "The `notebook_login` function can only be used in a notebook (Jupyter or" " Colab) and you need the `ipywdidgets` module: `pip install ipywidgets`." ) box_layout = widgets.Layout( display="flex", flex_flow="column", align_items="center", width="50%" ) token_widget = widgets.Password(description="Token:") token_finish_button = widgets.Button(description="Login") switch_button = widgets.Button(description="Use password") login_token_widget = widgets.VBox( [ widgets.HTML(NOTEBOOK_LOGIN_TOKEN_HTML_START), token_widget, token_finish_button, widgets.HTML(NOTEBOOK_LOGIN_TOKEN_HTML_END), switch_button, ], layout=box_layout, ) display(login_token_widget) # Deprecated page for login input_widget = widgets.Text(description="Username:"******"Password:"******"Login") login_password_widget = widgets.VBox( [ widgets.HTML(value=NOTEBOOK_LOGIN_PASSWORD_HTML), widgets.HBox([input_widget, password_widget]), password_finish_button, ], layout=box_layout, ) # On click events def login_token_event(t): token = token_widget.value # Erase token and clear value to make sure it's not saved in the notebook. token_widget.value = "" clear_output() _login(HfApi(), token=token) token_finish_button.on_click(login_token_event) def login_password_event(t): username = input_widget.value password = password_widget.value # Erase password and clear value to make sure it's not saved in the notebook. password_widget.value = "" clear_output() _login(HfApi(), username=username, password=password) password_finish_button.on_click(login_password_event) def switch_event(t): clear_output() display(login_password_widget) switch_button.on_click(switch_event)
def create_textinputquiz_widget(description, text_description, correct_answer, a2, hint): ##grid for option table correct_answer = correct_answer ##float ##str alternativ = widgets.Text(value='', placeholder='', description='', disabled=False, layout=(Layout(width='auto'))) ##question description description_out = widgets.Output(layout=Layout(width='auto')) with description_out: print(description) ##description before text widget text_description_out = widgets.Output(layout=Layout(width='auto')) with text_description_out: print(text_description) ##description after text widget e.g. units a2_out = widgets.Output(layout=Layout(width='auto')) with a2_out: print(a2) ## feedback_out = widgets.Output() def check_selection(b): a = alternativ.value if a == correct_answer: s = "Congratulation! You have finished tutorial 1. Check your answer : 1, 24, 4, 2, 2, 1, 1, yes, yes, 4, no, yes" else: s = "You are not trying to guess, aren't you? Check the hint!" with feedback_out: feedback_out.clear_output() print(s) return check = widgets.Button(description="check") check.on_click(check_selection) ## hint_out = widgets.Output() def hint_selection(b): with hint_out: print(hint) with feedback_out: feedback_out.clear_output() print(hint) hintbutton = widgets.Button(description="hint") hintbutton.on_click(hint_selection) return widgets.VBox([ description_out, widgets.HBox([text_description_out, alternativ, a2_out]), widgets.HBox([hintbutton, check]), feedback_out ], layout=Layout(display='flex', flex_flow='column', align_items='stretch', width='auto'))
def update_tab_contents(change): if change.new is None: return out, func, _ = out_specs[change.new] has_content = bool(out.get_state()["outputs"]) if not has_content: with out: func(*(i.copy() for i in results)) Tab.observe(update_tab_contents, names="selected_index") Gui = widgets.VBox([Selections, Tab]) def update_valid_signals(change): case_name = Case.value pyalgo, _, ok_gears, accdb, _, _ = load_interactive_case(case_name) Gear.options = ok_gears.columns.levels[1] pyalgo_columns = [ ("/".join(cc for cc in c if cc), c) for c in pyalgo.select_dtypes(exclude=[np.object]) if not c[0].startswith("ok_gear") and not c[0] == "t" ] PyAlgoSignals.options = pyalgo_columns
def tables_tab(db, output): w_columns = wdg.SelectMultiple(header="Hyperparameters:", options=db.rm.exp_params, db_vars=db.vars, var='columns') w_score_columns = wdg.SelectMultiple(header="Metrics:", options=db.rm.score_keys, db_vars=db.vars, var='score_columns') bstatus = widgets.Button(description="Jobs Status") blogs = widgets.Button(description="Jobs Logs") bfailed = widgets.Button(description="Jobs Failed") b_table = widgets.Button(description="Display Table") b_meta = widgets.Button(description="Display Meta Table") b_diff = widgets.Button(description="Display Filtered Table") w_avg_across = wdg.Dropdown(header='Avg Across', options=['None'] + db.rm.exp_params, db_vars=db.vars, var='avg_across') button = widgets.VBox([ widgets.HBox([ w_columns.get_widget(), w_score_columns.get_widget(), w_avg_across.get_widget() ]), widgets.HBox([ b_table, bstatus, blogs, bfailed, ]), ]) output_plot = widgets.Output() with output: display(button) display(output_plot) def on_table_clicked(b): output_plot.clear_output() with output_plot: db.update_rm() score_table = db.rm.get_score_table( columns=w_columns.update(), score_columns=w_score_columns.update(), avg_across=w_avg_across.update()) display(score_table) def on_job_status_clicked(b): output_plot.clear_output() with output_plot: db.update_rm() summary_list = db.rm.get_job_summary(verbose=db.rm.verbose, add_prefix=True) summary_dict = hu.group_list(summary_list, key='job_state', return_count=True) display(summary_dict) summary_dict = hu.group_list(summary_list, key='job_state', return_count=False) for state in summary_dict: n_jobs = len(summary_dict[state]) if n_jobs: display('Experiments %s: %d' % (state, n_jobs)) df = pd.DataFrame(summary_dict[state]) display(df.head()) def on_logs_clicked(b): output_plot.clear_output() with output_plot: summary_list = db.rm.get_job_summary(verbose=db.rm.verbose, add_prefix=True) n_logs = len(summary_list) for i, logs in enumerate(summary_list): print('\nLogs %d/%d' % (i + 1, n_logs), '=' * 50) print('exp_id:', logs['exp_id']) print('job_id:', logs['job_id']) print('job_state:', logs['job_state']) print( 'savedir:', os.path.join(db.rm_original.savedir_base, logs['exp_id'])) print('\nexp_dict') print('-' * 50) pprint.pprint(logs['exp_dict']) print('\nLogs') print('-' * 50) pprint.pprint(logs.get('logs')) def on_failed_clicked(b): output_plot.clear_output() with output_plot: db.update_rm() summary_list = db.rm.get_job_summary(verbose=db.rm.verbose, add_prefix=True) summary_dict = hu.group_list(summary_list, key='job_state', return_count=False) if 'FAILED' not in summary_dict: display('NO FAILED JOBS') return n_failed = len(summary_dict['FAILED']) if n_failed == 0: display('no failed experiments') else: for i, failed in enumerate(summary_dict['FAILED']): print('\nFailed %d/%d' % (i + 1, n_failed), '=' * 50) print('exp_id:', failed['exp_id']) print('job_id:', failed['job_id']) print('job_state:', 'FAILED') print( 'savedir:', os.path.join(db.rm_original.savedir_base, failed['exp_id'])) print('\nexp_dict') print('-' * 50) pprint.pprint(failed['exp_dict']) print('\nLogs') print('-' * 50) pprint.pprint(failed.get('logs')) # Add call listeners b_table.on_click(on_table_clicked) bstatus.on_click(on_job_status_clicked) blogs.on_click(on_logs_clicked) bfailed.on_click(on_failed_clicked)
plt.imshow(decoded_samples[i].reshape(50, int(input_dim / 50))) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() # In[35]: from IPython.display import Audio, display from ipywidgets import widgets original_widgets = [] for (audio) in x_train[0:10]: out = widgets.Output() with out: display(Audio(data=audio, rate=sr)) original_widgets.append(out) oBox = widgets.HBox(original_widgets) decoded_widgets = [] for (audio) in decoded_samples[0:10]: out = widgets.Output() with out: display(Audio(data=audio, rate=sr)) decoded_widgets.append(out) dBox = widgets.HBox(decoded_widgets) widgets.VBox([oBox, dBox]) # In[ ]:
def select(self, table_id): """ Selects a table based on the table_id and returns a widget container in which the user can select the set of variables and values to be included in the final table. Example -------- box = select(table_id = '10714') Parameters ---------- table_id : string the id of the desired table """ # get a list with dictionaries containing information about each variable self.variables = self.get_variables(table_id=table_id) table_info = pd.read_json(self.furl) table_title = table_info.iloc[0, 0] # get number of variables (ok, childish approach, can be simplified!) nvars = len(self.variables) var_list = list(range(nvars)) # a list of dictionaries of the values available for each variable option_list = [ OrderedDict( zip(self.variables[var]['valueTexts'], self.variables[var]['values'])) for var in var_list ] # create a selection widget for each variable # todo: skip widget or make it invisible if there is only one option? # todo: make first alternative a default selection initially for all tables? # todo: add buttons for selecting "all", "latest" , "first" and "none" selection_widgets = [ widgets.widget_selection.SelectMultiple(options=option_list[var], rows=8, layout={'width': '500px'}) for var in var_list ] # put all the widgets in a container variables_container = widgets.Tab(selection_widgets) # label each container with the variable label for var in var_list: title = str(self.variables[var]['text']) variables_container.set_title(var, title) # build widgets and put in one widget container headline = widgets.Label(value=table_title, color='blue') endline = widgets.Label(value='''Select category and click on elements to be included in the table (CTRL-A selects "all")''') url_text = widgets.Label(value=self.furl) from IPython.display import display button = widgets.Button(description="Click when finished") selection_container = widgets.VBox( [headline, endline, variables_container, url_text, button]) selection_container.layout.border = '3px grey solid' def clicked(b): print('Info is saved. You can now run the rest of the code :)') button.on_click(clicked) return selection_container
def start(self): def startImport(b): b.disabled = True print('start import...') parent_dir, files = self.previous_step.data self.progressbar.max = int(self.sample_num.value) if not parent_dir.lower().endswith('.zip'): sampled_ids = random.sample(range(0, len(files)), self.progressbar.max) for i in range(0, self.progressbar.max): file = files[sampled_ids[i]] base_name = path.basename(file) self.progressbar.value = i with open(parent_dir + '/' + file) as f: text = f.read() if file.lower().endswith('.xml'): self.loadTextFromXml(file, text, self.data) else: self.dataset_name = 'unknown' self.data = self.data.append( pd.DataFrame( [[None, base_name, text, None, None]], columns=[ 'BUNCH_ID', 'DOC_NAME', 'TEXT', 'DATE', 'REF_DATE' ])) self.progressbar.value = self.progressbar.max else: sampled_ids = random.sample(range(0, len(self.file_list)), self.progressbar.max) for i in range(0, self.progressbar.max): finfo = self.file_list[sampled_ids[i]] ifile = self.zfile.open(finfo) doc_text = ifile.read().decode("utf-8") base_name = path.basename(finfo.filename) self.progressbar.value = i if finfo.filename.lower().endswith('.xml'): self.loadTextFromXml(finfo, doc_text, self.data) else: self.dataset_name = 'unknown' self.data = self.data.append( pd.DataFrame( [[None, base_name, doc_text, None, None]], columns=[ 'BUNCH_ID', 'DOC_NAME', 'TEXT', 'DATE', 'REF_DATE' ])) self.zfile.close() self.progressbar.value = self.progressbar.max self.next_button.disabled = False # self.data.set_index('DOC_NAME', inplace=True) if self.dataset_name == 'n2c2': self.inferRefDate(self.data) print("Totally " + str(len(sampled_ids)) + " files have been imported into dataframe.\n" "They are parsed into " + str(len(self.data)) + " records in dataframe.") pass if self.previous_step.data is None: self.previous_step.start() parent_dir, files = self.previous_step.data if not parent_dir.lower().endswith('.zip'): label = widgets.HTML( "<h4>Read %s files from: </h4><p>%s</p>".format( len(files), parent_dir)) self.start_import_btn.on_click(startImport) self.sample_num.value = str(len(files)) self.progressbar.max = len(files) rows = [ label, self.sample_num, self.start_import_btn, self.progressbar ] + self.addSeparator(top='10px') + [ self.addPreviousNext(self.show_previous, self.show_next) ] else: import zipfile, os parent_dir = os.path.join(self.previous_step.path, parent_dir) self.zfile = zipfile.ZipFile(parent_dir) print('reading file list from {} ...'.format(parent_dir)) self.file_list = [ f for f in self.zfile.infolist() if not f.is_dir() ] label = widgets.HTML( "<h4>Read {} files from: </h4><p>{}</p>".format( len(self.file_list), parent_dir)) self.sample_num.value = str(len(self.file_list)) self.progressbar.max = len(self.file_list) self.start_import_btn.on_click(startImport) rows = [ label, self.sample_num, self.start_import_btn, self.progressbar ] + self.addSeparator(top='10px') + [ self.addPreviousNext(self.show_previous, self.show_next) ] vbox = widgets.VBox(rows) vbox.layout.flex_grown = 'column' clear_output() display(vbox) return self.data
def display(self): self.button_saveNetwork.on_click(self.save_network) main_options = widgets.VBox( [self.widget_filename, self.button_saveNetwork]) display(main_options)