Skip to content

Commit b781e9a

Browse files
committed
[MNT] Fix and update
1 parent b06c9db commit b781e9a

File tree

11 files changed

+558
-66
lines changed

11 files changed

+558
-66
lines changed

docs/api.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Python Reference API
111111
permtest_1samp
112112
permtest_rel
113113
permtest_pearsonr
114+
get_dominance_stats
114115

115116
.. _ref_metrics:
116117

@@ -148,6 +149,7 @@ Functions to download atlases and templates
148149
:toctree: generated/
149150

150151
fetch_cammoun2012
152+
fetch_civet
151153
fetch_conte69
152154
fetch_fsaverage
153155
fetch_pauli2018
@@ -195,6 +197,24 @@ Functions to generate (pseudo-random) datasets
195197
spin_data
196198
spin_parcels
197199

200+
.. _ref_civet:
201+
202+
:mod:`netneurotools.civet` - CIVET compatibility functions
203+
----------------------------------------------------------
204+
205+
.. automodule:: netneurotools.civet
206+
:no-members:
207+
:no-inherited-members:
208+
209+
.. currentmodule:: netneurotools.civet
210+
211+
.. autosummary::
212+
:template: function.rst
213+
:toctree: generated/
214+
215+
read_civet
216+
civet_to_freesurfer
217+
198218
.. _ref_utils:
199219

200220
:mod:`netneurotools.utils` - Miscellaneous, grab bag utilities

docs/installation.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ Alternatively, you can install ``netneurotools`` directly from PyPi with:
2727
Optional installation for surface plotting
2828
------------------------------------------
2929

30-
For surface plots like
31-
:py:func:`netneurotools.plotting.plot_fsaverage`, you would need
32-
VTK/mayavi/pysurfer installation. Here is a recipe for installing the
33-
bleeding-edge version of the dependencies. If you already have a working
34-
mayavi/pysurfer installation, there is generally no need to follow this
35-
part.
30+
In order to use surface plotting functionality like
31+
:py:func:`netneurotools.plotting.plot_fsaverage`, you will need a working
32+
``vtk``/``mayavi``/``pysurfer`` installation. These can generally be installed
33+
with the following command:
34+
35+
.. code-block: bash
36+
37+
pip install vtk mayavi pysurfer
38+
39+
However, we include instructions below for installing the bleeding-edge version
40+
of the dependencies. Note: if you already have a working ``mayavi``/``pysurfer``
41+
installation, there is generally no need to follow these instructions!
3642

3743
- Install Qt
3844

