def __init__(self,
                 features=None,
                 labels=None,
                 options=(),
                 classifier=None,
                 display_func=None,
                 eval_method=None,
                 reorder=None,
                 shuffle_prop=0.1,
                 hints=None,
                 keyboard_shortcuts=False,
                 *args,
                 **kwargs):
        """
        A class for labelling your data.

        This class is designed to label data for (semi-)supervised learning
        algorithms. It allows you to label data, periodically re-train your
        algorithm and assess its performance, and determine which data points
        to label next based on your model's predictions.

        """
        super().__init__(features=features,
                         labels=labels,
                         display_func=display_func,
                         options=options,
                         keyboard_shortcuts=keyboard_shortcuts,
                         hints=hints,
                         *args,
                         **kwargs)

        self.queue = SimpleLabellingQueue(features, labels)

        self.shuffle_prop = shuffle_prop

        self.classifier = validation.valid_classifier(classifier)
        if self.classifier is not None:
            self.retrain_button = widgets.Button(
                description="Retrain",
                disabled=False,
                button_style="",
                tooltip="Click me",
                icon="refresh",
            )
            self.retrain_button.on_click(self.retrain)
            self.model_performance = widgets.HTML(value="")
            self.top_bar.children = (
                widgets.HBox(
                    [*self.top_bar.children],
                    layout=widgets.Layout(width="50%"),
                ),
                widgets.HBox(
                    [self.retrain_button, self.model_performance],
                    layout=widgets.Layout(width="50%"),
                ),
            )

        if eval_method is None:
            self.eval_method = partial(
                sklearn.model_selection.cross_validate,
                cv=3,
                # n_jobs=-1,
                return_train_score=False,
            )
        elif not callable(eval_method):
            raise ValueError("The eval_method needs to be a callable.")
        else:
            self.eval_method = eval_method

        if reorder is not None and isinstance(reorder, str):
            if reorder not in prioritisation.functions:
                raise NotImplementedError(
                    "Unknown reordering function '{}'.".format(reorder))
            self.reorder = prioritisation.functions[reorder]
        elif reorder is not None and callable(reorder):
            self.reorder = reorder
        elif reorder is None:
            self.reorder = None
        else:
            raise ValueError(
                "The reorder argument needs to be either a function or the "
                "name of a function listed in superintendent.prioritisation.")

        self._annotation_loop = self._annotation_iterator()
        next(self._annotation_loop)
        self._compose()
def makeWidgets():
    studyList = [
        'ACC', 'BLCA', 'BRCA', 'CESC', 'CHOL', 'COAD', 'DLBC', 'ESCA', 'GBM',
        'HNSC', 'KICH', 'KIRC', 'KIRP', 'LAML', 'LGG', 'LIHC', 'LUAD', 'LUSC',
        'MESO', 'OV', 'PAAD', 'PCPG', 'PRAD', 'READ', 'SARC', 'SKCM', 'STAD',
        'TGCT', 'THCA', 'THYM', 'UCEC', 'UCS', 'UVM'
    ]

    study = widgets.Dropdown(options=studyList,
                             value='UCEC',
                             description='',
                             disabled=False)

    FeatureList1 = [
        'Gene Expression', 'Somatic Copy Number', 'MicroRNA Expression',
        'Clinical Numeric'
    ]

    #FeatureList2 = [ 'Gene Expression', 'Somatic Mutation Spearman','Somatic Mutation t-test', 'Somatic Copy Number', 'Clinical Numeric', 'Clinical Categorical', 'MicroRNA Expression'] ;
    FeatureList2 = [
        'Gene Expression', 'Somatic Mutation', 'Somatic Copy Number',
        'Clinical Numeric', 'MicroRNA Expression'
    ]

    feature1 = widgets.Dropdown(options=FeatureList1,
                                value='Gene Expression',
                                description='',
                                disabled=False)

    gene_names = widgets.Text(value='IGF2, ADAM6',
                              placeholder='Type gene names  ',
                              description='',
                              disabled=False)

    feature2 = widgets.Dropdown(options=FeatureList2,
                                value='Gene Expression',
                                description='',
                                disabled=False)

    significance = widgets.SelectionSlider(
        options=['0.05', '0.01', '0.005', '0.001'],
        value='0.01',
        description='',
        disabled=False,
        continuous_update=False,
        orientation='horizontal',
        readout=True)

    size = widgets.IntSlider(value=25, min=5, max=50,
                             description='')  # the n most variable genes

    cohortlist = widgets.FileUpload(
        accept=
        '',  # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'
        multiple=False  # True to accept multiple files upload else False
    )

    feature1_title = widgets.HTML('<em>Select Feature1 </em>')
    display(widgets.HBox([feature1_title, feature1]))

    genes_title = widgets.HTML('<em>Feature1 labels </em>')
    display(widgets.HBox([genes_title, gene_names]))

    feature2_title = widgets.HTML('<em>Select Feature2 </em>')
    display(widgets.HBox([feature2_title, feature2]))

    study_title = widgets.HTML('<em>Select a study </em>')
    display(widgets.HBox([study_title, study]))

    significance_title = widgets.HTML('<em>Significance level </em>')
    display(widgets.HBox([significance_title, significance]))

    size_title = widgets.HTML('<em>Minimum number of samples</em>')
    display(widgets.HBox([size_title, size]))

    cohort_title = widgets.HTML('<em>Cohort list</em>')
    display(widgets.HBox([cohort_title, cohortlist]))

    return ([
        study, feature1, feature2, gene_names, size, cohortlist, significance
    ])
Ejemplo n.º 3
0
    def __init__(self, datasource):
        self._datasource = datasource

        self._info = None

        if isinstance(self._datasource, connections.EventCollection):
            self._info = self._datasource.getInfo()

        components = []

        self._filter = widgets.Text(description="Filter",
                                    layout=widgets.Layout(width="50%"))
        b = widgets.Button(description="Set",
                           layout=widgets.Layout(width="50px"))
        b.on_click(self.filter)

        self._buttons = None

        if isinstance(self._datasource, connections.EventCollection):
            iconLayout = widgets.Layout(width="40px")
            self._nextButton = widgets.Button(icon="fa-step-forward",
                                              layout=iconLayout)
            self._prevButton = widgets.Button(icon="fa-step-backward",
                                              layout=iconLayout)
            self._firstButton = widgets.Button(icon="fa-backward",
                                               layout=iconLayout)
            self._lastButton = widgets.Button(icon="fa-forward",
                                              layout=iconLayout)
            self._playPauseButton = widgets.Button(
                icon="fa-pause",
                layout=widgets.Layout(width="40px", margin="0px 0px 0px 20px"))

            if self._datasource._paused:
                self._playPauseButton.icon = "fa-play"

            self._nextButton.on_click(self.next)
            self._prevButton.on_click(self.prev)
            self._firstButton.on_click(self.first)
            self._lastButton.on_click(self.last)
            self._playPauseButton.on_click(self.playPause)

            #self._buttons = widgets.HBox([self._nextButton,self._prevButton,self._firstButton,self._lastButton],layout=widgets.Layout(width="200px"))
            self._buttons = widgets.HBox([
                self._nextButton, self._prevButton, self._firstButton,
                self._lastButton, self._playPauseButton
            ],
                                         layout=widgets.Layout(width="300px"))

            if self._datasource._pages == 1:
                self._buttons.layout.display = "none"
            else:
                self._buttons.layout.display = "block"

            components.append(self._buttons)

        w = "80%"
        justify = "flex-end"
        if self._info == None or self._datasource._pages == 1:
            w = "100%"
            justify = "center"
        self._filterBox = widgets.HBox([self._filter, b],
                                       layout=widgets.Layout(
                                           width=w, justify_content=justify))
        components.append(self._filterBox)

        #self._panel = widgets.HBox(components,layout=widgets.Layout(width="100%",border="1px solid #d8d8d8"))
        self._panel = widgets.HBox(components,
                                   layout=widgets.Layout(width="100%"))

        self._filter.value = self._datasource.getFilter()
Ejemplo n.º 4
0
    def __init__(self):
        # dropdown menu for setting asset
        self.asset = ipywidgets.Dropdown(
            options=['atlas-local', 'atlas-s3', 'nsidc-s3'],
            value='atlas-s3',
            description='Asset:',
            disabled=False,
        )

        # dropdown menu for setting data release
        self.release = ipywidgets.Dropdown(
            options=['003', '004'],
            value='004',
            description='Release:',
            disabled=False,
        )

        # dropdown menu for setting surface type
        # 0-land, 1-ocean, 2-sea ice, 3-land ice, 4-inland water
        surface_type_options = [
            'Land', 'Ocean', 'Sea ice', 'Land ice', 'Inland water'
        ]
        self.surface_type = ipywidgets.Dropdown(
            options=surface_type_options,
            value='Land',
            description='Surface Type:',
            disabled=False,
        )

        # slider for setting length of ATL06-SR segment in meters
        self.length = ipywidgets.IntSlider(value=40,
                                           min=5,
                                           max=200,
                                           step=5,
                                           description='Length:',
                                           disabled=False,
                                           continuous_update=False,
                                           orientation='horizontal',
                                           readout=True,
                                           readout_format='d')

        # slider for setting step distance for successive segments in meters
        self.step = ipywidgets.IntSlider(value=20,
                                         min=5,
                                         max=200,
                                         step=5,
                                         description='Step:',
                                         disabled=False,
                                         continuous_update=False,
                                         orientation='horizontal',
                                         readout=True,
                                         readout_format='d')

        # slider for setting confidence level for PE selection
        # eventually would be good to switch this to a IntRangeSlider with value=[0,4]
        self.confidence = ipywidgets.IntSlider(value=4,
                                               min=0,
                                               max=4,
                                               step=1,
                                               description='Confidence:',
                                               disabled=False,
                                               continuous_update=False,
                                               orientation='horizontal',
                                               readout=True,
                                               readout_format='d')

        # selection for land surface classifications
        land_options = [
            'atl08_noise', 'atl08_ground', 'atl08_canopy',
            'atl08_top_of_canopy', 'atl08_unclassified'
        ]
        self.land_class = ipywidgets.SelectMultiple(options=land_options,
                                                    description='Land Class:',
                                                    disabled=False)

        # slider for setting maximum number of iterations
        # (not including initial least-squares-fit selection)
        self.iteration = ipywidgets.IntSlider(value=1,
                                              min=0,
                                              max=20,
                                              step=1,
                                              description='Iterations:',
                                              disabled=False,
                                              continuous_update=False,
                                              orientation='horizontal',
                                              readout=True,
                                              readout_format='d')

        # slider for setting minimum along track spread
        self.spread = ipywidgets.FloatSlider(value=20,
                                             min=1,
                                             max=100,
                                             step=0.1,
                                             description='Spread:',
                                             disabled=False,
                                             continuous_update=False,
                                             orientation='horizontal',
                                             readout=True,
                                             readout_format='0.1f')
        # slider for setting minimum photon event (PE) count
        self.count = ipywidgets.IntSlider(value=10,
                                          min=1,
                                          max=50,
                                          step=1,
                                          description='PE Count:',
                                          disabled=False,
                                          continuous_update=False,
                                          orientation='horizontal',
                                          readout=True,
                                          readout_format='d')

        # slider for setting minimum height of PE window in meters
        self.window = ipywidgets.FloatSlider(value=3,
                                             min=0.5,
                                             max=10,
                                             step=0.1,
                                             description='Window:',
                                             disabled=False,
                                             continuous_update=False,
                                             orientation='horizontal',
                                             readout=True,
                                             readout_format='0.1f')

        # slider for setting maximum robust dispersion in meters
        self.sigma = ipywidgets.FloatSlider(value=5,
                                            min=1,
                                            max=10,
                                            step=0.1,
                                            description='Sigma:',
                                            disabled=False,
                                            continuous_update=False,
                                            orientation='horizontal',
                                            readout=True,
                                            readout_format='0.1f')

        # dropdown menu for setting map projection for polygons
        # Global: Web Mercator (EPSG:3857)
        # North: Alaska Polar Stereographic (EPSG:5936)
        # South: Polar Stereographic South (EPSG:3031)
        projection_list = ['Global', 'North', 'South']
        self.projection = ipywidgets.Dropdown(
            options=projection_list,
            value='Global',
            description='Projection:',
            disabled=False,
        )

        # button and label for output file selection
        self.file = copy.copy(self.filename)
        self.savebutton = ipywidgets.Button(description="Save As")
        self.savelabel = ipywidgets.Text(value=self.file, disabled=False)
        # connect fileselect button with action
        self.savebutton.on_click(self.saveas_file)
        self.savelabel.observe(self.set_savefile)
        # create hbox of file selection
        self.filesaver = ipywidgets.HBox([self.savebutton, self.savelabel])

        # button and label for input file selection
        self.loadbutton = ipywidgets.Button(description="File select")
        self.loadlabel = ipywidgets.Text(value='', disabled=False)
        # connect fileselect button with action
        self.loadbutton.on_click(self.select_file)
        self.loadlabel.observe(self.set_loadfile)
        # create hbox of file selection
        self.fileloader = ipywidgets.HBox([self.loadbutton, self.loadlabel])
