Global -> Spline Metrics Handlers

tardis_em.utils.spline_metric.resample_filament(points, spacing_size)

Resamples points for each filament so they have the given spacing size.

Parameters: points (np.array): Array of shape [N, 4] where each column is [ID, X, Y, Z]. spacing_size (float): Desired spacing size between points.

Returns: np.array: Resampled array with the same structure.

class tardis_em.utils.spline_metric.FilterConnectedNearSegments(distance_th=1000, cylinder_radius=150)

Connect splines based on spline trajectory and splines end distances.

splines_direction(spline1: list, spline2: list) bool

Check if two splines facing the same direction. Knowing that spline1 facing a direction of spline2, check if spline2 also face a direction of spline1.

Parameters:
  • spline1 (list) – Sorted array of point with 3D coordinates.

  • spline2 (list) – Sorted array of point with 3D coordinates.

Returns:

If true, given splines facing the same direction

Return type:

bool

marge_splines(point_cloud: ndarray, omit_border: int, initial: bool) ndarray
Connect splines in the point_cloud that are close enough to each other

and return the reordered splines.

Example

  • Get dictionary with {id: [sorted list of points]}

  • While there is more than 1 spline in the dict:
    • Pick first spline in a dict.

    • Select all ends

    • Calc. end distance to all other ends.

    • If any ends within threshold distance:
      • Calculate initial end vector

      • Calculate distance to the cylinder

      • If any picked ends are within cylinder radius
        • Pick one with the smallest distance

        • Save two spline IDs to connected

    • If Connected ID list not empty:
      • Marge and sort points from two IDs

      • Check tortuosity

      • If tortuosity <= 1.5:
        • Add to the connected dict

        • Remove from dict

        • start over

    • If connected ID list is empty or tortuosity > 1.5:
      • Add only initial spline to the connected dict

      • Remove from dict

      • start over

Parameters:
  • point_cloud (np.ndarray) – Array with segmented and sorted point cloud of a shape [ID, X, Y, Z].

  • omit_border (int) – In A, distance from the border as a limit not to connect splines.

  • initial (bool) – Initial run for the operation.

Returns:

Array of segmented point cloud with connected splines that fit the criteria

Return type:

np.ndarray

class tardis_em.utils.spline_metric.FilterSpatialGraph(connect_seg_if_closer_then=1000, cylinder_radius=200, filter_short_segments=1000)

Calculate length of each spline and distance between all splines ends.

This clas iterate over all splines in array [ID, X, Y, Z] by given ID. Firstly if specified during initialization, class calculate distance between all splines ends and use it to define which splines are obviously broken. Then it calculate length of all the splines (also new one) and filter out ones that are too short.

class tardis_em.utils.spline_metric.SpatialGraphCompare(distance_threshold: int, interaction_threshold: float)

Compare two spatial graphs and output filtered-out array’s of splines based on similarity.

This class take as na input two arrays of shape [n, 3 or 4] for 2D or 3D point cloud. This arrays contain [ID x X x Y x Z] dimensions.

The comparison is archived by calculating cdist for all splines from one spatial graph to all splines from second spatial graph. And for each spline it output probability of similarity and splines id’s.

The Probability is calculated as a ration of points (in threshold contact) to all points in spline.

The selection threshold for the spline interaction is given between 0 and 1.

tardis_em.utils.spline_metric.compare_splines_probability(spline_1: ndarray, spline_2: ndarray, threshold=100)

Compare two splines and calculate probability of how likely given two splines are the same line given array of points for same or similar splines with no matching coordinates of points.

Calculates the probability of two splines being similar by comparing the distance between their points and taking the mean of the matching points below a threshold.

Parameters:
  • spline_1 (np.ndarray) – The first spline to compare, represented as an array of points.

  • spline_2 (np.ndarray) – The second spline to compare, represented as an array of points.

  • threshold (int) – The maximum distance between points for them to be considered matching.

