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

add init_params method #511

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions jaxley/channels/hh.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, name: Optional[str] = None):
f"{prefix}_eNa": 50.0,
f"{prefix}_eK": -77.0,
f"{prefix}_eLeak": -54.3,
f"celsius": 37.0,
}
self.channel_states = {
f"{prefix}_m": 0.2,
Expand Down Expand Up @@ -75,6 +76,17 @@ def init_state(self, states, v, params, delta_t):
f"{prefix}_h": alpha_h / (alpha_h + beta_h),
f"{prefix}_n": alpha_n / (alpha_n + beta_n),
}

def init_params(self, states, v, params):
"""Initialize the parameters given the temperature."""
prefix = self._name
q10 = 2.3
t = params["celsius"]
gna = q10 ** ((t - 37.0) / 10.0) * params[f"{prefix}_gNa"]
gk = q10 ** ((t - 37.0) / 10.0) * params[f"{prefix}_gK"]
gleak = q10 ** ((t - 37.0) / 10.0) * params[f"{prefix}_gLeak"]
return {f"{prefix}_gNa": gna, f"{prefix}_gK": gk, f"{prefix}_gLeak": gleak}


@staticmethod
def m_gate(v):
Expand Down
41 changes: 41 additions & 0 deletions jaxley/modules/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,47 @@ def init_states(self, delta_t: float = 0.025):
# no issues with overriding states).
self.nodes.loc[channel_indices, key] = val

@only_allow_module
def init_params(self):
"""Run `channel.init_params()` to initialize parameters."""
# Update states of the channels.
channel_nodes = self.base.nodes
states = self.base._get_states_from_nodes_and_edges()

# We do not use any `pstate` for initializing. In principle, we could change
# that by allowing an input `params` and `pstate` to this function.
# `voltage_solver` could also be `jax.sparse` here, because both of them
# build the channel parameters in the same way.
params = self.base.get_all_parameters([], voltage_solver="jaxley.thomas")

for channel in self.base.channels:
name = channel._name
channel_indices = channel_nodes.loc[channel_nodes[name]][
"global_comp_index"
].to_numpy()
voltages = channel_nodes.loc[channel_indices, "v"].to_numpy()

channel_param_names = list(channel.channel_params.keys())
channel_state_names = list(channel.channel_states.keys())
channel_states = query_channel_states_and_params(
states, channel_state_names, channel_indices
)
channel_params = query_channel_states_and_params(
params, channel_param_names, channel_indices
)

init_params = channel.init_params(
channel_states, voltages, channel_params
)

# `init_params` might not return all channel states. Only the ones that are
# returned are updated here.
for key, val in init_params.items():
# Note that we are overriding `self.nodes` here, but `self.nodes` is
# not used above to actually compute the current states (so there are
# no issues with overriding states).
self.nodes.loc[channel_indices, key] = val

def _init_morph_for_debugging(self):
"""Instandiates row and column inds which can be used to solve the voltage eqs.

Expand Down
Loading