topostats.tracing.tracingfuncs#

Miscellaneous tracing functions.

Module Contents#

Classes#

getSkeleton

Skeltonisation : "A Fast Parallel Algorithm for Thinning Digital Patterns" by Zhang et al., 1984.

reorderTrace

genTracingFuncs

class topostats.tracing.tracingfuncs.getSkeleton(image_data: numpy.typing.NDArray, binary_map: numpy.typing.NDArray, number_of_columns: int, number_of_rows: int, pixel_size: float)#

Skeltonisation : “A Fast Parallel Algorithm for Thinning Digital Patterns” by Zhang et al., 1984.

Parameters:
  • image_data (npt.NDArray) – Image to be traced.

  • binary_map (npt.NDArray) – Image mask.

  • number_of_columns (int) – Number of columns.

  • number_of_rows (int) – Number of rows.

  • pixel_size (float) – Pixel to nm scaling.

getDNAmolHeightStats()#

Get molecule heights.

doSkeletonising()#

Check if the skeletonising is finished.

_doSkeletonisingIteration()#

Do an iteration of skeletonisation.

Check for the local binary pixel environment and assess the local height values to decide whether to delete a point.

_deletePixelSubit1(point: numpy.typing.NDArray) bool#

Check whether a point should be deleted based on local binary environment and local height values.

Parameters:

point (npt.NDArray) – Point to be checked.

Returns:

Whether the point should be deleted.

Return type:

bool

_deletePixelSubit2(point: numpy.typing.NDArray) bool#

Check whether a point should be deleted based on local binary environment and local height values.

Parameters:

point (npt.NDArray) – Point to be checked.

Returns:

Whether the point should be deleted.

Return type:

bool

_binaryThinCheck_a() bool#

Binary thin check A.

Returns:

Whether the condition is met.

Return type:

bool

_binaryThinCheck_b() bool#

Binary thin check B.

Returns:

Whether the condition is met.

Return type:

bool

_binaryThinCheck_c() bool#

Binary thin check C.

Returns:

Whether the condition is met.

Return type:

bool

_binaryThinCheck_d() bool#

Binary thin check D.

Returns:

Whether the condition is met.

Return type:

bool

_binaryThinCheck_csharp() bool#

Binary thin check C#.

Returns:

Whether the condition is met.

Return type:

bool

_binaryThinCheck_dsharp() bool#

Binary thin check D#

Returns:

Whether the condition is met.

Return type:

bool

_checkHeights(candidate_points: numpy.typing.NDArray) numpy.typing.NDArray#

Check heights.

Parameters:

candidate_points (npt.NDArray) - > npt.NDArra) – Candidate points to be checked.

Returns:

Candidate points.

Return type:

npt.NDArray

_checkWhichHeightPoints()#

Check which height points.

_initialiseHeightFindingDict()#
_getHorizontalLeftHeights(x: int, y: int) float#

Calculate heights left (west).

Parameters:
  • x (int) – X coordinate.

  • y (int) – Y coordinate.

Returns:

Height left (west).

Return type:

float

_getHorizontalRightHeights(x, y)#

Calculate heights right (east).

Parameters:
  • x (int) – X coordinate.

  • y (int) – Y coordinate.

Returns:

Height right (east).

Return type:

float

_getVerticalUpwardHeights(x, y)#

Calculate heights upwards (north).

Parameters:
  • x (int) – X coordinate.

  • y (int) – Y coordinate.

Returns:

Height upwards (north).

Return type:

float

_getVerticalDonwardHeights(x, y)#

Calculate heights downwards (south).

Parameters:
  • x (int) – X coordinate.

  • y (int) – Y coordinate.

Returns:

Height downwards (south).

Return type:

float

_getDiaganolLeftUpwardHeights(x, y)#

Calculate heights diagonal left upwards (north east).

Parameters:
  • x (int) – X coordinate.

  • y (int) – Y coordinate.

Returns:

Height to diagonal left upwards (north east).

Return type:

float

