Skip to content

Commit

Permalink
remove placement-algo dep
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudon committed Nov 7, 2024
1 parent c42d131 commit 061df57
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
2 changes: 0 additions & 2 deletions requirements/base.pip
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
atlas_analysis>=0.0.5
bluepymm>=0.8.5
bluepyparallel>=0.0.8
brainbuilder>=0.20
Expand All @@ -19,7 +18,6 @@ neurom>=3.2.2,<4
neurots>=3.6,<4
numpy>=1.26.4
pandas>=1.5.3
placement_algorithm>=2.3.1
PyYAML>=6
region_grower>=1.3,<2
scipy>=1.10
Expand Down
13 changes: 9 additions & 4 deletions src/synthesis_workflow/synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
from neurom.check.morphology_checks import has_apical_dendrite
from neurom.core.dataformat import COLS
from neurots import extract_input
from placement_algorithm.app import utils
from placement_algorithm.app.choose_morphologies import Master as ChooseMorphologyMaster

try:
from placement_algorithm.app.choose_morphologies import Master as ChooseMorphologyMaster

with_placement_algo = True
except ImportError:
with_placement_algo = False
from tmd.io.io import load_population
from tqdm import tqdm
from voxcell import CellCollection
Expand Down Expand Up @@ -279,7 +284,7 @@ def create_axon_morphologies_tsv(
f"Either 'morphs_df_path' or all the following parameter should be None: {_params}"
)

if all(check_placement_params.values()):
if all(check_placement_params.values()) and with_placement_algo:
L.info("Use placement algorithm for axons")

kwargs = {
Expand Down Expand Up @@ -320,7 +325,7 @@ def create_axon_morphologies_tsv(
random_state=42,
)["name"].to_list()

utils.dump_morphology_list(axon_morphs, axon_morphs_path)
axon_morphs.to_csv(axon_morphs_path, sep="\t", na_rep="N/A")


def get_target_length(soma_layer, target_layer, cortical_thicknesses):
Expand Down
4 changes: 2 additions & 2 deletions src/synthesis_workflow/tasks/synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from neurots.generate.orientations import fit_3d_angles
from neurots.validator import validate_neuron_distribs
from neurots.validator import validate_neuron_params
from placement_algorithm.app.compact_annotations import _collect_annotations
from region_grower.synthesize_morphologies import SynthesizeMorphologies
from region_grower.utils import NumpyEncoder
from tqdm import tqdm
Expand All @@ -33,6 +32,7 @@
from synthesis_workflow.synthesis import get_axon_base_dir
from synthesis_workflow.synthesis import get_neurite_types
from synthesis_workflow.synthesis import rescale_morphologies
from synthesis_workflow.utils import collect_annotations
from synthesis_workflow.tasks.circuit import SliceCircuit
from synthesis_workflow.tasks.config import CircuitConfig
from synthesis_workflow.tasks.config import DiametrizerConfig
Expand Down Expand Up @@ -280,7 +280,7 @@ class CreateAnnotationsFile(WorkflowTask):
def run(self):
"""Actual process of the task."""
# pylint: disable=protected-access
annotations = _collect_annotations(self.annotation_dir, self.morph_db)
annotations = collect_annotations(self.annotation_dir, self.morph_db)

with open(self.destination, "w", encoding="utf-8") as f:
json.dump(annotations, f, indent=4, sort_keys=True)
Expand Down
39 changes: 39 additions & 0 deletions src/synthesis_workflow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import json
import logging
from pathlib import Path
from tqdm import tqdm
import lxml.etree

import dictdiffer
import numpy as np
Expand Down Expand Up @@ -192,3 +194,40 @@ def create_circuit_config(nodes_file, morphology_path):
"edges": [],
}
}


def parse_annotations(filepath):
"""Parse XML with morphology annotations."""
etree = lxml.etree.parse(filepath)
result = {}
for elem in etree.findall("placement"):
attr = dict(elem.attrib)
rule_id = attr.pop("rule")
if rule_id in result:
raise KeyError(f"Duplicate annotation for rule '{rule_id}'")
result[rule_id] = attr
return result


def parse_morphdb(filepath):
"""Parse (ext)neuronDB.dat file."""
columns = ["morphology", "layer", "mtype"]
first_row = pd.read_csv(filepath, sep=r"\s+", header=None, nrows=1)
if first_row.shape[1] > 3:
columns.append("etype")
return pd.read_csv(
filepath, sep=r"\s+", names=columns, usecols=columns, na_filter=False, dtype={"layer": str}
)


def collect_annotations(annotation_dir, morphdb_path):
result = {}
if morphdb_path is None:
for filepath in tqdm(Path(annotation_dir).glob("*.xml")):
result[Path(filepath).stem] = parse_annotations(filepath)
else:
morphdb = parse_morphdb(morphdb_path)
for morph in tqdm(morphdb["morphology"].unique()):
filepath = Path(annotation_dir) / (morph + ".xml")
result[morph] = parse_annotations(filepath)
return result

0 comments on commit 061df57

Please sign in to comment.