Exemplo n.º 1
0
 def setUp(self):
     self.app = Application.Application(TestUI.UserInterface(), set_global=False)
     self.source_image = numpy.random.randn(1024, 1024).astype(numpy.float32)
     self.exposure = 1.0
     HardwareSource.HardwareSourceManager().hardware_sources = []
     HardwareSource.HardwareSourceManager().hardware_source_added_event = Event.Event()
     HardwareSource.HardwareSourceManager().hardware_source_removed_event = Event.Event()
Exemplo n.º 2
0
    def test_recorder_state_is_reported_properly(self):
        app = Application.Application(TestUI.UserInterface(), set_global=False)
        document_model = DocumentModel.DocumentModel()
        document_controller = DocumentController.DocumentController(
            app.ui, document_model, workspace_id="library")
        with contextlib.closing(document_controller):
            data_item = DataItem.DataItem(numpy.ones((8, 8)))
            document_model.append_data_item(data_item)
            recorder_state_ref = ["unknown"]

            def recorder_state_changed(recorder_state):
                recorder_state_ref[0] = recorder_state

            recorder = RecorderPanel.Recorder(document_controller, data_item)
            recorder.on_recording_state_changed = recorder_state_changed
            with contextlib.closing(recorder):
                with document_model.data_item_live(data_item):
                    count = 4
                    recorder.start_recording(10, 1, count)
                    for i in range(count):
                        recorder.continue_recording(10 + i + 0.25)
                        data_item.set_data(data_item.data + 1)
                        if i != count - 1:  # last iteration it should fall out of transaction state since it is finished
                            self.assertEqual(recorder_state_ref[0],
                                             "recording")
                        else:
                            self.assertEqual(recorder_state_ref[0], "stopped")
                self.assertEqual(recorder_state_ref[0], "stopped")
Exemplo n.º 3
0
    def test_data_item_display_thumbnail_source_produces_data_item_mime_data(
            self):
        app = Application.Application(TestUI.UserInterface(), set_global=False)
        document_model = DocumentModel.DocumentModel()
        document_controller = DocumentController.DocumentController(
            app.ui, document_model, workspace_id="library")
        with contextlib.closing(document_controller):
            data_item = DataItem.DataItem(numpy.random.randn(8, 8))
            document_model.append_data_item(data_item)
            display_item = document_model.get_display_item_for_data_item(
                data_item)
            display_item.display_type = "image"
            thumbnail_source = DataItemThumbnailWidget.DataItemThumbnailSource(
                app.ui)
            finished = threading.Event()

            def thumbnail_data_changed(data):
                finished.set()

            thumbnail_source.on_thumbnail_data_changed = thumbnail_data_changed
            thumbnail_source.set_display_item(display_item)
            finished.wait(1.0)
            finished.clear()
            finished.wait(1.0)
            mime_data = app.ui.create_mime_data()
            valid, thumbnail = thumbnail_source.populate_mime_data_for_drag(
                mime_data, Geometry.IntSize(64, 64))
            self.assertTrue(valid)
            self.assertIsNotNone(thumbnail)
            self.assertTrue(
                mime_data.has_format(MimeTypes.DISPLAY_ITEM_MIME_TYPE))
Exemplo n.º 4
0
 def test_launch_with_no_project_then_cancel_open_dialog(self):
     with create_memory_profile_context() as profile_context:
         # use lower level calls to create the profile and open the window via the app
         profile = profile_context.create_profile(add_project=False)
         profile.read_profile()
         app = Application.Application(TestUI.UserInterface(),
                                       set_global=False)
         app._set_profile_for_test(profile)
         app.initialize(load_plug_ins=False)
         try:
             # ensure no project references
             self.assertEqual(0, len(profile.project_references))
             # set up mock calls to get through start call and check conditions
             app.ui.get_file_paths_dialog = unittest.mock.Mock(
                 return_value=([], str(), str()))
             app.show_ok_dialog = unittest.mock.Mock()
             app.show_ok_dialog.side_effect = press_ok_and_complete
             app.show_choose_project_dialog = unittest.mock.Mock()
             app.show_choose_project_dialog.side_effect = app.show_open_project_dialog
             # start the app
             app.start(profile=profile)
             # check the mock calls
             app.ui.get_file_paths_dialog.assert_called_once()
             app.show_choose_project_dialog.assert_called_once()
             app.show_ok_dialog.assert_not_called()
             # ensure no project is loaded
             self.assertEqual(0, len(profile.project_references))
             self.assertEqual(0, len(app.windows))
         finally:
             app._set_profile_for_test(None)
             app.exit()
             app.deinitialize()
