def get_acquisition_handler_fn(multi_acquire_parameters, current_parameters_index, multi_acquire_settings):
                camera_frame_parameters = camera_hardware_source.get_current_frame_parameters()
                scan_frame_parameters = scan_hardware_source.get_current_frame_parameters()
                camera_frame_parameters.exposure_ms = multi_acquire_parameters[current_parameters_index]['exposure_ms']
                camera_frame_parameters.processing = multi_acquire_settings['processing']
                scan_frame_parameters.scan_id = scan_frame_parameters.scan_id or uuid.uuid4()
                grab_synchronized_info = scan_hardware_source.grab_synchronized_get_info(scan_frame_parameters=scan_frame_parameters,
                                                                                    camera=camera_hardware_source,
                                                                                    camera_frame_parameters=camera_frame_parameters)

                camera_data_channel = MultiAcquire.CameraDataChannel(document_model, camera_hardware_source.display_name, grab_synchronized_info,
                                                                     multi_acquire_parameters, multi_acquire_settings, current_parameters_index,
                                                                     stack_metadata_keys=[['hardware_source', 'defocus']])
                enabled_channels = scan_hardware_source.get_enabled_channels()
                enabled_channel_names = [scan_hardware_source.data_channels[i].name for i in enabled_channels]
                scan_data_channel = MultiAcquire.ScanDataChannel(document_model, enabled_channel_names, grab_synchronized_info,
                                                                 multi_acquire_parameters, multi_acquire_settings, current_parameters_index)
                camera_data_channel.start()
                scan_data_channel.start()
                sequence_behavior = MultiAcquire.SequenceBehavior(multi_acquire_controller, current_parameters_index)
                si_sequence_behavior = MultiAcquire.SISequenceBehavior(None, None, sequence_behavior, 1)
                handler =  MultiAcquire.SISequenceAcquisitionHandler(camera_hardware_source, camera_data_channel, camera_frame_parameters,
                                                                     scan_hardware_source, scan_data_channel, scan_frame_parameters,
                                                                     si_sequence_behavior)

                listener = handler.camera_data_channel.progress_updated_event.listen(multi_acquire_controller.set_progress_counter)

                def finish_fn():
                    listener.close()
                    handler.camera_data_channel.stop()
                    handler.scan_data_channel.stop()

                handler.finish_fn = finish_fn

                return handler
 def create_channels() -> None:
     assert camera
     assert scan_controller
     nonlocal camera_data_channel, scan_data_channel
     stack_metadata_keys = getattr(camera.camera,
                                   'stack_metadata_keys',
                                   None)
     camera_data_channel = MultiAcquire.CameraDataChannel(
         document_model,
         camera.display_name,
         grab_synchronized_info,
         multi_acquire_parameters,
         multi_acquire_settings,
         current_parameters_index,
         stack_metadata_keys=stack_metadata_keys)
     enabled_channels = scan_controller.get_enabled_channels(
     )
     enabled_channel_names = [
         scan_controller.data_channels[i].name or str()
         for i in enabled_channels
     ]
     scan_data_channel = MultiAcquire.ScanDataChannel(
         document_model, enabled_channel_names,
         grab_synchronized_info, multi_acquire_parameters,
         multi_acquire_settings, current_parameters_index)
     camera_data_channel.start()
     scan_data_channel.start()
     channels_ready_event.set()
                            def get_acquisition_handler_fn(
                                    multi_acquire_parameters,
                                    current_parameters_index,
                                    multi_acquire_settings):
                                camera_frame_parameters = camera.get_current_frame_parameters(
                                )
                                scan_frame_parameters = scan_controller.get_current_frame_parameters(
                                )
                                camera_frame_parameters[
                                    'exposure_ms'] = multi_acquire_parameters[
                                        current_parameters_index][
                                            'exposure_ms']
                                camera_frame_parameters[
                                    'processing'] = 'sum_project' if multi_acquire_settings[
                                        'bin_spectra'] else None
                                scan_frame_parameters.setdefault(
                                    'scan_id', str(uuid.uuid4()))
                                grab_synchronized_info = scan_controller.grab_synchronized_get_info(
                                    scan_frame_parameters=scan_frame_parameters,
                                    camera=camera,
                                    camera_frame_parameters=
                                    camera_frame_parameters)

                                camera_data_channel = MultiAcquire.CameraDataChannel(
                                    document_model, camera.display_name,
                                    grab_synchronized_info,
                                    multi_acquire_parameters,
                                    multi_acquire_settings,
                                    current_parameters_index)
                                enabled_channels = scan_controller.get_enabled_channels(
                                )
                                enabled_channel_names = [
                                    scan_controller.data_channels[i].name
                                    for i in enabled_channels
                                ]
                                scan_data_channel = MultiAcquire.ScanDataChannel(
                                    document_model, enabled_channel_names,
                                    grab_synchronized_info,
                                    multi_acquire_parameters,
                                    multi_acquire_settings,
                                    current_parameters_index)
                                camera_data_channel.start()
                                scan_data_channel.start()
                                handler = MultiAcquire.SISequenceAcquisitionHandler(
                                    camera, camera_data_channel,
                                    camera_frame_parameters, scan_controller,
                                    scan_data_channel, scan_frame_parameters)

                                listener = handler.camera_data_channel.progress_updated_event.listen(
                                    multi_acquire_controller.
                                    set_progress_counter)

                                def finish_fn():
                                    listener.close()
                                    handler.camera_data_channel.stop()
                                    handler.scan_data_channel.stop()

                                handler.finish_fn = finish_fn

                                return handler
