def quantityfunction(self, quantity):
        if self.w is not None:
            self.w.close()
        add_mn = False
        add_c = False
        elem = None
        if ':' in quantity and self.result.matrix_structure is not None and self.result.element_wise:
            add_mn = True
            elem = (0, 0)
            if self.result.complex_elements:
                add_c = True
                elem = (0, 0, 0)

        fun, obj = self.quantity_getattr(quantity, elem)

        to_plot = fun.original(obj, maxent_result=self.result, element=elem)
        if isinstance(to_plot, list):
            default = OrderedDict()
            for qty in to_plot[::-1]:
                default.update(qty[2])
        else:
            x, y, default = fun.original(obj,
                                         maxent_result=self.result,
                                         element=elem)
        if add_mn:
            default['n_m'] = self.result.matrix_structure[0]
            default['n_n'] = self.result.matrix_structure[1]
        if add_c:
            default['n_c'] = 2

        if self.inter is not None:
            self.inter.widget.close()
        additional_widgets = OrderedDict()
        for key in default:
            if key in ['x_label', 'y_label', 'label']:
                continue
            if "n_" + key in default:
                continue
            origkey = key
            if key.startswith("n_"):
                key = key[2:]
            if isinstance(default[origkey], bool):
                additional_widgets[key] = \
                    widgets.Checkbox(
                    value=default[origkey],
                    description=key
                )
            elif isinstance(default[origkey], int):
                toval = 0
                if 'n_' + key in default:
                    toval = default['n_' + key] - 1
                additional_widgets[key] = \
                    widgets.IntSlider(value=0,
                                      min=0,
                                      max=toval,
                                      step=1,
                                      description=key)

        def widget_change():
            with self.out:
                clear_output(wait=True)
                arguments = dict()
                for key, widget in additional_widgets.iteritems():
                    arguments[key] = widget.value
                self.plotfunction(quantity, **arguments)
                show_inline_matplotlib_plots()

        for key, widget in additional_widgets.iteritems():
            widget.observe(lambda change: widget_change(),
                           names='value',
                           type='change')
        children = [value for key, value in additional_widgets.iteritems()]
        self.w = widgets.VBox(children=children)
        display(self.w)
        try:
            self.out.clear_output()
        except:
            pass
        self.out = Output()
        display(self.out)
        widget_change()
Esempio n. 2
0
    def __init__(self, m_desc, FuseEdges=False, max_stack=30, max_width=9.0):
        # Options
        self.color_accept = 'chartreuse3'
        self.color_reject = 'red'
        self.color_neutral = 'dodgerblue2'
        self.max_width = max_width
        self.fuse = FuseEdges
        # PDA specific options
        self.condition = 'ACCEPT_F'
        self.stack_size = 6
        # initialize
        self.valid_input = True
        self.machine = m_desc
        self.machine_obj = dotObj_pda(self.machine, FuseEdges=FuseEdges)
        self.copy_source = reformat_edge_labels(
            set_graph_size(self.machine_obj.source, max_width))

        # Set things we need for the animation
        self.machine_steps = []
        self.feed_steps = []
        self.stack_steps = []
        self.from_nodes = self.machine['q0']
        self.to_nodes = self.machine['q0']
        self.animated = False
        self.is_back_step = False

        # Setup the widgets
        # Top row for user input
        self.user_input = widgets.Text(value='',
                                       placeholder='Sigma: {{{}}}'.format(
                                           ','.join(
                                               sorted(self.machine['Sigma']))),
                                       description='Input:',
                                       layout=Layout(width='500px'))
        self.user_input.observe(self.on_input_change, names='value')
        self.alternate_start = widgets.Dropdown(options=sorted(
            self.machine['Q']),
                                                value=self.machine['q0'],
                                                description='Start State:',
                                                disabled=False,
                                                layout=Layout(width='200px'))
        self.generate_button = widgets.Button(description="Animate",
                                              button_style='primary',
                                              disabled=False)
        self.generate_button.on_click(self.generate_animation)
        self.acceptance_toggle = widgets.Dropdown(options=[
            ('State', 'ACCEPT_F'), ('Stack', 'ACCEPT_S')
        ],
                                                  description='Acceptance:',
                                                  disabled=False,
                                                  layout=Layout(width='160px'))

        # Bottom row for player controls
        self.play_controls = widgets.Play(interval=950,
                                          value=0,
                                          min=0,
                                          max=100,
                                          step=1,
                                          description="Press play",
                                          disabled=True)
        self.play_controls.observe(self.on_play_step, names='value')

        self.speed_control = widgets.IntSlider(value=1,
                                               min=1,
                                               max=10,
                                               step=1,
                                               description='Speed:',
                                               disabled=False,
                                               continuous_update=False,
                                               orientation='horizontal',
                                               readout=True,
                                               readout_format='d')
        self.speed_control.observe(self.on_speed_change, names='value')

        # Create the controls for stepping through the animation
        self.backward = widgets.Button(icon='step-backward',
                                       layout=Layout(width='40px'),
                                       disabled=True)
        self.forward = widgets.Button(icon='step-forward',
                                      layout=Layout(width='40px'),
                                      disabled=True)
        self.backward.on_click(self.on_backward_click)
        self.forward.on_click(self.on_forward_click)

        # set the widget to display the machine
        self.machine_display = widgets.Output()
        with self.machine_display:
            display(Source(self.copy_source))

        # set the widget to display the stack
        self.stack_display = widgets.Output()
        s_state = self.set_stack_display()
        with self.stack_display:
            display(Source(s_state))
        self.stack_size_slider = widgets.IntSlider(value=self.stack_size,
                                                   min=2,
                                                   max=max_stack,
                                                   step=1,
                                                   description='Stack Size:',
                                                   disabled=False,
                                                   continuous_update=False,
                                                   orientation='horizontal',
                                                   readout=True,
                                                   readout_format='d')
        self.stack_size_slider.observe(self.on_stack_size_change,
                                       names='value')

        # set the widget to display the feed
        self.feed_display = widgets.Output()
        f_state, inspecting = self.generate_feed('', 0, 0, [])
        with self.feed_display:
            display(Source(f_state))

        self.path_dropdown = widgets.Dropdown(options={},
                                              value=None,
                                              description='Path:',
                                              disabled=True,
                                              layout=Layout(width='200px'))
        self.path_dropdown.observe(self.on_path_change, names='value')

        # arrange the widgets in the display area
        row1 = widgets.HBox(
            [self.user_input, self.acceptance_toggle, self.generate_button])
        row2 = widgets.HBox([self.stack_size_slider])
        ms_disp = widgets.HBox([self.stack_display, self.machine_display])
        play_row = widgets.HBox([
            self.path_dropdown, self.play_controls, self.backward,
            self.forward, self.speed_control
        ])
        w = widgets.VBox([row1, row2, ms_disp, self.feed_display, play_row])
        display(w)

        self.play_controls.disabled = True
        self.forward.disabled = True
        self.backward.disabled = True
        self.speed_control.disabled = True
Esempio n. 3
0
    def __init__(self,
                 m_desc,
                 FuseEdges=False,
                 show_rejected=False,
                 max_width=10.0,
                 accept_color='chartreuse3',
                 reject_color='red',
                 neutral_color='dodgerblue2'):
        # Options
        self.color_accept = accept_color
        self.color_reject = reject_color
        self.color_neutral = neutral_color
        self.max_width = max_width
        self.fuse = FuseEdges
        # TM specific option
        self.show_rejected = show_rejected

        # initialize
        self.valid_input = True
        self.machine = m_desc
        self.machine_obj = dotObj_tm(self.machine, FuseEdges=FuseEdges)
        self.copy_source = reformat_edge_labels(
            set_graph_size(self.machine_obj.source, max_width))

        # Set things we need for the animation
        self.machine_steps = []
        self.tape_steps = []
        self.from_nodes = self.machine['q0']
        self.to_nodes = self.machine['q0']
        self.animated = False
        self.is_back_step = False

        # Top row for user input
        self.user_input = widgets.Text(value='',
                                       placeholder='Sigma: {{{}}}'.format(
                                           ','.join(
                                               sorted(self.machine['Sigma']))),
                                       description='Input:',
                                       layout=Layout(width='500px'))
        self.user_input.observe(self.on_input_change, names='value')
        self.generate_button = widgets.Button(description="Animate",
                                              button_style='primary',
                                              disabled=False)
        self.generate_button.on_click(self.generate_animation)
        self.start_fuel = widgets.BoundedIntText(value=10,
                                                 min=0,
                                                 max=1000,
                                                 step=1,
                                                 description='Fuel:',
                                                 layout=Layout(width='160px'),
                                                 disabled=False)

        # Bottom row for player controls
        self.play_controls = widgets.Play(interval=950,
                                          value=0,
                                          min=0,
                                          max=100,
                                          step=1,
                                          description="Press play",
                                          disabled=True)
        self.play_controls.observe(self.on_play_step, names='value')

        self.speed_control = widgets.IntSlider(value=1,
                                               min=1,
                                               max=10,
                                               step=1,
                                               description='Speed:',
                                               disabled=False,
                                               continuous_update=False,
                                               orientation='horizontal',
                                               readout=True,
                                               readout_format='d')
        self.speed_control.observe(self.on_speed_change, names='value')

        # Create the controls for stepping through the animation
        self.backward = widgets.Button(icon='step-backward',
                                       layout=Layout(width='40px'),
                                       disabled=True)
        self.forward = widgets.Button(icon='step-forward',
                                      layout=Layout(width='40px'),
                                      disabled=True)
        self.backward.on_click(self.on_backward_click)
        self.forward.on_click(self.on_forward_click)

        # set the widget to display the machine
        self.machine_display = widgets.Output()
        with self.machine_display:
            display(Source(self.copy_source))

            # set a widget to display rejected output
            self.rejection_display = widgets.Output()
            self.rejection_text = widgets.HTML(value="")
            self.reject_msg_start = '<p style="color:{}; text-align:center"><b>\'<span style="font-family:monospace">'.format(
                self.color_reject)
            self.reject_msg_end = '</span>\' was REJECTED</b></br>(Try running with more \'fuel\')</p>'

        # set the widget to display the tape
        self.tape_display = widgets.Output()
        with self.tape_display:
            display(Source(self.generate_tape('........', 0, '', 10)))

        self.path_dropdown = widgets.Dropdown(options={},
                                              value=None,
                                              description='',
                                              disabled=True,
                                              layout=Layout(width='200px'))
        self.path_dropdown.observe(self.on_path_change, names='value')

        # TODO: REMOVE TESTING CODE
        self.test_output = widgets.Output()

        # arrange the widgets in the display area
        row1 = widgets.HBox(
            [self.user_input, self.start_fuel,
             self.generate_button])  # not displaying alternate start state
        ms_disp = widgets.HBox([self.machine_display])
        tp_disp = widgets.HBox([self.tape_display])
        play_row = widgets.HBox([
            self.path_dropdown, self.play_controls, self.backward,
            self.forward, self.speed_control
        ])
        w = widgets.VBox([
            row1, self.rejection_display, ms_disp, tp_disp, play_row,
            self.test_output
        ])
        display(w)

        self.play_controls.disabled = True
        self.forward.disabled = True
        self.backward.disabled = True
        self.speed_control.disabled = True
Esempio n. 4
0
aanpassingstijd_voorraad_eindproducten = 1  # [week]
aanpassingstijd_voorraad_componenten = 1  # [week]
aanpassingstijd_voorraad_onderdelen = 1  # [week]

# interface voor aan- of uitzetten rationele strategie
strategie_button = widgets.Checkbox(
    value=False,
    description="Rationeel",
    icon='check')

# interface voor de tijdconstante van de pijplijnvoorraad
style = {'description_width': 'initial'}
aanpassingstijd_pijplijn_eindproducten_slider = widgets.IntSlider(
    value=10,
    min=1,
    max=10,
    step=1,
    description="Eindproducten [week]",
    style=style,
    readout_format="d")
aanpassingstijd_pijplijn_componenten_slider = widgets.IntSlider(
    value=10,
    min=1,
    max=10,
    step=1,
    description="Componenten [week]",
    style=style,
    readout_format="d")
