Metadata-Version: 2.4
Name: histoseg
Version: 0.1.9.3
Summary: Python toolkit for H&E image analysis, spatial contour analysis, and 3D contour reconstruction.
Author-email: Taobo Hu <taobo.hu@scilifelab.se>
License: Required Notice: Copyright (c) 2025 SPATHO AB.
        Required Notice: Copyright (c) 2025 Mengping Long, Taobo Hu, Mats Nilsson.
        Required Notice: Any commercial use requires a separate commercial license from SPATHO AB.
        
        # PolyForm Noncommercial License 1.0.0
        
        <https://polyformproject.org/licenses/noncommercial/1.0.0>
        
        ## Acceptance
        
        In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
        
        ## Copyright License
        
        The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
        
        ## Distribution License
        
        The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
        
        ## Notices
        
        You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
        
        > Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
        
        ## Changes and New Works License
        
        The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
        
        ## Patent License
        
        The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
        
        ## Noncommercial Purposes
        
        Any noncommercial purpose is a permitted purpose.
        
        ## Personal Uses
        
        Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
        
        ## Noncommercial Organizations
        
        Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
        
        ## Fair Use
        
        You may have "fair use" rights for the software under the law. These terms do not limit them.
        
        ## No Other Rights
        
        These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
        
        ## Patent Defense
        
        If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
        
        ## Violations
        
        The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
        
        ## No Liability
        
        As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
        
        ## Definitions
        
        The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
        
        **You** refers to the individual or entity agreeing to these terms.
        
        **Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
        
        **Your licenses** are all the licenses granted to you for the software under these terms.
        
        **Use** means anything you do with the software requiring one of your licenses.
        
Project-URL: Homepage, https://github.com/hutaobo/HistoSeg
Project-URL: Documentation, https://histoseg.readthedocs.io
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: pandas
Requires-Dist: pyarrow
Requires-Dist: scipy
Requires-Dist: scikit-image
Requires-Dist: scikit-learn
Requires-Dist: seaborn
Requires-Dist: shapely
Requires-Dist: matplotlib
Requires-Dist: networkx
Requires-Dist: openai>=1.0.0
Provides-Extra: he
Requires-Dist: torch; extra == "he"
Requires-Dist: transformers; extra == "he"
Requires-Dist: accelerate; extra == "he"
Requires-Dist: pillow; extra == "he"
Requires-Dist: opencv-python-headless; extra == "he"
Requires-Dist: rasterio; extra == "he"
Dynamic: license-file

<div align="center">

# HistoSeg

<p align="center">
  <a href="https://pypi.org/project/histoseg/"><img alt="PyPI" src="https://img.shields.io/pypi/v/histoseg.svg"></a>
  <a href="https://histoseg.readthedocs.io/en/latest/"><img alt="Docs" src="https://readthedocs.org/projects/histoseg/badge/?version=latest"></a>
  <a href="https://github.com/hutaobo/HistoSeg/actions/workflows/publish.yml"><img alt="Publish to PyPI" src="https://github.com/hutaobo/HistoSeg/actions/workflows/publish.yml/badge.svg?branch=main"></a>
  <a href="https://polyformproject.org/licenses/noncommercial/1.0.0/"><img alt="License: PolyForm Noncommercial 1.0.0" src="https://img.shields.io/badge/License-PolyForm--Noncommercial%201.0.0-blue.svg"></a>
</p>

**Python toolkit for H&E image analysis, spatial contour analysis, and 3D contour reconstruction.**

</div>

HistoSeg has three public feature groups:

- **HE Analysis** (`histoseg.he`) for image-based H&E tissue segmentation, neutral tissue partitioning, and aligned-image change detection.
- **Contour Analysis** (`histoseg.contour`) for contour extraction from spatial/cell-coordinate data, including Pattern1 isolines and multi-structure Xenium exports.
- **3D Analysis** (`histoseg.threed`) for same-sample, multi-slice Xenium contour alignment and reconstruction workflows.