Ejemplo n.º 5
0
                          disabled=False)
w_exportname = widgets.Text(value='users/<username>/<path>',
                            placeholder=' ',
                            disabled=False)
w_location = widgets.Text(value='Jülich',
                          placeholder=' ',
                          description='',
                          disabled=False)

w_goto = widgets.Button(description="GoTo", disabled=False)
w_collect = widgets.Button(description="Collect", disabled=True)
w_preview = widgets.Button(description="Preview", disabled=True)
w_export = widgets.Button(description='Export to assets', disabled=True)
w_dates1 = widgets.VBox([w_startdate1, w_enddate1, w_iterations])
w_dates2 = widgets.VBox([w_startdate2, w_enddate2, w_scale])
w_dates = widgets.HBox([w_platform, w_dates1, w_dates2])
w_exp = widgets.HBox([w_export, w_exportname])
w_go = widgets.HBox([w_collect, w_preview, w_exp])
w_txt = widgets.HBox([w_text, w_goto, w_location])
box = widgets.VBox([w_txt, w_dates, w_go])


def on_widget_change(b):
    w_preview.disabled = True
    w_export.disabled = True


w_platform.observe(on_widget_change, names='value')
w_startdate1.observe(on_widget_change, names='value')
w_enddate1.observe(on_widget_change, names='value')
w_startdate2.observe(on_widget_change, names='value')
Ejemplo n.º 6
0
    def __init__(self):
        with open('popular_virus_names.json', 'r') as f:
            popular_viruses_keys = json.load(f)

        self._virus_dropdown = self._create_indicator_dropdown(
            popular_viruses_keys, 0)
        self._plot_container = widgets.Output()
        _app_container = widgets.VBox([
            widgets.HBox([self._virus_dropdown]),
            self._plot_container,
        ],
                                      layout=widgets.Layout(
                                          align_items='center',
                                          flex='3 0 auto'))
        self.container = widgets.VBox([
            widgets.HTML(('<h1>Development indicators</h1>'
                          '<h1> </h1>'
                          '<h3>Select virus of interest</h3>'),
                         layout=widgets.Layout(margin='0 0 2em 0')),
            widgets.HBox([
                _app_container,
            ])
        ],
                                      layout=widgets.Layout(
                                          flex='1 1 auto',
                                          margin='0 auto 0 auto',
                                          max_width='1024px'))

        # Loading and preprocessing data.
        n_viruses = 5
        n_entries = 10
        # n_viruses = 87
        # n_entries = 100

        viruses_full = load_viruses()
        patients_identity_full, patients_entries_full = load_patients()
        self.viruses = {
            k: viruses_full[k]
            for k in popular_viruses_keys[:n_viruses]
        }
        random_idx = np.arange(patients_entries_full.shape[0])
        np.random.shuffle(random_idx)
        random_idx = random_idx[:n_entries]
        patients_entries = patients_entries_full[random_idx]
        self.patients_identity = patients_identity_full[random_idx]
        aligner = Align.PairwiseAligner()
        aligner.open_gap_score = -1
        aligner.extend_gap_score = -1
        aligner.target_end_gap_score = 0.0
        aligner.query_end_gap_score = 0.0

        def comp_al_score(patients_vdna):
            patients_entries, virus_dna = patients_vdna
            return compute_virus_alignment_score(patients_entries, virus_dna,
                                                 aligner)

        tuples_to_process = [(patients_entries, v_dna)
                             for v_dna in self.viruses.values()]
        # with Pool(8) as p:
        #     alignements_scors_list = p.map(
        #         comp_al_score,
        #         tuples_to_process
        #     )
        alignements_scors_list = list(
            map(lambda x: comp_al_score(x), tuples_to_process))
        self.result = np.array(alignements_scors_list).T
        patients_results = dict()
        for patient_id, entry in zip(self.patients_identity, self.result):
            if patient_id not in patients_results:
                patients_results[patient_id] = []
            else:
                patients_results[patient_id] += [entry]
        for k in patients_results.keys():
            patients_results[k] = np.array(patients_results[k])
        patients_means_per_virus = dict()
        for k in patients_results.keys():
            patients_means_per_virus[k] = np.median(patients_results[k],
                                                    axis=0)
Ejemplo n.º 7
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
Ejemplo n.º 8
0
    value='id_text',
    description='Sort By: ')
out = widgets.Output()


def submit_search(change):
    # "linking function with output"
    with out:
        # what happens when we press the button
        clear_output()
        display(search(text.value))


# when maxhits is set larger than 250, default becomes no links
def update_maxhits(change):
    links.value = maxhits.value < 250
    submit_search(change)


# linking button and function together using a button's method
button.on_click(submit_search)
text.on_submit(submit_search)
sortby.observe(submit_search, 'value')
maxhits.observe(update_maxhits, 'value')
#links.observe(submit_search, 'value')
# displaying button and its output together
col1 = widgets.VBox([text, links, button])
col2 = widgets.VBox([maxhits, sortby])
box = widgets.HBox([col1, col2])
disp = widgets.VBox([box, out])
Ejemplo n.º 9
0
def cloud_hunt(ctx=None):
    """
    Displays a UI to build LQL queries to do threat hunting.
    """
    global lw_ctx  # To be able to pass the grid to widget functions.
    tables = utils.load_yaml_file("tables.yaml")
    table_filters = utils.load_yaml_file("filters.yaml")

    options = [DEFAULT_TABLE_PICK]
    options.extend([x.get("display_name") for x in tables])
    options.append(DEFAULT_CUSTOM_PICK)
    table_list = ipywidgets.Dropdown(options=options,
                                     value=DEFAULT_TABLE_PICK,
                                     description="",
                                     disabled=False)
    table_list.observe(_generate_table_filters)

    table_box = ipywidgets.HBox()
    table_box.children = (ipywidgets.Label("Pick a table: "), table_list)

    verify_button = ipywidgets.Button(
        value=False,
        description="Verify Query",
        disabled=False,
        tooltip="Build the LQL query and verify it",
        icon="check")
    verify_button.on_click(_verify_button)

    execute_button = ipywidgets.Button(
        value=False,
        description="Execute Query",
        button_style="success",
        disabled=False,
        tooltip="Build the LQL query and execute it")
    execute_button.on_click(_execute_button)

    box_layout = ipywidgets.Layout(display="flex",
                                   flex_flow="column",
                                   align_items="stretch",
                                   width="1000px")

    title = ipywidgets.HTML(
        value=("<div align=\"center\"><h1>Cloud Hunting</h1><i>Assistant to "
               "build LQL queries to perform threat hunting within your "
               "environment.</i><br/><br/></div>"))

    start_time, end_time = utils.get_start_and_end_time(ctx)
    start_widget = ipywidgets.Text(value=start_time,
                                   placeholder="YYYY-MM-DDTHH:MM:SSZ",
                                   description="",
                                   disabled=False)
    end_widget = ipywidgets.Text(value=end_time,
                                 placeholder="YYYY-MM-DDTHH:MM:SSZ",
                                 description="",
                                 disabled=False)

    filter_box = ipywidgets.VBox()
    result_label = ipywidgets.HTML()

    grid = ipywidgets.Box(children=[
        title,
        table_box,
        ipywidgets.HBox(children=[
            ipywidgets.Label("Start Time:"), start_widget,
            ipywidgets.Label("End Time:"), end_widget
        ]),
        filter_box,
        ipywidgets.HBox(children=(verify_button, execute_button)),
        result_label,
    ],
                          layout=box_layout)

    ctx.add_state("query_builder", "query_table_box", table_box)
    ctx.add_state("query_builder", "query_start_widget", start_widget)
    ctx.add_state("query_builder", "query_end_widget", end_widget)
    ctx.add_state("query_builder", "query_filter_box", filter_box)
    ctx.add_state("query_builder", "query_label", result_label)

    ctx.add_state("query_builder", "query_tables", tables)
    ctx.add_state("query_builder", "query_filters", table_filters)
    lw_ctx = ctx
    query_builder.lw_ctx = ctx

    display(grid)  # noqa: F821
Ejemplo n.º 10
0
    def __init__(self, n_channels=2, *args, **kwargs):
        """Parameters
        ----------
        n_channels
            Number of channels present on the images
        *args, **kwargs
            Passed to superclass constructor
        """
        fig, ax = plt.subplots()
        self.image_display = ImageDisplay(ax)

        # Create boxes for entering ROI dimensions
        dt_children = []
        self._mpl_rois = []
        for i in range(n_channels):
            color = self.colors[i]
            alpha = 0.3

            c = _ExtentsInput(color, alpha)
            c.observe(self._roi_extents_changed, "extents")
            dt_children.append(c)

            r = mpl.widgets.RectangleSelector(self.image_display.ax,
                                              self._roi_drawn,
                                              interactive=True,
                                              rectprops={
                                                  "facecolor": color,
                                                  "alpha": alpha
                                              },
                                              useblit=True)
            r.active = False
            self._mpl_rois.append(r)
        self._dimension_tabs = ipywidgets.Tab(dt_children)
        self._dimension_tabs.observe(self._active_channel_changed,
                                     "selected_index")

        # Checkbox to force same size on all channels
        self._same_size_box = ipywidgets.Checkbox(description="Same size",
                                                  value=True)
        traitlets.link((self, "same_size"), (self._same_size_box, "value"))

        # Buttons for splitting horizontally and vertically
        self._split_hor_button = ipywidgets.Button(
            description="split horizontally")
        self._split_hor_button.on_click(self._split_hor)
        self._split_ver_button = ipywidgets.Button(
            description="split vertically")
        self._split_ver_button.on_click(self._split_ver)
        # Button for swapping channels (i.e., reversing their order)
        self._swap_button = ipywidgets.Button(description="swap channels")
        self._swap_button.on_click(self._swap_chan)

        super().__init__([
            self._dimension_tabs,
            ipywidgets.HBox([
                self._split_hor_button, self._split_ver_button,
                self._swap_button, self._same_size_box
            ]), self.image_display
        ])

        self._n_channels = n_channels
        self.channel_names = [
            "channel {}".format(i) for i in range(n_channels)
        ]

        self._update_extents_lock = threading.Lock()
        self._update_rois_lock = threading.Lock()

        self._active_channel_changed()