aanpassingstijd_pijplijn_onderdelen_slider = widgets.IntSlider(
    value=10,
    min=1,
    max=10,
Esempio n. 5
0
def tool_template(m=None):

    widget_width = "250px"
    padding = "0px 0px 0px 5px"  # upper, right, bottom, left

    toolbar_button = widgets.ToggleButton(
        value=False,
        tooltip="Toolbar",
        icon="gear",
        layout=widgets.Layout(width="28px", height="28px", padding="0px 0px 0px 4px"),
    )

    close_button = widgets.ToggleButton(
        value=False,
        tooltip="Close the tool",
        icon="times",
        button_style="primary",
        layout=widgets.Layout(height="28px", width="28px", padding="0px 0px 0px 4px"),
    )

    checkbox = widgets.Checkbox(
        description="Checkbox",
        indent=False,
        layout=widgets.Layout(padding=padding, width=widget_width),
    )

    dropdown = widgets.Dropdown(
        options=["Option 1", "Option 2", "Option 3"],
        value=None,
        description="Dropdown:",
        layout=widgets.Layout(width=widget_width, padding=padding),
        style={"description_width": "initial"},
    )

    int_slider = widgets.IntSlider(
        min=1,
        max=100,
        description="Int Slider: ",
        readout=False,
        continuous_update=True,
        layout=widgets.Layout(width="220px", padding=padding),
        style={"description_width": "initial"},
    )

    int_slider_label = widgets.Label()
    widgets.jslink((int_slider, "value"), (int_slider_label, "value"))

    float_slider = widgets.FloatSlider(
        min=1,
        max=100,
        description="Float Slider: ",
        readout=False,
        continuous_update=True,
        layout=widgets.Layout(width="220px", padding=padding),
        style={"description_width": "initial"},
    )

    float_slider_label = widgets.Label()
    widgets.jslink((float_slider, "value"), (float_slider_label, "value"))

    color = widgets.ColorPicker(
        concise=False,
        description="Color:",
        value="white",
        style={"description_width": "initial"},
        layout=widgets.Layout(width=widget_width, padding=padding),
    )

    text = widgets.Text(
        value="",
        description="Textbox:",
        placeholder="Placeholder",
        style={"description_width": "initial"},
        layout=widgets.Layout(width=widget_width, padding=padding),
    )

    textarea = widgets.Textarea(
        placeholder="Placeholder",
        layout=widgets.Layout(width=widget_width),
    )

    buttons = widgets.ToggleButtons(
        value=None,
        options=["Apply", "Reset", "Close"],
        tooltips=["Apply", "Reset", "Close"],
        button_style="primary",
    )
    buttons.style.button_width = "80px"

    output = widgets.Output(layout=widgets.Layout(width=widget_width, padding=padding))

    toolbar_widget = widgets.VBox()
    toolbar_widget.children = [toolbar_button]
    toolbar_header = widgets.HBox()
    toolbar_header.children = [close_button, toolbar_button]
    toolbar_footer = widgets.VBox()
    toolbar_footer.children = [
        checkbox,
        widgets.HBox([int_slider, int_slider_label]),
        widgets.HBox([float_slider, float_slider_label]),
        dropdown,
        text,
        color,
        textarea,
        buttons,
        output,
    ]

    toolbar_event = ipyevents.Event(
        source=toolbar_widget, watched_events=["mouseenter", "mouseleave"]
    )

    def handle_toolbar_event(event):

        if event["type"] == "mouseenter":
            toolbar_widget.children = [toolbar_header, toolbar_footer]
        elif event["type"] == "mouseleave":
            if not toolbar_button.value:
                toolbar_widget.children = [toolbar_button]
                toolbar_button.value = False
                close_button.value = False

    toolbar_event.on_dom_event(handle_toolbar_event)

    def toolbar_btn_click(change):
        if change["new"]:
            close_button.value = False
            toolbar_widget.children = [toolbar_header, toolbar_footer]
        else:
            if not close_button.value:
                toolbar_widget.children = [toolbar_button]

    toolbar_button.observe(toolbar_btn_click, "value")

    def close_btn_click(change):
        if change["new"]:
            toolbar_button.value = False
            if m is not None:
                if m.tool_control is not None and m.tool_control in m.controls:
                    m.remove_control(m.tool_control)
                    m.tool_control = None
            toolbar_widget.close()

    close_button.observe(close_btn_click, "value")

    def button_clicked(change):
        if change["new"] == "Apply":
            with output:
                output.clear_output()
                print("Running ...")
        elif change["new"] == "Reset":
            textarea.value = ""
            output.clear_output()
        elif change["new"] == "Close":
            if m is not None:
                m.toolbar_reset()
                if m.tool_control is not None and m.tool_control in m.controls:
                    m.remove_control(m.tool_control)
                    m.tool_control = None
            toolbar_widget.close()

        buttons.value = None

    buttons.observe(button_clicked, "value")

    toolbar_button.value = True
    if m is not None:
        toolbar_control = WidgetControl(widget=toolbar_widget, position="topright")

        if toolbar_control not in m.controls:
            m.add_control(toolbar_control)
            m.tool_control = toolbar_control
    else:
        return toolbar_widget
def cfs_dsp(coeff, tables, n_eqn, y_lm, line_nms, tbl_horz=0):
    """
INPUTS
tables           (list of df's)  summary table's
ceoff            (list of df's)  input data
line_nms         (list of strings) legend entry names
y_lm             (int) upper y limit of the plot

OUTPUTS
Plot and Tables Displayed with interactive widgets

    """
    # Layout of each widget
    box_hlayout = ipw.Layout(display='flex',
                             flex_flow='row',
                             align_items='stretch',
                             width='95%')

    box_vlayout = ipw.Layout(display='flex',
                             flex_flow='column',
                             align_items='stretch',
                             width='10%',
                             height='auto',
                             justify_content='space-between')

    if n_eqn == 1:
        # Coefficient selection widget
        coeff_sel = ipw.IntSlider(min=1,
                                  max=coeff[0].shape[1],
                                  value=1,
                                  step=1,
                                  description='Coefficient:',
                                  width='auto',
                                  layout=box_hlayout,
                                  style={'description_width': 'initial'})
    elif n_eqn > 1:
        # Coefficient selection widget
        coeff_sel = ipw.IntSlider(min=1,
                                  max=coeff[0][0].shape[1],
                                  value=1,
                                  step=1,
                                  description='Coefficient:',
                                  width='auto',
                                  layout=box_hlayout,
                                  style={'description_width': 'initial'})
    # Equation selection widget
    eqn_sel = ipw.IntSlider(min=1,
                            max=n_eqn,
                            value=1,
                            step=1,
                            description='Equation:',
                            width='auto',
                            layout=box_hlayout,
                            style={'description_width': 'initial'})
    # PU bandwidth constant selection widget
    ch_sel = ipw.FloatSlider(min=0.1,
                             max=3,
                             value=2.5,
                             step=0.2,
                             description='Bandwidth Constant',
                             width='auto',
                             layout=box_hlayout,
                             style={'description_width': 'initial'})
    # Horizontal display range slider widget
    xlim_sel = ipw.FloatRangeSlider(value=[-0.4, 0.4],
                                    min=-1,
                                    max=1,
                                    step=0.05,
                                    description='x limits:',
                                    disabled=False,
                                    continuous_update=False,
                                    orientation='horizontal',
                                    readout=True,
                                    readout_format='.1f',
                                    width='auto',
                                    layout=box_hlayout,
                                    style={'description_width': 'initial'})

    ylim_sel = ipw.FloatSlider(min=0,
                               max=15,
                               value=y_lm,
                               step=1,
                               description='y limits',
                               orientation='vertical',
                               length='auto',
                               layout=box_vlayout,
                               style={'description_length': 'initial'},
                               readout=False)

    # Interactive call of density function plot
    coeff_out = ipw.interactive_output(
        coeffden, {
            'coeff': ipw.fixed(coeff),
            'line_nms': ipw.fixed(line_nms),
            'x_lm': xlim_sel,
            'y_lm': ylim_sel,
            'c_h': ch_sel,
            'w': coeff_sel,
            'n_eqn': ipw.fixed(n_eqn),
            's_eqn': eqn_sel
        })
    # Interactive call of table display function
    table_out = ipw.interactive_output(
        tbl_dsp, {
            'tables': ipw.fixed(tables),
            'n_eqn': ipw.fixed(n_eqn),
            's_eqn': eqn_sel,
            'line_nms': ipw.fixed(line_nms),
            'horz': ipw.fixed(tbl_horz)
        })
    # Return of the constructed block with widgetes and tables.
    if n_eqn == 1:
        return ipw.VBox([
            table_out,
            ipw.HBox([coeff_out, ylim_sel]), coeff_sel, ch_sel, xlim_sel
        ])
    else:
        return ipw.VBox([
            table_out,
            ipw.HBox([coeff_out, ylim_sel]), coeff_sel, ch_sel, xlim_sel,
            eqn_sel
        ])
    out.append('')
    out.append('{name: <16} | {typing: <16} | {default: <16} | {help}'.format(name='Attribute', typing='Type', 
                                                                             allownone='Nullable', default='Default', help='Help'))
    out.append('{0:-<16}-|-{0:-<16}-|-{0:-<16}-|----'.format('-'))
    for name, t in sorted(w.traits(sync=True).items()):
        if name in ('_model_module', '_view_module', '_model_module_version', '_view_module_version', 
                    '_dom_classes', 'layout'):
            # document these separately, since they apply to all classes
            pass
        if name in ('_view_count'):
            # don't document this since it is totally experimental at this point
            continue

        s = '{name: <16} | {typing: <16} | {default: <16} | {help}'.format(name='`%s`'%name, typing=typing(t), 
                                                            allownone='*' if t.allow_none else '', 
                                                                                               default=jsdefault(t),
                                                                                              help=t.help if t.help else '')
        out.append(s)
    out.append('')
    return '\n'.join(out)

out = header
for n,w in widgets_to_document:
    if issubclass(w, Link):
        out += '\n'+format_widget(n, w((widgets.IntSlider(), 'value'), (widgets.IntSlider(), 'value')))
    elif issubclass(w, widgets.SelectionRangeSlider) or issubclass(w, widgets.SelectionSlider):
        out += '\n'+format_widget(n,w(options=[1]))
    else:
        out += '\n'+format_widget(n,w())
print(out)
         (1+random_steps))
    ax2.plot(market_info[market_info['Date']>= split_date]['Date'].astype(datetime.datetime),
             random_walk[::-1])
    ax1.set_title('Single Point Random Walk')
    ax1.set_ylabel('')
    # for static figures, you may wish to insert the random seed value
#    ax1.annotate('Random Seed: %d'%freq, xy=(0.75, 0.2),  xycoords='axes fraction',
#            xytext=(0.75, 0.2), textcoords='axes fraction')
    ax1.legend(bbox_to_anchor=(0.1, 1), loc=2, borderaxespad=0., prop={'size': 14})
    ax2.set_title('Full Interval Random Walk')
    fig.text(0.0, 0.5, 'Ethereum Price ($)', va='center', rotation='vertical',fontsize=12)
    plt.tight_layout()
#    plt.savefig('image%d.png'%freq, bbox_inches='tight')
    plt.show()

interact(plot_func, freq =widgets.IntSlider(min=200,max=210,step=1,value=205, description='Random Seed:'))

"""
==
Long Short Term Memory (LTSM)
==
"""
for coins in ['bt_', 'eth_']:
    kwargs = { coins+'close_off_high': lambda x: 2*(x[coins+'High']- x[coins+'Close'])/(x[coins+'High']-x[coins+'Low'])-1,
            coins+'volatility': lambda x: (x[coins+'High']- x[coins+'Low'])/(x[coins+'Open'])}
    market_info = market_info.assign(**kwargs)

model_data = market_info[['Date']+[coin+metric for coin in ['bt_', 'eth_']
                                   for metric in ['Close','Volume','close_off_high','volatility']]]
# need to reverse the data frame so that subsequent rows represent later timepoints
model_data = model_data.sort_values(by='Date')
def checkerboard(image1, image2, pattern=3, invert=False, **viewer_kwargs):  # noqa: C901
    """Compare two images with a checkerboard pattern.

    This is particularly useful for examining registration results.

    Parameters
    ----------
    image1 : array_like, itk.Image, or vtk.vtkImageData
        First image to use in the checkerboard.

    image2 : array_like, itk.Image, or vtk.vtkImageData
        Second image to use in the checkerboard.

    pattern : int, optional, default: 3
        Size of the checkerboard pattern.

    invert : bool, optional, default: False
        Swap inputs.

    viewer_kwargs : optional
        Keyword arguments for the viewer. See help(itkwidgets.view).

    """

    itk_image1 = to_itk_image(image1)
    if not itk_image1:
        itk_image1 = itk.output(image1)
    itk_image2 = to_itk_image(image2)
    if not itk_image2:
        itk_image2 = itk.output(image2)
    input1 = itk_image1
    input2 = itk_image2

    region_image1 = itk_image1.GetLargestPossibleRegion()
    region_image2 = itk_image2.GetLargestPossibleRegion()
    same_physical_space = \
        np.allclose(np.array(itk_image1.GetOrigin()), np.array(itk_image2.GetOrigin())) and \
        np.allclose(np.array(itk_image1.GetSpacing()), np.array(itk_image2.GetSpacing())) and \
        itk_image1.GetDirection() == itk_image2.GetDirection() and \
        np.allclose(np.array(region_image1.GetIndex()), np.array(region_image2.GetIndex())) and \
        np.allclose(
            np.array(
                region_image1.GetSize()), np.array(
                region_image2.GetSize()))
    if not same_physical_space:
        upsample_image2 = True
        if itk_image1.GetSpacing() != itk_image2.GetSpacing():
            min1 = min(itk_image1.GetSpacing())
            min2 = min(itk_image2.GetSpacing())
            if min2 < min1:
                upsample_image2 = False
        else:
            size1 = max(itk.size(itk_image1))
            size2 = max(itk.size(itk_image1))
            if size2 > size1:
                upsample_image2 = False

        if upsample_image2:
            resampler = itk.ResampleImageFilter.New(itk_image2)
            resampler.UseReferenceImageOn()
            resampler.SetReferenceImage(itk_image1)
            resampler.Update()
            input2 = resampler.GetOutput()
        else:
            resampler = itk.ResampleImageFilter.New(itk_image1)
            resampler.UseReferenceImageOn()
            resampler.SetReferenceImage(itk_image2)
            resampler.Update()
            input1 = resampler.GetOutput()

    checkerboard_filter = itk.CheckerBoardImageFilter.New(input1, input2)

    dimension = itk_image1.GetImageDimension()
    checker_pattern = [pattern] * dimension
    checkerboard_filter.SetCheckerPattern(checker_pattern)
    checkerboard_filter_inverse = itk.CheckerBoardImageFilter.New(
        input2, input1)

    if invert:
        checkerboard_filter_inverse.Update()
        checkerboard = checkerboard_filter_inverse.GetOutput()
    else:
        checkerboard_filter.Update()
        checkerboard = checkerboard_filter.GetOutput()

    if 'annotations' not in viewer_kwargs:
        viewer_kwargs['annotations'] = False
    if 'interpolation' not in viewer_kwargs:
        viewer_kwargs['interpolation'] = False
    if 'ui_collapsed' not in viewer_kwargs:
        viewer_kwargs['ui_collapsed'] = True
    viewer = Viewer(image=checkerboard, **viewer_kwargs)

    # Heuristic to specify the max pattern size
    max_size1 = int(min(itk.size(itk_image1)) / 8)
    max_size1 = max(max_size1, pattern * 2)
    max_size2 = int(min(itk.size(itk_image2)) / 8)
    max_size2 = max(max_size2, pattern * 2)
    max_size = max(max_size1, max_size2)

    pattern_slider = widgets.IntSlider(value=pattern, min=2, max=max_size,
                                       step=1, description='Pattern size:')
    invert_checkbox = widgets.Checkbox(value=invert, description='Invert')

    def update_checkerboard(change):
        checker_pattern = [pattern_slider.value] * dimension
        checkerboard_filter.SetCheckerPattern(checker_pattern)
        checkerboard_filter_inverse.SetCheckerPattern(checker_pattern)
        if invert_checkbox.value:
            checkerboard_filter_inverse.Update()
            viewer.image = checkerboard_filter_inverse.GetOutput()
        else:
            checkerboard_filter.Update()
            viewer.image = checkerboard_filter.GetOutput()
    pattern_slider.observe(update_checkerboard, ['value'])
    invert_checkbox.observe(update_checkerboard, ['value'])

    widget = widgets.VBox([viewer,
                           widgets.HBox([pattern_slider, invert_checkbox])])

    return widget
