Exemple #1
0
    def display(self):
        self.update_rm()

        # Select Exp Group
        l_exp_group = widgets.Label(value="Select exp_group", layout=self.layout_label,)

        exp_group_list = list(self.rm_original.exp_groups.keys())
        exp_group_selected = 'all'
        if self.vars.get('exp_group', 'all') in exp_group_list:
            exp_group_selected = self.vars.get('exp_group', 'all')

        d_exp_group = widgets.Dropdown(
            options=exp_group_list,
            value=exp_group_selected,
            layout=self.layout_dropdown,
        )
        self.rm_original.exp_list_all = self.rm_original.exp_groups.get(d_exp_group.value, 'all')
        l_n_exps = widgets.Label(value='Total Exps %d' % len(self.rm_original.exp_list_all), layout=self.layout,)
                
        def on_group_change(change):
            if change['type'] == 'change' and change['name'] == 'value':
                self.rm_original.exp_list_all = self.rm_original.exp_groups[change['new']]
                l_n_exps.value = 'Total Exps %d' % len(self.rm_original.exp_list_all)
        
        d_exp_group.observe(on_group_change)

        display(widgets.VBox([l_exp_group,
                        widgets.HBox([d_exp_group, l_n_exps, self.t_filterby_list]) 
        ]))

        hj.init_datatable_mode()
        tables = widgets.Output()
        plots = widgets.Output()
        images = widgets.Output()
        meta = widgets.Output()

        main_out = widgets.Output()
        # Display tabs
        tab = widgets.Tab(children = [tables, plots, images, meta])
        tab.set_title(0, 'Tables')
        tab.set_title(1, 'Plots')
        tab.set_title(2, 'Images')
        tab.set_title(3, 'Meta')
            
        with main_out:
            display(tab)
            tables.clear_output()
            plots.clear_output()
            images.clear_output()
            meta.clear_output()

            # show tabs
            self.table_tab(tables)
            self.plot_tab(plots)
            self.images_tab(images)
            self.meta_tab(meta)

        display(main_out)

        if self.wide_display:
            display(HTML("<style>.container { width:100% !important; }</style>"))

        # This makes cell show full height display
        style = """
        <style>
            .output_scroll {
                height: unset !important;
                border-radius: unset !important;
                -webkit-box-shadow: unset !important;
                box-shadow: unset !important;
            }
        </style>
        """
        display(HTML(style))
Exemple #2
0
    def __init__(self, text, initial, correct, inst=None, klammer=False):
        
        textfield = widgets.Textarea(value=initial, placeholder='Type something', description='', disabled=False, layout=Layout(width='500px'))
        # textfield.observe(on)
        textbox = widgets.HTML(value='<h4 style="font-size:14px;">{}</h4>'.format(text))
        instbox = widgets.HTML(value='<i>{}</i>'.format(inst)) 
        
        submit_button = widgets.Button(description="Korrekt?", layout=widgets.Layout(width="350px"))
        give_up_button = widgets.Button(description="Lösung zeigen", layout=widgets.Layout(width="150px"))
        button_box = widgets.HBox(children=[submit_button, give_up_button])

        super().__init__(children=[textbox, instbox, textfield, button_box])


        def on_ok_button_clicked(button):
            given_answer = textfield.value
            ans = given_answer.split()
            answer = ''.join(ans)
            corr = correct.split()
            newc = ''.join(corr)

            if answer == newc:
                button.style.button_color = 'lightgreen'
                button.description = "Richtig!"

            else:
                button.style.button_color = 'red'
                
                if klammer:
                    more = True
                    stack = []
                    not_open = 0
                    for i,c in enumerate(given_answer):
                        if c == '[':
                            stack.append(i)
                        elif c == ']' and stack:
                            stack.pop()
                        elif c == ']':
                            not_open += 1
                        elif c in ['(',')','{','}']:
                            button.description="Falsche Klammern!"
                            more = False
                            break
                    
                    if stack and more:
                        more = False
                        button.description =str(len(stack))+" Klammern schließen!"

                    if not_open and more:
                        more = False
                        button.description = str(not_open)+" Klammern öffnen!"

                    if more:
                        button.description = "Noch eine Übereinstimmung mit Synatxbaum erreicht!"

        def on_show_solution_button_clicked(button):
            textfield.value = correct
            textfield.disabled = True
            submit_button.disabled = True
            give_up_button.disabled = True

        submit_button.on_click(on_ok_button_clicked)
        give_up_button.on_click(on_show_solution_button_clicked)
Exemple #3
0
    def normalization(self, roi=None, force=False, force_mean_ob=False, notebook=False, use_only_sample=False):
        """normalization of the data
                
        Parameters:
            roi: ROI object or list of ROI objects - object defines the region of the sample and OB that have to match
        in intensity
            force: boolean - True will force the normalization to occur, even if it had been
                run before with the same data set (default False)
        notebook: boolean - turn on this option if you run the library from a
             notebook to have a progress bar displayed showing you the progress of the loading (default False)
        use_only_sample - turn on this option to normalize the sample data using the ROI on the sample. each pixel
            counts will be divided by the average counts of all the ROI of the same image

        Return:
            True - status of the normalization (True if every went ok, this is mostly used for the unit test)

        Raises:
            IOError: if no sample loaded
            IOError: if no OB loaded and use_only_sample if False
            IOError: if use_only_sample is True and no ROI provided
            IOError: if size of sample and OB do not match
        
        """
        if not force:
            # does nothing if normalization has already been run
            if self.__exec_process_status['normalization']:
                return
        self.__exec_process_status['normalization'] = True

        # make sure we loaded some sample data
        if self.data['sample']['data'] is None:
            raise IOError("No normalization available as no data have been loaded")

        # make sure we loaded some ob data
        if not use_only_sample:
            if self.data['ob']['data'] is None:
                raise IOError("No normalization available as no OB have been loaded")

            # make sure the data loaded have the same size
            if not self.data_loaded_have_matching_shape():
                raise ValueError("Data loaded do not have the same shape!")

        if notebook:
            from ipywidgets import widgets
        from IPython.core.display import display

        # make sure, if provided, roi has the right type and fits into the images
        b_list_roi = False

        if not use_only_sample:

            if roi:
                b_list_roi = self.check_roi_format(roi)

            if roi:

                if b_list_roi:
                    _sample_corrected_normalized = self.calculate_corrected_normalized(data_type=DataType.sample,
                                                                                       roi=roi)
                    _ob_corrected_normalized = self.calculate_corrected_normalized(data_type=DataType.ob,
                                                                                   roi=roi)

                else:
                    _x0 = roi.x0
                    _y0 = roi.y0
                    _x1 = roi.x1
                    _y1 = roi.y1

                    _sample_corrected_normalized = [_sample / np.mean(_sample[_y0:_y1 + 1, _x0:_x1 + 1])
                                                    for _sample in self.data['sample']['data']]
                    _ob_corrected_normalized = [_ob / np.mean(_ob[_y0:_y1 + 1, _x0:_x1 + 1])
                                                for _ob in self.data['ob']['data']]

            else:

                _sample_corrected_normalized = copy.deepcopy(self.data['sample']['data'])
                _ob_corrected_normalized = copy.deepcopy(self.data['ob']['data'])

            self.data[DataType.sample]['data'] = _sample_corrected_normalized
            self.data[DataType.ob]['data'] = _ob_corrected_normalized

            # if the number of sample and ob do not match, use mean of obs
            nbr_sample = len(self.data['sample']['file_name'])
            nbr_ob = len(self.data['ob']['file_name'])
            if (nbr_sample != nbr_ob) or force_mean_ob:  # work with mean ob
                _ob_corrected_normalized = np.mean(_ob_corrected_normalized, axis=0)
                self.data['ob']['data_mean'] = _ob_corrected_normalized
                _working_ob = copy.deepcopy(_ob_corrected_normalized)
                _working_ob[_working_ob == 0] = np.NaN

                if notebook:
                    # turn on progress bar
                    _message = "Normalization"
                    box1 = widgets.HBox([widgets.Label(_message,
                                                       layout=widgets.Layout(width='20%')),
                                         widgets.IntProgress(max=len(self.data['sample']['data']))])
                    display(box1)
                    w1 = box1.children[1]

                normalized_data = []
                for _index, _sample in enumerate(self.data['sample']['data']):
                    _norm = np.divide(_sample, _working_ob)
                    _norm[np.isnan(_norm)] = 0
                    _norm[np.isinf(_norm)] = 0
                    normalized_data.append(_norm)

                    if notebook:
                        w1.value = _index + 1

            else:  # 1 ob for each sample
                # produce normalized data
                sample_ob = zip(self.data['sample']['data'], self.data['ob']['data'])

                if notebook:
                    # turn on progress bar
                    _message = "Normalization"
                    box1 = widgets.HBox([widgets.Label(_message,
                                                       layout=widgets.Layout(width='20%')),
                                         widgets.IntProgress(max=len(self.data['sample']['data']))])
                    display(box1)
                    w1 = box1.children[1]

                normalized_data = []
                for _index, [_sample, _ob] in enumerate(sample_ob):
                    _working_ob = copy.deepcopy(_ob)
                    _working_ob[_working_ob == 0] = np.NaN
                    _norm = np.divide(_sample, _working_ob)
                    _norm[np.isnan(_norm)] = 0
                    _norm[np.isinf(_norm)] = 0
                    normalized_data.append(_norm)

                    if notebook:
                        w1.value = _index + 1

            self.data['normalized'] = normalized_data

        else:  # use_sample_only with ROI
            normalized_data = self.calculate_corrected_normalized_without_ob(roi=roi)
            self.data['normalized'] = normalized_data

        return True
    def generate_interactive_plot(self):
        if not self._calculated:
            return None

        y = self.integrated.index.get_level_values(1)
        x = self.integrated.index.get_level_values(0)

        fig = make_subplots(rows=1, cols=2, horizontal_spacing=0.12)
        self.c = go.Contour(
            z=Mapper.reshape_df(self.integrated),
            x=x.drop_duplicates(),
            y=y.drop_duplicates(),
            colorscale='Viridis',
            colorbar=dict(x=-.2, len=1),
            contours=dict(
                start=float(self.integrated.min()),
                end=float(self.integrated.max()),
                size=float(self.integrated.max() - self.integrated.min()) /
                self.levels))

        self.construct_scatter()
        self.specx_slider.value = self.specx
        self.dspecx_slider.value = self.dspecx

        self.specx_bg_slider.min = self.specx - self.dspecx
        self.specx_bg_slider.max = self.specx + self.dspecx

        fig.add_trace(self.c, 1, 1)
        fig.add_trace(self.s, 1, 2)
        fig.add_trace(self.s_als, 1, 2)
        fig.add_trace(self.s_corr, 1, 2)
        fig.add_trace(self.s_peak, 1, 2)
        fig.update_xaxes(title_text="X pos (µm)", row=1, col=1)
        fig.update_yaxes(title_text="Y pos (µm)", row=1, col=1)
        fig.update_xaxes(title_text="Wavenumber (cm-1)", row=1, col=2)
        fig.update_yaxes(title_text="Intensity (arb. units)", row=1, col=2)
        fig.update_layout(width=1000, height=500, autosize=True)

        self.g = go.FigureWidget(fig, layout=go.Layout(barmode='overlay'))
        self.g.data[0].name = 'ν={:.2f},Δν={:.2f}'.format(
            self.specx, self.dspecx)
        self.g.data[1].name = 'x={:.2f},y={:.2f}'.format(self.x, self.y)
        self.g.data[2].name = 'Baseline'
        self.g.data[3].name = 'Corr'
        self.g.data[4].name = 'Detected Peak'
        self.g.data[4].marker = dict(size=8, color='red', symbol='cross')
        self.g.add_shape(type='line',
                         x0=self.spec_bgx - self.dspec_bgx,
                         xref='x2',
                         yref='paper',
                         y0=0,
                         x1=self.spec_bgx - self.dspec_bgx,
                         y1=1,
                         line_width=3,
                         name='l1')
        self.g.add_shape(type='line',
                         x0=self.spec_bgx + self.dspec_bgx,
                         xref='x2',
                         yref='paper',
                         y0=0,
                         x1=self.spec_bgx + self.dspec_bgx,
                         y1=1,
                         line_width=3,
                         name='l2')

        self.specx_slider.observe(self.change_slider, names="value")
        self.dspecx_slider.observe(self.change_slider, names="value")
        self.contour_select.observe(self.change_contour, names="value")
        self.dspecx_bg_slider.observe(self.change_slider, names="value")
        self.specx_bg_slider.observe(self.change_slider, names="value")
        self.recalc_button.on_click(self.update_contour)
        self.lam_text.observe(self.update_bs_params, names="value")
        self.p_text.observe(self.update_bs_params, names="value")
        self.level_slider.observe(self.change_levels, names='value')
        self.g.data[0].on_click(self.change_xy)
        self.mask_button.on_click(self.mask_point)

        return widgets.VBox([
            widgets.HBox([
                widgets.VBox([
                    self.specx_slider, self.dspecx_slider,
                    self.specx_bg_slider, self.dspecx_bg_slider,
                    widgets.HBox([self.recalc_button, self.recalc_progress])
                ]),
                widgets.VBox([
                    widgets.HBox([self.contour_select, self.mask_button]),
                    self.level_slider, self.lam_text, self.p_text
                ])
            ]), self.g
        ])
