def test_buttons(self): # colour button cb = self.buttons['ColourButton'] red = (255, 0, 0) cb.set_colour(red) test.gui_loop() self.assertEqual(red, cb.get_colour()) test.gui_loop() # PopupImageButton pib = self.buttons['PopupImageButton'] nb_options = 5 for i in range(nb_options): def tmp(option=i): print "option %s chosen" % option pib.add_choice("option %s" % i, tmp) test.gui_loop() self.assertEqual(len(pib.choices), nb_options) self.assertEqual(pib.menu.MenuItemCount, nb_options) pib.remove_choice("option 0") test.gui_loop() self.assertEqual(len(pib.choices), nb_options - 1) self.assertEqual(pib.menu.MenuItemCount, nb_options - 1) test.gui_loop()
def test_bandwidth_stream_panel(self): # Spectrum data = numpy.ones((251, 1, 1, 200, 300), dtype="uint16") data[:, 0, 0, :, 3] = range(200) data[:, 0, 0, :, 3] *= 3 data[2, :, :, :, :] = range(300) data[200, 0, 0, 2] = range(300) wld = 433e-9 + numpy.array(range(data.shape[0])) * 0.1e-9 md = {model.MD_SW_VERSION: "1.0-test", model.MD_DESCRIPTION: "Spectrum", model.MD_ACQ_DATE: time.time(), model.MD_BPP: 12, model.MD_PIXEL_SIZE: (2e-5, 2e-5), # m/px model.MD_POS: (1.2e-3, -30e-3), # m model.MD_WL_LIST: wld, } spec_data = model.DataArray(data, md) fake_spec_stream = StaticSpectrumStream("First Fixed Stream", spec_data) tab_mod = self.create_simple_tab_model() stream_bar = self.app.test_frame.stream_bar _ = StreamBarController(tab_mod, stream_bar) stream_panel = stream_comp.StreamPanel(stream_bar, fake_spec_stream) stream_bar.add_stream_panel(stream_panel) test.gui_loop()
def test_add_text_field(self): self.settings_panel.clear_all() self.settings_panel.add_text_field("Read only", "value", True) self.settings_panel.add_divider() self.settings_panel.add_text_field("Writable", "other value") self.assertEqual(len(self.settings_panel.GetChildren()), 5) gui_loop(0.5)
def test_divider(self): self.settings_panel.clear_all() self.settings_panel.add_readonly_field("Above the divider") self.settings_panel.add_divider() self.settings_panel.add_readonly_field("Below the divider") self.assertEqual(len(self.settings_panel.GetChildren()), 3) gui_loop(0.5)
def test_mirror_arc_overlay(self): cnvs = miccanvas.SparcARCanvas(self.panel) cnvs.scale = 20000 self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) cnvs.flip = 0 cnvs.update_drawing() def flip(evt): if cnvs.flip == wx.VERTICAL: cnvs.flip = 0 else: cnvs.flip = wx.VERTICAL cnvs.update_drawing() evt.Skip() def zoom(evt): if evt.GetWheelRotation() > 0: cnvs.scale *= 1.1 else: cnvs.scale *= 0.9 cnvs.update_drawing() cnvs.Bind(wx.EVT_LEFT_DCLICK, flip) cnvs.Bind(wx.EVT_MOUSEWHEEL, zoom) test.gui_loop()
def test_world_select_overlay(self): cnvs = miccanvas.DblMicroscopeCanvas(self.panel) tab_mod = self.create_simple_tab_model() view = tab_mod.focussedView.value self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) cnvs.setView(view, tab_mod) wsol = wol.WorldSelectOverlay(cnvs) wsol.activate() cnvs.add_world_overlay(wsol) tol = vol.TextViewOverlay(cnvs) tol.add_label("Right click to toggle tool") cnvs.add_view_overlay(tol) test.gui_loop() def toggle(evt): if wsol.active: wsol.deactivate() else: wsol.activate() evt.Skip() cnvs.Bind(wx.EVT_RIGHT_UP, toggle)
def test_mirror_arc_overlay(self): cnvs = miccanvas.SparcARCanvas(self.panel) cnvs.scale = 20000 cnvs.add_world_overlay(cnvs.mirror_ol) cnvs.mirror_ol.activate() self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) def zoom(evt): mi, ma = 1000, 80000 abs_val = abs(evt.step_value) if evt.direction == wx.RIGHT: old = cnvs.scale cnvs.scale *= 1.1 ** abs_val print("bigger %0.2f > %0.2f" % (old, cnvs.scale)) if not mi <= cnvs.scale <= ma: cnvs.scale = ma else: print("smaller") cnvs.scale *= 0.9 ** abs_val if not mi <= cnvs.scale <= ma: cnvs.scale = mi wx.CallAfter(cnvs.update_drawing) try: self.pm = Powermate(self.frame) except LookupError: if TEST_NOHW: self.skipTest("No hardware detected, skipping test") cnvs.Bind(EVT_KNOB_ROTATE, zoom) test.gui_loop()
def test_dyeexpander(self): mic_mod = FakeMicroscopeModel() stream_bar = self.app.test_frame.stream_bar _ = StreamController(mic_mod, stream_bar) fake_fluo_stream = FakeFluoStream("Fluo Stream") dye_panel = stream_comp.StreamPanel( stream_bar, fake_fluo_stream, mic_mod) stream_bar.add_stream(dye_panel) # print stream_panel._expander.GetSize() stream_panel = stream_comp.StreamPanel( stream_bar, fake_fluo_stream, mic_mod) stream_bar.add_stream(stream_panel) # print stream_panel._expander.GetSize() test.gui_loop() # Clear remaining streams stream_bar.clear() test.gui_loop() self.assertEqual(stream_bar.get_size(), 0)
def test_plot_viewport(self): vwp = viewport.PointSpectrumViewport(self.panel) self.add_control(vwp, wx.EXPAND, proportion=1) for horz, vert in PLOTS: vwp.canvas.set_1d_data(horz, vert) vwp.Refresh() test.gui_loop(0.5)
def test_crosshair_overlay(self): cnvs = miccanvas.DblMicroscopeCanvas(self.panel) self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) hol = vol.CrossHairOverlay(cnvs) cnvs.add_view_overlay(hol) test.gui_loop()
def test_spot_mode_overlay(self): cnvs = miccanvas.DblMicroscopeCanvas(self.panel) self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) sol = vol.SpotModeOverlay(cnvs) cnvs.add_view_overlay(sol) test.gui_loop()
def test_add_readonly_field(self): self.settings_panel.clear_all() self.settings_panel.set_default_message("add_readonly_field") self.settings_panel.add_readonly_field("No value") self.settings_panel.add_readonly_field("No select", "!@#$^%$#%", False) self.settings_panel.add_readonly_field("Can select", ":) :) :)") self.assertEqual(len(self.settings_panel.GetChildren()), 6) gui_loop(0.5)
def test_grid_resize(self): test.set_sleep_time(300) gp = self.frame.grid_panel gui_loop() self.frame.SetSize((600, 600)) self.frame.Center()
def test_threading(self): self.app.test_frame.SetSize((400, 400)) self.app.test_frame.Center() self.app.test_frame.Layout() test.gui_loop() tab = self.create_simple_tab_model() view = tab.focussedView.value # Changes in default values might affect other test, so we need to know self.assertEqual(view.mpp.value, 1e-6, "The default mpp value has changed!") cnvs = miccanvas.DblMicroscopeCanvas(self.panel) cnvs.default_margin = 0 cnvs.fit_view_to_next_image = False # Create a even black background, so we can test pixel values cnvs.background_brush = wx.BRUSHSTYLE_SOLID self.add_control(cnvs, flags=wx.EXPAND, proportion=1) test.gui_loop() # Changes in default values might affect other test, so we need to know self.assertEqual(cnvs.scale, 1, "Default canvas scale has changed!") cnvs.setView(view, tab) # Setting the view, calls _onMPP with the view.mpp value # mpwu / mpp = scale => 1 (fixed, default) / view.mpp (1e-5) self.assertEqual(cnvs.scale, 1 / view.mpp.value) # Make sure the buffer is set at the right size # self.assertEqual(cnvs._bmp_buffer_size, (300, 300)) ############ Create test image ############### img = generate_img_data(20, 20, 4) # 100 pixels is 1e-4 meters img.metadata[model.MD_PIXEL_SIZE] = (1e-6, 1e-6) img.metadata[model.MD_POS] = (0, 0) img.metadata[model.MD_DIMS] = "YXC" # im_scale = img.metadata[model.MD_PIXEL_SIZE][0] / cnvs.mpwu # self.assertEqual(im_scale, img.metadata[model.MD_PIXEL_SIZE][0]) stream1 = RGBStream("s1", img) view.addStream(stream1) # Verify view mpp and canvas scale self.assertEqual(view.mpp.value, 1e-6, "Default mpp value has changed!") self.assertEqual(cnvs.scale, 1 / view.mpp.value, "Canvas scale should not have changed!") cnvs.update_drawing() view.mpp.value = 1e-5 shift = (10, 10) cnvs.shift_view(shift)
def test_add_stream(self): test.gui_loop() tab_mod = self.create_simple_tab_model() stream_bar = self.app.test_frame.stream_bar streambar_cont = StreamBarController(tab_mod, stream_bar) self.assertEqual(stream_bar.btn_add_stream.IsShown(), True) # No actions should be linked to the add stream button self.assertEqual(len(streambar_cont.get_actions()), 0) # Add a callback/name combo to the add button def brightfield_callback(): fake_stream = FakeBrightfieldStream("Brightfield") fixed_entry = stream_comp.StreamPanel(stream_bar, fake_stream) stream_bar.add_stream_panel(fixed_entry) streambar_cont.add_action("Brightfield", brightfield_callback) brightfield_callback() test.gui_loop() self.assertEqual(len(streambar_cont.get_actions()), 1) self.assertEqual(stream_bar.get_size(), 1) # Add another callback/name combo to the add button def sem_callback(): fake_stream = FakeSEMStream("SEM:EDT") fixed_entry = stream_comp.StreamPanel(stream_bar, fake_stream) stream_bar.add_stream_panel(fixed_entry) streambar_cont.add_action("SEM:EDT", sem_callback) sem_callback() test.gui_loop() self.assertEqual(len(streambar_cont.get_actions()), 2) self.assertEqual(stream_bar.get_size(), 2) # Remove the Brightfield stream streambar_cont.remove_action("Brightfield") test.gui_loop() self.assertEqual(len(streambar_cont.get_actions()), 1) # Add another callback/name combo to the add button def custom_callback(): fake_stream = FakeFluoStream("Custom") custom_entry = stream_comp.StreamPanel(stream_bar, fake_stream) stream_bar.add_stream_panel(custom_entry) streambar_cont.add_action("Custom", custom_callback) # Clear remaining streams stream_bar.clear() test.gui_loop()
def setUp(self): test.gui_loop() self.mmodel = self.create_simple_tab_model() self.view = self.mmodel.focussedView.value self.canvas = miccanvas.DblMicroscopeCanvas(self.panel) self.canvas.background_brush = wx.BRUSHSTYLE_SOLID # no special background self.add_control(self.canvas, flags=wx.EXPAND, proportion=1) test.gui_loop() self.canvas.setView(self.view, self.mmodel)
def test_view_select_overlay(self): # Create and add a miccanvas cnvs = miccanvas.DblMicroscopeCanvas(self.panel) self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) vsol = vol.ViewSelectOverlay(cnvs) vsol.activate() cnvs.add_view_overlay(vsol) # cnvs.current_mode = guimodel.TOOL_ZOOM test.gui_loop()
def test_spot_mode_overlay(self): cnvs = miccanvas.DblMicroscopeCanvas(self.panel) cnvs.background_brush = wx.BRUSHSTYLE_CROSS_HATCH self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) sol = vol.SpotModeOverlay(cnvs) sol.activate() cnvs.add_view_overlay(sol) cnvs.update_drawing() test.gui_loop()
def test_pixel_select_overlay(self): cnvs = miccanvas.DblMicroscopeCanvas(self.panel) tab_mod = self.create_simple_tab_model() view = tab_mod.focussedView.value self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) # FIXME: when setView is called *before* the add_control, the picture goes black and no # pixels are visible cnvs.setView(view, tab_mod) cnvs.current_mode = TOOL_POINT psol = wol.PixelSelectOverlay(cnvs) psol.activate() psol.enabled = True cnvs.add_world_overlay(psol) psol.set_data_properties(1e-05, (0.0, 0.0), (17, 19)) width_va = omodel.IntVA(1) psol.connect_selection(omodel.TupleVA(), width_va) view.mpp.value = 1e-06 psol._selected_pixel_va.value = (8, 8) test.gui_loop() # Tool toggle for debugging tol = vol.TextViewOverlay(cnvs) tol.add_label("Right click to toggle tool", (10, 30)) cnvs.add_view_overlay(tol) def toggle(evt): if psol.active: psol.deactivate() else: psol.activate() evt.Skip() cnvs.Bind(wx.EVT_RIGHT_UP, toggle) cnvs.disable_drag() def on_key(evt): k = evt.GetKeyCode() if k == wx.WXK_DOWN and width_va.value > 1: width_va.value -= 1 elif k == wx.WXK_UP: width_va.value += 1 else: pass cnvs.Bind(wx.EVT_KEY_UP, on_key)
def test_bandwidth_stream_panel(self): tab_mod = self.create_simple_tab_model() stream_bar = self.app.test_frame.stream_bar _ = StreamBarController(tab_mod, stream_bar) fake_spec_stream = FakeSpectrumStream("First Fixed Stream") stream_panel = stream_comp.StreamPanel(stream_bar, fake_spec_stream) stream_bar.add_stream_panel(stream_panel) test.gui_loop()
def test_polar_overlay(self): cnvs = miccanvas.AngularResolvedCanvas(self.panel) self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) test.gui_loop(0.1) cnvs.polar_overlay.phi_deg = 60 cnvs.polar_overlay.theta_deg = 60 cnvs.polar_overlay.intensity_label.text = "101" test.gui_loop()
def test_onedimensional_canvas(self): cnvs = miccanvas.TwoDPlotCanvas(self.panel) cnvs.SetBackgroundColour("#00599B") self.add_control(cnvs, wx.EXPAND, proportion=1) im_data = generate_img_data(200, 10, 4) cnvs.set_2d_data(im_data) cnvs.update_drawing() test.gui_loop(0.2)
def test_basic_move(self): mpp = 0.00001 self.view.mpp.value = mpp self.assertEqual(mpp, self.view.mpp.value) im1 = model.DataArray(numpy.zeros((11, 11, 3), dtype="uint8")) px1_cent = (5, 5) # Red pixel at center, (5,5) im1[px1_cent] = [255, 0, 0] im1.metadata[model.MD_PIXEL_SIZE] = (mpp * 10, mpp * 10) im1.metadata[model.MD_POS] = (0, 0) stream1 = RGBStream("s1", im1) im2 = model.DataArray(numpy.zeros((201, 201, 3), dtype="uint8")) #pylint: disable=E1101 px2_cent = tuple((s - 1) // 2 for s in im2.shape[:2]) # Blue pixel at center (100,100) im2[px2_cent] = [0, 0, 255] # 200, 200 => outside of the im1 # (+0.5, -0.5) to make it really in the center of the pixel im2.metadata[model.MD_PIXEL_SIZE] = (mpp, mpp) im2.metadata[model.MD_POS] = (200.5 * mpp, 199.5 * mpp) stream2 = RGBStream("s2", im2) self.view.addStream(stream1) self.view.addStream(stream2) # view might set its mpp to the mpp of first image => reset it self.view.mpp.value = mpp self.assertEqual(mpp, self.view.mpp.value) shift = (100, 100) self.canvas.shift_view(shift) # merge the images ratio = 0.5 self.view.merge_ratio.value = ratio self.assertEqual(ratio, self.view.merge_ratio.value) test.gui_loop() # it's supposed to update in less than 1s wx.MilliSleep(500) test.gui_loop() # copy the buffer into a nice image here result_im = get_image_from_buffer(self.canvas) px1 = get_rgb(result_im, result_im.Width / 2 + shift[0], result_im.Height / 2 + shift[1]) self.assertEqual(px1, (255, 0, 0)) px2 = get_rgb(result_im, result_im.Width / 2 + 200 + shift[0], result_im.Height / 2 - 200 + shift[1]) self.assertEqual(px2, (0, 0, 255))
def test_add_combobox_control(self): conf = { 'labels': ['one', 'two', 'three', 'four', 'five'], 'choices': [1, 2, 3, 4, 5], } self.settings_panel.clear_all() _, ctrl = self.settings_panel.add_combobox_control("Combobox", value=2, conf=conf) gui_loop() gui_loop(0.5)
def test_add_radio_control(self): conf = { 'size': (-1, 16), 'choices': [1, 2, 3, 4, 5], 'labels': ['a', 'b', 'c', 'd', 'e'], 'units': 'm', } self.settings_panel.clear_all() _, ctrl = self.settings_panel.add_radio_control("Radio", value=3, conf=conf) gui_loop(0.5)
def test_bitmap_canvas(self): self.frame.SetSize((1000, 1000)) cnvs = canvas.BitmapCanvas(self.panel) cnvs.SetBackgroundColour("#00599B") self.add_control(cnvs, wx.EXPAND, proportion=1) im_data = generate_img_data(300, 300, 4) img = (im_data, (0.0, 0.0), (1.0, 1.0), True, None, None, 0, None, "bitmap test") cnvs.set_images([img, None]) cnvs.update_drawing() test.gui_loop(0.5)
def test_colour_button(self): btn = buttons.ColourButton(self.panel, colour=wx.RED) self.add_control(btn, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL) self.btn = buttons.ColourButton(self.panel, colour=wx.BLUE, use_hover=True) self.add_control(self.btn, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL) test.gui_loop() def switch_image(_): self.btn.set_colour("#FF44FF") self.btn.Bind(wx.EVT_LEFT_DOWN, switch_image)
def test_view_select_overlay(self): # Create and add a miccanvas cnvs = miccanvas.SecomCanvas(self.panel) # cnvs.SetBackgroundColour(wx.WHITE) # cnvs.SetForegroundColour("#DDDDDD") self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) vsol = vol.ViewSelectOverlay(cnvs, "test view selection") cnvs.view_overlays.append(vsol) cnvs.active_overlay = vsol cnvs.current_mode = miccanvas.MODE_SECOM_ZOOM test.gui_loop()
def test_focus_overlay(self): cnvs = miccanvas.DblMicroscopeCanvas(self.panel) self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) class FakeFocus(object): def moveRel(self, dummy): pass tab_mod = self.create_simple_tab_model() view = tab_mod.focussedView.value view._focus = [FakeFocus(), FakeFocus()] cnvs.setView(view, tab_mod) test.gui_loop()
def test_px_int_txt_ctrl(self): ctrl = UnitIntegerCtrl(self.panel, value=123456789, min_val=1, max_val=10000000000, unit='px') self.add_control(ctrl, label="UnitIntegerCtrl px", flags=wx.EXPAND | wx.ALL) self.assertEqual(ctrl.GetValue(), 123456789) self.assertEqual(ctrl.get_value_str(), u"123456789 px") test.gui_loop(100) # Create simulator and focus the field sim = wx.UIActionSimulator() # Focusing the field will select all the text in it ctrl.SetFocus() test.gui_loop(100) # Set the value to 1 px (minus and period should not register) for c in "-0.001\r": sim.Char(ord(c)) test.gui_loop(10) self.assertEqual(ctrl.GetValue(), 1) self.assertEqual(ctrl.get_value_str(), u"1 px") ctrl.SetSelection(0, 20) for c in "44px\r": sim.Char(ord(c)) test.gui_loop(10) self.assertEqual(ctrl.GetValue(), 44) self.assertEqual(ctrl.get_value_str(), u"44 px")
def test_tab_button(self): btn = buttons.TabButton(self.panel, label="Tab Test") self.add_control(btn, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL) test.gui_loop() test.gui_loop(0.5) btn.SetToggle(True)
def tearDown(self): test.gui_loop(1) super(ButtonsTestCase, self).tearDown()
def test_foldpanel_manipulation(self): appfpb = self.app.test_frame.fpb fpb_height = appfpb.BestSize.GetHeight() # Add an extra fold panel new_panel = appfpb.create_and_add_item("Test panel 4", False) self.app.test_frame.Layout() test.gui_loop(0.1) # The height of the parent should be 42 pixels higher # (CaptionBar height + 1px border) self.assertEqual(fpb_height + 42, appfpb.BestSize.GetHeight()) self.assertEqual(len(appfpb.GetChildren()), 4) test.gui_loop(0.1) new_panel.add_item( wx.StaticText(new_panel, new_panel.GetId(), "ADDED LABEL")) test.gui_loop(0.1) # A scroll bars should not appear yet self.assertEqual(appfpb.has_vert_scrollbar(), False) self.assertEqual(appfpb.has_horz_scrollbar(), False) for i in range(6): new_panel.add_item( wx.StaticText(new_panel, new_panel.GetId(), "ADDED LABEL %d" % i)) test.gui_loop(0.1) # Vertical scroll bar should have appeared self.assertEqual(appfpb.has_vert_scrollbar(), True) self.assertEqual(appfpb.has_horz_scrollbar(), False) new_panel.add_item( wx.StaticText(new_panel, new_panel.GetId(), "ADDED LABEL")) new_panel.add_item( wx.StaticText(new_panel, new_panel.GetId(), "ADDED LABEL")) test.gui_loop(0.1) # 10 Child windows in the new panel self.assertEqual(len(new_panel._container.GetChildren()), 9) # 4 fold panels total in the bar self.assertEqual(len(appfpb.GetChildren()), 4) test.gui_loop(0.1) appfpb.remove_item(new_panel) test.gui_loop(0.1) # New panel removed, back to 3 self.assertEqual(len(appfpb.GetChildren()), 3) # TODO: sometimes the window is bigger, and the scrollbar doesn't appear # even if we've added 7 children false_pos_warn = ("This might be a false positive. " "Run test module stand-alone to verify") # Scroll bars should be gone again self.assertEqual(appfpb.has_vert_scrollbar(), False, false_pos_warn) self.assertEqual(appfpb.has_horz_scrollbar(), False, false_pos_warn) test.gui_loop(0.1) top_panel = self.app.test_frame.panel_1 new_labels = [] # Normally, 5 children for dummy in range(5): item = wx.StaticText(top_panel, top_panel.GetId(), "ADDED LABEL") top_panel.add_item(item) new_labels.append(item) test.gui_loop(0.1) # No scrollbar yet, but almost full (normally) self.assertEqual(appfpb.has_vert_scrollbar(), False, false_pos_warn) self.assertEqual(appfpb.has_horz_scrollbar(), False, false_pos_warn) test.gui_loop(0.1) # One last "drop" to make the scrollbar appear item = wx.StaticText(top_panel, top_panel.GetId(), "ADDED LABEL") top_panel.add_item(item) new_labels.append(item) test.gui_loop(0.1) # Vertical Scroll bar self.assertEqual(appfpb.has_vert_scrollbar(), True, false_pos_warn) self.assertEqual(appfpb.has_horz_scrollbar(), False, false_pos_warn) # Count children of the top fold panel: 2 labels and 5 added labels: 7 total self.assertEqual(len(top_panel._container.GetChildren()), 8) new_labels.reverse() for label in new_labels: top_panel.remove_item(label) test.gui_loop(0.1) # Count children of the top fold panel: 1 caption bar, 2 labels self.assertEqual(len(top_panel._container.GetChildren()), 2) top_panel.remove_all() test.gui_loop(0.1) # Count children of the top fold panel: 1 caption bar self.assertEqual(len(top_panel._container.GetChildren()), 0) # Insert 3 windows, out of order, into the top fold panel item = wx.StaticText(top_panel, top_panel.GetId(), "LABEL 1") top_panel.insert_item(item, 0) test.gui_loop(0.1) item = wx.StaticText(top_panel, top_panel.GetId(), "LABEL 2") top_panel.insert_item(item, 0) test.gui_loop(0.1) item = wx.StaticText(top_panel, top_panel.GetId(), "LABEL 3") top_panel.insert_item(item, 0) test.gui_loop(0.1)
def test_roa_select_overlay(self): # but it should be a simple miccanvas cnvs = miccanvas.DblMicroscopeCanvas(self.panel) self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) rsol = wol.RepetitionSelectOverlay(cnvs) rsol.activate() cnvs.add_world_overlay(rsol) cnvs.scale = 400 cnvs.update_drawing() test.gui_loop() wroi = [-0.1, 0.3, 0.2, 0.4] # in m rsol.set_physical_sel(wroi) test.gui_loop() wroi_back = rsol.get_physical_sel() for o, b in zip(wroi, wroi_back): self.assertAlmostEqual(o, b, msg="wroi (%s) != bak (%s)" % (wroi, wroi_back)) rsol.repetition = (3, 2) rsol.fill = wol.RepetitionSelectOverlay.FILL_POINT pos = cnvs.margins[0] + 10, cnvs.margins[1] + 10 rsol.add_label("Repetition fill will change in 2 seconds.", pos, colour=(0.8, 0.2, 0.1)) # cnvs.update_drawing() test.gui_loop(2) rsol.set_physical_sel((-0.1, -0.3, 0.4, 0.4)) rsol.repetition = (50, 80) rsol.fill = wol.RepetitionSelectOverlay.FILL_GRID pos = cnvs.margins[0] + 10, cnvs.margins[1] + 10 rsol.add_label("Repetition fill will change in 2 seconds.", pos, colour=(0.8, 0.2, 0.1)) # cnvs.update_drawing() test.gui_loop(2) # Fine grid => solid colour rsol.repetition = (500, 800) rsol.fill = wol.RepetitionSelectOverlay.FILL_GRID pos = cnvs.margins[0] + 10, cnvs.margins[1] + 10 rsol.add_label("Repetition fill will change in 2 seconds.", pos, colour=(0.8, 0.2, 0.1)) # cnvs.update_drawing() test.gui_loop(2) tol = vol.TextViewOverlay(cnvs) tol.add_label("Right click to toggle tool") cnvs.add_view_overlay(tol) def toggle(evt): if rsol.active: rsol.deactivate() else: rsol.activate() evt.Skip() cnvs.Bind(wx.EVT_RIGHT_UP, toggle) test.gui_loop()
def test_text_view_overlay_rotate(self): cnvs = canvas.BitmapCanvas(self.panel) self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) ol = vol.TextViewOverlay(cnvs) ol.canvas_padding = 0 cnvs.add_view_overlay(ol) # Text should exactly overlap rl = ol.add_label(u"█ you should only see red", pos=(0, 0), font_size=20, deg=0, flip=False, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05) sl = ol.add_label(u"█ you should only see red", pos=(0, 0), font_size=20, colour=(1, 0, 0), align=wx.ALIGN_LEFT) test.gui_loop(0.1) self.assertEqual(rl.render_pos, sl.render_pos) ol.clear_labels() ol.add_label(u"█ no rotate", pos=(200, 0), font_size=20, colour=(1, 0, 0), align=wx.ALIGN_LEFT) tl = ol.add_label(u"█ rotate left", pos=(200, 25), font_size=20, deg=0, flip=False, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) tr = ol.add_label(u"rotate right █", pos=(200, 50), font_size=20, align=wx.ALIGN_RIGHT, deg=0, flip=False, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) tc = ol.add_label(u"rotate center █", pos=(200, 75), font_size=20, align=wx.ALIGN_CENTRE_HORIZONTAL, deg=0, flip=False, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(1) for l in (tl, tr, tc): l.deg = 15 test.gui_loop(0.5) cnvs.Refresh()
def test_basic_display(self): """ Draws a view with two streams, one with a red pixel with a low density and one with a blue pixel at a high density. """ mpp = 0.00001 self.view.mpp.value = mpp self.assertEqual(mpp, self.view.mpp.value) self.view.show_crosshair.value = False # Disable auto fit because (1) it's not useful as we set everything # manually, and (2) depending on whether it's called immediately after # adding the first stream or only after the second stream, the result # is different. self.canvas.fit_view_to_next_image = False # add images im1 = model.DataArray(numpy.zeros((11, 11, 3), dtype="uint8")) px1_cent = (5, 5) # Red pixel at center, (5,5) im1[px1_cent] = [255, 0, 0] im1.metadata[model.MD_PIXEL_SIZE] = (mpp * 10, mpp * 10) im1.metadata[model.MD_POS] = (0, 0) im1.metadata[model.MD_DIMS] = "YXC" stream1 = RGBStream("s1", im1) im2 = model.DataArray(numpy.zeros((201, 201, 3), dtype="uint8")) px2_cent = tuple((s - 1) // 2 for s in im2.shape[:2]) # Blue pixel at center (100,100) im2[px2_cent] = [0, 0, 255] # 200, 200 => outside of the im1 # (+0.5, -0.5) to make it really in the center of the pixel im2.metadata[model.MD_PIXEL_SIZE] = (mpp, mpp) im2.metadata[model.MD_POS] = (200.5 * mpp, 199.5 * mpp) im2.metadata[model.MD_DIMS] = "YXC" stream2 = RGBStream("s2", im2) self.view.addStream(stream1) self.view.addStream(stream2) # reset the mpp of the view, as it's automatically set to the first image test.gui_loop(0.5) logging.debug("View pos = %s, fov = %s, mpp = %s", self.view.view_pos.value, self.view.fov_buffer.value, self.view.mpp.value) self.view.mpp.value = mpp shift = (63, 63) self.canvas.shift_view(shift) # merge the images ratio = 0.5 self.view.merge_ratio.value = ratio # self.assertEqual(ratio, self.view.merge_ratio.value) # it's supposed to update in less than 0.5s test.gui_loop(0.5) # copy the buffer into a nice image here result_im = get_image_from_buffer(self.canvas) # for i in range(result_im.GetWidth()): # for j in range(result_im.GetHeight()): # px = get_rgb(result_im, i, j) # if px != (0, 0, 0): # print px, i, j px1 = get_rgb(result_im, result_im.Width // 2 + shift[0], result_im.Height // 2 + shift[1]) self.assertEqual(px1, (128, 0, 0)) # Ratio is at 0.5, so 255 becomes 128 px2 = get_rgb(result_im, result_im.Width // 2 + 200 + shift[0], result_im.Height // 2 - 200 + shift[1]) self.assertEqual(px2, (0, 0, 255)) # remove first picture self.view.removeStream(stream1) test.gui_loop(0.5) result_im = get_image_from_buffer(self.canvas) px2 = get_rgb(result_im, result_im.Width // 2 + 200 + shift[0], result_im.Height // 2 - 200 + shift[1]) self.assertEqual(px2, (0, 0, 255))
def test_expander(self): test.gui_loop() tab_mod = self.create_simple_tab_model() stream_bar = self.app.test_frame.stream_bar _ = StreamBarController(tab_mod, stream_bar) fake_sem_stream = FakeSEMStream("First Fixed Stream") stream_panel = stream_comp.StreamPanel(stream_bar, fake_sem_stream) stream_bar.add_stream_panel(stream_panel) test.gui_loop() # REMOVE BUTTON TEST old_label_pos = stream_panel._header.ctrl_label.GetPosition() stream_panel.show_remove_btn(False) test.gui_loop() self.assertFalse(stream_panel._header.btn_remove.IsShown()) new_label_pos = stream_panel._header.ctrl_label.GetPosition() self.assertEqual(old_label_pos, new_label_pos) stream_panel.show_remove_btn(True) test.gui_loop() self.assertTrue(stream_panel._header.btn_remove.IsShown()) new_label_pos = stream_panel._header.ctrl_label.GetPosition() self.assertEqual(old_label_pos, new_label_pos) # END REMOVE BUTTON TEST # VISIBILITY BUTTON TEST old_pbtn_pos = stream_panel._header.btn_update.GetPosition() stream_panel.show_visible_btn(False) test.gui_loop() self.assertFalse(stream_panel._header.btn_show.IsShown()) new_pbtn_pos = stream_panel._header.btn_update.GetPosition() self.assertEqual(old_pbtn_pos, new_pbtn_pos) stream_panel.show_visible_btn(True) test.gui_loop() self.assertTrue(stream_panel._header.btn_show.IsShown()) new_pbtn_pos = stream_panel._header.btn_update.GetPosition() self.assertEqual(old_pbtn_pos, new_pbtn_pos) # END VISIBILITY BUTTON TEST # PLAY BUTTON TEST old_vbtn_pos = stream_panel._header.btn_show.GetPosition() stream_panel.show_updated_btn(False) test.gui_loop() self.assertFalse(stream_panel._header.btn_update.IsShown()) new_vbtn_pos = stream_panel._header.btn_show.GetPosition() self.assertEqual(old_vbtn_pos, new_vbtn_pos) stream_panel.show_updated_btn(True) test.gui_loop() self.assertTrue(stream_panel._header.btn_update.IsShown()) new_vbtn_pos = stream_panel._header.btn_show.GetPosition() self.assertEqual(old_vbtn_pos, new_vbtn_pos) # END BUTTON TEST # Clear remaining streams stream_bar.clear() test.gui_loop() self.assertEqual(stream_bar.get_size(), 0)
def test_clear_all(self): self.settings_panel.clear_all() self.assertEqual(0, len(self.settings_panel.GetChildren())) gui_loop(0.5)
def test_clear_default_message(self): self.settings_panel.set_default_message("Default msg test") self.assertEqual(True, self.settings_panel.message_ctrl.IsShown()) self.settings_panel.clear_default_message() self.assertEqual(False, self.settings_panel.message_ctrl.IsShown()) gui_loop(0.5)
def test_dye_ctrls(self): # Fake data to be used tab_mod = self.create_simple_tab_model() stream_bar = self.app.test_frame.stream_bar stream_cont = StreamBarController(tab_mod, stream_bar) fake_fluo_stream = FakeFluoStream("Fluo Stream") # Add the same stream twice sp1 = stream_cont.addStream(fake_fluo_stream) sp2 = stream_cont.addStream(fake_fluo_stream) self.assertIsInstance(sp1, StreamController) self.assertIsInstance(sp2, StreamController) # Test dye choices self.assertSequenceEqual( sorted(dye.DyeDatabase.keys()), sorted(sp1.stream_panel._header.ctrl_label.GetChoices())) # Get the excitation combo box (there should be only one) self.assertIn("excitation", sp1.entries) excitation_combo = sp1.entries["excitation"].value_ctrl # No real value testing, but at least making sure that changing the VA value, changes the # GUI components for choice in fake_fluo_stream.excitation.choices: old_value = excitation_combo.GetValue() old_colour = sp1._btn_excitation.colour changed = fake_fluo_stream.excitation.value != choice # Skip if the current value is equal to choice if changed: fake_fluo_stream.excitation.value = choice test.gui_loop(0.1) self.assertNotEqual(old_value, excitation_combo.GetValue()) self.assertNotEqual(old_colour, sp1._btn_excitation.colour) # Get the emission combo box (there should be only one) self.assertIn("emission", sp1.entries) emission_combo = sp1.entries["emission"].value_ctrl # No real value testing, but at least making sure that changing the VA value, changes the # GUI components for choice in fake_fluo_stream.emission.choices: old_value = emission_combo.GetValue() old_colour = sp2._btn_emission.colour changed = fake_fluo_stream.emission.value != choice # Skip if the current value is equal to choice if changed: fake_fluo_stream.emission.value = choice test.gui_loop(0.1) self.assertNotEqual(old_value, emission_combo.GetValue()) self.assertNotEqual(old_colour, sp2._btn_emission.colour) # Test intensity control values by manipulating the VAs # TODO: Move to separate test case txt_lowi = sp1.entries["low_intensity"].value_ctrl txt_highi = sp1.entries["high_intensity"].value_ctrl for i in range(0, 11): v = i / 10.0 fake_fluo_stream.intensityRange.value = (v, 1.0) test.gui_loop(0.1) self.assertEqual(v, txt_lowi.GetValue()) for i in range(0, 11): v = i / 10.0 fake_fluo_stream.intensityRange.value = (0.0, v) test.gui_loop(0.1) self.assertEqual(v, txt_highi.GetValue()) # Test if the range gets updated when the histogram changes fake_fluo_stream.intensityRange.range = ((0.25, 0.25), (0.75, 0.75)) fake_fluo_stream.histogram.notify(fake_fluo_stream.histogram.value) test.gui_loop(0.1) self.assertEqual((0.25, 0.75), txt_lowi.GetValueRange())
def test_static_streams(self): tab_mod = self.create_simple_tab_model() stream_bar = self.app.test_frame.stream_bar stream_cont = StreamBarController(tab_mod, stream_bar) fluomd = { model.MD_DESCRIPTION: "test", model.MD_ACQ_DATE: time.time(), model.MD_BPP: 12, model.MD_BINNING: (1, 2), # px, px model.MD_PIXEL_SIZE: (1e-6, 2e-5), # m/px model.MD_POS: (13.7e-3, -30e-3), # m model.MD_EXP_TIME: 1.2, # s model.MD_IN_WL: (500e-9, 520e-9), # m model.MD_OUT_WL: (600e-9, 630e-9), # m } fluod = model.DataArray(numpy.zeros((512, 256), dtype="uint16"), fluomd) # Create the streams the same way as when opening a file, in # cont.tabs.AnalysisTab.display_new_data() fluo_panel = stream_cont.addStatic("Fluo Stream", fluod, cls=StaticFluoStream, add_to_view=True) # Check it indeed created a panel entry to a static fluo stream self.assertIsInstance(fluo_panel.stream, StaticFluoStream) # White box testing: we expect that the excitation/emission information # are simple text, so no reference to the value controls needs to be saved # Get the emission combo box (there should be only one) self.assertNotIn("emission", fluo_panel.entries) self.assertNotIn("excitation", fluo_panel.entries) test.gui_loop() semmd = { model.MD_DESCRIPTION: "test", model.MD_ACQ_DATE: time.time(), model.MD_BPP: 12, model.MD_BINNING: (1, 2), # px, px model.MD_PIXEL_SIZE: (1e-6, 2e-5), # m/px model.MD_POS: (13.7e-3, -30e-3), # m model.MD_EXP_TIME: 1.2, # s } semd = model.DataArray(numpy.zeros((256, 256), dtype="uint16"), semmd) # Create the streams the same way as when opening a file, in # cont.tabs.AnalysisTab.display_new_data() sem_cont = stream_cont.addStatic("SEM Stream", semd, cls=StaticSEMStream, add_to_view=True) # Check it indeed created a panel entry to a static fluo stream self.assertIsInstance(sem_cont.stream, StaticSEMStream) # White box testing: we expect autobc is available self.assertIn("autobc", sem_cont.entries) # Clear remaining streams stream_bar.clear() test.gui_loop() self.assertEqual(stream_bar.get_size(), 0)
def test_navigable_plot_canvas(self): # Create and add a test plot canvas # cnvs = canvas.PlotCanvas(self.panel) cnvs = miccanvas.NavigableBarPlotCanvas(self.panel) cnvs.SetBackgroundColour(wx.BLACK) cnvs.SetForegroundColour("#DDDDDD") cnvs.set_closure(canvas.PLOT_CLOSE_STRAIGHT) self.add_control(cnvs, wx.EXPAND, proportion=1) # def toggle(event): # canv = event.GetEventObject() # if canv.plot_mode == canvas.PLOT_MODE_BAR: # canv.set_plot_mode(canvas.PLOT_MODE_LINE) # else: # canv.set_plot_mode(canvas.PLOT_MODE_BAR) # event.Skip() # Enable this bind to enable render toggling by clicking # cnvs.Bind(wx.EVT_LEFT_UP, toggle) test.gui_loop() test_data = [(0.5, 0.5), (0.6, 4.5), (4.5, 4.5), (4.6, 0.5)] test.gui_loop() cnvs.set_data(test_data) test.gui_loop(0.2) cnvs.set_plot_mode(canvas.PLOT_MODE_BAR) for horz, vert in PLOTS: cnvs.set_1d_data(horz, vert) range_x = (min(horz), max(horz)) w = abs(range_x[1] - range_x[0]) range_y = (min(vert), max(vert)) h = abs(range_y[1] - range_y[0]) # Set range smaller than width test_xrange = (range_x[0] + w * 0.1, range_x[1] - w * 0.1) cnvs.set_ranges(test_xrange, range_y) self.assertEqual(test_xrange, cnvs.display_xrange) test.gui_loop(0.2) # Set range smaller than width test_yrange = (range_y[0] + h * 0.1, range_y[1] - h * 0.1) cnvs.set_ranges(range_x, test_yrange) self.assertEqual(test_yrange, cnvs.display_yrange) test.gui_loop(0.2) test.gui_loop()
def test_stream_interface(self): test.gui_loop() tab_mod = self.create_simple_tab_model() stream_bar = self.app.test_frame.stream_bar _ = StreamBarController(tab_mod, stream_bar) # Hide the Stream add button self.assertEqual(stream_bar.btn_add_stream.IsShown(), True) stream_bar.hide_add_button() test.gui_loop() self.assertEqual(stream_bar.btn_add_stream.IsShown(), False) # Show Stream add button stream_bar.show_add_button() test.gui_loop() self.assertEqual(stream_bar.btn_add_stream.IsShown(), True) # Add an editable entry fake_cstream = FakeFluoStream("First Custom Stream") custom_entry = stream_comp.StreamPanel(stream_bar, fake_cstream) stream_bar.add_stream_panel(custom_entry) test.gui_loop() self.assertEqual(stream_bar.get_size(), 1) self.assertEqual(stream_bar.stream_panels.index(custom_entry), 0) # Add a fixed stream fake_fstream1 = FakeSEMStream("First Fixed Stream") fixed_entry = stream_comp.StreamPanel(stream_bar, fake_fstream1) stream_bar.add_stream_panel(fixed_entry) test.gui_loop() self.assertEqual(stream_bar.get_size(), 2) self.assertEqual(stream_bar.stream_panels.index(fixed_entry), 0) self.assertEqual(stream_bar.stream_panels.index(custom_entry), 1) # Add a fixed stream fake_fstream2 = FakeSEMStream("Second Fixed Stream") fixed_entry2 = stream_comp.StreamPanel(stream_bar, fake_fstream2) stream_bar.add_stream_panel(fixed_entry2) test.gui_loop() self.assertEqual(stream_bar.get_size(), 3) self.assertEqual(stream_bar.stream_panels.index(fixed_entry2), 1) self.assertEqual(stream_bar.stream_panels.index(custom_entry), 2) # Hide first stream by changing to a view that only show SEM streams semview = guimodel.MicroscopeView("SEM view", stream_classes=(SEMStream, )) # stream_bar.hide_stream(0) tab_mod.focussedView.value = semview test.gui_loop() self.assertEqual(stream_bar.get_size(), 3) self.assertFalse(custom_entry.IsShown()) # Delete the second fixed stream stream_bar.remove_stream_panel(fixed_entry2) test.gui_loop() self.assertEqual(stream_bar.get_size(), 2) # Clear remaining streams stream_bar.clear() test.gui_loop() self.assertEqual(stream_bar.get_size(), 0)
def test_pyramidal_3x2(self): """ Draws a view with two streams, one pyramidal stream square completely green, and the other is a red square with a blue square in the center """ mpp = 0.00001 self.view.mpp.value = mpp self.assertEqual(mpp, self.view.mpp.value) self.view.show_crosshair.value = False self.canvas.fit_view_to_next_image = False # There is no viewport, so FoV is not updated automatically => display # everything possible self.view.fov_buffer.value = (1.0, 1.0) init_pos = (1.0, 2.0) FILENAME = u"test" + tiff.EXTENSIONS[0] # 1 row of 2 tiles w = 600 h = 300 md = { model.MD_PIXEL_SIZE: (mpp, mpp), model.MD_POS: init_pos, model.MD_DIMS: 'YXC' } arr = model.DataArray(numpy.zeros((h, w, 3), dtype="uint8")) # make it all green arr[:, :] = [0, 255, 0] data = model.DataArray(arr, metadata=md) # export tiff.export(FILENAME, data, pyramid=True) acd = tiff.open_data(FILENAME) stream1 = RGBStream("test", acd.content[0]) im2 = model.DataArray(numpy.zeros((800, 800, 3), dtype="uint8")) # red background im2[:, :] = [255, 0, 0] # Blue square at center im2[390:410, 390:410] = [0, 0, 255] im2.metadata[model.MD_PIXEL_SIZE] = (mpp, mpp) im2.metadata[model.MD_POS] = init_pos im2.metadata[model.MD_DIMS] = "YXC" stream2 = RGBStream("s2", im2) self.view.addStream(stream1) self.view.addStream(stream2) self.canvas.shift_view((-init_pos[0] / mpp, init_pos[1] / mpp)) test.gui_loop(0.5) self.view.mpp.value = mpp # reset the mpp of the view, as it's automatically set to the first image test.gui_loop(0.5) result_im = get_image_from_buffer(self.canvas) # result_im.SaveFile('big.bmp', wx.BITMAP_TYPE_BMP) px2 = get_rgb(result_im, result_im.Width // 2, result_im.Height // 2) # center pixel, 1/3 green, 2/3 blue. The red image is the largest image self.assertEqual(px2, (0, 76, 179)) px2 = get_rgb(result_im, result_im.Width // 2 - 30, result_im.Height // 2 - 30) # background of the images, 1/3 green, 2/3 red self.assertEqual(px2, (179, 76, 0))
def test_marking_line_overlay(self): cnvs = miccanvas.TwoDPlotCanvas(self.panel) mlol = cnvs.markline_overlay self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) rgb = numpy.empty((30, 200, 3), dtype=numpy.uint8) data = model.DataArray(rgb) cnvs.set_2d_data(data, unit_x='m', unit_y='m', range_x=[200e-9, 500e-9], range_y=[0, 20e-6]) test.gui_loop() mlol.val.value = (201e-9, 10e-6) cnvs.Refresh() test.gui_loop(0.5) mlol.orientation = vol.MarkingLineOverlay.HORIZONTAL cnvs.Refresh() test.gui_loop(0.5) mlol.orientation = vol.MarkingLineOverlay.VERTICAL mlol.val.value = (301e-9, 12e-6) cnvs.Refresh() test.gui_loop(0.5) mlol.orientation = vol.MarkingLineOverlay.HORIZONTAL | vol.MarkingLineOverlay.VERTICAL mlol.val.value = (401e-9, 20e-6) cnvs.Refresh() test.gui_loop(0.5) # Out of the range mlol.val.value = (0, 0) cnvs.Refresh() test.gui_loop(0.5)
def tearDown(self): test.gui_loop() self.remove_all()
def test_text_view_overlay_align(self): cnvs = canvas.BitmapCanvas(self.panel) self.add_control(cnvs, wx.EXPAND, proportion=1, clear=True) ol = vol.TextViewOverlay(cnvs) cnvs.add_view_overlay(ol) ol.add_label("TextViewOverlay left", pos=(ol.view_width / 2, 10), colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05) ol.add_label("TextViewOverlay right", pos=(ol.view_width / 2, 26), align=wx.ALIGN_RIGHT, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05) ol.add_label("TextViewOverlay center", pos=(ol.view_width / 2, 42), align=wx.ALIGN_CENTER_HORIZONTAL, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05) ol.add_label("|", pos=(ol.view_width / 2, 58), align=wx.ALIGN_CENTER_HORIZONTAL, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) ol.add_label("|", pos=(ol.view_width / 2, 74), align=wx.ALIGN_CENTER_HORIZONTAL, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) ol.add_label("Relative to the center", pos=(ol.view_width / 2, 90), align=wx.ALIGN_CENTER_HORIZONTAL, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05) # Example on how a right aligned label can be kept on the right on resize def realign(evt): for label in ol.labels: label.pos = (ol.view_width / 2, label.pos[1]) evt.Skip() cnvs.Bind(wx.EVT_SIZE, realign) ol.canvas_padding = 0 ol.add_label("top left", pos=(0, 0), align=wx.ALIGN_LEFT, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05) ol.add_label("top right", pos=(ol.view_width, 0), align=wx.ALIGN_RIGHT, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05) ol.add_label("bottom left", pos=(0, ol.view_height), align=wx.ALIGN_BOTTOM, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05) ol.add_label("bottom right", pos=(ol.view_width, ol.view_height), align=wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM, flip=False, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05) ol.add_label("SHOULD NOT BE SEEN!", pos=(ol.view_width, ol.view_height / 2), align=wx.ALIGN_LEFT, flip=False, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05) ol.add_label("Visible because of flip", pos=(ol.view_width, ol.view_height / 2), align=wx.ALIGN_LEFT, flip=True, colour=hex_to_frgb(gui.FG_COLOUR_EDIT)) test.gui_loop(0.05)
def test_basic_display(self): """ Draws a view with two streams, one with a red pixel with a low density and one with a blue pixel at a high density. """ mpp = 0.00001 self.view.mpp.value = mpp self.assertEqual(mpp, self.view.mpp.value) self.view.show_crosshair.value = False # add images im1 = model.DataArray(numpy.zeros((11, 11, 3), dtype="uint8")) px1_cent = (5, 5) # Red pixel at center, (5,5) im1[px1_cent] = [255, 0, 0] im1.metadata[model.MD_PIXEL_SIZE] = (mpp * 10, mpp * 10) im1.metadata[model.MD_POS] = (0, 0) stream1 = RGBStream("s1", im1) im2 = model.DataArray(numpy.zeros((201, 201, 3), dtype="uint8")) px2_cent = tuple((s - 1) // 2 for s in im2.shape[:2]) # Blue pixel at center (100,100) im2[px2_cent] = [0, 0, 255] # 200, 200 => outside of the im1 # (+0.5, -0.5) to make it really in the center of the pixel im2.metadata[model.MD_PIXEL_SIZE] = (mpp, mpp) im2.metadata[model.MD_POS] = (200.5 * mpp, 199.5 * mpp) stream2 = RGBStream("s2", im2) self.view.addStream(stream1) self.view.addStream(stream2) # reset the mpp of the view, as it's automatically set to the first image self.view.mpp.value = mpp shift = (63, 63) self.canvas.shift_view(shift) # merge the images ratio = 0.5 self.view.merge_ratio.value = ratio self.assertEqual(ratio, self.view.merge_ratio.value) test.gui_loop(500) # it's supposed to update in less than 0.5s test.gui_loop(500) # copy the buffer into a nice image here result_im = get_image_from_buffer(self.canvas) # for i in range(result_im.GetWidth()): # for j in range(result_im.GetHeight()): # px = get_rgb(result_im, i, j) # if px != (0, 0, 0): # print px, i, j px1 = get_rgb(result_im, result_im.Width // 2 + shift[0], result_im.Height // 2 + shift[1]) self.assertEqual(px1, (255, 0, 0)) px2 = get_rgb(result_im, result_im.Width // 2 + 200 + shift[0], result_im.Height // 2 - 200 + shift[1]) self.assertEqual(px2, (0, 0, 255)) # remove first picture self.view.removeStream(stream1) test.gui_loop() test.gui_loop(500) result_im = get_image_from_buffer(self.canvas) px2 = get_rgb(result_im, result_im.Width // 2 + 200 + shift[0], result_im.Height // 2 - 200 + shift[1]) self.assertEqual(px2, (0, 0, 255))
def test_caption_position(self): """ Test if the caption positions don't when expanding and collapsing""" test.gui_loop(0.1) ini_positions = [i.GetPosition() for i in self.foldpanelitems] # Panel 1 COLLAPSE event = self.build_caption_event(self.app.test_frame.panel_1) wx.PostEvent(self.app.test_frame.panel_1, event) test.gui_loop(0.1) new_positions = [i.GetPosition() for i in self.foldpanelitems] self.assertEqual(ini_positions[0], new_positions[0]) self.assertGreater(ini_positions[1][1], new_positions[1][1]) self.assertGreater(ini_positions[2][1], new_positions[2][1]) test.gui_loop(0.1) # Panel 1 EXPAND wx.PostEvent(self.app.test_frame.panel_1, event) test.gui_loop(0.1) new_positions = [i.GetPosition() for i in self.foldpanelitems] self.assertEqual(new_positions, ini_positions) test.gui_loop(0.1) # Panel 2 EXPAND event = self.build_caption_event(self.app.test_frame.panel_2) wx.PostEvent(self.app.test_frame.panel_2, event) test.gui_loop(0.1) new_positions = [i.GetPosition() for i in self.foldpanelitems] self.assertEqual(ini_positions[0], new_positions[0]) self.assertEqual(new_positions[1][1], ini_positions[1][1]) self.assertGreater(new_positions[2][1], ini_positions[2][1]) test.gui_loop(0.1) # Panel 2 COLLAPSE wx.PostEvent(self.app.test_frame.panel_2, event) test.gui_loop(0.1) new_positions = [i.GetPosition() for i in self.foldpanelitems] self.assertEqual(new_positions, ini_positions) test.gui_loop(0.1)
def test_pyramidal_zoom(self): """ Draws a view with two streams, one pyramidal stream square completely green, and the other is a red square with a blue square in the center """ mpp = 0.00001 self.view.mpp.value = mpp self.assertEqual(mpp, self.view.mpp.value) self.view.show_crosshair.value = False self.canvas.fit_view_to_next_image = False # There is no viewport, so FoV is not updated automatically => display # everything possible self.view.fov_buffer.value = (1.0, 1.0) init_pos = (200.5 * mpp, 199.5 * mpp) FILENAME = u"test" + tiff.EXTENSIONS[0] # 1 row of 2 tiles w = 512 h = 250 md = { model.MD_PIXEL_SIZE: (mpp, mpp), model.MD_POS: init_pos, model.MD_DIMS: 'YXC' } arr = model.DataArray(numpy.zeros((h, w, 3), dtype="uint8")) # make it all green arr[:, :] = [0, 255, 0] data = model.DataArray(arr, metadata=md) # export tiff.export(FILENAME, data, pyramid=True) acd = tiff.open_data(FILENAME) stream1 = RGBStream("test", acd.content[0]) im2 = model.DataArray(numpy.zeros((201, 201, 3), dtype="uint8")) # red background im2[:, :] = [255, 0, 0] # Blue square at center im2[90:110, 90:110] = [0, 0, 255] im2.metadata[model.MD_PIXEL_SIZE] = (mpp, mpp) im2.metadata[model.MD_POS] = init_pos im2.metadata[model.MD_DIMS] = "YXC" stream2 = RGBStream("s2", im2) self.view.addStream(stream1) self.view.addStream(stream2) self.canvas.shift_view((-200.5, 199.5)) test.gui_loop(0.5) result_im = get_image_from_buffer(self.canvas) px2 = get_rgb(result_im, result_im.Width // 2, result_im.Height // 2) # center pixel, 2/3 green, 1/3 blue. The green image is the largest image self.assertEqual(px2, (0, 179, 76)) px2 = get_rgb(result_im, result_im.Width // 2 - 30, result_im.Height // 2 - 30) # background of the images, 2/3 green, 1/3 red self.assertEqual(px2, (76, 179, 0)) self.view.mpp.value = mpp shift = (63, 63) self.canvas.shift_view(shift) # merge the images ratio = 0.5 self.view.merge_ratio.value = ratio self.assertEqual(ratio, self.view.merge_ratio.value) test.gui_loop(0.5) result_im = get_image_from_buffer(self.canvas) px = get_rgb(result_im, result_im.Width // 2, result_im.Height // 2) # center pixel, now pointing to the background of the larger squares # half red, half green self.assertEqual(px, (128, 127, 0)) # copy the buffer into a nice image here result_im = get_image_from_buffer(self.canvas) px1 = get_rgb(result_im, result_im.Width // 2 + shift[0], result_im.Height // 2 + shift[1]) self.assertEqual(px1, (0, 127, 128)) # Ratio is at 0.5, so 255 becomes 128 px2 = get_rgb(result_im, result_im.Width // 2 + 200 + shift[0], result_im.Height // 2 - 200 + shift[1]) self.assertEqual(px2, (0, 0, 0)) self.assertAlmostEqual(1e-05, self.view.mpp.value) numpy.testing.assert_almost_equal([0.001375, 0.002625], self.view.view_pos.value) # Fit to content, and check it actually does self.canvas.fit_view_to_content(recenter=True) test.gui_loop(0.5) exp_mpp = (mpp * w) / self.canvas.ClientSize[0] self.assertAlmostEqual(exp_mpp, self.view.mpp.value) # after fitting, the center of the view should be the center of the image numpy.testing.assert_almost_equal(init_pos, self.view.view_pos.value) # remove green picture result_im = get_image_from_buffer(self.canvas) # result_im.SaveFile('tmp3.bmp', wx.BITMAP_TYPE_BMP) self.view.removeStream(stream1) test.gui_loop(0.5) # copy the buffer into a nice image here result_im = get_image_from_buffer(self.canvas) # result_im.SaveFile('tmp4.bmp', wx.BITMAP_TYPE_BMP) self.canvas.fit_view_to_content(recenter=True) # only .mpp changes, but the image keeps centered exp_mpp = (mpp * im2.shape[0]) / self.canvas.ClientSize[0] # TODO: check the precision self.assertAlmostEqual(exp_mpp, self.view.mpp.value) # ,6 numpy.testing.assert_almost_equal(init_pos, self.view.view_pos.value) test.gui_loop(0.5) result_im = get_image_from_buffer(self.canvas) # center of the translated red square with blue square on the center # pixel must be completely blue px2 = get_rgb(result_im, result_im.Width // 2 + shift[0], result_im.Height // 2 + shift[1]) # the center is red self.assertEqual(px2, (255, 0, 0)) self.canvas.fit_to_content()
def setUp(self): test.gui_loop() self.remove_all()
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")
def xtest_calc_img_buffer_rect(self): # Setting up test frame self.app.test_frame.SetSize((500, 500)) self.app.test_frame.Center() self.app.test_frame.Layout() test.gui_loop() test.gui_loop() tab = self.create_simple_tab_model() view = tab.focussedView.value # Changes in default values might affect other test, so we need to know self.assertEqual(view.mpp.value, 1e-6, "The default mpp value has changed!") cnvs = miccanvas.DblMicroscopeCanvas(self.panel) cnvs.fit_view_to_next_image = False # Create a even black background, so we can test pixel values cnvs.background_brush = wx.BRUSHSTYLE_SOLID self.add_control(cnvs, flags=wx.EXPAND, proportion=1) test.gui_loop(0.01) # Changes in default values might affect other test, so we need to know self.assertEqual(cnvs.scale, 1, "Default canvas scale has changed!") cnvs.setView(view, tab) # Setting the view, calls _onMPP with the view.mpp value # mpwu / mpp = scale => 1 (fixed, default) / view.mpp (1e-5) self.assertEqual(cnvs.scale, 1 / view.mpp.value) # Make sure the buffer is set at the right size expected_size = tuple(s + 2 * 512 for s in self.app.test_frame.ClientSize) self.assertEqual(cnvs._bmp_buffer_size, expected_size) ############ Create test image ############### img = generate_img_data(100, 100, 4) # 100 pixels is 1e-4 meters img.metadata[model.MD_PIXEL_SIZE] = (1e-6, 1e-6) img.metadata[model.MD_POS] = im_pos = (0, 0) img.metadata[model.MD_DIMS] = "YXC" im_scale = img.metadata[model.MD_PIXEL_SIZE][0] self.assertEqual(im_scale, img.metadata[model.MD_PIXEL_SIZE][0]) stream1 = RGBStream("s1", img) view.addStream(stream1) # Verify view mpp and canvas scale self.assertEqual(view.mpp.value, 1e-6, "Default mpp value has changed!") self.assertEqual(cnvs.scale, 1 / view.mpp.value, "Canvas scale should not have changed!") cnvs.update_drawing() # We're going to control the render size of the image using the # following meter per pixel values mpps = [1e-6, 1e-7, 1e-8] #, 1e-9, 1e-10] # They should set the canvas scales to the following values exp_scales = [1e6, 1e7, 1e8] #, 1e9, 1e10] exp_b_rect = [ (711, 697, 100.0, 100.0), # (261, 247, 1000.0, 1000.0), # (-4239, -4253, 10000.0, 10000.0), ] for mpp, scale, rect in zip(mpps, exp_scales, exp_b_rect): view.mpp.value = mpp self.assertAlmostEqual(scale, cnvs.scale) calc_rect = cnvs._calc_img_buffer_rect(img.shape[:2], im_scale, im_pos) for ev, v in zip(rect, calc_rect): self.assertAlmostEqual(ev, v) test.gui_loop(0.1) stream1 = RGBStream("stream_one", img) # Set the mpp again, because the on_size handler will recalculate it view.mpp._value = 1 # Dummy image shape = (200, 201, 4) rgb = numpy.empty(shape, dtype=numpy.uint8) rgb[...] = 255 darray = DataArray(rgb) logging.getLogger().setLevel(logging.DEBUG) buffer_rect = (0, 0) + cnvs._bmp_buffer_size logging.debug("Buffer size is %s", buffer_rect) im_scales = [0.00001, 0.33564, 0.9999, 1, 1.3458, 2, 3.0, 101.0, 333.5] im_centers = [(0.0, 0.0), (-1.5, 5.2), (340.0, -220.0), (-20.0, -1.0)] canvas.scale = 0.5 # Expected rectangles for the given image scales and canvas scale 0.5 rects = [ (611.9994975, 611.9995, 0.001005, 0.001), (595.13409, 595.218, 33.73182, 33.564), (561.755025, 562.005, 100.48995000000001, 99.99), (561.75, 562.0, 100.5, 100.0), (544.37355, 544.71, 135.2529, 134.58), (511.5, 512.0, 201.0, 200.0), (461.25, 462.0, 301.5, 300.0), (-4463.25, -4438.0, 10150.5, 10100.0), (-16146.375, -16063.0, 33516.75, 33350.0), ] for im_center in im_centers: logging.debug("Center: %s", im_center) for im_scale, rect in zip(im_scales, rects): logging.debug("Scale: %s", im_scale) b_rect = cnvs._calc_img_buffer_rect(darray.shape[:2], im_scale, im_center) for v in b_rect: self.assertIsInstance(v, float) rect = (rect[0] + im_center[0] * cnvs.scale, rect[1] + im_center[1] * cnvs.scale, rect[2], rect[3]) # logging.debug(b_rect) for b, r in zip(b_rect, rect): self.assertAlmostEqual(b, r) canvas.scale = 1.0 # Expected rectangle size for the given image scales and canvas scale 1 rects = [ (611.998995, 611.999, 0.00201, 0.002), (578.26818, 578.436, 67.46364, 67.128), (511.51005, 512.01, 200.97990000000001, 199.98), (511.5, 512.0, 201.0, 200.0), (476.7471, 477.41999999999996, 270.5058, 269.16), (411.0, 412.0, 402.0, 400.0), (310.5, 312.0, 603.0, 600.0), (-9538.5, -9488.0, 20301.0, 20200.0), (-32904.75, -32738.0, 67033.5, 66700.0), ] for im_center in im_centers: logging.debug("Center: %s", im_center) for im_scale, rect in zip(im_scales, rects): logging.debug("Scale: %s", im_scale) b_rect = cnvs._calc_img_buffer_rect(darray.shape[:2], im_scale, im_center) for v in b_rect: self.assertIsInstance(v, float) # logging.debug(b_rect) rect = (rect[0] + im_center[0] * cnvs.scale, rect[1] + im_center[1] * cnvs.scale, rect[2], rect[3]) # logging.debug(b_rect) for b, r in zip(b_rect, rect): self.assertAlmostEqual(b, r) canvas.scale = 2.3 # Expected rectangles for the given image scales and canvas scale 2.3 rects = [ (611.9976885, 611.9977, 0.0046229999999999995, 0.0046), (534.416814, 534.8028, 155.166372, 154.3944), (380.873115, 382.023, 462.25377, 459.95399999999995), (380.85, 382.0, 462.29999999999995, 459.99999999999994), (300.91833, 302.466, 622.16334, 619.068), (149.70000000000005, 152.00000000000006, 924.5999999999999, 919.9999999999999), (-81.44999999999993, -78.0, 1386.8999999999999, 1380.0), (-22734.149999999998, -22618.0, 46692.299999999996, 46460.0), (-76476.525, -76093.0, 154177.05, 153410.0), ] for im_center in im_centers: logging.debug("Center: %s", im_center) for im_scale, rect in zip(im_scales, rects): logging.debug("Scale: %s", im_scale) b_rect = cnvs._calc_img_buffer_rect(darray.shape[:2], im_scale, im_center) for v in b_rect: self.assertIsInstance(v, float) # logging.debug(b_rect) rect = (rect[0] + im_center[0] * cnvs.scale, rect[1] + im_center[1] * cnvs.scale, rect[2], rect[3]) # logging.debug(b_rect) for b, r in zip(b_rect, rect): self.assertAlmostEqual(b, r) logging.getLogger().setLevel(logging.ERROR)
def test_va_connector(self): va = model.FloatContinuous(0.3, (0.0, 1.0)) slider = UnitFloatSlider(self.panel, value=0.5, size=(-1, 18), unit="s", min_val=0.0, max_val=1.0) self.add_control(slider, flags=wx.EXPAND | wx.ALL) self.assertEqual(slider.GetValue(), 0.5) test.gui_loop(0.5) # Setting and getting the value directly should give the same value slider.SetValue(0.6) test.gui_loop(0.2) self.assertEqual(slider.GetValue(), 0.6) # After connecting the VA the control should have the same value as the VA con = widgets.VigilantAttributeConnector(va, slider, events=wx.EVT_SLIDER) test.gui_loop(0.2) self.assertEqual(slider.GetValue(), 0.3) # Chaning the VA changes the control value va.value = 0.8 test.gui_loop(0.2) self.assertEqual(slider.GetValue(), 0.8) # When pausing the VA, the control value should not change when the VA's does con.pause() va.value = 0.9 test.gui_loop(0.2) self.assertEqual(slider.GetValue(), 0.8) # Resuming the connection should update the control to the VA's current value con.resume() test.gui_loop(0.2) self.assertEqual(slider.GetValue(), 0.9) # When the control is manipulated, the VA's value is updated slider.SetValue(0.1) slider._send_slider_update_event() # Simulate a real user generated event test.gui_loop(0.2) self.assertEqual(va.value, 0.1) # When the connection is paused, changes in the control are not passed to the VA con.pause() slider.SetValue(0.2) slider._send_slider_update_event() # Simulate a real user generated event test.gui_loop(0.2) self.assertEqual(va.value, 0.1) # Resuming causes the value of the **VA** to be passed to the control con.resume() test.gui_loop(0.2) self.assertEqual(slider.GetValue(), 0.1)
def test_combo(self): self.add_control(wx.StaticText(self.panel, label="Test Odemis combo-box"), flags=wx.ALL | wx.EXPAND, border=2) choices = ["Apple", "Orange", "Pear", "Banana", "Cherry"] # ComboBox with fixed choices cfixed = combo.ComboBox(self.panel, wx.ID_ANY, size=(-1, 16), style=wx.NO_BORDER | wx.TE_PROCESS_ENTER | wx.CB_READONLY, choices=choices) cfixed.SetSelection(2) self.add_control(cfixed, flags=wx.ALL | wx.EXPAND, border=2, label="Fixed:") # ComboBox with free entry cfree = combo.ComboBox(self.panel, wx.ID_ANY, size=(-1, 16), style=wx.NO_BORDER | wx.TE_PROCESS_ENTER, choices=choices) cfree.SetSelection(0) self.add_control(cfree, flags=wx.ALL | wx.EXPAND, border=2, label="Free:") test.gui_loop(0.1) self.assertEqual(cfixed.GetValue(), choices[2]) print(cfree.GetBackgroundColour()) # Test to use the standard controls # Result: with wxPython4, it almost works, but it stills show a border, # which makes the control quite high. self.add_control(wx.StaticText(self.panel, label="Standard"), flags=wx.ALL | wx.EXPAND, border=2) choices = ["Apple", "Orange", "Pear", "Banana", "Cherry"] # ComboBox with fixed choices cfixed = wx.ComboBox(self.panel, wx.ID_ANY, size=(-1, 23), value=choices[2], choices=choices, style=wx.NO_BORDER | wx.CB_DROPDOWN | wx.TE_PROCESS_ENTER | wx.CB_READONLY) cfixed.SetForegroundColour(gui.FG_COLOUR_EDIT) cfixed.SetBackgroundColour(self.panel.GetBackgroundColour()) cfixed.SetSelection(2) self.add_control(cfixed, flags=wx.ALL | wx.EXPAND, border=2, label="Fixed:") # ComboBox with free entry cfree = cfixed = wx.ComboBox(self.panel, wx.ID_ANY, size=(-1, 23), value=choices[2], choices=choices, style=wx.NO_BORDER | wx.CB_DROPDOWN | wx.TE_PROCESS_ENTER) cfixed.SetForegroundColour(gui.FG_COLOUR_EDIT) cfixed.SetBackgroundColour(self.panel.GetBackgroundColour()) cfree.SetSelection(0) self.add_control(cfree, flags=wx.ALL | wx.EXPAND, border=2, label="Free:") test.gui_loop(0.1)
def test_pf_connector(self): """ Test ProgressiveFutureConnector """ # Add a gauge (progress bar) and label for testing gauge = wx.Gauge(self.panel) stxt = wx.StaticText(self.panel) self.add_control(gauge, flags=wx.EXPAND | wx.ALL) self.add_control(stxt, flags=wx.EXPAND | wx.ALL) test.gui_loop(0.2) # Create the ProgressiveFuture now = time.time() pf = model.ProgressiveFuture(now, now + 60) # one min # future.task_canceller = self.cancel_task # Create the connector pfc = widgets.ProgressiveFutureConnector(pf, bar=gauge, label=stxt) test.gui_loop(0.3) # need to wait at least 0.25 s # Check ratio at beginning r1 = gauge.Value / gauge.Range self.assertLessEqual(r1, 0.1) self.assertGreater(len(stxt.LabelText), 6) # wait 2 s and see if the progress increased test.gui_loop(2) r2 = gauge.Value / gauge.Range self.assertGreater(r2, r1) # Make it look a lot longer => should update backwards pf.set_progress(end=now + 120) test.gui_loop(0.3) # need to wait at least 0.25 s r3 = gauge.Value / gauge.Range self.assertLess(r3, r2) # wait 2 s and see if the progress increased test.gui_loop(2) r4 = gauge.Value / gauge.Range self.assertGreater(r4, r3) # Make it look a little longer => should not update pf.set_progress(end=now + 121) test.gui_loop(0.3) # need to wait at least 0.25 s r5 = gauge.Value / gauge.Range self.assertEqual(r5, r4)
def tearDown(self): test.gui_loop(0.1) super(ComboTestCase, self).tearDown()
def test_pyramidal_one_tile(self): """ Draws a view with two streams, one pyramidal stream square completely green, and the other is a red square with a blue square in the center """ mpp = 0.00001 self.view.mpp.value = mpp self.assertEqual(mpp, self.view.mpp.value) self.view.show_crosshair.value = False self.canvas.fit_view_to_next_image = False FILENAME = u"test" + tiff.EXTENSIONS[0] w = 201 h = 201 md = { model.MD_PIXEL_SIZE: (mpp, mpp), model.MD_POS: (200.5 * mpp, 199.5 * mpp), model.MD_DIMS: 'YXC' } arr = model.DataArray(numpy.zeros((h, w, 3), dtype="uint8")) # make it all green arr[:, :] = [0, 255, 0] data = model.DataArray(arr, metadata=md) # export tiff.export(FILENAME, data, pyramid=True) acd = tiff.open_data(FILENAME) stream1 = RGBStream("test", acd.content[0]) im2 = model.DataArray(numpy.zeros((201, 201, 3), dtype="uint8")) # red background im2[:, :] = [255, 0, 0] # Blue square at center im2[90:110, 90:110] = [0, 0, 255] # 200, 200 => outside of the im1 # (+0.5, -0.5) to make it really in the center of the pixel im2.metadata[model.MD_PIXEL_SIZE] = (mpp, mpp) im2.metadata[model.MD_POS] = (200.5 * mpp, 199.5 * mpp) im2.metadata[model.MD_DIMS] = "YXC" stream2 = RGBStream("s2", im2) self.view.addStream(stream1) self.view.addStream(stream2) test.gui_loop(0.5) self.canvas.shift_view((-200.5, 199.5)) test.gui_loop(0.5) result_im = get_image_from_buffer(self.canvas) px2 = get_rgb(result_im, result_im.Width // 2, result_im.Height // 2) # center pixel, 1/3 green, 2/3 blue self.assertEqual(px2, (0, 76, 179)) px2 = get_rgb(result_im, result_im.Width // 2 - 30, result_im.Height // 2 - 30) # background of the images, 1/3 green, 2/3 red self.assertEqual(px2, (179, 76, 0)) self.view.mpp.value = mpp shift = (63, 63) self.canvas.shift_view(shift) # merge the images ratio = 0.5 self.view.merge_ratio.value = ratio # self.assertEqual(ratio, self.view.merge_ratio.value) # it's supposed to update in less than 0.5s test.gui_loop(0.5) result_im = get_image_from_buffer(self.canvas) px = get_rgb(result_im, result_im.Width // 2, result_im.Height // 2) # center pixel, now pointing to the background of the larger squares # half red, half green self.assertEqual(px, (127, 128, 0)) # copy the buffer into a nice image here result_im = get_image_from_buffer(self.canvas) px1 = get_rgb(result_im, result_im.Width // 2 + shift[0], result_im.Height // 2 + shift[1]) self.assertEqual(px1, (0, 128, 127)) # Ratio is at 0.5, so 255 becomes 128 px2 = get_rgb(result_im, result_im.Width // 2 + 200 + shift[0], result_im.Height // 2 - 200 + shift[1]) self.assertEqual(px2, (0, 0, 0)) # remove first picture self.view.removeStream(stream1) test.gui_loop(0.5) result_im = get_image_from_buffer(self.canvas) # center of the translated red square with blue square on the center # pixel must be completely blue px2 = get_rgb(result_im, result_im.Width // 2 + shift[0], result_im.Height // 2 + shift[1]) self.assertEqual(px2, (0, 0, 255))
def test_plot_viewport(self): # vwp = viewport.PlotViewport(self.panel) vwp = viewport.PointSpectrumViewport(self.panel) vwp.canvas.SetBackgroundColour("#333") self.add_control(vwp, wx.EXPAND, proportion=1) vwp.canvas.SetForegroundColour("#27C4CC") for mode in MODES: vwp.canvas.set_plot_mode(mode) """ # Note: With the new version of the plotting canvas, which can # be navigated, all ranges will be accepted, and no ValueError is raised for plot in BAD_RANGED_PLOTS: with self.assertRaises(ValueError): logging.debug("Testing range X = %s, range Y = %s", plot[0], plot[1]) vwp.canvas.set_1d_data(plot[2], plot[3], range_x=plot[0], range_y=plot[1]) vwp.canvas.draw() test.gui_loop(0.3) """ vwp.Refresh() for plot in BAD_PLOTS: with self.assertRaises(ValueError): vwp.canvas.set_1d_data(plot[0], plot[1]) vwp.canvas.draw() test.gui_loop(0.1) vwp.Refresh() for plot in INCORRECT_PLOTS: vwp.canvas.set_1d_data(plot[0], plot[1]) vwp.bottom_legend.range = (min(plot[0]), max(plot[0])) vwp.left_legend.range = (min(plot[1]), max(plot[1])) test.gui_loop(0.1) vwp.Refresh() for plot in PLOTS: vwp.canvas.set_1d_data(plot[0], plot[1]) vwp.bottom_legend.range = (min(plot[0]), max(plot[0])) vwp.left_legend.range = (min(plot[1]), max(plot[1])) test.gui_loop(0.1) vwp.Refresh() for plot in RANGED_PLOTS[:-1]: vwp.canvas.set_1d_data(plot[2], plot[3], range_x=plot[0], range_y=plot[1]) vwp.bottom_legend.range = (min(plot[0]), max(plot[0])) vwp.left_legend.range = (min(plot[1]), max(plot[1])) test.gui_loop(0.1) # Test setting ranges for plot in RANGED_PLOTS[:-1]: range_x = plot[0] range_y = plot[1] # data width and height w = abs(range_x[1] - range_x[0]) h = abs(range_y[1] - range_y[0]) vwp.canvas.set_1d_data(plot[2], plot[3], range_x=range_x, range_y=range_y) vwp.bottom_legend.range = (min(plot[0]), max(plot[0])) vwp.left_legend.range = (min(plot[1]), max(plot[1])) # Test setting bad ranges test_xrange = (range_x[1], range_x[0]) with self.assertRaises(ValueError): vwp.hrange.value = test_xrange test_yrange = (range_y[1], range_y[0]) with self.assertRaises(ValueError): vwp.vrange.value = test_yrange # Test setting ranges that fall within the data ranges test_xrange = (range_x[0] + w * 0.2, range_x[1] - w * 0.2) vwp.hrange.value = test_xrange self.assertEqual(vwp.hrange.value, test_xrange) test_yrange = (range_y[0] + h * 0.2, range_y[1] - h * 0.2) vwp.vrange.value = test_yrange self.assertEqual(vwp.vrange.value, test_yrange) test.gui_loop(0.1) vwp.Refresh()
def test_unit_float_txt_ctrl(self): # Create a field with an 'm' unit mctrl = UnitFloatCtrl(self.panel, value=123456789, unit='m') self.add_control(mctrl, label=mctrl.__class__.__name__, flags=wx.EXPAND | wx.ALL) # Test the initial value test.gui_loop(0.1) self.assertEqual(mctrl.GetValue(), 123456789) self.assertEqual(mctrl.get_value_str(), u"123.456789 Mm") # Create simulator and focus the field sim = wx.UIActionSimulator() # Focusing the field will select all the text in it mctrl.SetFocus() test.gui_loop(0.1) # Set the value to 0.001 Mm for c in "0.001\r": sim.Char(ord(c)) test.gui_loop(0.02) self.assertEqual(mctrl.GetValue(), 1000) self.assertEqual(mctrl.get_value_str(), u"1 km") # Move the caret to the start of the field mctrl.SetSelection(0, 0) for c in "00000\r": sim.Char(ord(c)) test.gui_loop(0.02) self.assertEqual(mctrl.GetValue(), 1000) self.assertEqual(mctrl.get_value_str(), u"1 km") # Move the caret to the start of the field mctrl.SetSelection(0, 0) # Create illegal number for c in "e\r": sim.Char(ord(c)) test.gui_loop(0.1) self.assertEqual(mctrl.GetValue(), None) self.assertEqual(mctrl.get_value_str(), u"") # Add 2nd control wctrl = UnitFloatCtrl(self.panel, value=3.44, unit='W') self.add_control(wctrl, label=wctrl.__class__.__name__, flags=wx.EXPAND | wx.ALL) wctrl.SetFocus() test.gui_loop(0.1) wctrl.SetSelection(0, 20) for c in "44e-9W\r": sim.Char(ord(c)) test.gui_loop(0.02) self.assertEqual(wctrl.GetValue(), 44e-9) self.assertEqual(wctrl.get_value_str(), u"44 nW") test.gui_loop(0.1)