Esempio n. 10
0
    def __init__(self,
                 data,
                 channels=None,
                 cmaps=None,
                 flip_map=False,
                 rescale_type=None,
                 limits=None,
                 num_colors=256,
                 height_pixels=3,
                 unit_per_pix=None,
                 scalebar_units=None,
                 unit=None,
                 scale_ypos=0.05,
                 scale_color='white',
                 scale_font_size=12,
                 scale_text_centered=False,
                 ax=None,
                 fig_scaling=3):
        """
        Class implementing methods to create interactive animations via ipywidgets in notebooks
        and to save animations as movies. Most options parameters are the same as for microplots.
        
        Parameters
        ----------
        data: microfilm.dataset.Data object or ndarray
            object allowing for easy loading of images. If an
            ndarray is used it needs dimension ordering CTXY
        channels: list of str
            list of channels from data object to plot
        cmaps: list of str
            colormap names
        flip_map: bool or list of bool
            invert colormap or not
        rescale_type: str or list of str
            'min_max': between extrema values of image
            'dtype': full range of image dtype
            'zero_max': between zero and image max
            'limits': between limits given by parameter limits
        limits: list or list of lists
            [min, max] limits to use for rescaling
        num_colors: int
            number of steps in color scale
        height_pixels: int
            height of scale bar
        unit_per_pix: float
            pixel scaling (e.g. 25um per pixel)
        scalebar_units: float
            size of scale bar in true units
        unit: str
            name of the scale unit
        scale_y_pos: float
            y position of scale bar (0-1)
        scale_color: str
            color of scale bar
        scale_font_size: int
            size of text, set to None for no text
        scale_text_centered: bool
            center text above scale bar
        ax: Matplotlib axis
            provide existing axis
        fig_scaling: int
            control figure scaling
        
        Attributes
        ----------
        
        """

        if isinstance(data, np.ndarray):
            data = Nparray(nparray=data)

        self.data = data

        if channels is None:
            self.channels = self.data.channel_name
        else:
            self.channels = channels

        self.cmaps = cmaps
        self.flip_map = flip_map
        self.num_colors = num_colors

        self.rescale_type = check_rescale_type(rescale_type, limits)
        self.limits = limits

        self.time_slider = ipw.IntSlider(description="Time",
                                         min=0,
                                         max=self.data.K - 1,
                                         value=0,
                                         continuous_update=True)
        self.time_slider.observe(self.update_timeslider, names="value")

        self.output = ipw.Output()
        self.timestamps = None

        # initialize
        images = [self.data.load_frame(x, 0) for x in self.channels]
        with self.output:
            self.microim = microshow(images,
                                     cmaps=cmaps,
                                     flip_map=flip_map,
                                     rescale_type=rescale_type,
                                     limits=limits,
                                     num_colors=num_colors,
                                     height_pixels=height_pixels,
                                     unit_per_pix=unit_per_pix,
                                     scalebar_units=scalebar_units,
                                     unit=unit,
                                     scale_ypos=scale_ypos,
                                     scale_color=scale_color,
                                     scale_font_size=scale_font_size,
                                     scale_text_centered=scale_text_centered,
                                     ax=ax,
                                     fig_scaling=fig_scaling)

        self.ui = ipw.VBox([self.output, self.time_slider])
Esempio n. 11
0
def view_layers(logdir, mode=0, ppc=20):
    '''
    DOCUMENTATION
    :param logdir: path to log directory that contains pickled run logs
    :param mode: viewing mode index. Must be an int between 0 and 2
        0: limits the viewing to feedforward information only (weights, biases, net_input, output)
        1: same as 0, but also includes gradient information (gweights, gbiases, gnet_input, goutput)
        2: same as 2, but also includes cumulative gradient information
    :return:
    '''
    plt.ion()
    # get runlog filenames and paths
    FILENAMES, RUNLOG_PATHS = [sorted(l) for l in list_pickles(logdir)]

    # get testing epochs and losses data
    EPOCHS, LOSSES, LOSS_SUMS = get_data_by_key(
        runlog_path=RUNLOG_PATHS[0], keys=['enum', 'loss',
                                           'loss_sum']).values()

    # get layer names and layer dims to set up figure
    layer_names = get_layer_names(runlog_path=RUNLOG_PATHS[0])
    layer_names.reverse()
    layer_dims = get_layer_dims(runlog_path=RUNLOG_PATHS[0],
                                layer_names=layer_names)

    # set up and make figure
    figure = _make_figure(layer_dims=layer_dims,
                          mode=mode,
                          ppc=ppc,
                          dpi=96,
                          fig_title='view_layers: ' + logdir)

    num_layers = len(layer_names)
    disp_targs = [True] + [False for l in layer_names[1:]]

    axes_dicts = []
    for i, (layer_name, disp_targ) in enumerate(zip(layer_names, disp_targs)):
        sp_divider = SubplotDivider(figure,
                                    num_layers,
                                    1,
                                    i + 1,
                                    aspect=True,
                                    anchor='NW')
        vdims = [dim[0] for dim in layer_dims.values()]
        sp_divider._subplotspec._gridspec._row_height_ratios = [
            vdim + 1.8 for vdim in vdims
        ]
        axes_dicts.append(
            _divide_axes_grid(mpl_figure=figure,
                              divider=sp_divider,
                              layer_name=layer_name.upper().replace('_', ' '),
                              inp_size=layer_dims[layer_name][1],
                              layer_size=layer_dims[layer_name][0],
                              mode=mode,
                              target=disp_targ))
    plt.tight_layout()

    _widget_layout = widgets.Layout(width='100%')

    run_widget = widgets.Dropdown(options=dict(zip(FILENAMES, RUNLOG_PATHS)),
                                  description='Run log: ',
                                  value=RUNLOG_PATHS[0],
                                  layout=_widget_layout)

    cmap_widget = widgets.Dropdown(options=sorted([
        'BrBG', 'bwr', 'coolwarm', 'PiYG', 'PRGn', 'PuOr', 'RdBu', 'RdGy',
        'RdYlBu', 'RdYlGn', 'seismic'
    ]),
                                   description='Colors: ',
                                   value='coolwarm',
                                   disabled=False,
                                   layout=_widget_layout)

    vrange_widget = widgets.FloatSlider(value=1.0,
                                        min=0,
                                        max=8,
                                        step=.1,
                                        description='V-range: ',
                                        continuous_update=False,
                                        layout=_widget_layout)

    step_widget = widgets.IntSlider(value=0,
                                    min=0,
                                    max=len(EPOCHS) - 1,
                                    step=1,
                                    description='Step index: ',
                                    continuous_update=False,
                                    layout=_widget_layout)

    pattern_options = get_pattern_options(runlog_path=RUNLOG_PATHS[0],
                                          tind=step_widget.value)
    options_map = {}
    for i, pattern_option in enumerate(pattern_options):
        options_map[pattern_option] = i
    pattern_widget = widgets.Dropdown(options=options_map,
                                      value=0,
                                      description='Pattern: ',
                                      disabled=False,
                                      layout=_widget_layout)

    loss_observer = LossDataObsever(
        epoch_list=EPOCHS,
        loss_list=LOSSES,
        loss_sum_list=LOSS_SUMS,
        tind=step_widget.value,
        pind=pattern_widget.value,
    )

    fig_observer = FigureObserver(mpl_figure=figure)

    step_widget.observe(handler=loss_observer.on_epoch_change, names='value')
    pattern_widget.observe(handler=loss_observer.on_pattern_change,
                           names='value')

    def on_runlog_change(change):
        if change['type'] == 'change' and change['name'] == 'value':
            newEPOCHS, newLOSSES, newLOSS_SUMS = get_data_by_key(
                runlog_path=change['new'], keys=['enum', 'loss',
                                                 'loss_sum']).values()
            step_widget.max = len(newEPOCHS) - 1
            step_widget.value = 0
            pattern_widget.value = 0
            loss_observer.new_runlog(newEPOCHS, newLOSSES, newLOSS_SUMS)

    run_widget.observe(on_runlog_change)

    controls_dict = dict(
        runlog_path=run_widget,
        img_dicts=widgets.fixed(axes_dicts),
        layer_names=widgets.fixed(layer_names),
        colormap=cmap_widget,
        vrange=vrange_widget,
        tind=step_widget,
        pind=pattern_widget,
    )

    row_layout = widgets.Layout(display='flex',
                                flex_flow='row',
                                justify_content='center')

    stretch_layout = widgets.Layout(display='flex',
                                    flex_flow='row',
                                    justify_content='space-around')

    control_panel_rows = [
        widgets.Box(
            children=[controls_dict['runlog_path'], controls_dict['pind']],
            layout=row_layout),
        widgets.Box(
            children=[controls_dict['colormap'], controls_dict['vrange']],
            layout=row_layout),
        widgets.Box(children=[controls_dict['tind']], layout=row_layout),
        widgets.Box(children=[
            loss_observer.epoch_widget, loss_observer.loss_sum_widget,
            loss_observer.loss_widget, fig_observer.widget
        ],
                    layout=stretch_layout)
    ]

    controls_panel = widgets.Box(children=control_panel_rows,
                                 layout=widgets.Layout(display='flex',
                                                       flex_flow='column',
                                                       padding='5px',
                                                       border='ridge 1px',
                                                       align_items='stretch',
                                                       width='100%'))

    widgets.interactive_output(f=_draw_layers, controls=controls_dict)
    display(controls_panel)
Esempio n. 12
0
def showPeakFit(peakNumber,
                peaks_ws,
                MDData,
                UBMatrix,
                dQ,
                padeCoefficients,
                predpplCoefficients,
                mindtBinWidth=15,
                dQPixel=0.003,
                fracHKL=0.5,
                q_frame='lab',
                neigh_length_m=3,
                dtS=0.015,
                pplmin_frac=0.4,
                pplmax_frac=1.5,
                nTheta=50,
                nPhi=50,
                intensityCutoff=250,
                edgeCutoff=3,
                fracStop=0.05,
                plotResults=False,
                strongPeakParams=None):

    #Get some peak variables
    peak = peaks_ws.getPeak(peakNumber)
    wavelength = peak.getWavelength()  #in Angstrom
    energy = 81.804 / wavelength**2 / 1000.0  #in eV
    flightPath = peak.getL1() + peak.getL2()  #in m
    scatteringHalfAngle = 0.5 * peak.getScattering()
    Box = ICCFT.getBoxFracHKL(peak,
                              peaks_ws,
                              MDData,
                              UBMatrix,
                              peakNumber,
                              dQ,
                              fracHKL=0.5,
                              dQPixel=dQPixel,
                              q_frame=q_frame)
    box = Box
    #Set up our filters
    qMask = ICCFT.getHKLMask(UBMatrix, frac=fracHKL, dQPixel=dQPixel, dQ=dQ)
    n_events = Box.getNumEventsArray()
    nX, nY, nZ = n_events.shape
    cX = nX // 2
    cY = nY // 2
    cZ = nZ // 2
    dP = 5
    qMask[cX - dP:cX + dP, cY - dP:cY + dP, cZ - dP:cZ + dP] = 0
    neigh_length_m = 3
    convBox = 1.0 * \
    np.ones([neigh_length_m, neigh_length_m,
             neigh_length_m]) / neigh_length_m**3
    conv_n_events = convolve(n_events, convBox)
    bgMask = np.logical_and(conv_n_events > 0, qMask > 0)
    meanBG = np.mean(n_events[bgMask])
    #predppl = np.polyval(f,meanBG)*1.96
    predppl = np.polyval([1, 0], meanBG) * 1.96
    qMask = ICCFT.getHKLMask(UBMatrix, frac=0.5, dQPixel=dQPixel, dQ=dQ)

    #Calculate the

    Y3D1, gIDX1, pp_lambda, params1 = BVGFT.get3DPeak(
        peak,
        box,
        padeCoefficients,
        qMask,
        nTheta=nTheta,
        nPhi=nPhi,
        plotResults=plotResults,
        zBG=1.96,
        fracBoxToHistogram=1.0,
        bgPolyOrder=1,
        strongPeakParams=strongPeakParams,
        predCoefficients=predpplCoefficients,
        q_frame=q_frame,
        mindtBinWidth=mindtBinWidth,
        pplmin_frac=pplmin_frac,
        pplmax_frac=pplmax_frac,
        forceCutoff=intensityCutoff,
        edgeCutoff=edgeCutoff)
    given_ppl = predppl
    predpplCoefficients2 = [0, 0, predppl]
    Y3D2, gIDX2, pp_lambda2, params2 = BVGFT.get3DPeak(
        peak,
        box,
        padeCoefficients,
        qMask,
        nTheta=nTheta,
        nPhi=nPhi,
        plotResults=False,
        zBG=1.96,
        fracBoxToHistogram=1.0,
        bgPolyOrder=1,
        strongPeakParams=strongPeakParams,
        predCoefficients=predpplCoefficients2,
        q_frame=q_frame,
        mindtBinWidth=mindtBinWidth,
        pplmin_frac=0.99999,
        pplmax_frac=1.0001,
        forceCutoff=intensityCutoff,
        edgeCutoff=edgeCutoff,
        figureNumber=3)
    I1 = np.sum(Y3D1[Y3D1 / Y3D1.max() > fracStop])
    I2 = np.sum(Y3D2[Y3D2 / Y3D2.max() > fracStop])
    print('Peak %i: Old: %i; New: %i; Ell: %i' %
          (peakNumber, I1, I2, peak.getIntensity()))

    slider = widgets.IntSlider(value=Y3D1.shape[1] // 2,
                               min=0,
                               max=Y3D1.shape[2] - 1,
                               step=1,
                               description='z Slice:',
                               disabled=False,
                               continuous_update=False,
                               orientation='horizontal',
                               readout=True,
                               readout_format='d')
    return slider, n_events, Y3D1, Y3D2