Exemple #5
0
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)
Exemple #6
0
def show_grid(data_frame,
              remote_js=None,
              precision=None,
              grid_options=None,
              show_toolbar=False):
    """
    Main entry point for rendering DataFrames as SlickGrids.

    Parameters
    ----------
    remote_js : bool
        Whether to load slickgrid.js from a local filesystem or from a
        remote CDN.  Loading from the local filesystem means that SlickGrid
        will function even when not connected to the internet, but grid
        cells created with local filesystem loading will not render
        correctly on external sharing services like NBViewer.
    precision : integer
        The number of digits of precision to display for floating-point
        values.  If unset, we use the value of
        `pandas.get_option('display.precision')`.
    grid_options : dict
        Options to use when creating javascript SlickGrid instances.  See
        the `SlickGrid documentation <https://github.com/mleibman/SlickGrid/wiki/Grid-Options>`_ for
        information on the available options.  See the Notes section below for
        the list of options that qgrid sets by default.

    Notes
    -----
    By default, the following options get passed into SlickGrid when
    ``show_grid`` is called.  See the `SlickGrid documentation
    <https://github.com/mleibman/SlickGrid/wiki/Grid-Options>`_ for information
    about these options.
    ::
        {
            'fullWidthRows': True,
            'syncColumnCellResize': True,
            'forceFitColumns': True,
            'rowHeight': 28,
            'enableColumnReorder': False,
            'enableTextSelectionOnCells': True,
            'editable': True,
            'autoEdit': False
        }
    show_toolbar: bool
        Whether to show a toolbar with options for adding/removing rows and
        exporting the widget to a static view.


    See Also
    --------
    set_defaults : Permanently set global defaults for `show_grid`.
    set_grid_option : Permanently set individual SlickGrid options.
    """

    if remote_js is None:
        remote_js = defaults.remote_js
    if precision is None:
        precision = defaults.precision
        if not isinstance(precision, Integral):
            raise TypeError("precision must be int, not %s" % type(precision))
    if grid_options is None:
        grid_options = defaults.grid_options
        if not isinstance(grid_options, dict):
            raise TypeError("grid_options must be dict, not %s" %
                            type(grid_options))

    # create a visualization for the dataframe
    grid = QGridWidget(df=data_frame,
                       precision=precision,
                       grid_options=json.dumps(grid_options),
                       remote_js=remote_js)

    if show_toolbar:
        add_row = widgets.Button(description="Add Row")
        add_row.on_click(grid.add_row)

        rem_row = widgets.Button(description="Remove Row")
        rem_row.on_click(grid.remove_row)

        export = widgets.Button(description="Export")
        export.on_click(grid.export)

        display(widgets.HBox((add_row, rem_row, export)), grid)
    else:
        display(grid)
Exemple #7
0
    def __init__(self, nwb):
        super().__init__()
        self.nwb = nwb
        self.show_spikes = False

        self.btn_spike_times = widgets.Button(description='Show spike times',
                                              button_style='')
        self.btn_spike_times.on_click(self.spikes_viewer)

        # Start time and duration controller
        self.tmin = get_timeseries_mint(
            nwb.processing['ophys'].data_interfaces['fluorescence'].
            roi_response_series['roi_response_series'])
        self.tmax = get_timeseries_maxt(
            nwb.processing['ophys'].data_interfaces['fluorescence'].
            roi_response_series['roi_response_series'])
        self.time_window_controller = StartAndDurationController(
            tmin=self.tmin,
            tmax=self.tmax,
            start=0,
            duration=5,
        )

        # Electrophys single trace
        self.electrical = SingleTracePlotlyWidget(
            timeseries=nwb.processing['ecephys'].
            data_interfaces['filtered_membrane_voltage'],
            foreign_time_window_controller=self.time_window_controller,
        )
        self.electrical.out_fig.update_layout(
            title=None,
            showlegend=False,
            xaxis_title=None,
            yaxis_title='Volts',
            width=840,
            height=230,
            margin=dict(l=60, r=200, t=8, b=20),
            # yaxis={"position": 0, "anchor": "free"},
            yaxis={
                "range": [
                    min(self.electrical.out_fig.data[0].y),
                    max(self.electrical.out_fig.data[0].y)
                ],
                "autorange":
                False
            },
            xaxis={
                "showticklabels":
                False,
                "ticks":
                "",
                "range": [
                    min(self.electrical.out_fig.data[0].x),
                    max(self.electrical.out_fig.data[0].x)
                ],
                "autorange":
                False
            })
        # Fluorescence single trace
        self.fluorescence = SingleTracePlotlyWidget(
            timeseries=nwb.processing['ophys'].data_interfaces['fluorescence'].
            roi_response_series['roi_response_series'],
            foreign_time_window_controller=self.time_window_controller,
        )
        self.fluorescence.out_fig.update_layout(
            title=None,
            showlegend=False,
            width=840,
            height=230,
            margin=dict(l=60, r=200, t=8, b=20),
            yaxis_title='dF/F',
            yaxis={
                "range": [
                    min(self.fluorescence.out_fig.data[0].y),
                    max(self.fluorescence.out_fig.data[0].y)
                ],
                "autorange":
                False
            },
            xaxis={
                "range": [
                    min(self.fluorescence.out_fig.data[0].x),
                    max(self.fluorescence.out_fig.data[0].x)
                ],
                "autorange":
                False,
                "constrain":
                "domain",
                "anchor":
                "free"
            })
        # Two photon imaging
        self.photon_series = ImageSeriesWidget(
            imageseries=nwb.acquisition['raw_ophys'],
            foreign_time_window_controller=self.time_window_controller,
        )
        self.photon_series.out_fig.update_layout(
            showlegend=False,
            margin=dict(l=30, r=5, t=65, b=65),
        )

        # Frame controller
        self.frame_controller = widgets.FloatSlider(
            value=0,
            step=1 / self.nwb.acquisition['raw_ophys'].rate,
            min=self.time_window_controller.value[0],
            max=self.time_window_controller.value[1],
            description='Frame: ',
            style={'description_width': '55px'},
            continuous_update=False,
            readout=False,
            orientation='horizontal',
            layout=Layout(width='645px'),
        )

        # Add line traces marking Image frame point
        self.frame_point = go.Scatter(x=[0, 0], y=[-1000, 1000])
        self.electrical.out_fig.add_trace(self.frame_point)
        self.fluorescence.out_fig.add_trace(self.frame_point)

        # Updates frame point
        self.frame_controller.observe(self.update_frame_point)

        # Updates list of valid spike times at each change in time range
        self.time_window_controller.observe(self.updated_time_range)

        # Layout
        hbox_header = widgets.HBox(
            [self.btn_spike_times, self.time_window_controller])
        vbox_widgets = widgets.VBox([self.electrical, self.fluorescence])
        hbox_widgets = widgets.HBox([vbox_widgets, self.photon_series])

        self.children = [hbox_header, self.frame_controller, hbox_widgets]

        self.update_spike_traces()