Ejemplo n.º 11
0
def label_images(path_to_images, path_to_annotations):
    '''
    Provides user-interactive environment for labeling smiles and open mouth on images.
    Images have to be pre-processed with process_folder() function.
    
    Parameters
    ----------
    path_to_images : str
        Path to folder with images to label.
    path_to_annotations : str
        Path to annotaions csv file.
    '''

    # Create ImageList() object
    imgs = ImageList(path_to_images, path_to_annotations)
    # Get initial state for buttons
    labels = imgs.get_labels()

    # Create image and buttons widgets
    image_frame = widgets.Image(value=imgs.get_image(), width=400, height=400)
    title = widgets.Label(value=imgs.filename())
    button_next = widgets.Button(description='',
                                 tooltip='Next image',
                                 icon='forward')
    button_prev = widgets.Button(description='',
                                 tooltip='Previous image',
                                 icon='backward')
    button_save = widgets.Button(description='',
                                 tooltip='Save changes',
                                 icon='save')
    button_del = widgets.Button(description='',
                                tooltip='Delete image',
                                icon='trash')
    button_smile = widgets.ToggleButton(value=bool(labels['smile']),
                                        description='Smile',
                                        tooltip='Label smile',
                                        icon='smile-o')
    button_mouth_open = widgets.ToggleButton(value=bool(labels['mouth_open']),
                                             description='Mouth open',
                                             tooltip='Label mouth open',
                                             icon='circle-o')

    # Distribute buttons in table
    column_1 = widgets.VBox([button_prev, widgets.Label()])
    column_2 = widgets.VBox([button_smile, button_save])
    column_3 = widgets.VBox([button_mouth_open, button_del])
    column_4 = widgets.VBox([button_next, widgets.Label()])
    buttons = widgets.HBox([column_1, column_2, column_3, column_4])

    def change_image(action):
        # Save current labels
        imgs.set_labels((button_smile.value, button_mouth_open.value))
        # Change current image
        image_frame.value = action()
        # Read labels
        labels = imgs.get_labels()
        button_smile.value, button_mouth_open.value = bool(
            labels['smile']), bool(labels['mouth_open'])
        # Add filename
        title.value = imgs.filename()

    # Configure on_click events
    button_next.on_click(lambda x: change_image(imgs.forward))
    button_prev.on_click(lambda x: change_image(imgs.backward))
    button_del.on_click(lambda x: change_image(imgs.delete))
    button_save.on_click(lambda x: imgs.set_labels(
        (button_smile.value, button_mouth_open.value)))

    # Display widgets
    display(buttons)
    display(image_frame)
    display(title)
    def _run(self, workers, progress):
        self.TStart = time.time()

        pool = None
        if workers > 0:
            pool = multiprocessing.Pool(processes=workers)

        pbar = None
        if progress:
            from IPython.display import display
            import ipywidgets

            lbl = ipywidgets.Label(self._dataset,
                                   layout=ipywidgets.Layout(width="70%"))
            info = ipywidgets.Label("- kevt/s",
                                    layout=ipywidgets.Layout(
                                        width="10%",
                                        display='flex',
                                        justify_content='flex-end'))
            pbar = ipywidgets.IntProgress(
                min=0,
                max=len(self._filelist),
                layout=ipywidgets.Layout(width="17%"))
            cancel = ipywidgets.Button(tooltip='Abort processing',
                                       icon='times',
                                       button_style='danger',
                                       layout=ipywidgets.Layout(width="3%"))

            def abort(b):
                if pool is not None:
                    pool.terminate()
                b.disabled = True

            cancel.on_click(abort)
            bar = ipywidgets.HBox([lbl, info, pbar, cancel])
            display(bar)

        try:
            jobs = (JobDummy(fname, self._treename, self._user_params,
                             self._eventsdef, self._worker)
                    for fname in self._filelist)
            if pool is not None:
                res = set(pool.apply_async(job) for job in jobs)
                while True:
                    finished = set()
                    for r in res:
                        if r.ready():
                            if not r.successful():
                                raise r.get()
                            out = r.get()
                            with self._user_callback.lock:
                                self._user_callback.on_streams_update(*out)
                            self.EventsProcessed += out[0]
                            self._filesProcessed += 1
                            finished.add(r)
                    res -= finished
                    finished = None
                    if progress:
                        pbar.value = self._filesProcessed
                        info.value = "{:>5.0f} kevt/s".format(
                            self.EventsProcessed /
                            (time.time() - self.TStart) / 1000)
                        if cancel.disabled:
                            break
                    if len(res) == 0:
                        if progress:
                            cancel.disabled = True
                        break
                    time.sleep(0.2)
            else:
                for job in jobs:
                    out = job()
                    with self._user_callback.lock:
                        self._user_callback.on_streams_update(*out)
                    self.EventsProcessed += out[0]
                    self._filesProcessed += 1
                    if progress:
                        pbar.value = self._filesProcessed
                        info.value = "{:>5.0f} kevt/s".format(
                            self.EventsProcessed /
                            (time.time() - self.TStart) / 1000)
                        if cancel.disabled:
                            break
                if progress:
                    cancel.disabled = True
        except Exception:
            if pool is not None:
                pool.terminate()
            if progress:
                cancel.disabled = True
                info.value = "Exception"
            raise

        pool = None
        self.TFinish = time.time()
Ejemplo n.º 13
0
    def __init__(self,
                 base_project="physionet-data.",
                 db="mimic_core",
                 exclude="iii",
                 query_size=1000,
                 *args,
                 **kwargs):
        self.base_project = base_project
        self.current_db = None
        self.df = None
        self.qs = query_size
        self.credentials, self.pid = get_gcreds()
        self.dbs = ["mimic_core", "mimic_derived", "mimic_hosp", "mimic_icu"]
        self.conn = get_client(self.credentials, self.pid)
        if not os.path.exists(".mimic_info.yaml"):
            print("This will take a bit of time...")

            self.dbs.sort()
            self.info = {}
            for d in self.dbs:
                print("processing database %s" % d)
                db = self.conn.database(self.base_project + d)
                tables = db.list_tables()
                tmp = {t: int(db.table(t).count().execute()) for t in tables}
                self.info[d] = tmp
            clear_output()
            print("Completed")
            with open(".mimic_info.yaml", "w") as f0:
                yaml.dump(self.info, f0)
        else:
            with open(".mimic_info.yaml") as f0:
                self.info = yaml.safe_load(f0)

        self.sdbs = ipw.Dropdown(options=[None] + self.dbs[:],
                                 value=None,
                                 description="Select DB")
        self.sdbs.observe(self.set_db, "value")

        self.stable = ipw.Dropdown(description="Select Table")
        self.stable.observe(self.set_table, "value")
        if self.qs:
            step = self.qs
        else:
            step = 1
        self.offset = ipw.IntSlider(description="offset",
                                    step=self.qs,
                                    continuous_update=False)
        self.offset.observe(self.update_offset, "value")

        self.out = ipw.Output()
        children = kwargs.get("children", [])

        self.graph_type = ipw.Dropdown(
            options=[None, "describe", "categorical", "numeric"],
            value=None,
            description="Viz Type")
        self.kind = ipw.Dropdown(options=[
            "count", "swarm", "box", "boxen", "violin", "bar", "point"
        ],
                                 value="count")
        opts = [None]
        self.xsel = ipw.Dropdown(options=opts, value=None, description="x")
        self.ysel = ipw.Dropdown(options=opts, value=None, description="y")
        self.hsel = ipw.Dropdown(options=opts, value=None, description="hue")
        self.rsel = ipw.Dropdown(options=opts,
                                 value=None,
                                 description="row var")
        self.csel = ipw.Dropdown(options=opts,
                                 value=None,
                                 description="col var")

        self.graph_type.observe(self.disp_plot, "value")
        self.kind.observe(self.disp_plot, "value")
        self.xsel.observe(self.disp_plot, "value")
        self.ysel.observe(self.disp_plot, "value")
        self.hsel.observe(self.disp_plot, "value")
        self.rsel.observe(self.disp_plot, "value")
        self.csel.observe(self.disp_plot, "value")

        self.plot_out = ipw.Output()

        tmp = ipw.HBox([
            self.graph_type, self.kind,
            ipw.VBox([self.xsel, self.ysel]),
            ipw.VBox([self.hsel, self.rsel, self.csel])
        ])

        children = [
            ipw.HBox([self.sdbs, self.stable, self.offset]), self.out, tmp,
            self.plot_out
        ] + children

        super(BQBrowser, self).__init__(children=children)
        self.disp_df()
        self.disp_plot()
