brainx-skill 1.0.6 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/adapters/gemini.js +7 -0
- package/adapters/opencode.js +7 -0
- package/adapters/windsurf.js +7 -0
- package/installation/lib/installer.js +4 -1
- package/manifest.json +5 -1
- package/package.json +7 -2
- package/skills/braincell/SKILL.md +182 -0
- package/skills/braincell/references/area-scaled-hh-pattern.md +54 -0
- package/skills/braincell/references/braincell-custom-ion-channel-authoring.md +210 -0
- package/skills/braincell/references/channel-library.md +170 -0
- package/skills/braincell/references/ion-library.md +154 -0
- package/skills/braincell/references/mixions-for-adaptation.md +81 -0
- package/skills/braincell/references/multicompartment/braincell-manual-morphology-construction.md +180 -0
- package/skills/braincell/references/multicompartment/cv-policy-reference.md +133 -0
- package/skills/braincell/references/multicompartment/filter-function-library.md +162 -0
- package/skills/braincell/references/multicompartment/morphology-io-loading-validation.md +135 -0
- package/skills/braincell/references/multicompartment/multicompartment-cell-workflow.md +391 -0
- package/skills/braincell/references/multicompartment/probe-reference.md +128 -0
- package/skills/braincell/references/multicompartment/topology-building-and-visualization.md +121 -0
- package/skills/braincell/references/scripts/calcium_channel_gating.py +66 -0
- package/skills/braincell/references/scripts/cell_multicompartment_reference.py +221 -0
- package/skills/braincell/references/scripts/channel_ablation.py +61 -0
- package/skills/braincell/references/scripts/fi_curve.py +68 -0
- package/skills/braincell/references/scripts/hh_neuron_basics.py +59 -0
- package/skills/braincell/references/scripts/spike_frequency_adaptation.py +75 -0
- package/skills/braincell/references/scripts/t_current_rebound.py +72 -0
- package/skills/braincell/references/scripts/thalamic_neurons.py +228 -0
- package/skills/braincell/references/solver-library-with-effects.md +188 -0
- package/skills/brainevent/SKILL.md +268 -0
- package/skills/brainevent/references/connectivity-variants.md +312 -0
- package/skills/brainevent/references/custom-operators-cpu.md +304 -0
- package/skills/brainevent/references/custom-operators-gpu.md +468 -0
- package/skills/brainevent/references/scripts/102_EI_net_1996.py +107 -0
- package/skills/brainevent/references/scripts/204_joglekar_2018_propagation.py +168 -0
- package/skills/brainevent/references/scripts/coba_ei_teaching.py +356 -0
- package/skills/brainevent/references/sparse-formats.md +182 -0
- package/skills/brainevent/references/synaptic-plasticity.md +285 -0
- package/skills/brainpy-state/SKILL.md +232 -0
- package/skills/brainpy-state/references/array-creation.md +158 -0
- package/skills/brainpy-state/references/brain-dynamics-delay-protocol.md +96 -0
- package/skills/brainpy-state/references/braintools/brainstate-control-flow-patterns.md +334 -0
- package/skills/brainpy-state/references/braintools/data-preprocessing.md +79 -0
- package/skills/brainpy-state/references/braintools/metric.md +152 -0
- package/skills/brainpy-state/references/braintools/optimizer.md +179 -0
- package/skills/brainpy-state/references/braintools/parameter-initializer.md +183 -0
- package/skills/brainpy-state/references/braintools/surrogate.md +147 -0
- package/skills/brainpy-state/references/component-selection.md +273 -0
- package/skills/brainpy-state/references/nest-compatible/devices.md +157 -0
- package/skills/brainpy-state/references/nest-compatible/divergence-and-parity.md +114 -0
- package/skills/brainpy-state/references/nest-compatible/integration-categories.md +41 -0
- package/skills/brainpy-state/references/nest-compatible/model-library.md +200 -0
- package/skills/brainpy-state/references/nest-compatible/nest-workflow.md +156 -0
- package/skills/brainpy-state/references/nest-compatible/network-building.md +264 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brette_et_al_2007.py +320 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brunel_alpha.py +108 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brunel_delta.py +93 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/clopath_synapse_spike_pairing.py +171 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/evaluate_tsodyks2_synapse.py +147 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/spatial_gaussex.py +112 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/synapsecollection.py +198 -0
- package/skills/brainpy-state/references/nest-compatible/synapse-and-connectivity.md +153 -0
- package/skills/brainpy-state/references/projection-patterns.md +125 -0
- package/skills/brainpy-state/references/scripts/103_COBA_2005.py +86 -0
- package/skills/brainpy-state/references/scripts/106_COBA_HH_2007.py +181 -0
- package/skills/brainpy-state/references/scripts/107_gamma_oscillation_1996.py +156 -0
- package/skills/brainpy-state/references/scripts/109_fast_global_oscillation.py +111 -0
- package/skills/brainpy-state/references/scripts/201_surrogate_grad_lif_fashion_mnist.py +221 -0
- package/skills/brainpy-state/references/scripts/training-snn.py +119 -0
- package/skills/brainstate/SKILL.md +1 -1
- package/skills/brainstate/references/brainstate/brainstate-control-flow-patterns.md +250 -399
- package/skills/brainstate/references/brainstate/brainstate-transformed-diagnostics.md +6 -2
- package/skills/brainstate/references/brainstate/parameter-constraints-regularization.md +16 -2
- package/skills/brainstate/references/brainstate/parameter-transforms-regularizers-catalog.md +8 -3
- package/skills/brainstate/references/brainstate/randomness-and-reproducibility.md +64 -143
- package/skills/brainstate/references/brainstate/transformation-grad-expansion.md +13 -1
- package/skills/brainstate/references/brainstate/transformation-jit-expansion.md +11 -1
- package/skills/brainstate/references/brainstate/transformation-vmap-expansion.md +35 -7
- package/skills/brainstate/references/collective_model_operations.md +9 -9
- package/skills/brainstate/references/extension_mechanisms.md +12 -1
- package/skills/brainstate/references/libraries/prebuilt-activation-library.md +106 -97
- package/skills/brainstate/references/libraries/prebuilt-layer-library.md +102 -176
- package/skills/brainstate/references/model-interop-and-migration.md +11 -1
- package/skills/brainstate/references/size-inference-variations.md +11 -0
- package/skills/brainstate/references/state-graph-operations.md +13 -15
- package/skills/brainstate/references/state_collections_and_utilities.md +12 -1
- package/skills/brainx-general-guard/SKILL.md +153 -0
- package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/advanced-randomness.md +303 -0
- package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/randomness-and-reproducibility.md +62 -0
- package/skills/brainstate/references/braintools-optimizer-reference.md +0 -133
- package/skills/brainstate/references/libraries/solver-library-with-effects.md +0 -85
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# CV policy reference
|
|
2
|
+
|
|
3
|
+
Use this reference after opening `references/multicompartment/multicompartment-cell-workflow.md` when control-volume resolution must be chosen, varied by branch type, inspected, or checked for numerical convergence.
|
|
4
|
+
|
|
5
|
+
## Discretization model
|
|
6
|
+
|
|
7
|
+
A `CVPolicy` converts each continuous morphology branch into normalized `(prox, dist)` intervals that become isopotential control volumes; more and shorter CVs resolve spatial voltage gradients with more State and computation.
|
|
8
|
+
|
|
9
|
+
`Cell` treats the policy output as the source of truth for compartment splitting. Morphology sample points and branches do not determine the final compartment count.
|
|
10
|
+
|
|
11
|
+
## Choose a policy
|
|
12
|
+
|
|
13
|
+
| API | Use when | Important behavior and result |
|
|
14
|
+
|---|---|---|
|
|
15
|
+
| `Cell(morphology, cv_policy=None)` | Use the package default for a structural baseline. | `None` selects `CVPerBranch()` with one CV per branch. |
|
|
16
|
+
| `CVPerBranch(cv_per_branch=1)` | A fixed, predictable count per branch is required. | It splits every branch uniformly in normalized coordinates and returns the same count for short and long branches. |
|
|
17
|
+
| `MaxCVLen(max_cv_len, keep_odd=True)` | Physical branch length should cap CV size. | It computes a count independently for each branch, then splits that branch uniformly so CV lengths do not exceed the target up to tolerance. |
|
|
18
|
+
| `DLambda(d_lambda, frequency=..., keep_odd=True)` | Detailed cable modeling should follow electrical rather than only geometric length. | It computes electrotonic length from diameter, axial resistivity, membrane capacitance, and frequency, then sizes CVs as a fraction `d_lambda` of the AC length constant. |
|
|
19
|
+
| `CVPolicyByTypeRule(branch_types, policy)` | One or more morphology branch types need a distinct policy. | It pairs a tuple of type names with the policy applied to matching branches. |
|
|
20
|
+
| `CompositeByTypePolicy(rules, default_policy)` | Soma, dendrite, axon, or custom branches need different rules. | It checks rules in order and applies `default_policy` when no rule matches. |
|
|
21
|
+
| `policy.resolve_cv_bounds(morphology, paint_rules=...)` | Implementing or testing a policy directly. | It returns branch-wise tuples of normalized `(prox, dist)` intervals; normal modeling should let `Cell` call it. |
|
|
22
|
+
|
|
23
|
+
Use `CVPerBranch` for a reproducible baseline or deliberately uniform branch resolution. Use `MaxCVLen` when the modeling assumption is a physical maximum compartment length. Use `DLambda` as the detailed cable-model starting point when electrical length should control refinement.
|
|
24
|
+
|
|
25
|
+
## Inspect a policy before simulation
|
|
26
|
+
|
|
27
|
+
Construct cells with candidate policies and inspect the actual CV records; do not infer the result from morphology point count.
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
import braincell
|
|
31
|
+
import brainunit as u
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
morphology = braincell.Morphology.from_swc("neuron.swc")
|
|
35
|
+
|
|
36
|
+
baseline = braincell.Cell(
|
|
37
|
+
morphology,
|
|
38
|
+
cv_policy=braincell.CVPerBranch(cv_per_branch=1),
|
|
39
|
+
)
|
|
40
|
+
length_limited = braincell.Cell(
|
|
41
|
+
morphology,
|
|
42
|
+
cv_policy=braincell.MaxCVLen(max_cv_len=20.0 * u.um),
|
|
43
|
+
)
|
|
44
|
+
electrical = braincell.Cell(
|
|
45
|
+
morphology,
|
|
46
|
+
cv_policy=braincell.DLambda(
|
|
47
|
+
d_lambda=0.1,
|
|
48
|
+
frequency=100.0 * u.Hz,
|
|
49
|
+
),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
for name, cell in (
|
|
53
|
+
("baseline", baseline),
|
|
54
|
+
("length_limited", length_limited),
|
|
55
|
+
("electrical", electrical),
|
|
56
|
+
):
|
|
57
|
+
assert cell.n_cv == len(cell.cvs)
|
|
58
|
+
print(name, cell.n_cv)
|
|
59
|
+
print([(cv.branch_id, cv.prox, cv.dist) for cv in cell.cvs[:4]])
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Inspect `cv.branch_id`, `cv.branch_type`, `cv.prox`, `cv.dist`, `cv.length`, and `cv.area` when count alone cannot explain the spatial layout.
|
|
63
|
+
|
|
64
|
+
## Apply policies by branch type
|
|
65
|
+
|
|
66
|
+
Compose type rules when one global resolution rule would over-resolve some cables or under-resolve others.
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import braincell
|
|
70
|
+
import brainunit as u
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
policy = braincell.CompositeByTypePolicy(
|
|
74
|
+
rules=(
|
|
75
|
+
braincell.CVPolicyByTypeRule(
|
|
76
|
+
branch_types=("soma",),
|
|
77
|
+
policy=braincell.CVPerBranch(cv_per_branch=3),
|
|
78
|
+
),
|
|
79
|
+
braincell.CVPolicyByTypeRule(
|
|
80
|
+
branch_types=(
|
|
81
|
+
"dendrite",
|
|
82
|
+
"basal_dendrite",
|
|
83
|
+
"apical_dendrite",
|
|
84
|
+
),
|
|
85
|
+
policy=braincell.DLambda(
|
|
86
|
+
d_lambda=0.1,
|
|
87
|
+
frequency=100.0 * u.Hz,
|
|
88
|
+
),
|
|
89
|
+
),
|
|
90
|
+
),
|
|
91
|
+
default_policy=braincell.MaxCVLen(max_cv_len=40.0 * u.um),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
cell = braincell.Cell(morphology, cv_policy=policy)
|
|
95
|
+
print("total CVs:", cell.n_cv)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Use the exact type strings present in the loaded morphology. Inspect branch types after import before relying on a by-type rule; unmatched or misspelled types silently take the default policy.
|
|
99
|
+
|
|
100
|
+
## Respect `DLambda` cable-property constraints
|
|
101
|
+
|
|
102
|
+
`DLambda` requires uniform axial resistivity (`ra`) and membrane capacitance (`cm`) within each branch because both enter the AC length constant. Painting a different `CableProperty` onto only a sub-interval of one branch raises `ValueError`.
|
|
103
|
+
|
|
104
|
+
Resting potential and temperature may vary within a branch without violating this specific `DLambda` constraint.
|
|
105
|
+
|
|
106
|
+
If sub-branch `ra` or `cm` variation is scientifically required, choose a compatible policy such as `MaxCVLen`, or split the geometry into branches whose cable properties are internally uniform.
|
|
107
|
+
|
|
108
|
+
Always pass `d_lambda` explicitly. Also pass `frequency` explicitly when reproducibility matters; do not copy the concept-page shorthand that omits the required `d_lambda` argument.
|
|
109
|
+
|
|
110
|
+
## Check spatial convergence
|
|
111
|
+
|
|
112
|
+
Treat CV policy as a numerical assumption, not a visualization preference.
|
|
113
|
+
|
|
114
|
+
1. Choose a policy from the spatial question and inspect `cell.n_cv` and `cell.cvs`.
|
|
115
|
+
2. Run the scientific protocol with fixed morphology, mechanisms, stimulus, solver, and `dt`.
|
|
116
|
+
3. Refine only the CV policy: increase `cv_per_branch`, reduce `max_cv_len`, or reduce `d_lambda`.
|
|
117
|
+
4. Compare the relevant traces, spike timing, peaks, attenuation, or other spatial observable at the same probe locations.
|
|
118
|
+
5. Accept the coarser policy only when the observable remains within the task's stated tolerance.
|
|
119
|
+
|
|
120
|
+
Do not change `dt`, solver, and CV policy in the same convergence comparison. A stable CV count does not establish time-step or solver convergence.
|
|
121
|
+
|
|
122
|
+
Open `references/multicompartment/topology-building-and-visualization.md` when CV coverage, locset ownership, or the runtime node mapping must be inspected. Return to `references/multicompartment/multicompartment-cell-workflow.md` for mechanism declaration and simulation.
|
|
123
|
+
|
|
124
|
+
## Sources
|
|
125
|
+
|
|
126
|
+
- [Discretization](https://brainx.chaobrain.com/braincell/concepts/discretization.html)
|
|
127
|
+
- [Cell in BrainCell](https://brainx.chaobrain.com/braincell/tutorials/cell.html)
|
|
128
|
+
- [CVPolicy API](https://brainx.chaobrain.com/braincell/apis/generated/braincell.CVPolicy.html)
|
|
129
|
+
- [CVPerBranch API](https://brainx.chaobrain.com/braincell/apis/generated/braincell.CVPerBranch.html)
|
|
130
|
+
- [MaxCVLen API](https://brainx.chaobrain.com/braincell/apis/generated/braincell.MaxCVLen.html)
|
|
131
|
+
- [DLambda API](https://brainx.chaobrain.com/braincell/apis/generated/braincell.DLambda.html)
|
|
132
|
+
- [CVPolicyByTypeRule API](https://brainx.chaobrain.com/braincell/apis/generated/braincell.CVPolicyByTypeRule.html)
|
|
133
|
+
- [CompositeByTypePolicy API](https://brainx.chaobrain.com/braincell/apis/generated/braincell.CompositeByTypePolicy.html)
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Filter function library
|
|
2
|
+
|
|
3
|
+
Use this reference after opening `references/multicompartment/multicompartment-cell-workflow.md` when `paint()` or `place()` needs explicit branches, anatomical types, metric ranges, set composition, structural points, or sampled locations.
|
|
4
|
+
|
|
5
|
+
## Selection model
|
|
6
|
+
|
|
7
|
+
A region selects continuous branch intervals for `cell.paint(...)`; a locset selects branch-local points for `cell.place(...)`. Keep selection expressions separate from the mechanisms attached to them.
|
|
8
|
+
|
|
9
|
+
| Selection | Materialized result | Attach with |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| `RegionExpr` | `RegionMask.intervals`, containing `(branch_id, prox, dist)` tuples | `cell.paint(region, *density_declarations)` |
|
|
12
|
+
| `LocsetExpr` | `LocsetMask.points` and human-readable `display_names` | `cell.place(locset, *point_declarations)` |
|
|
13
|
+
|
|
14
|
+
Resolve either expression with `morphology.select(expression)` before declaration when imported branch types, names, or topology may differ from the intended target.
|
|
15
|
+
|
|
16
|
+
## Region selectors
|
|
17
|
+
|
|
18
|
+
Use regions for cable properties, ions, channels, and other density declarations.
|
|
19
|
+
|
|
20
|
+
| API | Use when | Important behavior and result |
|
|
21
|
+
|---|---|---|
|
|
22
|
+
| `AllRegion()` | A declaration applies to every branch interval. | It selects the entire morphology. |
|
|
23
|
+
| `EmptyRegion()` | A compositional identity or intentionally empty target is required. | It selects no intervals. |
|
|
24
|
+
| `BranchSlice(branch_index, prox, dist)` | Exact branch ids and normalized branch intervals are known. | Scalar or parallel sequence inputs materialize explicit `(branch_id, prox, dist)` intervals. |
|
|
25
|
+
| `branch_in(property, values)` | A branch property must equal one value or belong to a set or tuple. | It is the helper form of `BranchInFilter(property, values)` and selects whole matching branches. |
|
|
26
|
+
| `BranchInFilter(property, values)` | The explicit selector class is preferable to the helper. | It has the same equality or membership behavior as `branch_in(...)`. |
|
|
27
|
+
| `branch_range(property, bounds, closed="neither")` | A numeric or BrainUnit quantity-valued branch property must lie in a range. | It is the helper form of `BranchRangeFilter`; use `None` for an open lower or upper bound. |
|
|
28
|
+
| `BranchRangeFilter(property, bounds, closed="neither")` | The explicit range-selector class is preferable to the helper. | `closed` controls whether the left, right, both, or neither bound is included. |
|
|
29
|
+
|
|
30
|
+
Supported branch properties in the documented implementation are:
|
|
31
|
+
|
|
32
|
+
| Family | Properties |
|
|
33
|
+
|---|---|
|
|
34
|
+
| Metadata and topology | `branch_id`, `name`, `type`, `parent_id`, `n_children`, `branch_order`, `n_tapers` |
|
|
35
|
+
| Geometry | `length`, `mean_radius`, `area`, `volume` |
|
|
36
|
+
|
|
37
|
+
Use BrainUnit quantities for physical bounds:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import brainunit as u
|
|
41
|
+
from braincell.filter import branch_in, branch_range
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
dendrites = branch_in(
|
|
45
|
+
"type",
|
|
46
|
+
("dendrite", "basal_dendrite", "apical_dendrite"),
|
|
47
|
+
)
|
|
48
|
+
long_branches = branch_range(
|
|
49
|
+
"length",
|
|
50
|
+
(100.0 * u.um, None),
|
|
51
|
+
closed="left",
|
|
52
|
+
)
|
|
53
|
+
target = dendrites & long_branches
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Do not use `branch_range(...)` to select a partial interval within each branch. It filters whole branches by a property; use `BranchSlice(...)` for branch-local intervals.
|
|
57
|
+
|
|
58
|
+
## Region set algebra
|
|
59
|
+
|
|
60
|
+
Compose small selectors rather than duplicating mechanism declarations.
|
|
61
|
+
|
|
62
|
+
| Expression | Result |
|
|
63
|
+
|---|---|
|
|
64
|
+
| `left | right` | Union of both regions. |
|
|
65
|
+
| `left & right` | Intersection of both regions. |
|
|
66
|
+
| `left - right` | Parts of `left` not selected by `right`. |
|
|
67
|
+
| `region.complement()` | All morphology cable outside the region. |
|
|
68
|
+
|
|
69
|
+
Overlapping slices are normalized in the materialized mask. Inspect `morphology.select(region).intervals` when exact interval boundaries matter.
|
|
70
|
+
|
|
71
|
+
## Locset selectors
|
|
72
|
+
|
|
73
|
+
Use locsets for clamps, probes, synapses, junctions, and other point declarations.
|
|
74
|
+
|
|
75
|
+
| API | Use when | Important behavior and result |
|
|
76
|
+
|---|---|---|
|
|
77
|
+
| `RootLocation(x)` | Select one normalized position on the root branch. | `RootLocation(0.5)` selects the root midpoint. |
|
|
78
|
+
| `AtLocation(branch, x)` | Select a known branch name or id and normalized position. | It returns one explicit branch-local point. |
|
|
79
|
+
| `at(branch, x)` | Use the helper form of `AtLocation`. | It returns the same explicit point selector. |
|
|
80
|
+
| `BranchPoints()` | Select bifurcation sites. | It chooses branch-point locations on the parent side. |
|
|
81
|
+
| `Terminals()` | Select terminal tips. | It chooses the distal endpoint of every terminal branch. |
|
|
82
|
+
| `UniformSamples(region, count)` | Place a fixed number of evenly distributed samples within a region. | It returns `count` branch-local points. |
|
|
83
|
+
| `RandomSamples(region, count, seed)` | Place reproducible random samples within a region. | The same morphology, region, count, and seed resolve to the same points. |
|
|
84
|
+
|
|
85
|
+
Locsets support `|`, `&`, and `-` for union, intersection, and difference. A composed locset still materializes as concrete `(branch_id, x)` points, and a multi-point locset expands one placed declaration per point during lowering.
|
|
86
|
+
|
|
87
|
+
## Verify and apply selectors
|
|
88
|
+
|
|
89
|
+
Resolve semantic selectors before attaching mechanisms, then use the same expressions for declaration and topology inspection.
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
import braincell
|
|
93
|
+
import braincell.mech as mech
|
|
94
|
+
import brainunit as u
|
|
95
|
+
from braincell.filter import Terminals, branch_in, branch_range
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
morphology = braincell.Morphology.from_swc("neuron.swc")
|
|
99
|
+
|
|
100
|
+
dendrites = branch_in(
|
|
101
|
+
"type",
|
|
102
|
+
("dendrite", "basal_dendrite", "apical_dendrite"),
|
|
103
|
+
)
|
|
104
|
+
long_dendrites = dendrites & branch_range(
|
|
105
|
+
"length",
|
|
106
|
+
(100.0 * u.um, None),
|
|
107
|
+
closed="left",
|
|
108
|
+
)
|
|
109
|
+
terminal_sites = Terminals()
|
|
110
|
+
|
|
111
|
+
region_mask = morphology.select(long_dendrites)
|
|
112
|
+
terminal_mask = morphology.select(terminal_sites)
|
|
113
|
+
|
|
114
|
+
assert region_mask.intervals, "no long dendrite matched the imported labels"
|
|
115
|
+
assert terminal_mask.points, "morphology has no terminal sites"
|
|
116
|
+
print(region_mask.intervals)
|
|
117
|
+
print(terminal_mask.display_names)
|
|
118
|
+
|
|
119
|
+
cell = braincell.Cell(morphology)
|
|
120
|
+
cell.paint(
|
|
121
|
+
long_dendrites,
|
|
122
|
+
mech.Channel(
|
|
123
|
+
"IL",
|
|
124
|
+
g_max=0.03 * u.mS / u.cm**2,
|
|
125
|
+
E=-65.0 * u.mV,
|
|
126
|
+
),
|
|
127
|
+
)
|
|
128
|
+
cell.place(terminal_sites, mech.StateProbe())
|
|
129
|
+
cell.init_state()
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Use a region only with `paint()` and a locset only with `place()`. A selector that resolves correctly against `Morphology` can still map unexpectedly after discretization; open `references/multicompartment/topology-building-and-visualization.md` to inspect CV ownership and runtime placement.
|
|
133
|
+
|
|
134
|
+
## Exported but unavailable selectors
|
|
135
|
+
|
|
136
|
+
The current filter API exports additional names, but the official filter tutorial marks these selectors as not implemented:
|
|
137
|
+
|
|
138
|
+
| Exported name | Intended family |
|
|
139
|
+
|---|---|
|
|
140
|
+
| `SubtreeRegion` | A branch and its distal subtree. |
|
|
141
|
+
| `RadiusRangeRegion` | Cable selected by local radius. |
|
|
142
|
+
| `TreeDistanceRegion` | Cable selected by along-tree distance. |
|
|
143
|
+
| `EuclideanDistanceRegion` | Cable selected by straight-line distance. |
|
|
144
|
+
| `RegionAnchors` | Region anchor construction. |
|
|
145
|
+
| `StepSamples` | Step-spaced locset sampling. |
|
|
146
|
+
|
|
147
|
+
Do not use an exported name as proof that the operation works. Prefer the implemented selectors above, construct equivalent explicit slices or points when practical, or inspect the installed BrainCell version before using a newly implemented surface.
|
|
148
|
+
|
|
149
|
+
## Common failures
|
|
150
|
+
|
|
151
|
+
- Do not pass a region to `place()` or a locset to `paint()`.
|
|
152
|
+
- Do not guess imported type strings; inspect `branch.type` values or materialize the selector first.
|
|
153
|
+
- Do not omit BrainUnit quantities from physical metric bounds.
|
|
154
|
+
- Do not confuse `branch_range(...)` property filtering with `BranchSlice(...)` interval selection.
|
|
155
|
+
- Do not use unseeded random placement when the model must be reproducible.
|
|
156
|
+
- Do not assume a nonempty morphology selector maps to the expected CV midpoint; inspect the topology after choosing the CV policy.
|
|
157
|
+
|
|
158
|
+
## Sources
|
|
159
|
+
|
|
160
|
+
- [Regions and locsets](https://brainx.chaobrain.com/braincell/concepts/regions_locsets.html)
|
|
161
|
+
- [Region and locset filters](https://brainx.chaobrain.com/braincell/tutorials/filter.html)
|
|
162
|
+
- [braincell.filter API](https://brainx.chaobrain.com/braincell/apis/filter.html)
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# BrainCell morphology IO, loading paths, and validation
|
|
2
|
+
|
|
3
|
+
Use this reference after opening `references/multicompartment/multicompartment-cell-workflow.md` when geometry must come from SWC, Neurolucida ASC, NeuroML2, NeuroMorpho.Org, or a BrainCell checkpoint. Keep manual branch construction in `references/multicompartment/braincell-manual-morphology-construction.md`.
|
|
4
|
+
|
|
5
|
+
## Choose a loading path
|
|
6
|
+
|
|
7
|
+
Each reader returns the same simulation-facing `Morphology`; choose the path from the source format and the validation evidence the task requires.
|
|
8
|
+
|
|
9
|
+
| API | Use when | Important behavior and result |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| `Morphology.from_swc(path, options=..., mode=..., return_report=False)` | The reconstruction is an SWC point/parent table. | It maps SWC structure identifiers to typed branches, validates the tree, and returns a `Morphology`; with `return_report=True`, it returns `(morphology, SwcReport)`. |
|
|
12
|
+
| `Morphology.from_asc(path, return_report=False)` | The reconstruction is Neurolucida ASC and its spines, markers, contours, or metadata matter. | It parses the richer nested format and returns a `Morphology`; with `return_report=True`, it also returns an `AscReport`. |
|
|
13
|
+
| `NeuroMlReader().read(path)` | The source is NeuroML2. | It imports segment groups and cable geometry into a `Morphology`; it does not import channel or network definitions. |
|
|
14
|
+
| `Morphology.from_neuromorpho(neuron_id, ...)` | A known NeuroMorpho.Org integer neuron id should be downloaded, cached, parsed, and returned directly. | It reuses the cached download on later calls and returns a `Morphology`. |
|
|
15
|
+
| `braincell.io.load_neuromorpho(neuron_id, return_report=False)` | The NeuroMorpho path also needs the IO-module surface or a validation report. | It returns the same morphology path; with `return_report=True`, it returns `(morphology, report)`. |
|
|
16
|
+
| `braincell.io.load_morpho(path)` | A processed BrainCell morphology checkpoint should be reused without parsing the original reconstruction. | It returns the stored `Morphology` or raises a checkpoint compatibility error. |
|
|
17
|
+
|
|
18
|
+
Use SWC for the portable default interchange path. Use ASC when its additional annotations must survive import. Use a BrainCell checkpoint only after the source reconstruction has already been parsed, reviewed, and optionally edited.
|
|
19
|
+
|
|
20
|
+
## Load and validate SWC
|
|
21
|
+
|
|
22
|
+
An SWC load is a parse-and-validation operation; request the report whenever malformed geometry, automatic repairs, or structure-type mapping could change the model.
|
|
23
|
+
|
|
24
|
+
| API | Description |
|
|
25
|
+
|---|---|
|
|
26
|
+
| `SwcReadOptions(standardize_safe_fixes=..., unknown_type_as_custom=..., require_root_type_soma=...)` | Use to choose whether safe corrections are applied, unknown structure ids become `CustomBranch`, and the root must be a soma. |
|
|
27
|
+
| `Morphology.from_swc(path, options=options, return_report=True)` | Use for the normal load-and-review path; it returns the morphology and the findings or fixes in `SwcReport`. |
|
|
28
|
+
| `SwcReader().check(path)` | Use when the file must be validated without constructing a `Morphology`; it returns the report. |
|
|
29
|
+
| `SwcReader().read(path)` | Use only when the lower-level reader is needed directly; it parses and returns the morphology. |
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import braincell
|
|
33
|
+
from braincell.filter import branch_in
|
|
34
|
+
from braincell.io import SwcReadOptions
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
options = SwcReadOptions(
|
|
38
|
+
standardize_safe_fixes=True,
|
|
39
|
+
unknown_type_as_custom=True,
|
|
40
|
+
require_root_type_soma=True,
|
|
41
|
+
)
|
|
42
|
+
morphology, report = braincell.Morphology.from_swc(
|
|
43
|
+
"neuron.swc",
|
|
44
|
+
options=options,
|
|
45
|
+
return_report=True,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
print(report)
|
|
49
|
+
print(morphology.topo())
|
|
50
|
+
|
|
51
|
+
soma = morphology.select(branch_in("type", "soma"))
|
|
52
|
+
assert soma.intervals, "validated morphology has no branch typed as soma"
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
SWC ids `1`, `2`, `3`, and `4` map to soma, axon, basal dendrite, and apical dendrite. Other ids become custom branches only when the chosen reader policy permits it. Inspect the report before accepting disconnected points, a missing soma, unknown types, or applied repairs.
|
|
56
|
+
|
|
57
|
+
## Handle format-specific behavior
|
|
58
|
+
|
|
59
|
+
### Neurolucida ASC
|
|
60
|
+
|
|
61
|
+
Use the high-level constructor unless lower-level ASC models are required.
|
|
62
|
+
|
|
63
|
+
| API | Description |
|
|
64
|
+
|---|---|
|
|
65
|
+
| `Morphology.from_asc(path, return_report=True)` | Load typed branches and return the morphology with an `AscReport` containing issues and metadata. |
|
|
66
|
+
| `AscReader().read(path)` | Parse ASC through the lower-level reader and return the morphology. |
|
|
67
|
+
| `AscSpineRecord` / `AscMetadata` | Inspect spine annotations or file-level metadata when those ASC-only details affect preprocessing. |
|
|
68
|
+
|
|
69
|
+
Do not assume a successful ASC parse makes coincident points, radii, branch labels, or imported annotations appropriate for the intended simulation.
|
|
70
|
+
|
|
71
|
+
### NeuroML2
|
|
72
|
+
|
|
73
|
+
Use `NeuroMlReader` only for morphology import:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from braincell.io import NeuroMlReader
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
morphology = NeuroMlReader().read("neuron.cell.nml")
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Declare ions, channels, clamps, probes, and network structure separately. BrainCell does not import those NeuroML2 definitions automatically.
|
|
83
|
+
|
|
84
|
+
### NeuroMorpho.Org
|
|
85
|
+
|
|
86
|
+
Install the IO extra before using the repository client: `python -m pip install -U "braincell[io]"`.
|
|
87
|
+
|
|
88
|
+
| API | Description |
|
|
89
|
+
|---|---|
|
|
90
|
+
| `Morphology.from_neuromorpho(neuron_id, ...)` | Use the shortest path for a known integer neuron id; it downloads once, caches, parses, and returns the morphology. |
|
|
91
|
+
| `NeuroMorphoClient().iter_search(**criteria)` | Search by criteria such as species, brain region, or cell type; it yields typed neuron records. |
|
|
92
|
+
| `NeuroMorphoClient().get_neuron(neuron_id)` | Retrieve the detailed record for one integer neuron id. |
|
|
93
|
+
| `NeuroMorphoClient().download(neuron_id)` | Download a reconstruction and return its download record. |
|
|
94
|
+
| `braincell.io.fetch_neuromorpho(neuron_id, mode="standard")` | Fetch raw files without parsing; use `"standard"`, `"original"`, or `"both"` and receive a `NeuroMorphoDownloadRecord`. |
|
|
95
|
+
| `cache_dir=...` | Use on repository entry points when the default user cache must be replaced with an explicit location. |
|
|
96
|
+
|
|
97
|
+
Use the integer id from the neuron's NeuroMorpho.Org URL, not its display name. Treat cached content as an IO optimization, not as validation evidence.
|
|
98
|
+
|
|
99
|
+
## Save and reload processed geometry
|
|
100
|
+
|
|
101
|
+
Checkpoint a cleaned or edited BrainCell object when the exact processed geometry must be reused.
|
|
102
|
+
|
|
103
|
+
| API | Description |
|
|
104
|
+
|---|---|
|
|
105
|
+
| `braincell.io.save_morpho(morphology, path)` | Serialize a morphology and return the written `Path`. |
|
|
106
|
+
| `braincell.io.load_morpho(path)` | Reload a morphology checkpoint without reparsing its original source. |
|
|
107
|
+
| `braincell.io.save_branch(branch, path)` | Serialize one branch. |
|
|
108
|
+
| `braincell.io.load_branch(path)` | Reload one branch checkpoint. |
|
|
109
|
+
| `CheckpointError` | Catch when a checkpoint cannot be read. |
|
|
110
|
+
| `CheckpointVersionError` | Catch when the file format version is unsupported by the installed BrainCell. |
|
|
111
|
+
|
|
112
|
+
Do not use a checkpoint to hide the original validation record. Preserve the source identifier, reader options, report, BrainCell version, and any edits with the model provenance.
|
|
113
|
+
|
|
114
|
+
## Verify the loaded morphology
|
|
115
|
+
|
|
116
|
+
A successful load proves that the reader returned a `Morphology` under the chosen validation policy; it does not prove that the reconstruction is biologically or numerically suitable.
|
|
117
|
+
|
|
118
|
+
1. Read the validation report and distinguish detected issues from applied fixes.
|
|
119
|
+
2. Inspect `morphology.topo()` for the intended root, connectivity, names, and parent-child structure.
|
|
120
|
+
3. Select soma, axon, and dendrite types with `morphology.select(...)`; confirm that later region expressions will reach the intended branches.
|
|
121
|
+
4. Inspect coordinates, lengths, and radii for zero-length, disconnected, or implausible geometry.
|
|
122
|
+
5. Visualize the branch tree before choosing a CV policy or attaching mechanisms.
|
|
123
|
+
6. Record the reader options, input identity, and checkpoint version with the simulation.
|
|
124
|
+
|
|
125
|
+
Open `references/multicompartment/topology-building-and-visualization.md` when branch, CV, or runtime placement must be inspected visually. Return to `references/multicompartment/multicompartment-cell-workflow.md` after validation to choose a CV policy and declare the cell.
|
|
126
|
+
|
|
127
|
+
## Sources
|
|
128
|
+
|
|
129
|
+
- [IO overview](https://brainx.chaobrain.com/braincell/file_formats/overview.html)
|
|
130
|
+
- [SWC](https://brainx.chaobrain.com/braincell/file_formats/swc.html)
|
|
131
|
+
- [Neurolucida ASC](https://brainx.chaobrain.com/braincell/file_formats/asc.html)
|
|
132
|
+
- [NeuroML2](https://brainx.chaobrain.com/braincell/file_formats/neuroml2.html)
|
|
133
|
+
- [NeuroMorpho.Org](https://brainx.chaobrain.com/braincell/file_formats/neuromorpho.html)
|
|
134
|
+
- [Checkpointing](https://brainx.chaobrain.com/braincell/file_formats/checkpointing.html)
|
|
135
|
+
- [Morphology](https://brainx.chaobrain.com/braincell/concepts/morphology.html)
|