Esempio n. 13
0
def options(X):

    style = {'description_width': 'initial'}  #general style settings

    #horizontal line widget
    HL = widgets.HTML(
        value=
        '<hr style="height:3px;border:none;color:#333;background-color:#333;" />'
    )

    M_title = widgets.HTML(value='<h3>Select data type:</h3>')
    M_widge = widgets.RadioButtons(
        options=['Magnetisations', 'Lower branch subtracted'],
        value='Lower branch subtracted',
        style=style)

    ### Horizontal smoothing ###
    S_title = widgets.HTML(value='<h3>Set smoothing parameters:</h3>')

    #SC widgets
    Sc_widge = widgets.IntRangeSlider(value=[2, 10],
                                      min=2,
                                      max=10,
                                      step=1,
                                      description='Select $s_c$ range:',
                                      continuous_update=False,
                                      orientation='horizontal',
                                      readout=True,
                                      readout_format='.0f',
                                      style=style)

    Sb_widge = widgets.IntRangeSlider(value=[2, 10],
                                      min=2,
                                      max=10,
                                      step=1,
                                      description='Select $s_u$ range:',
                                      continuous_update=False,
                                      orientation='horizontal',
                                      readout=True,
                                      readout_format='.0f',
                                      style=style)

    lambda_widge = widgets.FloatRangeSlider(
        value=[0.0, 0.2],
        min=0,
        max=0.2,
        step=0.04,
        description='Select $\lambda$ range:',
        disabled=False,
        continuous_update=False,
        orientation='horizontal',
        readout=True,
        readout_format='.2f',
        style=style)

    constraint_widge = widgets.Checkbox(value=False,
                                        description='Assume $Sc_0$ = $Su_0$',
                                        style=style)
    constraint_html = widgets.HTML(
        value=
        'Check <a href="https://forcaist.github.io/FORCaist.github.io/dfaq.html#AssumeS" target="_blank">FORCsensei FAQ</a> for more information about the &nbsp;Sc<sub>0</sub> = Su<sub>0</sub> option'
    )
    constraint_widge1 = widgets.Checkbox(value=False,
                                         description='Assume $Sc_1$ = $Su_1$',
                                         style=style)
    constraint_html1 = widgets.HTML(
        value=
        'Check <a href="https://forcaist.github.io/FORCaist.github.io/dfaq.html#AssumeS1" target="_blank">FORCsensei FAQ</a> for more information about the &nbsp;Sc<sub>1</sub> = Su<sub>1</sub> option'
    )

    pike_widge = widgets.Checkbox(
        value=False,
        description=
        'Assume $Sc_0$ = $Su_0$ = $Sc_1$ = $Su_1$ and $\lambda$ = 0',
        style=style)

    #find number of points in window of interest
    X['Hc'] = 0.5 * (X['H'] - X['Hr'])
    X['Hb'] = 0.5 * (X['H'] + X['Hr'])
    Hidx = np.argwhere(in_window(X, X['Hc'], X['Hb']) == True)
    H0 = X['Hc'][Hidx] + X['Hb'][Hidx]
    Hr0 = X['Hb'][Hidx] - X['Hc'][Hidx]

    down_title = widgets.HTML(value='<h3>Specify downsampling:</h3>')

    Npts = int(np.sum((np.abs(H0) < X['Hopen']) & (np.abs(Hr0) < X['Hopen'])))

    down_widge = widgets.IntSlider(value=np.minimum(Npts, 2000),
                                   min=100,
                                   max=Npts,
                                   step=1,
                                   description='Number of points:',
                                   disabled=False,
                                   continuous_update=False,
                                   orientation='horizontal',
                                   readout=True,
                                   readout_format='d',
                                   style=style)

    #display number of models to compare
    model_widge = widgets.interactive_output(
        variforc_array_size, {
            'SC': Sc_widge,
            'SB': Sb_widge,
            'L': lambda_widge,
            'CN': constraint_widge,
            'CN1': constraint_widge1,
            'PK': pike_widge
        })

    #display FAQ information about progress bar
    progress_html = widgets.HTML(
        value=
        'Model comparison can take some time, check <a href="https://forcaist.github.io/FORCaist.github.io/dfaq.html#progress" target="_blank">FORCsensei FAQ</a> for information about monitoring progress of the calculation'
    )

    #combined widget
    DS = VBox([down_title, down_widge])
    SC = VBox([
        M_title, M_widge, HL, S_title, Sc_widge, Sb_widge, lambda_widge,
        HBox([constraint_widge, constraint_html]),
        HBox([constraint_widge1, constraint_html1]), pike_widge, model_widge,
        progress_html
    ])

    ### Setup Multiprocessing tab ####################
    X['ncore'] = 4

    #header
    dask_title = widgets.HTML(value='<h3>DASK multiprocessing:</h3>')

    #selection widget
    dask_widge = widgets.IntSlider(value=X['ncore'],
                                   min=1,
                                   max=20,
                                   step=1,
                                   description='Number of DASK workers:',
                                   disabled=False,
                                   continuous_update=False,
                                   orientation='horizontal',
                                   readout=True,
                                   readout_format='d',
                                   style=style)

    dask_html = widgets.HTML(
        value=
        'Check <a href="https://forcaist.github.io/FORCaist.github.io/dfaq.html#dask_workers" target="_blank">FORCsensei FAQ</a> for more information about selecting Dask workers'
    )

    #final multiprocessing widget
    mpl_widge = VBox([dask_title, dask_widge, dask_html])

    ### CONSTRUCT TAB MENU #############
    method_nest = widgets.Tab()
    method_nest.children = [SC, DS, mpl_widge]
    method_nest.set_title(0, 'MODEL ENSEMBLE')
    method_nest.set_title(1, 'DOWNSAMPLING')
    method_nest.set_title(2, 'PROCESSING')

    display(method_nest)

    ### SETUP OUTPUT ####
    X['constraint'] = constraint_widge
    X['constraint1'] = constraint_widge1
    X['pike'] = pike_widge
    X['Mtype'] = M_widge
    X['SC'] = Sc_widge
    X['SB'] = Sb_widge
    X['lambda'] = lambda_widge
    X['Ndown'] = down_widge
    X['workers'] = dask_widge

    return X
Esempio n. 14
0
def vis(model, trained_model, pixel_array):

    #if path is provided as model load model
    if type(trained_model) == str:
        model_path = trained_model
        #load model
        trained_model = tf.keras.models.load_model(model_path, compile=False)
    else:
        pass

    #get weights
    loaded_weights = trained_model.get_weights()
    #new_model.summary()
    model.set_weights(loaded_weights)

    #transform pixel array from (depth,length,width) to (1,depth,length,width,1)
    if len(np.shape(pixel_array)) == 3:
        pixel_array = np.expand_dims(pixel_array, 3)
        pixel_array = np.expand_dims(pixel_array, 0)

    #call the interpretability methods
    print("Calculating the saliency maps. This may take several minutes.")
    capi_vsali = methods.call_vsaliency(model, model_modifier, loss,
                                        pixel_array)
    capi_sali = methods.call_smooth(model, model_modifier, loss, pixel_array)
    capi_grad = methods.call_grad(model, model_modifier, loss, pixel_array)
    capi_gradplus = methods.call_gradplus(model, model_modifier, loss,
                                          pixel_array)
    capi_faster_scorecam = methods.call_faster_scorecam(
        model, model_modifier, loss, pixel_array)

    #clear print statement
    clear_output(wait=True)

    #define the widgets
    layer = widgets.IntSlider(description='Slice:',
                              min=0,
                              max=(np.shape(pixel_array)[1] - 1),
                              orientation='horizontal')

    method = widgets.ToggleButtons(
        options=[
            'Vanilla Saliency', 'SmoothGrad', 'GradCam', 'GradCam++',
            'ScoreCam'
        ],
        description='Method:',
        disabled=False,
        button_style='',  # 'success', 'info', 'warning', 'danger' or ''
        tooltips=[
            'Description of slow', 'Description of regular',
            'Description of fast'
        ],
        #     icons=['check'] * 3
    )

    attention = widgets.ToggleButtons(
        options=['Slice-wise', "Max"],
        description='Attention:',
        disabled=False,
        button_style='',  # 'success', 'info', 'warning', 'danger' or ''
        tooltips=[
            'Description of slow', 'Description of regular',
            'Description of fast'
        ],
        #     icons=['check'] * 3
    )

    alpha = widgets.FloatSlider(
        value=0.5,
        min=0,
        max=1.0,
        step=0.001,
        description='Overlay:',
        disabled=False,
        continuous_update=True,
        orientation='horizontal',
        readout=True,
        readout_format='.1f',
    )

    play = widgets.Play(value=0,
                        min=0,
                        max=(np.shape(pixel_array)[1] - 1),
                        step=1,
                        interval=500,
                        description="Press play",
                        disabled=False)
    widgets.jslink((play, 'value'), (layer, 'value'))

    #assemble the widget
    ui = widgets.VBox([attention, method, layer, alpha, play])

    #create the overlay of original image and heatmap
    def explore_3dimage(layer, attention, method, alpha):

        #define the visualize
        if attention == "Slice-wise":
            attention_mode = 'slice'
        # elif attention == "Average":
        #    attention_mode = 'mean'
        elif attention == "Max":
            attention_mode = 'max'

        #load interpretability method
        if method == 'Vanilla Saliency':
            heatmap = mode(attention_mode, capi_vsali)
            capi = capi_vsali
        elif method == 'SmoothGrad':
            heatmap = mode(attention_mode, capi_sali)
            capi = capi_sali
        elif method == 'GradCam':
            heatmap = mode(attention_mode, capi_grad)
            capi = capi_grad
        elif method == 'GradCam++':
            heatmap = mode(attention_mode, capi_gradplus)
            capi = capi_gradplus
        elif method == 'ScoreCam':
            heatmap = mode(attention_mode, capi_faster_scorecam)
            capi = capi_faster_scorecam

        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))

        gs = gridspec.GridSpec(1, 2, width_ratios=[2, 1.5])
        ax1 = plt.subplot(gs[0])
        ax2 = plt.subplot(gs[1])

        mri_image_slice = np.squeeze(pixel_array)[layer, :, :]
        mri_image_slice = np.float64(mri_image_slice)

        #max methods:
        if len(np.shape(heatmap)) == 2:
            heatmap_slice = heatmap

        #slice methods
        elif len(np.shape(heatmap)) == 3:
            heatmap_slice = np.squeeze(heatmap)[layer, :, :]
            heatmap_slice = np.float64(heatmap_slice)

        ax1.imshow(mri_image_slice)
        ax1.imshow(heatmap_slice, cmap='jet', alpha=alpha)
        ax1.set_title(method + " - " + attention, fontsize=18)
        ax1.axis('off')
        ax2.set_xlabel('Slice', fontsize=13)
        ax2.set_ylabel('Pixel intensities', fontsize=13)
        ax2.set_title("Attention histogram", fontsize=18)

        # calculate GAP in z-direction
        capi = np.squeeze(capi)
        capi_gap = np.apply_over_axes(np.mean, capi, [1, 2])

        # normalize
        capi_gap_norm = (capi_gap - min(capi_gap)) / (max(capi_gap) -
                                                      min(capi_gap))
        max_slice = np.argmax(capi_gap, axis=0)
        ax2.plot(np.squeeze(capi_gap))
        plt.vlines(x=max_slice, ymin=0, ymax=max(capi_gap), linestyle="--")
        plt.text(max_slice + 0.5, 0.1 * max(capi_gap),
                 "max slice: \n" + str(max_slice[0][0]))
        plt.vlines(x=layer,
                   ymin=0,
                   ymax=np.squeeze(capi_gap)[layer],
                   linestyle="dotted",
                   color="b")
        plt.text(layer + 0.5, 0.03 * max(capi_gap),
                 "current slice: \n" + str(layer))
        plt.grid(axis=('both'), linestyle="--")
        xt = ax2.get_xticks()
        plt.ylim(0)
        ax2.set_xticks(xt)
        ax2.set_xticklabels(xt)
        plt.xlim(left=0, right=(np.shape(pixel_array)[1] - 1))
        fig.subplots_adjust(wspace=0.5)

        return layer

    #interactive output
    out = widgets.interactive_output(explore_3dimage, {
        'attention': attention,
        'method': method,
        'layer': layer,
        'alpha': alpha
    })
    #ensure smoother sliding through the layers
    out.layout.height = '550px'
    display(ui, out)