Ejemplo n.º 14
0
def make_backend_widget(backend_item: BackendWithProviders) -> wid.HBox:
    """ Construct a backend widget for a given device.

    Args:
        backend_item: A ``BackendWithProviders`` instance containing the
            backend instance and a list of providers from which the backend can be accessed.

    Returns:
        The widget with backend information.
    """
    backend = backend_item.backend
    backend_providers = backend_item.providers

    status = backend.status()
    config = backend.configuration()
    props = backend.properties().to_dict()
    next_resrv = get_next_reservation(backend)

    name_str = "<font size='5' face='monospace'>%s</font>"
    backend_name = wid.HTML(value=name_str % backend.name())

    qubits_wid = wid.HTML(value=STAT_FONT_TITLE.format("Qubits:"))
    qubits_val_wid = wid.HTML(value=STAT_FONT_VALUE.format(config.n_qubits))

    status_wid = wid.HTML(value=STAT_FONT_TITLE.format("Status:"))

    color = '#000000'
    status_msg = status.status_msg
    if status_msg == 'active':
        color = '#34BC6E'
        if next_resrv:
            status_msg += ' [R]'
    if status_msg in ['maintenance', 'internal']:
        color = '#FFB000'

    status_val_wid = wid.HTML(
        value=STAT_FONT_VALUE_COLOR.format(color=color, msg=status_msg))

    left_labels = wid.VBox(children=[qubits_wid, status_wid])
    left_values = wid.VBox(children=[qubits_val_wid, status_val_wid],
                           layout=wid.Layout(margin="1px 0px 0px 0px"))

    left_stats = wid.HBox(children=[left_labels, left_values],
                          layout=wid.Layout(width='175px'))

    version_wid = wid.HTML(value=STAT_FONT_TITLE.format("Version:"))
    version_val_wid = wid.HTML(value=STAT_FONT_VALUE.format(
        config.backend_version),
                               layout=wid.Layout(margin="3px 0px 0px 0px"))

    queue_wid = wid.HTML(value=STAT_FONT_TITLE.format("Queue:"))
    queue_val_wid = wid.HTML(value=STAT_FONT_VALUE.format(status.pending_jobs),
                             layout=wid.Layout(margin="5px 0px 0px 0px"))

    right_labels = wid.VBox(children=[version_wid, queue_wid])
    right_values = wid.VBox(children=[version_val_wid, queue_val_wid])

    right_stats = wid.HBox(children=[right_labels, right_values],
                           layout=wid.Layout(width='auto',
                                             margin="0px 0px 0px 20px"))

    stats = wid.HBox(children=[left_stats, right_stats])

    # Backend reservation.
    reservation_title = wid.HTML(value=STAT_FONT_TITLE.format("Reservation:"))
    if next_resrv:
        start_dt_str = duration_difference(next_resrv.start_datetime)
        reservation_val = RESERVATION_STR.format(start_dt=start_dt_str,
                                                 duration=next_resrv.duration)
    else:
        reservation_val = RESERVATION_NONE
    reservation_val_wid = wid.HTML(value=reservation_val)
    reservation_wid = wid.HBox(
        children=[reservation_title, reservation_val_wid])

    providers_label = wid.HTML(value=STAT_FONT_TITLE.format("Providers:"))

    providers_list = provider_buttons(backend_providers)

    device_stats = wid.VBox(children=[
        backend_name, stats, reservation_wid, providers_label, providers_list
    ],
                            layout=wid.Layout(width='auto',
                                              margin="0px 20px 0px 0px"))

    n_qubits = config.n_qubits

    qubit_size = 15
    line_width = 3
    if n_qubits < 10:
        qubit_size = 18
        line_width = 4
    if n_qubits >= 27:
        qubit_size = 12
        line_width = 3
    if n_qubits > 50:
        qubit_size = 12
        line_width = 3

    _gmap = iplot_gate_map(backend,
                           figsize=(150, 150),
                           qubit_size=qubit_size,
                           line_width=line_width,
                           qubit_color='#031981',
                           line_color='#031981',
                           label_qubits=False,
                           as_widget=True)

    gmap = wid.Box(children=[_gmap],
                   layout=wid.Layout(
                       width='auto',
                       justify_content='center',
                       align_content='center',
                   ))

    # Get basic device stats
    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)

    if n_qubits != 1:
        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
        if num_cx:
            avg_cx_err = round(sum_cx_err / (num_cx) * 100, 2)
        else:
            avg_cx_err = 100.0

    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 * 100, 2)

    t12_label = wid.HTML(
        value="<font size='2'>Avg. T<sub>1</sub> / T<sub>2</sub></font>:")
    t12_str = "<font size='3' face='monospace'>{t1}/{t2}</font><font size='2'> {units}</font>"
    t12_wid = wid.HTML(
        value=t12_str.format(t1=avg_t1, t2=avg_t2, units=t1_units))

    meas_label = wid.HTML(value="<font size='2'>Avg. Readout Err.:</font>")
    meas_str = "<font size='3' face='monospace'>{merr}</font><font size='2'> %</font>"
    meas_wid = wid.HTML(value=meas_str.format(merr=avg_meas_err))

    cx_str = "<font size='3' face='monospace'>{cx_err}</font><font size='2'> %</font>"
    if n_qubits != 1:
        cx_label = wid.HTML(value="<font size='2'>Avg. CX Err.:</font>")
        cx_wid = wid.HTML(value=cx_str.format(cx_err=avg_cx_err))

    quant_vol = 'None'
    try:
        quant_vol = config.quantum_volume
    except AttributeError:
        pass
    qv_label = wid.HTML(value="<font size='2'>Quantum Volume:</font>")
    qv_str = "<font size='3' face='monospace'>{qv}</font><font size='2'></font>"
    qv_wid = wid.HTML(value=qv_str.format(qv=quant_vol),
                      layout=wid.Layout(margin="-1px 0px 0px 0px"))

    if n_qubits != 1:
        left_wids = [t12_label, cx_label, meas_label, qv_label]
        right_wids = [t12_wid, cx_wid, meas_wid, qv_wid]

    else:
        left_wids = [t12_label, meas_label, qv_label]
        right_wids = [t12_wid, meas_wid, qv_wid]

    left_wid = wid.VBox(children=left_wids,
                        layout=wid.Layout(margin="2px 0px 0px 0px"))
    right_wid = wid.VBox(children=right_wids,
                         layout=wid.Layout(margin="0px 0px 0px 3px"))

    stats = wid.HBox(children=[left_wid, right_wid])

    gate_map = wid.VBox(children=[gmap, stats],
                        layout=wid.Layout(width='230px',
                                          justify_content='center',
                                          align_content='center',
                                          margin="-25px 0px 0px 0px"))

    out = wid.HBox(children=[device_stats, gate_map],
                   layout=wid.Layout(width='auto',
                                     height='auto',
                                     padding="5px 5px 5px 5px",
                                     justify_content='space-between',
                                     max_width='700px',
                                     border='1px solid #212121'))

    # Attach information to the backend panel for later updates.
    out._backend = backend  # pylint: disable=protected-access
    out._status_val_wid = status_val_wid
    out._queue_val_wid = queue_val_wid
    out._reservation_val_wid = reservation_val_wid
    return out
Ejemplo n.º 15
0


defaultloc = 'chr5:142,903,034-142,906,867'
defaultgeneid = 'Actb'
defaultrefseqid = 'NM_007393'
defaultstrand = "-"

#### Accordian Change File Locations ####
w_default_folder = widgets.Text(
    description='RnaSeq Folder',
    value=default_track_folder,
)
w_default_folder.width="80%"
w_default_folder_valid = widgets.Valid(value=True)
hboxdefaultfolder = widgets.HBox([w_default_folder,w_default_folder_valid])
page1 = widgets.VBox(children=[hboxdefaultfolder])

accord = widgets.Accordion(children=[page1], width="80%")
display(accord)

accord.set_title(1, 'Defaults')


#### Define Widgets ###
lookuptype_widget = widgets.RadioButtons(
    options={'By location':'location','By geneid (3\'UTR only)':"geneid"},
    value='location',
    description='Lookup:',
    disabled=False
)
Ejemplo n.º 16
0
    def widget(self):
        # Alright let's set this all up.
        grid_contents = []
        for i, view in enumerate(self.grid_views):
            visible = ipywidgets.Checkbox(value=view.visible,
                                          description=f"Level {i}")
            ipywidgets.jslink((visible, "value"), (view, "visible"))
            color_picker = ipywidgets.ColorPicker(value=view.material.color,
                                                  concise=True)
            ipywidgets.jslink((color_picker, "value"),
                              (view.material, "color"))
            line_slider = ipywidgets.FloatSlider(value=view.material.linewidth,
                                                 min=0.0,
                                                 max=10.0)
            ipywidgets.jslink((line_slider, "value"),
                              (view.material, "linewidth"))
            grid_contents.extend([visible, color_picker, line_slider])

        dropdown = ipywidgets.Dropdown(
            options=["inferno", "viridis", "plasma", "magma", "cividis"],
            value="inferno",
            description="Colormap:",
            disable=False,
        )

        traitlets.link((dropdown, "value"), (self, "grid_colormap"))

        button = ipywidgets.Button(description="Add Keyframe")

        def on_button_clicked(b):
            self.position_list = self.position_list + [
                self.renderer.camera.position
            ]
            select.options += ((f"Position {len(self.position_list)}",
                                self.renderer.camera.position), )

        button.on_click(on_button_clicked)

        select = ipywidgets.Select(options=[],
                                   description="Positions:",
                                   disabled=False)

        def on_selection_changed(change):
            self.renderer.camera.position = tuple(change["new"])

        select.observe(on_selection_changed, ["value"])

        return ipywidgets.HBox([
            self.renderer,
            button,
            select,
            ipywidgets.VBox([
                dropdown,
                ipywidgets.GridBox(
                    grid_contents,
                    layout=ipywidgets.Layout(
                        width=r"60%",
                        grid_template_columns=r"30% 10% auto",
                        align_items="stretch",
                    ),
                ),
            ]),
        ])
Ejemplo n.º 17
0
    def __init__(self, df):
        """
        + initialize class with dataframe
        + initiatlive all ipywidgets
        """
        self.df = df
        self.split_param = ' - * - '

        self.creative_types = ('traffic driver', 'interactive non video',
                               'branded driver', 'video', 'interactive video',
                               'no match')

        self.df['advert_order'] = self.df[
            'Advertiser'] + self.split_param + self.df['Order']

        self.AO_dropdown = ipywidgets.Dropdown(
            options=sorted(set(self.df['advert_order'])),
            value=list(sorted(set(self.df['advert_order'])))[0],
            disabled=False)

        self.site_dropdown = ipywidgets.Dropdown(options=['qz', 'wrk', 'zty'],
                                                 value='qz',
                                                 disabled=False)

        self.placement_dropdown = ipywidgets.Dropdown(options=[
            'engage mobile', 'engage desktop', 'marquee mobile',
            'marquee desktop', 'inline mobile', 'inline desktop'
        ],
                                                      value='engage mobile',
                                                      disabled=False)

        self.creative_type_dropdown = ipywidgets.Dropdown(
            options=self.creative_types,
            value=self.creative_types[0],
            disabled=False)

        self.metric_measurement = ipywidgets.Dropdown(
            options=['DFP CTR', '3P CTR', 'Viewability', 'VSR', 'IR'],
            value='DFP CTR',
            disabled=False)

        self.d1_DatePicker = ipywidgets.DatePicker(disabled=False)
        self.d2_DatePicker = ipywidgets.DatePicker(disabled=False)

        #CNV - creative.name.version
        self.CNV_multiple = ipywidgets.SelectMultiple(
            options=sorted(set(
                self.df['creative.name.version'].fillna('None'))),
            value=[],
            rows=3,
            description='creative.name.version',
            disabled=False,
            layout=ipywidgets.Layout(width='50%', height='280px'))

        self.aggregate_checkbox = ipywidgets.Checkbox(
            value=False, description='Display aggregate', disabled=False)

        self.button = ipywidgets.Button(description="CHART IT !",
                                        layout=ipywidgets.Layout(
                                            width='100%', height='55px'))

        self.left_box = ipywidgets.VBox([
            self.AO_dropdown, self.site_dropdown, self.placement_dropdown,
            self.creative_type_dropdown, self.metric_measurement,
            self.d1_DatePicker, self.d2_DatePicker, self.aggregate_checkbox,
            self.button
        ])

        self.display = ipywidgets.HBox([self.left_box, self.CNV_multiple])
Ejemplo n.º 18
0
 def __get_tab0(self):
     return widgets.HBox([
         widgets.VBox([self.norm_selector, self.norm_selector.value[1]]),
         self.norm_btn_wgt
     ])
Ejemplo n.º 19
0
],
                   _titles={i: t
                            for i, t in enumerate(titles)},
                   layout=tab_layout)

homedir = os.getcwd()

tool_title = widgets.Label(r'\(\textbf{pc4sbml2}\)')
if nanoHUB_flag or hublib_flag:
    # define this, but don't use (yet)
    remote_cb = widgets.Checkbox(
        indent=False,
        value=False,
        description='Submit as Batch Job to Clusters/Grid')

    top_row = widgets.HBox(children=[read_config, tool_title])
    gui = widgets.VBox(children=[top_row, tabs, run_button.w])
    fill_gui_params(read_config.options['DEFAULT'])
else:
    top_row = widgets.HBox(children=[tool_title])
    gui = widgets.VBox(children=[top_row, tabs, run_button])
    fill_gui_params("data/PhysiCell_settings.xml")

# pass in (relative) directory where output data is located
output_dir = "tmpdir"
# svg.update(output_dir)

sub.update_dropdown_fields(
    "data")  # WARNING: generates multiple "<Figure size...>" stdout!
# animate_tab.update_dropdown_fields("data")
Ejemplo n.º 20
0
 def widget(self):
     return widgets.VBox([
         widgets.HBox(
             [self.axis_pos, self.index_slider, self.axis_selector]),
         self.out_main, self.out
     ])
Ejemplo n.º 21
0
                            width='auto',
                            margin='0px 0px 0px 0px')
slot_middle = widgets.Box(children=items, layout=box_layout)

slot = widgets.VBox(children=[slot_top, slot_middle, slot_bottom],
                    layout=widgets.Layout(display='flex',
                                          flex_flow='column',
                                          align_items='center',
                                          width='auto',
                                          margin='0px 0px 0px 0px'))

slot._stored_ints = []
imgs = [
    'waiting.png', 'bell.png', 'cherry.png', 'grape.png', 'lemon.png',
    'orange.png', 'strawberry.png', 'watermelon.png', 'seven.png'
]
slot._images = {}
for kk, img in enumerate(imgs):
    slot._images[kk - 1] = open(script_dir + "/symbols/%s" % img, "rb").read()

gen = widgets.ToggleButtons(
    options=['Simulator', 'ibmq_5_tenerife', 'ANU QRNG'],
    description='',
    disabled=False,
    button_style='')

out = widgets.Output()

opts = widgets.HBox(children=[gen, out],
                    layout=widgets.Layout(border='1px solid black'))
