Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization modules #71

Merged
merged 46 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
1980fa2
initial import of optimization modules
iraikov Nov 1, 2023
f5827e2
update to poetry lock file
iraikov Nov 3, 2023
09f9f0e
factoring out click interface for optimize_network
iraikov Nov 3, 2023
a4446f2
added missing imports from optimization in network clamp
iraikov Nov 7, 2023
8ff512a
formatting fix
iraikov Nov 7, 2023
4bdc036
Merge branch 'main' into feature/optimization
frthjf Nov 15, 2023
4dbd831
various bug fixes in infrastructure scripts
iraikov Nov 16, 2023
27d9e74
opto stim: make expProb parameter optional and set default value to 1
iraikov Nov 16, 2023
b591580
Merge branch 'main' into feature/optimization
frthjf Jan 23, 2024
30cd8fa
Merge branch 'main' into feature/optimization
iraikov Jan 23, 2024
3656fbd
Merge branch 'feature/optimization' of https://github.com/GazzolaLab/…
iraikov Jan 23, 2024
ff9380b
Merge branch 'main' into feature/optimization
frthjf Mar 5, 2024
d905d79
Fix always-true network shutdown
frthjf Mar 6, 2024
9051357
Update poetry lock
frthjf Mar 6, 2024
de4c25d
Merge branch 'feature/optimization' of https://github.com/GazzolaLab/…
iraikov Mar 6, 2024
92a81e5
multiple fixes to network clamp optimization and synapse parameter up…
iraikov Mar 7, 2024
8a64e43
fixes related to optimize_network
iraikov Mar 7, 2024
d35f8ea
Apply black formatting
frthjf Mar 18, 2024
a8172f9
Merge branch 'main' into feature/optimization
frthjf Mar 19, 2024
57ffa4a
Merge branch 'main' into feature/optimization
frthjf Mar 21, 2024
2213729
Disable MPI for non-mpi interfaces
frthjf Mar 22, 2024
b765f7a
Merge branch 'main' into feature/optimization
frthjf Mar 22, 2024
975d311
Merge branch 'main' into feature/optimization
frthjf Mar 23, 2024
7c5f216
added missing call to subworlds in network setup;
iraikov Mar 24, 2024
e65ed8e
Merge branch 'feature/optimization' of https://github.com/GazzolaLab/…
iraikov Mar 24, 2024
5604aea
Apply codestyle
frthjf Mar 24, 2024
367825b
Remove target_trj_rate_map_dict residuals
frthjf Mar 25, 2024
55252e7
Add Config.clamp attribute
frthjf Mar 25, 2024
e1c63c9
Merge branch 'main' into feature/optimization
frthjf Mar 25, 2024
6657a4f
further fixes to support for reduced cell models
iraikov Mar 25, 2024
dac9808
Merge branch 'main' into feature/optimization
frthjf Apr 3, 2024
13b2798
Merge branch 'main' into feature/optimization
frthjf Apr 4, 2024
cd75b8f
Apply style
frthjf Apr 4, 2024
6c80245
bug fixes in check_synapses
iraikov Apr 4, 2024
fa8f87e
Merge branch 'feature/optimization' of https://github.com/GazzolaLab/…
iraikov Apr 4, 2024
8761f5c
bug fixes in reposition_trees
iraikov Apr 5, 2024
5b0c33d
small refactor of measure_distances to avoid use of bcast_cell_attrib…
iraikov Apr 5, 2024
1fd8aaa
generate_connections: use population dict from Env
iraikov Apr 5, 2024
fb5f32a
bug fixes in generate_connections
iraikov Apr 5, 2024
f2402b5
Rollback hardcoded types
frthjf Apr 6, 2024
6204bec
More type checker fixes
frthjf Apr 6, 2024
6aaa347
Ignore missing populations in make_h5types
frthjf Apr 6, 2024
2682ce0
Merge branch 'main' into feature/optimization
frthjf Apr 6, 2024
504c6a5
Raise more meaningful error for population defintion mismatch
frthjf Apr 9, 2024
5b87a38
connection generator: added chunk_size setting to append_graph calls
iraikov Apr 11, 2024
e690b39
Merge branch 'feature/optimization' of https://github.com/GazzolaLab/…
iraikov Apr 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading