Skip to content

Commit

Permalink
Merge pull request #71 from GazzolaLab/feature/optimization
Browse files Browse the repository at this point in the history
Optimization modules
  • Loading branch information
iraikov authored Apr 13, 2024
2 parents feaf583 + e690b39 commit 0d6e856
Show file tree
Hide file tree
Showing 30 changed files with 1,752 additions and 510 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: check-json
- id: end-of-file-fixer
- id: requirements-txt-fixer
- repo: https://github.com/psf/black
rev: 23.1.0
rev: 24.3.0
hooks:
- id: black
additional_dependencies: ["click==8.0.4"]
182 changes: 113 additions & 69 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "miv-simulator"
version = "0.0.3"
version = "0.0.4"
description = "Mind-in-Vitro simulator"
readme = "README.md"
authors = []
Expand Down Expand Up @@ -28,6 +28,7 @@ matplotlib = "^3.6.0"
quantities = "^0.15.0"
pydantic = "^2.3.0"
neuron = "^8.2.3"
distgfs = {version = "^1.1.0", optional = true}


[tool.poetry.dev-dependencies]
Expand Down Expand Up @@ -88,6 +89,7 @@ check-config = "scripts.tools.check_config:main"
cut-slice = "scripts.tools.cut_slice:main"
sample-cells = "scripts.tools.sample_cells:main"
reposition-trees = "scripts.tools.reposition_trees:main"
optimize-network = "scripts.optimization.optimize_network:main_cli"


