def _create_volume(self): volume = Volume(storage_path=self.storage_path) volume.origin = [[0.0, 0.0, 0.0]] volume.voxel_size = [self.parser.zooms[0], self.parser.zooms[1], self.parser.zooms[2]] if self.parser.units is not None and len(self.parser.units) > 0: volume.voxel_unit = self.parser.units[0] return volume
def _create_volume(self): volume = Volume() volume.origin = numpy.array([[0.0, 0.0, 0.0]]) volume.voxel_size = numpy.array([self.parser.zooms[0], self.parser.zooms[1], self.parser.zooms[2]]) if self.parser.units is not None and len(self.parser.units) > 0: volume.voxel_unit = self.parser.units[0] return h5.store_complete(volume, self.storage_path), volume
def test_volume(self): dt = Volume() summary_info = dt._find_summary_info() assert summary_info['Origin'].shape == (0, ) assert summary_info['Voxel size'].shape == (0, ) assert summary_info['Volume type'] == 'Volume' assert summary_info['Units'] == 'mm' assert dt.origin.shape == (0, ) assert dt.voxel_size.shape == (0, ) assert dt.voxel_unit == 'mm'
def test_store_load_volume(tmph5factory): volume = Volume(origin=numpy.zeros((3, 3)), voxel_size=numpy.zeros((3, 3))) tmp_file = tmph5factory("Volume_{}.h5".format(volume.gid)) with VolumeH5(tmp_file) as f: f.store(volume) volume_stored = Volume() with pytest.raises(TraitAttributeError): volume_stored.origin with VolumeH5(tmp_file) as f: f.load_into(volume_stored) assert volume_stored.origin is not None
def test_spatialpatternvolume(self): dt = patterns.SpatialPatternVolume(spatial=FiniteSupportEquation(), volume=Volume(origin=numpy.array([]), voxel_size=numpy.array([])), focal_points_volume=numpy.array([1])) assert dt.space is None assert dt.spatial is not None assert dt.spatial_pattern is None assert dt.volume is not None assert dt.focal_points_volume is not None
def test_store_load_structuralMRI(tmph5factory): volume = Volume( origin=numpy.zeros((3, 3)), voxel_size=numpy.zeros((3, 3)) ) structural_mri = StructuralMRI( array_data=numpy.zeros((3, 3)), weighting="T1", volume=volume ) tmp_file = tmph5factory("StructuralMRI_{}.h5".format(volume.gid)) with StructuralMRIH5(tmp_file) as f: f.store(structural_mri) structural_mri_stored = StructuralMRI() with pytest.raises(TraitAttributeError): structural_mri_stored.array_data with pytest.raises(TraitAttributeError): structural_mri_stored.volume with StructuralMRIH5(tmp_file) as f: f.load_into(structural_mri_stored) assert structural_mri_stored.array_data.shape == (3, 3) # referenced datatype is not loaded with pytest.raises(TraitAttributeError): structural_mri_stored.volume
def launch(self, data_file, apply_corrections=False, connectivity=None): """ Execute import operations: """ try: parser = NIFTIParser(data_file) # Create volume DT volume = Volume(storage_path=self.storage_path) volume.set_operation_id(self.operation_id) volume.origin = [[0.0, 0.0, 0.0]] volume.voxel_size = [ parser.zooms[0], parser.zooms[1], parser.zooms[2] ] if parser.units is not None and len(parser.units) > 0: volume.voxel_unit = parser.units[0] if parser.has_time_dimension or not connectivity: # Now create TimeSeries and fill it with data from NIFTI image time_series = TimeSeriesVolume(storage_path=self.storage_path) time_series.set_operation_id(self.operation_id) time_series.volume = volume time_series.title = "NIFTI Import - " + os.path.split( data_file)[1] time_series.labels_ordering = ["Time", "X", "Y", "Z"] time_series.start_time = 0.0 if len(parser.zooms) > 3: time_series.sample_period = float(parser.zooms[3]) else: # If no time dim, set sampling to 1 sec time_series.sample_period = 1 if parser.units is not None and len(parser.units) > 1: time_series.sample_period_unit = parser.units[1] parser.parse(time_series, True) return [volume, time_series] else: region2volume_mapping = RegionVolumeMapping( storage_path=self.storage_path) region2volume_mapping.set_operation_id(self.operation_id) region2volume_mapping.volume = volume region2volume_mapping.connectivity = connectivity region2volume_mapping.title = "NIFTI Import - " + os.path.split( data_file)[1] region2volume_mapping.dimensions_labels = ["X", "Y", "Z"] region2volume_mapping.apply_corrections = apply_corrections parser.parse(region2volume_mapping, False) return [volume, region2volume_mapping] except ParseException, excep: logger = get_logger(__name__) logger.exception(excep) raise LaunchException(excep)
def launch(self, data_file, apply_corrections=False, connectivity=None): """ Execute import operations: """ try: parser = NIFTIParser(data_file) # Create volume DT volume = Volume(storage_path=self.storage_path) volume.set_operation_id(self.operation_id) volume.origin = [[0.0, 0.0, 0.0]] volume.voxel_size = [parser.zooms[0], parser.zooms[1], parser.zooms[2]] if parser.units is not None and len(parser.units) > 0: volume.voxel_unit = parser.units[0] if parser.has_time_dimension or not connectivity: # Now create TimeSeries and fill it with data from NIFTI image time_series = TimeSeriesVolume(storage_path=self.storage_path) time_series.set_operation_id(self.operation_id) time_series.volume = volume time_series.title = "NIFTI Import - " + os.path.split(data_file)[1] time_series.labels_ordering = ["Time", "X", "Y", "Z"] time_series.start_time = 0.0 if len(parser.zooms) > 3: time_series.sample_period = float(parser.zooms[3]) else: # If no time dim, set sampling to 1 sec time_series.sample_period = 1 if parser.units is not None and len(parser.units) > 1: time_series.sample_period_unit = parser.units[1] parser.parse(time_series, True) return [volume, time_series] else: region2volume_mapping = RegionVolumeMapping(storage_path=self.storage_path) region2volume_mapping.set_operation_id(self.operation_id) region2volume_mapping.volume = volume region2volume_mapping.connectivity = connectivity region2volume_mapping.title = "NIFTI Import - " + os.path.split(data_file)[1] region2volume_mapping.dimensions_labels = ["X", "Y", "Z"] region2volume_mapping.apply_corrections = apply_corrections parser.parse(region2volume_mapping, False) return [volume, region2volume_mapping] except ParseException, excep: logger = get_logger(__name__) logger.exception(excep) raise LaunchException(excep)
def launch(self, view_model): resolution = view_model.resolution weighting = view_model.weighting inj_f_thresh = view_model.inj_f_thresh / 100. vol_thresh = view_model.vol_thresh project = dao.get_project_by_id(self.current_project_id) manifest_file = self.file_handler.get_allen_mouse_cache_folder( project.name) manifest_file = os.path.join(manifest_file, 'mouse_connectivity_manifest.json') cache = MouseConnectivityCache(resolution=resolution, manifest_file=manifest_file) # the method creates a dictionary with information about which experiments need to be downloaded ist2e = dictionary_builder(cache, False) # the method downloads experiments necessary to build the connectivity projmaps = download_an_construct_matrix(cache, weighting, ist2e, False) # the method cleans the file projmaps in 4 steps projmaps = pms_cleaner(projmaps) # download from the AllenSDK the annotation volume, the template volume vol, annot_info = cache.get_annotation_volume() template, template_info = cache.get_template_volume() # rotate template in the TVB 3D reference: template = rotate_reference(template) # grab the StructureTree instance structure_tree = cache.get_structure_tree() # the method includes in the parcellation only brain regions whose volume is greater than vol_thresh projmaps = areas_volume_threshold(cache, projmaps, vol_thresh, resolution) # the method exclude from the experimental dataset # those exps where the injected fraction of pixel in the injection site is lower than than the inj_f_thr projmaps = infected_threshold(cache, projmaps, inj_f_thresh) # the method creates file order and keyword that will be the link between the SC order and the # id key in the Allen database [order, key_ord] = create_file_order(projmaps, structure_tree) # the method builds the Structural Connectivity (SC) matrix structural_conn = construct_structural_conn(projmaps, order, key_ord) # the method returns the coordinate of the centres and the name of the brain areas in the selected parcellation [centres, names] = construct_centres(cache, order, key_ord) # the method returns the tract lengths between the brain areas in the selected parcellation tract_lengths = construct_tract_lengths(centres) # the method associated the parent and the grandparents to the child in the selected parcellation with # the biggest volume [unique_parents, unique_grandparents ] = parents_and_grandparents_finder(cache, order, key_ord, structure_tree) # the method returns a volume indexed between 0 and N-1, with N=tot brain areas in the parcellation. # -1=background and areas that are not in the parcellation vol_parcel = mouse_brain_visualizer(vol, order, key_ord, unique_parents, unique_grandparents, structure_tree, projmaps) # results: Connectivity, Volume & RegionVolumeMapping # Connectivity result_connectivity = Connectivity() result_connectivity.centres = centres result_connectivity.region_labels = numpy.array(names) result_connectivity.weights = structural_conn result_connectivity.tract_lengths = tract_lengths result_connectivity.configure() # Volume result_volume = Volume() result_volume.origin = numpy.array([[0.0, 0.0, 0.0]]) result_volume.voxel_size = numpy.array( [resolution, resolution, resolution]) # result_volume.voxel_unit= micron # Region Volume Mapping result_rvm = RegionVolumeMapping() result_rvm.volume = result_volume result_rvm.array_data = vol_parcel result_rvm.connectivity = result_connectivity result_rvm.title = "Volume mouse brain " result_rvm.dimensions_labels = ["X", "Y", "Z"] # Volume template result_template = StructuralMRI() result_template.array_data = template result_template.weighting = 'T1' result_template.volume = result_volume connectivity_index = h5.store_complete(result_connectivity, self.storage_path) volume_index = h5.store_complete(result_volume, self.storage_path) rvm_index = h5.store_complete(result_rvm, self.storage_path) template_index = h5.store_complete(result_template, self.storage_path) return [connectivity_index, volume_index, rvm_index, template_index]
def test_get_min_max(self): dt = Volume(array_data=numpy.array(range(30))) assert dt.get_min_max_values() == (0, 29)
edge_min_length=0.0, edge_max_length=2.0, zero_based_triangles=False, split_triangles=numpy.arange(0), number_of_split_slices=1, split_slices=dict(), bi_hemispheric=False, # surface_type="surface", valid_for_simulations=True) sensors = Sensors(sensors_type="SEEG", labels=numpy.array(["s1", "s2", "s3"]), locations=numpy.zeros((3, 3)), number_of_sensors=3, has_orientation=True, orientations=numpy.zeros((3, 3)), usable=numpy.array([True, False, True])) volume = Volume(origin=numpy.zeros((3, 3)), voxel_size=numpy.zeros((3, 3))) projection_matrix = ProjectionMatrix(projection_type="projSEEG", sources=cortical_surface, sensors=sensors, projection_data=numpy.zeros((5, 3))) local_connectivity = LocalConnectivity( surface=cortical_surface, matrix=scipy.sparse.csc_matrix(numpy.eye(8) + numpy.eye(8)[:, ::-1]), cutoff=12, )
def launch(self, resolution, weighting, inf_vox_thresh, vol_thresh): resolution = int(resolution) weighting = int(weighting) inf_vox_thresh = float(inf_vox_thresh) vol_thresh = float(vol_thresh) project = dao.get_project_by_id(self.current_project_id) manifest_file = self.file_handler.get_allen_mouse_cache_folder( project.name) manifest_file = os.path.join(manifest_file, 'mouse_connectivity_manifest.json') cache = MouseConnectivityCache(resolution=resolution, manifest_file=manifest_file) # the method creates a dictionary with information about which experiments need to be downloaded ist2e = DictionaireBuilder(cache, False) # the method downloads experiments necessary to build the connectivity projmaps = DownloadAndConstructMatrix(cache, weighting, ist2e, False) # the method cleans the file projmaps in 4 steps projmaps = pmsCleaner(projmaps) #download from the AllenSDK the annotation volume, the ontology, the template volume Vol, annot_info = cache.get_annotation_volume() ontology = cache.get_ontology() template, template_info = cache.get_template_volume() #rotate template in the TVB 3D reference: template = RotateReference(template) # the method includes in the parcellation only brain regions whose volume is greater than vol_thresh projmaps = AreasVolumeTreshold(cache, projmaps, vol_thresh, resolution, Vol, ontology) # the method includes in the parcellation only brain regions where at least one injection experiment had infected more than N voxel (where N is inf_vox_thresh) projmaps = AreasVoxelTreshold(cache, projmaps, inf_vox_thresh, Vol, ontology) # the method creates file order and keyord that will be the link between the SC order and the id key in the Allen database [order, key_ord] = CreateFileOrder(projmaps, ontology) # the method builds the Structural Connectivity (SC) matrix SC = ConstructingSC(projmaps, order, key_ord) # the method returns the coordinate of the centres and the name of the brain areas in the selected parcellation [centres, names] = Construct_centres(cache, ontology, order, key_ord) # the method returns the tract lengths between the brain areas in the selected parcellation tract_lengths = ConstructTractLengths(centres) # the method associated the parent and the grandparents to the child in the selected parcellation with the biggest volume [unique_parents, unique_grandparents ] = ParentsAndGrandParentsFinder(cache, order, key_ord, ontology) # the method returns a volume indexed between 0 and N-1, with N=tot brain areas in the parcellation. -1=background and areas that are not in the parcellation Vol_parcel = MouseBrainVisualizer(Vol, order, key_ord, unique_parents, unique_grandparents, ontology, projmaps) # results: Connectivity, Volume & RegionVolumeMapping # Connectivity result_connectivity = Connectivity(storage_path=self.storage_path) result_connectivity.centres = centres result_connectivity.region_labels = names result_connectivity.weights = SC result_connectivity.tract_lengths = tract_lengths # Volume result_volume = Volume(storage_path=self.storage_path) result_volume.origin = [[0.0, 0.0, 0.0]] result_volume.voxel_size = [resolution, resolution, resolution] # result_volume.voxel_unit= micron # Region Volume Mapping result_rvm = RegionVolumeMapping(storage_path=self.storage_path) result_rvm.volume = result_volume result_rvm.array_data = Vol_parcel result_rvm.connectivity = result_connectivity result_rvm.title = "Volume mouse brain " result_rvm.dimensions_labels = ["X", "Y", "Z"] # Volume template result_template = StructuralMRI(storage_path=self.storage_path) result_template.array_data = template result_template.weighting = 'T1' result_template.volume = result_volume return [ result_connectivity, result_volume, result_rvm, result_template ]
class NIFTIParser(): """ This class reads content of a NIFTI file and builds / returns a TimeSeries instance filled with details. """ def __init__(self, storage_path, operation_id): self.logger = get_logger(__name__) self.storage_path = storage_path self.operation_id = operation_id def parse(self, data_file): """ Parse NIFTI file and returns TimeSeries for it. """ if data_file is None: raise ParseException( "Please select NIFTI file which contains data to import") if not os.path.exists(data_file): raise ParseException("Provided file %s does not exists" % data_file) try: nifti_image = nib.load(data_file) except nib.spatialimages.ImageFileError, e: self.logger.exception(e) msg = "File: %s does not have a valid NIFTI-1 format." % data_file raise ParseException(msg) nifti_image_hdr = nifti_image.get_header() # Create volume for time series volume = Volume(storage_path=self.storage_path) volume.set_operation_id(self.operation_id) volume.origin = [[0.0, 0.0, 0.0]] # Now create TimeSeries and fill it with data from NIFTI image time_series = TimeSeriesVolume(storage_path=self.storage_path) time_series.set_operation_id(self.operation_id) time_series.volume = volume time_series.title = "NIFTI Import - " + os.path.split(data_file)[1] time_series.labels_ordering = ["Time", "X", "Y", "Z"] time_series.start_time = 0.0 # Copy data from NIFTI file to our TVB storage # In NIFTI format time si the 4th dimension, while our TimeSeries has # it as first dimension, so we have to adapt imported data # Check if there is a time dimensions (4th dimension). nifti_data_shape = nifti_image_hdr.get_data_shape() has_time_dimension = len(nifti_data_shape) > 3 time_dim_size = nifti_data_shape[3] if has_time_dimension else 1 nifti_data = nifti_image.get_data() if has_time_dimension: for i in range(time_dim_size): time_series.write_data_slice([nifti_data[:, :, :, i, ...]]) else: time_series.write_data_slice([nifti_data]) time_series.close_file() # Force closing HDF5 file # Extract sample unit measure units = nifti_image_hdr.get_xyzt_units() if units is not None and len(units) == 2: volume.voxel_unit = units[0] time_series.sample_period_unit = units[1] # Extract sample rate # Usually zooms defines values for x, y, z, time and other dimensions zooms = nifti_image_hdr.get_zooms() if has_time_dimension: time_series.sample_period = float(zooms[3]) else: time_series.sample_period = 1.0 # If no time dim, set sampling to 1 sec # Get voxtel dimensions for x,y, z volume.voxel_size = [zooms[0], zooms[1], zooms[2]] return time_series
def launch(self, resolution, weighting, inj_f_thresh, vol_thresh): resolution = int(resolution) weighting = int(weighting) inj_f_thresh = float(inj_f_thresh)/100. vol_thresh = float(vol_thresh) project = dao.get_project_by_id(self.current_project_id) manifest_file = self.file_handler.get_allen_mouse_cache_folder(project.name) manifest_file = os.path.join(manifest_file, 'mouse_connectivity_manifest.json') cache = MouseConnectivityCache(resolution=resolution, manifest_file=manifest_file) # the method creates a dictionary with information about which experiments need to be downloaded ist2e = dictionary_builder(cache, False) # the method downloads experiments necessary to build the connectivity projmaps = download_an_construct_matrix(cache, weighting, ist2e, False) # the method cleans the file projmaps in 4 steps projmaps = pms_cleaner(projmaps) # download from the AllenSDK the annotation volume, the template volume vol, annot_info = cache.get_annotation_volume() template, template_info = cache.get_template_volume() # rotate template in the TVB 3D reference: template = rotate_reference(template) # grab the StructureTree instance structure_tree = cache.get_structure_tree() # the method includes in the parcellation only brain regions whose volume is greater than vol_thresh projmaps = areas_volume_threshold(cache, projmaps, vol_thresh, resolution) # the method exclude from the experimental dataset # those exps where the injected fraction of pixel in the injection site is lower than than the inj_f_thr projmaps = infected_threshold(cache, projmaps, inj_f_thresh) # the method creates file order and keyword that will be the link between the SC order and the # id key in the Allen database [order, key_ord] = create_file_order(projmaps, structure_tree) # the method builds the Structural Connectivity (SC) matrix structural_conn = construct_structural_conn(projmaps, order, key_ord) # the method returns the coordinate of the centres and the name of the brain areas in the selected parcellation [centres, names] = construct_centres(cache, order, key_ord) # the method returns the tract lengths between the brain areas in the selected parcellation tract_lengths = construct_tract_lengths(centres) # the method associated the parent and the grandparents to the child in the selected parcellation with # the biggest volume [unique_parents, unique_grandparents] = parents_and_grandparents_finder(cache, order, key_ord, structure_tree) # the method returns a volume indexed between 0 and N-1, with N=tot brain areas in the parcellation. # -1=background and areas that are not in the parcellation vol_parcel = mouse_brain_visualizer(vol, order, key_ord, unique_parents, unique_grandparents, structure_tree, projmaps) # results: Connectivity, Volume & RegionVolumeMapping # Connectivity result_connectivity = Connectivity(storage_path=self.storage_path) result_connectivity.centres = centres result_connectivity.region_labels = names result_connectivity.weights = structural_conn result_connectivity.tract_lengths = tract_lengths # Volume result_volume = Volume(storage_path=self.storage_path) result_volume.origin = [[0.0, 0.0, 0.0]] result_volume.voxel_size = [resolution, resolution, resolution] # result_volume.voxel_unit= micron # Region Volume Mapping result_rvm = RegionVolumeMapping(storage_path=self.storage_path) result_rvm.volume = result_volume result_rvm.array_data = vol_parcel result_rvm.connectivity = result_connectivity result_rvm.title = "Volume mouse brain " result_rvm.dimensions_labels = ["X", "Y", "Z"] # Volume template result_template = StructuralMRI(storage_path=self.storage_path) result_template.array_data = template result_template.weighting = 'T1' result_template.volume = result_volume return [result_connectivity, result_volume, result_rvm, result_template]
def launch(self, resolution, weighting, inf_vox_thresh, vol_thresh): resolution = int(resolution) weighting = int(weighting) inf_vox_thresh = float(inf_vox_thresh) vol_thresh = float(vol_thresh) project = dao.get_project_by_id(self.current_project_id) manifest_file = self.file_handler.get_allen_mouse_cache_folder(project.name) manifest_file = os.path.join(manifest_file, "mouse_connectivity_manifest.json") cache = MouseConnectivityCache(resolution=resolution, manifest_file=manifest_file) # the method creates a dictionary with information about which experiments need to be downloaded ist2e = DictionaireBuilder(cache, False) # the method downloads experiments necessary to build the connectivity projmaps = DownloadAndConstructMatrix(cache, weighting, ist2e, False) # the method cleans the file projmaps in 4 steps projmaps = pmsCleaner(projmaps) Vol, annot_info = cache.get_annotation_volume() ontology = cache.get_ontology() # the method includes in the parcellation only brain regions whose volume is greater than vol_thresh projmaps = AreasVolumeTreshold(projmaps, vol_thresh, resolution, Vol, ontology) # the method includes in the parcellation only brain regions where at least one injection experiment had infected more than N voxel (where N is inf_vox_thresh) projmaps = AreasVoxelTreshold(cache, projmaps, inf_vox_thresh, Vol, ontology) # the method creates file order and keyord that will be the link between the SC order and the id key in the Allen database [order, key_ord] = CreateFileOrder(projmaps, ontology) # the method builds the Structural Connectivity (SC) matrix SC = ConstructingSC(projmaps, order, key_ord) # the method returns the coordinate of the centres and the name of the brain areas in the selected parcellation [centres, names] = Construct_centres(ontology, order, key_ord, Vol) # the method returns the tract lengths between the brain areas in the selected parcellation tract_lengths = ConstructTractLengths(centres) # the method associated the parent and the grandparents to the child in the selected parcellation with the biggest volume [unique_parents, unique_grandparents] = ParentsAndGrandParentsFinder(order, key_ord, Vol, ontology) # the method returns a volume indexed between 0 and N-1, with N=tot brain areas in the parcellation. -1=background and areas that are not in the parcellation Vol_parcel = MouseBrainVisualizer(Vol, order, key_ord, unique_parents, unique_grandparents, ontology, projmaps) # results: Connectivity, Volume & RegionVolumeMapping # Connectivity result_connectivity = Connectivity(storage_path=self.storage_path) result_connectivity.centres = centres result_connectivity.region_labels = names result_connectivity.weights = SC result_connectivity.tract_lengths = tract_lengths # Volume result_volume = Volume(storage_path=self.storage_path) result_volume.origin = [[0.0, 0.0, 0.0]] result_volume.voxel_size = [resolution, resolution, resolution] # result_volume.voxel_unit= micron # Region Volume Mapping result_rvm = RegionVolumeMapping(storage_path=self.storage_path) result_rvm.volume = result_volume result_rvm.array_data = Vol_parcel result_rvm.connectivity = result_connectivity result_rvm.title = "Volume mouse brain " result_rvm.dimensions_labels = ["X", "Y", "Z"] return [result_connectivity, result_rvm, result_volume]