Exemplo n.º 5
0
    def test_launch_with_no_project_then_open_a_project_with_error(self):
        # test both json error and general storage error
        for uuid_error, storage_error in ((True, False), (False, True)):
            with self.subTest(uuid_error=uuid_error,
                              storage_error=storage_error):
                with create_memory_profile_context() as profile_context:
                    # use lower level calls to create the profile and open the window via the app
                    profile = profile_context.create_profile(add_project=False)
                    profile.read_profile()
                    app = Application.Application(TestUI.UserInterface(),
                                                  set_global=False)
                    app._set_profile_for_test(profile)
                    app.initialize(load_plug_ins=False)
                    try:
                        # ensure no project references
                        self.assertEqual(0, len(profile.project_references))

                        # set up mock calls to get through start call
                        def setup_bad_project(args):
                            project_reference = TestContext.add_project_memory(
                                profile,
                                load=False,
                                make_uuid_error=uuid_error,
                                make_storage_error=storage_error)
                            return project_reference

                        app.show_choose_project_dialog = unittest.mock.Mock()
                        app.show_choose_project_dialog.side_effect = app.show_open_project_dialog
                        profile.open_project = unittest.mock.Mock()
                        profile.open_project.side_effect = setup_bad_project
                        app.ui.get_file_paths_dialog = unittest.mock.Mock()
                        app.ui.get_file_paths_dialog.side_effect = [
                            (["PATH"], str(), str()), ([], str(), str())
                        ]
                        app.show_ok_dialog = unittest.mock.Mock()
                        app.show_ok_dialog.side_effect = press_ok_and_complete
                        logging.getLogger(
                            "loader").setLevel = unittest.mock.Mock(
                            )  # ignore this call

                        # start the app
                        app.start(profile=profile)

                        # check the mock calls
                        self.assertEqual(
                            2, len(app.ui.get_file_paths_dialog.mock_calls))
                        profile.open_project.assert_called_once()
                        app.show_ok_dialog.assert_called_once()

                        # ensure a single project is loaded
                        self.assertEqual(1, len(profile.project_references))
                        self.assertEqual(
                            "invalid",
                            profile.project_references[0].project_state)
                        self.assertEqual(0, len(app.windows))
                    finally:
                        app._set_profile_for_test(None)
                        app.exit()
                        app.deinitialize()
Exemplo n.º 6
0
 def setUp(self):
     self.app = Application.Application(TestUI.UserInterface(), set_global=False)
     self.t = FilterPanel.TreeNode()
     self.t.insert_value(["1969", "02"], "Chris")
     self.t.insert_value(["1965", "12"], "Hans")
     self.t.insert_value(["1970", "02"], "Lara")
     self.t.insert_value(["2000", "06"], "Julian")
     self.t.insert_value(["2000", "03"], "Holden")
     self.t.insert_value(["2000", "06"], "Tristan")
     self.t.insert_value(["2000", "06"], "Skywalker")
Exemplo n.º 7
0
def main(args, bootstrap_args):
    from nion.swift import Facade
    from nion.swift import Application
    from nion.ui import Application as ApplicationUI
    warnings.simplefilter("always", RuntimeWarning)
    Facade.initialize()
    app = Application.Application(ApplicationUI.make_ui(bootstrap_args))
    app.initialize(use_root_dir=False)
    Facade.start_server()
    return app
Exemplo n.º 8
0
 def create_document_controller_with_application(
         self) -> DocumentController.DocumentController:
     app = Application.Application(TestUI.UserInterface(), set_global=False)
     document_model = self.create_document_model(auto_close=False)
     document_controller = app.create_document_controller(
         document_model, "library")
     self.__items_exit.append(document_controller.close)
     self.__app = app  # hold a reference
     app._set_document_model(
         document_model)  # required to allow API to find document model
     return document_controller
Exemplo n.º 9
0
    def test_launch_with_no_project_then_open_a_project_needing_upgrade_but_unable_to_upgrade(
            self):
        with create_memory_profile_context() as profile_context:
            # use lower level calls to create the profile and open the window via the app
            profile = profile_context.create_profile(add_project=False)
            profile.read_profile()
            app = Application.Application(TestUI.UserInterface(),
                                          set_global=False)
            app._set_profile_for_test(profile)
            app.initialize(load_plug_ins=False)
            try:
                # ensure no project references
                self.assertEqual(0, len(profile.project_references))

                # set up mock calls to get through start call
                def setup_needs_upgrade_project(args):
                    project_reference = TestContext.add_project_memory(
                        profile, load=False, d={"version": 0})
                    return project_reference

                profile.open_project = unittest.mock.Mock()
                profile.open_project.side_effect = setup_needs_upgrade_project
                app.ui.get_file_paths_dialog = unittest.mock.Mock()
                app.ui.get_file_paths_dialog = unittest.mock.Mock(
                    return_value=(["PATH"], str(), str()))
                app.show_choose_project_dialog = unittest.mock.Mock()
                app.show_choose_project_dialog.side_effect = app.show_open_project_dialog
                app.show_ok_cancel_dialog = unittest.mock.Mock()
                app.show_ok_cancel_dialog.side_effect = press_ok_cancel_and_ok_complete
                app.show_ok_dialog = unittest.mock.Mock()
                profile.upgrade = unittest.mock.Mock()
                profile.upgrade.side_effect = FileExistsError()
                logging.getLogger("loader").setLevel = unittest.mock.Mock(
                )  # ignore this call

                # start the app
                app.start(profile=profile)

                # check the mock calls
                self.assertEqual(1,
                                 len(app.ui.get_file_paths_dialog.mock_calls))
                profile.open_project.assert_called_once()
                app.show_choose_project_dialog.assert_called_once()
                app.show_ok_cancel_dialog.assert_called_once()
                app.show_ok_dialog.assert_called_once()

                # ensure a single project is loaded
                self.assertEqual(1, len(profile.project_references))
                self.assertEqual(0, len(app.windows))
            finally:
                app._set_profile_for_test(None)
                app.exit()
                app.deinitialize()
