Ejemplo n.º 1
0
from graph_notebook.seed.load_query import get_data_sets, get_queries
from graph_notebook.status.get_status import get_status
from graph_notebook.widgets import Force
from graph_notebook.options import OPTIONS_DEFAULT_DIRECTED, vis_options_merge

sparql_table_template = retrieve_template("sparql_table.html")
sparql_explain_template = retrieve_template("sparql_explain.html")
sparql_construct_template = retrieve_template("sparql_construct.html")
gremlin_table_template = retrieve_template("gremlin_table.html")
pre_container_template = retrieve_template("pre_container.html")
loading_wheel_template = retrieve_template("loading_wheel.html")
error_template = retrieve_template("error.html")

loading_wheel_html = loading_wheel_template.render()
DEFAULT_LAYOUT = widgets.Layout(max_height='600px',
                                overflow='scroll',
                                width='100%')

logging.basicConfig()
logger = logging.getLogger("graph_magic")

DEFAULT_MAX_RESULTS = 1000

GREMLIN_CANCEL_HINT_MSG = '''You must supply a string queryId when using --cancelQuery, for example: %gremlin_status --cancelQuery --queryId my-query-id'''
SPARQL_CANCEL_HINT_MSG = '''You must supply a string queryId when using --cancelQuery, for example: %sparql_status --cancelQuery --queryId my-query-id'''
SEED_LANGUAGE_OPTIONS = ['', 'Gremlin', 'SPARQL']


class QueryMode(Enum):
    DEFAULT = 'query'
    EXPLAIN = 'explain'
Ejemplo n.º 2
0
    def __init__(self):
        """Set up all widget GUI elements and class attributes."""
        self.status_output = None
        self.subsys_candidates_dict = self.get_subsys_candidates()
        self.interactions_count = 0
        self.current_interaction_key = ''
        self.interactions_dict = {}

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

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

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

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

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

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

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

        # == Make GUI connections ======================================================================================
        self.connect_ui()
Ejemplo n.º 3
0
def task_timeline():
    path_input = widgets.Button(description="View task timeline")

    breakdown_basic = "Basic"
    breakdown_task = "Task Breakdowns"

    breakdown_opt = widgets.Dropdown(
        options=["Basic", "Task Breakdowns"],
        value="Task Breakdowns",
        disabled=False,
    )
    obj_dep = widgets.Checkbox(value=True,
                               disabled=False,
                               layout=widgets.Layout(width='20px'))
    task_dep = widgets.Checkbox(value=True,
                                disabled=False,
                                layout=widgets.Layout(width='20px'))
    # Labels to bypass width limitation for descriptions.
    label_tasks = widgets.Label(value='Task submissions',
                                layout=widgets.Layout(width='110px'))
    label_objects = widgets.Label(value='Object dependencies',
                                  layout=widgets.Layout(width='130px'))
    label_options = widgets.Label(value='View options:',
                                  layout=widgets.Layout(width='100px'))
    start_box, end_box, range_slider, time_opt = get_sliders(False)
    display(widgets.HBox([task_dep, label_tasks, obj_dep, label_objects]))
    display(widgets.HBox([label_options, breakdown_opt]))
    display(path_input)

    # Check that the trace viewer renderer file is present, and copy it to the
    # current working directory if it is not present.
    if not os.path.exists("trace_viewer_full.html"):
        shutil.copy(
            os.path.join(os.path.dirname(os.path.abspath(__file__)),
                         "../core/src/catapult_files/trace_viewer_full.html"),
            "trace_viewer_full.html")

    def handle_submit(sender):
        json_tmp = tempfile.mktemp() + ".json"

        # Determine whether task components should be displayed or not.
        if breakdown_opt.value == breakdown_basic:
            breakdown = False
        elif breakdown_opt.value == breakdown_task:
            breakdown = True
        else:
            raise ValueError("Unexpected breakdown value '{}'".format(
                breakdown_opt.value))

        low, high = map(lambda x: x / 100., range_slider.value)

        smallest, largest, num_tasks = ray.global_state._job_length()
        diff = largest - smallest

        if time_opt.value == total_time_value:
            tasks = _truncated_task_profiles(start=smallest + diff * low,
                                             end=smallest + diff * high)
        elif time_opt.value == total_tasks_value:
            if range_slider.value[0] == 0:
                tasks = _truncated_task_profiles(num_tasks=int(num_tasks *
                                                               high),
                                                 fwd=True)
            else:
                tasks = _truncated_task_profiles(num_tasks=int(num_tasks *
                                                               (high - low)),
                                                 fwd=False)
        else:
            raise ValueError("Unexpected time value '{}'".format(
                time_opt.value))
        # Write trace to a JSON file
        print("Collected profiles for {} tasks.".format(len(tasks)))
        print("Dumping task profile data to {}, "
              "this might take a while...".format(json_tmp))
        ray.global_state.dump_catapult_trace(json_tmp,
                                             tasks,
                                             breakdowns=breakdown,
                                             obj_dep=obj_dep.value,
                                             task_dep=task_dep.value)
        print("Opening html file in browser...")

        trace_viewer_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            "../core/src/catapult_files/index.html")

        html_file_path = _get_temp_file_path(suffix=".html")
        json_file_path = _get_temp_file_path(suffix=".json")

        print("Pointing to {} named {}".format(json_tmp, json_file_path))
        shutil.copy(json_tmp, json_file_path)

        with open(trace_viewer_path) as f:
            data = f.read()

        # Replace the demo data path with our own
        # https://github.com/catapult-project/catapult/blob/
        # 33a9271eb3cf5caf925293ec6a4b47c94f1ac968/tracing/bin/index.html#L107
        data = data.replace("../test_data/big_trace.json", json_file_path)

        with open(html_file_path, "w+") as f:
            f.write(data)

        # Display the task trace within the Jupyter notebook
        clear_output(wait=True)
        print("To view fullscreen, open chrome://tracing in Google Chrome "
              "and load `{}`".format(json_tmp))
        display(IFrame(html_file_path, 900, 800))

    path_input.on_click(handle_submit)
Ejemplo n.º 4
0
def config_tab(backend):
    """The backend configuration widget.

    Args:
        backend (IBMQBackend | FakeBackend): The backend.

    Returns:
        grid: A GridBox widget.
    """
    status = backend.status().to_dict()
    config = backend.configuration().to_dict()

    config_dict = {**status, **config}

    upper_list = ["n_qubits"]

    if "quantum_volume" in config.keys():
        if config["quantum_volume"]:
            upper_list.append("quantum_volume")

    upper_list.extend([
        "operational",
        "status_msg",
        "pending_jobs",
        "backend_version",
        "basis_gates",
        "max_shots",
        "max_experiments",
    ])

    lower_list = list(set(config_dict.keys()).difference(upper_list))
    # Remove gates because they are in a different tab
    lower_list.remove("gates")
    # Look for hamiltonian
    if "hamiltonian" in lower_list:
        htex = config_dict["hamiltonian"]["h_latex"]
        config_dict["hamiltonian"] = "$$%s$$" % htex

    upper_str = "<table>"
    upper_str += """<style>
table {
    border-collapse: collapse;
    width: auto;
}

th, td {
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {background-color: #f6f6f6;}
</style>"""

    footer = "</table>"

    # Upper HBox widget data

    upper_str += "<tr><th>Property</th><th>Value</th></tr>"
    for key in upper_list:
        upper_str += "<tr><td><font style='font-weight:bold'>{}</font></td><td>{}</td></tr>".format(
            key,
            config_dict[key],
        )
    upper_str += footer

    upper_table = widgets.HTMLMath(value=upper_str,
                                   layout=widgets.Layout(width="100%",
                                                         grid_area="left"))

    image_widget = widgets.Output(layout=widgets.Layout(
        display="flex-inline",
        grid_area="right",
        padding="10px 10px 10px 10px",
        width="auto",
        max_height="325px",
        align_items="center",
    ))

    if not config["simulator"]:
        with image_widget:
            qubit_size = 24
            if config["n_qubits"] > 20:
                qubit_size = 34
            gate_map = plot_gate_map(backend, qubit_size=qubit_size)
            display(gate_map)
        plt.close(gate_map)

    lower_str = "<table>"
    lower_str += """<style>
table {
    border-collapse: collapse;
    width: auto;
}

th, td {
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {background-color: #f6f6f6;}
</style>"""
    lower_str += "<tr><th></th><th></th></tr>"
    for key in lower_list:
        if key != "name":
            lower_str += f"<tr><td>{key}</td><td>{config_dict[key]}</td></tr>"
    lower_str += footer

    lower_table = widgets.HTMLMath(value=lower_str,
                                   layout=widgets.Layout(width="auto",
                                                         grid_area="bottom"))

    grid = widgets.GridBox(
        children=[upper_table, image_widget, lower_table],
        layout=widgets.Layout(
            grid_template_rows="auto auto",
            grid_template_columns="31% 23% 23% 23%",
            grid_template_areas="""
                               "left right right right"
                               "bottom bottom bottom bottom"
                               """,
            grid_gap="0px 0px",
        ),
    )

    return grid
Ejemplo n.º 5
0
def obs_job_form(job: Job, mock=False):
    get_job_func = _get_job_func(job, mock)

    info = InfoComponent()
    job_header_id_label = widgets.HTML(value=f"<b>Job ID</b>")
    job_header_name_label = widgets.HTML(value=f"<b>Job Name</b>")
    job_header_progress_label = widgets.HTML(value=f"<b>Progress</b>")
    job_header_status_label = widgets.HTML(value=f"<b>Status</b>")
    job_progress_bar = widgets.IntProgress(value=job.progress, min=0, max=100)
    job_status_label = widgets.Label(job.status)
    job_id_label = widgets.Label(job.id)
    job_name_label = widgets.Label(job.name)
    job_cancel_button = widgets.Button(description="Cancel",
                                       icon="times-circle")
    job_cancel_button.on_click(
        _get_handle_cancel_button_clicked(job.id, info.message_func, mock))
    job_grid_box = widgets.GridBox(
        children=[
            job_header_id_label, job_header_name_label,
            job_header_progress_label, job_header_status_label,
            job_cancel_button, job_id_label, job_name_label, job_progress_bar,
            job_status_label,
            widgets.Label()
        ],
        layout=widgets.Layout(width='100%',
                              grid_template_rows='auto auto',
                              grid_template_columns='22% 21% 27% 10% 20%'))
    task_header_name_label = widgets.HTML(value=f"<b>Task</b>")
    task_header_progress_label = widgets.HTML(value=f"<b>Progress</b>")
    task_header_status_label = widgets.HTML(value=f"<b>Status</b>")

    task_gridbox_children = widgets.GridBox(
        children=[
            task_header_name_label, task_header_progress_label,
            task_header_status_label
        ],
        layout=widgets.Layout(grid_template_columns='43% 43% 14%'))

    info_displayed = []

    def _toggle_info_display(task_details, details_button, task_id, task_logs):
        task_details.clear_output()
        if details_button.icon == "chevron-circle-down":
            task_details.layout = {'border': '1px solid black'}
            info_displayed.append(task_id)
            with task_details:
                for log in task_logs:
                    print(log)
            details_button.icon = "chevron-circle-up"
        else:
            task_details.layout = {}
            info_displayed.remove(task_id)
            details_button.icon = "chevron-circle-down"

    def _get_details_button(_func, task_details, task_id, task_logs):
        details_button = widgets.Button(icon="chevron-circle-down",
                                        tooltip="Show processing details",
                                        layout=widgets.Layout(width='80%'))

        def _apply_func(b):
            _func(task_details, details_button, task_id, task_logs)

        details_button.on_click(_apply_func)
        if task_id not in info_displayed:
            details_button.icon = "chevron-circle-down"
            task_details.layout = {}
        else:
            details_button.icon = "chevron-circle-up"
            task_details.layout = {'border': '1px solid black'}
            for line in task_logs:
                task_details.append_stdout(f'{line}\n')

        return details_button

    def _get_tasks_gridbox(grid_job):
        gridbox_children = [task_gridbox_children]
        task_details_list = []
        for task_name in grid_job.tasks.names:
            progress = grid_job.tasks.get(task_name).progress
            status = grid_job.tasks.get(task_name).status
            progress = widgets.IntProgress(value=progress, min=0, max=100)
            status_label = widgets.Label(status)
            task_name_label = widgets.Label(task_name)
            task_details = widgets.Output()
            logs = grid_job.tasks.get(task_name).logs
            details_button = _get_details_button(_toggle_info_display,
                                                 task_details, task_name, logs)
            task_details_list.append(task_details)
            row_children = [
                task_name_label, progress, status_label, details_button
            ]
            row_box = widgets.GridBox(
                children=row_children,
                layout=widgets.Layout(grid_template_columns='43% 43% 9% 5%'))
            gridbox_children.append(row_box)
            gridbox_children.append(task_details)
        grid_box = widgets.GridBox(children=gridbox_children,
                                   layout=widgets.Layout(width='100%'))
        return grid_box

    tasks_gridbox = _get_tasks_gridbox(job)
    info = InfoComponent()
    job_monitor = widgets.VBox(
        [job_grid_box, tasks_gridbox,
         info.as_widget(100)])

    def monitor(progress_bar, status_bar, cancel_button):
        while job.status not in ['succeeded', 'cancelled', 'failed']:
            time.sleep(10)
            job_state = get_job_func(job, info.message_func)
            if job_state is not None:
                _update_job(job, job_state)
            progress_bar.value = job.progress
            status_bar.value = job.status
            grid_box = _get_tasks_gridbox(job)
            if job.status != 'new' and job.status != 'running':
                cancel_button.disabled = True
            job_monitor.children = ([
                job_grid_box, grid_box,
                info.as_widget(100)
            ])

    monitor_components = (job_progress_bar, job_status_label,
                          job_cancel_button)
    _monitor(monitor, job_monitor, monitor_components)
