topostats.tracing.splining#

Order single pixel skeletons with or without NodeStats Statistics.

Attributes#

Classes#

splineTrace

Smooth the ordered trace via an average of splines.

windowTrace

Obtain a smoothed trace of a molecule.

Functions#

measure_contour_length(→ float)

Contour length for each of the splined traces accounting for whether the molecule is circular or linear.

measure_end_to_end_distance(splined_trace, ...)

Euclidean distance between the start and end of linear molecules.

splining_image(→ None)

Obtain smoothed traces of pixel-wise ordered traces for molecules in an image.

interpolate_between_two_points_distance(...)

Interpolate between two points to create a new point at a set distance between the two.

resample_points_regular_interval(→ numpy.typing.NDArray)

Resample a set of points to be at regular spatial intervals.

Module Contents#

topostats.tracing.splining.LOGGER#
class topostats.tracing.splining.splineTrace(topostats_object: topostats.classes.TopoStats, grain: int, molecule: int, spline_step_size: float | None = None, spline_linear_smoothing: float | None = None, spline_circular_smoothing: float | None = None, spline_degree: int | None = None)[source]#

Smooth the ordered trace via an average of splines.

Parameters:
  • topostats_object (TopoStats) – TopoStats object with ordered traces calculated for grains and molecules.

  • grain (int) – Grain number to process.

  • molecule (int) – Molecule number to process.

  • spline_step_size (float) – Step length in meters to use a coordinate for splining.

  • spline_linear_smoothing (float) – Amount of linear spline smoothing.

  • spline_circular_smoothing (float) – Amount of circular spline smoothing.

  • spline_degree (int) – Degree of the spline. Cubic splines are recommended. Even values of k should be avoided especially with a small s-value.

image#
mol_ordered_trace#
mol_is_circular#
pixel_to_nm_scaling#
spline_step_size = None#
spline_linear_smoothing = None#
spline_circular_smoothing = None#
spline_degree = None#
tracing_stats#
get_splined_traces(fitted_trace: numpy.typing.NDArray) numpy.typing.NDArray[source]#

Get a splined version of the fitted trace - useful for finding the radius of gyration etc.

This function actually calculates the average of several splines which is important for getting a good fit on the lower resolution data.

Parameters:

fitted_trace (npt.NDArray) – Numpy array of the fitted trace.

Returns:

Splined (smoothed) array of trace.

Return type:

npt.NDArray

static remove_duplicate_consecutive_tuples(tuple_list: list[tuple | numpy.typing.NDArray]) list[tuple][source]#

Remove duplicate consecutive tuples from a list.

Parameters:

tuple_list (list[tuple | npt.NDArray]) – List of tuples or numpy ndarrays to remove consecutive duplicates from.

Returns:

List of tuples with consecutive duplicates removed.

Return type:

list[Tuple]

Examples

For the list of tuples [(1, 2), (1, 2), (1, 2), (2, 3), (2, 3), (3, 4)], this function will return [(1, 2), (2, 3), (3, 4)]

run_spline_trace() tuple[numpy.typing.NDArray, dict][source]#

Pipeline to run the splining smoothing and obtaining smoothing stats.

Returns:

Tuple of Nx2 smoothed trace coordinates, and smoothed trace statistics.

Return type:

tuple[npt.NDArray, dict]

class topostats.tracing.splining.windowTrace(topostats_object: topostats.classes.TopoStats, grain: int, molecule: int, rolling_window_size: float | None = None, rolling_window_resampling: bool = False, rolling_window_resample_interval_nm: float = 0.5)[source]#

Obtain a smoothed trace of a molecule.

Parameters:
  • topostats_object (TopoStats) – TopoStats object with ordered traces calculated for grains and molecules.

  • grain (int) – Grain number to process.

  • molecule (int) – Molecule number to process.

  • rolling_window_size (np.float64, optional) – The length of the rolling window too average over, by default 6.0.

  • rolling_window_resampling (bool, optional) – Whether to resample the rolling window, by default False.

  • rolling_window_resample_interval_nm (float, optional) – The regular spatial interval (nm) to resample the rolling window, by default 0.5.

mol_ordered_trace#
mol_is_circular#
pixel_to_nm_scaling#
rolling_window_size#
rolling_window_resampling = False#
rolling_window_resample_interval_nm = 499999999.99999994#
tracing_stats#
static pool_trace_circular(molecule: topostats.classes.Molecule, rolling_window_size: numpy.float64 = 6.0, pixel_to_nm_scaling: float = 1) numpy.typing.NDArray[numpy.float64][source]#

Smooth a pixelwise ordered trace of circular molecules via a sliding window.

Parameters:
  • molecule (Molecule) – Molecule object with attribute .ordered_coords, the ordered coordinates to be splined.

  • rolling_window_size (np.float64, optional) – The length of the rolling window too average over, by default 6.0.

  • pixel_to_nm_scaling (float, optional) – The pixel to nm scaling factor, by default 1.

Returns:

