diff --git a/jaxley/modules/cell.py b/jaxley/modules/cell.py index 10b0f700..90bdd995 100644 --- a/jaxley/modules/cell.py +++ b/jaxley/modules/cell.py @@ -27,8 +27,6 @@ loc_of_index, remap_to_consecutive, ) -from jaxley.utils.swc import swc_to_jaxley - class Cell(Module): """Cell class. @@ -306,82 +304,3 @@ def rotate(self, degrees: float, rotation_axis: str = "xy"): """ nodes = self.set_global_index_and_index(self.view) self.pointer._rotate(degrees=degrees, rotation_axis=rotation_axis, view=nodes) - - -def read_swc( - fname: str, - nseg: int, - max_branch_len: float = 300.0, - min_radius: Optional[float] = None, - assign_groups: bool = False, -) -> Cell: - """Reads SWC file into a `jx.Cell`. - - Jaxley assumes cylindrical compartments and therefore defines length and radius - for every compartment. The surface area is then 2*pi*r*length. For branches - consisting of a single traced point we assume for them to have area 4*pi*r*r. - Therefore, in these cases, we set lenght=2*r. - - Args: - fname: Path to the swc file. - nseg: The number of compartments per branch. - max_branch_len: If a branch is longer than this value it is split into two - branches. - min_radius: If the radius of a reconstruction is below this value it is clipped. - assign_groups: If True, then the identity of reconstructed points in the SWC - file will be used to generate groups `undefined`, `soma`, `axon`, `basal`, - `apical`, `custom`. See here: - http://www.neuronland.org/NLMorphologyConverter/MorphologyFormats/SWC/Spec.html - - Returns: - A `jx.Cell` object. - """ - parents, pathlengths, radius_fns, types, coords_of_branches = swc_to_jaxley( - fname, max_branch_len=max_branch_len, sort=True, num_lines=None - ) - nbranches = len(parents) - - non_split = 1 / nseg - range_ = np.linspace(non_split / 2, 1 - non_split / 2, nseg) - - comp = Compartment() - branch = Branch([comp for _ in range(nseg)]) - cell = Cell( - [branch for _ in range(nbranches)], parents=parents, xyzr=coords_of_branches - ) - - radiuses = np.asarray([radius_fns[b](range_) for b in range(len(parents))]) - radiuses_each = radiuses.ravel(order="C") - if min_radius is None: - assert np.all( - radiuses_each > 0.0 - ), "Radius 0.0 in SWC file. Set `read_swc(..., min_radius=...)`." - else: - radiuses_each[radiuses_each < min_radius] = min_radius - - lengths_each = np.repeat(pathlengths, nseg) / nseg - - cell.set("length", lengths_each) - cell.set("radius", radiuses_each) - - # Description of SWC file format: - # http://www.neuronland.org/NLMorphologyConverter/MorphologyFormats/SWC/Spec.html - ind_name_lookup = { - 0: "undefined", - 1: "soma", - 2: "axon", - 3: "basal", - 4: "apical", - 5: "custom", - } - types = np.asarray(types).astype(int) - if assign_groups: - for type_ind in np.unique(types): - if type_ind < 5.5: - name = ind_name_lookup[type_ind] - else: - name = f"custom{type_ind}" - indices = np.where(types == type_ind)[0].tolist() - if len(indices) > 0: - cell.branch(indices).add_to_group(name) - return cell