Skip to main content

Python behavioral core port of the DeProj MATLAB toolbox

Project description

DeProjPy

deprojpy is a Python implementation of the core matlab project DeProj workflow: deproject segmented epithelial cell contours onto a height-map surface, compute 3D cell geometry, and estimate distances on the deprojected surface.

The goal is not GUI parity with MATLAB. The focus is a scriptable Python API for deprojection, feature measurement, plotting, and surface-distance calculations.

Installation

Install from PyPI:

python -m pip install deprojpy

For development, clone the repository and install it in editable mode:

git clone https://github.com/zen-laboratory/DeProjPy.git
cd DeProjPy
python -m pip install -e ".[test]"
python -m pytest

The label-image workflow uses the published companion labelimage-tools package, which is installed automatically with DeProjPy. To install or upgrade it explicitly:

python -m pip install "labelimage-tools[all]"

Quick start: DeProj-style mask input

The MATLAB-style workflow starts from a binary-like segmentation mask and a height map. In the mask image, cell interiors are non-zero and cell borders are zero-valued ridges.

import deprojpy as dp

mask, heightmap = dp.load_tiff_pair("samples/Segmentation-2.tif", "samples/HeightMap-2.tif")
result = dp.from_heightmap(mask, heightmap,
    pixel_size=0.183, voxel_depth=1.0, units="µm",
    invert_z=True, inpaint_zeros=True, prune_zeros=True)

df = result.to_dataframe()
result.to_csv("measurements.csv")

Returned boundaries, centers, and junction centroids use geometric (x, y, z) order in physical units. Input images still use normal image indexing (row, column).

Plot a scalar feature on the deprojected cell polygons:

fig, ax = dp.plotting.plot_feature_map(result, "area")
fig, ax = dp.plotting.plot_heightmap_with_centers(heightmap, result)

Why deproject?

Measurements made directly on the 2D segmentation image can underestimate cell geometry when the tissue surface is curved or tilted. DeProjPy measures cell contours after lifting them onto the height-map surface, so quantities such as area and perimeter are reported in surface-corrected physical units.

A useful diagnostic is the relative area difference between the deprojected surface area and the original projected 2D area:

fig, ax = dp.plotting.plot_relative_error_map(result, 'area')

Positive values indicate cells whose surface-corrected area is larger than their projected 2D area. This is the geometric correction DeProj is designed to make visible and measurable.

Alternative input: labeled segmentations

DeProjPy is modernized compared to the Matlab version, as it can also start from an integer label image, where each cell has a unique label and background is zero. This is useful for outputs from segmentation pipelines that already return labels rather than DeProj-style ridge masks, such as FishFeats.

DeProj-style mask Label image
from_heightmap(mask, heightmap, ...) from_labels(labels, heightmap, ...)
Cell interiors are non-zero; borders are zero. All pixels in a cell share a unique integer label; background is zero.

import deprojpy as dp

labels, heightmap = dp.load_label_heightmap_pair("samples/Labels-2.tif", "samples/HeightMap-2.tif")
result = dp.from_labels(labels, heightmap,
    pixel_size=0.183, voxel_depth=1.0, units="µm",
    invert_z=True, inpaint_zeros=True, prune_zeros=True)

df = result.to_dataframe()

The label workflow returns the same kind of DeprojResult as the mask workflow. Original label IDs are preserved as source_label when available.

Surface distances

DeProjPy introduces new reusable tools for distances constrained to a surface.

There are two main distance ideas:

  • straight surface distance: sample the surface along the straight segment in xy and measure the lifted 3D polyline;
  • graph geodesic distance: build a sparse surface graph and compute shortest paths on the graph. This is an approximation to the continuous geodesic.

For plotting paths on the fitted DeProj cell-contour surface, build the calculator from cell boundaries:

import numpy as np
from deprojpy.surface_distance import SurfaceDistanceCalculator, SurfaceGraph

centers = df[["center_x", "center_y"]].to_numpy()

calc = SurfaceDistanceCalculator.from_cell_boundaries(result)
d_straight = calc.straight_distance(centers[10], centers[200], input_units="physical")

graph = SurfaceGraph.from_calculator(calc, step="auto", connectivity="16")
d_graph, path_px = graph.distance(centers[10], centers[200],
    input_units="physical", return_path=True)

For all-pairs straight surface distances between cell centers:

D = calc.straight_pairwise_distances(centers, input_units="physical")

Graph paths are returned in pixel coordinates, matching graph nodes. To plot them in 3D, sample the calculator surface and convert xy back to physical units:

path_z = calc.sample_height(path_px, input_units="pixel")
path_xyz = np.column_stack([path_px * result.pixel_size, path_z])

Use SurfaceDistanceCalculator.from_result(...) when you want distances on the prepared height-map surface. Use SurfaceDistanceCalculator.from_cell_boundaries(...) when you want paths and distances on the fitted surface represented by the deprojected cell contours.

What is in a result?

A DeprojResult contains:

  • result.epicells: one EpiCell per retained cell;
  • result.to_dataframe(): a tabular summary of cell features;
  • result.prepared_heightmap: the height map used for deprojection;
  • deprojected boundaries, centers, junctions, and geometry in physical units.

The most common workflow is:

result = dp.from_heightmap(mask, heightmap, ...)
df = result.to_dataframe()

or:

result = dp.from_labels(labels, heightmap, ...)
df = result.to_dataframe()

More examples

See docs/cookbook.md and the scripts in examples/ for copy-pastable workflows covering plotting, diagnostics, label inputs, exports, curvature maps, and surface distances.

Validation status

The Python implementation is tested with synthetic invariants and the original DeProj sample images. It is not yet certified against MATLAB golden outputs.

With the original sample files, expected smoke-check values include:

  • image shape (282, 508);
  • 426 retained cells;
  • finite positive cell areas and perimeters.

License

DeProjPy is distributed under the BSD 3-Clause license. It is partly a Python implementation/port of the core workflow from the original BSD-licensed MATLAB DeProj project. This package does not imply endorsement by the original authors or institutions.

Project details


Download files

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

Source Distribution

deprojpy-0.1.0.tar.gz (45.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deprojpy-0.1.0-py3-none-any.whl (43.2 kB view details)

Uploaded Python 3

File details

Details for the file deprojpy-0.1.0.tar.gz.

File metadata

  • Download URL: deprojpy-0.1.0.tar.gz
  • Upload date:
  • Size: 45.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for deprojpy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 03025b7e70cb44e6da9335b936f68de46c44526529c2e5d8ef50da4afc781777
MD5 7633f5f6fbcd46d4d2389e70cf187018
BLAKE2b-256 dabbf16654f481107ef4bc83aa438346a7434a3b88da6b0aa46890d21e13498f

See more details on using hashes here.

Provenance

The following attestation bundles were made for deprojpy-0.1.0.tar.gz:

Publisher: publish-pypi.yml on zen-laboratory/DeProjPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deprojpy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: deprojpy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 43.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for deprojpy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e1ec64e4466681894d2ba017e004d237109a0e03989027839bb7c289dd1b5a2
MD5 52cf5a7c61dbce06d259b7a728d717f9
BLAKE2b-256 d0cb739b268dc0d0af1e6294ed9127c06d66d49f50272a6e8228caca348fddf0

See more details on using hashes here.

Provenance

The following attestation bundles were made for deprojpy-0.1.0-py3-none-any.whl:

Publisher: publish-pypi.yml on zen-laboratory/DeProjPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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