Skip to content

Commit 28ec9fb

Browse files
authored
Merge pull request #19 from Nauman702/outlier-detection
iqr-detector and test fixed
2 parents fdaf6d5 + 4e80a7f commit 28ec9fb

16 files changed

+157
-92
lines changed

data/group-00/hash_list.txt

Lines changed: 0 additions & 40 deletions
This file was deleted.

data/group-01/hash_list.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
bca9dec7410a0e8923fa3b65fee6173a9e4856a5 group-01/sub-01/func/sub-01_task-taskzero_run-01_bold.nii.gz
2+
24d97e3cb1a1678feee5a4753ad02b488323f7a5 group-01/sub-01/func/sub-01_task-taskzero_run-01_events.tsv
3+
025d9abc3a167fa754d659ff3a422eea8cdf7749 group-01/sub-01/func/sub-01_task-taskzero_run-02_bold.nii.gz
4+
c3ff62dfd461bfe5d49fa45b2f2ea0321aadb766 group-01/sub-01/func/sub-01_task-taskzero_run-02_events.tsv
5+
9bb1455c39865ead33d7dc420a8b40d8f243275e group-01/sub-02/func/sub-02_task-taskzero_run-01_bold.nii.gz
6+
2fd7b7003f64650de027223d1c99d4caa3641a6d group-01/sub-02/func/sub-02_task-taskzero_run-01_events.tsv
7+
6c48dcf16fa8d7267b9fa5d2d4b88c1bb919c08b group-01/sub-02/func/sub-02_task-taskzero_run-02_bold.nii.gz
8+
810dc40e8611b018819c9490de73ec925e08ab14 group-01/sub-02/func/sub-02_task-taskzero_run-02_events.tsv
9+
49e269f1607eb8a221272bd76964ec88561d7ca3 group-01/sub-03/func/sub-03_task-taskzero_run-01_bold.nii.gz
10+
6af496f45d9443cccdb8a79141ba1ebe8afb3847 group-01/sub-03/func/sub-03_task-taskzero_run-01_events.tsv
11+
67d90a6542ca279dd7c99ec230dfbfca851d6d02 group-01/sub-03/func/sub-03_task-taskzero_run-02_bold.nii.gz
12+
e945186474ae35e5df51c026947bb06bf67ddcee group-01/sub-03/func/sub-03_task-taskzero_run-02_events.tsv
13+
51a92f264ad1c5b420db8c9a8a646511f3e87fc1 group-01/sub-04/func/sub-04_task-taskzero_run-01_bold.nii.gz
14+
015aeff46f289c2832a92c8581cfb935e6f34ee1 group-01/sub-04/func/sub-04_task-taskzero_run-01_events.tsv
15+
b76af6dcd19d0ca957efd3334210c531562b8a6a group-01/sub-04/func/sub-04_task-taskzero_run-02_bold.nii.gz
16+
50d292c3fbc6ecfb909f049dbad38e0a36e02d83 group-01/sub-04/func/sub-04_task-taskzero_run-02_events.tsv
17+
fa55e7da82dd191df064ed540cf4553d59f9dc7b group-01/sub-05/func/sub-05_task-taskzero_run-01_bold.nii.gz
18+
701295cbe1fa6267ed4ca58c8619c42f622338e7 group-01/sub-05/func/sub-05_task-taskzero_run-01_events.tsv
19+
f7135ef1fdf5c1b62c6767ebdee3fa998069166a group-01/sub-05/func/sub-05_task-taskzero_run-02_bold.nii.gz
20+
6e0f56978dd658e625566b914231076a0a678e40 group-01/sub-05/func/sub-05_task-taskzero_run-02_events.tsv
21+
b919151922a83363efde5782213ee8de8d9fb643 group-01/sub-06/func/sub-06_task-taskzero_run-01_bold.nii.gz
22+
88410b71fca75f657a2f2f0a8eb400797754525a group-01/sub-06/func/sub-06_task-taskzero_run-01_events.tsv
23+
b823efec8389bc3c2e4f6aada4fbf526b58dd01b group-01/sub-06/func/sub-06_task-taskzero_run-02_bold.nii.gz
24+
4e6946049fb46e4f6b7e7883d3b55c1b0061f997 group-01/sub-06/func/sub-06_task-taskzero_run-02_events.tsv
25+
90f9725a48b73c917ac0a282da1b060dbe872d46 group-01/sub-07/func/sub-07_task-taskzero_run-01_bold.nii.gz
26+
c06930a4a1663198d3d949fbf8c5b0ac94e46507 group-01/sub-07/func/sub-07_task-taskzero_run-01_events.tsv
27+
0860567f1017787fd36ddeae06dc5ee898750004 group-01/sub-07/func/sub-07_task-taskzero_run-02_bold.nii.gz
28+
c899f8a386e746f0660516bb1a9066a8541bac83 group-01/sub-07/func/sub-07_task-taskzero_run-02_events.tsv
29+
fadece5df3ce9202e28dd4d3141f29537ea33b6c group-01/sub-08/func/sub-08_task-taskzero_run-01_bold.nii.gz
30+
54c2c9d7c0c745091e5a1ac0552a9419638a9974 group-01/sub-08/func/sub-08_task-taskzero_run-01_events.tsv
31+
6ceea843d281c9c81ac5f090ffeeb3277adb7b26 group-01/sub-08/func/sub-08_task-taskzero_run-02_bold.nii.gz
32+
c97ab4258b82ff4ada2f69365f69bcb676e6f49f group-01/sub-08/func/sub-08_task-taskzero_run-02_events.tsv
33+
79cdcbf53b1c95b1361c7ec3d7803c5e4f36c2c6 group-01/sub-09/func/sub-09_task-taskzero_run-01_bold.nii.gz
34+
568b4d2ded5f573402f7ec23a35c5a640c1cad5c group-01/sub-09/func/sub-09_task-taskzero_run-01_events.tsv
35+
5127c4267c5a5c46588027d4cf6edde2d8ea758c group-01/sub-09/func/sub-09_task-taskzero_run-02_bold.nii.gz
36+
5d92eda8fd7c0c7fdff06f780e7b0353fefc6d03 group-01/sub-09/func/sub-09_task-taskzero_run-02_events.tsv
37+
fcfc83c1d2e430f165166156c3566bf9fb3dd6ef group-01/sub-10/func/sub-10_task-taskzero_run-01_bold.nii.gz
38+
5ed9a6ec53a4ba45e1429192996db0dc55e4675a group-01/sub-10/func/sub-10_task-taskzero_run-01_events.tsv
39+
89e6a99f02a789b8224342d909280a38930fdb7c group-01/sub-10/func/sub-10_task-taskzero_run-02_bold.nii.gz
40+
b149f2bc4f2d57ecab62a328893daf77f054ff5a group-01/sub-10/func/sub-10_task-taskzero_run-02_events.tsv

