Esempio n. 1
0
 def __init__(self, motor, config=Configuration(), *args, **kwargs):
     widgets.Button.__init__(self, *args, **kwargs)
     
     # Config
     self.config = config
     
     # Motor associated to the button
     self.motor = motor
     if type(motor) != py4syn.epics.MotorClass.Motor:
         logprint("Passed to class MotorSetValueButton constructor a argument with wrong type. Expected py4syn.epics.MotorClass.Motor, received " + str(type(motor)), config=self.config)
         
         raise("Passed to class MotorSetValueButton constructor a argument with wrong type. Expected py4syn.epics.MotorClass.Motor, received " + str(type(motor)))
     
     # Bounded float text associated to the button
     self.bounded_float = widgets.BoundedFloatText(
                             value=motor.getRealPosition(),
                             min=motor.getLowLimitValue(),
                             max=motor.getHighLimitValue(),
                             step=0.01,
                             description=motor.motorDesc,
                             disabled=False
                           )
     
     # class Button values for MotorSetValueButton
     self.description = 'Set Target VAL'
     self.disabled = False
     self.button_style = 'success'
     self.tooltip = 'Click me'
     self.icon = ''    
     
     # Set callback function for click event
     self.on_click(self._motor_set_val_button)
     
     # Widgets Boxes
     self.output = widgets.Output()
