Metadata-Version: 2.5
Name: wsidicomizer
Version: 0.32.0
Summary: Tool for reading WSI files from proprietary formats and optionally convert them to to DICOM
Project-URL: Repository, https://github.com/imi-bigpicture/wsidicomizer
Author-email: Erik O Gabrielsson <erik.o.gabrielsson@sectra.com>
License: Apache-2.0
License-File: LICENCE
Keywords: converter,dicom,digital pathology,whole slide image
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Healthcare Industry
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Requires-Python: >=3.12
Requires-Dist: click<9.0.0,>=8.3.0
Requires-Dist: czifile>=2026.3.12
Requires-Dist: numpy>=1.22.0
Requires-Dist: opentile<0.28.0,>=0.27.0
Requires-Dist: pydicom>=3.0.0
Requires-Dist: tiffslide<5.0.0,>=4.0.0
Requires-Dist: universal-pathlib<1.0.0,>=0.3.0
Requires-Dist: wsidicom<0.38.0,>=0.37.0
Provides-Extra: bioformats
Requires-Dist: ome-types<0.8.0,>=0.5.0; extra == 'bioformats'
Requires-Dist: scyjava<2.0.0,>=1.8.1; extra == 'bioformats'
Provides-Extra: imagecodecs
Requires-Dist: imagecodecs>=2026.11.11; extra == 'imagecodecs'
Provides-Extra: isyntax
Requires-Dist: pyisyntax<0.2.0,>=0.1.2; extra == 'isyntax'
Provides-Extra: kernels
Requires-Dist: wsidicom[kernels]<0.38.0,>=0.37.0; extra == 'kernels'
Provides-Extra: opencv
Requires-Dist: opencv-python-headless>=4.5.0; extra == 'opencv'
Provides-Extra: openslide
Requires-Dist: openslide-bin<5.0.0,>=4.0.1; extra == 'openslide'
Requires-Dist: openslide-python<2.0.0,>=1.4.6; extra == 'openslide'
Provides-Extra: pyjpegls
Requires-Dist: pyjpegls<2.0.0,>=1.2.0; extra == 'pyjpegls'
Provides-Extra: pylibjpeg-openjpeg
Requires-Dist: pylibjpeg-openjpeg<3.0.0,>=2.1.1; extra == 'pylibjpeg-openjpeg'
Provides-Extra: pylibjpeg-rle
Requires-Dist: pylibjpeg-rle<3.0.0,>=2.0.0; extra == 'pylibjpeg-rle'
Description-Content-Type: text/markdown

# *wsidicomizer*

*wsidicomizer* is a Python library for opening WSIs in proprietary formats and optionally convert them to DICOM. The aims of the project are:

- Provide read support for various proprietary formats.
- Provide lossless conversion for files supported by opentile.
- Provide 'as good as possible' conversion for other formats.
- Simplify the encoding of WSI metadata into DICOM.

## Supported formats

*wsidicomizer* currently supports the following formats:

- Aperio svs (lossless)
- Hamamatsu ndpi (lossless)
- Philips tiff (lossless)
- Zeiss czi (lossy)
- 3DHISTECH mrxs, brightfield and fluorescence (lossy)
- Trestle tif (lossless base level where opentile re-grids it, otherwise lossy)
- Ventana bif (lossy), and already-stitched Ventana tif (lossless base level)
- Olympus vsi (lossless)
- Optional: Formats supported by Bioformats (lossy)

The native mrxs reader composes the overlapping camera tiles. Slides it can not read (reduced pyramids, more than three fluorescence channels, tiles without recorded positions) are passed to the `openslide` extra, if installed.

Olympus vsi files with several scanned areas, channels, focal planes or time points, or with uncompressed tiles, are not supported.

With the `openslide` extra the following formats are also supported:

- Leica scn (lossy)
- Sakura svslide (lossy)
- Hamamatsu vms, vmu (lossy)

