def test_response_from_mask(): fdata, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) data = nib.load(fdata).get_data() gtab = gradient_table(bvals, bvecs) ten = TensorModel(gtab) tenfit = ten.fit(data) FA = fractional_anisotropy(tenfit.evals) FA[np.isnan(FA)] = 0 radius = 3 for fa_thr in np.arange(0, 1, 0.1): response_auto, ratio_auto, nvoxels = auto_response(gtab, data, roi_center=None, roi_radius=radius, fa_thr=fa_thr, return_number_of_voxels=True) ci, cj, ck = np.array(data.shape[:3]) / 2 mask = np.zeros(data.shape[:3]) mask[ci - radius: ci + radius, cj - radius: cj + radius, ck - radius: ck + radius] = 1 mask[FA <= fa_thr] = 0 response_mask, ratio_mask = response_from_mask(gtab, data, mask) assert_equal(int(np.sum(mask)), nvoxels) assert_array_almost_equal(response_mask[0], response_auto[0]) assert_almost_equal(response_mask[1], response_auto[1]) assert_almost_equal(ratio_mask, ratio_auto)
def test_response_from_mask(): fdata, fbvals, fbvecs = get_data('small_64D') bvals = np.load(fbvals) bvecs = np.load(fbvecs) data = nib.load(fdata).get_data() gtab = gradient_table(bvals, bvecs) ten = TensorModel(gtab) tenfit = ten.fit(data) FA = fractional_anisotropy(tenfit.evals) FA[np.isnan(FA)] = 0 radius = 3 for fa_thr in np.arange(0, 1, 0.1): response_auto, ratio_auto, nvoxels = auto_response( gtab, data, roi_center=None, roi_radius=radius, fa_thr=fa_thr, return_number_of_voxels=True) ci, cj, ck = np.array(data.shape[:3]) // 2 mask = np.zeros(data.shape[:3]) mask[ci - radius:ci + radius, cj - radius:cj + radius, ck - radius:ck + radius] = 1 mask[FA <= fa_thr] = 0 response_mask, ratio_mask = response_from_mask(gtab, data, mask) assert_equal(int(np.sum(mask)), nvoxels) assert_array_almost_equal(response_mask[0], response_auto[0]) assert_almost_equal(response_mask[1], response_auto[1]) assert_almost_equal(ratio_mask, ratio_auto)
def execution(self, context): #if an existing tensor has already been fitted dont compute a new one . if self.tensor_coefficients is not None and self.tensor_model is not None: context.write('Fitted Tensor already exists ! Let s use it !') tensor_coeff_vol = aims.read(self.tensor_coefficients.fullPath()) tensor_coeff = np.asarray(tensor_coeff_vol) hdr = tensor_coeff_vol.header() tensor_model = load(self.tensor_model.fullPath()) tenfit = TensorFit(tensor_model, tensor_coeff) if self.mask is not None: mask_vol = aims.read(self.mask.fullPath()) mask = vol_to_array(mask_vol) mask = array_to_mask(mask) else: context.write( 'No mask provided ! Estimating impulsionnal response from the whole volume or brain is not really accurate ! A default mask based on Fractionnal Anisotropy is computed. ' ) fa = tenfit.fa # just to avoid nan is case of wrong fitting fa = np.clip(fa, 0, 1) #high FA vale is associated with single fiber direction voxel mask = fa > self.fa_threshold mask = mask.astype(bool) #code extracted from dipy response_from_mask function indices = np.where(mask > 0) sub_tenfit = tenfit[indices] lambdas = sub_tenfit.evals[:, :2] gtab = sub_tenfit.model.gtab vol = aims.read(self.diffusion_data.fullPath()) data = np.asarray(vol) S0s = data[indices][:, np.nonzero(gtab.b0s_mask)[0]] response, ratio = _get_response(S0s, lambdas) else: context.write('No Tensor Fitted Yet! Compute a new one') gtab = load(self.gradient_table.fullPath()) if is_multi_shell(gtab): context.warning( "The DWI scheme for this data is multishell: bvalues", shells(gtab), ". CSD implementation used in Diffuse currently only handle single shell DWI scheme. By default the higher shell bval", max_shell(gtab), " is selected") context.warning( "Even if only the outer shell is use for deconvolution, the following estimation method will use the full DWI scheme for response estimation. It might be inaccurate if the deconvolved shell bvalue is too high (b5000)" ) vol = aims.read(self.diffusion_data.fullPath()) data = np.asarray(vol) if self.mask is not None: mask_vol = aims.read(self.mask.fullPath()) mask = vol_to_array(mask_vol) mask = array_to_mask(mask) response, ratio = response_from_mask(gtab, data, mask) else: context.warning( "No mask provided ! Compute a high-FA based mask: FA higher than " + str(self.fa_threshold) + " are considered as single direction voxels") #default tensor model --> we dont store it for now tensor = TensorModel(gtab) #whole volume fit tenfit = tensor.fit(data) fa = tenfit.fa # just to avoid nan is case of wrong fitting fa = np.clip(fa, 0, 1) # high FA vale is associated with single fiber direction voxel mask = fa > self.fa_threshold mask = mask.astype(bool) indices = np.where(mask) # code extracted from dipy response_from_mask function sub_tenfit = tenfit[indices] lambdas = sub_tenfit.evals[:, :2] gtab = sub_tenfit.model.gtab vol = aims.read(self.diffusion_data.fullPath()) data = np.asarray(vol) S0s = data[indices][:, np.nonzero(gtab.b0s_mask)[0]] response, ratio = _get_response(S0s, lambdas) #store the response dump(response, self.response.fullPath())
print response1 # In[ ]: #recursive response function from dipy.reconst.csdeconv import recursive_response #logic1 = np.logical_and(labels>117, labels<148) #logic2 = np.logical_and(labels>283, labels<314) #logic = np.logical_or(logic1, logic2) #logic_ = np.logical_or(labels==150, labels==316) #wm = np.where(logic, 1, np.where(logic_, 1, 0)) #wm = np.where(logic, 1,0) response2 = recursive_response(gtab, data, mask=labels==118, sh_order=8, peak_thr=0.01, init_fa=0.08, init_trace=0.0021, iter=8, convergence=0.001, parallel=True) print response2 # In[ ]: #response function from mask from dipy.reconst.csdeconv import response_from_mask response3, ratio3 = response_from_mask(gtab, data, mask=labels==118) print ratio3 print response3
def test_response_from_mask_deprecated(): with warnings.catch_warnings(record=True) as cw: warnings.simplefilter("always", DeprecationWarning) gtab, data, mask, _, _ = get_test_data() _ = response_from_mask(gtab, data, mask) npt.assert_(issubclass(cw[0].category, DeprecationWarning))