Skip to content

Commit 613c083

Browse files
committed
Slightly more basic code
1 parent 19984f5 commit 613c083

File tree

4 files changed

+89
-38
lines changed

4 files changed

+89
-38
lines changed

README.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,49 @@
11
# Diagnostics project
22

3-
Script go in the `scripts` directory.
3+
Scripts go in the `scripts` directory.
44

5-
Library code (such as Python modules or packages) goes in the `packages` directory.
5+
Library code (Python modules) goes in the `findoutlie` directory.
66

7-
You should put this `packages` directory on your Python PATH.
7+
You should put the code in this `findoutlie` directory on your Python PATH.
88

9-
This file has instructions on how to get, validate and process the data.
9+
This README file has instructions on how to get, validate and process the data.
1010

1111
## Get the data
1212

13-
cd data
14-
curl -LO http://nipy.bic.berkeley.edu/psych-214/group00.tar.gz
15-
tar zxvf group00.tar.gz
16-
cd ..
13+
```
14+
cd data
15+
curl -L https://figshare.com/ndownloader/files/{DATA_ID} -o group_data.tar
16+
tar zxvf group.tar
17+
cd ..
18+
```
1719

1820
## Check the data
1921

20-
python3 scripts/validate_data.py data
22+
```
23+
python3 scripts/validate_data.py data
24+
```
2125

2226
## Find outliers
2327

24-
python3 scripts/find_outliers.py data
28+
```
29+
python3 scripts/find_outliers.py data
30+
```
2531

2632
This should print output to the terminal of form:
2733

28-
<filename> <outlier_index>, <outlier_index>, ...
29-
<filename> <outlier_index>, <outlier_index>, ...
34+
```
35+
<filename>, <outlier_index>, <outlier_index>, ...
36+
<filename>, <outlier_index>, <outlier_index>, ...
37+
```
3038

3139
Where `<filename>` is the name of the image that has outlier scans, and
3240
`<outlier_index>` is an index to the volume in the 4D image that you have
33-
indentified as an outlier. 0 refers to the first volume. For example:
34-
35-
group00_sub01_run1.nii 3, 21, 22, 104
36-
group00_sub02_run2.nii 11, 33 91
37-
group00_sub04_run2.nii 101, 102, 132
38-
group00_sub07_run2.nii 0, 1, 2, 166, 167
39-
group00_sub09_run2.nii 3
41+
identified as an outlier. 0 refers to the first volume. For example:
42+
43+
```
44+
data/sub-01/func/sub-01_task-taskzero_run-01_bold.nii.gz, 3, 21, 22, 104
45+
data/sub-01/func/sub-01_task-taskzero_run-02_bold.nii.gz, 11, 33, 91
46+
data/sub-03/func/sub-03_task-taskzero_run-02_bold.nii.gz, 101, 102, 132
47+
data/sub-08/func/sub-08_task-taskzero_run-01_bold.nii.gz, 0, 1, 2, 166, 167
48+
data/sub-09/func/sub-08_task-taskzero_run-01_bold.nii.gz, 3
49+
```

findoutlie/__init__.py

Whitespace-only changes.

findoutlie/outfind.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
""" Module with routines for finding outliers
2+
"""
3+
4+
import os.path as op
5+
from glob import glob
6+
7+
8+
def detect_outliers(fname):
9+
return [42]
10+
11+
12+
def find_outliers(data_directory):
13+
""" Return filenames and outlier indices for images in `data_directory`.
14+
15+
Parameters
16+
----------
17+
data_directory : str
18+
Directory containing containing images.
19+
20+
Returns
21+
-------
22+
outlier_dict : dict
23+
Dictionary with keys being filenames and values being lists of outliers
24+
for filename.
25+
"""
26+
image_fnames = glob(op.join(data_directory, '**', 'sub-*.nii.gz'),
27+
recursive=True)
28+
outlier_dict = {}
29+
for fname in image_fnames:
30+
outliers = detect_outliers(fname)
31+
outlier_dict[fname] = outliers
32+
return outlier_dict

scripts/find_outliers.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,45 @@
55
python3 scripts/find_outliers.py data
66
"""
77

8+
import os.path as op
89
import sys
910

10-
def find_outliers(data_directory):
11-
""" Print filenames and outlier indices for images in `data_directory`.
11+
from argparse import ArgumentParser, RawDescriptionHelpFormatter
1212

13-
Print filenames and detected outlier indices to the terminal.
13+
# Put the findoutlie directory on the Python path.
14+
PACKAGE_DIR = op.join(op.dirname(__file__), '..')
15+
sys.path.append(PACKAGE_DIR)
1416

15-
Parameters
16-
----------
17-
data_directory : str
18-
Directory containing containing images.
17+
from findoutlie import outfind
1918

20-
Returns
21-
-------
22-
None
23-
"""
24-
# Your code here
25-
raise RuntimeError('No code yet')
19+
20+
def print_outliers(data_directory):
21+
outlier_dict = outfind.find_outliers(data_directory)
22+
for fname, outliers in outlier_dict.items():
23+
if len(outliers) == 0:
24+
continue
25+
outlier_strs = []
26+
for out_ind in outliers:
27+
outlier_strs.append(str(out_ind))
28+
print(', '.join([fname] + outlier_strs))
29+
30+
31+
def get_parser():
32+
parser = ArgumentParser(description=__doc__, # Usage from docstring
33+
formatter_class=RawDescriptionHelpFormatter)
34+
parser.add_argument('data_directory',
35+
help='Directory containing data')
36+
return parser
2637

2738

2839
def main():
2940
# This function (main) called when this file run as a script.
3041
#
3142
# Get the data directory from the command line arguments
32-
if len(sys.argv) < 2:
33-
raise RuntimeError("Please give data directory on "
34-
"command line")
35-
data_directory = sys.argv[1]
36-
# Call function to validate data in data directory
37-
find_outliers(data_directory)
43+
parser = get_parser()
44+
args = parser.parse_args()
45+
# Call function to find outliers.
46+
print_outliers(args.data_directory)
3847

3948

4049
if __name__ == '__main__':

0 commit comments

Comments
 (0)