def test_cryo_feature_overlay(self): """ Test behavior of CryoFeatureOverlay """ cnvs = miccanvas.DblMicroscopeCanvas(self.panel) self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) tab_mod = self.create_cryo_tab_model() # Create a dummy stage & focus to attach to the view stage = TMCLController(name="test_stage", role="stage", port="/dev/fake3", axes=["x", "y"], ustepsize=[1e-6, 1e-6], rng=[[-3e-3, 3e-3], [-3e-3, 3e-3]], refproc="Standard") focus = TMCLController(name="test_focus", role="focus", port="/dev/fake3", axes=["z"], ustepsize=[1e-6], rng=[[-3e-3, 3e-3]], refproc="Standard") tab_mod.main.stage = stage tab_mod.main.focus = focus fview = FeatureOverviewView("fakeview", stage=stage) tab_mod.views.value.append(fview) tab_mod.focussedView.value = fview cnvs.setView(fview, tab_mod) # Save current empty canvas img = wx.Bitmap.ConvertToImage(cnvs._bmp_buffer) buffer_clear = wxImage2NDImage(img) cryofeature_overlay = wol.CryoFeatureOverlay(cnvs, tab_mod) cnvs.add_world_overlay(cryofeature_overlay) cryofeature_overlay.active.value = True # Add features to the tab's features list tab_mod.add_new_feature(0, 0) tab_mod.add_new_feature(0.001, 0.001) cnvs._dc_buffer.SelectObject(wx.NullBitmap) # Flush the buffer cnvs._dc_buffer.SelectObject(cnvs._bmp_buffer) img = wx.Bitmap.ConvertToImage(cnvs._bmp_buffer) buffer_feature = wxImage2NDImage(img) # Compare empty canvas with the added _features_va assert_array_not_equal( buffer_clear, buffer_feature, msg= "Buffers are equal, which means the added _features_va didn't appear on the canvas." ) test.gui_loop()
def test_current_pos_crosshair_overlay(self): """ Test behaviour of CurrentPosCrossHairOverlay """ cnvs = miccanvas.DblMicroscopeCanvas(self.panel) self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) tab_mod = self.create_simple_tab_model() # create a dummy stage to attach to the view stage = TMCLController(name="test", role="test", port="/dev/fake3", axes=["x", "y"], ustepsize=[1e-6, 1e-6], rng=[[-3e-3, 3e-3], [-3e-3, 3e-3]], refproc="Standard") # Add a tiled area view to the tab model logging.debug(stage.position.value) fview = FeatureOverviewView("fakeview", stage=stage) tab_mod.views.value.append(fview) tab_mod.focussedView.value = fview cnvs.setView(fview, tab_mod) cnvs.view.show_crosshair.value = False slol = wol.CurrentPosCrossHairOverlay(cnvs) slol.active.value = True cnvs.add_world_overlay(slol) # stage start at 0,0 (cross hair at center) -> move bt 1mm, 1mm -> then back to 0,0 stage.moveAbs({'x': 1e-3, 'y': 1e-3}).result() img = wx.Bitmap.ConvertToImage(cnvs._bmp_buffer) buffer_ch_move = wxImage2NDImage(img) test.gui_loop(1) stage.moveAbs({'x': 0, 'y': 0}).result() cnvs.update_drawing() cnvs._dc_buffer.SelectObject(wx.NullBitmap) # Flush the buffer cnvs._dc_buffer.SelectObject(cnvs._bmp_buffer) img = wx.Bitmap.ConvertToImage(cnvs._bmp_buffer) buffer_ch_center = wxImage2NDImage(img) assert_array_not_equal( buffer_ch_center, buffer_ch_move, msg= "Buffers are equal, which means the crosshair didn't change on stage movement." ) test.gui_loop()
def test_simple(self): size = (32, 64) wximage = wx.Image(*size) # black RGB ndimage = wxImage2NDImage(wximage) self.assertEqual(ndimage.shape[0:2], size[-1:-3:-1]) self.assertEqual(ndimage.shape[2], 3) # RGB self.assertTrue((ndimage[0, 0] == [0, 0, 0]).all())
def test_simple(self): size = (32, 64) wximage = wx.EmptyImage(*size) # black RGB ndimage = wxImage2NDImage(wximage) self.assertEqual(ndimage.shape[0:2], size[-1:-3:-1]) self.assertEqual(ndimage.shape[2], 3) # RGB self.assertTrue((ndimage[0,0] == [0, 0, 0]).all())
def snapshot_viewport(self, tab, filepath, exporter, anim): """ Save a snapshot of the raw image from the focused view to the filesystem. :param tab: (Tab) the current tab to save the snapshot from :param filepath: (str) full path to the destination file :param exporter: (func) exporter to use for writing the file :param anim: (bool) if True will show an animation When no dialog is shown, the name of the file will follow the scheme `date`-`time`.tiff (e.g., 20120808-154812.tiff) and it will be saved in the user's picture directory. """ try: tab_data_model = tab.tab_data_model # Take all the streams available streams = tab_data_model.streams.value if not streams: logging.info("Failed to take snapshot, no stream in tab %s", tab.name) return if anim: self.start_snapshot_animation() # get currently focused view view = tab_data_model.focussedView.value if not view: try: view = tab_data_model.views.value[0] except IndexError: view = None # let's try to get a thumbnail if not view or view.thumbnail.value is None: thumbnail = None else: # need to convert from wx.Image to ndimage thumbnail = img.wxImage2NDImage(view.thumbnail.value, keep_alpha=False) # add some basic info to the image mpp = view.mpp.value metadata = {model.MD_POS: view.view_pos.value, model.MD_PIXEL_SIZE: (mpp, mpp), model.MD_DESCRIPTION: "Composited image preview"} thumbnail = model.DataArray(thumbnail, metadata=metadata) # for each stream seen in the viewport raw_images = [] for s in streams: data = s.raw # list of raw images for this stream (with metadata) for d in data: # add the stream name to the image if not hasattr(d, "metadata"): # Not a DataArray => let's try to convert it try: d = model.DataArray(d) except Exception: logging.warning("Raw data of stream %s doesn't seem to be DataArray", s.name.value) continue if model.MD_DESCRIPTION not in d.metadata: d.metadata[model.MD_DESCRIPTION] = s.name.value raw_images.append(d) popup.show_message(self._main_frame, "Snapshot saved in %s" % (filepath,), timeout=3 ) # record everything to a file exporter.export(filepath, raw_images, thumbnail) logging.info("Snapshot saved as file '%s'.", filepath) except Exception: logging.exception("Failed to save snapshot")
def snapshot_viewport(self, tab, filepath, exporter, anim): """ Save a snapshot of the raw image from the focused view to the filesystem. :param tab: (Tab) the current tab to save the snapshot from :param filepath: (str) full path to the destination file :param exporter: (func) exporter to use for writing the file :param anim: (bool) if True will show an animation When no dialog is shown, the name of the file will follow the scheme `date`-`time`.tiff (e.g., 20120808-154812.tiff) and it will be saved in the user's picture directory. """ try: tab_data_model = tab.tab_data_model # Take all the streams available streams = tab_data_model.streams.value if not streams: logging.info("Failed to take snapshot, no stream in tab %s", tab.name) return if anim: self.start_snapshot_animation() # get currently focused view view = tab_data_model.focussedView.value if not view: try: view = tab_data_model.views.value[0] except IndexError: view = None # let's try to get a thumbnail if not view or view.thumbnail.value is None: thumbnail = None else: # need to convert from wx.Image to ndimage thumbnail = img.wxImage2NDImage(view.thumbnail.value, keep_alpha=False) # add some basic info to the image mpp = view.mpp.value metadata = { model.MD_POS: view.view_pos.value, model.MD_PIXEL_SIZE: (mpp, mpp), model.MD_DESCRIPTION: "Composited image preview" } thumbnail = model.DataArray(thumbnail, metadata=metadata) # for each stream seen in the viewport raw_images = [] for s in streams: data = s.raw if isinstance(data, tuple): # 2D tuple = tiles data = [mergeTiles(data)] for d in data: # add the stream name to the image if not hasattr(d, "metadata"): # Not a DataArray => let's try to convert it try: d = model.DataArray(d) except Exception: logging.warning( "Raw data of stream %s doesn't seem to be DataArray", s.name.value) continue if model.MD_DESCRIPTION not in d.metadata: d.metadata[model.MD_DESCRIPTION] = s.name.value raw_images.append(d) popup.show_message(self._main_frame, "Snapshot saved in %s" % (filepath, ), timeout=3) # record everything to a file exporter.export(filepath, raw_images, thumbnail) logging.info("Snapshot saved as file '%s'.", filepath) except Exception: logging.exception("Failed to save snapshot")
def test_label_overlay(self): logging.getLogger().setLevel(logging.DEBUG) cnvs = miccanvas.DblMicroscopeCanvas(self.panel) tab_mod = self.create_simple_tab_model() tab_mod.tool.choices |= {TOOL_RULER, TOOL_LABEL} view = tab_mod.focussedView.value self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) cnvs.setView(view, tab_mod) gol = cnvs.gadget_overlay test.gui_loop(0.1) # gadget overlay with no tools img = wx.Bitmap.ConvertToImage(cnvs._bmp_buffer) buffer_empty = wxImage2NDImage(img) self.assertTrue(numpy.all(buffer_empty == 0)) # Create a "big label" p_start_pos = (-0.00055, -0.00055) p_end_pos = (0.00055, 0.00055) label = wol.LabelGadget(cnvs, p_start_pos, p_end_pos) label.text = 'label1' gol._tools.append(label) # Create a 10 px label v_start_pos = (500, 500) v_end_pos = (500, 510) offset = cnvs.get_half_buffer_size() p_start_pos = cnvs.view_to_phys(v_start_pos, offset) p_end_pos = cnvs.view_to_phys(v_end_pos, offset) label = wol.LabelGadget(cnvs, p_start_pos, p_end_pos) label.text = 'label2' gol._tools.append(label) # Create a 1 px label b_start_pos = (599, 670) b_end_pos = (599, 671) offset = cnvs.get_half_buffer_size() p_start_pos = cnvs.buffer_to_phys(b_start_pos, offset) p_end_pos = cnvs.buffer_to_phys(b_end_pos, offset) label = wol.LabelGadget(cnvs, p_start_pos, p_end_pos) label.text = 'label3' gol._tools.append(label) # Add one ruler that will become the selected ruler p_start_pos = (0, 0) p_end_pos = (0, 0.00035) selected_label = wol.LabelGadget(cnvs, p_start_pos, p_end_pos) selected_label.text = 'label4' gol._tools.append(selected_label) # update drawing cnvs.update_drawing() test.gui_loop(0.1) # ruler overlay with 4 labels cnvs._dc_buffer.SelectObject(wx.NullBitmap) # Flush the buffer cnvs._dc_buffer.SelectObject(cnvs._bmp_buffer) img = wx.Bitmap.ConvertToImage(cnvs._bmp_buffer) new_buffer = wxImage2NDImage(img) assert_array_not_equal( buffer_empty, new_buffer, msg="Buffers are equal, which means that the labels were not drawn" ) # make the last label the selected one (highlighted) gol._selected_tool = selected_label gol._selected_tool.text = 'selected_label' cnvs.update_drawing() test.gui_loop(0.1) # ruler overlay with 4 labels, 1 of them is selected (highlighted) cnvs._dc_buffer.SelectObject(wx.NullBitmap) # Flush the buffer cnvs._dc_buffer.SelectObject(cnvs._bmp_buffer) img = wx.ImageFromBitmap(cnvs._bmp_buffer) sel_buffer = wxImage2NDImage(img) assert_array_not_equal( new_buffer, sel_buffer, msg="Buffers are equal, which means that the labels were not drawn" ) # Create a ruler v_start_pos = (500, 500) v_end_pos = (500, 510) offset = cnvs.get_half_buffer_size() p_start_pos = cnvs.view_to_phys(v_start_pos, offset) p_end_pos = cnvs.view_to_phys(v_end_pos, offset) ruler = wol.RulerGadget(cnvs, p_start_pos, p_end_pos) gol._tools.append(ruler) # Update drawing cnvs.update_drawing() test.gui_loop(0.1) # Gadget overlay with 4 rulers and 1 label # Ruler overlay with 4 rulers cnvs._dc_buffer.SelectObject(wx.NullBitmap) # Flush the buffer cnvs._dc_buffer.SelectObject(cnvs._bmp_buffer) img = wx.Bitmap.ConvertToImage(cnvs._bmp_buffer) rul_buffer = wxImage2NDImage(img) assert_array_not_equal( sel_buffer, rul_buffer, msg="Buffers are equal, which means that the ruler was not drawn")