Full documentation: [histoseg.readthedocs.io](https://histoseg.readthedocs.io)

## When To Use Each Feature Group

Use **HE Analysis** when your input is an H&E image such as PNG, JPG, TIFF, or GeoTIFF and you want masks, overlays, heatmaps, GeoJSON polygons, or region tables.

Use **Contour Analysis** when your input is spatial cell-coordinate data such as Xenium `cells.parquet` plus cluster assignments, and you want geometry extracted from cell neighborhoods or selected cluster groups.

Use **3D Analysis** when you are preparing for multi-slice Xenium contour reconstruction from the same sample. The first released workflow soft-aligns a hard-aligned moving contour GeoJSON to a fixed reference slice with a conservative TPS displacement field.

## Installation

```bash
pip install -U histoseg
```

For local Hugging Face MedSAM-backed HE segmentation:

```bash
pip install -U "histoseg[he]"
```

For development:

```bash
git clone https://github.com/hutaobo/HistoSeg.git
cd HistoSeg
pip install -U pip
pip install -e ".[he]"
```

## HE Analysis Quickstart

```python
from histoseg.he import HESegmentationConfig, run_he_segmentation

result = run_he_segmentation(
    HESegmentationConfig(
        image="/path/to/he.png",
        out_dir="outputs/he_all_elements",
        task="all_elements",
        backend="heuristic",
        n_components=6,
    )
)

print(result.overlay_png)
print(result.geojson)
```

```bash
histoseg-he all-elements \
  --image /path/to/he.png \
  --out-dir outputs/he_all_elements \
  --backend heuristic
```

HE Analysis currently supports:

- `single`: tissue foreground extraction, or user-prompted region extraction from boxes/points
- `all_elements`: neutral tissue component partitioning (`component_1`, `component_2`, ...)
- `change`: aligned before/after H&E change detection

## Contour Analysis Quickstart

```python
from histoseg.contour import Pattern1IsolineConfig, run_pattern1_isoline

cfg = Pattern1IsolineConfig(
    clusters_csv="/path/to/clusters.csv",
    cells_parquet="/path/to/cells.parquet",
    tissue_boundary_csv="/path/to/tissue_boundary.csv",
    out_dir="outputs/pattern1_isoline0p5",
    pattern1_clusters=(10, 23, 19, 27, 14, 20, 25, 26),
)

result = run_pattern1_isoline(cfg)
print(result.preview_png)
print(len(result.contours))
```

```bash
histoseg-contour pattern1 \
  --clusters-csv clusters.csv \
  --cells-parquet cells.parquet \
  --out-dir outputs/pattern1 \
  --pattern1-clusters 10,23,19
```

Contour Analysis currently supports:

- Pattern1 isoline contour generation from clustered cell coordinates
- multi-structure contour partitioning
- Xenium Explorer annotation exports
- Hugging Face dataset helper workflows for Xenium-style inputs

## 3D Analysis Quickstart

```python
from histoseg.threed import (
    ThreeDContourReconstructionConfig,
    run_3d_contour_reconstruction,
)

cfg = ThreeDContourReconstructionConfig(
    fixed_geojson="slice_01.geojson",
    moving_hard_aligned_geojson="slice_02_hard_aligned_to_01.geojson",
    out_dir="outputs/3d_soft_alignment",
    group_property="structure",
    diagnostic_structure="Structure 5",
)

result = run_3d_contour_reconstruction(cfg)
print(result.soft_aligned_geojson)
print(result.diagnostic_report_png)
```

```bash
histoseg-3d reconstruct \
  --fixed-geojson slice_01.geojson \
  --moving-hard-aligned-geojson slice_02_hard_aligned_to_01.geojson \
  --out-dir outputs/3d_soft_alignment \
  --group-property structure \
  --diagnostic-structure "Structure 5"
```

3D Analysis currently performs the soft TPS registration/pre-reconstruction step. It does not yet generate a 3D mesh or volume.

## Outputs

HistoSeg workflows write reviewable artifacts such as:

- PNG previews and overlays
- label maps and heatmaps
- GeoJSON polygons
- CSV/Parquet region or contour tables
- `params.json` and `metrics.json` provenance files

## Documentation

- [HE Analysis](https://histoseg.readthedocs.io/en/latest/he_analysis.html)
- [Contour Analysis](https://histoseg.readthedocs.io/en/latest/contour_analysis.html)
- [3D Analysis](https://histoseg.readthedocs.io/en/latest/3d_analysis.html)
- [3D contour soft alignment tutorial](https://histoseg.readthedocs.io/en/latest/tutorials/3d_soft_alignment.html)
- [API Reference](https://histoseg.readthedocs.io/en/latest/api/index.html)

## License

This project is distributed under the **PolyForm Noncommercial 1.0.0** license.
Academic and other noncommercial use is permitted.
Any commercial use requires a separate commercial license from **SPATHO AB**.
See [LICENSE](LICENSE) for details.