Exemple #8
0
def stage_control(stage):
    # icons are from "font-awesome"
    where_functions = list()
    move_buttons = list()
    home_buttons = list()
    pos_fields = list()
    clusters = list()

    # xy ------------------------------------------------------------
    if hasattr(stage, 'XY'):
        xy = stage.XY
        where_functions.append(xy.where_xy)
        
        x_minus = widgets.Button(
            ax='x',
            sign=-1,
            description='-x',
            button_style='primary',
            icon='fa-arrow-left',
            width='50px')

        x_plus = widgets.Button(
            ax='x',
            sign=1,
            description='+x',
            button_style='primary',
            icon='fa-arrow-right',
            width='50px')

        y_minus = widgets.Button(
            ax='y',
            sign=-1,
            description='-y',
            button_style='primary',
            icon='fa-arrow-up',
            width='50px')

        y_plus = widgets.Button(
            ax='y',
            sign=1,
            description='+y',
            button_style='primary',
            icon='fa-arrow-down',
            width='50px')

        xy_home = widgets.Button(
            ax='xy',
            button_style='primary',
            icon='fa-home',
            width='50px')

        xy_slider = widgets.FloatSlider(description='[mm]', min=.05, max=10, step=.05, orientation='vertical',
                                        height='150px')

        x_pos = widgets.Text(
            ax='x',
            value='0',
            placeholder='enter pos',
            description='X:',
            width='150px')

        y_pos = widgets.Text(
            ax='y',
            value='0',
            placeholder='enter pos',
            description='Y:',
            width='150px')

        move_buttons.extend([x_minus, x_plus, y_minus, y_plus])
        home_buttons.append(xy_home)
        pos_fields.extend([x_pos, y_pos])

        xy_cluster = widgets.HBox(
            [xy_slider, widgets.VBox([widgets.HBox([x_minus, x_plus, xy_home]), widgets.HBox([y_minus, y_plus])])])
        clusters.append(xy_cluster)

    # z ------------------------------------------------------------
    if hasattr(stage, 'Z'):
        z = stage.Z
        where_functions.append(z.where)
        
        z_minus = widgets.Button(
            ax='z',
            sign=-1,
            description='-z',
            button_style='primary',
            icon='fa-arrow-up')

        z_plus = widgets.Button(
            ax='z',
            sign=1,
            description='+z',
            button_style='primary',
            icon='fa-arrow-down')

        z_home = widgets.Button(
            ax='z',
            button_style='primary',
            icon='fa-home',
            width='50px')

        z_slider = widgets.FloatSlider(description='[mm]',
                                       min=.05, max=10,
                                       step=.05,
                                       orientation='vertical',
                                       height='150px')

        z_pos = widgets.Text(
            ax='z',
            value='0',
            placeholder='enter pos',
            description='Z:',
            width='150px')

        move_buttons.extend([z_minus, z_plus])
        home_buttons.append(z_home)
        pos_fields.append(z_pos)

        z_cluster = widgets.VBox([widgets.HBox([z_slider, widgets.VBox([z_minus, z_plus]), z_home])])
        clusters.append(z_cluster)


    # functions ------------------------------------------------------------
    def print_pos():
        display.clear_output()
        where = tuple()
        for w in where_functions:
            where += w()
        where += (1,)  # add 'w' before affine
        for name, frame in stage.frames.iteritems():
            coords = tuple(np.dot(where, np.linalg.inv(frame.trans)))
            print '(' + ', '.join(format(p, '.3f') for p in coords[:-1]) + ')', name

    def move(b):
        if b.ax == 'x':
            xy.move_relative_xy(b.sign * xy_slider.value, 0)
        elif b.ax == 'y':
            xy.move_relative_xy(0, b.sign * xy_slider.value)
        elif b.ax == 'z':
            z.move_relative(b.sign * z_slider.value)

        print_pos()

    for button in move_buttons:
        button.on_click(move)

    def home(b):
        if b.ax == 'xy':
            if hasattr(stage, 'Z'):
                z.home()
            xy.home_xy()
        elif b.ax == 'z':
            z.home()

        print_pos()

    for button in home_buttons:
        button.on_click(home)

    def pos(b):
        if b.ax == 'x':
            x_curr, y_curr = xy.where_xy()
            xy.goto_xy(b.value, y_curr)
        if b.ax == 'y':
            x_curr, y_curr = xy.where_xy()
            xy.goto_xy(x_curr, b.value)
        elif b.ax == 'z':
            z.goto(float(b.value))

        print_pos()

    for field in pos_fields:
        field.on_submit(pos)

    line = widgets.Label(value="$---------------------------------------$")
    print_pos()

    element_list = [widgets.HBox(pos_fields), line, widgets.HBox(clusters)]
    return widgets.VBox(element_list)
    def build_widget(self):
        from ipywidgets import widgets

        start, end = self.experiment.usable_replay_range
        options = []
        current = start
        while current <= end:
            # Never display microseconds
            options.append((current.replace(microsecond=0).time().isoformat(), current))
            current += datetime.timedelta(seconds=1)
            # But we need to keep microseconds in the first value, so we don't go before
            # the experiment start when scrubbing backwards
            current = current.replace(microsecond=0)
        scrubber = widgets.SelectionSlider(
            description="Current time",
            options=options,
            disabled=False,
            continuous_update=False,
        )

        def advance(change):
            if self.realtime:
                # We're being driven in realtime, the advancement
                # here is just to keep the UI in sync
                return
            old_status = self.experiment.widget.status
            self.experiment.widget.status = "Updating"
            self.experiment.widget.render()
            self(change["new"])
            self.experiment.widget.status = old_status
            self.experiment.widget.render()

        scrubber.observe(advance, "value")

        def realtime_callback():
            self.experiment.widget.render()
            try:
                scrubber.value = self.experiment._replay_time_index.replace(
                    microsecond=0
                )
            except Exception:
                # The scrubber is an approximation of the current time, we shouldn't
                # bail out if it can't be updated (for example at experiment bounds)
                pass
            if not self.realtime:
                raise StopIteration()

        play_button = widgets.ToggleButton(
            value=False,
            description="",
            disabled=False,
            tooltip="Play back in realtime",
            icon="play",
        )

        def playback(change):
            import threading

            if change["new"]:
                thread = threading.Thread(
                    target=self.in_realtime, kwargs={"callback": realtime_callback}
                )
                thread.start()
            else:
                self.realtime = False

        play_button.observe(playback, "value")

        self.widget = widgets.HBox(children=[scrubber, play_button])
        return self.widget
Exemple #10
0
    def images_tab(self, output):
        tfigsize = widgets.Text(
            value=str(self.vars.get('figsize_images', '(10,5)')),
            description='figsize:',
            disabled=False
                )
        llegend_list = widgets.Text(
            value=str(self.vars.get('legend_list', '[model]')),
            description='legend_list:',
            disabled=False
                )
        
        t_n_images = widgets.Text(
            value=str(self.vars.get('n_images', '5')),
            description='n_images:',
            disabled=False
                )

        t_n_exps = widgets.Text(
            value=str(self.vars.get('n_exps', '3')),
            description='n_exps:',
            disabled=False
                )
        t_dirname = widgets.Text(
            value=str(self.vars.get('dirname', 'images')),
            description='dirname:',
            disabled=False
                )
        
        brefresh = widgets.Button(description="Display")
        button = widgets.VBox([brefresh,
                widgets.HBox([t_n_exps, t_n_images]),
                widgets.HBox([tfigsize, llegend_list, ]),
                widgets.HBox([t_dirname, ]),
                
                            ])

        output_plot = widgets.Output()

        with output:
            display(button)
            display(output_plot)

        def on_clicked(b):
            self.update_rm()
            output_plot.clear_output()
            with output_plot:
                w, h = tfigsize.value.strip('(').strip(')').split(',')
                self.vars['figsize_images'] = (int(w), int(h))
                self.vars['legend_list'] = get_list_from_str(llegend_list.value)
                self.vars['n_images'] = int(t_n_images.value)
                self.vars['n_exps'] = int(t_n_exps.value)
                self.vars['dirname'] = t_dirname.value
                self.rm.get_images(legend_list=self.vars['legend_list'], 
                        n_images=self.vars['n_images'],
                        n_exps=self.vars['n_exps'],
                        figsize=self.vars['figsize_images'],
                        dirname=self.vars['dirname'])
                show_inline_matplotlib_plots()
                
        brefresh.on_click(on_clicked)
Exemple #11
0
def share_tab(self, output):
    with output:
        ldownload = widgets.Label(
            value="Get 'score_list.pkl' and 'exp_dict' for each experiment.",
            #    layout=self.layout_button
        )
        bdownload = widgets.Button(description="Download Results",
                                   layout=self.layout_button)

        bdownload_out = widgets.Output(layout=self.layout_button)

        # bdownload_dropbox = widgets.Button(description="Upload to Dropbox",
        #                         layout=self.layout_button)

        # bdownload_out_dropbox  = widgets.Output(layout=self.layout_button)

        l_fname_list = widgets.Text(value=str(self.vars.get('fname_list', '')),
                                    layout=self.layout_dropdown,
                                    description='fname_list:',
                                    disabled=False)

        # l_dropbox_path = widgets.Text(
        #     value=str(self.vars.get('dropbox_path', '/shared')),
        #     description='dropbox_path:',
        #     layout=self.layout_dropdown,
        #     disabled=False
        #         )
        # l_access_token_path = widgets.Text(
        #     value=str(self.vars.get('access_token', '')),
        #     description='access_token:',
        #     layout=self.layout_dropdown,
        #     disabled=False
        #         )
        def on_upload_clicked(b):
            fname = 'results.zip'
            bdownload_out_dropbox.clear_output()
            self.vars['fname_list'] = hu.get_list_from_str(l_fname_list.value)
            self.vars['dropbox_path'] = l_dropbox_path.value
            self.vars['access_token'] = l_access_token_path.value
            with bdownload_out_dropbox:
                self.rm.to_zip(savedir_base='',
                               fname=fname,
                               fname_list=self.vars['fname_list'],
                               dropbox_path=self.vars['dropbox_path'],
                               access_token=self.vars['access_token'])

            os.remove('results.zip')
            display('result.zip sent to dropbox at %s.' %
                    self.vars['dropbox_path'])

        def on_download_clicked(b):
            fname = 'results.zip'
            bdownload_out.clear_output()
            self.vars['fname_list'] = hu.get_list_from_str(l_fname_list.value)

            with bdownload_out:
                self.rm.to_zip(savedir_base='',
                               fname=fname,
                               fname_list=self.vars['fname_list'])
            bdownload_out.clear_output()
            with bdownload_out:
                display('%d exps zipped.' % len(self.rm.exp_list))
            display(FileLink(fname, result_html_prefix="Download: "))

        bdownload.on_click(on_download_clicked)
        bdownload_zip = widgets.VBox([bdownload, bdownload_out])
        # bdownload_dropbox.on_click(on_upload_clicked)
        # bdownload_dropbox_vbox = widgets.VBox([ bdownload_dropbox, bdownload_out_dropbox])
        display(
            widgets.VBox([
                # widgets.HBox([l_fname_list, l_dropbox_path, l_access_token_path]),
                ldownload,
                widgets.HBox([bdownload_zip])
            ]))