def column_bivariate_eda_interact(
    data: pd.DataFrame,
    column1: str,
    column2: str,
    summary_type: str = None,
    auto_update: bool = True,
    data_dict: str = None,
    notes_file: str = None,
    figure_dir: str = None,
    savefig_button: Optional[widgets.Button] = None,
) -> None:
    data = data.copy()

    data[column1] = coerce_column_type(data[column1], summary_type)
    data[column2] = coerce_column_type(data[column2], summary_type)

    color_palette_widget = widgets.Text(**WIDGET_PARAMS["color_palette"])
    if summary_type == "numeric-numeric":
        lower_trim1_widget = widgets.BoundedIntText(
            **WIDGET_PARAMS["lower_trim1"])
        lower_trim1_widget.max = data.shape[0] - 1
        upper_trim1_widget = widgets.BoundedIntText(
            **WIDGET_PARAMS["upper_trim1"])
        upper_trim1_widget.max = data.shape[0] - 1
        lower_trim2_widget = widgets.BoundedIntText(
            **WIDGET_PARAMS["lower_trim2"])
        lower_trim2_widget.max = data.shape[0] - 1
        upper_trim2_widget = widgets.BoundedIntText(
            **WIDGET_PARAMS["upper_trim2"])
        upper_trim2_widget.max = data.shape[0] - 1
        fig_height_widget = widgets.IntSlider(**WIDGET_PARAMS["fig_height"])
        fig_width_widget = widgets.IntSlider(**WIDGET_PARAMS["fig_width"])
        fig_width_widget.value = fig_height_widget.value
        widget = widgets.interactive(
            numeric_numeric_bivariate_summary,
            {"manual": not auto_update},
            data=widgets.fixed(data),
            column1=widgets.fixed(column1),
            column2=widgets.fixed(column2),
            fig_height=fig_height_widget,
            fig_width=fig_width_widget,
            fontsize=widgets.FloatSlider(**WIDGET_PARAMS["fontsize"]),
            color_palette=color_palette_widget,
            trend_line=widgets.Dropdown(**WIDGET_PARAMS["trend_line"]),
            alpha=widgets.FloatSlider(**WIDGET_PARAMS["alpha"]),
            lower_trim1=lower_trim1_widget,
            upper_trim1=upper_trim1_widget,
            lower_trim2=lower_trim2_widget,
            upper_trim2=upper_trim2_widget,
            transform1=widgets.Dropdown(**WIDGET_PARAMS["transform1"]),
            transform2=widgets.Dropdown(**WIDGET_PARAMS["transform2"]),
            clip=widgets.BoundedFloatText(**WIDGET_PARAMS["clip"]),
            reference_line=widgets.Checkbox(**WIDGET_PARAMS["reference_line"]),
            plot_density=widgets.Checkbox(**WIDGET_PARAMS["plot_kde"]),
            interactive=widgets.fixed(True),
        )
    else:
        print("No EDA support for this variable type")
        return

    if data_dict is not None:
        print(
            f"Column1 Description: {data_dict[column1] if column1 in data_dict else 'N/A'}\n"
            f"Column2 Description: {data_dict[column2] if column2 in data_dict else 'N/A'}\n"
        )

    print("=====================")
    print("General Plot Controls")
    print("=====================")
    general_controls = widgets.HBox(
        widget.children[:4], layout=widgets.Layout(flex_flow="row wrap"))
    display(general_controls)

    print("=========================")
    print("Summary Specific Controls")
    print("=========================")
    widget.update()

    controls = widgets.HBox(widget.children[4:-1],
                            layout=widgets.Layout(flex_flow="row wrap"))
    display(controls)

    print("==============")
    print("Summary Output")
    print("==============")
    output = widget.children[-1]
    display(output)

    # Handle EDA notes and summary configuration
    if notes_file is not None:
        info_button = widgets.Button(
            description="Save Notes",
            disabled=False,
            button_style="info",
            icon="save",
            layout=widgets.widgets.Layout(width="15%", height="50px"),
            tooltip=f"Save EDA notes to {notes_file}",
        )
        if not os.path.exists(notes_file):
            notes = {f"{column1}-{column2}": ""}
        else:
            with open(notes_file, "r") as fid:
                notes = json.load(fid)
            if f"{column1}-{column2}" not in notes:
                notes[f"{column1}-{column2}"] = ""

        notes_entry = widgets.Textarea(
            value=notes[f"{column1}-{column2}"],
            placeholder="Take EDA Notes Here",
            description="EDA Notes:",
            layout=dict(width="80%", height="auto"),
            disabled=False,
        )
        info_button.description = "Save Notes"

        def savenotes_on_click(x):
            if notes_file is not None:
                if not os.path.exists(notes_file):
                    notes = {}
                else:
                    with open(notes_file, "r") as fid:
                        notes = json.load(fid)
                notes[f"{column1}-{column2}"] = notes_entry.value
                with open(notes_file, "w") as fid:
                    json.dump(notes, fid)

        info_button.on_click(savenotes_on_click)

        display(
            widgets.HBox(
                [notes_entry, info_button],
                layout=widgets.widgets.Layout(height="200px"),
            ),
            layout=widgets.Layout(flex_flow="row wrap"),
        )

    # Add callback to save current figure if figure_dir is specified
    if figure_dir is not None:

        def savefig_on_click(x):
            widget.result[1].savefig(f"{figure_dir}/{column1}-{column2}.png")

        savefig_button.on_click(savefig_on_click)
Esempio n. 16
0
    def plot(
        self,
        input_image: Image or List[Image] = None,
        resolve_kwargs: dict = None,
        interval: float = None,
        **kwargs
    ):
        """Visualizes the output of the feature.

        Resolves the feature and visualizes the result. If the output is an Image,
        show it using `pyplot.imshow`. If the output is a list, create an `Animation`.
        For notebooks, the animation is played inline using `to_jshtml()`. For scripts,
        the animation is played using the matplotlib backend.

        Any parameters in kwargs will be passed to `pyplot.imshow`.

        Parameters
        ----------
        input_image : Image or List[Image], optional
            Passed as argument to `resolve` call
        resolve_kwargs : dict, optional
            Passed as kwarg arguments to `resolve` call
        interval : float
            The time between frames in animation in ms. Default 33.
        kwargs
            keyword arguments passed to the method pyplot.imshow()
        """

        import matplotlib.animation as animation
        import matplotlib.pyplot as plt
        from IPython.display import HTML, display

        if input_image is not None:
            input_image = [Image(input_image)]

        output_image = self.resolve(input_image, **(resolve_kwargs or {}))

        # If a list, assume video
        if isinstance(output_image, Image):
            # Single image
            plt.imshow(output_image[:, :, 0], **kwargs)
            plt.show()

        else:
            # Assume video
            fig = plt.figure()
            images = []
            plt.axis("off")
            for image in output_image:
                images.append([plt.imshow(image[:, :, 0], **kwargs)])

            interval = (
                interval or output_image[0].get_property("interval") or (1 / 30 * 1000)
            )

            anim = animation.ArtistAnimation(
                fig, images, interval=interval, blit=True, repeat_delay=0
            )

            try:
                get_ipython  # Throws NameError if not in Notebook
                display(HTML(anim.to_jshtml()))
                return anim

            except NameError:
                # Not in an notebook
                plt.show()

            except RuntimeError:
                # In notebook, but animation failed
                import ipywidgets as widgets

                Warning(
                    "Javascript animation failed. This is a non-performant fallback."
                )

                def plotter(frame=0):
                    plt.imshow(output_image[frame][:, :, 0], **kwargs)
                    plt.show()

                return widgets.interact(
                    plotter,
                    frame=widgets.IntSlider(
                        value=0, min=0, max=len(images) - 1, step=1
                    ),
                )
Esempio n. 17
0
def catmaid(predictions, testslide_subfolders, fig_size, fig_name):

    #initialization
    #     slides = getSlideList(testslide_subfolders, training_dir, 0)[0]
    #     x_set = slides.slideXYList
    #     x_set_max = slides.n_tiles
    slide_fm = np.zeros(((EM_tile_size * 6), (EM_tile_size * 6)))

    #figure (FM prediction)
    slide_test = predictions

    #figure (FM)
    for i in range(2):
        for j in range(2):
            tile_name = "{}_{}_{}.png".format(i, j, 5)
            filename = training_dir + testslide_subfolders[0]["FM"] + tile_name
            im_tile = cv2.imread(filename)
            if im_tile is None:
                im_tile = np.ones((1024, 1024)) * 255
            else:
                im_tile = im_tile[:, :,
                                  1]  # use the first channel (second and third are the same)
            slide_fm[1024 * i:1024 * (i + 1),
                     1024 * j:(j + 1) * 1024] = im_tile / 255

    #get EM slides for different zoom levels
    em_slides = []
    zoom_lvl = [5, 4, 3, 2, 1, 0]
    for k in range(len(zoom_lvl)):
        em_slides.extend(
            getSlideList(testslide_subfolders, training_dir, zoom_lvl[k]))

    #adjust fm images to transparent images
    slide_test_grey = rescale_intensity(slide_test, out_range=np.uint8)
    slide_test_rgb = grey2rgb(np.array(slide_test_grey, dtype=np.uint8),
                              alpha=True)
    slide_fm_grey = rescale_intensity(slide_fm, out_range=np.uint8)
    slide_fm_rgb = grey2rgb(np.array(slide_fm_grey, dtype=np.uint8),
                            alpha=True)

    #set color
    slide_test_rgb[:, :, 0] = 0
    slide_test_rgb[:, :, 2] = 0
    slide_fm_rgb[:, :, 1] = 0

    #set image pixel size
    cut_on = 0
    cut_off = 1024, 1024

    def transparent(trans_pred, trans_real, trans_par1, trans_par2, zoom,
                    x_slid, y_slid, save_img):
        #invert transparency
        tran_1 = 255 - trans_pred / 100 * 255
        trans_par1[:, :, 3] = tran_1
        tran_2 = 255 - trans_real / 100 * 255
        trans_par2[:, :, 3] = tran_2

        #create em image
        coords = cut_off[0] * x_slid / 100 * 2**zoom, cut_off[
            1] * y_slid / 100 * 2**zoom
        tile_xy = int(coords[0] / EM_tile_size), int(coords[1] / EM_tile_size)
        coords_em = int(coords[0] -
                        (EM_tile_size * tile_xy[0])), int(coords[1] -
                                                          (EM_tile_size *
                                                           tile_xy[1]))
        coords_fm = coords[0] / (2**zoom), coords[1] / (2**zoom)
        EM_case = True
        slide_em_grey = em_slides[zoom].createPatch(EM_tile_size, tile_xy,
                                                    cut_off, cut_on, coords_em,
                                                    EM_case)

        #create fm images
        trans_par2 = trans_par2[int(coords_fm[0]):int(coords_fm[0] +
                                                      cut_off[0] / (2**zoom)),
                                int(coords_fm[1]):int(coords_fm[1] +
                                                      cut_off[1] / (2**zoom))]
        trans_par1 = trans_par1[int(coords_fm[0]):int(coords_fm[0] +
                                                      cut_off[0] / (2**zoom)),
                                int(coords_fm[1]):int(coords_fm[1] +
                                                      cut_off[1] / (2**zoom))]
        real_fm = np.repeat(np.repeat(trans_par2, 2**zoom, axis=1),
                            2**zoom,
                            axis=0)
        pred_fm = np.repeat(np.repeat(trans_par1, 2**zoom, axis=1),
                            2**zoom,
                            axis=0)

        #calculate things
        #accuracy:

        #pearson:
        def calc_pearson(y_true2, y_pred2):
            #doesnt work yet, and will prob not due to differences in length/height
            #y_true = rescale_intensity(y_true, out_range=np.uint8)
            #y_pred = rescale_intensity(y_pred, out_range=np.uint8)
            y_true = y_true2[:, :, :3]
            y_pred = y_pred2[:, :, :3]
            y_true_mean = y_true - np.mean(y_true)
            y_pred_mean = y_pred - np.mean(y_pred)
            return np.sum(y_true_mean * y_pred_mean) / np.sqrt(
                np.sum((y_true_mean)**2) * np.sum((y_pred_mean)**2))

        #pearson_coeff = calc_pearson(real_fm, pred_fm)
        #print(pearson_coeff)

        #plot
        fig = plt.figure(figsize=(fig_size, fig_size))
        plt.imshow(slide_em_grey, cmap='Greys')
        plt.imshow(pred_fm)
        plt.imshow(real_fm)
        plt.axis('off')
        if save_img == True:
            filename = os.path.join(
                "figures", fig_name + '_' + str(int(trans_pred)) + 'Tpred_' +
                str(int(trans_real)) + 'Treal_' + str(int(zoom)) + 'Zoom_' +
                str(int(y_slid)) + '-' + str(int(x_slid)) + 'Pos.png')
            fig.savefig(filename)
            #imsave(filename, tst)

    #widget info
    button = wg.Button(description="Save")
    button_output = wg.Output()

    slider1 = wg.IntSlider(min=0, max=100, step=1, value=100)
    slider2 = wg.IntSlider(min=0, max=100, step=1, value=100)
    slider3 = wg.IntSlider(min=0, max=5, step=1, value=0)

    slider4 = wg.IntSlider(min=0,
                           max=120,
                           step=1,
                           value=0,
                           orientation='horizontal',
                           readout=True)
    slider5 = wg.IntSlider(min=0, max=120, step=1, value=0, readout=True)

    #     slider6 = wg.IntSlider(min=0, max=100, step=1, value=100)
    #     slider7 = wg.IntSlider(min=0, max=100, step=1, value=100)
    #     slider8 = wg.IntSlider(min=0, max=100, step=1, value=100)

    slider1.style.handle_color = 'lightblue'
    slider2.style.handle_color = 'purple'

    slid_1_text = wg.HBox(
        [slider1, wg.Label('Transparency of FM (predicted)')])
    slid_2_text = wg.HBox([slider2, wg.Label('Transparency of FM (real)')])
    slid_3_text = wg.HBox([slider3, wg.Label('Zoom')])
    slid_4_text = wg.HBox([slider4, wg.Label('Position (y)')])
    slid_5_text = wg.HBox([slider5, wg.Label('Position (x)')])

    slids_out = wg.VBox(
        [slid_1_text, slid_2_text, slid_3_text, slid_5_text, slid_4_text])

    func_out = wg.interactive_output(
        transparent, {
            'trans_pred': slider1,
            'trans_real': slider2,
            'trans_par1': wg.fixed(slide_test_rgb),
            'trans_par2': wg.fixed(slide_fm_rgb),
            'zoom': slider3,
            'x_slid': slider4,
            'y_slid': slider5,
            'save_img': wg.fixed(False)
        })

    def test(b):
        transparent(slider1.value, slider2.value, slide_test_rgb, slide_fm_rgb,
                    slider3.value, slider4.value, slider5.value, True)
        filename = os.path.join(
            "figures", fig_name + '_' + str(int(slider1.value)) + 'Tpred_' +
            str(int(slider2.value)) + 'Treal_' + str(int(slider3.value)) +
            'Zoom_' + str(int(slider5.value)) + '-' + str(int(slider4.value)) +
            'Pos.png')
        with button_output:
            print("Image saved as: ", filename)

    button.on_click(test)

    display(button, button_output, slids_out, func_out)
