def __call__(self, raw_image, return_maxima_locations=False): """ Takes a raw_image and produces an optimized CSPad geometry. """ self.optimize_geometry(raw_image) if return_maxima_locations: if self.use_filter: raw_image = utils.preprocess_image(raw_image, threshold=self.threshold, sigma=self.sigma, minf_size=self.minf_size, rank_size=self.rank_size, sobel=self.sobel) assembled_image = self.cspad(raw_image) bc, bv = self._bin_intensities_by_radius(self.beam_location, assembled_image) maxima_locations = self._maxima_indices(bv) return self.cspad, maxima_locations else: return self.cspad
def optimize_geometry(self, raw_image): """ Find the center of `image`, which contains one or more concentric rings. Parameters ---------- raw_image : ndarray The image, in pyana/psana's raw format. use_edge_mask : bool Whether or not to apply an edge mask noise filter before finding the center. Highly recommended. Returns ------- params_dict : dict Dict of the pyana parameters used to optimize the geometry. """ print "" print "Beginning optimization..." print "Optimizing:", self.params_to_optimize print "Objective function: %s" % self.objective_type print "" initial_guesses = np.concatenate([ self.cspad.get_param(p).flatten() \ for p in self.params_to_optimize ]) # check to make sure the parameters are more or less on the same abs # scale -- this can help ensure the optimizer works as advertised if ((np.abs(initial_guesses).std() / np.abs(initial_guesses).mean()) > 5.0): print "\nWARNING:" print " There is a large variation in the magnitudes of the intial" print " parameters passed to the optimization routine. Consider an" print " appropriate scaling to equilize the scale of each." print "Parameters:", initial_guesses print "" # turn on interactive plotting -- this is the only way I've gotten it # to work if self.plot_each_iteration: plt.ion() self._fig = plt.figure(figsize=(12, 6)) self._axL = self._fig.add_subplot(121) self._axL.set_aspect('equal') self._axR = self._fig.add_subplot(122) if self.use_filter: image = utils.preprocess_image(raw_image, threshold=self.threshold, sigma=self.sigma, minf_size=self.minf_size, rank_size=self.rank_size, sobel=self.sobel) else: image = raw_image # benchmark our starting condition init_objective = float(self._objective(initial_guesses, image)) time0 = time.clock() # run minimization -- downhill simplex opt_params = optimize.fmin_powell(self._objective, initial_guesses, args=(image, ), xtol=1e-3, ftol=1e-3, disp=0) # un-ravel & inject the param values in the CSPad object param_dict = self._unravel_params(opt_params) self.cspad.set_many_params(param_dict.keys(), param_dict.values()) # print some diagnostics final_objective = self._objective(opt_params, image) print "" print "Optimization terminated normally" print "--------------------------------" print "Final objective value: %.4e" % final_objective print "Objective function change: %.4e" % (final_objective - init_objective) print "Time to reach solution: %.2g min" % ( (time.clock() - time0) / 60.0) #print "Final parameters:" #pprint(param_dict) print "" # turn off interactive plotting if self.plot_each_iteration: plt.ioff() return
def optimize_geometry(self, raw_image): """ Find the center of `image`, which contains one or more concentric rings. Parameters ---------- raw_image : ndarray The image, in pyana/psana's raw format. use_edge_mask : bool Whether or not to apply an edge mask noise filter before finding the center. Highly recommended. Returns ------- params_dict : dict Dict of the pyana parameters used to optimize the geometry. """ print "" print "Beginning optimization..." print "Optimizing:", self.params_to_optimize print "Objective function: %s" % self.objective_type print "" initial_guesses = np.concatenate([ self.cspad.get_param(p).flatten() \ for p in self.params_to_optimize ]) # check to make sure the parameters are more or less on the same abs # scale -- this can help ensure the optimizer works as advertised if ( ( np.abs(initial_guesses).std() / np.abs(initial_guesses).mean()) > 5.0): print "\nWARNING:" print " There is a large variation in the magnitudes of the intial" print " parameters passed to the optimization routine. Consider an" print " appropriate scaling to equilize the scale of each." print "Parameters:", initial_guesses print "" # turn on interactive plotting -- this is the only way I've gotten it # to work if self.plot_each_iteration: plt.ion() self._fig = plt.figure(figsize=(12,6)) self._axL = self._fig.add_subplot(121) self._axL.set_aspect('equal') self._axR = self._fig.add_subplot(122) if self.use_filter: image = utils.preprocess_image(raw_image, threshold=self.threshold, sigma=self.sigma, minf_size=self.minf_size, rank_size=self.rank_size, sobel=self.sobel) else: image = raw_image # benchmark our starting condition init_objective = float( self._objective(initial_guesses, image) ) time0 = time.clock() # run minimization -- downhill simplex opt_params = optimize.fmin_powell(self._objective, initial_guesses, args=(image,), xtol=1e-3, ftol=1e-3, disp=0) # un-ravel & inject the param values in the CSPad object param_dict = self._unravel_params(opt_params) self.cspad.set_many_params(param_dict.keys(), param_dict.values()) # print some diagnostics final_objective = self._objective(opt_params, image) print "" print "Optimization terminated normally" print "--------------------------------" print "Final objective value: %.4e" % final_objective print "Objective function change: %.4e" % (final_objective - init_objective) print "Time to reach solution: %.2g min" % ((time.clock()-time0) / 60.0) #print "Final parameters:" #pprint(param_dict) print "" # turn off interactive plotting if self.plot_each_iteration: plt.ioff() return