Ejemplo n.º 6
0
    def prepare_app(self):
        """
        Prepares one iteration of the user session.
        Makes sure that configurations are set correctly.
        """
        self.create_xi_grid()
        self.simulate_DSGE()
        ''' --- GUI plot/slider --- '''
        #https://pypi.org/project/jupyter-ui-poll/
        #        plt.rcParams['figure.dpi'] = 127
        #        fig = plt.figure()
        #        fig.set_figheight(3.6)
        #        ax1 = fig.add_subplot(221)
        #        ax2 = fig.add_subplot(222, sharex=ax1, sharey=ax1)
        #        ax3 = fig.add_subplot(223, sharex=ax1, sharey=ax1)
        #        ax4 = fig.add_subplot(224, sharex=ax1, sharey=ax1)
        #        fig.suptitle('IRFs to an shock to TFP', fontsize=12, fontweight='bold')
        #        plt.subplots_adjust(left=0.07, bottom=0.19, hspace=0.42)
        #        self.x_axis_points = list(range(1,self.n_irfs_periods+1))
        #        ax3.set_xlabel('Period')
        #        ax4.set_xlabel('Period')
        #        ax1.title.set_text('dy')
        #        ax2.title.set_text('dc')
        #        ax3.title.set_text('labobs')
        #        ax4.title.set_text('dw')
        #        ax1.axhline(y=0, color='k', linestyle='dashed')
        #        ax2.axhline(y=0, color='k', linestyle='dashed')
        #        ax3.axhline(y=0, color='k', linestyle='dashed')
        #        ax4.axhline(y=0, color='k', linestyle='dashed')
        #        ax1.set_ylim([-0.1, 0.1])
        #        ax2.set_ylim([-0.1, 0.1])
        #        ax3.set_ylim([-0.1, 0.1])
        #        ax4.set_ylim([-0.1, 0.1])
        #        #fig.text(0.1, 0.01, "$\it{Please\ select\ the\ most\ realistic\ hyperparameter\ configuration. }$")

        plt.rcParams['figure.dpi'] = 135
        fig = plt.figure()
        fig.set_figheight(3.9)
        ax1 = fig.add_subplot(221)
        ax2 = fig.add_subplot(222, sharex=ax1)
        ax3 = fig.add_subplot(223, sharex=ax1)
        ax4 = fig.add_subplot(224, sharex=ax1)
        fig.suptitle('IRFs to an shock to TFP', fontsize=12, fontweight='bold')
        plt.subplots_adjust(left=0.07, bottom=0.19, hspace=0.42)
        self.x_axis_points = list(range(1, self.n_irfs_periods + 1))
        ax3.set_xlabel('Period')
        ax4.set_xlabel('Period')
        ax1.title.set_text('dy')
        ax2.title.set_text('dc')
        ax3.title.set_text('labobs')
        ax4.title.set_text('dw')
        ax1.axhline(y=0, color='k', linestyle='dashed')
        ax2.axhline(y=0, color='k', linestyle='dashed')
        ax3.axhline(y=0, color='k', linestyle='dashed')
        ax4.axhline(y=0, color='k', linestyle='dashed')
        #fig.text(0.1, 0.01, "$\it{Please\ select\ the\ most\ realistic\ hyperparameter\ configuration. }$")

        #l1 = ax1.errorbar(self.x_axis_points, np.mean(self.irf(0,'dy'),axis=0), np.std(self.irf(0,'dy'),axis=0), elinewidth=0.7, label='IRF_dy')
        #l2 = ax2.errorbar(self.x_axis_points, np.mean(self.irf(0,'dc'),axis=0), np.std(self.irf(0,'dc'),axis=0), elinewidth=0.7, label='IRF_dc')
        #l3 = ax3.errorbar(self.x_axis_points, np.mean(self.irf(0,'labobs'),axis=0), np.std(self.irf(0,'labobs'),axis=0), elinewidth=0.7, label='IRF_labobs')
        #l4 = ax4.errorbar(self.x_axis_points, np.mean(self.irf(0,'dw'),axis=0), np.std(self.irf(0,'dw'),axis=0), elinewidth=0.7, label='IRF_dw')

        ax1.set_ylim([-0.2 + 0.1, 0.6 + 0.1])
        ax2.set_ylim([-0.1 + 0.1, 0.3 + 0.1])
        ax3.set_ylim([-0.3 + 0.1, 0.1 + 0.1])
        ax4.set_ylim([-0.05 + 0.1, 0.15 + 0.1])
        ax1.set_yticks([-0.2, 0, 0.2, 0.4, 0.6])
        ax2.set_yticks([-0.1, 0, 0.1, 0.2, 0.3])
        ax3.set_yticks([-0.3, 0.2, -0.1, 0, 0.1])
        ax4.set_yticks([-0.05, 0, 0.05, 0.1, 0.15])

        l1, = ax1.plot(self.x_axis_points,
                       np.mean(self.irf(0, 'dy'), axis=0),
                       lw=2)
        l2, = ax2.plot(self.x_axis_points,
                       np.mean(self.irf(0, 'dc'), axis=0),
                       lw=2)
        l3, = ax3.plot(self.x_axis_points,
                       np.mean(self.irf(0, 'labobs'), axis=0),
                       lw=2)
        l4, = ax4.plot(self.x_axis_points,
                       np.mean(self.irf(0, 'dw'), axis=0),
                       lw=2)

        plt.draw()
        slider = widgets.IntSlider(min=1,
                                   max=self.user_feedback_grid_size,
                                   step=1,
                                   description='Slider: ',
                                   value=1,
                                   continuous_update=False,
                                   readout=False,
                                   layout=widgets.Layout(width='60%',
                                                         height='80px',
                                                         position='right'))
        button = widgets.Button(description='Confirm',
                                icon='fa-check',
                                button_style='success',
                                layout=Layout(width='90px'))

        def confirm(event):
            typed_value = int(slider.value)
            self.user_feedback = self.current_xi_grid[(int(typed_value) -
                                                       1), :]
            self.user_feedback_was_given = True
            plt.close('all')

        button.on_click(confirm)
        plt.show(block=False)
        return button, slider, fig, l1, l2, l3, l4