Esempio n. 18
0
    def __init__(self, zeros=1, poles=1, show_phase=None, show_dB=False, real_filter=True):
        self.out = Output(layout={'width': '980px', 'height': '450px'})
        self.axs = []
        #I change this because it is going to be the discrete zero pole plot demo
        self.discrete_mode = False#not continuous
        self.show_phase = True #not self.discrete_mode if show_phase is None else show_phase
        self.actual_change = True
        self.real_filter=real_filter
        self.show_dB = show_dB
        # Initialize zeros and poles
        z_x = []
        z_y = []
        p_x = []
        p_y = []

        for i in range(zeros):
            z_x.append(0.5)
            z_y.append(0)

        for i in range(poles):
            p_x.append(-0.5)
            p_y.append(0)

        self.collapsed_points = []

        # Initialize the figure with the initial number of zeros and poles
        self.init_figure(z_x, z_y, p_x, p_y)
        # Call the super class with the zero pole axis to enable the draggable markers
        super().__init__(ax=self.axs[0], lines=self.axs[0].lines[1:], update_func=self.change_freq_res, update_conj=self.update_conjugate)


        # Non causal text field
        self.tx_deb = self.axs[0].text(-1.75, 1.5, '', fontdict={'color': 'red', 'size': 12})
    
        # Debug text field
        self.tx_debug = self.axs[0].text(-1.75, 1, '', fontdict={'color': 'red', 'size': 15, 'font':'Arial'})

        # 'Calculation not possible' text fields
        self.cnp_gain = None
        self.cnp_ph = None

        # Text field numbers
        self.lastzeroRe = 0
        self.lastzeroIm = 0

        # Init frequency response plot
        self.change_freq_res(init=True)

        # Widgets
        # Zeros
        self.zero_range = widgets.IntSlider(value=zeros, min=0, max=zeros+10, step=1, description='Zeros:', continuous_update=False)

        self.zero_range.observe(self.on_zero_change, names='value')
        # Poles
        self.pole_range = widgets.IntSlider(value=poles, min=0, max=poles+10, step=1, description='Poles:', continuous_update=False)

        self.pole_range.observe(self.on_pole_change, names='value')

        # Check box to show phase plot
        self.phase_check = widgets.Checkbox(value=self.show_phase, description='Show phase')
        self.phase_check.observe(self.show_phase_callback, names='value')

        # Check box to show gain in dB
        self.dB_check = widgets.Checkbox(value=self.show_dB, description='dB')
        self.dB_check.observe(self.show_dB_callback, names='value')

        # Button to switch between continuous and discrete mode
        #self.mode_button = widgets.Button(description='Changer au cas continue' if self.discrete_mode else 'Changer au cas discret',
        #                                  layout=Layout(width='20%'))
        #self.mode_button.on_click(self.mode_button_callback)

        # Button to change to real filter
        self.real_button=widgets.Checkbox(value=self.real_filter, description = "Real filter", layout=Layout(width='50%'))
        
        self.real_button.observe(self.real_filter_callback, names='value')

        # Float text widgets
        self.input_Zero_RE = widgets.FloatText(value=self.lastzeroRe, description='Re:')
        self.input_Zero_RE.observe(self.Zero_RE_Caller, names='value')

        self.input_Zero_IM = widgets.FloatText(value=self.lastzeroIm, description='Im:')
        self.input_Zero_IM.observe(self.Zero_IM_Caller, names='value')


        self.collapsing = False
        # Display widgets and plot
        display(VBox([self.out,
                      HBox([self.zero_range, self.pole_range, self.phase_check]),
                      HBox([self.input_Zero_RE, self.input_Zero_IM, self.dB_check, self.real_button])]))
        plt.tight_layout(pad=0.4, w_pad=1.5, h_pad=1.0)
        
        if self.real_filter:
            self.change_conjugate()
import numpy as np
from scipy.integrate import odeint
from matplotlib import pyplot as plt

from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
from IPython.display import display

growth = widgets.FloatSlider(min=0., max=10.)
carrying = widgets.IntSlider(min=10., max=1000.)
print("Growth rate:")
display(growth)
print("Carrying capacity:")
display(carrying)

K = 100.  #carrying capacity
r = growth.value


def exponential(C, t):
    dCdt = C * r
    return dCdt


def logistic(C, t):
    dCdt = C * r * (1 - C / K)
    return dCdt


times = np.linspace(0., 2., 100)
exp_Cs = odeint(func=exponential, y0=[1.], t=times)
Esempio n. 20
0
def demo_rk2():
    """
	Create widgets, set layout, and use as inputs to run the main code
	"""

    steps = widgets.IntSlider(
        value=3,
        description='Steps',
        min=0,
        max=50,
        continuous_update=False,
    )

    h = widgets.FloatSlider(
        value=0.5,
        description='Step Size',
        min=0.1,
        max=1,
        step=0.1,
    )

    y0 = widgets.FloatSlider(
        value=0.,
        description='y(0)',
        min=-1.,
        max=1.,
        step=0.1,
    )

    eqn = widgets.Dropdown(
        options=["Function1", "Function2", "Function3", "Function4"],
        description='Derivative Function')

    rk2 = widgets.FloatSlider(value=0.5,
                              min=0.1,
                              max=1.0,
                              step=0.1,
                              description='Beta for RK2',
                              continuous_update=True)

    zoom = widgets.Checkbox(value=True, description='Zoom In')

    deriv = widgets.Checkbox(value=False, description='Show Derivative Field')

    evals = widgets.Checkbox(value=True, description='Show Next Step')

    # create list of controls to pass to function as inputs
    controls = {
        'h': h,
        'steps': steps,
        'y0': y0,
        'neval': fixed(2),
        'method': fixed("RK2"),
        'eqn': eqn,
        'zoom': zoom,
        'show_deriv': deriv,
        'show_grad': evals,
        'rk2': rk2
    }

    # set up the layout for UI interface
    col1 = [h, steps, y0]
    col2 = [eqn, rk2]
    col3 = [zoom, evals, deriv]
    layout = HBox([VBox(col1), VBox(col2), VBox(col3)])

    return VBox([layout, interactive_output(run_ode_methods, controls)])
Esempio n. 21
0
import ipywidgets
from IPython.display import display

wrapper_proto_log = ProtoLog(
    "test_data/SensorFusion_SSL_WrapperPacket", SSL_WrapperPacket,
)


# +
from bokeh.plotting import figure
from bokeh.io import output_notebook, show, push_notebook
from python_tools.plotting.plot_ssl_wrapper import SSLWrapperPlotter

output_notebook()

fig = figure(plot_width=900, plot_height=900, match_aspect=True)

ssl_wrapper_plotter = SSLWrapperPlotter(fig)


def plot_ssl_wrapper_at_idx(idx):
    ssl_wrapper_plotter.plot_ssl_wrapper(wrapper_proto_log[idx])
    push_notebook()


show(fig, notebook_handle=True)

slider = ipywidgets.IntSlider(min=0, max=len(wrapper_proto_log) - 1)
ipywidgets.interact(plot_ssl_wrapper_at_idx, idx=slider)
# -
Esempio n. 22
0
def demo_methods():
    """
	Create widgets, set layout, and use as inputs to run the main code
	"""

    steps = widgets.IntSlider(
        value=3,
        description='Steps',
        min=0,
        max=50,
        continuous_update=False,
    )

    h = widgets.FloatSlider(
        value=0.5,
        description='Step Size',
        min=0.1,
        max=1,
        step=0.1,
    )

    y0 = widgets.FloatSlider(
        value=0.,
        description='y(0)',
        min=-1.,
        max=1.,
        step=0.1,
    )

    method = widgets.Dropdown(options=['Euler', 'IEuler', 'ClassicRK4'],
                              value='ClassicRK4',
                              description='Solution Method')

    eqn = widgets.Dropdown(
        options=["Function1", "Function2", "Function3", "Function4"],
        description='Derivative Function')

    zoom = widgets.Checkbox(value=True, description='Zoom In')

    deriv = widgets.Checkbox(value=False, description='Show Derivative Field')

    evals = widgets.Checkbox(value=True, description='Show Next Step')

    neval = widgets.IntSlider(value=4,
                              description='Number of Derivative Evaluations',
                              min=0,
                              max=4,
                              continuous_update=False)

    # Update value of number of evaluations widget based on method widget
    def change_method(*args):
        if method.value == 'Euler':
            neval.min = 0
            neval.max = 0
            neval.value = 0
        elif method.value == 'IEuler':
            neval.min = 0
            neval.max = 2
            neval.value = 0
        elif method.value == 'ClassicRK4':
            neval.min = 0
            neval.max = 4
            neval.value = 0

    method.observe(change_method)

    # create list of controls to pass to function as inputs
    controls = {
        'h': h,
        'steps': steps,
        'y0': y0,
        'neval': neval,
        'method': method,
        'eqn': eqn,
        'zoom': zoom,
        'show_deriv': deriv,
        'show_grad': evals,
        'rk2': fixed(1.)
    }

    # set up the layout for UI interface
    col1 = [h, steps, y0]
    col2 = [eqn, method, neval]
    col3 = [zoom, evals, deriv]
    layout = HBox([VBox(col1), VBox(col2), VBox(col3)])

    return VBox([layout, interactive_output(run_ode_methods, controls)])
Esempio n. 23
0
def get_para_widgets():
    para_selector = widgets.IntSlider(
        min=2, max=60, description="On Balance volume")
    para_selector_widgets = [para_selector]
    return para_selector_widgets
