def plot2(): fig5 = plt.figure() x = map(lambda x: x + gauss(0,0.02)*(1-x*x), arange(-1,1,0.001)) y = map(lambda x: x + gauss(0,0.02)*(1-x*x), arange(-1,1,0.001)) z = map(lambda x: x + gauss(0,0.02)*(1-x*x), arange(-1,1,0.001)) line = array(zip(x,y,z)) lpc = LPCImpl(h = 0.05, mult = 2, it = 200, cross = False, scaled = False, convergence_at = 0.001) lpc_curve = lpc.lpc(X=line) ax = Axes3D(fig5) ax.set_title('testNoisyLine2') curve = lpc_curve[0]['save_xd'] ax.scatter(x,y,z, c = 'red') ax.plot(curve[:,0],curve[:,1],curve[:,2]) saveToPdf(fig5, '/tmp/testNoisyLine2.pdf') residuals_calc = LPCResiduals(line, tube_radius = 0.05, k = 10) residual_diags = residuals_calc.getPathResidualDiags(lpc_curve[0]) fig6 = plt.figure() #plt.plot(lpc_curve[0]['lamb'][1:], residual_diags['line_seg_num_NN'], drawstyle = 'step', linestyle = '--') plt.plot(lpc_curve[0]['lamb'][1:], residual_diags['line_seg_mean_NN']) plt.plot(lpc_curve[0]['lamb'][1:], residual_diags['line_seg_std_NN']) saveToPdf(fig6, '/tmp/testNoisyLine2PathResiduals.pdf') coverage_graph = residuals_calc.getCoverageGraph(lpc_curve[0], arange(0.001, .102, 0.005)) fig7 = plt.figure() plt.plot(coverage_graph[0],coverage_graph[1]) saveToPdf(fig7, '/tmp/testNoisyLine2Coverage.pdf') residual_graph = residuals_calc.getGlobalResiduals(lpc_curve[0]) fig8 = plt.figure() plt.plot(residual_graph[0], residual_graph[1]) saveToPdf(fig8, '/tmp/testNoisyLine2Residuals.pdf') fig9 = plt.figure() plt.plot(range(len(lpc_curve[0]['lamb'])), lpc_curve[0]['lamb']) saveToPdf(fig9, '/tmp/testNoisyLine2PathLength.pdf')
def testDistanceBetweenCurves(self): l1 = {'save_xd': array([[0.5,1,0], [1.5,1,0]]), 'lamb':array([0.0, 1.0])} l2 = {'save_xd': array([[0,0,0], [1,0,0], [2,0,0]])} x = arange(-1,1,0.005) line = array(zip(x,x,x)) #not actually needed for calcualtion, but dummy argument to residuals_cal for now residuals_calc = LPCResiduals(line, tube_radius = 0.2) dist = residuals_calc._distanceBetweenCurves(l1,l2)
def helixHeteroscedasticDiags(): #Parameterise a helix (no noise) fig5 = plt.figure() t = arange(-1,1,0.0005) x = map(lambda x: x + gauss(0,0.001 + 0.001*sin(2*pi*x)**2), (1 - t*t)*sin(4*pi*t)) y = map(lambda x: x + gauss(0,0.001 + 0.001*sin(2*pi*x)**2), (1 - t*t)*cos(4*pi*t)) z = map(lambda x: x + gauss(0,0.001 + 0.001*sin(2*pi*x)**2), t) line = array(zip(x,y,z)) lpc = LPCImpl(h = 0.1, t0 = 0.1, mult = 1, it = 500, scaled = False, cross = False) lpc_curve = lpc.lpc(X=line) ax = Axes3D(fig5) ax.set_title('helixHeteroscedastic') curve = lpc_curve[0]['save_xd'] ax.scatter(x,y,z, c = 'red') ax.plot(curve[:,0],curve[:,1],curve[:,2]) saveToPdf(fig5, '/tmp/helixHeteroscedastic.pdf') residuals_calc = LPCResiduals(line, tube_radius = 0.2, k = 20) residual_diags = residuals_calc.getPathResidualDiags(lpc_curve[0]) fig6 = plt.figure() #plt.plot(lpc_curve[0]['lamb'][1:], residual_diags['line_seg_num_NN'], drawstyle = 'step', linestyle = '--') plt.plot(lpc_curve[0]['lamb'][1:], residual_diags['line_seg_mean_NN']) plt.plot(lpc_curve[0]['lamb'][1:], residual_diags['line_seg_std_NN']) saveToPdf(fig6, '/tmp/helixHeteroscedasticPathResiduals.pdf') coverage_graph = residuals_calc.getCoverageGraph(lpc_curve[0], arange(0.01, .052, 0.01)) fig7 = plt.figure() plt.plot(coverage_graph[0],coverage_graph[1]) saveToPdf(fig7, '/tmp/helixHeteroscedasticCoverage.pdf') residual_graph = residuals_calc.getGlobalResiduals(lpc_curve[0]) fig8 = plt.figure() plt.plot(residual_graph[0], residual_graph[1]) saveToPdf(fig8, '/tmp/helixHeteroscedasticResiduals.pdf') fig9 = plt.figure() plt.plot(range(len(lpc_curve[0]['lamb'])), lpc_curve[0]['lamb']) saveToPdf(fig9, '/tmp/helixHeteroscedasticPathLength.pdf')
def testResiduals(self): x = map(lambda x: x + gauss(0,0.005 + 0.3*x*x), arange(-1,1,0.005)) y = map(lambda x: x + gauss(0,0.005 + 0.3*x*x), arange(-1,1,0.005)) z = map(lambda x: x + gauss(0,0.005 + 0.3*x*x), arange(-1,1,0.005)) line = array(zip(x,y,z)) lpc = LPCImpl(h = 0.05, convergence_at = 0.0001, it = 100, mult = 2) lpc_curve = lpc.lpc(X=line) residuals_calc = LPCResiduals(line, tube_radius = 1) residuals_graph = residuals_calc.getGlobalResiduals(lpc_curve[0])
def testNoisyLine1Residuals(self): x = map(lambda x: x + gauss(0,0.005), arange(-1,1,0.005)) y = map(lambda x: x + gauss(0,0.005), arange(-1,1,0.005)) z = map(lambda x: x + gauss(0,0.005), arange(-1,1,0.005)) line = array(zip(x,y,z)) lpc = LPCImpl(h = 0.2, convergence_at = 0.0005, it = 500, mult = 2) lpc_curve = lpc.lpc(X=line) residuals_calc = LPCResiduals(line, tube_radius = 0.1) residual_diags = residuals_calc.getPathResidualDiags(lpc_curve[0])
class lpcAnalyser(object): def __init__(self, filename): ''' Constructor ''' self._parser = lpcAnalysisParser(filename) self._initReader() self._initResiduals() self._initPruner() self._setOutputFilename() self._initPurity() def _initReader(self): run_parameters = self._parser.getReadParameters() if run_parameters['type'] == 'lpcAnalysisPickleReader': self._reader = lpcAnalysisPickleReader(**run_parameters['params']) else: raise ValueError, 'Specified type of reader is not recognised' def _initResiduals(self): residual_parameters = self._parser.getResidualsParameters() if residual_parameters['type'] == 'LPCResiduals': tau_range = residual_parameters['params'].pop('tau_range') self._residuals = LPCResiduals(**residual_parameters['params']) self._residuals_runner = LPCResidualsRunner(self._residuals, tau_range) else: raise ValueError, 'Specified type of residuals calculator is not recognised' def _initPruner(self): pruner_parameters = self._parser.getPrunerParameters() if pruner_parameters['type'] == 'lpcCurvePruner': self._pruner = lpcCurvePruner(self._residuals_runner, **pruner_parameters['params']) else: raise ValueError, 'Specified type of residuals calculator is not recognised' def _setOutputFilename(self): misc = self._parser.getMiscParameters() self._output_filename = misc['params']['output_filename'] def _initPurity(self): self._purity = LPCPurityCalculator(self._residuals_runner) def runAnalyser(self): out_data = [] while 1: try: evt = self._reader.getNextEvent() self._residuals.setDataPoints(evt[0]['Xi']) self._residuals_runner.setLpcCurves(evt[0]['lpc_curve']) self._residuals_runner.calculateResiduals(calc_residuals = False, calc_containment_matrix = False) remaining_curves = self._pruner.pruneCurves() #muon_proton_hits = truth_evt.getParticleHits([13, 2212]) #eff = LPCEfficiencyCalculator(remaining_curves, evt['data_range'], muon_proton_hits, tau) TODO - move from examples/toytracks voxel_to_pdg_dictionary = evt[1].getParticlesInVoxelDict() pur = self._purity.calculatePurity(remaining_curves, evt[0]['data_range'], voxel_to_pdg_dictionary) out_data.append({'voxel_dict': voxel_to_pdg_dictionary, 'pur': pur}) except EOFError: break #TODO - factor this out into a class which enters the analysis configuration file outfile = open(self._output_filename, 'w') cPickle.dump(out_data, outfile, -1)
def testNoisyLine2Residuals(self): #contains data that gets more scattered at each end of the line x = map(lambda x: x + gauss(0,0.005 + 0.3*x*x), arange(-1,1,0.005)) y = map(lambda x: x + gauss(0,0.005 + 0.3*x*x), arange(-1,1,0.005)) z = map(lambda x: x + gauss(0,0.005 + 0.3*x*x), arange(-1,1,0.005)) line = array(zip(x,y,z)) lpc = LPCImpl(h = 0.05, convergence_at = 0.001, it = 100, mult = 2) lpc_curve = lpc.lpc(X=line) residuals_calc = LPCResiduals(line, tube_radius = 1) residual_diags = residuals_calc.getPathResidualDiags(lpc_curve[0])
def _initResiduals(self): residual_parameters = self._parser.getResidualsParameters() if residual_parameters['type'] == 'LPCResiduals': tau_range = residual_parameters['params'].pop('tau_range') self._residuals = LPCResiduals(**residual_parameters['params']) self._residuals_runner = LPCResidualsRunner(self._residuals, tau_range) else: raise ValueError, 'Specified type of residuals calculator is not recognised'
def LPCEfficiencyCalculator(curves, data_range, particle_hits, tau): #first rescale the curves then calculate the proton and muon efficiency if data_range is None: data_range = 1.0 muon_hits = array([[h[0][0], h[0][1], h[0][2]] for h in particle_hits[13]]) / data_range proton_hits = array([[h[0][0], h[0][1], h[0][2]] for h in particle_hits[2212]]) / data_range residuals_calc = LPCResiduals(muon_hits, tube_radius = 0.12) residuals_runner = LPCResidualsRunner(curves, residuals_calc) residuals_runner.setTauRange([tau]) muon_residuals = residuals_runner.calculateResiduals(True, False, False) muon_coverage = max([len(muon_residuals['curve_residuals'][i]['coverage_indices'][tau]) for i in range(len(curves))]) muon_efficiency = float(muon_coverage) / len(muon_hits) residuals_calc.setDataPoints(proton_hits) proton_residuals = residuals_runner.calculateResiduals(True, False, False) proton_coverage = max([len(proton_residuals['curve_residuals'][i]['coverage_indices'][tau]) for i in range(len(curves))]) proton_efficiency = float(proton_coverage) / len(proton_hits) print 'residuals'