Exemplo n.º 10
0
 def test_data_item_removed_implicitly_from_data_group_undo_redo(self):
     app = Application.Application(TestUI.UserInterface(), set_global=False)
     document_model = DocumentModel.DocumentModel()
     document_controller = DocumentController.DocumentController(
         app.ui, document_model, workspace_id="library")
     with contextlib.closing(document_controller):
         # setup three data items and put in a group
         data_item1 = DataItem.DataItem(numpy.random.randn(4, 4))
         data_item2 = DataItem.DataItem(numpy.random.randn(4, 4))
         data_item3 = DataItem.DataItem(numpy.random.randn(4, 4))
         document_model.append_data_item(data_item1)
         document_model.append_data_item(data_item2)
         document_model.append_data_item(data_item3)
         data_group = DataGroup.DataGroup()
         display_item1 = document_model.get_display_item_for_data_item(
             data_item1)
         display_item2 = document_model.get_display_item_for_data_item(
             data_item2)
         display_item3 = document_model.get_display_item_for_data_item(
             data_item3)
         data_group.append_display_item(display_item1)
         data_group.append_display_item(display_item2)
         data_group.append_display_item(display_item3)
         document_model.append_data_group(data_group)
         # remove the 2nd data item
         command = document_controller.create_remove_data_items_command(
             [data_item2])
         command.perform()
         document_controller.push_undo_command(command)
         self.assertEqual(2, len(document_model.data_items))
         self.assertEqual(2, len(data_group.display_items))
         self.assertEqual(document_model.display_items[0],
                          data_group.display_items[0])
         self.assertEqual(document_model.display_items[1],
                          data_group.display_items[1])
         # undo and check
         document_controller.handle_undo()
         self.assertEqual(3, len(document_model.data_items))
         self.assertEqual(3, len(data_group.display_items))
         self.assertEqual(document_model.display_items[0],
                          data_group.display_items[0])
         self.assertEqual(document_model.display_items[1],
                          data_group.display_items[1])
         self.assertEqual(document_model.display_items[2],
                          data_group.display_items[2])
         # redo and check
         document_controller.handle_redo()
         self.assertEqual(2, len(document_model.data_items))
         self.assertEqual(2, len(data_group.display_items))
         self.assertEqual(document_model.display_items[0],
                          data_group.display_items[0])
         self.assertEqual(document_model.display_items[1],
                          data_group.display_items[1])
Exemplo n.º 11
0
 def test_inserting_items_data_group_undo_redo(self):
     app = Application.Application(TestUI.UserInterface(), set_global=False)
     document_model = DocumentModel.DocumentModel()
     document_controller = DocumentController.DocumentController(app.ui, document_model, workspace_id="library")
     with contextlib.closing(document_controller):
         # setup three data items and put in a group
         data_item1 = DataItem.DataItem(numpy.random.randn(4, 4))
         data_item3 = DataItem.DataItem(numpy.random.randn(4, 4))
         data_item4 = DataItem.DataItem(numpy.random.randn(4, 4))
         document_model.append_data_item(data_item1)
         document_model.append_data_item(data_item3)
         document_model.append_data_item(data_item4)
         data_group = DataGroup.DataGroup()
         display_item1 = document_model.get_display_item_for_data_item(data_item1)
         display_item3 = document_model.get_display_item_for_data_item(data_item3)
         display_item4 = document_model.get_display_item_for_data_item(data_item4)
         data_group.append_display_item(display_item1)
         data_group.append_display_item(display_item3)
         document_model.append_data_group(data_group)
         self.assertEqual(3, len(document_model.data_items))
         self.assertEqual(2, len(data_group.display_items))
         self.assertListEqual([data_item1, data_item3, data_item4], list(document_model.data_items))
         self.assertListEqual([display_item1, display_item3], list(data_group.display_items))
         # insert new items
         data_item2 = DataItem.DataItem(numpy.random.randn(4, 4))
         document_model.append_data_item(data_item2)
         display_item2 = document_model.get_display_item_for_data_item(data_item2)
         command = DocumentController.DocumentController.InsertDataGroupDataItemsCommand(document_controller, data_group, [data_item2, data_item4], 1)
         command.perform()
         document_controller.push_undo_command(command)
         self.assertEqual(4, len(document_model.data_items))
         self.assertEqual(4, len(data_group.display_items))
         self.assertListEqual([data_item1, data_item3, data_item4, data_item2], list(document_model.data_items))
         self.assertListEqual([display_item1, display_item2, display_item4, display_item3], list(data_group.display_items))
         # undo and check
         document_controller.handle_undo()
         self.assertEqual(4, len(document_model.data_items))
         self.assertEqual(2, len(data_group.display_items))
         self.assertListEqual([data_item1, data_item3, data_item4, data_item2], list(document_model.data_items))
         self.assertListEqual([display_item1, display_item3], list(data_group.display_items))
         # redo and check
         document_controller.handle_redo()
         data_item2 = document_model.data_items[3]
         self.assertEqual(4, len(document_model.data_items))
         self.assertEqual(4, len(data_group.display_items))
         self.assertListEqual([data_item1, data_item3, data_item4, data_item2], list(document_model.data_items))
         self.assertListEqual([display_item1, display_item2, display_item4, display_item3], list(data_group.display_items))
Exemplo n.º 12
0
 def test_deleting_data_items_out_of_order_from_data_group_undo_redo(self):
     app = Application.Application(TestUI.UserInterface(), set_global=False)
     document_model = DocumentModel.DocumentModel()
     document_controller = DocumentController.DocumentController(
         app.ui, document_model, workspace_id="library")
     with contextlib.closing(document_controller):
         # setup three data items in a group
         data_item1 = DataItem.DataItem(numpy.random.randn(4, 4))
         data_item2 = DataItem.DataItem(numpy.random.randn(4, 4))
         data_item3 = DataItem.DataItem(numpy.random.randn(4, 4))
         document_model.append_data_item(data_item1)
         document_model.append_data_item(data_item2)
         document_model.append_data_item(data_item3)
         data_group = DataGroup.DataGroup()
         display_item1 = document_model.get_display_item_for_data_item(
             data_item1)
         display_item2 = document_model.get_display_item_for_data_item(
             data_item2)
         display_item3 = document_model.get_display_item_for_data_item(
             data_item3)
         data_group.append_display_item(display_item1)
         data_group.append_display_item(display_item2)
         data_group.append_display_item(display_item3)
         document_model.append_data_group(data_group)
         # remove two of the three
         command = DocumentController.DocumentController.RemoveDataGroupDisplayItemsCommand(
             document_model, data_group, [display_item3, display_item1])
         command.perform()
         document_controller.push_undo_command(command)
         self.assertEqual(1, len(data_group.display_items))
         self.assertEqual(0, len(data_group.data_groups))
         self.assertEqual(display_item2, data_group.display_items[0])
         # undo and check
         document_controller.handle_undo()
         self.assertEqual(3, len(data_group.display_items))
         self.assertEqual(0, len(data_group.data_groups))
         self.assertEqual(display_item1, data_group.display_items[0])
         self.assertEqual(display_item2, data_group.display_items[1])
         self.assertEqual(display_item3, data_group.display_items[2])
         # redo and check
         document_controller.handle_redo()
         self.assertEqual(1, len(data_group.display_items))
         self.assertEqual(0, len(data_group.data_groups))
         self.assertEqual(display_item2, data_group.display_items[0])