_getDiaganolLeftDownwardHeights(x, y)#

Calculate heights diagonal left downwards (south west).

Parameters:
  • x (int) – X coordinate.

  • y (int) – Y coordinate.

Returns:

Height diagonal left downwards (south west).

Return type:

float

_getDiaganolRightUpwardHeights(x: int, y: int) float#

Calculate heights diagonal right upwards (north east).

Parameters:
  • x (int) – X coordinate.

  • y (int) – Y coordinate.

Returns:

Height diagonal right upwards (north east).

Return type:

float

_getDiaganolRightDownwardHeights(x, y)#

Calculate heights diagonal right downwards (south east).

Parameters:
  • x (int) – X coordinate.

  • y (int) – Y coordinate.

Returns:

Height heights diagonal right downwards (south east).

Return type:

float

_condemnPoint(x: int, y: int) float#

Condemn a point.

Parameters:
  • x (int) – X coordinate.

  • y (int) – Y coordinate.

Returns:

Height to be condemned.

Return type:

float

_identifyHighestPoint(x, y, index_direction, indexed_heights)#
finalSkeletonisationIteration()#

A final skeletonisation iteration that removes “hanging” pixels.

Examples of such pixels are:

[0, 0, 0] [0, 1, 0] [0, 0, 0] [0, 1, 1] [0, 1, 1] [0, 1, 1]

case 1: [0, 1, 0] or case 2: [0, 1, 0] or case 3: [1, 1, 0]

This is useful for the future functions that rely on local pixel environment to make assessments about the overall shape/structure of traces

_binaryFinalThinCheck_a()#

Binary final thin check A.

_binaryFinalThinCheck_b()#

Binary final thin check B.

_binaryThinCheck_b_returncount()#

Binary final thin check B return count.

pruneSkeleton()#

Function to remove the hanging branches from the skeletons.

These are a persistent problem in the overall tracing process.

_findBranchEnds(coordinates)#
_deleteSquareEnds(coordinates)#
class topostats.tracing.tracingfuncs.reorderTrace#
static linearTrace(trace_coordinates)#

My own function to order the points from a linear trace.

This works by checking the local neighbours for a given pixel (starting at one of the ends). If this pixel has only one neighbour in the array of unordered points, this must be the next pixel in the trace – and it is added to the ordered points trace and removed from the remaining_unordered_coords array.

If there is more than one neighbouring pixel, a fairly simple function (checkVectorsCandidatePoints) finds which pixel incurs the smallest change in angle compared with the rest of the trace and chooses that as the next point.

This process is repeated until all the points are placed in the ordered trace array or the other end point is reached.

static circularTrace(trace_coordinates)#

An alternative implementation of the linear tracing algorithm but with some adaptations to work with circular dna molecules

static circularTrace_old(trace_coordinates)#

Reorders the coordinates of a trace from a circular DNA molecule (with no loops) using a polar coordinate system with reference to the center of mass

I think every step of this can be vectorised for speed up

This is vulnerable to bugs if the dna molecule folds in on itself slightly

loopedCircularTrace()#
loopedLinearTrace()#
class topostats.tracing.tracingfuncs.genTracingFuncs#
static getLocalPixelsBinary(binary_map, x, y)#
static countNeighbours(x, y, trace_coordinates)#

Counts the number of neighbouring points for a given coordinate in a list of points

static getNeighbours(x, y, trace_coordinates)#

Returns an array containing the neighbouring points for a given coordinate in a list of points

static countandGetNeighbours(x, y, trace_coordinates)#

Returns the number of neighbouring points for a coordinate and an array containing the those points

static returnPointsInArray(points_array, trace_coordinates)#
static makeGrid(x, y, size)#
static findBestNextPoint(x, y, ordered_points, candidate_points)#
static checkVectorsCandidatePoints(x, y, ordered_points, candidate_points)#

Finds which neighbouring pixel incurs the smallest angular change with reference to a previous pixel in the ordered trace, and chooses that as the next point