findoutlie/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
""" Init for findoutlie module
22
"""
33

4-
__version__ = '0.1a0'
4+
__version__ = "0.1a0"

findoutlie/detectors.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
# Any imports you need
1414
# +++your code here+++
15+
import numpy as np
1516

1617

1718
def iqr_detector(measures, iqr_proportion=1.5):
@@ -51,3 +52,22 @@ def iqr_detector(measures, iqr_proportion=1.5):
5152
# * You'll likely need np.logical_or
5253
# https://textbook.nipraxis.org/numpy_logical.html
5354
# +++your code here+++
55+
56+
Q1 = np.percentile(measures, 25, interpolation="midpoint")
57+
Q2 = np.percentile(measures, 50, interpolation="midpoint")
58+
Q3 = np.percentile(measures, 75, interpolation="midpoint")
59+
IQR = Q3 - Q1
60+
61+
outlier = []
62+
63+
for i in range(len(measures)):
64+
if (measures[i] > (Q3 + IQR * iqr_proportion)) | (
65+
measures[i] < (Q1 - IQR * iqr_proportion)
66+
):
67+
68+
outlier.append(True)
69+
else:
70+
outlier.append(False)
71+
72+
#print(outlier)
73+
return np.array(outlier)

findoutlie/metrics.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,19 @@ def dvars(img):
3030
# You may be be able to solve this in four lines, without a loop.
3131
# But solve it any way you can.
3232
# This is a placeholder, replace it to write your solution.
33-
data=img.get_fdata()
34-
vol_shape=data.shape[:-1]
35-
n_voxels=np.prod(vol_shape)
33+
data = img.get_fdata()
34+
vol_shape = data.shape[:-1]
35+
n_voxels = np.prod(vol_shape)
3636

3737
voxel_by_time = np.reshape(data, (n_voxels, data.shape[-1]))
3838

39-
vol_diff= voxel_by_time[...,1:]-voxel_by_time[...,:-1] # 2D array
40-
#print(vol_diff.shape())
41-
print(vol_diff)
42-
#vol_diff_1D=vol_diff.flatten()
39+
vol_diff = voxel_by_time[..., 1:] - voxel_by_time[..., :-1] # 2D array
40+
# print(vol_diff.shape())
41+
# print(vol_diff)
42+
# vol_diff_1D=vol_diff.flatten()
4343
dvar_val = np.sqrt(np.mean(vol_diff ** 2, axis=0))
44-
#print(dvar_val.shape())
45-
print(dvar_val)
44+
# print(dvar_val.shape())
45+
# print(dvar_val)
4646
return dvar_val
4747

48-
49-
50-
raise NotImplementedError('Code up this function')
48+
raise NotImplementedError("Code up this function")