Ejemplo n.º 22
0
    def __init__(self,
                 data,
                 slcs=(slice(None, ), ),
                 title=None,
                 norm=None,
                 fig_handle=None,
                 time_in_title=True,
                 **kwargs):

        self._data, self._slcs, self.im_xlt, self.time_in_title = data, slcs, None, time_in_title
        # # # -------------------- Tab0 --------------------------
        items_layout = Layout(flex='1 1 auto', width='auto')
        # normalization
        # general parameters: vmin, vmax, clip
        self.if_vmin_auto = widgets.Checkbox(value=True,
                                             description='Auto',
                                             layout=items_layout)
        self.if_vmax_auto = widgets.Checkbox(value=True,
                                             description='Auto',
                                             layout=items_layout)
        self.vmin_wgt = widgets.FloatText(value=np.min(data),
                                          description='vmin:',
                                          continuous_update=False,
                                          disabled=self.if_vmin_auto.value,
                                          layout=items_layout)
        self.vlogmin_wgt = widgets.FloatText(value=self.eps,
                                             description='vmin:',
                                             continuous_update=False,
                                             disabled=self.if_vmin_auto.value,
                                             layout=items_layout)
        self.vmax_wgt = widgets.FloatText(value=np.max(data),
                                          description='vmax:',
                                          continuous_update=False,
                                          disabled=self.if_vmin_auto.value,
                                          layout=items_layout)
        self.if_clip_cm = widgets.Checkbox(value=True,
                                           description='Clip',
                                           layout=items_layout)
        # PowerNorm specific
        self.gamma = widgets.FloatText(value=1,
                                       description='gamma:',
                                       continuous_update=False,
                                       layout=items_layout)
        # SymLogNorm specific
        self.linthresh = widgets.FloatText(value=self.eps,
                                           description='linthresh:',
                                           continuous_update=False,
                                           layout=items_layout)
        self.linscale = widgets.FloatText(value=1.0,
                                          description='linscale:',
                                          continuous_update=False,
                                          layout=items_layout)

        # build the widgets tuple
        ln_wgt = (LogNorm,
                  widgets.VBox([
                      widgets.HBox([self.vlogmin_wgt, self.if_vmin_auto]),
                      widgets.HBox([self.vmax_wgt, self.if_vmax_auto]),
                      self.if_clip_cm
                  ]))
        n_wgt = (Normalize,
                 widgets.VBox([
                     widgets.HBox([self.vmin_wgt, self.if_vmin_auto]),
                     widgets.HBox([self.vmax_wgt, self.if_vmax_auto]),
                     self.if_clip_cm
                 ]))
        pn_wgt = (PowerNorm,
                  widgets.VBox([
                      widgets.HBox([self.vmin_wgt, self.if_vmin_auto]),
                      widgets.HBox([self.vmax_wgt, self.if_vmax_auto]),
                      self.if_clip_cm, self.gamma
                  ]))
        sln_wgt = (SymLogNorm,
                   widgets.VBox([
                       widgets.HBox([self.vmin_wgt, self.if_vmin_auto]),
                       widgets.HBox([self.vmax_wgt, self.if_vmax_auto]),
                       self.if_clip_cm, self.linthresh, self.linscale
                   ]))

        # find out default value for norm_selector
        norm_avail = {
            'Log': ln_wgt,
            'Normalize': n_wgt,
            'Power': pn_wgt,
            'SymLog': sln_wgt
        }
        self.norm_selector = widgets.Dropdown(options=norm_avail,
                                              value=norm_avail.get(
                                                  norm, n_wgt),
                                              description='Normalization:')
        # additional care for LorNorm()
        self.__handle_lognorm()
        # re-plot button
        self.norm_btn_wgt = widgets.Button(description='Apply',
                                           disabled=False,
                                           tooltip='set colormap',
                                           icon='refresh')
        tab0 = self.__get_tab0()

        # # # -------------------- Tab1 --------------------------
        # title
        if not title:
            title = osh5vis.default_title(data, show_time=self.time_in_title)
        self.if_reset_title = widgets.Checkbox(value=True, description='Auto')
        self.title = widgets.Text(value=title,
                                  placeholder='data',
                                  continuous_update=False,
                                  description='Title:',
                                  disabled=self.if_reset_title.value)
        # x label
        self.if_reset_xlabel = widgets.Checkbox(value=True, description='Auto')
        self.xlabel = widgets.Text(value=osh5vis.axis_format(
            data.axes[1].long_name, data.axes[1].units),
                                   placeholder='x',
                                   continuous_update=False,
                                   description='X label:',
                                   disabled=self.if_reset_xlabel.value)
        # y label
        self.if_reset_ylabel = widgets.Checkbox(value=True, description='Auto')
        self.ylabel = widgets.Text(value=osh5vis.axis_format(
            data.axes[0].long_name, data.axes[0].units),
                                   placeholder='y',
                                   continuous_update=False,
                                   description='Y label:',
                                   disabled=self.if_reset_ylabel.value)
        # colorbar
        self.if_reset_cbar = widgets.Checkbox(value=True, description='Auto')
        self.cbar = widgets.Text(value=data.units.tex(),
                                 placeholder='a.u.',
                                 continuous_update=False,
                                 description='Colorbar:',
                                 disabled=self.if_reset_cbar.value)

        tab1 = widgets.VBox([
            widgets.HBox([self.title, self.if_reset_title]),
            widgets.HBox([self.xlabel, self.if_reset_xlabel]),
            widgets.HBox([self.ylabel, self.if_reset_ylabel]),
            widgets.HBox([self.cbar, self.if_reset_cbar])
        ])

        # # # -------------------- Tab2 --------------------------
        self.setting_instructions = widgets.Label(
            value="Enter invalid value to reset", layout=items_layout)
        self.apply_range_btn = widgets.Button(description='Apply',
                                              disabled=False,
                                              tooltip='set range',
                                              icon='refresh')
        self.axis_lim_wgt = widgets.HBox(
            [self.setting_instructions, self.apply_range_btn])
        # x axis
        self.x_min_wgt = widgets.FloatText(value=self._data.axes[1].min,
                                           description='xmin:',
                                           continuous_update=False,
                                           layout=items_layout)
        self.x_max_wgt = widgets.FloatText(value=self._data.axes[1].max,
                                           description='xmax:',
                                           continuous_update=False,
                                           layout=items_layout)
        self.x_step_wgt = widgets.FloatText(value=self._data.axes[1].increment,
                                            continuous_update=False,
                                            description='$\Delta x$:',
                                            layout=items_layout)
        self.xaxis_lim_wgt = widgets.HBox(
            [self.x_min_wgt, self.x_max_wgt, self.x_step_wgt])
        # y axis
        self.y_min_wgt = widgets.FloatText(value=self._data.axes[0].min,
                                           description='ymin:',
                                           continuous_update=False,
                                           layout=items_layout)
        self.y_max_wgt = widgets.FloatText(value=self._data.axes[0].max,
                                           description='ymax:',
                                           continuous_update=False,
                                           layout=items_layout)
        self.y_step_wgt = widgets.FloatText(value=self._data.axes[0].increment,
                                            continuous_update=False,
                                            description='$\Delta y$:',
                                            layout=items_layout)
        self.yaxis_lim_wgt = widgets.HBox(
            [self.y_min_wgt, self.y_max_wgt, self.y_step_wgt])
        tab2 = widgets.VBox(
            [self.axis_lim_wgt, self.xaxis_lim_wgt, self.yaxis_lim_wgt])

        # # # -------------------- Tab3 --------------------------
        tab3 = self.if_lineout_wgt = widgets.Checkbox(
            value=False,
            description='X/Y Lineouts (incomplete feature)',
            layout=items_layout)

        # # # -------------------- Tab4 --------------------------
        user_cmap = kwargs.pop('cmap', 'jet')
        self.cmap_selector = widgets.Dropdown(options=self.colormaps_available,
                                              value=user_cmap,
                                              description='Colormap:')
        self.cmap_reverse = widgets.Checkbox(value=False,
                                             description='Reverse',
                                             layout=items_layout)
        tab4 = widgets.HBox([self.cmap_selector, self.cmap_reverse])

        # construct the tab
        self.tab = widgets.Tab()
        self.tab.children = [tab0, tab1, tab2, tab3, tab4]
        [self.tab.set_title(i, tt) for i, tt in enumerate(self.tab_contents)]
        # display(self.tab)

        # link and activate the widgets
        self.if_reset_title.observe(self.__update_title, 'value')
        self.if_reset_xlabel.observe(self.__update_xlabel, 'value')
        self.if_reset_ylabel.observe(self.__update_ylabel, 'value')
        self.if_reset_cbar.observe(self.__update_cbar, 'value')
        self.norm_btn_wgt.on_click(self.update_norm)
        self.if_vmin_auto.observe(self.__update_vmin, 'value')
        self.if_vmax_auto.observe(self.__update_vmax, 'value')
        self.norm_selector.observe(self.__update_norm_wgt, 'value')
        self.cmap_selector.observe(self.update_cmap, 'value')
        self.cmap_reverse.observe(self.update_cmap, 'value')
        self.title.observe(self.update_title, 'value')
        self.xlabel.observe(self.update_xlabel, 'value')
        self.ylabel.observe(self.update_ylabel, 'value')
        self.cbar.observe(self.update_cbar, 'value')
        self.y_max_wgt.observe(self.__update_y_max, 'value')
        self.y_min_wgt.observe(self.__update_y_min, 'value')
        self.x_max_wgt.observe(self.__update_x_max, 'value')
        self.x_min_wgt.observe(self.__update_x_min, 'value')
        self.x_step_wgt.observe(self.__update_delta_x, 'value')
        self.y_step_wgt.observe(self.__update_delta_y, 'value')
        self.apply_range_btn.on_click(self.update_plot_area)
        self.if_lineout_wgt.observe(self.toggle_lineout, 'value')

        # plotting and then setting normalization colors
        self.out = Output()
        self.out_main = Output()
        self.observer_thrd, self.cb = None, None
        self.fig = plt.figure() if fig_handle is None else fig_handle
        self.ax = self.fig.add_subplot(111)
        with self.out_main:
            self.im, self.cb = self.plot_data()
            display(self.fig)
Ejemplo n.º 23
0
        def execute_notebook(notebook_filename, notebook_save_filename,
                             params):
            log = UserMessages()

            with open(notebook_filename) as file_handler:
                notebook = nbformat.read(file_handler, as_version=4)
                b_errors = False
                execute_preprocessor = ExecutePreprocessor(
                    timeout=args.get('cell_timeout'),
                    allow_errors=args.get('allow_errors'))
                kernel_manager = None
                kernel_comm = None
                progress_bar = args.get('enable_progress_bar')

                if params:
                    for nb_cell in notebook.cells:
                        if nb_cell.cell_type == 'code':
                            new_cell_source = utils.substitute_params(
                                nb_cell.source, params)
                            nb_cell.source = new_cell_source
                            break

                try:
                    if progress_bar:

                        progress_bar = widgets.IntProgress(
                            value=0,
                            min=0,
                            max=len(notebook.cells),
                            step=1,
                            bar_style=
                            'info',  # 'success', 'info', 'warning', 'danger' or ''
                            orientation='horizontal')

                        kernel_manager, kernel_comm = start_new_kernel(
                            kernel_name=notebook['metadata']['kernelspec']
                            ['name'])
                        execute_preprocessor.km = kernel_manager
                        execute_preprocessor.kc = kernel_comm
                        execute_preprocessor.nb = notebook

                        display_label = notebook_filename
                        if notebook_save_filename:
                            display_label = display_label + ' : ' + notebook_save_filename
                        display(
                            widgets.HBox(
                                [widgets.Label(display_label), progress_bar]))

                        for idx, nb_cell in enumerate(notebook.cells):
                            execute_preprocessor.preprocess_cell(
                                nb_cell,
                                resources={'metadata': {}},
                                cell_index=idx)
                            progress_bar.value = idx + 1
                    else:
                        log.info("Running Notebook: " + notebook_filename)
                        execute_preprocessor.preprocess(
                            notebook, {'metadata': {}})
                except CellExecutionError:
                    b_errors = True
                    if progress_bar:
                        progress_bar.bar_style = 'danger'
                    raise
                except AttributeError:
                    b_errors = True
                    if progress_bar:
                        progress_bar.bar_style = 'danger'
                    raise
                finally:
                    if notebook_save_filename:
                        with open(notebook_save_filename,
                                  mode='wt') as file_handler:
                            nbformat.write(notebook, file_handler)

                    if kernel_manager or kernel_comm:
                        kernel_comm.stop_channels()
                        kernel_manager.shutdown_kernel()

                    if not b_errors:
                        if progress_bar:
                            progress_bar.bar_style = 'success'
                        else:
                            log.info(notebook_filename +
                                     " was executed successfully.")
                    elif b_errors and not progress_bar:
                        log.error(notebook_filename + " execution failed.")
