Пример #1
0
    def __init__(self):
        """Set up all widget GUI elements and class attributes."""
        self.status_output = None
        self.subsys_candidates_dict = self.get_subsys_candidates()
        self.interactions_count = 0
        self.current_interaction_key = ''
        self.interactions_dict = {}

        # == subsystems panel ==========================================================================================
        label = ipywidgets.Label(value="Select all HilbertSpace\n subsystems (Ctrl-Click)")
        self.subsys_refresh_button = ipywidgets.Button(icon='refresh', layout=ipywidgets.Layout(width='35px'))
        self.subsys_toprow = ipywidgets.HBox([label, self.subsys_refresh_button])

        self.subsys_widget = ipywidgets.SelectMultiple(
            options=list(self.subsys_candidates_dict.keys()),
            rows=10,
            description='',
            disabled=False
        )
        self.subsys_box = ipywidgets.VBox([self.subsys_toprow, self.subsys_widget])

        # == InteractionTerms list panel ===============================================================================
        label = ipywidgets.Label(value="Interaction term(s)   ")
        self.interact_new_button = ipywidgets.Button(icon='plus', layout=ipywidgets.Layout(width='35px'))
        self.interact_del_button = ipywidgets.Button(icon='remove', layout=ipywidgets.Layout(width='35px'))
        self.interact_buttons = ipywidgets.HBox([label, self.interact_new_button, self.interact_del_button])
        self.interact_list_widget = ipywidgets.Select(
            options=[],
            rows=10,
            description='',
            disabled=False,
            layout=ipywidgets.Layout(width='200px')
        )
        self.interact_list_box = ipywidgets.VBox([self.interact_buttons, self.interact_list_widget])

        # == Panel for specifying an InteractionTerm ===================================================================
        self.op1_widget = ipywidgets.Text(description='op1', placeholder='e.g., <subsys1>.n_operator()')
        self.op2_widget = ipywidgets.Text(description='op2', placeholder='e.g., <subsys2>.creation_operator()')
        self.op1subsys_widget = ipywidgets.Dropdown(
            options=self.subsys_widget.value,
            description='subsys1',
            disabled=False
        )
        self.op2subsys_widget = ipywidgets.Dropdown(
            options=self.subsys_widget.value,
            description='subsys2',
            disabled=False
        )
        self.g_widget = ipywidgets.FloatText(description='g_strength')
        self.addhc_widget = ipywidgets.Dropdown(description='add_hc', options=['False', 'True'])

        self.interact_box = ipywidgets.VBox([
            ipywidgets.Label(value="Specify interaction"),
            self.op1subsys_widget,
            self.op1_widget,
            self.op2subsys_widget,
            self.op2_widget,
            self.g_widget,
            self.addhc_widget
        ])
        self.interact_box.layout.display = 'none'

        # == Central run button, status output field ===================================================================
        self.run_button = ipywidgets.Button(description='Create HilbertSpace object',
                                            layout=ipywidgets.Layout(width='200px'))
        self.status_output = ipywidgets.Output()

        # == Wrap everything into boxes ================================================================================
        self.all_panels = ipywidgets.HBox([self.subsys_box, self.interact_list_box, self.interact_box],
                                          layout=ipywidgets.Layout(grid_gap='50px'))
        self.ui = ipywidgets.VBox([self.all_panels, self.run_button, self.status_output])

        # == Make GUI connections ======================================================================================
        self.connect_ui()
Пример #2
0
# In[30]:

import Arm_Lib
# 创建机械臂驱动实例
arm = Arm_Lib.Arm_Device()
joints_0 = [xy[0], xy[1], 0, 0, 90, 30]
arm.Arm_serial_servo_write6_array(joints_0, 1000)

# ### 创建控件

# In[31]:

button_layout = widgets.Layout(width='320px',
                               height='60px',
                               align_self='center')
output = widgets.Output()
# 调整滑杆
joint1_slider = widgets.IntSlider(description='joint1 :',
                                  value=xy[0],
                                  min=70,
                                  max=110,
                                  step=1,
                                  orientation='horizontal')
joint2_slider = widgets.IntSlider(description='joint2 :',
                                  value=xy[1],
                                  min=115,
                                  max=155,
                                  step=1,
                                  orientation='horizontal')
