def setUp(self): # Input self.data = hdf5.read_data(os.path.join(DATA_DIR, "example_input.h5")) C, T, Z, Y, X = self.data[0].shape self.data[0].shape = Y, X self.small_data = self.data[0][350:400, 325:375] # Input drifted by known value self.data_drifted = hdf5.read_data( os.path.join(DATA_DIR, "example_drifted.h5")) C, T, Z, Y, X = self.data_drifted[0].shape self.data_drifted[0].shape = Y, X # Input drifted by random value z = 1j # imaginary unit self.deltar = numpy.random.uniform(-100, 100) self.deltac = numpy.random.uniform(-100, 100) nr, nc = self.data[0].shape array_nr = numpy.arange(-numpy.fix(nr / 2), numpy.ceil(nr / 2)) array_nc = numpy.arange(-numpy.fix(nc / 2), numpy.ceil(nc / 2)) Nr = fft.ifftshift(array_nr) Nc = fft.ifftshift(array_nc) [Nc, Nr] = numpy.meshgrid(Nc, Nr) self.data_random_drifted = fft.ifft2( fft.fft2(self.data[0]) * numpy.power( math.e, z * 2 * math.pi * (self.deltar * Nr / nr + self.deltac * Nc / nc))) # Noisy inputs noise = random.normal(0, 3000, self.data[0].size) noise_array = noise.reshape(self.data[0].shape[0], self.data[0].shape[1]) self.data_noisy = self.data[0] + noise_array self.data_drifted_noisy = self.data_drifted[0] + noise_array self.data_random_drifted_noisy = self.data_random_drifted + noise_array # Small input drifted by random value self.small_deltar = numpy.random.uniform(-10, 10) self.small_deltac = numpy.random.uniform(-10, 10) nr, nc = self.small_data.shape array_nr = numpy.arange(-numpy.fix(nr / 2), numpy.ceil(nr / 2)) array_nc = numpy.arange(-numpy.fix(nc / 2), numpy.ceil(nc / 2)) Nr = fft.ifftshift(array_nr) Nc = fft.ifftshift(array_nc) [Nc, Nr] = numpy.meshgrid(Nc, Nr) self.small_data_random_drifted = fft.ifft2( fft.fft2(self.small_data) * numpy.power( math.e, z * 2 * math.pi * (self.small_deltar * Nr / nr + self.small_deltac * Nc / nc))) # Small noisy inputs small_noise = random.normal(0, 3000, self.small_data.size) small_noise_array = small_noise.reshape(self.small_data.shape[0], self.small_data.shape[1]) self.small_data_noisy = self.small_data + small_noise_array self.small_data_random_drifted_noisy = self.small_data_random_drifted + small_noise_array
def setUp(self): data = hdf5.read_data("ar-example-input.h5") self.data = data # test also for different polar parameters data_mini = hdf5.read_data("ar-example-minimirror-input.h5") self.data_mini = data_mini white_data_512 = model.DataArray(numpy.empty((512, 512), dtype="uint16")) white_data_512[...] = 255 white_mag_512 = 0.4917 white_spxs_512 = (13e-6, 13e-6) white_binning_512 = (2, 2) white_data_512.metadata[model.MD_AR_POLE] = (283, 259) white_data_512.metadata[model.MD_AR_XMAX] = 13.25e-3 white_data_512.metadata[model.MD_AR_HOLE_DIAMETER] = 0.6e-3 white_data_512.metadata[model.MD_AR_FOCUS_DISTANCE] = 0.5e-3 white_data_512.metadata[model.MD_AR_PARABOLA_F] = 2.5e-3 white_pxs_512 = (white_spxs_512[0] * white_binning_512[0] / white_mag_512, white_spxs_512[1] * white_binning_512[1] / white_mag_512) white_data_512.metadata[model.MD_PIXEL_SIZE] = white_pxs_512 self.white_data_512 = white_data_512 white_data_1024 = model.DataArray(numpy.empty((1024, 1024), dtype="uint16")) white_data_1024[...] = 255 white_mag_1024 = 0.4917 white_spxs_1024 = (13e-6, 13e-6) white_binning_1024 = (2, 2) white_data_1024.metadata[model.MD_AR_POLE] = (283, 259) white_data_1024.metadata[model.MD_AR_XMAX] = 13.25e-3 white_data_1024.metadata[model.MD_AR_HOLE_DIAMETER] = 0.6e-3 white_data_1024.metadata[model.MD_AR_FOCUS_DISTANCE] = 0.5e-3 white_data_1024.metadata[model.MD_AR_PARABOLA_F] = 2.5e-3 white_pxs_1024 = (white_spxs_1024[0] * white_binning_1024[0] / white_mag_1024, white_spxs_1024[1] * white_binning_1024[1] / white_mag_1024) white_data_1024.metadata[model.MD_PIXEL_SIZE] = white_pxs_1024 self.white_data_1024 = white_data_1024 white_data_2500 = model.DataArray(numpy.empty((2560, 2160), dtype="uint16")) white_data_2500[...] = 255 white_mag_2500 = 0.4917 white_spxs_2500 = (13e-6, 13e-6) white_binning_2500 = (2, 2) white_data_2500.metadata[model.MD_AR_POLE] = (283, 259) white_data_2500.metadata[model.MD_AR_XMAX] = 13.25e-3 white_data_2500.metadata[model.MD_AR_HOLE_DIAMETER] = 0.6e-3 white_data_2500.metadata[model.MD_AR_FOCUS_DISTANCE] = 0.5e-3 white_data_2500.metadata[model.MD_AR_PARABOLA_F] = 2.5e-3 # These values makes the computation much harder: # white_mag_2500 = 0.53 # white_spxs_2500 = (6.5e-6, 6.5e-6) # white_binning_2500 = (1, 1) # white_data_2500.metadata[model.MD_AR_POLE] = (1480, 1129) white_pxs_2500 = (white_spxs_2500[0] * white_binning_2500[0] / white_mag_2500, white_spxs_2500[1] * white_binning_2500[1] / white_mag_2500) white_data_2500.metadata[model.MD_PIXEL_SIZE] = white_pxs_2500 self.white_data_2500 = white_data_2500
def setUp(self): # Input self.data = hdf5.read_data("example_input.h5") C, T, Z, Y, X = self.data[0].shape self.data[0].shape = Y, X self.small_data = self.data[0][350:400, 325:375] # Input drifted by known value self.data_drifted = hdf5.read_data("example_drifted.h5") C, T, Z, Y, X = self.data_drifted[0].shape self.data_drifted[0].shape = Y, X # Input drifted by random value z = 1j # imaginary unit self.deltar = numpy.random.uniform(-100, 100) self.deltac = numpy.random.uniform(-100, 100) nr, nc = self.data[0].shape array_nr = numpy.arange(-numpy.fix(nr / 2), numpy.ceil(nr / 2)) array_nc = numpy.arange(-numpy.fix(nc / 2), numpy.ceil(nc / 2)) Nr = fft.ifftshift(array_nr) Nc = fft.ifftshift(array_nc) [Nc, Nr] = numpy.meshgrid(Nc, Nr) self.data_random_drifted = fft.ifft2(fft.fft2(self.data[0]) * numpy.power(math.e, z * 2 * math.pi * (self.deltar * Nr / nr + self.deltac * Nc / nc))) # Noisy inputs noise = random.normal(0, 3000, self.data[0].size) noise_array = noise.reshape(self.data[0].shape[0], self.data[0].shape[1]) self.data_noisy = self.data[0] + noise_array self.data_drifted_noisy = self.data_drifted[0] + noise_array self.data_random_drifted_noisy = self.data_random_drifted + noise_array # Small input drifted by random value self.small_deltar = numpy.random.uniform(-10, 10) self.small_deltac = numpy.random.uniform(-10, 10) nr, nc = self.small_data.shape array_nr = numpy.arange(-numpy.fix(nr / 2), numpy.ceil(nr / 2)) array_nc = numpy.arange(-numpy.fix(nc / 2), numpy.ceil(nc / 2)) Nr = fft.ifftshift(array_nr) Nc = fft.ifftshift(array_nc) [Nc, Nr] = numpy.meshgrid(Nc, Nr) self.small_data_random_drifted = fft.ifft2(fft.fft2(self.small_data) * numpy.power(math.e, z * 2 * math.pi * (self.small_deltar * Nr / nr + self.small_deltac * Nc / nc))) # Small noisy inputs small_noise = random.normal(0, 3000, self.small_data.size) small_noise_array = small_noise.reshape(self.small_data.shape[0], self.small_data.shape[1]) self.small_data_noisy = self.small_data + small_noise_array self.small_data_random_drifted_noisy = self.small_data_random_drifted + small_noise_array
def test_find_center_big(self): """ Test FindCenterCoordinates on large data """ # Note: it's not very clear why, but the center is not exactly the same # as with the original data. expected_coordinates = [(-0.0003367114224783442, -0.022941682748052378), (0.42179351215760619, -0.25668360673638801), (0.054153514028894206, -0.046475569488448026), (0.15117193581594143, 0.20813363301021551), (0.1963834856403108, -0.18329597166583256), (0.23159684275306583, 1.3670166271550004), (-1.3363782613242998, 0.20192181693837058), (-0.14978764151902624, 0.66067572281822606), (-0.058984235874285897, 0.13071737132569164), (0.021009283646695891, -0.007037802630523865)] for i in range(10): data = hdf5.read_data(os.path.join(TEST_IMAGE_PATH, "image" + str(i + 1) + ".h5"))[0] data.shape = data.shape[-2:] Y, X = data.shape databig = numpy.zeros((200 + Y, 200 + X), data.dtype) databig += numpy.min(data) # We put it right at the center, so shouldn't change expected coordinates databig[100:100 + Y:, 100: 100 + X] = data spot_coordinates = spot.FindCenterCoordinates(databig) numpy.testing.assert_almost_equal(spot_coordinates, expected_coordinates[i], 3)
def test_find_center(self): """ Test FindCenterCoordinates """ data = [] subimages = [] for i in xrange(10): data.append(hdf5.read_data("image" + str(i + 1) + ".h5")) C, T, Z, Y, X = data[i][0].shape data[i][0].shape = Y, X subimages.append(data[i][0]) spot_coordinates = coordinates.FindCenterCoordinates(subimages) expected_coordinates = [(-0.00019439548586790034, -0.023174120210179554), (0.41813787193469681, -0.77556146879261101), (0.05418032832973009, -0.046573726263258203), (0.15117173005078957, 0.20813259555303279), (0.15372338817998937, -0.071307409462406962), (0.22214464176322843, 1.5448851668913044), (-1.3567379189595801, 0.20634334863259929), (-0.068717256379618827, 0.76902400758882417), (-0.064496044288789064, 0.14000630665134439), (0.020941736978718473, -0.0071056828496776324)] numpy.testing.assert_almost_equal(spot_coordinates, expected_coordinates, 3)
def test_find_center_big(self): """ Test FindCenterCoordinates on large data """ # Note: it's not very clear why, but the center is not exactly the same # as with the original data. expected_coordinates = [(-0.0003367114224783442, -0.022941682748052378), (0.42179351215760619, -0.25668360673638801), (0.054153514028894206, -0.046475569488448026), (0.15117193581594143, 0.20813363301021551), (0.1963834856403108, -0.18329597166583256), (0.23159684275306583, 1.3670166271550004), (-1.3363782613242998, 0.20192181693837058), (-0.14978764151902624, 0.66067572281822606), (-0.058984235874285897, 0.13071737132569164), (0.021009283646695891, -0.007037802630523865)] for i in range(10): data = hdf5.read_data( os.path.join(TEST_IMAGE_PATH, "image" + str(i + 1) + ".h5"))[0] data.shape = data.shape[-2:] Y, X = data.shape databig = numpy.zeros((200 + Y, 200 + X), data.dtype) databig += numpy.min(data) # We put it right at the center, so shouldn't change expected coordinates databig[100:100 + Y:, 100:100 + X] = data spot_coordinates = spot.FindCenterCoordinates(databig) numpy.testing.assert_almost_equal(spot_coordinates, expected_coordinates[i], 3)
def setUp(self): self.data = hdf5.read_data(os.path.dirname(__file__) + "/grid_10x10.h5") C, T, Z, Y, X = self.data[0].shape self.data[0].shape = Y, X self.fake_img = self.data[0] if self.backend_was_running: self.skipTest("Running backend found")
def setUp(self): self.data = hdf5.read_data(os.path.join(TEST_IMAGE_PATH, "one_spot.h5")) C, T, Z, Y, X = self.data[0].shape self.data[0].shape = Y, X self.fake_img = self.data[0] if self.backend_was_running: self.skipTest("Running backend found")
def setUp(self): self.data = hdf5.read_data("grid_10x10.h5") C, T, Z, Y, X = self.data[0].shape self.data[0].shape = Y, X self.fake_img = self.data[0] if self.backend_was_running: self.skipTest("Running backend found")
def setUp(self): data = hdf5.read_data("ar-example-input.h5") mag = 0.4917 spxs = (13e-6, 13e-6) binning = (4, 4) # data[0].metadata[model.MD_BASELINE] = 820 data[0].metadata[model.MD_BINNING] = binning data[0].metadata[model.MD_SENSOR_PIXEL_SIZE] = spxs data[0].metadata[model.MD_LENS_MAG] = mag data[0].metadata[model.MD_AR_POLE] = (141, 255 - 139.449038462) mag = data[0].metadata[model.MD_LENS_MAG] pxs = (spxs[0] * binning[0] / mag, spxs[1] * binning[1] / mag) data[0].metadata[model.MD_PIXEL_SIZE] = pxs self.data = data white_data_512 = model.DataArray( numpy.empty((512, 512), dtype="uint16")) white_data_512[...] = 255 white_mag_512 = 0.4917 white_spxs_512 = (13e-6, 13e-6) white_binning_512 = (2, 2) white_data_512.metadata[model.MD_AR_POLE] = (283, 259) white_pxs_512 = (white_spxs_512[0] * white_binning_512[0] / white_mag_512, white_spxs_512[1] * white_binning_512[1] / white_mag_512) white_data_512.metadata[model.MD_PIXEL_SIZE] = white_pxs_512 self.white_data_512 = white_data_512 white_data_1024 = model.DataArray( numpy.empty((1024, 1024), dtype="uint16")) white_data_1024[...] = 255 white_mag_1024 = 0.4917 white_spxs_1024 = (13e-6, 13e-6) white_binning_1024 = (2, 2) white_data_1024.metadata[model.MD_AR_POLE] = (283, 259) white_pxs_1024 = (white_spxs_1024[0] * white_binning_1024[0] / white_mag_1024, white_spxs_1024[1] * white_binning_1024[1] / white_mag_1024) white_data_1024.metadata[model.MD_PIXEL_SIZE] = white_pxs_1024 self.white_data_1024 = white_data_1024 white_data_2500 = model.DataArray( numpy.empty((2560, 2160), dtype="uint16")) white_data_2500[...] = 255 white_mag_2500 = 0.4917 white_spxs_2500 = (13e-6, 13e-6) white_binning_2500 = (2, 2) white_data_2500.metadata[model.MD_AR_POLE] = (283, 259) # These values makes the computation much harder: # white_mag_2500 = 0.53 # white_spxs_2500 = (6.5e-6, 6.5e-6) # white_binning_2500 = (1, 1) # white_data_2500.metadata[model.MD_AR_POLE] = (1480, 1129) white_pxs_2500 = (white_spxs_2500[0] * white_binning_2500[0] / white_mag_2500, white_spxs_2500[1] * white_binning_2500[1] / white_mag_2500) white_data_2500.metadata[model.MD_PIXEL_SIZE] = white_pxs_2500 self.white_data_2500 = white_data_2500
def setUp(self): data = hdf5.read_data(os.path.join(PATH, "spectrum_fitting.h5"))[1] data = numpy.squeeze(data) self.data = data self.wl_in_meters = numpy.linspace(470e-9, 1030e-9, 167) max_bw = data.shape[0] // 2 min_bw = (max_bw - data.shape[0]) + 1 self.wl_in_pixels = list(range(min_bw, max_bw + 1)) self._peak_fitter = peak.PeakFitter()
def test_no_hole(self): """ Test FindCircleCenter raises exception """ data = hdf5.read_data(os.path.join(TEST_IMAGE_PATH, "blank_image.h5")) C, T, Z, Y, X = data[0].shape data[0].shape = Y, X self.assertRaises(LookupError, delphi.FindCircleCenter, data[0], 0.02032, 3)
def setUp(self): if self.backend_was_running: self.skipTest("Running backend found") # image for FakeCCD self.data = hdf5.read_data("../align/test/one_spot.h5") C, T, Z, Y, X = self.data[0].shape self.data[0].shape = Y, X self.fake_img = self.data[0]
def test_no_hole(self): """ Test FindCircleCenter raises exception """ data = hdf5.read_data("blank_image.h5") C, T, Z, Y, X = data[0].shape data[0].shape = Y, X self.assertRaises(IOError, delphi.FindCircleCenter, data[0], 0.02032, 3)
def test_find_lens_center(self): """ Test FindCircleCenter for lenses """ data = hdf5.read_data("navcam-calib2.h5") Z, Y, X = data[0].shape lens_coordinates = delphi.FindCircleCenter(data[0][0], delphi.LENS_RADIUS, 6) expected_coordinates = (450.5, 445.5) numpy.testing.assert_almost_equal(lens_coordinates, expected_coordinates)
def test_precomputed(self): data = self.data C, T, Z, Y, X = data[0].shape data[0].shape = Y, X result = polar.AngleResolved2Polar(data[0], 201) desired_output = hdf5.read_data("desired201x201image.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_precomputed(self): data = self.data C, T, Z, Y, X = data[0].shape data[0].shape = Y, X result = angleres.AngleResolved2Polar(data[0], 201) desired_output = hdf5.read_data("desired201x201image.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_precomputed_mini(self): data_mini = self.data_mini C, T, Z, Y, X = data_mini[0].shape data_mini[0].shape = Y, X result = angleres.AngleResolved2Polar(data_mini[0], 201) desired_output = hdf5.read_data("desired201x201imagemini.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], atol=1e-07)
def test_find_hole_center(self): """ Test FindCircleCenter for holes """ data = hdf5.read_data("sem_hole.h5") C, T, Z, Y, X = data[0].shape data[0].shape = Y, X hole_coordinates = delphi.FindCircleCenter(data[0], 0.02032, 3) expected_coordinates = (390.5, 258.5) numpy.testing.assert_almost_equal(hole_coordinates, expected_coordinates)
def test_precomputed(self): # These are example data (computer generated) data = hdf5.read_data("image1.h5")[0] data.shape = data.shape[-2:] si = spot.SpotIntensity(data) # guessed background self.assertAlmostEqual(si, 0.713582339927869) # Same thing, with some static background background = numpy.zeros(data.shape, dtype=data.dtype) background += 50 si = spot.SpotIntensity(data, background) self.assertAlmostEqual(si, 0.8621370728816907)
def test_find_lens_center(self): """ Test FindRingCenter for lenses """ data = hdf5.read_data(os.path.join(TEST_IMAGE_PATH, "navcam-calib2.h5")) imgs = img.RGB2Greyscale(img.ensureYXC(data[0])) # lens_coordinates = delphi.FindCircleCenter(data[0][0], delphi.LENS_RADIUS, 5) # expected_coordinates = (-5.9703947e-05, 1.5257675e-04) # (451.5, 445.5) px lens_coordinates = delphi.FindRingCenter(imgs) expected_coordinates = (-1.6584835e-05, 1.3084411e-04) # 454.75, 446.1) px numpy.testing.assert_almost_equal(lens_coordinates, expected_coordinates)
def test_1024x1024(self): """ Test for 1024x1024 white image input """ white_data_1024 = self.white_data_1024 result = angleres.AngleResolved2Polar(white_data_1024, 201) desired_output = hdf5.read_data("desired_white_1024.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_100x100(self): data = self.data C, T, Z, Y, X = data[0].shape data[0].shape = Y, X result = angleres.AngleResolved2Polar(data[0], 101) desired_output = hdf5.read_data("desired100x100image.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def testReadMDOutWlBands(self): """ Checks that we hand MD_OUT_WL if it contains multiple bands. OME supports only one value, so it's ok to discard some info. """ metadata = [ { model.MD_SW_VERSION: "1.0-test", model.MD_HW_NAME: "fake hw", model.MD_DESCRIPTION: "blue dye", model.MD_ACQ_DATE: time.time() + 1, model.MD_BPP: 12, model.MD_BINNING: (1, 1), # px, px model.MD_PIXEL_SIZE: (1e-6, 1e-6), # 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: ((630e-9, 660e-9), (675e-9, 690e-9)), # m model.MD_USER_TINT: (255, 0, 65), # purple model.MD_LIGHT_POWER: 100e-3, # W } ] size = (512, 256) dtype = numpy.dtype("uint16") ldata = [] for i, md in enumerate(metadata): a = model.DataArray(numpy.zeros(size[::-1], dtype), md.copy()) a[i, i] = i # "watermark" it ldata.append(a) # export hdf5.export(FILENAME, ldata) # check data rdata = hdf5.read_data(FILENAME) self.assertEqual(len(rdata), len(ldata)) im = rdata[0] emd = metadata[0].copy() rmd = im.metadata img.mergeMetadata(emd) img.mergeMetadata(rmd) self.assertEqual(rmd[model.MD_DESCRIPTION], emd[model.MD_DESCRIPTION]) iwl = rmd[model.MD_IN_WL] # nm self.assertTrue((emd[model.MD_IN_WL][0] <= iwl[0] and iwl[1] <= emd[model.MD_IN_WL][-1])) # It should be within at least one of the bands owl = rmd[model.MD_OUT_WL] # nm for eowl in emd[model.MD_OUT_WL]: if eowl[0] <= owl[0] and owl[1] <= eowl[-1]: break else: self.fail("Out wl %s is not within original metadata" % (owl,))
def test_100x100(self): data = self.data C, T, Z, Y, X = data[0].shape data[0].shape = Y, X result = polar.AngleResolved2Polar(data[0], 101) desired_output = hdf5.read_data("desired100x100image.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def setUp(self): data = hdf5.read_data("ar-example-input.h5") mag = 0.4917 spxs = (13e-6, 13e-6) binning = (4, 4) # data[0].metadata[model.MD_BASELINE] = 820 data[0].metadata[model.MD_BINNING] = binning data[0].metadata[model.MD_SENSOR_PIXEL_SIZE] = spxs data[0].metadata[model.MD_LENS_MAG] = mag data[0].metadata[model.MD_AR_POLE] = (141, 255 - 139.449038462) mag = data[0].metadata[model.MD_LENS_MAG] pxs = (spxs[0] * binning[0] / mag, spxs[1] * binning[1] / mag) data[0].metadata[model.MD_PIXEL_SIZE] = pxs self.data = data white_data_512 = model.DataArray(numpy.empty((512, 512), dtype="uint16")) white_data_512[...] = 255 white_mag_512 = 0.4917 white_spxs_512 = (13e-6, 13e-6) white_binning_512 = (2, 2) white_data_512.metadata[model.MD_AR_POLE] = (283, 259) white_pxs_512 = (white_spxs_512[0] * white_binning_512[0] / white_mag_512, white_spxs_512[1] * white_binning_512[1] / white_mag_512) white_data_512.metadata[model.MD_PIXEL_SIZE] = white_pxs_512 self.white_data_512 = white_data_512 white_data_1024 = model.DataArray(numpy.empty((1024, 1024), dtype="uint16")) white_data_1024[...] = 255 white_mag_1024 = 0.4917 white_spxs_1024 = (13e-6, 13e-6) white_binning_1024 = (2, 2) white_data_1024.metadata[model.MD_AR_POLE] = (283, 259) white_pxs_1024 = (white_spxs_1024[0] * white_binning_1024[0] / white_mag_1024, white_spxs_1024[1] * white_binning_1024[1] / white_mag_1024) white_data_1024.metadata[model.MD_PIXEL_SIZE] = white_pxs_1024 self.white_data_1024 = white_data_1024 white_data_2500 = model.DataArray(numpy.empty((2560, 2160), dtype="uint16")) white_data_2500[...] = 255 white_mag_2500 = 0.4917 white_spxs_2500 = (13e-6, 13e-6) white_binning_2500 = (2, 2) white_data_2500.metadata[model.MD_AR_POLE] = (283, 259) # These values makes the computation much harder: # white_mag_2500 = 0.53 # white_spxs_2500 = (6.5e-6, 6.5e-6) # white_binning_2500 = (1, 1) # white_data_2500.metadata[model.MD_AR_POLE] = (1480, 1129) white_pxs_2500 = (white_spxs_2500[0] * white_binning_2500[0] / white_mag_2500, white_spxs_2500[1] * white_binning_2500[1] / white_mag_2500) white_data_2500.metadata[model.MD_PIXEL_SIZE] = white_pxs_2500 self.white_data_2500 = white_data_2500
def testExportRead(self): """ Checks that we can read back an image and a thumbnail """ # create 2 simple greyscale images sizes = [(512, 256), (500, 400) ] # different sizes to ensure different acquisitions dtype = numpy.dtype("uint16") white = (12, 52) # non symmetric position ldata = [] num = 2 # TODO: check support for combining channels when same data shape for i in range(num): a = model.DataArray(numpy.zeros(sizes[i][::-1], dtype)) a[white[::-1]] = 1027 ldata.append(a) # thumbnail : small RGB completely red tshape = (sizes[0][1] // 8, sizes[0][0] // 8, 3) tdtype = numpy.uint8 thumbnail = model.DataArray(numpy.zeros(tshape, tdtype)) thumbnail[:, :, 0] += 255 # red blue = (12, 22) # non symmetric position thumbnail[blue[::-1]] = [0, 0, 255] thumbnail.metadata[model.MD_POS] = (0.1, -2) thumbnail.metadata[model.MD_PIXEL_SIZE] = (13e-6, 13e-6) # export hdf5.export(FILENAME, ldata, thumbnail) # check it's here st = os.stat(FILENAME) # this test also that the file is created self.assertGreater(st.st_size, 0) # check data rdata = hdf5.read_data(FILENAME) self.assertEqual(len(rdata), num) for i, im in enumerate(rdata): subim = im[0, 0, 0] # remove C,T,Z dimensions self.assertEqual(subim.shape, sizes[i][-1::-1]) self.assertEqual(subim[white[-1:-3:-1]], ldata[i][white[-1:-3:-1]]) # check thumbnail rthumbs = hdf5.read_thumbnail(FILENAME) self.assertEqual(len(rthumbs), 1) im = rthumbs[0] self.assertEqual(im.shape, tshape) self.assertEqual(im[0, 0].tolist(), [255, 0, 0]) self.assertEqual(im[blue[::-1]].tolist(), [0, 0, 255]) self.assertAlmostEqual(im.metadata[model.MD_POS], thumbnail.metadata[model.MD_POS])
def testReadMDOutWlBands(self): """ Checks that we hand MD_OUT_WL if it contains multiple bands. OME supports only one value, so it's ok to discard some info. """ metadata = [{model.MD_SW_VERSION: "1.0-test", model.MD_HW_NAME: "fake hw", model.MD_DESCRIPTION: "blue dye", model.MD_ACQ_DATE: time.time() + 1, model.MD_BPP: 12, model.MD_BINNING: (1, 1), # px, px model.MD_PIXEL_SIZE: (1e-6, 1e-6), # 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: ((630e-9, 660e-9), (675e-9, 690e-9)), # m model.MD_USER_TINT: (255, 0, 65), # purple model.MD_LIGHT_POWER: 100e-3 # W }, ] size = (512, 256) dtype = numpy.dtype("uint16") ldata = [] for i, md in enumerate(metadata): a = model.DataArray(numpy.zeros(size[::-1], dtype), md.copy()) a[i, i] = i # "watermark" it ldata.append(a) # export hdf5.export(FILENAME, ldata) # check data rdata = hdf5.read_data(FILENAME) self.assertEqual(len(rdata), len(ldata)) im = rdata[0] emd = metadata[0].copy() rmd = im.metadata img.mergeMetadata(emd) img.mergeMetadata(rmd) self.assertEqual(rmd[model.MD_DESCRIPTION], emd[model.MD_DESCRIPTION]) iwl = rmd[model.MD_IN_WL] # nm self.assertTrue((emd[model.MD_IN_WL][0] <= iwl[0] and iwl[1] <= emd[model.MD_IN_WL][-1])) # It should be within at least one of the bands owl = rmd[model.MD_OUT_WL] # nm for eowl in emd[model.MD_OUT_WL]: if (eowl[0] <= owl[0] and owl[1] <= eowl[-1]): break else: self.fail("Out wl %s is not within original metadata" % (owl,))
def test_512x512(self): """ Test for 512x512 white image input """ white_data_512 = self.white_data_512 Y, X = white_data_512.shape result = polar.AngleResolved2Polar(white_data_512, 201) desired_output = hdf5.read_data("desired_white_512.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_2560x2160(self): """ Test for 2560x2160 white image input """ white_data_2500 = self.white_data_2500 Y, X = white_data_2500.shape result = polar.AngleResolved2Polar(white_data_2500, 2000, dtype=numpy.float16) desired_output = hdf5.read_data("desired_white_2500.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_find_hole_center(self): """ Test FindCircleCenter for holes """ # Note: this hole image has a better contrast and less noise than typical # hole images on the DELPHI data = hdf5.read_data(os.path.join(TEST_IMAGE_PATH, "sem_hole.h5")) C, T, Z, Y, X = data[0].shape data[0].shape = Y, X hole_coordinates = delphi.FindCircleCenter(data[0], 0.02032, 6, darkest=True) expected_coordinates = (0.0052705, -0.0018415) # (391.5, 257.5) px numpy.testing.assert_almost_equal(hole_coordinates, expected_coordinates)
def testExportRead(self): """ Checks that we can read back an image and a thumbnail """ # create 2 simple greyscale images sizes = [(512, 256), (500, 400)] # different sizes to ensure different acquisitions dtype = numpy.dtype("uint16") white = (12, 52) # non symmetric position ldata = [] num = 2 # TODO: check support for combining channels when same data shape for i in range(num): a = model.DataArray(numpy.zeros(sizes[i][::-1], dtype)) a[white[::-1]] = 1027 ldata.append(a) # thumbnail : small RGB completely red tshape = (sizes[0][1] // 8, sizes[0][0] // 8, 3) tdtype = numpy.uint8 thumbnail = model.DataArray(numpy.zeros(tshape, tdtype)) thumbnail[:, :, 0] += 255 # red blue = (12, 22) # non symmetric position thumbnail[blue[::-1]] = [0, 0, 255] thumbnail.metadata[model.MD_POS] = (0.1, -2) thumbnail.metadata[model.MD_PIXEL_SIZE] = (13e-6, 13e-6) # export hdf5.export(FILENAME, ldata, thumbnail) # check it's here st = os.stat(FILENAME) # this test also that the file is created self.assertGreater(st.st_size, 0) # check data rdata = hdf5.read_data(FILENAME) self.assertEqual(len(rdata), num) for i, im in enumerate(rdata): subim = im[0, 0, 0] # remove C,T,Z dimensions self.assertEqual(subim.shape, sizes[i][-1::-1]) self.assertEqual(subim[white[-1:-3:-1]], ldata[i][white[-1:-3:-1]]) # check thumbnail rthumbs = hdf5.read_thumbnail(FILENAME) self.assertEqual(len(rthumbs), 1) im = rthumbs[0] self.assertEqual(im.shape, tshape) self.assertEqual(im[0, 0].tolist(), [255, 0, 0]) self.assertEqual(im[blue[::-1]].tolist(), [0, 0, 255]) self.assertAlmostEqual(im.metadata[model.MD_POS], thumbnail.metadata[model.MD_POS])
def test_uint16_input(self): """ Tests for input of DataArray with uint16 ndarray. """ data = self.data C, T, Z, Y, X = data[0].shape data[0].shape = Y, X result = polar.AngleResolved2Polar(data[0], 201) desired_output = hdf5.read_data("desired201x201image.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_autofocus_spect(self): """ Test AutoFocus on 1 line CCD for example spectrum. """ # Make sure the image is the example spectrum image, in case this test runs after test_autofocus_slit. data = hdf5.read_data(os.path.dirname(odemis.__file__) + "/driver/sparc-spec-sim.h5") new_img = img.ensure2DImage(data[0]) self.ccd.set_image(new_img) self.focus.moveAbs({"z": self._good_focus - 200e-6}).result() f = align.AutoFocus(self.spectrometer, None, self.focus, method=MTD_BINARY) foc_pos, foc_lev = f.result(timeout=900) logging.debug("Found focus at {} good focus at {}".format(foc_pos, self._good_focus)) # The focus step size is 10.9e-6, the tolerance is set to 2.5e-5; approximately two focus steps. numpy.testing.assert_allclose(foc_pos, self._good_focus, atol=2.5e-5)
def test_2560x2160(self): """ Test for 2560x2160 white image input """ white_data_2500 = self.white_data_2500 result = angleres.AngleResolved2Polar(white_data_2500, 2000) desired_output = hdf5.read_data("desired_white_2500.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_divide_and_find_center_grid(self): """ Test DivideInNeighborhoods combined with FindCenterCoordinates """ grid_data = hdf5.read_data("grid_10x10.h5") C, T, Z, Y, X = grid_data[0].shape grid_data[0].shape = Y, X subimages, subimage_coordinates = coordinates.DivideInNeighborhoods(grid_data[0], (10, 10), 40) spot_coordinates = [spot.FindCenterCoordinates(i) for i in subimages] optical_coordinates = coordinates.ReconstructCoordinates(subimage_coordinates, spot_coordinates) self.assertEqual(len(subimages), 100)
def test_background_substraction_precomputed(self): """ Test clean up before polar conversion """ data = self.data C, T, Z, Y, X = data[0].shape data[0].shape = Y, X clean_data = angleres.ARBackgroundSubtract(data[0]) result = angleres.AngleResolved2Polar(clean_data, 201) desired_output = hdf5.read_data("substracted_background_image.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_1000x1000(self): data = self.data data[0] = data[0].astype(numpy.int64) data[0] = numpy.right_shift(data[0], 8) data[0] = data[0].astype(numpy.int8) C, T, Z, Y, X = data[0].shape data[0].shape = Y, X result = angleres.AngleResolved2Polar(data[0], 1001) desired_output = hdf5.read_data("desired1000x1000image.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1)
def test_float_input(self): """ Tests for input of DataArray with float ndarray. """ data = self.data data[0] = data[0].astype(numpy.float) C, T, Z, Y, X = data[0].shape data[0].shape = Y, X result = angleres.AngleResolved2Polar(data[0], 201) desired_output = hdf5.read_data("desired201x201image.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_measure_focus(self): """ Test MeasureFocus """ data = hdf5.read_data(os.path.dirname(__file__) + "/grid_10x10.h5") C, T, Z, Y, X = data[0].shape data[0].shape = Y, X input = data[0] prev_res = autofocus.MeasureSEMFocus(input) for i in range(1, 10, 1): blur = ndimage.gaussian_filter(input, sigma=i) res = autofocus.MeasureSEMFocus(blur) self.assertGreater(prev_res, res) prev_res = res
def test_2560x2160(self): """ Test for 2560x2160 white image input """ white_data_2500 = self.white_data_2500 Y, X = white_data_2500.shape result = polar.AngleResolved2Polar(white_data_2500, 2000, dtype=numpy.float16) desired_output = hdf5.read_data("desired_white_2500.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X # FIXME: doesn't seem to pass on 64 bits ?! floating point computation differences? numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_background_substraction_float_input(self): """ Tests for input of DataArray with float ndarray. """ data = self.data data[0] = data[0].astype(numpy.float) C, T, Z, Y, X = data[0].shape data[0].shape = Y, X clean_data = angleres.ARBackgroundSubtract(data[0]) result = angleres.AngleResolved2Polar(clean_data, 201) desired_output = hdf5.read_data("substracted_background_image.h5") C, T, Z, Y, X = desired_output[0].shape desired_output[0].shape = Y, X numpy.testing.assert_allclose(result, desired_output[0], rtol=1e-04)
def test_devide_and_find_center_grid(self): """ Test DivideInNeighborhoods combined with FindCenterCoordinates """ grid_data = hdf5.read_data("grid_10x10.h5") C, T, Z, Y, X = grid_data[0].shape grid_data[0].shape = Y, X subimages, subimage_coordinates = coordinates.DivideInNeighborhoods( grid_data[0], (10, 10), 40) spot_coordinates = coordinates.FindCenterCoordinates(subimages) optical_coordinates = coordinates.ReconstructCoordinates( subimage_coordinates, spot_coordinates) self.assertEqual(len(subimages), 100)