def notebook_interaction(self, display=True):
        from ipywidgets import Checkbox
        from traitlets import TraitError as TraitletError
        from IPython.display import display as ip_display

        try:
            container = super(ScalableFixedPattern,
                              self).notebook_interaction(display=False)
            interpolate = Checkbox(description='interpolate',
                                   value=self.interpolate)

            def on_interpolate_change(change):
                self.interpolate = change['new']

            interpolate.observe(on_interpolate_change, names='value')

            container.children = (container.children[0], interpolate) + \
                container.children[1:]

            if not display:
                return container
            ip_display(container)
        except TraitletError:
            if display:
                print('This function is only avialable when running in a'
                      ' notebook')
            else:
                raise
Exemple #2
0
    def notebook_interaction(self, display=True):
        """Creates interactive notebook widgets for all component parameters,
        if available.
        Requires `ipywidgets` to be installed.
        Parameters
        ----------
        display : bool
            if True (default), attempts to display the widgets.
            Otherwise returns the formatted widget object.
        """
        from ipywidgets import (Checkbox, VBox)
        from traitlets import TraitError as TraitletError
        from IPython.display import display as ip_display
        try:
            active = Checkbox(description='active', value=self.active)

            def on_active_change(change):
                self.active = change['new']
            active.observe(on_active_change, names='value')

            container = VBox([active])
            for parameter in self.parameters:
                container.children += parameter.notebook_interaction(False),

            if not display:
                return container
            ip_display(container)
        except TraitletError:
            if display:
                _logger.info('This function is only avialable when running in'
                             ' a notebook')
            else:
                raise
Exemple #3
0
    def notebook_interaction(self, display=True):

        from ipywidgets import (Checkbox, FloatSlider, VBox)
        from traitlets import TraitError as TraitletError
        from IPython.display import display as ip_display

        try:
            active = Checkbox(description='active', value=self.active)

            def on_active_change(change):
                self.active = change['new']
            active.observe(on_active_change, names='value')

            fine_structure = Checkbox(description='Fine structure',
                                      value=self.fine_structure_active)

            def on_fine_structure_active_change(change):
                self.fine_structure_active = change['new']
            fine_structure.observe(on_fine_structure_active_change,
                                   names='value')

            fs_smoothing = FloatSlider(description='Fine structure smoothing',
                                       min=0, max=1, step=0.001,
                                       value=self.fine_structure_smoothing)

            def on_fs_smoothing_change(change):
                self.fine_structure_smoothing = change['new']
            fs_smoothing.observe(on_fs_smoothing_change, names='value')

            container = VBox([active, fine_structure, fs_smoothing])
            for parameter in [self.intensity, self.effective_angle,
                              self.onset_energy]:
                container.children += parameter.notebook_interaction(False),

            if not display:
                return container
            ip_display(container)
        except TraitletError:
            if display:
                print('This function is only avialable when running in a'
                      ' notebook')
            else:
                raise
Exemple #4
0
# You can select any time series and any forecast date, just click on `Run Interact` to generate the predictions from our served endpoint and see the plot.

# In[99]:

style = {'description_width': 'initial'}

# In[100]:


@interact_manual(customer_id=IntSlider(min=0, max=369, value=91, style=style),
                 forecast_day=IntSlider(min=0, max=100, value=51, style=style),
                 confidence=IntSlider(min=60,
                                      max=95,
                                      value=80,
                                      step=5,
                                      style=style),
                 history_weeks_plot=IntSlider(min=1,
                                              max=20,
                                              value=1,
                                              style=style),
                 show_samples=Checkbox(value=False),
                 continuous_update=False)
def plot_interact(customer_id, forecast_day, confidence, history_weeks_plot,
                  show_samples):
    plot(predictor,
         target_ts=timeseries[customer_id],
         forecast_date=end_training + datetime.timedelta(days=forecast_day),
         show_samples=show_samples,
         plot_history=history_weeks_plot * 12 * 7,
         confidence=confidence)
