def parse_sparse3d_scn(data): """ A function to retrieve sparse tensor input from larcv::EventSparseTensor3D object Returns the data in format to pass to SCN Args: array of larcv::EventSparseTensor3D Return: voxels - numpy array(int32) with shape (N,3) - coordinates data - numpy array(float32) with shape (N,C) - pixel values/channels """ meta = None output = [] np_voxels = None for event_tensor3d in data: num_point = event_tensor3d.as_vector().size() if meta is None: meta = event_tensor3d.meta() np_voxels = np.empty(shape=(num_point, 3), dtype=np.int32) larcv.fill_3d_voxels(event_tensor3d, np_voxels) else: assert meta == event_tensor3d.meta() np_data = np.empty(shape=(num_point, 1), dtype=np.float32) larcv.fill_3d_pcloud(event_tensor3d, np_data) output.append(np_data) return np_voxels, np.concatenate(output, axis=-1)
def parse_sparse3d(data): """ A function to retrieve sparse tensor from larcv::EventSparseTensor3D object Args: length 1 array of larcv::EventSparseTensor3D Return: a numpy array with the shape (N,4) where 4=3+1 represents (x,y,z) coordinate and stored pixel value. """ event_tensor3d = data[0] num_point = event_tensor3d.as_vector().size() np_data = np.empty(shape=(num_point, 4), dtype=np.float32) larcv.fill_3d_pcloud(event_tensor3d, np_data) return np_data
def parse_sparse3d(event_tensor3d): """ A function to retrieve sparse tensor from larcv::EventSparseTensor3D object Args: event_tensor3d (larcv::EventSparseTensor3D): larcv C++ object for a 3d sparse tensor object Return: a pair of numpy arrays (coords,value), where coords has shape (N,3) representing 3D pixel coordinate and value (N,1) stores pixel values. """ num_point = event_tensor3d.as_vector().size() np_voxels = np.zeros(shape=(num_point, 3), dtype=np.int32) np_data = np.zeros(shape=(num_point, 1), dtype=np.float32) larcv.fill_3d_voxels(event_tensor3d, np_voxels) larcv.fill_3d_pcloud(event_tensor3d, np_data) return np_voxels, np_data
def parse_sparse3d_scn(data): """ A function to retrieve sparse tensor input from larcv::EventSparseTensor3D object Returns the data in format to pass to SCN Args: length 1 array of larcv::EventSparseTensor3D Return: voxels - numpy array(int32) with shape (N,3) - coordinates data - numpy array(float32) with shape (N,1) - pixel value """ event_tensor3d = data[0] num_point = event_tensor3d.as_vector().size() np_voxels = np.empty(shape=(num_point, 3), dtype=np.int32) np_data = np.empty(shape=(num_point, 1), dtype=np.float32) larcv.fill_3d_voxels(event_tensor3d, np_voxels) larcv.fill_3d_pcloud(event_tensor3d, np_data) return np_voxels, np_data
def initialize(self): from larcv import larcv from ROOT import TChain ch_data = TChain('sparse3d_%s_tree' % self._flags.DATA_KEY) ch_label = TChain('sparse3d_%s_tree' % self._flags.LABEL_KEY) for f in self._flags.INPUT_FILE: ch_data.AddFile(f) ch_label.AddFile(f) self._num_entries = ch_data.GetEntries() self._data = [] self._label = [] br_data, br_label = (None, None) event_fraction = 1. / self.num_entries() * 100. total_point = 0. for i in range(self.num_entries()): ch_data.GetEntry(i) ch_label.GetEntry(i) if br_data is None: br_data = getattr(ch_data, 'sparse3d_%s_branch' % self._flags.DATA_KEY) br_label = getattr( ch_label, 'sparse3d_%s_branch' % self._flags.LABEL_KEY) num_point = br_data.as_vector().size() np_data = np.zeros(shape=(num_point, 4), dtype=np.float32) np_label = np.zeros(shape=(num_point, 4), dtype=np.float32) larcv.fill_3d_pcloud(br_data, np_data) larcv.fill_3d_pcloud(br_label, np_label) self._data.append(np.expand_dims(np_data, 0)) self._label.append(np.expand_dims(np_label, 0)) total_point += np_data.size sys.stdout.write( 'Processed %d%% ... %d MB\r' % (int(event_fraction * i), int(total_point * 4 * 2 / 1.e6))) sys.stdout.flush() sys.stdout.write('\n') sys.stdout.flush()
def parse_sparse3d(data): """ A function to retrieve sparse tensor from larcv::EventSparseTensor3D object Args: array of larcv::EventSparseTensor3D (one per channel) Return: a numpy array with the shape (N,3+C) where 3+C represents (x,y,z) coordinate and C stored pixel values (channels). """ meta = None output = [] for event_tensor3d in data: num_point = event_tensor3d.as_vector().size() if meta is None: meta = event_tensor3d.meta() np_voxels = np.empty(shape=(num_point, 3), dtype=np.int32) larcv.fill_3d_voxels(event_tensor3d, np_voxels) output.append(np_voxels) else: assert meta == event_tensor3d.meta() np_values = np.empty(shape=(num_point, 1), dtype=np.float32) larcv.fill_3d_pcloud(event_tensor3d, np_values) output.append(np_values) return np.concatenate(output, axis=-1)
def initialize(self): self._last_entry = -1 self._event_keys = [] self._metas = [] # configure the input from larcv import larcv from ROOT import TChain ch_data = TChain('sparse3d_%s_tree' % self._flags.DATA_KEY) ch_label = None if self._flags.LABEL_KEY: ch_label = TChain('sparse3d_%s_tree' % self._flags.LABEL_KEY) for f in self._flags.INPUT_FILE: ch_data.AddFile(f) if ch_label: ch_label.AddFile(f) self._data = [] self._label = [] br_data,br_label=(None,None) event_fraction = 1./ch_data.GetEntries() * 100. total_point = 0. for i in range(ch_data.GetEntries()): ch_data.GetEntry(i) if ch_label: ch_label.GetEntry(i) if br_data is None: br_data = getattr(ch_data, 'sparse3d_%s_branch' % self._flags.DATA_KEY) if ch_label: br_label = getattr(ch_label, 'sparse3d_%s_branch' % self._flags.LABEL_KEY) num_point = br_data.as_vector().size() if num_point < 256: continue np_data = np.zeros(shape=(num_point,4),dtype=np.float32) larcv.fill_3d_pcloud(br_data, np_data) self._data.append(np_data) self._event_keys.append((br_data.run(),br_data.subrun(),br_data.event())) self._metas.append(larcv.Voxel3DMeta(br_data.meta())) if ch_label: np_label = np.zeros(shape=(num_point,1),dtype=np.float32) larcv.fill_3d_pcloud(br_label, np_label) np_label = np_label.reshape([num_point]) - 1. self._label.append(np_label) total_point += np_data.size sys.stdout.write('Processed %d%% ... %d MB\r' % (int(event_fraction*i),int(total_point*4*2/1.e6))) sys.stdout.flush() sys.stdout.write('\n') sys.stdout.flush() self._num_channels = self._data[-1].shape[-1] self._num_entries = len(self._data) # Output if self._flags.OUTPUT_FILE: import tempfile cfg = ''' IOManager: { Verbosity: 2 Name: "IOManager" IOMode: 1 OutFileName: "%s" InputFiles: [] InputDirs: [] StoreOnlyType: [] StoreOnlyName: [] } ''' cfg = cfg % self._flags.OUTPUT_FILE cfg_file = tempfile.NamedTemporaryFile('w') cfg_file.write(cfg) cfg_file.flush() self._fout = larcv.IOManager(cfg_file.name) self._fout.initialize()
def process(input_data, output_data): """ input_data and output_data types should be larcv.SparseTensor3D. input_data holds 3D segmentation label values per voxel, (0,1,2,3,4) = (HIP,MIP,Shower,Delta,Michel) output_data should be empty (or will be overwritten) return is None This function will change some voxels of (Delta,Michel) into Shower. Those remain unchanged are the primary ionization trajectories attached to (HIP,MIP) pixels. """ if input_data.as_vector().size() < 1: output_data.set(input_data, input_data.meta()) return #t0=time.time() voxels = np.zeros(shape=[input_data.as_vector().size(), 3], dtype=np.int32) values = np.zeros(shape=[input_data.as_vector().size(), 1], dtype=np.float32) #if debug>0: print('c0', time.time()-t0) #t0=time.time() from larcv import larcv larcv.fill_3d_voxels(input_data, voxels) larcv.fill_3d_pcloud(input_data, values) #if debug>0: print('c1', time.time()-t0) values = values.squeeze(axis=1) #t0=time.time() no_correction = len(np.where(values > 2)[0]) == 0 trivial_correction = len(np.where(values < 3)[0]) == 0 #if debug>0: print('c2', time.time()-t0) # Nothing to correct, return if no_correction: output_data.set(input_data, input_data.meta()) return # Only michel/delta ray, make them all shower if trivial_correction: values[:] = 2. vs = larcv.as_tensor3d(voxels, values, input_data.meta(), -1.) output_data.set(vs, input_data.meta()) return # Reaching here means something to correct. # DBSCAN #t0=time.time() others_idx, others_vox = dbscan(voxels, values, find_valmax=1.) deltas_idx, deltas_vox = dbscan(voxels, values, find_val=3., min_samples=1) michels_idx, michels_vox = dbscan(voxels, values, find_val=4., min_samples=1) #if debug>0: print('c3', time.time()-t0) #t0=time.time() correlated_deltas = correlate(others_vox, deltas_vox) #if debug>0: print('c4', time.time()-t0) #t0=time.time() correlated_michels = correlate(others_vox, michels_vox) #if debug>0: print('c5', time.time()-t0) #t0=time.time() for i, correlation in enumerate(correlated_deltas): if correlation > 0: continue values[deltas_idx[i]] = 2. for i, correlation in enumerate(correlated_michels): if correlation > 0: continue values[michels_idx[i]] = 2. #if debug>0: print('c6', time.time()-t0) vs = larcv.as_tensor3d(voxels, values, input_data.meta(), -1.) output_data.set(vs, input_data.meta()) return