Example #4
0
 def __init__(self, api):
     self.__api = api
     self.panel_id = 'MultiAcquire-Panel'
     self.panel_name = 'Multi-Acquire'
     self.panel_positions = ['left', 'right']
     self.panel_position = 'right'
     self.api = api
     self.line_edit_widgets = {}
     self.push_button_widgets = {}
     self.multi_acquire_controller = MultiAcquire.MultiAcquireController()
     self.__acquisition_state_changed_event_listener = self.multi_acquire_controller.acquisition_state_changed_event.listen(
         self.acquisition_state_changed)
     self.__multi_eels_parameters_changed_event_listener = self.multi_acquire_controller.spectrum_parameters.parameters_changed_event.listen(
         self.spectrum_parameters_changed)
     self.__progress_updated_event_listener = self.multi_acquire_controller.progress_updated_event.listen(
         self.update_progress_bar)
     self.__settings_changed_event_listener = None
     self.__component_registered_event_listener = None
     self.__component_unregistered_event_listener = None
     self.__superscan_frame_parameters_changed_event_listener = None
     self.__new_data_ready_event_listener = None
     self._stem_controller = None
     self.eels_camera = None
     self._superscan = None
     self.settings_window_open = False
     self.parameters_window_open = False
     self.parameter_column = None
     self.result_data_items = {}
     self.__result_data_items_refs = []
     self.__acquisition_running = False
     self.__display_queue = queue.Queue()
     self.__display_thread = None
     self.__acquisition_thread = None
     self.__data_processed_event = threading.Event()
