def _getPolarProjection(self, pos): """ Return the polar projection of the image at the given position. pos (tuple of 2 floats): position (must be part of the ._sempos returns DataArray: the polar projection """ if pos in self._polar: polard = self._polar[pos] else: # Compute the polar representation data = self._sempos[pos] try: if numpy.prod(data.shape) > (1280 * 1080): # AR conversion fails one 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 " "azymuthal projection in reduced precision.", data.shape) y, x = data.shape if y > x: small_shape = 1024, int(round(1024 * x / y)) else: small_shape = int(round(1024 * y / x)), 1024 # resize data = img.rescale_hq(data, small_shape) dtype = numpy.float16 else: dtype = None # just let the function use the best one size = min(min(data.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. bg_data = self.background.value if bg_data is None: # Simple version: remove the background value data0 = polar.ARBackgroundSubtract(data) else: data0 = img.Subtract(data, bg_data) # metadata from data # 2 x size of original image (on smallest axis) and at most # the size of a full-screen canvas polard = polar.AngleResolved2Polar(data0, size, hole=False, dtype=dtype) self._polar[pos] = polard except Exception: logging.exception("Failed to convert to azymuthal projection") return data # display it raw as fallback return polard
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 = polar.ARBackgroundSubtract(data[0]) result = polar.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_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 = polar.ARBackgroundSubtract(data[0]) result = polar.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_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 = polar.ARBackgroundSubtract(data[0]) result = polar.AngleResolved2Polar(clean_data, 201) desired_output = polar.AngleResolved2Polar(data[0].astype(float), 201) numpy.testing.assert_allclose(result, desired_output, rtol=1)