Exemple #12
0
    def plot_tab(self, output):
        ## add stuff
        
        llegend_list = 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='legend_format:',
            layout=self.layout_dropdown,
            disabled=False
                )
        ltitle_format = widgets.Text(
            value=str(self.vars.get('title_format', '')),
            description='title_format:',
            layout=self.layout_dropdown,
            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
                )

        
        t_y_metric = widgets.Text(
            value=str(self.vars.get('y_metrics', 'train_loss')),
            description='y_metrics:',
            disabled=False
                )

        t_x_metric = widgets.Text(
            value=str(self.vars.get('x_metric', 'epoch')),
            description='x_metric:',
            disabled=False
                )

        t_groupby_list = widgets.Text(
            value=str(self.vars.get('groupby_list')),
            description='groupby_list:',
            disabled=False
                )

        t_mode = widgets.Text(
            value=str(self.vars.get('mode', 'line')),
            description='mode:',
            disabled=False
                )

        t_bar_agg = widgets.Text(
            value=str(self.vars.get('bar_agg', 'mean')),
            description='bar_agg:',
            disabled=False
                )

        t_title_list = 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,
                )

        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'
            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)

        brefresh = widgets.Button(description="Display")
        button = widgets.VBox([widgets.HBox([brefresh]),
                widgets.HBox([t_title_list, d_style]),
                widgets.HBox([t_y_metric, t_x_metric, ]),
                widgets.HBox([t_groupby_list, llegend_list, ]),
                widgets.HBox([t_mode, t_bar_agg]),
                widgets.HBox([ltitle_format, llegend_format]),
                widgets.HBox([bdownload, bdownload_out]) 
                ])

        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")

            self.update_rm()

            output_plot.clear_output()
            with output_plot:
                self.vars['y_metrics'] = get_list_from_str(t_y_metric.value)
                self.vars['x_metric'] = t_x_metric.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'] = get_list_from_str(llegend_list.value)
                self.vars['legend_format'] = llegend_format.value
                self.vars['log_metric_list'] = get_list_from_str(llog_metric_list.value)
                self.vars['groupby_list'] = get_list_from_str(t_groupby_list.value)
                self.vars['mode'] = t_mode.value
                self.vars['title_list'] = get_list_from_str(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.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'])
        
           
                
                
                show_inline_matplotlib_plots()
        
        d_style.observe(on_clicked)
        brefresh.on_click(on_clicked)

        with output:
            display(button)
            display(output_plot)
Exemple #13
0
    def table_tab(self, output):
        d_columns_txt = widgets.Label(value="Select Hyperparam column", 
                                      layout=self.layout_label,)
        d_columns = widgets.Dropdown(
                    options=['None'] + self.rm.exp_params,
                    value='None',
                    layout=self.layout_dropdown,
                    disabled=False,
                )
        d_score_columns_txt = widgets.Label(value="Select Score column",
                                            layout=self.layout_label,)
        d_score_columns = widgets.Dropdown(
                options=self.rm_original.score_keys,
                value='None',
                layout=self.layout_dropdown,
                disabled=False,
            )

        bstatus = widgets.Button(description="Jobs Status")
        blogs = widgets.Button(description="Jobs Logs")
        bfailed = widgets.Button(description="Jobs Failed")

        b_meta = widgets.Button(description="Show exp_id")
        b_diff = widgets.Button(description="Filter Columns")

        brefresh = widgets.Button(description="Display")

        button = widgets.VBox([widgets.HBox([brefresh]),
                               widgets.HBox([b_meta, b_diff]),
                               widgets.HBox([bstatus, blogs, bfailed]),
                               widgets.HBox([d_columns_txt, d_score_columns_txt]),
                               widgets.HBox([d_columns, d_score_columns ]),
        ])
        output_plot = widgets.Output()

        with output:
            display(button)
            display(output_plot)

        def on_refresh_clicked(b):
            self.update_rm()

            self.vars['columns'] = get_list_from_str(d_columns.value)
            self.vars['score_columns'] = get_list_from_str(d_score_columns.value)
            score_table = self.rm.get_score_table(columns=self.vars.get('columns'), 
                                            score_columns=self.vars.get('score_columns'),
                                            hparam_diff=self.vars.get('hparam_diff', 0),
                                            show_meta=self.vars.get('show_meta', 0))

            output_plot.clear_output()
            with output_plot:
                display(score_table) 

        def on_table_clicked(b):
            self.update_rm()
            table_dict = self.rm.get_job_summary(verbose=self.rm.verbose,
                                            username=self.vars.get('username'))

            output_plot.clear_output()
            with output_plot:
                display(table_dict['status'])
                display(table_dict['table'])   

        def on_logs_clicked(b):
            self.update_rm()
            table_dict = self.rm.get_job_summary(verbose=self.rm.verbose,
                                            username=self.vars.get('username'))
            output_plot.clear_output()
            n_logs = len(table_dict['logs'])
            with output_plot:
                for i, logs in enumerate(table_dict['logs']):
                        print('\nLogs %d/%d' % (i, n_logs), '='*50)
                        print('exp_id:', logs['exp_id'])
                        print('job_id:', logs['job_id'])

                        print('\nexp_dict')
                        print('-'*50)
                        pprint.pprint(logs['exp_dict'])
                        
                        print('\nLogs')
                        print('-'*50)
                        pprint.pprint(logs['logs'])     
        
        def on_failed_clicked(b):
            self.update_rm()
            table_dict = self.rm.get_job_summary(verbose=self.rm.verbose,
                                            username=self.vars.get('username'))
            output_plot.clear_output()
            n_failed = len(table_dict['logs_failed'])
            with output_plot:
                if len(table_dict['failed']) == 0:
                    display('no failed experiments')
                else:
                    # display(table_dict['failed'])
                    for i, failed in enumerate(table_dict['logs_failed']):
                        print('\nFailed %d/%d' % (i, n_failed), '='*50)
                        print('exp_id:', failed['exp_id'])
                        print('job_id:', failed['job_id'])

                        print('\nexp_dict')
                        print('-'*50)
                        pprint.pprint(failed['exp_dict'])
                        
                        print('\nLogs')
                        print('-'*50)
                        pprint.pprint(failed['logs'])

                        

        # Add call listeners
        brefresh.on_click(on_refresh_clicked)
        bstatus.on_click(on_table_clicked)
        blogs.on_click(on_logs_clicked)
        bfailed.on_click(on_failed_clicked)

        d_columns.observe(on_refresh_clicked)
        d_score_columns.observe(on_refresh_clicked)

        # meta stuff and column filtration
        def on_bmeta_clicked(b):
            self.vars['show_meta'] = 1 - self.vars.get('show_meta', 0)
            on_refresh_clicked(None)

        def on_hparam_diff_clicked(b):
            self.vars['hparam_diff'] = 2 - self.vars.get('hparam_diff', 0)
            on_refresh_clicked(None)

        b_meta.on_click(on_bmeta_clicked)
        b_diff.on_click(on_hparam_diff_clicked)
Exemple #14
0
    def meta_tab(self, output):
        with output:
            l_savedir_base = widgets.Label(value="savedir_base:", layout=self.layout_label,)
            l_filterby_list= widgets.Label(value="filterby_list:", layout=self.layout_label,)
            

            bdownload = widgets.Button(description="Zip to Download Experiments", 
                                    layout=self.layout_button)
                                    
            bdownload_out = widgets.Output(layout=self.layout_button)

            bdownload_dropbox = widgets.Button(description="Upload to Dropbox", 
                                    layout=self.layout_button)
                                    
            bdownload_out_dropbox  = widgets.Output(layout=self.layout_button)

            l_fname_list = widgets.Text(
                value=str(self.vars.get('fname_list', '')),
                layout=self.layout_dropdown,
                description='fname_list:',
                disabled=False
                    )

            l_dropbox_path = widgets.Text(
                value=str(self.vars.get('dropbox_path', '/shared')),
                description='dropbox_path:',
                layout=self.layout_dropdown,
                disabled=False
                    )
            l_access_token_path = widgets.Text(
                value=str(self.vars.get('access_token', '')),
                description='access_token:',
                layout=self.layout_dropdown,
                disabled=False
                    )
            def on_upload_clicked(b):
                fname = 'results.zip'
                bdownload_out_dropbox.clear_output()
                self.vars['fname_list'] = get_list_from_str(l_fname_list.value)
                self.vars['dropbox_path'] = l_dropbox_path.value
                self.vars['access_token'] = l_access_token_path.value
                with bdownload_out_dropbox:
                    self.rm.to_zip(savedir_base='', fname=fname, 
                                   fname_list=self.vars['fname_list'],
                                   dropbox_path=self.vars['dropbox_path'],
                                   access_token=self.vars['access_token'])

                os.remove('results.zip')
                display('result.zip sent to dropbox at %s.' % self.vars['dropbox_path'])


            def on_download_clicked(b):
                fname = 'results.zip'
                bdownload_out.clear_output()
                self.vars['fname_list'] = get_list_from_str(l_fname_list.value)

                with bdownload_out:
                    self.rm.to_zip(savedir_base='', fname=fname, 
                                   fname_list=self.vars['fname_list'])
                bdownload_out.clear_output()
                with bdownload_out:
                    display('%d exps zipped.' % len(self.rm.exp_list))
                display(FileLink(fname, result_html_prefix="Download: "))
                # bdownload_out.clear_output()
                # with bdownload_out:
                #     display('%d exps zipped.' % len(self.rm.exp_list))
                    
                

                

            bdownload.on_click(on_download_clicked)
            bdownload_zip = widgets.VBox([bdownload, bdownload_out])
            bdownload_dropbox.on_click(on_upload_clicked)
            bdownload_dropbox_vbox = widgets.VBox([ bdownload_dropbox, bdownload_out_dropbox])
            display(widgets.VBox([
                            widgets.HBox([l_savedir_base, self.t_savedir_base, ]), 
                            widgets.HBox([l_filterby_list, self.t_filterby_list,  ]),
                            widgets.HBox([l_fname_list, l_dropbox_path, l_access_token_path]),
                            widgets.HBox([bdownload_zip,
                                        bdownload_dropbox_vbox])
            ]) 
            )
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 = "You are right! The second level 2 magic word is 'HAS'"
        else:
            s = "It seems like you got it wrong. 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'))
Exemple #16
0
 def make_horizontal_box(cls, children, layout=Layout()):
     return widgets.HBox(children, layout=layout)
Exemple #17
0
 def make_horizontal_box(
     children: Collection[widgets.Widget], layout=Layout()) -> widgets.HBox:
     "Make a horizontal box with `children` and `layout`."
     return widgets.HBox(children, layout=layout)
Exemple #18
0
def slider(fn: Callable[[int], Any],
           min=0,
           max=10,
           step=1,
           id2index: Optional[Callable[[str], Optional[int]]] = None,
           clear_output: bool = True) -> None:
    """Interactive slider. Useful for navigating a list of items/entities/tables

    Parameters
    ----------
    fn: Callable[[int], Any]
        a rendering function that render the item at a given position in the list
    min: int
        the start range of this slider
    max: int
        the stop range of this (inclusive)
    step: int
    id2index: function
    clear_output: bool
        clear the output before each function call
    """
    # an output container
    output = widgets.Output()

    # define navigating buttons, slider, and index jumper
    prev_btn = widgets.Button(
        description='Previous',
        disabled=False,
        button_style='',
        icon='arrow-circle-left'  # (FontAwesome names without the `fa-` prefix)
    )
    next_btn = widgets.Button(
        description='Next',
        disabled=False,
        button_style='',
        icon='arrow-circle-right'  # (FontAwesome names without the `fa-` prefix)
    )
    next_btn.layout.margin = "0 0 0 8px"

    slider = widgets.IntSlider(value=min,
                               min=min,
                               max=max,
                               step=step,
                               continuous_update=False)
    slider.layout.margin = "0 0 0 8px"

    index_jumper = widgets.Text(
        value=str(min),
        description=f'Index [{slider.min}, {slider.max}]',
        disabled=False)
    id_jumper = widgets.Text(value='', description=f'Enter ID', disabled=False)

    # define functions to interactive update the index
    def update_index(index: int):
        if index < slider.min or index > slider.max:
            print(
                f"[ERROR] Invalid index for the slider. Get {index} while range is [{slider.min}, {slider.max}]"
            )
            return
        next_btn.disabled = index == slider.max
        prev_btn.disabled = index == slider.min
        slider.value = index
        index_jumper.value = str(index)

    def navigate(btn):
        with output:
            if btn == next_btn:
                update_index(slider.value + 1)
            elif btn == prev_btn:
                update_index(slider.value - 1)

    def on_slider_change(change):
        with output:
            if clear_output:
                output.clear_output()
            update_index(change['new'])
            fn(change['new'])

    def on_index_jumper_edit(change):
        index_jumper.value = "".join((c for c in change['new'] if c.isdigit()))

    def on_index_jumper_change(_sender):
        if index_jumper.value != "":
            update_index(int(index_jumper.value))

    def on_id_jumper_change(_sender):
        assert id2index is not None
        idx = id2index(id_jumper.value.strip())
        if idx is not None:
            update_index(idx)

    next_btn.on_click(navigate)
    prev_btn.on_click(navigate)
    slider.observe(on_slider_change, names='value')
    index_jumper.observe(on_index_jumper_edit, names='value')
    index_jumper.on_submit(on_index_jumper_change)
    id_jumper.on_submit(on_id_jumper_change)

    container = [prev_btn, next_btn, slider, index_jumper]
    if id2index is not None:
        container.append(id_jumper)

    display(widgets.HBox(container), output)

    with output:
        fn(slider.value)
Exemple #19
0
def generate_interface(ai):
    """Generate an interactive interface on a jupyter notebook to play against
    an agent.

    Parameters:
    -----------
    ai : RPS object
        Artificial rps agent.

    Returns:
    --------
    widget : Jupyter Notebook Widget
        Widget containing the interface.
    """
    def play(hand):
        def func(button):
            result = ai.play_one(hand)
            text = u"<br/><h1 style='background-color:{}'>{}</b>"
            if result == 0:
                text = text.format(u'yellow', u'döntetlen')
            elif result == -1:
                text = text.format(u'red', u'vereség')
            else:
                text = text.format(u'green', u'győzelem')
            result_box.value = text

            pics = {
                'k': u"<img src='pics/rock.gif' align='left'/>",
                'p': u"<img src='pics/paper.gif' align='left'/>",
                'o': u"<img src='pics/scissors.gif' align='left'/>"
            }
            ai_hand.value = (u'<center><b>AI:</b></center><br/>' +
                             pics[ai.game_log[-1]['ai']])
            player_hand.value = (u'<center><b>Ön:</b></center><br/>' +
                                 pics[hand])

        return func

    ai_hand = widgets.HTML()
    player_hand = widgets.HTML()
    result_box = widgets.HTML()

    result_container = widgets.HBox()
    result_container.children = [ai_hand, result_box, player_hand]

    rock_button = widgets.Button(description='ko')
    rock_button.on_click(play('k'))

    paper_button = widgets.Button(description='papir')
    paper_button.on_click(play('p'))

    scissors_button = widgets.Button(description='ollo')
    scissors_button.on_click(play('o'))

    button_container = widgets.HBox()
    button_container.children = [rock_button, paper_button, scissors_button]

    container = widgets.VBox()
    container.children = [button_container, result_container]

    return container
Exemple #20
0
 def __init__(self):
     laWidgets.LaWidgets.__init__(self)
     # =============================================================================
     #         self.pathText = self.createPathText()
     #         self.pathButton = self.createPathButton()
     #         self.nText = self.createTextInt(val=0, minVal=0, maxVal=100, stepSize=1, desc="n")
     #         self.p0Text = self.createTextInt(val=1, minVal=0, maxVal=100, stepSize=1, desc="p0")
     #         self.mText = self.createTextInt(val=1, minVal=0, maxVal=100, stepSize=1, desc="m")
     #         self.pText = self.createTextFloat(val=0.3, minVal=0.0, maxVal=1.0, stepSize=0.001, desc="p")
     #         self.initDText = self.createTextFloat(val=0.3, minVal=0.0, maxVal=1.0, stepSize=0.001, desc="initD")
     #         self.initDSlider = self.createSliderFloat(val=0.3, minVal=0.0, maxVal=1.0, stepSize=0.001, desc="initD")
     #         self.initDLink = self.createLink(self.initDText, self.initDSlider)
     #         self.analysisButton = self.createButton(desc = 'run Analysis')
     # =============================================================================
     self.loadButton = self.createButton(desc='load')
     self.dbscanButton = self.createButton(desc='DBSCAN')
     self.minSampleText = self.createTextInt(val=5,
                                             minVal=0,
                                             maxVal=500,
                                             stepSize=1,
                                             desc="min_Samples")
     self.epsText = self.createTextFloat(val=10.0,
                                         minVal=0.0,
                                         maxVal=1000.0,
                                         stepSize=0.1,
                                         desc="eps")
     self.channelSelector = self.createSelector(opt=['1', '2'],
                                                val='1',
                                                desc='channel')
     self.clusterSelector = self.createDropDown(desc='cluster')
     self.visClusterButton = self.createButton(desc='plot cluster')
     self.min_x_filter_text = self.createTextInt(val=0,
                                                 minVal=0,
                                                 maxVal=45000,
                                                 stepSize=1,
                                                 desc="min x [nm]")
     self.max_x_filter_text = self.createTextInt(val=45000,
                                                 minVal=0,
                                                 maxVal=45000,
                                                 stepSize=1,
                                                 desc="max x [nm]")
     self.x_filter = widgets.HBox(
         (self.min_x_filter_text, self.max_x_filter_text))
     self.min_stdX_filter_text = self.createTextFloat(val=0.0,
                                                      minVal=0.0,
                                                      maxVal=1000.0,
                                                      stepSize=0.1,
                                                      desc="min std_x [nm]")
     self.max_stdX_filter_text = self.createTextFloat(val=30.0,
                                                      minVal=0.0,
                                                      maxVal=1000.0,
                                                      stepSize=0.1,
                                                      desc="max std_x [nm]")
     self.stdX_filter = widgets.HBox(
         (self.min_stdX_filter_text, self.max_stdX_filter_text))
     self.min_y_filter_text = self.createTextInt(val=0,
                                                 minVal=0,
                                                 maxVal=81000,
                                                 stepSize=1,
                                                 desc="min y [nm]")
     self.min_y_filter_text = self.createTextInt(val=0,
                                                 minVal=0,
                                                 maxVal=81000,
                                                 stepSize=1,
                                                 desc="min y [nm]")
     self.max_y_filter_text = self.createTextInt(val=81000,
                                                 minVal=0,
                                                 maxVal=81000,
                                                 stepSize=1,
                                                 desc="max y [nm]")
     self.y_filter = widgets.HBox(
         (self.min_y_filter_text, self.max_y_filter_text))
     self.min_stdY_filter_text = self.createTextFloat(val=0.0,
                                                      minVal=0.0,
                                                      maxVal=1000.0,
                                                      stepSize=0.1,
                                                      desc="min std_y [nm]")
     self.max_stdY_filter_text = self.createTextFloat(val=30.0,
                                                      minVal=0.0,
                                                      maxVal=1000.0,
                                                      stepSize=0.1,
                                                      desc="max std_y [nm]")
     self.stdY_filter = widgets.HBox(
         (self.min_stdY_filter_text, self.max_stdY_filter_text))
     self.min_int_filter_text = self.createTextInt(val=0,
                                                   minVal=0,
                                                   maxVal=10000,
                                                   stepSize=1,
                                                   desc="min int")
     self.max_int_filter_text = self.createTextInt(val=10000,
                                                   minVal=0,
                                                   maxVal=10000,
                                                   stepSize=1,
                                                   desc="max int")
     self.int_filter = widgets.HBox(
         (self.min_int_filter_text, self.max_int_filter_text))
     self.min_area_filter_text = self.createTextInt(
         val=0,
         minVal=0,
         maxVal=1000,
         stepSize=1,
         desc="min area [nm\u00B2]")
     self.max_area_filter_text = self.createTextInt(
         val=1000,
         minVal=0,
         maxVal=1000,
         stepSize=1,
         desc="max area [nm\u00B2]")
     self.area_filter = widgets.HBox(
         (self.min_area_filter_text, self.max_area_filter_text))
     self.filterButton = self.createButton(desc='Filter Fiducials')
     self.visFiducialButton = self.createButton(desc='plot fiducials')
     self.x_shift_float = self.createSliderFloat(val=0.0,
                                                 minVal=-2000.0,
                                                 maxVal=2000.0,
                                                 stepSize=1.0,
                                                 ori='horizontal',
                                                 desc="dx")
     self.y_shift_float = self.createSliderFloat(val=0.0,
                                                 minVal=-2000.0,
                                                 maxVal=2000.0,
                                                 stepSize=1.0,
                                                 ori='vertical',
                                                 desc="dy")
     self.shift = widgets.HBox((self.y_shift_float, self.x_shift_float))
     self.thr_dist = self.createTextInt(val=250,
                                        minVal=0,
                                        maxVal=1000,
                                        stepSize=1,
                                        desc="distance threshold [nm]")
     self.matrixButton = self.createButton(desc='create matrix')
     self.outFilePrefix = self.createTextStr(value='channel2',
                                             desc='file prefix',
                                             placeHolder='channel2')
     self.saveButton = self.createButton(desc='save matrix')
    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,
                        use_field_mesh=ptcl_use_field_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,
                        use_field_mesh=ptcl_use_field_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')
        set_widget_dimensions(slider, width=500)

        # Forward button
        button_p = widgets.Button(description="+")
        set_widget_dimensions(button_p, width=40)
        button_p.on_click(step_fw)

        # Backward button
        button_m = widgets.Button(description="-")
        set_widget_dimensions(button_m, width=40)
        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 \
                    ["1dcartesian", "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(value=0.,
                                               description=r'Theta:',
                                               min=-math.pi / 2,
                                               max=math.pi / 2)
            set_widget_dimensions(theta_button, width=250)
            theta_button.observe(refresh_field, 'value', 'change')
            # Slicing buttons (for 3D)
            slicing_dir_button = widgets.ToggleButtons(
                value=self.axis_labels[0],
                options=self.axis_labels,
                description='Slicing direction:')
            slicing_dir_button.observe(refresh_field, 'value', 'change')
            slicing_button = widgets.FloatSlider(description='Slicing:',
                                                 min=-1.,
                                                 max=1.,
                                                 value=0.)
            set_widget_dimensions(slicing_button, width=250)
            slicing_button.observe(refresh_field, 'value', 'change')

            # Plotting options
            # ----------------
            # Figure number
            fld_figure_button = widgets.IntText(description='Figure ', value=0)
            set_widget_dimensions(fld_figure_button, width=50)
            # Range of values
            fld_range_button = widgets.IntRangeSlider(min=-10, max=10)
            set_widget_dimensions(fld_range_button, width=220)
            fld_range_button.observe(refresh_field, 'value', 'change')
            # Order of magnitude
            fld_magnitude_button = widgets.IntText(description='x 10^',
                                                   value=9)
            set_widget_dimensions(fld_magnitude_button, width=50)
            fld_magnitude_button.observe(refresh_field, 'value', 'change')
            # Use button
            fld_use_button = widgets.Checkbox(description=' Use this range',
                                              value=False)
            set_widget_dimensions(fld_use_button, left_margin=100)
            fld_use_button.observe(refresh_field, 'value', 'change')
            # Colormap button
            fld_color_button = widgets.Select(options=sorted(
                plt.cm.datad.keys()),
                                              value='jet')
            set_widget_dimensions(fld_color_button, height=50, width=200)
            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(children=[
                    fieldtype_button, coord_button, mode_button, theta_button
                ])
            elif self.geometry in ["1dcartesian", "2dcartesian"]:
                container_fields = widgets.VBox(
                    children=[fieldtype_button, coord_button])
            elif self.geometry == "3dcartesian":
                container_fields = widgets.VBox(children=[
                    fieldtype_button, coord_button, slicing_dir_button,
                    slicing_button
                ])
            set_widget_dimensions(container_fields, width=260)
            # Plotting options container
            container_fld_magnitude = widgets.HBox(
                children=[fld_magnitude_button, fld_use_button])
            set_widget_dimensions(container_fld_magnitude, height=50)
            if self.geometry == "1dcartesian":
                container_fld_plots = widgets.VBox(children=[
                    fld_figure_button, fld_range_button,
                    container_fld_magnitude
                ])
            else:
                container_fld_plots = widgets.VBox(children=[
                    fld_figure_button, fld_range_button,
                    container_fld_magnitude, fld_color_button
                ])
            set_widget_dimensions(container_fld_plots, width=260)
            # 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(children=[
                accord1,
                widgets.HBox(children=[fld_refresh_toggle, fld_refresh_button])
            ])
            set_widget_dimensions(container_fld, width=300)

        # Particle widgets
        # ----------------
        if (self.avail_species is not None):

            # Particle quantities
            # -------------------
            # Species selection
            ptcl_species_button = widgets.Dropdown(options=self.avail_species)
            set_widget_dimensions(ptcl_species_button, width=250)
            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)
            set_widget_dimensions(ptcl_figure_button, width=50)
            # Number of bins
            ptcl_bins_button = widgets.IntText(description='nbins:', value=100)
            set_widget_dimensions(ptcl_bins_button, width=60)
            ptcl_bins_button.observe(refresh_ptcl, 'value', 'change')
            # Colormap button
            ptcl_color_button = widgets.Select(options=sorted(
                plt.cm.datad.keys()),
                                               value='Blues')
            set_widget_dimensions(ptcl_color_button, height=50, width=200)
            ptcl_color_button.observe(refresh_ptcl, 'value', 'change')
            # Range of values
            ptcl_range_button = widgets.IntRangeSlider(min=0,
                                                       max=10,
                                                       value=(0, 5))
            set_widget_dimensions(ptcl_range_button, width=220)
            ptcl_range_button.observe(refresh_ptcl, 'value', 'change')
            # Order of magnitude
            ptcl_magnitude_button = widgets.IntText(description='x 10^',
                                                    value=9)
            set_widget_dimensions(ptcl_magnitude_button, width=50)
            ptcl_magnitude_button.observe(refresh_ptcl, 'value', 'change')
            # Use button
            ptcl_use_button = widgets.Checkbox(description=' Use this range',
                                               value=False)
            set_widget_dimensions(ptcl_use_button, left_margin=100)
            ptcl_use_button.observe(refresh_ptcl, 'value', 'change')
            # Use field mesh buttons
            ptcl_use_field_button = widgets.Checkbox(
                description=' Use field mesh', value=True)
            set_widget_dimensions(ptcl_use_field_button, left_margin=100)
            ptcl_use_field_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(children=[
                ptcl_species_button, ptcl_xaxis_button, ptcl_yaxis_button
            ])
            set_widget_dimensions(container_ptcl_quantities, width=310)
            # Particle selection container
            container_ptcl_select = ptcl_select_widget.to_container()
            # Plotting options container
            container_ptcl_bins = widgets.HBox(
                children=[ptcl_bins_button, ptcl_use_field_button])
            container_ptcl_magnitude = widgets.HBox(
                children=[ptcl_magnitude_button, ptcl_use_button])
            set_widget_dimensions(container_ptcl_magnitude, height=50)
            container_ptcl_plots = widgets.VBox(children=[
                ptcl_figure_button, container_ptcl_bins, ptcl_range_button,
                container_ptcl_magnitude, ptcl_color_button
            ])
            set_widget_dimensions(container_ptcl_plots, width=310)
            # 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(children=[
                accord2,
                widgets.HBox(
                    children=[ptcl_refresh_toggle, ptcl_refresh_button])
            ])
            set_widget_dimensions(container_ptcl, width=370)

        # 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)
