class DKIModel: """A wrapper of :obj:`dipy.reconst.dki.DiffusionKurtosisModel.""" __slots__ = ("_model", "_S0", "_mask") def __init__(self, gtab, S0=None, mask=None, **kwargs): """Instantiate the wrapped tensor model.""" from dipy.reconst.dki import DiffusionKurtosisModel self._S0 = None if S0 is not None: self._S0 = np.clip( S0.astype("float32") / S0.max(), a_min=1e-5, a_max=1.0, ) self._mask = mask if mask is None and S0 is not None: self._mask = self._S0 > np.percentile(self._S0, 35) if self._mask is not None: self._S0 = self._S0[self._mask.astype(bool)] kwargs = { k: v for k, v in kwargs.items() if k in ( "min_signal", "return_S0_hat", "fit_method", "weighting", "sigma", "jac", ) } self._model = DiffusionKurtosisModel(gtab, **kwargs) def fit(self, data, **kwargs): """Clean-up permitted args and kwargs, and call model's fit.""" self._model = self._model.fit(data[self._mask, ...]) def predict(self, gradient, **kwargs): """Propagate model parameters and call predict.""" predicted = np.squeeze( self._model.predict( _rasb2dipy(gradient), S0=self._S0, ) ) if predicted.ndim == 3: return predicted retval = np.zeros_like(self._mask, dtype="float32") retval[self._mask, ...] = predicted return retval
def tensorFit(self, m='dti', bv=None, removea0=0, mask=True): if bv != None: self.backups = [self.pdata, self.avbvals, self.nbvals] self.pdata, self.avbvals = selectBvals(self, bv) self.avbvals = np.array(self.avbvals) self.nbvals = len(bv) if removea0 != 0: print "Removing %s A0 images." % removea0 self.pdata = self.pdata[:, :, :, removea0:] self.nA0 -= removea0 self.avbvals = self.avbvals[removea0:] self.bvals = self.bvals[removea0:] # thresh=np.mean(self.pdata[:5,:5,:,0]) if mask: print "Generating brain mask: Erosion>Dilation>MedianOtsu>LargestComponent" self.mask = ndi.binary_dilation( ndi.binary_erosion(ndi.binary_dilation(brain_mask(self.pdata)))) self.mask = largest_component(self.mask) else: print "Not using brain mask." self.mask = np.zeros(self.shape[:-1]) + 1 bvalmat = np.array(self.avbvals) self.bvecmat = construct_bvecs(self) if len(self.bvecmat) != len(self.avbvals): print "Error. Cannot process this image." print bvalmat.shape self.gtab = gradient_table( bvalmat, self.bvecmat ) #creates a gradient table with b-vals and diffusion dirs for processing if m == 'dti': from dipy.reconst.dti import TensorModel starttime = datetime.now() print "Fitting tensor model." ten = TensorModel(self.gtab) self.tenfit = ten.fit(self.pdata, self.mask) time = datetime.now() - starttime tenfile = self.name + "_tenfit.pickle" print "Tensor fit completed in %r seconds. Saving file %s." % ( time.seconds, tenfile) # with open(tenfile, 'w') as f: TOO INEFFICIENT # pickle.dump([self, self.tenfit], f) from dipy.reconst.dti import color_fa self.cfa = color_fa(self.tenfit.fa, self.tenfit.evecs) if m == 'RESTORE': from dipy.reconst.dti import TensorModel mean_std = 2.5 * np.mean( np.std(self.pdata[..., self.gtab.b0s_mask], -1)) #conservative thresh starttime = datetime.now() print "Fitting tensor model using the RESTORE method. Sigma=", mean_std ten = TensorModel(self.gtab, fit_method="RESTORE", sigma=mean_std) self.tenfit = ten.fit(self.pdata, self.mask) time = datetime.now() - starttime tenfile = self.name + "_tenfit.pickle" print "Tensor fit completed in %r seconds. Saving file %s." % ( time.seconds, tenfile) # with open(tenfile, 'w') as f: TOO INEFFICIENT # pickle.dump([self, self.tenfit], f) from dipy.reconst.dti import color_fa self.cfa = color_fa(self.tenfit.fa, self.tenfit.evecs) elif m == 'dki': from dipy.reconst.dki import DiffusionKurtosisModel starttime = datetime.now() print "Fitting kurtosis tensor." kurt = DiffusionKurtosisModel(self.gtab) self.tenfit = kurt.fit(self.pdata) time = datetime.now() - starttime print "Kurtosis fit completed in %r seconds." % time.seconds if bv != None: self.pdata = self.backups[0] self.avbvals = self.backups[1] self.nbvals = self.backups[2]