Exemplo n.º 13
0
    def test_launch_with_no_project_then_open_a_project(self):
        with create_memory_profile_context() as profile_context:
            # use lower level calls to create the profile and open the window via the app
            profile = profile_context.create_profile(add_project=False)
            profile.read_profile()
            app = Application.Application(TestUI.UserInterface(),
                                          set_global=False)
            app._set_profile_for_test(profile)
            app.initialize(load_plug_ins=False)
            try:
                # ensure no project references
                self.assertEqual(0, len(profile.project_references))

                # set up mock calls to get through start call
                profile.open_project = unittest.mock.Mock()
                profile.open_project.side_effect = lambda x: TestContext.add_project_memory(
                    profile, load=False)
                app.ui.get_file_paths_dialog = unittest.mock.Mock(
                    return_value=(["PATH"], str(), str()))
                app.show_ok_dialog = unittest.mock.Mock()
                app.show_ok_dialog.side_effect = press_ok_and_complete
                app.show_choose_project_dialog = unittest.mock.Mock()
                app.show_choose_project_dialog.side_effect = app.show_open_project_dialog
                logging.getLogger("loader").setLevel = unittest.mock.Mock(
                )  # ignore this call

                # start the app
                app.start(profile=profile)

                # check the mock calls
                app.ui.get_file_paths_dialog.assert_called_once()
                profile.open_project.assert_called_once()
                app.show_choose_project_dialog.assert_called_once()
                app.show_ok_dialog.assert_not_called()

                # ensure a single project is loaded
                self.assertEqual(1, len(profile.project_references))
                self.assertEqual("loaded",
                                 profile.project_references[0].project_state)
                self.assertEqual(1, len(app.windows))
            finally:
                app._set_profile_for_test(None)
                app.exit()
                app.deinitialize()
Exemplo n.º 14
0
 def test_insert_data_group_undo_redo(self):
     app = Application.Application(TestUI.UserInterface(), set_global=False)
     document_model = DocumentModel.DocumentModel()
     document_controller = DocumentController.DocumentController(
         app.ui, document_model, workspace_id="library")
     with contextlib.closing(document_controller):
         # setup three data items and put two in a group
         data_item1 = DataItem.DataItem(numpy.random.randn(4, 4))
         data_item3 = DataItem.DataItem(numpy.random.randn(4, 4))
         document_model.append_data_item(data_item1)
         document_model.append_data_item(data_item3)
         data_group1 = DataGroup.DataGroup()
         data_group1.append_display_item(
             document_model.get_display_item_for_data_item(data_item1))
         data_group2 = DataGroup.DataGroup()
         data_group3 = DataGroup.DataGroup()
         data_group2_uuid = data_group2.uuid
         data_group3.append_display_item(
             document_model.get_display_item_for_data_item(data_item3))
         document_model.append_data_group(data_group1)
         document_model.append_data_group(data_group3)
         # insert the middle data group
         command = DocumentController.DocumentController.InsertDataGroupCommand(
             document_model, document_model, 1, data_group2)
         command.perform()
         document_controller.push_undo_command(command)
         self.assertEqual(3, len(document_model.data_groups))
         self.assertEqual(data_group1, document_model.data_groups[0])
         self.assertEqual(data_group3, document_model.data_groups[2])
         self.assertEqual(data_group2_uuid,
                          document_model.data_groups[1].uuid)
         # undo and check
         document_controller.handle_undo()
         self.assertEqual(2, len(document_model.data_groups))
         self.assertEqual(data_group1, document_model.data_groups[0])
         self.assertEqual(data_group3, document_model.data_groups[1])
         # redo and check
         document_controller.handle_redo()
         self.assertEqual(3, len(document_model.data_groups))
         self.assertEqual(data_group1, document_model.data_groups[0])
         self.assertEqual(data_group3, document_model.data_groups[2])
         self.assertEqual(data_group2_uuid,
                          document_model.data_groups[1].uuid)
Exemplo n.º 15
0
 def test_switching_library_closes_document_only_once(self):
     current_working_directory = os.getcwd()
     workspace1_dir = os.path.join(current_working_directory, "__Test1")
     workspace2_dir = os.path.join(current_working_directory, "__Test2")
     Cache.db_make_directory_if_needed(workspace1_dir)
     Cache.db_make_directory_if_needed(workspace2_dir)
     try:
         app = Application.Application(TestUI.UserInterface(),
                                       set_global=False)
         app.initialize(load_plug_ins=False)
         app.start(True, fixed_workspace_dir=workspace1_dir)
         app.switch_library(workspace2_dir,
                            skip_choose=True,
                            fixed_workspace_dir=workspace2_dir)
         app.exit()
         app.deinitialize()
     finally:
         #logging.debug("rmtree %s", workspace_dir)
         shutil.rmtree(workspace1_dir)
         shutil.rmtree(workspace2_dir)