Ejemplo n.º 7
0
class TSTools(object):

    def __init__(self):

        TSTools.band_index2 = 4
        TSTools.pyccd_flag2 = False
        TSTools.minv = 0
        TSTools.maxv = 6000
        TSTools.b1 = 'SWIR1'
        TSTools.b2 = 'NIR'
        TSTools.b3 = 'RED'


    ####### Starting Variables #######

    pyccd_flag2 = False
    current_band = ''
    band_index2 = 4
    click_col = ''
    point_color = ['#43a2ca']
    click_df = pd.DataFrame()
    click_geojson = ''
    PyCCDdf = pd.DataFrame()
    results = ''
    band_list = ['BLUE', 'GREEN', 'RED', 'NIR', 'SWIR1', 'SWIR2', 'BRIGHTNESS',
                 'GREENNESS', 'WETNESS', 'NDVI', 'NDFI', 'EVI', 'GV', 'Shade',
                 'NPV', 'Soil', ]
    doy_range = [1, 365]
    step = 1  #in years


    ###### Widgets ######
    ylim2 = plots.make_range_slider([0, 4000], -10000, 10000, 500, 'YLim:')
    xlim2 = plots.make_range_slider([2000, 2020], 1984, 2020, 1, 'XLim:')

    band_selector2 = plots.make_drop('SWIR1', band_list, 'Select band')
    image_band_1 = plots.make_drop('SWIR1', band_list, 'Red:')
    image_band_2 = plots.make_drop('NIR', band_list, 'Green:')
    image_band_3 = plots.make_drop('RED', band_list, 'Blue:')

    # Checkbox
    color_check = plots.make_checkbox(False, 'Color DOY', False)

    stretch_min = plots.make_text_float(0, 0, 'Min:')
    stretch_max = plots.make_text_float(6000, 6000, 'Max:')

    # Buttons
    pyccd_button2 = plots.make_button(False, 'Run PyCCD 2', icon='')
    toggle_pyccd_button2 = plots.make_button(False, 'Clear PyCCD', icon='')
    clear_layers = plots.make_button(False, 'Clear Map', icon='')

    # HTML
    hover_label = plots.make_html('Test Value')

    ###### Plots ######

    # Scales
    # Dates
    lc1_x2 = plots.make_bq_scale('date', datetime.date(xlim2.value[0], 2, 1), 
                                 datetime.date(xlim2.value[1], 1, 1))

    # DOY
    lc1_x3 = plots.make_bq_scale('linear', 0, 365)

    # Reflectance
    lc2_y2 = plots.make_bq_scale('linear', ylim2.value[0], ylim2.value[1])

    # plots
    lc3 = plots.make_bq_plot('scatter', [], [], {'x': lc1_x2, 'y': lc2_y2}, 
                             [1, 1],
                             {'click': 'select', 'hover': 'tooltip'},
                             {'opacity': 1.0, 'fill': 'DarkOrange', 
                              'stroke': 'Red'},
                             {'opacity': 0.5}, display_legend=True, 
                             labels=['Clicked point'])

    lc6 = plots.make_bq_plot('lines', [], [], {'x': lc1_x2, 'y': lc2_y2},
                             [1, 1], {}, {}, {}, colors=['black'],
                             stroke_width=3, labels=['PyCCD Model'],
                             display_legend=False)

    lc7 = plots.make_bq_plot('scatter', [], [], {'x': lc1_x2, 'y': lc2_y2},
                             [1, 1], {}, {}, {}, labels=['Model Endpoint'],
                             colors=['red'], marker='triangle-up')

    lc8 = plots.make_bq_plot('scatter', [], [], {'x': lc1_x3, 'y': lc2_y2},
                             [1, 1],
                             {'click': 'select', 'hover': 'tooltip'},
                             {'opacity': 1.0, 'fill': 'DarkOrange',
                              'stroke': 'Red'},
                             {'opacity': 0.5}, display_legend=True,
                             labels=['Clicked point'])

    # Axis
    x_ax2 = plots.make_bq_axis('Date', lc1_x2, num_ticks=6, tick_format='%Y',
                               orientation='horizontal')
    x_ax3 = plots.make_bq_axis('DOY', lc1_x3, num_ticks=6,
                               orientation='horizontal')

    y_ay2 = plots.make_bq_axis('SWIR1', lc2_y2, orientation='vertical')

    # Figures
    fig2 = plots.make_bq_figure([lc3, lc6, lc7], [x_ax2, y_ay2],
                                {'height': '300px', 'width': '100%'},
                                'Clicked TS')
    fig3 = plots.make_bq_figure([lc8], [x_ax3, y_ay2], {'height': '300px',
                                'width': '100%'}, 'Clicked DOY')

    ###### Functions ######

    # Change y axis for the clicked point
    def change_yaxis2(value):

        TSTools.lc2_y2.min = TSTools.ylim2.value[0]
        TSTools.lc2_y2.max = TSTools.ylim2.value[1]

    # Change x axis for the clicked point
    def change_xaxis2(value):

        TSTools.lc1_x2.min = datetime.date(TSTools.xlim2.value[0], 2, 1)
        TSTools.lc1_x2.max = datetime.date(TSTools.xlim2.value[1], 2, 1)

    # Display date of observation when hovering on scatterplot
    def hover_event(self, target):

        TSTools.hover_label.value = str(target['data']['x'])

    # Functions for changing image stretch
    def change_image_band1(change):
        new_band = change['new']
        TSTools.b1 = new_band

    def change_image_band2(change):
        new_band = change['new']
        TSTools.b2 = new_band

    def change_image_band3(change):
        new_band = change['new']
        TSTools.b3 = new_band

   # Band selection for clicked point
    def on_band_selection2(change):
        new_band = change['new']
        band_index = change['owner'].index
        TSTools.band_index2 = band_index
        TSTools.lc3.x = TSTools.click_df['datetime']
        TSTools.lc3.y = TSTools.click_df[new_band]
        TSTools.plot_ts(TSTools.lc3, 'ts')
        TSTools.plot_ts(TSTools.lc8, 'doy')
        TSTools.y_ay2.label = new_band
        if TSTools.pyccd_flag2:
            TSTools.do_pyccd2(0)

    # Clear everything on map
    def clear_map(b):
        lft.clear_map(TSTools.m, streets=True)

    def add_image2(self, target):
        m = TSTools.m
        df = TSTools.click_df
        current_band = TSTools.band_list[TSTools.band_index2]
        sample_col = TSTools.click_col
        stretch_min = TSTools.stretch_min.value
        stretch_max = TSTools.stretch_max.value
        b1 = TSTools.b1
        b2 = TSTools.b2
        b3 = TSTools.b3
        lft.click_event(target, m, current_band, df, sample_col, stretch_min,
                        stretch_max, b1, b2, b3)

    # Plot ts for point
    def do_draw(self, action, geo_json):
        current_band = TSTools.band_list[TSTools.band_index2]
        doy_range = TSTools.doy_range
        _col, _df = utils.handle_draw(action, geo_json, current_band, 
                                      list(TSTools.xlim2.value), doy_range)
        TSTools.click_geojson = geo_json
        TSTools.click_df = _df
        TSTools.click_col = _col

        TSTools.plot_ts(TSTools.lc3, 'ts')
        TSTools.plot_ts(TSTools.lc8, 'doy')

    # Add time series data to plots
    def plot_ts(plot, plottype):
        df = TSTools.click_df

        if TSTools.color_check.value is True:
            color_marks = list(df['color'].values)
        else:
            color_marks = None

        band = TSTools.band_list[TSTools.band_index2]

        if plottype == 'ts':
            plots.add_plot_ts(df, plot, band=band, color_marks=color_marks)
        else:
            plots.add_plot_doy(df, plot, band=band, color_marks=color_marks)

        if TSTools.pyccd_flag2:
            TSTools.do_pyccd2(0)

    # Run pyccd for the clicked location
    def do_pyccd2(b):

        TSTools.pyccd_flag2 = True
        display_legend = TSTools.lc7.display_legend
        dfPyCCD = TSTools.click_df
        band_index = TSTools.band_index2
        TSTools.results = ccd_tools.run_pyccd(display_legend, dfPyCCD, band_index)
        if band_index > 5:
            TSTools.lc6.y = []
            TSTools.lc6.x = []
            TSTools.lc7.y = []
            TSTools.lc7.x = []
            TSTools.lc7.display_legend = False
            return
        else:
            ccd_tools.plot_pyccd(dfPyCCD, TSTools.results, band_index, (0, 4000),
                                 TSTools.lc6, TSTools.lc7)
            TSTools.lc7.display_legend = True


    # Clear pyccd results
    def clear_pyccd2(b):
        TSTools.lc6.x = []
        TSTools.lc7.y = []

    ####### Widget Interactions #######
    dc = ipyleaflet.DrawControl(marker={'shapeOptions': {'color': '#ff0000'}},
                                polygon={}, circle={}, circlemarker={},
                                polyline={})

    zoom = 5
    layout = widgets.Layout(width='50%')
    center = (3.3890701010382958, -67.32297252983098)
    m = lft.make_map(zoom, layout, center)

    # Display controls
    ylim2.observe(change_yaxis2)
    xlim2.observe(change_xaxis2)
    clear_layers.on_click(clear_map)
    band_selector2.observe(on_band_selection2, names='value')
    image_band_1.observe(change_image_band1, names='value')
    image_band_2.observe(change_image_band2, names='value')
    image_band_3.observe(change_image_band3, names='value')

    # pyccd
    pyccd_button2.on_click(do_pyccd2)
    toggle_pyccd_button2.on_click(clear_pyccd2)

    # Plots
    lc3.on_element_click(add_image2)
    lc3.tooltip = hover_label
    lc3.on_hover(hover_event)
    lc8.on_element_click(add_image2)
    lc8.tooltip = hover_label
    lc8.on_hover(hover_event)

    # Mapping
    measure = ipyleaflet.MeasureControl(position='bottomleft',
                                        active_color = 'orange',
                                        primary_length_unit = 'kilometers'
                                        )
    measure.completed_color = 'red'
    
    dc.on_draw(do_draw)
    m.add_control(dc)
    m.add_control(measure)
    m.add_control(ipyleaflet.LayersControl())
Ejemplo n.º 8
0
 def dataexchange_choose_revision(self, region_name='us-east-1'):
     self.dataexchange_list_revisions(region_name=region_name)
     self.r = widgets.RadioButtons(options=self.revisions.keys(),
                                   description='Revision: ',
                                   layout=widgets.Layout(width='100%'))
     return self.r
Ejemplo n.º 9
0
    def __init__(self,
                 description='Input image',
                 example_paths=None,
                 example_labels=None,
                 enable_upload=True,
                 enable_crop=True):
        assert example_paths is not None or enable_upload
        if example_paths is not None:
            example_paths = list(map(Path, example_paths))
            if example_labels is not None:
                assert len(example_paths) == len(example_labels)
            else:
                example_labels = [path.stem for path in example_paths]
        self.example_paths = example_paths
        self.example_labels = example_labels
        self.have_choice = example_paths is not None
        self.enable_upload = enable_upload
        self.enable_crop = enable_crop

        self.w_label = widgets.Label(value=description)

        if enable_upload:
            self.w_upload = widgets.FileUpload(accept='image/*',
                                               description='Upload an image')
        else:
            self.w_upload = None

        if example_paths is not None:
            self.ws_ex_images = [
                widgets.Image(width=150,
                              height=150,
                              value=open(path, 'rb').read())
                for path in example_paths
            ]
            self.t0 = self.ws_ex_images
            self.ws_ex_buttons = [
                widgets.Button(description=label, button_style='')
                for label in example_labels
            ]
            ws_stack_img_button = [
                widgets.VBox([i, b])
                for i, b in zip(self.ws_ex_images, self.ws_ex_buttons)
            ]
            self.w_img_choice = widgets.GridBox(
                ws_stack_img_button,
                layout=widgets.Layout(
                    grid_template_columns="repeat(5, 200px)"))
        else:
            self.ws_ex_images = None
            self.ws_ex_buttons = None
            self.w_img_choice = None

        self.w_show_img = widgets.Image(width=300, height=300)

        if enable_crop:
            self.w_choose_crop = widgets.Checkbox(value=False,
                                                  description="Crop image?")
            self.w_crop_slider_x = widgets.IntRangeSlider(min=0,
                                                          max=0,
                                                          value=(0, 0),
                                                          step=1,
                                                          description='X Crop')
            self.w_crop_slider_y = widgets.IntRangeSlider(min=0,
                                                          max=0,
                                                          value=(0, 0),
                                                          step=1,
                                                          description='Y Crop')
            self.w_crop_slider_x.layout.visibility = 'hidden'
            self.w_crop_slider_y.layout.visibility = 'hidden'
            self.w_crop = widgets.HBox([
                self.w_choose_crop, self.w_crop_slider_x, self.w_crop_slider_y
            ])
        else:
            self.w_choose_crop = None
            self.w_crop_slider_x = None
            self.w_crop_slider_y = None
            self.w_crop = None

        all_widgets = [
            self.w_label, self.w_upload, self.w_img_choice, self.w_show_img,
            self.w_crop
        ]
        all_widgets = [w for w in all_widgets if w is not None]
        self.value = None
        self.stored = None
        if self.have_choice:
            for i in range(len(self.ws_ex_buttons)):
                self.ws_ex_buttons[i].id = i
                self.ws_ex_buttons[i].on_click(self.observe_choice)
            self.observe_choice(self.ws_ex_buttons[0])
        if self.enable_upload:
            self.w_upload.observe(self.observe_upload)
        if self.enable_crop:
            self.w_choose_crop.observe(self.observe_choose_crop)
            self.w_crop_slider_y.observe(self.observe_crop)
            self.w_crop_slider_x.observe(self.observe_crop)
        super().__init__(all_widgets)
Ejemplo n.º 10
0
    def _ipython_display_(self):
        from IPython.display import display
        from IPython.display import clear_output
        import ipywidgets as widgets
        from lux.core.frame import LuxDataFrame

        series_repr = super(LuxSeries, self).__repr__()

        ldf = LuxDataFrame(self)

        # Default column name 0 causes errors
        if self.name is None:
            ldf = ldf.rename(columns={0: " "})
        self._ldf = ldf

        try:
            # Ignore recommendations when Series a results of:
            # 1) Values of the series are of dtype objects (df.dtypes)
            is_dtype_series = (
                all(isinstance(val, np.dtype) for val in self.values) and len(self.values) != 0
            )
            # 2) Mixed type, often a result of a "row" acting as a series (df.iterrows, df.iloc[0])
            # Tolerant for NaNs + 1 type
            mixed_dtype = len(set(type(val) for val in self.values)) >= 2
            if ldf._pandas_only or is_dtype_series or mixed_dtype:
                print(series_repr)
                ldf._pandas_only = False
            else:
                if not self.index.nlevels >= 2:
                    ldf.maintain_metadata()

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

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

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

                self._widget = ldf._widget
                self._recommendation = ldf._recommendation

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

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

                button.on_click(on_button_clicked)
                on_button_clicked(None)

        except (KeyboardInterrupt, SystemExit):
            raise
        except Exception:
            warnings.warn(
                "\nUnexpected error in rendering Lux widget and recommendations. "
                "Falling back to Pandas display.\n"
                "Please report the following issue on Github: https://github.com/lux-org/lux/issues \n",
                stacklevel=2,
            )
            warnings.warn(traceback.format_exc())
            display(self.to_pandas())
    'seaborn-ticks',
    'seaborn-poster',
    'seaborn-pastel',
    'fast',
    'seaborn-darkgrid',
    'seaborn-bright',
    'Solarize_Light2',
    'seaborn-dark-palette',
    'grayscale',
    'seaborn-muted',
    'dark_background',
    'seaborn-deep',
    'seaborn-paper',
    'classic'
]

