def estimate_deadlayer_from_energy_differences(): f, axarr = plt.subplots( 2 ) # lists to store all the energy difference measurements # and geometry measurements for 2 middle peaks moved_calibrated_energies = meas.meas.empty( (2,32,32 ) ) centered_calibrated_energies = meas.meas.empty( (2,32,32) ) calibrated_energy_array = [ centered_calibrated_energies, moved_calibrated_energies ] # construct the sec differences from geometry cosine_matrices = geom.get_cosine_matrices( debug=0 ) secant_differences = meas.meas.from_array( 1 / cosine_matrices[ 'pu_238_moved' ][ 0 ] - 1 / cosine_matrices[ 'pu_238_centered' ][ 0 ] ) for x in range( 32 ): print( x ) centered_mu = dbmgr.centered.get_mu_for_x_strip( x ) moved_mu = dbmgr.moved.get_mu_for_x_strip( x ) # secant_differences = meas.meas.from_list( # 1 / cosine_matrices[ 'pu_238_moved' ][ 0, x, : ] # - 1 / cosine_matrices[ 'pu_238_centered' ][ 0, x, : ] ) # do least squares fit on each pixel mu_vals_array = [ centered_mu, moved_mu ] for i in range( 2 ): for j in range( 32 ): if mu_vals_array[i][1][0][j].x == np.nan: for l in range(2): calibrated_energy_array[i][l][x][j] = meas.nan continue # construct arrays of mu values for features 0 and 2, ie the calibration # sources. mu_vals = [ mu_vals_array[i][k][l][j].x for k in [0,2] for l in [0,1] ] mu_deltas = [ mu_vals_array[i][k][l][j].dx for k in [0,2] for l in [0,1] ] if np.count_nonzero( ~np.isnan( mu_vals ) ) < 3: for l in range(2): calibrated_energy_array[i][l][x][j] = meas.nan continue # print( mu_vals ) # print( mu_deltas ) linear_fit = jstats.linear_calibration( flattened_peak_calibration_energies, mu_vals, mu_deltas, print_results = 0, invert = 1 ) if linear_fit is not None: m, b, f = linear_fit for l in range(2): calibrated_energy_array[i][l][x][j] = meas.meas( m.x * mu_vals_array[i][1][l][j].x + b.x, m.x * mu_vals_array[i][1][l][j].dx ) #energy_differences_flat[l].append( calibrated_energy ) else: for l in range(2): calibrated_energy_array[i][l][x][j] = meas.nan continue # f( mu_vals_array[i][1][l][j] ) # load interpolation # si_interpolation = stop.stopping_power_interpolation( 'si', [ 5.40, 5.55 ] ) energy_differences = centered_calibrated_energies - moved_calibrated_energies x = secant_differences.x.flatten() xerr = secant_differences.dx.flatten() for j in range( 2 ): y = energy_differences[j].x.flatten() yerr = energy_differences[j].dx.flatten() indices = ~ np.isnan( y ) energy_differences_mean = meas.meas( y, yerr ).nanmean( option = 'weighted' ) secant_differences_mean = meas.meas( x[ indices ], xerr[ indices ] ).nanmean( option = 'weighted' ) deadlayer = ( energy_differences_mean / ( secant_differences_mean * stopping_power_energies[i] ) ) print( 'deadlayer: ' + str( deadlayer ) ) print( 'energy_differences_mean: ' + str( energy_differences_mean ) ) print( 'secant_differences_mean: ' + str( secant_differences_mean ) ) # print( x[ ~np.isnan( y ) ] ) # print( y[ ~np.isnan( y ) ] ) jplt.plot( axarr[j], x, y, xerr = xerr, yerr = yerr, ylabel = r'$\Delta E$', xlabel = r'$\Delta \cos \theta$' ) axarr[j].text( 0.1, 0.9, 'Peak %d' % (j,), transform = axarr[j].transAxes, fontsize=12, verticalalignment='top' ) # jstats.linear_calibration( ax = axarr[j] ) plt.show() return 0
def estimate_deadlayer_from_energy_values(): f, axarr = plt.subplots( 2 ) # lists to store all the energy difference measurements # and geometry measurements for 2 middle peaks centered_calibrated_energies = meas.meas.empty( (2,32,32) ) moved_calibrated_energies = meas.meas.empty( (2,32,32 ) ) calibrated_energy_array = [ centered_calibrated_energies, moved_calibrated_energies ] # construct the sec differences from geometry all_cosine_matrices = geom.get_cosine_matrices() secant_matrices = [ 1 / meas.meas.from_array( all_cosine_matrices[i][0] ) for i in [ 'pu_238_centered', 'pu_238_moved' ] ] for x in range( 32 ): print( x ) centered_mu = dbmgr.centered.get_mu_for_x_strip( x ) moved_mu = dbmgr.moved.get_mu_for_x_strip( x ) # do least squares fit on each pixel mu_vals_array = [ centered_mu, moved_mu ] for i in range( 2 ): for y in range( 32 ): if meas.isnan( mu_vals_array[i][1][0][y] ) : for l in range(2): calibrated_energy_array[i][l][x][y] = meas.nan continue # construct arrays of mu values for features 0 and 2, ie the calibration # sources. mu_vals = [ mu_vals_array[i][k][l][y].x for k in [0,2] for l in [0,1] ] mu_deltas = [ mu_vals_array[i][k][l][y].dx for k in [0,2] for l in [0,1] ] if np.count_nonzero( ~np.isnan( mu_vals ) ) < 3: for l in range(2): calibrated_energy_array[i][l][x][y] = meas.nan continue linear_fit = jstats.linear_calibration( flattened_peak_calibration_energies, mu_vals, mu_deltas, print_results = 0, invert = 1 ) if linear_fit is not None: # print('success') m, b, f = linear_fit for l in range(2): # calibrated_energy_array[i][l][x][y] = m * mu_vals_array[i][1][l][y] + b energy = meas.meas( m.x * mu_vals_array[i][1][l][y].x + b.x, m.x * mu_vals_array[i][1][l][y].dx ) calibrated_energy_array[i][l][x][y] = energy else: for l in range(2): calibrated_energy_array[i][l][x][y] = meas.nan continue axarr[0].set_title( 'Ignoring left peak, ignoring source DL' ) x = np.concatenate( [ secant_matrices[i].x.flatten() for i in [0,1] ] ) xerr = np.concatenate( [ secant_matrices[i].dx.flatten() for i in [0,1] ] ) for j in range(2) : y = np.concatenate( [ calibrated_energy_array[i][j].x.flatten() for i in [0,1] ] ) yerr = np.concatenate( [ calibrated_energy_array[i][j].dx.flatten() for i in [0,1] ] ) jplt.plot( axarr[j], x, y, xerr = xerr, yerr = yerr, ylabel = r'$E$', xlabel = r'$\sec \theta$' ) axarr[j].text( 0.1, 0.9, 'Peak %d' % (j,), transform = axarr[j].transAxes, fontsize=12, verticalalignment='top' ) ret = jstats.linear_calibration( x, y, dy = yerr, ax = axarr[j] ) if ret is not None: deadlayer = ret[0] / stopping_power_energies[i] print( ret[2].fit_report() ) print( 'deadlayer: ' + str( deadlayer ) ) axarr[j].text( 0.1, 0.2, r'Effective DL = $ %d \pm %d $ nm ' % ( abs( deadlayer.x ) , deadlayer.dx ), transform = axarr[j].transAxes, fontsize=12, verticalalignment='top' ) print( '\n\n' ) plt.show() return 0
def linear_calibration(x, y, dy=None, dx=None, m_guess=None, b_guess=None, print_results=0, ax=None, invert=0, ignore_zero_dy=True, scatter=False, color='r', linestyle='-', leglabel=None): x = np.asarray(x) y = np.asarray(y) dy = np.asarray(dy) model = LinearModel() if m_guess is None: xmin = min(x) xmax = max(x) ymin = min(y) ymax = max(y) m_guess = (ymax - ymin) / (xmax - xmin) if b_guess is None: b_guess = (y[0] - m_guess * x[0]) model.make_params(m=m_guess, b=b_guess) # construct weights based on the inputs for dx and dy. if dy is not None: if dx is None: if ignore_zero_dy: weights = np.where(dy != 0, 1.0 / dy, np.nan) else: weights = 1.0 / np.asarray(dy) else: raise NotImplementedError() # weights = 1.0 / np.sqrt( else: if dx is None: weights = None else: raise NotImplementedError() mask = np.isnan(x) | np.isnan(y) if weights is not None: mask |= np.isnan(weights) num_nan_points = np.count_nonzero(mask) num_data_points = len(x) - num_nan_points if num_data_points < 3: return None # apply the least squares fit model_result = model.fit(y, x=x, weights=weights, nan_policy='omit') if print_results: print(model_result.fit_report()) # determine if we had a succussful fit. ier # is the return code of scipy.optimize.leastsq. # if this is 1, then lmfit thinks we succeeded. successful_fit = (model_result.success and (model_result.ier <= 4)) # and ( model_result.redchi < reduc_chisq_max ) ) if not successful_fit: return None mparam = model_result.params['slope'] bparam = model_result.params['intercept'] m = meas.meas(mparam.value, mparam.stderr) b = meas.meas(bparam.value, bparam.stderr) # error analysis built into these calculations via meas. if invert: b = -b / m m = 1 / m if ax is not None: graph_func = lambda _x: m.x * _x + b.x ax_xaxis = np.array([xmin, xmax]) ax.plot(ax_xaxis, graph_func(ax_xaxis), color=color, linestyle=linestyle, label=leglabel) return m, b, model_result # , lambda _x :
# status: unresolved import libjacob.jmeas as meas import numpy as np x = meas.meas( np.arange(4), np.zeros(4) ) y = np.array( [ 0.0, 1.0 ] ) # print( 1.0 - x ) # print( 1 - x ) # print( -x ) print( 'performing addition' ) print( x + y[0] ) print( 'performing addition' ) print( np.asscalar( y[0] ) + x ) print( 'performing addition' ) print( y[0] + x ) print( 'performing addition' ) print( 0.0 + x )
def get_all_peak_grids(self, read_from_file=1): # construct appropriately sized container for all the peak grids peak_grids = [[0] * self.num_peaks_per_feature[i] for i in range(self.num_features)] for i in range(self.num_features): peak_grids[i] = [meas.meas.empty(self.dimensions) ] * self.num_peaks_per_feature[i] # meas.meas.empty( self.num_peaks_per_fit + self.dimensions ) if read_from_file: peak_paths = [[[ self.peak_vals_dir + self.name + '_%d_%d_%s.bin' % (i, j, s) for s in ['x', 'dx'] ] for j in range(self.num_peaks_per_feature[i])] for i in range(self.num_features)] if all([ os.path.exists(path) for path in np.array(peak_paths).flatten() ]): for i in range(self.num_features): for j in range(self.num_peaks_per_feature[i]): peak = np.fromfile(peak_paths[i][j][0]).reshape( self.dimensions) peak_delta = np.fromfile(peak_paths[i][j][1]).reshape( self.dimensions) peak_grids[i][j] = meas.meas(peak, peak_delta) return peak_grids else: print( 'INFO: requested to read peak and peak_delta from files, but they aren\'t there. constructing them now...' ) # construct the array from the db if the files don't exist # yet, or we requested a fresh fetch. disconnect_conn_when_done = 0 if self.conn is None: self.connect() disconnect_conn_when_done = 1 # populate the grid for i in range(3): for j in range(2): peak_grids[i][j] = meas.meas.empty(self.dimensions) cursor = self.conn.cursor() cursor.execute('SELECT * FROM ' + _tablename) rownum = 0 total_iterations = (self.dimensions[0] * self.dimensions[1] * self.num_features) for row in cursor: rownum += 1 estimate_time_left(rownum, total_iterations, num_updates=100) x = row['x'] y = row['y'] feature = row['fit_id'] print((x, y, feature)) # if fit didn't converge then all the peak values for that # feature are assigned np.nan if not row['successful_fit']: for i in range(self.num_peaks_per_feature[feature]): peak_grids[feature][i][x, y] = meas.nan continue model = _from_bin(row['model']) # throw out the data if we didn't estimate errorbars. if not model.errorbars: for i in range(self.num_peaks_per_feature[feature]): peak_grids[feature][i][x, y] = meas.nan continue # do check on chi2. TODO: this should be removed and incorporated # in the fit itself. if 1 - chi2.cdf(model.chisqr, model.nfree) < 0.05: for i in range(self.num_peaks_per_feature[feature]): peak_grids[feature][i][x, y] = meas.nan continue # do a monte carlo simulation for each peak in this feature. peaks = spec.estimate_alpha_peakpos(model, plot=0) # check if the fit used the alternate shape if len(peaks) != self.num_peaks_per_feature[feature]: # todo: this doesn't work in the general case. # this should depend on the specified alternate shape. peaks = meas.append(meas.nan, peaks) # populate the corresponding entry of the grid. for i in range(self.num_peaks_per_feature[feature]): peak_grids[feature][i][x, y] = peaks[i] # reset the counter estimate_time_left(0, 0, reset=1) if disconnect_conn_when_done: self.disconnect() # write to a file if requested. note that we have already constructed # all the file names. if read_from_file: for i in range(self.num_features): for j in range(self.num_peaks_per_feature[i]): peak_grids[i][j].x.tofile(peak_paths[i][j][0]) peak_grids[i][j].dx.tofile(peak_paths[i][j][1]) return peak_grids
def get_all_mu_grids(self, read_from_file=0): # construct appropriately sized container for all the mu grids mu_grids = [[0] * self.num_peaks_per_feature[i] for i in range(self.num_features)] for i in range(self.num_features): mu_grids[i] = [meas.meas.empty(self.dimensions) ] * self.num_peaks_per_feature[i] # meas.meas.empty( self.num_peaks_per_fit + self.dimensions ) if read_from_file: mu_paths = [[[ self.mu_vals_dir + self.name + '_%d_%d_%s.bin' % (i, j, s) for s in ['x', 'dx'] ] for j in range(self.num_peaks_per_feature[i])] for i in range(self.num_features)] if all([ os.path.exists(path) for path in np.array(mu_paths).flatten() ]): for i in range(self.num_features): for j in range(self.num_peaks_per_feature[i]): mu = np.fromfile(mu_paths[i][j][0]).reshape( self.dimensions) mu_delta = np.fromfile(mu_paths[i][j][1]).reshape( self.dimensions) mu_grids[i][j] = meas.meas(mu, mu_delta) return mu_grids else: print( 'INFO: requested to read mu and mu_delta from files, but they aren\'t there. constructing them now...' ) # construct the array from the db if the files don't exist # yet, or we requested a fresh fetch. disconnect_conn_when_done = 0 if self.conn is None: self.connect() disconnect_conn_when_done = 1 # populate the grid for i in range(3): for j in range(2): mu_grids[i][j] = meas.meas.empty(self.dimensions) cursor = self.conn.cursor() cursor.execute('SELECT * FROM ' + _tablename) for row in cursor: x = row['x'] y = row['y'] feature = row['fit_id'] # if fit didn't converge then all the mu values for that # feature are assigned np.nan if not row['successful_fit']: for i in range(self.num_peaks_per_feature[feature]): mu_grids[feature][i][x, y] = meas.nan continue params = spec.get_alpha_params_dict(_from_bin(row['model'])) if not spec.alpha_model_valid_params_predicate(params): for i in range(self.num_peaks_per_feature[feature]): mu_grids[feature][i][x, y] = meas.nan continue mu = params['mu'] if len(mu) > 1: if (np.abs(mu[1].x - mu[0].x - 20) > 10 or any(mu.dx < 0.1)): for i in range(self.num_peaks_per_feature[feature]): mu_grids[feature][i][x, y] = meas.nan continue # check if the fit used the alternate shape if len(mu) != self.num_peaks_per_feature[feature]: # todo: this doesn't work in the general case. # this should depend on the specified alternate shape. mu = meas.append(meas.nan, mu) # populate the corresponding entry of the grid. for i in range(self.num_peaks_per_feature[feature]): mu_grids[feature][i][x, y] = mu[i] if disconnect_conn_when_done: self.disconnect() # write to a file if requested. note that we have already constructed # all the file names. if read_from_file: for i in range(self.num_features): for j in range(self.num_peaks_per_feature[i]): mu_grids[i][j].x.tofile(mu_paths[i][j][0]) mu_grids[i][j].dx.tofile(mu_paths[i][j][1]) return mu_grids
import libjacob.jmeas as meas import numpy as np x = np.array([[[meas.meas(2, 3)] * 3] * 2] * 3) print('before from_array: ') print(x) print('\n') y = meas.meas.from_array(x) print('after from_array: ') print(y)