def spike_detector(fname, Zalph=5., lZalph=3., histeresis=True, hthres=2., verbose=0): """ fname: name of nifti image, time is last dimension other: see spikes_from_slice_diff function """ img = nib.load(fname) arr = img.get_data() assert len(arr.shape) == 4 # need a 4d nifti assert arr.shape[3] > max(arr.shape[:-1]), \ print("time dim should be greater than others and shape is:", arr.shape) # qc = time_slice_diffs(arr) # tsdiffplot.plot_tsdiffs(qc) smd2 = qc['slice_mean_diff2'] spikes = spikes_from_slice_diff(smd2, Zalph=Zalph, lZalph=lZalph, histeresis=histeresis, hthres=hthres, verbose=verbose) final = final_detection(spikes, verbose=verbose) times_to_correct = np.where(final.sum(axis=1) > 0)[0] slices_to_correct = {} for tim in times_to_correct: slices_to_correct[tim] = np.where(final[tim,:] > 0)[0] if verbose: print("total number of outliers found:", spikes.sum()) print("total number of slices to be corrected:", final.sum()) print("number of time to be corrected:", (final.sum(axis=1) > 0).sum()) return qc, spikes, (times_to_correct, slices_to_correct)
def test_against_matlab_results(): fimg = load_image(funcfile) results = tsd.time_slice_diffs(fimg) # struct as record only to avoid deprecation warning tsd_results = sio.loadmat(pjoin(TEST_DATA_PATH, 'tsdiff_results.mat'), struct_as_record=True, squeeze_me=True) yield assert_array_almost_equal( results['volume_means'], tsd_results['g']) yield assert_array_almost_equal( results['volume_mean_diff2'], tsd_results['imgdiff']) yield assert_array_almost_equal( results['slice_mean_diff2'], tsd_results['slicediff']) # next tests are from saved, reloaded volumes at 16 bit integer # precision, so are not exact, but very close, given that the mean # of this array is around 3200 yield assert_array_almost_equal( results['diff2_mean_vol'], tsd_results['diff2_mean_vol'], decimal=1) yield assert_array_almost_equal( results['slice_diff2_max_vol'], tsd_results['slice_diff2_max_vol'], decimal=1)
def test_time_slice_diffs(): n_tps = 10 n_slices = 4 slice_shape = (2,3) slice_size = np.prod(slice_shape) vol_shape = slice_shape + (n_slices,) vol_size = np.prod(vol_shape) ts = np.random.normal(size=vol_shape + (n_tps,)) * 100 + 10 expected = {} expected['volume_means'] = ts.reshape((vol_size, -1)).mean(0) # difference over time ^2 diffs2 = np.diff(ts, axis=-1)**2 expected['volume_mean_diff2'] = np.mean( diffs2.reshape((vol_size, -1)), 0) expected['slice_mean_diff2'] = np.zeros((n_tps-1, n_slices)) for s in range(n_slices): v = diffs2[:,:,s,:].reshape((slice_size, -1)) expected['slice_mean_diff2'][:,s] = np.mean(v, 0) expected['diff2_mean_vol'] = np.mean(diffs2, -1) max_diff_is = np.argmax(expected['slice_mean_diff2'], 0) sdmv = np.empty(vol_shape) for si, dti in enumerate(max_diff_is): sdmv[:,:,si] = diffs2[:,:,si,dti] expected['slice_diff2_max_vol'] = sdmv results = tsd.time_slice_diffs(ts) for key in expected: yield assert_array_almost_equal(results[key], expected[key]) # tranposes, reset axes, get the same result results = tsd.time_slice_diffs(ts.T, 0, 1) results['diff2_mean_vol'] = results['diff2_mean_vol'].T results['slice_diff2_max_vol'] = results['slice_diff2_max_vol'].T for key in expected: yield assert_array_almost_equal(results[key], expected[key]) ts_t = ts.transpose((1, 3, 0, 2)) results = tsd.time_slice_diffs(ts_t, 1, -1) results['diff2_mean_vol'] = results['diff2_mean_vol'].transpose( ((1,0,2))) results['slice_diff2_max_vol'] = results['slice_diff2_max_vol'].transpose( ((1,0,2))) for key in expected: yield assert_array_almost_equal(results[key], expected[key])
def one_detection(sh,sli,tim): arr = make_data(sh) arr = make_bad_slices(arr, sli, tim) qc = time_slice_diffs(arr) smd2 = qc['slice_mean_diff2'] print("\n smd2 ine one detec\n", smd2) spikes = fqc.spikes_from_slice_diff(smd2, Zalph=5., lZalph=3., histeresis=True, hthres=.2, verbose=1) print("\n spikes ine one detec\n", spikes) final = fqc.final_detection(spikes, verbose=1) print("\n final ine one detec\n", final) times_to_correct = np.where(final.sum(axis=1) > 0)[0] print("times_to_correct: ",times_to_correct) slices_to_correct = {} for ti in times_to_correct: slices_to_correct[ti] = np.where(final[ti,:] > 0)[0] return times_to_correct, slices_to_correct
def one_detection(sh, sli, tim): arr = make_data(sh) arr = make_bad_slices(arr, sli, tim) qc = time_slice_diffs(arr) smd2 = qc['slice_mean_diff2'] print("\n smd2 ine one detec\n", smd2) spikes = fqc.spikes_from_slice_diff(smd2, Zalph=5., lZalph=3., histeresis=True, hthres=.2, verbose=1) print("\n spikes ine one detec\n", spikes) final = fqc.final_detection(spikes, verbose=1) print("\n final ine one detec\n", final) times_to_correct = np.where(final.sum(axis=1) > 0)[0] print("times_to_correct: ", times_to_correct) slices_to_correct = {} for ti in times_to_correct: slices_to_correct[ti] = np.where(final[ti, :] > 0)[0] return times_to_correct, slices_to_correct
def spike_detector(fname, Zalph=5., lZalph=3., histeresis=True, hthres=2., verbose=0): """ fname: name of nifti image, time is last dimension other: see spikes_from_slice_diff function """ img = nib.load(fname) arr = img.get_data() assert len(arr.shape) == 4 # need a 4d nifti assert arr.shape[3] > max(arr.shape[:-1]), \ print("time dim should be greater than others and shape is:", arr.shape) # qc = time_slice_diffs(arr) # tsdiffplot.plot_tsdiffs(qc) smd2 = qc['slice_mean_diff2'] spikes = spikes_from_slice_diff(smd2, Zalph=Zalph, lZalph=lZalph, histeresis=histeresis, hthres=hthres, verbose=verbose) final = final_detection(spikes, verbose=verbose) times_to_correct = np.where(final.sum(axis=1) > 0)[0] slices_to_correct = {} for tim in times_to_correct: slices_to_correct[tim] = np.where(final[tim, :] > 0)[0] if verbose: print("total number of outliers found:", spikes.sum()) print("total number of slices to be corrected:", final.sum()) print("number of time to be corrected:", (final.sum(axis=1) > 0).sum()) return qc, spikes, (times_to_correct, slices_to_correct)