output_formats = {
    'Plot vertical bar': 'plot_bar',
    'Plot horisontal bar': 'plot_barh',
    'Plot vertical bar, stacked': 'plot_bar_stacked',
    'Plot horisontal bar, stacked': 'plot_barh_stacked',
    'Plot line': 'plot_line',
    'Plot stacked line': 'plot_line_stacked',
    # 'Chart ': 'chart',
    'Table': 'table',
    'Pivot': 'pivot'
}

toggle_style = dict(icon='', layout=widgets.Layout(width='100px', left='0'))
drop_style = dict(layout=widgets.Layout(width='260px'))
Ejemplo n.º 12
0
    def render_quiz(self, question, options, correct_ans, b_multiple_correct):
        selected_options = []
        
        # check if text input is needed for this quiz
        b_text_input = len(options) == 1
        
        if b_text_input:
            correct_ans = self.parse_text_answer(correct_ans)
        
        def on_submit(change):
            if b_text_input:
                answer = options_widget.value
                answer = answer.strip()
                del selected_options[:]
                selected_options.append(answer)
            c = set(correct_ans)
            s = set(selected_options)
            if len(s) == 0:
                print('Please choose options!')
            elif s == c:
                print(self.get_correct_response())
            elif s < c:
                if b_text_input:
                    alt = ', '.join(list(c-s))
                    print(f"That's right!\n(Alternatively, {alt} is/are also correct!)")
                else:
                    print(f'{len(s)} out of {len(c)} correct! Keep trying...')
            elif s.isdisjoint(c):
                print('Wrong answer! Please try again.')                
            else:
                print(f'{len(s&c)} out of {len(c)} correct and {len(s-c)} wrong!')
        
        def on_change_checkbox(change):
            checked = change['owner'].value
            text = change['owner'].description
            if checked:
                selected_options.append(text)
            else:
                if len(selected_options) > 0:
                    # initially this list will be empty
                    selected_options.remove(text)
        
        def on_change_radiobutton(change):
            old = change['old']
            new = change['new']
            if len(selected_options) > 0:
                # initially this list will be empty
                selected_options.remove(old)  
            selected_options.append(new)

        layout_style = layout=widgets.Layout(width='80%')
        heading_widget = widgets.HTML(value='<h3><b>Quiz Question</b></h3>')
        question_widget = widgets.HTMLMath(value=f'<b>{question}</b>')
        submit_button_widget = widgets.Button(description='Submit', button_style='info')
        submit_button_widget.on_click(on_submit)
        
        if b_multiple_correct:
            checkboxes = [widgets.Checkbox(value=False, description=option, layout=layout_style) for option in options]
            options_widget = widgets.VBox(checkboxes)
            # add event listener
            for checkbox in checkboxes:
                checkbox.observe(on_change_checkbox, names='value')
        elif b_text_input:
            options_widget = widgets.Text(placeholder='Type your answer')
        else:
            options_widget = widgets.RadioButtons(options=options, value=None, layout=layout_style)
            options_widget.observe(on_change_radiobutton, names='value')
        
        # put the widgets in verticle box holder
        quiz_widget = widgets.VBox([heading_widget, question_widget, options_widget, submit_button_widget])
        
        display(quiz_widget)
Ejemplo n.º 13
0
    html += "<tr><th>{}</th><th></tr>".format(circuit.name)
    html += "<tr><td>Width</td><td>{}</td></tr>".format(circuit.width())
    html += "<tr><td>Depth</td><td>{}</td></tr>".format(circuit.depth())
    html += "<tr><td>Total Gates</td><td>{}</td></tr>".format(sum(ops.values()))
    html += "<tr><td>Non-local Gates</td><td>{}</td></tr>".format(num_nl)
    html += "</table>"

    out_wid = wid.HTML(html)
    return out_wid


head_style = 'font-family: IBM Plex Sans, Arial, Helvetica, sans-serif;' \
             ' font-size: 20px; font-weight: medium;'

property_label = wid.HTML("<p style='{}'>Circuit Properties</p>".format(head_style),
                          layout=wid.Layout(margin='0px 0px 10px 0px'))


def properties_widget(circuit: QuantumCircuit) -> wid.VBox:
    """Create a HTML table widget with header for a given quantum circuit.

    Args:
        circuit: Input quantum circuit.

    Returns:
        Output widget.
    """
    properties = wid.VBox(children=[property_label,
                                    circuit_data_table(circuit)],
                          layout=wid.Layout(width='40%',
                                            height='auto'))
Ejemplo n.º 14
0
    def db_reset(self, line):
        host = self.graph_notebook_config.host
        port = self.graph_notebook_config.port
        ssl = self.graph_notebook_config.ssl

        logger.info(f'calling system endpoint {host}')
        parser = argparse.ArgumentParser()
        parser.add_argument('-g',
                            '--generate-token',
                            action='store_true',
                            help='generate token for database reset')
        parser.add_argument('-t',
                            '--token',
                            nargs=1,
                            default='',
                            help='perform database reset with given token')
        parser.add_argument('-y',
                            '--yes',
                            action='store_true',
                            help='skip the prompt and perform database reset')
        args = parser.parse_args(line.split())
        generate_token = args.generate_token
        skip_prompt = args.yes
        request_generator = create_request_generator(
            self.graph_notebook_config.auth_mode,
            self.graph_notebook_config.iam_credentials_provider_type)
        logger.info(
            f'used credentials_provider_mode={self.graph_notebook_config.iam_credentials_provider_type.name} and auth_mode={self.graph_notebook_config.auth_mode.name} to make system request'
        )
        if generate_token is False and args.token == '':
            if skip_prompt:
                res = initiate_database_reset(host, port, ssl,
                                              request_generator)
                token = res['payload']['token']
                res = perform_database_reset(token, host, port, ssl,
                                             request_generator)
                logger.info(f'got the response {res}')
                return res

            output = widgets.Output()
            source = 'Are you sure you want to delete all the data in your cluster?'
            label = widgets.Label(source)
            text_hbox = widgets.HBox([label])
            check_box = widgets.Checkbox(
                value=False,
                disabled=False,
                indent=False,
                description=
                'I acknowledge that upon deletion the cluster data will no longer be available.',
                layout=widgets.Layout(width='600px', margin='5px 5px 5px 5px'))
            button_delete = widgets.Button(description="Delete")
            button_cancel = widgets.Button(description="Cancel")
            button_hbox = widgets.HBox([button_delete, button_cancel])

            display(text_hbox, check_box, button_hbox, output)

            def on_button_delete_clicked(b):
                result = initiate_database_reset(host, port, ssl,
                                                 request_generator)

                text_hbox.close()
                check_box.close()
                button_delete.close()
                button_cancel.close()
                button_hbox.close()

                if not check_box.value:
                    with output:
                        print('Checkbox is not checked.')
                    return
                token = result['payload']['token']
                if token == "":
                    with output:
                        print('Failed to get token.')
                        print(result)
                    return

                result = perform_database_reset(token, host, port, ssl,
                                                request_generator)

                if 'status' not in result or result['status'] != '200 OK':
                    with output:
                        print(
                            'Database reset failed, please try the operation again or reboot the cluster.'
                        )
                        print(result)
                        logger.error(result)
                    return

                retry = 10
                poll_interval = 5
                interval_output = widgets.Output()
                job_status_output = widgets.Output()
                status_hbox = widgets.HBox([interval_output])
                vbox = widgets.VBox([status_hbox, job_status_output])
                display(vbox)

                last_poll_time = time.time()
                while retry > 0:
                    time_elapsed = int(time.time() - last_poll_time)
                    time_remaining = poll_interval - time_elapsed
                    interval_output.clear_output()
                    if time_elapsed > poll_interval:
                        with interval_output:
                            print('checking status...')
                        job_status_output.clear_output()
                        with job_status_output:
                            display_html(HTML(loading_wheel_html))
                        try:
                            retry -= 1
                            interval_check_response = get_status(
                                host, port, ssl, request_generator)
                        except Exception as e:
                            # Exception is expected when database is resetting, continue waiting
                            with job_status_output:
                                last_poll_time = time.time()
                                time.sleep(1)
                                continue
                        job_status_output.clear_output()
                        with job_status_output:
                            if interval_check_response["status"] == 'healthy':
                                interval_output.close()
                                print('Database has been reset.')
                                return
                        last_poll_time = time.time()
                    else:
                        with interval_output:
                            print(
                                f'checking status in {time_remaining} seconds')
                    time.sleep(1)
                with output:
                    print(result)
                    if interval_check_response["status"] != 'healthy':
                        print(
                            "Could not retrieve the status of the reset operation within the allotted time. "
                            "If the database is not healthy after 1 min, please try the operation again or "
                            "reboot the cluster.")

            def on_button_cancel_clicked(b):
                text_hbox.close()
                check_box.close()
                button_delete.close()
                button_cancel.close()
                button_hbox.close()
                with output:
                    print('Database reset operation has been canceled.')

            button_delete.on_click(on_button_delete_clicked)
            button_cancel.on_click(on_button_cancel_clicked)
            return
        elif generate_token:
            res = initiate_database_reset(host, port, ssl, request_generator)
        else:
            # args.token is an array of a single string, e.g., args.token=['ade-23-c23'], use index 0 to take the string
            res = perform_database_reset(args.token[0], host, port, ssl,
                                         request_generator)

        logger.info(f'got the response {res}')
        return res
Ejemplo n.º 15
0
    def __join_button_clicked(self, b):
        with self.table_join_out:
            clear_output()

            new_join_button = widgets.Button(
                description="JOIN",
                icon='',
                layout=widgets.Layout(width='100px'),
                style=widgets.ButtonStyle(button_color='#E58975'))

            new_remove_button = widgets.Button(
                description="REMOVE",
                icon='',
                layout=widgets.Layout(width='170px'),
                style=widgets.ButtonStyle(button_color='#E58975'))
            new_join_button.on_click(self.__join_button_clicked)
            new_remove_button.on_click(self.__join_button_clicked)

            if b.description == "JOIN":
                self.join_button.layout.visibility = 'hidden'
                new_tables_dropdown = widgets.Dropdown(
                    options=self.table_list,
                    description='Table',
                    layout=widgets.Layout(width='600px'),
                    style={'description_width': 'initial'})
                widgets.interactive_output(self.__trigger_column_widget,
                                           {'table': new_tables_dropdown})
                self.list_of_tables.append(
                    new_tables_dropdown)  #add into list of table object
                #######use a Int widget to store the index
                save_index = widgets.IntText(value=len(self.list_of_tables) -
                                             1,
                                             description='Any:')

                on_condition = widgets.interactive_output(
                    self.get_on_field, {
                        'dropdown1': self.list_of_tables[-2],
                        'dropdown2': self.list_of_tables[-1],
                        'index': save_index
                    })
                on_object_ui = widgets.HBox([
                    widgets.Box([self.list_of_tables[-1]],
                                layout=widgets.Layout(width="40%")),
                    widgets.Box([on_condition],
                                layout=widgets.Layout(top="-6px", width="60%"))
                ])

                self.list_of_on_object.append(
                    on_object_ui)  #add into on condition object
            elif b.description == "REMOVE":
                self.list_of_tables.pop()
                self.list_of_on_object.pop()
            #### change the column field list
            string = f"(table_name='{self.list_of_tables[0].value}' "
            for x in range(1, len(self.list_of_tables)):
                string = string + f"OR table_name='{self.list_of_tables[x].value}' "
            string = string + ")"
            self.table_text.value = string
            ####

            for x in self.list_of_on_object[:-1]:
                display(x)
            if len(self.list_of_on_object) != 0:
                self.table_object = widgets.HBox([
                    self.list_of_on_object[-1], new_join_button,
                    new_remove_button
                ])
                display(self.table_object)
            else:
                self.join_button.layout.visibility = 'visible'
            self.view_query_button.click()  #################################
Ejemplo n.º 16
0
def Print_commands(expt):
    '''
    Print a list of commands from a log file. Add the scan numbers at the end of each line.

    Parameters
    ----------
    expt : object
        object from the class Experiment
    '''


    try:
        default_value = expt.default_wm_log_file
    except:
        default_value = expt.list_logs_files[0]

    w_select_log = widgets.Dropdown(
                 options=expt.list_logs_files,
                 value=default_value,
                 layout=widgets.Layout(width='400px'),
                 style={'description_width': 'initial'})

    def on_button_select_clicked(b):

        # Keep history of the chosen file
        expt.default_wm_log_file = w_select_log.value

        pathToFile = expt.logs_dir+w_select_log.value

        # Extract the formatted commands
        rlog_lines = Extract_commands(pathToFile)
        
        # Convert numpy.ndarray into a simple array
        # (ipywidget does not like numpy array as options)
        
        rlog_lines = [str(rlog_line) for rlog_line in rlog_lines]
        
        w_select_commands = widgets.SelectMultiple(options=rlog_lines,rows=30,
                                                   layout=widgets.Layout(width='800px'),
                                                   value=[str(rlog_lines[-1])])

        display(w_select_commands)

        def on_button_insert_commands_clicked(b):
            """
            Display selected commands and scan numbers in a markdown cell.
            """ 

            code = '```python\n'+''.join(w_select_commands.value)+'```'

            Utils.Create_cell(code=code, position ='above', celltype='markdown', is_print=True)

            Utils.Delete_current_cell()

            Utils.Create_cell(code='FE.Action.Choose(expt)',
                        position ='at_bottom', celltype='code', is_print=False)  

        # Click to insert a script
        button_insert_commands = widgets.Button(description="Insert commands")
        button_insert_commands.on_click(on_button_insert_commands_clicked)

        display(button_insert_commands)

    button = widgets.Button(description="Select log")
    button.on_click(on_button_select_clicked)

    display(widgets.HBox([w_select_log, button]))  
Ejemplo n.º 17
0
 def __init__(self):
     self.selected_on_field = [None] * 20  # hardcode max join tables
     self.table_lst = []  # dropdown items for a table
     self.list_of_tables = []  # list of table objects
     self.list_of_on_object = []  # list of on_condition objects
     self.count = 0
     self.count_num_clicks = 0
     table_join_condition = {}
     self.column_type_dictionary = {}
     self.description_dictionary = {}
     self.condition_list = []
     self.query_body = ""
     self.table_join_out = widgets.Output(layout=widgets.Layout(
         width='100%'))
     self.where_condition_out = widgets.Output(layout=widgets.Layout(
         width='100%'))
     self.query_out = widgets.Output(layout=widgets.Layout(width='100%'))
     self.query_out.layout.border = "1px solid green"
     self.edit_out = widgets.Output(layout=widgets.Layout(width='100%'))
     self.result = widgets.Output(layout=widgets.Layout(width='100%'))
     self.view_query_button = widgets.Button(
         description="View Query",
         layout=widgets.Layout(width='100px'),
         style=widgets.ButtonStyle(button_color='#E58975'))
     self.view_query_button.on_click(self.__display_query)
     display(widgets.HBox([self.view_query_button, self.query_out]))
     self.clear_button = widgets.Button(
         description="CLEAR",
         layout=widgets.Layout(flex='1 1 auto', width='auto'),
         style=widgets.ButtonStyle(button_color='#E58975'))
     self.clear_button.on_click(self.__clear_button_clicked)
     self.search_button = widgets.Button(
         description="SEARCH",
         layout=widgets.Layout(flex='1 1 auto', width='auto'),
         style=widgets.ButtonStyle(button_color='#E58975'))
     self.search_button.on_click(self.__search_button_clicked)
     self.edit_button = widgets.Button(
         description="EDIT QUERY",
         layout=widgets.Layout(flex='1 1 auto', width='auto'),
         style=widgets.ButtonStyle(button_color='#E58975'))
     self.edit_button.on_click(self.__edit_button_clicked)
     self.tmp_query = widgets.Textarea(description="",
                                       value="",
                                       layout=widgets.Layout(
                                           flex='1 1 auto',
                                           width='auto',
                                           height='100%'))
     self.tmp_query.layout.visibility = 'hidden'
     self.__get_service()
     display(
         widgets.HBox(
             [self.clear_button, self.edit_button, self.search_button]))
     display(
         widgets.VBox([self.tmp_query],
                      layout=widgets.Layout(height='200px')))
     display(self.result)
Ejemplo n.º 18
0
def Create_form():
    '''Display the widget for creation and printing of the experiment form.'''    
    
    short_layout = widgets.Layout(width='200px', height='40px')
    medium_layout = widgets.Layout(width='300px', height='40px')
    long_layout = widgets.Layout(width='800px', height='40px')
    style = {'description_width': 'initial'}

    title = widgets.Text(
            placeholder='Title',
            description='Experiment title:',
            layout=long_layout,
            style=style)

    number = widgets.Text(
            placeholder='Number',
            description='Experiment number:',
            layout=medium_layout,
            style=style)

    ttype = widgets.Text(
            placeholder='Proposal, Commissioning, ...',
            description='Type:',
            layout=medium_layout,
            style=style)

    safety = widgets.Dropdown(
            options=['Green', 'Yellow', 'Red'],
            value='Yellow',
            description='Safety:',
            layout=widgets.Layout(width='150px'),
            style=style)

    date  = widgets.Text(
            placeholder='DD/MM/YYYY - DD/MM/YYYY',
            description='Date:',
            layout=medium_layout,
            style=style)

    proposer = widgets.Text(
               placeholder='Name',
               description='Main proposer:',
               layout=short_layout,
               style=style)

    contact = widgets.Text(
               placeholder='Name',
               description='Local contact:',
               layout=medium_layout,
               style=style)

    users = widgets.Text(
            placeholder='Names',
            description='Users (on site):',
            layout=long_layout,
            style=style)

    recording = widgets.Text(
               placeholder='Full path to the recording directory',
               description='Recording directory:',
               layout=long_layout,
               style=style)


    current = widgets.Text(
               placeholder='450 mA, 500 mA, ...',
               description='Current:',
               layout=medium_layout,
               style=style)

    mode     = widgets.Text(
               placeholder='Hybrid, Top-up, ...',
               description='Mode:',
               layout=medium_layout,
               style=style)

    dcm   = widgets.Dropdown(
            options=['Si111', 'InSb', 'Not Used'],
            value='Si111',
            description='DCM:',
            layout=widgets.Layout(width='150px'),
            style=style)

    mgm   = widgets.Dropdown(
            options=['In use', 'Not used'],
            value='Not used',
            description='MGM:',
            layout=widgets.Layout(width='150px'),
            style=style)

    energy  = widgets.Text(
               placeholder='Value(s)',
               description='Energy (keV):',
               layout=medium_layout,
               style=style)

    energy_type = widgets.Dropdown(
                  options=['Fixed', 'Variable'],
                  value='Fixed',
                  description='Fixed/Variable energy:',
                  layout=widgets.Layout(width='300px'),
                  style=style)

    wavelength = widgets.Text(
                 placeholder='Value(s)',
                 description='Wavelength (nm):',
                 layout=medium_layout,
                 style=style)

    harmonic  =  widgets.Text(
                 placeholder='Value(s)',
                 description='Harmonic:',
                 layout=short_layout,
                 style=style)

    polarisation = widgets.Text(
                 placeholder='Value(s)',
                 value='LH',
                 description='Polarisation:',
                 layout=short_layout,
                 style=style)

    phase = widgets.Text(
                 placeholder='Value(s)',
                 value='0',
                 description='Phase (deg):',
                 layout=short_layout,
                 style=style)

    m1  = widgets.Dropdown(
          options=['M1-A Pt Track', 'M1-A B4C Track', 'M1-B (B4C)', 'No M1'],
          value='M1-A Pt Track',
          description='M1:',
          layout=widgets.Layout(width='200px'),
          style=style)

    m2  = widgets.Dropdown(
          options=['M2 Pt Track', 'M2 B4C Track', 'No M2'],
          value='M2 Pt Track',
          description='M2:',
          layout=widgets.Layout(width='200px'),
          style=style)

    m3  = widgets.Dropdown(
          options=['M3 Pt Track', 'M3 B4C Track', 'No M3'],
          value='No M3',
          description='M3:',
          layout=widgets.Layout(width='200px'),
          style=style)

    m4  = widgets.Dropdown(
          options=['M4 Pt Track', 'M4 Si Track', 'No M4'],
          value='M4 Pt Track',
          description='M4:',
          layout=widgets.Layout(width='200px'),
          style=style)

    horizontal_foc = widgets.Checkbox(
                     value=True,
                     description='Horizontal focalisation:',
                     layout=long_layout,
                     style=style)

    vertical_foc = widgets.Checkbox(
                   value=True,
                   description='Vertical focalisation:',
                   layout=long_layout,
                   style=style)


    horizontal_size = widgets.Text(
                      placeholder='Value(s)',
                      description='Horizontal beamsize (mm):',
                      layout=medium_layout,
                      style=style)

    vertical_size   = widgets.Text(
                      placeholder='Value(s)',
                      description='Vertical beamsize (mm):',
                      layout=medium_layout,
                      style=style)

    mon1  = widgets.Text(
            placeholder='Empty or type of mon',
            description='mon1:',
            layout=short_layout,
            style=style)

    mon2  = widgets.Text(
            placeholder='Empty or type of mon',
            description='mon2:',
            layout=short_layout,
            style=style)

    mon3  = widgets.Text(
            placeholder='Empty or type of mon',
            description='mon3:',
            layout=short_layout,
            style=style)

    mon4  = widgets.Text(
            placeholder='Empty or type of mon',
            description='mon4:',
            layout=short_layout,
            style=style)

    detectors  = widgets.Textarea(
                 placeholder='Type of detectors',
                 description='Detectors:',
                 layout=long_layout,
                 style=style)

    remarks     = widgets.Textarea(
                 placeholder='Remarks',
                 description='Remarks:',
                 layout=long_layout,
                 style=style)

    display(title)
    display(widgets.HBox([date, number]))
    display(widgets.HBox([ttype, safety]))
    print('-'*100)
    display(widgets.HBox([proposer, contact]))
    display(users)
    print('-'*100)
    display(recording)
    print('\033[1m'+'Machine:')
    display(widgets.HBox([current, mode]))
    print('\033[1m'+'Optics:')
    display(widgets.HBox([dcm, mgm]))
    display(widgets.HBox([m1, m2, m3, m4]))
    print('\033[1m'+'Beam:')
    display(widgets.HBox([energy_type, energy, wavelength]))
    display(widgets.HBox([harmonic, polarisation, phase]))
    display(widgets.HBox([horizontal_foc, vertical_foc]))
    display(widgets.HBox([horizontal_size, vertical_size]))
    print('\033[1m'+'Monitors and XBPM:')
    display(widgets.HBox([mon1, mon2, mon3, mon4]))
    display(detectors)
    print('\033[1m'+'Remarks:')
    display(remarks)
    
    def on_button_clicked(b):
        
        txt = []
        txt.append('$\LARGE \\textbf{SIRIUS Beamline}:\\textbf{Experiment %s}$'%number.value)

        ########################################
        # Prepare the title in several parts
        # Avoid having a line larger than the page

        # Max number of characters allowed by line
        max_length = 70

       
        title_split = title.value.split(' ')
        title_blocks = [] 

        j=0
        for k in range(0,len(title_split)):
            title_part = ''
            for i in range(j,len(title_split)): 
                if len(title_part)<max_length:
                    title_part += title_split[i]+' '
                    j=j+1
                else:
                    break
            title_blocks.append(title_part)        

        for title_block in title_blocks:
            if title_block != '':        
                txt.append('$\Large \\color{red}{\\bf %s}$'%('\ '.join(title_block.split(' '))))

        
        # Former (simpler version), but allows line longer than the page width
        #ptitle = ('\ '.join(title.value.split(' ')))
        #txt.append('$\Large \\color{red}{\\bf %s}$'%ptitle)
        ########################################
        
        txt.append('* %s %s'%(ttype.description,ttype.value)+'\n'
                    +'* %s %s'%(safety.description,safety.value)+'\n'
                    +'* %s %s'%(date.description,date.value))

        txt.append('* %s %s'%(proposer.description,proposer.value)+'\n'
                    +'* %s %s'%(contact.description,contact.value)+'\n'
                    +'* %s %s'%(users.description,users.value)+'\n'
                    +'* %s %s'%(recording.description,recording.value))

        txt.append('* Machine:'+'\n'
                    +'\t * %s %s'%(current.description,current.value)+'\n'
                    +'\t * %s %s'%(mode.description,mode.value))

        txt.append('* Optics:'+'\n'
                    +'\t * %s %s'%(dcm.description,dcm.value)+'\n'
                    +'\t * %s %s'%(mgm.description,mgm.value)+'\n'
                    +'\t * %s %s'%(m1.description,m1.value)+'\n'
                    +'\t * %s %s'%(m2.description,m2.value)+'\n'
                    +'\t * %s %s'%(m3.description,m3.value)+'\n'
                    +'\t * %s %s'%(m4.description,m4.value))

        txt.append('* Beam:'+'\n'
                    +'\t * %s %s'%(energy_type.description,energy_type.value)+'\n'
                    +'\t * %s %s'%(energy.description,energy.value)+'\n'
                    +'\t * %s %s'%(wavelength.description,wavelength.value)+'\n'
                    +'\t * %s %s'%(harmonic.description,harmonic.value)+'\n'
                    +'\t * %s %s'%(polarisation.description,polarisation.value)+'\n'
                    +'\t * %s %s'%(phase.description,phase.value)+'\n'
                    +'\t * %s %s'%(horizontal_foc.description,horizontal_foc.value)+'\n'
                    +'\t * %s %s'%(vertical_foc.description,vertical_foc.value)+'\n'
                    +'\t * %s %s'%(horizontal_size.description,horizontal_size.value)+'\n'
                    +'\t * %s %s'%(vertical_size.description,vertical_size.value))

        txt.append('* Monitors and XBPM:'+'\n'
                    +'\t * %s %s'%(mon1.description,mon1.value)+'\n'
                    +'\t * %s %s'%(mon2.description,mon2.value)+'\n'
                    +'\t * %s %s'%(mon3.description,mon3.value)+'\n'
                    +'\t * %s %s'%(mon4.description,mon4.value)+'\n'
                    +'\t * %s %s'%(detectors.description,detectors.value))

        txt.append('* %s %s'%(remarks.description,remarks.value))

        txt.reverse()
        
        for elem in txt:
            Utils.Create_cell(code=elem, position ='below', celltype='markdown', is_print=True)
        
        Utils.Create_cell(code='# Experimental setup', position ='below', celltype='markdown', is_print=True)
        
        # Remove the widget when done
        Utils.Delete_current_cell()
        
    button = widgets.Button(description="Print form")
    out = widgets.Output()
    display(button,out)
    button.on_click(on_button_clicked)
