Source code for subcell_pipeline.analysis.compression_metrics.vectors
"""Methods for vector operations."""importnumpyasnpfromsubcell_pipeline.analysis.compression_metrics.constantsimportABSOLUTE_TOLERANCE
[docs]defget_unit_vector(vector:np.ndarray)->np.ndarray:""" Calculate the unit vector from a given vector. Parameters ---------- vector Array containing the x,y,z positions of the vector. Returns ------- : Unit vector. """ifnp.linalg.norm(vector)<ABSOLUTE_TOLERANCEornp.isnan(vector).any():returnnp.array([0,0,0])else:vec_length=np.linalg.norm(vector)returnvector/vec_length
[docs]defget_end_to_end_unit_vector(polymer_trace:np.ndarray)->np.ndarray:""" Calculate the unit vector of the end-to-end axis of a polymer trace. Parameters ---------- polymer_trace Array containing the x,y,z positions of the polymer trace. Returns ------- : Unit vector of the end-to-end axis of the polymer trace. """assertlen(polymer_trace)>1,"Polymer trace must have at least 2 points"assertpolymer_trace.shape[1]==3,"Polymer trace must have 3 columns"end_to_end_axis=polymer_trace[-1]-polymer_trace[0]returnget_unit_vector(end_to_end_axis)