fscpy 2.0.0__tar.gz → 2.0.1__tar.gz
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.
- {fscpy-2.0.0 → fscpy-2.0.1}/PKG-INFO +5 -5
- {fscpy-2.0.0 → fscpy-2.0.1}/README.md +4 -4
- {fscpy-2.0.0 → fscpy-2.0.1}/pyproject.toml +1 -1
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/parcellation/connectivity.py +1 -1
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/solver.py +11 -7
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fscpy.egg-info/PKG-INFO +5 -5
- {fscpy-2.0.0 → fscpy-2.0.1}/tests/test_parcellation.py +53 -51
- {fscpy-2.0.0 → fscpy-2.0.1}/tests/test_voxel.py +103 -93
- {fscpy-2.0.0 → fscpy-2.0.1}/LICENSE +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/setup.cfg +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/__init__.py +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/parcellation/__init__.py +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/parcellation/streamlines.py +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/utils.py +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/voxel/__init__.py +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/voxel/fod.py +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/voxel/roi.py +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/voxel/support.py +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fsc/weighted_graph.py +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fscpy.egg-info/SOURCES.txt +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fscpy.egg-info/dependency_links.txt +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fscpy.egg-info/requires.txt +0 -0
- {fscpy-2.0.0 → fscpy-2.0.1}/src/fscpy.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fscpy
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.1
|
|
4
4
|
Summary: Function-structure coupling on parcellation and FOD-derived voxel graphs
|
|
5
5
|
Author: Viljami Sairanen
|
|
6
6
|
License: GNU GENERAL PUBLIC LICENSE
|
|
@@ -699,8 +699,8 @@ Dynamic: license-file
|
|
|
699
699
|
|
|
700
700
|
The Python package is imported as `fsc` and provides two workflows:
|
|
701
701
|
|
|
702
|
-
- **Parcellation FSC:** a structural connectivity (SC) matrix and a functional connectivity (FC) matrix produce a symmetric matrix of structural-edge support. Relevant publication for this version is the Network Neuroscience paper 10.1162/NETN.a.589.
|
|
703
|
-
- **Voxelwise FSC:** a spherical-harmonic fibre orientation distribution (FOD) defines a weighted white-matter voxel graph. One supernode per functional parcel connects to neighbouring white-matter voxels through FOD-supported interfaces. The output is a voxelwise map of white-matter edge support. This version is explained in the Aperture Neuro paper 10.52294/001c.172225.
|
|
702
|
+
- **Parcellation FSC:** a structural connectivity (SC) matrix and a functional connectivity (FC) matrix produce a symmetric matrix of structural-edge support. Relevant publication for this version is the Network Neuroscience paper [doi:10.1162/NETN.a.589](https://doi.org/10.1162/NETN.a.589).
|
|
703
|
+
- **Voxelwise FSC:** a spherical-harmonic fibre orientation distribution (FOD) defines a weighted white-matter voxel graph. One supernode per functional parcel connects to neighbouring white-matter voxels through FOD-supported interfaces. The output is a voxelwise map of white-matter edge support. This version is explained in the Aperture Neuro paper [doi:10.52294/001c.172225](https://doi.org/10.52294/001c.172225).
|
|
704
704
|
|
|
705
705
|
These workflows use different structural graphs, so their numerical outputs are **not interchangeable**.
|
|
706
706
|
|
|
@@ -785,11 +785,11 @@ nib.save(nib.Nifti1Image(support, fod_image.affine), 'fsc_support.nii.gz')
|
|
|
785
785
|
|
|
786
786
|
`labels_in_fod_space()` uses nearest-neighbour resampling and assumes the images are **already spatially registered**. It does not register images or reorient SH coefficients. By default, the FOD coefficient convention is DIPY's `tournier07` basis with `legacy=True`; the supplied image must use the matching convention. An optional `wm_mask` can further restrict graph voxels; the FOD-norm threshold is still applied. ROI voxels must have positive labels, lie outside the selected WM voxel graph, and have FOD-supported WM contacts.
|
|
787
787
|
|
|
788
|
-
The voxel workflow applies each finite,
|
|
788
|
+
The voxel workflow applies each finite, positive FC pair **independently in the mathematical model**. For efficiency, the implementation solves reusable grounded-Laplacian basis fields relative to one ROI per connected component, then reconstructs pair solutions. It accumulates **absolute** structural currents. Voxel support sums currents on **WM–WM edges only**, excluding ROI-supernode interface edges. Pairs without a structural path contribute zero. Specify `pairs=[(0, 1)]` to select one pair by zero-based FC matrix indices.
|
|
789
789
|
|
|
790
790
|
## Numerical interpretation
|
|
791
791
|
|
|
792
|
-
In both workflows,
|
|
792
|
+
In both workflows, positive FC magnitudes define imposed potential differences. SC values and FOD interface weights act as conductances, **not lengths or resistances**. FSC reports how the weighted structural graph supports the imposed correlations; it is not a direct measurement of axonal traffic or a uniquely determined anatomical tract.
|
|
793
793
|
|
|
794
794
|
The low-level `fsc.solve_fsc()` interface solves **simultaneous** functional constraints through modified nodal analysis and returns an `FSCResult` (potentials, currents and residual diagnostics). This is distinct from `solve_from_parcellations()` and `solve_fsc_voxel()`, which evaluate FC pairs separately and sum their absolute structural-edge currents.
|
|
795
795
|
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
The Python package is imported as `fsc` and provides two workflows:
|
|
6
6
|
|
|
7
|
-
- **Parcellation FSC:** a structural connectivity (SC) matrix and a functional connectivity (FC) matrix produce a symmetric matrix of structural-edge support. Relevant publication for this version is the Network Neuroscience paper 10.1162/NETN.a.589.
|
|
8
|
-
- **Voxelwise FSC:** a spherical-harmonic fibre orientation distribution (FOD) defines a weighted white-matter voxel graph. One supernode per functional parcel connects to neighbouring white-matter voxels through FOD-supported interfaces. The output is a voxelwise map of white-matter edge support. This version is explained in the Aperture Neuro paper 10.52294/001c.172225.
|
|
7
|
+
- **Parcellation FSC:** a structural connectivity (SC) matrix and a functional connectivity (FC) matrix produce a symmetric matrix of structural-edge support. Relevant publication for this version is the Network Neuroscience paper [doi:10.1162/NETN.a.589](https://doi.org/10.1162/NETN.a.589).
|
|
8
|
+
- **Voxelwise FSC:** a spherical-harmonic fibre orientation distribution (FOD) defines a weighted white-matter voxel graph. One supernode per functional parcel connects to neighbouring white-matter voxels through FOD-supported interfaces. The output is a voxelwise map of white-matter edge support. This version is explained in the Aperture Neuro paper [doi:10.52294/001c.172225](https://doi.org/10.52294/001c.172225).
|
|
9
9
|
|
|
10
10
|
These workflows use different structural graphs, so their numerical outputs are **not interchangeable**.
|
|
11
11
|
|
|
@@ -90,11 +90,11 @@ nib.save(nib.Nifti1Image(support, fod_image.affine), 'fsc_support.nii.gz')
|
|
|
90
90
|
|
|
91
91
|
`labels_in_fod_space()` uses nearest-neighbour resampling and assumes the images are **already spatially registered**. It does not register images or reorient SH coefficients. By default, the FOD coefficient convention is DIPY's `tournier07` basis with `legacy=True`; the supplied image must use the matching convention. An optional `wm_mask` can further restrict graph voxels; the FOD-norm threshold is still applied. ROI voxels must have positive labels, lie outside the selected WM voxel graph, and have FOD-supported WM contacts.
|
|
92
92
|
|
|
93
|
-
The voxel workflow applies each finite,
|
|
93
|
+
The voxel workflow applies each finite, positive FC pair **independently in the mathematical model**. For efficiency, the implementation solves reusable grounded-Laplacian basis fields relative to one ROI per connected component, then reconstructs pair solutions. It accumulates **absolute** structural currents. Voxel support sums currents on **WM–WM edges only**, excluding ROI-supernode interface edges. Pairs without a structural path contribute zero. Specify `pairs=[(0, 1)]` to select one pair by zero-based FC matrix indices.
|
|
94
94
|
|
|
95
95
|
## Numerical interpretation
|
|
96
96
|
|
|
97
|
-
In both workflows,
|
|
97
|
+
In both workflows, positive FC magnitudes define imposed potential differences. SC values and FOD interface weights act as conductances, **not lengths or resistances**. FSC reports how the weighted structural graph supports the imposed correlations; it is not a direct measurement of axonal traffic or a uniquely determined anatomical tract.
|
|
98
98
|
|
|
99
99
|
The low-level `fsc.solve_fsc()` interface solves **simultaneous** functional constraints through modified nodal analysis and returns an `FSCResult` (potentials, currents and residual diagnostics). This is distinct from `solve_from_parcellations()` and `solve_fsc_voxel()`, which evaluate FC pairs separately and sum their absolute structural-edge currents.
|
|
100
100
|
|
|
@@ -60,7 +60,7 @@ def solve_from_parcellations(sc, fc):
|
|
|
60
60
|
potentials[mask] = solve(rhs[mask])
|
|
61
61
|
|
|
62
62
|
resistance = potentials[source] - potentials[target]
|
|
63
|
-
potentials *=
|
|
63
|
+
potentials *= value / resistance
|
|
64
64
|
|
|
65
65
|
currents = graph.conductance * (
|
|
66
66
|
potentials[graph.edge_i] - potentials[graph.edge_j]
|
|
@@ -55,14 +55,14 @@ class FSCResult:
|
|
|
55
55
|
|
|
56
56
|
|
|
57
57
|
def fc_pairs(fc):
|
|
58
|
-
"""Get finite,
|
|
58
|
+
"""Get finite, positive FC pairs from the upper triangle of a square matrix."""
|
|
59
59
|
fc = np.asarray(fc, dtype=float)
|
|
60
60
|
if fc.ndim != 2 or fc.shape[0] != fc.shape[1]:
|
|
61
61
|
raise ValueError("FC must be square.")
|
|
62
62
|
if not np.allclose(fc, fc.T, equal_nan=True):
|
|
63
63
|
raise ValueError("FC must be symmetric.")
|
|
64
64
|
|
|
65
|
-
selected = np.triu(np.isfinite(fc) & (fc
|
|
65
|
+
selected = np.triu(np.isfinite(fc) & (fc > 0), k=1)
|
|
66
66
|
pairs = np.column_stack(np.nonzero(selected))
|
|
67
67
|
values = fc[pairs[:, 0], pairs[:, 1]]
|
|
68
68
|
return pairs, values
|
|
@@ -78,6 +78,10 @@ def functional_connectivity_from_fc_pairs(pairs, values, n_nodes):
|
|
|
78
78
|
if np.any(pairs < 0) or np.any(pairs >= n_nodes):
|
|
79
79
|
raise ValueError("FC pair index outside graph.")
|
|
80
80
|
|
|
81
|
+
selected = np.isfinite(values) & (values > 0)
|
|
82
|
+
pairs = pairs[selected]
|
|
83
|
+
values = values[selected]
|
|
84
|
+
|
|
81
85
|
n_pairs = len(pairs)
|
|
82
86
|
|
|
83
87
|
rows = np.empty(2 * n_pairs, dtype=int)
|
|
@@ -174,7 +178,7 @@ def _solve_mna(structural_graph, functional_connectivity, prepared, *,
|
|
|
174
178
|
ground_node=0, solver="auto", rtol=1e-8, maxiter=None):
|
|
175
179
|
"""Solve FSC using modified nodal analysis.
|
|
176
180
|
|
|
177
|
-
Solve [L B; B.T 0] [potential; source_current] = [0;
|
|
181
|
+
Solve [L B; B.T 0] [potential; source_current] = [0; s_FC].
|
|
178
182
|
``functional_connectivity`` contains both B and its target values.
|
|
179
183
|
|
|
180
184
|
``solver='auto'`` uses sparse direct solving for small systems and
|
|
@@ -183,7 +187,7 @@ def _solve_mna(structural_graph, functional_connectivity, prepared, *,
|
|
|
183
187
|
"""
|
|
184
188
|
n_nodes = structural_graph.n_nodes
|
|
185
189
|
incidence = functional_connectivity.incidence.tocsr()
|
|
186
|
-
constraints = np.
|
|
190
|
+
constraints = np.asarray(functional_connectivity.constraints, dtype=float).reshape(-1)
|
|
187
191
|
n_constraints = len(constraints)
|
|
188
192
|
|
|
189
193
|
if incidence.shape != (n_nodes, n_constraints):
|
|
@@ -313,7 +317,7 @@ def solve_fsc_voxel(structural_graph, parcellation, fc, pairs=None, *,
|
|
|
313
317
|
# Ignore zero, nonfinite and self-connections.
|
|
314
318
|
valid = (
|
|
315
319
|
np.isfinite(values)
|
|
316
|
-
& (values
|
|
320
|
+
& (values > 0)
|
|
317
321
|
& (pairs[:, 0] != pairs[:, 1])
|
|
318
322
|
)
|
|
319
323
|
pairs = pairs[valid]
|
|
@@ -412,7 +416,7 @@ def solve_fsc_voxel(structural_graph, parcellation, fc, pairs=None, *,
|
|
|
412
416
|
f"Invalid effective resistance for ROI pair {pair}."
|
|
413
417
|
)
|
|
414
418
|
|
|
415
|
-
current =
|
|
419
|
+
current = value / resistance
|
|
416
420
|
potentials *= current
|
|
417
421
|
|
|
418
422
|
# Check current conservation at every graph node.
|
|
@@ -420,7 +424,7 @@ def solve_fsc_voxel(structural_graph, parcellation, fc, pairs=None, *,
|
|
|
420
424
|
balance[source] -= current
|
|
421
425
|
balance[target] += current
|
|
422
426
|
|
|
423
|
-
relative_residual = np.linalg.norm(balance) /
|
|
427
|
+
relative_residual = np.linalg.norm(balance) / value
|
|
424
428
|
|
|
425
429
|
if (
|
|
426
430
|
not np.isfinite(relative_residual)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fscpy
|
|
3
|
-
Version: 2.0.
|
|
3
|
+
Version: 2.0.1
|
|
4
4
|
Summary: Function-structure coupling on parcellation and FOD-derived voxel graphs
|
|
5
5
|
Author: Viljami Sairanen
|
|
6
6
|
License: GNU GENERAL PUBLIC LICENSE
|
|
@@ -699,8 +699,8 @@ Dynamic: license-file
|
|
|
699
699
|
|
|
700
700
|
The Python package is imported as `fsc` and provides two workflows:
|
|
701
701
|
|
|
702
|
-
- **Parcellation FSC:** a structural connectivity (SC) matrix and a functional connectivity (FC) matrix produce a symmetric matrix of structural-edge support. Relevant publication for this version is the Network Neuroscience paper 10.1162/NETN.a.589.
|
|
703
|
-
- **Voxelwise FSC:** a spherical-harmonic fibre orientation distribution (FOD) defines a weighted white-matter voxel graph. One supernode per functional parcel connects to neighbouring white-matter voxels through FOD-supported interfaces. The output is a voxelwise map of white-matter edge support. This version is explained in the Aperture Neuro paper 10.52294/001c.172225.
|
|
702
|
+
- **Parcellation FSC:** a structural connectivity (SC) matrix and a functional connectivity (FC) matrix produce a symmetric matrix of structural-edge support. Relevant publication for this version is the Network Neuroscience paper [doi:10.1162/NETN.a.589](https://doi.org/10.1162/NETN.a.589).
|
|
703
|
+
- **Voxelwise FSC:** a spherical-harmonic fibre orientation distribution (FOD) defines a weighted white-matter voxel graph. One supernode per functional parcel connects to neighbouring white-matter voxels through FOD-supported interfaces. The output is a voxelwise map of white-matter edge support. This version is explained in the Aperture Neuro paper [doi:10.52294/001c.172225](https://doi.org/10.52294/001c.172225).
|
|
704
704
|
|
|
705
705
|
These workflows use different structural graphs, so their numerical outputs are **not interchangeable**.
|
|
706
706
|
|
|
@@ -785,11 +785,11 @@ nib.save(nib.Nifti1Image(support, fod_image.affine), 'fsc_support.nii.gz')
|
|
|
785
785
|
|
|
786
786
|
`labels_in_fod_space()` uses nearest-neighbour resampling and assumes the images are **already spatially registered**. It does not register images or reorient SH coefficients. By default, the FOD coefficient convention is DIPY's `tournier07` basis with `legacy=True`; the supplied image must use the matching convention. An optional `wm_mask` can further restrict graph voxels; the FOD-norm threshold is still applied. ROI voxels must have positive labels, lie outside the selected WM voxel graph, and have FOD-supported WM contacts.
|
|
787
787
|
|
|
788
|
-
The voxel workflow applies each finite,
|
|
788
|
+
The voxel workflow applies each finite, positive FC pair **independently in the mathematical model**. For efficiency, the implementation solves reusable grounded-Laplacian basis fields relative to one ROI per connected component, then reconstructs pair solutions. It accumulates **absolute** structural currents. Voxel support sums currents on **WM–WM edges only**, excluding ROI-supernode interface edges. Pairs without a structural path contribute zero. Specify `pairs=[(0, 1)]` to select one pair by zero-based FC matrix indices.
|
|
789
789
|
|
|
790
790
|
## Numerical interpretation
|
|
791
791
|
|
|
792
|
-
In both workflows,
|
|
792
|
+
In both workflows, positive FC magnitudes define imposed potential differences. SC values and FOD interface weights act as conductances, **not lengths or resistances**. FSC reports how the weighted structural graph supports the imposed correlations; it is not a direct measurement of axonal traffic or a uniquely determined anatomical tract.
|
|
793
793
|
|
|
794
794
|
The low-level `fsc.solve_fsc()` interface solves **simultaneous** functional constraints through modified nodal analysis and returns an `FSCResult` (potentials, currents and residual diagnostics). This is distinct from `solve_from_parcellations()` and `solve_fsc_voxel()`, which evaluate FC pairs separately and sum their absolute structural-edge currents.
|
|
795
795
|
|
|
@@ -1,51 +1,53 @@
|
|
|
1
|
-
"""Small deterministic tests for parcellation FSC."""
|
|
2
|
-
|
|
3
|
-
import numpy as np
|
|
4
|
-
|
|
5
|
-
import fsc
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def five_node_matrices():
|
|
9
|
-
sc = np.array([
|
|
10
|
-
[0, 1, 1, 0, 0],
|
|
11
|
-
[1, 0, 0, 0, 0],
|
|
12
|
-
[1, 0, 0, 1, 1],
|
|
13
|
-
[0, 0, 1, 0, 0],
|
|
14
|
-
[0, 0, 1, 0, 0],
|
|
15
|
-
], dtype=float)
|
|
16
|
-
fc = np.zeros((5, 5))
|
|
17
|
-
fc[1, 4] = fc[4, 1] = 1.0
|
|
18
|
-
return sc, fc
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def test_five_node_path():
|
|
22
|
-
sc, fc = five_node_matrices()
|
|
23
|
-
support = fsc.solve_from_parcellations(sc, fc)
|
|
24
|
-
expected = np.zeros((5, 5))
|
|
25
|
-
for i, j in [(1, 0), (0, 2), (2, 4)]:
|
|
26
|
-
expected[i, j] = expected[j, i] = 1 / 3
|
|
27
|
-
np.testing.assert_allclose(support, expected, rtol=1e-12, atol=1e-12)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def
|
|
31
|
-
sc, fc = five_node_matrices()
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
1
|
+
"""Small deterministic tests for parcellation FSC."""
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
import fsc
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def five_node_matrices():
|
|
9
|
+
sc = np.array([
|
|
10
|
+
[0, 1, 1, 0, 0],
|
|
11
|
+
[1, 0, 0, 0, 0],
|
|
12
|
+
[1, 0, 0, 1, 1],
|
|
13
|
+
[0, 0, 1, 0, 0],
|
|
14
|
+
[0, 0, 1, 0, 0],
|
|
15
|
+
], dtype=float)
|
|
16
|
+
fc = np.zeros((5, 5))
|
|
17
|
+
fc[1, 4] = fc[4, 1] = 1.0
|
|
18
|
+
return sc, fc
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_five_node_path():
|
|
22
|
+
sc, fc = five_node_matrices()
|
|
23
|
+
support = fsc.solve_from_parcellations(sc, fc)
|
|
24
|
+
expected = np.zeros((5, 5))
|
|
25
|
+
for i, j in [(1, 0), (0, 2), (2, 4)]:
|
|
26
|
+
expected[i, j] = expected[j, i] = 1 / 3
|
|
27
|
+
np.testing.assert_allclose(support, expected, rtol=1e-12, atol=1e-12)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_negative_and_empty_fc():
|
|
31
|
+
sc, fc = five_node_matrices()
|
|
32
|
+
np.testing.assert_array_equal(
|
|
33
|
+
fsc.solve_from_parcellations(sc, -fc),
|
|
34
|
+
np.zeros_like(sc),
|
|
35
|
+
)
|
|
36
|
+
np.testing.assert_array_equal(
|
|
37
|
+
fsc.solve_from_parcellations(sc, np.zeros_like(fc)),
|
|
38
|
+
np.zeros_like(sc),
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_general_mna_single_constraint():
|
|
43
|
+
sc, fc = five_node_matrices()
|
|
44
|
+
graph = fsc.graph_from_sc(sc)
|
|
45
|
+
constraints = fsc.functional_connectivity_from_fc_matrix(fc)
|
|
46
|
+
result = fsc.solve_fsc(graph, constraints, solver="direct")
|
|
47
|
+
|
|
48
|
+
assert result.relative_residual < 1e-10
|
|
49
|
+
np.testing.assert_allclose(
|
|
50
|
+
result.potentials[1] - result.potentials[4], 1.0, atol=1e-12
|
|
51
|
+
)
|
|
52
|
+
np.testing.assert_allclose(np.abs(result.edge_currents),
|
|
53
|
+
np.array([1 / 3, 1 / 3, 0, 1 / 3]), atol=1e-12)
|
|
@@ -1,93 +1,103 @@
|
|
|
1
|
-
"""Synthetic voxel graph tests; no MRI datasets or DIPY installation needed."""
|
|
2
|
-
|
|
3
|
-
import numpy as np
|
|
4
|
-
|
|
5
|
-
import fsc
|
|
6
|
-
from fsc.voxel import ParcellationMapping, VoxelMapping, voxel_support
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def synthetic_graph():
|
|
10
|
-
# WM graph: 0 -- 1 -- 2; ROI nodes 3, 4, 5 attach to WM 0, 2, 1.
|
|
11
|
-
graph = fsc.WeightedGraph(
|
|
12
|
-
n_nodes=6,
|
|
13
|
-
edge_i=np.array([0, 1, 3, 4, 5]),
|
|
14
|
-
edge_j=np.array([1, 2, 0, 2, 1]),
|
|
15
|
-
conductance=np.ones(5),
|
|
16
|
-
)
|
|
17
|
-
node_id = np.full((5, 3, 3), -1)
|
|
18
|
-
node_id[1:4, 1, 1] = np.arange(3)
|
|
19
|
-
voxel = VoxelMapping(node_id, np.eye(4), np.array([0, 1]))
|
|
20
|
-
parcels = ParcellationMapping(
|
|
21
|
-
roi_labels=np.array([10, 20, 30]),
|
|
22
|
-
roi_nodes=np.array([3, 4, 5]),
|
|
23
|
-
interface_edge_indices=np.array([2, 3, 4]),
|
|
24
|
-
contact_voxel_indices=np.array([0, 1, 2]),
|
|
25
|
-
)
|
|
26
|
-
return graph, voxel, parcels
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def test_single_pair_and_voxel_support():
|
|
30
|
-
graph, voxel, parcels = synthetic_graph()
|
|
31
|
-
fc = np.zeros((3, 3))
|
|
32
|
-
fc[0, 1] = fc[1, 0] = 1.0
|
|
33
|
-
currents = fsc.solve_fsc_voxel(graph, parcels, fc, solver="cg", rtol=1e-12)
|
|
34
|
-
np.testing.assert_allclose(currents, [0.25, 0.25, 0.25, 0.25, 0.0], atol=1e-12)
|
|
35
|
-
support = voxel_support(graph, voxel, currents)
|
|
36
|
-
np.testing.assert_allclose(support[1:4, 1, 1], [0.25, 0.5, 0.25], atol=1e-12)
|
|
37
|
-
assert np.count_nonzero(support) == 3
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def test_roi_basis_matches_individual_pairs():
|
|
41
|
-
graph, _, parcels = synthetic_graph()
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
[0.
|
|
45
|
-
[
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
np.testing.
|
|
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
|
-
|
|
1
|
+
"""Synthetic voxel graph tests; no MRI datasets or DIPY installation needed."""
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
import fsc
|
|
6
|
+
from fsc.voxel import ParcellationMapping, VoxelMapping, voxel_support
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def synthetic_graph():
|
|
10
|
+
# WM graph: 0 -- 1 -- 2; ROI nodes 3, 4, 5 attach to WM 0, 2, 1.
|
|
11
|
+
graph = fsc.WeightedGraph(
|
|
12
|
+
n_nodes=6,
|
|
13
|
+
edge_i=np.array([0, 1, 3, 4, 5]),
|
|
14
|
+
edge_j=np.array([1, 2, 0, 2, 1]),
|
|
15
|
+
conductance=np.ones(5),
|
|
16
|
+
)
|
|
17
|
+
node_id = np.full((5, 3, 3), -1)
|
|
18
|
+
node_id[1:4, 1, 1] = np.arange(3)
|
|
19
|
+
voxel = VoxelMapping(node_id, np.eye(4), np.array([0, 1]))
|
|
20
|
+
parcels = ParcellationMapping(
|
|
21
|
+
roi_labels=np.array([10, 20, 30]),
|
|
22
|
+
roi_nodes=np.array([3, 4, 5]),
|
|
23
|
+
interface_edge_indices=np.array([2, 3, 4]),
|
|
24
|
+
contact_voxel_indices=np.array([0, 1, 2]),
|
|
25
|
+
)
|
|
26
|
+
return graph, voxel, parcels
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_single_pair_and_voxel_support():
|
|
30
|
+
graph, voxel, parcels = synthetic_graph()
|
|
31
|
+
fc = np.zeros((3, 3))
|
|
32
|
+
fc[0, 1] = fc[1, 0] = 1.0
|
|
33
|
+
currents = fsc.solve_fsc_voxel(graph, parcels, fc, solver="cg", rtol=1e-12)
|
|
34
|
+
np.testing.assert_allclose(currents, [0.25, 0.25, 0.25, 0.25, 0.0], atol=1e-12)
|
|
35
|
+
support = voxel_support(graph, voxel, currents)
|
|
36
|
+
np.testing.assert_allclose(support[1:4, 1, 1], [0.25, 0.5, 0.25], atol=1e-12)
|
|
37
|
+
assert np.count_nonzero(support) == 3
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def test_roi_basis_matches_individual_pairs():
|
|
41
|
+
graph, _, parcels = synthetic_graph()
|
|
42
|
+
|
|
43
|
+
fc = np.array([
|
|
44
|
+
[0.0, 0.8, -0.4],
|
|
45
|
+
[0.8, 0.0, 0.6],
|
|
46
|
+
[-0.4, 0.6, 0.0],
|
|
47
|
+
])
|
|
48
|
+
|
|
49
|
+
combined = fsc.solve_fsc_voxel(graph, parcels, fc, rtol=1e-12)
|
|
50
|
+
|
|
51
|
+
summed = np.zeros(len(graph.edge_i))
|
|
52
|
+
for pair in [(0, 1), (0, 2), (1, 2)]:
|
|
53
|
+
summed += fsc.solve_fsc_voxel(
|
|
54
|
+
graph, parcels, fc, pairs=[pair], rtol=1e-12
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
np.testing.assert_allclose(combined, summed, atol=1e-12)
|
|
58
|
+
|
|
59
|
+
# Negative correlations must not contribute.
|
|
60
|
+
positive_fc = np.maximum(fc, 0)
|
|
61
|
+
np.testing.assert_allclose(
|
|
62
|
+
combined,
|
|
63
|
+
fsc.solve_fsc_voxel(graph, parcels, positive_fc, rtol=1e-12),
|
|
64
|
+
atol=1e-12,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
np.testing.assert_array_equal(
|
|
68
|
+
fsc.solve_fsc_voxel(graph, parcels, np.zeros_like(fc)),
|
|
69
|
+
np.zeros(len(graph.edge_i)),
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_disconnected_rois_have_no_support():
|
|
74
|
+
graph = fsc.WeightedGraph(
|
|
75
|
+
n_nodes=4,
|
|
76
|
+
edge_i=np.array([2, 3]),
|
|
77
|
+
edge_j=np.array([0, 1]),
|
|
78
|
+
conductance=np.ones(2),
|
|
79
|
+
)
|
|
80
|
+
parcels = ParcellationMapping(
|
|
81
|
+
roi_labels=np.array([1, 2]),
|
|
82
|
+
roi_nodes=np.array([2, 3]),
|
|
83
|
+
interface_edge_indices=np.array([0, 1]),
|
|
84
|
+
contact_voxel_indices=np.array([0, 1]),
|
|
85
|
+
)
|
|
86
|
+
fc = np.array([[0.0, 1.0], [1.0, 0.0]])
|
|
87
|
+
np.testing.assert_array_equal(
|
|
88
|
+
fsc.solve_fsc_voxel(graph, parcels, fc), np.zeros(2)
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def test_synthetic_fod_example():
|
|
93
|
+
"""Run the complete image-to-support example when optional deps exist."""
|
|
94
|
+
import runpy
|
|
95
|
+
from pathlib import Path
|
|
96
|
+
import pytest
|
|
97
|
+
|
|
98
|
+
pytest.importorskip("nibabel")
|
|
99
|
+
pytest.importorskip("dipy")
|
|
100
|
+
script = Path(__file__).resolve().parents[1] / "examples" / "voxel.py"
|
|
101
|
+
output = runpy.run_path(str(script))
|
|
102
|
+
support = output["support"]
|
|
103
|
+
assert np.all(support[2:7, 2, 2] > 0)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|