#!/usr/bin/local/python import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as plt_patches from pypad import cspad from pypad import utils from pypad import read from pypad import plot cs1 = cspad.CSPad.load("examples/my_cspad.cspad") cs2 = cspad.CSPad.default() cs3 = cspad.CSPad.default() cs3.quad_rotation[0] = 10. cs3.quad_rotation[1] = 0. cs3.quad_rotation[2] = 2. cs3.quad_rotation[3] = -10. raw_image = read.load_raw_image("examples/gold-minus490mm.h5") fig = plt.figure() ax = plt.subplot(121) plot.imshow_cspad(cs3(raw_image), ax=ax, scrollable=True) ax = plt.subplot(122) plot.sketch_2x1s(cs3.pixel_positions, ax) plt.show()
def _objective(self, param_vals, raw_image): """ The objective function for finding a good center. Minimize this. Parameters ---------- param_vals : ndarray A flat array of the parameters we're optimizing. Which parameters these are depends on the entires in `list_of_params`. list_of_params : list of str A list of which parameters are contained in `params_to_opt`. Returns ------- obj : float The value of the function. Lower is better. See Also -------- self._inject_params_into_dict : function Makes sense of `params_to_opt` and `list_of_params` """ # un-ravel & inject the param values in the geometry object param_dict = self._unravel_params(param_vals) self.cspad.set_many_params(param_dict.keys(), param_dict.values()) # compute the radial profile bc, bv = self.cspad.intensity_profile(raw_image, n_bins=None) if self.radius_range == None: bin_centers, bin_values = bc, bv else: bin_centers, bin_values = self._slice(bc, bv) # if plotting is requested, plot away! if self.plot_each_iteration: self._axL.cla() self._axR.cla() plot.sketch_2x1s(self.cspad.pixel_positions, self._axL) self._axR.plot(bin_centers, bin_values, lw=2, color='k') self._axR.set_xlabel('Radius (mm)') self._axR.set_ylabel('Intensity (arb. units)') self._axR.set_ylim((0, bin_values.max() * 1.1)) plt.draw() # --------- HERE IS THE OBJECTIVE FUNCTION -- MODIFY TO PLAY ----------- # test to see if any ASICS are overlapping, and if they are return a big # number so the optimizer avoids those regions if self.cspad.do_asics_overlap: print "Move caused ASIC overlap: rejecting it" return 1.0e300 # new objective function : overlap integral if self.objective_type == 'overlap': bins = len(bin_centers) quad_profiles = [] for i in range(4): bc, bv = self.cspad.intensity_profile(raw_image, n_bins=bins, quad=i) quad_profiles.append(bv) m = np.min([a.shape for a in quad_profiles]) quad_profiles = np.array([a[:m] for a in quad_profiles]) obj = -np.sum(np.product(quad_profiles, axis=0)) # old objective function : peak height elif self.objective_type == 'peak_height': obj = -bin_values.max() if self.width_weight != 0.0: obj += self.width_weight * self._simple_width( bin_values, bar=self.horizontal_cut) if self.peak_weight != 0.0: obj += self.peak_weight else: raise ValueError('No implemented objective_type: %s' % objective_type) # ---------------------------------------------------------------------- print "objective value: %.4e" % obj return obj
def _objective(self, param_vals, raw_image): """ The objective function for finding a good center. Minimize this. Parameters ---------- param_vals : ndarray A flat array of the parameters we're optimizing. Which parameters these are depends on the entires in `list_of_params`. list_of_params : list of str A list of which parameters are contained in `params_to_opt`. Returns ------- obj : float The value of the function. Lower is better. See Also -------- self._inject_params_into_dict : function Makes sense of `params_to_opt` and `list_of_params` """ # un-ravel & inject the param values in the geometry object param_dict = self._unravel_params(param_vals) self.cspad.set_many_params(param_dict.keys(), param_dict.values()) # compute the radial profile bc, bv = self.cspad.intensity_profile(raw_image, n_bins=None) if self.radius_range == None: bin_centers, bin_values = bc, bv else: bin_centers, bin_values = self._slice(bc, bv) # if plotting is requested, plot away! if self.plot_each_iteration: self._axL.cla() self._axR.cla() plot.sketch_2x1s(self.cspad.pixel_positions, self._axL) self._axR.plot(bin_centers, bin_values, lw=2, color='k') self._axR.set_xlabel('Radius (mm)') self._axR.set_ylabel('Intensity (arb. units)') self._axR.set_ylim((0, bin_values.max()*1.1)) plt.draw() # --------- HERE IS THE OBJECTIVE FUNCTION -- MODIFY TO PLAY ----------- # test to see if any ASICS are overlapping, and if they are return a big # number so the optimizer avoids those regions if self.cspad.do_asics_overlap: print "Move caused ASIC overlap: rejecting it" return 1.0e300 # new objective function : overlap integral if self.objective_type == 'overlap': bins = len(bin_centers) quad_profiles = [] for i in range(4): bc, bv = self.cspad.intensity_profile(raw_image, n_bins=bins, quad=i) quad_profiles.append(bv) m = np.min([ a.shape for a in quad_profiles ]) quad_profiles = np.array([ a[:m] for a in quad_profiles ]) obj = - np.sum(np.product(quad_profiles, axis=0)) # old objective function : peak height elif self.objective_type == 'peak_height': obj = - bin_values.max() if self.width_weight != 0.0: obj += self.width_weight * self._simple_width(bin_values, bar=self.horizontal_cut) if self.peak_weight != 0.0: obj += self.peak_weight else: raise ValueError('No implemented objective_type: %s' % objective_type) # ---------------------------------------------------------------------- print "objective value: %.4e" % obj return obj