Coverage for subcell_pipeline/analysis/compression_metrics/compression_metric.py: 0%

27 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2024-08-29 15:14 +0000

1"""Enumerations for compression metric analysis.""" 

2 

3from enum import Enum 

4from typing import Any, Callable, Union 

5 

6import numpy as np 

7 

8from subcell_pipeline.analysis.compression_metrics.polymer_trace import ( 

9 get_asymmetry_of_peak, 

10 get_average_distance_from_end_to_end_axis, 

11 get_bending_energy_from_trace, 

12 get_compression_ratio, 

13 get_contour_length_from_trace, 

14 get_sum_bending_energy, 

15 get_third_component_variance, 

16 get_total_fiber_twist, 

17 get_twist_angle, 

18) 

19 

20 

21class CompressionMetric(Enum): 

22 """Enumeration for compression metrics.""" 

23 

24 NON_COPLANARITY = "non_coplanarity" 

25 PEAK_ASYMMETRY = "peak_asymmetry" 

26 SUM_BENDING_ENERGY = "sum_bending_energy" 

27 AVERAGE_PERP_DISTANCE = "average_perp_distance" 

28 TOTAL_FIBER_TWIST = "total_fiber_twist" 

29 ENERGY_ASYMMETRY = "energy_asymmetry" 

30 CALC_BENDING_ENERGY = "calc_bending_energy" 

31 CONTOUR_LENGTH = "contour_length" 

32 COMPRESSION_RATIO = "compression_ratio" 

33 TWIST_ANGLE = "twist_angle" 

34 

35 def label(self: Enum) -> str: 

36 """ 

37 Return the label for the compression metric. 

38 

39 Returns 

40 ------- 

41 : 

42 The label for the compression metric. 

43 """ 

44 labels = { 

45 CompressionMetric.NON_COPLANARITY.value: "Non-coplanarity", 

46 CompressionMetric.PEAK_ASYMMETRY.value: "Peak Asymmetry", 

47 CompressionMetric.SUM_BENDING_ENERGY.value: "Sum Bending Energy", 

48 CompressionMetric.AVERAGE_PERP_DISTANCE.value: ( 

49 "Average Perpendicular Distance" 

50 ), 

51 CompressionMetric.TOTAL_FIBER_TWIST.value: "Fiber Twist", 

52 CompressionMetric.CALC_BENDING_ENERGY.value: "Calculated Bending Energy", 

53 CompressionMetric.CONTOUR_LENGTH.value: "Contour Length", 

54 CompressionMetric.COMPRESSION_RATIO.value: "Compression Ratio", 

55 CompressionMetric.TWIST_ANGLE.value: "Twist Angle", 

56 } 

57 return labels.get(self.value, "") 

58 

59 def description(self: Enum) -> str: 

60 """ 

61 Return the description for the compression metric. 

62 

63 Returns 

64 ------- 

65 : 

66 The description (and units) for the compression metric. 

67 """ 

68 units = { 

69 CompressionMetric.NON_COPLANARITY.value: "3rd component variance from PCA", 

70 CompressionMetric.PEAK_ASYMMETRY.value: "normalized peak distance", 

71 CompressionMetric.SUM_BENDING_ENERGY.value: "sum of bending energy", 

72 CompressionMetric.AVERAGE_PERP_DISTANCE.value: "distance (nm)", 

73 CompressionMetric.TOTAL_FIBER_TWIST.value: "total fiber twist", 

74 CompressionMetric.CALC_BENDING_ENERGY.value: "energy", 

75 CompressionMetric.CONTOUR_LENGTH.value: "filament contour length (nm)", 

76 CompressionMetric.COMPRESSION_RATIO.value: "compression ratio", 

77 CompressionMetric.TWIST_ANGLE.value: ( 

78 "difference between initial and final tangent (degrees)" 

79 ), 

80 } 

81 return units.get(self.value, "") 

82 

83 def bounds(self: Enum) -> tuple[float, float]: 

84 """ 

85 Return the default bounds for the compression metric. 

86 

87 Returns 

88 ------- 

89 : 

90 The default bounds for the compression metric. 

91 """ 

92 bounds = { 

93 CompressionMetric.NON_COPLANARITY.value: (0, 0.03), 

94 CompressionMetric.PEAK_ASYMMETRY.value: (0, 0.5), 

95 CompressionMetric.SUM_BENDING_ENERGY.value: (0, 0), # TODO 

96 CompressionMetric.AVERAGE_PERP_DISTANCE.value: (0, 85.0), 

97 CompressionMetric.TOTAL_FIBER_TWIST.value: (0, 0), # TODO 

98 CompressionMetric.CALC_BENDING_ENERGY.value: (0, 10), 

99 CompressionMetric.CONTOUR_LENGTH.value: (480, 505), 

100 CompressionMetric.COMPRESSION_RATIO.value: (0, 1), # TODO 

101 CompressionMetric.TWIST_ANGLE.value: (-180, 180), 

102 } 

103 return bounds.get(self.value, (0, 0)) 

104 

105 def calculate_metric( 

106 self, polymer_trace: np.ndarray, **options: dict[str, Any] 

107 ) -> Union[float, np.floating[Any]]: 

108 """ 

109 Calculate the compression metric for the given polymer trace. 

110 

111 Parameters 

112 ---------- 

113 polymer_trace 

114 Array containing the x,y,z positions of the polymer trace 

115 **options 

116 Additional options as key-value pairs. 

117 

118 Returns 

119 ------- 

120 : 

121 The calculated compression metric for the polymer 

122 """ 

123 functions: dict[CompressionMetric, Callable] = { 

124 CompressionMetric.NON_COPLANARITY: get_third_component_variance, 

125 CompressionMetric.PEAK_ASYMMETRY: get_asymmetry_of_peak, 

126 CompressionMetric.SUM_BENDING_ENERGY: get_sum_bending_energy, 

127 CompressionMetric.AVERAGE_PERP_DISTANCE: ( 

128 get_average_distance_from_end_to_end_axis 

129 ), 

130 CompressionMetric.TOTAL_FIBER_TWIST: get_total_fiber_twist, 

131 CompressionMetric.CALC_BENDING_ENERGY: get_bending_energy_from_trace, 

132 CompressionMetric.CONTOUR_LENGTH: get_contour_length_from_trace, 

133 CompressionMetric.COMPRESSION_RATIO: get_compression_ratio, 

134 CompressionMetric.TWIST_ANGLE: get_twist_angle, 

135 } 

136 return functions[self](polymer_trace, **options)