threshold_slider = widgets.IntSlider(description='threshold :',
                                     value=threshold,
Пример #3
0
            for i in range(0, len(lat)):
                x, y = m(lon[i], lat[i])
                m.plot(x, y, 'or', markersize=15, alpha=0.8)
            #plt.savefig('./imgs/ezproxy_fails_'+users[0]+'_'+thisDate2+'_log.png', bbox_inches = "tight")
            plt.show()
        else:
            print("Private IP Range")
            print(ips)


# In[8]:

outZ = widgets.Output(
    layout={
        'border': '0px solid #777777',
        'height': '2.3em',
        'padding': '0px',
        'width': '99%'
    })
outA = widgets.Output(
    layout={
        'border': '0px solid #777777',
        'height': '2.3em',
        'padding': '0px',
        'width': '310px'
    })
outB = widgets.Output(
    layout={
        'border': '0px solid #777777',
        'height': '2.3em',
        'padding': '0px',
Пример #4
0
    def __init__(self, height='256px', *args, **kwargs):
        super(LoggingWidget, self).__init__(*args, **kwargs)

        self.out = ipywidgets.Output()
        self.set_layout(height)
Пример #5
0
import ipywidgets as w
from ipywidgets import Layout, Box
import bqplot as bq
import numpy as np

#debugging output to widget
out = w.Output(layout={'border': '1px solid black'})


class Lightcurve_plot():
    '''plot utility for lightcurves'''
    def __init__(self,
                 filters=['G', 'R', 'I'],
                 filter_button_colors=['green', 'red', 'magenta']):
        '''Initialize plot widget'''

        #define filters
        self.filters = filters
        self.filter_button_colors = filter_button_colors
        self.n_filters = len(self.filters)

        #initialize data
        self.x = []
        self.data = []
        self.err = []
        self.diffImage = []
        self.tempImage = []
        self.sciImage = []

        #define scales for axes
        self.sc_x = bq.LinearScale()
Пример #6
0
    def __init__(self, m_desc, FuseEdges=False, max_stack=30, max_width=9.0):
        # Options
        self.color_accept = 'chartreuse3'
        self.color_reject = 'red'
        self.color_neutral = 'dodgerblue2'
        self.max_width = max_width
        self.fuse = FuseEdges
        # PDA specific options
        self.condition = 'ACCEPT_F'
        self.stack_size = 6
        # initialize
        self.valid_input = True
        self.machine = m_desc
        self.machine_obj = dotObj_pda(self.machine, FuseEdges=FuseEdges)
        self.copy_source = reformat_edge_labels(
            set_graph_size(self.machine_obj.source, max_width))

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        self.play_controls.disabled = True
        self.forward.disabled = True
        self.backward.disabled = True
        self.speed_control.disabled = True
Пример #8
0
    def __init__(
        self,
        filename,
        figsize=(5, 5),
        colormap=None,
        orient_radiology=None,
        guidelines=None,
        animation_speed=300,
    ):
        """
        Turn .nii files into interactive plots using ipywidgets.

        Args
        ----
            filename : str
                    The path to your ``.nii`` file. Can be a string, or a
                    ``PosixPath`` from python3's pathlib.
            figsize : tuple
                    The figure size for each individual view.
            colormaps : tuple, list
                    A list of colormaps, or single colormap.
                    If None, a selection of maps is used to populate the
                    dropdown widget.
            orient_radiology : bool
                    Whether to display the images in the "radiological" style,
                    i.e. with lef and right reversed. If None, a checkbox is
                    offered.
            guidelines : bool
                    Whether to display guide lines. If None, a checkbox is
                    offered.
            animation_speed : int
                    The speed used for the animation, in milliseconds between
                    frames (the lower, the faster, up to a limit).
        """

        if hasattr(filename, "get_data"):
            self.data = filename
        else:
            filename = str(filename)
            if not os.path.isfile(filename):
                raise OSError("File " + filename + " not found.")
            # load data to ensures that the file readable by nibabel
            self.data = nib.load(str(filename))

        self.displays = [widgets.Output() for _ in range(3)]
        self._generate_axes(figsize=figsize)

        # set how many dimensions this file has
        self.ndim = len(self.data.shape)

        # initialise the control components of this widget
        self.dims = ["x", "y", "z", "t"][:self.ndim]
        self.controls = {}
        for i, dim in enumerate(self.dims):
            maxval = self.data.shape[i] - 1
            self.controls[dim] = PlaySlider(
                min=0,
                max=maxval,
                value=maxval // 2,
                interval=animation_speed,
                label=dim.upper(),
                continuous_update=False,
            )
            widgets.link((self.controls[dim], "value"), (self, dim))

        if not isinstance(colormap, str):
            self.color_picker = get_cmap_dropdown(colormap)
            widgets.link((self.color_picker, "value"), (self, "colormap"))
            self.color_reverser = widgets.Checkbox(
                description="Reverse colormap", indent=True)
            widgets.link((self.color_reverser, "value"),
                         (self, "reverse_colors"))
        else:
            self.color_picker = widgets.HBox([])
            self.color_reverser = widgets.HBox([])
            self.colormap = colormap

        if guidelines is None:
            self.guideline_picker = widgets.Checkbox(value=True,
                                                     description="Show guides",
                                                     indent=False)
            widgets.link((self.guideline_picker, "value"),
                         (self, "guidelines"))
        else:
            self.guideline_picker = widgets.Box([])
            self.guidelines = guidelines

        if orient_radiology is None:
            self.orientation_switcher = widgets.Checkbox(
                value=self.orient_radiology,
                indent=False,
                description="Radiological Orientation",
            )
            widgets.link(
                (self.orientation_switcher, "value"),
                (self, "orient_radiology"),
            )
        else:
            self.orientation_switcher = widgets.Box([])
            self.orient_radiology = orient_radiology

        self._update_orientation(True)
Пример #9
0
    def state_problem(self):

        # show title and statement
        super().state_problem()

        # show expressions used in problem
        if len(self.expression) == 1:
            if type(self.expression[0] == str):
                display(Markdown(self.expression[0]))
            else:
                display(Math(latex(self.expression[0])))
        if len(self.expression) > 1:
            for i in range(len(self.expression)):
                if type(self.expression[i] == str):
                    display(
                        Markdown("**(" + str(i + 1) + ")** &nbsp;&nbsp;" +
                                 self.expression[i]))
                else:
                    display(
                        Math("**(" + str(i + 1) + ")** \quad" +
                             latex(self.expression[i])))

        if self.input_widget:
            # if input_widget flag is set and multiple inputs required,
            # display instructions
            if self.num_inputs > 1:
                display(Markdown("*Separate multiple answers by comma(s).*"))

            # next create text widget and button

            # Setup text input field
            self.input_field = widgets.Text(value='',
                                            placeholder='Type here',
                                            description='Answer: ',
                                            continuous_update=False,
                                            disabled=False)

            self.input_field.observe(self.on_enter, names='value')

            # Submit button
            self.submit_button = widgets.Button(
                description='Submit',
                disabled=False,
                button_style=
                'success',  # 'success', 'info', 'warning', 'danger' or ''
                tooltip='Check answer',
            )

            self.submit_button.on_click(self.on_submit)

            # Put widgets in box
            display(widgets.Box([self.input_field, self.submit_button]))

        else:
            #if not, display cell instructions
            display(Markdown("*Enter the answer(s) in the cell below.*"))

        if self.output_widget:
            # Add dedicated output area
            self.out = widgets.Output()
            display(self.out)
import numpy as np
from photutils import centroid_com
import ipywidgets as ipw

from astropy.stats import sigma_clipped_stats
from astropy.table import Table
from astropy.nddata import Cutout2D

import matplotlib.pyplot as plt
from stellarphot.visualization import seeing_plot

__all__ = ['set_keybindings', 'box', 'make_show_event']

out = ipw.Output()
out2 = ipw.Output()
out3 = ipw.Output()


def set_keybindings(image_widget, scroll_zoom=False):
    """
    Set image widget keyboard bindings. The bindings are:

    + Pan by click-and-drag or with arrow keys.
    + Zoom by scrolling or using the ``+``/``-`` keys.
    + Adjust contrast by Ctrl-left click and drag; reset with
      shift-right-click.

    Any existing key bindings are removed.

    Parameters
    ----------
Пример #11
0
    def _ipython_display_(self):
        from IPython.display import HTML, Markdown, display
        from IPython.display import clear_output
        import ipywidgets as widgets

        try:
            if self._pandas_only:
                display(self.display_pandas())
                self._pandas_only = False
            if not self.index.nlevels >= 2 or self.columns.nlevels >= 2:
                self.maintain_metadata()

                if self._intent != [] and (not hasattr(self, "_compiled")
                                           or not self._compiled):
                    from lux.processor.Compiler import Compiler

                    self.current_vis = Compiler.compile_intent(
                        self, self._intent)

            if lux.config.default_display == "lux":
                self._toggle_pandas_display = False
            else:
                self._toggle_pandas_display = True

            # df_to_display.maintain_recs() # compute the recommendations (TODO: This can be rendered in another thread in the background to populate self._widget)
            self.maintain_recs()

            # Observers(callback_function, listen_to_this_variable)
            self._widget.observe(self.remove_deleted_recs,
                                 names="deletedIndices")
            self._widget.observe(self.set_intent_on_click,
                                 names="selectedIntentIndex")

            button = widgets.Button(
                description="Toggle Table/Lux",
                layout=widgets.Layout(width="200px", top="6px", bottom="6px"),
            )
            self.output = widgets.Output()
            self._sampled = lux.config.executor.execute_preview(self)
            display(button, self.output)

            def on_button_clicked(b):
                with self.output:
                    if b:
                        self._toggle_pandas_display = not self._toggle_pandas_display
                    clear_output()

                    # create connection string to display
                    connect_str = self.table_name
                    connection_type = str(type(lux.config.SQLconnection))
                    if "psycopg2.extensions.connection" in connection_type:
                        connection_dsn = lux.config.SQLconnection.get_dsn_parameters(
                        )
                        host_name = connection_dsn["host"]
                        host_port = connection_dsn["port"]
                        dbname = connection_dsn["dbname"]
                        connect_str = host_name + ":" + host_port + "/" + dbname

                    elif "sqlalchemy.engine.base.Engine" in connection_type:
                        db_connection = str(lux.config.SQLconnection)
                        db_start = db_connection.index("@") + 1
                        db_end = len(db_connection) - 1
                        connect_str = db_connection[db_start:db_end]

                    if self._toggle_pandas_display:
                        notification = "Here is a preview of the **{}** database table: **{}**".format(
                            self.table_name, connect_str)
                        display(Markdown(notification),
                                self._sampled.display_pandas())
                    else:
                        # b.layout.display = "none"
                        display(self._widget)
                        # b.layout.display = "inline-block"

            button.on_click(on_button_clicked)
            on_button_clicked(None)

        except (KeyboardInterrupt, SystemExit):
            raise
        except Exception:
            if lux.config.pandas_fallback:
                warnings.warn(
                    "\nUnexpected error in rendering Lux widget and recommendations. "
                    "Falling back to Pandas display.\n"
                    "Please report the following issue on Github: https://github.com/lux-org/lux/issues \n",
                    stacklevel=2,
                )
                warnings.warn(traceback.format_exc())
                display(self.display_pandas())
            else:
                raise
Пример #12
0
 def _make_click_output(self):
     return W.Output()
Пример #13
0
class CytoscapeVisualizer(VisualizerBase):
    """
    A visualization class for visualizing an rdflib.graph.Graph object via ipycytoscape.

    :param graph: an rdflib.graph.Graph object
    :param show_outputs: a boolean, decides whether or not to show feedback from clicks on the graph.
    :param cyto_widget: the actual ipycytoscape visualization.
    """

    cyto_widget = T.Instance(CytoscapeWidget, allow_none=True)
    nodes = T.List()
    click_output = T.Instance(W.Output)
    click_output_box = T.Instance(W.VBox)
    show_outputs = T.Bool(False)
    log = W.Output(layout={"border": "1px solid black"})
    node_style = T.Dict()
    edge_style = T.Dict()

    @T.default("click_output")
    def _make_click_output(self):
        return W.Output()

    @T.default("node_style")
    def _make_default_node(self):
        return {
            "selector": "node",
            "css": {
                "background-color": self.node_color
            },
        }

    @T.default("edge_style")
    def _make_default_edge(self):
        return {"selector": "edge", "css": {"line-color": self.edge_color}}

    @T.default("nodes")
    def _make_default_nodes(self):
        return []

    @T.default("click_output_box")
    def _make_default_click_output_box(self):
        return W.VBox()

    @T.default("node_selector_box")
    def _make_node_selector_box(self):
        return W.VBox()

    @T.default("cyto_widget")
    def _make_default_cyto_widget(self):
        cyto_widget = CytoscapeWidget(box_select_enabled=True, )
        cyto_widget.on("node", "click", self.log_node_clicks)
        cyto_widget.on("edge", "click", self.log_edge_clicks)
        cyto_widget.on("node", "boxselect", self.log_box_select)

        cyto_widget.set_style([self.node_style, self.edge_style])
        return cyto_widget

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.output_title = W.HTML("<h3>Output from Clicks</h3>")
        self.click_output_box.children = [self.output_title, self.click_output]

        T.dlink(
            (self, "show_outputs"),
            (self.click_output_box.layout, "visibility"),
            lambda x: "visible" if x else "hidden",
        )

        if self.show_outputs is True:
            self.children = [
                self.cyto_widget,
                self.click_output_box,
            ]
        else:
            self.children = [
                self.cyto_widget,
            ]

    def log_node_clicks(self, node):
        self.selected_nodes = tuple([URIRef(node["data"]["id"])])
        with self.click_output:
            print(f"node clicked: {node['data']}")
            print("-------------------------------")

    def log_edge_clicks(self, edge):
        self.selected_edges.append(edge["data"])
        with self.click_output:
            print("edge clicked:")
            print(f'edge source: {edge["data"]["source"]}')
            print(f'edge target: {edge["data"]["target"]}')
            print("-------------------------------")

    def log_box_select(self, arg):
        with self.click_output:
            print(arg)

    @T.observe("graph")
    def update_cyto_widget_graph(self, change):
        try:
            self.cyto_widget.headless = True
            for edge in list(self.cyto_widget.graph.edges):
                try:
                    self.cyto_widget.graph.remove_edge(edge)
                except Exception as err:
                    with self.log:
                        print(f"error removing node {edge}:\n{err}")

            for node in list(self.cyto_widget.graph.nodes):
                try:
                    self.cyto_widget.graph.remove_node(node)
                except Exception as err:
                    with self.log:
                        print(f"error removing node {node}:\n{err}")

            if len(self.cyto_widget.graph.nodes) != 0:
                with self.log:
                    print(
                        "Unexpected number of nodes remaining after graph cleared."
                    )
        finally:
            self.cyto_widget.headless = False
        new_json = self.build_cytoscape_json(change.new)
        self.cyto_widget.graph.add_graph_from_json(new_json, directed=True)

    def build_cytoscape_json(self, graph: Graph):
        """
        A function to build the specific json format that
        ipycytoscape reads. Takes in an rdflib.graph.Graph object as
        a parameter.
        """
        # collect uris & edges
        element_set = set()
        edges = []
        for i, (s, p, o) in enumerate(graph):
            if p != RDF.type:
                if isinstance(s, URIRef):
                    element_set.add(s)
                if isinstance(o, URIRef):
                    element_set.add(o)
                if not isinstance(o, Literal):
                    edges.append({
                        "source": s,
                        "target": o,
                        "label": Path(p).name
                    })
        # create nodes
        nodes = {}
        for uri in element_set:
            pathed_node = Path(uri)
            nodes[uri] = {
                "id": uri,
                "name": f"{pathed_node.parent.name} {pathed_node.name}",
            }
        return {
            "nodes": [{
                "data": v
            } for v in nodes.values()],
            "edges": [{
                "data": v
            } for v in edges],
        }
Пример #14
0
def all_widgets():

    to_return = []
    m1 = Model()

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

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

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

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

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

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

    dl_button.on_click(dl_on_button_click)

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

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

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

    dp_button.on_click(dp_on_button_click)

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

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

    ri_button.on_click(ri_on_button_click)

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

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

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

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

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

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

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

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

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

    tm_button.on_click(tm_on_button_clicked)

    return to_return
Пример #15
0
def build_toolbox(tools_dict, max_width="1080px", max_height="600px"):
    """Build the GEE toolbox.

    Args:
        tools_dict (dict): A dictionary containing information for all tools.
        max_width (str, optional): The maximum width of the widget.
        max_height (str, optional): The maximum height of the widget.

    Returns:
        object: An ipywidget representing the toolbox.
    """
    left_widget = widgets.VBox(layout=widgets.Layout(min_width="175px"))
    center_widget = widgets.VBox(
        layout=widgets.Layout(min_width="200px", max_width="200px")
    )
    right_widget = widgets.Output(
        layout=widgets.Layout(width="630px", max_height=max_height)
    )
    full_widget = widgets.HBox(
        [left_widget, center_widget, right_widget],
        layout=widgets.Layout(max_width=max_width, max_height=max_height),
    )

    search_widget = widgets.Text(
        placeholder="Search tools ...", layout=widgets.Layout(width="170px")
    )
    label_widget = widgets.Label(layout=widgets.Layout(width="170px"))
    label_widget.value = f"{len(tools_dict)} Available Tools"
    close_btn = widgets.Button(
        description="Close Toolbox", icon="close", layout=widgets.Layout(width="170px")
    )

    categories = {}
    categories["All Tools"] = []
    for key in tools_dict.keys():
        category = tools_dict[key]["category"]
        if category not in categories.keys():
            categories[category] = []
        categories[category].append(tools_dict[key]["name"])
        categories["All Tools"].append(tools_dict[key]["name"])

    options = list(categories.keys())
    all_tools = categories["All Tools"]
    all_tools.sort()
    category_widget = widgets.Select(
        options=options, layout=widgets.Layout(width="170px", height="165px")
    )
    tools_widget = widgets.Select(
        options=[], layout=widgets.Layout(width="195px", height="400px")
    )

    def category_selected(change):
        if change["new"]:
            selected = change["owner"].value
            options = categories[selected]
            options.sort()
            tools_widget.options = options
            label_widget.value = f"{len(options)} Available Tools"

    category_widget.observe(category_selected, "value")

    def tool_selected(change):
        if change["new"]:
            selected = change["owner"].value
            tool_dict = tools_dict[selected]
            with right_widget:
                right_widget.clear_output()
                display(tool_gui(tool_dict, max_height=max_height))

    tools_widget.observe(tool_selected, "value")

    def search_changed(change):
        if change["new"]:
            keyword = change["owner"].value
            if len(keyword) > 0:
                selected_tools = []
                for tool in all_tools:
                    if keyword.lower() in tool.lower():
                        selected_tools.append(tool)
                if len(selected_tools) > 0:
                    tools_widget.options = selected_tools
                label_widget.value = f"{len(selected_tools)} Available Tools"
        else:
            tools_widget.options = all_tools
            label_widget.value = f"{len(tools_dict)} Available Tools"

    search_widget.observe(search_changed, "value")

    def close_btn_clicked(b):
        full_widget.close()

    close_btn.on_click(close_btn_clicked)

    category_widget.value = list(categories.keys())[0]
    tools_widget.options = all_tools
    left_widget.children = [category_widget, search_widget, label_widget, close_btn]
    center_widget.children = [tools_widget]

    return full_widget
Пример #16
0
    def start_viewer(self, cad_width, cad_height, theme):
        info(f"zmq_port:   {self.zmq_port}")
        info(f"theme:      {theme}")
        info(f"cad_width:  {cad_width}")
        info(f"cad_height: {cad_height}")

        set_defaults(theme=theme, cad_width=cad_width, height=cad_height)

        self.interactive = widgets.Output(layout=widgets.Layout(height="0px"))

        self.cad_display = CadqueryDisplay()
        cad_view = self.cad_display.create()

        # remove jupyter cadquery start message
        clear_output()

        self.log_view = widgets.Accordion(children=[self.log_output])
        self.log_view.set_title(0, "Log")
        self.log_view.selected_index = None
        display(widgets.VBox([cad_view, self.interactive, self.log_view]))

        logo = pickle.loads(base64.b64decode(LOGO_DATA))
        self._display(logo, True)
        self.cad_display.splash = True

        stop_viewer()

        context = zmq.Context()
        socket = context.socket(zmq.REP)
        for i in range(5):
            try:
                socket.bind(f"tcp://*:{self.zmq_port}")
                break
            except Exception as ex:
                print(f"{ex}: retrying ... ")
                time.sleep(1)

        self.zmq_server = socket
        info("zmq started\n")

        def return_error(error_msg):
            error(error_msg)
            socket.send_json({"result": "error", "msg": error_msg})

        def return_success(t):
            info(f"duration: {time.time() - t:7.2f}")
            socket.send_json({"result": "success"})

        def msg_handler():
            while True:
                msg = socket.recv()
                try:
                    data = pickle.loads(msg)
                except Exception as ex:
                    return_error(str(ex))
                    continue

                self.interactive.outputs = ()
                self.interactive.layout.height = f"0px"

                if data.get("type") == "data":
                    try:
                        t = time.time()
                        self._display(data)
                        return_success(t)

                    except Exception as ex:
                        error_msg = f"{type(ex).__name__}: {ex}"
                        return_error(error_msg)

                elif data.get("type") == "animation":
                    try:
                        t = time.time()
                        animation = Animation(self.root_group)
                        for track in data["tracks"]:
                            animation.add_track(*track)
                        widget = animation.animate(data["speed"],
                                                   data["autoplay"])

                        # With INTERACTIVE: display(widget) does not work, see
                        # https://ipywidgets.readthedocs.io/en/7.6.3/examples/Output%20Widget.html#Interacting-with-output-widgets-from-background-threads
                        #
                        # INTERACTIVE.addpend_display_data(widget) doesn't work either, see https://github.com/jupyter-widgets/ipywidgets/issues/1811
                        # Should be solved, however isn't
                        #
                        mime_data = {
                            "output_type": "display_data",
                            "data": {
                                "text/plain": "AnimationAction",
                                "application/vnd.jupyter.widget-view+json": {
                                    "version_major": 2,
                                    "version_minor": 0,
                                    "model_id": widget.model_id,
                                },
                            },
                            "metadata": {},
                        }
                        self.interactive.outputs = (mime_data, )
                        self.interactive.layout.height = f"40px"
                        return_success(t)

                    except Exception as ex:
                        error_msg = f"{type(ex).__name__}: {ex}"
                        return_error(error_msg)
                else:
                    return_error(f"Wrong message type {data.get('type')}")

        thread = threading.Thread(target=msg_handler)
        thread.setDaemon(True)
        thread.start()
        self.cad_display.info.add_html("<b>zmq server started</b>")
Пример #17
0
def timelapse(m=None):
    """Creates timelapse animations.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    temp_output = widgets.Output()

    def submit_clicked(b):

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

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

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

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

        if m is not None:

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

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

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

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

    create_gif.on_click(submit_clicked)

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

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

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

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

    def handle_toolbar_event(event):

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

    toolbar_event.on_dom_event(handle_toolbar_event)

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

    toolbar_button.observe(toolbar_btn_click, "value")

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

    close_button.observe(close_btn_click, "value")

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

        buttons.value = None

    buttons.observe(button_clicked, "value")

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

        if toolbar_control not in m.controls:
            m.add_control(toolbar_control)
            m.tool_control = toolbar_control
    else:
        return toolbar_widget
Пример #18
0
def catmaid(predictions, testslide_subfolders, fig_size, fig_name):

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

    #figure (FM prediction)
    slide_test = predictions

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

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

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

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

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

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

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

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

        #calculate things
        #accuracy:

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

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

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

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

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

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

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

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

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

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

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

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

    button.on_click(test)

    display(button, button_output, slids_out, func_out)
Пример #19
0
    def initGui(self):

        selectWidgetHeight = '175px'

        # x stat list
        xStatList = widgets.Select(
            options=self._statList,
            value=self._statList[0],
            # rows=10,
            description='',
            disabled=False,
            layout={'height': selectWidgetHeight})
        xStatList.observe(self.on_x_stat_select, names='value')

        # y stat list
        yStatList = widgets.Select(
            options=self._statList,
            value=self._statList[0],
            # rows=10,
            description='',
            disabled=False,
            layout={'height': selectWidgetHeight})
        yStatList.observe(self.on_y_stat_select, names='value')

        # corresponds to hue
        if self._hueCols is not None:
            # popup of hue cols
            hueDropdown = widgets.Dropdown(
                options=self._hueCols,
                value=self._hueCols[0],
                description='',
                disabled=False,
            )
            hueDropdown.observe(self.on_select_hue0, names='value')
            # list of unique objects (from df) for a given hue
            self.selectFiles = widgets.SelectMultiple(
                options=self._selectedHues,
                value=self._selectedHues,  # select all
                rows=8,
                description='',
                disabled=False)
            self.selectFiles.observe(self.on_select_hue, names='value')

            hueBox = widgets.VBox([hueDropdown, self.selectFiles])

        hBox = widgets.HBox([xStatList, yStatList, hueBox])
        display(hBox)

        #
        checkboxList = []
        for key, value in self.plotOptions.items():
            aCheckbox = widgets.Checkbox(description=key,
                                         value=value,
                                         disabled=False,
                                         indent=False)
            aCheckbox.observe(self.on_checkbox, names='value')
            checkboxList.append(aCheckbox)

        hBox = widgets.HBox(checkboxList)
        display(hBox)

        # output to show one line update
        self.myScatterOut = widgets.Output()
        hBox = widgets.HBox([self.myScatterOut])
        display(hBox)

        # scatter plot
        self.scatterFig = plt.figure(figsize=(6, 6))
        #self.scatterFig.set_tight_layout(True)

        self.gs = self.scatterFig.add_gridspec(2,
                                               2,
                                               width_ratios=(7, 2),
                                               height_ratios=(2, 7),
                                               left=0.1,
                                               right=0.9,
                                               bottom=0.1,
                                               top=0.9,
                                               wspace=0.05,
                                               hspace=0.05)

        self.axScatter = self.scatterFig.add_subplot(self.gs[1, 0])

        self.scatterFig.canvas.mpl_connect('pick_event', self.on_scatter_pick)

        # x/y hist
        marginalHist = True
        if marginalHist:
            self.axHistX = self.scatterFig.add_subplot(self.gs[0, 0],
                                                       sharex=self.axScatter)
            self.axHistY = self.scatterFig.add_subplot(self.gs[1, 1],
                                                       sharey=self.axScatter)
            #
            self.axHistX.spines['right'].set_visible(False)
            self.axHistX.spines['top'].set_visible(False)
            self.axHistY.spines['right'].set_visible(False)
            self.axHistY.spines['top'].set_visible(False)

        # output widgets to show table of x/y stats
        self.myOutStat = widgets.Output()
        hBox = widgets.HBox([self.myOutStat])
        display(hBox)
Пример #20
0
def backend_widget(backend):
    """Creates a backend widget.
    """
    config = backend.configuration().to_dict()
    props = backend.properties().to_dict()

    name = widgets.HTML(value="<h4>{name}</h4>".format(name=backend.name()),
                        layout=widgets.Layout())

    n_qubits = config['n_qubits']

    qubit_count = widgets.HTML(
        value="<h5><b>{qubits}</b></h5>".format(qubits=n_qubits),
        layout=widgets.Layout(justify_content='center'))

    cmap = widgets.Output(layout=widgets.Layout(min_width='250px',
                                                max_width='250px',
                                                max_height='250px',
                                                min_height='250px',
                                                justify_content='center',
                                                align_items='center',
                                                margin='0px 0px 0px 0px'))

    with cmap:
        _cmap_fig = plot_gate_map(backend,
                                  plot_directed=False,
                                  label_qubits=False)
        if _cmap_fig is not None:
            display(_cmap_fig)
            # Prevents plot from showing up twice.
            plt.close(_cmap_fig)

    pending = generate_jobs_pending_widget()

    is_oper = widgets.HTML(value="<h5></h5>",
                           layout=widgets.Layout(justify_content='center'))

    least_busy = widgets.HTML(value="<h5></h5>",
                              layout=widgets.Layout(justify_content='center'))

    t1_units = props['qubits'][0][0]['unit']
    avg_t1 = round(sum([q[0]['value'] for q in props['qubits']]) / n_qubits, 1)
    avg_t2 = round(sum([q[1]['value'] for q in props['qubits']]) / n_qubits, 1)
    t12_widget = widgets.HTML(value="<h5>{t1} / {t2} {units}</h5>".format(
        t1=avg_t1, t2=avg_t2, units=t1_units),
                              layout=widgets.Layout())

    sum_cx_err = 0
    num_cx = 0
    for gate in props['gates']:
        if gate['gate'] == 'cx':
            for param in gate['parameters']:
                if param['name'] == 'gate_error':
                    # Value == 1.0 means gate effectively off
                    if param['value'] != 1.0:
                        sum_cx_err += param['value']
                        num_cx += 1
    avg_cx_err = round(sum_cx_err / (num_cx), 4)
    cx_widget = widgets.HTML(
        value="<h5>{cx_err}</h5>".format(cx_err=avg_cx_err),
        layout=widgets.Layout())

    avg_meas_err = 0
    for qub in props['qubits']:
        for item in qub:
            if item['name'] == 'readout_error':
                avg_meas_err += item['value']
    avg_meas_err = round(avg_meas_err / n_qubits, 4)
    meas_widget = widgets.HTML(
        value="<h5>{meas_err}</h5>".format(meas_err=avg_meas_err),
        layout=widgets.Layout())

    out = widgets.VBox([
        name, cmap, qubit_count, pending, is_oper, least_busy, t12_widget,
        cx_widget, meas_widget
    ],
                       layout=widgets.Layout(display='inline-flex',
                                             flex_flow='column',
                                             align_items='center'))

    out._is_alive = True
    return out
Пример #21
0
def eda_num_vs_target_binary(dataset):
    dataset_num = dataset.select_dtypes(exclude='object')
    dataset_num_target = pd.DataFrame(dataset[dataset.columns.values[-1]])

    if dataset[dataset.columns.values[-1]].dtype == object:
        dataset = pd.concat([dataset_num, dataset_num_target], axis=1)
    else:
        dataset = dataset_num.copy()

    tab_contents = [i for i in dataset.columns.values[0:-1]]
    children = [widgets.Output() for value in tab_contents]
    tab = widgets.Tab(children=children)
    [tab.set_title(num, name) for num, name in enumerate(tab_contents)]
    display(tab)

    for i, k in enumerate(dataset.columns.values[0:-1]):
        #dataset['conversions'] = dataset_num_target.copy()
        # descriptive stats table
        dataset1 = pd.DataFrame(dataset[k].describe(exclude='object'))
        dataset1.columns = ['']
        dataset1 = dataset1.drop('count')
        dataset1.loc['missing'] = dataset[k].isnull().sum().sum()
        dataset1.loc['kurtosis'] = dataset[k].kurtosis(axis=0)
        dataset1.loc['skew'] = dataset[k].skew(axis=0)
        dataset1 = dataset1.T
        dataset1.index.name = 'Descriptive Statistics'
        dataset1 = dataset1.style.set_precision(2)

        out_dataset1 = widgets.Output()
        out_dataset2_styled = widgets.Output()
        with out_dataset1:
            display(dataset1)

        hbox = widgets.HBox([out_dataset1])

        with children[i]:
            display(hbox)

            x_titles = list(dataset.columns)

            fig1 = px.histogram(dataset,
                                x=dataset[k],
                                color=dataset[dataset.columns.values[-1]],
                                facet_row=dataset[dataset.columns.values[-1]],
                                color_discrete_sequence=['blue', 'red'])
            fig2 = px.box(dataset,
                          y=dataset[k],
                          color=dataset[dataset.columns.values[-1]],
                          color_discrete_sequence=['blue', 'red'])

            trace1 = fig1['data'][0]  #fig1 y=n0
            trace2 = fig1['data'][1]  #fig1 y=yes
            trace3 = fig2['data'][0]
            trace4 = fig2['data'][1]

            fig1['data'][0]['showlegend'] = True
            fig1['data'][1]['showlegend'] = True
            fig1['data'][0]['showlegend'] = False
            fig1['data'][1]['showlegend'] = False

            fig = make_subplots(rows=2,
                                cols=2,
                                subplot_titles=['Histogram', 'Box plot'],
                                specs=[[{
                                    'type': 'histogram'
                                }, {
                                    'type': 'box',
                                    'rowspan': 2
                                }], [{
                                    'type': 'histogram'
                                }, None]])
            fig.layout["xaxis"].title.text = ''
            fig.layout["xaxis2"].title.text = ''
            fig.layout["xaxis3"].title.text = k
            fig.layout["yaxis"].title.text = 'count'
            fig.layout["yaxis2"].title.text = k
            fig.layout["yaxis3"].title.text = 'count'

            fig.add_trace(trace1, 1, 1)
            fig.add_trace(trace2, 2, 1)
            fig.add_trace(trace3, 1, 2)
            fig.add_trace(trace4, 1, 2)

            fig.update_layout(template='plotly_dark', boxmode='group')
            fig.show()
Пример #22
0
    def _repr_html_(self):
        from IPython.display import display
        from IPython.display import clear_output
        import ipywidgets as widgets

        try:
            if self._pandas_only:
                display(self.display_pandas())
                self._pandas_only = False
            else:
                if self.index.nlevels >= 2 or self.columns.nlevels >= 2:
                    warnings.warn(
                        "\nLux does not currently support dataframes "
                        "with hierarchical indexes.\n"
                        "Please convert the dataframe into a flat "
                        "table via `pandas.DataFrame.reset_index`.\n",
                        stacklevel=2,
                    )
                    display(self.display_pandas())
                    return

                if len(self) <= 0:
                    warnings.warn(
                        "\nLux can not operate on an empty dataframe.\nPlease check your input again.\n",
                        stacklevel=2,
                    )
                    display(self.display_pandas())
                    return
                self.maintain_metadata()

                if self._intent != [] and (not hasattr(self, "_compiled")
                                           or not self._compiled):
                    from lux.processor.Compiler import Compiler

                    self.current_vis = Compiler.compile_intent(
                        self, self._intent)

                if lux.config.default_display == "lux":
                    self._toggle_pandas_display = False
                else:
                    self._toggle_pandas_display = True

                # df_to_display.maintain_recs() # compute the recommendations (TODO: This can be rendered in another thread in the background to populate self._widget)
                self.maintain_recs()

                # Observers(callback_function, listen_to_this_variable)
                self._widget.observe(self.remove_deleted_recs,
                                     names="deletedIndices")
                self._widget.observe(self.set_intent_on_click,
                                     names="selectedIntentIndex")

                if len(self._recommendation) > 0:
                    # box = widgets.Box(layout=widgets.Layout(display='inline'))
                    button = widgets.Button(
                        description="Toggle Pandas/Lux",
                        layout=widgets.Layout(width="140px", top="5px"),
                    )
                    self.output = widgets.Output()
                    # box.children = [button,output]
                    # output.children = [button]
                    # display(box)
                    display(button, self.output)

                    def on_button_clicked(b):
                        with self.output:
                            if b:
                                self._toggle_pandas_display = not self._toggle_pandas_display
                            clear_output()
                            if self._toggle_pandas_display:
                                display(self.display_pandas())
                            else:
                                # b.layout.display = "none"
                                display(self._widget)
                                # b.layout.display = "inline-block"

                    button.on_click(on_button_clicked)
                    on_button_clicked(None)
                else:
                    warnings.warn(
                        "\nLux defaults to Pandas when there are no valid actions defined.",
                        stacklevel=2,
                    )
                    display(self.display_pandas())

        except (KeyboardInterrupt, SystemExit):
            raise
        except Exception:
            if lux.config.pandas_fallback:
                warnings.warn(
                    "\nUnexpected error in rendering Lux widget and recommendations. "
                    "Falling back to Pandas display.\n"
                    "Please report the following issue on Github: https://github.com/lux-org/lux/issues \n",
                    stacklevel=2,
                )
                warnings.warn(traceback.format_exc())
                display(self.display_pandas())
            else:
                raise
Пример #23
0
def display_gui(documents, corpus, domain_logic):

    compute_callback = compute_most_discriminating_terms
    display_callback = display_most_discriminating_terms

    lw = lambda w: ipywidgets.Layout(width=w)

    include_pos_tags = ['ADJ', 'VERB', 'NUM', 'ADV', 'NOUN', 'PROPN']
    normalize_options = {
        'Lemma': spacy.attrs.LEMMA,
        'Lower': spacy.attrs.LOWER,
        'Orth': spacy.attrs.ORTH
    }

    # FIXME VARYING ASPECTS:
    #party_preset_options = document_index.get_party_preset_options()
    #parties_options = [ x for x in document_index.get_countries_list() if x != 'ALL OTHER' ]
    #group_options = document_index.genre.unique()
    #signed_years = { d._.meta['signed_year'] for d in corpus }
    #period_default = (min(signed_years), max(signed_years))

    # FIXME VARYING ASPECTS:
    group1_filters = generate_group_filters(documents,
                                            domain_logic.DOCUMENT_FILTERS)
    group2_filters = generate_group_filters(documents,
                                            domain_logic.DOCUMENT_FILTERS)

    gui = types.SimpleNamespace(
        progress=ipywidgets.IntProgress(value=0,
                                        min=0,
                                        max=5,
                                        step=1,
                                        description='',
                                        layout=lw('90%')),
        group1_filters=group1_filters,
        group2_filters=group2_filters,
        #group1_preset=utility.widgets.dropdown('Presets', party_preset_options, None, layout=lw('250px')),
        #group2_preset=utility.widgets.dropdown('Presets', party_preset_options, None, layout=lw('250px')),
        top_n_terms=ipywidgets.IntSlider(
            description='#terms',
            min=10,
            max=1000,
            value=100,
            tooltip=
            'The total number of most discriminating terms to return for each group'
        ),
        max_n_terms=ipywidgets.IntSlider(
            description='#top',
            min=1,
            max=2000,
            value=2000,
            tooltip=
            'Only consider terms whose document frequency is within the top # terms out of all terms'
        ),
        include_pos=ipywidgets.SelectMultiple(description='POS',
                                              options=include_pos_tags,
                                              value=include_pos_tags,
                                              rows=7,
                                              layout=lw('150px')),
        #period1=ipywidgets.IntRangeSlider(description='Period', min=period_default[0], max=period_default[1], value=period_default, layout=lw('250px')),
        #period2=ipywidgets.IntRangeSlider(description='Period', min=period_default[0], max=period_default[1], value=period_default, layout=lw('250px')),
        #closed_region=ipywidgets.ToggleButton(description='Closed regions', icon='check', value=True, disabled=False, layout=lw('140px')),
        #sync_period=ipywidgets.ToggleButton(description='Sync period', icon='check', value=False, disabled=False, layout=lw('140px'), tooltop='HEJ'),
        normalize=ipywidgets.Dropdown(description='Normalize',
                                      options=normalize_options,
                                      value=spacy.attrs.LEMMA,
                                      layout=lw('200px')),
        compute=ipywidgets.Button(description='Compute',
                                  icon='',
                                  button_style='Success',
                                  layout=lw('120px')),
        output=ipywidgets.Output(layout={'border': '1px solid black'}))

    boxes = ipywidgets.VBox([
        ipywidgets.HBox([
            ipywidgets.VBox([x['widget'] for x in group1_filters]),
            ipywidgets.VBox([x['widget'] for x in group2_filters]),
            ipywidgets.VBox([gui.include_pos],
                            layout=ipywidgets.Layout(align_items='flex-end')),
            ipywidgets.VBox(
                [gui.top_n_terms, gui.max_n_terms, gui.normalize, gui.compute],
                layout=ipywidgets.Layout(align_items='flex-end')
            ),  # layout=ipywidgets.Layout(align_items='flex-end')),
            #    ipywidgets.VBox([
            #        gui.compute,
            #        ipywidgets.HTML(
            #            '<b>#terms</b> is the  number of most discriminating<br>terms to return for each group.<br>' +
            #            '<b>#top</b> Consider only terms with a frequency<br>within the top #top terms out of all terms<br>'
            #            '<b>Closed region</b> If checked, then <u>both</u> treaty parties<br>must be within selected region'
            #        )
            #    ])
        ]),
        gui.output
    ])

    display(boxes)

    #def on_group1_preset_change(change):
    #    if gui.group1_preset.value is None:
    #        return
    #    gui.group1.value = gui.group1.options if 'ALL' in gui.group1_preset.value else gui.group1_preset.value

    #def on_group2_preset_change(change):
    #    if gui.group2_preset.value is None:
    #        return
    #    gui.group2.value = gui.group2.options if 'ALL' in gui.group2_preset.value else gui.group2_preset.value

    #def on_period1_change(change):
    #    if gui.sync_period.value:
    #        gui.period2.value = gui.period1.value

    #def on_period2_change(change):
    #    if gui.sync_period.value:
    #        gui.period1.value = gui.period2.value

    #gui.group1_preset.observe(on_group1_preset_change, names='value')
    #gui.group2_preset.observe(on_group2_preset_change, names='value')

    #gui.period1.observe(on_period1_change, names='value')
    #gui.period2.observe(on_period2_change, names='value')

    def compute_callback_handler(*_args):
        gui.output.clear_output()
        with gui.output:
            try:
                gui.compute.disabled = True
                df = compute_callback(
                    documents=documents,
                    corpus=corpus,
                    group1_filters=[(x['field'], x['widget'].value)
                                    for x in group1_filters],
                    group2_filters=[(x['field'], x['widget'].value)
                                    for x in group2_filters],
                    top_n_terms=gui.top_n_terms.value,
                    max_n_terms=gui.max_n_terms.value,
                    include_pos=gui.include_pos.value,
                    #period1=gui.period1.value,
                    #period2=gui.period2.value,
                    #closed_region=gui.closed_region.value,
                    normalize=gui.normalize.value)
                if df is not None:
                    display_callback(df)
                else:
                    logger.info('No data for selected groups or periods.')
            except Exception as ex:
                logger.error(ex)
            finally:
                gui.compute.disabled = False

    gui.compute.on_click(compute_callback_handler)
    return gui
Пример #24
0
def tool_template(m=None):

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    def handle_toolbar_event(event):

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

    toolbar_event.on_dom_event(handle_toolbar_event)

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

    toolbar_button.observe(toolbar_btn_click, "value")

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

    close_button.observe(close_btn_click, "value")

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

        buttons.value = None

    buttons.observe(button_clicked, "value")

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

        if toolbar_control not in m.controls:
            m.add_control(toolbar_control)
            m.tool_control = toolbar_control
    else:
        return toolbar_widget
Пример #25
0
def to_output_widget(items) -> widgets.Widget:
    out = widgets.Output()
    with out:
        for item in items:
            ip.display(item)
    return out
Пример #26
0
def open_data_widget(m):
    """A widget for opening local vector/raster data.

    Args:
        m (object): geemap.Map
    """
    tool_output = widgets.Output()
    tool_output_ctrl = WidgetControl(widget=tool_output, position="topright")

    if m.tool_output_ctrl is not None and m.tool_output_ctrl in m.controls:
        m.remove_control(m.tool_output_ctrl)

    file_type = widgets.ToggleButtons(
        options=["Shapefile", "GeoJSON", "GeoTIFF"],
        tooltips=[
            "Open a shapefile",
            "Open a GeoJSON file",
            "Open a GeoTIFF",
        ],
    )

    file_chooser = FileChooser(os.getcwd())
    file_chooser.filter_pattern = "*.shp"
    file_chooser.use_dir_icons = True

    style = {"description_width": "initial"}
    layer_name = widgets.Text(
        value="Shapefile",
        description="Enter a layer name:",
        tooltip="Enter a layer name for the selected file",
        style=style,
        layout=widgets.Layout(width="454px", padding="0px 0px 0px 5px"),
    )

    convert_bool = widgets.Checkbox(
        description="Convert to ee.FeatureCollection?",
        indent=False,
        layout=widgets.Layout(padding="0px 0px 0px 5px"),
    )
    convert_hbox = widgets.HBox([convert_bool])

    ok_cancel = widgets.ToggleButtons(
        value=None,
        options=["Apply", "Reset", "Close"],
        tooltips=["Apply", "Reset", "Close"],
        button_style="primary",
    )

    bands = widgets.Text(
        value="1",
        description="Bands:",
        tooltip="Enter a list of band indices",
        style=style,
        layout=widgets.Layout(width="110px"),
    )

    colormap = widgets.Dropdown(
        options=[],
        value=None,
        description="colormap:",
        layout=widgets.Layout(width="172px"),
        style=style,
    )

    x_dim = widgets.Text(
        value="x",
        description="x_dim:",
        tooltip="The x dimension",
        style=style,
        layout=widgets.Layout(width="80px"),
    )

    y_dim = widgets.Text(
        value="y",
        description="y_dim:",
        tooltip="The xydimension",
        style=style,
        layout=widgets.Layout(width="80px"),
    )

    raster_options = widgets.HBox()

    main_widget = widgets.VBox(
        [file_type, file_chooser, layer_name, convert_hbox, raster_options, ok_cancel]
    )

    tool_output.clear_output()
    with tool_output:
        display(main_widget)

    # def chooser_callback(chooser):
    #     if len(layer_name.value) == 0 and file_chooser.selected is not None:
    #         layer_name.value = os.path.splitext(file_chooser.selected_filename)[0]

    def bands_changed(change):
        if change["new"] and "," in change["owner"].value:
            colormap.value = None
            colormap.disabled = True
        else:
            colormap.disabled = False

    bands.observe(bands_changed, "value")

    def file_type_changed(change):
        ok_cancel.value = None
        file_chooser.default_path = os.getcwd()
        file_chooser.reset()
        layer_name.value = file_type.value
        if change["new"] == "Shapefile":
            file_chooser.filter_pattern = "*.shp"
            raster_options.children = []
            convert_hbox.children = [convert_bool]
        elif change["new"] == "GeoJSON":
            file_chooser.filter_pattern = "*.geojson"
            raster_options.children = []
            convert_hbox.children = [convert_bool]
        elif change["new"] == "GeoTIFF":
            import matplotlib.pyplot as plt

            file_chooser.filter_pattern = "*.tif"
            colormap.options = plt.colormaps()
            colormap.value = "terrain"
            raster_options.children = [bands, colormap, x_dim, y_dim]
            convert_hbox.children = []

    def ok_cancel_clicked(change):
        if change["new"] == "Apply":
            m.default_style = {"cursor": "wait"}
            file_path = file_chooser.selected

            if file_path is not None:
                ext = os.path.splitext(file_path)[1]
                with tool_output:
                    if ext.lower() == ".shp":
                        if convert_bool.value:
                            ee_object = shp_to_ee(file_path)
                            m.addLayer(ee_object, {}, layer_name.value)
                            m.centerObject(ee_object)
                        else:
                            m.add_shapefile(
                                file_path, style=None, layer_name=layer_name.value
                            )
                    elif ext.lower() == ".geojson":
                        if convert_bool.value:
                            ee_object = geojson_to_ee(file_path)
                            m.addLayer(ee_object, {}, layer_name.value)
                            m.centerObject(ee_object)
                        else:
                            m.add_geojson(
                                file_path, style=None, layer_name=layer_name.value
                            )
                    elif ext.lower() == ".tif":
                        sel_bands = [int(b.strip()) for b in bands.value.split(",")]
                        m.add_raster(
                            image=file_path,
                            bands=sel_bands,
                            layer_name=layer_name.value,
                            colormap=colormap.value,
                            x_dim=x_dim.value,
                            y_dim=y_dim.value,
                        )
            else:
                print("Please select a file to open.")

            m.toolbar_reset()
            m.default_style = {"cursor": "default"}

        elif change["new"] == "Reset":
            file_chooser.reset()
            tool_output.clear_output()
            with tool_output:
                display(main_widget)
            m.toolbar_reset()
        elif change["new"] == "Close":
            if m.tool_output_ctrl is not None and m.tool_output_ctrl in m.controls:
                m.remove_control(m.tool_output_ctrl)
                m.tool_output_ctrl = None
                m.toolbar_reset()

        ok_cancel.value = None

    file_type.observe(file_type_changed, names="value")
    ok_cancel.observe(ok_cancel_clicked, names="value")
    # file_chooser.register_callback(chooser_callback)

    m.add_control(tool_output_ctrl)
    m.tool_output_ctrl = tool_output_ctrl
Пример #27
0
    def __init__(self,
                 m_desc,
                 FuseEdges=False,
                 pick_start=False,
                 max_width=10.0,
                 accept_color='chartreuse3',
                 reject_color='red',
                 neutral_color='dodgerblue2'):
        # Options
        self.color_accept = accept_color
        self.color_reject = reject_color
        self.color_neutral = neutral_color
        self.max_width = max_width
        self.fuse = FuseEdges
        # DFA specific option

        self.valid_input = True
        self.machine = m_desc
        self.machine_obj = dotObj_nfa(self.machine, FuseEdges=FuseEdges)
        self.copy_source = reformat_edge_labels(
            set_graph_size(self.machine_obj.source, max_width),
            additional=' color=black arrowsize=1 penwidth=1')

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

        # Setup the widgets
        # Top row for user input
        self.user_input = widgets.Text(value='',
                                       placeholder='Sigma: {{{}}}'.format(
                                           ','.join(
                                               sorted(self.machine['Sigma']))),
                                       description='Input:',
                                       layout=Layout(width='500px'))
        self.user_input.observe(self.on_input_change, names='value')
        self.alternate_start = widgets.SelectMultiple(
            options=sorted(self.machine['Q']),
            value=tuple(self.machine['Q0']),
            rows=5,
            description='Start States:',
            disabled=False,
            layout=Layout(width='200px'))
        self.generate_button = widgets.Button(description="Animate",
                                              button_style='primary',
                                              disabled=False)
        self.generate_button.on_click(self.generate_animation)

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

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

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

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

        # set the widget to display the feed
        self.feed_display = widgets.Output()

        # arrange the widgets in the display area
        row1 = widgets.HBox([self.user_input, self.generate_button])
        if pick_start:
            row1 = widgets.HBox(
                [self.user_input, self.alternate_start, self.generate_button])
        row2 = widgets.HBox([
            self.play_controls, self.backward, self.forward, self.speed_control
        ])
        w = widgets.VBox([row1, self.machine_display, self.feed_display, row2])
        display(w)

        self.play_controls.disabled = True
        self.forward.disabled = True
        self.backward.disabled = True
        self.speed_control.disabled = True
Пример #28
0
def tool_gui(tool_dict, max_width="420px", max_height="600px"):
    """Create a GUI for a tool based on the tool dictionary.

    Args:
        tool_dict (dict): The dictionary containing the tool info.
        max_width (str, optional): The max width of the tool dialog.
        max_height (str, optional): The max height of the tool dialog.

    Returns:
        object: An ipywidget object representing the tool interface.
    """
    tool_widget = widgets.VBox(
        layout=widgets.Layout(max_width=max_width, max_height=max_height)
    )
    children = []
    args = {}
    required_inputs = []
    style = {"description_width": "initial"}
    max_width = str(int(max_width.replace("px", "")) - 10) + "px"

    header_width = str(int(max_width.replace("px", "")) - 104) + "px"
    header = widgets.Label(
        value=f'Current Tool: {tool_dict["label"]}',
        style=style,
        layout=widgets.Layout(width=header_width),
    )
    code_btn = widgets.Button(
        description="View Code", layout=widgets.Layout(width="100px")
    )

    children.append(widgets.HBox([header, code_btn]))

    desc = widgets.Textarea(
        value=f'Description: {tool_dict["description"]}',
        layout=widgets.Layout(width="410px", max_width=max_width),
        disabled=True,
    )
    children.append(desc)

    params = tool_dict["parameters"]
    # for param in params:
    #     items = params[param]
    #     required = ""
    #     if items["optional"] == "false":
    #         required = "*"
    #         required_inputs.append(param)
    #     label = items["name"] + required
    #     param_type = items["parameter_type"]
    #     default_value = None

    #     if (items["default_value"] != "null") and (len(items["default_value"]) > 0):
    #         if "false" in items["default_value"]:
    #             default_value = False
    #         elif "true" in items["default_value"]:
    #             default_value = True
    #         else:
    #             default_value = items["default_value"].replace('"', "")

    #     layout = widgets.Layout(width="500px", max_width=max_width)

    #     if isinstance(param_type, str):
    #         # display(data_types[param_type])

    #         if param_type == "Boolean":
    #             var_widget = widgets.Checkbox(
    #                 description=label, style=style, layout=layout, value=default_value
    #             )
    #         elif param_type in [
    #             "Directory",
    #             "ExistingFile",
    #             "ExistingFileOrFloat",
    #             "FileList",
    #             "NewFile",
    #         ]:
    #             var_widget = FileChooser(title=label)
    #         else:
    #             var_widget = widgets.Text(description=label, style=style, layout=layout)
    #             if default_value is not None:
    #                 var_widget.value = str(default_value)

    #         args[param] = var_widget

    #         children.append(var_widget)
    #     elif isinstance(param_type, dict):

    #         if "OptionList" in param_type:
    #             var_widget = widgets.Dropdown(
    #                 options=param_type["OptionList"],
    #                 description=label,
    #                 style=style,
    #                 layout=layout,
    #             )
    #         elif list(param_type.keys())[0] in [
    #             "Directory",
    #             "ExistingFile",
    #             "ExistingFileOrFloat",
    #             "FileList",
    #             "NewFile",
    #         ]:
    #             var_widget = FileChooser(title=label)
    #         else:
    #             var_widget = FileChooser(title=label)
    #         args[param] = var_widget

    #         children.append(var_widget)

    run_btn = widgets.Button(description="Run", layout=widgets.Layout(width="100px"))
    cancel_btn = widgets.Button(
        description="Cancel", layout=widgets.Layout(width="100px")
    )
    help_btn = widgets.Button(description="Help", layout=widgets.Layout(width="100px"))
    import_btn = widgets.Button(
        description="Import",
        tooltip="Import the script to a new cell",
        layout=widgets.Layout(width="98px"),
    )
    tool_output = widgets.Output(layout=widgets.Layout(max_height="200px"))
    children.append(widgets.HBox([run_btn, cancel_btn, help_btn, import_btn]))
    children.append(tool_output)
    tool_widget.children = children

    def run_button_clicked(b):
        tool_output.clear_output()

        required_params = required_inputs.copy()
        args2 = []
        for arg in args:

            line = ""
            if isinstance(args[arg], FileChooser):
                if arg in required_params and args[arg].selected is None:
                    with tool_output:
                        print(f"Please provide inputs for required parameters.")
                        break
                elif arg in required_params:
                    required_params.remove(arg)
                if arg == "i":
                    line = f"-{arg}={args[arg].selected}"
                else:
                    line = f"--{arg}={args[arg].selected}"
            elif isinstance(args[arg], widgets.Text):
                if arg in required_params and len(args[arg].value) == 0:
                    with tool_output:
                        print(f"Please provide inputs for required parameters.")
                        break
                elif arg in required_params:
                    required_params.remove(arg)
                if args[arg].value is not None and len(args[arg].value) > 0:
                    line = f"--{arg}={args[arg].value}"
            elif isinstance(args[arg], widgets.Checkbox):
                line = f"--{arg}={args[arg].value}"
            args2.append(line)

        if len(required_params) == 0:
            with tool_output:
                # wbt.run_tool(tool_dict["name"], args2)
                pass

    def help_button_clicked(b):
        import webbrowser

        tool_output.clear_output()
        with tool_output:
            html = widgets.HTML(
                value=f'<a href={tool_dict["link"]} target="_blank">{tool_dict["link"]}</a>'
            )
            display(html)
        webbrowser.open_new_tab(tool_dict["link"])

    def code_button_clicked(b):
        import webbrowser

        with tool_output:
            html = widgets.HTML(
                value=f'<a href={tool_dict["link"]} target="_blank">{tool_dict["link"]}</a>'
            )
            display(html)
        webbrowser.open_new_tab(tool_dict["link"])

    def cancel_btn_clicked(b):
        tool_output.clear_output()

    def import_button_clicked(b):
        tool_output.clear_output()

        content = []

        create_code_cell("\n".join(content))

    import_btn.on_click(import_button_clicked)
    run_btn.on_click(run_button_clicked)
    help_btn.on_click(help_button_clicked)
    code_btn.on_click(code_button_clicked)
    cancel_btn.on_click(cancel_btn_clicked)

    return tool_widget
Пример #29
0
from IPython.display import display
from IPython.core.display import Markdown

import rafal

# open a new rafal session and connect
session = rafal.Session(verbose=True)

# check auto connect (no proxies)
env_url = environ.get('rf_url', None)
env_user = environ.get('rf_user', None)
env_pwd = environ.get('rf_pwd', None)
if env_url and env_user and env_pwd:
    user = env_user
    session.connect(url=env_url, login=env_user, pwd=env_pwd)
    uiConnect = widgets.Output()
else:
    # config loading from config.json file
    config = json.load(open(Path(__file__).parent / 'config.json'))
    config_user_file = Path('config_user.json')
    if config_user_file.exists():
        config_user = json.load(open(config_user_file))
        if "Rafal.urls" in config_user:
            config["Rafal.urls"].update(config_user["Rafal.urls"])

    user = config['Rafal.user']
    urls = config['Rafal.urls']

    # UI connect
    myPwd = config.get('Rafal.password', environ.get('jpwd', None))
    pwd_exists = bool(myPwd)
Пример #30
0
from smif.controller.build import get_model_run_definition, build_model_run
from smif.controller.load import load_resolution_sets
from smif.data_layer.datafile_interface import DatafileInterface
from smif.data_layer.data_handle import DataHandle
from smif.model.scenario_model import ScenarioModel

handler = DatafileInterface('./')

available_modelrun = widgets.RadioButtons(
    description='Model Runs:',
    options=sorted([x['name'] for x in handler.read_sos_model_runs()]))

plt.ioff()
dep_ax = plt.gca()

show_dep_graph = widgets.Output()

global models
global store
global modelrun
global dep_graph


def plot_dep_graph(dep_graph):
    show_dep_graph.clear_output(wait=True)
    with show_dep_graph:
        dep_ax.clear()
        dep_graph_relabelled = nx.relabel_nodes(dep_graph,
                                                {x: x.name
                                                 for x in dep_graph},
                                                copy=True)