Ejemplo n.º 24
0
def create_gui(geometry=None,
               callback=None,
               opts_choice=None,
               opts_range=None,
               opts_color=None,
               initial_values=None,
               layout=None,
               height=400,
               width=400,
               background='gray',
               orthographic=False,
               camera_position=[0, 0, -10],
               view=(10, -10, -10, 10),
               fov=50,
               add_objects=True,
               add_labels=True,
               show_object_info=False,
               otype_column=None,
               jslink=True):
    """ creates simple gui to visualise 3d geometry,
    with a callback to update geometry according to option widgets 

    Properties
    ----------
    geometry : pandas3js.models.GeometricCollection
    callback : function
        callback(GeometricCollection, options_dict)
    opts_choice : None or dict
        {opt_name:(initial, list)} create dropdown boxes with callbacks to callback
    opts_range : None or dict
        {opt_name:(initial, list)} create select slider with callbacks to callback
    opts_color : None or list
        {opt_name:init_color,...} create select color palette with callbacks to callback
    inital_values : None or dict
        initial values for options (default is first value of list)
    layout : None or list
        (tab_name,[option_name, ...]) pairs, 
        if nested list, then these will be vertically aligned
        by default all go in 'Other' tab
    height : int
        renderer height
    width : int
        renderer width
    background : str
        renderer background color (html)
    orthographic : bool
        use orthographic camera (True) or perspective (False) 
    camera_position : tuple
        position of camera in scene
    view : tuple
        initial view extents (top,bottom,left,right) (orthographic only)
    fov : float
        camera field of view (perspective only)
    add_objects : bool
        add objects to scene
    add_labels : bool
        add object labels to scene
    show_object_info : bool
        if True, show coordinate of object under mouse (currently only works for Perspective)
    jslink : bool
        if True, where possible, create client side links
        http://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Events.html#The-difference-between-linking-in-the-kernel-and-linking-in-the-client     
    
    Returns
    -------
    gui : widgets.Box
        containing rendered scene and option widgets
    gcollect : pandas3js.GeometricCollection
        the collection of current geometric objects
    options_view : dict_items
        a view of the current options values
        
    Examples
    --------

    >>> import pandas3js as pjs
    >>> import pandas as pd
    >>> data = {1:{'id':[0],'position':[(0,0,0)],
    ...              'c1':'red','c2':'blue'},
    ...         2:{'id':[0],'position':[(1,2,3)],
    ...               'c1':'red','c2':'blue'}}
    ...
    >>> def callback(geometry,options):
    ...     df = pd.DataFrame(data[options['config']])
    ...     ctype = options.get('color','c1')
    ...     df['color'] = df[ctype]
    ...     df['label'] = 'myobject'
    ...     df['otype'] = 'pandas3js.models.Sphere'
    ...     geometry.change_by_df(df[['id','position','otype',
    ...                               'color','label']],otype_column='otype')
    ...
    >>> gui, collect, opts = pjs.views.create_gui(callback=callback,
    ...                     opts_choice={'color':['c1','c2']},
    ...                     opts_range={'config':[1,2]})
    ...
    >>> [pjs.utils.obj_to_str(c) for c in gui.children]
    ['ipywidgets.widgets.widget_selectioncontainer.Tab', 'pythreejs.pythreejs.Renderer']
    >>> collect.trait_df().loc[0]
    color                                              red
    groups                                          (all,)
    id                                                   0
    label                                         myobject
    label_color                                        red
    label_transparency                                   1
    label_visible                                    False
    other_info                                            
    otype                 pandas3js.models.idobject.Sphere
    position                               (0.0, 0.0, 0.0)
    radius                                               1
    transparency                                         1
    visible                                           True
    Name: 0, dtype: object
    >>> config_select = gui.children[0].children[1].children[1].children[1]
    >>> pjs.utils.obj_to_str(config_select)
    'ipywidgets.widgets.widget_selection.SelectionSlider'
    >>> config_select.value = 2
    >>> collect.trait_df().loc[0]
    color                                              red
    groups                                          (all,)
    id                                                   0
    label                                         myobject
    label_color                                        red
    label_transparency                                   1
    label_visible                                    False
    other_info                                            
    otype                 pandas3js.models.idobject.Sphere
    position                               (1.0, 2.0, 3.0)
    radius                                               1
    transparency                                         1
    visible                                           True
    Name: 0, dtype: object
    >>> color_select = gui.children[0].children[1].children[1].children[0]
    >>> pjs.utils.obj_to_str(color_select)
    'ipywidgets.widgets.widget_selection.ToggleButtons'
    >>> color_select.value = 'c2'
    >>> collect.trait_df().loc[0]
    color                                             blue
    groups                                          (all,)
    id                                                   0
    label                                         myobject
    label_color                                        red
    label_transparency                                   1
    label_visible                                    False
    other_info                                            
    otype                 pandas3js.models.idobject.Sphere
    position                               (1.0, 2.0, 3.0)
    radius                                               1
    transparency                                         1
    visible                                           True
    Name: 0, dtype: object
    
    """
    ## intialise options
    init_vals = {} if initial_values is None else initial_values

    opts_choice = {} if opts_choice is None else opts_choice
    all_options = {
        label: init_vals[label] if label in init_vals else options[0]
        for label, options in opts_choice.items()
    }
    opts_range = {} if opts_range is None else opts_range
    all_options.update({
        label: init_vals[label] if label in init_vals else options[0]
        for label, options in opts_range.items()
    })
    opts_color = {} if opts_color is None else opts_color
    all_options.update({
        label: init_vals[label] if label in init_vals else init
        for label, init in opts_color.items()
    })

    if len(all_options
           ) != len(opts_choice) + len(opts_range) + len(opts_color):
        raise ValueError(
            'options in opts_choice, opts_slide, and opts_color are not unique'
        )

    ## intialise layout
    layout = [] if layout is None else layout
    layout_dict = OrderedDict(layout)
    if len(layout_dict) != len(layout):
        raise ValueError('layout tab names are not unique')

    ## initialise renderer
    if geometry is None:
        gcollect = pjs.models.GeometricCollection()
    else:
        gcollect = geometry
    scene = pjs.views.create_js_scene_view(gcollect,
                                           add_objects=add_objects,
                                           add_labels=add_labels,
                                           jslink=jslink)
    camera, renderer = pjs.views.create_jsrenderer(
        scene,
        orthographic=orthographic,
        camera_position=camera_position,
        view=view,
        fov=fov,
        height=height,
        width=width,
        background=background)

    ## create minimal callback
    if callback is None:

        def callback(geometry, options):
            return

    ## initialise geometry in renderer
    with renderer.hold_trait_notifications():
        callback(gcollect, all_options)

    ## Create controls and callbacks
    controls = {}

    # a check box for showing labels
    if add_labels:
        toggle = widgets.Checkbox(value=False, description='View Label:')

        def handle_toggle(change):
            for obj in gcollect.idobjects:
                obj.label_visible = change.new

        toggle.observe(handle_toggle, names='value')
        controls['View Label'] = toggle

    # zoom sliders for orthographic
    if orthographic:
        top, bottom, left, right = view
        axiszoom = widgets.FloatSlider(
            value=0,
            min=-10,
            max=10,
            step=0.1,
            description='zoom',
            continuous_update=True,
        )

        def handle_axiszoom(change):
            if change.new > 1:
                zoom = 1. / change.new
            elif change.new < -1:
                zoom = -change.new
            else:
                zoom = 1
            with renderer.hold_trait_notifications():
                camera.left = zoom * left
                camera.right = zoom * right
                camera.top = zoom * top
                camera.bottom = zoom * bottom

        axiszoom.observe(handle_axiszoom, names='value')

        controls['Orthographic Zoom'] = axiszoom

    # add additional options

    dd_min = 4  # min amount of options before switch to toggle buttons
    for label in opts_choice:
        options = opts_choice[label]
        initial = init_vals[label] if label in init_vals else options[0]
        assert initial in list(
            options), "initial value {0} for {1} not in range: {2}".format(
                initial, label, list(options))
        if (len(options) == 2 and True in options and False in options
                and isinstance(options[0], bool)
                and isinstance(options[1], bool)):
            ddown = widgets.Checkbox(value=initial, description=label)
        elif len(options) < dd_min:
            ddown = widgets.ToggleButtons(options=list(options),
                                          description=label,
                                          value=initial)
        else:
            ddown = widgets.Dropdown(options=list(options),
                                     description=label,
                                     value=initial)
        handle = _create_callback(renderer, ddown, callback, gcollect,
                                  all_options)
        ddown.observe(handle, names='value')

        controls[label] = ddown

    for label in opts_range:
        options = opts_range[label]
        initial = init_vals[label] if label in init_vals else options[0]
        assert initial in list(
            options), "initial value {0} for {1} not in range: {2}".format(
                initial, label, list(options))
        slider = widgets.SelectionSlider(description=label,
                                         value=initial,
                                         options=list(options),
                                         continuous_update=False)
        handle = _create_callback(renderer, slider, callback, gcollect,
                                  all_options)
        slider.observe(handle, names='value')

        controls[label] = slider

    for label in opts_color:
        option = init_vals[label] if label in init_vals else opts_color[label]
        color = widgets.ColorPicker(description=label,
                                    value=option,
                                    concise=False)
        handle = _create_callback(renderer, color, callback, gcollect,
                                  all_options)
        color.observe(handle, names='value')

        controls[label] = slider

    # add mouse hover information box
    # TODO doesn't work for orthographic https://github.com/jovyan/pythreejs/issues/101
    if not orthographic and show_object_info:
        # create information box
        click_picker = tjs.Picker(root=scene.children[0], event='mousemove')
        infobox = widgets.HTMLMath()

        def change_info(change):
            if click_picker.object:
                infobox.value = 'Object Coordinate: ({1:.3f}, {2:.3f}, {3:.3f})<br>{0}'.format(
                    click_picker.object.other_info,
                    *click_picker.object.position)
            else:
                infobox.value = ''

        click_picker.observe(change_info, names=['object'])
        renderer.controls = renderer.controls + [click_picker]
        renderer = widgets.HBox([renderer, infobox])

    if not controls:
        return (renderer, gcollect, all_options.viewitems() if hasattr(
            all_options, 'viewitems') else all_options.items()
                )  # python 2/3 compatability

    ## layout tabs and controls
    tabs = OrderedDict()
    for tab_name, clist in layout_dict.items():
        vbox_list = []

        for cname in clist:
            if isinstance(cname, list):
                hbox_list = [controls.pop(subcname) for subcname in cname]
                vbox_list.append(widgets.HBox(hbox_list))
            else:
                vbox_list.append(controls.pop(cname))
        tabs[tab_name] = widgets.VBox(vbox_list)

    if 'Orthographic Zoom' in controls:
        tabs.setdefault('View', widgets.Box())
        tabs['View'] = widgets.VBox(
            [tabs['View'], controls.pop('Orthographic Zoom')])

    if 'View Label' in controls:
        tabs.setdefault('View', widgets.Box())
        tabs['View'] = widgets.VBox([tabs['View'], controls.pop('View Label')])

    # deal with remaining controls
    if controls:
        vbox_list = []
        for cname in natural_sort(controls):
            vbox_list.append(controls.pop(cname))
        tabs.setdefault('Other', widgets.Box())
        tabs['Other'] = widgets.VBox([tabs['Other'], widgets.VBox(vbox_list)])

    options = widgets.Tab(children=tuple(tabs.values()))
    for i, name in enumerate(tabs):
        options.set_title(i, name)

    return (widgets.VBox([options,
                          renderer]), gcollect, all_options.viewitems()
            if hasattr(all_options, 'viewitems') else all_options.items()
            )  # python 2/3 compatability
