def _sort_eigensystem(parameters_dict): """Sort the eigensystem of the Tensor parameterized by eigen values and vectors. Args: parameters_dict (dict): the results from optimization. This expects each value to be a (n, ...) array with for each voxel either a scalar or a vector. Returns: tuple: the sorted eigenvalues, """ eigenvectors = np.stack(tensor_spherical_to_cartesian(np.squeeze(parameters_dict['theta']), np.squeeze(parameters_dict['phi']), np.squeeze(parameters_dict['psi'])), axis=0) eigenvalues = np.atleast_2d(np.squeeze(np.dstack([parameters_dict['d'], parameters_dict['dperp0'], parameters_dict['dperp1']]))) ranking = np.atleast_2d(np.squeeze(np.argsort(eigenvalues, axis=1, kind='mergesort')[:, ::-1])) voxels_range = np.arange(ranking.shape[0]) sorted_eigenvalues = np.concatenate([eigenvalues[voxels_range, ranking[:, ind], None] for ind in range(ranking.shape[1])], axis=1) sorted_eigenvectors = np.stack([eigenvectors[ranking[:, ind], voxels_range, :] for ind in range(ranking.shape[1])]) return sorted_eigenvalues, sorted_eigenvectors, ranking
def extra_optimization_maps(results, input_data=None): """Return some interesting measures like FA, MD, RD and AD. This function is meant to be used as a post processing routine in Tensor-like compartment models. Args: results (dict): Dictionary containing at least theta, phi, psi, d, dperp0 and dperp1 We will use this to generate some standard measures from the diffusion Tensor. input_data (mdt.utils.MRIInputData): optionally, the input data used during the model fitting. Returns: dict: as keys typical elements like 'FA and 'MD' as interesting output and as per values the maps. These maps are per voxel, and optionally per instance per voxel """ output = { 'FA': DTIMeasures.fractional_anisotropy(results['d'], results['dperp0'], results['dperp1']), 'MD': (results['d'] + results['dperp0'] + results['dperp1']) / 3., 'AD': results['d'], 'RD': (results['dperp0'] + results['dperp1']) / 2.0, } if all('{}.std'.format(el) in results for el in ['d', 'dperp0', 'dperp1']): output.update({ 'FA.std': DTIMeasures.fractional_anisotropy_std(results['d'], results['dperp0'], results['dperp1'], results['d.std'], results['dperp0.std'], results['dperp1.std'], covariances=results.get( 'covariances', None)), 'MD.std': np.sqrt(results['d.std'] + results['dperp0.std'] + results['dperp1.std']) / 3., 'AD.std': results['d.std'], 'RD.std': (results['dperp0.std'] + results['dperp1.std']) / 2.0, }) if all(el in results for el in ['theta', 'phi', 'psi']): eigenvectors = tensor_spherical_to_cartesian( np.squeeze(results['theta']), np.squeeze(results['phi']), np.squeeze(results['psi'])) for ind in range(3): output.update({'vec{}'.format(ind): eigenvectors[ind]}) return output
def sort_eigensystem(parameters_dict): eigenvectors = np.stack(tensor_spherical_to_cartesian(np.squeeze(parameters_dict['theta']), np.squeeze(parameters_dict['phi']), np.squeeze(parameters_dict['psi'])), axis=0) eigenvalues = np.atleast_2d(np.squeeze(np.dstack([parameters_dict['d'], parameters_dict['dperp0'], parameters_dict['dperp1']]))) ranking = np.atleast_2d(np.squeeze(np.argsort(eigenvalues, axis=1, kind='mergesort')[:, ::-1])) voxels_range = np.arange(ranking.shape[0]) sorted_eigenvalues = np.concatenate([eigenvalues[voxels_range, ranking[:, ind], None] for ind in range(ranking.shape[1])], axis=1) sorted_eigenvectors = np.stack([eigenvectors[ranking[:, ind], voxels_range, :] for ind in range(ranking.shape[1])]) return sorted_eigenvalues, sorted_eigenvectors, ranking
def extra_optimization_maps(results): """Return some interesting measures like FA, MD, RD and AD. Args: results (dict): Dictionary containing at least theta, phi, psi, d, dperp0 and dperp1 We will use this to generate some standard measures from the diffusion Tensor. Returns: dict: as keys typical elements like 'FA and 'MD' as interesting output and as per values the maps. These maps are per voxel, and optionally per instance per voxel """ output = { 'FA': DTIMeasures.fractional_anisotropy(results['d'], results['dperp0'], results['dperp1']), 'MD': (results['d'] + results['dperp0'] + results['dperp1']) / 3., 'AD': results['d'], 'RD': (results['dperp0'] + results['dperp1']) / 2.0, } if all('{}.std'.format(el) in results for el in ['d', 'dperp0', 'dperp1']): output.update({ 'FA.std': DTIMeasures.fractional_anisotropy_std( results['d'], results['dperp0'], results['dperp1'], results['d.std'], results['dperp0.std'], results['dperp1.std']), 'MD.std': np.sqrt(results['d.std'] + results['dperp0.std'] + results['dperp1.std']) / 3., 'AD.std': results['d.std'], 'RD.std': (results['dperp0.std'] + results['dperp1.std']) / 2.0, }) if all(el in results for el in ['theta', 'phi', 'psi']): eigenvectors = tensor_spherical_to_cartesian(np.squeeze(results['theta']), np.squeeze(results['phi']), np.squeeze(results['psi'])) for ind in range(3): output.update({'vec{}'.format(ind): eigenvectors[ind]}) return output