Skip to content

Commit

Permalink
Revert "Merging Diagnostics into Tests"
Browse files Browse the repository at this point in the history
  • Loading branch information
soazig committed Dec 12, 2015
1 parent f7f8bab commit 2fbd534
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 132 deletions.
58 changes: 34 additions & 24 deletions code/utils/functions/diagnostics.py → code/utils/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

import numpy as np

import pdb

def vol_std(data):
""" Return standard deviation across voxels for 4D array `data`
Expand All @@ -23,12 +23,10 @@ def vol_std(data):
One dimensonal array where ``std_values[i]`` gives the standard
deviation of all voxels contained in ``data[..., i]``.
"""
T = data.shape[-1]
data_2d = np.reshape(data, (-1, T))
return np.std(data_2d, axis=0)

std_values = [np.std(np.ravel(data[...,i])) for i in range(0, np.shape(data)[3])]
return np.array(std_values)

def iqr_outliers(arr_1d, iqr_scale=1.5):
def iqr_outliers(arr_1d, iqr_scale=1):
""" Return indices of outliers identified by interquartile range
Parameters
Expand All @@ -51,13 +49,16 @@ def iqr_outliers(arr_1d, iqr_scale=1.5):
"""
# Hint : np.lookfor('centile')
# Hint : np.lookfor('nonzero')
pct_25, pct_75 = np.percentile(arr_1d, [25, 75])
iqr = pct_75 - pct_25
lo_thresh = pct_25 - iqr * iqr_scale
hi_thresh = pct_75 + iqr * iqr_scale
is_outlier = (arr_1d < lo_thresh) | (arr_1d > hi_thresh)
return np.nonzero(is_outlier)[0], (lo_thresh, hi_thresh)

sevenfifth = np.percentile(arr_1d,75)
twenfifth = np.percentile(arr_1d,25)
iqr = sevenfifth-twenfifth
low = twenfifth-iqr_scale*iqr
hi = iqr_scale*iqr+sevenfifth
outliers1 = np.array(np.nonzero(arr_1d > hi))[0]
outliers2 = np.array(np.nonzero((arr_1d < low)))[0]
outliers = np.hstack((outliers1,outliers2))
outliers.sort()
return outliers,(low, hi)

def vol_rms_diff(arr_4d):
""" Return root mean square of differences between sequential volumes
Expand All @@ -75,11 +76,12 @@ def vol_rms_diff(arr_4d):
the mean (across voxels) of the squared difference between volume i and
volume i + 1.
"""
T = arr_4d.shape[-1]
diff_data = np.diff(arr_4d, axis=-1)
diff_data_2d = np.reshape(diff_data, (-1, T-1))
return np.sqrt(np.mean(diff_data_2d ** 2, axis=0))


rms_values=[]
for i in range(arr_4d.shape[-1]-1):
diff_vol = arr_4d[...,i+1] - arr_4d[...,i]
rms_values.append(np.sqrt(np.mean(diff_vol ** 2)))
return np.array(rms_values)

def extend_diff_outliers(diff_indices):
""" Extend difference-based outlier indices `diff_indices` by pairing
Expand All @@ -93,14 +95,22 @@ def extend_diff_outliers(diff_indices):
Returns
-------
extended_indices : array
extended_indices :
Array where each index ``j`` in `diff_indices has been replaced by two
indices, ``j`` and ``j+1``, unless ``j+1`` is present in
``diff_indices``. For example, if the input was ``[3, 7, 8, 12, 20]``,
``[3, 4, 7, 8, 9, 12, 13, 20, 21]``.
"""
# Make two columns with the second being the first plus 1
ext_outliers = np.column_stack((diff_indices, diff_indices + 1))
# Use numpy reshape ordering to get the values from the first row, then the
# second row etc...
return np.unique(ext_outliers.ravel())
i=0
while (i < len(diff_indices)):
if i is (len(diff_indices)-1):
diff_indices = np.insert(diff_indices,i+1,diff_indices[i]+1)
break
if diff_indices[i+1] == (diff_indices[i]+1):
i=i+1
continue
diff_indices = np.insert(diff_indices,i+1,diff_indices[i]+1)
i=i+2
return diff_indices


108 changes: 0 additions & 108 deletions code/utils/tests/test_diagnostics.py

This file was deleted.

0 comments on commit 2fbd534

Please sign in to comment.