-
Notifications
You must be signed in to change notification settings - Fork 12
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
.comp and .loc addresses #285 #288
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,18 +63,44 @@ def __init__( | |
# Coordinates. | ||
self.xyzr = [float("NaN") * np.zeros((2, 4))] | ||
|
||
def _compartment_view(self) -> CompartmentView: | ||
view = deepcopy(self.nodes) | ||
view["global_comp_index"] = view["comp_index"] | ||
view["global_branch_index"] = view["branch_index"] | ||
view["global_cell_index"] = view["cell_index"] | ||
return CompartmentView(self, view) | ||
|
||
@property | ||
def comp(self) -> CompartmentView: | ||
"""Return a compartment of the discretized branch. | ||
|
||
Args: | ||
index: integer or float between 0 to 1 for | ||
legacy. | ||
""" | ||
return self._compartment_view() | ||
|
||
def loc(self, loc: float) -> CompartmentView: | ||
"""Return compartment of the discretized branch that is | ||
closest to the provided location. | ||
|
||
Args: | ||
loc: float between 0 and 1 | ||
""" | ||
return self.comp.loc(loc) | ||
|
||
def __getattr__(self, key): | ||
# Ensure that hidden methods such as `__deepcopy__` still work. | ||
if key.startswith("__"): | ||
return super().__getattribute__(key) | ||
|
||
if key == "comp": | ||
view = deepcopy(self.nodes) | ||
view["global_comp_index"] = view["comp_index"] | ||
view["global_branch_index"] = view["branch_index"] | ||
view["global_cell_index"] = view["cell_index"] | ||
return CompartmentView(self, view) | ||
elif key in self.group_nodes: | ||
# if key == "comp": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for cleaning this up! (please remove the comments) |
||
# view = deepcopy(self.nodes) | ||
# view["global_comp_index"] = view["comp_index"] | ||
# view["global_branch_index"] = view["branch_index"] | ||
# view["global_cell_index"] = view["cell_index"] | ||
# return CompartmentView(self, view) | ||
if key in self.group_nodes: | ||
inds = self.group_nodes[key].index.values | ||
view = self.nodes.loc[inds] | ||
view["global_comp_index"] = view["comp_index"] | ||
|
@@ -138,6 +164,28 @@ def __call__(self, index: float): | |
new_view.view["comp_index"] -= new_view.view["comp_index"].iloc[0] | ||
return new_view | ||
|
||
def __getattr__(self, key): | ||
assert key == "comp" | ||
def _compartment_view(self) -> CompartmentView: | ||
return CompartmentView(self.pointer, self.view) | ||
|
||
@property | ||
def comp(self): | ||
"""Return a compartment of the discretized branch. | ||
|
||
Args: | ||
index: integer or float between 0 to 1 for | ||
legacy. | ||
""" | ||
return self._compartment_view() | ||
|
||
def loc(self, loc: float): | ||
"""Return compartment of the branch that is | ||
closest to the continuous float location between 0 and 1. | ||
|
||
Args: | ||
loc: float between 0 and 1 | ||
""" | ||
return self.comp.loc(loc) | ||
|
||
# def __getattr__(self, key): | ||
# assert key == "comp" | ||
# return CompartmentView(self.pointer, self.view) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from typing import Callable, Dict, List, Optional, Tuple | ||
from typing import Callable, Dict, List, Optional, Tuple, Union | ||
import warnings | ||
|
||
import jax.numpy as jnp | ||
import numpy as np | ||
|
@@ -63,7 +64,34 @@ def __init__(self, pointer, view): | |
view = view.assign(controlled_by_param=view.comp_index) | ||
super().__init__(pointer, view) | ||
|
||
def __call__(self, loc: float): | ||
def __call__(self, index: Union[float, int]) -> "CompartmentView": | ||
"""Selects a specific compartment with integer indexing from a view onto all compartments. | ||
|
||
The resulting object will also be a CompartmentView. | ||
""" | ||
if index == "all": | ||
pass | ||
else: | ||
# support for legacy code | ||
if isinstance(index, float) and 0 <= index <= 1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it is only 5 people hacking on Jaxley right now I do not think that we have to support legacy code. Please remove this. |
||
# map the float to an int index | ||
# i.e. the range [0, 1] to [0, N-1] where N is the number of segments | ||
mapped_index = index_of_loc(0, index, self.pointer.nseg) | ||
warnings.warn("Float values for 'index' are deprecated and will be removed in future versions. " | ||
"Use an integer index instead.", DeprecationWarning) | ||
index = mapped_index | ||
elif not isinstance(index, int): | ||
raise ValueError("Index must be an integer or a float between 0 and 1.") | ||
assert ( | ||
index >= 0 and index < self.pointer.nseg | ||
), f"Compartments must be indexed by a discrete value between 0 and {self.pointer.nseg - 1}. Provided was {index}." | ||
return super().adjust_view("comp_index", index) | ||
|
||
def loc(self, loc: float) -> "CompartmentView": | ||
"""Selects a specific compartment with relative location indexing from a view onto all compartments. | ||
|
||
The resulting object will also be a CompartmentView. | ||
""" | ||
if loc != "all": | ||
assert ( | ||
loc >= 0.0 and loc <= 1.0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it is only 5 people hacking on Jaxley right now I do not think that we have to support legacy code. Please remove the legacy option.