def sort(self): perm = flex.sort_permutation( data=flex.abs(flex.double(self.array_of_a())), reverse=True) return sum( flex.select(self.array_of_a(), perm), flex.select(self.array_of_b(), perm), self.c(), self.use_c())
def fake_it(self, size): selection_set = self.non_para_bootstrap.draw(size) isel = flex.int() for element in selection_set: isel.append(int(element)) new_x = flex.double(flex.select(self.x_data, isel)) new_y = flex.double(flex.select(self.y_data, isel)) return new_x, new_y
def test_masked_mean_filter(): from scitbx.array_family import flex from dials.algorithms.image.filter import mean_filter # Create an image image = flex.random_double(2000 * 2000) image.reshape(flex.grid(2000, 2000)) mask = flex.random_bool(2000 * 2000, 0.99).as_int() mask.reshape(flex.grid(2000, 2000)) # Calculate the summed area table mask2 = mask.deep_copy() mean = mean_filter(image, mask2, (3, 3), 1) # For a selection of random points, ensure that the value is the # sum of the area under the kernel eps = 1e-7 for i in range(10000): i = random.randint(10, 1990) j = random.randint(10, 1990) m1 = mean[j, i] p = image[j - 3:j + 4, i - 3:i + 4] m = mask[j - 3:j + 4, i - 3:i + 4] if mask[j, i] == 0: m2 = 0.0 else: p = flex.select(p, flags=m) mv = flex.mean_and_variance(flex.double(p)) m2 = mv.mean() assert m1 == pytest.approx(m2, abs=eps)
def is_correct(self, shoebox, mask, n_sigma, min_pixels): from scitbx.array_family import flex flags = [m & (1 << 0) for m in mask] pixels = flex.select(shoebox, flags=flags) assert (len(pixels) >= min_pixels) meanp = flex.mean_and_variance(flex.double(pixels)) maxp = flex.max(flex.double(pixels)) m = meanp.mean() s = meanp.unweighted_sample_standard_deviation() assert (maxp <= m + n_sigma * s)
def is_correct(self, shoebox, mask, n_sigma, min_pixels): from scitbx.array_family import flex flags = [m & (1 << 0) for m in mask] pixels = flex.select(shoebox, flags=flags) assert(len(pixels) >= min_pixels) meanp = flex.mean_and_variance(flex.double(pixels)) maxp = flex.max(flex.double(pixels)) m = meanp.mean() s = meanp.unweighted_sample_standard_deviation() assert(maxp <= m + n_sigma * s)
def show_minimize_multi_histogram(f=None, reset=True): global minimize_multi_histogram minimizer_types = list(minimize_multi_histogram.keys()) counts = flex.double(list(minimize_multi_histogram.values())) perm = flex.sort_permutation(data=counts, reverse=True) minimizer_types = flex.select(minimizer_types, perm) counts = counts.select(perm) n_total = flex.sum(counts) for m, c in zip(minimizer_types, counts): print("%-39s %5.3f %6d" % (m, c / max(1, n_total), c), file=f) print(file=f) if (reset): minimize_multi_histogram = {"None": 0}
def show_minimize_multi_histogram(f=None, reset=True): global minimize_multi_histogram minimizer_types = minimize_multi_histogram.keys() counts = flex.double(minimize_multi_histogram.values()) perm = flex.sort_permutation(data=counts, reverse=True) minimizer_types = flex.select(minimizer_types, perm) counts = counts.select(perm) n_total = flex.sum(counts) for m,c in zip(minimizer_types, counts): print >> f, "%-39s %5.3f %6d" % (m, c/max(1,n_total), c) print >> f if (reset): minimize_multi_histogram = {"None": 0}
def run(self): from dials.algorithms.image.filter import fano_filter from scitbx.array_family import flex from random import randint # Create an image image = flex.random_double(2000 * 2000) image.reshape(flex.grid(2000, 2000)) mask = flex.random_bool(2000 * 2000, 0.99).as_int() mask.reshape(flex.grid(2000, 2000)) # Calculate the summed area table mask2 = mask.deep_copy() fano_filter = fano_filter(image, mask2, (3, 3), 2) mean = fano_filter.mean() var = fano_filter.sample_variance() fano = fano_filter.fano() # For a selection of random points, ensure that the value is the # sum of the area under the kernel eps = 1e-7 for i in range(10000): i = randint(10, 1990) j = randint(10, 1990) m1 = mean[j, i] v1 = var[j, i] f1 = fano[j, i] p = image[j - 3:j + 4, i - 3:i + 4] m = mask[j - 3:j + 4, i - 3:i + 4] if mask[j, i] == 0: m2 = 0.0 v2 = 0.0 f2 = 1.0 else: p = flex.select(p, flags=m) mv = flex.mean_and_variance(flex.double(p)) m2 = mv.mean() v2 = mv.unweighted_sample_variance() f2 = v2 / m2 assert (abs(m1 - m2) <= eps) assert (abs(v1 - v2) <= eps) assert (abs(f1 - f2) <= eps) # Test passed print 'OK'
def run(self): from dials.algorithms.image.filter import fano_filter from scitbx.array_family import flex from random import randint # Create an image image = flex.random_double(2000 * 2000) image.reshape(flex.grid(2000, 2000)) mask = flex.random_bool(2000 * 2000, 0.99).as_int() mask.reshape(flex.grid(2000, 2000)) # Calculate the summed area table mask2 = mask.deep_copy() fano_filter = fano_filter(image, mask2, (3, 3), 2) mean = fano_filter.mean() var = fano_filter.sample_variance() fano = fano_filter.fano() # For a selection of random points, ensure that the value is the # sum of the area under the kernel eps = 1e-7 for i in range(10000): i = randint(10, 1990) j = randint(10, 1990) m1 = mean[j,i] v1 = var[j,i] f1 = fano[j,i] p = image[j-3:j+4,i-3:i+4] m = mask[j-3:j+4,i-3:i+4] if mask[j,i] == 0: m2 = 0.0 v2 = 0.0 f2 = 1.0 else: p = flex.select(p, flags=m) mv = flex.mean_and_variance(flex.double(p)) m2 = mv.mean() v2 = mv.unweighted_sample_variance() f2 = v2 / m2 assert(abs(m1 - m2) <= eps) assert(abs(v1 - v2) <= eps) assert(abs(f1 - f2) <= eps) # Test passed print 'OK'
def test(): from scitbx.array_family import flex from dials.algorithms.image.filter import index_of_dispersion_filter # Create an image image = flex.random_double(2000 * 2000) image.reshape(flex.grid(2000, 2000)) mask = flex.random_bool(2000 * 2000, 0.99).as_int() mask.reshape(flex.grid(2000, 2000)) # Calculate the summed area table mask2 = mask.deep_copy() index_of_dispersion_filter = index_of_dispersion_filter( image, mask2, (3, 3), 2) mean = index_of_dispersion_filter.mean() var = index_of_dispersion_filter.sample_variance() index_of_dispersion = index_of_dispersion_filter.index_of_dispersion() # For a selection of random points, ensure that the value is the # sum of the area under the kernel eps = 1e-7 for i in range(10000): i = random.randint(10, 1990) j = random.randint(10, 1990) m1 = mean[j, i] v1 = var[j, i] f1 = index_of_dispersion[j, i] p = image[j - 3:j + 4, i - 3:i + 4] m = mask[j - 3:j + 4, i - 3:i + 4] if mask[j, i] == 0: m2 = 0.0 v2 = 0.0 f2 = 1.0 else: p = flex.select(p, flags=m) mv = flex.mean_and_variance(flex.double(p)) m2 = mv.mean() v2 = mv.unweighted_sample_variance() f2 = v2 / m2 assert m1 == pytest.approx(m2, abs=eps) assert v1 == pytest.approx(v2, abs=eps) assert f1 == pytest.approx(f2, abs=eps)
def tst_masked_mean_filter(self): from dials.algorithms.image.filter import mean_filter from scitbx.array_family import flex from random import randint # Create an image image = flex.random_double(2000 * 2000) image.reshape(flex.grid(2000, 2000)) mask = flex.random_bool(2000 * 2000, 0.99).as_int() mask.reshape(flex.grid(2000, 2000)) # Calculate the summed area table mask2 = mask.deep_copy() mean = mean_filter(image, mask2, (3, 3), 1) # For a selection of random points, ensure that the value is the # sum of the area under the kernel eps = 1e-7 for i in range(10000): i = randint(10, 1990) j = randint(10, 1990) m1 = mean[j,i] p = image[j-3:j+4,i-3:i+4] m = mask[j-3:j+4,i-3:i+4] if mask[j,i] == 0: m2 = 0.0 else: p = flex.select(p, flags=m) mv = flex.mean_and_variance(flex.double(p)) m2 = mv.mean() s1 = flex.sum(flex.double(p)) s2 = flex.sum(m.as_1d()) assert(abs(m1 - m2) <= eps) # Test passed print 'OK'
def random_permutation(s): from scitbx.array_family import flex return flex.select(s, flex.random_permutation(size=len(s)))
from dials.algorithms.background import IndexOfDispersionDiscriminator from dials.algorithms.background import normal_expected_n_sigma print normal_expected_n_sigma(30 * 30) discriminate_normal = NormalDiscriminator(n_sigma = normal_expected_n_sigma(30 * 30)) discriminate_poisson = IndexOfDispersionDiscriminator(n_sigma = 2) grid_i = flex.int(flex.grid(grid.all())) for i, g in enumerate(grid): grid_i[i] = int(g) mask_normal = discriminate_normal(grid_i) mask_poisson = discriminate_poisson(grid_i) normal_pixels = flex.select(grid, flags=(mask_normal == 1)) poisson_pixels = flex.select(grid, flags=(mask_poisson == 1)) all_pixels = list(grid) line = grid.as_numpy_array()[15,:] from matplotlib import pylab pylab.subplot2grid((3, 3), (0, 0)) pylab.hist(all_pixels, bins=20) pylab.subplot2grid((3, 3), (0, 1)) pylab.hist(normal_pixels, bins=20) pylab.subplot2grid((3, 3), (0, 2)) pylab.hist(poisson_pixels, bins=20) pylab.subplot2grid((3, 3), (1, 0), colspan=3) pylab.plot(line) pylab.subplot2grid((3, 3), (2, 0))