Skip to content

Commit 14da3df

Browse files
committed
Unit tests
1 parent ca0cb0b commit 14da3df

17 files changed

Lines changed: 472 additions & 20 deletions

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ Running our prebuilt ```sfcn-reg-predict-brain-age```-container can be done via
7272
docker run \
7373
--rm \
7474
--name predict-brain-age \
75-
--mount type=bind,source=/Users/esten/images,target=/images \
76-
--mount type=bind,source=/Users/esten/preds,target=/predictions \
75+
--mount type=bind,source=<path-to-folder-with-images>,target=/images \
76+
--mount type=bind,source=<path-to-folder-with-predictions>,target=/predictions \
7777
estenhl/sfcn-reg-predict-brain-age
7878
```
7979

@@ -94,8 +94,23 @@ docker run \
9494
-it \
9595
--rm \
9696
--mount type=bind,source=/Users/esten/freesurfer-license.txt,target=/usr/local/freesurfer/license.txt \
97-
--mount type=bind,source=/Users/esten/tmp/images/tmp.nii.gz,target=/tmp.nii.gz \
97+
--mount type=bind,source=<path-to-example-image>,target=/tmp.nii.gz \
9898
estenhl/freesurfer:5.3
9999
mkdir subjects
100100
recon-all -sd subjects -s tmp -i /tmp.nii.gz -autorecon1
101+
```
102+
### 2. Build a FreeSurfer and FSL container
103+
```
104+
docker build \
105+
--tag estenhl/freesurfer_and_fsl:6.0 \
106+
--file docker/Dockerfile.freesurfer_and_fsl .
107+
```
108+
109+
### 2b. Test the FreeSurfer and FSL container
110+
Test the FSL-portion of the container by running flirt
111+
```
112+
flirt \
113+
-in tmp.nii.gz \
114+
-out flirted.nii.gz \
115+
-ref /usr/local/fsl/data/linearMNI/MNI152lin_T1_1mm_brain.nii.gz
101116
```
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM estenhl/freesurfer:5.3
22

33
RUN yum update
4-
RUN yum -y install bc dc git libopenblas python tar unzip wget
4+
RUN yum -y install bc dc git libopenblas python tar unzip wget file libquadmath
55
RUN curl -sL https://rpm.nodesource.com/setup_10.x | bash -
66
RUN yum -y install nodejs npm
77
RUN yum clean all
@@ -11,13 +11,12 @@ RUN npm install -gq bids-validator
1111

1212
RUN wget -q http://fsl.fmrib.ox.ac.uk/fsldownloads/fslinstaller.py
1313
RUN chmod 755 /fslinstaller.py
14-
#RUN /fslinstaller.py -d /usr/local/fsl -q
14+
RUN /fslinstaller.py -d /usr/local/fsl -q
1515

16-
#ENV FSLDIR=/usr/local/fsl
17-
#ENV PATH=$FSLDIR/bin:$PATH
18-
#RUN /bin/bash -c 'source /usr/local/fsl/etc/fslconf/fsl.sh'
19-
#ENV FSLMULTIFILEQUIT=TRUE
20-
#ENV FSLOUTPUTTYPE=NIFTI_GZ
21-
#RUN ln -s /usr/local/fsl/bin/eddy_openmp /usr/local/fsl/bin/eddy
16+
ENV FSLDIR=/usr/local/fsl
17+
ENV PATH=$FSLDIR/bin:$PATH
18+
RUN /bin/bash -c 'source /usr/local/fsl/etc/fslconf/fsl.sh'
19+
ENV FSLMULTIFILEQUIT=TRUE
20+
ENV FSLOUTPUTTYPE=NIFTI_GZ
2221

2322
CMD bash