[build-system]
Expand Down
85 changes: 69 additions & 16 deletions src/miv_simulator/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ def __init__(
self.random.seed(self.gid)
self.spike_detector = None
self.spike_onset_delay = 0.0
self.is_reduced = True
if not isinstance(cell_config, BRKconfig):
raise RuntimeError(
"BRKneuron: argument cell_attrs must be of type BRKconfig"
Expand Down Expand Up @@ -422,6 +421,10 @@ def ais(self):
def hillock(self):
return self.nodes["hillock"]

@property
def is_reduced(self):
return True


class PRneuron:
"""
Expand Down Expand Up @@ -458,7 +461,6 @@ def __init__(
self.random.seed(self.gid)
self.spike_detector = None
self.spike_onset_delay = 0.0
self.is_reduced = True
if not isinstance(cell_config, PRconfig):
raise RuntimeError(
"PRneuron: argument cell_attrs must be of type PRconfig"
Expand Down Expand Up @@ -558,6 +560,10 @@ def ais(self):
def hillock(self):
return self.nodes["hillock"]

@property
def is_reduced(self):
return True


class SCneuron:
"""
Expand Down Expand Up @@ -611,7 +617,6 @@ def __init__(
self.random.seed(self.gid)
self.spike_detector = None
self.spike_onset_delay = 0.0
self.is_reduced = True

SC_nrn = h.SC_nrn()

Expand Down Expand Up @@ -678,6 +683,10 @@ def ais(self) -> List[Any]:
def hillock(self):
return self.nodes["hillock"]

@property
def is_reduced(self):
return True


class BiophysCell:
"""
Expand Down Expand Up @@ -951,20 +960,18 @@ def import_morphology_from_hoc(
sec_info_dict = {}
root_sec = None
for sec_type, sec_index_list in default_hoc_sec_lists.items():
hoc_sec_attr_name = sec_type
hoc_sec_attr_name = f"{sec_type}_list"
if not hasattr(hoc_cell, hoc_sec_attr_name):
hoc_sec_attr_name = f"{sec_type}_list"
hoc_sec_attr_name = sec_type
if hasattr(hoc_cell, hoc_sec_attr_name) and (
getattr(hoc_cell, hoc_sec_attr_name) is not None
):
sec_list = list(getattr(hoc_cell, hoc_sec_attr_name))
hoc_obj = getattr(hoc_cell, hoc_sec_attr_name)
if hasattr(hoc_cell, sec_index_list):
sec_indexes = list(getattr(hoc_cell, sec_index_list))
else:
raise AttributeError(
"import_morphology_from_hoc: %s is not an attribute of the hoc cell"
% sec_index_list
)
sec_indexes = list(range(len(sec_list)))
if sec_type == "soma":
root_sec = sec_list[0]
for sec, index in zip(sec_list, sec_indexes):
Expand Down Expand Up @@ -1355,6 +1362,34 @@ def filter_nodes(
return result


def get_mech_rules_dict(cell, **rules):
"""
Used by modify_mech_param. Takes in a series of arguments and constructs a validated rules dictionary that will be
used to update a cell's mechanism dictionary.
:param cell: :class:'BiophysCell'
:param rules: dict
:return: dict
"""
rules_dict = {
name: rules[name]
for name in (
name
for name in ["value", "origin"]
if name in rules and rules[name] is not None
)
}
if "origin" in rules_dict:
origin_type = rules_dict["origin"]
valid_sec_types = [
sec_type for sec_type in cell.nodes if len(cell.nodes[sec_type]) > 0
]
if origin_type not in valid_sec_types + ["parent", "branch_origin"]:
raise ValueError(
f"get_mech_rules_dict: cannot inherit from invalid origin type: {origin_type}"
)
return rules_dict


def report_topology(
env: AbstractEnv, cell: BiophysCell, node: Optional[SectionNode] = None
) -> None:
Expand Down Expand Up @@ -1782,12 +1817,14 @@ def init_circuit_context(
syn_name,
zip_longest(
weights_syn_ids,
[
{"weight": Promise(expr_closure, [x])}
for x in weights_values
]
if expr_closure
else [{"weight": x} for x in weights_values],
(
[
{"weight": Promise(expr_closure, [x])}
for x in weights_values
]
if expr_closure
else [{"weight": x} for x in weights_values]
),
),
multiple=multiple_weights,
append=append_weights,
Expand Down Expand Up @@ -2058,11 +2095,16 @@ def make_biophys_cell(
)
_, tree_dict = next(tree_attr_iter)

hoc_cell = make_hoc_cell(
env, population_name, gid, neurotree_dict=tree_dict
)

cell = BiophysCell(
env=env,
gid=gid,
population_name=population_name,
hoc_cell=hoc_cell,
neurotree_dict=tree_dict,
env=env,
mech_file_path=mech_file_path,
mech_dict=mech_dict,
)
Expand Down Expand Up @@ -2474,3 +2516,14 @@ def record_cell(
recs.append(rec)

return recs


default_reduced_cell_constructors = {
"pr_nrn": make_PR_cell,
"brk_nrn": make_BRK_cell,
"sc_nrn": make_SC_cell,
}


def get_reduced_cell_constructor(template_name):
return default_reduced_cell_constructors.get(template_name.lower(), None)
20 changes: 4 additions & 16 deletions src/miv_simulator/clamps/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,11 @@ def init_biophys_cell(
else:
correct_for_spines_flag = False

reduced_cons = cells.get_reduced_cell_constructor(template_name)

## Load cell gid and its synaptic attributes and connection data
if template_name.lower() == "pr_nrn":
cell = cells.make_PR_cell(
env,
pop_name,
gid,
tree_dict=cell_dict.get("morph", None),
synapses_dict=cell_dict.get("synapse", None),
connection_graph=cell_dict.get("connectivity", None),
weight_dict=cell_dict.get("weight", None),
mech_dict=mech_dict,
load_synapses=True,
load_weights=load_weights,
load_edges=load_connections,
)
elif template_name.lower() == "sc_nrn":
cell = cells.make_SC_cell(
if reduced_cons is not None:
cell = reduced_cons(
env,
pop_name,
gid,
Expand Down
Loading

0 comments on commit 0d6e856

Please sign in to comment.