pyLQT 1.0.dev7__tar.gz → 1.0.dev9__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.
- {pylqt-1.0.dev7/src/pyLQT.egg-info → pylqt-1.0.dev9}/PKG-INFO +8 -6
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/README.md +3 -3
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/pyproject.toml +5 -3
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/cli.py +4 -1
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/grey_matter/metrics.py +6 -9
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/network/metrics.py +3 -1
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/utils/configs.py +8 -10
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/utils/downloads.py +75 -133
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/utils/registration.py +104 -1
- pylqt-1.0.dev9/src/pyLQT/lesion_metrics/white_matter/metrics.py +114 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/white_matter/utils.py +17 -15
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/pipeline.py +48 -39
- pylqt-1.0.dev9/src/pyLQT/utils/__init__.py +0 -0
- pylqt-1.0.dev9/src/pyLQT/utils/logs.py +40 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9/src/pyLQT.egg-info}/PKG-INFO +8 -6
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT.egg-info/SOURCES.txt +3 -1
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT.egg-info/requires.txt +3 -1
- pylqt-1.0.dev7/src/pyLQT/lesion_metrics/white_matter/metrics.py +0 -160
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/LICENSE +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/setup.cfg +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/__init__.py +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/__init__.py +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/grey_matter/__init__.py +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/grey_matter/utils.py +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/network/__init__.py +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/network/utils.py +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/utils/__init__.py +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT/lesion_metrics/white_matter/__init__.py +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT.egg-info/dependency_links.txt +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT.egg-info/entry_points.txt +0 -0
- {pylqt-1.0.dev7 → pylqt-1.0.dev9}/src/pyLQT.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyLQT
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.dev9
|
|
4
4
|
Summary: Python version of LQT from Griffis et al. 2021 (https://www.sciencedirect.com/science/article/pii/S2213158221000838)
|
|
5
5
|
Author-email: Antonin HUDRY <hudryantoninm@gmail.com>
|
|
6
6
|
License-Expression: GPL-3.0-only
|
|
@@ -9,17 +9,19 @@ Classifier: Development Status :: 2 - Pre-Alpha
|
|
|
9
9
|
Classifier: Intended Audience :: Science/Research
|
|
10
10
|
Classifier: Intended Audience :: Healthcare Industry
|
|
11
11
|
Classifier: Programming Language :: Python
|
|
12
|
-
Requires-Python: >=3.
|
|
12
|
+
Requires-Python: >=3.11
|
|
13
13
|
Description-Content-Type: text/markdown
|
|
14
14
|
License-File: LICENSE
|
|
15
|
-
Requires-Dist: numpy
|
|
15
|
+
Requires-Dist: numpy==2.6.4
|
|
16
16
|
Requires-Dist: nibabel>5.0
|
|
17
17
|
Requires-Dist: pandas>3.0
|
|
18
|
+
Requires-Dist: napari[all]
|
|
18
19
|
Requires-Dist: scipy>1.15
|
|
19
20
|
Requires-Dist: requests>2.0
|
|
20
21
|
Requires-Dist: dipy>1.10
|
|
21
22
|
Requires-Dist: templateflow
|
|
22
23
|
Requires-Dist: networkx>=3.6.1
|
|
24
|
+
Requires-Dist: antspyx
|
|
23
25
|
Dynamic: license-file
|
|
24
26
|
|
|
25
27
|
# pyLQT
|
|
@@ -64,20 +66,19 @@ Specifies the atlas NIfTI file, its corresponding label CSV, and its reference s
|
|
|
64
66
|
{
|
|
65
67
|
"name": "glasser360",
|
|
66
68
|
"atlas": "glasser360.nii.gz",
|
|
67
|
-
"
|
|
69
|
+
"labels": "glasser360_labels.csv",
|
|
68
70
|
"space": "MNI152NLin2009cAsym"
|
|
69
71
|
}
|
|
70
72
|
```
|
|
71
73
|
|
|
72
74
|
##### Tractography configuration
|
|
73
75
|
|
|
74
|
-
Specifies the tractography dataset, the folder containing its `.trk`
|
|
76
|
+
Specifies the tractography dataset, the folder containing its `.trk` or `.trk.gz` files, and its reference space.
|
|
75
77
|
|
|
76
78
|
```json
|
|
77
79
|
{
|
|
78
80
|
"name": "HCP1065",
|
|
79
81
|
"tracts": "hcp1065_avg_tracts_trk",
|
|
80
|
-
"references": "hcp1065_avg_tracts",
|
|
81
82
|
"space": "MNI152NLin2009aAsym"
|
|
82
83
|
}
|
|
83
84
|
```
|
|
@@ -104,6 +105,7 @@ pyLQT --lesion_cfg <path/to/lesion_config> --out_folder <path/to/outputs>
|
|
|
104
105
|
| `--tracts_cfg` | `None` | Path to a `.json` file or `None` | Path to the tracts configuration file. |
|
|
105
106
|
| `--out_folder` | — | Path to a directory | Path to the output folder. |
|
|
106
107
|
| `--registration_type` | `syn` | `syn`, `affine` | Registration type used to transform the lesion and atlas from their respective spaces to tract space. |
|
|
108
|
+
|`--sc_distance_threshold` | `5.0` | Float above `0.0` | Threshold for distance filtering of tracts endpoints in structural connectivity computation. |
|
|
107
109
|
| `--spared_threshold` | `0.5` | Float between `0` and `1` | Threshold used to define spared parcel-to-parcel connectivity. |
|
|
108
110
|
| `--delete_tmp` | `True` | `True`, `False` | Whether to delete temporary files generated during processing. |
|
|
109
111
|
|
|
@@ -40,20 +40,19 @@ Specifies the atlas NIfTI file, its corresponding label CSV, and its reference s
|
|
|
40
40
|
{
|
|
41
41
|
"name": "glasser360",
|
|
42
42
|
"atlas": "glasser360.nii.gz",
|
|
43
|
-
"
|
|
43
|
+
"labels": "glasser360_labels.csv",
|
|
44
44
|
"space": "MNI152NLin2009cAsym"
|
|
45
45
|
}
|
|
46
46
|
```
|
|
47
47
|
|
|
48
48
|
##### Tractography configuration
|
|
49
49
|
|
|
50
|
-
Specifies the tractography dataset, the folder containing its `.trk`
|
|
50
|
+
Specifies the tractography dataset, the folder containing its `.trk` or `.trk.gz` files, and its reference space.
|
|
51
51
|
|
|
52
52
|
```json
|
|
53
53
|
{
|
|
54
54
|
"name": "HCP1065",
|
|
55
55
|
"tracts": "hcp1065_avg_tracts_trk",
|
|
56
|
-
"references": "hcp1065_avg_tracts",
|
|
57
56
|
"space": "MNI152NLin2009aAsym"
|
|
58
57
|
}
|
|
59
58
|
```
|
|
@@ -80,6 +79,7 @@ pyLQT --lesion_cfg <path/to/lesion_config> --out_folder <path/to/outputs>
|
|
|
80
79
|
| `--tracts_cfg` | `None` | Path to a `.json` file or `None` | Path to the tracts configuration file. |
|
|
81
80
|
| `--out_folder` | — | Path to a directory | Path to the output folder. |
|
|
82
81
|
| `--registration_type` | `syn` | `syn`, `affine` | Registration type used to transform the lesion and atlas from their respective spaces to tract space. |
|
|
82
|
+
|`--sc_distance_threshold` | `5.0` | Float above `0.0` | Threshold for distance filtering of tracts endpoints in structural connectivity computation. |
|
|
83
83
|
| `--spared_threshold` | `0.5` | Float between `0` and `1` | Threshold used to define spared parcel-to-parcel connectivity. |
|
|
84
84
|
| `--delete_tmp` | `True` | `True`, `False` | Whether to delete temporary files generated during processing. |
|
|
85
85
|
|
|
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pyLQT"
|
|
7
7
|
keywords = ["LQT", "lqt", "lesion", "brain", ""]
|
|
8
|
-
version = "1.0.
|
|
8
|
+
version = "1.0.dev9"
|
|
9
9
|
authors = [
|
|
10
10
|
{name="Antonin HUDRY", email="hudryantoninm@gmail.com"},
|
|
11
11
|
]
|
|
@@ -19,16 +19,18 @@ classifiers = [
|
|
|
19
19
|
]
|
|
20
20
|
|
|
21
21
|
readme = "README.md"
|
|
22
|
-
requires-python = ">=3.
|
|
22
|
+
requires-python = ">=3.11"
|
|
23
23
|
dependencies = [
|
|
24
|
-
"numpy
|
|
24
|
+
"numpy==2.6.4",
|
|
25
25
|
"nibabel>5.0",
|
|
26
26
|
"pandas>3.0",
|
|
27
|
+
"napari[all]",
|
|
27
28
|
"scipy>1.15",
|
|
28
29
|
"requests>2.0",
|
|
29
30
|
"dipy>1.10",
|
|
30
31
|
"templateflow",
|
|
31
32
|
"networkx>=3.6.1",
|
|
33
|
+
"antspyx",
|
|
32
34
|
]
|
|
33
35
|
|
|
34
36
|
license = "GPL-3.0-only"
|
|
@@ -43,13 +43,15 @@ def main():
|
|
|
43
43
|
)
|
|
44
44
|
|
|
45
45
|
parser.add_argument(
|
|
46
|
-
"--
|
|
46
|
+
"--sc_distance_threshold",
|
|
47
|
+
type=float,
|
|
47
48
|
default=5.0,
|
|
48
49
|
help='[float (0,inf), default=5.0] Defines threshold for distance filtering of tracts endpoints in structural connectivity computation.'
|
|
49
50
|
)
|
|
50
51
|
|
|
51
52
|
parser.add_argument(
|
|
52
53
|
"--spared_threshold",
|
|
54
|
+
type=float,
|
|
53
55
|
default=0.5,
|
|
54
56
|
help='[float (0,1), default=0.5] Defines threshold for spared parcel-to-parcel connectivity'
|
|
55
57
|
)
|
|
@@ -68,6 +70,7 @@ def main():
|
|
|
68
70
|
tracts_cfg=args.tracts_cfg,
|
|
69
71
|
out_folder=args.out_folder,
|
|
70
72
|
registration_type=args.registration_type,
|
|
73
|
+
sc_distance_threshold=args.sc_distance_threshold,
|
|
71
74
|
spared_threshold=args.spared_threshold,
|
|
72
75
|
delete_tmp=args.delete_tmp,
|
|
73
76
|
)
|
|
@@ -3,8 +3,9 @@ import nibabel as nib
|
|
|
3
3
|
|
|
4
4
|
from nibabel.processing import resample_from_to
|
|
5
5
|
from .utils import compute_parcel_damage
|
|
6
|
+
from pyLQT.utils.logs import log_execution
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
@log_execution
|
|
8
9
|
def get_parcel_damage(
|
|
9
10
|
lesion,
|
|
10
11
|
atlas,
|
|
@@ -44,14 +45,11 @@ def get_parcel_damage(
|
|
|
44
45
|
Returns
|
|
45
46
|
-------
|
|
46
47
|
pandas.DataFrame
|
|
47
|
-
Copy of ``atlas_labels`` with an additional ``out_col``
|
|
48
|
-
|
|
49
|
-
lesion.
|
|
48
|
+
Copy of ``atlas_labels`` with an additional column, defined by ``out_col``
|
|
49
|
+
containing the proportion of each parcel affected by the lesion.
|
|
50
50
|
"""
|
|
51
51
|
|
|
52
|
-
lesion_resampled = resample_from_to(lesion,
|
|
53
|
-
atlas,
|
|
54
|
-
order=1)
|
|
52
|
+
lesion_resampled = resample_from_to(lesion, atlas, order=1)
|
|
55
53
|
|
|
56
54
|
lesion_volume = lesion_resampled.get_fdata()
|
|
57
55
|
atlas_volume = atlas.get_fdata().astype(np.int32)
|
|
@@ -79,5 +77,4 @@ def get_parcel_damage(
|
|
|
79
77
|
if out_path:
|
|
80
78
|
damage_frame.to_csv(out_path / 'parcel_damage_frame.csv', index=False)
|
|
81
79
|
|
|
82
|
-
return damage_frame
|
|
83
|
-
|
|
80
|
+
return damage_frame
|
|
@@ -3,7 +3,9 @@ import networkx as nx
|
|
|
3
3
|
|
|
4
4
|
from scipy.sparse import csr_matrix
|
|
5
5
|
from scipy.sparse.csgraph import shortest_path
|
|
6
|
+
from pyLQT.utils.logs import log_execution
|
|
6
7
|
|
|
8
|
+
@log_execution
|
|
7
9
|
def get_parcel_wise_disconnection_severity(
|
|
8
10
|
structural_connectivity,
|
|
9
11
|
structural_connectivity_disconnected,
|
|
@@ -65,7 +67,7 @@ def get_parcel_wise_disconnection_severity(
|
|
|
65
67
|
|
|
66
68
|
return disconnections_severity, per_spared_connections
|
|
67
69
|
|
|
68
|
-
|
|
70
|
+
@log_execution
|
|
69
71
|
def get_sspl_increase(
|
|
70
72
|
structural_connectivity,
|
|
71
73
|
per_spared_connections,
|
|
@@ -34,7 +34,7 @@ def atlas_cfg_read(atlas_cfg_path):
|
|
|
34
34
|
Name of the atlas.
|
|
35
35
|
- atlas : nibabel.Nifti1Image
|
|
36
36
|
Loaded atlas image.
|
|
37
|
-
-
|
|
37
|
+
- labels : pandas.DataFrame
|
|
38
38
|
DataFrame containing the atlas parcel labels.
|
|
39
39
|
- atlas_space : str
|
|
40
40
|
Space in which the atlas is defined.
|
|
@@ -66,9 +66,6 @@ def tracts_cfg_read(tracts_cfg_path):
|
|
|
66
66
|
- 'tracts_folder':
|
|
67
67
|
path to the folder containing .trk files, relative to
|
|
68
68
|
the folder containing the config file
|
|
69
|
-
- 'references_folder':
|
|
70
|
-
path to the folder containing nifti reference files
|
|
71
|
-
of the tracts, relative to the folder containing the config file
|
|
72
69
|
- 'space':
|
|
73
70
|
standard space name available in templateflow or path
|
|
74
71
|
to reference nifti relative to the folder containing the config file
|
|
@@ -82,20 +79,18 @@ def tracts_cfg_read(tracts_cfg_path):
|
|
|
82
79
|
Name of the tracts.
|
|
83
80
|
- tracts_folder : pathlib.Path
|
|
84
81
|
Path to folder containing .trk tract files.
|
|
85
|
-
- references_folder : pathlib.Path
|
|
86
|
-
Path to folder containing .nii tract reference files.
|
|
87
82
|
- tracts_space : str
|
|
88
|
-
Space in which the tracts are defined
|
|
83
|
+
Space in which the tracts are defined, that can be used as reference when
|
|
84
|
+
loaded.
|
|
89
85
|
"""
|
|
90
86
|
with tracts_cfg_path.open("r") as f:
|
|
91
87
|
tracts_cfg = json.load(f)
|
|
92
88
|
|
|
93
89
|
tracts_name = tracts_cfg.get("name")
|
|
94
90
|
tracts_folder = Path(tracts_cfg_path.parent / tracts_cfg.get("tracts"))
|
|
95
|
-
references_folder = Path(tracts_cfg_path.parent / tracts_cfg.get("references"))
|
|
96
91
|
tracts_space = tracts_cfg.get("space")
|
|
97
92
|
|
|
98
|
-
return tracts_name, tracts_folder,
|
|
93
|
+
return tracts_name, tracts_folder, tracts_space
|
|
99
94
|
|
|
100
95
|
def lesion_cfg_read(lesion_cfg_path: Path):
|
|
101
96
|
"""Read a tracts configuration file and load the associated data.
|
|
@@ -134,4 +129,7 @@ def lesion_cfg_read(lesion_cfg_path: Path):
|
|
|
134
129
|
lesion_folder = Path(lesion_cfg_path.parent / lesion_cfg.get("lesion_folder"))
|
|
135
130
|
lesion_space = lesion_cfg.get("space")
|
|
136
131
|
|
|
137
|
-
return lesion_cohort_name, lesion_folder, lesion_space
|
|
132
|
+
return lesion_cohort_name, lesion_folder, lesion_space
|
|
133
|
+
|
|
134
|
+
if __name__=="__main__":
|
|
135
|
+
atlas_cfg_read(Path('D:/internship_myspace_lab/pyLQT/test_download/Schaefer2018_100Parcels_7Networks_order.csv'))
|
|
@@ -4,111 +4,13 @@ import warnings
|
|
|
4
4
|
import requests
|
|
5
5
|
import shutil
|
|
6
6
|
import gzip
|
|
7
|
+
import re
|
|
7
8
|
import nibabel as nib
|
|
8
9
|
import numpy as np
|
|
9
10
|
import pandas as pd
|
|
10
11
|
|
|
11
12
|
from templateflow import api as tf
|
|
12
13
|
from pathlib import Path
|
|
13
|
-
|
|
14
|
-
def create_tract_list(
|
|
15
|
-
references_folder,
|
|
16
|
-
tracts_folder
|
|
17
|
-
):
|
|
18
|
-
"""Create paired lists of tractogram and reference image paths.
|
|
19
|
-
|
|
20
|
-
Tractograms and their corresponding NIfTI reference images are matched
|
|
21
|
-
based on their relative paths within their respective folders. The
|
|
22
|
-
function also checks that all reference images share the same voxel
|
|
23
|
-
grid and affine.
|
|
24
|
-
|
|
25
|
-
Parameters
|
|
26
|
-
----------
|
|
27
|
-
references_folder : pathlib.Path
|
|
28
|
-
Root directory containing the NIfTI reference images.
|
|
29
|
-
tracts_folder : pathlib.Path
|
|
30
|
-
Root directory containing the tractogram files.
|
|
31
|
-
|
|
32
|
-
Returns
|
|
33
|
-
-------
|
|
34
|
-
tuple[list[pathlib.Path], list[pathlib.Path]]
|
|
35
|
-
Two lists containing:
|
|
36
|
-
|
|
37
|
-
- tracts_pathlist : list[pathlib.Path]
|
|
38
|
-
Paths to the tractogram files.
|
|
39
|
-
- references_pathlist : list[pathlib.Path]
|
|
40
|
-
Paths to the corresponding NIfTI reference images.
|
|
41
|
-
|
|
42
|
-
Only tractograms with a corresponding NIfTI reference are included.
|
|
43
|
-
Warnings are issued for missing pairs and for reference images with
|
|
44
|
-
mismatched grids or affines.
|
|
45
|
-
"""
|
|
46
|
-
trk_paths = sorted(tracts_folder.rglob("*.trk.gz"))
|
|
47
|
-
nifti_paths = sorted(references_folder.rglob("*.nii.gz"))
|
|
48
|
-
|
|
49
|
-
# Check that every TRK has a corresponding NIfTI
|
|
50
|
-
nifti_relatives = {
|
|
51
|
-
p.relative_to(references_folder)
|
|
52
|
-
for p in nifti_paths
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
for trk_path in trk_paths:
|
|
56
|
-
trk_relative = trk_path.relative_to(tracts_folder)
|
|
57
|
-
expected_nifti = Path(
|
|
58
|
-
str(trk_relative).replace(".trk.gz", ".nii.gz")
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
if expected_nifti not in nifti_relatives:
|
|
62
|
-
warnings.warn(f"No NIfTI found for {expected_nifti}")
|
|
63
|
-
|
|
64
|
-
# Check that every NIfTI has a corresponding TRK
|
|
65
|
-
trk_relatives = {
|
|
66
|
-
p.relative_to(tracts_folder)
|
|
67
|
-
for p in trk_paths
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
for nifti_path in nifti_paths:
|
|
71
|
-
nifti_relative = nifti_path.relative_to(references_folder)
|
|
72
|
-
expected_trk = Path(
|
|
73
|
-
str(nifti_relative).replace(".nii.gz", ".trk.gz")
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
if expected_trk not in trk_relatives:
|
|
77
|
-
warnings.warn(f"No TRK found for {expected_trk}")
|
|
78
|
-
|
|
79
|
-
tracts_pathlist = []
|
|
80
|
-
references_pathlist = []
|
|
81
|
-
|
|
82
|
-
for trk_path in trk_paths:
|
|
83
|
-
trk_relative = trk_path.relative_to(tracts_folder)
|
|
84
|
-
|
|
85
|
-
nifti_path = (
|
|
86
|
-
references_folder
|
|
87
|
-
/ Path(str(trk_relative).replace(".trk.gz", ".nii.gz"))
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
if not nifti_path.exists():
|
|
91
|
-
continue
|
|
92
|
-
|
|
93
|
-
tracts_pathlist.append(trk_path)
|
|
94
|
-
references_pathlist.append(nifti_path)
|
|
95
|
-
|
|
96
|
-
# Verify that all NIfTIs are on the same grid
|
|
97
|
-
reference = None
|
|
98
|
-
|
|
99
|
-
# TODO too simple check, should be based on affine with real world coordinates.
|
|
100
|
-
for nifti_path in references_pathlist:
|
|
101
|
-
nifti = nib.nifti1.load(nifti_path)
|
|
102
|
-
|
|
103
|
-
if reference is None:
|
|
104
|
-
reference = nifti
|
|
105
|
-
else:
|
|
106
|
-
if nifti.shape != reference.shape:
|
|
107
|
-
warnings.warn(f"NIfTI grid mismatch: {nifti_path}")
|
|
108
|
-
if not np.allclose(nifti.affine, reference.affine):
|
|
109
|
-
warnings.warn(f"NIfTI affine mismatch: {nifti_path}")
|
|
110
|
-
|
|
111
|
-
return tracts_pathlist, references_pathlist
|
|
112
14
|
|
|
113
15
|
def uncompress_trk_gz(
|
|
114
16
|
trk_path,
|
|
@@ -137,6 +39,77 @@ def uncompress_trk_gz(
|
|
|
137
39
|
|
|
138
40
|
return out_path
|
|
139
41
|
|
|
42
|
+
|
|
43
|
+
def fetch_schaefer_atlas(
|
|
44
|
+
out_folder,
|
|
45
|
+
n_parcel=100,
|
|
46
|
+
n_network=7,
|
|
47
|
+
resolution=1
|
|
48
|
+
):
|
|
49
|
+
if n_parcel not in (100, 200, 300, 400, 500, 600, 700, 800, 900, 1000):
|
|
50
|
+
raise ValueError("Number of parcels must be between 100 and 1000.")
|
|
51
|
+
if n_network not in (7, 17):
|
|
52
|
+
raise ValueError("Number of networks must be 7 or 17.")
|
|
53
|
+
if resolution not in (1, 2):
|
|
54
|
+
raise ValueError("Resolution must be 1 or 2 mm.")
|
|
55
|
+
|
|
56
|
+
base_url = (
|
|
57
|
+
"https://api.github.com/repos/ThomasYeoLab/CBIG/contents/"
|
|
58
|
+
"stable_projects/brain_parcellation/Schaefer2018_LocalGlobal/"
|
|
59
|
+
"Parcellations/MNI"
|
|
60
|
+
)
|
|
61
|
+
response = requests.get(base_url, timeout=30)
|
|
62
|
+
response.raise_for_status()
|
|
63
|
+
files = response.json()
|
|
64
|
+
|
|
65
|
+
prefix = f"Schaefer2018_{n_parcel}Parcels_{n_network}Networks_order"
|
|
66
|
+
atlas_name = f"{prefix}_FSLMNI152_{resolution}mm.nii.gz"
|
|
67
|
+
labels_name = f"{prefix}.txt"
|
|
68
|
+
available = {f["name"]: f for f in files}
|
|
69
|
+
|
|
70
|
+
labels_url = f"{base_url}/freeview_lut"
|
|
71
|
+
response = requests.get(labels_url, timeout=30)
|
|
72
|
+
response.raise_for_status()
|
|
73
|
+
labels_files = {f["name"]: f for f in response.json()}
|
|
74
|
+
|
|
75
|
+
if atlas_name not in available:
|
|
76
|
+
raise FileNotFoundError(f"Atlas not found: {atlas_name}")
|
|
77
|
+
if labels_name not in labels_files:
|
|
78
|
+
raise FileNotFoundError(f"Labels not found: {labels_name}")
|
|
79
|
+
|
|
80
|
+
out_folder = Path(out_folder)
|
|
81
|
+
out_folder.mkdir(parents=True, exist_ok=True)
|
|
82
|
+
|
|
83
|
+
results = {}
|
|
84
|
+
for key, filename, file_info in (("atlas", atlas_name, available[atlas_name]), ("labels", labels_name, labels_files[labels_name])):
|
|
85
|
+
output_path = out_folder / filename
|
|
86
|
+
if not output_path.exists():
|
|
87
|
+
with requests.get(file_info["download_url"], stream=True, timeout=60) as r:
|
|
88
|
+
r.raise_for_status()
|
|
89
|
+
with open(output_path, "wb") as f:
|
|
90
|
+
for chunk in r.iter_content(chunk_size=8192):
|
|
91
|
+
if chunk:
|
|
92
|
+
f.write(chunk)
|
|
93
|
+
results[key] = output_path
|
|
94
|
+
|
|
95
|
+
schaefer_atlas = nib.load(results["atlas"])
|
|
96
|
+
with open(results["labels"], "r") as f:
|
|
97
|
+
schaefer_labels = pd.DataFrame(
|
|
98
|
+
[
|
|
99
|
+
parts[:2]
|
|
100
|
+
for line in f
|
|
101
|
+
if (parts := line.strip().split())
|
|
102
|
+
],
|
|
103
|
+
columns=["Parcel", "Label"]
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
csv_path = results["labels"].with_suffix(".csv")
|
|
107
|
+
schaefer_labels.to_csv(csv_path, index=False)
|
|
108
|
+
schaefer_labels = pd.read_csv(csv_path)
|
|
109
|
+
print(schaefer_labels)
|
|
110
|
+
return schaefer_atlas, schaefer_labels
|
|
111
|
+
|
|
112
|
+
|
|
140
113
|
def default_atlas_download(out_folder: Path):
|
|
141
114
|
atlas_cfg_folder = out_folder / "glasser360_config"
|
|
142
115
|
atlas_cfg_folder.mkdir(parents=True, exist_ok=True)
|
|
@@ -204,9 +177,8 @@ def default_atlas_download(out_folder: Path):
|
|
|
204
177
|
def default_tracts_download(out_folder: Path):
|
|
205
178
|
"""Download the default HCP1065 tractography dataset and create its configuration.
|
|
206
179
|
|
|
207
|
-
The function downloads the HCP1065 average tractograms and
|
|
208
|
-
|
|
209
|
-
file. Existing folders are reused.
|
|
180
|
+
The function downloads the HCP1065 average tractograms and creates a default
|
|
181
|
+
configuration file. Existing folders are reused.
|
|
210
182
|
|
|
211
183
|
Parameters
|
|
212
184
|
----------
|
|
@@ -248,41 +220,11 @@ def default_tracts_download(out_folder: Path):
|
|
|
248
220
|
|
|
249
221
|
zip_path.unlink()
|
|
250
222
|
|
|
251
|
-
references_folder = tracts_cfg_folder / "hcp1065_avg_tracts"
|
|
252
|
-
|
|
253
|
-
if references_folder.exists():
|
|
254
|
-
warnings.warn(
|
|
255
|
-
f"References folder already exists, using it: {references_folder}",
|
|
256
|
-
UserWarning,
|
|
257
|
-
stacklevel=2
|
|
258
|
-
)
|
|
259
|
-
else:
|
|
260
|
-
url = "https://github.com/data-others/atlas/releases/download/hcp1065/hcp1065_avg_tracts_nifti.zip"
|
|
261
|
-
zip_path = out_folder / "hcp1065_avg_tracts_nifti.zip"
|
|
262
|
-
|
|
263
|
-
response = requests.get(url, stream=True, timeout=60)
|
|
264
|
-
response.raise_for_status()
|
|
265
|
-
|
|
266
|
-
with zip_path.open("wb") as f:
|
|
267
|
-
for chunk in response.iter_content(chunk_size=1024*1024):
|
|
268
|
-
if chunk:
|
|
269
|
-
f.write(chunk)
|
|
270
|
-
|
|
271
|
-
with zipfile.ZipFile(zip_path) as zip_file:
|
|
272
|
-
zip_file.extractall(tracts_cfg_folder)
|
|
273
|
-
|
|
274
|
-
zip_path.unlink()
|
|
275
|
-
|
|
276
|
-
extracted_niftis = tracts_cfg_folder / "nifti"
|
|
277
|
-
if extracted_niftis.exists():
|
|
278
|
-
extracted_niftis.rename(references_folder)
|
|
279
|
-
|
|
280
223
|
tracts_name = "HCP1065"
|
|
281
224
|
tracts_space = "MNI152NLin2009aAsym"
|
|
282
225
|
|
|
283
226
|
config = {"name": tracts_name,
|
|
284
227
|
"tracts": "hcp1065_avg_tracts_trk",
|
|
285
|
-
"references": "hcp1065_avg_tracts",
|
|
286
228
|
"space": tracts_space}
|
|
287
229
|
|
|
288
230
|
tracts_cfg = Path(tracts_cfg_folder / "HCP1065_config.json")
|
|
@@ -319,4 +261,4 @@ def get_ref_space_tf(space: str, res: int = 1):
|
|
|
319
261
|
|
|
320
262
|
if __name__=="__main__":
|
|
321
263
|
|
|
322
|
-
|
|
264
|
+
fetch_schaefer_atlas(out_folder='D:/internship_myspace_lab/pyLQT/schaefer_100parcels_17networks', n_network=17)
|
|
@@ -4,6 +4,11 @@ from dipy.align.imwarp import SymmetricDiffeomorphicRegistration
|
|
|
4
4
|
from dipy.align.metrics import CCMetric
|
|
5
5
|
from dipy.align import write_mapping
|
|
6
6
|
|
|
7
|
+
from ants.utils.nibabel_nifti_to_ants import from_nibabel_nifti, to_nibabel_nifti
|
|
8
|
+
import ants
|
|
9
|
+
import nibabel as nib
|
|
10
|
+
|
|
11
|
+
'''
|
|
7
12
|
def register_affine(
|
|
8
13
|
moving,
|
|
9
14
|
static,
|
|
@@ -107,4 +112,102 @@ def register_syn(
|
|
|
107
112
|
if out_path:
|
|
108
113
|
write_mapping(mapping, out_path)
|
|
109
114
|
|
|
110
|
-
return mapping
|
|
115
|
+
return mapping
|
|
116
|
+
'''
|
|
117
|
+
|
|
118
|
+
def register_affine(
|
|
119
|
+
moving,
|
|
120
|
+
static,
|
|
121
|
+
out_path=None
|
|
122
|
+
):
|
|
123
|
+
"""Register a moving image to a static image using affine registration.
|
|
124
|
+
|
|
125
|
+
Parameters
|
|
126
|
+
----------
|
|
127
|
+
moving : nibabel.Nifti1Image
|
|
128
|
+
Image to be registered to the static image.
|
|
129
|
+
static : nibabel.Nifti1Image
|
|
130
|
+
Reference image defining the target space.
|
|
131
|
+
out_path : pathlib.Path | None, optional
|
|
132
|
+
Prefix for saving registration transforms. If None, ANTs
|
|
133
|
+
manages the transform output files.
|
|
134
|
+
|
|
135
|
+
Returns
|
|
136
|
+
-------
|
|
137
|
+
dict
|
|
138
|
+
ANTs registration result containing forward and inverse transforms.
|
|
139
|
+
"""
|
|
140
|
+
|
|
141
|
+
moving_ants = from_nibabel_nifti(moving)
|
|
142
|
+
static_ants = from_nibabel_nifti(static)
|
|
143
|
+
|
|
144
|
+
registration = ants.registration(
|
|
145
|
+
fixed=static_ants,
|
|
146
|
+
moving=moving_ants,
|
|
147
|
+
type_of_transform="Affine",
|
|
148
|
+
aff_metric="mattes",
|
|
149
|
+
aff_sampling=32,
|
|
150
|
+
aff_random_sampling_rate=0.2,
|
|
151
|
+
aff_iterations=(500, 250, 100),
|
|
152
|
+
verbose=False,
|
|
153
|
+
outprefix=str(out_path) if out_path else ""
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
return registration
|
|
157
|
+
|
|
158
|
+
def register_syn(
|
|
159
|
+
moving,
|
|
160
|
+
static,
|
|
161
|
+
out_path=None
|
|
162
|
+
):
|
|
163
|
+
"""Register a moving image to a static image using SyN registration.
|
|
164
|
+
|
|
165
|
+
Parameters
|
|
166
|
+
----------
|
|
167
|
+
moving : nibabel.Nifti1Image
|
|
168
|
+
Image to be registered to the static image.
|
|
169
|
+
static : nibabel.Nifti1Image
|
|
170
|
+
Reference image defining the target space.
|
|
171
|
+
out_path : pathlib.Path | None, optional
|
|
172
|
+
Prefix for saving registration transforms. If None, ANTs
|
|
173
|
+
manages the transform output files.
|
|
174
|
+
|
|
175
|
+
Returns
|
|
176
|
+
-------
|
|
177
|
+
dict
|
|
178
|
+
ANTs registration result containing forward and inverse transforms.
|
|
179
|
+
"""
|
|
180
|
+
|
|
181
|
+
moving_ants = from_nibabel_nifti(moving)
|
|
182
|
+
static_ants = from_nibabel_nifti(static)
|
|
183
|
+
|
|
184
|
+
registration = ants.registration(
|
|
185
|
+
fixed=static_ants,
|
|
186
|
+
moving=moving_ants,
|
|
187
|
+
type_of_transform="SyN",
|
|
188
|
+
syn_metric="CC",
|
|
189
|
+
syn_sampling=4,
|
|
190
|
+
reg_iterations=(20, 10, 5),
|
|
191
|
+
verbose=False,
|
|
192
|
+
outprefix=str(out_path) if out_path else ""
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
return registration
|
|
196
|
+
|
|
197
|
+
def apply_registration(
|
|
198
|
+
moving,
|
|
199
|
+
static,
|
|
200
|
+
registration,
|
|
201
|
+
interpolation="linear"
|
|
202
|
+
):
|
|
203
|
+
moving_ants = from_nibabel_nifti(moving)
|
|
204
|
+
static_ants = from_nibabel_nifti(static)
|
|
205
|
+
|
|
206
|
+
transformed = ants.apply_transforms(
|
|
207
|
+
fixed=static_ants,
|
|
208
|
+
moving=moving_ants,
|
|
209
|
+
transformlist=registration["fwdtransforms"],
|
|
210
|
+
interpolator=interpolation
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
return to_nibabel_nifti(transformed)
|