Exemple #22
0
 def make_horizontal_box(cls, children):
     return widgets.HBox(children)
Exemple #23
0
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",
                               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)

    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')

    w_legend = wdg.SelectMultiple(header="Legend:",
                                  options=db.rm.exp_params,
                                  db_vars=db.vars,
                                  var='legend_list')

    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=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',
                             options=['last', 'max', 'mean'],
                             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()) > 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)
df.sample(3)
df['carrier'].unique()

month = widgets.IntSlider(value=1.0,
                          min=1.0,
                          max=12.0,
                          step=1.0,
                          description='Month:',
                          continuous_update=False)

use_date = widgets.Checkbox(
    description='Date: ',
    value=True,
)

container = widgets.HBox(children=[use_date, month])

textbox = widgets.Dropdown(
    description='Airline: ',
    value='DL',
    options='Origin Airport:',
)

# Assign an empty figure widget with two traces
trace1 = go.Histogram(x=df['arr_delay'], opacity=0.75, name='Arrival Delays')
trace2 = go.Histogram(x=df['dep_delay'], opacity=0.75, name='Departure Delays')
g = go.FigureWidget(data=[trace1, trace2],
                    layout=go.Layout(title=dict(text='NYC FlightDatabase'),
                                     barmode='overlay'))

Exemple #25
0
def plot_interactive_mapper_graph(pipeline, data, layout='kamada_kawai',
                                  layout_dim=2, color_variable=None,
                                  node_color_statistic=None,
                                  color_by_columns_dropdown=False,
                                  plotly_kwargs=None):
    """Plotting function for interactive Mapper graphs.

    Parameters
    ----------
    pipeline : :class:`~gtda.mapper.pipeline.MapperPipeline` object
        Mapper pipeline to act on to data.

    data : array-like of shape (n_samples, n_features)
        Data used to generate the Mapper graph. Can be a pandas dataframe.

    layout : None, str or callable, optional, default: ``'kamada-kawai'``
        Layout algorithm for the graph. Can be any accepted value for the
        ``layout`` parameter in the :meth:`layout` method of
        :class:`igraph.Graph`. [1]_

    layout_dim : int, default: ``2``
        The number of dimensions for the layout. Can be 2 or 3.

    color_variable : object or None, optional, default: ``None``
        Specifies which quantity is to be used for node coloring.

            1. If a numpy ndarray or pandas dataframe, `color_variable`
               must have the same length as `data` and is interpreted as
               a quantity of interest according to which node of the Mapper
               graph is to be colored (see `node_color_statistic`).
            2. If ``None`` then equivalent to passing `data`.
            3. If an object implementing :meth:`transform` or
               :meth:`fit_transform`, e.g. a scikit-learn estimator or
               pipeline, it is applied to `data` to generate the quantity
               of interest.
            4. If an index or string, or list of indices / strings, equivalent
               to selecting a column or subset of columns from `data`.

    node_color_statistic :None, callable, or ndarray of shape (n_nodes,) or \
        (n_nodes, 1), optional, default: ``None``
        Specifies how to determine the colors of each node. If a
        numpy array, it must have the same length as the number of nodes in
        the Mapper graph, and its values are used directly for node
        coloring, ignoring `color_variable`. Otherwise, it can be a
        callable object which is used to obtain a summary statistic, within
        each Mapper node, of the quantity specified by `color_variable`. The
        default value ``None`` is equivalent to passing ``numpy.mean``.

    color_by_columns_dropdown : bool, optional, default: ``False``
        If ``True``, a dropdown widget is generated which allows the user to
        color Mapper nodes according to any column in `data`.

    plotly_kwargs : dict, optional, default: ``None``
        Keyword arguments to configure the Plotly Figure.

    Returns
    -------
    box : :class:`ipywidgets.VBox` object
    A box containing the following widgets: parameters of the clustering
    algorithm, parameters for the covering scheme, a Mapper graph arising
    from those parameters, a validation box, and logs.

    References
    ----------
    .. [1] `igraph.Graph.layout
            <https://igraph.org/python/doc/igraph.Graph-class.html#layout>`_
            documentation.

    """

    # clone pipeline to avoid side effects from in-place parameter changes
    pipe = clone(pipeline)

    if node_color_statistic is not None:
        _node_color_statistic = node_color_statistic
    else:
        _node_color_statistic = np.mean

    def get_widgets_per_param(param, value):
        if isinstance(value, float):
            return (param, widgets.FloatText(
                value=value,
                step=0.05,
                description=param.split('__')[1],
                continuous_update=False,
                disabled=False
            ))
        elif isinstance(value, int):
            return (param, widgets.IntText(
                value=value,
                step=1,
                description=param.split('__')[1],
                continuous_update=False,
                disabled=False
            ))
        elif isinstance(value, str):
            return (param, widgets.Text(
                value=value,
                description=param.split('__')[1],
                continuous_update=False,
                disabled=False
            ))
        else:
            return None

    def update_figure(figure, edge_trace, node_trace, layout_dim):
        figure.data[0].x = edge_trace.x
        figure.data[0].y = edge_trace.y
        figure.data[1].x = node_trace.x
        figure.data[1].y = node_trace.y

        if layout_dim == 3:
            figure.data[0].z = edge_trace.z
            figure.data[1].z = node_trace.z

        figure.data[1].marker.size = node_trace.marker.size
        figure.data[1].marker.color = node_trace.marker.color
        figure.data[1].marker.cmin = node_trace.marker.cmin
        figure.data[1].marker.cmax = node_trace.marker.cmax
        figure.data[1].marker.sizeref = node_trace.marker.sizeref
        figure.data[1].hoverlabel = node_trace.hoverlabel
        figure.data[1].hovertext = node_trace.hovertext

    def on_parameter_change(change):
        handler.clear_logs()
        try:
            for param, value in cover_params.items():
                if isinstance(value, (int, float, str)):
                    pipe.set_params(
                        **{param: cover_params_widgets[param].value})
            for param, value in cluster_params.items():
                if isinstance(value, (int, float, str)):
                    pipe.set_params(
                        **{param: cluster_params_widgets[param].value})

            logger.info("Updating figure ...")
            with fig.batch_update():
                (node_trace, edge_trace, node_elements, node_colors,
                 plot_options) = _calculate_graph_data(
                    pipe, data, layout, layout_dim,
                    color_variable, _node_color_statistic, plotly_kwargs
                )
                update_figure(fig, edge_trace, node_trace, layout_dim)

                # Update color by column buttons
                is_data_dataframe = hasattr(data, 'columns')
                if color_by_columns_dropdown:
                    column_color_buttons = _get_column_color_buttons(
                        data, is_data_dataframe, node_elements, node_colors,
                        plot_options['node_trace_marker_colorscale'])
                else:
                    column_color_buttons = None

                button_height = 1.1
                fig.update_layout(
                    updatemenus=[
                        go.layout.Updatemenu(
                            buttons=column_color_buttons,
                            direction="down",
                            pad={"r": 10, "t": 10},
                            showactive=True,
                            x=0.11,
                            xanchor='left',
                            y=button_height,
                            yanchor="top"
                        ),
                    ])

            valid.value = True
        except Exception:
            exception_data = traceback.format_exc().splitlines()
            logger.exception(exception_data[-1])
            valid.value = False

    def observe_widgets(params, widgets):
        for param, value in params.items():
            if isinstance(value, (int, float, str)):
                widgets[param].observe(on_parameter_change, names='value')

    # define output widget to capture logs
    out = widgets.Output()

    @out.capture()
    def click_box(change):
        if logs_box.value:
            out.clear_output()
            handler.show_logs()
        else:
            out.clear_output()

    # initialise logging
    logger = logging.getLogger(__name__)
    handler = OutputWidgetHandler()
    handler.setFormatter(logging.Formatter(
        '%(asctime)s - [%(levelname)s] %(message)s'))
    logger.addHandler(handler)
    logger.setLevel(logging.INFO)

    # initialise cover and cluster dictionaries of parameters and widgets
    cover_params = dict(filter(lambda x: x[0].startswith('cover'),
                               pipe.get_mapper_params().items()))
    cover_params_widgets = dict(
        filter(None, map(lambda x: get_widgets_per_param(*x),
                         cover_params.items())))
    cluster_params = dict(filter(lambda x: x[0].startswith('clusterer'),
                                 pipe.get_mapper_params().items()))
    cluster_params_widgets = dict(
        filter(None, map(lambda x: get_widgets_per_param(*x),
                         cluster_params.items())))

    # initialise widgets for validating input parameters of pipeline
    valid = widgets.Valid(
        value=True,
        description='Valid parameters',
        style={'description_width': '100px'},
    )

    # initialise widget for showing the logs
    logs_box = widgets.Checkbox(
        description='Show logs: ',
        value=False,
        indent=False
    )

    # initialise figure with initial pipeline and config
    if plotly_kwargs is None:
        plotly_kwargs = dict()

    fig = plot_static_mapper_graph(
        pipe, data, layout, layout_dim, color_variable, _node_color_statistic,
        color_by_columns_dropdown, plotly_kwargs, clone_pipeline=False)

    observe_widgets(cover_params, cover_params_widgets)
    observe_widgets(cluster_params, cluster_params_widgets)

    logs_box.observe(click_box, names='value')

    # define containers for input widgets
    container_cover = widgets.HBox(
        children=list(cover_params_widgets.values()))

    container_cluster_layout = Layout(display='flex', flex_flow='row wrap')

    container_cluster = widgets.HBox(
        children=list(cluster_params_widgets.values()),
        layout=container_cluster_layout)

    box = widgets.VBox(
        [container_cover, container_cluster, fig, valid, logs_box, out])
    return box
    def createUI(self):

        # ------------
        # Callbacks
        # ------------
        # Callback for image label dropdown menu
        def dropdown_changed(obj):
            # Note that updating the dropdown label in code (e.g. in the updateUI() function)
            # also triggers this change event. Hence need to check if self.boUpdatingUI is False.
            if obj['type'] == 'change' and obj[
                    'name'] == 'value' and not self.boUpdatingUI:
                imgIndex = int(obj['owner'].description[6:])
                imgObj = self.dataset.images[imgIndex]
                newLabelName = obj['owner'].value
                oldLabelName = imgObj.label

                # physically move image to sub-directory of the new label
                imgObj.label = newLabelName
                imgPathSrc = os.path.join(self.imgOrigDir, oldLabelName,
                                          imgObj.filename)
                imgPathDst = os.path.join(self.imgOrigDir, newLabelName,
                                          imgObj.filename)
                if os.path.exists(imgPathDst):
                    raise Exception(
                        "Cannot more image from {} to {} since the destination already exists."
                        .format(imgPathSrc, imgPathDst))
                shutil.move(imgPathSrc, imgPathDst)
                print("Moved image file from {} to {}.".format(
                    imgPathSrc, imgPathDst))

        # Callback for "zoom" button
        def img_button_pressed(obj):
            imgIndex = int(obj.value)
            imgObj = self.dataset.images[imgIndex]
            self.updateZoomUI(imgObj)

        # Callback for "next images" or "previous images" buttons
        def page_button_pressed(obj):
            self.pageIndex += int(obj.value)
            self.pageIndex = max(0, self.pageIndex)
            self.pageIndex = min(self.pageIndex, len(self.pageImgIndices) - 1)
            self.updateUI()

        # Callback for "image page" slider
        def page_slider_changed(obj):
            try:
                self.pageIndex = int(obj['new']['value'])
                self.updateUI()
            except Exception as e:
                pass

        # Init
        self.boUpdatingUI = False

        # ------------
        # UI - image grid
        # ------------
        self.wImgs = []
        self.wLabels = []
        self.wButtons = []
        wImgLabelButtons = []

        for i in range(self.gridSize[0] * self.gridSize[1]):
            # Initialize images
            wImg = widgets.Image(width=150, description="")
            #wImg = widgets.Image(height=400, description="")
            self.wImgs.append(wImg)

            # Initialize dropdown menus
            wLabel = widgets.Dropdown(options=self.labels,
                                      value=self.labels[0],
                                      text="Image 0",
                                      description="Image 0")
            wLabel.layout.width = '200px'
            wLabel.observe(dropdown_changed, names='value')
            self.wLabels.append(wLabel)

            # Initialize zoom buttons
            wButton = widgets.Button(description="Image id: ", value="")
            wButton.layout.width = "100px"
            wButton.button_style = 'warning'
            wButton.on_click(img_button_pressed)
            self.wButtons.append(wButton)

            # combine into image grid widget
            wImgLabelButton = widgets.VBox(children=[wButton, wImg, wLabel])
            wImgLabelButton.width = '230px'
            wImgLabelButtons.append(wImgLabelButton)

        # Image grid widget
        wGridHBoxes = []
        for r in range(self.gridSize[0]):
            hbox = widgets.HBox(children=[
                wImgLabelButtons[r * self.gridSize[1] + c]
                for c in range(self.gridSize[1])
            ])
            hbox.layout.padding = '10px'
            wGridHBoxes.append(hbox)
        wImgGrid = widgets.VBox(wGridHBoxes)

        # ------------
        # UI - zoom window
        # ------------
        wNextPageButton = widgets.Button(description="Next images", value="1")
        wNextPageButton.value = "1"  # should not be necessary but bug on some jupyter versions otherwise
        wNextPageButton.layout.width = '120px'
        wNextPageButton.button_style = 'primary'
        wNextPageButton.on_click(page_button_pressed)

        wPreviousPageButton = widgets.Button(description="Previous images",
                                             value="-1",
                                             layout=Layout(
                                                 color='white',
                                                 background_color='lightblue'))
        wPreviousPageButton.value = "-1"
        wPreviousPageButton.layout.width = '120px'
        wPreviousPageButton.button_style = 'primary'
        wPreviousPageButton.on_click(page_button_pressed)

        self.wPageSlider = IntSlider(min=0,
                                     max=len(self.pageImgIndices) - 1,
                                     step=1,
                                     value=self.pageIndex,
                                     continuous_update=False,
                                     description='Image page:')
        self.wPageSlider.observe(page_slider_changed)

        self.wZoomHeader = widgets.Text("")
        self.wZoomHeader.layout.width = "100px"
        self.wZoomHeader.layout.color = 'white'
        self.wZoomHeader.layout.background_color = 'orange'
        self.wZoomImg = widgets.Image()
        self.wZoomImg.layout.width = str(self.wZoomImgWidth) + 'px'
        self.wZoomTextArea = widgets.Textarea()
        self.wZoomTextArea.layout.width = '500px'
        self.wZoomTextArea.layout.height = '100px'

        #wZoomButtonSlider = widgets.HBox([widgets.VBox([wNextPageButton, wPreviousPageButton]),
        #                                  self.wPageSlider])  # self.wZoomHeader
        wZoomButtonSlider = widgets.VBox(
            [wNextPageButton, wPreviousPageButton, self.wPageSlider])
        wZoomButtonSlider.layout.width = str(self.wZoomImgWidth +
                                             20) + 'px'  # '420px'

        # ------------
        # UI - final
        # ------------
        annotationUI = widgets.HBox(children=[
            widgets.VBox(children=[
                wZoomButtonSlider, self.wZoomImg, self.wZoomTextArea
            ],
                         width=520), wImgGrid
        ])
        annotationUI.layout.border_color = 'black'
        annotationUI.layout.border_style = 'solid'
        tabsUI = widgets.Tab(children=[annotationUI])
        tabsUI.set_title(0, 'Image Annotation')

        # Update UI with actual images
        self.updateUI()
        return (tabsUI)
