imi-bigpicture / wsidicomizer

Python library for converting WSI files to DICOM
Apache License 2.0
54 stars 7 forks source link

Handling of missing properties #89

Closed ChristianMarzahl closed 7 months ago

ChristianMarzahl commented 11 months ago

Dear authors,

Thank you very much for this helpful repository.

Observation

The conversion throws an exception if the PROPERTY_NAME_MPP_X value is not set in a tiff file.

    created_files = WsiDicomizer.convert(
                    ^^^^^^^^^^^^^^^^^^^^^
  /python3.11/site-packages/wsidicomizer/wsidicomizer.py", line 215, in convert
    with cls.open(
         ^^^^^^^^^
/python3.11/site-packages/wsidicomizer/wsidicomizer.py", line 137, in open
    return cls(source, label)
           ^^^^^^^^^^^^^^^^^^
/python3.11/site-packages/wsidicom/wsidicom.py", line 82, in __init__
    self._levels = Levels.open(source.level_instances)
                               ^^^^^^^^^^^^^^^^^^^^^^
/python3.11/site-packages/wsidicomizer/dicomizer_source.py", line 121, in level_instances
    return [
           ^
/python3.11/site-packages/wsidicomizer/dicomizer_source.py", line 123, in <listcomp>
    self._create_level_image_data(level_index),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/python3.11/site-packages/wsidicomizer/sources/tiffslide/tiffslide_source.py", line 93, in _create_level_image_data
    return TiffSlideLevelImageData(
           ^^^^^^^^^^^^^^^^^^^^^^^^
/python3.11/site-packages/wsidicomizer/sources/tiffslide/tiffslide_image_data.py", line 200, in __init__
    base_mpp_x = float(self._slide.properties[PROPERTY_NAME_MPP_X])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: float() argument must be a string or a real number, not 'NoneType'

Environment

OS: Linux (Ubuntu) 
22.04

wsidicomizer 0.10.2

Possible Solution

Could you set a default value for MPP to enable the conversion with missing values? https://github.com/imi-bigpicture/wsidicomizer/blob/539d18ae1596dd70c6fc1982a2267213cb027b3a/wsidicomizer/sources/tiffslide/tiffslide_image_data.py#L200

 base_mpp_x = float(self._slide.properties.get("PROPERTY_NAME_MPP_X", 1.0)

With kind regards, Christian

erikogabrielsson commented 11 months ago

Hi @ChristianMarzahl and thanks for reporting this issue.

I have worked on a more flexible interface for providing metadata, were one can specify metadata that should override the metadata from the image file as well as metadata that should be used as default if no other metadata is available. This might be worth putting into that.

I dont now what the source file format is (if it is possible, please share the file)? As it is opened by the tiffslide reader, is it supported by tiffslide.?

ChristianMarzahl commented 11 months ago

Dear @erikogabrielsson,

First of all, thank you very much for your quick response. The file was created with the Philips SDK and was converted from .isyntax to TIFF. Unfortunately, I can't share the file, but I will try to create one I can share. It looks like the SDK is not storing any meta information in the resulting tiff file.

Regarding tiffslide: Yes, the Python package can open the WSI and extract tiles. image

Your flexible interface for setting meta data sounds excellent.

erikogabrielsson commented 10 months ago

Ho @ChristianMarzahl,

The development branches for adding more metadata functionality to wsidicom and wsidicomizer are here and here. With these changes, one can specify a base pixel spacing (either overriding or defaulting), see test.

If you are able to share a test image for from your converter I can test if this works. Otherwise you are free to test it yourself. There is still some work needed before this is released.

ChristianMarzahl commented 9 months ago

Dear @erikogabrielsson,

Thank you for looking into this. It took a little bit longer to figure out that the attached file is anonymous enough to act as a demo I can provide to you: https://drive.google.com/file/d/1nPwvexvbm-2f3rOzsLdNkkumGe-vhVxV/view?usp=sharing

The WSI was converted with Philips SDK from .isyntax to TIFF. We saw this exception with all the examples.

With kind regards, Christian

erikogabrielsson commented 9 months ago

Thanks for sharing @ChristianMarzahl. I will look into this as soon as possible.

Is the philips converter avaiable somewhere?

ChristianMarzahl commented 8 months ago

Dear @erikogabrielsson,

Yes: https://www.usa.philips.com/healthcare/sites/pathology/about/sdk

By the way. Thank you very much for updating the wsidicomizer.

With kind regards, Christian

erikogabrielsson commented 8 months ago

Hi @ChristianMarzahl

Short update on this. Using the new metadata feature in wsidicom and wsidicomizer one can insert missing or override properties, such as pixel spacing:

from wsidicom.geometry import SizeMm
from wsidicom.metadata.image import Image
from wsidicomizer import WsiDicomizer
from wsidicomizer.metadata import WsiDicomizerMetadata

input_file = Path(r'C:\research_data\ISyntaxToTiff.tiff')

image =  Image(pixel_spacing=SizeMm(0.00025, 0.00025))
metadata = WsiDicomizerMetadata(image=image)

with WsiDicomizer.open(input_file, default_metadata=metadata) as wsi:
    print(f"wsi of size: {wsi.mm_size} and pixel spacing {wsi.pixel_spacing}")
    region = wsi.read_region_mm((0, 8), 6, (10, 10))
region

And get:

wsi of size: SizeMm(width=24.064, height=22.784) and pixel spacing SizeMm(width=0.00025, height=0.00025)

image

This is unfortunately not ready for release, as Im waiting for highdicom to support Python 3.12. You can however test the feature by installing from git:

erikogabrielsson commented 7 months ago

Fixed in 0.12.0. Metadata such as pixel spacing can now be specified if missing.