def test_masked_filter(): from numpy import median from dials.algorithms.image.filter import median_filter xsize = 200 ysize = 300 kernel = (3, 3) image = generate_image(xsize, ysize) mask = generate_mask(xsize, ysize) result = median_filter(image, mask, kernel) eps = 1e-7 for j in range(kernel[0], ysize - kernel[0]): for i in range(kernel[1], xsize - kernel[1]): if mask[j, i]: j0 = j - kernel[0] j1 = j + kernel[0] + 1 i0 = i - kernel[1] i1 = i + kernel[1] + 1 pixels = image[j0:j1, i0:i1] pmask = mask[j0:j1, i0:i1] pixels = pixels.as_1d().select(pmask.as_1d()) if len(pixels) & 1: value = median(pixels.as_numpy_array()) else: pixels = sorted(pixels) value = pixels[len(pixels) // 2] assert result[j, i] == pytest.approx(value, abs=eps)
def tst_masked_filter(self): from numpy import median from dials.algorithms.image.filter import median_filter xsize = 200 ysize = 300 image = self.generate_image(xsize, ysize) mask = self.generate_mask(xsize, ysize) result = median_filter(image, mask, self.kernel) eps = 1e-7 for j in range(self.kernel[0], ysize - self.kernel[0]): for i in range(self.kernel[1], xsize - self.kernel[1]): if mask[j, i]: j0 = j - self.kernel[0] j1 = j + self.kernel[0] + 1 i0 = i - self.kernel[1] i1 = i + self.kernel[1] + 1 pixels = image[j0:j1, i0:i1] pmask = mask[j0:j1, i0:i1] pixels = pixels.as_1d().select(pmask.as_1d()) if len(pixels) & 1: value = median(pixels.as_numpy_array()) else: pixels = sorted(list(pixels)) value = pixels[len(pixels) // 2] assert (abs(value - result[j, i]) < eps) else: assert (abs(result[j, i] - 0.0) < eps) print 'OK'
def tst_masked_filter(self): from numpy import median from dials.algorithms.image.filter import median_filter xsize = 200 ysize = 300 image = self.generate_image(xsize, ysize) mask = self.generate_mask(xsize, ysize) result = median_filter(image, mask, self.kernel) eps = 1e-7 for j in range(self.kernel[0], ysize-self.kernel[0]): for i in range(self.kernel[1], xsize-self.kernel[1]): if mask[j,i]: j0 = j - self.kernel[0] j1 = j + self.kernel[0] + 1 i0 = i - self.kernel[1] i1 = i + self.kernel[1] + 1 pixels = image[j0:j1,i0:i1] pmask = mask[j0:j1,i0:i1] pixels = pixels.as_1d().select(pmask.as_1d()) if len(pixels) & 1: value = median(pixels.as_numpy_array()) else: pixels = sorted(list(pixels)) value = pixels[len(pixels) // 2] assert(abs(value - result[j,i]) < eps) else: assert(abs(result[j,i] - 0.0) < eps) print 'OK'
def test_filter(): from numpy import median from dials.algorithms.image.filter import median_filter xsize = 200 ysize = 300 kernel = (3, 3) image = generate_image(xsize, ysize) result = median_filter(image, kernel) eps = 1e-7 for j in range(kernel[0], ysize - kernel[0]): for i in range(kernel[1], xsize - kernel[1]): j0 = j - kernel[0] j1 = j + kernel[0] + 1 i0 = i - kernel[1] i1 = i + kernel[1] + 1 pixels = image[j0:j1, i0:i1] value = median(pixels.as_numpy_array()) assert result[j, i] == pytest.approx(value, abs=eps)
def tst_filter(self): from numpy import median from dials.algorithms.image.filter import median_filter xsize = 200 ysize = 300 image = self.generate_image(xsize, ysize) result = median_filter(image, self.kernel) eps = 1e-7 for j in range(self.kernel[0], ysize - self.kernel[0]): for i in range(self.kernel[1], xsize - self.kernel[1]): j0 = j - self.kernel[0] j1 = j + self.kernel[0] + 1 i0 = i - self.kernel[1] i1 = i + self.kernel[1] + 1 pixels = image[j0:j1, i0:i1] value = median(pixels.as_numpy_array()) assert (abs(value - result[j, i]) < eps) print 'OK'
def tst_filter(self): from numpy import median from dials.algorithms.image.filter import median_filter xsize = 200 ysize = 300 image = self.generate_image(xsize, ysize) result = median_filter(image, self.kernel) eps = 1e-7 for j in range(self.kernel[0], ysize-self.kernel[0]): for i in range(self.kernel[1], xsize-self.kernel[1]): j0 = j - self.kernel[0] j1 = j + self.kernel[0] + 1 i0 = i - self.kernel[1] i1 = i + self.kernel[1] + 1 pixels = image[j0:j1,i0:i1] value = median(pixels.as_numpy_array()) assert(abs(value - result[j,i]) < eps) print 'OK'
def finalize(self, data, mask): ''' Finalize the model :param data: The data array :param mask: The mask array ''' from dials.algorithms.image.filter import median_filter, mean_filter from dials.algorithms.image.fill_holes import diffusion_fill from dials.algorithms.image.fill_holes import simple_fill from dials.array_family import flex # Print some image properties sub_data = data.as_1d().select(mask.as_1d()) logger.info('Raw image statistics:') logger.info(' min: %d' % int(flex.min(sub_data))) logger.info(' max: %d' % int(flex.max(sub_data))) logger.info(' mean: %d' % int(flex.mean(sub_data))) logger.info('') # Transform to polar logger.info('Transforming image data to polar grid') result = self.transform.to_polar(data, mask) data = result.data() mask = result.mask() sub_data = data.as_1d().select(mask.as_1d()) logger.info('Polar image statistics:') logger.info(' min: %d' % int(flex.min(sub_data))) logger.info(' max: %d' % int(flex.max(sub_data))) logger.info(' mean: %d' % int(flex.mean(sub_data))) logger.info('') # Filter the image to remove noise if self.kernel_size > 0: if self.filter_type == 'median': logger.info('Applying median filter') data = median_filter(data, mask, (self.kernel_size, 0)) sub_data = data.as_1d().select(mask.as_1d()) logger.info('Median polar image statistics:') logger.info(' min: %d' % int(flex.min(sub_data))) logger.info(' max: %d' % int(flex.max(sub_data))) logger.info(' mean: %d' % int(flex.mean(sub_data))) logger.info('') elif self.filter_type == 'mean': logger.info('Applying mean filter') mask_as_int = mask.as_1d().as_int() mask_as_int.reshape(mask.accessor()) data = mean_filter(data, mask_as_int, (self.kernel_size, 0), 1) sub_data = data.as_1d().select(mask.as_1d()) logger.info('Mean polar image statistics:') logger.info(' min: %d' % int(flex.min(sub_data))) logger.info(' max: %d' % int(flex.max(sub_data))) logger.info(' mean: %d' % int(flex.mean(sub_data))) logger.info('') else: raise RuntimeError('Unknown filter_type: %s' % self.filter_type) # Fill any remaining holes logger.info("Filling holes") data = simple_fill(data, mask) data = diffusion_fill(data, mask, self.niter) mask = flex.bool(data.accessor(), True) sub_data = data.as_1d().select(mask.as_1d()) logger.info('Filled polar image statistics:') logger.info(' min: %d' % int(flex.min(sub_data))) logger.info(' max: %d' % int(flex.max(sub_data))) logger.info(' mean: %d' % int(flex.mean(sub_data))) logger.info('') # Transform back logger.info('Transforming image data from polar grid') result = self.transform.from_polar(data, mask) data = result.data() mask = result.mask() sub_data = data.as_1d().select(mask.as_1d()) logger.info('Final image statistics:') logger.info(' min: %d' % int(flex.min(sub_data))) logger.info(' max: %d' % int(flex.max(sub_data))) logger.info(' mean: %d' % int(flex.mean(sub_data))) logger.info('') # Fill in any discontinuities # FIXME NEED TO HANDLE DISCONTINUITY # mask = ~self.transform.discontinuity()[:-1,:-1] # data = diffusion_fill(data, mask, self.niter) # Get and apply the mask mask = self.experiment.imageset.get_mask(0)[0] mask = mask.as_1d().as_int().as_double() mask.reshape(data.accessor()) data *= mask # Return the result return data
def finalize(self, data, mask): """ Finalize the model :param data: The data array :param mask: The mask array """ from dials.algorithms.image.filter import median_filter, mean_filter from dials.algorithms.image.fill_holes import diffusion_fill from dials.algorithms.image.fill_holes import simple_fill from dials.array_family import flex # Print some image properties sub_data = data.as_1d().select(mask.as_1d()) logger.info("Raw image statistics:") logger.info(" min: %d" % int(flex.min(sub_data))) logger.info(" max: %d" % int(flex.max(sub_data))) logger.info(" mean: %d" % int(flex.mean(sub_data))) logger.info("") # Transform to polar logger.info("Transforming image data to polar grid") result = self.transform.to_polar(data, mask) data = result.data() mask = result.mask() sub_data = data.as_1d().select(mask.as_1d()) logger.info("Polar image statistics:") logger.info(" min: %d" % int(flex.min(sub_data))) logger.info(" max: %d" % int(flex.max(sub_data))) logger.info(" mean: %d" % int(flex.mean(sub_data))) logger.info("") # Filter the image to remove noise if self.kernel_size > 0: if self.filter_type == "median": logger.info("Applying median filter") data = median_filter(data, mask, (self.kernel_size, 0), periodic=True) sub_data = data.as_1d().select(mask.as_1d()) logger.info("Median polar image statistics:") logger.info(" min: %d" % int(flex.min(sub_data))) logger.info(" max: %d" % int(flex.max(sub_data))) logger.info(" mean: %d" % int(flex.mean(sub_data))) logger.info("") elif self.filter_type == "mean": logger.info("Applying mean filter") mask_as_int = mask.as_1d().as_int() mask_as_int.reshape(mask.accessor()) data = mean_filter(data, mask_as_int, (self.kernel_size, 0), 1) sub_data = data.as_1d().select(mask.as_1d()) logger.info("Mean polar image statistics:") logger.info(" min: %d" % int(flex.min(sub_data))) logger.info(" max: %d" % int(flex.max(sub_data))) logger.info(" mean: %d" % int(flex.mean(sub_data))) logger.info("") else: raise RuntimeError("Unknown filter_type: %s" % self.filter_type) # Fill any remaining holes logger.info("Filling holes") data = simple_fill(data, mask) data = diffusion_fill(data, mask, self.niter) mask = flex.bool(data.accessor(), True) sub_data = data.as_1d().select(mask.as_1d()) logger.info("Filled polar image statistics:") logger.info(" min: %d" % int(flex.min(sub_data))) logger.info(" max: %d" % int(flex.max(sub_data))) logger.info(" mean: %d" % int(flex.mean(sub_data))) logger.info("") # Transform back logger.info("Transforming image data from polar grid") result = self.transform.from_polar(data, mask) data = result.data() mask = result.mask() sub_data = data.as_1d().select(mask.as_1d()) logger.info("Final image statistics:") logger.info(" min: %d" % int(flex.min(sub_data))) logger.info(" max: %d" % int(flex.max(sub_data))) logger.info(" mean: %d" % int(flex.mean(sub_data))) logger.info("") # Fill in any discontinuities mask = ~self.transform.discontinuity()[:-1, :-1] data = diffusion_fill(data, mask, self.niter) # Get and apply the mask mask = self.experiment.imageset.get_mask(0)[0] mask = mask.as_1d().as_int().as_double() mask.reshape(data.accessor()) data *= mask # Return the result return data