netneurotools/civet.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Functions for working with CIVET data (ugh)
4+
"""
5+
6+
import nibabel as nib
7+
import numpy as np
8+
from scipy.interpolate import griddata
9+
10+
from .datasets import fetch_civet, fetch_fsaverage
11+
12+
_MNI305to152 = np.array([[0.9975, -0.0073, 0.0176, -0.0429],
13+
[0.0146, 1.0009, -0.0024, 1.5496],
14+
[-0.0130, -0.0093, 0.9971, 1.1840],
15+
[0.0000, 0.0000, 0.0000, 1.0000]])
16+
17+
18+
def read_civet(fname):
19+
"""
20+
Reads a CIVET-style .obj geometry file
21+
22+
Parameters
23+
----------
24+
fname : str or os.PathLike
25+
Filepath to .obj file
26+
27+
Returns
28+
-------
29+
vertices : (N, 3)
30+
triangles : (T, 3)
31+
"""
32+
33+
k, polygons = 0, []
34+
with open(fname, 'r') as src:
35+
n_vert = int(src.readline().split()[6])
36+
vertices = np.zeros((n_vert, 3))
37+
for i, line in enumerate(src):
38+
if i < n_vert:
39+
vertices[i] = [float(i) for i in line.split()]
40+
elif i >= (2 * n_vert) + 5:
41+
if not line.strip():
42+
k = 1
43+
elif k == 1:
44+
polygons.extend([int(i) for i in line.split()])
45+
46+
triangles = np.reshape(np.asarray(polygons), (-1, 3))
47+
48+
return vertices, triangles
49+
50+
51+
def civet_to_freesurfer(brainmap, surface='mid', version='v1',
52+
freesurfer='fsaverage6', method='nearest',
53+
data_dir=None):
54+
"""
55+
Projects `brainmap` in CIVET space to `freesurfer` fsaverage space
56+
57+
Uses a nearest-neighbor projection based on the geometry of the vertices
58+
59+
Parameters
60+
----------
61+
brainmap : array_like
62+
CIVET brainmap to be converted to freesurfer space
63+
surface : {'white', 'mid'}, optional
64+
Which CIVET surface to use for geometry of `brainmap`. Default: 'mid'
65+
version : {'v1', 'v2'}, optional
66+
Which CIVET version to use for geometry of `brainmap`. Default: 'v1'
67+
freesurfer : str, optional
68+
Which version of FreeSurfer space to project data to. Must be one of
69+
{'fsaverage', 'fsaverage3', 'fsaverage4', 'fsaverage5', 'fsaverage6'}.
70+
Default: 'fsaverage6'
71+
method : {'nearest', 'linear'}, optional
72+
What method of interpolation to use when projecting the data between
73+
surfaces. Default: 'nearest'
74+
data_dir : str, optional
75+
Path to use as data directory. If not specified, will check for
76+
environmental variable 'NNT_DATA'; if that is not set, will use
77+
`~/nnt-data` instead. Default: None
78+
79+
Returns
80+
-------
81+
data : np.ndarray
82+
Provided `brainmap` mapped to FreeSurfer
83+
"""
84+
85+
brainmap = np.asarray(brainmap)
86+
densities = (81924, 327684)
87+
n_vert = brainmap.shape[0]
88+
if n_vert not in densities:
89+
raise ValueError('Unable to interpret `brainmap` space; provided '
90+
'array must have length in {}. Received: {}'
91+
.format(densities, n_vert))
92+
93+
n_vert = n_vert // 2
94+
icbm = fetch_civet(density='41k' if n_vert == 40962 else '164k',
95+
version=version, data_dir=data_dir, verbose=0)[surface]
96+
fsavg = fetch_fsaverage(version=freesurfer, data_dir=data_dir, verbose=0)
97+
fsavg = fsavg['pial' if surface == 'mid' else surface]
98+
99+
data = []
100+
for n, hemi in enumerate(('lh', 'rh')):
101+
sl = slice(n_vert * n, n_vert * (n + 1))
102+
vert_cv, _ = read_civet(getattr(icbm, hemi))
103+
vert_fs = nib.affines.apply_affine(
104+
_MNI305to152, nib.freesurfer.read_geometry(getattr(fsavg, hemi))[0]
105+
)
106+
data.append(griddata(vert_cv, brainmap[sl], vert_fs, method=method))
107+
108+
return np.hstack(data)

netneurotools/data/osf.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,33 @@
113113
"md5": "8f75b95c0e47ae935d10745baefa2c49"
114114
}
115115
},
116+
"tpl-civet": {
117+
"v1": {
118+
"civet41k": {
119+
"url": [
120+
"mb37e",
121+
"601daffd84ecf800fe031868"
122+
],
123+
"md5": "b27219c876464992e1b61da1c60d8d6e"
124+
}
125+
},
126+
"v2": {
127+
"civet41k": {
128+
"url": [
129+
"mb37e",
130+
"601dafe77ad0a80119d9483c"
131+
],
132+
"md5": "a47b015e471c6a800d236f107fda5b4a"
133+
},
134+
"civet164k": {
135+
"url": [
136+
"mb37e",
137+
"601dafe87ad0a8011ad94938"
138+
],
139+
"md5": "02537ea65d5366acd8de729022a34bab"
140+
}
141+
}
142+
},
116143
"ds-connectomes": {
117144
"celegans": {
118145
"url": [
@@ -352,6 +379,15 @@
352379
"md5": "d8378f33107ed5d98c27e8070ebb5aa2"
353380
}
354381
},
382+
"atl-mmpall": {
383+
"fslr32k": {
384+
"url": [
385+
"mb37e",
386+
"6047bac259e910009b83114f"
387+
],
388+
"md5": "fd641742685a239d9c3f60e19a280ca2"
389+
}
390+
},
355391
"atl-voneconomo_koskinas": {
356392
"url": [
357393
"mb37e",

netneurotools/datasets/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
'fetch_cammoun2012', 'fetch_pauli2018', 'fetch_fsaverage', 'fetch_conte69',
77
'fetch_connectome', 'available_connectomes', 'fetch_vazquez_rodriguez2019',
88
'fetch_mirchi2018', 'make_correlated_xy', 'fetch_schaefer2018',
9-
'fetch_hcp_standards', 'fetch_voneconomo'
9+
'fetch_hcp_standards', 'fetch_voneconomo', 'fetch_mmpall', 'fetch_civet'
1010
]
1111

1212
from .fetchers import (fetch_cammoun2012, fetch_pauli2018, fetch_fsaverage,
1313
fetch_conte69, fetch_connectome, available_connectomes,
1414
fetch_vazquez_rodriguez2019, fetch_schaefer2018,
15-
fetch_hcp_standards, fetch_voneconomo)
15+
fetch_hcp_standards, fetch_voneconomo, fetch_mmpall,
16+
fetch_civet)
1617
from .generators import (make_correlated_xy)
1718
from .mirchi import (fetch_mirchi2018)

0 commit comments

Comments
 (0)