Skip to main content

Python package for evaluating neuron segmentations in terms of the number of splits and merges

Project description

SkeletonMetrics

License Code Style

semantic-release: angular

Python package for assessing the accuracy of a predicted neuron segmentation by comparing it to a set of ground truth skeletons. This tool detects topological mistakes (i.e. splits and merges) in a predicted segmentation and then computes several skeleton-based metrics that quantify its topological accuracy.

Details

We begin with a set of ground truth skeletons stored as individual SWC files, where the "xyz" coordinates correspond to voxels in an image. Each ground truth skeleton is loaded and represented as a NetworkX graph with the voxel coordinates as a node-level attribute. The evaluation is performed by first labeling the nodes of each graph with the corresponding segment IDs from the predicted segmentation. Topological mistakes are then detected by examining the labels of each edge, see figure below for an overview of how splits and merges are detected.

Topological mistakes detected in skeleton
Figure: Edges in skeletons are either correctly or incorrectly reconstructed based on the presence of mergers or splits that affect nodes attached to an edge. Colors correspond to segment IDs. From top to bottom: correct edge (both nodes have the same ID), split edge (nodes assigned to different segments), omitted edge (one or two nodes do not have an associated ID), merged edge (node assigned to a segment that covers more than one skeleton).

Metrics computed for each ground truth skeleton:

  • Number of Splits: Number of segments that a ground truth skeleton is broken into.
  • Number of Merges: Number of segments that are incorrectly merged into a single segment.
  • Percentage of Omit Edges: Proportion of edges in the ground truth that are omitted in the predicted segmentation.
  • Percentage of Merged Edges: Proportion of edges that are merged in the predicted segmentation.
  • Edge Accuracy: Proportion of edges that are correctly reconstructed in the predicted segmentation.
  • Expected Run Length (ERL): Expected length of segments or edges in the predicted segmentation.

Usage

Here is a simple example of evaluating a predicted segmentation.

from tifffile import imread
from xlwt import Workbook

import numpy as np

from segmentation_skeleton_metrics.skeleton_metric import SkeletonMetric


def evaluate():
    # Initializations
    pred_labels = imread(pred_labels_path)
    skeleton_metric = SkeletonMetric(
        target_swcs_pointer,
        pred_labels,
        fragments_pointer=pred_swcs_pointer,
        output_dir=output_dir,
    )
    full_results, avg_results = skeleton_metric.run()

    # Report results
    print(f"Averaged Results...")
    for stat_name in avg_results.keys():
        print(f"   {stat_name}: {round(avg_results[stat_name], 4)}")

    print(f"\nTotal Results...")
    print("# splits:", np.sum(list(skeleton_metric.split_cnt.values())))
    print("# merges:", np.sum(list(skeleton_metric.merge_cnt.values())))


if __name__ == "__main__":
    # Initializations
    output_dir = "./"
    pred_labels_path = "./pred_labels.tif"
    pred_swcs_pointer = "./pred_swcs.zip"
    target_swcs_pointer = "./target_swcs.zip"

    # Run
    evaluate()

Note: this Python package can also be used to evaluate the accuracy of a segmentation in which split mistakes have been corrected.

Installation

To use the software, in the root directory, run

pip install -e .

To develop the code, run

pip install -e .[dev]

To install this package from PyPI, run

pip install aind-segmentation-evaluation

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

segmentation_skeleton_metrics-4.9.26.tar.gz (641.9 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file segmentation_skeleton_metrics-4.9.26.tar.gz.

File metadata

File hashes

Hashes for segmentation_skeleton_metrics-4.9.26.tar.gz
Algorithm Hash digest
SHA256 5f7e28e29427560f7e358da1f9ab7c24be4c841a692ad1c0e80b6d85631206ae
MD5 388fbe9f09f7511f2f5170497282d022
BLAKE2b-256 61a8e38f75ba56f1239b5777bead11d90943406059d10e0cf55d3e0e00a688a4

See more details on using hashes here.

File details

Details for the file segmentation_skeleton_metrics-4.9.26-py3-none-any.whl.

File metadata

File hashes

Hashes for segmentation_skeleton_metrics-4.9.26-py3-none-any.whl
Algorithm Hash digest
SHA256 62ad83a9bb4eed66376099e85230c3add233f5fb1b682bb1df75ae22c3070a87
MD5 3b00a956224c94d48ce91d067e8bb9ea
BLAKE2b-256 0e18eab0549a011e03451c548610f14ccd48a2067398385faea8f8dd652d4f4a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page