def test_aligned_stream(self): """ Test the AlignedSEMStream """ # Use fake ccd in order to have just one spot ccd = mock.FakeCCD(self.fake_img) # first try using the metadata correction st = stream.AlignedSEMStream("sem-md", self.sed, self.sed.data, self.ebeam, ccd, self.stage, self.focus, shiftebeam=stream.MTD_MD_UPD) # we don't really care about the SEM image, so the faster the better self.ebeam.dwellTime.value = self.ebeam.dwellTime.range[0] # start one image acquisition (so it should do the calibration) f = acq.acquire([st]) received, _ = f.result() self.assertTrue(received, "No image received after 30 s") # Check the correction metadata is there md = self.sed.getMetadata() self.assertIn(model.MD_POS_COR, md) # Check the position of the image is correct pos_cor = md[model.MD_POS_COR] pos_dict = self.stage.position.value pos = (pos_dict["x"], pos_dict["y"]) exp_pos = tuple(p - c for p, c in zip(pos, pos_cor)) imd = received[0].metadata self.assertEqual(exp_pos, imd[model.MD_POS]) # Check the calibration doesn't happen again on a second acquisition bad_cor = (-1, -1) # stupid impossible value self.sed.updateMetadata({model.MD_POS_COR: bad_cor}) f = acq.acquire([st]) received, _ = f.result() self.assertTrue(received, "No image received after 10 s") # if calibration has happened (=bad), it has changed the metadata md = self.sed.getMetadata() self.assertEqual(bad_cor, md[model.MD_POS_COR], "metadata has been updated while it shouldn't have") # Check calibration happens again after a stage move f = self.stage.moveRel({"x": 100e-6}) f.result() # make sure the move is over time.sleep(0.1) # make sure the stream had time to detect position has changed f = acq.acquire([st]) received, _ = f.result() self.assertTrue(received, "No image received after 30 s") # if calibration has happened (=good), it has changed the metadata md = self.sed.getMetadata() self.assertNotEqual(bad_cor, md[model.MD_POS_COR], "metadata hasn't been updated while it should have") ccd.terminate()
def test_light_already_on(self): """ Test when chamber light is already on """ bl = self.light bl.power.value[0] = 10 # bright image - light is already on ccd = mock.FakeCCD(self.img_ccd_lon) f = turnOnLight(bl, ccd) time.sleep(1) self.assertTrue(f.done()) f.result()
def test_slit_cancel(self): """ Test cancelling does cancel (relatively quickly) """ bl = self.light ccd = mock.FakeCCD(self.img_ccd_loff) f = turnOnLight(bl, ccd) time.sleep(1) cancelled = f.cancel() self.assertTrue(cancelled) self.assertTrue(f.cancelled()) with self.assertRaises(CancelledError): f.result(5)
def test_slit_off_on_ccd(self): """ Test from dark slit image to bright slit image (gray) """ bl = self.light ccd = mock.FakeCCD(self.img_ccd_loff) f = turnOnLight(bl, ccd) time.sleep(1) self.assertFalse(f.done()) # simulate light turning on ccd.fake_img = self.img_ccd_lon time.sleep(1) self.assertTrue(f.done()) self.assertFalse(f.cancelled()) f.result()
def test_slit_off_on_spccd(self): """ Test from dark slit image to bright slit image (gray) """ bl = self.light ccd = mock.FakeCCD(self.img_spccd_loff) f = turnOnLight(bl, ccd) time.sleep(1) # > exposureTime (=0.1 s) self.assertFalse(f.done()) logging.debug("changing image") # simulate light turning on ccd.fake_img = self.img_spccd_lon time.sleep(1) self.assertTrue(f.done()) f.result()
def test_slit_timeout(self): """ Test Timeout """ bl = self.light ccd = mock.FakeCCD(self.img_ccd_loff) f = turnOnLight(bl, ccd) # no change in the image time.sleep(1) self.assertFalse(f.done()) with self.assertRaises(futures.TimeoutError): f.result(timeout=5) self.assertFalse(f.done()) self.assertFalse(f.cancelled()) f.cancel() self.assertTrue(f.done()) self.assertTrue(f.cancelled())
def test_1pix_off_on(self): """ Test from dark image to bright image of 1 pixel """ bl = self.light img_dark = numpy.ones((1, 1), dtype=numpy.uint16) # type: ndarray da_dark = model.DataArray(img_dark) ccd = mock.FakeCCD(da_dark) f = turnOnLight(bl, ccd) time.sleep(1) self.assertFalse(f.done()) # simulate light turning on img_bright = numpy.array([10], dtype=numpy.uint16) br_bright = model.DataArray(img_bright) ccd.fake_img = br_bright time.sleep(1) self.assertTrue(f.done()) f.result()
def test_no_detection_of_change(self): """ Test when we get a new dark image with a bright spot but no change is detected """ bl = self.light ccd = mock.FakeCCD(self.img_ccd_loff) f = turnOnLight(bl, ccd) time.sleep(1) self.assertFalse(f.done()) spoted_img = self.img_ccd_loff spoted_img[42:47, 93] = 65000 with self.assertRaises(futures.TimeoutError): f.result(timeout=5) self.assertFalse(f.done()) self.assertFalse(f.cancelled()) f.cancel() self.assertTrue(f.done()) self.assertTrue(f.cancelled())