Exemplo n.º 16
0
 def test_data_group_rename_undo_redo(self):
     app = Application.Application(TestUI.UserInterface(), set_global=False)
     document_model = DocumentModel.DocumentModel()
     document_controller = DocumentController.DocumentController(app.ui, document_model, workspace_id="library")
     with contextlib.closing(document_controller):
         # setup data group
         data_group = DataGroup.DataGroup()
         data_group.title = "ethel"
         document_model.append_data_group(data_group)
         # rename
         command = document_controller.create_rename_data_group_command(data_group, "fred")
         command.perform()
         document_controller.push_undo_command(command)
         self.assertEqual(data_group.title, "fred")
         # undo and check
         document_controller.handle_undo()
         self.assertEqual(data_group.title, "ethel")
         # redo and check
         document_controller.handle_redo()
         self.assertEqual(data_group.title, "fred")
Exemplo n.º 17
0
 def test_recorder_records_live_data(self):
     app = Application.Application(TestUI.UserInterface(), set_global=False)
     document_model = DocumentModel.DocumentModel()
     document_controller = DocumentController.DocumentController(
         app.ui, document_model, workspace_id="library")
     with contextlib.closing(document_controller):
         data_item = DataItem.DataItem(numpy.ones((8, 8)))
         document_model.append_data_item(data_item)
         recorder = RecorderPanel.Recorder(document_controller, data_item)
         with contextlib.closing(recorder):
             with document_model.data_item_live(data_item):
                 count = 4
                 recorder.start_recording(10, 1, count)
                 for i in range(count):
                     recorder.continue_recording(10 + i + 0.25)
                     recorder.continue_recording(10 + i + 0.75)
                     data_item.set_data(data_item.data + 1)
         self.assertEqual(2, len(document_model.data_items))
         recorded_data_item = document_model.data_items[1]
         self.assertTrue(recorded_data_item.xdata.is_sequence)
         self.assertEqual((4, 8, 8),
                          recorded_data_item.xdata.dimensional_shape)
Exemplo n.º 18
0
 def setUp(self):
     self.app = Application.Application(TestUI.UserInterface(),
                                        set_global=False)
     self.document_model = DocumentModel.DocumentModel()
     self.document_controller = DocumentController.DocumentController(
         self.app.ui, self.document_model, workspace_id="library")
     data = numpy.full((10, 10), 200, dtype=numpy.uint32)
     data[5, 5] = 650
     self.data_item = DataItem.DataItem(data)
     self.document_model.append_data_item(self.data_item)
     self.display_item = self.document_model.get_display_item_for_data_item(
         self.data_item)
     self.histogram_panel = HistogramPanel.HistogramPanel(
         self.document_controller,
         "histogram-panel",
         None,
         debounce=False,
         sample=False)
     self.histogram_canvas_item = self.histogram_panel._histogram_widget._histogram_canvas_item
     self.document_controller.show_display_item(self.display_item)
     self.histogram_canvas_item.update_layout((0, 0), (80, 300),
                                              immediate=True)
Exemplo n.º 19
0
def main(args, bootstrap_args):
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "nionutils"))
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "nionui"))
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "niondata"))
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "nionswift"))
    sys.path.insert(
        0,
        os.path.join(os.path.dirname(__file__),
                     "nionswift-instrumentation-kit"))
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), "nionswift-io"))
    sys.path.insert(0, os.path.join(os.path.dirname(__file__),
                                    "eels-analysis"))
    # these imports need to occur AFTER the args are parsed and the path
    # is updated accordingly.
    from nion.swift import Facade
    from nion.swift import Application
    from nion.ui import Application as ApplicationUI
    warnings.simplefilter("always", RuntimeWarning)
    Facade.initialize()
    app = Application.Application(ApplicationUI.make_ui(bootstrap_args))
    app.initialize()
    Facade.start_server()
    return app
Exemplo n.º 20
0
 def test_recorder_puts_recorded_data_item_under_transaction(self):
     app = Application.Application(TestUI.UserInterface(), set_global=False)
     document_model = DocumentModel.DocumentModel()
     document_controller = DocumentController.DocumentController(
         app.ui, document_model, workspace_id="library")
     with contextlib.closing(document_controller):
         data_item = DataItem.DataItem(numpy.ones((8, 8)))
         document_model.append_data_item(data_item)
         recorder = RecorderPanel.Recorder(document_controller, data_item)
         with contextlib.closing(recorder):
             with document_model.data_item_live(data_item):
                 count = 4
                 recorder.start_recording(10, 1, count)
                 for i in range(count):
                     recorder.continue_recording(10 + i + 0.25)
                     data_item.set_data(data_item.data + 1)
                     if i != count - 1:  # last iteration it should fall out of transaction state since it is finished
                         self.assertTrue(document_model.data_items[1].
                                         in_transaction_state)
                     else:
                         self.assertFalse(document_model.data_items[1].
                                          in_transaction_state)
         self.assertFalse(document_model.data_items[1].in_transaction_state)
Exemplo n.º 21
0
 def setUp(self):
     self.app = Application.Application(TestUI.UserInterface(), set_global=True)
     self.app.workspace_dir = str()
