Coverage for local_installation_linux/mumott/output_handling/reconstruction_derived_quantities.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2025-05-05 21:21 +0000

1from dataclasses import dataclass 

2import numpy as np 

3 

4 

5def get_sorted_eigenvectors(tensors_array: np.array): 

6 """ Caluculate eigenvectors and eigenvalues of an array of 

7 3 by 3 matrices and sort according to increasing eigenvalues. 

8 

9 Parameters 

10 ---------- 

11 tensors_array 

12 numpy array containing the 3 by 3 tensors with the tensor 

13 indicies as the two last. 

14 

15 Returns 

16 ------- 

17 eigenvalues 

18 numpy array containing the eigenvalues of the tensors where 

19 the last dimension indexes the eigenvalues. Smallest eigenvalues 

20 come first. 

21 eigenvectors 

22 numpy array containing the eigenvalues of the tensors where 

23 the last dimension indexes the eigenvalues and the second 

24 to last dimension is the vector index. 

25 """ 

26 

27 volume_shape = tensors_array.shape[:-2] 

28 

29 # Compute and sort eigenvectors 

30 w, v = np.linalg.eigh(tensors_array.reshape(-1, 3, 3)) 

31 sorting = np.argsort(w, axis=1).reshape(-1, 3, 1) 

32 v = v.transpose(0, 2, 1) 

33 v = np.take_along_axis(v, sorting, axis=1) 

34 v = v.transpose(0, 2, 1) 

35 v = v / np.sqrt(np.sum(v ** 2, axis=1).reshape(-1, 1, 3)) 

36 eigenvectors = v.reshape(volume_shape + (3, 3,)) 

37 eigenvalues = np.sort(w, axis=-1).reshape(volume_shape + (3,)) 

38 

39 # Flip eigenvectors to have a positive z-component 

40 for ii in range(3): 

41 whereflip = eigenvectors[..., 2, ii] < 0.0 

42 eigenvectors[whereflip, :, ii] = -eigenvectors[whereflip, :, ii] 

43 

44 return eigenvalues, eigenvectors 

45 

46 

47@dataclass 

48class ReconstructionDerivedQuantities: 

49 """ A number of useful quantities that have been computed from the coefficients of a 

50 reconstruction. 

51 

52 Attributes 

53 ---------- 

54 volume_shape : tuple 

55 The shape if the reconstructed volume. 

56 mean_intensity : np.array 

57 The mean intensity of the reconstructed functions over the unit sphere for each voxel. 

58 fractional_anisostropy : np.array 

59 A measure of the relative amount of anisotropic scattering in each voxel. 

60 eigenvector_1 

61 eigenvector_2 

62 eigenvector_3 : np.array 

63 The eingenvectors of the second order tensor component of the function. 

64 Sorted by ascending eigenvalue. 

65 eigenvalue_1 

66 eigenvalue_2 

67 eigenvalue_3 : np.array 

68 The eingenvalues of the second order tensor component of the function. 

69 Sorted by ascending eigenvalue. 

70 second_moment_tensor : np.array 

71 The second-moment tensor, which includes both the zero-th and second-order 

72 parts of the reconstructed functions. 

73 """ 

74 

75 volume_shape: tuple 

76 mean_intensity: np.array 

77 fractional_anisotropy: np.array 

78 eigenvector_1: np.array 

79 eigenvector_2: np.array 

80 eigenvector_3: np.array 

81 eigenvalue_1: np.array 

82 eigenvalue_2: np.array 

83 eigenvalue_3: np.array 

84 second_moment_tensor: np.array 

85 

86 def save_to_disk(filename : str): 

87 raise NotImplementedError