Beispiel #1
0
    def _get_data(self):
        """
            Loads data and metadata for the C. elegans connectome.
        """
        # Get subfolder with .obj files
        subdirs = get_subdirs(self.data_folder)
        if not subdirs:
            raise ValueError(
                "The data folder should include a subfolder which stores the neurons .obj files"
            )

        try:
            self.objs_fld = [f for f in subdirs if "objs_smoothed" in f][0]
        except:
            raise FileNotFoundError(
                "Could not find subdirectory with .obj files")

        # Get filepath to each .obj
        self.neurons_files = [
            f for f in listdir(self.objs_fld) if f.lower().endswith(".obj")
        ]

        # Get synapses and skeleton files
        try:
            skeletons_file = [
                f for f in listdir(self.data_folder)
                if f.endswith("skeletons.json")
            ][0]
        except:
            raise FileNotFoundError(
                "Could not find file with neurons skeleton data")

        try:
            synapses_file = [
                f for f in listdir(self.data_folder)
                if f.endswith("synapses.csv")
            ][0]
        except:
            raise FileNotFoundError("Could not find file with synapses data")

        # load data
        self.skeletons_data = load_json(skeletons_file)
        self.synapses_data = pd.read_csv(synapses_file, sep=";")

        # Get neurons metadata
        try:
            metadata_file = [
                f for f in listdir(self.data_folder)
                if "neuron_metadata.csv" in f
            ][0]
        except:
            raise FileNotFoundError(
                f"Could not find neurons metadata file {metadata_file}")

        self.neurons_metadata = pd.read_csv(metadata_file)
        self.neurons_names = list(self.neurons_metadata.neuron.values)
Beispiel #2
0
    def _load_cached_neuron(self, neuron_name):
        """
		Load a cached neuron's VTK actors

		:param neuron_name: str with neuron name

		"""
        if not neuron_name or neuron_name is None:
            return None

        if self.render_neurites:
            allowed_components = ['soma', 'axon', 'dendrites']
            skipped_components = []
        else:
            allowed_components = ['soma']
            skipped_components = ['axon', 'dendrites']

        neuron_files = [
            f for f in listdir(self.morphology_cache) if neuron_name in f
        ]
        if not neuron_files: return None
        else:
            neuron_actors = {}
            for f in neuron_files:
                component = os.path.split(f)[-1].split(".")[0].split("_")[-1]
                if component not in allowed_components and component not in skipped_components:
                    raise ValueError(
                        "Weird file name, unsure what to do: {}".format(f))
                elif component not in allowed_components and component in skipped_components:
                    continue
                neuron_actors[component] = load(f)
            return neuron_actors
Beispiel #3
0
    def _load_cached_neuron(self, neuron_name, params):
        """
		Load a cached neuron's VTK actors

		:param neuron_name: str with neuron name

		"""
        if not neuron_name or neuron_name is None:
            return None

        # Load the yaml file with metadata about previously rendered neurons
        if not neuron_name in self.cache_metadata.keys(
        ):  # the neuron was cached before the metadata was in place
            return None

        # Check if the params match those of the cached neuron
        cached_params = self.cache_metadata[neuron_name]
        diff_params = [v1 for v1,v2 in zip(cached_params.values(), params.values())\
             if v1 != v2]
        if diff_params:
            return None

        # All is good with the params, load cached actors
        if self.render_neurites:
            if self.render_dendrites and self.render_axons:
                allowed_components = ['soma', 'axon', 'dendrites']
                skipped_components = []
            elif not self.render_dendrites and not self.render_axons:
                allowed_components = ['soma']
                skipped_components = ['axon', 'dendrites']
            elif not self.render_dendrites:
                allowed_components = ['soma', 'axon']
                skipped_components = ['dendrites']
            else:
                allowed_components = ['soma', 'dendrites']
                skipped_components = ['axon']
        else:
            allowed_components = ['soma']
            skipped_components = ['axon', 'dendrites']

        neuron_files = [
            f for f in listdir(self.morphology_cache) if neuron_name in f
        ]
        if not neuron_files: return None
        else:
            neuron_actors = {}
            for f in neuron_files:
                component = os.path.split(f)[-1].split(".")[0].split("_")[-1]
                if component not in allowed_components and component not in skipped_components:
                    raise ValueError(
                        "Weird file name, unsure what to do: {}".format(f))
                elif component not in allowed_components and component in skipped_components:
                    continue
                neuron_actors[component] = load(f)
            return neuron_actors
Beispiel #4
0
def load_cached_gene(cache, metric, grid_size):
    """
        Loads a gene's data from cache
    """
    files = [
        f for f in listdir(cache) if metric in f and not f.endswith(".mhd")
    ]
    if not files:
        return None
    if len(files) > 1:
        raise NotImplementedError("Deal with more than one file found")
    else:
        return read_raw(files[0], grid_size)
Beispiel #5
0
	def add_neuron(self, neuron, shadow_axis=None, shadow_offset=-20,   
					**kwargs):
		if isinstance(neuron, list):
			neurons = neuron
		else:
			if isinstance(neuron, str):
				if os.path.isdir(neuron):
					neurons = listdir(neuron)
			else:
				neurons = [neuron]
		
		actors = []
		for neuron in neurons:
			if isinstance(neuron, str):
				neuron = self.parse_neuron_swc(neuron,  **kwargs)
			elif isinstance(neuron, Morphology):
				neuron = self.parse_neurons_swc_allen(neuron, **kwargs)

			actor = self.scene.add_vtkactor(neuron)


			# scals = actor.points()[:, 1]
			# alphas = np.linspace(0.82, .83, 250)
			# actor.pointColors(scals, alpha=alphas, cmap="Greens_r")

			# actor.points()[:, 0] += np.random.normal(0, 2000)
			# actor.points()[:, 2] += np.random.normal(0, 2000)

			if shadow_axis == 'x':
				actor.addShadow(x = shadow_offset)
			elif shadow_axis == 'y':
				actor.addShadow(y = shadow_offset)
			elif shadow_axis == 'z':
				actor.addShadow(z = shadow_offset)
			
			actors.append(neuron)




		
		return actors
Beispiel #6
0
def test_io():
    fld = os.getcwd()
    if not isinstance(listdir(fld), list):
        raise ValueError(f"listdir returned: {listdir(fld)}")
    if not isinstance(get_subdirs(fld), list):
        raise ValueError(f"get_subdirs returned: {get_subdirs(fld)}")
Beispiel #7
0
def test_streamlines():
    scene = Scene()
    streamlines_files = listdir("Examples/example_files/streamlines")[:2]
    scene.add_streamlines(streamlines_files, color="green")