Skip to content

Commit

Permalink
Updates the way the neuronDB files are found.
Browse files Browse the repository at this point in the history
Change-Id: I89d245a70f56b12e9d6c32c26c72297c082cfea6
  • Loading branch information
arnaudon authored and adrien-berchet committed Nov 19, 2020
1 parent 7bace29 commit 4a4269e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/synthesis_workflow/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def slice_per_mtype(cells, mtypes):

def slice_n_cells(cells, n_cells, random_state=0):
"""Selects n_cells random cells per mtypes."""
if n_cells <= 0:
return cells
sampled_cells = pd.DataFrame()
for mtype in cells.mtype.unique():
samples = cells[cells.mtype == mtype].sample(
Expand Down
23 changes: 11 additions & 12 deletions src/synthesis_workflow/tasks/synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from synthesis_workflow.tasks.luigi_tools import WorkflowTask
from synthesis_workflow.tasks.utils import GetSynthesisInputs
from synthesis_workflow.tools import ensure_dir
from synthesis_workflow.tools import find_case_insensitive_file
from synthesis_workflow.tools import load_neurondb_to_dataframe


Expand Down Expand Up @@ -79,11 +80,13 @@ def requires(self):
def run(self):
""""""

L.debug("Build morphology dataframe from %s", self.neurondb_path)
neurondb_path = find_case_insensitive_file(self.neurondb_path)

L.debug("Build morphology dataframe from %s", neurondb_path)

mtype_taxonomy_path = self.input().ppath / self.mtype_taxonomy_path
morphs_df = load_neurondb_to_dataframe(
self.neurondb_path,
neurondb_path,
self.morphology_dirs,
mtype_taxonomy_path,
self.apical_points_path,
Expand Down Expand Up @@ -323,19 +326,17 @@ class BuildAxonMorphologies(WorkflowTask):
nb_jobs = luigi.IntParameter(default=20, description="Number of workers.")
"""int: Number of workers."""

def get_neuron_db_path(self, db_name):
def get_neuron_db_path(self, ext):
"""Helper function to fix neuronDB vs neurondb in file names."""
return (
Path(self.axon_cells_path) / Path(self.neurondb_basename).parent / db_name
).with_suffix(".xml")
return (Path(self.axon_cells_path) / self.neurondb_basename).with_suffix(
"." + ext
)

def requires(self):
""""""
tasks = {"circuit": SliceCircuit()}

neurondb_path = self.get_neuron_db_path("neurondb")
if not neurondb_path.exists():
neurondb_path = self.get_neuron_db_path("neuronDB")
neurondb_path = self.get_neuron_db_path("xml")

tasks["axon_cells"] = BuildAxonMorphsDF(
neurondb_path=neurondb_path,
Expand All @@ -353,9 +354,7 @@ def run(self):
axon_cells = self.input()["axon_cells"].path
else:
axon_cells = None
neurondb_path = (
Path(self.axon_cells_path) / self.neurondb_basename
).with_suffix(".dat")
neurondb_path = find_case_insensitive_file(self.get_neuron_db_path("dat"))

if any(
[
Expand Down
42 changes: 42 additions & 0 deletions src/synthesis_workflow/tools.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""utils functions."""
import glob
import json
import logging
import sys
Expand All @@ -20,6 +21,8 @@
from voxcell import VoxelData
from voxcell.nexus.voxelbrain import LocalAtlas

L = logging.getLogger(__name__)


def add_mtype_taxonomy(morphs_df, mtype_taxonomy):
"""From a dict with mtype to morph_class, fill in the morphs_df dataframe.
Expand Down Expand Up @@ -120,6 +123,45 @@ def ensure_dir(file_path):
Path(file_path).parent.mkdir(parents=True, exist_ok=True)


def find_case_insensitive_file(path):
"""Helper function to find a file ignoring case."""

def either(c):
return "[%s%s]" % (c.lower(), c.upper()) if c.isalpha() else c

# Return the exact path if it exists
exact_path = Path(path)
if exact_path.exists():
return exact_path

directory = exact_path.parent

# Try to find the file ignoring the case
pattern = "".join(map(either, exact_path.name))
complete_path = (directory / pattern).as_posix()

all_possible_files = glob.glob(complete_path)

if len(all_possible_files) == 1:
found_file = all_possible_files[0]
L.warning(
"The file '%s' was not found, using '%s' instead.", exact_path, found_file
)
return found_file
else:
if len(all_possible_files) == 0:
error_msg = (
f"The file '{exact_path.as_posix()}' could not be found even "
"ignoring case."
)
else:
error_msg = (
f"The file '{exact_path}' could not be found but several files were found "
f"ignoring case: {all_possible_files}"
)
raise ValueError(error_msg)


def update_morphs_df(morphs_df_path, new_morphs_df):
"""Updates a morphs_df with new entries to preserve duplicates."""
return pd.read_csv(morphs_df_path).merge(new_morphs_df, how="left")
Expand Down
2 changes: 1 addition & 1 deletion src/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Package version."""
VERSION = "0.0.7"
VERSION = "0.0.8.dev0"

0 comments on commit 4a4269e

Please sign in to comment.