Exemplo n.º 22
0
 def setUp(self):
     self.app = Application.Application(TestUI.UserInterface(),
                                        set_global=False)
    def test_acquire_multi_eels_spectrum_image(self):
        scan_size = (10, 10)

        app = Application.Application(TestUI.UserInterface(), set_global=False)
        for sum_frames in [True, False]:
            for bin_spectra in [True, False]:
                with self.subTest(sum_frames=sum_frames,
                                  bin_spectra=bin_spectra):
                    settings = {
                        'x_shifter': 'EELS_MagneticShift_Offset',
                        'blanker': 'C_Blank',
                        'x_shift_delay': 0.05,
                        'focus': '',
                        'focus_delay': 0,
                        'auto_dark_subtract': False,
                        'bin_spectra': bin_spectra,
                        'blanker_delay': 0.05,
                        'sum_frames': sum_frames,
                        'camera_hardware_source_id': ''
                    }
                    parameters = [{
                        'index': 0,
                        'offset_x': 0,
                        'exposure_ms': 5,
                        'frames': 2
                    }, {
                        'index': 1,
                        'offset_x': 160,
                        'exposure_ms': 8,
                        'frames': 1
                    }]
                    with TestContext.create_memory_context() as test_context:
                        document_model = test_context.create_document_model(
                            auto_close=False)
                        document_controller = app.create_document_controller(
                            document_model, "library")
                        with contextlib.ExitStack() as cm:
                            cm.callback(document_controller.close)
                            total_acquisition_time = 0.0
                            for params in parameters:
                                # the simulator cant go super fast, so make sure we give it enough time
                                total_acquisition_time += params[
                                    'frames'] * max(
                                        params['exposure_ms'], 100
                                    ) * 1e-3 * scan_size[0] * scan_size[1]
                                # add some extra overhead time
                                total_acquisition_time += 0.15
                                total_acquisition_time += settings[
                                    'x_shift_delay'] * 2
                            total_acquisition_time += settings[
                                'x_shift_delay'] * 2

                            stem_controller, camera = self._get_stem_controller_and_camera(
                                is_eels=True)
                            scan_controller = self._get_scan_controller(
                                stem_controller)
                            scan_controller.set_enabled_channels([0, 1])
                            scan_frame_parameters = scan_controller.get_current_frame_parameters(
                            )
                            scan_frame_parameters['size'] = scan_size
                            scan_controller.set_current_frame_parameters(
                                scan_frame_parameters)

                            multi_acquire_controller = self._set_up_multi_acquire(
                                settings, parameters, stem_controller)
                            multi_acquire_controller.scan_controller = scan_controller

                            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

                            progress = 0

                            def update_progress(minimum, maximum, value):
                                nonlocal progress
                                progress = minimum + value / maximum
                                document_controller.periodic()

                            progress_event_listener = multi_acquire_controller.progress_updated_event.listen(
                                update_progress)
                            cm.callback(progress_event_listener.close)
                            starttime = time.time()
                            multi_acquire_controller.start_multi_acquire_spectrum_image(
                                get_acquisition_handler_fn)
                            endtime = time.time()
                            document_controller.periodic()

                            self.assertAlmostEqual(progress, 1, places=1)

                            multi_acquire_data_items = list()
                            haadf_data_items = list()
                            maadf_data_items = list()
                            for data_item in document_model.data_items:
                                if 'MultiAcquire' in data_item.title:
                                    if 'HAADF' in data_item.title:
                                        haadf_data_items.append(data_item)
                                    elif 'MAADF' in data_item.title:
                                        maadf_data_items.append(data_item)
                                    else:
                                        multi_acquire_data_items.append(
                                            data_item)

                            self.assertEqual(len(multi_acquire_data_items),
                                             len(parameters))
                            self.assertEqual(len(haadf_data_items),
                                             len(parameters))
                            self.assertEqual(len(maadf_data_items),
                                             len(parameters))

                            camera_frame_parameters = camera.get_current_frame_parameters(
                            )
                            scan_frame_parameters = scan_controller.get_current_frame_parameters(
                            )

                            for data_item, haadf_data_item in zip(
                                    multi_acquire_data_items,
                                    haadf_data_items):
                                with self.subTest():
                                    camera_dims = camera.get_expected_dimensions(
                                        camera_frame_parameters['binning'])
                                    total_shape = tuple(
                                        scan_frame_parameters['size'])
                                    haadf_shape = tuple(
                                        scan_frame_parameters['size'])
                                    index = data_item.xdata.metadata[
                                        'MultiAcquire.parameters']['index']
                                    if parameters[index][
                                            'frames'] > 1 and not settings[
                                                'sum_frames']:
                                        total_shape = (
                                            parameters[index]['frames'],
                                        ) + total_shape
                                        haadf_shape = (
                                            parameters[index]['frames'],
                                        ) + haadf_shape
                                    if settings['bin_spectra']:
                                        total_shape += camera_dims[1:]
                                    else:
                                        total_shape += camera_dims

                                    self.assertSequenceEqual(
                                        data_item.data.shape, total_shape)
                                    self.assertSequenceEqual(
                                        haadf_data_item.data.shape,
                                        haadf_shape)

                            self.assertLess(starttime - endtime,
                                            total_acquisition_time)