notebooks/Download and preprocess IXI.ipynb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5+
"id": "c86bc653",
56
"metadata": {},
67
"source": [
78
"## 0. Configure paths and URLs"
@@ -10,6 +11,7 @@
1011
{
1112
"cell_type": "code",
1213
"execution_count": null,
14+
"id": "bfbb419a",
1315
"metadata": {},
1416
"outputs": [],
1517
"source": [
@@ -34,6 +36,7 @@
3436
},
3537
{
3638
"cell_type": "markdown",
39+
"id": "45a3f9e7",
3740
"metadata": {},
3841
"source": [
3942
"## 1. Setup folder structure and ensure all necessary software and packages is installed"
@@ -42,6 +45,7 @@
4245
{
4346
"cell_type": "code",
4447
"execution_count": null,
48+
"id": "87ad8b9a",
4549
"metadata": {},
4650
"outputs": [],
4751
"source": [
@@ -66,6 +70,7 @@
6670
},
6771
{
6872
"cell_type": "markdown",
73+
"id": "fea34c9b",
6974
"metadata": {},
7075
"source": [
7176
"## 2. Download, extract and reorganize the data"
@@ -74,6 +79,7 @@
7479
{
7580
"cell_type": "code",
7681
"execution_count": null,
82+
"id": "d8322ab2",
7783
"metadata": {},
7884
"outputs": [],
7985
"source": [
@@ -113,6 +119,7 @@
113119
},
114120
{
115121
"cell_type": "markdown",
122+
"id": "aec8d830",
116123
"metadata": {},
117124
"source": [
118125
"## 3. Preprocess the data"
@@ -121,6 +128,7 @@
121128
{
122129
"cell_type": "code",
123130
"execution_count": null,
131+
"id": "48f0b9ad",
124132
"metadata": {},
125133
"outputs": [],
126134
"source": [
@@ -136,6 +144,7 @@
136144
{
137145
"cell_type": "code",
138146
"execution_count": null,
147+
"id": "52b2e554",
139148
"metadata": {},
140149
"outputs": [],
141150
"source": [
@@ -168,6 +177,7 @@
168177
{
169178
"cell_type": "code",
170179
"execution_count": null,
180+
"id": "469a67c7",
171181
"metadata": {},
172182
"outputs": [],
173183
"source": [
@@ -190,6 +200,7 @@
190200
{
191201
"cell_type": "code",
192202
"execution_count": null,
203+
"id": "86837eca",
193204
"metadata": {},
194205
"outputs": [],
195206
"source": [
@@ -212,6 +223,7 @@
212223
{
213224
"cell_type": "code",
214225
"execution_count": null,
226+
"id": "1e8fa9d4",
215227
"metadata": {},
216228
"outputs": [],
217229
"source": [
@@ -234,6 +246,7 @@
234246
{
235247
"cell_type": "code",
236248
"execution_count": null,
249+
"id": "5f4d1cb0",
237250
"metadata": {},
238251
"outputs": [],
239252
"source": [
@@ -258,6 +271,7 @@
258271
{
259272
"cell_type": "code",
260273
"execution_count": null,
274+
"id": "57dbe8d7",
261275
"metadata": {},
262276
"outputs": [],
263277
"source": [
@@ -272,6 +286,7 @@
272286
{
273287
"cell_type": "code",
274288
"execution_count": null,
289+
"id": "398a46fe",
275290
"metadata": {},
276291
"outputs": [],
277292
"source": []

pyment/callbacks/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .resetter import Resettable
1+
from .resetter import Resettable, Resetter

pyment/callbacks/resetter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Resetter(Callback):
1717
def __init__(self, obj: Resettable):
1818
super().__init__()
1919

20+
assert isinstance(obj, Resettable)
21+
2022
self.obj = obj
2123

2224
def on_epoch_end(self, *args, **kwargs) -> None:

pyment/data/datasets/dataset.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class Dataset(ABC):
77
@abstractproperty
88
def y(self) -> np.ndarray:
99
"""Returns the target vector of the dataset"""
10+
pass
1011

11-
def __len__(self):
12-
return len(self.y)
12+
@abstractproperty
13+
def __len__(self) -> int:
14+
"""Returns length of the dataset"""
15+
pass

pyment/data/datasets/nifti_dataset.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def from_folder(cls, root: str, *, images: str = 'images',
6262

6363
@property
6464
def variables(self):
65-
if len(self._labels) == 0:
65+
if self._labels is None or len(self._labels) == 0:
6666
return []
6767

6868
return list(self._labels.keys())
@@ -111,4 +111,7 @@ def __init__(self, paths: np.ndarray,
111111
target: str = None) -> NiftiDataset:
112112
self._paths = paths
113113
self._labels = labels
114-
self.target = target
114+
self.target = target
115+
116+
def __len__(self) -> int:
117+
return len(self.paths)

pyment/utils/preprocessing/freesurfer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def run(self):
7676

7777
def convert_mgz_to_nii_gz(src: str, dest: str, *,
7878
silence: bool = True) -> None:
79+
assert src.endswith('mgz'), ('First argument to convert_mgz_to_nii_gz '
80+
'be an mgz-file')
81+
7982
logger.debug(f'Running convert on {src}')
8083

8184
cmd = f'mri_convert {src} {dest} -ot nii'

pyment/utils/preprocessing/fsl.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ def reorient2std_folder(src: str, dest: str, *, silence: bool = True):
3232
silence=silence)
3333

3434

35-
def flirt(src: str, dest: str, *, template: str, silence: bool = True):
35+
def flirt(src: str, dest: str, *, template: str,
36+
degrees_of_freedom: int = 6, silence: bool = True):
3637
logger.debug(f'Running flirt on {src} with template {template}')
3738

38-
cmd = f'flirt -in {src} -out {dest} -ref {template} -dof 6'
39+
cmd = (f'flirt -in {src} -out {dest} -ref {template} '
40+
f'-dof {degrees_of_freedom}')
3941

4042
run(cmd, silence=silence)
4143

4244

43-
def flirt_folder(src: str, dest: str, *, template: str, silence: bool = True):
45+
def flirt_folder(src: str, dest: str, *, template: str,
46+
degrees_of_freedom: int = 6, silence: bool = True):
4447
if not os.path.isdir(dest):
4548
os.makedirs(dest)
4649

@@ -52,4 +55,4 @@ def flirt_folder(src: str, dest: str, *, template: str, silence: bool = True):
5255
continue
5356

5457
flirt(os.path.join(src, filename), path, template=template,
55-
silence=silence)
58+
silence=silence, degrees_of_freedom=degrees_of_freedom)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
jupyterlab==3.2.1
22
matplotlib==3.4.3
3+
mock==4.0.3
34
nibabel==3.2.1
45
pandas==1.3.4
56
pytest==6.2.4

0 commit comments

Comments
 (0)