Skip to content

Commit

Permalink
Add Config helper to simplify config file validation
Browse files Browse the repository at this point in the history
  • Loading branch information
frthjf committed Nov 15, 2023
1 parent 1a1bb08 commit 129a73a
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 15 deletions.
69 changes: 68 additions & 1 deletion src/miv_simulator/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
import copy
from pydantic import (
BaseModel as _BaseModel,
Field,
Expand Down Expand Up @@ -300,3 +300,70 @@ def probabilities_sum_to_one(x):


CellDistributions = Dict[PopulationName, CellDistribution]

SynapticProjections = Dict[
PostSynapticPopulationName,
List[PreSynapticPopulationName],
]

sentinel = object()


class Config:
def __init__(self, data: Dict) -> None:
self._data = copy.deepcopy(data)

# compatibility
self.get("Cell Types.STIM", {}).setdefault("synapses", {})

@classmethod
def from_yaml(cls, filepath: str) -> "Config":
from miv_simulator.utils import from_yaml

return cls(from_yaml(filepath))

def get(self, path: str, default=sentinel, splitter: str = "."):
d = self._data
for key in path.split(splitter):
if key not in d:
if default is not sentinel:
return default

d = d[key]

return d

@property
def cell_distributions(self) -> CellDistributions:
return self.get("Geometry.Cell Distribution")

@property
def layer_extents(self) -> LayerExtents:
return self.get("Geometry.Parametric Surface.Layer Extents")

@property
def geometry_rotation(self) -> Rotation:
return self.get("Geometry.Parametric Surface.Rotation", (0.0, 0.0, 0.0))

@property
def geometry_origin(self) -> Origin:
return self.get(
"Geometry.Parametric Surface.Origin",
{"U": "median", "V": "median", "L": "max"},
)

@property
def axon_extents(self) -> AxonExtents:
return self.get("Connection Generator.Axon Extent")

@property
def synapses(self) -> Synapses:
return self.get("Connection Generator.Synapses")

@property
def projections(self) -> SynapticProjections:
return {post: list(pre.keys()) for post, pre in self.synapses.items()}

@property
def cell_types(self) -> CellTypes:
return self.get("Cell Types")
4 changes: 2 additions & 2 deletions src/miv_simulator/geometry/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def interp_soma_distances(
return soma_distances


# !deprecated, use distance interpolant
# !for imperative API, use distance interpolant
def make_distance_interpolant(
comm, geometry_config, make_volume, resolution=[30, 30, 10], nsample=1000
):
Expand Down Expand Up @@ -696,7 +696,7 @@ def RBFInterpolant(
return origin_ranges, ip_dist_u, ip_dist_v


# !deprecated, use measure_soma_distances
# !for imperative API, use measure_soma_distances
def measure_distances(
comm,
geometry_config,
Expand Down
5 changes: 1 addition & 4 deletions src/miv_simulator/interface/h5_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ class Config(BaseModel):
model_config = ConfigDict(extra="forbid")

cell_distributions: config.CellDistributions = Field("???")
projections: Dict[
config.PostSynapticPopulationName,
List[config.PreSynapticPopulationName],
] = Field("???")
projections: config.SynapticProjections = Field("???")

def config_from_file(self, filename: str) -> Dict:
return from_yaml(filename)
Expand Down
2 changes: 1 addition & 1 deletion src/miv_simulator/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def plot_coordinates(
return ax


# !deprecated, needs refactoring
# !needs refactoring
def plot_coords_in_volume(
populations,
coords_path,
Expand Down
6 changes: 3 additions & 3 deletions src/miv_simulator/simulator/distribute_synapses.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def mpi_excepthook(type, value, traceback):
sys.excepthook = mpi_excepthook


# !deprecated, use update_synapse_statistics instead
# !for imperative API, use update_synapse_statistics instead
def update_syn_stats(env, syn_stats_dict, syn_dict):
return update_synapse_statistics(syn_dict, syn_stats_dict)

Expand Down Expand Up @@ -114,7 +114,7 @@ def local_syn_summary(syn_stats_dict):
return str.join("\n", res)


# !deprecated, use check_synapses instead
# !for imperative API, use check_synapses instead
def check_syns(
gid,
morph_dict,
Expand Down Expand Up @@ -179,7 +179,7 @@ def check_synapses(
)


# !deprecated, use distribute_synapses instead
# !for imperative API, use distribute_synapses instead
def distribute_synapse_locations(
config,
template_path,
Expand Down
4 changes: 2 additions & 2 deletions src/miv_simulator/simulator/generate_connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def mpi_excepthook(type, value, traceback):
sys.excepthook = mpi_excepthook


# !deprecated, use distance connections instead
# !for imperative API, use distance connections instead
def generate_distance_connections(
config,
include,
Expand Down Expand Up @@ -209,7 +209,7 @@ def generate_connections(
f"Generating connectivity for populations {destination_populations}..."
)

# !deprecated, does not seem applicable any longer
# !for imperative API, does not seem applicable any longer
# if len(soma_distances) == 0:
# (origin_ranges, ip_dist_u, ip_dist_v) = make_distance_interpolant(
# env, resolution=resolution, nsample=interp_chunk_size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def rho(x):
return in_nodes


# !deprecated, use generate_network_architecture() instead
# !for imperative API, use generate_network_architecture() instead
def generate_soma_coordinates(
config: str,
types_path: str,
Expand Down
2 changes: 1 addition & 1 deletion src/miv_simulator/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def write_to_yaml(file_path, data, convert_scalars=False):
)


# !deprecated, use from_yaml instead
# !for imperative API, use from_yaml instead
def read_from_yaml(
file_path: str, include_loader: None = None
) -> Dict[str, Dict[str, Dict[str, Union[Dict[str, float], Dict[str, int]]]]]:
Expand Down

0 comments on commit 129a73a

Please sign in to comment.