Ejemplo n.º 19
0
def config_tab(backend: Union[IBMBackend, FakeBackend]) -> wid.GridBox:
    """The backend configuration widget.

    Args:
        backend: Display configuration widget for this backend.

    Returns:
        A widget containing backend configuration information.
    """
    status = backend.status().to_dict()
    config = backend.configuration().to_dict()
    next_resrv = get_next_reservation(backend)
    if next_resrv:
        reservation_str = "in {} ({}m)".format(
            duration_difference(next_resrv.start_datetime),
            next_resrv.duration)
    else:
        reservation_str = "-"

    config_dict = {**status, **config}
    config_dict["reservation"] = reservation_str

    upper_list = ["n_qubits"]

    if "quantum_volume" in config.keys():
        if config["quantum_volume"]:
            upper_list.append("quantum_volume")

    upper_list.extend([
        "operational",
        "status_msg",
        "pending_jobs",
        "reservation",
        "backend_version",
        "basis_gates",
        "max_shots",
        "max_experiments",
    ])

    lower_list = list(set(config_dict.keys()).difference(upper_list))
    # Remove gates because they are in a different tab
    lower_list.remove("gates")
    # Look for hamiltonian
    if "hamiltonian" in lower_list:
        htex = config_dict["hamiltonian"]["h_latex"]
        config_dict["hamiltonian"] = "$$%s$$" % htex

    upper_str = "<table>"
    upper_str += """<style>
table {
    border-collapse: collapse;
    width: auto;
    font-family:IBM Plex Sans, Arial, sans-serif !important;
}

th, td {
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {background-color: #f6f6f6;}
</style>"""

    footer = "</table>"

    # Upper HBox widget data

    upper_str += "<tr><th>Property</th><th>Value</th></tr>"
    for key in upper_list:
        upper_str += (
            "<tr><td><font style='font-weight:bold'>%s</font></td><td>%s</td></tr>"
            % (key, config_dict[key]))
    upper_str += footer

    upper_table = wid.HTMLMath(value=upper_str,
                               layout=wid.Layout(width="100%",
                                                 grid_area="left"))

    img_child = []
    if not config["simulator"]:
        img_child = [iplot_gate_map(backend, as_widget=True)]

    image_widget = wid.HBox(
        children=img_child,
        layout=wid.Layout(
            grid_area="right",
            max_height="350px",
            margin="0px 0px 0px 0px",
            display="flex-inline",
            align_items="center",
            justify_content="center",
            width="auto",
        ),
    )

    lower_str = "<table>"
    lower_str += """<style>
table {
    border-collapse: collapse;
    width: auto;
}

th, td {
    text-align: left;
    padding: 8px !important;
}

tr:nth-child(even) {background-color: #f6f6f6;}
</style>"""
    lower_str += "<tr><th></th><th></th></tr>"
    for key in lower_list:
        if key != "name":
            lower_str += "<tr><td>%s</td><td>%s</td></tr>" % (key,
                                                              config_dict[key])
    lower_str += footer

    lower_table = wid.HTMLMath(value=lower_str,
                               layout=wid.Layout(width="auto",
                                                 grid_area="bottom"))

    grid = wid.GridBox(
        children=[upper_table, image_widget, lower_table],
        layout=wid.Layout(
            max_height="500px",
            margin="10px",
            overflow="hidden scroll",
            grid_template_rows="auto auto",
            grid_template_columns="33% 21% 21% 21%",
            grid_template_areas="""
                                         "left right right right"
                                         "bottom bottom bottom bottom"
                                         """,
            grid_gap="0px 0px",
        ),
    )

    return grid
Ejemplo n.º 20
0
    def on_button_select_clicked(b):
        
        # Keep history of the chosen file
        expt.default_wm_log_file = w_select_log.value
        
        # Construct the list of wm in the log
        list_wm = []
        log_file =  w_select_log.value
        with open(expt.logs_dir+log_file, encoding="utf8", errors='ignore') as f:
            for line in f:
                if "# " in line: temp = line
                if ("wm " in line and "pwm" not in line and "ERROR" not in line):
                    list_wm.append(temp.replace('\n','')+'; '+line.replace('\n',''))
        f.close()

        if list_wm == []:
            print(PN._RED+"No positions in the log file: "+log_file+PN._RESET)
            print(PN._RED+"Choose another log file."+PN._RESET)
            return

        w_select_wm = widgets.Dropdown(
                     options=list_wm,
                     value=list_wm[-1],
                     layout=widgets.Layout(width='700px'),
                     style={'description_width': 'initial'})

        display(w_select_wm)

        def on_button_print_clicked(b):
             
            # Extract the date of the wm (used as an identifier to be found in the log)
            date_wm = w_select_wm.value.split('; ')[0]

            # Find the right lines in the log
            list_sensors = []
            is_date_found = False
            count = 0
            with open(expt.logs_dir+log_file) as f:
                for line in f:
                    if date_wm in line: 
                        is_date_found = True
                    if (is_date_found and '-------' in line):
                        count += 1    
                    if (count == 2 and '-------' not in line):
                        # append name, value, unit
                        list_sensors.append(line.split()[2:5])

                        # if unit is not present
                        if ('[' in line.split()[4]):  
                            list_sensors[-1][-1] = ' '

                        # if unit is 'No unit'
                        if ('No' in line.split()[4]):  
                            list_sensors[-1][-1] = ' '

            f.close()

            # Display as a markdown table
            max_elem_per_line = 6
            nb_elem = len(list_sensors)
            if nb_elem >= max_elem_per_line:
                nb_line = math.ceil(nb_elem/max_elem_per_line)
                nb_per_line = math.ceil(nb_elem/nb_line)
            else:
                nb_per_line = max_elem_per_line

            list_sensors_str = []
            for i in range(0,nb_elem,nb_per_line):
                list_sensors_cut = list_sensors[i:i+nb_per_line]

                tbw_str = '' 
                for sensor in list_sensors_cut:
                    tbw_str += '|'+str(sensor[0])
                tbw_str += '|'+' \n'

                for sensor in list_sensors_cut:
                    tbw_str += '|'+':-:'
                tbw_str += '|'+' \n'

                for sensor in list_sensors_cut:
                    tbw_str += '|'+str(sensor[1])
                tbw_str += '|'+' \n'

                for sensor in list_sensors_cut:
                    tbw_str += '|'+str(sensor[2])
                tbw_str += '|'

                list_sensors_str.append(tbw_str)

            for sensor_str in list_sensors_str[::-1]:
                # Create markdown cells with the tables
                Utils.Create_cell(code=sensor_str, position ='below', celltype='markdown', is_print=True)

            # Put title    
            Utils.Create_cell(code='### '+w_select_wm.value.split('; ')[1], position ='below', celltype='markdown', is_print=True)
            
            Utils.Delete_current_cell()
        
            Utils.Create_cell(code='FE.Action.Choose(expt)',
                        position ='at_bottom', celltype='code', is_print=False)  
            
        button = widgets.Button(description="Insert positions")
        display(button)
        button.on_click(on_button_print_clicked)
Ejemplo n.º 21
0
            #sn.cubehelix_palette(3, start=2.85, rot=-0.10, hue=2, dark=.4, light=.7),
        'blue2': [[0.424, 0.787, 1.0], [0.172, 0.517, 0.855], [0.057, 0.219, 0.486]]
            #sn.cubehelix_palette(3, start=2.85, rot=-0.10, hue=2, dark=.2, light=.7)
        }