MxN Smoothed ordered trace coordinates.

Return type:

npt.NDArray[np.float64]

static pool_trace_linear(molecule: topostats.classes.Molecule, rolling_window_size: numpy.float64 = 6.0, pixel_to_nm_scaling: float = 1) numpy.typing.NDArray[numpy.float64][source]#

Smooth a pixelwise ordered trace of linear molecules via a sliding window.

Parameters:
  • molecule (Molecule) – Molecule object with attribute .ordered_coords, the ordered coordinates to be splined.

  • rolling_window_size (np.float64, optional) – The length of the rolling window too average over, by default 6.0.

  • pixel_to_nm_scaling (float, optional) – The pixel to nm scaling factor, by default 1.

Returns:

MxN Smoothed ordered trace coordinates.

Return type:

npt.NDArray[np.float64]

run_window_trace() tuple[numpy.typing.NDArray, dict][source]#

Pipeline to run the rolling window smoothing and obtaining smoothing stats.

Returns:

Tuple of Nx2 smoothed trace coordinates, and smoothed trace statistics.

Return type:

tuple[npt.NDArray, dict]

topostats.tracing.splining.measure_contour_length(splined_trace: numpy.typing.NDArray, mol_is_circular: bool, pixel_to_nm_scaling: float) float[source]#

Contour length for each of the splined traces accounting for whether the molecule is circular or linear.

Contour length units are nm.

Parameters:
  • splined_trace (npt.NDArray) – The splined trace.

  • mol_is_circular (bool) – Whether the molecule is circular or not.

  • pixel_to_nm_scaling (float) – Scaling factor from pixels to nanometres.

Returns:

Length of molecule in nanometres (nm).

Return type:

float

topostats.tracing.splining.measure_end_to_end_distance(splined_trace, mol_is_circular, pixel_to_nm_scaling: float)[source]#

Euclidean distance between the start and end of linear molecules.

The hypotenuse is calculated between the start ([0,0], [0,1]) and end ([-1,0], [-1,1]) of linear molecules. If the molecule is circular then the distance is set to zero (0).

Parameters:
  • splined_trace (npt.NDArray) – The splined trace.

  • mol_is_circular (bool) – Whether the molecule is circular or not.

  • pixel_to_nm_scaling (float) – Scaling factor from pixels to nanometres.

Returns:

Length of molecule in nanometres (nm).

Return type:

float

topostats.tracing.splining.splining_image(topostats_object: topostats.classes.TopoStats, method: str | None = None, rolling_window_size: float | None = None, spline_step_size: float | None = None, spline_linear_smoothing: float | None = None, spline_circular_smoothing: float | None = None, spline_degree: int | None = None, rolling_window_resampling: bool | None = None, rolling_window_resample_regular_spatial_interval: float | None = None) None[source]#

Obtain smoothed traces of pixel-wise ordered traces for molecules in an image.

Parameters:
  • topostats_object (TopoStats) – TopoStats object with ordered traces of grain crops to be splined.

  • method (str) – Method of trace smoothing, options are ‘splining’ and ‘rolling_window’.

  • rolling_window_size (float) – Length in meters to average coordinates over in the rolling window.

  • spline_step_size (float) – Step length in meters to use a coordinate for splining.

  • spline_linear_smoothing (float) – Amount of linear spline smoothing.

  • spline_circular_smoothing (float) – Amount of circular spline smoothing.

  • spline_degree (int) – Degree of the spline. Cubic splines are recommended. Even values of k should be avoided especially with a small s-value.

  • rolling_window_resampling (bool, optional) – Whether to resample the rolling window, by default False.

  • rolling_window_resample_regular_spatial_interval (float, optional) – The regular spatial interval (nm) to resample the rolling window, by default 0.5.

topostats.tracing.splining.interpolate_between_two_points_distance(point1: numpy.typing.NDArray[numpy.float32], point2: numpy.typing.NDArray[numpy.float32], distance: numpy.float32) numpy.typing.NDArray[numpy.float32][source]#

Interpolate between two points to create a new point at a set distance between the two.

Parameters:
  • point1 (npt.NDArray[np.float32]) – The first point.

  • point2 (npt.NDArray[np.float32]) – The second point.

  • distance (np.float32) – The distance to interpolate between the two points.

Returns:

The new point at the specified distance between the two points.

Return type:

npt.NDArray[np.float32]

topostats.tracing.splining.resample_points_regular_interval(points: numpy.typing.NDArray, interval: float, circular: bool) numpy.typing.NDArray[source]#

Resample a set of points to be at regular spatial intervals.

Note: This is NOT intended to be pure interpolation, as interpolated points would not produce uniformly spaced points in cartesian space.

Parameters:
  • points (npt.NDArray) – The points to resample.

  • interval (float) – The distance that all returned points should be apart.

  • circular (bool) – If True, the first and last points will be connected to form a closed loop. If False, the first and last points will not be connected.

Returns:

The resampled points, evenly spaced at the specified interval.

Return type:

npt.NDArray