def launch(self, weights, tracts, input_data): """ Execute import operations: process the weights and tracts csv files, then use the reference connectivity passed as input_data for the rest of the attributes. :param weights: csv file containing the weights measures :param tracts: csv file containing the tracts measures :param input_data: a reference connectivity with the additional attributes :raises LaunchException: when the number of nodes in CSV files doesn't match the one in the connectivity """ dti_service = DTIPipelineService() dti_service._process_csv_file(weights, dti_service.WEIGHTS_FILE) dti_service._process_csv_file(tracts, dti_service.TRACT_FILE) weights_matrix = read_list_data(os.path.join(os.path.dirname(weights), dti_service.WEIGHTS_FILE)) tract_matrix = read_list_data(os.path.join(os.path.dirname(tracts), dti_service.TRACT_FILE)) FilesHelper.remove_files([os.path.join(os.path.dirname(weights), dti_service.WEIGHTS_FILE), os.path.join(os.path.dirname(tracts), dti_service.TRACT_FILE)]) if weights_matrix.shape[0] != input_data.orientations.shape[0]: raise LaunchException("The csv files define %s nodes but the connectivity you selected as reference " "has only %s nodes." % (weights_matrix.shape[0], input_data.orientations.shape[0])) result = Connectivity() result.storage_path = self.storage_path result.nose_correction = input_data.nose_correction result.centres = input_data.centres result.region_labels = input_data.region_labels result.weights = weights_matrix result.tract_lengths = tract_matrix result.orientations = input_data.orientations result.areas = input_data.areas result.cortical = input_data.cortical result.hemispheres = input_data.hemispheres return result
def launch(self, view_model): # type: (CSVConnectivityImporterModel) -> ConnectivityIndex """ Execute import operations: process the weights and tracts csv files, then use the reference connectivity passed as input_data for the rest of the attributes. :raises LaunchException: when the number of nodes in CSV files doesn't match the one in the connectivity """ weights_matrix = self._read_csv_file(view_model.weights, view_model.weights_delimiter) tract_matrix = self._read_csv_file(view_model.tracts, view_model.tracts_delimiter) self.storage_interface.remove_files([view_model.weights, view_model.tracts]) conn_index = self.load_entity_by_gid(view_model.input_data) if weights_matrix.shape[0] != conn_index.number_of_regions: raise LaunchException("The csv files define %s nodes but the connectivity you selected as reference " "has only %s nodes." % (weights_matrix.shape[0], conn_index.number_of_regions)) input_connectivity = h5.load_from_index(conn_index) result = Connectivity() result.centres = input_connectivity.centres result.region_labels = input_connectivity.region_labels result.weights = weights_matrix result.tract_lengths = tract_matrix result.orientations = input_connectivity.orientations result.areas = input_connectivity.areas result.cortical = input_connectivity.cortical result.hemispheres = input_connectivity.hemispheres result.configure() return h5.store_complete(result, self.storage_path)
def _branch_connectivity(self, original_conn, new_weights, interest_areas, new_tracts=None): # type: (Connectivity, numpy.array, numpy.array, numpy.array) -> Connectivity """ Generate new Connectivity based on a previous one, by changing weights (e.g. simulate lesion). The returned connectivity has the same number of nodes. The edges of unselected nodes will have weight 0. :param original_conn: Original Connectivity, to copy from :param new_weights: weights matrix for the new connectivity :param interest_areas: ndarray of the selected node id's :param new_tracts: tracts matrix for the new connectivity """ new_weights, interest_areas, new_tracts = self._reorder_arrays(original_conn, new_weights, interest_areas, new_tracts) if new_tracts is None: new_tracts = original_conn.tract_lengths for i in range(len(original_conn.weights)): for j in range(len(original_conn.weights)): if i not in interest_areas or j not in interest_areas: new_weights[i][j] = 0 final_conn = Connectivity() final_conn.parent_connectivity = original_conn.gid.hex final_conn.saved_selection = interest_areas.tolist() final_conn.weights = new_weights final_conn.centres = original_conn.centres final_conn.region_labels = original_conn.region_labels final_conn.orientations = original_conn.orientations final_conn.cortical = original_conn.cortical final_conn.hemispheres = original_conn.hemispheres final_conn.areas = original_conn.areas final_conn.tract_lengths = new_tracts final_conn.configure() return final_conn
def launch(self, weights, weights_delimiter, tracts, tracts_delimiter, input_data): """ Execute import operations: process the weights and tracts csv files, then use the reference connectivity passed as input_data for the rest of the attributes. :param weights: csv file containing the weights measures :param tracts: csv file containing the tracts measures :param input_data: a reference connectivity with the additional attributes :raises LaunchException: when the number of nodes in CSV files doesn't match the one in the connectivity """ weights_matrix = self._read_csv_file(weights, weights_delimiter) tract_matrix = self._read_csv_file(tracts, tracts_delimiter) FilesHelper.remove_files([weights, tracts]) if weights_matrix.shape[0] != input_data.number_of_regions: raise LaunchException("The csv files define %s nodes but the connectivity you selected as reference " "has only %s nodes." % (weights_matrix.shape[0], input_data.number_of_regions)) result = Connectivity() result.storage_path = self.storage_path result.centres = input_data.centres result.region_labels = input_data.region_labels result.weights = weights_matrix result.tract_lengths = tract_matrix result.orientations = input_data.orientations result.areas = input_data.areas result.cortical = input_data.cortical result.hemispheres = input_data.hemispheres return result
def launch(self, weights, tracts, input_data): """ Execute import operations: process the weights and tracts csv files, then use the reference connectivity passed as input_data for the rest of the attributes. """ dti_service = dtipipelineservice.DTIPipelineService() dti_service._process_csv_file(weights, dti_service.WEIGHTS_FILE) dti_service._process_csv_file(tracts, dti_service.TRACT_FILE) weights_matrix = read_list_data(os.path.join(os.path.dirname(weights), dti_service.WEIGHTS_FILE)) tract_matrix = read_list_data(os.path.join(os.path.dirname(tracts), dti_service.TRACT_FILE)) FilesHelper.remove_files([os.path.join(os.path.dirname(weights), dti_service.WEIGHTS_FILE), os.path.join(os.path.dirname(tracts), dti_service.TRACT_FILE)]) if weights_matrix.shape[0] != input_data.orientations.shape[0]: raise LaunchException("The csv files define %s nodes but the connectivity you selected as reference has only %s nodes."%( weights_matrix.shape[0], input_data.orientations.shape[0])) result = Connectivity() result.storage_path = self.storage_path result.nose_correction = input_data.nose_correction result.centres = input_data.centres result.region_labels = input_data.region_labels result.weights = weights_matrix result.tract_lengths = tract_matrix result.orientations = input_data.orientations result.areas = input_data.areas result.cortical = input_data.cortical result.hemispheres = input_data.hemispheres return result
def _cut_connectivity(self, original_conn, new_weights, interest_areas, new_tracts=None): # type: (Connectivity, numpy.array, numpy.array, numpy.array) -> Connectivity """ Generate new Connectivity object based on current one, by removing nodes (e.g. simulate lesion). Only the selected nodes will get used in the result. The order of the indices in interest_areas matters. If indices are not sorted then the nodes will be permuted accordingly. :param original_conn: Original Connectivity(HasTraits), to cut nodes from :param new_weights: weights matrix for the new connectivity :param interest_areas: ndarray with the selected node id's. :param new_tracts: tracts matrix for the new connectivity """ new_weights, interest_areas, new_tracts = self._reorder_arrays( original_conn, new_weights, interest_areas, new_tracts) if new_tracts is None: new_tracts = original_conn.tract_lengths[ interest_areas, :][:, interest_areas] else: new_tracts = new_tracts[interest_areas, :][:, interest_areas] new_weights = new_weights[interest_areas, :][:, interest_areas] final_conn = Connectivity() final_conn.parent_connectivity = None final_conn.weights = new_weights final_conn.centres = original_conn.centres[interest_areas, :] final_conn.region_labels = original_conn.region_labels[interest_areas] if original_conn.orientations is not None and len( original_conn.orientations): final_conn.orientations = original_conn.orientations[ interest_areas, :] if original_conn.cortical is not None and len(original_conn.cortical): final_conn.cortical = original_conn.cortical[interest_areas] if original_conn.hemispheres is not None and len( original_conn.hemispheres): final_conn.hemispheres = original_conn.hemispheres[interest_areas] if original_conn.areas is not None and len(original_conn.areas): final_conn.areas = original_conn.areas[interest_areas] final_conn.tract_lengths = new_tracts final_conn.saved_selection = [] final_conn.configure() return final_conn
def networkx2connectivity(network_obj, storage_path): """ Populate Connectivity DataType from NetworkX object. """ network_obj.load() weights_matrix, tract_matrix, labels_vector = [], [], [] positions, areas, orientation = [], [], [] # Read all nodes graph_data = network_obj.data graph_size = len(graph_data.nodes()) for node in graph_data.nodes(): positions.append([ graph_data.node[node][ct.KEY_POS_X], graph_data.node[node][ct.KEY_POS_Y], graph_data.node[node][ct.KEY_POS_Z] ]) labels_vector.append(graph_data.node[node][ct.KEY_POS_LABEL]) if ct.KEY_AREA in graph_data.node[node]: areas.append(graph_data.node[node][ct.KEY_AREA]) if ct.KEY_ORIENTATION_AVG in graph_data.node[node]: orientation.append(graph_data.node[node][ct.KEY_ORIENTATION_AVG]) weights_matrix.append([0.0] * graph_size) tract_matrix.append([0.0] * graph_size) # Read all edges for edge in network_obj.data.edges(): start = edge[0] end = edge[1] weights_matrix[start][end] = graph_data.adj[start][end][ct.KEY_WEIGHT] tract_matrix[start][end] = graph_data.adj[start][end][ct.KEY_TRACT] meta = network_obj.get_metadata_as_dict() result = Connectivity() result.storage_path = storage_path result.nose_correction = meta[ct.KEY_NOSE] if ct.KEY_NOSE in meta else None result.weights = weights_matrix result.centres = positions result.region_labels = labels_vector result.set_metadata({'description': 'Array Columns: labels, X, Y, Z'}, 'centres') result.orientations = orientation result.areas = areas result.tract_lengths = tract_matrix return result, (meta[ct.KEY_UID] if ct.KEY_UID in meta else None)
def networkx2connectivity(network_obj, storage_path): """ Populate Connectivity DataType from NetworkX object. """ network_obj.load() weights_matrix, tract_matrix, labels_vector = [], [], [] positions, areas, orientation = [], [], [] # Read all nodes graph_data = network_obj.data graph_size = len(graph_data.nodes()) for node in graph_data.nodes(): positions.append([graph_data.node[node][ct.KEY_POS_X], graph_data.node[node][ct.KEY_POS_Y], graph_data.node[node][ct.KEY_POS_Z]]) labels_vector.append(graph_data.node[node][ct.KEY_POS_LABEL]) if ct.KEY_AREA in graph_data.node[node]: areas.append(graph_data.node[node][ct.KEY_AREA]) if ct.KEY_ORIENTATION_AVG in graph_data.node[node]: orientation.append(graph_data.node[node][ct.KEY_ORIENTATION_AVG]) weights_matrix.append([0.0] * graph_size) tract_matrix.append([0.0] * graph_size) # Read all edges for edge in network_obj.data.edges(): start = edge[0] end = edge[1] weights_matrix[start][end] = graph_data.adj[start][end][ct.KEY_WEIGHT] tract_matrix[start][end] = graph_data.adj[start][end][ct.KEY_TRACT] meta = network_obj.get_metadata_as_dict() result = Connectivity() result.storage_path = storage_path result.nose_correction = meta[ct.KEY_NOSE] if ct.KEY_NOSE in meta else None result.weights = weights_matrix result.centres = positions result.region_labels = labels_vector result.set_metadata({'description':'Array Columns: labels, X, Y, Z'},'centres') result.orientations = orientation result.areas = areas result.tract_lengths = tract_matrix return result, (meta[ct.KEY_UID] if ct.KEY_UID in meta else None)
def launch(self, uploaded, rotate_x=0, rotate_y=0, rotate_z=0): """ Execute import operations: unpack ZIP and build Connectivity object as result. :param uploaded: an archive containing the Connectivity data to be imported :returns: `Connectivity` :raises LaunchException: when `uploaded` is empty or nonexistent :raises Exception: when * weights or tracts matrix is invalid (negative values, wrong shape) * any of the vector orientation, areas, cortical or hemisphere is \ different from the expected number of nodes """ if uploaded is None: raise LaunchException("Please select ZIP file which contains data to import") files = FilesHelper().unpack_zip(uploaded, self.storage_path) weights_matrix = None centres = None labels_vector = None tract_matrix = None orientation = None areas = None cortical_vector = None hemisphere_vector = None for file_name in files: if file_name.lower().find(self.WEIGHT_TOKEN) >= 0: weights_matrix = read_list_data(file_name) continue if file_name.lower().find(self.POSITION_TOKEN) >= 0: centres = read_list_data(file_name, skiprows=1, usecols=[1, 2, 3]) labels_vector = read_list_data(file_name, dtype=numpy.str, skiprows=1, usecols=[0]) continue if file_name.lower().find(self.TRACT_TOKEN) >= 0: tract_matrix = read_list_data(file_name) continue if file_name.lower().find(self.ORIENTATION_TOKEN) >= 0: orientation = read_list_data(file_name) continue if file_name.lower().find(self.AREA_TOKEN) >= 0: areas = read_list_data(file_name) continue if file_name.lower().find(self.CORTICAL_INFO) >= 0: cortical_vector = read_list_data(file_name, dtype=numpy.bool) continue if file_name.lower().find(self.HEMISPHERE_INFO) >= 0: hemisphere_vector = read_list_data(file_name, dtype=numpy.bool) continue ### Clean remaining text-files. FilesHelper.remove_files(files, True) result = Connectivity() result.storage_path = self.storage_path result.nose_correction = [rotate_x, rotate_y, rotate_z] ### Fill positions if centres is None: raise Exception("Positions for Connectivity Regions are required! " "We expect a file *position* inside the uploaded ZIP.") expected_number_of_nodes = len(centres) if expected_number_of_nodes < 2: raise Exception("A connectivity with at least 2 nodes is expected") result.centres = centres if labels_vector is not None: result.region_labels = labels_vector ### Fill and check weights if weights_matrix is not None: if numpy.any([x < 0 for x in weights_matrix.flatten()]): raise Exception("Negative values are not accepted in weights matrix! " "Please check your file, and use values >= 0") if weights_matrix.shape != (expected_number_of_nodes, expected_number_of_nodes): raise Exception("Unexpected shape for weights matrix! " "Should be %d x %d " % (expected_number_of_nodes, expected_number_of_nodes)) result.weights = weights_matrix ### Fill and check tracts if tract_matrix is not None: if numpy.any([x < 0 for x in tract_matrix.flatten()]): raise Exception("Negative values are not accepted in tracts matrix! " "Please check your file, and use values >= 0") if tract_matrix.shape != (expected_number_of_nodes, expected_number_of_nodes): raise Exception("Unexpected shape for tracts matrix! " "Should be %d x %d " % (expected_number_of_nodes, expected_number_of_nodes)) result.tract_lengths = tract_matrix if orientation is not None: if len(orientation) != expected_number_of_nodes: raise Exception("Invalid size for vector orientation. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.orientations = orientation if areas is not None: if len(areas) != expected_number_of_nodes: raise Exception("Invalid size for vector areas. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.areas = areas if cortical_vector is not None: if len(cortical_vector) != expected_number_of_nodes: raise Exception("Invalid size for vector cortical. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.cortical = cortical_vector if hemisphere_vector is not None: if len(hemisphere_vector) != expected_number_of_nodes: raise Exception("Invalid size for vector hemispheres. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.hemispheres = hemisphere_vector return result
def launch(self, view_model): # type: (ZIPConnectivityImporterModel) -> [ConnectivityIndex] """ Execute import operations: unpack ZIP and build Connectivity object as result. :raises LaunchException: when `uploaded` is empty or nonexistent :raises Exception: when * weights or tracts matrix is invalid (negative values, wrong shape) * any of the vector orientation, areas, cortical or hemisphere is \ different from the expected number of nodes """ if view_model.uploaded is None: raise LaunchException( "Please select ZIP file which contains data to import") files = self.storage_interface.unpack_zip(view_model.uploaded, self.get_storage_path()) weights_matrix = None centres = None labels_vector = None tract_matrix = None orientation = None areas = None cortical_vector = None hemisphere_vector = None for file_name in files: file_name_low = file_name.lower() if self.WEIGHT_TOKEN in file_name_low: weights_matrix = self.read_list_data(file_name) elif self.CENTRES_TOKEN in file_name_low or self.CENTRES_TOKEN2 in file_name_low: centres = self.read_list_data(file_name, usecols=[1, 2, 3]) labels_vector = self.read_list_data(file_name, dtype=numpy.str, usecols=[0]) elif self.TRACT_TOKEN in file_name_low: tract_matrix = self.read_list_data(file_name) elif self.ORIENTATION_TOKEN in file_name_low: orientation = self.read_list_data(file_name) elif self.AREA_TOKEN in file_name_low: areas = self.read_list_data(file_name) elif self.CORTICAL_INFO in file_name_low: cortical_vector = self.read_list_data(file_name, dtype=numpy.bool) elif self.HEMISPHERE_INFO in file_name_low: hemisphere_vector = self.read_list_data(file_name, dtype=numpy.bool) # Clean remaining text-files. self.storage_interface.remove_files(files, True) result = Connectivity() # Fill positions if centres is None: raise Exception( "Region centres are required for Connectivity Regions! " "We expect a file that contains *centres* inside the uploaded ZIP." ) expected_number_of_nodes = len(centres) if expected_number_of_nodes < 2: raise Exception("A connectivity with at least 2 nodes is expected") result.centres = centres if labels_vector is not None: result.region_labels = labels_vector # Fill and check weights if weights_matrix is not None: if weights_matrix.shape != (expected_number_of_nodes, expected_number_of_nodes): raise Exception( "Unexpected shape for weights matrix! " "Should be %d x %d " % (expected_number_of_nodes, expected_number_of_nodes)) result.weights = weights_matrix if view_model.normalization: result.weights = result.scaled_weights( view_model.normalization) # Fill and check tracts. Allow empty files for tracts, they will be computed by tvb-library. if tract_matrix is not None: if tract_matrix.size != 0: if numpy.any([x < 0 for x in tract_matrix.flatten()]): raise Exception( "Negative values are not accepted in tracts matrix! " "Please check your file, and use values >= 0") if tract_matrix.shape != (expected_number_of_nodes, expected_number_of_nodes): raise Exception( "Unexpected shape for tracts matrix! " "Should be %d x %d " % (expected_number_of_nodes, expected_number_of_nodes)) result.tract_lengths = tract_matrix if orientation is not None: if len(orientation) != expected_number_of_nodes: raise Exception( "Invalid size for vector orientation. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.orientations = orientation if areas is not None: if len(areas) != expected_number_of_nodes: raise Exception( "Invalid size for vector areas. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.areas = areas if cortical_vector is not None: if len(cortical_vector) != expected_number_of_nodes: raise Exception( "Invalid size for vector cortical. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.cortical = cortical_vector if hemisphere_vector is not None: if len(hemisphere_vector) != expected_number_of_nodes: raise Exception( "Invalid size for vector hemispheres. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.hemispheres = hemisphere_vector result.configure() return self.store_complete(result)
def launch(self, uploaded, rotate_x=0, rotate_y=0, rotate_z=0): """ Execute import operations: unpack ZIP and build Connectivity object as result. """ if uploaded is None: raise LaunchException( "Please select ZIP file which contains data to import") files = FilesHelper().unpack_zip(uploaded, self.storage_path) weights_matrix = None centres = None labels_vector = None tract_matrix = None orientation = None areas = None cortical_vector = None hemisphere_vector = None for file_name in files: if file_name.lower().find(self.WEIGHT_TOKEN) >= 0: weights_matrix = read_list_data(file_name) continue if file_name.lower().find(self.POSITION_TOKEN) >= 0: centres = read_list_data(file_name, skiprows=1, usecols=[1, 2, 3]) labels_vector = read_list_data(file_name, dtype=numpy.str, skiprows=1, usecols=[0]) continue if file_name.lower().find(self.TRACT_TOKEN) >= 0: tract_matrix = read_list_data(file_name) continue if file_name.lower().find(self.ORIENTATION_TOKEN) >= 0: orientation = read_list_data(file_name) continue if file_name.lower().find(self.AREA_TOKEN) >= 0: areas = read_list_data(file_name) continue if file_name.lower().find(self.CORTICAL_INFO) >= 0: cortical_vector = read_list_data(file_name, dtype=numpy.bool) continue if file_name.lower().find(self.HEMISPHERE_INFO) >= 0: hemisphere_vector = read_list_data(file_name, dtype=numpy.bool) continue ### Clean remaining text-files. FilesHelper.remove_files(files, True) result = Connectivity() result.storage_path = self.storage_path result.nose_correction = [rotate_x, rotate_y, rotate_z] ### Fill positions if centres is None: raise Exception( "Positions for Connectivity Regions are required! " "We expect a file *position* inside the uploaded ZIP.") expected_number_of_nodes = len(centres) if expected_number_of_nodes < 2: raise Exception("A connectivity with at least 2 nodes is expected") result.centres = centres if labels_vector is not None: result.region_labels = labels_vector ### Fill and check weights if weights_matrix is not None: if numpy.any([x < 0 for x in weights_matrix.flatten()]): raise Exception( "Negative values are not accepted in weights matrix! " "Please check your file, and use values >= 0") if weights_matrix.shape != (expected_number_of_nodes, expected_number_of_nodes): raise Exception( "Unexpected shape for weights matrix! " "Should be %d x %d " % (expected_number_of_nodes, expected_number_of_nodes)) result.weights = weights_matrix ### Fill and check tracts if tract_matrix is not None: if numpy.any([x < 0 for x in tract_matrix.flatten()]): raise Exception( "Negative values are not accepted in tracts matrix! " "Please check your file, and use values >= 0") if tract_matrix.shape != (expected_number_of_nodes, expected_number_of_nodes): raise Exception( "Unexpected shape for tracts matrix! " "Should be %d x %d " % (expected_number_of_nodes, expected_number_of_nodes)) result.tract_lengths = tract_matrix if orientation is not None: if len(orientation) != expected_number_of_nodes: raise Exception( "Invalid size for vector orientation. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.orientations = orientation if areas is not None: if len(areas) != expected_number_of_nodes: raise Exception( "Invalid size for vector areas. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.areas = areas if cortical_vector is not None: if len(cortical_vector) != expected_number_of_nodes: raise Exception( "Invalid size for vector cortical. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.cortical = cortical_vector if hemisphere_vector is not None: if len(hemisphere_vector) != expected_number_of_nodes: raise Exception( "Invalid size for vector hemispheres. " "Expected the same as region-centers number %d" % expected_number_of_nodes) result.hemispheres = hemisphere_vector return result