# Shared, Unique
prim_colors = {
        'main': [[0.1, 0.7, 0.1], [1, 1, 1]],
        'main-r': [[1, 1, 1], [0.1, 0.7, 0.1]]
        }

disp_colors = False



# Layout
widget_lay = widgets.Layout(width='60%')
subui_lay = widgets.Layout(width='90%')
html_lay = widgets.Layout(position='center', width='35%')
test_lay = widgets.Layout(width='32%')
check_lay = widgets.Layout(width='90%', position='left', margin='0px 0px 0px 0px')
check_html_lay = widgets.Layout(width='85%', position='right', margin='0px 0px 0px 0px')

## Select colors widget
# active
active_widget = widgets.Dropdown(options=active_colors, layout=widget_lay)
active_title = widgets.HTML('Dynamic:', layout=html_lay)
active_ui = widgets.HBox([active_title, active_widget])#, layout=subui_lay)

# static
static_widget = widgets.Dropdown(options=static_colors, layout=widget_lay)
static_title = widgets.HTML('Static:', layout=html_lay)
Ejemplo n.º 22
0
    def on_button_validate_path_clicked(b):
        """
        Validate the path of the script folder. Open selection for the script.
        """

        if not os.path.exists(w_path_to_dir.value):
            print(PN._RED+"Wrong folder name."+PN._RESET)
            print("")
            return

        # Pass the current value of the directory to the default one
        expt.path_to_dir = w_path_to_dir.value

        # Define the list of log files in the log directory
        list_scripts_files = [file for file in sorted(os.listdir(expt.path_to_dir)) if '.ipy' in file][::-1]

        if len(list_scripts_files) < 1:
            print(PN._RED+"There is no script in this folder."+PN._RESET)
            print("")
            return                

        # Widget to select script
        w_select_script = widgets.Dropdown(
             options=list_scripts_files,
             value=list_scripts_files[-1],
             layout=widgets.Layout(width='300px'),
             style={'description_width': 'Script:'})

        def on_button_insert_script_clicked(b):
            """
            Display a script in a markdown cell.
            Add scan numbers if asked.
            """ 

            # Get and insert the script
            path_to_dir = w_path_to_dir.value
            script_name = w_select_script.value
            text_file = open(path_to_dir+script_name)
            
            # Import original script
            script_init = text_file.read()
            
            # Split the different lines
            script_split = script_init.split('\n')
            
            
            # Check if the value of first script is a number
            # If not, do not add the counting
            
            if np.char.isnumeric(w_index_first_scan.value):

                
                ###################################
                # Add scan number
                ###################################

                # List of names which triggers an increment of the scan number
                list_trig = ['cont_regh', 'scan']

                count = int(w_index_first_scan.value)

                is_loop = False
                for i in range(len(script_split)):

                    if is_loop:

                        # Fist run on the loop to collect info
                        loop_length = 0
                        nb_scan_in_loop = 0
                        is_first_run = True
                        for k in range(i,len(script_split)):
                            if is_first_run: 
                                if ('    ' in script_split[k]) or ('\t' in script_split[k]):
                                    loop_length +=1
                                    if any(elem in script_split[k] for elem in list_trig):
                                        nb_scan_in_loop+=1
                                else:
                                    is_first_run = False

                        # Attribute the scan numbers
                        for j in range(repet_nb-1):
                            for k in range(i,i+loop_length):
                                if any(elem in script_split[k] for elem in list_trig):
                                        script_split[k] = script_split[k]+' #'+str(count)
                                        count+=1

                        is_loop = False

                    # Detect a loop
                    if ('in range' in script_split[i]):
                        repet_nb = int(''.join([str(s) for s in script_split[i] if s.isdigit()]))
                        is_loop = True

                    # Regular scan not in a loop
                    if any(elem in script_split[i] for elem in list_trig):
                        script_split[i] = script_split[i]+' #'+str(count)
                        count+=1

                ##################################
            
            
            # Re-create the script with added scan numbers
            script_modif ='\n'.join(script_split)
            
            text_file.close()
            
            
            code = '```python\n'+script_modif+'```'

            Utils.Create_cell(code='### '+ script_name, position ='above', celltype='markdown', is_print=True)
            Utils.Create_cell(code=code, position ='above', celltype='markdown', is_print=True)
            
            Utils.Delete_current_cell()
        
            Utils.Create_cell(code='FE.Action.Choose(expt)',
                        position ='at_bottom', celltype='code', is_print=False)  

        
        # Get number of the first scan
        w_index_first_scan = widgets.Text(value='',
                                             style = {'description_width': 'initial'},
                                             layout = widgets.Layout(width='200px'),
                                             description='Index first scan')
        

        button_insert_script = widgets.Button(description="Insert script")
        button_insert_script.on_click(on_button_insert_script_clicked)

        display(widgets.HBox([w_select_script, w_index_first_scan, button_insert_script]))
Ejemplo n.º 23
0
def gates_tab(backend):
    """The multiple qubit gate error widget.

    Args:
        backend (IBMQBackend | FakeBackend): The backend.

    Returns:
        VBox: A VBox widget.
    """
    props = backend.properties()

    multi_qubit_gates = [g for g in props.gates if len(g.qubits) > 1]

    header_html = "<div><font style='font-weight:bold'>{key}</font>: {value}</div>"
    header_html = header_html.format(key="last_update_date",
                                     value=props.last_update_date)

    update_date_widget = widgets.HTML(value=header_html,
                                      layout=widgets.Layout(grid_area="top"))

    gate_html = "<table>"
    gate_html += """<style>
table {
    border-collapse: collapse;
    width: auto;
}

th, td {
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {background-color: #f6f6f6;};
</style>"""

    gate_html += "<tr><th></th><th>Type</th><th>Gate error</th></tr>"
    gate_footer = "</table>"

    # Split gates into two columns
    left_num = math.ceil(len(multi_qubit_gates) / 3)
    mid_num = math.ceil((len(multi_qubit_gates) - left_num) / 2)

    left_table = gate_html

    for qub in range(left_num):
        gate = multi_qubit_gates[qub]
        qubits = gate.qubits
        ttype = gate.gate
        error = round(props.gate_error(gate.gate, qubits), 5)

        left_table += "<tr><td><font style='font-weight:bold'>%s</font>"
        left_table += "</td><td>%s</td><td>%s</td></tr>"
        left_table = left_table % (f"{ttype}{qubits[0]}_{qubits[1]}", ttype,
                                   error)
    left_table += gate_footer

    middle_table = gate_html

    for qub in range(left_num, left_num + mid_num):
        gate = multi_qubit_gates[qub]
        qubits = gate.qubits
        ttype = gate.gate
        error = round(props.gate_error(gate.gate, qubits), 5)

        middle_table += "<tr><td><font style='font-weight:bold'>%s</font>"
        middle_table += "</td><td>%s</td><td>%s</td></tr>"
        middle_table = middle_table % (f"{ttype}{qubits[0]}_{qubits[1]}",
                                       ttype, error)
    middle_table += gate_footer

    right_table = gate_html

    for qub in range(left_num + mid_num, len(multi_qubit_gates)):
        gate = multi_qubit_gates[qub]
        qubits = gate.qubits
        ttype = gate.gate
        error = round(props.gate_error(gate.gate, qubits), 5)

        right_table += "<tr><td><font style='font-weight:bold'>%s</font>"
        right_table += "</td><td>%s</td><td>%s</td></tr>"
        right_table = right_table % (f"{ttype}{qubits[0]}_{qubits[1]}", ttype,
                                     error)
    right_table += gate_footer

    left_table_widget = widgets.HTML(value=left_table,
                                     layout=widgets.Layout(grid_area="left"))
    middle_table_widget = widgets.HTML(
        value=middle_table, layout=widgets.Layout(grid_area="middle"))
    right_table_widget = widgets.HTML(value=right_table,
                                      layout=widgets.Layout(grid_area="right"))

    grid = widgets.GridBox(
        children=[
            update_date_widget, left_table_widget, middle_table_widget,
            right_table_widget
        ],
        layout=widgets.Layout(
            grid_template_rows="auto auto",
            grid_template_columns="33% 33% 33%",
            grid_template_areas="""
                                                   "top top top"
                                                   "left middle right"
                                                   """,
            grid_gap="0px 0px",
        ),
    )

    return grid
Ejemplo n.º 24
0
def Insert_image(expt):
    '''
    Insert an image in the notebook.

    Parameters
    ----------
    expt : object
        object from the class Expt
    '''  

    # Check if there is already a path for images directories
    # If not, use the recording directory
    try:
        path_to_img_default = expt.path_to_img
    except:
        path_to_img_default = expt.recording_dir

    # Widget to write path
    w_path_to_img = widgets.Text(
            value=path_to_img_default,
            description='Images directory:',
            layout=widgets.Layout(width='800px', height='40px'),
            style={'description_width': 'initial'})

    def on_button_validate_path_clicked(b):
        """
        Validate the path of the images folder. Open selection for the image.
        """

        if not os.path.exists(w_path_to_img.value):
            print(PN._RED+"Wrong folder name."+PN._RESET)
            print("")
            return

        # Pass the current value of the directory to the default one
        expt.path_to_img = w_path_to_img.value

        # Define the list of img files in the directory
        list_img_files = [file for file in sorted(os.listdir(expt.path_to_img))][::-1]

        if len(list_img_files) < 1:
            print(PN._RED+"There is no image in this folder."+PN._RESET)
            print("")
            return                

        # Widget to select image
        w_select_img = widgets.Dropdown(
             options=list_img_files,
             value=list_img_files[-1],
             layout=widgets.Layout(width='300px'),
             style={'description_width': 'Image:'})

        def on_button_insert_image_clicked(b):
            """
            Insert an image in a markdown cell.
            """ 

            # Get and insert the image
            path_to_img = w_path_to_img.value+w_select_img.value

            Utils.Create_cell(code='![]('+ path_to_img+')', position ='above', celltype='markdown', is_print=True)
            
            Utils.Delete_current_cell()
        
            Utils.Create_cell(code='FE.Action.Choose(expt)',
                        position ='at_bottom', celltype='code', is_print=False)  

        button_insert_image = widgets.Button(description="Insert image")
        button_insert_image.on_click(on_button_insert_image_clicked)

        display(widgets.HBox([w_select_img, button_insert_image]))

    button_validate_path = widgets.Button(description="Validate path")
    button_validate_path.on_click(on_button_validate_path_clicked)
    display(widgets.HBox([w_path_to_img, button_validate_path]))
