Utils
compute_axial_conductances(comp_edges, params)
¶
Given comp_edges
, radius, length, r_a, cm, compute the axial conductances.
Note that the resulting axial conductances will already by divided by the
capacitance cm
.
Source code in jaxley/utils/cell_utils.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
|
compute_children_and_parents(branch_edges)
¶
Build indices used during `._init_morph_custom_spsolve().
Source code in jaxley/utils/cell_utils.py
480 481 482 483 484 485 486 487 488 |
|
compute_children_indices(parents)
¶
Return all children indices of every branch.
Example:
parents = [-1, 0, 0]
compute_children_indices(parents) -> [[1, 2], [], []]
Source code in jaxley/utils/cell_utils.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
compute_coupling_cond(rad1, rad2, r_a1, r_a2, l1, l2)
¶
Return the coupling conductance between two compartments.
Equations taken from https://en.wikipedia.org/wiki/Compartmental_neuron_models
.
radius
: um
r_a
: ohm cm
length_single_compartment
: um
coupling_conds
: S * um / cm / um^2 = S / cm / um -> *10**7 -> mS / cm^2
Source code in jaxley/utils/cell_utils.py
227 228 229 230 231 232 233 234 235 236 237 238 |
|
compute_coupling_cond_branchpoint(rad, r_a, l)
¶
Return the coupling conductance between one compartment and a comp with l=0.
From https://en.wikipedia.org/wiki/Compartmental_neuron_models
If one compartment has l=0.0 then the equations simplify.
R_long = \sum_i r_a * L_i/2 / crosssection_i
with crosssection = pi * r**2
For a single compartment with L>0, this turns into: R_long = r_a * L/2 / crosssection
Then, g_long = crosssection * 2 / L / r_a
Then, the effective conductance is g_long / zylinder_area. So: g = pi * r**2 * 2 / L / r_a / 2 / pi / r / L g = r / r_a / L**2
Source code in jaxley/utils/cell_utils.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
compute_impact_on_node(rad, r_a, l)
¶
Compute the weight with which a compartment influences its node.
In order to satisfy Kirchhoffs current law, the current at a branch point must be
proportional to the crosssection of the compartment. We only require proportionality
here because the branch point equation reads:
g_1 * (V_1 - V_b) + g_2 * (V_2 - V_b) = 0.0
Because R_long = r_a * L/2 / crosssection, we get g_long = crosssection * 2 / L / r_a \propto rad**2 / L / r_a
This equation can be multiplied by any constant.
Source code in jaxley/utils/cell_utils.py
264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
compute_morphology_indices_in_levels(num_branchpoints, child_belongs_to_branchpoint, par_inds, child_inds)
¶
Return (row, col) to build the sparse matrix defining the voltage eqs.
This is run at init
, not during runtime.
Source code in jaxley/utils/cell_utils.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
|
convert_point_process_to_distributed(current, radius, length)
¶
Convert current point process (nA) to distributed current (uA/cm2).
This function gets called for synapses and for external stimuli.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current |
ndarray
|
Current in |
required |
radius |
ndarray
|
Compartment radius in |
required |
length |
ndarray
|
Compartment length in |
required |
Return
Current in uA/cm2
.
Source code in jaxley/utils/cell_utils.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
equal_segments(branch_property, nseg_per_branch)
¶
Generates segments where some property is the same in each segment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
branch_property |
list
|
List of values of the property in each branch. Should have
|
required |
Source code in jaxley/utils/cell_utils.py
15 16 17 18 19 20 21 22 23 |
|
get_num_neighbours(num_children, nseg_per_branch, num_branches)
¶
Number of neighbours of each compartment.
Source code in jaxley/utils/cell_utils.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
group_and_sum(values_to_sum, inds_to_group_by, num_branchpoints)
¶
Group values by whether they have the same integer and sum values within group.
This is used to construct the last diagonals at the branch points.
Written by ChatGPT.
Source code in jaxley/utils/cell_utils.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
interpolate_xyz(loc, coords)
¶
Perform a linear interpolation between xyz-coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loc |
float
|
The location in [0,1] along the branch. |
required |
coords |
ndarray
|
Array containing the reconstructed xyzr points of the branch. |
required |
Return
Interpolated xyz coordinate at loc
, shape `(3,).
Source code in jaxley/utils/cell_utils.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
linear_segments(initial_val, endpoint_vals, parents, nseg_per_branch)
¶
Generates segments where some property is linearly interpolated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_val |
float
|
The value at the tip of the soma. |
required |
endpoint_vals |
list
|
The value at the endpoints of each branch. |
required |
Source code in jaxley/utils/cell_utils.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
loc_of_index(global_comp_index, global_branch_index, nseg_per_branch)
¶
Return location corresponding to global compartment index.
Source code in jaxley/utils/cell_utils.py
219 220 221 222 223 224 |
|
local_index_of_loc(loc, global_branch_ind, nseg_per_branch)
¶
Returns the local index of a comp given a loc [0, 1] and the index of a branch.
This is used because we specify locations such as synapses as a value between 0 and 1. We have to convert this onto a discrete segment here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
branch_ind |
Index of the branch. |
required | |
loc |
float
|
Location (in [0, 1]) along that branch. |
required |
nseg_per_branch |
int
|
Number of segments of each branch. |
required |
Returns:
Type | Description |
---|---|
int
|
The local index of the compartment. |
Source code in jaxley/utils/cell_utils.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
|
merge_cells(cumsum_num_branches, cumsum_num_branchpoints, arrs, exclude_first=True)
¶
Build full list of which branches are solved in which iteration.
From the branching pattern of single cells, this “merges” them into a single ordering of branches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cumsum_num_branches |
List[int]
|
cumulative number of branches. E.g., for three cells with
10, 15, and 5 branches respectively, this will should be a list containing
|
required |
arrs |
List[List[ndarray]]
|
A list of a list of arrays that should be merged. |
required |
exclude_first |
bool
|
If |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
A list of arrays which contain the branch indices that are computed at each |
ndarray
|
level (i.e., iteration). |
Source code in jaxley/utils/cell_utils.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
params_to_pstate(params, indices_set_by_trainables)
¶
Make outputs get_parameters()
conform with outputs of .data_set()
.
make_trainable()
followed by params=get_parameters()
does not return indices
because these indices would also be differentiated by jax.grad
(as soon as
the params
are passed to def simulate(params)
. Therefore, in jx.integrate
,
we run the function to add indices to the dict. The outputs of params_to_pstate
are of the same shape as the outputs of .data_set()
.
Source code in jaxley/utils/cell_utils.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
|
query_channel_states_and_params(d, keys, idcs)
¶
Get dict with subset of keys and values from d.
This is used to restrict a dict where every item contains all states to only the ones that are relevant for the channel. E.g.
states = {'eCa': Array([ 0., 0., nan]}
will be
states = {'eCa': Array([ 0., 0.]}
Only loops over necessary keys, as opposed to looping over d.items()
.
Source code in jaxley/utils/cell_utils.py
401 402 403 404 405 406 407 408 409 410 411 412 413 |
|
remap_to_consecutive(arr)
¶
Maps an array of integers to an array of consecutive integers.
E.g. [0, 0, 1, 4, 4, 6, 6] -> [0, 0, 1, 2, 2, 3, 3]
Source code in jaxley/utils/cell_utils.py
279 280 281 282 283 284 285 |
|
compute_rotation_matrix(axis, angle)
¶
Return the rotation matrix associated with counterclockwise rotation about the given axis by the given angle.
Can be used to rotate a coordinate vector by multiplying it with the rotation matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
axis |
ndarray
|
The axis of rotation. |
required |
angle |
float
|
The angle of rotation in radians. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
A 3x3 rotation matrix. |
Source code in jaxley/utils/plot_utils.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
create_cone_frustum_mesh(length, radius_bottom, radius_top, bottom_dome=False, top_dome=False)
¶
Generates mesh points for a cone frustum, with optional domes at either end.
This is used to render the traced morphology in 3D (and to project it to 2D)
as part of plot_morph
. Sections between two traced coordinates with two
different radii can be represented by a cone frustum. Additionally, the ends
of the frustum can be capped with hemispheres to ensure that two neighbouring
frustums are connected smoothly (like ball joints).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
length |
float
|
The length of the frustum. |
required |
radius_bottom |
float
|
The radius of the bottom of the frustum. |
required |
radius_top |
float
|
The radius of the top of the frustum. |
required |
bottom_dome |
bool
|
If True, a dome is added to the bottom of the frustum.
The dome is a hemisphere with radius |
False
|
top_dome |
bool
|
If True, a dome is added to the top of the frustum.
The dome is a hemisphere with radius |
False
|
Returns:
Type | Description |
---|---|
ndarray
|
An array of mesh points. |
Source code in jaxley/utils/plot_utils.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
create_cylinder_mesh(length, radius)
¶
Generates mesh points for a cylinder.
This is used to render cylindrical compartments in 3D (and to project it to 2D)
as part of plot_comps
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
length |
float
|
The length of the cylinder. |
required |
radius |
float
|
The radius of the cylinder. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An array of mesh points. |
Source code in jaxley/utils/plot_utils.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
create_sphere_mesh(radius)
¶
Generates mesh points for a sphere.
This is used to render spherical compartments in 3D (and to project it to 2D)
as part of plot_comps
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
radius |
float
|
The radius of the sphere. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An array of mesh points. |
Source code in jaxley/utils/plot_utils.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
extract_outline(points)
¶
Get the outline of a 2D/3D shape.
Extracts the subset of points which form the convex hull, i.e. the outline of the input points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points |
ndarray
|
An array of points / corrdinates. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
An array of points which form the convex hull. |
Source code in jaxley/utils/plot_utils.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
plot_comps(module_or_view, view, dims=(0, 1), col='k', ax=None, comp_plot_kwargs={}, true_comp_length=True)
¶
Plot compartmentalized neural mrophology.
Plots the projection of the cylindrical compartments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_or_view |
Union[Module, View]
|
The module or view to plot. |
required |
view |
DataFrame
|
The view of the module. |
required |
dims |
Tuple[int]
|
The dimensions to plot / to project the cylinder onto, i.e. [0,1] xy-plane or [0,1,2] for 3D. |
(0, 1)
|
col |
str
|
The color for all compartments |
'k'
|
ax |
Optional[Axes]
|
The matplotlib axis to plot on. |
None
|
comp_plot_kwargs |
Dict
|
The plot kwargs for plt.fill. |
{}
|
true_comp_length |
bool
|
If True, the length of the compartment is used, i.e. the length of the traced neurite. This means for zig-zagging neurites the cylinders will be longer than the straight-line distance between the start and end point of the neurite. This can lead to overlapping and miss-aligned cylinders. Setting this False will use the straight-line distance instead for nicer plots. |
True
|
Returns:
Type | Description |
---|---|
Axes
|
Plot of the compartmentalized morphology. |
Source code in jaxley/utils/plot_utils.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
|
plot_graph(xyzr, dims=(0, 1), col='k', ax=None, type='line', morph_plot_kwargs={})
¶
Plot morphology.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xyzr |
ndarray
|
The coordinates of the morphology. |
required |
dims |
Tuple[int]
|
Which dimensions to plot. 1=x, 2=y, 3=z coordinate. Must be a tuple of two or three of them. |
(0, 1)
|
col |
str
|
The color for all branches. |
'k'
|
ax |
Optional[Axes]
|
The matplotlib axis to plot on. |
None
|
type |
str
|
Either |
'line'
|
morph_plot_kwargs |
Dict
|
The plot kwargs for plt.plot or plt.scatter. |
{}
|
Source code in jaxley/utils/plot_utils.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
plot_mesh(mesh_points, orientation, center, dims, ax=None, **kwargs)
¶
Plot the 2D projection of a volume mesh on a cardinal plane.
Project the projection of a cylinder that is oriented in 3D space. - Create cylinder mesh - rotate cylinder mesh to orient it lengthwise along a given orientation vector. - move its center - project onto plane - compute outline of projected mesh. - fill area inside the outline
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mesh_points |
ndarray
|
coordinates of the xyz mesh that define the volume |
required |
orientation |
ndarray
|
orientation vector. The cylinder will be oriented along this vector. |
required |
center |
ndarray
|
The x,y,z coordinates of the center of the cylinder. |
required |
dims |
Tuple[int]
|
The dimensions to plot / to project the cylinder onto, |
required |
ax |
Axes
|
The matplotlib axis to plot on. |
None
|
Returns:
Type | Description |
---|---|
Axes
|
Plot of the cylinder projection. |
Source code in jaxley/utils/plot_utils.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
plot_morph(module_or_view, view, dims=(0, 1), col='k', ax=None, morph_plot_kwargs={})
¶
Plot the detailed morphology.
Plots the traced morphology it was traced. That means at every point that was
traced a disc of radius r
is plotted. The outline of the discs are then
connected to form the morphology. This means every trace segement can be
represented by a cone frustum. To prevent breaks in the morphology, each
segement is connected with a ball joint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_or_view |
Union[Module, View]
|
The module or view to plot. |
required |
view |
DataFrame
|
The view dataframe of the module. |
required |
dims |
Tuple[int]
|
The dimensions to plot / to project the cylinder onto, i.e. [0,1] xy-plane or [0,1,2] for 3D. |
(0, 1)
|
col |
str
|
The color for all branches |
'k'
|
ax |
Optional[Axes]
|
The matplotlib axis to plot on. |
None
|
morph_plot_kwargs |
Dict
|
The plot kwargs for plt.fill. |
{}
|
Returns:
Type | Description |
---|---|
Axes
|
Plot of the detailed morphology. |
Source code in jaxley/utils/plot_utils.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
|
build_radiuses_from_xyzr(radius_fns, branch_indices, min_radius, nseg)
¶
Return the radiuses of branches given SWC file xyzr.
Returns an array of shape (num_branches, nseg)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
radius_fns |
List[Callable]
|
Functions which, given compartment locations return the radius. |
required |
branch_indices |
List[int]
|
The indices of the branches for which to return the radiuses. |
required |
min_radius |
Optional[float]
|
If passed, the radiuses are clipped to be at least as large. |
required |
nseg |
int
|
The number of compartments that every branch is discretized into. |
required |
Source code in jaxley/utils/swc.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
|
swc_to_jaxley(fname, max_branch_len=100.0, sort=True, num_lines=None)
¶
Read an SWC file and bring morphology into jaxley
compatible formats.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fname |
str
|
Path to swc file. |
required |
max_branch_len |
float
|
Maximal length of one branch. If a branch exceeds this length,
it is split into equal parts such that each subbranch is below
|
100.0
|
num_lines |
Optional[int]
|
Number of lines of the SWC file to read. |
None
|
Source code in jaxley/utils/swc.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
nested_checkpoint_scan(f, init, xs, length=None, *, nested_lengths, scan_fn=jax.lax.scan, checkpoint_fn=jax.checkpoint)
¶
A version of lax.scan that supports recursive gradient checkpointing.
Code taken from: https://github.com/google/jax/issues/2139
The interface of nested_checkpoint_scan
exactly matches lax.scan, except for
the required nested_lengths
argument.
The key feature of nested_checkpoint_scan
is that gradient calculations
require O(max(nested_lengths)) memory, vs O(prod(nested_lengths)) for unnested
scans, which it achieves by re-evaluating the forward pass
len(nested_lengths) - 1
times.
nested_checkpoint_scan
reduces to lax.scan
when nested_lengths
has a
single element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[Carry, Dict[str, ndarray]], Tuple[Carry, Output]]
|
function to scan over. |
required |
init |
Carry
|
initial value. |
required |
xs |
Dict[str, ndarray]
|
scanned over values. |
required |
length |
Optional[int]
|
leading length of all dimensions |
None
|
nested_lengths |
Sequence[int]
|
required list of lengths to scan over for each level of
checkpointing. The product of nested_lengths must match length (if
provided) and the size of the leading axis for all arrays in |
required |
scan_fn |
function matching the API of lax.scan |
scan
|
|
checkpoint_fn |
Callable[[Func], Func]
|
function matching the API of jax.checkpoint. |
checkpoint
|
Source code in jaxley/utils/jax_utils.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
gather_synapes(number_of_compartments, post_syn_comp_inds, current_each_synapse_voltage_term, current_each_synapse_constant_term)
¶
Compute current at the post synapse.
All this does it that it sums the synaptic currents that come into a particular compartment. It returns an array of as many elements as there are compartments.
Source code in jaxley/utils/syn_utils.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|