def test_filled_constructor(dimension, execution_number): projection_id = random.randint(0, 10) number_of_voxels = [] image_sizes = [] origin = [] total_volume = 1.0 total_voxels = 1 for dim in range(dimension): L = random.uniform(0.001, 1e4) N = random.randint(1, 2e4) O = random.uniform(0.001, 1e4) number_of_voxels.append(N) image_sizes.append(L) origin.append(O) total_volume *= L total_voxels *= N if dimension == 1: im = larcv.ImageMeta1D(projection_id, number_of_voxels, image_sizes) elif dimension == 2: im = larcv.ImageMeta2D(projection_id, number_of_voxels, image_sizes) elif dimension == 3: im = larcv.ImageMeta3D(projection_id, number_of_voxels, image_sizes) elif dimension == 4: im = larcv.ImageMeta4D(projection_id, number_of_voxels, image_sizes) else: assert (False) assert (im.n_dims() == dimension) assert (im.total_voxels() == total_voxels)
def image_meta_factory(dimension): if dimension == 1: return larcv.ImageMeta1D() elif dimension == 2: return larcv.ImageMeta2D() elif dimension == 3: return larcv.ImageMeta3D() elif dimension == 4: return larcv.ImageMeta4D() else: raise Expection("Can't do anything with dimension == ", dimension)
def write_sparse_tensors(file_name, voxel_set_list, dimension, n_projections): from copy import copy io_manager = larcv.IOManager(larcv.IOManager.kWRITE) io_manager.set_out_file(file_name) io_manager.initialize() # For this test, the meta is pretty irrelevant as long as it is consistent meta_list = [] for projection in range(n_projections): if dimension == 2: meta_list.append(larcv.ImageMeta2D()) else: meta_list.append(larcv.ImageMeta3D()) for dim in range(dimension): L = 10. N = 128 meta_list[-1].set_dimension(dim, L, N) meta_list[-1].set_projection_id(projection) for i in range(len(voxel_set_list)): io_manager.set_id(1001, 0, i) # Get a piece of data, sparse tensor: if dimension == 2: ev_sparse = io_manager.get_data("sparse2d", "test") else: ev_sparse = io_manager.get_data("sparse3d", "test") for projection in range(n_projections): vs = larcv.VoxelSet() indexes = voxel_set_list[i][projection]['indexes'] values = voxel_set_list[i][projection]['values'] for j in range(voxel_set_list[i][projection]['n_voxels']): vs.emplace(indexes[j], values[j], False) ev_sparse.set(vs, meta_list[projection]) io_manager.save_entry() print("Finished event loop") # assert(io_manager.get_n_entries_out() == rand_num_events) io_manager.finalize() return
def _write_image2d(self, data, producer): ''' Write image2d data to file Data is written under the provided producer name. This function takes a list of data that it can iterate through, where each list element is a projection ID. ''' ev_image2d = self._io.get_data("image2d", producer) for projection_id, image in enumerate(data): meta = larcv.ImageMeta2D() meta.set_dimension(0, image.shape[0], image.shape[0]) meta.set_dimension(1, image.shape[1], image.shape[1]) meta.set_projection_id(projection_id) img = larcv.as_image2d_meta(image, meta) ev_image2d.emplace(img) # print(img) return
def _write_sparse2d(self, data, producer): '''Write sparse2d data to file. Data is writting under the provided producer name. This function takes a list of data that it can iterate through, assigning each element of the list contains a dict with the following three elements: - 'index' is a flattened 1D array of index values, which are the 'raveled' values - 'value' is the list of features - 'meta' is the meta data object. It is meant to have the arugments for the constructor of ImageMeta2d, so it is a list in the following order: [xmin, ymin, xmax, ymax, n_x, n_y, projection_id] Arguments: data {list} -- list (by projection ID) of dict objects. dict must have the keys 'index', 'value' and 'meta' producer {[type]} -- [description] ''' _writable_data = self._io.get_data("sparse2d", producer) for projection in range(len(data)): value = data[projection]['values'] index = data[projection]['index'] shape = data[projection]['shape'] meta = larcv.ImageMeta2D() meta.set_projection_id(projection) meta.set_dimension(0, shape[0], int(shape[0])) meta.set_dimension(1, shape[1], int(shape[1])) # We need to put in voxels and indexes together. Argsort handles this: perm = numpy.argsort(index) # First, copy all of the values into a VoxelSet object: voxel_set = larcv.VoxelSet() voxel_set.set(index[perm], value[perm]) # for i in range(len(value)): # _ = [voxel_set.emplace(index[i], value[i], False) for i in range(len(value))] # larcv.as_voxelset(value.astype(numpy.float32), index.astype(numpy.uint64)) # Add the voxel set: _writable_data.set(voxel_set, meta) return
def write_sparse_clusters(file_name, voxel_set_array_list, dimension=2, n_projections=3): import copy io_manager = larcv.IOManager(larcv.IOManager.kWRITE) io_manager.set_out_file(file_name) io_manager.initialize() # For this test, the meta is pretty irrelevant as long as it is consistent meta_list = [] for projection in range(n_projections): if dimension == 2: meta_list.append(larcv.ImageMeta2D()) else: meta_list.append(larcv.ImageMeta3D()) for dim in range(dimension): L = 10. N = 128 meta_list[-1].set_dimension(dim, L, N) meta_list[-1].set_projection_id(projection) for i in range(len(voxel_set_array_list)): io_manager.set_id(1001, 0, i) # Get a piece of data, sparse tensor: if dimension == 2: ev_cluster = io_manager.get_data("cluster2d", "test") else: ev_cluster = io_manager.get_data("cluster3d", "test") # Holder for the voxels to store: for projection in range(n_projections): clusters = voxel_set_array_list[i][projection] if dimension == 2: vsa = larcv.SparseCluster2D() else: vsa = larcv.SparseCluster3D() for cluster in range(len(clusters)): vs = larcv.VoxelSet() vs.id(cluster) indexes = clusters[cluster]['indexes'] values = clusters[cluster]['values'] for j in range(clusters[cluster]['n_voxels']): vs.emplace(indexes[j], values[j], False) vsa.insert(vs) # sparse_cluster.set(vsa, ) vsa.meta(meta_list[projection]) # ev_cluster.emplace(vsa, copy.copy(meta_list[projection])) ev_cluster.set(vsa) io_manager.save_entry() # assert(io_manager.get_n_entries_out() == rand_num_events) io_manager.finalize() return
def test_import_ImageMeta_h(): im = larcv.ImageMeta2D() im = larcv.ImageMeta3D()