Esempio n. 24
0
def all_widgets():

    to_return = []
    m1 = Model()

    to_return.append(
        widgets.Label("Please enter the url and foldername for the dataset"))
    url = widgets.Text()
    to_return.append(url)

    foldertitle = widgets.Text()
    to_return.append(foldertitle)

    dl_button = widgets.Button(description="Download Images")
    to_return.append(dl_button)

    dl_output = widgets.Output()
    to_return.append(dl_output)

    folder_out = widgets.Label()
    to_return.append(folder_out)

    def dl_on_button_click(b):
        with dl_output:
            clear_output()
            m1.img_cat, m1.data_loc = CNNModules.file_setup(
                url.value, foldertitle.value)
            folder_out.value = "All those images have been downloaded to this location: " + str(
                m1.data_loc)
            dp_button.disabled = False

    dl_button.on_click(dl_on_button_click)

    to_return.append(
        widgets.Label(
            'Slide the bar to adjust the size we will reduce the image to'))
    imgsize = widgets.IntSlider(min=1, max=100, value=50)
    to_return.append(imgsize)

    dp_button = widgets.Button(description="Prep Data", disabled=True)
    dp_output = widgets.Output()
    to_return.append(dp_button)
    to_return.append(dp_output)

    def dp_on_button_click(b):
        with dp_output:
            clear_output()
            training_data = CNNModules.data_preprocess(m1.data_loc,
                                                       imgsize.value,
                                                       m1.img_cat)
            m1.X, m1.y = CNNModules.restructure_data(training_data)
            ri_button.disabled = False
            tm_button.disabled = False

    dp_button.on_click(dp_on_button_click)

    to_return.append(
        widgets.Label(
            "Press the button to check out what a random image looks like now")
    )
    ri_button = widgets.Button(description="Check it out", disabled=True)
    ri_output = widgets.Output()
    matrix_rep = widgets.Label()

    def ri_on_button_click(b):
        with ri_output:
            clear_output()
            randint = random.randint(0, len(m1.X) - 1)
            CNNModules.display_image(m1.X[randint])
            matrix_rep.value = m1.X[randint]

    ri_button.on_click(ri_on_button_click)

    to_return.append(ri_button)
    to_return.append(ri_output)
    to_return.append(matrix_rep)

    to_return.append(
        widgets.Label(
            "Adjust the following variables and click the button below to train your model on the given dataset"
        ))

    to_return.append(widgets.Label("Number of Hidden Layers"))
    numlayers = widgets.widgets.BoundedIntText(value=1,
                                               min=0,
                                               max=4,
                                               step=1,
                                               disabled=False)
    to_return.append(numlayers)

    to_return.append(widgets.Label("Number of Nodes per Layer"))
    numnodes = widgets.widgets.BoundedIntText(value=64,
                                              min=1,
                                              max=100,
                                              step=1,
                                              disabled=False)
    to_return.append(numnodes)

    to_return.append(widgets.Label("Batch Size"))
    batchsize = widgets.widgets.BoundedIntText(value=32,
                                               min=1,
                                               max=200,
                                               step=1,
                                               disabled=False)
    to_return.append(batchsize)

    to_return.append(widgets.Label("Epochs"))
    epochs = widgets.widgets.BoundedIntText(value=3,
                                            min=1,
                                            max=10,
                                            step=1,
                                            disabled=False)
    to_return.append(epochs)

    tm_button = widgets.Button(description="Train the model", disabled=True)
    to_return.append(tm_button)

    tm_output = widgets.Output()
    to_return.append(tm_output)

    def tm_on_button_clicked(b):
        with tm_output:
            clear_output()
            model, model_metrics = CNNModules.build_model(
                m1.X, m1.y, numlayers.value, numnodes.value, len(m1.img_cat),
                batchsize.value, epochs.value)
            m1.model = model

    tm_button.on_click(tm_on_button_clicked)

    return to_return
Esempio n. 25
0
def timelapse(m=None):
    """Creates timelapse animations.

    Args:
        m (geemap.Map, optional): A geemap Map instance. Defaults to None.

    Returns:
        ipywidgets: The interative GUI.
    """
    if m is not None:
        m.add_basemap("HYBRID")

    widget_width = "350px"
    padding = "0px 0px 0px 5px"  # upper, right, bottom, left
    style = {"description_width": "initial"}

    toolbar_button = widgets.ToggleButton(
        value=False,
        tooltip="Toolbar",
        icon="gear",
        layout=widgets.Layout(width="28px", height="28px", padding="0px 0px 0px 4px"),
    )

    close_button = widgets.ToggleButton(
        value=False,
        tooltip="Close the tool",
        icon="times",
        button_style="primary",
        layout=widgets.Layout(height="28px", width="28px", padding="0px 0px 0px 4px"),
    )

    collection = widgets.Dropdown(
        options=[
            "Landsat TM-ETM-OLI Surface Reflectance",
            "Sentinel-2AB Surface Reflectance",
            "MODIS",
        ],
        value="Landsat TM-ETM-OLI Surface Reflectance",
        description="Collection:",
        layout=widgets.Layout(width=widget_width, padding=padding),
        style=style,
    )

    title = widgets.Text(
        value="Timelapse",
        description="Title:",
        style=style,
        layout=widgets.Layout(width="181px", padding=padding),
    )

    bands = widgets.Dropdown(
        description="RGB:",
        options=[
            "Red/Green/Blue",
            "NIR/Red/Green",
            "SWIR2/SWIR1/NIR",
            "NIR/SWIR1/Red",
            "SWIR2/NIR/Red",
            "SWIR2/SWIR1/Red",
            "SWIR1/NIR/Blue",
            "NIR/SWIR1/Blue",
            "SWIR2/NIR/Green",
            "SWIR1/NIR/Red",
        ],
        value="NIR/Red/Green",
        style=style,
        layout=widgets.Layout(width="165px", padding=padding),
    )

    speed = widgets.IntSlider(
        description="Frames/sec:",
        tooltip="Frames per second",
        value=10,
        min=1,
        max=30,
        readout=False,
        style=style,
        layout=widgets.Layout(width="142px", padding=padding),
    )

    speed_label = widgets.Label(
        layout=widgets.Layout(width="20px", padding=padding),
    )
    widgets.jslink((speed, "value"), (speed_label, "value"))

    cloud = widgets.Checkbox(
        value=True,
        description="Apply fmask (remove clouds, shadows, snow)",
        tooltip="Apply fmask (remove clouds, shadows, snow)",
        style=style,
    )

    start_year = widgets.IntSlider(
        description="Start Year:",
        value=1984,
        min=1984,
        max=2020,
        readout=False,
        style=style,
        layout=widgets.Layout(width="138px", padding=padding),
    )

    start_year_label = widgets.Label()
    widgets.jslink((start_year, "value"), (start_year_label, "value"))

    end_year = widgets.IntSlider(
        description="End Year:",
        value=2020,
        min=1984,
        max=2020,
        readout=False,
        style=style,
        layout=widgets.Layout(width="138px", padding=padding),
    )
    end_year_label = widgets.Label()
    widgets.jslink((end_year, "value"), (end_year_label, "value"))

    start_month = widgets.IntSlider(
        description="Start Month:",
        value=5,
        min=1,
        max=12,
        readout=False,
        style=style,
        layout=widgets.Layout(width="145px", padding=padding),
    )

    start_month_label = widgets.Label(
        layout=widgets.Layout(width="20px", padding=padding),
    )
    widgets.jslink((start_month, "value"), (start_month_label, "value"))

    end_month = widgets.IntSlider(
        description="End Month:",
        value=10,
        min=1,
        max=12,
        readout=False,
        style=style,
        layout=widgets.Layout(width="155px", padding=padding),
    )

    end_month_label = widgets.Label()
    widgets.jslink((end_month, "value"), (end_month_label, "value"))

    font_size = widgets.IntSlider(
        description="Font size:",
        value=30,
        min=10,
        max=50,
        readout=False,
        style=style,
        layout=widgets.Layout(width="152px", padding=padding),
    )

    font_size_label = widgets.Label()
    widgets.jslink((font_size, "value"), (font_size_label, "value"))

    font_color = widgets.ColorPicker(
        concise=False,
        description="Font color:",
        value="white",
        style=style,
        layout=widgets.Layout(width="170px", padding=padding),
    )

    progress_bar_color = widgets.ColorPicker(
        concise=False,
        description="Progress bar:",
        value="blue",
        style=style,
        layout=widgets.Layout(width="180px", padding=padding),
    )

    # Normalized Satellite Indices: https://www.usna.edu/Users/oceano/pguth/md_help/html/norm_sat.htm

    nd_options = [
        "Vegetation Index (NDVI)",
        "Water Index (NDWI)",
        "Modified Water Index (MNDWI)",
        "Snow Index (NDSI)",
        "Soil Index (NDSI)",
        "Burn Ratio (NBR)",
        "Customized",
    ]
    nd_indices = widgets.Dropdown(
        options=nd_options,
        value=None,
        description="Normalized Difference Index:",
        style=style,
        layout=widgets.Layout(width="347px", padding=padding),
    )

    first_band = widgets.Dropdown(
        description="1st band:",
        options=["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"],
        value=None,
        style=style,
        layout=widgets.Layout(width="171px", padding=padding),
    )

    second_band = widgets.Dropdown(
        description="2nd band:",
        options=["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"],
        value=None,
        style=style,
        layout=widgets.Layout(width="172px", padding=padding),
    )

    nd_threshold = widgets.FloatSlider(
        value=0,
        min=-1,
        max=1,
        step=0.01,
        description="Threshold:",
        orientation="horizontal",
        readout=False,
        style=style,
        layout=widgets.Layout(width="159px", padding=padding),
    )

    nd_threshold_label = widgets.Label(
        layout=widgets.Layout(width="35px", padding=padding),
    )
    widgets.jslink((nd_threshold, "value"), (nd_threshold_label, "value"))

    nd_color = widgets.ColorPicker(
        concise=False,
        description="Color:",
        value="blue",
        style=style,
        layout=widgets.Layout(width="145px", padding=padding),
    )

    def nd_index_change(change):
        if nd_indices.value == "Vegetation Index (NDVI)":
            first_band.value = "NIR"
            second_band.value = "Red"
        elif nd_indices.value == "Water Index (NDWI)":
            first_band.value = "NIR"
            second_band.value = "SWIR1"
        elif nd_indices.value == "Modified Water Index (MNDWI)":
            first_band.value = "Green"
            second_band.value = "SWIR1"
        elif nd_indices.value == "Snow Index (NDSI)":
            first_band.value = "Green"
            second_band.value = "SWIR1"
        elif nd_indices.value == "Soil Index (NDSI)":
            first_band.value = "SWIR1"
            second_band.value = "NIR"
        elif nd_indices.value == "Burn Ratio (NBR)":
            first_band.value = "NIR"
            second_band.value = "SWIR2"
        elif nd_indices.value == "Customized":
            first_band.value = None
            second_band.value = None

    nd_indices.observe(nd_index_change, names="value")

    create_gif = widgets.Button(
        description="Create timelapse",
        button_style="primary",
        tooltip="Click to create timelapse",
        style=style,
        layout=widgets.Layout(padding=padding),
    )

    temp_output = widgets.Output()

    def submit_clicked(b):

        if start_year.value > end_year.value:
            print("The end year must be great than the start year.")
            return
        if start_month.value > end_month.value:
            print("The end month must be great than the start month.")
            return
        if start_year.value == end_year.value:
            add_progress_bar = False
        else:
            add_progress_bar = True

        start_date = str(start_month.value).zfill(2) + "-01"
        end_date = str(end_month.value).zfill(2) + "-30"

        with output:
            print("Computing... Please wait...")

        nd_bands = None
        if (first_band.value is not None) and (second_band.value is not None):
            nd_bands = [first_band.value, second_band.value]

        if m is not None:

            out_dir = os.path.expanduser("~/Downloads")
            if not os.path.exists(out_dir):
                os.makedirs(out_dir)

            out_gif = os.path.join(out_dir, "timelapse_" + random_string(3) + ".gif")

            with temp_output:
                temp_output.clear_output()
                m.add_landsat_ts_gif(
                    roi=m.user_roi,
                    label=title.value,
                    start_year=start_year.value,
                    end_year=end_year.value,
                    start_date=start_date,
                    end_date=end_date,
                    bands=bands.value.split("/"),
                    font_color=font_color.value,
                    frames_per_second=speed.value,
                    font_size=font_size.value,
                    add_progress_bar=add_progress_bar,
                    progress_bar_color=progress_bar_color.value,
                    out_gif=out_gif,
                    apply_fmask=cloud.value,
                    nd_bands=nd_bands,
                    nd_threshold=nd_threshold.value,
                    nd_palette=["black", nd_color.value],
                )
                if m.user_roi is not None:
                    m.centerObject(m.user_roi)

            with output:
                print("The timelapse has been added to the map.")
                link = create_download_link(
                    out_gif,
                    title="Click here to download: ",
                )
                display(link)

    create_gif.on_click(submit_clicked)

    buttons = widgets.ToggleButtons(
        value=None,
        options=["Reset", "Close"],
        tooltips=["Reset", "Close"],
        button_style="primary",
    )
    buttons.style.button_width = "95px"

    output = widgets.Output(layout=widgets.Layout(width=widget_width, padding=padding))

    toolbar_widget = widgets.VBox()
    toolbar_widget.children = [toolbar_button]
    toolbar_header = widgets.HBox()
    toolbar_header.children = [close_button, toolbar_button]
    toolbar_footer = widgets.VBox()
    toolbar_footer.children = [
        collection,
        widgets.HBox([title, bands]),
        widgets.HBox([speed, speed_label, progress_bar_color]),
        widgets.HBox([start_year, start_year_label, end_year, end_year_label]),
        widgets.HBox([start_month, start_month_label, end_month, end_month_label]),
        widgets.HBox([font_size, font_size_label, font_color]),
        cloud,
        nd_indices,
        widgets.HBox([first_band, second_band]),
        widgets.HBox([nd_threshold, nd_threshold_label, nd_color]),
        widgets.HBox([create_gif, buttons]),
        output,
    ]

    toolbar_event = ipyevents.Event(
        source=toolbar_widget, watched_events=["mouseenter", "mouseleave"]
    )

    def handle_toolbar_event(event):

        if event["type"] == "mouseenter":
            toolbar_widget.children = [toolbar_header, toolbar_footer]
        elif event["type"] == "mouseleave":
            if not toolbar_button.value:
                toolbar_widget.children = [toolbar_button]
                toolbar_button.value = False
                close_button.value = False

    toolbar_event.on_dom_event(handle_toolbar_event)

    def toolbar_btn_click(change):
        if change["new"]:
            close_button.value = False
            toolbar_widget.children = [toolbar_header, toolbar_footer]
        else:
            if not close_button.value:
                toolbar_widget.children = [toolbar_button]

    toolbar_button.observe(toolbar_btn_click, "value")

    def close_btn_click(change):
        if change["new"]:
            toolbar_button.value = False
            if m is not None:
                if m.tool_control is not None and m.tool_control in m.controls:
                    m.remove_control(m.tool_control)
                    m.tool_control = None
                m.toolbar_reset()
            toolbar_widget.close()

    close_button.observe(close_btn_click, "value")

    def button_clicked(change):
        if change["new"] == "Reset":
            with output:
                output.clear_output()
        elif change["new"] == "Close":
            if m is not None:
                m.toolbar_reset()
                if m.tool_control is not None and m.tool_control in m.controls:
                    m.remove_control(m.tool_control)
                    m.tool_control = None
            toolbar_widget.close()

        buttons.value = None

    buttons.observe(button_clicked, "value")

    toolbar_button.value = True
    if m is not None:
        toolbar_control = WidgetControl(widget=toolbar_widget, position="topright")

        if toolbar_control not in m.controls:
            m.add_control(toolbar_control)
            m.tool_control = toolbar_control
    else:
        return toolbar_widget