Exemplo n.º 24
0
    def test_switch_to_project_with_error(self):
        # test both json error and general storage error
        for uuid_error, storage_error in ((True, False), (False, True)):
            with self.subTest(uuid_error=uuid_error,
                              storage_error=storage_error):
                with create_memory_profile_context() as profile_context:
                    # use lower level calls to create the profile and open the window via the app
                    profile = profile_context.create_profile(add_project=False)
                    profile.read_profile()
                    app = Application.Application(TestUI.UserInterface(),
                                                  set_global=False)
                    app._set_profile_for_test(profile)
                    TestContext.add_project_memory(profile, load=False)
                    TestContext.add_project_memory(profile, load=False)
                    app.initialize(load_plug_ins=False)
                    try:
                        # ensure two project references
                        self.assertEqual(2, len(profile.project_references))
                        profile.last_project_reference = profile.project_references[
                            0].uuid
                        logging.getLogger(
                            "loader").setLevel = unittest.mock.Mock(
                            )  # ignore this call
                        app.show_choose_project_dialog = unittest.mock.Mock()
                        app.show_choose_project_dialog.side_effect = app.show_open_project_dialog

                        # start the app
                        app.start(profile=profile)

                        # ensure a single project is loaded
                        self.assertEqual(2, len(profile.project_references))
                        self.assertEqual(
                            "loaded",
                            profile.project_references[0].project_state)
                        self.assertEqual(
                            "unloaded",
                            profile.project_references[1].project_state)
                        self.assertEqual(1, len(app.windows))
                        self.assertEqual(profile.last_project_reference,
                                         profile.project_references[0].uuid)

                        # switching to an error project should display an ok message with an error, then the file
                        # open dialog (which will be cancelled). no windows should be open at the end.

                        # set up mock calls to get through the switch.
                        if uuid_error:
                            profile.read_project = unittest.mock.Mock()
                            profile.read_project.side_effect = Exception()
                        if storage_error:
                            profile.project_references[
                                1].make_storage = unittest.mock.Mock()
                            profile.project_references[
                                1].make_storage.side_effect = Exception()
                        app.ui.get_file_paths_dialog = unittest.mock.Mock()
                        app.ui.get_file_paths_dialog.side_effect = [
                            ([], str(), str()), ([], str(), str())
                        ]
                        app.show_ok_dialog = unittest.mock.Mock()
                        app.show_ok_dialog.side_effect = press_ok_and_complete
                        logging.getLogger(
                            "loader").setLevel = unittest.mock.Mock(
                            )  # ignore this call

                        # switch project and check
                        app.switch_project_reference(
                            profile.project_references[1])
                        app.ui.get_file_paths_dialog.assert_called_once()
                        app.show_ok_dialog.assert_called_once()
                        self.assertEqual(
                            "unloaded",
                            profile.project_references[0].project_state)
                        self.assertEqual(
                            "unloaded",
                            profile.project_references[1].project_state)
                        self.assertEqual(0, len(app.windows))
                        self.assertEqual(profile.last_project_reference,
                                         profile.project_references[0].uuid)
                    finally:
                        app._set_profile_for_test(None)
                        app.exit()
                        app.deinitialize()
Exemplo n.º 25
0
    def test_switching_project_reloads_new_project_on_relaunch(self):
        with create_memory_profile_context() as profile_context:
            # use lower level calls to create the profile and open the window via the app
            profile = profile_context.create_profile(add_project=False)
            profile.read_profile()
            app = Application.Application(TestUI.UserInterface(),
                                          set_global=False)
            app._set_profile_for_test(profile)
            TestContext.add_project_memory(profile, load=False)
            TestContext.add_project_memory(profile, load=False)
            app.initialize(load_plug_ins=False)
            try:
                # ensure one project reference
                self.assertEqual(2, len(profile.project_references))
                profile.last_project_reference = profile.project_references[
                    0].uuid
                logging.getLogger("loader").setLevel = unittest.mock.Mock(
                )  # ignore this call

                # start the app
                app.start(profile=profile)

                # ensure a single project is loaded
                self.assertEqual(2, len(profile.project_references))
                self.assertEqual("loaded",
                                 profile.project_references[0].project_state)
                self.assertEqual("unloaded",
                                 profile.project_references[1].project_state)
                self.assertEqual(1, len(app.windows))
                self.assertEqual(profile.last_project_reference,
                                 profile.project_references[0].uuid)

                # switch project and check
                app.switch_project_reference(profile.project_references[1])
                self.assertEqual("unloaded",
                                 profile.project_references[0].project_state)
                self.assertEqual("loaded",
                                 profile.project_references[1].project_state)
                self.assertEqual(1, len(app.windows))
                self.assertEqual(profile.last_project_reference,
                                 profile.project_references[1].uuid)
            finally:
                app._set_profile_for_test(None)
                app.exit()
                app.deinitialize()

            # reload and check
            profile = profile_context.create_profile(add_project=False)
            profile.read_profile()
            app = Application.Application(TestUI.UserInterface(),
                                          set_global=False)
            app._set_profile_for_test(profile)
            app.initialize(load_plug_ins=False)
            try:
                # ensure two project references
                self.assertEqual(2, len(profile.project_references))
                profile.last_project_reference = profile.project_references[
                    0].uuid

                # start the app
                app.start(profile=profile)

                # switch project and check
                app.switch_project_reference(profile.project_references[1])
                self.assertEqual("unloaded",
                                 profile.project_references[0].project_state)
                self.assertEqual("loaded",
                                 profile.project_references[1].project_state)
                self.assertEqual(1, len(app.windows))
                self.assertEqual(profile.last_project_reference,
                                 profile.project_references[1].uuid)
            finally:
                app._set_profile_for_test(None)
                app.exit()
                app.deinitialize()