Exemple #27
0
    def __init__(self,
                 plot_mpl_cls,
                 run_dir_options=None,
                 fig=None,
                 output_widget=None,
                 **kwargs):
        """
        Parameters
        ----------
        run_dir_options: list
            list of tuples with label and filepath
        plot_mpl_cls: a valid plot_mpl class handle (not string!)
            Specifies the underlying plot_mpl visualizer that will be used.
        fig: a matplotlib figure instance.(optional)
            If no figure is provided, the widget will create its own figure.
            Otherwise the widget will draw into the provided figure and will
            not create its own.
        output_widget: None or instance of ipywidgets.Output
            used for capturing messages from the visualizers.
            If None, a new output widget is created and displayed
            as a child. If not None, it is not displayed but only
            used for capturing output and the owner of the output
            is responsible for displaying it.
        kwargs: options for plotting, passed on to matplotlib commands.
        """
        widgets.VBox.__init__(self)

        if output_widget is not None:
            # for a given output widget we don't add
            # it to the children but rely on the owner
            # of the widget to display it somewhere
            assert isinstance(output_widget, widgets.Output)
            add_out_to_children = False
        else:
            output_widget = widgets.Output(
                layout={'border': '1px solid black'})
            add_out_to_children = True
        self.output_widget = output_widget

        # if no figure is given, we create one and add it to children
        # if one is given, we don't add it as part of children
        add_fig_to_children = fig is None
        # setting of figure and ax
        self.fig = None
        self.ax = None
        self._init_fig_and_ax(fig, **kwargs)

        # create the PIConGPU visualizer instance but do not set
        # any run directories yet
        self.plot_mpl = plot_mpl_cls(None, ax=self.ax)

        self.label_path_lut = None
        self._create_label_path_lookup(run_dir_options)

        # widgets for selecting the simulation and the simulation step
        # dependent on the derived class which widget it should be
        # use the simulation labels of the plot_mpl visualizer from picongpu
        self.sim_drop = self._create_sim_dropdown(
            sorted(list(self.label_path_lut.keys())))
        self.sim_drop.observe(self._handle_run_dir_selection_callback,
                              names='value')

        self.sim_time_slider = widgets.SelectionSlider(
            description='Time [s]',
            options=[""],
            continuous_update=False,
            # layout=widgets.Layout(width='65%', height='10%'))
        )
        self.sim_time_slider.observe(self._visualize_callback, names='value')

        # widgets that this specific widget might need
        # to expose the parameters of the plot_mpl object.
        self.widgets_for_vis_args = \
            self._create_widgets_for_vis_args()
        # Its changes will result in changes to the plot
        for _, widg in self.widgets_for_vis_args.items():
            widg.observe(self._visualize_callback, names='value')

        # register the ui elements that will be displayed
        # as children of this object.
        vis_widgets = widgets.VBox(children=[
            self.sim_drop,
            widgets.VBox(children=list(self.widgets_for_vis_args.values()))
        ])
        if add_fig_to_children:
            top = widgets.HBox(children=[vis_widgets, self.fig.canvas])
        else:
            top = vis_widgets

        if add_out_to_children:
            bottom = widgets.VBox(
                children=[self.sim_time_slider, self.output_widget])
        else:
            bottom = self.sim_time_slider

        self.children = [top, bottom]
