def fractional_anisotropy(d, dperp0, dperp1): """Calculate the fractional anisotropy (FA). Returns: ndarray: the fractional anisotropy for each voxel. """ def compute(d, dperp0, dperp1): d, dperp0, dperp1 = map( lambda el: np.squeeze(el).astype(np.float64), [d, dperp0, dperp1]) return np.sqrt(1 / 2.) * np.sqrt( ((d - dperp0)**2 + (dperp0 - dperp1)**2 + (dperp1 - d)**2) / (d**2 + dperp0**2 + dperp1**2)) if len(d.shape) > 1 and d.shape[1] > 1: fa = np.zeros(d.shape[:2]) for batch_start, batch_end in split_in_batches(d.shape[1], max_batch_size=100): fa[:, batch_start:batch_end] = compute( d[:, batch_start:batch_end], dperp0[:, batch_start:batch_end], dperp1[:, batch_start:batch_end]) return fa else: return compute(d, dperp0, dperp1)
def sample(self, nmr_samples, burnin=0, thinning=1): """Take additional samples from the given likelihood and prior, using this sampler. This method can be called multiple times in which the sample state is stored in between. Args: nmr_samples (int): the number of samples to return burnin (int): the number of samples to discard before returning samples thinning (int): how many sample we wait before storing a new one. This will draw extra samples such that the total number of samples generated is ``nmr_samples * (thinning)`` and the number of samples stored is ``nmr_samples``. If set to one or lower we store every sample after the burn in. Returns: SamplingOutput: the sample output object """ if not thinning or thinning < 1: thinning = 1 if not burnin or burnin < 0: burnin = 0 max_samples_per_batch = max(1000 // thinning, 100) with self._logging(nmr_samples, burnin, thinning): if burnin > 0: for batch_start, batch_end in split_in_batches( burnin, max_samples_per_batch): self._sample(batch_end - batch_start, return_output=False) if nmr_samples > 0: outputs = [] for batch_start, batch_end in split_in_batches( nmr_samples, max_samples_per_batch): outputs.append( self._sample(batch_end - batch_start, thinning=thinning)) return SimpleSampleOutput(*[ np.concatenate([o[ind] for o in outputs], axis=-1) for ind in range(3) ])
def get_division(self, cl_environments, nmr_instances): return list( split_in_batches(nmr_instances, nmr_batches=len(cl_environments)))