Disordered Tracing Modules
Generates disordered traces (pruned skeletons) and metrics.
disorderedTrace
Calculate disordered traces for a DNA molecule and calculates statistics from those traces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
NDArray
|
Cropped image, typically padded beyond the bounding box. |
required |
mask
|
NDArray
|
Labelled mask for the grain, typically padded beyond the bounding box. |
required |
filename
|
str
|
Filename being processed. |
required |
pixel_to_nm_scaling
|
float
|
Pixel to nm scaling. |
required |
min_skeleton_size
|
int
|
Minimum skeleton size below which tracing statistics are not calculated. |
10
|
mask_smoothing_params
|
dict
|
Dictionary of parameters to smooth the grain mask for better quality skeletonisation results. Contains a gaussian 'sigma' and number of dilation iterations. |
None
|
skeletonisation_params
|
dict
|
Skeletonisation Parameters. Method of skeletonisation to use 'topostats' is the original TopoStats method. Three methods from scikit-image are available 'zhang', 'lee' and 'thin'. |
None
|
pruning_params
|
dict
|
Dictionary of pruning parameters. Contains 'method', 'max_length', 'height_threshold', 'method_values' 'method_outlier' and 'only_height_prune_endpoints'. |
None
|
n_grain
|
int
|
Grain number being processed (only used in logging). |
None
|
Source code in topostats\tracing\disordered_tracing.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
__init__(image: npt.NDArray, mask: npt.NDArray, filename: str, pixel_to_nm_scaling: float, min_skeleton_size: int = 10, mask_smoothing_params: dict | None = None, skeletonisation_params: dict | None = None, pruning_params: dict | None = None, n_grain: int = None)
Calculate disordered traces for a DNA molecule and calculates statistics from those traces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
NDArray
|
Cropped image, typically padded beyond the bounding box. |
required |
mask
|
NDArray
|
Labelled mask for the grain, typically padded beyond the bounding box. |
required |
filename
|
str
|
Filename being processed. |
required |
pixel_to_nm_scaling
|
float
|
Pixel to nm scaling. |
required |
min_skeleton_size
|
int
|
Minimum skeleton size below which tracing statistics are not calculated. |
10
|
mask_smoothing_params
|
dict
|
Dictionary of parameters to smooth the grain mask for better quality skeletonisation results. Contains a gaussian 'sigma' and number of dilation iterations. |
None
|
skeletonisation_params
|
dict
|
Skeletonisation Parameters. Method of skeletonisation to use 'topostats' is the original TopoStats method. Three methods from scikit-image are available 'zhang', 'lee' and 'thin'. |
None
|
pruning_params
|
dict
|
Dictionary of pruning parameters. Contains 'method', 'max_length', 'height_threshold', 'method_values', 'method_outlier' and 'only_height_prune_endpoints'. |
None
|
n_grain
|
int
|
Grain number being processed (only used in logging). |
None
|
Source code in topostats\tracing\disordered_tracing.py
calculate_dna_width(smoothed_mask: npt.NDArray, pruned_skeleton: npt.NDArray, pixel_to_nm_scaling: float = 1) -> float
staticmethod
Calculate the mean width in metres of the DNA using the trace and mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
smoothed_mask
|
NDArray
|
Smoothed mask to be measured. |
required |
pruned_skeleton
|
NDArray
|
Pruned skeleton. |
required |
pixel_to_nm_scaling
|
float
|
Scaling of pixels to nanometres. |
1
|
Returns:
| Type | Description |
|---|---|
float
|
Width of grain in metres. |
Source code in topostats\tracing\disordered_tracing.py
re_add_holes(orig_mask: npt.NDArray, smoothed_mask: npt.NDArray, holearea_min_max: tuple[float | int | None] = (2, None)) -> npt.NDArray
Restore holes in masks that were occluded by dilation.
As Gaussian dilation smoothing methods can close holes in the original mask, this function obtains those holes
(based on the general background being the first due to padding) and adds them back into the smoothed mask. When
paired with smooth_mask, this essentially just smooths the outer edge of the mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
orig_mask
|
NDArray
|
Original mask. |
required |
smoothed_mask
|
NDArray
|
Original mask but with inner and outer edged smoothed. The smoothing operation may have closed up important holes in the mask. |
required |
holearea_min_max
|
tuple[float | int | None]
|
Tuple of minimum and maximum hole area (in nanometers) to replace from the original mask into the smoothed mask. |
(2, None)
|
Returns:
| Type | Description |
|---|---|
NDArray
|
Smoothed mask with holes restored. |
Source code in topostats\tracing\disordered_tracing.py
remove_touching_edge(skeleton: npt.NDArray) -> npt.NDArray
staticmethod
Remove any skeleton points touching the border (to prevent errors later).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skeleton
|
NDArray
|
A binary array where touching clusters of 1's become 0's if touching the edge of the array. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
Skeleton without points touching the border. |
Source code in topostats\tracing\disordered_tracing.py
smooth_mask(grain: npt.NDArray, dilation_iterations: int = 2, gaussian_sigma: float | int = 2, holearea_min_max: tuple[int | float | None] = (0, None)) -> npt.NDArray
Smooth a grain mask based on the lower number of binary pixels added from dilation or gaussian.
This method ensures gaussian smoothing isn't too aggressive and covers / creates gaps in the mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grain
|
NDArray
|
Numpy array of the grain mask. |
required |
dilation_iterations
|
int
|
Number of times to dilate the grain to smooth it. Default is 2. |
2
|
gaussian_sigma
|
float | None
|
Gaussian sigma value to smooth the grains after an Otsu threshold. If None, defaults to 2. |
2
|
holearea_min_max
|
tuple[float | int | None]
|
Tuple of minimum and maximum hole area (in nanometers) to replace from the original mask into the smoothed mask. |
(0, None)
|
Returns:
| Type | Description |
|---|---|
NDArray
|
Numpy array of smoothed image. |
Source code in topostats\tracing\disordered_tracing.py
trace_dna()
Perform the DNA skeletonisation and cleaning pipeline.
Source code in topostats\tracing\disordered_tracing.py
check_pixel_touching_edge(mask: npt.NDArray) -> bool
Check if any pixels in a mask touch the edge of the image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
NDArray
|
Numpy array of mask to be checked. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True or False if a pixel is found on the edge of the image. |
Source code in topostats\tracing\disordered_tracing.py
crop_array(array: npt.NDArray, bounding_box: tuple, pad_width: int = 0) -> npt.NDArray
Crop an array.
Ideally we pad the array that is being cropped so that we have heights outside of the grains bounding box. However, in some cases, if a grain is near the edge of the image scan this results in requesting indexes outside of the existing image. In which case we get as much of the image padded as possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
NDArray
|
2D Numpy array to be cropped. |
required |
bounding_box
|
Tuple
|
Tuple of coordinates to crop, should be of form (min_row, min_col, max_row, max_col). |
required |
pad_width
|
int
|
Padding to apply to bounding box. |
0
|
Returns:
| Type | Description |
|---|---|
NDArray()
|
Cropped array. |
Source code in topostats\tracing\disordered_tracing.py
disordered_trace_grain(cropped_image: npt.NDArray, cropped_mask: npt.NDArray, pixel_to_nm_scaling: float, mask_smoothing_params: dict, skeletonisation_params: dict, pruning_params: dict, filename: str = None, min_skeleton_size: int = 10, n_grain: int = None) -> dict
Trace an individual grain.
Tracing involves multiple steps...
- Skeletonisation
- Pruning of side branches (artefacts from skeletonisation).
- Ordering of the skeleton.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cropped_image
|
NDArray
|
Cropped array from the original image defined as the bounding box from the labelled mask. |
required |
cropped_mask
|
NDArray
|
Cropped array from the labelled image defined as the bounding box from the labelled mask. This should have been converted to a binary mask. |
required |
pixel_to_nm_scaling
|
float
|
Pixel to nm scaling. |
required |
mask_smoothing_params
|
dict
|
Dictionary of parameters to smooth the grain mask for better quality skeletonisation results. Contains a gaussian 'sigma' and number of dilation iterations. |
required |
skeletonisation_params
|
dict
|
Dictionary of skeletonisation parameters, options are 'zhang' (scikit-image) / 'lee' (scikit-image) / 'thin' (scikitimage) or 'topostats' (original TopoStats method). |
required |
pruning_params
|
dict
|
Dictionary of pruning parameters. |
required |
filename
|
str
|
File being processed. |
None
|
min_skeleton_size
|
int
|
Minimum size of grain in pixels after skeletonisation. |
10
|
n_grain
|
int
|
Grain number being processed. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary of the contour length, whether the image is circular or linear, the end-to-end distance and an array of coordinates. |
Source code in topostats\tracing\disordered_tracing.py
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 | |
find_connections(row: pd.Series, skan_df: pd.DataFrame) -> str
Compile the neighbouring branch indexes of the row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
Series
|
A row from the Skan summarize dataframe. |
required |
skan_df
|
DataFrame
|
The statistics DataFrame produced by Skan's |
required |
Returns:
| Type | Description |
|---|---|
str
|
A string representation of a list of matching row indices where the node src and dst columns match that of the rows. String is needed for csv compatibility since csvs can't hold lists. |
Source code in topostats\tracing\disordered_tracing.py
get_skan_image(original_image: npt.NDArray, pruned_skeleton: npt.NDArray, skan_column: str) -> npt.NDArray
Label each branch with it's Skan branch type label.
Branch types (+1 compared to Skan docs) are defined as: 1 = Endpoint-to-endpoint (isolated branch) 2 = Junction-to-endpoint 3 = Junction-to-junction 4 = Isolated cycle
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_image
|
NDArray
|
Height image from which the pruned skeleton is derived from. |
required |
pruned_skeleton
|
NDArray
|
Single pixel thick skeleton mask. |
required |
skan_column
|
str
|
A column from Skan's summarize function to colour the branch segments with. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
2D array where the background is 0, and skeleton branches label as their Skan branch type. |
Source code in topostats\tracing\disordered_tracing.py
grain_anchor(array_shape: tuple, bounding_box: list, pad_width: int) -> list
Extract anchor (min_row, min_col) from labelled regions and align individual traces over the original image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array_shape
|
tuple
|
Shape of original array. |
required |
bounding_box
|
list
|
A list of region properties returned by 'skimage.measure.regionprops()'. |
required |
pad_width
|
int
|
Padding for image. |
required |
Returns:
| Type | Description |
|---|---|
list(Tuple)
|
A list of tuples of the min_row, min_col of each bounding box. |
Source code in topostats\tracing\disordered_tracing.py
pad_bounding_box(array_shape: tuple, bounding_box: list, pad_width: int) -> list
Pad coordinates, if they extend beyond image boundaries stop at boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array_shape
|
tuple
|
Shape of original image (row, columns). |
required |
bounding_box
|
list
|
List of coordinates 'min_row', 'min_col', 'max_row', 'max_col'. |
required |
pad_width
|
int
|
Cells to pad arrays by. |
required |
Returns:
| Type | Description |
|---|---|
list
|
List of padded coordinates. |
Source code in topostats\tracing\disordered_tracing.py
segment_heights(row: pd.Series, skan_skeleton: skan.Skeleton, image: npt.NDArray) -> npt.NDArray
Obtain an ordered list of heights from the skan defined skeleton segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
Series
|
A row from the Skan summarize dataframe. |
required |
skan_skeleton
|
Skeleton
|
The graphical representation of the skeleton produced by Skan. |
required |
image
|
NDArray
|
The image the skeleton was produced from. |
required |
Returns:
| Type | Description |
|---|---|
NDArray
|
Heights along the segment, naturally ordered by Skan. |
Source code in topostats\tracing\disordered_tracing.py
segment_middles(row: pd.Series, skan_skeleton: skan.csr.Skeleton, image: npt.NDArray) -> float
Obtain the pixel value in the middle of the ordered segment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
Series
|
A row from the Skan summarize dataframe. |
required |
skan_skeleton
|
Skeleton
|
The graphical representation of the skeleton produced by Skan. |
required |
image
|
NDArray
|
The image the skeleton was produced from. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The single or mean pixel value corresponding to the middle coordinate(s) of the segment. |
Source code in topostats\tracing\disordered_tracing.py
trace_image_disordered(topostats_object: TopoStats, class_index: int, min_skeleton_size: int, mask_smoothing_params: dict, skeletonisation_params: dict, pruning_params: dict) -> None
Processor function for tracing image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
topostats_object
|
TopoStats
|
TopoStats object post ''Grains'' so that there are ''GrainCrops'' nested within. |
required |
class_index
|
int
|
Index of the class to trace. |
required |
min_skeleton_size
|
int
|
Minimum size of grain in pixels after skeletonisation. |
required |
mask_smoothing_params
|
dict
|
Dictionary of parameters to smooth the grain mask for better quality skeletonisation results. Contains a gaussian 'sigma' and number of dilation iterations. |
required |
skeletonisation_params
|
dict
|
Dictionary of options for skeletonisation, options are 'zhang' (scikit-image) / 'lee' (scikit-image) / 'thin' (scikitimage) or 'topostats' (original TopoStats method). |
required |
pruning_params
|
dict
|
Dictionary of options for pruning. |
required |
Source code in topostats\tracing\disordered_tracing.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 | |