def test_inverted_mirror_ar2polar(self): data_invMirror = ensure2DImage(self.data_invMir[0]) result_invMirror = angleres.AngleResolved2Polar(data_invMirror, 1134) # get the inverted image of the one that corresponds to the flipped mirror data = data_invMirror[::-1, :] data.metadata[model.MD_AR_FOCUS_DISTANCE] *= -1 arpole = data.metadata[model.MD_AR_POLE] data.metadata[model.MD_AR_POLE] = (arpole[0], data_invMirror.shape[0] - 1 - arpole[1]) result_standardMirror = angleres.AngleResolved2Polar(data, 1134) numpy.testing.assert_allclose(result_invMirror, result_standardMirror, atol=1e-7)
def test_int8_input(self): """ Tests for input of DataArray with int8 ndarray. """ data = self.data # scipy.misc.bytescale(data) # for debug 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], 201) desired_output = angleres.AngleResolved2Polar(data[0].astype(float), 201) numpy.testing.assert_allclose(result, desired_output, rtol=1e-04)
def test_background_substraction_int8_input(self): """ Tests for input of DataArray with int8 ndarray. """ data = self.data # scipy.misc.bytescale(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 clean_data = angleres.ARBackgroundSubtract(data[0]) result = angleres.AngleResolved2Polar(clean_data, 201) desired_output = angleres.AngleResolved2Polar(data[0].astype(float), 201) numpy.testing.assert_allclose(result, desired_output, rtol=1)
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_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_2000x2000(self): data = self.data C, T, Z, Y, X = data[0].shape data[0].shape = Y, X result = angleres.AngleResolved2Polar(data[0], 2001) desired_output = hdf5.read_data("desired2000x2000image.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 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_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_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_uint16_input_rect2polar(self): """ Tests for input of DataArray with uint16 ndarray from rectangular projection to polar projection. """ data = self.data C, T, Z, Y, X = data[0].shape data[0].shape = Y, X data[0].metadata[MD_POL_MODE] = MD_POL_S1 # add necessary metadata # convert to rectangular data_rect = angleres.AngleResolved2Rectangular(data[0], (100, 400)) # convert from rectangular to polar result = angleres.Rectangular2Polar(data_rect, 201) result_polar_1 = RGB2Greyscale(result) # convert input data directly to polar representation result_polar_2 = angleres.AngleResolved2Polar(data[0], 201) self.assertEqual(result.shape, (201, 201, 3)) self.assertEqual(result_polar_2.shape, result_polar_1.shape)
def _project2Polar(self, pos): """ Return the polar projection of the image at the given position. pos (float, float, string or None): position (must be part of the ._pos) returns DataArray: the polar projection """ # Note: Need a copy of the link to the dict. If self._polar is reset while # still running this method, the dict might get new entries again, though it should be empty. polar = self._polar if pos in polar: polard = polar[pos] else: # Compute the polar representation data = self._pos[pos] try: # Get bg image, if existing. It must match the polarization (defaulting to MD_POL_NONE). bg_image = self._getBackground( data.metadata.get(MD_POL_MODE, MD_POL_NONE)) if bg_image is None: # Simple version: remove the background value data_bg_corr = angleres.ARBackgroundSubtract(data) else: data_bg_corr = img.Subtract(data, bg_image) # metadata from data if numpy.prod(data_bg_corr.shape) > (1280 * 1080): # AR conversion fails with very large images due to too much # memory consumed (> 2Gb). So, rescale + use a "degraded" type that # uses less memory. As the display size is small (compared # to the size of the input image, it shouldn't actually # affect much the output. logging.info( "AR image is very large %s, will convert to " "azimuthal projection in reduced precision.", data_bg_corr.shape) y, x = data_bg_corr.shape if y > x: small_shape = 1024, int(round(1024 * x / y)) else: small_shape = int(round(1024 * y / x)), 1024 # resize data_bg_corr = img.rescale_hq(data_bg_corr, small_shape) # 2 x size of original image (on smallest axis) and at most # the size of a full-screen canvas size = min(min(data_bg_corr.shape) * 2, 1134) # TODO: First compute quickly a low resolution and then # compute a high resolution version. # TODO: could use the size of the canvas that will display # the image to save some computation time. # Warning: allocates lot of memory, which will not be free'd until # the current thread is terminated. polard = angleres.AngleResolved2Polar(data_bg_corr, size, hole=False) # TODO: don't hold too many of them in cache (eg, max 3 * 1134**2) polar[pos] = polard except Exception: logging.exception("Failed to convert to azimuthal projection") return data # display it raw as fallback return polard