Esempio n. 2
0
    def _click_button(b):
        # Clear previous logs outputs
        b.output.clear_output()

        # with statement to output logs in stdou (if this option is enabled)
        with b.output:
            # Change button to a "clicked status"
            b.disabled = True
            b.button_style = ''
            b.description = 'Opening Interface...'

            # Create a subprocess with the slits script from sol-widgets
            try:
                logprint("Opening slits interface", config=b.config)
                subprocess.Popen(["slits", b.left, b.right, b.top, b.bottom])

                logprint("Finished opening slits interface", config=b.config)
            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint("Error in opening slits interface",
                         "[ERROR]",
                         config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            # We should sleep for some time to give some responsiveness to the user
            time.sleep(1.0)

            # Reenable button
            b.disabled = False
            b.button_style = 'success'
            b.description = 'Open Slits Interface'
Esempio n. 3
0
    def _start_button(b):
        # Clear previous logs outputs
        b.output.clear_output()

        # with statement to output logs in stdou (if this option is enabled)
        with b.output:
            # Change button to a "clicked status"
            b.disabled = True
            b.button_style = ''
            b.description = 'Scaling...'

            try:
                logprint("Starting Scaler", config=b.config)
                subprocess.Popen(["scaler " + b.macro.value], shell=True)

                logprint("Finished openning Scaler", config=b.config)
            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint("Error in openning Scaler",
                         "[ERROR]",
                         config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            # We should sleep for some time to give some responsiveness to the user
            time.sleep(1.0)

            # Change button layout monitoring
            b.disabled = False
            b.button_style = 'success'
            b.description = 'Start Scaler GUI'
Esempio n. 4
0
    def _button_click(b):
        # Clear previous logs outputs
        b.output.clear_output()

        # with statement to output logs in stdou (if this option is enabled)
        with b.output:
            # Change button to a "clicked status"
            b.disabled = True
            b.button_style = ''

            # We should sleep for some time to give some responsiveness to the user
            time.sleep(0.5)

            logprint("Setting PV " + b.pv_name + " to value " +
                     b.bounded_text.value,
                     config=b.config)
            try:
                # Move the motor to target absolute position
                b.pv.put(b.bounded_text.value, wait=False)
                logprint("Set value " + b.bounded_text.value + " to PV " +
                         b.pv_name,
                         config=b.config)
            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint("Error in setting value " + b.bounded_text.value +
                         " to PV " + b.pv_name,
                         "[ERROR]",
                         config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            # Change button layout back to normal
            b.disabled = False
            b.button_style = 'success'
Esempio n. 5
0
    def _start_button(b):
        # Clear previous logs outputs
        b.output.clear_output()

        # With statement to output logs in Jupyter stdout (if this option is enabled)
        with b.output:
            # Change button to a "clicked status"
            b.disabled = True
            b.button_style = ''
            b.description = 'Executing...'

            try:
                logprint("Executing move", config=b.config)
                # TODO: call move main from another thread
                move.main(b.bounded_text.value.split())

                logprint("Finished executing move", config=b.config)
            except SystemExit as e:
                pass
            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint("Error in executing move", "[ERROR]", config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            # We should sleep for some time to give some responsiveness to the user
            time.sleep(1.0)

            # Change button layout monitoring
            b.disabled = False
            b.button_style = 'success'
            b.description = 'Execute move'
Esempio n. 6
0
    def _start_button(b):
        # Clear previous logs outputs
        b.output.clear_output()
        
        # with statement to output logs in stdou (if this option is enabled)
        with b.output:
            # Change button to a "clicked status"
            b.disabled = True
            b.button_style = ''
            b.description='Opening interface...'

            try:
                logprint("Starting " + b.interface, config=b.config)

                if b.macro.value is not "":
                    if b.interface is not "energy_scan_gui":
                        subprocess.Popen([b.interface, "-m", b.macro.value])
                    else:
                        subprocess.Popen([b.interface, b.macro.value])
                else:
                    subprocess.Popen([b.interface])

                logprint("Finished openning " + b.interface, config=b.config)
            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint("Error in openning " + b.interface, "[ERROR]", config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            # We should sleep for some time to give some responsiveness to the user
            time.sleep(1.0)

            # Change button layout monitoring
            b.disabled = False
            b.button_style = 'success'
            b.description = 'Start ' + b.interface
Esempio n. 7
0
    def _start_button(b):
        # Clear previous logs outputs
        b.output.clear_output()

        # with statement to output logs in stdou (if this option is enabled)
        with b.output:
            # Change button to a "clicked status"
            b.disabled = True
            b.button_style = ''
            b.description = 'Executing...'

            try:
                logprint("Executing command " + b.command, config=b.config)

                b.command_dict.execute(b.command, b.arguments.value)

                logprint("Finished executing command " + b.command,
                         config=b.config)
            except Exception as error:
                # If any error occurs, log that but dont stop code exection
                logprint("Error in executing command " + b.command,
                         "[ERROR]",
                         config=b.config)
                logprint(str(error), "[ERROR]", config=b.config)
            except SystemExit as value:
                logprint("Finished executing command " + b.command +
                         " with SystemExit(" + str(value) + ")",
                         config=b.config)

            # We should sleep for some time to give some responsiveness to the user
            time.sleep(1.0)

            # Change button layout monitoring
            b.disabled = False
            b.button_style = 'success'
            b.description = 'Execute Command ' + '"' + b.command + '"'
Esempio n. 8
0
    def _motor_set_val_button(b):
        # Clear previous logs outputs
        b.output.clear_output()
        
        # with statement to output logs in stdou (if this option is enabled)
        with b.output:
            # Change button to a "clicked status"
            b.disabled = True
            b.button_style = ''

            logprint("Starting motor " + b.motor.pvName + " movement to absolute value " + str(b.bounded_float.value), config=b.config)
            try:
                # Move the motor to target absolute position
                b.motor.setAbsolutePosition(b.bounded_float.value, waitComplete=False)
                logprint("Set absolute value " + str(b.bounded_float.value) + " for motor " + b.motor.pvName, config=b.config)
            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint("Error in setting absolute value " + str(b.bounded_float.value) + " for motor " + b.motor.pvName, "[ERROR]", config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            # Change button layout back to normal
            b.disabled = False
            b.button_style = 'success'
Esempio n. 9
0
    def _start_button(b):
        # Clear previous logs outputs
        b.output.clear_output()

        # with statement to output logs in stdou (if this option is enabled)
        with b.output:
            # Change button to a "clicked status"
            b.disabled = True
            b.button_style = ''
            b.description = 'Initializing...'
            # We should sleep for some time to give some responsiveness to the user
            time.sleep(0.5)

            # Get motors PV names from the text box
            # Filter names
            motor_list_names = []
            names_comma_space_ent = []
            names_space = b.text.value.split(' ')
            for name in names_space:
                names_comma_space = name.split(",")

                for name_wout_comma in names_comma_space:
                    names_comma_space_ent.append(name_wout_comma.split("\n"))

            lin_names = sum(names_comma_space_ent, [])
            motor_list_names = [
                name for name in lin_names if name != "" and name != "\n"
            ]

            logprint("Starting motors " + ', '.join(motor_list_names) +
                     " initialization",
                     config=b.config)

            # Create motors and add a MotorSetValueButton to them
            try:
                b.motors_list = [
                    configurate_motor(name, ' '.join(name.split(':')[-2:]),
                                      b.config) for name in motor_list_names
                ]

                b.motors_value_buttons = [
                    MotorSetValueButton(motor, b.config)
                    for motor in b.motors_list
                ]

                # Set Target Value Button Widget children to all initilized motors boxes
                widgets_boxes = [
                    motor_button.box_motor_button()
                    for motor_button in b.motors_value_buttons
                ]
                widgets_target_outputs = [
                    motor_button.box_motor_output()
                    for motor_button in b.motors_value_buttons
                ]
                b.target_value_buttons.children = tuple(widgets_boxes +
                                                        widgets_target_outputs)

                logprint("Finished motors " + ', '.join(motor_list_names) +
                         " initialization",
                         config=b.config)
            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint("Error in initialziation of motors " +
                         ', '.join(motor_list_names),
                         "[ERROR]",
                         config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

                # Reset Target Value Boxes to default Label
                b.target_value_buttons.children = (
                    widgets.Label("No motors initilized to be showed."), )

            # Change button layout back to normal
            b.disabled = False
            b.button_style = 'success'
            b.description = 'Start Motor Initializaton'
Esempio n. 10
0
    def _click_button(b):
        with b.output:
            # Change button to a "clicked status"
            b.disabled = True
            b.button_style = ''
            b.description = 'Exporting...'

            # We should sleep for some time to give some responsiveness to the user
            time.sleep(0.5)

            # Get configuration run-time values
            b.notebook_name = b.config.notebook_name.value
            b.plots_list = b.config.plots_list

            # Check if notebook name is not empty, if it is, print an error message and
            # change button status temporarily to an error descripton. Then restart button.
            if b.notebook_name == "":
                logprint("Notebook name not defined in configuration cell",
                         "[ERROR]",
                         config=b.config)
                # Change button status to a "error status"
                b.disabled = True
                b.button_style = 'danger'
                b.description = 'ERROR. Notebook\'s name not set'

                time.sleep(2.0)

                # Reenable button
                b.disabled = False
                b.button_style = 'warning'
                b.description = 'Export Notebook to HTML'

                return

            try:
                # For every plot registered in the plots_list, we have to set these
                # plots export flag to True to start the export
                for plot in b.plots_list:
                    plot.export = True

                # Time sleep to the plot_list thread update the display
                time.sleep(1.0)

                # Get time stamp for the export name
                ts = time.gmtime()
                time_stamp = time.strftime("%Y-%m-%d-%H:%M:%S", ts)
                output_file = time_stamp + '-' + b.notebook_name

                # Save the notebook to display the static images
                display(Javascript('IPython.notebook.save_checkpoint();'))

                # Call nbconvert to do the export
                os.system(
                    "python3 -m nbconvert ./" + b.notebook_name +
                    ".ipynb --template=nbextensions --output-dir=./exports --output="
                    + output_file + " --to html")

                # For every plot registered in the plots_list, we have to set these
                # plots export flag to False to end the export
                for plot in b.plots_list:
                    plot.export = False

            except Exception as e:
                logprint(str(e), "[ERROR]", config=b.config)

            # Reenable button
            b.disabled = False
            b.button_style = 'warning'
            b.description = 'Export Notebook to HTML'
Esempio n. 11
0
    def _monitor_button(b):
        # Clear previous logs outputs
        b.output.clear_output()
        
        # with statement to output logs in stdou (if this option is enabled)
        with b.output:
            # Disabel button to avoid double commands
            b.disabled=True

            if b.monitoring_status:
                # Change button appearence
                b.description='Start PV Monitoring'
                b.button_style='success'

                # Stop displaying the motors widgets, show only button and text box
                b.main_box.children = (b.text, b)

                logprint("Stopped monitoring PVs " + ', '.join([pv.pvname for pv in b.pv_list]), config=b.config)

                # Reset motor list
                b.pv_list = []      
            else:
                # Change button appearence
                b.description='Stop PV Monitoring'
                b.button_style = 'danger'

                # Stop displaying text box
                b.main_box.children = (b,)

                # Get motors PV names from the text box
                # Filter names
                pv_list_names = []
                names_comma_space_ent = []
                names_space = b.text.value.split(' ')
                for name in names_space:
                    names_comma_space = name.split(",")

                    for name_wout_comma in names_comma_space:
                        names_comma_space_ent.append(name_wout_comma.split("\n"))

                lin_names = sum(names_comma_space_ent, [])
                pv_list_names = [name for name in lin_names if name != "" and name != "\n"]
                
                logprint("Started monitoring PVs " + ', '.join(pv_list_names), config=b.config)

                # Create PVs and add a monitor callback to them
                # Also add these PVs values as children of main_box widget
                try:
                    b.pv_list = [PV(name) for name in pv_list_names]

                    for pv in b.pv_list:
                        pv.add_callback(b._monitor_callback)

                        value = pv.get()
                        b.pv_values[pv.pvname] = (value, str(value))

                        b.main_box.children += (widgets.HBox([widgets.Label(pv.pvname), widgets.Label(str(value))]),)

                    logprint("Monitoring PVs " + ', '.join(pv_list_names), config=b.config)        

                except Exception as e:
                    # If any error occurs, log that but dont stop code exection
                    logprint("Error in monitoring PVs " + ', '.join(pv_list_names), "[ERROR]", config=b.config)
                    logprint(str(e), "[ERROR]", config=b.config)

            # Re enable button
            b.disabled=False

            # Switch status
            b.monitoring_status = not b.monitoring_status
Esempio n. 12
0
    def _scan_button(b):
        # Clear previous logs outputs
        b.output.clear_output()
        
        # with statement to output logs in stdout (if this option is enabled)
        with b.output:
            # Change button appearence
            b.description = 'Scanning'
            b.button_style = ''

            # Disable button to avoid double commands
            b.disabled = True

            # Disable box edition to avoid erros
            boxes = [b.text_motors, b.text_config, b.text_start, b.text_end, 
                    b.text_step_points, b.text_time, b.text_optimum, b.text_output]

            for box in boxes:
                box.disabled = True

            # Reset motor list
            b.motor_list = []

            # Get motors names from the text box
            motor_list_names = b.text_motors.value.split(' ')
            logprint("Scanning on motors" + ', '.join(motor_list_names), config=b.config)

            # Get config file name from the text box
            config_name = b.text_config.value
            logprint("YML config file: " + config_name, config=b.config)

            # Load scan parameters
            start = []
            end = []
            step_or_points = []
            time = []
            try:
                # Get lists from text boxes
                start = [eval(b.text_start.value)]
                end = [eval(b.text_end.value)]
                step_or_points = [eval(b.text_step_points.value)]
                time = [eval(b.text_time.value)]

                logprint("Loaded 'start, end, step or points, time' scan parameters", config=b.config)
            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint("Error loading 'start, end, step or points, time' scan parameters", "[ERROR]", config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            # Get output file name
            output = b.text_output.value if b.text_output.value != '' else 'test'
            
            # Get absolute path to file, and create a scans directory if the directory doesn't exist
            mypath = Path().absolute() / 'scans'
            if not mypath.is_dir():
                mypath.mkdir()
            
            # Put the path to the output
            output = str(mypath) + '/' + output
            
            # Get optimum move option
#             optimum = b.text_optimum.value if b.checkbox_optimum.value else False
            optimum = False

            # Get sync option
            sync = False
            # Initiate scan
            try:
                js = JupyScan(motor_list_names, start, end, step_or_points, time, config_name, optimum, sync, output)
                
                js.run()
                logprint("Finished scan, output saved in file " + output, config=b.config)
            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint("Error in trying to scan", "[ERROR]", config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            # Change button appearence
            b.description = 'Start Scan'
            b.button_style = 'success'

            # Re enable button
            b.disabled = False

            # Re enable box edition 
            for box in boxes:
                box.disabled = False
Esempio n. 13
0
    def _scan_button(b):
        # Clear previous logs outputs
        b.output.clear_output()

        # with statement to output logs in stdout (if this option is enabled)
        with b.output:
            # Change button appearence
            b.description = 'Scanning'
            b.button_style = ''

            # Disable button to avoid double commands
            b.disabled = True

            # Disable box edition to avoid erros
            boxes = [
                b.text_motors, b.text_config, b.text_start, b.text_end,
                b.text_step_points, b.text_time, b.text_optimum, b.text_output
            ]

            for box in boxes:
                box.disabled = True

            # Reset motor list
            b.motor_list = []

            # Get motors names from the text box
            names_comma_space_ent = []
            motor_list_names = b.text_motors.value.split(' ')
            for name in motor_list_names:
                names_comma_space = name.split(",")

                for name_wout_comma in names_comma_space:
                    names_comma_space_ent.append(name_wout_comma.split("\n"))

            lin_names = sum(names_comma_space_ent, [])
            motor_list_names = [
                name for name in lin_names if name != "" and name != "\n"
            ]
            logprint("Scanning on motors" + ', '.join(motor_list_names),
                     config=b.config)

            # Get config file name from the text box
            config_name = b.text_config.value
            logprint("YML config file: " + config_name, config=b.config)

            # Load scan parameters
            start = []
            end = []
            step_or_points = []
            time = []
            try:
                # Get lists from text boxes
                start = b.text_start.value
                end = b.text_end.value
                step_or_points = b.text_step_points.value
                time = b.text_time.value

                logprint(
                    "Loaded 'start, end, step or points, time' scan parameters",
                    config=b.config)
            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint(
                    "Error loading 'start, end, step or points, time' scan parameters",
                    "[ERROR]",
                    config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            # Get output file name
            output = b.text_output.value if b.text_output.value != '' else 'test'

            # Get absolute path to file, and create a scans directory if the directory doesn't exist
            mypath = Path().absolute() / 'scans'
            if not mypath.is_dir():
                mypath.mkdir()

            # Put the path to the output
            output = str(mypath) + '/' + output

            # Edge
            edge = 0.0
            # Initiate scan
            try:
                command = "/usr/local/SOL/scan-utils/energy-scan" + \
                          " -c " + config_name + \
                          " -o " + output + \
                          " --motor " + ' '.join(motor_list_names) + \
                          " --start " + start + \
                          " --end " + end + \
                          " --step-or-points " + step_or_points + \
                          " --time " + time + \
                          " --edge " + str(edge)

                subprocess.run([command], shell=True, check=True)
                logprint("Started scan, output saved in file " + output,
                         config=b.config)

            except Exception as e:
                # If any error occurs, log that but dont stop code exection
                logprint("Error in trying to energy scan",
                         "[ERROR]",
                         config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            b.scan_names = b.get_scan_name(output, 1)

            dfs = []
            dfs.append(pd.DataFrame())
            while dfs[0].empty:
                label = []
                dfs, label = b.update_pd(b.scan_names, label)

            number_motors = len(motor_list_names)

            b.create_figure(len(dfs[0].columns) - number_motors)

            # Plot
            try:
                for i in range(len(dfs)):
                    if dfs[i].empty:
                        continue

                    diff_df = dfs[i].diff().dropna()
                    diff_diff_df = diff_df[i].diff().dropna()

                    for j in range(len(dfs[i].columns) - number_motors):
                        # Plot function
                        b.fig['data'][i + j * len(dfs) +
                                      j]['x'] = dfs[i].index.values
                        b.fig['data'][i + j * len(dfs) + j]['y'] = dfs[i][
                            dfs[i].columns[number_motors + j]].values

                        # Plot First Diff function
                        b.fig['data'][i + 1 + j * len(dfs) +
                                      j]['x'] = diff_df.index.values
                        b.fig['data'][i + 1 + j * len(dfs) + j]['y'] = (
                            diff_df[diff_df.columns[number_motors + j]] /
                            diff_df[0]).values

                        # Plot Second Diff function
                        b.fig['data'][i + 2 + j * len(dfs) +
                                      j]['x'] = diff_diff_df.index.values
                        b.fig['data'][i + 2 + j * len(dfs) + j]['y'] = (
                            diff_diff_df[diff_diff_df.columns[number_motors +
                                                              j]] /
                            diff_df[0]).values

            except Exception as e:
                logprint("Error in trying to plot energy scan",
                         "[ERROR]",
                         config=b.config)
                logprint(str(e), "[ERROR]", config=b.config)

            # Change button appearence
            b.description = 'Start Energy Scan'
            b.button_style = 'success'

            # Re enable button
            b.disabled = False

            # Re enable box edition
            for box in boxes:
                box.disabled = False
Esempio n. 14
0
    def _monitor_button(b):
        # Clear previous logs outputs
        b.output.clear_output()

        # with statement to output logs in stdou (if this option is enabled)
        with b.output:
            # Disabel button to avoid double commands
            b.disabled = True

            if b.monitoring_status:
                # Change button appearence
                b.description = 'Start Motor Monitoring'
                b.button_style = 'success'

                # Stop displaying the motors widgets, show only button and text box
                b.main_box.children = (
                    b.main_box.children[0],
                    b.text,
                )

                logprint("Stopped monitoring motors " +
                         ', '.join([motor.pvName for motor in b.motors_list]),
                         config=b.config)

                # Reset motor list
                b.motors_list = []
            else:
                # Change button appearence
                b.description = 'Stop Motor Monitoring'
                b.button_style = 'danger'

                # Stop displaying text box
                b.main_box.children = (b.main_box.children[0], )

                # Get motors PV names from the text box
                motor_list_names = b.text.value.split(' ')
                logprint("Started monitoring motors " +
                         ', '.join(motor_list_names),
                         config=b.config)

                # Create motors and add a monitor callback to them
                # Also add these motor values as children of main_box widget
                try:
                    b.motors_list = [
                        configurate_motor(name, ''.join(name.split(':')[-2:]))
                        for name in motor_list_names
                    ]

                    for motor in b.motors_list:
                        index = motor.motor.add_callback(
                            'RBV', b._monitor_callback)

                        rbv = motor.getRealPosition()
                        b.motors_values[motor.pvName + ".RBV"] = (rbv,
                                                                  str(rbv))

                        b.main_box.children += (widgets.HBox([
                            widgets.Label(motor.pvName + ".RBV"),
                            widgets.Label(str(rbv))
                        ]), )

                    logprint("Monitoring motors " +
                             ', '.join(motor_list_names),
                             config=b.config)

                except Exception as e:
                    # If any error occurs, log that but dont stop code exection
                    logprint("Error in monitoring motors " +
                             ', '.join(motor_list_names),
                             "[ERROR]",
                             config=b.config)
                    logprint(str(e), "[ERROR]", config=b.config)

            # Re enable button
            b.disabled = False

            # Switch status
            b.monitoring_status = not b.monitoring_status