Exemple #28
0
def tables_tab(db, output):
    w_columns = wdg.SelectMultiple(header="Hyperparameters:",
                                   options=db.rm.exp_params,
                                   db_vars=db.vars,
                                   var='columns',
                                   select_all=True)
    w_score_columns = wdg.SelectMultiple(header="Metrics:",
                                         options=db.rm.score_keys,
                                         db_vars=db.vars,
                                         var='score_columns',
                                         select_all=True)

    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")

    # download logs
    bdownload = widgets.Button(description="Download Logs")
    bdownload_out = widgets.Output()

    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, bdownload, bdownload_out]),
    ])
    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 get_logs(failed_only=False):
        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:
            stdout = ('NO FAILED JOBS')
            return stdout

        n_failed = len(summary_dict['FAILED'])

        if n_failed == 0:
            stdout = ('no failed experiments\n')
        else:
            stdout = ''
            for i, failed in enumerate(summary_dict['FAILED']):
                stdout += ('\nFailed %d/%d ' % (i + 1, n_failed) + '=' * 50)
                stdout += ('\nexp_id: ' + failed['exp_id'])
                stdout += ('\njob_id: ' + failed['job_id'])
                stdout += ('\njob_state: ' + 'FAILED')
                stdout += ('\nsavedir: ' + os.path.join(
                    db.rm_original.savedir_base, failed['exp_id']))

                stdout += ('\n\nexp_dict')
                stdout += ('\n' + '-' * 50 + '\n')
                stdout += pprint.pformat(failed['exp_dict'])

                stdout += ('\n\nLogs\n')
                stdout += ('-' * 50 + '\n')
                stdout += pprint.pformat(failed.get('logs'))
                stdout += ('\n')

        return stdout

    def on_failed_clicked(b):
        output_plot.clear_output()
        with output_plot:
            db.update_rm()
            stdout = get_logs(failed_only=True)
            print(stdout)

    def on_download_clicked(b):
        fname = 'logs.txt'
        hu.save_txt(fname, get_logs(failed_only=True))

        bdownload_out.clear_output()

        with bdownload_out:
            display(FileLink(fname, result_html_prefix="Download: "))

    bdownload.on_click(on_download_clicked)

    # 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)