Ejemplo n.º 25
0
def obs_jobs_form(mock=False):
    info = InfoComponent()
    job_header_id_label = widgets.HTML(value=f"<b>Job ID</b>")
    job_header_name_label = widgets.HTML(value=f"<b>Job Name</b>")
    job_header_progress_label = widgets.HTML(value=f"<b>Progress</b>")
    job_header_status_label = widgets.HTML(value=f"<b>Status</b>")
    jobs_box_children = [
        job_header_id_label, job_header_name_label, job_header_progress_label,
        job_header_status_label,
        widgets.Label()
    ]
    grid_template_rows = 'auto'
    job_components = {}
    for job in JOBS.values():
        job_progress_bar = widgets.IntProgress(value=job.progress,
                                               min=0,
                                               max=100)
        job_status_label = widgets.Label(job.status)
        job_id_label = widgets.Label(job.id)
        job_name_label = widgets.Label(job.name)
        job_cancel_button = widgets.Button(description="Cancel",
                                           icon="times-circle")
        job_cancel_button.on_click(
            _get_handle_cancel_button_clicked(job.id, info.message_func, mock))
        jobs_box_children.append(job_id_label)
        jobs_box_children.append(job_name_label)
        jobs_box_children.append(job_progress_bar)
        jobs_box_children.append(job_status_label)
        jobs_box_children.append(job_cancel_button)
        grid_template_rows = grid_template_rows + ' auto'
        job_components[f'{job.id}'] = (job_progress_bar, job_status_label,
                                       job_cancel_button,
                                       _get_job_func(job, mock), job)
    jobs_monitor_component = widgets.GridBox(
        children=jobs_box_children,
        layout=widgets.Layout(width='100%',
                              grid_template_rows=grid_template_rows,
                              grid_template_columns='10% 20% 40% 10% 20%'))
    jobs_full_component = widgets.VBox(
        [jobs_monitor_component, info.as_widget(100)])

    def jobs_monitor_func(components, empty):
        at_least_one_job_unfinished = True
        while at_least_one_job_unfinished:
            at_least_one_job_unfinished = False
            for progress_bar, status_label, cancel_button, job_func, component_job in components.values(
            ):
                job_state = job_func(component_job, info.message_func)
                if job_state is not None:
                    _update_job(component_job, job_state)
                progress_bar.value = component_job.progress
                status_label.value = component_job.status
                if component_job.status != 'new' and component_job.status != 'running':
                    cancel_button.disabled = True
                if component_job.status not in [
                        'succeeded', 'cancelled', 'failed'
                ]:
                    at_least_one_job_unfinished = True
            time.sleep(10)

    _monitor(jobs_monitor_func, jobs_full_component, (job_components, None))
Ejemplo n.º 26
0
    def __init__(self, metadata, blk, rauc_sigs=None, lazy=False):
        """
        Initialize a new EphyviewerConfiguratorWidget.
        """

        assert HAVE_IPYWIDGETS, 'ipywidgets is a requirement for EphyviewerConfiguratorWidget'

        # initial the configurator
        EphyviewerConfigurator.__init__(self,
                                        metadata=metadata,
                                        blk=blk,
                                        rauc_sigs=rauc_sigs,
                                        lazy=lazy)

        self.viewer_settings['traces'].update({
            'icon': 'line-chart',
            'description': 'Traces'
        })
        self.viewer_settings['traces_rauc'].update({
            'icon': 'area-chart',
            'description': 'RAUC'
        })
        self.viewer_settings['freqs'].update({
            'icon': 'wifi',
            'description': 'Frequencies'
        })
        self.viewer_settings['spike_trains'].update({
            'icon':
            'barcode',
            'description':
            'Spike Trains'
        })
        self.viewer_settings['epochs'].update({
            'icon': 'align-left',
            'description': 'Read-Only Epochs'
        })
        self.viewer_settings['epoch_encoder'].update({
            'icon':
            'align-left',
            'description':
            'Epoch Encoder'
        })
        self.viewer_settings['video'].update({
            'icon': 'youtube-play',
            'description': 'Video'
        })
        self.viewer_settings['event_list'].update({
            'icon': 'list',
            'description': 'Events'
        })
        self.viewer_settings['data_frame'].update({
            'icon':
            'table',
            'description':
            'Annotation Table'
        })

        # create a widget container which will be displayed whenever the
        # EphyviewerConfiguratorWidget is displayed
        self.main_widget = ipywidgets.HBox()
        self._ipython_display_ = self.main_widget._ipython_display_

        # create buttons for controlling which elements to show
        self.controls = {}
        for name, d in self.viewer_settings.items():
            self.controls[name] = ipywidgets.ToggleButton(
                value=d['show'],
                disabled=d['disabled'],
                icon=d['icon'],
                description=d['description'],
                tooltip=d['reason'])
            self.controls[name].key = name  # save control name for _on_toggle
            self.controls[name].observe(self._on_toggle, names='value')
        controls_vbox = ipywidgets.VBox(list(self.controls.values()))

        # create the launch button
        self.launch_button = ipywidgets.Button(
            icon='rocket',
            description='Launch',
            layout=ipywidgets.Layout(height='auto'))
        self.launch_button.on_click(self._on_launch_clicked)

        # populate the box
        self.main_widget.children = [controls_vbox, self.launch_button]
Ejemplo n.º 27
0
class Step(traitlets.HasTraits):

    layout = ipyw.Layout(border="1px lightgray solid",
                         margin='5px',
                         padding='15px')
    button_layout = ipyw.Layout(margin='10px 5px 5px 5px')

    def __init__(self, context, previous_step=None):
        super(Step, self).__init__()
        self.context = context
        self.previous_step = previous_step
        self.next_step = None
        self.panel = None
        self._ondisplay = False
        return

    def createPanel(self):
        body = self.createBody()
        navigation = self.createNavigation()
        panel = ipyw.VBox(children=[body, navigation])
        return panel

    def createBody(self):
        raise NotImplementedError

    def createNavigation(self):
        previous_step = self.previous_step
        buttons = []
        if previous_step:
            PREVIOUS = ipyw.Button(description='PREVIOUS')
            PREVIOUS.on_click(self.handle_previous_button_click)
            buttons.append(PREVIOUS)
        #
        NEXT = ipyw.Button(description='NEXT')
        NEXT.on_click(self.handle_next_button_click)
        buttons.append(NEXT)
        return ipyw.HBox(children=buttons)

    def show(self):
        if self.panel is None:
            self.panel = self.createPanel()
        if not self._ondisplay:
            display(self.panel)
            self._ondisplay = True
        self.panel.layout.display = 'block'

    def remove(self):
        self.panel.layout.display = 'none'

    def handle_next_button_click(self, s):
        if not self.validate():
            return
        self.remove()
        self.nextStep()

    def handle_previous_button_click(self, s):
        self.remove()
        self.previousStep()

    def previousStep(self):
        step = self.previous_step
        step.show()

    def validate(self):
        raise NotImplementedError

    def nextStep(self):
        if self.next_step is None:
            self.next_step = next_step = self.createNextStep()
        else:
            next_step = self.next_step
        # no next step. done
        if next_step is None: return
        # has next step. make sure it knows about this step
        next_step.previous_step = self
        next_step.show()
        return

    def createNextStep(self):
        raise NotImplementedError
Ejemplo n.º 28
0
    def __init__(self,
                 file=None,
                 local_data_root=None,
                 remote_data_root=None,
                 initial_selection=None):
        """
        Initialize a new MetadataSelectorWidget.
        """

        assert HAVE_IPYWIDGETS, 'ipywidgets is a requirement for MetadataSelectorWidget'

        # load the metadata and set the initial selection
        MetadataSelector.__init__(self,
                                  file=file,
                                  local_data_root=local_data_root,
                                  remote_data_root=remote_data_root,
                                  initial_selection=initial_selection)

        # create a widget container which will be displayed whenever the
        # MetadataSelectorWidget is displayed
        self.main_widget = ipywidgets.VBox()
        self._ipython_display_ = self.main_widget._ipython_display_

        # create the selector widget
        self.selector = ipywidgets.Select()

        if self.all_metadata is not None:
            # create display text for the selector from keys and descriptions
            self.selector.options = zip(_selector_labels(self.all_metadata),
                                        self.all_metadata.keys())

            # set initial selection
            if self._selection is None:
                self.selector.value = list(self.all_metadata)[0]
            else:
                self.selector.value = self._selection

        # use monospace font for items in the selector
        self.selector.add_class('metadata-selector')
        try:
            display(
                HTML(
                    '<style>.metadata-selector select {font-family: monospace;}</style>'
                ))
        except NameError:
            # likely operating outside Jupyter notebook
            pass

        # set other selector display options
        self.selector.description = 'Data set:'
        self.selector.rows = 20
        self.selector.layout = ipywidgets.Layout(width='99%')
        self.selector.style = {'description_width': 'initial'}

        # configure the _on_select function to be called whenever the selection changes
        self.selector.observe(self._on_select, names='value')
        if self.all_metadata is not None:
            self._on_select({'new': self.selector.value
                             })  # run now on initial selection

        # create the reload button
        self.reload_button = ipywidgets.Button(
            icon='refresh',
            description='Reload',
            layout=ipywidgets.Layout(height='auto'),
            disabled=False)
        self.reload_button.on_click(self._on_reload_clicked)

        # create the download button
        self.download_button = ipywidgets.Button(
            icon='download',
            description='Download',
            layout=ipywidgets.Layout(height='auto'),
            disabled=False)
        self.download_button.on_click(self._on_download_clicked)

        # populate the box
        # self.main_widget.children = [self.selector, self.reload_button, self.download_button]
        self.main_widget.children = [self.selector, self.reload_button]
Ejemplo n.º 29
0
if nanoHUB_flag or hublib_flag:
    read_config = widgets.Dropdown(
        description='Load Config',
        options=get_config_files(),
        tooltip='Config File or Previous Run',
    )
    read_config.style = {
        'description_width': '%sch' % str(len(read_config.description) + 1)
    }
    read_config.observe(read_config_cb, names='value')

tab_height = 'auto'
tab_layout = widgets.Layout(
    width='auto',
    height=tab_height,
    overflow_y='scroll',
)  # border='2px solid black',
titles = [
    'About', 'Config Basics', 'Microenvironment', 'User Params',
    'Out: Cell Plots', 'Out: Substrate Plots'
]
tabs = widgets.Tab(children=[
    about_tab.tab, config_tab.tab, microenv_tab.tab, user_tab.tab, svg.tab,
    sub.tab
],
                   _titles={i: t
                            for i, t in enumerate(titles)},
                   layout=tab_layout)

homedir = os.getcwd()
Ejemplo n.º 30
0
    def __init__(self,
                 label='Run',
                 tooltip='Run Simulation',
                 start_func=None,
                 done_func=None,
                 outcb=None,
                 width='auto',
                 cachename=None,
                 cachedir=None,
                 cachecb=None,
                 showcache=True):
        self.label = label
        self.tooltip = tooltip
        self.start_func = start_func
        self.done_func = done_func
        self.outcb = outcb
        self.cachename = cachename
        self.q = Queue()
        self.thread = 0
        self.status = None
        self.output = None
        self.width = width
        self.cachecb = cachecb
        self.showcache = showcache
        self.cbuf = collections.deque(maxlen=200)  # circular buffer

        if start_func is None:
            print("start_func is required", file=sys.stderr)

        if cachename:
            # set up cache
            if cachedir is None:
                try:
                    cachedir = os.environ['CACHEDIR']
                except:
                    print("ERROR: cachename is set, but CACHEDIR is not",
                          file=sys.stderr)
                    print(
                        "Set the environment variable 'CACHEDIR' to the directory",
                        file=sys.stderr)
                    print("where you want the cache to be located.",
                          file=sys.stderr)
                    sys.exit(1)

            self.cachedir = os.path.join(cachedir, cachename)
            self.cachetabdir = os.path.join(self.cachedir, '.cache_table')
            if not os.path.isdir(self.cachedir):
                os.makedirs(self.cachedir)
            memory = Memory(cachedir=self.cachetabdir, verbose=0)

            @memory.cache
            def make_rname(*args):
                # uuid should be unique, but check just in case
                while True:
                    fname = str(uuid.uuid4()).replace('-', '')
                    if not os.path.isdir(os.path.join(self.cachedir, fname)):
                        break
                return fname

            self.make_rname = make_rname

        self.but = w.Button(description=self.label,
                            tooltip=self.tooltip,
                            button_style='success')
        self.output = w.Textarea(layout={'width': '100%', 'height': '400px'})
        self.acc = w.Accordion(children=[self.output])
        self.acc.set_title(0, 'Output')
        self.acc.selected_index = None
        self.but.on_click(self._but_cb)
        self.disabled = False
        _layout = w.Layout(flex_flow='column',
                           justify_content='flex-start',
                           width=self.width)
        self.w = w.VBox([self.but], layout=_layout)