Exemplo n.º 26
0
    def test_acquire_multi_eels_spectrum_image_produces_data_of_correct_shape(
            self):
        for sum_frames in [True, False]:
            for bin_spectra in [True, False]:
                with self.subTest(sum_frames=sum_frames,
                                  bin_spectra=bin_spectra):
                    settings = {
                        'x_shifter': 'EELS_MagneticShift_Offset',
                        'blanker': 'C_Blank',
                        'x_shift_delay': 0.05,
                        'focus': '',
                        'focus_delay': 0,
                        'auto_dark_subtract': False,
                        'bin_spectra': bin_spectra,
                        'blanker_delay': 0.05,
                        'sum_frames': sum_frames,
                        'camera_hardware_source_id': ''
                    }
                    parameters = [{
                        'index': 0,
                        'offset_x': 0,
                        'exposure_ms': 5,
                        'frames': 2
                    }, {
                        'index': 1,
                        'offset_x': 160,
                        'exposure_ms': 8,
                        'frames': 1
                    }]
                    app = Application.Application(TestUI.UserInterface(),
                                                  set_global=True)
                    document_model = DocumentModel.DocumentModel()
                    document_controller = app.create_document_controller(
                        document_model, None)
                    total_acquisition_time = 0.0
                    for parms in parameters:
                        # the simulator cant go super fast, so make sure we give it enough time
                        total_acquisition_time += parms['frames'] * max(
                            parms['exposure_ms'], 100) * 1e-3
                        # add some extra overhead time
                        total_acquisition_time += 0.15
                        total_acquisition_time += settings['x_shift_delay'] * 2
                    total_acquisition_time += settings['x_shift_delay'] * 2
                    api = Facade.get_api('~1.0', '~1.0')
                    si_receiver = MultiAcquirePanel.MultiAcquirePanelDelegate(
                        api)
                    si_receiver._MultiAcquirePanelDelegate__progress_updated_event_listener.close(
                    )
                    si_receiver._MultiAcquirePanelDelegate__progress_updated_event_listener = None
                    si_receiver._MultiAcquirePanelDelegate__multi_eels_parameters_changed_event_listener.close(
                    )
                    si_receiver._MultiAcquirePanelDelegate__multi_eels_parameters_changed_event_listener = None
                    si_receiver._MultiAcquirePanelDelegate__acquisition_state_changed_event_listener.close(
                    )
                    si_receiver._MultiAcquirePanelDelegate__acquisition_state_changed_event_listener = None
                    multi_acquire = self._set_up_multi_acquire(
                        settings,
                        parameters,
                        multi_acquire_instance=si_receiver.
                        multi_acquire_controller)
                    multi_acquire.stem_controller, multi_acquire.camera = self._get_stem_controller_and_camera(
                        is_eels=True)
                    multi_acquire.superscan = self._get_scan_controller(
                        multi_acquire.stem_controller)
                    # enable binning for speed
                    frame_parameters = multi_acquire.camera.get_current_frame_parameters(
                    )
                    frame_parameters['binning'] = 8
                    multi_acquire.camera.set_current_frame_parameters(
                        frame_parameters)
                    scan_frame_parameters = multi_acquire.superscan.get_current_frame_parameters(
                    )
                    scan_frame_parameters['size'] = (10, 10)
                    scan_frame_parameters['fov_size_nm'] = 16
                    multi_acquire.superscan.set_current_frame_parameters(
                        scan_frame_parameters)
                    progress = 0

                    def update_progress(minimum, maximum, value):
                        nonlocal progress
                        progress = minimum + value / maximum
                        document_controller.periodic()

                    progress_event_listener = multi_acquire.progress_updated_event.listen(
                        update_progress)
                    new_data_ready_event_listener = multi_acquire.new_data_ready_event.listen(
                        si_receiver.add_to_display_queue)

                    def acquisition_state_changed(info_dict):
                        if info_dict.get('message') in {
                                'end processing', 'exception'
                        }:
                            si_receiver._data_processed_event.set()

                    acquisition_state_changed_event_listener = multi_acquire.acquisition_state_changed_event.listen(
                        acquisition_state_changed)
                    data_receiver_thread = threading.Thread(
                        target=si_receiver.process_display_queue, daemon=True)
                    data_receiver_thread.start()
                    # starttime = time.time()
                    multi_acquire.camera.start_playing()
                    multi_acquire.acquire_multi_eels_spectrum_image()
                    document_controller.periodic()
                    self.assertTrue(si_receiver._data_processed_event.wait(10))
                    self.assertGreaterEqual(progress, 1)
                    #self.assertLess(time.time() - starttime, total_acquisition_time)
                    multi_acquire_data_items = list()
                    for data_item in document_model.data_items:
                        if 'MultiAcquire' in data_item.title:
                            multi_acquire_data_items.append(data_item)
                    self.assertEqual(len(multi_acquire_data_items),
                                     len(parameters) * 2)
                    for data_item in multi_acquire_data_items:
                        with self.subTest():
                            camera_dims = multi_acquire.camera.get_expected_dimensions(
                                frame_parameters['binning'])
                            total_shape = tuple(scan_frame_parameters['size'])
                            haadf_shape = tuple(scan_frame_parameters['size'])
                            index = data_item.xdata.metadata[
                                'MultiAcquire.parameters']['index']
                            if parameters[index]['frames'] > 1 and not settings[
                                    'sum_frames']:
                                total_shape = (parameters[index]['frames'],
                                               ) + total_shape
                                haadf_shape = (parameters[index]['frames'],
                                               ) + haadf_shape
                            if settings['bin_spectra']:
                                total_shape += camera_dims[1:]
                            else:
                                total_shape += camera_dims
                            if 'HAADF' in data_item.title:
                                self.assertSequenceEqual(
                                    data_item.data.shape, haadf_shape)
                            else:
                                self.assertSequenceEqual(
                                    data_item.data.shape, total_shape)
                    data_receiver_thread.join(3)
                    self.assertFalse(data_receiver_thread.is_alive())
                    progress_event_listener.close()
                    new_data_ready_event_listener.close()
                    acquisition_state_changed_event_listener.close()
                    si_receiver.close()
                    app.exit()
Exemplo n.º 27
0
 def setUp(self):
     TestContext.begin_leaks()
     self.app = Application.Application(TestUI.UserInterface(),
                                        set_global=False)
Exemplo n.º 28
0
 def create_application(self) -> Application.Application:
     app = Application.Application(TestUI.UserInterface(), set_global=False)
     self.__app = app  # hold a reference
     return app
Exemplo n.º 29
0
 def setUp(self):
     TestContext.begin_leaks()
     self.app = Application.Application(TestUI.UserInterface(),
                                        set_global=True)
     self.app.workspace_dir = str()