Exemple #29
0
    def load(self, file='', folder='', data=None, data_type='sample', auto_gamma_filter=True,
             manual_gamma_filter=False, notebook=False, manual_gamma_threshold=0.1):
        """
        Function to read individual files, entire files from folder, list of files or event data arrays.
        Data are also gamma filtered if requested.
        
        Parameters:
           file: list -  full path to a single file, or list of files
           folder: string - full path to folder containing files to load
           data: numpy array - 2D array of data to load
           data_type: string - 'sample', 'ob' or 'df (default 'sample')
           auto_gamma_filter: boolean - will correct the gamma filter automatically (highest count possible
                for the data type will be replaced by the average of the 9 neighboring pixels) (default True)
           manual_gamma_filter: boolean - apply or not gamma filtering to the data loaded (default False)
           notebooks: boolean - turn on this option if you run the library from a
             notebook to have a progress bar displayed showing you the progress of the loading (default False)
            manual_gamma_threshold: float between 0 and 1 - manual gamma coefficient to use (default 0.1)

        Warning:
            Algorithm won't be allowed to run if any of the main algorithm have been run already, such as
                oscillation, crop, binning, df_correction.

        """

        list_exec_flag = [_flag for _flag in self.__exec_process_status.values()]
        box1 = None
        if True in list_exec_flag:
            raise IOError("Operation not allowed as you already worked on this data set!")

        if notebook:
            from ipywidgets import widgets
            from IPython.core.display import display

        if not file == '':
            if isinstance(file, str):
                self.load_file(file=file,
                               data_type=data_type,
                               auto_gamma_filter=auto_gamma_filter,
                               manual_gamma_filter=manual_gamma_filter,
                               manual_gamma_threshold=manual_gamma_threshold)
            elif isinstance(file, list):
                if notebook:
                    # turn on progress bar
                    _message = "Loading {}".format(data_type)
                    box1 = widgets.HBox([widgets.Label(_message,
                                                       layout=widgets.Layout(width='20%')),
                                         widgets.IntProgress(max=len(file)),
                                         widgets.Label("Time remaining:",
                                                       layout=widgets.Layout(width='10%')),
                                         widgets.Label(" >> calculating << ")])
                    display(box1)
                    w1 = box1.children[1]
                    time_remaining_ui = box1.children[-1]

                start_time = time.time()
                for _index, _file in enumerate(file):
                    self.load_file(file=_file,
                                   data_type=data_type,
                                   auto_gamma_filter=auto_gamma_filter,
                                   manual_gamma_filter=manual_gamma_filter,
                                   manual_gamma_threshold=manual_gamma_threshold)
                    if notebook:
                        w1.value = _index + 1
                        end_time = time.time()
                        takes_its_going_to_take = self.calculate_how_long_its_going_to_take(index_we_are=_index + 1,
                                                                                            time_it_took_so_far=end_time - start_time,
                                                                                            total_number_of_loop=len(
                                                                                                file))
                        time_remaining_ui.value = "{}".format(takes_its_going_to_take)

                if notebook:
                    box1.close()

        elif not folder == '':
            # load all files from folder
            list_images = get_sorted_list_images(folder=folder)
            if notebook:
                # turn on progress bar
                _message = "Loading {}".format(data_type)
                box1 = widgets.HBox([widgets.Label(_message,
                                                   layout=widgets.Layout(width='20%')),
                                     widgets.IntProgress(max=len(list_images)),
                                     widgets.Label("Time remaining:",
                                                   layout=widgets.Layout(width='10%')),
                                     widgets.Label(" >> calculating << ")])
                display(box1)
                w1 = box1.children[1]
                time_remaining_ui = box1.children[-1]

            start_time = time.time()
            for _index, _image in enumerate(list_images):
                full_path_image = os.path.join(folder, _image)
                self.load_file(file=full_path_image,
                               data_type=data_type,
                               auto_gamma_filter=auto_gamma_filter,
                               manual_gamma_filter=manual_gamma_filter,
                               manual_gamma_threshold=manual_gamma_threshold)
                if notebook:
                    # update progress bar
                    w1.value = _index + 1
                    end_time = time.time()
                    takes_its_going_to_take = self.calculate_how_long_its_going_to_take(index_we_are=_index + 1,
                                                                                        time_it_took_so_far=end_time - start_time,
                                                                                        total_number_of_loop=len(
                                                                                            list_images))
                    time_remaining_ui.value = "{}".format(takes_its_going_to_take)

            if notebook:
                box1.close()

        elif not data is None:
            self.load_data(data=data, data_type=data_type)
Exemple #30
0
def show_plot():
    plt.close('all')
    plt.ioff()

    # load in the trace
    from glob import glob
    import os
    global df, trace_o, trace_n, title
    df = pd.read_csv('merged_data.csv')

    # read in the models
    fit_o = models.oocyte_model(df)
    fit_n = models.oocyte_model(df)

    # read in the traces
    trace_o = pm.load_trace('trace_o', fit_o)
    trace_n = pm.load_trace('trace_n', fit_n)

    fig, ax = plt.subplots(1, 1, figsize=(8, 6))
    out = widgets.Output()

    #res_df = pd.read_pickle("res.pkl")
    def click(b):
        global o_obs, o_x, n_obs, n_x, df

        mask = df['i_ind'] == choice.value
        if (sum(mask) > 0):
            title_comp = df.loc[mask, 'ucode'].values[0].split('_')
            fig.suptitle("{}".format(title_comp[0]))
            ax.set_title("day {}.rep {}, str {}".format(*title_comp[1:]))

        Vmax_o = Vmax_o_slide.value
        ao = ao_slide.value
        to = to_slide.value
        Vmin_o = Vmin_o_slide.value

        Vmax_n = Vmax_n_slide.value
        a0 = a0_slide.value
        t0 = t0_slide.value
        a1 = a1_slide.value
        t1 = t1_slide.value
        Vmin_n = Vmin_n_slide.value
        #plt.figure(2)

        ax.clear()
        ax.plot(-o_x, o_obs, 'o', color='red')
        ax.plot(-n_x, n_obs, 'o', color='blue')
        if (True):
            x = np.linspace(-2, 16, num=100)
            ax.plot(-x,
                    bu.rise_only(x, Vmax_o, to, ao, 1.0, Vmin_o),
                    color='red')
            ax.plot(-x,
                    bu.rise_and_fall(x, Vmax_n, t0, a0, 1.0, t1, a1, 1,
                                     Vmin_n),
                    color='blue')

            ax.plot([-to, -to],
                    [0, bu.rise_only(to, Vmax_o, to, ao, 1.0, Vmin_o)],
                    ls='--',
                    color='red',
                    lw=1.5)
            ax.plot([-t0, -t0], [
                0,
                bu.rise_and_fall(t0, Vmax_n, t0, a0, 1.0, t1, a1, 1, Vmin_n)
            ],
                    ls='--',
                    color='blue',
                    lw=1.5)
            ax.plot([-t1, -t1], [
                0,
                bu.rise_and_fall(t1, Vmax_n, t0, a0, 1.0, t1, a1, 1, Vmin_n)
            ],
                    ls='--',
                    color='blue',
                    lw=1.5)

            ax.set_xticks(range(-14, 2, 2))
            ax.set_ylim(0, 200)
            ax.axhline(Vmax_o, ls='--', color='red')
            ax.axvline(0, ls='--', color='grey', lw=1)

            [xmin, xmax] = ax.get_xlim()
            [ymin, ymax] = ax.get_ylim()
            ax.annotate(r'$t_o$', (to + 0.2, ymax - 10))
            ax.annotate(r'$Vmax_{o}}$', (xmax - 2, Vmax_o + 10))

        with out:
            clear_output(wait=True)
            display(ax.figure)

    #choice=Dropdown(
    #
    #	options='cont_r1 cont_r2 cont_r3 caged_d04_r1 caged_d07_r1'.split(),
    #	value='cont_r1',
    #	description='Number:',
    #	disabled=False,
    #)
    choice = widgets.IntText(value=0,
                             min=0,
                             max=len(df['i_ind'].unique()),
                             step=1,
                             description='Test:',
                             disabled=False,
                             continuous_update=False,
                             readout=True,
                             readout_format='d')

    Vmax_o_slide = FloatSlider(description=r'$V$max$_o$',
                               value=150,
                               min=0,
                               max=300,
                               continuous_update=False)
    Vmax_o_slide.observe(click, names='value')
    Vmin_o_slide = FloatSlider(description=r'$V$min$_o$',
                               value=15,
                               min=0,
                               max=30,
                               continuous_update=False)
    Vmin_o_slide.observe(click, names='value')
    ao_slide = FloatSlider(description=r'$a_o$',
                           value=0.2,
                           min=0.,
                           max=0.75,
                           continuous_update=False)
    ao_slide.observe(click, names='value')
    to_slide = FloatSlider(description=r'$t_o$',
                           value=1,
                           min=-2,
                           max=6,
                           continuous_update=False)
    to_slide.observe(click, names='value')

    Vmax_n_slide = FloatSlider(description=r'$V$max$_{n}$',
                               value=150,
                               min=0,
                               max=300,
                               continuous_update=False)
    Vmax_n_slide.observe(click, names='value')
    Vmin_n_slide = FloatSlider(description=r'$V$min$_n$',
                               value=15,
                               min=0,
                               max=30,
                               continuous_update=False)
    Vmin_n_slide.observe(click, names='value')
    a0_slide = FloatSlider(description=r'$a_0$',
                           value=0.4,
                           min=0.0,
                           max=1.5,
                           continuous_update=False)
    a0_slide.observe(click, names='value')
    t0_slide = FloatSlider(description=r'$t_0$',
                           value=0,
                           min=-4,
                           max=8,
                           continuous_update=False)
    t0_slide.observe(click, names='value')

    a1_slide = FloatSlider(description=r'$a_1$',
                           value=0.4,
                           min=0.0,
                           max=8,
                           continuous_update=False)
    a1_slide.observe(click, names='value')
    t1_slide = FloatSlider(description=r'$t_1$',
                           value=0.5,
                           min=-2,
                           max=6,
                           continuous_update=False)
    t1_slide.observe(click, names='value')

    def choice_selected(b):
        global o_obs, o_x, n_obs, n_x, df, trace_o, trace_n
        if (False):
            name = choice.value
            df = pd.read_csv("results_analyse/{}.csv".format(name))
            o_obs = dh.unpack_results(df, 1, 'o', volume=False)
            o_x = np.arange(len(o_obs))
            n_obs = dh.unpack_results(df, 1, 'n', volume=False)
            n_x = np.arange(len(n_obs))
        else:
            iexp = choice.value
            mask = df['i_ind'] == iexp
            if (sum(mask) > 0):
                o_obs = df.loc[mask, 'Oc_size']
                o_x = df.loc[mask, 'pos']
                n_obs = df.loc[mask, 'Ns_size']
                n_x = o_x

                vars_o = 'Vmax_o,t_o,a_o,Vmin_o'.split(',')
                vars_n = 'Vmax_n t0 a0 t1 a1 Vmin_n'.split()
                theta_o = np.median(bu.pull_post(trace_o, vars_o, iexp),
                                    axis=0)
                theta_n = np.median(bu.pull_post(trace_n, vars_n, iexp),
                                    axis=0)
                for slide, val in zip(
                    [Vmax_o_slide, to_slide, ao_slide, Vmin_o_slide], theta_o):
                    slide.value = val
                for slide, val in zip([
                        Vmax_n_slide, t0_slide, a0_slide, t1_slide, a1_slide,
                        Vmin_n_slide
                ], theta_n):
                    slide.value = val

        #rown = "{}_1".format(name)
        #Vmax_n_slide.value= res_df.loc[rown,('Vmax_n','m')]
        #a0_slide.value= res_df.loc[rown,('a0','m')]
        #t0_slide.value= -res_df.loc[rown,('t0','m')]
        #a1_slide.value= res_df.loc[rown,('a1','m')]
        #t1_slide.value= -res_df.loc[rown,('t1','m')]

        #Vmax_o_slide.value= res_df.loc[rown,('Vmax_o','m')]
        #ao_slide.value= res_df.loc[rown,('a_o','m')]
        #to_slide.value= -res_df.loc[rown,('t_o','m')]
        click(None)
        #f(Vmax_slide.value, a0_slide.value, t0_slide.value)
        return

    choice_selected(None)
    choice.observe(choice_selected)

    #display(VBox([mslide,cslide]))
    oocyte_params = widgets.VBox([
        Label(value="Oocyte"), Vmax_o_slide, ao_slide, to_slide, Vmin_o_slide
    ])
    nurse_params = widgets.VBox(
        [Vmax_n_slide, a0_slide, t0_slide, a1_slide, t1_slide, Vmin_n_slide])
    box = widgets.VBox(
        [choice, widgets.HBox([oocyte_params, nurse_params]), out])
    display(box)

    click(None)