Ejemplo n.º 25
0
    def __init__(self,
                 backend,
                 dataset,
                 x,
                 y=None,
                 z=None,
                 w=None,
                 grid=None,
                 limits=None,
                 shape=128,
                 what="count(*)",
                 f=None,
                 vshape=16,
                 selection=None,
                 grid_limits=None,
                 normalize=None,
                 colormap="afmhot",
                 figure_key=None,
                 fig=None,
                 what_kwargs={},
                 grid_before=None,
                 vcount_limits=None,
                 controls_selection=False,
                 **kwargs):
        super(PlotBase, self).__init__(x=x,
                                       y=y,
                                       z=z,
                                       w=w,
                                       what=what,
                                       vcount_limits=vcount_limits,
                                       grid_limits=grid_limits,
                                       f=f,
                                       **kwargs)
        self.backend = backend
        self.vgrids = [None, None, None]
        self.vcount = None
        self.dataset = dataset
        self.limits = self.get_limits(limits)
        self.shape = shape
        self.selection = selection
        #self.grid_limits = grid_limits
        self.normalize = normalize
        self.colormap = colormap
        self.what_kwargs = what_kwargs
        self.grid_before = grid_before
        self.figure_key = figure_key
        self.fig = fig
        self.vshape = vshape

        self._new_progressbar()

        self.output = widgets.Output()
        # with self.output:
        if 1:
            self._cleanups = []

            self.progress = widgets.FloatProgress(value=0.0,
                                                  min=0.0,
                                                  max=1.0,
                                                  step=0.01)
            self.progress.layout.width = "95%"
            self.progress.layout.max_width = '500px'
            self.progress.description = "progress"

            self.backend.create_widget(self.output, self, self.dataset,
                                       self.limits)
            self.control_widget = widgets.VBox()

            # self.create_tools()
            self.widget = widgets.VBox([
                self.control_widget, self.backend.widget, self.progress,
                self.output
            ])
            if grid is None:
                self.update_grid()
            else:
                self.grid = grid

            self.widget_f = widgets.Dropdown(
                options=[('identity',
                          'identity'), ('log',
                                        'log'), ('log10',
                                                 'log10'), ('log1p', 'log1p')])
            widgets.link((self, 'f'), (self.widget_f, 'value'))
            self.observe(lambda *__: self.update_image(), 'f')
            self.add_control_widget(self.widget_f)

            self.widget_grid_limits_min = widgets.FloatSlider(
                value=0, min=0, max=100, step=0.1, description='vmin%')
            self.widget_grid_limits_max = widgets.FloatSlider(
                value=100, min=0, max=100, step=0.1, description='vmax%')
            widgets.link((self.widget_grid_limits_min, 'value'),
                         (self, 'grid_limits_min'))
            widgets.link((self.widget_grid_limits_max, 'value'),
                         (self, 'grid_limits_max'))
            #widgets.link((self.widget_grid_limits_min, 'f'), (self.widget_f, 'value'))
            self.observe(lambda *__: self.update_image(),
                         ['grid_limits_min', 'grid_limits_max'])
            self.add_control_widget(self.widget_grid_limits_min)
            self.add_control_widget(self.widget_grid_limits_max)

            self.widget_grid_limits = None

        selections = vaex.dataset._ensure_list(self.selection)
        selections = [_translate_selection(k) for k in selections]
        selections = [k for k in selections if k]
        self.widget_selection_active = widgets.ToggleButtons(
            options=list(zip(selections, selections)), description='selection')
        self.controls_selection = controls_selection
        if self.controls_selection:
            self.add_control_widget(self.widget_selection_active)

            modes = ['replace', 'and', 'or', 'xor', 'subtract']
            self.widget_selection_mode = widgets.ToggleButtons(
                options=modes, description='mode')
            self.add_control_widget(self.widget_selection_mode)

            self.widget_selection_undo = widgets.Button(options=modes,
                                                        description='undo',
                                                        icon='arrow-left')
            self.widget_selection_redo = widgets.Button(options=modes,
                                                        description='redo',
                                                        icon='arrow-right')
            self.add_control_widget(
                widgets.HBox([
                    widgets.Label('history', layout={'width': '80px'}),
                    self.widget_selection_undo, self.widget_selection_redo
                ]))

            def redo(*ignore):
                selection = _translate_selection(
                    self.widget_selection_active.value)
                self.dataset.selection_redo(name=selection)
                check_undo_redo()

            self.widget_selection_redo.on_click(redo)

            def undo(*ignore):
                selection = _translate_selection(
                    self.widget_selection_active.value)
                self.dataset.selection_undo(name=selection)
                check_undo_redo()

            self.widget_selection_undo.on_click(undo)

            def check_undo_redo(*ignore):
                selection = _translate_selection(
                    self.widget_selection_active.value)
                self.widget_selection_undo.disabled = not self.dataset.selection_can_undo(
                    selection)
                self.widget_selection_redo.disabled = not self.dataset.selection_can_redo(
                    selection)

            self.widget_selection_active.observe(check_undo_redo, 'value')
            check_undo_redo()

            callback = self.dataset.signal_selection_changed.connect(
                check_undo_redo)
            callback = self.dataset.signal_selection_changed.connect(
                lambda *x: self.update_grid())

        def _on_limits_change(*args):
            self._progressbar.cancel()
            self.update_grid()

        self.backend.observe(_on_limits_change, "limits")
        for attrname in "x y z vx vy vz".split():

            def _on_change(*args, attrname=attrname):
                limits_index = {'x': 0, 'y': 1, 'z': 2}.get(attrname)
                if limits_index is not None:
                    self.backend.limits[limits_index] = None
                self.update_grid()

            self.observe(_on_change, attrname)
        self.observe(lambda *args: self.update_grid(), "what")
        self.observe(lambda *args: self.update_image(), "vcount_limits")
Ejemplo n.º 26
0
# Labels for the velocity slider in Figure 1
speed_label = widgets.HTML(value='<b>Radial Velocity of Source</b>: ')
speed_label.layout.width = longspeed_label_width

vel_label = widgets.HTML(value='0', positioning='right')
vel_label.layout.width = vel_value_width 
vel_label.layout.align_content = 'center'

vunits = widgets.HTML(value='km/s')
vunits.width = unit_text_width

direction = widgets.HTML(value='')
direction.layout.width = longdirection_width

# these boxes contain display commands for the velocity of each star
fig1controls_box = widgets.HBox([speed_label, vel_label, vunits, direction])
fig1controls_box.layout.width = '400px'

# Define Figure 1
figure1 = bq.Figure(title = 'Hydrogen Cloud Absorbtion Spectrum', axes=[ax_x_fig1, ax_y_fig1], animation = 100,
                    marks = [fig1_wide_line, hydrogen_line_lab_simple, hydrogen_line_moving_simple, fig1_Lab_label], 
                    padding_y=0, min_aspect_ratio=2.5, max_aspect_ratio=2.5)
figure1.layout.width = '500px'

# Update the widgets
fig1_vel_slider.observe(simple_update, names=['value'])
fig1_dval.observe(simple_update, names=['value'])
fig1_gval.observe(simple_update, names=['value'])
fig1_bval.observe(simple_update, names=['value'])
fig1_aval.observe(simple_update, names=['value'])
fig1_reset.on_click(simple_reset)
Ejemplo n.º 27
0
    def __init__(self,
                 hdu=None,
                 im=None,
                 wcs=None,
                 show_rainbow=True,
                 *args,
                 **kwargs):
        super().__init__(*args, **kwargs)
        # self._4d_idx = 0  # Lock 4th dim to this for now

        if hdu is not None:
            self.im = hdu.data
            self.wcs = WCS(hdu.header)
        elif im is not None:
            self.im = im
            self.wcs = wcs
        else:
            print("Provide a 3D HDU or image and wcs object")

        self.nddata = NDData(self.im, wcs=self.wcs)
        self.load_nddata(self.nddata, n=0)

        # get wave info:

        self.dwave = self.wcs.wcs.cdelt[2]
        self.wave_start = self.wcs.wcs.crval[2]
        self.nwave = np.shape(self.im)[0]
        self.wave_end = self.wave_start + self.nwave * self.dwave
        self.show_rainbow = show_rainbow

        #zscale = ZScaleInterval(contrast=0.3, krej=2.5)
        #vmin, vmax = zscale.get_limits(values=self.im)

        self.cuts = 'stddev'

        self.wave_widget = widgets.IntSlider(
            min=self.wave_start,
            max=self.wave_end,
            step=self.dwave,
            value=self.wave_start,
            continuous_update=False,
        )

        self.slider = widgets.interactive(self.show_slice,
                                          wave=self.wave_widget)

        self.animate_button = widgets.Button(
            description="Scan Cube",
            disabled=False,
            button_style="success",
            tooltip="Click this to scan in wavelength dimension",
        )

        # For line profile plot
        self._cur_islice = None
        self._cur_ix = None
        self._cur_iy = None
        self.line_out = widgets.Output()
        self.line_plot = None
        self.plot_xlabel = "Wavelength (A)"
        self.plot_ylabel = "Flux Density"  # (10^-17 erg cm-2 s-1 arcsec-2"

        if self.show_rainbow:
            self.set_rainbow()

        # If plot shows, rerun cell to hide it.
        ax = plt.gca()
        self.line_plot = ax

        self.scan = widgets.Play(
            value=self.wave_start,
            min=self.wave_start,
            max=self.wave_end,
            step=self.dwave,
            #            interval=500,
            description="Scan Cube",
            disabled=False,
        )

        widgets.jslink((self.scan, "value"), (self.wave_widget, "value"))

        left_panel = widgets.VBox(
            [widgets.HBox([self.wave_widget, self.scan]), self])

        display(widgets.HBox([left_panel, self.line_out]))