Example #5
0
 def _set_up_multi_acquire(self,
                           settings: dict,
                           parameters: list,
                           multi_acquire_instance=None):
     multi_acquire = multi_acquire_instance or MultiAcquire.MultiAcquireController(
     )
     multi_acquire._MultiAcquireController__savepath = None
     multi_acquire.settings.update(settings)
     multi_acquire.spectrum_parameters[:] = parameters
     return multi_acquire
 def create_channels():
     nonlocal camera_data_channel, scan_data_channel
     camera_data_channel = MultiAcquire.CameraDataChannel(
         document_model, camera.display_name,
         grab_synchronized_info, multi_acquire_parameters,
         multi_acquire_settings, current_parameters_index)
     enabled_channels = self.scan_controller.get_enabled_channels(
     )
     enabled_channel_names = [
         self.scan_controller.data_channels[i].name
         for i in enabled_channels
     ]
     scan_data_channel = MultiAcquire.ScanDataChannel(
         document_model, enabled_channel_names,
         grab_synchronized_info, multi_acquire_parameters,
         multi_acquire_settings, current_parameters_index)
     camera_data_channel.start()
     scan_data_channel.start()
     channels_ready_event.set()
                def create_acquisition_handler(
                    multi_acquire_parameters: MultiAcquire.MultiEELSParameters,
                    current_parameters_index: int,
                    multi_acquire_settings: MultiAcquire.MultiEELSSettings
                ) -> MultiAcquire.SISequenceAcquisitionHandler:
                    assert camera
                    assert scan_controller
                    assert multi_acquire_controller
                    document_model = self.document_controller._document_controller.document_model
                    camera_frame_parameters = camera.get_current_frame_parameters(
                    )
                    scan_frame_parameters = scan_controller.get_current_frame_parameters(
                    )
                    camera_frame_parameters.exposure_ms = multi_acquire_parameters[
                        current_parameters_index]['exposure_ms']
                    camera_frame_parameters.processing = multi_acquire_settings[
                        'processing']
                    scan_frame_parameters.scan_id = scan_frame_parameters.scan_id or uuid.uuid4(
                    )
                    grab_synchronized_info = scan_controller.grab_synchronized_get_info(
                        scan_frame_parameters=scan_frame_parameters,
                        camera=camera,
                        camera_frame_parameters=camera_frame_parameters)
                    camera_data_channel: typing.Optional[
                        MultiAcquire.CameraDataChannel] = None
                    scan_data_channel: typing.Optional[
                        MultiAcquire.ScanDataChannel] = None
                    channels_ready_event = threading.Event()

                    def create_channels() -> None:
                        assert camera
                        assert scan_controller
                        nonlocal camera_data_channel, scan_data_channel
                        stack_metadata_keys = getattr(camera.camera,
                                                      'stack_metadata_keys',
                                                      None)
                        camera_data_channel = MultiAcquire.CameraDataChannel(
                            document_model,
                            camera.display_name,
                            grab_synchronized_info,
                            multi_acquire_parameters,
                            multi_acquire_settings,
                            current_parameters_index,
                            stack_metadata_keys=stack_metadata_keys)
                        enabled_channels = scan_controller.get_enabled_channels(
                        )
                        enabled_channel_names = [
                            scan_controller.data_channels[i].name or str()
                            for i in enabled_channels
                        ]
                        scan_data_channel = MultiAcquire.ScanDataChannel(
                            document_model, enabled_channel_names,
                            grab_synchronized_info, multi_acquire_parameters,
                            multi_acquire_settings, current_parameters_index)
                        camera_data_channel.start()
                        scan_data_channel.start()
                        channels_ready_event.set()

                    self.document_controller.queue_task(create_channels)
                    assert channels_ready_event.wait(10)

                    assert camera_data_channel
                    assert scan_data_channel

                    sequence_behavior = MultiAcquire.SequenceBehavior(
                        multi_acquire_controller, current_parameters_index)
                    si_sequence_behavior = MultiAcquire.SISequenceBehavior(
                        None, None, sequence_behavior, 1)
                    handler = MultiAcquire.SISequenceAcquisitionHandler(
                        camera, camera_data_channel, camera_frame_parameters,
                        scan_controller, scan_data_channel,
                        scan_frame_parameters, si_sequence_behavior)

                    listener = handler.camera_data_channel.progress_updated_event.listen(
                        multi_acquire_controller.set_progress_counter)

                    def finish_fn() -> None:
                        listener.close()

                        def close_channels() -> None:
                            handler.camera_data_channel.stop()
                            handler.scan_data_channel.stop()

                        self.document_controller.queue_task(close_channels)

                    handler.finish_fn = finish_fn

                    return handler
    def create_panel_widget(
            self, ui: Facade.UserInterface,
            document_controller: Facade.DocumentWindow) -> Facade.ColumnWidget:
        # note: anything created here should be disposed in close.
        # this method may be called more than once.

        if not self.stem_controller:
            return ui.create_column_widget()

        self.line_edit_widgets = dict()
        self._stem_controller = None
        self._camera = None
        self._scan_controller = None
        self.multi_acquire_controller = MultiAcquire.MultiAcquireController(
            self.stem_controller,
            savepath=os.path.join(os.path.expanduser('~'), 'MultiAcquire'))
        self.__acquisition_state_changed_event_listener = self.multi_acquire_controller.acquisition_state_changed_event.listen(
            self.acquisition_state_changed)
        self.__multi_eels_parameters_changed_event_listener = self.multi_acquire_controller.spectrum_parameters.parameters_changed_event.listen(
            self.spectrum_parameters_changed)
        self.__progress_updated_event_listener = self.multi_acquire_controller.progress_updated_event.listen(
            self.update_progress_bar)
        self.__settings_changed_event_listener = None
        self.__component_registered_event_listener = None
        self.__component_unregistered_event_listener = None
        self.__scan_frame_parameters_changed_event_listener = None
        self.__new_scan_data_ready_event_listener = None

        self.settings_window_open = False
        self.parameter_column = None
        self.result_data_items = None
        self.__acquisition_running = False
        self.__acquisition_thread = None

        self.ui = ui
        self.document_controller = document_controller

        def start_clicked() -> None:
            multi_acquire_controller = self.multi_acquire_controller
            if not multi_acquire_controller:
                return
            if self.__acquisition_running:
                multi_acquire_controller.cancel()
            else:
                multi_acquire_controller.stem_controller = self.stem_controller
                multi_acquire_controller.camera = self.camera

                def run_multi_eels() -> None:
                    assert multi_acquire_controller
                    data_dict = multi_acquire_controller.acquire_multi_eels_spectrum(
                    )

                    def create_and_display_data_item() -> None:
                        self.create_result_data_item(data_dict)

                    document_controller.queue_task(create_and_display_data_item
                                                   )  # must occur on UI thread

                self.__acquisition_thread = threading.Thread(
                    target=run_multi_eels, daemon=True)
                self.__acquisition_thread.start()

        def start_si_clicked() -> None:
            multi_acquire_controller = self.multi_acquire_controller
            if not multi_acquire_controller:
                return
            if self.__acquisition_running:
                multi_acquire_controller.cancel()
            else:
                # Camera must be accessed from the UI thread, so do it here and re-use later
                camera = self.camera
                scan_controller = self.scan_controller
                assert camera
                assert scan_controller

                multi_acquire_controller.stem_controller = self.stem_controller
                multi_acquire_controller.camera = camera
                multi_acquire_controller.scan_controller = scan_controller

                def create_acquisition_handler(
                    multi_acquire_parameters: MultiAcquire.MultiEELSParameters,
                    current_parameters_index: int,
                    multi_acquire_settings: MultiAcquire.MultiEELSSettings
                ) -> MultiAcquire.SISequenceAcquisitionHandler:
                    assert camera
                    assert scan_controller
                    assert multi_acquire_controller
                    document_model = self.document_controller._document_controller.document_model
                    camera_frame_parameters = camera.get_current_frame_parameters(
                    )
                    scan_frame_parameters = scan_controller.get_current_frame_parameters(
                    )
                    camera_frame_parameters.exposure_ms = multi_acquire_parameters[
                        current_parameters_index]['exposure_ms']
                    camera_frame_parameters.processing = multi_acquire_settings[
                        'processing']
                    scan_frame_parameters.scan_id = scan_frame_parameters.scan_id or uuid.uuid4(
                    )
                    grab_synchronized_info = scan_controller.grab_synchronized_get_info(
                        scan_frame_parameters=scan_frame_parameters,
                        camera=camera,
                        camera_frame_parameters=camera_frame_parameters)
                    camera_data_channel: typing.Optional[
                        MultiAcquire.CameraDataChannel] = None
                    scan_data_channel: typing.Optional[
                        MultiAcquire.ScanDataChannel] = None
                    channels_ready_event = threading.Event()

                    def create_channels() -> None:
                        assert camera
                        assert scan_controller
                        nonlocal camera_data_channel, scan_data_channel
                        stack_metadata_keys = getattr(camera.camera,
                                                      'stack_metadata_keys',
                                                      None)
                        camera_data_channel = MultiAcquire.CameraDataChannel(
                            document_model,
                            camera.display_name,
                            grab_synchronized_info,
                            multi_acquire_parameters,
                            multi_acquire_settings,
                            current_parameters_index,
                            stack_metadata_keys=stack_metadata_keys)
                        enabled_channels = scan_controller.get_enabled_channels(
                        )
                        enabled_channel_names = [
                            scan_controller.data_channels[i].name or str()
                            for i in enabled_channels
                        ]
                        scan_data_channel = MultiAcquire.ScanDataChannel(
                            document_model, enabled_channel_names,
                            grab_synchronized_info, multi_acquire_parameters,
                            multi_acquire_settings, current_parameters_index)
                        camera_data_channel.start()
                        scan_data_channel.start()
                        channels_ready_event.set()

                    self.document_controller.queue_task(create_channels)
                    assert channels_ready_event.wait(10)

                    assert camera_data_channel
                    assert scan_data_channel

                    sequence_behavior = MultiAcquire.SequenceBehavior(
                        multi_acquire_controller, current_parameters_index)
                    si_sequence_behavior = MultiAcquire.SISequenceBehavior(
                        None, None, sequence_behavior, 1)
                    handler = MultiAcquire.SISequenceAcquisitionHandler(
                        camera, camera_data_channel, camera_frame_parameters,
                        scan_controller, scan_data_channel,
                        scan_frame_parameters, si_sequence_behavior)

                    listener = handler.camera_data_channel.progress_updated_event.listen(
                        multi_acquire_controller.set_progress_counter)

                    def finish_fn() -> None:
                        listener.close()

                        def close_channels() -> None:
                            handler.camera_data_channel.stop()
                            handler.scan_data_channel.stop()

                        self.document_controller.queue_task(close_channels)

                    handler.finish_fn = finish_fn

                    return handler

                self.__acquisition_thread = threading.Thread(
                    target=multi_acquire_controller.
                    start_multi_acquire_spectrum_image,
                    args=(create_acquisition_handler, ))
                self.__acquisition_thread.start()

        def settings_button_clicked() -> None:
            if not self.settings_window_open:
                self.settings_window_open = True
                self.show_config_box()

        def help_clicked() -> None:
            webbrowser.open(
                'https://nionswift-instrumentation.readthedocs.io/en/latest/userguide.html#multi-acquire-panel',
                new=2)

        def camera_changed(
                current_item: camera_base.CameraHardwareSource) -> None:
            multi_acquire_controller = self.multi_acquire_controller
            if current_item and multi_acquire_controller:
                multi_acquire_controller.settings[
                    'camera_hardware_source_id'] = current_item.hardware_source_id
                if current_item.features.get("has_masked_sum_option"):
                    self.binning_choice_combo_box.items = [
                        'Spectra', 'Images', 'MultiEELS Spectra',
                        'Virtual Detectors'
                    ]
                else:
                    self.binning_choice_combo_box.items = [
                        'Spectra', 'Images', 'MultiEELS Spectra'
                    ]
                binning_changed(
                    typing.cast(str,
                                self.binning_choice_combo_box.current_item))

        def binning_changed(current_item: str) -> None:
            multi_acquire_controller = self.multi_acquire_controller
            if not multi_acquire_controller:
                return
            if not self.__acquisition_running:
                self.start_button._widget.enabled = True
            if current_item == 'Spectra':
                multi_acquire_controller.settings['processing'] = 'sum_project'
                multi_acquire_controller.settings[
                    'use_multi_eels_calibration'] = False
            elif current_item == 'Images':
                multi_acquire_controller.settings['processing'] = None
                multi_acquire_controller.settings[
                    'use_multi_eels_calibration'] = False
            elif current_item == 'MultiEELS Spectra':
                multi_acquire_controller.settings['processing'] = 'sum_project'
                multi_acquire_controller.settings[
                    'use_multi_eels_calibration'] = True
            elif current_item == 'Virtual Detectors':
                multi_acquire_controller.settings['processing'] = 'sum_masked'
                multi_acquire_controller.settings[
                    'use_multi_eels_calibration'] = False
                self.start_button._widget.enabled = False

        camera_choice_row = ui.create_row_widget()
        self.binning_choice_combo_box = ui.create_combo_box_widget()
        # Delay connecting the callback functions because otherwise loading the plugin fails because "start button" is not defined yet
        self.binning_choice_combo_box.items = [
            'Spectra', 'Images', 'MultiEELS Spectra'
        ]
        sliders_icon_png = pkgutil.get_data(__name__,
                                            "resources/sliders_icon_24.png")
        assert sliders_icon_png
        settings_button = CanvasItem.BitmapButtonCanvasItem(
            CanvasItem.load_rgba_data_from_bytes(sliders_icon_png, "png"))
        settings_widget = ui._ui.create_canvas_widget(properties={
            "height": 24,
            "width": 24
        })
        settings_widget.canvas_item.add_canvas_item(settings_button)
        settings_button.on_button_clicked = settings_button_clicked
        help_icon_png = pkgutil.get_data(__name__,
                                         "resources/help_icon_24.png")
        assert help_icon_png
        help_button = CanvasItem.BitmapButtonCanvasItem(
            CanvasItem.load_rgba_data_from_bytes(help_icon_png, "png"))
        help_widget = ui._ui.create_canvas_widget(properties={
            "height": 24,
            "width": 24
        })
        help_widget.canvas_item.add_canvas_item(help_button)
        help_button.on_button_clicked = help_clicked
        self.camera_choice_combo_box = ui.create_combo_box_widget(
            item_text_getter=lambda camera: typing.cast(
                str, getattr(camera, 'display_name')))
        camera_choice_row.add_spacing(5)
        camera_choice_row.add(self.camera_choice_combo_box)
        camera_choice_row.add_spacing(10)
        camera_choice_row.add(self.binning_choice_combo_box)
        camera_choice_row.add_stretch()
        camera_choice_row.add_spacing(10)
        typing.cast(UserInterface.BoxWidget,
                    camera_choice_row._widget).add(help_widget)
        camera_choice_row.add_spacing(10)
        typing.cast(UserInterface.BoxWidget,
                    camera_choice_row._widget).add(settings_widget)
        camera_choice_row.add_spacing(10)
        self.update_camera_list()
        self.update_current_camera()
        self.__settings_changed_event_listener = self.multi_acquire_controller.settings.settings_changed_event.listen(
            self.update_current_camera)
        self.__settings_changed_event_listener_2 = self.multi_acquire_controller.settings.settings_changed_event.listen(
            self.update_binning_combo_box)

        def component_changed(component: typing.Any,
                              component_types: typing.Set[str]) -> None:
            if 'camera_hardware_source' in component_types:
                self.update_camera_list()

        self.__component_registered_event_listener = Registry.listen_component_registered_event(
            component_changed)
        self.__component_unregistered_event_listener = Registry.listen_component_unregistered_event(
            component_changed)

        change_parameters_row = ui.create_row_widget()
        change_parameters_row.add_spacing(5)
        change_parameters_row.add(
            ui.create_label_widget('MultiAcquire parameters:'))
        change_parameters_row.add_stretch()
        change_parameters_row.add_spacing(20)

        parameter_description_row = ui.create_row_widget()
        parameter_description_row.add_spacing(5)
        parameter_description_row.add(ui.create_label_widget('#'))
        parameter_description_row.add_spacing(20)
        offset_label = ui.create_label_widget('Offset')
        parameter_description_row.add(offset_label)
        parameter_description_row.add_stretch()
        parameter_description_row.add(ui.create_label_widget('Exposure (ms)'))
        parameter_description_row.add_stretch()
        parameter_description_row.add_stretch()
        parameter_description_row.add_stretch()
        frames_label = ui.create_label_widget('Frames')
        parameter_description_row.add(frames_label)
        parameter_description_row.add_stretch()

        def update_offset_label() -> None:
            multi_acquire_controller = self.multi_acquire_controller
            if not multi_acquire_controller:
                return
            if multi_acquire_controller.settings['shift_each_sequence_slice']:
                offset_label.text = 'Offset (per frame)  '
            else:
                offset_label.text = 'Offset                   '
            if multi_acquire_controller.settings['sum_frames']:
                frames_label.text = 'Frames (summed)'
            else:
                frames_label.text = 'Frames              '

        update_offset_label()
        self.__settings_changed_event_listener_3 = self.multi_acquire_controller.settings.settings_changed_event.listen(
            update_offset_label)

        add_remove_parameters_row = ui.create_row_widget()
        add_parameters_button = ui.create_push_button_widget('+')
        add_parameters_button._widget.set_property('width', 40)
        add_parameters_button.on_clicked = self.multi_acquire_controller.add_spectrum
        remove_parameters_button = ui.create_push_button_widget('-')
        remove_parameters_button._widget.set_property('width', 40)
        remove_parameters_button.on_clicked = self.multi_acquire_controller.remove_spectrum

        add_remove_parameters_row.add_spacing(5)
        add_remove_parameters_row.add(add_parameters_button)
        add_remove_parameters_row.add_spacing(5)
        add_remove_parameters_row.add(remove_parameters_button)
        add_remove_parameters_row.add_spacing(90)
        self.progress_bar = ui.create_progress_bar_widget()
        add_remove_parameters_row.add(self.progress_bar)
        add_remove_parameters_row.add_spacing(5)

        time_estimate_row = ui.create_row_widget()
        time_estimate_row.add_spacing(6)
        self.time_estimate_label = ui.create_label_widget()
        time_estimate_row.add(self.time_estimate_label)
        time_estimate_row.add_spacing(5)
        time_estimate_row.add_stretch()
        self.si_time_estimate_label = ui.create_label_widget()
        time_estimate_row.add(self.si_time_estimate_label)
        time_estimate_row.add_spacing(6)
        self.multi_acquire_controller.stem_controller = self.stem_controller
        self.multi_acquire_controller.scan_controller = self.scan_controller

        def frame_parameters_changed(
                profile_index: int,
                frame_parameters: scan_base.ScanFrameParameters) -> None:
            self.update_time_estimate()

        if self.scan_controller:
            self.__scan_frame_parameters_changed_event_listener = self.scan_controller.frame_parameters_changed_event.listen(
                frame_parameters_changed)
        self.update_camera_list()

        self.start_button = ui.create_push_button_widget('Start MultiAcquire')
        self.start_button.on_clicked = start_clicked
        self.start_si_button = ui.create_push_button_widget(
            'Start MultiAcquire spectrum image')
        self.start_si_button.on_clicked = start_si_clicked
        start_row = ui.create_row_widget()
        start_row.add_spacing(5)
        start_row.add(self.start_button)
        start_row.add_spacing(5)
        start_row.add_stretch()
        start_row.add(self.start_si_button)
        start_row.add_spacing(5)

        column = ui.create_column_widget()
        column.add_spacing(5)
        column.add(camera_choice_row)
        column.add_spacing(10)
        column.add(change_parameters_row)
        column.add_spacing(5)
        column.add(parameter_description_row)
        column.add_spacing(10)
        self.parameter_column = ui.create_column_widget()
        for spectrum_parameters in self.multi_acquire_controller.spectrum_parameters:
            line = self.create_parameter_line(spectrum_parameters)
            self.parameter_column.add(line)
        column.add(self.parameter_column)
        column.add_spacing(5)
        column.add(add_remove_parameters_row)
        column.add_spacing(15)
        column.add(time_estimate_row)
        column.add_spacing(5)
        column.add(start_row)
        column.add_spacing(10)
        column.add_stretch()
        # Make sure we update the available options in the binning combo box.
        camera_changed(
            typing.cast(camera_base.CameraHardwareSource,
                        self.camera_choice_combo_box.current_item))
        # Make sure the binning combo box shows the actual settings
        self.update_binning_combo_box()
        # Delay setting up the callbacks until the end to make sure loading the plugin doesn't fail due to missing attributes
        self.binning_choice_combo_box.on_current_item_changed = binning_changed
        self.camera_choice_combo_box.on_current_item_changed = camera_changed
        return column
                def create_acquisition_handler(multi_acquire_parameters: list,
                                               current_parameters_index: int,
                                               multi_acquire_settings: dict):
                    document_model = self.document_controller._document_controller.document_model
                    camera_frame_parameters = camera.get_current_frame_parameters(
                    )
                    scan_frame_parameters = self.scan_controller.get_current_frame_parameters(
                    )
                    camera_frame_parameters[
                        'exposure_ms'] = multi_acquire_parameters[
                            current_parameters_index]['exposure_ms']
                    camera_frame_parameters[
                        'processing'] = 'sum_project' if multi_acquire_settings[
                            'bin_spectra'] else None
                    scan_frame_parameters.setdefault('scan_id',
                                                     str(uuid.uuid4()))
                    grab_synchronized_info = self.scan_controller.grab_synchronized_get_info(
                        scan_frame_parameters=scan_frame_parameters,
                        camera=camera,
                        camera_frame_parameters=camera_frame_parameters)
                    camera_data_channel = None
                    scan_data_channel = None
                    channels_ready_event = threading.Event()

                    def create_channels():
                        nonlocal camera_data_channel, scan_data_channel
                        camera_data_channel = MultiAcquire.CameraDataChannel(
                            document_model, camera.display_name,
                            grab_synchronized_info, multi_acquire_parameters,
                            multi_acquire_settings, current_parameters_index)
                        enabled_channels = self.scan_controller.get_enabled_channels(
                        )
                        enabled_channel_names = [
                            self.scan_controller.data_channels[i].name
                            for i in enabled_channels
                        ]
                        scan_data_channel = MultiAcquire.ScanDataChannel(
                            document_model, enabled_channel_names,
                            grab_synchronized_info, multi_acquire_parameters,
                            multi_acquire_settings, current_parameters_index)
                        camera_data_channel.start()
                        scan_data_channel.start()
                        channels_ready_event.set()

                    self.document_controller.queue_task(create_channels)
                    assert channels_ready_event.wait(10)

                    si_sequence_behavior = MultiAcquire.SISequenceBehavior(
                        None, None, None, None)
                    handler = MultiAcquire.SISequenceAcquisitionHandler(
                        camera, camera_data_channel, camera_frame_parameters,
                        self.scan_controller, scan_data_channel,
                        scan_frame_parameters, si_sequence_behavior)

                    listener = handler.camera_data_channel.progress_updated_event.listen(
                        self.multi_acquire_controller.set_progress_counter)

                    def finish_fn():
                        listener.close()

                        def close_channels():
                            handler.camera_data_channel.stop()
                            handler.scan_data_channel.stop()

                        self.document_controller.queue_task(close_channels)

                    handler.finish_fn = finish_fn

                    return handler
    def create_panel_widget(self, ui, document_controller):
        # note: anything created here should be disposed in close.
        # this method may be called more than once.

        self.line_edit_widgets = {}
        self._stem_controller = None
        self._camera = None
        self._scan_controller = None
        self.multi_acquire_controller = MultiAcquire.MultiAcquireController(
            self.stem_controller,
            savepath=os.path.join(os.path.expanduser('~'), 'MultiAcquire'))
        self.__acquisition_state_changed_event_listener = self.multi_acquire_controller.acquisition_state_changed_event.listen(
            self.acquisition_state_changed)
        self.__multi_eels_parameters_changed_event_listener = self.multi_acquire_controller.spectrum_parameters.parameters_changed_event.listen(
            self.spectrum_parameters_changed)
        self.__progress_updated_event_listener = self.multi_acquire_controller.progress_updated_event.listen(
            self.update_progress_bar)
        self.__settings_changed_event_listener = None
        self.__component_registered_event_listener = None
        self.__component_unregistered_event_listener = None
        self.__scan_frame_parameters_changed_event_listener = None
        self.__new_scan_data_ready_event_listener = None

        self.settings_window_open = False
        self.parameter_column = None
        self.result_data_items = None
        self.__acquisition_running = False
        self.__display_queue = None
        self.__display_thread = None
        self.__acquisition_thread = None

        self.ui = ui
        self.document_controller = document_controller

        def start_clicked():
            if self.__acquisition_running:
                self.multi_acquire_controller.cancel()
            else:
                self.multi_acquire_controller.stem_controller = self.stem_controller
                self.multi_acquire_controller.camera = self.camera

                def run_multi_eels():
                    data_dict = self.multi_acquire_controller.acquire_multi_eels_spectrum(
                    )

                    def create_and_display_data_item():
                        self.create_result_data_item(data_dict)

                    document_controller.queue_task(create_and_display_data_item
                                                   )  # must occur on UI thread

                self.__acquisition_thread = threading.Thread(
                    target=run_multi_eels, daemon=True)
                self.__acquisition_thread.start()

        def start_si_clicked():
            if self.__acquisition_running:
                self.multi_acquire_controller.cancel()
            else:
                # Camera must be accessed from the UI thread, so do it here and re-use later
                camera = self.camera
                self.multi_acquire_controller.stem_controller = self.stem_controller
                self.multi_acquire_controller.camera = camera
                self.multi_acquire_controller.scan_controller = self.scan_controller

                def create_acquisition_handler(multi_acquire_parameters: list,
                                               current_parameters_index: int,
                                               multi_acquire_settings: dict):
                    document_model = self.document_controller._document_controller.document_model
                    camera_frame_parameters = camera.get_current_frame_parameters(
                    )
                    scan_frame_parameters = self.scan_controller.get_current_frame_parameters(
                    )
                    camera_frame_parameters[
                        'exposure_ms'] = multi_acquire_parameters[
                            current_parameters_index]['exposure_ms']
                    camera_frame_parameters[
                        'processing'] = 'sum_project' if multi_acquire_settings[
                            'bin_spectra'] else None
                    scan_frame_parameters.setdefault('scan_id',
                                                     str(uuid.uuid4()))
                    grab_synchronized_info = self.scan_controller.grab_synchronized_get_info(
                        scan_frame_parameters=scan_frame_parameters,
                        camera=camera,
                        camera_frame_parameters=camera_frame_parameters)
                    camera_data_channel = None
                    scan_data_channel = None
                    channels_ready_event = threading.Event()

                    def create_channels():
                        nonlocal camera_data_channel, scan_data_channel
                        camera_data_channel = MultiAcquire.CameraDataChannel(
                            document_model, camera.display_name,
                            grab_synchronized_info, multi_acquire_parameters,
                            multi_acquire_settings, current_parameters_index)
                        enabled_channels = self.scan_controller.get_enabled_channels(
                        )
                        enabled_channel_names = [
                            self.scan_controller.data_channels[i].name
                            for i in enabled_channels
                        ]
                        scan_data_channel = MultiAcquire.ScanDataChannel(
                            document_model, enabled_channel_names,
                            grab_synchronized_info, multi_acquire_parameters,
                            multi_acquire_settings, current_parameters_index)
                        camera_data_channel.start()
                        scan_data_channel.start()
                        channels_ready_event.set()

                    self.document_controller.queue_task(create_channels)
                    assert channels_ready_event.wait(10)

                    si_sequence_behavior = MultiAcquire.SISequenceBehavior(
                        None, None, None, None)
                    handler = MultiAcquire.SISequenceAcquisitionHandler(
                        camera, camera_data_channel, camera_frame_parameters,
                        self.scan_controller, scan_data_channel,
                        scan_frame_parameters, si_sequence_behavior)

                    listener = handler.camera_data_channel.progress_updated_event.listen(
                        self.multi_acquire_controller.set_progress_counter)

                    def finish_fn():
                        listener.close()

                        def close_channels():
                            handler.camera_data_channel.stop()
                            handler.scan_data_channel.stop()

                        self.document_controller.queue_task(close_channels)

                    handler.finish_fn = finish_fn

                    return handler

                self.__acquisition_thread = threading.Thread(
                    target=self.multi_acquire_controller.
                    start_multi_acquire_spectrum_image,
                    args=(create_acquisition_handler, ))
                self.__acquisition_thread.start()

        def settings_button_clicked():
            if not self.settings_window_open:
                self.settings_window_open = True
                self.show_config_box()

        def camera_changed(current_item):
            if current_item:
                self.multi_acquire_controller.settings[
                    'camera_hardware_source_id'] = current_item.hardware_source_id

        def binning_changed(current_item):
            if current_item == 'Spectra':
                self.multi_acquire_controller.settings['bin_spectra'] = True
                self.multi_acquire_controller.settings[
                    'use_multi_eels_calibration'] = False
            elif current_item == 'Images':
                self.multi_acquire_controller.settings['bin_spectra'] = False
                self.multi_acquire_controller.settings[
                    'use_multi_eels_calibration'] = False
            elif current_item == 'MultiEELS Spectra':
                self.multi_acquire_controller.settings['bin_spectra'] = True
                self.multi_acquire_controller.settings[
                    'use_multi_eels_calibration'] = True

        camera_choice_row = ui.create_row_widget()
        self.binning_choice_combo_box = ui.create_combo_box_widget()
        self.binning_choice_combo_box.on_current_item_changed = binning_changed
        self.binning_choice_combo_box.items = [
            'Spectra', 'Images', 'MultiEELS Spectra'
        ]
        settings_button = CanvasItem.BitmapButtonCanvasItem(
            CanvasItem.load_rgba_data_from_bytes(
                pkgutil.get_data(__name__, "resources/sliders_icon_24.png"),
                "png"))
        settings_widget = ui._ui.create_canvas_widget(properties={
            "height": 24,
            "width": 24
        })
        settings_widget.canvas_item.add_canvas_item(settings_button)
        settings_button.on_button_clicked = settings_button_clicked
        self.camera_choice_combo_box = ui.create_combo_box_widget(
            item_text_getter=lambda camera: getattr(camera, 'display_name'))
        self.camera_choice_combo_box.on_current_item_changed = camera_changed
        camera_choice_row.add_spacing(5)
        camera_choice_row.add(self.camera_choice_combo_box)
        camera_choice_row.add_spacing(10)
        camera_choice_row.add(self.binning_choice_combo_box)
        camera_choice_row.add_stretch()
        camera_choice_row.add_spacing(10)
        camera_choice_row._widget.add(settings_widget)
        camera_choice_row.add_spacing(10)
        self.update_camera_list()
        self.update_current_camera()
        self.__settings_changed_event_listener = self.multi_acquire_controller.settings.settings_changed_event.listen(
            self.update_current_camera)
        self.__settings_changed_event_listener_2 = self.multi_acquire_controller.settings.settings_changed_event.listen(
            self.update_binning_combo_box)

        def component_changed(component, component_types):
            if 'camera_hardware_source' in component_types:
                self.update_camera_list()

        self.__component_registered_event_listener = Registry.listen_component_registered_event(
            component_changed)
        self.__component_unregistered_event_listener = Registry.listen_component_unregistered_event(
            component_changed)

        change_parameters_row = ui.create_row_widget()
        change_parameters_row.add_spacing(5)
        change_parameters_row.add(
            ui.create_label_widget('MultiAcquire parameters:'))
        change_parameters_row.add_stretch()
        change_parameters_row.add_spacing(20)

        parameter_description_row = ui.create_row_widget()
        parameter_description_row.add_spacing(5)
        parameter_description_row.add(ui.create_label_widget('#'))
        parameter_description_row.add_spacing(20)
        parameter_description_row.add(ui.create_label_widget('Offset'))
        parameter_description_row.add_spacing(36)
        parameter_description_row.add_stretch()
        parameter_description_row.add(ui.create_label_widget('Exposure (ms)'))
        parameter_description_row.add_stretch()
        parameter_description_row.add(ui.create_label_widget('Frames'))
        parameter_description_row.add_spacing(5)
        parameter_description_row.add_stretch()

        add_remove_parameters_row = ui.create_row_widget()
        add_parameters_button = ui.create_push_button_widget('+')
        add_parameters_button._widget.set_property('width', 40)
        add_parameters_button.on_clicked = self.multi_acquire_controller.add_spectrum
        remove_parameters_button = ui.create_push_button_widget('-')
        remove_parameters_button._widget.set_property('width', 40)
        remove_parameters_button.on_clicked = self.multi_acquire_controller.remove_spectrum

        add_remove_parameters_row.add_spacing(5)
        add_remove_parameters_row.add(add_parameters_button)
        add_remove_parameters_row.add_spacing(5)
        add_remove_parameters_row.add(remove_parameters_button)
        add_remove_parameters_row.add_spacing(90)
        self.progress_bar = ui.create_progress_bar_widget()
        add_remove_parameters_row.add(self.progress_bar)
        add_remove_parameters_row.add_spacing(5)

        time_estimate_row = ui.create_row_widget()
        time_estimate_row.add_spacing(6)
        self.time_estimate_label = ui.create_label_widget()
        time_estimate_row.add(self.time_estimate_label)
        time_estimate_row.add_spacing(5)
        time_estimate_row.add_stretch()
        self.si_time_estimate_label = ui.create_label_widget()
        time_estimate_row.add(self.si_time_estimate_label)
        time_estimate_row.add_spacing(6)
        self.multi_acquire_controller.stem_controller = self.stem_controller
        self.multi_acquire_controller.scan_controller = self.scan_controller

        def frame_parameters_changed(profile_index, frame_parameters):
            self.update_time_estimate()

        if self.scan_controller:
            self.__scan_frame_parameters_changed_event_listener = self.scan_controller.frame_parameters_changed_event.listen(
                frame_parameters_changed)
        self.update_camera_list()

        self.start_button = ui.create_push_button_widget('Start MultiAcquire')
        self.start_button.on_clicked = start_clicked
        self.start_si_button = ui.create_push_button_widget(
            'Start MultiAcquire spectrum image')
        self.start_si_button.on_clicked = start_si_clicked
        start_row = ui.create_row_widget()
        start_row.add_spacing(5)
        start_row.add(self.start_button)
        start_row.add_spacing(5)
        start_row.add_stretch()
        start_row.add(self.start_si_button)
        start_row.add_spacing(5)

        column = ui.create_column_widget()
        column.add_spacing(5)
        column.add(camera_choice_row)
        column.add_spacing(10)
        column.add(change_parameters_row)
        column.add_spacing(5)
        column.add(parameter_description_row)
        column.add_spacing(10)
        self.parameter_column = ui.create_column_widget()
        for spectrum_parameters in self.multi_acquire_controller.spectrum_parameters:
            line = self.create_parameter_line(spectrum_parameters)
            self.parameter_column.add(line)
        column.add(self.parameter_column)
        column.add_spacing(5)
        column.add(add_remove_parameters_row)
        column.add_spacing(15)
        column.add(time_estimate_row)
        column.add_spacing(5)
        column.add(start_row)
        column.add_spacing(10)
        column.add_stretch()
        # Make sure the binning combo box shows the actual settings
        self.update_binning_combo_box()
        return column
 def _set_up_multi_acquire(self, settings: dict, parameters: list, stem_controller):
     multi_acquire = MultiAcquire.MultiAcquireController(stem_controller)
     multi_acquire.settings.update(settings)
     multi_acquire.spectrum_parameters[:] = parameters
     return multi_acquire