findoutlie/outfind.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def find_outliers(data_directory):
4646
Dictionary with keys being filenames and values being lists of outliers
4747
for filename.
4848
"""
49-
image_fnames = Path(data_directory).glob('**/sub-*.nii.gz')
49+
image_fnames = Path(data_directory).glob("**/sub-*.nii.gz")
5050
outlier_dict = {}
5151
for fname in image_fnames:
5252
outliers = detect_outliers(fname)

findoutlie/tests/test_detectors.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
# Hint: see the solutions if you are stuck.
2121
# +++your code here+++
2222

23+
import sys
24+
#export PYTHONPATH=$PYTHONPATH:C:/Users/nauma/Documents/nipraxis-work/diagnostics-NME/findoutlie
25+
sys.path.append('C:/Users/nauma/Documents/nipraxis-work/diagnostics-NME/findoutlie')
2326
import numpy as np
2427

2528
# This import needs the directory containing the findoutlie directory
@@ -30,16 +33,33 @@
3033
def test_iqr_detector():
3134
# From: http://www.purplemath.com/modules/boxwhisk3.htm
3235
example_values = np.array(
33-
[10.2, 14.1, 14.4, 14.4, 14.4, 14.5, 14.5, 14.6, 14.7, 14.7, 14.7,
34-
14.9, 15.1, 15.9, 16.4])
36+
[
37+
10.2,
38+
14.1,
39+
14.4,
40+
14.4,
41+
14.4,
42+
14.5,
43+
14.5,
44+
14.6,
45+
14.7,
46+
14.7,
47+
14.7,
48+
14.9,
49+
15.1,
50+
15.9,
51+
16.4,
52+
]
53+
)
3554
is_outlier = iqr_detector(example_values, 1.5)
3655
assert np.all(example_values[is_outlier] == [10.2, 15.9, 16.4])
3756
# Test not-default value for outlier proportion
57+
#print(is_outlier)
3858
is_outlier = iqr_detector(example_values, 0.5)
3959
assert np.all(example_values[is_outlier] == [10.2, 14.1, 15.1, 15.9, 16.4])
4060

4161

42-
if __name__ == '__main__':
62+
if __name__ == "__main__":
4363
# File being executed as a script
4464
test_iqr_detector()
45-
print('Tests passed')
65+
print("Tests passed")

findoutlie/tests/test_dvars.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from findoutlie.metrics import dvars
1515

1616

17-
TEST_FNAME = npx.fetch_file('ds114_sub009_t2r1.nii')
17+
TEST_FNAME = npx.fetch_file("ds114_sub009_t2r1.nii")
1818

1919

2020
def test_dvars():

findoutlie/tests/test_spm_funcs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import sys
1414

1515
MY_DIR = Path(__file__).parent
16-
EXAMPLE_FILENAME = 'ds107_sub012_t1r2_small.nii'
16+
EXAMPLE_FILENAME = "ds107_sub012_t1r2_small.nii"
1717

1818
# Here you should add the directory containing the findoutlie
1919
# directory to the Python path.
@@ -33,9 +33,9 @@
3333
def test_spm_globals():
3434
# Test get_spm_globals and spm_global functions
3535
example_path = MY_DIR / EXAMPLE_FILENAME
36-
expected_values = np.loadtxt(MY_DIR / 'global_signals.txt')
36+
expected_values = np.loadtxt(MY_DIR / "global_signals.txt")
3737
glob_vals = get_spm_globals(example_path)
38-
assert glob_vals is not None, 'Did you forget to return the values?'
38+
assert glob_vals is not None, "Did you forget to return the values?"
3939
assert np.allclose(glob_vals, expected_values, rtol=1e-4)
4040
img = nib.load(example_path)
4141
data = img.get_fdata()
@@ -46,7 +46,7 @@ def test_spm_globals():
4646
assert np.allclose(globals, expected_values, rtol=1e-4)
4747

4848

49-
if __name__ == '__main__':
49+
if __name__ == "__main__":
5050
# File being executed as a script
5151
test_spm_globals()
52-
print('Tests passed')
52+
print("Tests passed")

scripts/find_outliers.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from argparse import ArgumentParser, RawDescriptionHelpFormatter
1212

1313
# Put the findoutlie directory on the Python path.
14-
PACKAGE_DIR = Path(__file__).parent / '..'
14+
PACKAGE_DIR = Path(__file__).parent / ".."
1515
sys.path.append(str(PACKAGE_DIR))
1616

1717
from findoutlie import outfind
@@ -25,14 +25,15 @@ def print_outliers(data_directory):
2525
outlier_strs = []
2626
for out_ind in outliers:
2727
outlier_strs.append(str(out_ind))
28-
print(', '.join([str(fname)] + outlier_strs))
28+
print(", ".join([str(fname)] + outlier_strs))
2929

3030

3131
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')
32+
parser = ArgumentParser(
33+
description=__doc__, # Usage from docstring
34+
formatter_class=RawDescriptionHelpFormatter,
35+
)
36+
parser.add_argument("data_directory", help="Directory containing data")
3637
return parser
3738

3839

@@ -46,6 +47,6 @@ def main():
4647
print_outliers(args.data_directory)
4748

4849

49-
if __name__ == '__main__':
50+
if __name__ == "__main__":
5051
# Python is running this file as a script, not importing it.
5152
main()

0 commit comments

Comments
 (0)