Exemple #5
0
    def __init__(self):
        tab_height = '520px'
        tab_layout = Layout(
            width='800px',  # border='2px solid black',
            height=tab_height,
            overflow_y='scroll')

        self.output_dir = '.'

        max_frames = 505  # first time + 30240 / 60
        self.svg_plot = interactive(self.plot_svg,
                                    frame=(0, max_frames),
                                    continuous_update=False)
        svg_plot_size = '500px'
        self.svg_plot.layout.width = svg_plot_size
        self.svg_plot.layout.height = svg_plot_size
        self.use_defaults = True

        self.show_nucleus = 0  # 0->False, 1->True in Checkbox!
        self.show_edge = 1  # 0->False, 1->True in Checkbox!
        self.scale_radius = 1.0
        self.axes_min = 0.0
        self.axes_max = 2000  # hmm, this can change (TODO?)
        #        self.tab = HBox([svg_plot], layout=tab_layout)

        self.max_frames = BoundedIntText(
            min=0,
            max=99999,
            value=max_frames,
            description='Max',
            layout=Layout(flex='1 1 auto',
                          width='auto'),  #Layout(width='160px'),
        )
        self.max_frames.observe(self.update_max_frames)

        self.show_nucleus_checkbox = Checkbox(
            description='nucleus',
            value=False,
            disabled=False,
            layout=Layout(flex='1 1 auto',
                          width='auto'),  #Layout(width='160px'),
        )
        self.show_nucleus_checkbox.observe(self.show_nucleus_cb)

        self.show_edge_checkbox = Checkbox(
            description='edge',
            value=True,
            disabled=False,
            layout=Layout(flex='1 1 auto',
                          width='auto'),  #Layout(width='160px'),
        )
        self.show_edge_checkbox.observe(self.show_edge_cb)

        #        row1 = HBox([Label('(select slider: drag or left/right arrows)'),
        #            self.max_frames, VBox([self.show_nucleus_checkbox, self.show_edge_checkbox])])
        #            self.max_frames, self.show_nucleus_checkbox], layout=Layout(width='500px'))

        #        self.tab = VBox([row1,self.svg_plot], layout=tab_layout)

        items_auto = [
            Label('(select slider: drag or left/right arrows)'),
            self.max_frames,
            self.show_nucleus_checkbox,
            self.show_edge_checkbox,
        ]
        #row1 = HBox([Label('(select slider: drag or left/right arrows)'),
        #            max_frames, show_nucleus_checkbox, show_edge_checkbox],
        #            layout=Layout(width='800px'))
        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='90%')
        row1 = Box(children=items_auto, layout=box_layout)
        self.tab = VBox([row1, self.svg_plot], layout=tab_layout)
 def create_checkbox(self, kind, description, value, handler):
     checkbox = Checkbox(value=value, description=description, indent=False)
     checkbox.observe(handler, "value")
     checkbox.add_class("view_%s" % kind)
     return checkbox
    def __init__(self):

        micron_units = Label(
            'micron')  # use "option m" (Mac, for micro symbol)

        constWidth = '180px'
        tab_height = '500px'
        stepsize = 10

        #style = {'description_width': '250px'}
        style = {'description_width': '25%'}
        layout = {'width': '400px'}

        name_button_layout = {'width': '25%'}
        widget_layout = {'width': '15%'}
        widget2_layout = {'width': '10%'}
        units_button_layout = {'width': '15%'}
        desc_button_layout = {'width': '45%'}

        menv_var1 = Button(description='oxygen (mmHg)',
                           disabled=True,
                           layout=name_button_layout)
        menv_var1.style.button_color = 'tan'

        param_name1 = Button(description='diffusion_coefficient',
                             disabled=True,
                             layout=name_button_layout)

        self.oxygen_diffusion_coefficient = FloatText(value=100000.0,
                                                      step=10000,
                                                      style=style,
                                                      layout=widget_layout)

        param_name2 = Button(description='decay_rate',
                             disabled=True,
                             layout=name_button_layout)

        self.oxygen_decay_rate = FloatText(value=0.0,
                                           step=0.01,
                                           style=style,
                                           layout=widget_layout)
        param_name3 = Button(description='initial_condition',
                             disabled=True,
                             layout=name_button_layout)

        self.oxygen_initial_condition = FloatText(value=1.5,
                                                  style=style,
                                                  layout=widget_layout)
        param_name4 = Button(description='Dirichlet_boundary_condition',
                             disabled=True,
                             layout=name_button_layout)

        self.oxygen_Dirichlet_boundary_condition = FloatText(
            value=2.4, style=style, layout=widget_layout)
        self.oxygen_Dirichlet_boundary_condition_toggle = Checkbox(
            description='on/off',
            disabled=False,
            style=style,
            layout=widget_layout)
        self.calculate_gradient = Checkbox(description='calculate_gradients',
                                           disabled=False,
                                           layout=desc_button_layout)
        self.track_internal = Checkbox(description='track_in_agents',
                                       disabled=False,
                                       layout=desc_button_layout)

        #  ------- micronenv info
        menv_units_button1 = Button(description='micron^2/min',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button2 = Button(description='1/min',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button3 = Button(description='mmHg',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button4 = Button(description='mmHg',
                                    disabled=True,
                                    layout=units_button_layout)

        row_oxygen = [
            menv_var1,
        ]
        row1 = [
            param_name1, self.oxygen_diffusion_coefficient, menv_units_button1
        ]
        row2 = [param_name2, self.oxygen_decay_rate, menv_units_button2]
        row3 = [param_name3, self.oxygen_initial_condition, menv_units_button3]
        row4 = [
            param_name4, self.oxygen_Dirichlet_boundary_condition,
            menv_units_button4, self.oxygen_Dirichlet_boundary_condition_toggle
        ]
        row5 = [
            self.calculate_gradient,
        ]
        row6 = [
            self.track_internal,
        ]

        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='100%')
        box_oxygen = Box(children=row_oxygen, layout=box_layout)
        box1 = Box(children=row1, layout=box_layout)
        box2 = Box(children=row2, layout=box_layout)
        box3 = Box(children=row3, layout=box_layout)
        box4 = Box(children=row4, layout=box_layout)
        box5 = Box(children=row5, layout=box_layout)
        box6 = Box(children=row6, layout=box_layout)

        self.tab = VBox([
            box_oxygen,
            box1,
            box2,
            box3,
            box4,
            box5,
            box6,
        ])
Exemple #8
0
def all_options(analysis=True):
    options = Dropdown(options={
        'confined': 1,
        'leaky aquifer': 2,
        'flow barrier': 3,
        'recharge source': 4,
        'unconfined': 5
    },
                       value=1,
                       description='Aquifer type')
    approx = Checkbox(value=True, description='approx.')
    semilog = Checkbox(value=False, description='SemiLog')
    image = Checkbox(value=False, description='image')
    Q = FloatSlider(value=1000,
                    description=r'$Q$ [m$^3$/day]',
                    min=500,
                    max=1500,
                    step=500,
                    continuous_update=False)
    t = FloatLogSlider(value=1.0,
                       description=r'$t$ [day]',
                       base=10,
                       min=-1,
                       max=2,
                       step=0.2,
                       continuous_update=False)
    r = FloatSlider(value=200,
                    description=r'$r$ [m]',
                    min=100,
                    max=500,
                    step=100,
                    continuous_update=False)
    T = FloatSlider(value=300,
                    description=r'$T$ [m$^2$/day]',
                    min=100,
                    max=500,
                    step=100,
                    continuous_update=False)
    c = FloatLogSlider(value=1.e5,
                       description=r'$c$ [day]',
                       base=10,
                       min=2,
                       max=6,
                       step=1,
                       continuous_update=False)
    H = FloatSlider(value=2,
                    description=r'$b$ [m]',
                    min=2,
                    max=5,
                    step=1.5,
                    continuous_update=False)
    io = interactive_output(
        plot_theis, {
            'Q': Q,
            't': t,
            'r': r,
            'T': T,
            'approx': approx,
            'semilog': semilog,
            'itest': options,
            'image': image,
            'c': c,
            'H': H,
            'analysis': fixed(analysis)
        })
    return VBox([
        HBox([options]),
        HBox([Q, t, approx]),
        HBox([T, r, semilog]),
        HBox([H, c, image]), io
    ])
Exemple #9
0
    def __init__(self,
                 wvmin,
                 wvmax,
                 clouds,
                 show_labels=True,
                 widgets=('z', 'sigma', 'n', 'lyman', 'balmer', 'paschen'),
                 zmin=0.00,
                 zmax=0.10,
                 smin=1,
                 smax=500,
                 nmin=0,
                 nmax=0.1,
                 Pmin=0,
                 Pmax=10,
                 vmin=1,
                 vmax=100,
                 tmin=0,
                 tmax=20):
        self.clouds = clouds
        self.ncomponents = len(clouds)
        dv = (smax + smin) / 8.0
        dlam = dv / c * (wvmax - wvmin) / 2.0
        wv = np.linspace(wvmin, wvmax, int((wvmax - wvmin) / dlam))
        self.wv = wv

        # set max height of graph
        cloud = clouds[0]
        old_sigma = cloud.sigma
        old_n = cloud.n
        cloud.sigma = (smin + smax) / 2.0
        cloud.n = nmax
        self.ymax = 1.1 * np.amax(cloud.line_flux(wv))
        cloud.sigma = old_sigma
        cloud.n = old_n

        # construct widget dictionary
        widget_dict = {}
        letter = cycle(ascii_lowercase)
        for i, cloud in enumerate(self.clouds):
            # float sliders
            keys = ['z', 'sigma', 'n', 'P', 'v', 't']
            labels = [
                'Redshift: ', 'Dispersion: ', 'Density: ', 'Period: ',
                'Amplitude: ', 'Time: '
            ]
            widget_kwargs = {
                "disabled": False,
                "continuous_update": False,
                "orientation": "horizontal",
                "readout": True,
                "readout_format": ".4f"
            }
            values = [
                cloud.z, cloud.sigma, cloud.n, cloud.P, cloud.vsini, cloud.t
            ]
            bounds = [(zmin, zmax), (smin, smax), (nmin, nmax), (Pmin, Pmax),
                      (vmin, vmax), (tmin, tmax)]
            for j, key in enumerate(keys):
                value = values[j]
                if key not in widgets:
                    widget_dict[key + str(i)] = fixed(value)
                    continue
                if show_labels:
                    label = labels[j]
                else:
                    label = "({})".format(next(letter))
                lower, upper = bounds[j]
                widget_dict[key + str(i)] = FloatSlider(value=value,
                                                        min=lower,
                                                        max=upper,
                                                        step=(upper - lower) /
                                                        100,
                                                        description=label,
                                                        **widget_kwargs)
            # boolean checkboxes
            keys = ['lyman', 'balmer', 'paschen']
            labels = [s.capitalize() + ": " for s in keys]
            widget_kwargs = {"disabled": False}
            values = [cloud.lyman, cloud.balmer, cloud.paschen]
            for j, key in enumerate(keys):
                value = values[j]
                if key not in widgets:
                    widget_dict[key + str(i)] = fixed(value)
                    continue
                if show_labels:
                    label = labels[j]
                else:
                    label = "({})".format(next(letter))
                widget_dict[key + str(i)] = Checkbox(value=value,
                                                     description=label,
                                                     **widget_kwargs)

        super().__init__(self.plot, **widget_dict)
Exemple #10
0
class SubstrateTab(object):
    def __init__(self):

        self.output_dir = '.'
        #        self.output_dir = 'tmpdir'

        # self.fig = plt.figure(figsize=(7.2,6))  # this strange figsize results in a ~square contour plot

        self.use_defaults = True

        self.svg_xmin = 0
        self.svg_xrange = 2000
        self.xmin = -1000.
        self.xmax = 1000.
        self.ymin = -1000.
        self.ymax = 1000.
        self.x_range = 2000.
        self.y_range = 2000.

        self.show_nucleus = 0
        self.show_edge = False

        # initial value
        self.field_index = 4
        # self.field_index = self.mcds_field.value + 4

        # define dummy size of mesh (set in the tool's primary module)
        self.numx = 0
        self.numy = 0

        tab_height = '600px'
        tab_height = '500px'
        constWidth = '180px'
        constWidth2 = '150px'
        tab_layout = Layout(
            width='900px',  # border='2px solid black',
            height=tab_height,
        )  #overflow_y='scroll')

        max_frames = 1
        # self.mcds_plot = interactive(self.plot_substrate, frame=(0, max_frames), continuous_update=False)
        self.mcds_plot = interactive(self.plot_plots,
                                     frame=(0, max_frames),
                                     continuous_update=False)

        # "plot_size" controls the size of the tab height, not the plot (rf. figsize for that)
        # NOTE: the Substrates Plot tab has an extra row of widgets at the top of it (cf. Cell Plots tab)
        svg_plot_size = '700px'
        svg_plot_size = '600px'
        svg_plot_size = '700px'
        svg_plot_size = '900px'
        self.mcds_plot.layout.width = svg_plot_size
        self.mcds_plot.layout.height = svg_plot_size

        self.fontsize = 20

        self.max_frames = BoundedIntText(
            min=0,
            max=99999,
            value=max_frames,
            description='Max',
            layout=Layout(width='160px'),
        )
        self.max_frames.observe(self.update_max_frames)

        self.field_min_max = {'dummy': [0., 1.]}
        # hacky I know, but make a dict that's got (key,value) reversed from the dict in the Dropdown below
        self.field_dict = {0: 'dummy'}

        self.mcds_field = Dropdown(
            options={'dummy': 0},
            value=0,
            #     description='Field',
            layout=Layout(width=constWidth))
        # print("substrate __init__: self.mcds_field.value=",self.mcds_field.value)
        #        self.mcds_field.observe(self.mcds_field_cb)
        self.mcds_field.observe(self.mcds_field_changed_cb)

        # self.field_cmap = Text(
        #     value='viridis',
        #     description='Colormap',
        #     disabled=True,
        #     layout=Layout(width=constWidth),
        # )
        self.field_cmap = Dropdown(
            options=['viridis', 'jet', 'YlOrRd'],
            value='YlOrRd',
            #     description='Field',
            layout=Layout(width=constWidth))
        #self.field_cmap.observe(self.plot_substrate)
        #        self.field_cmap.observe(self.plot_substrate)
        self.field_cmap.observe(self.mcds_field_cb)

        self.cmap_fixed = Checkbox(
            description='Fix',
            disabled=False,
            #           layout=Layout(width=constWidth2),
        )

        self.save_min_max = Button(
            description='Save',  #style={'description_width': 'initial'},
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Save min/max for this substrate',
            disabled=True,
            layout=Layout(width='90px'))

        def save_min_max_cb(b):
            #            field_name = self.mcds_field.options[]
            #            field_name = next(key for key, value in self.mcds_field.options.items() if value == self.mcds_field.value)
            field_name = self.field_dict[self.mcds_field.value]
            #            print(field_name)
            #            self.field_min_max = {'oxygen': [0., 30.], 'glucose': [0., 1.], 'H+ ions': [0., 1.], 'ECM': [0., 1.], 'NP1': [0., 1.], 'NP2': [0., 1.]}
            self.field_min_max[field_name][0] = self.cmap_min.value
            self.field_min_max[field_name][1] = self.cmap_max.value
#            print(self.field_min_max)

        self.save_min_max.on_click(save_min_max_cb)

        self.cmap_min = FloatText(
            description='Min',
            value=0,
            step=0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_min.observe(self.mcds_field_cb)

        self.cmap_max = FloatText(
            description='Max',
            value=38,
            step=0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_max.observe(self.mcds_field_cb)

        def cmap_fixed_cb(b):
            if (self.cmap_fixed.value):
                self.cmap_min.disabled = False
                self.cmap_max.disabled = False
                self.save_min_max.disabled = False
            else:
                self.cmap_min.disabled = True
                self.cmap_max.disabled = True
                self.save_min_max.disabled = True
#            self.mcds_field_cb()

        self.cmap_fixed.observe(cmap_fixed_cb)

        field_cmap_row2 = HBox([self.field_cmap, self.cmap_fixed])

        #        field_cmap_row3 = HBox([self.save_min_max, self.cmap_min, self.cmap_max])
        items_auto = [
            self.save_min_max,  #layout=Layout(flex='3 1 auto', width='auto'),
            self.cmap_min,
            self.cmap_max,
        ]
        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='80%')
        field_cmap_row3 = Box(children=items_auto, layout=box_layout)

        #---------------------
        self.cell_edges_toggle = Checkbox(
            description='edges',
            disabled=False,
            value=False,
            #           layout=Layout(width=constWidth2),
        )

        def cell_edges_toggle_cb(b):
            # self.update()
            if (self.cell_edges_toggle.value):
                self.show_edge = True
            else:
                self.show_edge = False
            self.mcds_plot.update()

        self.cell_edges_toggle.observe(cell_edges_toggle_cb)

        self.cells_toggle = Checkbox(
            description='Cells',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def cells_toggle_cb(b):
            # self.update()
            self.mcds_plot.update()
            if (self.cells_toggle.value):
                self.cell_edges_toggle.disabled = False
            else:
                self.cell_edges_toggle.disabled = True

        self.cells_toggle.observe(cells_toggle_cb)

        #---------------------
        self.substrates_toggle = Checkbox(
            description='Substrates',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def substrates_toggle_cb(b):
            if (self.substrates_toggle.value):  # seems bass-ackwards
                self.cmap_fixed.disabled = False
                self.cmap_min.disabled = False
                self.cmap_max.disabled = False
                self.mcds_field.disabled = False
                self.field_cmap.disabled = False
            else:
                self.cmap_fixed.disabled = True
                self.cmap_min.disabled = True
                self.cmap_max.disabled = True
                self.mcds_field.disabled = True
                self.field_cmap.disabled = True

        self.substrates_toggle.observe(substrates_toggle_cb)

        self.grid_toggle = Checkbox(
            description='grid',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def grid_toggle_cb(b):
            # self.update()
            self.mcds_plot.update()

        self.grid_toggle.observe(grid_toggle_cb)

        #        field_cmap_row3 = Box([self.save_min_max, self.cmap_min, self.cmap_max])

        # mcds_tab = widgets.VBox([mcds_dir, mcds_plot, mcds_play], layout=tab_layout)
        # mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3, self.max_frames])  # mcds_dir
        #        mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3,])  # mcds_dir

        #        self.tab = HBox([mcds_params, self.mcds_plot], layout=tab_layout)
        #        self.tab = HBox([mcds_params, self.mcds_plot])

        help_label = Label('select slider: drag or left/right arrows')
        # row1 = Box([help_label, Box( [self.max_frames, self.mcds_field, self.field_cmap], layout=Layout(border='0px solid black',
        row1a = Box([self.max_frames, self.mcds_field, self.field_cmap],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        row1b = Box([self.cells_toggle, self.cell_edges_toggle],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        row1 = HBox([row1a, Label('.....'), row1b])

        row2a = Box([self.cmap_fixed, self.cmap_min, self.cmap_max],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        # row2b = Box( [self.substrates_toggle, self.grid_toggle], layout=Layout(border='1px solid black',
        row2b = Box([
            self.substrates_toggle,
        ],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        # row2 = HBox( [row2a, self.substrates_toggle, self.grid_toggle])
        row2 = HBox([row2a, Label('.....'), row2b])

        if (hublib_flag):
            self.download_button = Download('mcds.zip',
                                            style='warning',
                                            icon='cloud-download',
                                            tooltip='Download data',
                                            cb=self.download_cb)

            self.download_svg_button = Download(
                'svg.zip',
                style='warning',
                icon='cloud-download',
                tooltip='You need to allow pop-ups in your browser',
                cb=self.download_svg_cb)
            download_row = HBox([
                self.download_button.w, self.download_svg_button.w,
                Label("Download all cell plots (browser must allow pop-ups).")
            ])

            #        self.tab = VBox([row1, row2, self.mcds_plot])
            # self.tab = VBox([row1, row2, self.mcds_plot, download_row])
            # box_layout = Layout(border='0px solid')
            controls_box = VBox([row1,
                                 row2])  # ,width='50%', layout=box_layout)
            self.tab = VBox([controls_box, self.mcds_plot, download_row])
        else:
            # self.tab = VBox([row1, row2])
            self.tab = VBox([row1, row2, self.mcds_plot])

    #---------------------------------------------------
    def update_dropdown_fields(self, data_dir):
        # print('update_dropdown_fields called --------')
        self.output_dir = data_dir
        tree = None
        try:
            fname = os.path.join(self.output_dir, "initial.xml")
            tree = ET.parse(fname)
            xml_root = tree.getroot()
        except:
            print("Cannot open ", fname,
                  " to read info, e.g., names of substrate fields.")
            return

        xml_root = tree.getroot()
        self.field_min_max = {}
        self.field_dict = {}
        dropdown_options = {}
        uep = xml_root.find('.//variables')
        comment_str = ""
        field_idx = 0
        if (uep):
            for elm in uep.findall('variable'):
                # print("-----> ",elm.attrib['name'])
                self.field_min_max[elm.attrib['name']] = [0., 1.]
                self.field_dict[field_idx] = elm.attrib['name']
                dropdown_options[elm.attrib['name']] = field_idx
                field_idx += 1

#        constWidth = '180px'
# print('options=',dropdown_options)
        self.mcds_field.value = 0
        self.mcds_field.options = dropdown_options
#         self.mcds_field = Dropdown(
# #            options={'oxygen': 0, 'glucose': 1},
#             options=dropdown_options,
#             value=0,
#             #     description='Field',
#            layout=Layout(width=constWidth)
#         )

    def update_max_frames_expected(
            self, value):  # called when beginning an interactive Run
        self.max_frames.value = value  # assumes naming scheme: "snapshot%08d.svg"
        self.mcds_plot.children[0].max = self.max_frames.value

#    def update(self, rdir):

    def update(self, rdir=''):
        # with debug_view:
        #     print("substrates: update rdir=", rdir)

        if rdir:
            self.output_dir = rdir

        all_files = sorted(
            glob.glob(os.path.join(self.output_dir, 'output*.xml')))
        if len(all_files) > 0:
            last_file = all_files[-1]
            self.max_frames.value = int(
                last_file[-12:-4])  # assumes naming scheme: "snapshot%08d.svg"

        # with debug_view:
        #     print("substrates: added %s files" % len(all_files))

        # self.output_dir = rdir
        # if rdir == '':
        #     # self.max_frames.value = 0
        #     tmpdir = os.path.abspath('tmpdir')
        #     self.output_dir = tmpdir
        #     all_files = sorted(glob.glob(os.path.join(tmpdir, 'output*.xml')))
        #     if len(all_files) > 0:
        #         last_file = all_files[-1]
        #         self.max_frames.value = int(last_file[-12:-4])  # assumes naming scheme: "output%08d.xml"
        #         self.mcds_plot.update()
        #     return

        # all_files = sorted(glob.glob(os.path.join(rdir, 'output*.xml')))
        # if len(all_files) > 0:
        #     last_file = all_files[-1]
        #     self.max_frames.value = int(last_file[-12:-4])  # assumes naming scheme: "output%08d.xml"
        #     self.mcds_plot.update()

    def download_svg_cb(self):
        file_str = os.path.join(self.output_dir, '*.svg')
        # print('zip up all ',file_str)
        with zipfile.ZipFile('svg.zip', 'w') as myzip:
            for f in glob.glob(file_str):
                myzip.write(f, os.path.basename(
                    f))  # 2nd arg avoids full filename path in the archive

    def download_cb(self):
        file_xml = os.path.join(self.output_dir, '*.xml')
        file_mat = os.path.join(self.output_dir, '*.mat')
        # print('zip up all ',file_str)
        with zipfile.ZipFile('mcds.zip', 'w') as myzip:
            for f in glob.glob(file_xml):
                myzip.write(f, os.path.basename(
                    f))  # 2nd arg avoids full filename path in the archive
            for f in glob.glob(file_mat):
                myzip.write(f, os.path.basename(f))

    def update_max_frames(self, _b):
        self.mcds_plot.children[0].max = self.max_frames.value

    def mcds_field_changed_cb(self, b):
        # print("mcds_field_changed_cb: self.mcds_field.value=",self.mcds_field.value)
        if (self.mcds_field.value == None):
            return
        self.field_index = self.mcds_field.value + 4

        field_name = self.field_dict[self.mcds_field.value]
        #        print('mcds_field_cb: '+field_name)
        self.cmap_min.value = self.field_min_max[field_name][0]
        self.cmap_max.value = self.field_min_max[field_name][1]
        self.mcds_plot.update()

    def mcds_field_cb(self, b):
        #self.field_index = self.mcds_field.value
        #        self.field_index = self.mcds_field.options.index(self.mcds_field.value) + 4
        #        self.field_index = self.mcds_field.options[self.mcds_field.value]
        self.field_index = self.mcds_field.value + 4

        # field_name = self.mcds_field.options[self.mcds_field.value]
        # self.cmap_min.value = self.field_min_max[field_name][0]  # oxygen, etc
        # self.cmap_max.value = self.field_min_max[field_name][1]  # oxygen, etc

        #        self.field_index = self.mcds_field.value + 4

        #        print('field_index=',self.field_index)
        self.mcds_plot.update()

    #---------------------------------------------------------------------------
    def circles(self, x, y, s, c='b', vmin=None, vmax=None, **kwargs):
        """
        See https://gist.github.com/syrte/592a062c562cd2a98a83 

        Make a scatter plot of circles. 
        Similar to plt.scatter, but the size of circles are in data scale.
        Parameters
        ----------
        x, y : scalar or array_like, shape (n, )
            Input data
        s : scalar or array_like, shape (n, ) 
            Radius of circles.
        c : color or sequence of color, optional, default : 'b'
            `c` can be a single color format string, or a sequence of color
            specifications of length `N`, or a sequence of `N` numbers to be
            mapped to colors using the `cmap` and `norm` specified via kwargs.
            Note that `c` should not be a single numeric RGB or RGBA sequence 
            because that is indistinguishable from an array of values
            to be colormapped. (If you insist, use `color` instead.)  
            `c` can be a 2-D array in which the rows are RGB or RGBA, however. 
        vmin, vmax : scalar, optional, default: None
            `vmin` and `vmax` are used in conjunction with `norm` to normalize
            luminance data.  If either are `None`, the min and max of the
            color array is used.
        kwargs : `~matplotlib.collections.Collection` properties
            Eg. alpha, edgecolor(ec), facecolor(fc), linewidth(lw), linestyle(ls), 
            norm, cmap, transform, etc.
        Returns
        -------
        paths : `~matplotlib.collections.PathCollection`
        Examples
        --------
        a = np.arange(11)
        circles(a, a, s=a*0.2, c=a, alpha=0.5, ec='none')
        plt.colorbar()
        License
        --------
        This code is under [The BSD 3-Clause License]
        (http://opensource.org/licenses/BSD-3-Clause)
        """

        if np.isscalar(c):
            kwargs.setdefault('color', c)
            c = None

        if 'fc' in kwargs:
            kwargs.setdefault('facecolor', kwargs.pop('fc'))
        if 'ec' in kwargs:
            kwargs.setdefault('edgecolor', kwargs.pop('ec'))
        if 'ls' in kwargs:
            kwargs.setdefault('linestyle', kwargs.pop('ls'))
        if 'lw' in kwargs:
            kwargs.setdefault('linewidth', kwargs.pop('lw'))
        # You can set `facecolor` with an array for each patch,
        # while you can only set `facecolors` with a value for all.

        zipped = np.broadcast(x, y, s)
        patches = [Circle((x_, y_), s_) for x_, y_, s_ in zipped]
        collection = PatchCollection(patches, **kwargs)
        if c is not None:
            c = np.broadcast_to(c, zipped.shape).ravel()
            collection.set_array(c)
            collection.set_clim(vmin, vmax)

        ax = plt.gca()
        ax.add_collection(collection)
        ax.autoscale_view()
        # plt.draw_if_interactive()
        if c is not None:
            plt.sci(collection)
        # return collection

    #------------------------------------------------------------
    # def plot_svg(self, frame, rdel=''):
    def plot_svg(self, frame):
        # global current_idx, axes_max
        global current_frame
        current_frame = frame
        fname = "snapshot%08d.svg" % frame
        full_fname = os.path.join(self.output_dir, fname)
        # with debug_view:
        # print("plot_svg:", full_fname)
        # print("-- plot_svg:", full_fname)
        if not os.path.isfile(full_fname):
            print("Once output files are generated, click the slider.")
            return

        xlist = deque()
        ylist = deque()
        rlist = deque()
        rgb_list = deque()

        #  print('\n---- ' + fname + ':')
        #        tree = ET.parse(fname)
        tree = ET.parse(full_fname)
        root = tree.getroot()
        #  print('--- root.tag ---')
        #  print(root.tag)
        #  print('--- root.attrib ---')
        #  print(root.attrib)
        #  print('--- child.tag, child.attrib ---')
        numChildren = 0
        for child in root:
            #    print(child.tag, child.attrib)
            #    print("keys=",child.attrib.keys())
            if self.use_defaults and ('width' in child.attrib.keys()):
                self.axes_max = float(child.attrib['width'])
                # print("debug> found width --> axes_max =", axes_max)
            if child.text and "Current time" in child.text:
                svals = child.text.split()
                # title_str = "(" + str(current_idx) + ") Current time: " + svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"
                # title_str = "Current time: " + svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"
                title_str = svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"

            # print("width ",child.attrib['width'])
            # print('attrib=',child.attrib)
            # if (child.attrib['id'] == 'tissue'):
            if ('id' in child.attrib.keys()):
                # print('-------- found tissue!!')
                tissue_parent = child
                break

        # print('------ search tissue')
        cells_parent = None

        for child in tissue_parent:
            # print('attrib=',child.attrib)
            if (child.attrib['id'] == 'cells'):
                # print('-------- found cells, setting cells_parent')
                cells_parent = child
                break
            numChildren += 1

        num_cells = 0
        #  print('------ search cells')
        for child in cells_parent:
            #    print(child.tag, child.attrib)
            #    print('attrib=',child.attrib)
            for circle in child:  # two circles in each child: outer + nucleus
                #  circle.attrib={'cx': '1085.59','cy': '1225.24','fill': 'rgb(159,159,96)','r': '6.67717','stroke': 'rgb(159,159,96)','stroke-width': '0.5'}
                #      print('  --- cx,cy=',circle.attrib['cx'],circle.attrib['cy'])
                xval = float(circle.attrib['cx'])

                # map SVG coords into comp domain
                xval = (xval - self.svg_xmin
                        ) / self.svg_xrange * self.x_range + self.xmin

                s = circle.attrib['fill']
                # print("s=",s)
                # print("type(s)=",type(s))
                if (s[0:3] == "rgb"
                    ):  # if an rgb string, e.g. "rgb(175,175,80)"
                    rgb = list(map(int, s[4:-1].split(",")))
                    rgb[:] = [x / 255. for x in rgb]
                else:  # otherwise, must be a color name
                    rgb_tuple = mplc.to_rgb(mplc.cnames[s])  # a tuple
                    rgb = [x for x in rgb_tuple]

                # test for bogus x,y locations (rwh TODO: use max of domain?)
                too_large_val = 10000.
                if (np.fabs(xval) > too_large_val):
                    print("bogus xval=", xval)
                    break
                yval = float(circle.attrib['cy'])
                yval = (yval - self.svg_xmin
                        ) / self.svg_xrange * self.y_range + self.ymin
                if (np.fabs(yval) > too_large_val):
                    print("bogus xval=", xval)
                    break

                rval = float(circle.attrib['r'])
                # if (rgb[0] > rgb[1]):
                #     print(num_cells,rgb, rval)
                xlist.append(xval)
                ylist.append(yval)
                rlist.append(rval)
                rgb_list.append(rgb)

                # For .svg files with cells that *have* a nucleus, there will be a 2nd
                if (self.show_nucleus == 0):
                    #if (not self.show_nucleus):
                    break

            num_cells += 1

            # if num_cells > 3:   # for debugging
            #   print(fname,':  num_cells= ',num_cells," --- debug exit.")
            #   sys.exit(1)
            #   break

            # print(fname,':  num_cells= ',num_cells)

        xvals = np.array(xlist)
        yvals = np.array(ylist)
        rvals = np.array(rlist)
        rgbs = np.array(rgb_list)
        # print("xvals[0:5]=",xvals[0:5])
        # print("rvals[0:5]=",rvals[0:5])
        # print("rvals.min, max=",rvals.min(),rvals.max())

        # rwh - is this where I change size of render window?? (YES - yipeee!)
        #   plt.figure(figsize=(6, 6))
        #   plt.cla()
        # if (self.substrates_toggle.value):
        title_str += " (" + str(num_cells) + " agents)"
        # title_str = " (" + str(num_cells) + " agents)"
        # else:
        # mins= round(int(float(root.find(".//current_time").text)))  # TODO: check units = mins
        # hrs = int(mins/60)
        # days = int(hrs/24)
        # title_str = '%dd, %dh, %dm' % (int(days),(hrs%24), mins - (hrs*60))
        plt.title(title_str)

        #   plt.xlim(axes_min,axes_max)
        #   plt.ylim(axes_min,axes_max)
        #   plt.scatter(xvals,yvals, s=rvals*scale_radius, c=rgbs)

        # TODO: make figsize a function of plot_size? What about non-square plots?
        # self.fig = plt.figure(figsize=(9, 9))

        #        axx = plt.axes([0, 0.05, 0.9, 0.9])  # left, bottom, width, height
        #        axx = fig.gca()
        #        print('fig.dpi=',fig.dpi) # = 72

        #   im = ax.imshow(f.reshape(100,100), interpolation='nearest', cmap=cmap, extent=[0,20, 0,20])
        #   ax.xlim(axes_min,axes_max)
        #   ax.ylim(axes_min,axes_max)

        # convert radii to radii in pixels
        # ax2 = self.fig.gca()
        # N = len(xvals)
        # rr_pix = (ax2.transData.transform(np.vstack([rvals, rvals]).T) -
        #             ax2.transData.transform(np.vstack([np.zeros(N), np.zeros(N)]).T))
        # rpix, _ = rr_pix.T

        # markers_size = (144. * rpix / self.fig.dpi)**2   # = (2*rpix / fig.dpi * 72)**2
        # markers_size = markers_size/4000000.
        # print('max=',markers_size.max())

        #rwh - temp fix - Ah, error only occurs when "edges" is toggled on
        if (self.show_edge):
            try:
                # plt.scatter(xvals,yvals, s=markers_size, c=rgbs, edgecolor='black', linewidth=0.5)
                self.circles(xvals,
                             yvals,
                             s=rvals,
                             color=rgbs,
                             edgecolor='black',
                             linewidth=0.5)
                # cell_circles = self.circles(xvals,yvals, s=rvals, color=rgbs, edgecolor='black', linewidth=0.5)
                # plt.sci(cell_circles)
            except (ValueError):
                pass
        else:
            # plt.scatter(xvals,yvals, s=markers_size, c=rgbs)
            self.circles(xvals, yvals, s=rvals, color=rgbs)

        # if (self.show_tracks):
        #     for key in self.trackd.keys():
        #         xtracks = self.trackd[key][:,0]
        #         ytracks = self.trackd[key][:,1]
        #         plt.plot(xtracks[0:frame],ytracks[0:frame],  linewidth=5)

        # plt.xlim(self.axes_min, self.axes_max)
        # plt.ylim(self.axes_min, self.axes_max)
        #   ax.grid(False)
#        axx.set_title(title_str)
# plt.title(title_str)

#---------------------------------------------------------------------------

    def plot_substrate(self, frame, grid):
        # global current_idx, axes_max, gFileId, field_index
        if (self.substrates_toggle.value):
            fname = "output%08d_microenvironment0.mat" % frame
            xml_fname = "output%08d.xml" % frame
            # print("--- plot_substrate")
            # fullname = output_dir_str + fname

            #        fullname = fname
            full_fname = os.path.join(self.output_dir, fname)
            full_xml_fname = os.path.join(self.output_dir, xml_fname)
            #        self.output_dir = '.'

            #        if not os.path.isfile(fullname):
            if not os.path.isfile(full_fname):
                print("Once output files are generated, click the slider."
                      )  # No:  output00000000_microenvironment0.mat
                return

    #        tree = ET.parse(xml_fname)
            tree = ET.parse(full_xml_fname)
            xml_root = tree.getroot()
            mins = round(int(float(xml_root.find(
                ".//current_time").text)))  # TODO: check units = mins
            hrs = int(mins / 60)
            days = int(hrs / 24)
            title_str = '%dd, %dh, %dm' % (int(days),
                                           (hrs % 24), mins - (hrs * 60))

            info_dict = {}
            #        scipy.io.loadmat(fullname, info_dict)
            scipy.io.loadmat(full_fname, info_dict)
            M = info_dict['multiscale_microenvironment']
            #     global_field_index = int(mcds_field.value)
            #     print('plot_substrate: field_index =',field_index)
            f = M[
                self.
                field_index, :]  # 4=tumor cells field, 5=blood vessel density, 6=growth substrate
            # plt.clf()
            # my_plot = plt.imshow(f.reshape(400,400), cmap='jet', extent=[0,20, 0,20])

            # self.fig = plt.figure(figsize=(18.0,15))  # this strange figsize results in a ~square contour plot

            # plt.subplot(grid[0:1, 0:1])
            # main_ax = self.fig.add_subplot(grid[0:1, 0:1])  # works, but tiny upper-left region
            #main_ax = self.fig.add_subplot(grid[0:2, 0:2])
            # main_ax = self.fig.add_subplot(grid[0:, 0:2])
            #main_ax = self.fig.add_subplot(grid[:-1, 0:])   # nrows, ncols
            #main_ax = self.fig.add_subplot(grid[0:, 0:])   # nrows, ncols
            #main_ax = self.fig.add_subplot(grid[0:4, 0:])   # nrows, ncols
            main_ax = self.fig.add_subplot(grid[0:3, 0:])  # nrows, ncols

            # plt.rc('font', size=10)  # TODO: does this affect the Cell plots fonts too? YES. Not what we want.

            #     fig.set_tight_layout(True)
            #     ax = plt.axes([0, 0.05, 0.9, 0.9 ]) #left, bottom, width, height
            #     ax = plt.axes([0, 0.0, 1, 1 ])
            #     cmap = plt.cm.viridis # Blues, YlOrBr, ...
            #     im = ax.imshow(f.reshape(100,100), interpolation='nearest', cmap=cmap, extent=[0,20, 0,20])
            #     ax.grid(False)

            # print("substrates.py: ------- numx, numy = ", self.numx, self.numy )
            if (self.numx == 0):  # need to parse vals from the config.xml
                # fname = os.path.join(self.output_dir, "config.xml")
                # tree = ET.parse(fname)
                # xml_root = tree.getroot()
                # xmin = float(xml_root.find(".//x_min").text)
                # xmax = float(xml_root.find(".//x_max").text)
                # dx = float(xml_root.find(".//dx").text)
                # ymin = float(xml_root.find(".//y_min").text)
                # ymax = float(xml_root.find(".//y_max").text)
                # dy = float(xml_root.find(".//dy").text)
                # self.numx =  math.ceil( (xmax - xmin) / dx)
                # self.numy =  math.ceil( (ymax - ymin) / dy)
                mcds1 = pyMCDS('initial.xml', self.output_dir)
                self.numx = mcds1.data['mesh']['x_coordinates'].shape[0]
                self.numy = mcds1.data['mesh']['x_coordinates'].shape[1]

            xgrid = M[0, :].reshape(self.numy, self.numx)
            ygrid = M[1, :].reshape(self.numy, self.numx)

            num_contours = 15
            levels = MaxNLocator(nbins=num_contours).tick_values(
                self.cmap_min.value, self.cmap_max.value)
            contour_ok = True
            if (self.cmap_fixed.value):
                try:
                    substrate_plot = main_ax.contourf(
                        xgrid,
                        ygrid,
                        M[self.field_index, :].reshape(self.numy, self.numx),
                        levels=levels,
                        extend='both',
                        cmap=self.field_cmap.value,
                        fontsize=self.fontsize)
                except:
                    contour_ok = False
                    # print('got error on contourf 1.')
            else:
                try:
                    substrate_plot = main_ax.contourf(
                        xgrid,
                        ygrid,
                        M[self.field_index, :].reshape(self.numy, self.numx),
                        num_contours,
                        cmap=self.field_cmap.value)
                except:
                    contour_ok = False
                    # print('got error on contourf 2.')

            if (contour_ok):
                main_ax.set_title(title_str, fontsize=self.fontsize)
                main_ax.tick_params(labelsize=self.fontsize)
                # cbar = plt.colorbar(my_plot)
                cbar = self.fig.colorbar(substrate_plot, ax=main_ax)
                cbar.ax.tick_params(labelsize=self.fontsize)
                # cbar = main_ax.colorbar(my_plot)
                # cbar.ax.tick_params(labelsize=self.fontsize)
            # axes_min = 0
            # axes_max = 2000

            main_ax.set_xlim([self.xmin, self.xmax])
            main_ax.set_ylim([self.ymin, self.ymax])

            # if (frame == 0):  # maybe allow substrate grid display later
            #     xs = np.linspace(self.xmin,self.xmax,self.numx)
            #     ys = np.linspace(self.ymin,self.ymax,self.numy)
            #     hlines = np.column_stack(np.broadcast_arrays(xs[0], ys, xs[-1], ys))
            #     vlines = np.column_stack(np.broadcast_arrays(xs, ys[0], xs, ys[-1]))
            #     grid_lines = np.concatenate([hlines, vlines]).reshape(-1, 2, 2)
            #     line_collection = LineCollection(grid_lines, color="gray", linewidths=0.5)
            #     # ax = main_ax.gca()
            #     main_ax.add_collection(line_collection)
            #     # ax.set_xlim(xs[0], xs[-1])
            #     # ax.set_ylim(ys[0], ys[-1])

        # Now plot the cells (possibly on top of the substrate)
        if (self.cells_toggle.value):
            self.plot_svg(frame)

        # plt.subplot(grid[2, 0])
        # oxy_ax = self.fig.add_subplot(grid[2:, 0:1])
        #oxy_ax = self.fig.add_subplot(grid[:2, 2:])

        #oxy_ax = self.fig.add_subplot(grid[:-1, 0:2])  # nrows, ncols
        #oxy_ax = self.fig.add_subplot(grid[2:3, 0:1])  # nrows, ncols

        # oxy_ax = self.fig.add_subplot(grid[4:4, 0:1])  # invalid
#        main_ax = self.fig.add_subplot(grid[0:1, 0:1])

# experiment with small plot of oxygen (or whatever)
# oxy_ax = self.fig.add_subplot(grid[3:4, 0:1])  # nrows, ncols
# x = np.linspace(0, 500)
# oxy_ax.plot(x, 300*np.sin(x))

# mcds_play = widgets.Play(
# #     interval=10,
#     value=50,
#     min=0,
#     max=100,
#     step=1,
#     description="Press play",
#     disabled=False,
# )
# #mcds_slider = widgets.IntSlider()

# widgets.jslink((mcds_play, 'value'), (mcds_slider, 'value'))
# widgets.HBox([mcds_play, mcds_slider])

# def plot_oxygen(self, frame, grid):
#     self.fig = plt.figure(figsize=(18.0,15))  # this strange figsize results in a ~square contour plot
#     #plt.subplot(grid[2, 0])
#     plt.subplot(grid[0, 2])
#     x = np.linspace(0, 10)
#     plt.plot(np.sin(x))
#     # ax1.plot(np.sin(x))

#---------------------------------------------------------------------------

    def plot_plots(self, frame):
        # global current_idx, axes_max, gFileId, field_index
        #self.fig = plt.figure(figsize=(18, 12))
        # self.fig = plt.figure(figsize=(16.8, 14))
        if (self.substrates_toggle.value):
            self.fig = plt.figure(figsize=(14, 15.6))
        else:
            # self.fig = plt.figure(figsize=(14, 14.0))
            # self.fig = plt.figure(figsize=(14, 15.6))
            self.fig = plt.figure(figsize=(12, 12))
        grid = plt.GridSpec(4, 3, wspace=0.10, hspace=0.2)  # (nrows, ncols)
        self.plot_substrate(frame, grid)
    def __init__(self):
        
        micron_units = Label('micron')   # use "option m" (Mac, for micro symbol)

        constWidth = '180px'
        tab_height = '500px'
        stepsize = 10

        #style = {'description_width': '250px'}
        style = {'description_width': '25%'}
        layout = {'width': '400px'}

        name_button_layout={'width':'25%'}
        widget_layout = {'width': '15%'}
        units_button_layout ={'width':'15%'}
        desc_button_layout={'width':'45%'}

        param_name1 = Button(description='number_of_cells', disabled=True, layout=name_button_layout)
        param_name1.style.button_color = 'lightgreen'

        self.number_of_cells = IntText(
          value=3,
          step=0.1,
          style=style, layout=widget_layout)

        param_name2 = Button(description='is_motile', disabled=True, layout=name_button_layout)
        param_name2.style.button_color = 'tan'

        self.is_motile = Checkbox(
          value=False,
          style=style, layout=widget_layout)

        param_name3 = Button(description='persistence_time', disabled=True, layout=name_button_layout)
        param_name3.style.button_color = 'lightgreen'

        self.persistence_time = FloatText(
          value=30,
          step=1,
          style=style, layout=widget_layout)

        param_name4 = Button(description='migration_speed', disabled=True, layout=name_button_layout)
        param_name4.style.button_color = 'tan'

        self.migration_speed = FloatText(
          value=2,
          step=0.1,
          style=style, layout=widget_layout)

        param_name5 = Button(description='migration_bias', disabled=True, layout=name_button_layout)
        param_name5.style.button_color = 'lightgreen'

        self.migration_bias = FloatText(
          value=0.8,
          step=0.1,
          style=style, layout=widget_layout)

        param_name6 = Button(description='bias_migration_angle', disabled=True, layout=name_button_layout)
        param_name6.style.button_color = 'tan'

        self.bias_migration_angle = FloatText(
          value=30,
          step=1,
          style=style, layout=widget_layout)

        units_button1 = Button(description='', disabled=True, layout=units_button_layout) 
        units_button1.style.button_color = 'lightgreen'
        units_button2 = Button(description='', disabled=True, layout=units_button_layout) 
        units_button2.style.button_color = 'tan'
        units_button3 = Button(description='min', disabled=True, layout=units_button_layout) 
        units_button3.style.button_color = 'lightgreen'
        units_button4 = Button(description='micron/min', disabled=True, layout=units_button_layout) 
        units_button4.style.button_color = 'tan'
        units_button5 = Button(description='', disabled=True, layout=units_button_layout) 
        units_button5.style.button_color = 'lightgreen'
        units_button6 = Button(description='degree', disabled=True, layout=units_button_layout) 
        units_button6.style.button_color = 'tan'

        desc_button1 = Button(description='number of cell tracks to simulate', disabled=True, layout=desc_button_layout) 
        desc_button1.style.button_color = 'lightgreen'
        desc_button2 = Button(description='true if cells are motile', disabled=True, layout=desc_button_layout) 
        desc_button2.style.button_color = 'tan'
        desc_button3 = Button(description='mean persistence time', disabled=True, layout=desc_button_layout) 
        desc_button3.style.button_color = 'lightgreen'
        desc_button4 = Button(description='migration speed', disabled=True, layout=desc_button_layout) 
        desc_button4.style.button_color = 'tan'
        desc_button5 = Button(description='migration bias parameter', disabled=True, layout=desc_button_layout) 
        desc_button5.style.button_color = 'lightgreen'
        desc_button6 = Button(description='migration bias angle respect to x-axis', disabled=True, layout=desc_button_layout) 
        desc_button6.style.button_color = 'tan'

        row1 = [param_name1, self.number_of_cells, units_button1, desc_button1] 
        row2 = [param_name2, self.is_motile, units_button2, desc_button2] 
        row3 = [param_name3, self.persistence_time, units_button3, desc_button3] 
        row4 = [param_name4, self.migration_speed, units_button4, desc_button4] 
        row5 = [param_name5, self.migration_bias, units_button5, desc_button5] 
        row6 = [param_name6, self.bias_migration_angle, units_button6, desc_button6] 

        box_layout = Layout(display='flex', flex_flow='row', align_items='stretch', width='100%')
        box1 = Box(children=row1, layout=box_layout)
        box2 = Box(children=row2, layout=box_layout)
        box3 = Box(children=row3, layout=box_layout)
        box4 = Box(children=row4, layout=box_layout)
        box5 = Box(children=row5, layout=box_layout)
        box6 = Box(children=row6, layout=box_layout)

        self.tab = VBox([
          box1,
          box2,
          box3,
          box4,
          box5,
          box6,
        ])
Exemple #12
0
    def __init__(self):

        #        micron_units = HTMLMath(value=r"$\mu M$")
        micron_units = Label(
            'micron')  # use "option m" (Mac, for micro symbol)
        #        micron_units = Label('microns')   # use "option m" (Mac, for micro symbol)

        constWidth = '180px'
        # tab_height = '400px'
        tab_height = '500px'
        #        tab_layout = Layout(width='900px',   # border='2px solid black',
        #        tab_layout = Layout(width='850px',   # border='2px solid black',
        #                            height=tab_height, overflow_y='scroll',)
        #        np_tab_layout = Layout(width='800px',  # border='2px solid black',
        #                               height='350px', overflow_y='scroll',)

        # my_domain = [0,0,-10, 2000,2000,10, 20,20,20]  # [x,y,zmin,  x,y,zmax, x,y,zdelta]
        #        label_domain = Label('Domain ($\mu M$):')
        label_domain = Label('Domain (micron):')
        stepsize = 10
        disable_domain = False
        self.xmin = FloatText(
            step=stepsize,
            # description='$X_{min}$',
            description='Xmin',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        self.ymin = FloatText(
            step=stepsize,
            description='Ymin',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        self.zmin = FloatText(
            step=stepsize,
            description='Zmin',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        self.xmax = FloatText(
            step=stepsize,
            description='Xmax',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        self.ymax = FloatText(
            step=stepsize,
            description='Ymax',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        self.zmax = FloatText(
            step=stepsize,
            description='Zmax',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        #            description='$Time_{max}$',
        self.tmax = BoundedFloatText(
            min=0.,
            max=100000000,
            step=stepsize,
            description='Max Time',
            layout=Layout(width=constWidth),
        )
        self.xdelta = BoundedFloatText(
            min=1.,
            description='dx',  # 'dx',  # Mac: opt-j for delta
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )

        self.ydelta = BoundedFloatText(
            min=1.,
            description='dy',
            disabled=True,
            layout=Layout(width=constWidth),
        )
        self.zdelta = BoundedFloatText(
            min=1.,
            description='dz',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )

        def xdelta_cb(b):
            self.ydelta.value = self.xdelta.value
            self.zdelta.value = 0.5 * (self.xdelta.value + self.ydelta.value)
            self.zmin.value = -0.5 * self.zdelta.value
            self.zmax.value = 0.5 * self.zdelta.value

        self.xdelta.observe(xdelta_cb)
        """
        self.tdelta = BoundedFloatText(
            min=0.01,
            description='$Time_{delta}$',
            layout=Layout(width=constWidth),
        )
        """
        """
        self.toggle2D = Checkbox(
            description='2-D',
            layout=Layout(width=constWidth),
        )
        def toggle2D_cb(b):
            if (self.toggle2D.value):
                #zmin.disabled = zmax.disabled = zdelta.disabled = True
                zmin.disabled = True
                zmax.disabled = True
                zdelta.disabled = True
            else:
                zmin.disabled = False
                zmax.disabled = False
                zdelta.disabled = False
            
        self.toggle2D.observe(toggle2D_cb)
        """

        x_row = HBox([self.xmin, self.xmax, self.xdelta])
        y_row = HBox([self.ymin, self.ymax, self.ydelta])
        z_row = HBox([self.zmin, self.zmax, self.zdelta])

        self.omp_threads = BoundedIntText(
            min=1,
            max=4,
            description='# threads',
            layout=Layout(width=constWidth),
        )

        # self.toggle_prng = Checkbox(
        #     description='Seed PRNG', style={'description_width': 'initial'},  # e.g. 'initial'  '120px'
        #     layout=Layout(width=constWidth),
        # )
        # self.prng_seed = BoundedIntText(
        #     min = 1,
        #     description='Seed',
        #     disabled=True,
        #     layout=Layout(width=constWidth),
        # )
        # def toggle_prng_cb(b):
        #     if (toggle_prng.value):
        #         self.prng_seed.disabled = False
        #     else:
        #         self.prng_seed.disabled = True

        # self.toggle_prng.observe(toggle_prng_cb)
        #prng_row = HBox([toggle_prng, prng_seed])

        self.toggle_svg = Checkbox(
            description='Cells',  # SVG
            layout=Layout(width='150px'))  # constWidth = '180px'
        # self.svg_t0 = BoundedFloatText (
        #     min=0,
        #     description='$T_0$',
        #     layout=Layout(width=constWidth),
        # )
        self.svg_interval = BoundedIntText(
            min=1,
            max=
            99999999,  # TODO: set max on all Bounded to avoid unwanted default
            description='every',
            layout=Layout(width='160px'),
        )
        self.mcds_interval = BoundedIntText(
            min=1,
            max=99999999,
            description='every',
            #            disabled=True,
            layout=Layout(width='160px'),
        )

        # don't let this be > mcds interval
        def svg_interval_cb(b):
            if (self.svg_interval.value > self.mcds_interval.value):
                self.svg_interval.value = self.mcds_interval.value

        self.svg_interval.observe(
            svg_interval_cb)  # BEWARE: when fill_gui, this sets value = 1 !

        # don't let this be < svg interval
        def mcds_interval_cb(b):
            if (self.mcds_interval.value < self.svg_interval.value):
                self.mcds_interval.value = self.svg_interval.value

        self.mcds_interval.observe(
            mcds_interval_cb)  # BEWARE: see warning above

        def toggle_svg_cb(b):
            if (self.toggle_svg.value):
                # self.svg_t0.disabled = False
                self.svg_interval.disabled = False
            else:
                # self.svg_t0.disabled = True
                self.svg_interval.disabled = True

        self.toggle_svg.observe(toggle_svg_cb)

        self.toggle_mcds = Checkbox(
            #     value=False,
            description='Subtrates',  # Full
            layout=Layout(width='180px'),
        )

        # self.mcds_t0 = FloatText(
        #     description='$T_0$',
        #     disabled=True,
        #     layout=Layout(width=constWidth),
        # )
        def toggle_mcds_cb(b):
            if (self.toggle_mcds.value):
                # self.mcds_t0.disabled = False #False
                self.mcds_interval.disabled = False
            else:
                # self.mcds_t0.disabled = True
                self.mcds_interval.disabled = True

        self.toggle_mcds.observe(toggle_mcds_cb)

        svg_mat_output_row = HBox([
            Label('Plots:'), self.toggle_svg,
            HBox([self.svg_interval, Label('min')]), self.toggle_mcds,
            HBox([self.mcds_interval, Label('min')])
        ])

        # to sync, do this
        # svg_mat_output_row = HBox( [Label('Plots:'), self.svg_interval, Label('min')])

        #write_config_row = HBox([write_config_button, write_config_file])
        #run_sim_row = HBox([run_button, run_command_str, kill_button])
        # run_sim_row = HBox([run_button, run_command_str])
        # run_sim_row = HBox([run_button.w])  # need ".w" for the custom RunCommand widget

        label_blankline = Label('')
        # toggle_2D_seed_row = HBox([toggle_prng, prng_seed])  # toggle2D

        box_layout = Layout(border='1px solid')
        #        domain_box = VBox([label_domain,x_row,y_row,z_row], layout=box_layout)
        domain_box = VBox([label_domain, x_row, y_row], layout=box_layout)
        self.tab = VBox([
            domain_box,
            #                         label_blankline,
            HBox([self.tmax, Label('min')]),
            self.omp_threads,
            svg_mat_output_row,
            #                         HBox([self.substrate[3], self.diffusion_coef[3], self.decay_rate[3] ]),
        ])  # output_dir, toggle_2D_seed_
Exemple #13
0
class ConfigTab(object):
    def __init__(self):

        #        micron_units = HTMLMath(value=r"$\mu M$")
        micron_units = Label(
            'micron')  # use "option m" (Mac, for micro symbol)
        #        micron_units = Label('microns')   # use "option m" (Mac, for micro symbol)

        constWidth = '180px'
        # tab_height = '400px'
        tab_height = '500px'
        #        tab_layout = Layout(width='900px',   # border='2px solid black',
        #        tab_layout = Layout(width='850px',   # border='2px solid black',
        #                            height=tab_height, overflow_y='scroll',)
        #        np_tab_layout = Layout(width='800px',  # border='2px solid black',
        #                               height='350px', overflow_y='scroll',)

        # my_domain = [0,0,-10, 2000,2000,10, 20,20,20]  # [x,y,zmin,  x,y,zmax, x,y,zdelta]
        #        label_domain = Label('Domain ($\mu M$):')
        label_domain = Label('Domain (micron):')
        stepsize = 10
        disable_domain = False
        self.xmin = FloatText(
            step=stepsize,
            # description='$X_{min}$',
            description='Xmin',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        self.ymin = FloatText(
            step=stepsize,
            description='Ymin',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        self.zmin = FloatText(
            step=stepsize,
            description='Zmin',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        self.xmax = FloatText(
            step=stepsize,
            description='Xmax',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        self.ymax = FloatText(
            step=stepsize,
            description='Ymax',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        self.zmax = FloatText(
            step=stepsize,
            description='Zmax',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )
        #            description='$Time_{max}$',
        self.tmax = BoundedFloatText(
            min=0.,
            max=100000000,
            step=stepsize,
            description='Max Time',
            layout=Layout(width=constWidth),
        )
        self.xdelta = BoundedFloatText(
            min=1.,
            description='dx',  # 'dx',  # Mac: opt-j for delta
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )

        self.ydelta = BoundedFloatText(
            min=1.,
            description='dy',
            disabled=True,
            layout=Layout(width=constWidth),
        )
        self.zdelta = BoundedFloatText(
            min=1.,
            description='dz',
            disabled=disable_domain,
            layout=Layout(width=constWidth),
        )

        def xdelta_cb(b):
            self.ydelta.value = self.xdelta.value
            self.zdelta.value = 0.5 * (self.xdelta.value + self.ydelta.value)
            self.zmin.value = -0.5 * self.zdelta.value
            self.zmax.value = 0.5 * self.zdelta.value

        self.xdelta.observe(xdelta_cb)
        """
        self.tdelta = BoundedFloatText(
            min=0.01,
            description='$Time_{delta}$',
            layout=Layout(width=constWidth),
        )
        """
        """
        self.toggle2D = Checkbox(
            description='2-D',
            layout=Layout(width=constWidth),
        )
        def toggle2D_cb(b):
            if (self.toggle2D.value):
                #zmin.disabled = zmax.disabled = zdelta.disabled = True
                zmin.disabled = True
                zmax.disabled = True
                zdelta.disabled = True
            else:
                zmin.disabled = False
                zmax.disabled = False
                zdelta.disabled = False
            
        self.toggle2D.observe(toggle2D_cb)
        """

        x_row = HBox([self.xmin, self.xmax, self.xdelta])
        y_row = HBox([self.ymin, self.ymax, self.ydelta])
        z_row = HBox([self.zmin, self.zmax, self.zdelta])

        self.omp_threads = BoundedIntText(
            min=1,
            max=4,
            description='# threads',
            layout=Layout(width=constWidth),
        )

        # self.toggle_prng = Checkbox(
        #     description='Seed PRNG', style={'description_width': 'initial'},  # e.g. 'initial'  '120px'
        #     layout=Layout(width=constWidth),
        # )
        # self.prng_seed = BoundedIntText(
        #     min = 1,
        #     description='Seed',
        #     disabled=True,
        #     layout=Layout(width=constWidth),
        # )
        # def toggle_prng_cb(b):
        #     if (toggle_prng.value):
        #         self.prng_seed.disabled = False
        #     else:
        #         self.prng_seed.disabled = True

        # self.toggle_prng.observe(toggle_prng_cb)
        #prng_row = HBox([toggle_prng, prng_seed])

        self.toggle_svg = Checkbox(
            description='Cells',  # SVG
            layout=Layout(width='150px'))  # constWidth = '180px'
        # self.svg_t0 = BoundedFloatText (
        #     min=0,
        #     description='$T_0$',
        #     layout=Layout(width=constWidth),
        # )
        self.svg_interval = BoundedIntText(
            min=1,
            max=
            99999999,  # TODO: set max on all Bounded to avoid unwanted default
            description='every',
            layout=Layout(width='160px'),
        )
        self.mcds_interval = BoundedIntText(
            min=1,
            max=99999999,
            description='every',
            #            disabled=True,
            layout=Layout(width='160px'),
        )

        # don't let this be > mcds interval
        def svg_interval_cb(b):
            if (self.svg_interval.value > self.mcds_interval.value):
                self.svg_interval.value = self.mcds_interval.value

        self.svg_interval.observe(
            svg_interval_cb)  # BEWARE: when fill_gui, this sets value = 1 !

        # don't let this be < svg interval
        def mcds_interval_cb(b):
            if (self.mcds_interval.value < self.svg_interval.value):
                self.mcds_interval.value = self.svg_interval.value

        self.mcds_interval.observe(
            mcds_interval_cb)  # BEWARE: see warning above

        def toggle_svg_cb(b):
            if (self.toggle_svg.value):
                # self.svg_t0.disabled = False
                self.svg_interval.disabled = False
            else:
                # self.svg_t0.disabled = True
                self.svg_interval.disabled = True

        self.toggle_svg.observe(toggle_svg_cb)

        self.toggle_mcds = Checkbox(
            #     value=False,
            description='Subtrates',  # Full
            layout=Layout(width='180px'),
        )

        # self.mcds_t0 = FloatText(
        #     description='$T_0$',
        #     disabled=True,
        #     layout=Layout(width=constWidth),
        # )
        def toggle_mcds_cb(b):
            if (self.toggle_mcds.value):
                # self.mcds_t0.disabled = False #False
                self.mcds_interval.disabled = False
            else:
                # self.mcds_t0.disabled = True
                self.mcds_interval.disabled = True

        self.toggle_mcds.observe(toggle_mcds_cb)

        svg_mat_output_row = HBox([
            Label('Plots:'), self.toggle_svg,
            HBox([self.svg_interval, Label('min')]), self.toggle_mcds,
            HBox([self.mcds_interval, Label('min')])
        ])

        # to sync, do this
        # svg_mat_output_row = HBox( [Label('Plots:'), self.svg_interval, Label('min')])

        #write_config_row = HBox([write_config_button, write_config_file])
        #run_sim_row = HBox([run_button, run_command_str, kill_button])
        # run_sim_row = HBox([run_button, run_command_str])
        # run_sim_row = HBox([run_button.w])  # need ".w" for the custom RunCommand widget

        label_blankline = Label('')
        # toggle_2D_seed_row = HBox([toggle_prng, prng_seed])  # toggle2D

        box_layout = Layout(border='1px solid')
        #        domain_box = VBox([label_domain,x_row,y_row,z_row], layout=box_layout)
        domain_box = VBox([label_domain, x_row, y_row], layout=box_layout)
        self.tab = VBox([
            domain_box,
            #                         label_blankline,
            HBox([self.tmax, Label('min')]),
            self.omp_threads,
            svg_mat_output_row,
            #                         HBox([self.substrate[3], self.diffusion_coef[3], self.decay_rate[3] ]),
        ])  # output_dir, toggle_2D_seed_
#                         ], layout=tab_layout)  # output_dir, toggle_2D_seed_

# Populate the GUI widgets with values from the XML

    def fill_gui(self, xml_root):
        self.xmin.value = float(xml_root.find(".//x_min").text)
        self.xmax.value = float(xml_root.find(".//x_max").text)
        self.xdelta.value = float(xml_root.find(".//dx").text)

        self.ymin.value = float(xml_root.find(".//y_min").text)
        self.ymax.value = float(xml_root.find(".//y_max").text)
        self.ydelta.value = float(xml_root.find(".//dy").text)

        self.zmin.value = float(xml_root.find(".//z_min").text)
        self.zmax.value = float(xml_root.find(".//z_max").text)
        self.zdelta.value = float(xml_root.find(".//dz").text)

        self.tmax.value = float(xml_root.find(".//max_time").text)

        self.omp_threads.value = int(xml_root.find(".//omp_num_threads").text)

        if xml_root.find(".//full_data//enable").text.lower() == 'true':
            self.toggle_mcds.value = True
        else:
            self.toggle_mcds.value = False
        self.mcds_interval.value = int(
            xml_root.find(".//full_data//interval").text)

        # NOTE: do this *after* filling the mcds_interval, directly above, due to the callback/constraints on them
        if xml_root.find(".//SVG//enable").text.lower() == 'true':
            self.toggle_svg.value = True
        else:
            self.toggle_svg.value = False
        self.svg_interval.value = int(xml_root.find(".//SVG//interval").text)

    # Read values from the GUI widgets and generate/write a new XML
    def fill_xml(self, xml_root):
        # print('config.py fill_xml() !!!!!')
        # TODO: verify template .xml file exists!

        # TODO: verify valid type (numeric) and range?
        xml_root.find(".//x_min").text = str(self.xmin.value)
        xml_root.find(".//x_max").text = str(self.xmax.value)
        xml_root.find(".//dx").text = str(self.xdelta.value)
        xml_root.find(".//y_min").text = str(self.ymin.value)
        xml_root.find(".//y_max").text = str(self.ymax.value)
        xml_root.find(".//dy").text = str(self.ydelta.value)
        xml_root.find(".//z_min").text = str(self.zmin.value)
        xml_root.find(".//z_max").text = str(self.zmax.value)
        xml_root.find(".//dz").text = str(self.zdelta.value)

        xml_root.find(".//max_time").text = str(self.tmax.value)

        xml_root.find(".//omp_num_threads").text = str(self.omp_threads.value)

        xml_root.find(".//SVG").find(".//enable").text = str(
            self.toggle_svg.value)
        xml_root.find(".//SVG").find(".//interval").text = str(
            self.svg_interval.value)
        xml_root.find(".//full_data").find(".//enable").text = str(
            self.toggle_mcds.value)
        xml_root.find(".//full_data").find(".//interval").text = str(
            self.mcds_interval.value)

        #    user_details = ET.SubElement(root, "user_details")
        #    ET.SubElement(user_details, "PhysiCell_settings", name="version").text = "devel-version"
        #    ET.SubElement(user_details, "domain")
        #    ET.SubElement(user_details, "xmin").text = "-100"

        #    tree = ET.ElementTree(root)
        #    tree.write(write_config_file.value)
        #    tree.write("test.xml")

        # TODO: verify can write to this filename
#        tree.write(write_config_file.value)

    def get_num_svg_frames(self):
        if (self.toggle_svg.value):
            return int(self.tmax.value / self.svg_interval.value)
        else:
            return 0

    def get_num_substrate_frames(self):
        if (self.toggle_mcds.value):
            return int(self.tmax.value / self.mcds_interval.value)
        else:
            return 0
    def interact(self, fig=None, axes=None, constraints=None, free_vars=None, caching=True, **kwargs):
        """
        Display an interactive plot where all free variables can be manipulated, with the plot updating accordingly.
        The rest of the variables is considered as dependent.

        :param fig: matplotlib figure to update, can be omitted if axes should be created automatically
        :param axes: matplotlib axes to draw on, can be omitted if axes should be created automatically
        :param constraints: optional sympy (nx1)-matrix of eqns which should be fullfilled (will be "solved" via fmin)
        :param free_vars: optional sympy (rx1)-matrix of symbols which are treated as independent for the constraints
        :param caching: True (default) or False. Determines whether fmin results are cached in a dictionary
        :param kwargs: ipywidgets specifications using the SymPy symbol string representations as keys
                       there are different syntax possibilities:
                       vis_object.interact(x=(xmin, xmax))
                       vis_object.interact(x=(xmin, xmax, step))
                       vis_object.interact(x=(xmin, xmax, step, inistial_value))
        """
        assert in_ipython_context, "Interactive mode only works in an IPython notebook" \
                                   "(maybe you need to install `ipywidgets`, see visualization_requirements.txt)"

        widget_dict = dict()

        if constraints is not None:
            assert isinstance(constraints, sp.MatrixBase)

            solve_constraints = True
            if free_vars is None:
                free_vars = []

            # allow for scalar free var (convenience)
            if isinstance(free_vars, sp.Basic):
                free_vars = [free_vars]

            # distinguish between free and dependent variables
            var_list = list(self.variables)
            free_var_indices = [var_list.index(v) for v in free_vars]
            dependent_var_indices = [var_list.index(v) for v in self.variables if v not in free_vars]
            # dependent_vars = [v for v in self.variables if v not in free_vars]

            n_vars = len(self.variables)

            # expression which will be minimized
            min_expr = constraints.T*constraints
            assert min_expr.shape == (1, 1)

            constraint_norm_func = expr_to_func(self.variables, min_expr[0])

            all_vars = np.zeros((n_vars,))

            def min_target_func(dep_var_vals, free_var_vals):
                """
                Target function for minimization,
                second argument is considered as a parameter
                """

                all_vars[dependent_var_indices] = dep_var_vals
                all_vars[free_var_indices] = free_var_vals

                return constraint_norm_func(*all_vars)

            cbox = Checkbox(value=False, description='solve constraints (fmin)', icon='check',
                            tooltip='solve constraints numerically via fmin')
            widget_dict["chk_solve_constraints"] = cbox

        else:
            solve_constraints = False

        for var in self.variables:
            var_str = repr(var)

            widget_dict[var_str] = self.make_slider_from_kwarg(var_str, kwargs)

        if fig is None or axes is None:
            fig, axes = self.create_default_axes()
            plt.close()

        is_initialized = False
        last_cbox_value = False
        fmin_cache = {}

        # last result for the dependet vars
        last_fmin_result = None

        # noinspection PyShadowingNames
        def interact_fun(**kwargs):
            nonlocal is_initialized
            nonlocal last_cbox_value
            nonlocal last_fmin_result
            widget_var_values = np.array([kwargs[repr(var_symbol)] for var_symbol in self.variables])

            cbox_solve_constraints = kwargs.get("chk_solve_constraints", False)

            print("widget_var_values:", widget_var_values, "cbox:", cbox_solve_constraints)

            if solve_constraints and cbox_solve_constraints:

                free_var_values = [kwargs[repr(var_symbol)] for var_symbol in free_vars]

                # initialize the dep_var_values form widgets if we have no result yet or if the checkbox was unchecked
                if last_fmin_result is None:
                    dep_var_values = widget_var_values[dependent_var_indices]
                    x0_dep_vars = dep_var_values
                else:
                    x0_dep_vars = last_fmin_result

                # dict lookup with the arguments of min_target

                # does not work because we never come to see this key again
                # key_tuple = (tuple(np.round(x0_dep_vars, decimals=5)), tuple(free_var_values))

                key_tuple = tuple(free_var_values)
                cache_content = fmin_cache.get(key_tuple)
                print("cache:", key_tuple, cache_content)
                if caching and cache_content is not None:
                    dep_var_values_result = cache_content
                else:

                    print("calling fmin with x0=", x0_dep_vars, "args=", free_var_values)
                    res = fmin(min_target_func, x0=x0_dep_vars, args=(free_var_values,), full_output=True)
                    dep_var_values_result, fopt, n_it, fcalls, warnflag = res

                    # fill the cache if we had these arguments for the first time (and no error occurred)
                    if caching and warnflag == 0:
                        fmin_cache[key_tuple] = dep_var_values_result

                last_fmin_result = dep_var_values_result

                all_vars[free_var_indices] = free_var_values
                all_vars[dependent_var_indices] = dep_var_values_result

                variables_values = all_vars * 1.0

                # print all coordinates, convert to list for easy copy-pasting (commas)
                print("all coordinates:", list(variables_values))
            else:
                # just use the values from the widgets
                variables_values = widget_var_values

                # reset the cache if checkbox is deactivated
                fmin_cache.clear()
                last_fmin_result = None

            if not is_initialized:
                self.plot_init(variables_values, axes)
                is_initialized = True

            last_cbox_value = cbox_solve_constraints

            self.plot_update(variables_values, axes)
            ip_display(fig)

        # TODO: Maybe return the control elements or something, so that they can be customized
        interact(interact_fun, **widget_dict)
Exemple #15
0
def create_multi_answer_widget(question, options):

    question_widget = create_question_widget(question)

    # Need to make my own checkbox out of checkbox + label because LaTeX not displaying nicely in checkbox built in description label

    labels = [
        widgets.HTML(value='<style>p{word-wrap: break-word}</style> <p>' +
                     option['answer'] + ' </p>') for option in options
    ]
    checkboxes = [
        HBox([
            Checkbox(value=False,
                     style={'description_width': 'initial'},
                     layout=Layout(width='30px')), lbl
        ],
             layout=Layout(display='flex',
                           flex_flow='row wrap',
                           align_items='stretch',
                           width='auto')) for lbl in labels
    ]

    # for each option, create a feedback box on the left and a checkbox on the right
    vertical = []
    for cb in checkboxes:
        new_hbox = widgets.HBox([Label(value=''), cb],
                                layout=Layout(display='flex',
                                              flex_flow='row wrap',
                                              align_items='stretch',
                                              width='auto'))
        #new_hbox.box_style = 'info'
        vertical.append(new_hbox)

    # vertically laid out options with feedback on the left
    option_widget = widgets.VBox(vertical,
                                 layout=Layout(display='flex',
                                               flex_flow='column',
                                               align_items='stretch',
                                               width='100%'))
    score_widget = create_score_widget('#4DD0E1')

    multi_answer = VBox([question_widget, option_widget, score_widget],
                        layout=Layout(display='flex',
                                      flex_flow='column',
                                      align_items='stretch',
                                      width='100%'))

    multi_answer.box_style = 'info'

    # compare checkbox value with original
    def check_answers(b):

        #run through each option, compare expected answer (checked/unchecked) with actual student answer
        num_options = len(options)
        incorrect = 0
        missing = 0

        option_widget = multi_answer.children[1]
        score_widget = multi_answer.children[2]

        submit_button = score_widget.children[0]

        for i in range(num_options):

            opt = option_widget.children[i]

            lbl = opt.children[0]
            cb = opt.children[1].children[0]

            actual_answer = cb.value
            expected_answer = options[i]['correct']

            # clear feedback before giving new feedback
            opt.layout = Layout(border=None)
            lbl.value = ''
            lbl.layout = Layout(width='100px')

            # red border + 'incorrect' for incorrectly checked
            # green border + 'correct!' for correctly checked
            if (expected_answer and actual_answer):
                opt.layout = Layout(border='1px solid #81C784')
                lbl.value = 'Correct!'
            if (expected_answer and not actual_answer):
                missing += 1
            if (not expected_answer and actual_answer):
                lbl.value = 'Incorrect'
                opt.layout = Layout(border='1px solid #e57373')
                incorrect += 1

        # update the score label
        if incorrect + missing == 0:
            # Success! So disable checkboxes
            for i in range(num_options):
                opt = option_widget.children[i]
                cb = opt.children[1].children[0]
                cb.disabled = True

            if submit_button.description == 'Submit':
                text = ''
            else:
                text = 'Now you got it!'

            generate_feedback(multi_answer,
                              score_widget,
                              style='success',
                              feedback_text=text,
                              show_show_answer_btn=False)
            submit_button.layout.disable = True
        else:
            #Some incorrect answers, write feedback so they can try again
            if missing > 0:
                text = 'You missed some correct options, try again!'
            else:
                text = ''

            generate_feedback(multi_answer,
                              score_widget,
                              style='danger',
                              feedback_text=text,
                              show_show_answer_btn=False)

    submit_button = score_widget.children[0]
    submit_button.on_click(check_answers)

    return (multi_answer)
Exemple #16
0
class DemoSetup(Setup):
    ui_th_std0 = FloatSlider(description="th0 [K]",
                             value=Setup.th_std0,
                             min=280,
                             max=300)

    @property
    def th_std0(self):
        return self.ui_th_std0.value

    ui_qv0 = FloatSlider(description="qv0 [g/kg]",
                         value=Setup.qv0 * 1000,
                         min=5,
                         max=10)

    @property
    def qv0(self):
        return self.ui_qv0.value / 1000

    ui_p0 = FloatSlider(description="p0 [hPa]",
                        value=Setup.p0 / 100,
                        min=900,
                        max=1100)

    @property
    def p0(self):
        return self.ui_p0.value * 100

    ui_kappa = FloatSlider(description="kappa [1]",
                           value=Setup.kappa,
                           min=0,
                           max=1.5)

    @property
    def kappa(self):
        return self.ui_kappa.value

    ui_amplitude = FloatSlider(description="amplitude [kg s^-1 m^-2]",
                               value=Setup.rho_w_max,
                               min=-1,
                               max=1)

    @property
    def amplitude(self):
        return self.ui_amplitude.value

    ui_nx = IntSlider(value=Setup.grid[0], min=10, max=100, description="nx")
    ui_nz = IntSlider(value=Setup.grid[1], min=10, max=100, description="nz")

    @property
    def grid(self):
        return self.ui_nx.value, self.ui_nz.value

    ui_dt = FloatSlider(value=Setup.dt,
                        min=.5,
                        max=5,
                        description="dt (Eulerian)")

    @property
    def dt(self):
        return self.ui_dt.value

    ui_n_steps = IntSlider(value=Setup.n_steps,
                           min=1800,
                           max=7200,
                           description="# steps")

    @property
    def n_steps(self):
        return self.ui_n_steps.value

    ui_condensation_rtol_x = IntSlider(value=np.log10(
        Setup.condensation_rtol_thd),
                                       min=-9,
                                       max=-3,
                                       description="log_10(rtol_x)")

    @property
    def condensation_rtol_x(self):
        return 10**self.ui_condensation_rtol_x.value

    ui_condensation_rtol_thd = IntSlider(value=np.log10(
        Setup.condensation_rtol_thd),
                                         min=-9,
                                         max=-3,
                                         description="log_10(rtol_thd)")

    @property
    def condensation_rtol_thd(self):
        return 10**self.ui_condensation_rtol_thd.value

    ui_adaptive = Checkbox(value=Setup.adaptive,
                           description='adaptive timestep')

    @property
    def adaptive(self):
        return self.ui_adaptive.value

    ui_condensation_coord = Dropdown(
        options=['volume', 'volume logarithm'],
        value=Setup.condensation_coord,
        description='condensational variable coordinate')

    @property
    def condensation_coord(self):
        return self.ui_condensation_coord.value

# TODO    ui_ept = Checkbox(value=Setup.enable_particle_temperatures, description="enable particle temperatures")

    ui_processes = [
        Checkbox(value=Setup.processes[key], description=key)
        for key in Setup.processes.keys()
    ]

    @property
    def processes(self):
        result = {}
        for checkbox in self.ui_processes:
            result[checkbox.description] = checkbox.value
        return result

    # @property
    # def enable_particle_temperatures(self):
    #     return self.ui_ept.value

    ui_sdpg = IntSlider(value=Setup.n_sd_per_gridbox,
                        description="n_sd/gridbox",
                        min=1,
                        max=1000)

    @property
    def n_sd_per_gridbox(self):
        return self.ui_sdpg.value

    fct_description = "MPDATA: flux-corrected transport option"
    tot_description = "MPDATA: third-order terms option"
    iga_description = "MPDATA: infinite gauge option"
    nit_description = "MPDATA: number of iterations (1=UPWIND)"
    ui_mpdata_options = [
        Checkbox(value=Setup.mpdata_fct, description=fct_description),
        Checkbox(value=Setup.mpdata_tot, description=tot_description),
        Checkbox(value=Setup.mpdata_iga, description=iga_description),
        IntSlider(value=Setup.mpdata_iters,
                  description=nit_description,
                  min=1,
                  max=5)
    ]

    @property
    def mpdata_tot(self):
        for widget in self.ui_mpdata_options:
            if widget.description == self.tot_description:
                return widget.value
        raise Exception()

    @property
    def mpdata_fct(self):
        for widget in self.ui_mpdata_options:
            if widget.description == self.fct_description:
                return widget.value
        raise Exception()

    @property
    def mpdata_iga(self):
        for widget in self.ui_mpdata_options:
            if widget.description == self.iga_description:
                return widget.value
        raise Exception()

    @property
    def mpdata_iters(self):
        for widget in self.ui_mpdata_options:
            if widget.description == self.nit_description:
                return widget.value
        raise Exception()

    def box(self):
        layout = Accordion(children=[
            VBox([
                self.ui_th_std0, self.ui_qv0, self.ui_p0, self.ui_kappa,
                self.ui_amplitude
            ]),
            VBox([
                *self.ui_processes
                #   , self.ui_ept  # TODO
            ]),
            VBox([
                self.ui_nx, self.ui_nz, self.ui_sdpg, self.ui_dt,
                self.ui_n_steps, self.ui_condensation_rtol_x,
                self.ui_condensation_rtol_thd, self.ui_adaptive,
                self.ui_condensation_coord, *self.ui_mpdata_options
            ]),
            #            VBox([])  # TODO
        ])
        layout.set_title(0, 'environment parameters')
        layout.set_title(1, 'processes')
        layout.set_title(2, 'discretisation')
        #        layout.set_title(3, 'parallelisation')  # TODO
        return layout
Exemple #17
0
    def build_options(self):
        grid = GridspecLayout(4, 2)
        options_map = {}
        style = {'description_width': '60%'}

        # plot_type
        plot_type = Dropdown(
            description='Plot:',
            options=['force', 'decision', 'both'],
            description_tooltip='Which plot to draw decision, force or both',
            style=style)
        options_map['plot_type'] = plot_type

        # background_data
        background_data = Dropdown(
            description='Background data:',
            options={
                'KMeans': 'kmeans',
                'Custom variable': 'custom'
            },
            value='kmeans',
            description_tooltip=
            'What background data will be used to sample from, when simulating "missing" feature\n'
            ' - KMeans: use KMeans to sample from provided dataset\n'
            ' - Custom variable: provide variable with instances to use',
            style=style)
        options_map['background_data'] = background_data

        # kmeans_count (only show when KMeans is chosen)
        kmeans_count = BoundedIntText(
            value=100,
            min=1,
            max=len(self.X_train),
            description='Count of KMeans centers:',
            description_tooltip=
            'Number of means to use when creating background data',
            style=style)
        options_map['kmeans_count'] = kmeans_count

        # data (only show when Custom variable is chosen)
        data = UpdatingCombobox(
            options_keys=self.globals_options,
            description='Background data variable:',
            options=list(self.globals_options),
            description_tooltip=
            'Variable with background data from which the "missing" features will be sampled',
            style=style)
        options_map['data'] = data

        # set up swap of options
        def swap_kmeans(change):
            if change['new'] == 'kmeans':
                data.lookup_in_kernel = False
                grid[1, 1] = kmeans_count
            else:
                data.lookup_in_kernel = True
                grid[1, 1] = data

        background_data.observe(swap_kmeans, names=['value'])

        # link
        link = Dropdown(
            description='Link:',
            options=['identity', 'logit'],
            value='identity',
            description_tooltip=
            'A generalized linear model link to connect the feature importance values '
            'to the model output.\n'
            'Since the feature importance values, phi, sum up to the model output, '
            'it often makes sense to connect them to the ouput with a link function '
            'where link(outout) = sum(phi).\n '
            'If the model output is a probability then the LogitLink link function makes '
            'the feature importance values have log-odds units.',
            style=style)
        options_map['link'] = link

        # nsamples
        nsamples = BoundedIntText(
            min=1,
            max=999999,
            value=2048,
            disabled=True,
            description='Model sample size:',
            description_tooltip=
            'Number of times to re-evaluate the model when explaining each prediction.\n'
            'More samples lead to lower variance estimates of the SHAP values.\n'
            'The "auto" setting uses nsamples = 2 * X.shape[1] + 2048.',
            style=style)
        options_map['nsamples'] = nsamples

        # auto_nsamples
        auto_nsamples = Checkbox(description='Auto choose model sample size',
                                 value=True,
                                 style={'description_width': 'auto'})
        options_map['auto_nsamples'] = auto_nsamples

        def disable_nsamples(change):
            nsamples.disabled = change['new']

        auto_nsamples.observe(disable_nsamples, names=['value'])

        # l1_reg
        l1_reg = Combobox(
            description='L1 regularization:',
            options=['auto', 'aic', 'bic'],
            value='auto',
            description_tooltip=
            'The l1 regularization to use for feature selection '
            '(the estimation procedure is based on a debiased lasso).\n'
            ' - The auto option currently uses "aic" when less that 20% '
            'of the possible sample space is enumerated, otherwise it uses no regularization.\n'
            ' - The "aic" and "bic" options use the AIC and BIC rules for regularization.\n'
            ' - Integer selects a fix number of top features.\n'
            ' - float directly sets the "alpha" parameter of the sklearn.linear_model.Lasso model '
            'used for feature selection',
            style=style)
        options_map['l1_reg'] = l1_reg

        # class_to_explain (only if classification)
        if self.is_classification:
            class_to_explain = Dropdown(
                description='Class to plot:',
                options={val: e
                         for e, val in enumerate(self.class_names)},
                description_tooltip=
                'For classification select a class for which the prediction will be explained',
                style=style)
            options_map['class_to_explain'] = class_to_explain
            grid[3, 1] = class_to_explain

        grid[0, 0] = plot_type
        grid[1, 0] = background_data
        grid[1, 1] = kmeans_count
        grid[0, 1] = link
        grid[2, 0] = nsamples
        grid[2, 1] = auto_nsamples
        grid[3, 0] = l1_reg

        return options_map, grid
Exemple #18
0
    def make_config(self):
        layout = Layout()
        style = {"description_width": "initial"}
        checkbox1 = Checkbox(description="Show Targets", value=self.net.config["show_targets"],
                             layout=layout, style=style)
        checkbox1.observe(lambda change: self.set_attr(self.net.config, "show_targets", change["new"]), names='value')
        checkbox2 = Checkbox(description="Errors", value=self.net.config["show_errors"],
                             layout=layout, style=style)
        checkbox2.observe(lambda change: self.set_attr(self.net.config, "show_errors", change["new"]), names='value')

        hspace = IntText(value=self.net.config["hspace"], description="Horizontal space between banks:",
                         style=style, layout=layout)
        hspace.observe(lambda change: self.set_attr(self.net.config, "hspace", change["new"]), names='value')
        vspace = IntText(value=self.net.config["vspace"], description="Vertical space between layers:",
                         style=style, layout=layout)
        vspace.observe(lambda change: self.set_attr(self.net.config, "vspace", change["new"]), names='value')
        self.feature_bank = Select(description="Details:", value=self.net.config["dashboard.features.bank"],
                              options=[""] + [layer.name for layer in self.net.layers],
                              rows=1)
        self.feature_bank.observe(self.regenerate, names='value')
        self.control_select = Select(
            options=['Test', 'Train'],
            value=self.net.config["dashboard.dataset"],
            description='Dataset:',
            rows=1
        )
        self.control_select.observe(self.change_select, names='value')
        column1 = [self.control_select,
                   self.zoom_slider,
                   hspace,
                   vspace,
                   HBox([checkbox1, checkbox2]),
                   self.feature_bank,
                   self.feature_columns,
                   self.feature_scale
        ]
        ## Make layer selectable, and update-able:
        column2 = []
        layer = self.net.layers[-1]
        self.layer_select = Select(description="Layer:", value=layer.name,
                                   options=[layer.name for layer in
                                            self.net.layers],
                                   rows=1)
        self.layer_select.observe(self.update_layer_selection, names='value')
        column2.append(self.layer_select)
        self.layer_visible_checkbox = Checkbox(description="Visible", value=layer.visible, layout=layout)
        self.layer_visible_checkbox.observe(self.update_layer, names='value')
        column2.append(self.layer_visible_checkbox)
        self.layer_colormap = Select(description="Colormap:",
                                     options=[""] + AVAILABLE_COLORMAPS,
                                     value=layer.colormap if layer.colormap is not None else "", layout=layout, rows=1)
        self.layer_colormap_image = HTML(value="""<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap)))
        self.layer_colormap.observe(self.update_layer, names='value')
        column2.append(self.layer_colormap)
        column2.append(self.layer_colormap_image)
        ## get dynamic minmax; if you change it it will set it in layer as override:
        minmax = layer.get_act_minmax()
        self.layer_mindim = FloatText(description="Leftmost color maps to:", value=minmax[0], style=style)
        self.layer_maxdim = FloatText(description="Rightmost color maps to:", value=minmax[1], style=style)
        self.layer_mindim.observe(self.update_layer, names='value')
        self.layer_maxdim.observe(self.update_layer, names='value')
        column2.append(self.layer_mindim)
        column2.append(self.layer_maxdim)
        output_shape = layer.get_output_shape()
        self.layer_feature = IntText(value=layer.feature, description="Feature to show:", style=style)
        self.svg_rotate = Checkbox(description="Rotate", value=layer.visible, layout=layout)
        self.layer_feature.observe(self.update_layer, names='value')
        column2.append(self.layer_feature)
        self.svg_rotate = Checkbox(description="Rotate network",
                                   value=self.net.config["svg_rotate"],
                                   style={"description_width": 'initial'},
                                   layout=Layout(width="52%"))
        self.svg_rotate.observe(lambda change: self.set_attr(self.net.config, "svg_rotate", change["new"]), names='value')
        self.save_config_button = Button(icon="save", layout=Layout(width="10%"))
        self.save_config_button.on_click(self.save_config)
        column2.append(HBox([self.svg_rotate, self.save_config_button]))
        config_children = HBox([VBox(column1, layout=Layout(width="100%")),
                                VBox(column2, layout=Layout(width="100%"))])
        accordion = Accordion(children=[config_children])
        accordion.set_title(0, self.net.name)
        accordion.selected_index = None
        return accordion
)  # Here we call the Optimization function that returns the initial optimal weights.
# ************************************************************************************* GUI portion of the code that contains various labels,checkboxes etc.  ***********************

input_header = HBox([
    Label(value='Ticker', layout=Layout(width='120px', height='22px')),
    Label(value='Name of Asset', layout=Layout(width='120px', height='22px')),
    Label(value='Weight', layout=Layout(width='120px', height='22px'))
])
list_sec_input = [input_header]
lst_name = list(dict_settings['security'].keys())
lst_ticker = list(dict_settings['security'].values())
lst_weight = dict_settings['weight']

# Checkbox that determines the mktcap option. Places in python object's 'check_usemktcap' value field a true or false value
check_usemktcap = Checkbox(description='Use Market Cap as Weight',
                           value=dict_settings['usemktcap'],
                           layout=Layout(min_width='15px'),
                           style={'description_width': 'initial'})

label_usemktcap = Label(value=' ', layout={'height': '22px'})  # MktCap Label
label_usemktcap2 = Label(value='(not recommended when using ETF as proxy)',
                         layout=Layout(min_width='300px'))  # MktCap Label

# Option to load your own internal portfolio different from the original input.

#port_dict = {x['name']: x['id'].split(':')[2].replace('-','U') + '-' + x['id'].split(':')[3] for x in bqport.list_portfolios()}
#port_dict = {x['name']: x['id'].split(':')[2].replace('-','U') + '-' + x['id'].split(':')[3] for x in bq.port.list_portfolios()}
#load_button = Button(description='Load members')
#portfolio_dropdown = Dropdown(description='Portfolio:',options=sorted(set(port_dict.keys())))
#load_members_hbox = HBox([portfolio_dropdown, load_button])

for n in range(num_avail_ticker):
Exemple #20
0
    def __init__(self):

        self.output_dir = '.'
        #        self.output_dir = 'tmpdir'

        # self.fig = plt.figure(figsize=(7.2,6))  # this strange figsize results in a ~square contour plot

        self.use_defaults = True

        self.svg_xmin = 0
        self.svg_xrange = 2000
        self.xmin = -1000.
        self.xmax = 1000.
        self.ymin = -1000.
        self.ymax = 1000.
        self.x_range = 2000.
        self.y_range = 2000.

        self.show_nucleus = 0
        self.show_edge = False

        # initial value
        self.field_index = 4
        # self.field_index = self.mcds_field.value + 4

        # define dummy size of mesh (set in the tool's primary module)
        self.numx = 0
        self.numy = 0

        tab_height = '600px'
        tab_height = '500px'
        constWidth = '180px'
        constWidth2 = '150px'
        tab_layout = Layout(
            width='900px',  # border='2px solid black',
            height=tab_height,
        )  #overflow_y='scroll')

        max_frames = 1
        # self.mcds_plot = interactive(self.plot_substrate, frame=(0, max_frames), continuous_update=False)
        self.mcds_plot = interactive(self.plot_plots,
                                     frame=(0, max_frames),
                                     continuous_update=False)

        # "plot_size" controls the size of the tab height, not the plot (rf. figsize for that)
        # NOTE: the Substrates Plot tab has an extra row of widgets at the top of it (cf. Cell Plots tab)
        svg_plot_size = '700px'
        svg_plot_size = '600px'
        svg_plot_size = '700px'
        svg_plot_size = '900px'
        self.mcds_plot.layout.width = svg_plot_size
        self.mcds_plot.layout.height = svg_plot_size

        self.fontsize = 20

        self.max_frames = BoundedIntText(
            min=0,
            max=99999,
            value=max_frames,
            description='Max',
            layout=Layout(width='160px'),
        )
        self.max_frames.observe(self.update_max_frames)

        self.field_min_max = {'dummy': [0., 1.]}
        # hacky I know, but make a dict that's got (key,value) reversed from the dict in the Dropdown below
        self.field_dict = {0: 'dummy'}

        self.mcds_field = Dropdown(
            options={'dummy': 0},
            value=0,
            #     description='Field',
            layout=Layout(width=constWidth))
        # print("substrate __init__: self.mcds_field.value=",self.mcds_field.value)
        #        self.mcds_field.observe(self.mcds_field_cb)
        self.mcds_field.observe(self.mcds_field_changed_cb)

        # self.field_cmap = Text(
        #     value='viridis',
        #     description='Colormap',
        #     disabled=True,
        #     layout=Layout(width=constWidth),
        # )
        self.field_cmap = Dropdown(
            options=['viridis', 'jet', 'YlOrRd'],
            value='YlOrRd',
            #     description='Field',
            layout=Layout(width=constWidth))
        #self.field_cmap.observe(self.plot_substrate)
        #        self.field_cmap.observe(self.plot_substrate)
        self.field_cmap.observe(self.mcds_field_cb)

        self.cmap_fixed = Checkbox(
            description='Fix',
            disabled=False,
            #           layout=Layout(width=constWidth2),
        )

        self.save_min_max = Button(
            description='Save',  #style={'description_width': 'initial'},
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Save min/max for this substrate',
            disabled=True,
            layout=Layout(width='90px'))

        def save_min_max_cb(b):
            #            field_name = self.mcds_field.options[]
            #            field_name = next(key for key, value in self.mcds_field.options.items() if value == self.mcds_field.value)
            field_name = self.field_dict[self.mcds_field.value]
            #            print(field_name)
            #            self.field_min_max = {'oxygen': [0., 30.], 'glucose': [0., 1.], 'H+ ions': [0., 1.], 'ECM': [0., 1.], 'NP1': [0., 1.], 'NP2': [0., 1.]}
            self.field_min_max[field_name][0] = self.cmap_min.value
            self.field_min_max[field_name][1] = self.cmap_max.value
#            print(self.field_min_max)

        self.save_min_max.on_click(save_min_max_cb)

        self.cmap_min = FloatText(
            description='Min',
            value=0,
            step=0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_min.observe(self.mcds_field_cb)

        self.cmap_max = FloatText(
            description='Max',
            value=38,
            step=0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_max.observe(self.mcds_field_cb)

        def cmap_fixed_cb(b):
            if (self.cmap_fixed.value):
                self.cmap_min.disabled = False
                self.cmap_max.disabled = False
                self.save_min_max.disabled = False
            else:
                self.cmap_min.disabled = True
                self.cmap_max.disabled = True
                self.save_min_max.disabled = True
#            self.mcds_field_cb()

        self.cmap_fixed.observe(cmap_fixed_cb)

        field_cmap_row2 = HBox([self.field_cmap, self.cmap_fixed])

        #        field_cmap_row3 = HBox([self.save_min_max, self.cmap_min, self.cmap_max])
        items_auto = [
            self.save_min_max,  #layout=Layout(flex='3 1 auto', width='auto'),
            self.cmap_min,
            self.cmap_max,
        ]
        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='80%')
        field_cmap_row3 = Box(children=items_auto, layout=box_layout)

        #---------------------
        self.cell_edges_toggle = Checkbox(
            description='edges',
            disabled=False,
            value=False,
            #           layout=Layout(width=constWidth2),
        )

        def cell_edges_toggle_cb(b):
            # self.update()
            if (self.cell_edges_toggle.value):
                self.show_edge = True
            else:
                self.show_edge = False
            self.mcds_plot.update()

        self.cell_edges_toggle.observe(cell_edges_toggle_cb)

        self.cells_toggle = Checkbox(
            description='Cells',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def cells_toggle_cb(b):
            # self.update()
            self.mcds_plot.update()
            if (self.cells_toggle.value):
                self.cell_edges_toggle.disabled = False
            else:
                self.cell_edges_toggle.disabled = True

        self.cells_toggle.observe(cells_toggle_cb)

        #---------------------
        self.substrates_toggle = Checkbox(
            description='Substrates',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def substrates_toggle_cb(b):
            if (self.substrates_toggle.value):  # seems bass-ackwards
                self.cmap_fixed.disabled = False
                self.cmap_min.disabled = False
                self.cmap_max.disabled = False
                self.mcds_field.disabled = False
                self.field_cmap.disabled = False
            else:
                self.cmap_fixed.disabled = True
                self.cmap_min.disabled = True
                self.cmap_max.disabled = True
                self.mcds_field.disabled = True
                self.field_cmap.disabled = True

        self.substrates_toggle.observe(substrates_toggle_cb)

        self.grid_toggle = Checkbox(
            description='grid',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def grid_toggle_cb(b):
            # self.update()
            self.mcds_plot.update()

        self.grid_toggle.observe(grid_toggle_cb)

        #        field_cmap_row3 = Box([self.save_min_max, self.cmap_min, self.cmap_max])

        # mcds_tab = widgets.VBox([mcds_dir, mcds_plot, mcds_play], layout=tab_layout)
        # mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3, self.max_frames])  # mcds_dir
        #        mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3,])  # mcds_dir

        #        self.tab = HBox([mcds_params, self.mcds_plot], layout=tab_layout)
        #        self.tab = HBox([mcds_params, self.mcds_plot])

        help_label = Label('select slider: drag or left/right arrows')
        # row1 = Box([help_label, Box( [self.max_frames, self.mcds_field, self.field_cmap], layout=Layout(border='0px solid black',
        row1a = Box([self.max_frames, self.mcds_field, self.field_cmap],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        row1b = Box([self.cells_toggle, self.cell_edges_toggle],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        row1 = HBox([row1a, Label('.....'), row1b])

        row2a = Box([self.cmap_fixed, self.cmap_min, self.cmap_max],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        # row2b = Box( [self.substrates_toggle, self.grid_toggle], layout=Layout(border='1px solid black',
        row2b = Box([
            self.substrates_toggle,
        ],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        # row2 = HBox( [row2a, self.substrates_toggle, self.grid_toggle])
        row2 = HBox([row2a, Label('.....'), row2b])

        if (hublib_flag):
            self.download_button = Download('mcds.zip',
                                            style='warning',
                                            icon='cloud-download',
                                            tooltip='Download data',
                                            cb=self.download_cb)

            self.download_svg_button = Download(
                'svg.zip',
                style='warning',
                icon='cloud-download',
                tooltip='You need to allow pop-ups in your browser',
                cb=self.download_svg_cb)
            download_row = HBox([
                self.download_button.w, self.download_svg_button.w,
                Label("Download all cell plots (browser must allow pop-ups).")
            ])

            #        self.tab = VBox([row1, row2, self.mcds_plot])
            # self.tab = VBox([row1, row2, self.mcds_plot, download_row])
            # box_layout = Layout(border='0px solid')
            controls_box = VBox([row1,
                                 row2])  # ,width='50%', layout=box_layout)
            self.tab = VBox([controls_box, self.mcds_plot, download_row])
        else:
            # self.tab = VBox([row1, row2])
            self.tab = VBox([row1, row2, self.mcds_plot])
Exemple #21
0
class SubstrateTab(object):

    def __init__(self):
        
        self.output_dir = '.'
#        self.output_dir = 'tmpdir'

        # self.fig = plt.figure(figsize=(7.2,6))  # this strange figsize results in a ~square contour plot

        # initial value
        self.field_index = 4
        # self.field_index = self.mcds_field.value + 4

        tab_height = '500px'
        constWidth = '180px'
        constWidth2 = '150px'
        tab_layout = Layout(width='900px',   # border='2px solid black',
                            height=tab_height, ) #overflow_y='scroll')

        max_frames = 1   
        self.mcds_plot = interactive(self.plot_substrate, frame=(0, max_frames), continuous_update=False)  
        svg_plot_size = '700px'
        self.mcds_plot.layout.width = svg_plot_size
        self.mcds_plot.layout.height = svg_plot_size

        self.max_frames = BoundedIntText(
            min=0, max=99999, value=max_frames,
            description='Max',
           layout=Layout(width='160px'),
        )
        self.max_frames.observe(self.update_max_frames)

        self.field_min_max = {'dummy': [0., 1.]}
        # hacky I know, but make a dict that's got (key,value) reversed from the dict in the Dropdown below
        self.field_dict = {0:'dummy'}

        self.mcds_field = Dropdown(
            options={'dummy': 0},
            value=0,
            #     description='Field',
           layout=Layout(width=constWidth)
        )
        # print("substrate __init__: self.mcds_field.value=",self.mcds_field.value)
#        self.mcds_field.observe(self.mcds_field_cb)
        self.mcds_field.observe(self.mcds_field_changed_cb)

        # self.field_cmap = Text(
        #     value='viridis',
        #     description='Colormap',
        #     disabled=True,
        #     layout=Layout(width=constWidth),
        # )
        self.field_cmap = Dropdown(
            options=['viridis', 'jet', 'YlOrRd'],
            value='viridis',
            #     description='Field',
           layout=Layout(width=constWidth)
        )
        #self.field_cmap.observe(self.plot_substrate)
#        self.field_cmap.observe(self.plot_substrate)
        self.field_cmap.observe(self.mcds_field_cb)

        self.cmap_fixed = Checkbox(
            description='Fix',
            disabled=False,
#           layout=Layout(width=constWidth2),
        )

        self.save_min_max= Button(
            description='Save', #style={'description_width': 'initial'},
            button_style='success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Save min/max for this substrate',
            disabled=True,
           layout=Layout(width='90px')
        )

        def save_min_max_cb(b):
#            field_name = self.mcds_field.options[]
#            field_name = next(key for key, value in self.mcds_field.options.items() if value == self.mcds_field.value)
            field_name = self.field_dict[self.mcds_field.value]
#            print(field_name)
#            self.field_min_max = {'oxygen': [0., 30.], 'glucose': [0., 1.], 'H+ ions': [0., 1.], 'ECM': [0., 1.], 'NP1': [0., 1.], 'NP2': [0., 1.]}
            self.field_min_max[field_name][0] = self.cmap_min.value
            self.field_min_max[field_name][1] = self.cmap_max.value
#            print(self.field_min_max)

        self.save_min_max.on_click(save_min_max_cb)

        self.cmap_min = FloatText(
            description='Min',
            value=0,
            step = 0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_min.observe(self.mcds_field_cb)

        self.cmap_max = FloatText(
            description='Max',
            value=38,
            step = 0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_max.observe(self.mcds_field_cb)

        def cmap_fixed_cb(b):
            if (self.cmap_fixed.value):
                self.cmap_min.disabled = False
                self.cmap_max.disabled = False
                self.save_min_max.disabled = False
            else:
                self.cmap_min.disabled = True
                self.cmap_max.disabled = True
                self.save_min_max.disabled = True
#            self.mcds_field_cb()

        self.cmap_fixed.observe(cmap_fixed_cb)

        field_cmap_row2 = HBox([self.field_cmap, self.cmap_fixed])

#        field_cmap_row3 = HBox([self.save_min_max, self.cmap_min, self.cmap_max])
        items_auto = [
            self.save_min_max, #layout=Layout(flex='3 1 auto', width='auto'),
            self.cmap_min, 
            self.cmap_max,  
         ]
        box_layout = Layout(display='flex',
                    flex_flow='row',
                    align_items='stretch',
                    width='80%')
        field_cmap_row3 = Box(children=items_auto, layout=box_layout)

#        field_cmap_row3 = Box([self.save_min_max, self.cmap_min, self.cmap_max])

        # mcds_tab = widgets.VBox([mcds_dir, mcds_plot, mcds_play], layout=tab_layout)
        mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3, self.max_frames])  # mcds_dir
#        mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3,])  # mcds_dir

#        self.tab = HBox([mcds_params, self.mcds_plot], layout=tab_layout)
#        self.tab = HBox([mcds_params, self.mcds_plot])

        help_label = Label('select slider: drag or left/right arrows')
        row1 = Box([help_label, Box( [self.max_frames, self.mcds_field, self.field_cmap], layout=Layout(border='0px solid black',
                            width='50%',
                            height='',
                            align_items='stretch',
                            flex_direction='row',
                            display='flex'))] )
        row2 = Box([self.cmap_fixed, self.cmap_min, self.cmap_max], layout=Layout(border='0px solid black',
                            width='50%',
                            height='',
                            align_items='stretch',
                            flex_direction='row',
                            display='flex'))
        self.download_button = Download('mcds.zip', style='warning', icon='cloud-download', 
                                            tooltip='Download data', cb=self.download_cb)
        download_row = HBox([self.download_button.w, Label("Download all substrate data (browser must allow pop-ups).")])

#        self.tab = VBox([row1, row2, self.mcds_plot])
        self.tab = VBox([row1, row2, self.mcds_plot, download_row])

    #---------------------------------------------------
    def update_dropdown_fields(self, data_dir):
        # print('update_dropdown_fields called --------')
        self.output_dir = data_dir
        tree = None
        try:
            fname = os.path.join(self.output_dir, "initial.xml")
            tree = ET.parse(fname)
#            return
        except:
            print("Cannot open ",fname," to get names of substrate fields.")
            return

        xml_root = tree.getroot()
        self.field_min_max = {}
        self.field_dict = {}
        dropdown_options = {}
        uep = xml_root.find('.//variables')
        comment_str = ""
        field_idx = 0
        if (uep):
            for elm in uep.findall('variable'):
                # print("-----> ",elm.attrib['name'])
                self.field_min_max[elm.attrib['name']] = [0., 1.]
                self.field_dict[field_idx] = elm.attrib['name']
                dropdown_options[elm.attrib['name']] = field_idx
                field_idx += 1

#        constWidth = '180px'
        # print('options=',dropdown_options)
        self.mcds_field.value=0
        self.mcds_field.options=dropdown_options
#         self.mcds_field = Dropdown(
# #            options={'oxygen': 0, 'glucose': 1},
#             options=dropdown_options,
#             value=0,
#             #     description='Field',
#            layout=Layout(width=constWidth)
#         )

    def update_max_frames_expected(self, value):  # called when beginning an interactive Run
        self.max_frames.value = value  # assumes naming scheme: "snapshot%08d.svg"
        self.mcds_plot.children[0].max = self.max_frames.value

#    def update(self, rdir):
    def update(self, rdir=''):
        # with debug_view:
        #     print("substrates: update rdir=", rdir)        

        if rdir:
            self.output_dir = rdir

        all_files = sorted(glob.glob(os.path.join(self.output_dir, 'output*.xml')))
        if len(all_files) > 0:
            last_file = all_files[-1]
            self.max_frames.value = int(last_file[-12:-4])  # assumes naming scheme: "snapshot%08d.svg"

        # with debug_view:
        #     print("substrates: added %s files" % len(all_files))


        # self.output_dir = rdir
        # if rdir == '':
        #     # self.max_frames.value = 0
        #     tmpdir = os.path.abspath('tmpdir')
        #     self.output_dir = tmpdir
        #     all_files = sorted(glob.glob(os.path.join(tmpdir, 'output*.xml')))
        #     if len(all_files) > 0:
        #         last_file = all_files[-1]
        #         self.max_frames.value = int(last_file[-12:-4])  # assumes naming scheme: "output%08d.xml"
        #         self.mcds_plot.update()
        #     return

        # all_files = sorted(glob.glob(os.path.join(rdir, 'output*.xml')))
        # if len(all_files) > 0:
        #     last_file = all_files[-1]
        #     self.max_frames.value = int(last_file[-12:-4])  # assumes naming scheme: "output%08d.xml"
        #     self.mcds_plot.update()

    def download_cb(self):
        file_xml = os.path.join(self.output_dir, '*.xml')
        file_mat = os.path.join(self.output_dir, '*.mat')
        # print('zip up all ',file_str)
        with zipfile.ZipFile('mcds.zip', 'w') as myzip:
            for f in glob.glob(file_xml):
                myzip.write(f, os.path.basename(f)) # 2nd arg avoids full filename path in the archive
            for f in glob.glob(file_mat):
                myzip.write(f, os.path.basename(f))

    def update_max_frames(self,_b):
        self.mcds_plot.children[0].max = self.max_frames.value

    def mcds_field_changed_cb(self, b):
        # print("mcds_field_changed_cb: self.mcds_field.value=",self.mcds_field.value)
        if (self.mcds_field.value == None):
            return
        self.field_index = self.mcds_field.value + 4

        field_name = self.field_dict[self.mcds_field.value]
#        print('mcds_field_cb: '+field_name)
        self.cmap_min.value = self.field_min_max[field_name][0]
        self.cmap_max.value = self.field_min_max[field_name][1]
        self.mcds_plot.update()

    def mcds_field_cb(self, b):
        #self.field_index = self.mcds_field.value
#        self.field_index = self.mcds_field.options.index(self.mcds_field.value) + 4
#        self.field_index = self.mcds_field.options[self.mcds_field.value]
        self.field_index = self.mcds_field.value + 4

        # field_name = self.mcds_field.options[self.mcds_field.value]
        # self.cmap_min.value = self.field_min_max[field_name][0]  # oxygen, etc
        # self.cmap_max.value = self.field_min_max[field_name][1]  # oxygen, etc

#        self.field_index = self.mcds_field.value + 4

#        print('field_index=',self.field_index)
        self.mcds_plot.update()

    def plot_substrate(self, frame):
        # global current_idx, axes_max, gFileId, field_index
        fname = "output%08d_microenvironment0.mat" % frame
        xml_fname = "output%08d.xml" % frame
        # fullname = output_dir_str + fname

#        fullname = fname
        full_fname = os.path.join(self.output_dir, fname)
        full_xml_fname = os.path.join(self.output_dir, xml_fname)
#        self.output_dir = '.'

#        if not os.path.isfile(fullname):
        if not os.path.isfile(full_fname):
#            print("File does not exist: ", full_fname)
            print("Once output files are generated, click the slider.")  # No:  output00000000_microenvironment0.mat

            return

#        tree = ET.parse(xml_fname)
        tree = ET.parse(full_xml_fname)
        xml_root = tree.getroot()
        mins= round(int(float(xml_root.find(".//current_time").text)))  # TODO: check units = mins
        hrs = int(mins/60)
        days = int(hrs/24)
        title_str = '%dd, %dh, %dm' % (int(days),(hrs%24), mins - (hrs*60))


        info_dict = {}
#        scipy.io.loadmat(fullname, info_dict)
        scipy.io.loadmat(full_fname, info_dict)
        M = info_dict['multiscale_microenvironment']
        #     global_field_index = int(mcds_field.value)
        #     print('plot_substrate: field_index =',field_index)
        f = M[self.field_index, :]   # 4=tumor cells field, 5=blood vessel density, 6=growth substrate
        # plt.clf()
        # my_plot = plt.imshow(f.reshape(400,400), cmap='jet', extent=[0,20, 0,20])
    
        self.fig = plt.figure(figsize=(7.2,6))  # this strange figsize results in a ~square contour plot
        #     fig.set_tight_layout(True)
        #     ax = plt.axes([0, 0.05, 0.9, 0.9 ]) #left, bottom, width, height
        #     ax = plt.axes([0, 0.0, 1, 1 ])
        #     cmap = plt.cm.viridis # Blues, YlOrBr, ...
        #     im = ax.imshow(f.reshape(100,100), interpolation='nearest', cmap=cmap, extent=[0,20, 0,20])
        #     ax.grid(False)

        N = int(math.sqrt(len(M[0,:])))
        grid2D = M[0, :].reshape(N,N)
        xvec = grid2D[0, :]

        num_contours = 15
#        levels = MaxNLocator(nbins=10).tick_values(vmin, vmax)
        levels = MaxNLocator(nbins=num_contours).tick_values(self.cmap_min.value, self.cmap_max.value)
        if (self.cmap_fixed.value):
            my_plot = plt.contourf(xvec, xvec, M[self.field_index, :].reshape(N,N), levels=levels, extend='both', cmap=self.field_cmap.value)
        else:    
#        my_plot = plt.contourf(xvec, xvec, M[self.field_index, :].reshape(N,N), num_contours, cmap=self.field_cmap.value)
            my_plot = plt.contourf(xvec, xvec, M[self.field_index, :].reshape(N,N), num_contours, cmap=self.field_cmap.value)

        plt.title(title_str)
        plt.colorbar(my_plot)
        axes_min = 0
        axes_max = 2000
Exemple #22
0
def time_series(path):
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    from datetime import timedelta
    import pandas as pd
    import json
    import glob

    confvalues = config.read()
    inst = confvalues['set']['institution']
    file_info = glob.glob(f"{path}*_information.json")[0]

    with open(file_info, 'r') as f:
        info_data = json.loads(f.read())
    pid = info_data['ogc_fid'][0]
    crop_name = info_data['cropname'][0]
    area = info_data['area'][0]
    figure_dpi = 50

    def plot_ts_s2(cloud):
        file_ts = glob.glob(f"{path}*_time_series_s2.csv")[0]
        df = pd.read_csv(file_ts, index_col=0)

        df['date'] = pd.to_datetime(df['date_part'], unit='s')
        start_date = df.iloc[0]['date'].date()
        end_date = df.iloc[-1]['date'].date()
        print(f"From '{start_date}' to '{end_date}'.")

        pd.set_option('max_colwidth', 200)
        pd.set_option('display.max_columns', 20)

        # Plot settings are confirm IJRS graphics instructions
        plt.rcParams['axes.titlesize'] = 16
        plt.rcParams['axes.labelsize'] = 14
        plt.rcParams['xtick.labelsize'] = 12
        plt.rcParams['ytick.labelsize'] = 12
        plt.rcParams['legend.fontsize'] = 14

        df.set_index(['date'], inplace=True)

        dfB4 = df[df.band == 'B4'].copy()
        dfB8 = df[df.band == 'B8'].copy()
        datesFmt = mdates.DateFormatter('%-d %b %Y')
        if cloud is False:
            # Plot NDVI
            fig = plt.figure(figsize=(16.0, 10.0))
            axb = fig.add_subplot(1, 1, 1)

            axb.set_title(
                f"Parcel {pid} (crop: {crop_name}, area: {area:.2f} ha)")
            axb.set_xlabel("Date")
            axb.xaxis.set_major_formatter(datesFmt)

            axb.set_ylabel(r'DN')
            axb.plot(dfB4.index,
                     dfB4['mean'],
                     linestyle=' ',
                     marker='s',
                     markersize=10,
                     color='DarkBlue',
                     fillstyle='none',
                     label='B4')
            axb.plot(dfB8.index,
                     dfB8['mean'],
                     linestyle=' ',
                     marker='o',
                     markersize=10,
                     color='Red',
                     fillstyle='none',
                     label='B8')

            axb.set_xlim(start_date, end_date + timedelta(1))
            axb.set_ylim(0, 10000)

            axb.legend(frameon=False)  # loc=2)

            return plt.show()

        else:
            # Plot Cloud free NDVI.
            dfSC = df[df.band == 'SC'].copy()
            dfNDVI = (dfB8['mean'] - dfB4['mean']) / \
                (dfB8['mean'] + dfB4['mean'])

            cloudfree = ((dfSC['mean'] >= 4) & (dfSC['mean'] < 6))

            fig = plt.figure(figsize=(16.0, 10.0))
            axb = fig.add_subplot(1, 1, 1)

            axb.set_title(
                f"{inst}\nParcel {pid} (crop: {crop_name}, area: {area:.2f} sqm)"
            )

            axb.set_xlabel("Date")
            axb.xaxis.set_major_formatter(datesFmt)

            axb.set_ylabel(r'NDVI')
            axb.plot(dfNDVI.index,
                     dfNDVI,
                     linestyle=' ',
                     marker='s',
                     markersize=10,
                     color='DarkBlue',
                     fillstyle='none',
                     label='NDVI')
            axb.plot(dfNDVI[cloudfree].index,
                     dfNDVI[cloudfree],
                     linestyle=' ',
                     marker='P',
                     markersize=10,
                     color='Red',
                     fillstyle='none',
                     label='Cloud free NDVI')

            axb.set_xlim(start_date, end_date + timedelta(1))
            axb.set_ylim(0, 1.0)

            axb.legend(frameon=False)  # loc=2)

            return plt.show()

    def plot_ts_bs():
        import numpy as np
        file_ts = glob.glob(f"{path}*_time_series_bs.csv")[0]
        df = pd.read_csv(file_ts, index_col=0)

        df['date'] = pd.to_datetime(df['date_part'], unit='s')
        start_date = df.iloc[0]['date'].date()
        end_date = df.iloc[-1]['date'].date()
        print(f"From '{start_date}' to '{end_date}'.")

        pd.set_option('max_colwidth', 200)
        pd.set_option('display.max_columns', 20)

        # Plot settings are confirm IJRS graphics instructions
        plt.rcParams['axes.titlesize'] = 16
        plt.rcParams['axes.labelsize'] = 14
        plt.rcParams['xtick.labelsize'] = 12
        plt.rcParams['ytick.labelsize'] = 12
        plt.rcParams['legend.fontsize'] = 14

        df.set_index(['date'], inplace=True)
        datesFmt = mdates.DateFormatter('%-d %b %Y')
        # Plot Backscattering coefficient

        datesFmt = mdates.DateFormatter('%-d %b %Y')
        df = df[df['mean'] >= 0]  # to remove negative values

        dfVV = df[df.band == 'VV'].copy()
        dfVH = df[df.band == 'VH'].copy()
        fig = plt.figure(figsize=(16.0, 10.0))
        axb = fig.add_subplot(1, 1, 1)

        dfVV['mean'] = dfVV['mean'].map(lambda s: 10.0 * np.log10(s))
        dfVH['mean'] = dfVH['mean'].map(lambda s: 10.0 * np.log10(s))

        axb.set_title(
            f"{inst}\nParcel {pid} (crop: {crop_name}, area: {area:.2f} sqm)")
        axb.set_xlabel("Date")
        axb.xaxis.set_major_formatter(datesFmt)

        axb.set_ylabel(r'Backscattering coefficient, $\gamma\degree$ (dB)')
        axb.plot(dfVH.index,
                 dfVH['mean'],
                 linestyle=' ',
                 marker='s',
                 markersize=10,
                 color='DarkBlue',
                 fillstyle='none',
                 label='VH')
        axb.plot(dfVV.index,
                 dfVV['mean'],
                 linestyle=' ',
                 marker='o',
                 markersize=10,
                 color='Red',
                 fillstyle='none',
                 label='VV')

        axb.set_xlim(start_date, end_date + timedelta(1))
        axb.set_ylim(-25, 0)

        axb.legend(frameon=False)  # loc=2)

        return plt.show()

    def plot_ts_c6():
        file_ts = glob.glob(f"{path}*_time_series_c6.csv")[0]
        df = pd.read_csv(file_ts, index_col=0)

        df['date'] = pd.to_datetime(df['date_part'], unit='s')
        start_date = df.iloc[0]['date'].date()
        end_date = df.iloc[-1]['date'].date()
        print(f"From '{start_date}' to '{end_date}'.")

        pd.set_option('max_colwidth', 200)
        pd.set_option('display.max_columns', 20)
        datesFmt = mdates.DateFormatter('%-d %b %Y')

        # Plot settings are confirm IJRS graphics instructions
        plt.rcParams['axes.titlesize'] = 16
        plt.rcParams['axes.labelsize'] = 14
        plt.rcParams['xtick.labelsize'] = 12
        plt.rcParams['ytick.labelsize'] = 12
        plt.rcParams['legend.fontsize'] = 14

        df.set_index(['date'], inplace=True)

        # Plot Coherence

        dfVV = df[df.band == 'VV'].copy()
        dfVH = df[df.band == 'VH'].copy()
        fig = plt.figure(figsize=(16.0, 10.0))
        axb = fig.add_subplot(1, 1, 1)

        axb.set_title(
            f"{inst}\nParcel {pid} (crop: {crop_name}, area: {area:.2f} sqm)")
        axb.set_xlabel("Date")
        axb.xaxis.set_major_formatter(datesFmt)

        axb.set_ylabel(r'Coherence')
        axb.plot(dfVH.index,
                 dfVH['mean'],
                 linestyle=' ',
                 marker='s',
                 markersize=10,
                 color='DarkBlue',
                 fillstyle='none',
                 label='VH')
        axb.plot(dfVV.index,
                 dfVV['mean'],
                 linestyle=' ',
                 marker='o',
                 markersize=10,
                 color='Red',
                 fillstyle='none',
                 label='VV')

        axb.set_xlim(start_date, end_date + timedelta(1))
        axb.set_ylim(0, 1)

        axb.legend(frameon=False)  # loc=2)

        return plt.show()

    ts_cloud = Checkbox(value=True,
                        description='Cloud free',
                        disabled=False,
                        indent=False)

    ts_files = glob.glob(f"{path}*time_series*.csv")
    ts_file_types = [b.split('_')[-1].split('.')[0] for b in ts_files]
    ts_types = [t for t in data_options.pts_tstype() if t[1] in ts_file_types]

    ts_type = Dropdown(
        options=ts_types,
        description='Select type:',
        disabled=False,
    )

    btn_ts = Button(value=False,
                    description='Plot TS',
                    disabled=False,
                    button_style='info',
                    tooltip='Refresh output',
                    icon='')

    ts_out = Output()

    @btn_ts.on_click
    def btn_ts_on_click(b):
        btn_ts.description = 'Refresh'
        btn_ts.icon = 'refresh'
        with ts_out:
            ts_out.clear_output()
            if ts_type.value == 's2':
                plot_ts_s2(ts_cloud.value)
            elif ts_type.value == 'bs':
                plot_ts_bs()
            elif ts_type.value == 'c6':
                plot_ts_c6()

    def on_ts_type_change(change):
        if ts_type.value == 's2':
            wbox_ts.children = [btn_ts, ts_type, ts_cloud]
        else:
            wbox_ts.children = [btn_ts, ts_type]

    ts_type.observe(on_ts_type_change, 'value')

    wbox_ts = HBox([btn_ts, ts_type, ts_cloud])

    wbox = VBox([wbox_ts, ts_out])

    return wbox
Exemple #23
0
    def __init__(self):
        
        self.output_dir = '.'
#        self.output_dir = 'tmpdir'

        # self.fig = plt.figure(figsize=(7.2,6))  # this strange figsize results in a ~square contour plot

        # initial value
        self.field_index = 4
        # self.field_index = self.mcds_field.value + 4

        tab_height = '500px'
        constWidth = '180px'
        constWidth2 = '150px'
        tab_layout = Layout(width='900px',   # border='2px solid black',
                            height=tab_height, ) #overflow_y='scroll')

        max_frames = 1   
        self.mcds_plot = interactive(self.plot_substrate, frame=(0, max_frames), continuous_update=False)  
        svg_plot_size = '700px'
        self.mcds_plot.layout.width = svg_plot_size
        self.mcds_plot.layout.height = svg_plot_size

        self.max_frames = BoundedIntText(
            min=0, max=99999, value=max_frames,
            description='Max',
           layout=Layout(width='160px'),
        )
        self.max_frames.observe(self.update_max_frames)

        self.field_min_max = {'dummy': [0., 1.]}
        # hacky I know, but make a dict that's got (key,value) reversed from the dict in the Dropdown below
        self.field_dict = {0:'dummy'}

        self.mcds_field = Dropdown(
            options={'dummy': 0},
            value=0,
            #     description='Field',
           layout=Layout(width=constWidth)
        )
        # print("substrate __init__: self.mcds_field.value=",self.mcds_field.value)
#        self.mcds_field.observe(self.mcds_field_cb)
        self.mcds_field.observe(self.mcds_field_changed_cb)

        # self.field_cmap = Text(
        #     value='viridis',
        #     description='Colormap',
        #     disabled=True,
        #     layout=Layout(width=constWidth),
        # )
        self.field_cmap = Dropdown(
            options=['viridis', 'jet', 'YlOrRd'],
            value='viridis',
            #     description='Field',
           layout=Layout(width=constWidth)
        )
        #self.field_cmap.observe(self.plot_substrate)
#        self.field_cmap.observe(self.plot_substrate)
        self.field_cmap.observe(self.mcds_field_cb)

        self.cmap_fixed = Checkbox(
            description='Fix',
            disabled=False,
#           layout=Layout(width=constWidth2),
        )

        self.save_min_max= Button(
            description='Save', #style={'description_width': 'initial'},
            button_style='success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Save min/max for this substrate',
            disabled=True,
           layout=Layout(width='90px')
        )

        def save_min_max_cb(b):
#            field_name = self.mcds_field.options[]
#            field_name = next(key for key, value in self.mcds_field.options.items() if value == self.mcds_field.value)
            field_name = self.field_dict[self.mcds_field.value]
#            print(field_name)
#            self.field_min_max = {'oxygen': [0., 30.], 'glucose': [0., 1.], 'H+ ions': [0., 1.], 'ECM': [0., 1.], 'NP1': [0., 1.], 'NP2': [0., 1.]}
            self.field_min_max[field_name][0] = self.cmap_min.value
            self.field_min_max[field_name][1] = self.cmap_max.value
#            print(self.field_min_max)

        self.save_min_max.on_click(save_min_max_cb)

        self.cmap_min = FloatText(
            description='Min',
            value=0,
            step = 0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_min.observe(self.mcds_field_cb)

        self.cmap_max = FloatText(
            description='Max',
            value=38,
            step = 0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_max.observe(self.mcds_field_cb)

        def cmap_fixed_cb(b):
            if (self.cmap_fixed.value):
                self.cmap_min.disabled = False
                self.cmap_max.disabled = False
                self.save_min_max.disabled = False
            else:
                self.cmap_min.disabled = True
                self.cmap_max.disabled = True
                self.save_min_max.disabled = True
#            self.mcds_field_cb()

        self.cmap_fixed.observe(cmap_fixed_cb)

        field_cmap_row2 = HBox([self.field_cmap, self.cmap_fixed])

#        field_cmap_row3 = HBox([self.save_min_max, self.cmap_min, self.cmap_max])
        items_auto = [
            self.save_min_max, #layout=Layout(flex='3 1 auto', width='auto'),
            self.cmap_min, 
            self.cmap_max,  
         ]
        box_layout = Layout(display='flex',
                    flex_flow='row',
                    align_items='stretch',
                    width='80%')
        field_cmap_row3 = Box(children=items_auto, layout=box_layout)

#        field_cmap_row3 = Box([self.save_min_max, self.cmap_min, self.cmap_max])

        # mcds_tab = widgets.VBox([mcds_dir, mcds_plot, mcds_play], layout=tab_layout)
        mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3, self.max_frames])  # mcds_dir
#        mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3,])  # mcds_dir

#        self.tab = HBox([mcds_params, self.mcds_plot], layout=tab_layout)
#        self.tab = HBox([mcds_params, self.mcds_plot])

        help_label = Label('select slider: drag or left/right arrows')
        row1 = Box([help_label, Box( [self.max_frames, self.mcds_field, self.field_cmap], layout=Layout(border='0px solid black',
                            width='50%',
                            height='',
                            align_items='stretch',
                            flex_direction='row',
                            display='flex'))] )
        row2 = Box([self.cmap_fixed, self.cmap_min, self.cmap_max], layout=Layout(border='0px solid black',
                            width='50%',
                            height='',
                            align_items='stretch',
                            flex_direction='row',
                            display='flex'))
        self.download_button = Download('mcds.zip', style='warning', icon='cloud-download', 
                                            tooltip='Download data', cb=self.download_cb)
        download_row = HBox([self.download_button.w, Label("Download all substrate data (browser must allow pop-ups).")])

#        self.tab = VBox([row1, row2, self.mcds_plot])
        self.tab = VBox([row1, row2, self.mcds_plot, download_row])
Exemple #24
0
def time_series_widget(aoi, year, pid):

    path = normpath(
        join(config.get_value(['paths', 'temp']), aoi, str(year), str(pid)))
    # confvalues = config.read()
    # inst = confvalues['set']['institution']
    file_info = normpath(join(path, 'info.json'))

    with open(file_info, 'r') as f:
        info_data = json.loads(f.read())
    pid = info_data['pid'][0]

    ts_cloud = Checkbox(value=True,
                        description='Cloud free',
                        disabled=False,
                        indent=False)

    ts_files = glob.glob(normpath(join(path, '*time_series*.csv')))
    ts_file_types = [b.split('_')[-1].split('.')[0] for b in ts_files]
    if 's2' in ts_file_types:
        ts_file_types.append('ndvi')
    ts_types = [t for t in data_options.pts_tstype() if t[1] in ts_file_types]

    ts_type = Dropdown(
        options=ts_types,
        description='Select type:',
        disabled=False,
    )

    btn_ts = Button(value=False,
                    description='Plot TS',
                    disabled=False,
                    button_style='info',
                    tooltip='Refresh output',
                    icon='')

    ts_out = Output()

    @btn_ts.on_click
    def btn_ts_on_click(b):
        btn_ts.description = 'Refresh'
        btn_ts.icon = 'refresh'
        with ts_out:
            ts_out.clear_output()
            if ts_type.value == 's2':
                time_series.s2(aoi, str(year), str(pid), bands=['B4', 'B8'])
            elif ts_type.value == 'ndvi':
                time_series.ndvi(aoi, str(year), str(pid))
            elif ts_type.value == 'bs':
                time_series.s1(aoi, str(year), str(pid), 'bs')
            elif ts_type.value == 'c6':
                time_series.s1(aoi, str(year), str(pid), 'c6')

    def on_ts_type_change(change):
        if ts_type.value == 's2':
            wbox_ts.children = [btn_ts, ts_type]
        else:
            wbox_ts.children = [btn_ts, ts_type]

    ts_type.observe(on_ts_type_change, 'value')

    wbox_ts = HBox([btn_ts, ts_type, ts_cloud])

    wbox = VBox([wbox_ts, ts_out])

    return wbox
Exemple #25
0
class SVGTab(object):

    #    myplot = None

    def __init__(self):
        tab_height = '520px'
        tab_height = '550px'
        tab_layout = Layout(
            width='900px',  # border='2px solid black',
            height=tab_height)  #, overflow_y='scroll')

        self.output_dir = '.'
        #        self.output_dir = 'tmpdir'

        max_frames = 1
        self.svg_plot = interactive(self.plot_svg,
                                    frame=(0, max_frames),
                                    continuous_update=False)
        svg_plot_size = '500px'
        self.svg_plot.layout.width = svg_plot_size
        self.svg_plot.layout.height = svg_plot_size
        self.use_defaults = True

        self.show_nucleus = 0  # 0->False, 1->True in Checkbox!
        self.show_edge = 1  # 0->False, 1->True in Checkbox!
        self.scale_radius = 1.0
        self.axes_min = 0.0
        self.axes_max = 2000  # hmm, this can change (TODO?)
        # self.fig = plt.figure(figsize=(6, 6))
        #        self.tab = HBox([svg_plot], layout=tab_layout)

        self.max_frames = BoundedIntText(
            min=0,
            max=99999,
            value=max_frames,
            description='Max',
            # layout=Layout(flex='0 1 auto', width='auto'),  #Layout(width='160px'),
        )
        self.max_frames.observe(self.update_max_frames)

        self.show_nucleus_checkbox = Checkbox(
            description='nucleus',
            value=False,
            disabled=False,
            # layout=Layout(flex='1 1 auto', width='auto'),  #Layout(width='160px'),
        )
        self.show_nucleus_checkbox.observe(self.show_nucleus_cb)

        self.show_edge_checkbox = Checkbox(
            description='edge',
            value=True,
            disabled=False,
            # layout=Layout(flex='1 1 auto', width='auto'),  #Layout(width='160px'),
        )
        self.show_edge_checkbox.observe(self.show_edge_cb)

        #        row1 = HBox([Label('(select slider: drag or left/right arrows)'),
        #            self.max_frames, VBox([self.show_nucleus_checkbox, self.show_edge_checkbox])])
        #            self.max_frames, self.show_nucleus_checkbox], layout=Layout(width='500px'))

        #        self.tab = VBox([row1,self.svg_plot], layout=tab_layout)

        self.help_label = Label('select slider: drag or left/right arrows',
                                layout=Layout(flex='0 1 auto', width='auto'))
        #                                    layout=Layout(flex='3 1 auto', width='auto'))
        items_auto = [
            self.help_label,
            self.max_frames,
            self.show_nucleus_checkbox,
            self.show_edge_checkbox,
        ]
        #row1 = HBox([Label('(select slider: drag or left/right arrows)'),
        #            max_frames, show_nucleus_checkbox, show_edge_checkbox],
        #            layout=Layout(width='800px'))
        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='90%')
        # row1 = Box(children=items_auto, layout=box_layout)
        #row1 = Box(children=items_auto)
        row1 = Box([
            self.help_label,
            Box([
                self.max_frames, self.show_nucleus_checkbox,
                self.show_edge_checkbox
            ],
                layout=Layout(border='0px solid black',
                              width='60%',
                              height='',
                              align_items='stretch',
                              flex_direction='row',
                              display='flex'))
        ])

        # self.download_button = Download('svg.zip', style='warning', icon='cloud-download', tooltip='Download results', cb=self.download_cb)
        #self.download_button = Download('svg.zip', style='warning', icon='cloud-download', tooltip='Download results')

        #        self.tab = VBox([row1, self.svg_plot], layout=tab_layout)
        #        self.tab = VBox([row1, self.svg_plot, self.download_button.w], layout=tab_layout)
        self.tab = VBox([row1, self.svg_plot], layout=tab_layout)

#       self.output_dir_str = os.getenv('RESULTSDIR') + "/pc4nanobio/"

    def update_max_frames_expected(
            self, value):  # called when beginning an interactive Run
        with debug_view:
            print("update_max_frames_expected: SVG UPDATE", rdir)
            self.max_frames.value = value  # assumes naming scheme: "snapshot%08d.svg"
            self.svg_plot.children[0].max = self.max_frames.value
#        self.svg_plot.update()

    def update(self, rdir):
        with debug_view:
            print("svg.py: update(): rdir (->self.output_dir)=", rdir)
        self.output_dir = rdir

        if rdir == '':
            # self.max_frames.value = 0
            tmpdir = os.path.abspath('tmpdir')
            # with debug_view:
            #     print("svg.py: update(): tmpdir=", tmpdir)
            self.output_dir = tmpdir
            all_files = sorted(glob.glob(os.path.join(tmpdir,
                                                      'snapshot*.svg')))
            # with debug_view:
            #     print("svg.py: update(): len(all_files)=", len(all_files))
            if len(all_files) > 0:
                last_file = all_files[-1]
                self.max_frames.value = int(
                    last_file[-12:-4]
                )  # assumes naming scheme: "snapshot%08d.svg"
                self.svg_plot.update()
            return

        all_files = sorted(glob.glob(os.path.join(rdir, 'snapshot*.svg')))
        with debug_view:
            print("svg.py: update(): rdir != blank; len(all_files)=",
                  len(all_files))
        if len(all_files) > 0:
            last_file = all_files[-1]
            self.max_frames.value = int(
                last_file[-12:-4])  # assumes naming scheme: "snapshot%08d.svg"
            self.svg_plot.update()

    def download_cb(self):
        file_str = os.path.join(self.output_dir, '*.svg')
        # print('zip up all ',file_str)
        with zipfile.ZipFile('svg.zip', 'w') as myzip:
            for f in glob.glob(file_str):
                myzip.write(f, os.path.basename(
                    f))  # 2nd arg avoids full filename path in the archive

    def show_nucleus_cb(self, b):
        global current_frame
        if (self.show_nucleus_checkbox.value):
            self.show_nucleus = 1
        else:
            self.show_nucleus = 0
#        self.plot_svg(self,current_frame)
        self.svg_plot.update()

    def show_edge_cb(self, b):
        if (self.show_edge_checkbox.value):
            self.show_edge = 1
        else:
            self.show_edge = 0
        self.svg_plot.update()

    def update_max_frames(self, _b):
        self.svg_plot.children[0].max = self.max_frames.value

    def plot_svg(self, frame):
        # global current_idx, axes_max
        global current_frame
        current_frame = frame
        fname = "snapshot%08d.svg" % frame

        #        fullname = self.output_dir_str + fname
        #        fullname = fname  # do this for nanoHUB! (data appears in root dir?)
        full_fname = os.path.join(self.output_dir, fname)
        if not os.path.isfile(full_fname):
            # print("File does not exist: ", fname)
            # print("File does not exist: ", full_fname)
            #print("No: ", full_fname)
            print("Missing output file")  #  No:  snapshot00000000.svg
            return

        xlist = deque()
        ylist = deque()
        rlist = deque()
        rgb_list = deque()

        #  print('\n---- ' + fname + ':')
        #        tree = ET.parse(fname)
        tree = ET.parse(full_fname)
        root = tree.getroot()
        #  print('--- root.tag ---')
        #  print(root.tag)
        #  print('--- root.attrib ---')
        #  print(root.attrib)
        #  print('--- child.tag, child.attrib ---')
        numChildren = 0
        for child in root:
            #    print(child.tag, child.attrib)
            #    print("keys=",child.attrib.keys())
            if self.use_defaults and ('width' in child.attrib.keys()):
                self.axes_max = float(child.attrib['width'])
                # print("debug> found width --> axes_max =", axes_max)
            if child.text and "Current time" in child.text:
                svals = child.text.split()
                # title_str = "(" + str(current_idx) + ") Current time: " + svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"
                # title_str = "Current time: " + svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"
                #                title_str = svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"
                # mins= round(int(float(xml_root.find(".//current_time").text)))  # TODO: check units = mins
                mins = round(int(float(svals[7])))  # TODO: check units = mins
                hrs = int(mins / 60)
                days = int(hrs / 24)
                title_str = '%dd, %dh, %dm' % (int(days),
                                               (hrs % 24), mins - (hrs * 60))

            # print("width ",child.attrib['width'])
            # print('attrib=',child.attrib)
            # if (child.attrib['id'] == 'tissue'):
            if ('id' in child.attrib.keys()):
                # print('-------- found tissue!!')
                tissue_parent = child
                break

        # print('------ search tissue')
        cells_parent = None

        for child in tissue_parent:
            # print('attrib=',child.attrib)
            if (child.attrib['id'] == 'cells'):
                # print('-------- found cells, setting cells_parent')
                cells_parent = child
                break
            numChildren += 1

        num_cells = 0
        #  print('------ search cells')
        for child in cells_parent:
            #    print(child.tag, child.attrib)
            #    print('attrib=',child.attrib)
            for circle in child:  # two circles in each child: outer + nucleus
                #  circle.attrib={'cx': '1085.59','cy': '1225.24','fill': 'rgb(159,159,96)','r': '6.67717','stroke': 'rgb(159,159,96)','stroke-width': '0.5'}
                #      print('  --- cx,cy=',circle.attrib['cx'],circle.attrib['cy'])
                xval = float(circle.attrib['cx'])

                s = circle.attrib['fill']
                # print("s=",s)
                # print("type(s)=",type(s))
                if (s[0:3] == "rgb"
                    ):  # if an rgb string, e.g. "rgb(175,175,80)"
                    rgb = list(map(int, s[4:-1].split(",")))
                    rgb[:] = [x / 255. for x in rgb]
                else:  # otherwise, must be a color name
                    rgb_tuple = mplc.to_rgb(mplc.cnames[s])  # a tuple
                    rgb = [x for x in rgb_tuple]

                # test for bogus x,y locations (rwh TODO: use max of domain?)
                too_large_val = 10000.
                if (np.fabs(xval) > too_large_val):
                    print("bogus xval=", xval)
                    break
                yval = float(circle.attrib['cy'])
                if (np.fabs(yval) > too_large_val):
                    print("bogus xval=", xval)
                    break

                rval = float(circle.attrib['r'])
                # if (rgb[0] > rgb[1]):
                #     print(num_cells,rgb, rval)
                xlist.append(xval)
                ylist.append(yval)
                rlist.append(rval)
                rgb_list.append(rgb)

                # For .svg files with cells that *have* a nucleus, there will be a 2nd
                if (self.show_nucleus == 0):
                    #if (not self.show_nucleus):
                    break

            num_cells += 1

            # if num_cells > 3:   # for debugging
            #   print(fname,':  num_cells= ',num_cells," --- debug exit.")
            #   sys.exit(1)
            #   break

            # print(fname,':  num_cells= ',num_cells)

        xvals = np.array(xlist)
        yvals = np.array(ylist)
        rvals = np.array(rlist)
        rgbs = np.array(rgb_list)
        # print("xvals[0:5]=",xvals[0:5])
        # print("rvals[0:5]=",rvals[0:5])
        # print("rvals.min, max=",rvals.min(),rvals.max())

        # rwh - is this where I change size of render window?? (YES - yipeee!)
        #   plt.figure(figsize=(6, 6))
        #   plt.cla()
        title_str += " (" + str(num_cells) + " agents)"
        #   plt.title(title_str)
        #   plt.xlim(axes_min,axes_max)
        #   plt.ylim(axes_min,axes_max)
        #   plt.scatter(xvals,yvals, s=rvals*scale_radius, c=rgbs)
        #        plt.axes().set_aspect('equal', 'datalim')

        self.fig = plt.figure(figsize=(6, 6))  #rwh: move to __init__
        #        plt.figure(figsize=(7, 7))

        #        axx = plt.axes([0, 0.05, 0.9, 0.9])  # left, bottom, width, height
        #        axx = fig.gca()
        #        print('fig.dpi=',fig.dpi) # = 72

        #   im = ax.imshow(f.reshape(100,100), interpolation='nearest', cmap=cmap, extent=[0,20, 0,20])
        #   ax.xlim(axes_min,axes_max)
        #   ax.ylim(axes_min,axes_max)

        # convert radii to radii in pixels
        #        ax2 = fig.gca()
        ax2 = self.fig.gca()
        N = len(xvals)
        rr_pix = (ax2.transData.transform(np.vstack([rvals, rvals]).T) -
                  ax2.transData.transform(
                      np.vstack([np.zeros(N), np.zeros(N)]).T))
        rpix, _ = rr_pix.T

        #        markers_size = (144. * rpix / fig.dpi)**2   # = (2*rpix / fig.dpi * 72)**2
        markers_size = (144. * rpix /
                        self.fig.dpi)**2  # = (2*rpix / fig.dpi * 72)**2
        #        markers_size = (2*rpix / fig.dpi * 72)**2
        markers_size = markers_size / 4000000.
        #print('markers_size.max()=',markers_size.max())

        #        ax.scatter(xvals,yvals, s=rvals*self.scale_radius, c=rgbs)
        #        axx.scatter(xvals,yvals, s=markers_size, c=rgbs)

        #rwh - temp fix - Ah, error only occurs when "edges" is toggled on
        if (self.show_edge):
            plt.scatter(xvals,
                        yvals,
                        s=markers_size,
                        c=rgbs,
                        edgecolor='black',
                        linewidth=0.5)
        else:
            plt.scatter(xvals, yvals, s=markers_size, c=rgbs)

        plt.xlim(self.axes_min, self.axes_max)
        plt.ylim(self.axes_min, self.axes_max)
        #   ax.grid(False)
        #        axx.set_title(title_str)
        plt.title(title_str)
Exemple #26
0
    def __init__(self):

        micron_units = Label(
            'micron')  # use "option m" (Mac, for micro symbol)

        constWidth = '180px'
        tab_height = '500px'
        stepsize = 10

        #style = {'description_width': '250px'}
        style = {'description_width': '25%'}
        layout = {'width': '400px'}

        name_button_layout = {'width': '25%'}
        widget_layout = {'width': '15%'}
        units_button_layout = {'width': '15%'}
        desc_button_layout = {'width': '45%'}

        menv_var1 = Button(description='virion (virion/micron^3)',
                           disabled=True,
                           layout=name_button_layout)
        menv_var1.style.button_color = 'tan'

        param_name1 = Button(description='diffusion_coefficient',
                             disabled=True,
                             layout=name_button_layout)

        self.virion_diffusion_coefficient = FloatText(value=2.5,
                                                      step=0.1,
                                                      style=style,
                                                      layout=widget_layout)

        param_name2 = Button(description='decay_rate',
                             disabled=True,
                             layout=name_button_layout)

        self.virion_decay_rate = FloatText(value=0,
                                           step=0.01,
                                           style=style,
                                           layout=widget_layout)
        param_name3 = Button(description='initial_condition',
                             disabled=True,
                             layout=name_button_layout)

        self.virion_initial_condition = FloatText(value=0,
                                                  style=style,
                                                  layout=widget_layout)
        param_name4 = Button(description='Dirichlet_boundary_condition',
                             disabled=True,
                             layout=name_button_layout)

        self.virion_Dirichlet_boundary_condition = FloatText(
            value=0, style=style, layout=widget_layout)
        self.virion_Dirichlet_boundary_condition_toggle = Checkbox(
            description='on/off',
            disabled=False,
            style=style,
            layout=widget_layout)

        menv_var2 = Button(description='assembled_virion (virion/micron^3)',
                           disabled=True,
                           layout=name_button_layout)
        menv_var2.style.button_color = 'lightgreen'

        param_name5 = Button(description='diffusion_coefficient',
                             disabled=True,
                             layout=name_button_layout)

        self.assembled_virion_diffusion_coefficient = FloatText(
            value=2.5, step=0.1, style=style, layout=widget_layout)

        param_name6 = Button(description='decay_rate',
                             disabled=True,
                             layout=name_button_layout)

        self.assembled_virion_decay_rate = FloatText(value=0,
                                                     step=0.01,
                                                     style=style,
                                                     layout=widget_layout)
        param_name7 = Button(description='initial_condition',
                             disabled=True,
                             layout=name_button_layout)

        self.assembled_virion_initial_condition = FloatText(
            value=0, style=style, layout=widget_layout)
        param_name8 = Button(description='Dirichlet_boundary_condition',
                             disabled=True,
                             layout=name_button_layout)

        self.assembled_virion_Dirichlet_boundary_condition = FloatText(
            value=0, style=style, layout=widget_layout)
        self.assembled_virion_Dirichlet_boundary_condition_toggle = Checkbox(
            description='on/off',
            disabled=False,
            style=style,
            layout=widget_layout)

        menv_var3 = Button(description='interferon_1 (mol/micron^3)',
                           disabled=True,
                           layout=name_button_layout)
        menv_var3.style.button_color = 'tan'

        param_name9 = Button(description='diffusion_coefficient',
                             disabled=True,
                             layout=name_button_layout)

        self.interferon_1_diffusion_coefficient = FloatText(
            value=555.56, step=10, style=style, layout=widget_layout)

        param_name10 = Button(description='decay_rate',
                              disabled=True,
                              layout=name_button_layout)

        self.interferon_1_decay_rate = FloatText(value=1.02e-2,
                                                 step=0.001,
                                                 style=style,
                                                 layout=widget_layout)
        param_name11 = Button(description='initial_condition',
                              disabled=True,
                              layout=name_button_layout)

        self.interferon_1_initial_condition = FloatText(value=0,
                                                        style=style,
                                                        layout=widget_layout)
        param_name12 = Button(description='Dirichlet_boundary_condition',
                              disabled=True,
                              layout=name_button_layout)

        self.interferon_1_Dirichlet_boundary_condition = FloatText(
            value=0, style=style, layout=widget_layout)
        self.interferon_1_Dirichlet_boundary_condition_toggle = Checkbox(
            description='on/off',
            disabled=False,
            style=style,
            layout=widget_layout)

        menv_var4 = Button(
            description='pro_inflammatory_cytokine (mol/micron^3)',
            disabled=True,
            layout=name_button_layout)
        menv_var4.style.button_color = 'lightgreen'

        param_name13 = Button(description='diffusion_coefficient',
                              disabled=True,
                              layout=name_button_layout)

        self.pro_inflammatory_cytokine_diffusion_coefficient = FloatText(
            value=555.56, step=10, style=style, layout=widget_layout)

        param_name14 = Button(description='decay_rate',
                              disabled=True,
                              layout=name_button_layout)

        self.pro_inflammatory_cytokine_decay_rate = FloatText(
            value=1.02e-2, step=0.001, style=style, layout=widget_layout)
        param_name15 = Button(description='initial_condition',
                              disabled=True,
                              layout=name_button_layout)

        self.pro_inflammatory_cytokine_initial_condition = FloatText(
            value=0, style=style, layout=widget_layout)
        param_name16 = Button(description='Dirichlet_boundary_condition',
                              disabled=True,
                              layout=name_button_layout)

        self.pro_inflammatory_cytokine_Dirichlet_boundary_condition = FloatText(
            value=0, style=style, layout=widget_layout)
        self.pro_inflammatory_cytokine_Dirichlet_boundary_condition_toggle = Checkbox(
            description='on/off',
            disabled=False,
            style=style,
            layout=widget_layout)

        menv_var5 = Button(description='chemokine (mol/micron^3)',
                           disabled=True,
                           layout=name_button_layout)
        menv_var5.style.button_color = 'tan'

        param_name17 = Button(description='diffusion_coefficient',
                              disabled=True,
                              layout=name_button_layout)

        self.chemokine_diffusion_coefficient = FloatText(value=555.56,
                                                         step=10,
                                                         style=style,
                                                         layout=widget_layout)

        param_name18 = Button(description='decay_rate',
                              disabled=True,
                              layout=name_button_layout)

        self.chemokine_decay_rate = FloatText(value=1.02e-2,
                                              step=0.001,
                                              style=style,
                                              layout=widget_layout)
        param_name19 = Button(description='initial_condition',
                              disabled=True,
                              layout=name_button_layout)

        self.chemokine_initial_condition = FloatText(value=0,
                                                     style=style,
                                                     layout=widget_layout)
        param_name20 = Button(description='Dirichlet_boundary_condition',
                              disabled=True,
                              layout=name_button_layout)

        self.chemokine_Dirichlet_boundary_condition = FloatText(
            value=0, style=style, layout=widget_layout)
        self.chemokine_Dirichlet_boundary_condition_toggle = Checkbox(
            description='on/off',
            disabled=False,
            style=style,
            layout=widget_layout)

        menv_var6 = Button(description='debris (mol/micron^3)',
                           disabled=True,
                           layout=name_button_layout)
        menv_var6.style.button_color = 'lightgreen'

        param_name21 = Button(description='diffusion_coefficient',
                              disabled=True,
                              layout=name_button_layout)

        self.debris_diffusion_coefficient = FloatText(value=555.56,
                                                      step=10,
                                                      style=style,
                                                      layout=widget_layout)

        param_name22 = Button(description='decay_rate',
                              disabled=True,
                              layout=name_button_layout)

        self.debris_decay_rate = FloatText(value=1.02e-2,
                                           step=0.001,
                                           style=style,
                                           layout=widget_layout)
        param_name23 = Button(description='initial_condition',
                              disabled=True,
                              layout=name_button_layout)

        self.debris_initial_condition = FloatText(value=0,
                                                  style=style,
                                                  layout=widget_layout)
        param_name24 = Button(description='Dirichlet_boundary_condition',
                              disabled=True,
                              layout=name_button_layout)

        self.debris_Dirichlet_boundary_condition = FloatText(
            value=0, style=style, layout=widget_layout)
        self.debris_Dirichlet_boundary_condition_toggle = Checkbox(
            description='on/off',
            disabled=False,
            style=style,
            layout=widget_layout)
        self.calculate_gradient = Checkbox(description='calculate_gradients',
                                           disabled=False,
                                           layout=desc_button_layout)
        self.track_internal = Checkbox(description='track_in_agents',
                                       disabled=False,
                                       layout=desc_button_layout)

        #  ------- micronenv info
        menv_units_button1 = Button(description='micron^2/min',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button2 = Button(description='1/min',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button3 = Button(description='virion/micron^3',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button4 = Button(description='virion/micron^3',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button5 = Button(description='micron^2/min',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button6 = Button(description='1/min',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button7 = Button(description='virion/micron^3',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button8 = Button(description='virion/micron^3',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button9 = Button(description='micron^2/min',
                                    disabled=True,
                                    layout=units_button_layout)
        menv_units_button10 = Button(description='1/min',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button11 = Button(description='mol/micron^3',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button12 = Button(description='mol/micron^3',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button13 = Button(description='micron^2/min',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button14 = Button(description='1/min',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button15 = Button(description='mol/micron^3',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button16 = Button(description='mol/micron^3',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button17 = Button(description='micron^2/min',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button18 = Button(description='1/min',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button19 = Button(description='mol/micron^3',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button20 = Button(description='mol/micron^3',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button21 = Button(description='micron^2/min',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button22 = Button(description='1/min',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button23 = Button(description='mol/micron^3',
                                     disabled=True,
                                     layout=units_button_layout)
        menv_units_button24 = Button(description='mol/micron^3',
                                     disabled=True,
                                     layout=units_button_layout)

        row_virion = [
            menv_var1,
        ]
        row1 = [
            param_name1, self.virion_diffusion_coefficient, menv_units_button1
        ]
        row2 = [param_name2, self.virion_decay_rate, menv_units_button2]
        row3 = [param_name3, self.virion_initial_condition, menv_units_button3]
        row4 = [
            param_name4, self.virion_Dirichlet_boundary_condition,
            menv_units_button4, self.virion_Dirichlet_boundary_condition_toggle
        ]
        row_assembled_virion = [
            menv_var2,
        ]
        row5 = [
            param_name5, self.assembled_virion_diffusion_coefficient,
            menv_units_button5
        ]
        row6 = [
            param_name6, self.assembled_virion_decay_rate, menv_units_button6
        ]
        row7 = [
            param_name7, self.assembled_virion_initial_condition,
            menv_units_button7
        ]
        row8 = [
            param_name8, self.assembled_virion_Dirichlet_boundary_condition,
            menv_units_button8,
            self.assembled_virion_Dirichlet_boundary_condition_toggle
        ]
        row_interferon_1 = [
            menv_var3,
        ]
        row9 = [
            param_name9, self.interferon_1_diffusion_coefficient,
            menv_units_button9
        ]
        row10 = [
            param_name10, self.interferon_1_decay_rate, menv_units_button10
        ]
        row11 = [
            param_name11, self.interferon_1_initial_condition,
            menv_units_button11
        ]
        row12 = [
            param_name12, self.interferon_1_Dirichlet_boundary_condition,
            menv_units_button12,
            self.interferon_1_Dirichlet_boundary_condition_toggle
        ]
        row_pro_inflammatory_cytokine = [
            menv_var4,
        ]
        row13 = [
            param_name13, self.pro_inflammatory_cytokine_diffusion_coefficient,
            menv_units_button13
        ]
        row14 = [
            param_name14, self.pro_inflammatory_cytokine_decay_rate,
            menv_units_button14
        ]
        row15 = [
            param_name15, self.pro_inflammatory_cytokine_initial_condition,
            menv_units_button15
        ]
        row16 = [
            param_name16,
            self.pro_inflammatory_cytokine_Dirichlet_boundary_condition,
            menv_units_button16,
            self.pro_inflammatory_cytokine_Dirichlet_boundary_condition_toggle
        ]
        row_chemokine = [
            menv_var5,
        ]
        row17 = [
            param_name17, self.chemokine_diffusion_coefficient,
            menv_units_button17
        ]
        row18 = [param_name18, self.chemokine_decay_rate, menv_units_button18]
        row19 = [
            param_name19, self.chemokine_initial_condition, menv_units_button19
        ]
        row20 = [
            param_name20, self.chemokine_Dirichlet_boundary_condition,
            menv_units_button20,
            self.chemokine_Dirichlet_boundary_condition_toggle
        ]
        row_debris = [
            menv_var6,
        ]
        row21 = [
            param_name21, self.debris_diffusion_coefficient,
            menv_units_button21
        ]
        row22 = [param_name22, self.debris_decay_rate, menv_units_button22]
        row23 = [
            param_name23, self.debris_initial_condition, menv_units_button23
        ]
        row24 = [
            param_name24, self.debris_Dirichlet_boundary_condition,
            menv_units_button24,
            self.debris_Dirichlet_boundary_condition_toggle
        ]
        row25 = [
            self.calculate_gradient,
        ]
        row26 = [
            self.track_internal,
        ]

        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='100%')
        box_virion = Box(children=row_virion, layout=box_layout)
        box1 = Box(children=row1, layout=box_layout)
        box2 = Box(children=row2, layout=box_layout)
        box3 = Box(children=row3, layout=box_layout)
        box4 = Box(children=row4, layout=box_layout)
        box_assembled_virion = Box(children=row_assembled_virion,
                                   layout=box_layout)
        box5 = Box(children=row5, layout=box_layout)
        box6 = Box(children=row6, layout=box_layout)
        box7 = Box(children=row7, layout=box_layout)
        box8 = Box(children=row8, layout=box_layout)
        box_interferon_1 = Box(children=row_interferon_1, layout=box_layout)
        box9 = Box(children=row9, layout=box_layout)
        box10 = Box(children=row10, layout=box_layout)
        box11 = Box(children=row11, layout=box_layout)
        box12 = Box(children=row12, layout=box_layout)
        box_pro_inflammatory_cytokine = Box(
            children=row_pro_inflammatory_cytokine, layout=box_layout)
        box13 = Box(children=row13, layout=box_layout)
        box14 = Box(children=row14, layout=box_layout)
        box15 = Box(children=row15, layout=box_layout)
        box16 = Box(children=row16, layout=box_layout)
        box_chemokine = Box(children=row_chemokine, layout=box_layout)
        box17 = Box(children=row17, layout=box_layout)
        box18 = Box(children=row18, layout=box_layout)
        box19 = Box(children=row19, layout=box_layout)
        box20 = Box(children=row20, layout=box_layout)
        box_debris = Box(children=row_debris, layout=box_layout)
        box21 = Box(children=row21, layout=box_layout)
        box22 = Box(children=row22, layout=box_layout)
        box23 = Box(children=row23, layout=box_layout)
        box24 = Box(children=row24, layout=box_layout)
        box25 = Box(children=row25, layout=box_layout)
        box26 = Box(children=row26, layout=box_layout)

        self.tab = VBox([
            box_virion,
            box1,
            box2,
            box3,
            box4,
            box_assembled_virion,
            box5,
            box6,
            box7,
            box8,
            box_interferon_1,
            box9,
            box10,
            box11,
            box12,
            box_pro_inflammatory_cytokine,
            box13,
            box14,
            box15,
            box16,
            box_chemokine,
            box17,
            box18,
            box19,
            box20,
            box_debris,
            box21,
            box22,
            box23,
            box24,
            box25,
            box26,
        ])
Exemple #27
0
    def __init__(self):
        tab_height = '520px'
        tab_height = '550px'
        tab_layout = Layout(
            width='900px',  # border='2px solid black',
            height=tab_height)  #, overflow_y='scroll')

        self.output_dir = '.'
        #        self.output_dir = 'tmpdir'

        max_frames = 1
        self.svg_plot = interactive(self.plot_svg,
                                    frame=(0, max_frames),
                                    continuous_update=False)
        svg_plot_size = '500px'
        self.svg_plot.layout.width = svg_plot_size
        self.svg_plot.layout.height = svg_plot_size
        self.use_defaults = True

        self.show_nucleus = 0  # 0->False, 1->True in Checkbox!
        self.show_edge = 1  # 0->False, 1->True in Checkbox!
        self.scale_radius = 1.0
        self.axes_min = 0.0
        self.axes_max = 2000  # hmm, this can change (TODO?)
        # self.fig = plt.figure(figsize=(6, 6))
        #        self.tab = HBox([svg_plot], layout=tab_layout)

        self.max_frames = BoundedIntText(
            min=0,
            max=99999,
            value=max_frames,
            description='Max',
            # layout=Layout(flex='0 1 auto', width='auto'),  #Layout(width='160px'),
        )
        self.max_frames.observe(self.update_max_frames)

        self.show_nucleus_checkbox = Checkbox(
            description='nucleus',
            value=False,
            disabled=False,
            # layout=Layout(flex='1 1 auto', width='auto'),  #Layout(width='160px'),
        )
        self.show_nucleus_checkbox.observe(self.show_nucleus_cb)

        self.show_edge_checkbox = Checkbox(
            description='edge',
            value=True,
            disabled=False,
            # layout=Layout(flex='1 1 auto', width='auto'),  #Layout(width='160px'),
        )
        self.show_edge_checkbox.observe(self.show_edge_cb)

        #        row1 = HBox([Label('(select slider: drag or left/right arrows)'),
        #            self.max_frames, VBox([self.show_nucleus_checkbox, self.show_edge_checkbox])])
        #            self.max_frames, self.show_nucleus_checkbox], layout=Layout(width='500px'))

        #        self.tab = VBox([row1,self.svg_plot], layout=tab_layout)

        self.help_label = Label('select slider: drag or left/right arrows',
                                layout=Layout(flex='0 1 auto', width='auto'))
        #                                    layout=Layout(flex='3 1 auto', width='auto'))
        items_auto = [
            self.help_label,
            self.max_frames,
            self.show_nucleus_checkbox,
            self.show_edge_checkbox,
        ]
        #row1 = HBox([Label('(select slider: drag or left/right arrows)'),
        #            max_frames, show_nucleus_checkbox, show_edge_checkbox],
        #            layout=Layout(width='800px'))
        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='90%')
        # row1 = Box(children=items_auto, layout=box_layout)
        #row1 = Box(children=items_auto)
        row1 = Box([
            self.help_label,
            Box([
                self.max_frames, self.show_nucleus_checkbox,
                self.show_edge_checkbox
            ],
                layout=Layout(border='0px solid black',
                              width='60%',
                              height='',
                              align_items='stretch',
                              flex_direction='row',
                              display='flex'))
        ])

        # self.download_button = Download('svg.zip', style='warning', icon='cloud-download', tooltip='Download results', cb=self.download_cb)
        #self.download_button = Download('svg.zip', style='warning', icon='cloud-download', tooltip='Download results')

        #        self.tab = VBox([row1, self.svg_plot], layout=tab_layout)
        #        self.tab = VBox([row1, self.svg_plot, self.download_button.w], layout=tab_layout)
        self.tab = VBox([row1, self.svg_plot], layout=tab_layout)
Exemple #28
0
class SVGTab(object):

    #    myplot = None

    def __init__(self):
        tab_height = '520px'
        tab_layout = Layout(
            width='800px',  # border='2px solid black',
            height=tab_height,
            overflow_y='scroll')

        self.output_dir = '.'

        max_frames = 505  # first time + 30240 / 60
        self.svg_plot = interactive(self.plot_svg,
                                    frame=(0, max_frames),
                                    continuous_update=False)
        svg_plot_size = '500px'
        self.svg_plot.layout.width = svg_plot_size
        self.svg_plot.layout.height = svg_plot_size
        self.use_defaults = True

        self.show_nucleus = 0  # 0->False, 1->True in Checkbox!
        self.show_edge = 1  # 0->False, 1->True in Checkbox!
        self.scale_radius = 1.0
        self.axes_min = 0.0
        self.axes_max = 2000  # hmm, this can change (TODO?)
        #        self.tab = HBox([svg_plot], layout=tab_layout)

        self.max_frames = BoundedIntText(
            min=0,
            max=99999,
            value=max_frames,
            description='Max',
            layout=Layout(flex='1 1 auto',
                          width='auto'),  #Layout(width='160px'),
        )
        self.max_frames.observe(self.update_max_frames)

        self.show_nucleus_checkbox = Checkbox(
            description='nucleus',
            value=False,
            disabled=False,
            layout=Layout(flex='1 1 auto',
                          width='auto'),  #Layout(width='160px'),
        )
        self.show_nucleus_checkbox.observe(self.show_nucleus_cb)

        self.show_edge_checkbox = Checkbox(
            description='edge',
            value=True,
            disabled=False,
            layout=Layout(flex='1 1 auto',
                          width='auto'),  #Layout(width='160px'),
        )
        self.show_edge_checkbox.observe(self.show_edge_cb)

        #        row1 = HBox([Label('(select slider: drag or left/right arrows)'),
        #            self.max_frames, VBox([self.show_nucleus_checkbox, self.show_edge_checkbox])])
        #            self.max_frames, self.show_nucleus_checkbox], layout=Layout(width='500px'))

        #        self.tab = VBox([row1,self.svg_plot], layout=tab_layout)

        items_auto = [
            Label('(select slider: drag or left/right arrows)'),
            self.max_frames,
            self.show_nucleus_checkbox,
            self.show_edge_checkbox,
        ]
        #row1 = HBox([Label('(select slider: drag or left/right arrows)'),
        #            max_frames, show_nucleus_checkbox, show_edge_checkbox],
        #            layout=Layout(width='800px'))
        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='90%')
        row1 = Box(children=items_auto, layout=box_layout)
        self.tab = VBox([row1, self.svg_plot], layout=tab_layout)

#       self.output_dir_str = os.getenv('RESULTSDIR') + "/pc4nanobio/"

    def show_nucleus_cb(self, b):
        global current_frame
        if (self.show_nucleus_checkbox.value):
            self.show_nucleus = 1
        else:
            self.show_nucleus = 0
#        self.plot_svg(self,current_frame)
        self.svg_plot.update()

    def show_edge_cb(self, b):
        if (self.show_edge_checkbox.value):
            self.show_edge = 1
        else:
            self.show_edge = 0
        self.svg_plot.update()

    def update_max_frames(self, _b):
        self.svg_plot.children[0].max = self.max_frames.value

    def plot_svg(self, frame):
        # global current_idx, axes_max
        # print('plot_svg: SVG=', SVG)
        global current_frame
        current_frame = frame
        fname = "snapshot%08d.svg" % frame

        #        fullname = self.output_dir_str + fname
        #        fullname = fname  # do this for nanoHUB! (data appears in root dir?)
        full_fname = os.path.join(self.output_dir, fname)
        if not os.path.isfile(full_fname):
            #            print("File does not exist: ", fname)
            #            print("File does not exist: ", full_fname)
            print("No: ", full_fname)
            return

        xlist = deque()
        ylist = deque()
        rlist = deque()
        rgb_list = deque()

        #  print('\n---- ' + fname + ':')
        #        tree = ET.parse(fname)
        tree = ET.parse(full_fname)
        root = tree.getroot()
        #  print('--- root.tag ---')
        #  print(root.tag)
        #  print('--- root.attrib ---')
        #  print(root.attrib)
        #  print('--- child.tag, child.attrib ---')
        numChildren = 0
        for child in root:
            #    print(child.tag, child.attrib)
            #    print("keys=",child.attrib.keys())
            if self.use_defaults and ('width' in child.attrib.keys()):
                self.axes_max = float(child.attrib['width'])
                # print("debug> found width --> axes_max =", axes_max)
            if child.text and "Current time" in child.text:
                svals = child.text.split()
                # title_str = "(" + str(current_idx) + ") Current time: " + svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"
                # title_str = "Current time: " + svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"
                title_str = svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"

            # print("width ",child.attrib['width'])
            # print('attrib=',child.attrib)
            # if (child.attrib['id'] == 'tissue'):
            if ('id' in child.attrib.keys()):
                # print('-------- found tissue!!')
                tissue_parent = child
                break

        # print('------ search tissue')
        cells_parent = None

        for child in tissue_parent:
            # print('attrib=',child.attrib)
            if (child.attrib['id'] == 'cells'):
                # print('-------- found cells, setting cells_parent')
                cells_parent = child
                break
            numChildren += 1

        num_cells = 0
        #  print('------ search cells')
        for child in cells_parent:
            #    print(child.tag, child.attrib)
            #    print('attrib=',child.attrib)
            for circle in child:  # two circles in each child: outer + nucleus
                #  circle.attrib={'cx': '1085.59','cy': '1225.24','fill': 'rgb(159,159,96)','r': '6.67717','stroke': 'rgb(159,159,96)','stroke-width': '0.5'}
                #      print('  --- cx,cy=',circle.attrib['cx'],circle.attrib['cy'])
                xval = float(circle.attrib['cx'])

                s = circle.attrib['fill']
                # print("s=",s)
                # print("type(s)=",type(s))
                if (s[0:3] == "rgb"
                    ):  # if an rgb string, e.g. "rgb(175,175,80)"
                    rgb = list(map(int, s[4:-1].split(",")))
                    rgb[:] = [x / 255. for x in rgb]
                else:  # otherwise, must be a color name
                    rgb_tuple = mplc.to_rgb(mplc.cnames[s])  # a tuple
                    rgb = [x for x in rgb_tuple]

                # test for bogus x,y locations (rwh TODO: use max of domain?)
                too_large_val = 10000.
                if (np.fabs(xval) > too_large_val):
                    print("bogus xval=", xval)
                    break
                yval = float(circle.attrib['cy'])
                if (np.fabs(yval) > too_large_val):
                    print("bogus xval=", xval)
                    break

                rval = float(circle.attrib['r'])
                # if (rgb[0] > rgb[1]):
                #     print(num_cells,rgb, rval)
                xlist.append(xval)
                ylist.append(yval)
                rlist.append(rval)
                rgb_list.append(rgb)

                # For .svg files with cells that *have* a nucleus, there will be a 2nd
                if (self.show_nucleus == 0):
                    #if (not self.show_nucleus):
                    break

            num_cells += 1

            # if num_cells > 3:   # for debugging
            #   print(fname,':  num_cells= ',num_cells," --- debug exit.")
            #   sys.exit(1)
            #   break

            # print(fname,':  num_cells= ',num_cells)

        xvals = np.array(xlist)
        yvals = np.array(ylist)
        rvals = np.array(rlist)
        rgbs = np.array(rgb_list)
        # print("xvals[0:5]=",xvals[0:5])
        # print("rvals[0:5]=",rvals[0:5])
        # print("rvals.min, max=",rvals.min(),rvals.max())

        # rwh - is this where I change size of render window?? (YES - yipeee!)
        #   plt.figure(figsize=(6, 6))
        #   plt.cla()
        title_str += " (" + str(num_cells) + " agents)"
        #   plt.title(title_str)
        #   plt.xlim(axes_min,axes_max)
        #   plt.ylim(axes_min,axes_max)
        #   plt.scatter(xvals,yvals, s=rvals*scale_radius, c=rgbs)

        fig = plt.figure(figsize=(6, 6))
        #        axx = plt.axes([0, 0.05, 0.9, 0.9])  # left, bottom, width, height
        #        axx = fig.gca()
        #        print('fig.dpi=',fig.dpi) # = 72

        #   im = ax.imshow(f.reshape(100,100), interpolation='nearest', cmap=cmap, extent=[0,20, 0,20])
        #   ax.xlim(axes_min,axes_max)
        #   ax.ylim(axes_min,axes_max)

        # convert radii to radii in pixels
        ax2 = fig.gca()
        N = len(xvals)
        rr_pix = (ax2.transData.transform(np.vstack([rvals, rvals]).T) -
                  ax2.transData.transform(
                      np.vstack([np.zeros(N), np.zeros(N)]).T))
        rpix, _ = rr_pix.T

        markers_size = (144. * rpix /
                        fig.dpi)**2  # = (2*rpix / fig.dpi * 72)**2
        #        markers_size = (2*rpix / fig.dpi * 72)**2
        markers_size = markers_size / 4000000.
        #        print('max=',markers_size.max())

        #        ax.scatter(xvals,yvals, s=rvals*self.scale_radius, c=rgbs)
        #        axx.scatter(xvals,yvals, s=markers_size, c=rgbs)
        if (self.show_edge):
            plt.scatter(xvals,
                        yvals,
                        s=markers_size,
                        c=rgbs,
                        edgecolor='black',
                        linewidth='0.5')
        else:
            plt.scatter(xvals, yvals, s=markers_size, c=rgbs)
        plt.xlim(self.axes_min, self.axes_max)
        plt.ylim(self.axes_min, self.axes_max)
        #   ax.grid(False)
        #        axx.set_title(title_str)
        plt.title(title_str)
class SubstrateTab(object):
    def __init__(self):

        self.output_dir = '.'
        # self.output_dir = 'tmpdir'

        self.figsize_width_substrate = 15.0  # allow extra for colormap
        self.figsize_height_substrate = 12.5
        self.figsize_width_svg = 12.0
        self.figsize_height_svg = 12.0

        # self.fig = plt.figure(figsize=(self.figsize_width_substrate, self.figsize_height_substrate))
        # self.fig = plt.figure()
        self.fig = None

        # self.fig = plt.figure(figsize=(7.2,6))  # this strange figsize results in a ~square contour plot

        self.first_time = True
        self.modulo = 1

        self.use_defaults = True

        self.svg_delta_t = 1
        self.substrate_delta_t = 1
        self.svg_frame = 1
        self.substrate_frame = 1

        self.customized_output_freq = False
        self.therapy_activation_time = 1000000
        self.max_svg_frame_pre_therapy = 1000000
        self.max_substrate_frame_pre_therapy = 1000000

        self.svg_xmin = 0

        # Probably don't want to hardwire these if we allow changing the domain size
        # self.svg_xrange = 2000
        # self.xmin = -1000.
        # self.xmax = 1000.
        # self.ymin = -1000.
        # self.ymax = 1000.
        # self.x_range = 2000.
        # self.y_range = 2000.

        self.show_nucleus = False
        self.show_edge = True

        # initial value
        self.field_index = 4
        # self.field_index = self.mcds_field.value + 4

        # define dummy size of mesh (set in the tool's primary module)
        self.numx = 0
        self.numy = 0

        self.title_str = ''

        tab_height = '600px'
        tab_height = '500px'
        constWidth = '180px'
        constWidth2 = '150px'
        tab_layout = Layout(
            width='900px',  # border='2px solid black',
            height=tab_height,
        )  #overflow_y='scroll')

        max_frames = 2
        # self.mcds_plot = interactive(self.plot_substrate, frame=(0, max_frames), continuous_update=False)
        # self.i_plot = interactive(self.plot_plots, frame=(0, max_frames), continuous_update=False)

        # self.i_plot = interactive(self.plot_substrate0, frame=(0, max_frames), continuous_update=False)
        self.i_plot = interactive(self.plot_substrate,
                                  frame=(0, max_frames),
                                  continuous_update=False)

        self.frame_slider = IntSlider(
            min=0,
            max=10,
            step=1,
            description='Test:',
            disabled=False,
            continuous_update=False,
            orientation='horizontal',
            readout=True,
            readout_format='d',
        )
        # self.frame_slider.observe(self.frame_slider_cb)
        self.frame_slider.observe(self.plot_substrate)

        # "plot_size" controls the size of the tab height, not the plot (rf. figsize for that)
        # NOTE: the Substrates Plot tab has an extra row of widgets at the top of it (cf. Cell Plots tab)
        svg_plot_size = '700px'
        svg_plot_size = '600px'
        svg_plot_size = '700px'
        svg_plot_size = '900px'
        self.i_plot.layout.width = svg_plot_size
        self.i_plot.layout.height = svg_plot_size

        self.fontsize = 20

        # description='# cell frames',
        self.max_frames = BoundedIntText(
            min=0,
            max=99999,
            value=max_frames,
            description='# frames',
            layout=Layout(width='160px'),
        )
        self.max_frames.observe(self.update_max_frames)

        # self.field_min_max = {'dummy': [0., 1.]}
        # NOTE: manually setting these for now (vs. parsing them out of data/initial.xml)
        self.field_min_max = {
            'director signal': [0., 1.],
            'cargo signal': [0., 1.]
        }
        # hacky I know, but make a dict that's got (key,value) reversed from the dict in the Dropdown below
        # self.field_dict = {0:'dummy'}
        self.field_dict = {0: 'director signal', 1: 'cargo signal'}

        self.mcds_field = Dropdown(
            options={
                'director signal': 0,
                'cargo signal': 1
            },
            value=0,
            #     description='Field',
            layout=Layout(width=constWidth))
        # print("substrate __init__: self.mcds_field.value=",self.mcds_field.value)
        #        self.mcds_field.observe(self.mcds_field_cb)
        self.mcds_field.observe(self.mcds_field_changed_cb)

        # self.field_cmap = Text(
        #     value='viridis',
        #     description='Colormap',
        #     disabled=True,
        #     layout=Layout(width=constWidth),
        # )
        self.field_cmap = Dropdown(
            options=['viridis', 'jet', 'YlOrRd'],
            value='YlOrRd',
            #     description='Field',
            layout=Layout(width=constWidth))
        #        self.field_cmap.observe(self.plot_substrate)
        self.field_cmap.observe(self.mcds_field_cb)

        self.cmap_fixed = Checkbox(
            description='Fix',
            disabled=False,
            #           layout=Layout(width=constWidth2),
        )

        self.save_min_max = Button(
            description='Save',  #style={'description_width': 'initial'},
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Save min/max for this substrate',
            disabled=True,
            layout=Layout(width='90px'))

        def save_min_max_cb(b):
            #            field_name = self.mcds_field.options[]
            #            field_name = next(key for key, value in self.mcds_field.options.items() if value == self.mcds_field.value)
            field_name = self.field_dict[self.mcds_field.value]
            #            print(field_name)
            #            self.field_min_max = {'oxygen': [0., 30.], 'glucose': [0., 1.], 'H+ ions': [0., 1.], 'ECM': [0., 1.], 'NP1': [0., 1.], 'NP2': [0., 1.]}
            self.field_min_max[field_name][0] = self.cmap_min.value
            self.field_min_max[field_name][1] = self.cmap_max.value
#            print(self.field_min_max)

        self.save_min_max.on_click(save_min_max_cb)

        self.cmap_min = FloatText(
            description='Min',
            value=0,
            step=0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_min.observe(self.mcds_field_cb)

        self.cmap_max = FloatText(
            description='Max',
            value=38,
            step=0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_max.observe(self.mcds_field_cb)

        def cmap_fixed_cb(b):
            if (self.cmap_fixed.value):
                self.cmap_min.disabled = False
                self.cmap_max.disabled = False
                self.save_min_max.disabled = False
            else:
                self.cmap_min.disabled = True
                self.cmap_max.disabled = True
                self.save_min_max.disabled = True
#            self.mcds_field_cb()

        self.cmap_fixed.observe(cmap_fixed_cb)

        field_cmap_row2 = HBox([self.field_cmap, self.cmap_fixed])

        #        field_cmap_row3 = HBox([self.save_min_max, self.cmap_min, self.cmap_max])
        items_auto = [
            self.save_min_max,  #layout=Layout(flex='3 1 auto', width='auto'),
            self.cmap_min,
            self.cmap_max,
        ]
        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='80%')
        field_cmap_row3 = Box(children=items_auto, layout=box_layout)

        #---------------------
        self.cell_nucleus_toggle = Checkbox(
            description='nuclei',
            disabled=False,
            value=self.show_nucleus,
            #           layout=Layout(width=constWidth2),
        )

        def cell_nucleus_toggle_cb(b):
            # self.update()
            if (self.cell_nucleus_toggle.value):
                self.show_nucleus = True
            else:
                self.show_nucleus = False
            self.i_plot.update()

        self.cell_nucleus_toggle.observe(cell_nucleus_toggle_cb)

        #----
        self.cell_edges_toggle = Checkbox(
            description='edges',
            disabled=False,
            value=self.show_edge,
            #           layout=Layout(width=constWidth2),
        )

        def cell_edges_toggle_cb(b):
            # self.update()
            if (self.cell_edges_toggle.value):
                self.show_edge = True
            else:
                self.show_edge = False
            self.i_plot.update()

        self.cell_edges_toggle.observe(cell_edges_toggle_cb)

        self.cells_toggle = Checkbox(
            description='Cells',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def cells_toggle_cb(b):
            # self.update()
            self.i_plot.update()
            if (self.cells_toggle.value):
                self.cell_edges_toggle.disabled = False
                self.cell_nucleus_toggle.disabled = False
            else:
                self.cell_edges_toggle.disabled = True
                self.cell_nucleus_toggle.disabled = True

        self.cells_toggle.observe(cells_toggle_cb)

        #---------------------
        self.substrates_toggle = Checkbox(
            description='Substrates',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def substrates_toggle_cb(b):
            if (self.substrates_toggle.value):  # seems bass-ackwards
                self.cmap_fixed.disabled = False
                self.cmap_min.disabled = False
                self.cmap_max.disabled = False
                self.mcds_field.disabled = False
                self.field_cmap.disabled = False
            else:
                self.cmap_fixed.disabled = True
                self.cmap_min.disabled = True
                self.cmap_max.disabled = True
                self.mcds_field.disabled = True
                self.field_cmap.disabled = True

        self.substrates_toggle.observe(substrates_toggle_cb)

        self.grid_toggle = Checkbox(
            description='grid',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def grid_toggle_cb(b):
            # self.update()
            self.i_plot.update()

        self.grid_toggle.observe(grid_toggle_cb)

        #        field_cmap_row3 = Box([self.save_min_max, self.cmap_min, self.cmap_max])

        # mcds_tab = widgets.VBox([mcds_dir, mcds_plot, mcds_play], layout=tab_layout)
        # mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3, self.max_frames])  # mcds_dir
        #        mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3,])  # mcds_dir

        #        self.tab = HBox([mcds_params, self.mcds_plot], layout=tab_layout)

        help_label = Label('select slider: drag or left/right arrows')
        # row1 = Box([help_label, Box( [self.max_frames, self.mcds_field, self.field_cmap], layout=Layout(border='0px solid black',
        row1a = Box([self.max_frames, self.mcds_field, self.field_cmap],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        row1b = Box([
            self.cells_toggle, self.cell_nucleus_toggle, self.cell_edges_toggle
        ],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        row1 = HBox([row1a, Label('.....'), row1b])

        row2a = Box([self.cmap_fixed, self.cmap_min, self.cmap_max],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        # row2b = Box( [self.substrates_toggle, self.grid_toggle], layout=Layout(border='1px solid black',
        row2b = Box([
            self.substrates_toggle,
        ],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        # row2 = HBox( [row2a, self.substrates_toggle, self.grid_toggle])
        row2 = HBox([row2a, Label('.....'), row2b])

        row3 = HBox([
            self.frame_slider,
        ])

        if (hublib_flag):
            self.download_button = Download('mcds.zip',
                                            style='warning',
                                            icon='cloud-download',
                                            tooltip='Download data',
                                            cb=self.download_cb)

            self.download_svg_button = Download(
                'svg.zip',
                style='warning',
                icon='cloud-download',
                tooltip='You need to allow pop-ups in your browser',
                cb=self.download_svg_cb)
            download_row = HBox([
                self.download_button.w, self.download_svg_button.w,
                Label("Download all cell plots (browser must allow pop-ups).")
            ])

            # box_layout = Layout(border='0px solid')
            # controls_box = VBox([row1, row2])  # ,width='50%', layout=box_layout)
            controls_box = VBox([row1, row2,
                                 row3])  # ,width='50%', layout=box_layout)

            # self.tab = VBox([controls_box, self.i_plot, download_row])

            #            graph = GraphWidget()
            # iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])
            # self.fig = go.FigureWidget(data=go.Bar(y=[2, 3, 1]))

            # x = np.random.randn(2000)
            # y = np.random.randn(2000)
            self.x = np.random.randn(2)
            self.y = np.random.randn(2)
            # iplot([go.Histogram2dContour(x=x, y=y, contours=dict(coloring='heatmap')),
            # self.fig = go.FigureWidget(data=go.Histogram2dContour(x=x, y=y, contours_showlines=False, contours=dict(coloring='heatmap')))
            self.fig = go.FigureWidget(data=go.Histogram2dContour(
                x=self.x, y=self.y, contours=dict(coloring='heatmap')))

            # self.fig = go.FigureWidget( data = [
            #     {
            #     'type' : 'Histogram2dContour',
            #     'x' : self.x,
            #     'y' : self.y,
            #     'contours' : {
            #         'coloring': 'heatmap'
            #     }
            #     },
            #     {
            #     'type' : 'scatter',
            #     'x' : self.x,
            #     'y' : self.y,
            #     'line' : {
            #         'color': 'red',
            #         'width': 3
            #     }
            #     }
            #     ], layout={})
            # self.fig = go.FigureWidget(data=go.Contour(x=x, y=y, contours=dict(coloring='heatmap')))

            # x = np.linspace(-10, 10, 400)
            # y = np.linspace(-5, 5, 400)
            # x, y = np.meshgrid(x, y)
            # a = 1
            # b = 2
            # z = x**2/a**2 - y**2/b**2
            # contour = go.Contour(
            #     z=z
            # )
            # contour.contours.type = 'constraint'
            # contour.contours.value = 8
            # data = [contour]
            # # self.fig = go.Figure(data=data)
            # self.fig = go.FigureWidget(data=data)

            #                go.Scatter(x=x, y=y, mode='markers', marker=dict(color='white', size=3, opacity=0.3)), show_link=False)
            self.fig.update_layout(  # square figure layout for plotly?
                autosize=True,
                margin=dict(l=20, r=20, t=30, b=20),
                width=800,
                height=800,
                # margin=dict(
                #     l=50,
                #     r=50,
                #     b=100,
                #     t=100,
                #     pad=4
                # ),
                # paper_bgcolor="White",
                # paper_bgcolor="LightSteelBlue",
            )

            self.tab = VBox([controls_box, self.fig,
                             download_row])  # plotly approach

            # app_layout = AppLayout(
            #     center= self.fig.canvas,
            #     # footer= self.frame_slider,
            #     pane_heights=[0, 6, 1]
            # )
            # self.tab = app_layout

            # self.tab = VBox([controls_box, self.fig.canvas, download_row])

#        self.fig = plt.figure(figsize=(self.figsize_width_substrate, self.figsize_height_substrate))

        else:
            # self.tab = VBox([row1, row2])
            self.tab = VBox([row1, row2, self.i_plot])

    #---------------------------------------------------
    def frame_slider_cb(self, b):
        print('---- frame_slider_cb:  frame=', self.frame_slider.value)

    #---------------------------------------------------
    def update_dropdown_fields(self, data_dir):
        # print('update_dropdown_fields called --------')
        self.output_dir = data_dir
        tree = None
        try:
            fname = os.path.join(self.output_dir, "initial.xml")
            tree = ET.parse(fname)
            xml_root = tree.getroot()
        except:
            print("Cannot open ", fname,
                  " to read info, e.g., names of substrate fields.")
            return

        xml_root = tree.getroot()
        self.field_min_max = {}
        self.field_dict = {}
        dropdown_options = {}
        uep = xml_root.find('.//variables')
        comment_str = ""
        field_idx = 0
        if (uep):
            for elm in uep.findall('variable'):
                # print("-----> ",elm.attrib['name'])
                self.field_min_max[elm.attrib['name']] = [0., 1.]
                self.field_dict[field_idx] = elm.attrib['name']
                dropdown_options[elm.attrib['name']] = field_idx
                field_idx += 1

#        constWidth = '180px'
# print('options=',dropdown_options)
        self.mcds_field.value = 0
        self.mcds_field.options = dropdown_options
#         self.mcds_field = Dropdown(
# #            options={'oxygen': 0, 'glucose': 1},
#             options=dropdown_options,
#             value=0,
#             #     description='Field',
#            layout=Layout(width=constWidth)
#         )

# def update_max_frames_expected(self, value):  # called when beginning an interactive Run
#     self.max_frames.value = value  # assumes naming scheme: "snapshot%08d.svg"
#     self.mcds_plot.children[0].max = self.max_frames.value

#------------------------------------------------------------------------------

    def gcf(self):
        """Get current figure, or create a new one.

        :return: :any:`Figure`
        """
        if self.figure is None:
            return figure()
        else:
            return self.figure

#------------------------------------------------------------------------------

    def update_params(self, config_tab, user_params_tab):
        # xml_root.find(".//x_min").text = str(self.xmin.value)
        # xml_root.find(".//x_max").text = str(self.xmax.value)
        # xml_root.find(".//dx").text = str(self.xdelta.value)
        # xml_root.find(".//y_min").text = str(self.ymin.value)
        # xml_root.find(".//y_max").text = str(self.ymax.value)
        # xml_root.find(".//dy").text = str(self.ydelta.value)
        # xml_root.find(".//z_min").text = str(self.zmin.value)
        # xml_root.find(".//z_max").text = str(self.zmax.value)
        # xml_root.find(".//dz").text = str(self.zdelta.value)

        self.xmin = config_tab.xmin.value
        self.xmax = config_tab.xmax.value
        self.x_range = self.xmax - self.xmin
        self.svg_xrange = self.xmax - self.xmin
        self.ymin = config_tab.ymin.value
        self.ymax = config_tab.ymax.value
        self.y_range = self.ymax - self.ymin

        self.numx = math.ceil(
            (self.xmax - self.xmin) / config_tab.xdelta.value)
        self.numy = math.ceil(
            (self.ymax - self.ymin) / config_tab.ydelta.value)

        if (self.x_range > self.y_range):
            ratio = self.y_range / self.x_range
            self.figsize_width_substrate = 15.0  # allow extra for colormap
            self.figsize_height_substrate = 12.5 * ratio
            self.figsize_width_svg = 12.0
            self.figsize_height_svg = 12.0 * ratio
        else:  # x < y
            ratio = self.x_range / self.y_range
            self.figsize_width_substrate = 15.0 * ratio
            self.figsize_height_substrate = 12.5
            self.figsize_width_svg = 12.0 * ratio
            self.figsize_height_svg = 12.0

        self.svg_flag = config_tab.toggle_svg.value
        self.substrates_flag = config_tab.toggle_mcds.value
        # print("substrates: update_params(): svg_flag, toggle=",self.svg_flag,config_tab.toggle_svg.value)
        # print("substrates: update_params(): self.substrates_flag = ",self.substrates_flag)
        self.svg_delta_t = config_tab.svg_interval.value
        self.substrate_delta_t = config_tab.mcds_interval.value
        self.modulo = int(self.substrate_delta_t / self.svg_delta_t)
        # print("substrates: update_params(): modulo=",self.modulo)

        if self.customized_output_freq:
            #            self.therapy_activation_time = user_params_tab.therapy_activation_time.value   # NOTE: edit for user param name
            # print("substrates: update_params(): therapy_activation_time=",self.therapy_activation_time)
            self.max_svg_frame_pre_therapy = int(self.therapy_activation_time /
                                                 self.svg_delta_t)
            self.max_substrate_frame_pre_therapy = int(
                self.therapy_activation_time / self.substrate_delta_t)

#------------------------------------------------------------------------------
#    def update(self, rdir):
#   Called from driver module (e.g., pc4*.py) (among other places?)

    def update(self, rdir=''):
        # with debug_view:
        #     print("substrates: update rdir=", rdir)
        # print("substrates: update rdir=", rdir)

        if rdir:
            self.output_dir = rdir

        # print('update(): self.output_dir = ', self.output_dir)

        if self.first_time:
            # if True:
            self.first_time = False
            full_xml_filename = Path(
                os.path.join(self.output_dir, 'config.xml'))
            # print("substrates: update(), config.xml = ",full_xml_filename)
            # self.num_svgs = len(glob.glob(os.path.join(self.output_dir, 'snap*.svg')))
            # self.num_substrates = len(glob.glob(os.path.join(self.output_dir, 'output*.xml')))
            # print("substrates: num_svgs,num_substrates =",self.num_svgs,self.num_substrates)
            # argh - no! If no files created, then denom = -1
            # self.modulo = int((self.num_svgs - 1) / (self.num_substrates - 1))
            # print("substrates: update(): modulo=",self.modulo)
            if full_xml_filename.is_file():
                tree = ET.parse(
                    full_xml_filename
                )  # this file cannot be overwritten; part of tool distro
                xml_root = tree.getroot()
                self.svg_delta_t = int(xml_root.find(".//SVG//interval").text)
                self.substrate_delta_t = int(
                    xml_root.find(".//full_data//interval").text)
                # print("substrates: svg,substrate delta_t values=",self.svg_delta_t,self.substrate_delta_t)
                self.modulo = int(self.substrate_delta_t / self.svg_delta_t)
                # print("substrates: update(): modulo=",self.modulo)

        # all_files = sorted(glob.glob(os.path.join(self.output_dir, 'output*.xml')))  # if the substrates/MCDS

        all_files = sorted(
            glob.glob(os.path.join(self.output_dir, 'snap*.svg')))  # if .svg
        if len(all_files) > 0:
            last_file = all_files[-1]
            self.max_frames.value = int(
                last_file[-12:-4])  # assumes naming scheme: "snapshot%08d.svg"
        else:
            substrate_files = sorted(
                glob.glob(os.path.join(self.output_dir, 'output*.xml')))
            if len(substrate_files) > 0:
                last_file = substrate_files[-1]
                self.max_frames.value = int(last_file[-12:-4])

    def download_svg_cb(self):
        file_str = os.path.join(self.output_dir, '*.svg')
        # print('zip up all ',file_str)
        with zipfile.ZipFile('svg.zip', 'w') as myzip:
            for f in glob.glob(file_str):
                myzip.write(f, os.path.basename(
                    f))  # 2nd arg avoids full filename path in the archive

    def download_cb(self):
        file_xml = os.path.join(self.output_dir, '*.xml')
        file_mat = os.path.join(self.output_dir, '*.mat')
        # print('zip up all ',file_str)
        with zipfile.ZipFile('mcds.zip', 'w') as myzip:
            for f in glob.glob(file_xml):
                myzip.write(f, os.path.basename(
                    f))  # 2nd arg avoids full filename path in the archive
            for f in glob.glob(file_mat):
                myzip.write(f, os.path.basename(f))

    def update_max_frames(self, _b):
        self.i_plot.children[0].max = self.max_frames.value

    def mcds_field_changed_cb(self, b):
        # print("mcds_field_changed_cb: self.mcds_field.value=",self.mcds_field.value)
        if (self.mcds_field.value == None):
            return
        self.field_index = self.mcds_field.value + 4

        field_name = self.field_dict[self.mcds_field.value]
        # print('mcds_field_cb: '+field_name)
        self.cmap_min.value = self.field_min_max[field_name][0]
        self.cmap_max.value = self.field_min_max[field_name][1]
        self.i_plot.update()

    def mcds_field_cb(self, b):
        #self.field_index = self.mcds_field.value
        #        self.field_index = self.mcds_field.options.index(self.mcds_field.value) + 4
        #        self.field_index = self.mcds_field.options[self.mcds_field.value]
        self.field_index = self.mcds_field.value + 4

        # field_name = self.mcds_field.options[self.mcds_field.value]
        # self.cmap_min.value = self.field_min_max[field_name][0]  # oxygen, etc
        # self.cmap_max.value = self.field_min_max[field_name][1]  # oxygen, etc

        #        self.field_index = self.mcds_field.value + 4

        #        print('field_index=',self.field_index)
        self.i_plot.update()

    #---------------------------------------------------------------------------
    def circles(self, x, y, s, c='b', vmin=None, vmax=None, **kwargs):
        """
        See https://gist.github.com/syrte/592a062c562cd2a98a83 

        Make a scatter plot of circles. 
        Similar to plt.scatter, but the size of circles are in data scale.
        Parameters
        ----------
        x, y : scalar or array_like, shape (n, )
            Input data
        s : scalar or array_like, shape (n, ) 
            Radius of circles.
        c : color or sequence of color, optional, default : 'b'
            `c` can be a single color format string, or a sequence of color
            specifications of length `N`, or a sequence of `N` numbers to be
            mapped to colors using the `cmap` and `norm` specified via kwargs.
            Note that `c` should not be a single numeric RGB or RGBA sequence 
            because that is indistinguishable from an array of values
            to be colormapped. (If you insist, use `color` instead.)  
            `c` can be a 2-D array in which the rows are RGB or RGBA, however. 
        vmin, vmax : scalar, optional, default: None
            `vmin` and `vmax` are used in conjunction with `norm` to normalize
            luminance data.  If either are `None`, the min and max of the
            color array is used.
        kwargs : `~matplotlib.collections.Collection` properties
            Eg. alpha, edgecolor(ec), facecolor(fc), linewidth(lw), linestyle(ls), 
            norm, cmap, transform, etc.
        Returns
        -------
        paths : `~matplotlib.collections.PathCollection`
        Examples
        --------
        a = np.arange(11)
        circles(a, a, s=a*0.2, c=a, alpha=0.5, ec='none')
        plt.colorbar()
        License
        --------
        This code is under [The BSD 3-Clause License]
        (http://opensource.org/licenses/BSD-3-Clause)
        """

        if np.isscalar(c):
            kwargs.setdefault('color', c)
            c = None

        if 'fc' in kwargs:
            kwargs.setdefault('facecolor', kwargs.pop('fc'))
        if 'ec' in kwargs:
            kwargs.setdefault('edgecolor', kwargs.pop('ec'))
        if 'ls' in kwargs:
            kwargs.setdefault('linestyle', kwargs.pop('ls'))
        if 'lw' in kwargs:
            kwargs.setdefault('linewidth', kwargs.pop('lw'))
        # You can set `facecolor` with an array for each patch,
        # while you can only set `facecolors` with a value for all.

        zipped = np.broadcast(x, y, s)
        patches = [Circle((x_, y_), s_) for x_, y_, s_ in zipped]
        collection = PatchCollection(patches, **kwargs)
        if c is not None:
            c = np.broadcast_to(c, zipped.shape).ravel()
            collection.set_array(c)
            collection.set_clim(vmin, vmax)

        ax = plt.gca()
        ax.add_collection(collection)
        ax.autoscale_view()
        # plt.draw_if_interactive()
        if c is not None:
            plt.sci(collection)
        # return collection

    #------------------------------------------------------------
    # def plot_svg(self, frame, rdel=''):
    def plot_svg(self, frame):
        # global current_idx, axes_max
        global current_frame
        current_frame = frame
        fname = "snapshot%08d.svg" % frame
        full_fname = os.path.join(self.output_dir, fname)
        # with debug_view:
        # print("plot_svg:", full_fname)
        # print("-- plot_svg:", full_fname)
        if not os.path.isfile(full_fname):
            print("Once output files are generated, click the slider.")
            return

        xlist = deque()
        ylist = deque()
        rlist = deque()
        rgb_list = deque()

        #  print('\n---- ' + fname + ':')
        #        tree = ET.parse(fname)
        tree = ET.parse(full_fname)
        root = tree.getroot()
        #  print('--- root.tag ---')
        #  print(root.tag)
        #  print('--- root.attrib ---')
        #  print(root.attrib)
        #  print('--- child.tag, child.attrib ---')
        numChildren = 0
        for child in root:
            #    print(child.tag, child.attrib)
            #    print("keys=",child.attrib.keys())
            if self.use_defaults and ('width' in child.attrib.keys()):
                self.axes_max = float(child.attrib['width'])
                # print("debug> found width --> axes_max =", axes_max)
            if child.text and "Current time" in child.text:
                svals = child.text.split()
                # remove the ".00" on minutes
                self.title_str += "   cells: " + svals[2] + "d, " + svals[
                    4] + "h, " + svals[7][:-3] + "m"

                # self.cell_time_mins = int(svals[2])*1440 + int(svals[4])*60 + int(svals[7][:-3])
                # self.title_str += "   cells: " + str(self.cell_time_mins) + "m"   # rwh

            # print("width ",child.attrib['width'])
            # print('attrib=',child.attrib)
            # if (child.attrib['id'] == 'tissue'):
            if ('id' in child.attrib.keys()):
                # print('-------- found tissue!!')
                tissue_parent = child
                break

        # print('------ search tissue')
        cells_parent = None

        for child in tissue_parent:
            # print('attrib=',child.attrib)
            if (child.attrib['id'] == 'cells'):
                # print('-------- found cells, setting cells_parent')
                cells_parent = child
                break
            numChildren += 1

        num_cells = 0
        #  print('------ search cells')
        for child in cells_parent:
            #    print(child.tag, child.attrib)
            #    print('attrib=',child.attrib)
            for circle in child:  # two circles in each child: outer + nucleus
                #  circle.attrib={'cx': '1085.59','cy': '1225.24','fill': 'rgb(159,159,96)','r': '6.67717','stroke': 'rgb(159,159,96)','stroke-width': '0.5'}
                #      print('  --- cx,cy=',circle.attrib['cx'],circle.attrib['cy'])
                xval = float(circle.attrib['cx'])

                # map SVG coords into comp domain
                # xval = (xval-self.svg_xmin)/self.svg_xrange * self.x_range + self.xmin
                xval = xval / self.x_range * self.x_range + self.xmin

                s = circle.attrib['fill']
                # print("s=",s)
                # print("type(s)=",type(s))
                if (s[0:3] == "rgb"
                    ):  # if an rgb string, e.g. "rgb(175,175,80)"
                    rgb = list(map(int, s[4:-1].split(",")))
                    rgb[:] = [x / 255. for x in rgb]
                else:  # otherwise, must be a color name
                    rgb_tuple = mplc.to_rgb(mplc.cnames[s])  # a tuple
                    rgb = [x for x in rgb_tuple]

                # test for bogus x,y locations (rwh TODO: use max of domain?)
                too_large_val = 10000.
                if (np.fabs(xval) > too_large_val):
                    print("bogus xval=", xval)
                    break
                yval = float(circle.attrib['cy'])
                # yval = (yval - self.svg_xmin)/self.svg_xrange * self.y_range + self.ymin
                yval = yval / self.y_range * self.y_range + self.ymin
                if (np.fabs(yval) > too_large_val):
                    print("bogus xval=", xval)
                    break

                rval = float(circle.attrib['r'])
                # if (rgb[0] > rgb[1]):
                #     print(num_cells,rgb, rval)
                xlist.append(xval)
                ylist.append(yval)
                rlist.append(rval)
                rgb_list.append(rgb)

                # For .svg files with cells that *have* a nucleus, there will be a 2nd
                if (not self.show_nucleus):
                    #if (not self.show_nucleus):
                    break

            num_cells += 1

            # if num_cells > 3:   # for debugging
            #   print(fname,':  num_cells= ',num_cells," --- debug exit.")
            #   sys.exit(1)
            #   break

            # print(fname,':  num_cells= ',num_cells)

        xvals = np.array(xlist)
        yvals = np.array(ylist)
        rvals = np.array(rlist)
        rgbs = np.array(rgb_list)
        # print("xvals[0:5]=",xvals[0:5])
        # print("rvals[0:5]=",rvals[0:5])
        # print("rvals.min, max=",rvals.min(),rvals.max())

        # rwh - is this where I change size of render window?? (YES - yipeee!)
        #   plt.figure(figsize=(6, 6))
        #   plt.cla()
        # if (self.substrates_toggle.value):
        self.title_str += " (" + str(num_cells) + " agents)"
        # title_str = " (" + str(num_cells) + " agents)"
        # else:
        # mins= round(int(float(root.find(".//current_time").text)))  # TODO: check units = mins
        # hrs = int(mins/60)
        # days = int(hrs/24)
        # title_str = '%dd, %dh, %dm' % (int(days),(hrs%24), mins - (hrs*60))
        plt.title(self.title_str)

        plt.xlim(self.xmin, self.xmax)
        plt.ylim(self.ymin, self.ymax)

        #   plt.xlim(axes_min,axes_max)
        #   plt.ylim(axes_min,axes_max)
        #   plt.scatter(xvals,yvals, s=rvals*scale_radius, c=rgbs)

        # TODO: make figsize a function of plot_size? What about non-square plots?
        # self.fig = plt.figure(figsize=(9, 9))

        #        axx = plt.axes([0, 0.05, 0.9, 0.9])  # left, bottom, width, height
        #        axx = fig.gca()
        #        print('fig.dpi=',fig.dpi) # = 72

        #   im = ax.imshow(f.reshape(100,100), interpolation='nearest', cmap=cmap, extent=[0,20, 0,20])
        #   ax.xlim(axes_min,axes_max)
        #   ax.ylim(axes_min,axes_max)

        # convert radii to radii in pixels
        # ax2 = self.fig.gca()
        # N = len(xvals)
        # rr_pix = (ax2.transData.transform(np.vstack([rvals, rvals]).T) -
        #             ax2.transData.transform(np.vstack([np.zeros(N), np.zeros(N)]).T))
        # rpix, _ = rr_pix.T

        # markers_size = (144. * rpix / self.fig.dpi)**2   # = (2*rpix / fig.dpi * 72)**2
        # markers_size = markers_size/4000000.
        # print('max=',markers_size.max())

        #rwh - temp fix - Ah, error only occurs when "edges" is toggled on
        if (self.show_edge):
            try:
                # plt.scatter(xvals,yvals, s=markers_size, c=rgbs, edgecolor='black', linewidth=0.5)
                self.circles(xvals,
                             yvals,
                             s=rvals,
                             color=rgbs,
                             edgecolor='black',
                             linewidth=0.5)
                # cell_circles = self.circles(xvals,yvals, s=rvals, color=rgbs, edgecolor='black', linewidth=0.5)
                # plt.sci(cell_circles)
            except (ValueError):
                pass
        else:
            # plt.scatter(xvals,yvals, s=markers_size, c=rgbs)
            self.circles(xvals, yvals, s=rvals, color=rgbs)

        # if (self.show_tracks):
        #     for key in self.trackd.keys():
        #         xtracks = self.trackd[key][:,0]
        #         ytracks = self.trackd[key][:,1]
        #         plt.plot(xtracks[0:frame],ytracks[0:frame],  linewidth=5)

        # plt.xlim(self.axes_min, self.axes_max)
        # plt.ylim(self.axes_min, self.axes_max)
        #   ax.grid(False)
#        axx.set_title(title_str)
# plt.title(title_str)

#---------------------------------------------------------------------------
# assume "frame" is cell frame #, unless Cells is togggled off, then it's the substrate frame #
# def plot_substrate(self, frame, grid):

    def plot_substrate(self, frame):
        # def plot_substrate(self, b):
        # global current_idx, axes_max, gFileId, field_index
        frame = self.frame_slider.value
        print('--- plot_substrate: frame =', frame)

        # print("plot_substrate(): frame*self.substrate_delta_t  = ",frame*self.substrate_delta_t)
        # print("plot_substrate(): frame*self.svg_delta_t  = ",frame*self.svg_delta_t)
        self.title_str = ''

        if (self.substrates_toggle.value):
            # self.fig = plt.figure(figsize=(14, 15.6))
            # self.fig = plt.figure(figsize=(15.0, 12.5))

            # self.fig = plt.figure(figsize=(self.figsize_width_substrate, self.figsize_height_substrate))

            # rwh - funky way to figure out substrate frame for pc4cancerbots (due to user-defined "save_interval*")
            # self.cell_time_mins
            # self.substrate_frame = int(frame / self.modulo)
            if (self.customized_output_freq
                    and (frame > self.max_svg_frame_pre_therapy)):
                self.substrate_frame = self.max_substrate_frame_pre_therapy + (
                    frame - self.max_svg_frame_pre_therapy)
            else:
                self.substrate_frame = int(frame / self.modulo)

            fname = "output%08d_microenvironment0.mat" % self.substrate_frame
            xml_fname = "output%08d.xml" % self.substrate_frame
            # fullname = output_dir_str + fname

            #        fullname = fname
            full_fname = os.path.join(self.output_dir, fname)
            # print("--- plot_substrate(): full_fname=",full_fname)
            full_xml_fname = os.path.join(self.output_dir, xml_fname)
            #        self.output_dir = '.'

            #        if not os.path.isfile(fullname):
            if not os.path.isfile(full_fname):
                print("Once output files are generated, click the slider."
                      )  # No:  output00000000_microenvironment0.mat
                return

            tree = ET.parse(full_xml_fname)
            xml_root = tree.getroot()
            mins = round(int(float(xml_root.find(
                ".//current_time").text)))  # TODO: check units = mins
            self.substrate_mins = round(
                int(float(xml_root.find(
                    ".//current_time").text)))  # TODO: check units = mins

            hrs = int(mins / 60)
            days = int(hrs / 24)
            self.title_str = 'substrate: %dd, %dh, %dm' % (int(days),
                                                           (hrs % 24), mins -
                                                           (hrs * 60))

            info_dict = {}
            #        scipy.io.loadmat(fullname, info_dict)
            scipy.io.loadmat(full_fname, info_dict)
            M = info_dict['multiscale_microenvironment']
            #     global_field_index = int(mcds_field.value)
            #     print('plot_substrate: field_index =',field_index)
            f = M[
                self.
                field_index, :]  # 4=tumor cells field, 5=blood vessel density, 6=growth substrate

            try:
                xgrid = M[0, :].reshape(self.numy, self.numx)
                ygrid = M[1, :].reshape(self.numy, self.numx)
            except:
                print(
                    "substrates.py: mismatched mesh size for reshape: numx,numy=",
                    self.numx, self.numy)
                pass
#                xgrid = M[0, :].reshape(self.numy, self.numx)
#                ygrid = M[1, :].reshape(self.numy, self.numx)

            num_contours = 15
            levels = MaxNLocator(nbins=num_contours).tick_values(
                self.cmap_min.value, self.cmap_max.value)
            contour_ok = True
            if (self.cmap_fixed.value):
                try:
                    # substrate_plot = main_ax.contourf(xgrid, ygrid, M[self.field_index, :].reshape(self.numy, self.numx), levels=levels, extend='both', cmap=self.field_cmap.value, fontsize=self.fontsize)
                    substrate_plot = plt.contourf(
                        xgrid,
                        ygrid,
                        M[self.field_index, :].reshape(self.numy, self.numx),
                        levels=levels,
                        extend='both',
                        cmap=self.field_cmap.value,
                        fontsize=self.fontsize)
                except:
                    contour_ok = False
                    # print('got error on contourf 1.')
            else:
                try:
                    # substrate_plot = main_ax.contourf(xgrid, ygrid, M[self.field_index, :].reshape(self.numy,self.numx), num_contours, cmap=self.field_cmap.value)

                    # substrate_plot = plt.contourf(xgrid, ygrid, M[self.field_index, :].reshape(self.numy,self.numx), num_contours, cmap=self.field_cmap.value)

                    # plotly version
                    print('-- new plotly plot')
                    self.x = np.random.randn(frame * 200 + 1000)
                    self.y = np.random.randn(frame * 200 + 1000)
                    self.fig.data[0]['x'] = self.x
                    self.fig.data[0]['y'] = self.y
                    # self.fig = go.FigureWidget(data=go.Histogram2dContour(x=x, y=y, contours=dict(coloring='heatmap')))
                    # self.fig = go.FigureWidget(data=go.Histogram2dContour(x=x, y=y, contours=dict(coloring='heatmap')))
                    # self.fig = go.FigureWidget(data=go.Contour(x=x, y=y, contours=dict(coloring='heatmap')))
                    # self.fig.show()   # creates a *new* figure!

                    contour_ok = False

                except:
                    contour_ok = False
                    # print('got error on contourf 2.')

            if (contour_ok):
                # main_ax.set_title(self.title_str, fontsize=self.fontsize)
                plt.title(self.title_str, fontsize=self.fontsize)
                # main_ax.tick_params(labelsize=self.fontsize)
                # cbar = plt.colorbar(my_plot)
                # cbar = self.fig.colorbar(substrate_plot, ax=main_ax)
                cbar = self.fig.colorbar(substrate_plot)
                cbar.ax.tick_params(labelsize=self.fontsize)

            # plt.xlim(self.xmin, self.xmax)
            # plt.ylim(self.ymin, self.ymax)

        # Now plot the cells (possibly on top of the substrate)
        if (self.cells_toggle.value):
            if (not self.substrates_toggle.value):
                # self.fig = plt.figure(figsize=(12, 12))
                self.fig = plt.figure(figsize=(self.figsize_width_svg,
                                               self.figsize_height_svg))
            # self.plot_svg(frame)
            self.svg_frame = frame
            # print('plot_svg with frame=',self.svg_frame)
            self.plot_svg(self.svg_frame)
Exemple #30
0
    def __init__(self):

        micron_units = Label(
            'micron')  # use "option m" (Mac, for micro symbol)

        constWidth = '180px'
        tab_height = '500px'
        stepsize = 10

        #style = {'description_width': '250px'}
        style = {'description_width': '25%'}
        layout = {'width': '400px'}

        name_button_layout = {'width': '25%'}
        widget_layout = {'width': '15%'}
        units_button_layout = {'width': '15%'}
        desc_button_layout = {'width': '45%'}
        divider_button_layout = {'width': '40%'}

        param_name1 = Button(description='random_seed',
                             disabled=True,
                             layout=name_button_layout)
        param_name1.style.button_color = 'lightgreen'

        self.random_seed = IntText(value=0,
                                   step=1,
                                   style=style,
                                   layout=widget_layout)

        div_row1 = Button(description='---Virus Replication---',
                          disabled=True,
                          layout=divider_button_layout)

        param_name2 = Button(description='virion_uncoating_rate',
                             disabled=True,
                             layout=name_button_layout)
        param_name2.style.button_color = 'tan'

        self.virion_uncoating_rate = FloatText(value=0.01,
                                               step=0.001,
                                               style=style,
                                               layout=widget_layout)

        param_name3 = Button(description='uncoated_to_RNA_rate',
                             disabled=True,
                             layout=name_button_layout)
        param_name3.style.button_color = 'lightgreen'

        self.uncoated_to_RNA_rate = FloatText(value=0.01,
                                              step=0.001,
                                              style=style,
                                              layout=widget_layout)

        param_name4 = Button(description='protein_synthesis_rate',
                             disabled=True,
                             layout=name_button_layout)
        param_name4.style.button_color = 'tan'

        self.protein_synthesis_rate = FloatText(value=0.01,
                                                step=0.001,
                                                style=style,
                                                layout=widget_layout)

        param_name5 = Button(description='virion_assembly_rate',
                             disabled=True,
                             layout=name_button_layout)
        param_name5.style.button_color = 'lightgreen'

        self.virion_assembly_rate = FloatText(value=0.01,
                                              step=0.001,
                                              style=style,
                                              layout=widget_layout)

        div_row2 = Button(description='---Virus Adsorption and Export---',
                          disabled=True,
                          layout=divider_button_layout)

        param_name6 = Button(description='virion_export_rate',
                             disabled=True,
                             layout=name_button_layout)
        param_name6.style.button_color = 'tan'

        self.virion_export_rate = FloatText(value=0.01,
                                            step=0.001,
                                            style=style,
                                            layout=widget_layout)

        div_row3 = Button(
            description='---ACE2 receptor dynamics with virus binding---',
            disabled=True,
            layout=divider_button_layout)

        param_name7 = Button(description='ACE2_receptors_per_cell',
                             disabled=True,
                             layout=name_button_layout)
        param_name7.style.button_color = 'lightgreen'

        self.ACE2_receptors_per_cell = FloatText(value=1000,
                                                 step=100,
                                                 style=style,
                                                 layout=widget_layout)

        param_name8 = Button(description='ACE2_binding_rate',
                             disabled=True,
                             layout=name_button_layout)
        param_name8.style.button_color = 'tan'

        self.ACE2_binding_rate = FloatText(value=0.001,
                                           step=0.0001,
                                           style=style,
                                           layout=widget_layout)

        param_name9 = Button(description='ACE2_endocytosis_rate',
                             disabled=True,
                             layout=name_button_layout)
        param_name9.style.button_color = 'lightgreen'

        self.ACE2_endocytosis_rate = FloatText(value=0.01,
                                               step=0.001,
                                               style=style,
                                               layout=widget_layout)

        param_name10 = Button(description='ACE2_cargo_release_rate',
                              disabled=True,
                              layout=name_button_layout)
        param_name10.style.button_color = 'tan'

        self.ACE2_cargo_release_rate = FloatText(value=0.001,
                                                 step=0.0001,
                                                 style=style,
                                                 layout=widget_layout)

        param_name11 = Button(description='ACE2_recycling_rate',
                              disabled=True,
                              layout=name_button_layout)
        param_name11.style.button_color = 'lightgreen'

        self.ACE2_recycling_rate = FloatText(value=0.01,
                                             step=0.001,
                                             style=style,
                                             layout=widget_layout)

        div_row4 = Button(description='---Apoptotic Response---',
                          disabled=True,
                          layout=divider_button_layout)

        param_name12 = Button(description='max_infected_apoptosis_rate',
                              disabled=True,
                              layout=name_button_layout)
        param_name12.style.button_color = 'tan'

        self.max_infected_apoptosis_rate = FloatText(value=0.001,
                                                     step=0.0001,
                                                     style=style,
                                                     layout=widget_layout)

        param_name13 = Button(description='max_apoptosis_half_max',
                              disabled=True,
                              layout=name_button_layout)
        param_name13.style.button_color = 'lightgreen'

        self.max_apoptosis_half_max = FloatText(value=500,
                                                step=10,
                                                style=style,
                                                layout=widget_layout)

        param_name14 = Button(description='apoptosis_hill_power',
                              disabled=True,
                              layout=name_button_layout)
        param_name14.style.button_color = 'tan'

        self.apoptosis_hill_power = FloatText(value=1,
                                              step=0.1,
                                              style=style,
                                              layout=widget_layout)

        param_name15 = Button(description='virus_fraction_released_at_death',
                              disabled=True,
                              layout=name_button_layout)
        param_name15.style.button_color = 'lightgreen'

        self.virus_fraction_released_at_death = FloatText(value=0,
                                                          step=0.01,
                                                          style=style,
                                                          layout=widget_layout)

        div_row5 = Button(description='---Initialization Options--',
                          disabled=True,
                          layout=divider_button_layout)

        param_name16 = Button(description='multiplicity_of_infection',
                              disabled=True,
                              layout=name_button_layout)
        param_name16.style.button_color = 'tan'

        self.multiplicity_of_infection = FloatText(value=0.01,
                                                   step=0.001,
                                                   style=style,
                                                   layout=widget_layout)

        param_name17 = Button(description='use_single_infected_cell',
                              disabled=True,
                              layout=name_button_layout)
        param_name17.style.button_color = 'lightgreen'

        self.use_single_infected_cell = Checkbox(value=False,
                                                 style=style,
                                                 layout=widget_layout)

        div_row6 = Button(description='---Visualization Options---',
                          disabled=True,
                          layout=divider_button_layout)

        param_name18 = Button(description='color_variable',
                              disabled=True,
                              layout=name_button_layout)
        param_name18.style.button_color = 'tan'

        self.color_variable = Text(value='assembled virion',
                                   style=style,
                                   layout=widget_layout)

        units_button1 = Button(description='',
                               disabled=True,
                               layout=units_button_layout)
        units_button1.style.button_color = 'lightgreen'
        units_button2 = Button(description='',
                               disabled=True,
                               layout=units_button_layout)
        units_button2.style.button_color = 'lightgreen'
        units_button3 = Button(description='1/min',
                               disabled=True,
                               layout=units_button_layout)
        units_button3.style.button_color = 'tan'
        units_button4 = Button(description='1/min',
                               disabled=True,
                               layout=units_button_layout)
        units_button4.style.button_color = 'lightgreen'
        units_button5 = Button(description='1/min',
                               disabled=True,
                               layout=units_button_layout)
        units_button5.style.button_color = 'tan'
        units_button6 = Button(description='1/min',
                               disabled=True,
                               layout=units_button_layout)
        units_button6.style.button_color = 'lightgreen'
        units_button7 = Button(description='',
                               disabled=True,
                               layout=units_button_layout)
        units_button7.style.button_color = 'lightgreen'
        units_button8 = Button(description='1/min',
                               disabled=True,
                               layout=units_button_layout)
        units_button8.style.button_color = 'tan'
        units_button9 = Button(description='',
                               disabled=True,
                               layout=units_button_layout)
        units_button9.style.button_color = 'tan'
        units_button10 = Button(description='',
                                disabled=True,
                                layout=units_button_layout)
        units_button10.style.button_color = 'lightgreen'
        units_button11 = Button(description='1/min',
                                disabled=True,
                                layout=units_button_layout)
        units_button11.style.button_color = 'tan'
        units_button12 = Button(description='1/min',
                                disabled=True,
                                layout=units_button_layout)
        units_button12.style.button_color = 'lightgreen'
        units_button13 = Button(description='1/min',
                                disabled=True,
                                layout=units_button_layout)
        units_button13.style.button_color = 'tan'
        units_button14 = Button(description='1/min',
                                disabled=True,
                                layout=units_button_layout)
        units_button14.style.button_color = 'lightgreen'
        units_button15 = Button(description='',
                                disabled=True,
                                layout=units_button_layout)
        units_button15.style.button_color = 'lightgreen'
        units_button16 = Button(description='1/min',
                                disabled=True,
                                layout=units_button_layout)
        units_button16.style.button_color = 'tan'
        units_button17 = Button(description='virion',
                                disabled=True,
                                layout=units_button_layout)
        units_button17.style.button_color = 'lightgreen'
        units_button18 = Button(description='',
                                disabled=True,
                                layout=units_button_layout)
        units_button18.style.button_color = 'tan'
        units_button19 = Button(description='',
                                disabled=True,
                                layout=units_button_layout)
        units_button19.style.button_color = 'lightgreen'
        units_button20 = Button(description='',
                                disabled=True,
                                layout=units_button_layout)
        units_button20.style.button_color = 'lightgreen'
        units_button21 = Button(description='',
                                disabled=True,
                                layout=units_button_layout)
        units_button21.style.button_color = 'tan'
        units_button22 = Button(description='',
                                disabled=True,
                                layout=units_button_layout)
        units_button22.style.button_color = 'lightgreen'
        units_button23 = Button(description='',
                                disabled=True,
                                layout=units_button_layout)
        units_button23.style.button_color = 'lightgreen'
        units_button24 = Button(description='',
                                disabled=True,
                                layout=units_button_layout)
        units_button24.style.button_color = 'tan'

        desc_button1 = Button(description='',
                              disabled=True,
                              layout=desc_button_layout)
        desc_button1.style.button_color = 'lightgreen'
        desc_button2 = Button(
            description='rate at which an internalized virion is uncoated',
            disabled=True,
            layout=desc_button_layout)
        desc_button2.style.button_color = 'tan'
        desc_button3 = Button(
            description=
            'rate at which uncoated virion makes its mRNA available',
            disabled=True,
            layout=desc_button_layout)
        desc_button3.style.button_color = 'lightgreen'
        desc_button4 = Button(
            description='rate at mRNA creates complete set of proteins',
            disabled=True,
            layout=desc_button_layout)
        desc_button4.style.button_color = 'tan'
        desc_button5 = Button(
            description=
            'rate at which viral proteins are assembled into complete virion',
            disabled=True,
            layout=desc_button_layout)
        desc_button5.style.button_color = 'lightgreen'
        desc_button6 = Button(
            description='rate at which a virion is exported from a live cell',
            disabled=True,
            layout=desc_button_layout)
        desc_button6.style.button_color = 'tan'
        desc_button7 = Button(description='number of ACE2 receptors per cell',
                              disabled=True,
                              layout=desc_button_layout)
        desc_button7.style.button_color = 'lightgreen'
        desc_button8 = Button(description='ACE2 receptor-virus binding rate',
                              disabled=True,
                              layout=desc_button_layout)
        desc_button8.style.button_color = 'tan'
        desc_button9 = Button(
            description='ACE2 receptor-virus endocytosis rate',
            disabled=True,
            layout=desc_button_layout)
        desc_button9.style.button_color = 'lightgreen'
        desc_button10 = Button(
            description='ACE2 receptor-virus cargo release rate',
            disabled=True,
            layout=desc_button_layout)
        desc_button10.style.button_color = 'tan'
        desc_button11 = Button(description='ACE2 receptor recycling rate',
                               disabled=True,
                               layout=desc_button_layout)
        desc_button11.style.button_color = 'lightgreen'
        desc_button12 = Button(
            description='maximum rate of cell apoptosis due to viral infection',
            disabled=True,
            layout=desc_button_layout)
        desc_button12.style.button_color = 'tan'
        desc_button13 = Button(
            description=
            'viral load at which cells reach half max apoptosis rate',
            disabled=True,
            layout=desc_button_layout)
        desc_button13.style.button_color = 'lightgreen'
        desc_button14 = Button(
            description='Hill power for viral load apoptosis response',
            disabled=True,
            layout=desc_button_layout)
        desc_button14.style.button_color = 'tan'
        desc_button15 = Button(
            description='fraction of internal virus released at cell death',
            disabled=True,
            layout=desc_button_layout)
        desc_button15.style.button_color = 'lightgreen'
        desc_button16 = Button(
            description='multiplicity of infection: virions/cells at t=0',
            disabled=True,
            layout=desc_button_layout)
        desc_button16.style.button_color = 'tan'
        desc_button17 = Button(
            description='Infect center cell with one virion (overrides MOI)',
            disabled=True,
            layout=desc_button_layout)
        desc_button17.style.button_color = 'lightgreen'
        desc_button18 = Button(
            description='color cells based on this variable',
            disabled=True,
            layout=desc_button_layout)
        desc_button18.style.button_color = 'tan'

        row1 = [param_name1, self.random_seed, units_button1, desc_button1]
        row2 = [
            param_name2, self.virion_uncoating_rate, units_button3,
            desc_button2
        ]
        row3 = [
            param_name3, self.uncoated_to_RNA_rate, units_button4, desc_button3
        ]
        row4 = [
            param_name4, self.protein_synthesis_rate, units_button5,
            desc_button4
        ]
        row5 = [
            param_name5, self.virion_assembly_rate, units_button6, desc_button5
        ]
        row6 = [
            param_name6, self.virion_export_rate, units_button8, desc_button6
        ]
        row7 = [
            param_name7, self.ACE2_receptors_per_cell, units_button10,
            desc_button7
        ]
        row8 = [
            param_name8, self.ACE2_binding_rate, units_button11, desc_button8
        ]
        row9 = [
            param_name9, self.ACE2_endocytosis_rate, units_button12,
            desc_button9
        ]
        row10 = [
            param_name10, self.ACE2_cargo_release_rate, units_button13,
            desc_button10
        ]
        row11 = [
            param_name11, self.ACE2_recycling_rate, units_button14,
            desc_button11
        ]
        row12 = [
            param_name12, self.max_infected_apoptosis_rate, units_button16,
            desc_button12
        ]
        row13 = [
            param_name13, self.max_apoptosis_half_max, units_button17,
            desc_button13
        ]
        row14 = [
            param_name14, self.apoptosis_hill_power, units_button18,
            desc_button14
        ]
        row15 = [
            param_name15, self.virus_fraction_released_at_death,
            units_button19, desc_button15
        ]
        row16 = [
            param_name16, self.multiplicity_of_infection, units_button21,
            desc_button16
        ]
        row17 = [
            param_name17, self.use_single_infected_cell, units_button22,
            desc_button17
        ]
        row18 = [
            param_name18, self.color_variable, units_button24, desc_button18
        ]

        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='100%')
        box1 = Box(children=row1, layout=box_layout)
        box2 = Box(children=row2, layout=box_layout)
        box3 = Box(children=row3, layout=box_layout)
        box4 = Box(children=row4, layout=box_layout)
        box5 = Box(children=row5, layout=box_layout)
        box6 = Box(children=row6, layout=box_layout)
        box7 = Box(children=row7, layout=box_layout)
        box8 = Box(children=row8, layout=box_layout)
        box9 = Box(children=row9, layout=box_layout)
        box10 = Box(children=row10, layout=box_layout)
        box11 = Box(children=row11, layout=box_layout)
        box12 = Box(children=row12, layout=box_layout)
        box13 = Box(children=row13, layout=box_layout)
        box14 = Box(children=row14, layout=box_layout)
        box15 = Box(children=row15, layout=box_layout)
        box16 = Box(children=row16, layout=box_layout)
        box17 = Box(children=row17, layout=box_layout)
        box18 = Box(children=row18, layout=box_layout)

        self.tab = VBox([
            box1,
            div_row1,
            box2,
            box3,
            box4,
            box5,
            div_row2,
            box6,
            div_row3,
            box7,
            box8,
            box9,
            box10,
            box11,
            div_row4,
            box12,
            box13,
            box14,
            box15,
            div_row5,
            box16,
            box17,
            div_row6,
            box18,
        ])
Exemple #31
0
    def settings_panel(self):
        # getMolMap calulation settings.  NOTE: should only be called once.
        margin = 2

        num_angles_slider_text = "Number of Inverse Cone Angles to calculate:"
        num_angles_slider_widget = IntSlider(value=1, min=1, max=5)
        num_angles_slider = VBox(
            children=[HTML(value=num_angles_slider_text), num_angles_slider_widget], margin=margin, width="100%"
        )
        link((self.model, "num_angles"), (num_angles_slider_widget, "value"))
        sub_slider_text = "Subdivision value of the icosphere for numerical calculation:"
        sub_slider_widget = IntSlider(value=5, min=1, max=9)
        link((self.model, "sub"), (sub_slider_widget, "value"))
        sub_slider = VBox(children=[HTML(value=sub_slider_text), sub_slider_widget], margin=margin, width="100%")
        #        link((sub_slider, 'value'), (i, 'value'))
        #        print(self.width)
        #        sub_slider.align = 'center'
        #        sub_slider.width = '100%'
        #        sub_slider.border_color = 'black'
        #        sub_slider.border_width = 2

        radius_slider_text = "Cut radius measured from the central atom:"
        radius_slider_widget = FloatSlider(value=0, min=0, max=10)
        link((self.model, "radius"), (radius_slider_widget, "value"))
        radius_slider = VBox(children=[HTML(value=radius_slider_text), radius_slider_widget], margin=margin)

        atomradscale_slider_text = "Atomic radius scaling factor:"
        atomradscale_slider_widget = FloatSlider(value=1, min=0, max=4)
        link((self.model, "rad_scale"), (atomradscale_slider_widget, "value"))
        atomradscale_slider = VBox(
            children=[HTML(value=atomradscale_slider_text), atomradscale_slider_widget], margin=margin
        )

        excludeH_button = Checkbox(description="Exclude H from every geometry:")
        link((self.model, "excludeH"), (excludeH_button, "value"))
        excludeH_button.on_trait_change(self.excludeH_changed, "value")

        dont = "Don't exclude any elements"
        self.dont = dont
        # TODO: Syncronize exclude_list_widget with excludeH button and define an event on the
        # model to filter out the `dont` text.
        # Alternatevily, separate this option into a checkbox and hide the exclude options
        # while the button is selected.
        exclude_list_text = "Exclude elements from every geometry:"
        exclude_list_widget = SelectMultiple(
            options=[dont] + [e.symbol for e in ELEMENTS],
            selected_labels=[dont],
            color="Black",
            font_size=14,
            height=120,
        )
        link((exclude_list_widget, "value"), (self.model, "excludes"))
        # The dirty old SelectMultiple widget does not have an .on_trait_change method.
        # So we create a new traitlet (excludes_notifier), which has an .on_trait_change method
        # because it inherits HasTraits. We link the 'value' trait to excludes_notifier.excludes;
        # and we bind the event handler to excludes_notifier.on_trait_change
        self.excludes_notifier = ExcludesNotifier()
        link((exclude_list_widget, "value"), (self.excludes_notifier, "excludes"))
        self.excludes_notifier.on_trait_change(self.excludes_changed)

        exclude_list = VBox(children=[HTML(value=exclude_list_text), exclude_list_widget], margin=margin)

        atomrad_button = ToggleButtons(
            description="Atomic radius type:",
            options=["vdwrad", "covrad", "atmrad"],
            background_color="AliceBlue",
            margin=margin,
        )
        link((self.model, "rad_type"), (atomrad_button, "value"))
        runbutton = Button(
            description="Run calculation!",
            tooltip="Click here to calculate Buried Volumes and Inverse Cone Angles!",
            margin=margin * 3,
            border_color="#9acfea",
            # border_radius=5,
            border_width=3,
            font_size=20,
        )
        runbutton.on_click(self.run_button_clicked)

        basic_tab = VBox(children=[atomrad_button, excludeH_button])
        sliders = VBox(children=[num_angles_slider, atomradscale_slider, radius_slider, sub_slider])
        sliders.width = "100%"
        sliders.pack = "center"

        advanced_tab = VBox(children=[atomrad_button, sliders, exclude_list])
        main_window = Tab(children=[basic_tab, advanced_tab])
        main_window.set_title(0, "Basic")
        main_window.set_title(1, "Advanced")

        return ControlPanel(
            title="getMolMap Settings:",
            children=[main_window, runbutton],
            border_width=2,
            border_radius=4,
            margin=10,
            padding=0,
        )
Exemple #32
0
    def _make_widget_repr(self):
        self.widget_repr_name = Text(value='', description='representation')
        self.widget_repr_name._ngl_name = 'repr_name_text'
        repr_selection = Text(value=' ', description='selection')
        repr_selection._ngl_name = 'repr_selection'
        repr_selection.width = self.widget_repr_name.width = default.DEFAULT_TEXT_WIDTH 

        max_n_components = max(self._view.n_components-1, 0)
        self.widget_component_slider = IntSlider(value=0, max=max_n_components, min=0, description='component')
        self.widget_component_slider._ngl_name = 'component_slider'

        cvalue = ' '
        self.widget_component_dropdown = Dropdown(value=cvalue, options=[cvalue,],
                description='component')
        self.widget_component_dropdown._ngl_name = 'component_dropdown'

        self.widget_repr_slider = IntSlider(value=0, description='representation', width=default.DEFAULT_SLIDER_WIDTH)
        self.widget_repr_slider._ngl_name = 'repr_slider'
        self.widget_repr_slider.visible = True

        self.widget_component_slider.layout.width = default.DEFAULT_SLIDER_WIDTH
        self.widget_repr_slider.layout.width = default.DEFAULT_SLIDER_WIDTH
        self.widget_component_dropdown.layout.width = self.widget_component_dropdown.max_width = default.DEFAULT_TEXT_WIDTH

        # turn off for now
        self.widget_component_dropdown.layout.display = 'none'
        self.widget_component_dropdown.description = ''

        # self.widget_accordion_repr_parameters = Accordion()
        self.widget_accordion_repr_parameters = Tab()
        self.widget_repr_parameters =  self._make_widget_repr_parameters(self.widget_component_slider,
                self.widget_repr_slider,
                self.widget_repr_name)
        self.widget_accordion_repr_parameters.children = [self.widget_repr_parameters, Box()]
        self.widget_accordion_repr_parameters.set_title(0, 'Parameters')
        self.widget_accordion_repr_parameters.set_title(1, 'Hide')
        self.widget_accordion_repr_parameters.selected_index = 1
        
        checkbox_reprlist = Checkbox(value=False, description='reprlist')
        checkbox_reprlist._ngl_name = 'checkbox_reprlist'
        self.widget_repr_choices = self._make_repr_name_choices(self.widget_component_slider,
                self.widget_repr_slider)
        self.widget_repr_choices._ngl_name = 'reprlist_choices'

        self.widget_repr_add = self._make_add_widget_repr(self.widget_component_slider)

        def on_update_checkbox_reprlist(change):
            self.widget_repr_choices.visible= change['new']
        checkbox_reprlist.observe(on_update_checkbox_reprlist, names='value')

        def on_repr_name_text_value_changed(change):
            name = change['new'].strip()
            old = change['old'].strip()

            should_update = (self._real_time_update
                             and old and name
                             and name in REPRESENTATION_NAMES
                             and name != change['old'].strip())

            if should_update:
                component=self.widget_component_slider.value
                repr_index=self.widget_repr_slider.value
                self._view._remote_call('setRepresentation',
                                 target='Widget',
                                 args=[change['new'], {}, component, repr_index])
                self._view._request_repr_parameters(component, repr_index)

        def on_component_or_repr_slider_value_changed(change):
            self._view._request_repr_parameters(component=self.widget_component_slider.value,
                                                repr_index=self.widget_repr_slider.value)
            self.widget_component_dropdown.options = tuple(self._view._ngl_component_names)

            if self.widget_accordion_repr_parameters.selected_index >= 0:
                self.widget_repr_parameters.name = self.widget_repr_name.value
                self.widget_repr_parameters.repr_index = self.widget_repr_slider.value
                self.widget_repr_parameters.component_index = self.widget_component_slider.value

        def on_repr_selection_value_changed(change):
            if self._real_time_update:
                component = self.widget_component_slider.value
                repr_index = self.widget_repr_slider.value
                self._view._set_selection(change['new'],
                                          component=component,
                                          repr_index=repr_index)

        def on_change_component_dropdown(change):
            choice = change['new']
            if choice:
                 self.widget_component_slider.value = self._view._ngl_component_names.index(choice)

        self.widget_component_dropdown.observe(on_change_component_dropdown, names='value')

        self.widget_repr_slider.observe(on_component_or_repr_slider_value_changed, names='value')
        self.widget_component_slider.observe(on_component_or_repr_slider_value_changed, names='value')
        self.widget_repr_name.observe(on_repr_name_text_value_changed, names='value')
        repr_selection.observe(on_repr_selection_value_changed, names='value')

        self.widget_repr_control_buttons = self._make_button_repr_control(self.widget_component_slider,
        self.widget_repr_slider, repr_selection)

        blank_box = Box([Label("")])

        all_kids = [self.widget_repr_control_buttons,
                    blank_box,
                    self.widget_repr_add,
                    self.widget_component_dropdown,
                    self.widget_repr_name,
                    repr_selection,
                    self.widget_component_slider,
                    self.widget_repr_slider,
                    self.widget_repr_choices,
                    self.widget_accordion_repr_parameters
        ]

        vbox = VBox(all_kids)

        self._view._request_repr_parameters(component=self.widget_component_slider.value,
            repr_index=self.widget_repr_slider.value)

        self.widget_repr = _relayout_master(vbox, width='100%')

        self._refresh(self.widget_component_slider, self.widget_repr_slider)

        setattr(self.widget_repr, "_saved_widgets", [])
        for _box in self.widget_repr.children:
            if hasattr(_box, 'children'):
                for kid in _box.children:
                    self.widget_repr._saved_widgets.append(kid)

        return self.widget_repr
Exemple #33
0
    def show_m():

        multipoly = []
        multycent = []
        geom = view_spatial.trasform_geometry(info_data)
        poly = geom['coordinates'][0][0]
        #     poly = view_spatial.swap_xy(geom['coordinates'][0])[0]
        multipoly.append(poly)
        cent = view_spatial.centroid(poly)
        multycent.append(cent)

        cent = view_spatial.centroid(multycent)
        m = Map(center=cent, zoom=16, basemap=basemaps.OpenStreetMap.Mapnik)

        polygon = Polygon(locations=multipoly,
                          name='Parcel polygon',
                          color="yellow",
                          fill_color=None)

        m.add_layer(polygon)
        basemap2 = basemap_to_tiles(basemaps.Esri.WorldImagery)

        poly_text = HTML()
        poly_text.value = f"""Parcel ID: {pid}<br>
                                    Crop name: {crop_name}<br>
                                    Area: {area:.2f} sqm<br>
                                    Coordinates: {cent}
                                    """
        poly_text.placeholder = "HTML"
        poly_text.description = ""

        # Popup with a given location on the map:
        poly_popup = Popup(child=poly_text,
                           close_button=False,
                           auto_close=False,
                           close_on_escape_key=False)
        m.add_layer(poly_popup)

        # Popup associated to a layer
        polygon.popup = poly_popup

        # Layers control
        show_poly = Checkbox(value=True,
                             description='Polygon',
                             disabled=False,
                             indent=False,
                             layout=Layout(width='140px'))
        show_sat = Checkbox(value=False,
                            description='High res basemap',
                            disabled=False,
                            indent=False,
                            layout=Layout(width='140px'))

        def polygon_changed(b):
            try:
                if show_poly.value is True:
                    m.add_layer(polygon)
                else:
                    m.remove_layer(polygon)
            except Exception:
                pass

        show_poly.observe(polygon_changed)

        def show_sat_changed(b):
            try:
                if show_sat.value is True:
                    m.add_layer(basemap2)
                else:
                    m.remove_layer(basemap2)
            except Exception:
                pass

        show_sat.observe(show_sat_changed)

        try:
            csv_list = f"{ci_path}{pid}_images_list.{ci_band.value[0]}.csv"
            df = view_images.create_df(ci_path, pid, ci_band.value)

            geotiff = f"{ci_path}{df['imgs'][0]}.{ci_band.value[0]}.tif"
            bounds = view_spatial.bounds(geotiff)

            images = {}
            for i, row in df.iterrows():
                str_date = str(row['date'].date()).replace('-', '')
                workdir = os.getcwd().split('/')[-1]
                img_tc = f"{ci_path}{('').join(ci_band.value)}_{str_date}.png"

                # Create false color image if it does not exist
                # Merge bands (images path, export image path, bands list)
                if not os.path.isfile(img_tc):
                    imgs_path = f"{ci_path}{row['imgs']}"
                    view_images.merge_bands(imgs_path, img_tc, ci_band.value)
                values = config.read()

                # Set the current environment
                if eval(values['set']['jupyterlab']) is True:
                    image_path = f'files/{workdir}/{img_tc}'
                else:
                    image_path = img_tc
                images[i] = ImageOverlay(url=image_path,
                                         name=str_date,
                                         bounds=(bounds))

            # Time slider
            slider = IntSlider(value=1,
                               min=1,
                               max=len(images),
                               step=1,
                               description=str(df['date'][0].date()),
                               disabled=False,
                               continuous_update=False,
                               orientation='horizontal',
                               readout=True,
                               readout_format='d')
            show_chip = Checkbox(value=True,
                                 description='Chip image',
                                 disabled=False,
                                 indent=False,
                                 layout=Layout(width='140px'))

            def on_ci_band_change(change):
                pass

            ci_band.observe(on_ci_band_change, 'value')

            def show_chip_changed(b):
                try:
                    if show_chip.value is True:
                        m.add_layer(images[slider.value - 1])
                    else:
                        m.remove_layer(images[slider.value - 1])
                except Exception:
                    pass

            show_chip.observe(show_chip_changed)

            # Slider control
            play = Play(value=1,
                        min=1,
                        max=len(images),
                        step=1,
                        interval=1000,
                        description="Press play",
                        disabled=False)

            def slider_changed(b):
                if show_chip.value is True:
                    try:
                        m.substitute_layer(images[b['old'] - 1],
                                           images[b['new'] - 1])
                    except Exception:
                        pass
                    slider.description = str(df['date'][slider.value -
                                                        1].date())

            slider.observe(slider_changed)
            jslink((play, 'value'), (slider, 'value'))
            time_box = HBox([slider, play])
            time_control = WidgetControl(widget=time_box,
                                         position='bottomleft')
            m.add_control(time_control)
            m.add_layer(images[0])

            map_options = VBox([show_poly, show_chip, show_sat])
        except Exception as err:
            map_options = VBox([show_poly, show_sat])
            print(err)

        layers_control = WidgetControl(widget=map_options,
                                       position='topright',
                                       max_width=150)
        m.add_control(layers_control)
        return m
    def __init__(self):

        self.output_dir = '.'
        # self.output_dir = 'tmpdir'

        self.figsize_width_substrate = 15.0  # allow extra for colormap
        self.figsize_height_substrate = 12.5
        self.figsize_width_svg = 12.0
        self.figsize_height_svg = 12.0

        # self.fig = plt.figure(figsize=(self.figsize_width_substrate, self.figsize_height_substrate))
        # self.fig = plt.figure()
        self.fig = None

        # self.fig = plt.figure(figsize=(7.2,6))  # this strange figsize results in a ~square contour plot

        self.first_time = True
        self.modulo = 1

        self.use_defaults = True

        self.svg_delta_t = 1
        self.substrate_delta_t = 1
        self.svg_frame = 1
        self.substrate_frame = 1

        self.customized_output_freq = False
        self.therapy_activation_time = 1000000
        self.max_svg_frame_pre_therapy = 1000000
        self.max_substrate_frame_pre_therapy = 1000000

        self.svg_xmin = 0

        # Probably don't want to hardwire these if we allow changing the domain size
        # self.svg_xrange = 2000
        # self.xmin = -1000.
        # self.xmax = 1000.
        # self.ymin = -1000.
        # self.ymax = 1000.
        # self.x_range = 2000.
        # self.y_range = 2000.

        self.show_nucleus = False
        self.show_edge = True

        # initial value
        self.field_index = 4
        # self.field_index = self.mcds_field.value + 4

        # define dummy size of mesh (set in the tool's primary module)
        self.numx = 0
        self.numy = 0

        self.title_str = ''

        tab_height = '600px'
        tab_height = '500px'
        constWidth = '180px'
        constWidth2 = '150px'
        tab_layout = Layout(
            width='900px',  # border='2px solid black',
            height=tab_height,
        )  #overflow_y='scroll')

        max_frames = 2
        # self.mcds_plot = interactive(self.plot_substrate, frame=(0, max_frames), continuous_update=False)
        # self.i_plot = interactive(self.plot_plots, frame=(0, max_frames), continuous_update=False)

        # self.i_plot = interactive(self.plot_substrate0, frame=(0, max_frames), continuous_update=False)
        self.i_plot = interactive(self.plot_substrate,
                                  frame=(0, max_frames),
                                  continuous_update=False)

        self.frame_slider = IntSlider(
            min=0,
            max=10,
            step=1,
            description='Test:',
            disabled=False,
            continuous_update=False,
            orientation='horizontal',
            readout=True,
            readout_format='d',
        )
        # self.frame_slider.observe(self.frame_slider_cb)
        self.frame_slider.observe(self.plot_substrate)

        # "plot_size" controls the size of the tab height, not the plot (rf. figsize for that)
        # NOTE: the Substrates Plot tab has an extra row of widgets at the top of it (cf. Cell Plots tab)
        svg_plot_size = '700px'
        svg_plot_size = '600px'
        svg_plot_size = '700px'
        svg_plot_size = '900px'
        self.i_plot.layout.width = svg_plot_size
        self.i_plot.layout.height = svg_plot_size

        self.fontsize = 20

        # description='# cell frames',
        self.max_frames = BoundedIntText(
            min=0,
            max=99999,
            value=max_frames,
            description='# frames',
            layout=Layout(width='160px'),
        )
        self.max_frames.observe(self.update_max_frames)

        # self.field_min_max = {'dummy': [0., 1.]}
        # NOTE: manually setting these for now (vs. parsing them out of data/initial.xml)
        self.field_min_max = {
            'director signal': [0., 1.],
            'cargo signal': [0., 1.]
        }
        # hacky I know, but make a dict that's got (key,value) reversed from the dict in the Dropdown below
        # self.field_dict = {0:'dummy'}
        self.field_dict = {0: 'director signal', 1: 'cargo signal'}

        self.mcds_field = Dropdown(
            options={
                'director signal': 0,
                'cargo signal': 1
            },
            value=0,
            #     description='Field',
            layout=Layout(width=constWidth))
        # print("substrate __init__: self.mcds_field.value=",self.mcds_field.value)
        #        self.mcds_field.observe(self.mcds_field_cb)
        self.mcds_field.observe(self.mcds_field_changed_cb)

        # self.field_cmap = Text(
        #     value='viridis',
        #     description='Colormap',
        #     disabled=True,
        #     layout=Layout(width=constWidth),
        # )
        self.field_cmap = Dropdown(
            options=['viridis', 'jet', 'YlOrRd'],
            value='YlOrRd',
            #     description='Field',
            layout=Layout(width=constWidth))
        #        self.field_cmap.observe(self.plot_substrate)
        self.field_cmap.observe(self.mcds_field_cb)

        self.cmap_fixed = Checkbox(
            description='Fix',
            disabled=False,
            #           layout=Layout(width=constWidth2),
        )

        self.save_min_max = Button(
            description='Save',  #style={'description_width': 'initial'},
            button_style=
            'success',  # 'success', 'info', 'warning', 'danger' or ''
            tooltip='Save min/max for this substrate',
            disabled=True,
            layout=Layout(width='90px'))

        def save_min_max_cb(b):
            #            field_name = self.mcds_field.options[]
            #            field_name = next(key for key, value in self.mcds_field.options.items() if value == self.mcds_field.value)
            field_name = self.field_dict[self.mcds_field.value]
            #            print(field_name)
            #            self.field_min_max = {'oxygen': [0., 30.], 'glucose': [0., 1.], 'H+ ions': [0., 1.], 'ECM': [0., 1.], 'NP1': [0., 1.], 'NP2': [0., 1.]}
            self.field_min_max[field_name][0] = self.cmap_min.value
            self.field_min_max[field_name][1] = self.cmap_max.value
#            print(self.field_min_max)

        self.save_min_max.on_click(save_min_max_cb)

        self.cmap_min = FloatText(
            description='Min',
            value=0,
            step=0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_min.observe(self.mcds_field_cb)

        self.cmap_max = FloatText(
            description='Max',
            value=38,
            step=0.1,
            disabled=True,
            layout=Layout(width=constWidth2),
        )
        self.cmap_max.observe(self.mcds_field_cb)

        def cmap_fixed_cb(b):
            if (self.cmap_fixed.value):
                self.cmap_min.disabled = False
                self.cmap_max.disabled = False
                self.save_min_max.disabled = False
            else:
                self.cmap_min.disabled = True
                self.cmap_max.disabled = True
                self.save_min_max.disabled = True
#            self.mcds_field_cb()

        self.cmap_fixed.observe(cmap_fixed_cb)

        field_cmap_row2 = HBox([self.field_cmap, self.cmap_fixed])

        #        field_cmap_row3 = HBox([self.save_min_max, self.cmap_min, self.cmap_max])
        items_auto = [
            self.save_min_max,  #layout=Layout(flex='3 1 auto', width='auto'),
            self.cmap_min,
            self.cmap_max,
        ]
        box_layout = Layout(display='flex',
                            flex_flow='row',
                            align_items='stretch',
                            width='80%')
        field_cmap_row3 = Box(children=items_auto, layout=box_layout)

        #---------------------
        self.cell_nucleus_toggle = Checkbox(
            description='nuclei',
            disabled=False,
            value=self.show_nucleus,
            #           layout=Layout(width=constWidth2),
        )

        def cell_nucleus_toggle_cb(b):
            # self.update()
            if (self.cell_nucleus_toggle.value):
                self.show_nucleus = True
            else:
                self.show_nucleus = False
            self.i_plot.update()

        self.cell_nucleus_toggle.observe(cell_nucleus_toggle_cb)

        #----
        self.cell_edges_toggle = Checkbox(
            description='edges',
            disabled=False,
            value=self.show_edge,
            #           layout=Layout(width=constWidth2),
        )

        def cell_edges_toggle_cb(b):
            # self.update()
            if (self.cell_edges_toggle.value):
                self.show_edge = True
            else:
                self.show_edge = False
            self.i_plot.update()

        self.cell_edges_toggle.observe(cell_edges_toggle_cb)

        self.cells_toggle = Checkbox(
            description='Cells',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def cells_toggle_cb(b):
            # self.update()
            self.i_plot.update()
            if (self.cells_toggle.value):
                self.cell_edges_toggle.disabled = False
                self.cell_nucleus_toggle.disabled = False
            else:
                self.cell_edges_toggle.disabled = True
                self.cell_nucleus_toggle.disabled = True

        self.cells_toggle.observe(cells_toggle_cb)

        #---------------------
        self.substrates_toggle = Checkbox(
            description='Substrates',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def substrates_toggle_cb(b):
            if (self.substrates_toggle.value):  # seems bass-ackwards
                self.cmap_fixed.disabled = False
                self.cmap_min.disabled = False
                self.cmap_max.disabled = False
                self.mcds_field.disabled = False
                self.field_cmap.disabled = False
            else:
                self.cmap_fixed.disabled = True
                self.cmap_min.disabled = True
                self.cmap_max.disabled = True
                self.mcds_field.disabled = True
                self.field_cmap.disabled = True

        self.substrates_toggle.observe(substrates_toggle_cb)

        self.grid_toggle = Checkbox(
            description='grid',
            disabled=False,
            value=True,
            #           layout=Layout(width=constWidth2),
        )

        def grid_toggle_cb(b):
            # self.update()
            self.i_plot.update()

        self.grid_toggle.observe(grid_toggle_cb)

        #        field_cmap_row3 = Box([self.save_min_max, self.cmap_min, self.cmap_max])

        # mcds_tab = widgets.VBox([mcds_dir, mcds_plot, mcds_play], layout=tab_layout)
        # mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3, self.max_frames])  # mcds_dir
        #        mcds_params = VBox([self.mcds_field, field_cmap_row2, field_cmap_row3,])  # mcds_dir

        #        self.tab = HBox([mcds_params, self.mcds_plot], layout=tab_layout)

        help_label = Label('select slider: drag or left/right arrows')
        # row1 = Box([help_label, Box( [self.max_frames, self.mcds_field, self.field_cmap], layout=Layout(border='0px solid black',
        row1a = Box([self.max_frames, self.mcds_field, self.field_cmap],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        row1b = Box([
            self.cells_toggle, self.cell_nucleus_toggle, self.cell_edges_toggle
        ],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        row1 = HBox([row1a, Label('.....'), row1b])

        row2a = Box([self.cmap_fixed, self.cmap_min, self.cmap_max],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        # row2b = Box( [self.substrates_toggle, self.grid_toggle], layout=Layout(border='1px solid black',
        row2b = Box([
            self.substrates_toggle,
        ],
                    layout=Layout(border='1px solid black',
                                  width='50%',
                                  height='',
                                  align_items='stretch',
                                  flex_direction='row',
                                  display='flex'))
        # row2 = HBox( [row2a, self.substrates_toggle, self.grid_toggle])
        row2 = HBox([row2a, Label('.....'), row2b])

        row3 = HBox([
            self.frame_slider,
        ])

        if (hublib_flag):
            self.download_button = Download('mcds.zip',
                                            style='warning',
                                            icon='cloud-download',
                                            tooltip='Download data',
                                            cb=self.download_cb)

            self.download_svg_button = Download(
                'svg.zip',
                style='warning',
                icon='cloud-download',
                tooltip='You need to allow pop-ups in your browser',
                cb=self.download_svg_cb)
            download_row = HBox([
                self.download_button.w, self.download_svg_button.w,
                Label("Download all cell plots (browser must allow pop-ups).")
            ])

            # box_layout = Layout(border='0px solid')
            # controls_box = VBox([row1, row2])  # ,width='50%', layout=box_layout)
            controls_box = VBox([row1, row2,
                                 row3])  # ,width='50%', layout=box_layout)

            # self.tab = VBox([controls_box, self.i_plot, download_row])

            #            graph = GraphWidget()
            # iplot([{"x": [1, 2, 3], "y": [3, 1, 6]}])
            # self.fig = go.FigureWidget(data=go.Bar(y=[2, 3, 1]))

            # x = np.random.randn(2000)
            # y = np.random.randn(2000)
            self.x = np.random.randn(2)
            self.y = np.random.randn(2)
            # iplot([go.Histogram2dContour(x=x, y=y, contours=dict(coloring='heatmap')),
            # self.fig = go.FigureWidget(data=go.Histogram2dContour(x=x, y=y, contours_showlines=False, contours=dict(coloring='heatmap')))
            self.fig = go.FigureWidget(data=go.Histogram2dContour(
                x=self.x, y=self.y, contours=dict(coloring='heatmap')))

            # self.fig = go.FigureWidget( data = [
            #     {
            #     'type' : 'Histogram2dContour',
            #     'x' : self.x,
            #     'y' : self.y,
            #     'contours' : {
            #         'coloring': 'heatmap'
            #     }
            #     },
            #     {
            #     'type' : 'scatter',
            #     'x' : self.x,
            #     'y' : self.y,
            #     'line' : {
            #         'color': 'red',
            #         'width': 3
            #     }
            #     }
            #     ], layout={})
            # self.fig = go.FigureWidget(data=go.Contour(x=x, y=y, contours=dict(coloring='heatmap')))

            # x = np.linspace(-10, 10, 400)
            # y = np.linspace(-5, 5, 400)
            # x, y = np.meshgrid(x, y)
            # a = 1
            # b = 2
            # z = x**2/a**2 - y**2/b**2
            # contour = go.Contour(
            #     z=z
            # )
            # contour.contours.type = 'constraint'
            # contour.contours.value = 8
            # data = [contour]
            # # self.fig = go.Figure(data=data)
            # self.fig = go.FigureWidget(data=data)

            #                go.Scatter(x=x, y=y, mode='markers', marker=dict(color='white', size=3, opacity=0.3)), show_link=False)
            self.fig.update_layout(  # square figure layout for plotly?
                autosize=True,
                margin=dict(l=20, r=20, t=30, b=20),
                width=800,
                height=800,
                # margin=dict(
                #     l=50,
                #     r=50,
                #     b=100,
                #     t=100,
                #     pad=4
                # ),
                # paper_bgcolor="White",
                # paper_bgcolor="LightSteelBlue",
            )

            self.tab = VBox([controls_box, self.fig,
                             download_row])  # plotly approach

            # app_layout = AppLayout(
            #     center= self.fig.canvas,
            #     # footer= self.frame_slider,
            #     pane_heights=[0, 6, 1]
            # )
            # self.tab = app_layout

            # self.tab = VBox([controls_box, self.fig.canvas, download_row])

#        self.fig = plt.figure(figsize=(self.figsize_width_substrate, self.figsize_height_substrate))

        else:
            # self.tab = VBox([row1, row2])
            self.tab = VBox([row1, row2, self.i_plot])
Exemple #35
0
class Dashboard(VBox):
    """
    Build the dashboard for Jupyter widgets. Requires running
    in a notebook/jupyterlab.
    """
    def __init__(self, net, width="95%", height="550px", play_rate=0.5):
        self._ignore_layer_updates = False
        self.player = _Player(self, play_rate)
        self.player.start()
        self.net = net
        r = random.randint(1, 1000000)
        self.class_id = "picture-dashboard-%s-%s" % (self.net.name, r)
        self._width = width
        self._height = height
        ## Global widgets:
        style = {"description_width": "initial"}
        self.feature_columns = IntText(description="Detail columns:",
                                       value=self.net.config["dashboard.features.columns"],
                                       min=0,
                                       max=1024,
                                       style=style)
        self.feature_scale = FloatText(description="Detail scale:",
                                       value=self.net.config["dashboard.features.scale"],
                                       min=0.1,
                                       max=10,
                                       style=style)
        self.feature_columns.observe(self.regenerate, names='value')
        self.feature_scale.observe(self.regenerate, names='value')
        ## Hack to center SVG as justify-content is broken:
        self.net_svg = HTML(value="""<p style="text-align:center">%s</p>""" % ("",), layout=Layout(
            width=self._width, overflow_x='auto', overflow_y="auto",
            justify_content="center"))
        # Make controls first:
        self.output = Output()
        controls = self.make_controls()
        config = self.make_config()
        super().__init__([config, controls, self.net_svg, self.output])

    def propagate(self, inputs):
        """
        Propagate inputs through the dashboard view of the network.
        """
        if dynamic_pictures_check():
            return self.net.propagate(inputs, class_id=self.class_id, update_pictures=True)
        else:
            self.regenerate(inputs=input)

    def goto(self, position):
        if len(self.net.dataset.inputs) == 0 or len(self.net.dataset.targets) == 0:
            return
        if self.control_select.value == "Train":
            length = len(self.net.dataset.train_inputs)
        elif self.control_select.value == "Test":
            length = len(self.net.dataset.test_inputs)
        #### Position it:
        if position == "begin":
            self.control_slider.value = 0
        elif position == "end":
            self.control_slider.value = length - 1
        elif position == "prev":
            if self.control_slider.value - 1 < 0:
                self.control_slider.value = length - 1 # wrap around
            else:
                self.control_slider.value = max(self.control_slider.value - 1, 0)
        elif position == "next":
            if self.control_slider.value + 1 > length - 1:
                self.control_slider.value = 0 # wrap around
            else:
                self.control_slider.value = min(self.control_slider.value + 1, length - 1)
        self.position_text.value = self.control_slider.value


    def change_select(self, change=None):
        """
        """
        self.update_control_slider(change)
        self.regenerate()

    def update_control_slider(self, change=None):
        self.net.config["dashboard.dataset"] = self.control_select.value
        if len(self.net.dataset.inputs) == 0 or len(self.net.dataset.targets) == 0:
            self.total_text.value = "of 0"
            self.control_slider.value = 0
            self.position_text.value = 0
            self.control_slider.disabled = True
            self.position_text.disabled = True
            for child in self.control_buttons.children:
                if not hasattr(child, "icon") or child.icon != "refresh":
                    child.disabled = True
            return
        if self.control_select.value == "Test":
            self.total_text.value = "of %s" % len(self.net.dataset.test_inputs)
            minmax = (0, max(len(self.net.dataset.test_inputs) - 1, 0))
            if minmax[0] <= self.control_slider.value <= minmax[1]:
                pass # ok
            else:
                self.control_slider.value = 0
            self.control_slider.min = minmax[0]
            self.control_slider.max = minmax[1]
            if len(self.net.dataset.test_inputs) == 0:
                disabled = True
            else:
                disabled = False
        elif self.control_select.value == "Train":
            self.total_text.value = "of %s" % len(self.net.dataset.train_inputs)
            minmax = (0, max(len(self.net.dataset.train_inputs) - 1, 0))
            if minmax[0] <= self.control_slider.value <= minmax[1]:
                pass # ok
            else:
                self.control_slider.value = 0
            self.control_slider.min = minmax[0]
            self.control_slider.max = minmax[1]
            if len(self.net.dataset.train_inputs) == 0:
                disabled = True
            else:
                disabled = False
        self.control_slider.disabled = disabled
        self.position_text.disbaled = disabled
        self.position_text.value = self.control_slider.value
        for child in self.control_buttons.children:
            if not hasattr(child, "icon") or child.icon != "refresh":
                child.disabled = disabled

    def update_zoom_slider(self, change):
        if change["name"] == "value":
            self.net.config["svg_scale"] = self.zoom_slider.value
            self.regenerate()

    def update_position_text(self, change):
        # {'name': 'value', 'old': 2, 'new': 3, 'owner': IntText(value=3, layout=Layout(width='100%')), 'type': 'change'}
        self.control_slider.value = change["new"]

    def get_current_input(self):
        if self.control_select.value == "Train" and len(self.net.dataset.train_targets) > 0:
            return self.net.dataset.train_inputs[self.control_slider.value]
        elif self.control_select.value == "Test" and len(self.net.dataset.test_targets) > 0:
            return self.net.dataset.test_inputs[self.control_slider.value]

    def get_current_targets(self):
        if self.control_select.value == "Train" and len(self.net.dataset.train_targets) > 0:
            return self.net.dataset.train_targets[self.control_slider.value]
        elif self.control_select.value == "Test" and len(self.net.dataset.test_targets) > 0:
            return self.net.dataset.test_targets[self.control_slider.value]

    def update_slider_control(self, change):
        if len(self.net.dataset.inputs) == 0 or len(self.net.dataset.targets) == 0:
            self.total_text.value = "of 0"
            return
        if change["name"] == "value":
            self.position_text.value = self.control_slider.value
            if self.control_select.value == "Train" and len(self.net.dataset.train_targets) > 0:
                self.total_text.value = "of %s" % len(self.net.dataset.train_inputs)
                if self.net.model is None:
                    return
                if not dynamic_pictures_check():
                    self.regenerate(inputs=self.net.dataset.train_inputs[self.control_slider.value],
                                    targets=self.net.dataset.train_targets[self.control_slider.value])
                    return
                output = self.net.propagate(self.net.dataset.train_inputs[self.control_slider.value],
                                            class_id=self.class_id, update_pictures=True)
                if self.feature_bank.value in self.net.layer_dict.keys():
                    self.net.propagate_to_features(self.feature_bank.value, self.net.dataset.train_inputs[self.control_slider.value],
                                                   cols=self.feature_columns.value, scale=self.feature_scale.value, html=False)
                if self.net.config["show_targets"]:
                    if len(self.net.output_bank_order) == 1: ## FIXME: use minmax of output bank
                        self.net.display_component([self.net.dataset.train_targets[self.control_slider.value]],
                                                   "targets",
                                                   class_id=self.class_id,
                                                   minmax=(-1, 1))
                    else:
                        self.net.display_component(self.net.dataset.train_targets[self.control_slider.value],
                                                   "targets",
                                                   class_id=self.class_id,
                                                   minmax=(-1, 1))
                if self.net.config["show_errors"]: ## minmax is error
                    if len(self.net.output_bank_order) == 1:
                        errors = np.array(output) - np.array(self.net.dataset.train_targets[self.control_slider.value])
                        self.net.display_component([errors.tolist()],
                                                   "errors",
                                                   class_id=self.class_id,
                                                   minmax=(-1, 1))
                    else:
                        errors = []
                        for bank in range(len(self.net.output_bank_order)):
                            errors.append( np.array(output[bank]) - np.array(self.net.dataset.train_targets[self.control_slider.value][bank]))
                        self.net.display_component(errors, "errors",  class_id=self.class_id, minmax=(-1, 1))
            elif self.control_select.value == "Test" and len(self.net.dataset.test_targets) > 0:
                self.total_text.value = "of %s" % len(self.net.dataset.test_inputs)
                if self.net.model is None:
                    return
                if not dynamic_pictures_check():
                    self.regenerate(inputs=self.net.dataset.test_inputs[self.control_slider.value],
                                    targets=self.net.dataset.test_targets[self.control_slider.value])
                    return
                output = self.net.propagate(self.net.dataset.test_inputs[self.control_slider.value],
                                            class_id=self.class_id, update_pictures=True)
                if self.feature_bank.value in self.net.layer_dict.keys():
                    self.net.propagate_to_features(self.feature_bank.value, self.net.dataset.test_inputs[self.control_slider.value],
                                               cols=self.feature_columns.value, scale=self.feature_scale.value, html=False)
                if self.net.config["show_targets"]: ## FIXME: use minmax of output bank
                    self.net.display_component([self.net.dataset.test_targets[self.control_slider.value]],
                                               "targets",
                                               class_id=self.class_id,
                                               minmax=(-1, 1))
                if self.net.config["show_errors"]: ## minmax is error
                    if len(self.net.output_bank_order) == 1:
                        errors = np.array(output) - np.array(self.net.dataset.test_targets[self.control_slider.value])
                        self.net.display_component([errors.tolist()],
                                                   "errors",
                                                   class_id=self.class_id,
                                                   minmax=(-1, 1))
                    else:
                        errors = []
                        for bank in range(len(self.net.output_bank_order)):
                            errors.append( np.array(output[bank]) - np.array(self.net.dataset.test_targets[self.control_slider.value][bank]))
                        self.net.display_component(errors, "errors", class_id=self.class_id, minmax=(-1, 1))

    def toggle_play(self, button):
        ## toggle
        if self.button_play.description == "Play":
            self.button_play.description = "Stop"
            self.button_play.icon = "pause"
            self.player.resume()
        else:
            self.button_play.description = "Play"
            self.button_play.icon = "play"
            self.player.pause()

    def prop_one(self, button=None):
        self.update_slider_control({"name": "value"})

    def regenerate(self, button=None, inputs=None, targets=None):
        ## Protection when deleting object on shutdown:
        if isinstance(button, dict) and 'new' in button and button['new'] is None:
            return
        ## Update the config:
        self.net.config["dashboard.features.bank"] = self.feature_bank.value
        self.net.config["dashboard.features.columns"] = self.feature_columns.value
        self.net.config["dashboard.features.scale"] = self.feature_scale.value
        inputs = inputs if inputs is not None else self.get_current_input()
        targets = targets if targets is not None else self.get_current_targets()
        features = None
        if self.feature_bank.value in self.net.layer_dict.keys() and inputs is not None:
            if self.net.model is not None:
                features = self.net.propagate_to_features(self.feature_bank.value, inputs,
                                                          cols=self.feature_columns.value,
                                                          scale=self.feature_scale.value, display=False)
        svg = """<p style="text-align:center">%s</p>""" % (self.net.to_svg(
            inputs=inputs,
            targets=targets,
            class_id=self.class_id,
            highlights={self.feature_bank.value: {
                "border_color": "orange",
                "border_width": 30,
            }}))
        if inputs is not None and features is not None:
            html_horizontal = """
<table align="center" style="width: 100%%;">
 <tr>
  <td valign="top" style="width: 50%%;">%s</td>
  <td valign="top" align="center" style="width: 50%%;"><p style="text-align:center"><b>%s</b></p>%s</td>
</tr>
</table>"""
            html_vertical = """
<table align="center" style="width: 100%%;">
 <tr>
  <td valign="top">%s</td>
</tr>
<tr>
  <td valign="top" align="center"><p style="text-align:center"><b>%s</b></p>%s</td>
</tr>
</table>"""
            self.net_svg.value = (html_vertical if self.net.config["svg_rotate"] else html_horizontal) % (
                svg, "%s details" % self.feature_bank.value, features)
        else:
            self.net_svg.value = svg

    def make_colormap_image(self, colormap_name):
        from .layers import Layer
        if not colormap_name:
            colormap_name = get_colormap()
        layer = Layer("Colormap", 100)
        minmax = layer.get_act_minmax()
        image = layer.make_image(np.arange(minmax[0], minmax[1], .01),
                                 colormap_name,
                                 {"pixels_per_unit": 1,
                                  "svg_rotate": self.net.config["svg_rotate"]}).resize((300, 25))
        return image

    def set_attr(self, obj, attr, value):
        if value not in [{}, None]: ## value is None when shutting down
            if isinstance(value, dict):
                value = value["value"]
            if isinstance(obj, dict):
                obj[attr] = value
            else:
                setattr(obj, attr, value)
            ## was crashing on Widgets.__del__, if get_ipython() no longer existed
            self.regenerate()

    def make_controls(self):
        layout = Layout(width='100%', height="100%")
        button_begin = Button(icon="fast-backward", layout=layout)
        button_prev = Button(icon="backward", layout=layout)
        button_next = Button(icon="forward", layout=layout)
        button_end = Button(icon="fast-forward", layout=layout)
        #button_prop = Button(description="Propagate", layout=Layout(width='100%'))
        #button_train = Button(description="Train", layout=Layout(width='100%'))
        self.button_play = Button(icon="play", description="Play", layout=layout)
        step_down = Button(icon="sort-down", layout=Layout(width="95%", height="100%"))
        step_up = Button(icon="sort-up", layout=Layout(width="95%", height="100%"))
        up_down = HBox([step_down, step_up], layout=Layout(width="100%", height="100%"))
        refresh_button = Button(icon="refresh", layout=Layout(width="25%", height="100%"))

        self.position_text = IntText(value=0, layout=layout)

        self.control_buttons = HBox([
            button_begin,
            button_prev,
            #button_train,
            self.position_text,
            button_next,
            button_end,
            self.button_play,
            up_down,
            refresh_button
        ], layout=Layout(width='100%', height="100%"))
        length = (len(self.net.dataset.train_inputs) - 1) if len(self.net.dataset.train_inputs) > 0 else 0
        self.control_slider = IntSlider(description="Dataset index",
                                   continuous_update=False,
                                   min=0,
                                   max=max(length, 0),
                                   value=0,
                                   layout=Layout(width='100%'))
        if self.net.config["dashboard.dataset"] == "Train":
            length = len(self.net.dataset.train_inputs)
        else:
            length = len(self.net.dataset.test_inputs)
        self.total_text = Label(value="of %s" % length, layout=Layout(width="100px"))
        self.zoom_slider = FloatSlider(description="Zoom",
                                       continuous_update=False,
                                       min=0, max=1.0,
                                       style={"description_width": 'initial'},
                                       layout=Layout(width="65%"),
                                       value=self.net.config["svg_scale"] if self.net.config["svg_scale"] is not None else 0.5)

        ## Hook them up:
        button_begin.on_click(lambda button: self.goto("begin"))
        button_end.on_click(lambda button: self.goto("end"))
        button_next.on_click(lambda button: self.goto("next"))
        button_prev.on_click(lambda button: self.goto("prev"))
        self.button_play.on_click(self.toggle_play)
        self.control_slider.observe(self.update_slider_control, names='value')
        refresh_button.on_click(lambda widget: (self.update_control_slider(),
                                                self.output.clear_output(),
                                                self.regenerate()))
        step_down.on_click(lambda widget: self.move_step("down"))
        step_up.on_click(lambda widget: self.move_step("up"))
        self.zoom_slider.observe(self.update_zoom_slider, names='value')
        self.position_text.observe(self.update_position_text, names='value')
        # Put them together:
        controls = VBox([HBox([self.control_slider, self.total_text], layout=Layout(height="40px")),
                         self.control_buttons], layout=Layout(width='100%'))

        #net_page = VBox([control, self.net_svg], layout=Layout(width='95%'))
        controls.on_displayed(lambda widget: self.regenerate())
        return controls

    def move_step(self, direction):
        """
        Move the layer stepper up/down through network
        """
        options = [""] + [layer.name for layer in self.net.layers]
        index = options.index(self.feature_bank.value)
        if direction == "up":
            new_index = (index + 1) % len(options)
        else: ## down
            new_index = (index - 1) % len(options)
        self.feature_bank.value = options[new_index]
        self.regenerate()

    def make_config(self):
        layout = Layout()
        style = {"description_width": "initial"}
        checkbox1 = Checkbox(description="Show Targets", value=self.net.config["show_targets"],
                             layout=layout, style=style)
        checkbox1.observe(lambda change: self.set_attr(self.net.config, "show_targets", change["new"]), names='value')
        checkbox2 = Checkbox(description="Errors", value=self.net.config["show_errors"],
                             layout=layout, style=style)
        checkbox2.observe(lambda change: self.set_attr(self.net.config, "show_errors", change["new"]), names='value')

        hspace = IntText(value=self.net.config["hspace"], description="Horizontal space between banks:",
                         style=style, layout=layout)
        hspace.observe(lambda change: self.set_attr(self.net.config, "hspace", change["new"]), names='value')
        vspace = IntText(value=self.net.config["vspace"], description="Vertical space between layers:",
                         style=style, layout=layout)
        vspace.observe(lambda change: self.set_attr(self.net.config, "vspace", change["new"]), names='value')
        self.feature_bank = Select(description="Details:", value=self.net.config["dashboard.features.bank"],
                              options=[""] + [layer.name for layer in self.net.layers],
                              rows=1)
        self.feature_bank.observe(self.regenerate, names='value')
        self.control_select = Select(
            options=['Test', 'Train'],
            value=self.net.config["dashboard.dataset"],
            description='Dataset:',
            rows=1
        )
        self.control_select.observe(self.change_select, names='value')
        column1 = [self.control_select,
                   self.zoom_slider,
                   hspace,
                   vspace,
                   HBox([checkbox1, checkbox2]),
                   self.feature_bank,
                   self.feature_columns,
                   self.feature_scale
        ]
        ## Make layer selectable, and update-able:
        column2 = []
        layer = self.net.layers[-1]
        self.layer_select = Select(description="Layer:", value=layer.name,
                                   options=[layer.name for layer in
                                            self.net.layers],
                                   rows=1)
        self.layer_select.observe(self.update_layer_selection, names='value')
        column2.append(self.layer_select)
        self.layer_visible_checkbox = Checkbox(description="Visible", value=layer.visible, layout=layout)
        self.layer_visible_checkbox.observe(self.update_layer, names='value')
        column2.append(self.layer_visible_checkbox)
        self.layer_colormap = Select(description="Colormap:",
                                     options=[""] + AVAILABLE_COLORMAPS,
                                     value=layer.colormap if layer.colormap is not None else "", layout=layout, rows=1)
        self.layer_colormap_image = HTML(value="""<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap)))
        self.layer_colormap.observe(self.update_layer, names='value')
        column2.append(self.layer_colormap)
        column2.append(self.layer_colormap_image)
        ## get dynamic minmax; if you change it it will set it in layer as override:
        minmax = layer.get_act_minmax()
        self.layer_mindim = FloatText(description="Leftmost color maps to:", value=minmax[0], style=style)
        self.layer_maxdim = FloatText(description="Rightmost color maps to:", value=minmax[1], style=style)
        self.layer_mindim.observe(self.update_layer, names='value')
        self.layer_maxdim.observe(self.update_layer, names='value')
        column2.append(self.layer_mindim)
        column2.append(self.layer_maxdim)
        output_shape = layer.get_output_shape()
        self.layer_feature = IntText(value=layer.feature, description="Feature to show:", style=style)
        self.svg_rotate = Checkbox(description="Rotate", value=layer.visible, layout=layout)
        self.layer_feature.observe(self.update_layer, names='value')
        column2.append(self.layer_feature)
        self.svg_rotate = Checkbox(description="Rotate network",
                                   value=self.net.config["svg_rotate"],
                                   style={"description_width": 'initial'},
                                   layout=Layout(width="52%"))
        self.svg_rotate.observe(lambda change: self.set_attr(self.net.config, "svg_rotate", change["new"]), names='value')
        self.save_config_button = Button(icon="save", layout=Layout(width="10%"))
        self.save_config_button.on_click(self.save_config)
        column2.append(HBox([self.svg_rotate, self.save_config_button]))
        config_children = HBox([VBox(column1, layout=Layout(width="100%")),
                                VBox(column2, layout=Layout(width="100%"))])
        accordion = Accordion(children=[config_children])
        accordion.set_title(0, self.net.name)
        accordion.selected_index = None
        return accordion

    def save_config(self, widget=None):
        self.net.save_config()

    def update_layer(self, change):
        """
        Update the layer object, and redisplay.
        """
        if self._ignore_layer_updates:
            return
        ## The rest indicates a change to a display variable.
        ## We need to save the value in the layer, and regenerate
        ## the display.
        # Get the layer:
        layer = self.net[self.layer_select.value]
        # Save the changed value in the layer:
        layer.feature = self.layer_feature.value
        layer.visible = self.layer_visible_checkbox.value
        ## These three, dealing with colors of activations,
        ## can be done with a prop_one():
        if "color" in change["owner"].description.lower():
            ## Matches: Colormap, lefmost color, rightmost color
            ## overriding dynamic minmax!
            layer.minmax = (self.layer_mindim.value, self.layer_maxdim.value)
            layer.minmax = (self.layer_mindim.value, self.layer_maxdim.value)
            layer.colormap = self.layer_colormap.value if self.layer_colormap.value else None
            self.layer_colormap_image.value = """<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap))
            self.prop_one()
        else:
            self.regenerate()

    def update_layer_selection(self, change):
        """
        Just update the widgets; don't redraw anything.
        """
        ## No need to redisplay anything
        self._ignore_layer_updates = True
        ## First, get the new layer selected:
        layer = self.net[self.layer_select.value]
        ## Now, let's update all of the values without updating:
        self.layer_visible_checkbox.value = layer.visible
        self.layer_colormap.value = layer.colormap if layer.colormap != "" else ""
        self.layer_colormap_image.value = """<img src="%s"/>""" % self.net._image_to_uri(self.make_colormap_image(layer.colormap))
        minmax = layer.get_act_minmax()
        self.layer_mindim.value = minmax[0]
        self.layer_maxdim.value = minmax[1]
        self.layer_feature.value = layer.feature
        self._ignore_layer_updates = False
def interact_distributions():
    from ipywidgets import FloatSlider, Label, GridBox, interactive, Layout, VBox, \
                           HBox, Checkbox, IntSlider, Box, Button, widgets
    fx0 = FloatSlider(value=2,
                      description=" ",
                      min=.5,
                      max=4.,
                      step=.2,
                      continuous_update=False,
                      layout=Layout(width='auto', grid_area='vx0'))
    fy0 = FloatSlider(value=3,
                      description=" ",
                      min=.5,
                      max=4.,
                      step=.2,
                      continuous_update=False,
                      layout=Layout(width='auto', grid_area='vy0'))
    fs0 = FloatSlider(value=1,
                      description=" ",
                      min=.1,
                      max=4.,
                      step=.2,
                      continuous_update=False,
                      layout=Layout(width='auto', grid_area='vs0'))
    fd0 = FloatSlider(value=.9,
                      description=" ",
                      min=-2.,
                      max=2.,
                      step=.1,
                      continuous_update=False,
                      layout=Layout(width='auto', grid_area='vd0'))

    fx1 = FloatSlider(value=2,
                      description=" ",
                      min=.5,
                      max=4.,
                      step=.2,
                      continuous_update=False,
                      layout=Layout(width='auto', grid_area='vx1'))
    fy1 = FloatSlider(value=2,
                      description=" ",
                      min=.5,
                      max=4.,
                      step=.2,
                      continuous_update=False,
                      layout=Layout(width='auto', grid_area='vy1'))
    fs1 = FloatSlider(value=1,
                      description=" ",
                      min=.1,
                      max=4.,
                      step=.2,
                      continuous_update=False,
                      layout=Layout(width='auto', grid_area='vs1'))
    fd1 = FloatSlider(value=-.3,
                      description=" ",
                      min=-2.,
                      max=2.,
                      step=.1,
                      continuous_update=False,
                      layout=Layout(width='auto', grid_area='vd1'))
    fdummy = FloatSlider(value=2, description=" ", min=1, max=4, step=1)

    l = lambda s, p, w="auto": Label(s, layout=Layout(width=w, grid_area=p))

    bay = Checkbox(value=False,
                   description='show NATURAL frontiers',
                   disabled=False,
                   indent=False,
                   layout=Layout(width="80%"))

    resample = Button(description="resample data points")

    from IPython.core.display import clear_output

    def resample_onclick(_):
        global do_resample_points
        do_resample_points = True
        tmp = fdummy.value
        fdummy.value = tmp + (1 if tmp < 3 else -1)
        do_resample_points = False

    resample.on_click(resample_onclick)

    w = interactive(display_distributions,
                    x0=fx0,
                    y0=fy0,
                    s0=fs0,
                    d0=fd0,
                    x1=fx1,
                    y1=fy1,
                    s1=fs1,
                    d1=fd1,
                    show_bayesians=bay,
                    dummy=fdummy,
                    continuous_update=False)

    w.children[-1].layout = Layout(width='auto', grid_area='fig')

    controls = Box([bay, resample],
                   layout=Layout(grid_area="ctr",
                                 display="flex-flow",
                                 justify_content="flex-start",
                                 flex_flow="column",
                                 align_items='flex-start'))

    gb = GridBox(children=[
        fx0, fy0, fs0, fd0, fx1, fy1, fs1, fd1,
        l("AMERICAN TRILOBYTE", "h0"),
        l("AFRICAN TRILOBYTE", "h1"),
        l("size", "lx0"),
        l("weight", "ly0"),
        l("spread", "ls0"),
        l("tilt", "ld0"),
        l("size", "lx1"),
        l("weight", "ly1"),
        l("spread", "ls1"),
        l("tilt", "ld1"), controls
    ],
                 layout=Layout(
                     width='100%',
                     grid_template_rows='auto auto auto auto auto auto auto',
                     grid_template_columns='5% 30% 5% 30% 30%',
                     grid_template_areas='''
                "h0 h0 h1 h1 ."
                "lx0 vx0 lx1 vx1 ."
                "ly0 vy0 ly1 vy1 ctr"
                "ls0 vs0 ls1 vs1 ctr"
                "ld0 vd0 ld1 vd1 ctr"
                "fig fig fig fig fig"
                '''))

    def limit_fd0(*args):
        fd0.max = fs0.value + fs0.value * 0.5
        fd0.min = -fs0.value * 0.5

    def limit_fd1(*args):
        fd1.max = fs1.value + fs1.value * 0.5
        fd1.min = -fs1.value * 0.5

    fs0.observe(limit_fd0, "value")
    fd0.observe(limit_fd0, "value")
    fs1.observe(limit_fd1, "value")
    fd1.observe(limit_fd1, "value")

    w.children[0].value = 1
    widget1 = VBox([gb, w.children[-1]])
    display(widget1)
    return fx0, fy0, fs0, fd0, fx1, fy1, fs1, fd1
Exemple #37
0
 def configure_controllers(self):
     from ipywidgets import (interactive, Label, VBox, FloatSlider,
                             IntSlider, Checkbox)
     # continuous update
     self.continuous_update_button = Checkbox(
         value=False,
         description='Continuous update',
         disabled=False,
         indent=False,
     )
     self.controllers["continuous_update"] = interactive(
         self.set_continuous_update,
         value=self.continuous_update_button
     )
     # subplot
     number_of_plots = len(self.plotter.renderers)
     if number_of_plots > 1:
         self.sliders["subplot"] = IntSlider(
             value=number_of_plots - 1,
             min=0,
             max=number_of_plots - 1,
             step=1,
             continuous_update=False
         )
         self.controllers["subplot"] = VBox([
             Label(value='Select the subplot'),
             interactive(
                 self.set_subplot,
                 index=self.sliders["subplot"],
             )
         ])
     # azimuth
     default_azimuth = self.plotter.renderer._azimuth
     self.sliders["azimuth"] = FloatSlider(
         value=default_azimuth,
         min=-180.,
         max=180.,
         step=10.,
         continuous_update=False
     )
     # elevation
     default_elevation = self.plotter.renderer._elevation
     self.sliders["elevation"] = FloatSlider(
         value=default_elevation,
         min=-180.,
         max=180.,
         step=10.,
         continuous_update=False
     )
     # distance
     eps = 1e-5
     default_distance = self.plotter.renderer._distance
     self.sliders["distance"] = FloatSlider(
         value=default_distance,
         min=eps,
         max=2. * default_distance - eps,
         step=default_distance / 10.,
         continuous_update=False
     )
     # camera
     self.controllers["camera"] = VBox([
         Label(value='Camera settings'),
         interactive(
             self.set_camera,
             azimuth=self.sliders["azimuth"],
             elevation=self.sliders["elevation"],
             distance=self.sliders["distance"],
         )
     ])