def __init__(self, address, pickle_dirname=".", pickle_basename="hist", roi=None, hist_min=None, hist_max=None, n_slots=None, **kwds): """ @param address Address string XXX Que?! @param pickle_dirname Directory portion of output pickle file XXX mean, mu? @param pickle_basename Filename prefix of output pickle file image XXX mean, mu? @param calib_dir Directory with calibration information @param dark_path Path to input dark image @param dark_stddev Path to input dark standard deviation """ super(pixel_histograms, self).__init__(address=address, **kwds) self.pickle_dirname = cspad_tbx.getOptString(pickle_dirname) self.pickle_basename = cspad_tbx.getOptString(pickle_basename) self.hist_min = cspad_tbx.getOptFloat(hist_min) self.hist_max = cspad_tbx.getOptFloat(hist_max) self.n_slots = cspad_tbx.getOptInteger(n_slots) self.histograms = {} self.dimensions = None self.roi = cspad_tbx.getOptROI(roi) self.values = flex.long() self.sigma_scaling = False if self.hist_min is None: self.hist_min = -50 if self.hist_max is None: self.hist_max = 150 if self.n_slots is None: self.n_slots = 200
def __init__(self, address, pickle_dirname=".", pickle_basename="hist", roi=None, hist_min=None, hist_max=None, n_slots=None, **kwds): """ @param address Address string XXX Que?! @param pickle_dirname Directory portion of output pickle file XXX mean, mu? @param pickle_basename Filename prefix of output pickle file image XXX mean, mu? @param calib_dir Directory with calibration information @param dark_path Path to input dark image @param dark_stddev Path to input dark standard deviation """ super(pixel_histograms, self).__init__( address=address, **kwds ) self.pickle_dirname = cspad_tbx.getOptString(pickle_dirname) self.pickle_basename = cspad_tbx.getOptString(pickle_basename) self.hist_min = cspad_tbx.getOptFloat(hist_min) self.hist_max = cspad_tbx.getOptFloat(hist_max) self.n_slots = cspad_tbx.getOptInteger(n_slots) self.histograms = {} self.dimensions = None self.roi = cspad_tbx.getOptROI(roi) self.values = flex.long() self.sigma_scaling = False if self.hist_min is None: self.hist_min = -50 if self.hist_max is None: self.hist_max = 150 if self.n_slots is None: self.n_slots = 200
def run(self, flags, sweep=None, observations=None, **kwargs): obs_x, obs_y = observations.centroids().px_position_xy().parts() import numpy as np H, xedges, yedges = np.histogram2d( obs_x.as_numpy_array(), obs_y.as_numpy_array(),bins=self.nbins) from scitbx.array_family import flex H_flex = flex.double(H.flatten().astype(np.float64)) n_slots = min(int(flex.max(H_flex)), 30) hist = flex.histogram(H_flex, n_slots=n_slots) slots = hist.slots() cumulative_hist = flex.long(len(slots)) for i in range(len(slots)): cumulative_hist[i] = slots[i] if i > 0: cumulative_hist[i] += cumulative_hist[i-1] cumulative_hist = cumulative_hist.as_double()/flex.max( cumulative_hist.as_double()) cutoff = None gradients = flex.double() for i in range(len(slots)-1): x1 = cumulative_hist[i] x2 = cumulative_hist[i+1] g = (x2 - x1)/hist.slot_width() gradients.append(g) if (cutoff is None and i > 0 and g < self.gradient_cutoff and gradients[i-1] < self.gradient_cutoff): cutoff = hist.slot_centers()[i-1]-0.5*hist.slot_width() H_flex = flex.double(np.ascontiguousarray(H)) isel = (H_flex > cutoff).iselection() sel = np.column_stack(np.where(H > cutoff)) for (ix, iy) in sel: flags.set_selected( ((obs_x > xedges[ix]) & (obs_x < xedges[ix+1]) & (obs_y > yedges[iy]) & (obs_y < yedges[iy+1])), False) if 0: from matplotlib import pyplot fig, ax1 = pyplot.subplots() extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]] plot1 = ax1.imshow(H, extent=extent, interpolation="nearest") pyplot.xlim((0, pyplot.xlim()[1])) pyplot.ylim((0, pyplot.ylim()[1])) pyplot.gca().invert_yaxis() cbar1 = pyplot.colorbar(plot1) pyplot.axes().set_aspect('equal') pyplot.show() fig, ax1 = pyplot.subplots() ax2 = ax1.twinx() ax1.scatter(hist.slot_centers()-0.5*hist.slot_width(), cumulative_hist) ax1.set_ylim(0, 1) ax2.plot(hist.slot_centers()[:-1]-0.5*hist.slot_width(), gradients) ymin, ymax = pyplot.ylim() pyplot.vlines(cutoff, ymin, ymax, color='r') pyplot.show() H2 = H.copy() if cutoff is not None: H2[np.where(H2 >= cutoff)] = 0 fig, ax1 = pyplot.subplots() plot1 = ax1.pcolormesh(xedges, yedges, H2) pyplot.xlim((0, pyplot.xlim()[1])) pyplot.ylim((0, pyplot.ylim()[1])) pyplot.gca().invert_yaxis() cbar1 = pyplot.colorbar(plot1) pyplot.axes().set_aspect('equal') pyplot.show() return flags
def run(self, flags, sweep=None, observations=None, **kwargs): obs_x, obs_y = observations.centroids().px_position_xy().parts() import numpy as np H, xedges, yedges = np.histogram2d(obs_x.as_numpy_array(), obs_y.as_numpy_array(), bins=self.nbins) from scitbx.array_family import flex H_flex = flex.double(H.flatten().astype(np.float64)) n_slots = min(int(flex.max(H_flex)), 30) hist = flex.histogram(H_flex, n_slots=n_slots) slots = hist.slots() cumulative_hist = flex.long(len(slots)) for i in range(len(slots)): cumulative_hist[i] = slots[i] if i > 0: cumulative_hist[i] += cumulative_hist[i - 1] cumulative_hist = cumulative_hist.as_double() / flex.max( cumulative_hist.as_double()) cutoff = None gradients = flex.double() for i in range(len(slots) - 1): x1 = cumulative_hist[i] x2 = cumulative_hist[i + 1] g = (x2 - x1) / hist.slot_width() gradients.append(g) if (cutoff is None and i > 0 and g < self.gradient_cutoff and gradients[i - 1] < self.gradient_cutoff): cutoff = hist.slot_centers()[i - 1] - 0.5 * hist.slot_width() H_flex = flex.double(np.ascontiguousarray(H)) isel = (H_flex > cutoff).iselection() sel = np.column_stack(np.where(H > cutoff)) for (ix, iy) in sel: flags.set_selected( ((obs_x > xedges[ix]) & (obs_x < xedges[ix + 1]) & (obs_y > yedges[iy]) & (obs_y < yedges[iy + 1])), False) if 0: from matplotlib import pyplot fig, ax1 = pyplot.subplots() extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]] plot1 = ax1.imshow(H, extent=extent, interpolation="nearest") pyplot.xlim((0, pyplot.xlim()[1])) pyplot.ylim((0, pyplot.ylim()[1])) pyplot.gca().invert_yaxis() cbar1 = pyplot.colorbar(plot1) pyplot.axes().set_aspect('equal') pyplot.show() fig, ax1 = pyplot.subplots() ax2 = ax1.twinx() ax1.scatter(hist.slot_centers() - 0.5 * hist.slot_width(), cumulative_hist) ax1.set_ylim(0, 1) ax2.plot(hist.slot_centers()[:-1] - 0.5 * hist.slot_width(), gradients) ymin, ymax = pyplot.ylim() pyplot.vlines(cutoff, ymin, ymax, color='r') pyplot.show() H2 = H.copy() if cutoff is not None: H2[np.where(H2 >= cutoff)] = 0 fig, ax1 = pyplot.subplots() plot1 = ax1.pcolormesh(xedges, yedges, H2) pyplot.xlim((0, pyplot.xlim()[1])) pyplot.ylim((0, pyplot.ylim()[1])) pyplot.gca().invert_yaxis() cbar1 = pyplot.colorbar(plot1) pyplot.axes().set_aspect('equal') pyplot.show() return flags