The `bioformats` extra by default enables lossy support for the [BSD-licensed Bioformat formats](https://docs.openmicroscopy.org/bio-formats/8.0.1/supported-formats.html).

The `isyntax` extra enables lossy single-thread support for isynax files.

For czi, isyntax, mrxs, Trestle and Ventana only the base level is read from the file. The lower-resolution levels are instead created from the base level, which `convert()` does by default. `WsiDicomizer.open()` gives the base level only. Trestle and Ventana bif tiles overlap their neighbours; tiles opentile can not serve as a plain grid are composed into non-overlapping tiles and encoded anew.

## Installation

***Install wsidicomizer from pypi***

```console
pip install wsidicomizer
```

***Alternative install wsidicomizer from conda***

```console
conda install -c conda-forge wsidicomizer
```

See [Openslide support](#openslide-support) and [Bioformats support](#bioformats-support) for how to install optional extras.

For faster pyramid generation, install the `kernels` extra, which installs wsidicom-kernels:

```console
pip install wsidicomizer[kernels]
```

## Important note

Please note that this is an early release and the API is not frozen yet. Function names and functionality is prone to change.

## Requirements

*wsidicomizer* requires python >=3.12 and uses numpy, pydicom, wsidicom, opentile, tiffslide, czifile, click, and universal-pathlib.

## Basic cli-usage

***Convert a wsi-file into DICOM using cli-interface***

```console
wsidicomizer -i 'path_to_wsi_file' -o 'path_to_output_folder'
```

### Options

Run `wsidicomizer --help` for the options of the cli.

Using the no-confidential-flag properties according to [DICOM Basic Confidentiality Profile](https://dicom.nema.org/medical/dicom/current/output/html/part15.html#table_E.1-1) are not included in the output file. Properties otherwise included are currently:

- Acquisition DateTime
- Device Serial Number

## Basic usage

***Create metadata (Optional)***

```python
from wsidicom.conceptcode import (
    AnatomicPathologySpecimenTypesCode,
    ContainerTypeCode,
    SpecimenCollectionProcedureCode,
    SpecimenEmbeddingMediaCode,
    SpecimenFixativesCode,
    SpecimenSamplingProcedureCode,
    SpecimenStainsCode,
)
from wsidicom.metadata import (
    Collection,
    Embedding,
    Equipment,
    Fixation,
    Label,
    Patient,
    Sample,
    Series,
    Slide,
    SlideSample,
    Specimen,
    Staining,
    Study,
)
from wsidicomizer.metadata import WsiDicomizerMetadata

study = Study(identifier="Study identifier")
series = Series(number=1)
patient = Patient(name="FamilyName^GivenName")
label = Label(text="Label text")
equipment = Equipment(
    manufacturer="Scanner manufacturer",
    model_name="Scanner model name",
    device_serial_number="Scanner serial number",
    software_versions=["Scanner software versions"],
)

specimen = Specimen(
    identifier="Specimen",
    extraction_step=Collection(method=SpecimenCollectionProcedureCode("Excision")),
    type=AnatomicPathologySpecimenTypesCode("Gross specimen"),
    container=ContainerTypeCode("Specimen container"),
    steps=[Fixation(fixative=SpecimenFixativesCode("Neutral Buffered Formalin"))],
)

block = Sample(
    identifier="Block",
    sampled_from=[specimen.sample(method=SpecimenSamplingProcedureCode("Dissection"))],
    type=AnatomicPathologySpecimenTypesCode("tissue specimen"),
    container=ContainerTypeCode("Tissue cassette"),
    steps=[Embedding(medium=SpecimenEmbeddingMediaCode("Paraffin wax"))],
)

slide_sample = SlideSample(
    identifier="Slide sample",
    sampled_from=block.sample(method=SpecimenSamplingProcedureCode("Block sectioning")),
)

slide = Slide(
    identifier="Slide",
    stainings=[
        Staining(
            substances=[
                SpecimenStainsCode("hematoxylin stain"),
                SpecimenStainsCode("water soluble eosin stain"),
            ]
        )
    ],
    samples=[slide_sample],
)
metadata = WsiDicomizerMetadata(
    study=study,
    series=series,
    patient=patient,
    equipment=equipment,
    slide=slide,
    label=label,
)
```

***Convert a wsi-file into DICOM using python-interface***

```python
from wsidicomizer import WsiDicomizer

created_files = WsiDicomizer.convert(
    filepath=path_to_wsi_file,
    output_path=path_to_output_folder,
    metadata=metadata,
    tile_size=tile_size,
)
```

`convert()` returns the created files as a dictionary from path to a description of the file, as written. A pyramid level is described by a `WsiLevelFile`, a thumbnail by a `WsiThumbnailFile`, and a label or overview by a `WsiAssociatedFile`, all from `wsidicom`. For example, to find the label that was written:

```python
from wsidicom.metadata import ImageType

labels = [
    path
    for path, created_file in created_files.items()
    if created_file.image_type == ImageType.LABEL
]
```

***Import a wsi file as a WsiDicom object.***

```python
from wsidicomizer import WsiDicomizer

wsi = WsiDicomizer.open(path_to_wsi_file)
region = wsi.read_region((1000, 1000), 6, (200, 200))
wsi.close()
```

***Configure a single open or conversion with settings.***

`open()` and `convert()` take a keyword-only `settings` argument used for that call instead of the process-wide default. The wsidicomizer `Settings` extends the wsidicom `Settings` — so every wsidicom setting (e.g. `pyramid_resampling_filter`) is available alongside wsidicomizer's own (e.g. `default_tile_size`) — and nests per-source settings such as `opentile`:

```python
from wsidicomizer import Settings, WsiDicomizer
from opentile.config import Settings as OpenTileSettings

wsi = WsiDicomizer.open(
    path_to_wsi_file,
    settings=Settings(
        strict_uid_check=True,  # wsidicom setting
        pyramid_resampling_filter="box",  # wsidicom setting
        default_tile_size=1024,  # wsidicomizer setting
        opentile=OpenTileSettings(ndpi_frame_cache=64),  # opentile source setting
    ),
)
```

To change the process-wide default instead, use `set_default_settings(Settings(...))` (it also updates the wsidicom and opentile defaults the `Settings` carries).

## Metadata handling

The `open()` and `convert()` methods of `WsiDicomizer` takes three parameters that are important for inserting additional metadata into the DICOM dataset of the converted image:

- `metadata`
- `default_metadata`
- `metadata_post_processor`

### Metadata merging

When creating the DICOM dataset, the metadata provided in the `metadata` and `default_metadata` parameters are merged with metadata that is parsed from the source image file, with the following descending preference:

1. Metadata from the `metadata` parameter
2. Metadata from the source image
3. Metadata from the `default_metadata` parameter

For example:

- `equipment` in the `metadata`-parameter metadata will override the `equipment` metadata from the source image (if present).
- `optical_paths` in the `default_metadata`-parameter metadata will be overridden by any `optical_paths` present in the `metadata` parameter metadata or source image metadata.

Note that merging is also performed on nested metadata, e.g. `focus_method` in an `Image` can be merged from the different sources.

### Metadata post processing

After the metadata merge a pydicom `Dataset` is created from the result. Additional post processing can be performed using the `metadata_post_processor` parameter. This can be another `Dataset`, in which case the merged dataset is updated with (i.e. overwritten by) the provided dataset:

```python
from pydicom import Dataset

dataset = Dataset()
dataset.PatientAge = "042Y"

WsiDicomizer.convert(
    filepath=path_to_wsi_file,
    output_path=path_to_output_folder,
    metadata_post_processor=dataset,
)
```

For more complex processing a callback function that takes the merged `Dataset` and `WsiMetadata` as parameters and returns an updated `Dataset` can be used:

```python
from pydicom import Dataset
from wsidicom.metadata import WsiMetadata


def metadata_post_processor(dataset: Dataset, metadata: WsiMetadata) -> Dataset:
    dataset.PatientAge = "042Y"
    return dataset


WsiDicomizer.convert(
    filepath=path_to_wsi_file,
    output_path=path_to_output_folder,
    metadata_post_processor=metadata_post_processor,
)
```

### JSON metadata

WsiDicom provides methods for serializing and deserialising metadata to and from JSON. This is useful for example for providing metadata when performing conversion using the cli. As there is not yet any documentation on the JSON schema, the simplest way to produce metadata in the JSON-format is to first construct it in Python and then calling the provided serializer:

```python
import json
from wsidicom.metadata.schema.json import WsiMetadataJsonSchema

metadata = WsiDicomizerMetadata(
    study=study,
    series=series,
    patient=patient,
    equipment=equipment,
    slide=slide,
    label=label,
)
with open("metadata.json", "w") as f:
    json.dump(WsiMetadataJsonSchema().dump(metadata), f, indent=4)
```

## Openslide support

### Installation

Support for reading images using Openslide c library can optionally be enabled by installing *wsidicomizer* with the `openslide` extra:

```console
pip install wsidicomizer[openslide]
```

The extra also installs openslide-bin, which provides the OpenSlide library, so it does not need to be installed separately. Other ways to install OpenSlide are described at <https://openslide.org/download/>.

## Bioformats support

### Installation

Support for reading images using Bioformats java library can optionally be enabled by installing *wsidicomizer* with the `bioformats` extra:

```console
pip install wsidicomizer[bioformats]
```

The `bioformats` extra enables usage of the `bioformats` module.The required Bioformats java library (jar-file) is downloaded automatically when the module is imported using [scyjava](https://github.com/scijava/scyjava).

### Using

As the Bioformats library is a java library it needs to run in a java virtual machine (JVM). A JVM is started automatically when the `bioformats` module is imported. The JVM can´t be restarted in the same Python inteprenter, and is therefore left running once started. If you want to shutdown the JVM (without closing the Python inteprenter) you can call the shutdown_jvm()-method:

```python
import scyjava

scyjava.shutdown_jvm()
```

Due to the need to start a JVM, the `bioformats` module is not imported when using the default `WsiDicomzer`-class unless `SourceIdentifier.BIOFORMATS` is used as preferred_source:

```python
from wsidicomizer import SourceIdentifier, WsiDicomizer

with WsiDicomizer("input file", preferred_source=SourceIdentifier.BIOFORMASTS) as wsi:
    ...
```

### Bioformats version

The Bioformats java library is available in two versions, one with BSD and one with GPL2 license, and can read several [WSI formats](https://bio-formats.readthedocs.io/en/v8.3.0/supported-formats.html). However, most formats are only available in the GPL2 version. Due to the licensing incompatibility between Apache 2.0 and GPL2, *wsidicomizer* is distributed with a default setting of using the BSD licensed library. The loaded Biformats version can be changed by the user by setting the `BIOFORMATS_VERSION` environmental variable from the default value `bsd:8.3.0`.

## Limitations

Files with z-stacks or multiple focal paths are currently fully not supported.

## Other DICOM python tools

- [pydicom](https://pydicom.github.io/)
- [highdicom](https://github.com/MGHComputationalPathology/highdicom)
- [wsidicom](https://github.com/imi-bigpicture/wsidicom)

## Contributing

We welcome any contributions to help improve this tool for the WSI DICOM community!

We recommend first creating an issue before creating potential contributions to check that the contribution is in line with the goals of the project. To submit your contribution, please issue a pull request on the imi-bigpicture/wsidicomizer repository with your changes for review.

Our aim is to provide constructive and positive code reviews for all submissions. The project relies on gradual typing and roughly follows PEP8. However, we are not dogmatic. Most important is that the code is easy to read and understand.

## Acknowledgement

*wsidicomizer*: Copyright 2021 Sectra AB, licensed under Apache 2.0.

This project is part of a project that has received funding from the Innovative Medicines Initiative 2 Joint Undertaking under grant agreement No 945358. This Joint Undertaking receives support from the European Union’s Horizon 2020 research and innovation programme and EFPIA. IMI website: <www.imi.europa.eu>