Returns:

The probability of the splines being similar, ranging from 0.0 to 1.0.

Return type:

float

tardis_em.utils.spline_metric.smooth_spline(points: ndarray, s=0.5)

Spline smoothing given an ‘s’ smoothness factor.

Parameters:
  • points (np.ndarray) – Point array [(ID), X, Y, Z] with optional ID and Z

  • dimension.

  • s (float) – Smoothness factor.

Returns:

Smooth spline

Return type:

Returns

tardis_em.utils.spline_metric.sort_segment(coord: ndarray) ndarray

Sorting of the point cloud based on number of connections followed by searching of the closest point with cdist.

Parameters:

coord (np.ndarray) – Coordinates for each unsorted point idx.

Returns:

Array of point in line order.

Return type:

np.ndarray

tardis_em.utils.spline_metric.reorder_segments_id(coord: ndarray, order_range: list | None = None, order_list: ndarray | list | None = None) ndarray

Reorder list of segments to remove missing IDs

E.g. Change IDs from [1, 2, 3, 5, 6, 8] to [1, 2, 3, 4, 5, 6]

Parameters:
  • coord – Array of points in 3D or 3D with their segment ID

  • order_range – Costume id range for reordering

  • order_list – List of reorder IDs to match to coord.

Returns:

Array of points with reordered IDs values

Return type:

np.ndarray

tardis_em.utils.spline_metric.tortuosity(coord: ndarray) float

Calculate spline tortuosity.

Parameters:

coord (np.ndarray) – Coordinates for each unsorted point idx.

Returns:

Spline curvature measured with tortuosity.

Return type:

float

tardis_em.utils.spline_metric.total_length(coord: ndarray) float

Calculate total length of the spline.

Parameters:

coord (np.ndarray) – Coordinates for each unsorted point idx.

Returns:

Spline length.

Return type:

float

tardis_em.utils.spline_metric.length_list(coord: ndarray) list

Calculate total length of all splines and return it as a list.

Parameters:

coord (np.ndarray) – Coordinates for each unsorted point idx.

Returns:

Spline length list.

Return type:

list

tardis_em.utils.spline_metric.angle_between_vectors(v1, v2)

Calculate the angle in degrees between two vectors.

This function uses the dot product and the magnitudes of the vectors to calculate the angle between them according to the formula:

cos(theta) = (A . B) / (||A|| ||B||)

Parameters:
  • v1 (np.ndarray) – First input vector.

  • v2 (np.ndarray) – Second input vector.

Returns:

float The angle in degrees between vector ‘v1’ and ‘v2’.

tardis_em.utils.spline_metric.cut_150_degree(segments_array: ndarray)

Cut segments based on angles between adjacent vectors.

Given an array of line segments, this function calculates angles between adjacent vectors in each line. If the angle is less than or equal to 150 degrees, the segment is cut into two new segments.

Args: segments_array(np. ndarray): Array of line segments where the first column indicates the segment id and the remaining columns represent the coordinates of points.

Parameters:

segments_array

Returns:

Indicates whether any segment was cut,

and New array of cut segments.

Return type:

Tuple[bool, np.ndarray]

tardis_em.utils.spline_metric.sort_by_length(coord)

Sort all splines by their length.

Parameters:

coord – Array of coordinates.

Returns:

sorted and reorder splines.

Return type:

np.ndarray

class tardis_em.utils.spline_metric.ComputeConfidenceScore
static normalized_length(points: ndarray, min_l: float, max_l: float)

Calculate and normalize the total length of a sequence of points. The total length is calculated using the total_length function. The result is then normalized to a value between 0 and 1 based on the provided minimum and maximum length values.

Parameters:
  • points (np.ndarray)

  • min_l (float) – The minimum length value.

  • max_l (float) – The maximum length value.

combined_smoothness(points: ndarray, min_l: float, max_l: float)