Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_do_dir_path():
"""
Test do_dir_path functionality
"""
base_dir = str(Path(__file__).parent/"examples")
func_path = base_dir + '/002/fmri'
dwi_path = base_dir + '/002/dmri'
in_func = func_path + '/002.nii.gz'
in_dwi = dwi_path + '/std_dmri/iso_eddy_corrected_data_denoised_pre_reor.nii.gz'
in_files = [in_func, in_dwi]
atlases = ['Power', 'Shirer', 'Shen', 'Smith']
for inputs in in_files:
for atlas in atlases:
dir_path = utils.do_dir_path(atlas, inputs)
assert dir_path is not None
def test_normalize(x, thr, cp):
x = thresholding.threshold_proportional(x, 1, copy=True) # remove diagonal
s = thresholding.normalize(x)
assert np.max(s) <= 1 and np.min(s) >= 0
assert np.max(s) == 1 and np.min(s) == round(min(x.flatten())/max(x.flatten()), 1)
def test_threshold_absolute(x, thr, cp):
s = thresholding.threshold_absolute(x, thr, copy=cp)
s_test = [val for arr in s for val in arr if val >= thr] # search for value > thr
assert round(np.sum(s), 10) == round(np.sum(s_test), 10)
def test_density(x, thr):
d_known = thresholding.est_density(thresholding.threshold_absolute(x, thr, copy=True))
x = thresholding.density_thresholding(x, d_known)
d_test = thresholding.est_density(x)
assert np.equal(np.round(d_known, 1), np.round(d_test, 1))
def test_normalize(x, thr, cp):
x = thresholding.threshold_proportional(x, 1, copy=True) # remove diagonal
s = thresholding.normalize(x)
assert np.max(s) <= 1 and np.min(s) >= 0
assert np.max(s) == 1 and np.min(s) == round(min(x.flatten())/max(x.flatten()), 1)
def test_invert(x, thr, cp):
x_cp = x.copy() # invert modifies array in place and need orig to assert.
x_cp = thresholding.threshold_proportional(x_cp, thr, copy=cp)
s = thresholding.invert(x_cp)
x = x.flatten() # flatten arrays to more easily check s > x.
s = s.flatten()
s_gt_x = [inv_val > x[idx] for idx, inv_val in enumerate(s) if inv_val > 0]
assert False not in s_gt_x
def test_thr2prob(x, thr):
s = thresholding.threshold_absolute(thresholding.normalize(x), thr)
s[0][0] = 0.0000001
t = thresholding.thr2prob(s)
assert float(len(t[np.logical_and(t < 0.001, t > 0)])) == float(0.0)
def test_applyxfm():
"""
Test applyxfm functionality
"""
base_dir = str(Path(__file__).parent/"examples")
anat_dir = base_dir + '/003/anat'
## First test: Apply xfm from test_align to orig anat img.
inp = anat_dir + '/sub-003_T1w_brain.nii.gz'
ref = anat_dir + '/MNI152_T1_2mm_brain.nii.gz'
xfm = anat_dir + '/highres2standard.mat'
aligned = anat_dir + '/highres2standard_2.nii.gz'
reg_utils.applyxfm(ref, inp, xfm, aligned, interp='trilinear', dof=6)
# Check test_applyfxm = test_align outputs
out_applyxfm = nib.load(aligned)
out_applyxfm_data = out_applyxfm.get_data()
out_align_file = anat_dir + '/highres2standard.nii.gz'
out_align = nib.load(out_align_file)
out_align_data = out_align.get_data()
check_eq_arrays = np.array_equal(out_applyxfm_data, out_align_data)
assert check_eq_arrays is True
## Second test: Apply xfm to standard space roi (invert xfm first) >> native space roi.
# ref is native space anat image
ref = anat_dir + '/sub-003_T1w.nii.gz'
# input is standard space precuneus mask
inp = anat_dir + '/precuneous_thr_bin.nii.gz'
# xfm is standard2native from convert_xfm -omat standard2highres.mat highres2standard.mat
xfm = anat_dir + '/standard2highres.mat'
def test_thr2prob(x, thr):
s = thresholding.threshold_absolute(thresholding.normalize(x), thr)
s[0][0] = 0.0000001
t = thresholding.thr2prob(s)
assert float(len(t[np.logical_and(t < 0.001, t > 0)])) == float(0.0)
def test_align():
"""
Test align functionality
"""
# Linear registrattion
base_dir = str(Path(__file__).parent/"examples")
anat_dir = base_dir + '/003/anat'
inp = anat_dir + '/sub-003_T1w_brain.nii.gz'
ref = anat_dir + '/MNI152_T1_2mm_brain.nii.gz'
out = anat_dir + '/highres2standard.nii.gz'
xfm_out = anat_dir + '/highres2standard.mat'
reg_utils.align(inp, ref, xfm=xfm_out, out=out, dof=12, searchrad=True, bins=256, interp=None, cost="mutualinfo",
sch=None, wmseg=None, init=None)
highres2standard_linear = nib.load(out)
assert highres2standard_linear is not None