Ejemplo n.º 28
0
    def __init__(self,
                 the_workchain=None,
                 job_details=None,
                 presub_calls=[],
                 **kwargs):
        """ Submit Button

        :param 
        """

        self.the_workchain = the_workchain
        self.submit_details = {}
        self.job_details = job_details

        # list of methods to call after submit button is pressed
        self.presub_calls = presub_calls

        self.btn_submit = ipw.Button(description="Submit", disabled=False)
        self.walltime = ipw.IntText(value=86000,
                                    description='walltime',
                                    style={'description_width': '120px'},
                                    layout={'width': '30%'})

        self.submit_out = ipw.Output()

        self.job_details['walltime'] = self.walltime.value

        def set_walltime(c):
            self.job_details['walltime'] = self.walltime.value

        def on_btn_submit_press(b):

            for presub_call in self.presub_calls:
                presub_call()

            #self.parse_job_details()
            keys_defined = self.job_details.keys()
            with self.submit_out:
                clear_output()

                ### CHECK VALIDITY OF INPUTS
                if 'structure' not in keys_defined:
                    print("Please select a structure.")
                    return
                if 'cp2k_code' not in keys_defined:
                    print("Please select a computer.")
                    return
                if len(self.job_details['calc_name']) < 5:
                    print("Please enter a longer calculation description.")
                    return

                ### ODD CHARGE AND RKS
                odd_charge = self.job_details['slab_analyzed']['total_charge']
                if 'charge' in self.job_details.keys():
                    odd_charge += self.job_details['charge']
                    rks = True
                    if 'uks_switch' in self.job_details.keys():
                        if self.job_details['uks_switch'] == 'UKS':
                            rks = False
                if odd_charge % 2 > 0 and rks:
                    print("ODD CHARGE AND RKS")
                    return
                if self.job_details['workchain'] == 'NEBWorkchain':
                    if len(self.job_details['replica_pks'].split()) < 2:
                        print('Please select at least two  replica_pks')
                        return
                if self.job_details['workchain'] == 'PhononsWorkchain':
                    num_calcs = self.job_details['ncalcs']
                    num_rep = self.job_details['nreplicas']
                    if not (num_calcs % num_rep) == 0:
                        print('#Calculations', num_calcs)
                        print('#Replicas', num_rep)
                        print(
                            'Choose #Replicas so that #Calculations % #Replicas is 0.'
                        )
                        return
                self.structure = self.job_details['structure']
                self.code = self.job_details['cp2k_code']
                print("SUBMITTING workchain ", self.the_workchain)
                print("")

                self.btn_submit.disabled = True
                self.parse_job_details()
                for field in self.submit_details.keys():
                    #if field != 'slab_analyzed':
                    print(field, self.submit_details.get_dict()[field])

                if self.job_details['calc_name'] != 'DO NOT SUBMIT':
                    arg_dict = {
                        'code': self.code,
                        'structure': self.structure,
                        'parameters': self.submit_details
                    }
                    if self.struc_folder is not None:
                        arg_dict['struc_folder'] = self.struc_folder
                    outputs = submit(self.the_workchain, **arg_dict)
                    print(outputs)
                else:
                    print("TEST NO SUBMISSION")
                    theworkchain = self.the_workchain()
                    outputs = theworkchain.build_calc_inputs(
                        structure=self.structure,
                        input_dict=self.submit_details.get_dict())
                    print(outputs['parameters'].get_dict())

                print("")
                print("DONE")

            if self.job_details['calc_name'] != 'DO NOT SUBMIT':
                the_workcalc = load_node(outputs.pid)
                the_workcalc.description = self.job_details['calc_name']

        self.btn_submit.on_click(on_btn_submit_press)
        self.walltime.observe(set_walltime, 'value')
        ### ---------------------------------------------------------
        ### Define the ipw structure and create parent VBOX

        children = [
            ipw.HBox([self.btn_submit, self.walltime]), self.submit_out
        ]

        super(SubmitButton, self).__init__(children=children, **kwargs)
Ejemplo n.º 29
0
def input_reactions_widget(substances, properties):
    input_reactions_textbox = widgets.Textarea(
        value=
        '# Write reactions using substance \'symbols\'\n# e.g. Calcite (left column) not CaCO3\n# Carefully balance the reactions!\nH2O@ = H+ + OH-',
        description='',
        layout=Layout(max_width='max-content',
                      min_width='360px',
                      max_height='max-content',
                      height='340px'))
    """ Widget with a search field and lots of checkboxes """
    search_widget = widgets.Text(description="Search",
                                 style={'description_width': '50px'})

    subst_dict = {
        description: widgets.Text(layout={'width': '285px'},
                                  style={'description_width': '100px'},
                                  description=description,
                                  value=substances[description].formula(),
                                  disabled=True)
        for description in substances.keys()
    }
    box_layout = Layout(  #overflow_y='scroll',
        #border='3px solid black',
        width='max-content',
        height='320px',
        flex_flow='column',
        display='flex',
        align_items='stretch',
        color='blue')
    substs = [subst_dict[description]
              for description in substances.keys()]  #records]

    subst_widget = widgets.VBox(substs, layout=box_layout)

    # Wire the search field to the checkboxes
    def on_text_change(change):
        search_input = change['new']
        if search_input == '':
            # Reset search field
            new_options = [
                subst_dict[description] for description in substances.keys()
            ]
        else:
            # Filter by search field using difflib.
            #          close_matches = difflib.get_close_matches(search_input, records, n=5, cutoff=0.0)
            # close_matches = list(filter(lambda x: search_input in x, records))
            close_matches = [
                x for x in substances.keys() if re.search(
                    search_input, substances[x].formula(), re.IGNORECASE)
            ]
            new_options = [
                subst_dict[description] for description in close_matches
            ]
        subst_widget.children = new_options

    props_dict = {
        description: widgets.Checkbox(description=description,
                                      value=False,
                                      style={'description_width': '10px'})
        for description in properties
    }
    props_dict[properties[0]].value = True
    options_props = [props_dict[description] for description in properties]
    txt = widgets.Label(value='Properties')
    props_widget = widgets.VBox(options_props, layout=box_layout)
    props_txt = widgets.VBox([txt, props_widget])

    search_widget.observe(on_text_change, names="value")
    multi_select = widgets.VBox([search_widget, subst_widget])
    multi_data = widgets.HBox(
        [multi_select, input_reactions_textbox, props_txt])

    return multi_data
Ejemplo n.º 30
0
    def __init__(self):
        # Should now scale for the time of day effect
        # TAF - Temporal Allocation Factor
        #  Get from up-to-date Tom Tom web site:
        #      https://www.tomtom.com/en_gb/traffic-index/edinburgh-traffic/
        #  For a typical Wednesday since that seems to be the eveing rush-hour with most    congestion
        # TAF list has % congestion by hour starting at hour 0, midnight
        # In Jupyter Notebook make this a Figure
        self.TomTom_congestion = [2, 0, 0, 0, 0, 0, 13, 54, 80, 50, 38, 38, 40, \
            40, 42, 55, 81, 86, 54, 30, 20, 16, 14, 8]
        #print(sum(TomTom_congestion))
        self.trafficstats = {"AADT": 10000, "hour": 12, "vs": 15}
        # Now adjust for fleet Mix and AADT and Time
        # Defaults -  get from spreadsheet for 2020
        # Urban split for England in 2020 below since that's likely to be the most relevant
        # Use a selection box for this
        self.fleetmix_2020 = {
            "electric": 0.5,
            "petrol": 47.8,
            "diesel": 33.1,
            "petrol_lgv": 0.4,
            "diesel_lgv": 15.0,
            "rigid": 0.9,
            "artic": 0.4,
            "biodiesel": 0.1,
            "buses": 0.7,
            "motorcycle": 0.9,
            "lpg": 0.2
        }
        self.sumt = 0.0
        for x in self.fleetmix_2020.values():
            self.sumt = self.sumt + x
            self.sumtt = (str(int(self.sumt)))
        #print(self.sumtt)

        self.bft_electric = widgets.BoundedFloatText(
            value=self.fleetmix_2020["electric"],
            min=0.1,
            max=90,
            step=1,
            description="electric %",
            width=50)
        self.bft_petrol_cars = widgets.BoundedFloatText(
            value=self.fleetmix_2020["petrol"],
            min=1,
            max=99,
            step=1,
            description="petrol cars %",
            width=50)
        self.bft_diesel_cars = widgets.BoundedFloatText(
            value=self.fleetmix_2020["diesel"],
            min=1,
            max=99,
            step=1,
            description="diesel cars %",
            width=50)
        self.bft_petrol_lgv = widgets.BoundedFloatText(
            value=self.fleetmix_2020["petrol_lgv"],
            min=0.1,
            max=25.0,
            step=0.1,
            description="petrol lgv %",
            width=50)
        self.bft_diesel_lgv = widgets.BoundedFloatText(
            value=self.fleetmix_2020["diesel_lgv"],
            min=0.1,
            max=25.0,
            step=1,
            description="diesel lgv %",
            width=50)
        self.bft_rigid_truck = widgets.BoundedFloatText(
            value=self.fleetmix_2020["rigid"],
            min=0.1,
            max=10.0,
            step=0.1,
            description="rigid truck %",
            width=50)
        self.bft_artic_truck = widgets.BoundedFloatText(
            value=self.fleetmix_2020["artic"],
            min=0.1,
            max=10.0,
            step=0.1,
            description="artic truck %",
            width=50)
        self.bft_biodiesel = widgets.BoundedFloatText(
            value=self.fleetmix_2020["biodiesel"],
            min=0.1,
            max=10.0,
            step=0.1,
            description="biodiesel %",
            width=50)
        self.bft_buses = widgets.BoundedFloatText(
            value=self.fleetmix_2020["buses"],
            min=0.1,
            max=50.0,
            step=0.1,
            description="buses %",
            width=50)
        self.bft_motorcycles = widgets.BoundedFloatText(
            value=self.fleetmix_2020["motorcycle"],
            min=0.1,
            max=35.0,
            step=0.1,
            description="motorcycles %",
            width=50)
        self.bft_lpg = widgets.BoundedFloatText(
            value=self.fleetmix_2020["lpg"],
            min=0.1,
            max=35.0,
            step=0.1,
            description="LPG %",
            width=50)
        self.sumtotal = widgets.Text(value=self.sumtt,
                                     description="Total should be 100%",
                                     width=50,
                                     color='red')

        self.bft_electric.observe(self.bft_electric_eventhandler,
                                  names='value')
        self.bft_hour = widgets.BoundedIntText(value=self.trafficstats["hour"],
                                               min=1,
                                               max=24,
                                               step=1,
                                               description="Hour of Day")
        self.bft_AADT = widgets.BoundedIntText(value=self.trafficstats["AADT"],
                                               min=1000,
                                               max=755000,
                                               step=2500,
                                               description="AADT")
        self.bft_vs = widgets.BoundedIntText(value=self.trafficstats["vs"],
                                             min=10,
                                             max=130,
                                             step=5,
                                             description="Speed (km/h)")

        self.bft_petrol_cars.observe(self.bft_petrol_cars_eventhandler,
                                     names='value')
        self.bft_diesel_cars.observe(self.bft_diesel_cars_eventhandler,
                                     names='value')
        self.bft_petrol_lgv.observe(self.bft_petrol_lgv_eventhandler,
                                    names='value')
        self.bft_diesel_lgv.observe(self.bft_diesel_lgv_eventhandler,
                                    names='value')
        self.bft_rigid_truck.observe(self.bft_rigid_truck_eventhandler,
                                     names='value')
        self.bft_artic_truck.observe(self.bft_artic_truck_eventhandler,
                                     names='value')
        self.bft_biodiesel.observe(self.bft_biodiesel_eventhandler,
                                   names='value')
        self.bft_buses.observe(self.bft_buses_eventhandler, names='value')
        self.bft_motorcycles.observe(self.bft_motorcycles_eventhandler,
                                     names='value')
        self.bft_lpg.observe(self.bft_lpg_eventhandler, names='value')
        self.bft_hour.observe(self.bft_hour_eventhandler, names='value')
        self.bft_AADT.observe(self.bft_AADT_eventhandler, names='value')
        self.bft_vs.observe(self.bft_vs_eventhandler, names='value')
        self.btn = widgets.Button(description='Run RLINE', width=100)
        self.btn.style.button_color = 'tomato'
        self.btn.on_click(self.btn_eventhandler)

        self.h1 = widgets.HBox(children=[
            self.bft_electric, self.bft_petrol_cars, self.bft_diesel_cars
        ])
        self.h2 = widgets.HBox(children=[
            self.bft_petrol_lgv, self.bft_diesel_lgv, self.bft_rigid_truck
        ])
        self.h3 = widgets.HBox(children=[
            self.bft_artic_truck, self.bft_biodiesel, self.bft_buses
        ])
        self.h4 = widgets.HBox(
            children=[self.bft_motorcycles, self.bft_lpg, self.sumtotal])