def process_frame(frame, gcdfile='/cvmfs/icecube.opensciencegrid.org/data/GCD/GeoCalibDetectorStatus_2013.56429_V0.i3.gz', charge_scale=1.0, time_scale=1e-3): """ Processes a frame to create an event graph and metadata out of it. Parameters: ----------- frame : I3Frame The data frame to extract an event graph with features from. gcd_file : str Path to the gcd file. charge_scale : float The normalization constant for charge. time_scale : float The normalization constant for time. """ ### Meta data of the event for analysis of the classifier and creation of ground truth primary = dataclasses.get_most_energetic_neutrino(frame['I3MCTree']) if primary is None: get_weighted_primary(frame, MCPrimary='MCPrimary') primary = frame['MCPrimary'] # Obtain the PDG Encoding for ground truth #frame['PDGEncoding'] = dataclasses.I3Double(primary.pdg_encoding) #frame['InteractionType'] = dataclasses.I3Double(frame['I3MCWeightDict']['InteractionType']) frame['RunID'] = icetray.I3Int(frame['I3EventHeader'].run_id) frame['EventID'] = icetray.I3Int(frame['I3EventHeader'].event_id) frame['PrimaryEnergy'] = dataclasses.I3Double(primary.energy) ### Create features for each event features, coordinates, _ = get_events_from_frame(frame, charge_scale=charge_scale, time_scale=time_scale) for feature_name in vertex_features: frame[feature_name] = dataclasses.I3VectorFloat(features[feature_name]) ### Create offset lookups for the flattened feature arrays per event frame['NumberVertices'] = icetray.I3Int(len(features[features.keys()[0]])) ### Create coordinates for each vertex C = np.vstack(coordinates.values()).T #C, C_mean, C_std = normalize_coordinates(C, weights=None, copy=True) #C_cog, C_mean_cog, C_std_cog = normalize_coordinates(C, weights=features['TotalCharge'], copy=True) frame['VertexX'] = dataclasses.I3VectorFloat(C[:, 0]) frame['VertexY'] = dataclasses.I3VectorFloat(C[:, 1]) frame['VertexZ'] = dataclasses.I3VectorFloat(C[:, 2]) ### Output centering and true debug information frame['PrimaryX'] = dataclasses.I3Double(primary.pos.x) frame['PrimaryY'] = dataclasses.I3Double(primary.pos.y) frame['PrimaryZ'] = dataclasses.I3Double(primary.pos.z) frame['PrimaryAzimuth'] = dataclasses.I3Double(primary.dir.azimuth) frame['PrimaryZenith'] = dataclasses.I3Double(primary.dir.zenith) ### Apply labeling classify_wrapper(frame, None, gcdfile=gcdfile) return True
def add_hole_ice_to_geometry_frame(frame, positions=[], radii=[], scattering_lengths=[], absorption_lengths=[]): positions = (dataclasses.I3Position(pos[0], pos[1], pos[2]) for pos in positions) frame.Put("HoleIceCylinderPositions", dataclasses.I3VectorI3Position(positions)) frame.Put("HoleIceCylinderRadii", dataclasses.I3VectorFloat(radii)) frame.Put("HoleIceCylinderScatteringLengths", dataclasses.I3VectorFloat(scattering_lengths)) frame.Put("HoleIceCylinderAbsorptionLengths", dataclasses.I3VectorFloat(absorption_lengths))
def applyOpenCLWlenDependentFunction(xValues, functionOpenCL, getDerivative=False, useReferenceFunction=False): #print " number of values:", len(xValues) tester = clsim.I3CLSimFunctionTester( device=openCLDevice, workgroupSize=workgroupSize, workItemsPerIteration=workItemsPerIteration, wlenDependentValue=functionOpenCL) #print "maxWorkgroupSizeForKernel:", tester.maxWorkgroupSize # the function currently only accepts I3VectorFloat as its input type vector = dataclasses.I3VectorFloat( numpy.array(xValues) * I3Units.nanometer) if useReferenceFunction: if getDerivative: yValues = numpy.array(tester.EvaluateReferenceDerivative( vector)) / (1. / I3Units.nanometer) else: yValues = numpy.array(tester.EvaluateReferenceFunction(vector)) else: if getDerivative: yValues = numpy.array( tester.EvaluateDerivative(vector)) / (1. / I3Units.nanometer) else: yValues = numpy.array(tester.EvaluateFunction(vector)) return yValues
def applyOpenCLMediumPropertyFunction(xValues, layer, mediumProps, mode): tester = clsim.I3CLSimMediumPropertiesTester( device=openCLDevice, workgroupSize=workgroupSize, workItemsPerIteration=workItemsPerIteration, mediumProperties=mediumProps, randomService=None) vector = dataclasses.I3VectorFloat( numpy.array(xValues) * I3Units.nanometer) if mode == "phaseRefIndex": yValues = numpy.array(tester.EvaluatePhaseRefIndex(vector, layer)) elif mode == "dispersion": yValues = numpy.array(tester.EvaluateDispersion(vector, layer)) elif mode == "groupVelocity": yValues = numpy.array(tester.EvaluateGroupVelocity(vector, layer)) elif mode == "absorptionLength": yValues = numpy.array(tester.EvaluateAbsorptionLength(vector, layer)) elif mode == "scatteringLength": yValues = numpy.array(tester.EvaluateScatteringLength(vector, layer)) else: raise RuntimeError("Mode \"%s\" is not valid." % mode) return yValues
def evaluateScalarFieldOpenCL(xValues, yValues, zValues, scalarField, useReferenceFunction=False): tester = clsim.I3CLSimScalarFieldTester( device=openCLDevice, workgroupSize=workgroupSize, workItemsPerIteration=workItemsPerIteration, scalarField=scalarField) # the function currently only accepts I3VectorFloat as its input type vectorX = dataclasses.I3VectorFloat(numpy.array(xValues)) vectorY = dataclasses.I3VectorFloat(numpy.array(yValues)) vectorZ = dataclasses.I3VectorFloat(numpy.array(zValues)) if useReferenceFunction: yValues = tester.EvaluateReferenceFunction(vectorX, vectorY, vectorZ) else: yValues = tester.EvaluateFunction(vectorX, vectorY, vectorZ) return numpy.array(yValues)
def create_hole_ice_gcd_file(input_gcd, output_gcd, hole_ice_cylinders): hole_ice_cylinder_positions = dataclasses.I3VectorI3Position() hole_ice_cylinder_radii = dataclasses.I3VectorFloat() hole_ice_cylinder_scattering_lengths = dataclasses.I3VectorFloat() hole_ice_cylinder_absorption_lengths = dataclasses.I3VectorFloat() if len(hole_ice_cylinders) > 0: hole_ice_cylinder_positions = dataclasses.I3VectorI3Position( [cylinder["position"] for cylinder in hole_ice_cylinders]) hole_ice_cylinder_radii = dataclasses.I3VectorFloat( [cylinder["radius"] for cylinder in hole_ice_cylinders]) hole_ice_cylinder_scattering_lengths = dataclasses.I3VectorFloat( [cylinder["scattering_length"] for cylinder in hole_ice_cylinders]) hole_ice_cylinder_absorption_lengths = dataclasses.I3VectorFloat( [cylinder["absorption_length"] for cylinder in hole_ice_cylinders]) tray = I3Tray() tray.AddModule("I3Reader", Filename=input_gcd) tray.AddModule(add_hole_ice_to_geometry_frame, positions=hole_ice_cylinder_positions, radii=hole_ice_cylinder_radii, scattering_lengths=hole_ice_cylinder_scattering_lengths, absorption_lengths=hole_ice_cylinder_absorption_lengths, Streams=[icetray.I3Frame.Geometry]) tray.AddModule("I3Writer", Filename=output_gcd) tray.AddModule("TrashCan") tray.Execute() tray.Finish()
def evaluateVectorTransformOpenCL(xValues, yValues, zValues, VectorTransform, useReferenceFunction=False): tester = clsim.I3CLSimVectorTransformTester( device=openCLDevice, workgroupSize=workgroupSize, workItemsPerIteration=workItemsPerIteration, vectorTransform=VectorTransform) # the function currently only accepts I3VectorFloat as its input type vectorX = dataclasses.I3VectorFloat(numpy.array(xValues)) vectorY = dataclasses.I3VectorFloat(numpy.array(yValues)) vectorZ = dataclasses.I3VectorFloat(numpy.array(zValues)) if useReferenceFunction: retX, retY, retZ = tester.EvaluateReferenceFunction( vectorX, vectorY, vectorZ) else: retX, retY, retZ = tester.EvaluateFunction(vectorX, vectorY, vectorZ) return numpy.array([retX, retY, retZ]).T
def getRangeHistogram(frame, key, timeRange): ''' Given a frame, a key, and a tuple (start,end), attempt to create a histogram for the timeline widget. The returned histogram should be a dataclasses.I3VectorFloat with all values between 0 and 1. As with getTimeRange, this function may return None, or throw any exception, to indicate that producing a histogram with the given key is not possible. ''' import numpy # numpy has to be available for this function to yield histograms obj = frame[key] # If a thing doesn't have a values attribute, we can't use it below; # but maybe it's a mask. If not, this will throw and end the function. if not hasattr(obj, 'values'): obj = dataclasses.I3RecoPulseSeriesMap.from_frame(frame, key) divs = min(256, int(timeRange[1] - timeRange[0])) if type(obj) == dataclasses.I3RecoPulseSeriesMap: # histogram the pulse times, weighting by charge e1 = operator.attrgetter('time') e2 = operator.attrgetter('charge') elif type(obj) == dataclasses.I3DOMLaunchSeriesMap: # histogram the launch times, with equal weighting e1 = operator.attrgetter('time') e2 = lambda x: 1 else: return None hist_data, hist_weights = zip(*_i3map_values_iter(obj, e1, e2)) hist, _ = numpy.histogram(hist_data, bins=divs, range=timeRange, weights=hist_weights) # normalize with logarithmic scaling hist = numpy.log1p(hist) / numpy.log(sum(hist)) return dataclasses.I3VectorFloat(hist)
def process_frame(frame, charge_scale=1.0, time_scale=1e-3, append_coordinates_to_features=False): """ Processes a frame to create an event graph and metadata out of it. Parameters: ----------- frame : ? The data frame to extract an event graph with features from. charge_scale : float The normalization constant for charge. time_scale : float The normalization constant for time. append_coordinates_to_features : bool If the normalized coordinates should be appended to the feature matrix. """ global event_offset global distances_offset ### Meta data of the event for analysis of the classifier and creation of ground truth nu = dataclasses.get_most_energetic_neutrino(frame['I3MCTree']) # Obtain the PDG Encoding for ground truth frame['PDGEncoding'] = dataclasses.I3Double(nu.pdg_encoding) frame['InteractionType'] = dataclasses.I3Double( frame['I3MCWeightDict']['InteractionType']) frame['NumberChannels'] = dataclasses.I3Double( frame['IC86_Dunkman_L3_Vars']['NchCleaned']) frame['DCFiducialPE'] = dataclasses.I3Double( frame['IC86_Dunkman_L3_Vars']['DCFiducialPE']) frame['NeutrinoEnergy'] = dataclasses.I3Double( frame['trueNeutrino'].energy) # Some rare events do not produce a cascade try: frame['CascadeEnergy'] = dataclasses.I3Double( frame['trueCascade'].energy) except: frame['CascadeEnergy'] = dataclasses.I3Double(np.nan) try: # Appearently also frames with no primary muon contain this field, so to distinguish try to access it (which should throw an exception) frame['MuonEnergy'] = dataclasses.I3Double(frame['trueMuon'].energy) frame['TrackLength'] = dataclasses.I3Double(frame['trueMuon'].length) except: frame['MuonEnergy'] = dataclasses.I3Double(np.nan) frame['TrackLength'] = dataclasses.I3Double(np.nan) frame['DeltaLLH'] = dataclasses.I3Double( frame['IC86_Dunkman_L6'] ['delta_LLH']) # Used for a baseline classifcation frame['RunID'] = icetray.I3Int(frame['I3EventHeader'].run_id) frame['EventID'] = icetray.I3Int(frame['I3EventHeader'].event_id) frame['PrimaryEnergy'] = dataclasses.I3Double(nu.energy) ### Create features for each event features, coordinates = get_events_from_frame(frame, charge_scale=charge_scale, time_scale=time_scale) for feature_name in vertex_features: frame[feature_name] = dataclasses.I3VectorFloat(features[feature_name]) ### Create offset lookups for the flattened feature arrays per event frame['NumberHits'] = icetray.I3Int(len(features[features.keys()[0]])) #frame['Offset'] = icetray.I3Int(event_offset) event_offset += len(features[features.keys()[0]]) ### Create coordinates for each vertex C = np.vstack(coordinates.values()).T C, C_mean, C_std = normalize_coordinates(C, weights=None, copy=True) C_cog, C_mean_cog, C_std_cog = normalize_coordinates( C, weights=features['TotalCharge'], copy=True) frame['VertexX'] = dataclasses.I3VectorFloat(C[:, 0]) frame['VertexY'] = dataclasses.I3VectorFloat(C[:, 1]) frame['VertexZ'] = dataclasses.I3VectorFloat(C[:, 2]) frame['COGCenteredVertexX'] = dataclasses.I3VectorFloat(C_cog_centered[:, 0]) frame['COGCenteredVertexY'] = dataclasses.I3VectorFloat(C_cog_centered[:, 1]) frame['COGCenteredVertexZ'] = dataclasses.I3VectorFloat(C_cog_centered[:, 2]) ### Output centering and true debug information frame['PrimaryXOriginal'] = dataclasses.I3Double(nu.pos.x) frame['PrimaryYOriginal'] = dataclasses.I3Double(nu.pos.y) frame['PrimaryZOriginal'] = dataclasses.I3Double(nu.pos.z) frame['CMeans'] = dataclasses.I3VectorFloat(C_mean) frame['COGCenteredCMeans'] = dataclasses.I3VectorFloat(C_mean_cog) ### Compute targets for possible auxilliary tasks, i.e. position and direction of the interaction frame['PrimaryX'] = dataclasses.I3Double((nu.pos.x - C_mean[0]) / C_std[0]) frame['PrimaryY'] = dataclasses.I3Double((nu.pos.y - C_mean[1]) / C_std[1]) frame['PrimaryZ'] = dataclasses.I3Double((nu.pos.z - C_mean[2]) / C_std[2]) frame['COGCenteredPrimaryX'] = dataclasses.I3Double( (nu.pos.x - C_mean_cog[0]) / C_std_cog[0]) frame['COGCenteredPrimaryY'] = dataclasses.I3Double( (nu.pos.y - C_mean_cog[1]) / C_std_cog[1]) frame['COGCenteredPrimaryZ'] = dataclasses.I3Double( (nu.pos.z - C_mean_cog[2]) / C_std_cog[2]) frame['PrimaryAzimuth'] = dataclasses.I3Double(nu.dir.azimuth) frame['PrimaryZenith'] = dataclasses.I3Double(nu.dir.zenith) ### Compute possible reco inputs that apply to entire event sets track_reco = frame['IC86_Dunkman_L6_PegLeg_MultiNest8D_Track'] frame['RecoX'] = dataclasses.I3Double( (track_reco.pos.x - C_mean[0]) / C_std[0]) frame['RecoY'] = dataclasses.I3Double( (track_reco.pos.y - C_mean[1]) / C_std[1]) frame['RecoZ'] = dataclasses.I3Double( (track_reco.pos.z - C_mean[2]) / C_std[2]) frame['COGCenteredRecoX'] = dataclasses.I3Double( (track_reco.pos.x - C_mean_cog[0]) / C_std_cog[0]) frame['COGCenteredRecoY'] = dataclasses.I3Double( (track_reco.pos.y - C_mean_cog[1]) / C_std_cog[1]) frame['COGCenteredRecoZ'] = dataclasses.I3Double( (track_reco.pos.z - C_mean_cog[2]) / C_std_cog[2]) frame['RecoAzimuth'] = dataclasses.I3Double(track_reco.dir.azimuth) frame['RecoZenith'] = dataclasses.I3Double(track_reco.dir.zenith) return True