def make_background(self): import copy if hasattr(self, "background"): return self.background # raw background data background = copy.deepcopy(self.raw_data).as_double() # mask out the signal areas mask = self.get_signal_mask() background.as_1d().set_selected(mask.as_1d(), 0.0) inv_mask = (~mask).as_1d().as_int() inv_mask.reshape(self.raw_data.accessor()) from dials.algorithms.image.filter import summed_area from dials.array_family import flex summed_background = summed_area(background, (5, 5)) summed_mask = summed_area(inv_mask, (5, 5)) mean_background = summed_background / summed_mask.as_double() background.as_1d().set_selected(mask.as_1d(), mean_background.as_1d()) self.background = background return background
def fit(data, mask, model_data, model_mask, kernel): from dials.array_family import flex from dials.algorithms.image.filter import summed_area mask = mask & model_mask mask = mask.as_1d().as_int() mask.reshape(data.accessor()) data = data * mask model_data = model_data * mask.as_double() summed_data = summed_area(data, kernel).as_1d() summed_mask = summed_area(mask, kernel).as_1d() summed_model = summed_area(model_data, kernel).as_1d() scaled_mask = (model_mask.as_1d() == True) & (summed_mask > 2) & (summed_model > 0) indices = flex.size_t(range(len(data))).select(scaled_mask) scaled_data = flex.double(len(data)) scaled_data.set_selected( indices, summed_data.select(indices).as_double() / summed_model.select(indices)) scaled_data.reshape(data.accessor()) scaled_mask.reshape(data.accessor()) #for j in range(scaled_data.all()[0]): # last_scale = None # for i in range(scaled_data.all()[1]): # if scaled_mask[j,i] == False: # if last_scale != None: # scaled_data[j,i] = last_scale # scaled_mask[j,i] = True # else: # last_scale = scaled_data[j,i] return scaled_data, scaled_mask
def fit(data, mask, model_data, model_mask, kernel): from dials.array_family import flex from dials.algorithms.image.filter import summed_area mask = mask & model_mask mask = mask.as_1d().as_int() mask.reshape(data.accessor()) data = data * mask model_data = model_data * mask.as_double() summed_data = summed_area(data, kernel).as_1d() summed_mask = summed_area(mask, kernel).as_1d() summed_model = summed_area(model_data, kernel).as_1d() scaled_mask = (model_mask.as_1d() == True) & (summed_mask > 2) & (summed_model > 0) indices = flex.size_t(range(len(data))).select(scaled_mask) scaled_data = flex.double(len(data)) scaled_data.set_selected( indices, summed_data.select(indices).as_double() / summed_model.select(indices)) scaled_data.reshape(data.accessor()) scaled_mask.reshape(data.accessor()) # for j in range(scaled_data.all()[0]): # last_scale = None # for i in range(scaled_data.all()[1]): # if scaled_mask[j,i] == False: # if last_scale != None: # scaled_data[j,i] = last_scale # scaled_mask[j,i] = True # else: # last_scale = scaled_data[j,i] return scaled_data, scaled_mask
def test(): from dials.algorithms.image.filter import summed_area from scitbx.array_family import flex # Create an image image = flex.random_double(2000 * 2000) image.reshape(flex.grid(2000, 2000)) # Calculate the summed area table sa = summed_area(image, (3, 3)) # For a selection of random points, ensure that the value is the # sum of the area under the kernel for i in range(10000): i = random.randint(10, 1990) j = random.randint(10, 1990) v = sa[j, i] e = flex.sum(image[j - 3:j + 4, i - 3:i + 4]) assert e == pytest.approx(v, abs=1e-7)
def run(self): from dials.algorithms.image.filter import summed_area 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)) # Calculate the summed area table sa = summed_area(image, (3, 3)) # 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) v = sa[j, i] e = flex.sum(image[j - 3:j + 4, i - 3:i + 4]) assert (abs(e - v) <= eps) # Test passed print 'OK'
def run(self): """Extract the shoeboxes.""" from dials.util.options import flatten_reflections from dials.util.options import flatten_experiments from dials.util.options import flatten_experiments from dials.util import log from dials.array_family import flex from dials.util import Sorry # Parse the command line params, options = self.parser.parse_args(show_diff_phil=False) # Configure logging log.config() # Log the diff phil diff_phil = self.parser.diff_phil.as_str() if diff_phil is not "": logger.info("The following parameters have been modified:\n") logger.info(diff_phil) # Get the data reflections = flatten_reflections(params.input.reflections) experiments = flatten_experiments(params.input.experiments) if not any([experiments, reflections]): self.parser.print_help() exit(0) elif len(experiments) > 1: raise Sorry("More than 1 experiment set") elif len(experiments) == 1: imageset = experiments[0].imageset if len(reflections) != 1: raise Sorry("Need 1 reflection table, got %d" % len(reflections)) else: reflections = reflections[0] # Check the reflections contain the necessary stuff assert "bbox" in reflections assert "panel" in reflections # Get some models detector = imageset.get_detector() scan = imageset.get_scan() frame0, frame1 = scan.get_array_range() # Add some padding but limit to image volume if params.padding > 0: logger.info("Adding %d pixels as padding" % params.padding) x0, x1, y0, y1, z0, z1 = reflections["bbox"].parts() x0 -= params.padding x1 += params.padding y0 -= params.padding y1 += params.padding panel = reflections["panel"] for i in range(len(reflections)): width, height = detector[panel[i]].get_image_size() if x0[i] < 0: x0[i] = 0 if x1[i] > width: x1[i] = width if y0[i] < 0: y0[i] = 0 if y1[i] > height: y1[i] = height reflections["bbox"] = flex.int6(x0, x1, y0, y1, z0, z1) # Now iterate through the images, masking the shoebox pixels as necessary, # then model the background & compute intensity statistics x0, x1, y0, y1, z0, z1 = reflections["bbox"].parts() for frame in range(frame0, frame1): data = imageset.get_raw_data(frame)[0] mask = data < 0 data = data.as_double() sel = (z0 <= frame) & (z1 >= frame) _x0 = x0.select(sel) _x1 = x1.select(sel) _y0 = y0.select(sel) _y1 = y1.select(sel) for __x0, __x1, __y0, __y1 in zip(_x0, _x1, _y0, _y1): for y in range(__y0, __y1): for x in range(__x0, __x1): data[y, x] = 0.0 mask[y, x] = True imask = (~mask).as_1d().as_int() imask.reshape(data.accessor()) from dials.algorithms.image.filter import summed_area # smooth over scale corresponding to max shoebox size d = max(flex.max(_x1 - _x0), flex.max(_y1 - _y0)) summed_background = summed_area(data, (d, d)) summed_mask = summed_area(imask, (d, d)) mean_background = summed_background / summed_mask.as_double() data.as_1d().set_selected(mask.as_1d(), mean_background.as_1d()) print(flex.sum(data.select(data.as_1d() > 0)))