Coverage for subcell_pipeline/analysis/compression_metrics/vectors.py: 33%

12 statements  

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

1"""Methods for vector operations.""" 

2 

3import numpy as np 

4 

5from subcell_pipeline.analysis.compression_metrics.constants import ABSOLUTE_TOLERANCE 

6 

7 

8def get_unit_vector(vector: np.ndarray) -> np.ndarray: 

9 """ 

10 Calculate the unit vector from a given vector. 

11 

12 Parameters 

13 ---------- 

14 vector 

15 Array containing the x,y,z positions of the vector. 

16 

17 Returns 

18 ------- 

19 : 

20 Unit vector. 

21 """ 

22 if np.linalg.norm(vector) < ABSOLUTE_TOLERANCE or np.isnan(vector).any(): 

23 return np.array([0, 0, 0]) 

24 else: 

25 vec_length = np.linalg.norm(vector) 

26 return vector / vec_length 

27 

28 

29def get_end_to_end_unit_vector(polymer_trace: np.ndarray) -> np.ndarray: 

30 """ 

31 Calculate the unit vector of the end-to-end axis of a polymer trace. 

32 

33 Parameters 

34 ---------- 

35 polymer_trace 

36 Array containing the x,y,z positions of the polymer trace. 

37 

38 Returns 

39 ------- 

40 : 

41 Unit vector of the end-to-end axis of the polymer trace. 

42 """ 

43 assert len(polymer_trace) > 1, "Polymer trace must have at least 2 points" 

44 assert polymer_trace.shape[1] == 3, "Polymer trace must have 3 columns" 

45 

46 end_to_end_axis = polymer_trace[-1] - polymer_trace[0] 

47 

48 return get_unit_vector(end_to_end_axis)