Esempio n. 26
0
	def widget(self):
		pbox=[]

		# ファイル名とファイル内容
		self.iName_w =widgets.Text(description="トランザクション",value="",layout=Layout(width='99%'),disabled=True)
		self.iText_w =widgets.Textarea(value="",rows=5,layout=Layout(width='99%'),disabled=True)
		pbox.append(self.iName_w)
		pbox.append(self.iText_w)
		self.oPath_w =widgets.Text(description="出力パス",value="",layout=Layout(width='100%'),disabled=False)
		pbox.append(self.oPath_w)
		self.oDir_w =widgets.Text(description="ディレクトリ名",value="",layout=Layout(width='100%'),disabled=False)
		pbox.append(self.oDir_w)

		# traID 項目
		config_t={
			"options":[],
			"title":"トランザクションID",
			"rows":5,
			"width":300,
			"multiSelect":False,
			"message":None
		}
		self.traID_w=selfield_w(config_t)

		# item 項目
		config_i={
			"options":[],
			"title":"アイテム項目",
			"rows":5,
			"width":300,
			"multiSelect":False,
			"message":None
		}
		self.item_w=selfield_w(config_i)

		# class 項目
		config_c={
			"options":[],
			"title":"目的変数項目",
			"rows":5,
			"blank":True,
			"width":300,
			"multiSelect":False,
			"message":None
		}
		self.class_w=selfield_w(config_c)
		pbox.append(widgets.HBox([self.traID_w.widget(),self.item_w.widget(),self.class_w.widget()]))

		# 各種しきい値
		self.type_w=widgets.RadioButtons(options=['F:頻出集合', 'C:飽和集合', 'M:極大集合'],
				value='F:頻出集合', description='type:', disabled=False)
		pbox.append(self.type_w)

		self.minSup_w=widgets.FloatSlider(description='minSup', value=0.01, min=0.0, max=1.0, step=0.01)
		self.maxSup_w=widgets.FloatSlider(description='maxSup', value=1.0, min=0.0, max=1.0, step=0.01)
		pbox.append(widgets.HBox([self.minSup_w,self.maxSup_w]))

		self.minLen_w=widgets.IntSlider(description='minLen', value=1, min=1, max=10, step=1)
		self.maxLen_w=widgets.IntSlider(description='maxLen', value=10, min=1, max=10, step=1)
		pbox.append(widgets.HBox([self.minLen_w,self.maxLen_w]))

		self.minGR_w=widgets.FloatSlider(description='minGR', value=1.2, min=1.1, max=20.0, step=0.2)
		pbox.append(self.minGR_w)

		self.usetop_w=widgets.Checkbox(value=False, description='上位top番目のsupportをminSupに再設定する', disabled=False,style={'description_width': 'initial'})
		self.top_w=widgets.IntSlider(description='topの値', value=1000, min=1, max=10000, step=1)
		pbox.append(widgets.HBox([self.usetop_w,self.top_w]))

		box=widgets.VBox(pbox)
		return box
Esempio n. 27
0
    def __init__(self, ganpreffinder, ganpreffinder2):
        """
        :param ganpreffinder: GANPrefFinder class instance
        """
        self.optimize_Theta = False

        self.GANpf = ganpreffinder  #for user model
        self.GANpf2 = ganpreffinder2  #for no user model

        WIDGET_CONTINIOUS_UPDATE = self.GANpf.USING_CUDA
        WIDGET_CONTINIOUS_UPDATE2 = self.GANpf2.USING_CUDA
        WIDGET_WIDTH = 300

        self.strength = 0
        self.strength2 = 0
        self.query_count = 1
        self.query_count2 = 1
        #self.w = self.GANpf.get_init_W()
        init_img = self.GANpf.get_init_img()

        self.X, self.Xi = self.GANpf.get_next_query()
        self.X2, self.Xi2 = self.GANpf2.get_next_query()

        self.widget_image_init = widgets.Image(format='png',
                                               width=WIDGET_WIDTH,
                                               value=self.to_bytes(init_img))
        '''Panel with user model'''
        self.widget_image = widgets.Image(format='png',
                                          width=WIDGET_WIDTH,
                                          value=self.to_bytes(init_img))
        self.widget_pref_image = widgets.Image(format='png',
                                               width=WIDGET_WIDTH,
                                               value=self.to_bytes(init_img))
        self.widget_strength_slider = widgets.IntSlider(
            min=self.GANpf.left_bound,
            max=self.GANpf.right_bound,
            value=self.strength,
            continuous_update=WIDGET_CONTINIOUS_UPDATE,
            description='strength:')
        self.widget_strength_text = widgets.IntText(
            description="strength:",
            continuous_update=WIDGET_CONTINIOUS_UPDATE)
        self.widget_button_incs = widgets.Button(
            description="slider +10 (both sides)",
            buttin_style="info",
            layout=widgets.Layout(width="150px"))
        self.widget_button_decs = widgets.Button(
            description="slider -10 (both sides)",
            buttin_style="info",
            layout=widgets.Layout(width="150px"))
        self.widget_button_nq = widgets.Button(
            description="next query",
            buttin_style="info",
            layout=widgets.Layout(width="300px"))
        self.queries_count_text = widgets.Label(value=str(self.query_count))
        self.widget_chb_adaptive_init = widgets.Checkbox(
            value=self.GANpf.ADAPTIVE_INITIALIZATION,
            description='DONT USE ACQUISITION FUNCTION',
            disabled=False,
            indent=False)
        '''Panel without user model'''
        self.widget_image2 = widgets.Image(format='png',
                                           width=WIDGET_WIDTH,
                                           value=self.to_bytes(init_img))
        self.widget_pref_image2 = widgets.Image(format='png',
                                                width=WIDGET_WIDTH,
                                                value=self.to_bytes(init_img))
        self.widget_strength_slider2 = widgets.IntSlider(
            min=self.GANpf2.left_bound,
            max=self.GANpf2.right_bound,
            value=self.strength2,
            continuous_update=WIDGET_CONTINIOUS_UPDATE2,
            description='strength:')
        self.widget_strength_text2 = widgets.IntText(
            description="strength:",
            continuous_update=WIDGET_CONTINIOUS_UPDATE2)
        self.widget_button_incs2 = widgets.Button(
            description="slider +10 (both sides)",
            buttin_style="info",
            layout=widgets.Layout(width="150px"))
        self.widget_button_decs2 = widgets.Button(
            description="slider -10 (both sides)",
            buttin_style="info",
            layout=widgets.Layout(width="150px"))
        self.widget_button_nq2 = widgets.Button(
            description="next query",
            buttin_style="info",
            layout=widgets.Layout(width="300px"))
        self.queries_count_text2 = widgets.Label(value=str(self.query_count2))
        self.widget_chb_adaptive_init2 = widgets.Checkbox(
            value=self.GANpf2.ADAPTIVE_INITIALIZATION,
            description='DONT USE ACQUISITION FUNCTION',
            disabled=True,
            indent=False)
        ''' Sample preferred image '''
        self.widget_button_sample = widgets.Button(
            description="sample preferred image",
            buttin_style="info",
            layout=widgets.Layout(width="300px"))
        self.widget_sample_image = widgets.Image(format='png',
                                                 width=WIDGET_WIDTH,
                                                 value=self.to_bytes(init_img))
Esempio n. 28
0
def display_visual(graph_data, user_input, algorithm=None, problem=None):
    initial_node_colors = graph_data['node_colors']
    if user_input == False:

        def slider_callback(iteration):
            # don't show graph for the first time running the cell calling this function
            try:
                show_map(graph_data, node_colors=all_node_colors[iteration])
            except:
                pass

        def visualize_callback(Visualize):
            if Visualize is True:
                button.value = False

                global all_node_colors

                iterations, all_node_colors, node = algorithm(problem)
                solution = node.solution()
                all_node_colors.append(
                    final_path_colors(all_node_colors[0], problem, solution))

                slider.max = len(all_node_colors) - 1

                for i in range(slider.max + 1):
                    slider.value = i
                    #time.sleep(.5)

        slider = widgets.IntSlider(min=0, max=1, step=1, value=0)
        slider_visual = widgets.interactive(slider_callback, iteration=slider)
        display(slider_visual)

        button = widgets.ToggleButton(value=False)
        button_visual = widgets.interactive(visualize_callback,
                                            Visualize=button)
        display(button_visual)

    if user_input == True:
        node_colors = dict(initial_node_colors)
        if isinstance(algorithm, dict):
            assert set(algorithm.keys()).issubset({
                "Breadth First Tree Search", "Depth First Tree Search",
                "Breadth First Search", "Depth First Graph Search",
                "Best First Graph Search", "Uniform Cost Search",
                "Depth Limited Search", "Iterative Deepening Search",
                "Greedy Best First Search", "A-star Search",
                "Recursive Best First Search"
            })

            algo_dropdown = widgets.Dropdown(description="Search algorithm: ",
                                             options=sorted(
                                                 list(algorithm.keys())),
                                             value="Breadth First Tree Search")
            display(algo_dropdown)
        elif algorithm is None:
            print("No algorithm to run.")
            return 0

        def slider_callback(iteration):
            # don't show graph for the first time running the cell calling this function
            try:
                show_map(graph_data, node_colors=all_node_colors[iteration])
            except:
                pass

        def visualize_callback(Visualize):
            if Visualize is True:
                button.value = False

                problem = GraphProblem(start_dropdown.value,
                                       end_dropdown.value, romania_map)
                global all_node_colors

                user_algorithm = algorithm[algo_dropdown.value]

                iterations, all_node_colors, node = user_algorithm(problem)
                solution = node.solution()
                all_node_colors.append(
                    final_path_colors(all_node_colors[0], problem, solution))

                slider.max = len(all_node_colors) - 1

                for i in range(slider.max + 1):
                    slider.value = i
                    #time.sleep(.5)

        start_dropdown = widgets.Dropdown(description="Start city: ",
                                          options=sorted(
                                              list(node_colors.keys())),
                                          value="Arad")
        display(start_dropdown)

        end_dropdown = widgets.Dropdown(description="Goal city: ",
                                        options=sorted(list(
                                            node_colors.keys())),
                                        value="Fagaras")
        display(end_dropdown)

        button = widgets.ToggleButton(value=False)
        button_visual = widgets.interactive(visualize_callback,
                                            Visualize=button)
        display(button_visual)

        slider = widgets.IntSlider(min=0, max=1, step=1, value=0)
        slider_visual = widgets.interactive(slider_callback, iteration=slider)
        display(slider_visual)
Esempio n. 29
0
def get_para_widgets():
    para_selector = widgets.IntSlider(min=2,
                                      max=60,
                                      description="Rate of Change")
    para_selector_widgets = [para_selector]
    return para_selector_widgets
Esempio n. 30
0
def scatter_3D(x,
               y,
               z,
               interactive=False,
               color_by='z',
               markersize=2,
               tooltips=None,
               axes_names=['x', 'y', 'z'],
               log_axes=(False, False, False)):
    '''
    Args:
        interactive: whether to display an interactive slider to choose how many
            points to display
        color_by: can be one of: 'z', 'age'.
            'age' colors by the index (so the age of the sample)
        tooltips: an array with the same length as the number of points,
            containing a string to display beside them
        log_axes: whether the `x,y,z` axes should have a logarithmic scale
            (False => linear)
    '''
    # from https://plot.ly/python/sliders/
    x, y, z = x.flatten(), y.flatten(), z.flatten()
    num_samples = len(x)

    if color_by == 'z':
        color_by = z
        scale = 'Viridis'
    elif color_by == 'age':
        color_by = list(reversed(range(num_samples)))
        scale = 'Blues'

    data = [
        go.Scatter3d(x=x,
                     y=y,
                     z=z,
                     text=tooltips,
                     mode='markers',
                     opacity=0.9,
                     marker=dict(size=markersize,
                                 color=color_by,
                                 colorscale=scale,
                                 opacity=0.8))
    ]
    layout = go.Layout(title='3D Scatter Plot',
                       autosize=False,
                       width=900,
                       height=600,
                       margin=dict(l=0, r=0, b=0, t=0),
                       scene=dict(
                           xaxis=dict(title=axes_names[0],
                                      type='log' if log_axes[0] else None),
                           yaxis=dict(title=axes_names[1],
                                      type='log' if log_axes[1] else None),
                           zaxis=dict(title=axes_names[2],
                                      type='log' if log_axes[2] else None),
                       ))
    fig = go.Figure(data=data, layout=layout)

    if interactive:

        def plot(val):
            fig['data'][0].update(x=x[:val], y=y[:val], z=z[:val])
            ply.iplot(fig, show_link=False)

        # continuous_update = if the slider moves from 0 to 100, then call the
        # update function with every value from 0 to 100
        slider = widgets.IntSlider(value=num_samples,
                                   min=0,
                                   max=num_samples,
                                   continuous_update=False,
                                   width='100%')
        slider.description = 'first n points: '
        widgets.interact(plot, val=slider)
    else:
        ply.iplot(fig, show_link=False)