Skip to content

Commit

Permalink
Morph cell pickling (#525)
Browse files Browse the repository at this point in the history
* mv: add io module and move swc things there

* fix: isort and import tests fix

* fix: fix module prefix for Cell

* fix: fix circular imports

* enh: add morph test case to pickling

* fix: make tests pass
  • Loading branch information
jnsbck authored Nov 21, 2024
1 parent 640103f commit 7e71f66
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
3 changes: 2 additions & 1 deletion jaxley/io/swc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from jaxley.utils.cell_utils import (
_build_parents,
_compute_pathlengths,
_padded_radius_generating_fn,
_radius_generating_fns,
_split_into_branches_and_sort,
build_radiuses_from_xyzr,
Expand Down Expand Up @@ -69,7 +70,7 @@ def swc_to_jaxley(
parents[1:] += 1
parents = parents.tolist()
pathlengths = [0.1] + pathlengths
radius_fns = [lambda x: content[0, 5] * np.ones_like(x)] + radius_fns
radius_fns = [_padded_radius_generating_fn(content[0, 5])] + radius_fns
sorted_branches = [[0]] + sorted_branches

# Type of padded section is assumed to be of `custom` type:
Expand Down
35 changes: 24 additions & 11 deletions jaxley/utils/cell_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This file is part of Jaxley, a differentiable neuroscience simulator. Jaxley is
# licensed under the Apache License Version 2.0, see <https://www.apache.org/licenses/>

from functools import partial
from math import pi
from typing import Callable, Dict, List, Optional, Tuple, Union
from warnings import warn
Expand Down Expand Up @@ -186,6 +187,28 @@ def _radius_generating_fns(
return radius_fns


def _padded_radius(loc: float, radiuses: np.ndarray) -> float:
return radiuses * np.ones_like(loc)


def _radius(loc: float, cutoffs: np.ndarray, radiuses: np.ndarray) -> float:
"""Function which returns the radius via linear interpolation.
Defined outside of `_radius_generating_fns` to allow for pickling of the resulting
Cell object."""
index = np.digitize(loc, cutoffs, right=False)
left_rad = radiuses[index - 1]
right_rad = radiuses[index]
left_loc = cutoffs[index - 1]
right_loc = cutoffs[index]
loc_within_bin = (loc - left_loc) / (right_loc - left_loc)
return left_rad + (right_rad - left_rad) * loc_within_bin


def _padded_radius_generating_fn(radiuses: np.ndarray) -> Callable:
return partial(_padded_radius, radiuses=radiuses)


def _radius_generating_fn(radiuses: np.ndarray, each_length: np.ndarray) -> Callable:
# Avoid division by 0 with the `summed_len` below.
each_length[each_length < 1e-8] = 1e-8
Expand All @@ -201,17 +224,7 @@ def _radius_generating_fn(radiuses: np.ndarray, each_length: np.ndarray) -> Call
if len(radiuses) == 1:
radiuses = np.tile(radiuses, 2)

def radius(loc: float) -> float:
"""Function which returns the radius via linear interpolation."""
index = np.digitize(loc, cutoffs, right=False)
left_rad = radiuses[index - 1]
right_rad = radiuses[index]
left_loc = cutoffs[index - 1]
right_loc = cutoffs[index]
loc_within_bin = (loc - left_loc) / (right_loc - left_loc)
return left_rad + (right_rad - left_rad) * loc_within_bin

return radius
return partial(_radius, cutoffs=cutoffs, radiuses=radiuses)


def _compute_pathlengths(
Expand Down
7 changes: 5 additions & 2 deletions tests/test_pickle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This file is part of Jaxley, a differentiable neuroscience simulator. Jaxley is
# licensed under the Apache License Version 2.0, see <https://www.apache.org/licenses/>

import os
import pickle

import pytest
Expand All @@ -9,11 +10,13 @@
from jaxley.channels import HH
from jaxley.synapses import IonotropicSynapse

# create modules
# create modules (cannot use fixtures for pickling, since they rely on local func defs)
comp = jx.Compartment()
branch = jx.Branch(comp, 4)
cell = jx.Cell([branch] * 3, [-1, 0, 0])
net = jx.Network([cell] * 2)
fname = os.path.join(os.path.dirname(__file__), "swc_files", "morph.swc")
morph_cell = jx.read_swc(fname, nseg=1, max_branch_len=2_000, assign_groups=True)

# insert mechanisms
net.cell(0).branch("all").insert(HH())
Expand All @@ -24,7 +27,7 @@


@pytest.mark.parametrize(
"module", [comp, branch, cell, net], ids=lambda x: x.__class__.__name__
"module", [comp, branch, cell, morph_cell, net], ids=lambda x: x.__class__.__name__
)
def test_pickle(module):
pickled = pickle.dumps(module)
Expand Down

0 comments on commit 7e71f66

Please sign in to comment.