Skip to content

Commit

Permalink
Dicomdir (#127)
Browse files Browse the repository at this point in the history
* Add support for opening files in dicomdir
  • Loading branch information
erikogabrielsson authored Nov 23, 2023
1 parent 2cac168 commit 83e939b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Support for opening DICOMDIR files using `open_dicomdir()`.

## [0.13.0] - 2023-11-11

### Added
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ from wsidicom import WsiDicom
slide = WsiDicom.open([file_stream_1, file_stream_2, ... ])
```

***Or load a WSI dataset from a DICOMDIR.***

```python
from wsidicom import WsiDicom

slide = WsiDicom.open_dicomdir(path_to_dicom_dir)
```

***Or load a WSI dataset from DICOMWeb.***

```python
Expand Down
31 changes: 27 additions & 4 deletions wsidicom/file/wsidicom_file_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@

"""A source for reading WSI DICOM files."""

from collections import defaultdict
import io
import logging
from collections import defaultdict
from pathlib import Path
from typing import BinaryIO, Dict, Iterable, List, Optional, Tuple, Union

from pydicom.uid import UID
from pydicom import dcmread
from pydicom.errors import InvalidDicomError
from pydicom.filereader import _read_file_meta_info, read_preamble
from pydicom.fileset import FileSet
from pydicom.uid import UID, MediaStorageDirectoryStorage

from wsidicom.errors import (
WsiDicomNotFoundError,
Expand All @@ -33,8 +37,6 @@
from wsidicom.instance import ImageType, TileType, WsiDataset, WsiInstance
from wsidicom.source import Source
from wsidicom.uid import ANN_SOP_CLASS_UID, WSI_SOP_CLASS_UID, SlideUids
from pydicom.filereader import read_preamble, _read_file_meta_info
from pydicom.errors import InvalidDicomError


class WsiDicomFileSource(Source):
Expand Down Expand Up @@ -160,6 +162,27 @@ def contains_levels(self) -> bool:
"""Returns true source has one level that can be read with WsiDicom."""
return len(self.image_files) > 0

@classmethod
def open_dicomdir(cls, path: Union[str, Path]):
"""Open a DICOMDIR file and return a WsiDicomFileSource for contained files.
Parameters
----------
path: Union[str, Path]
Path to DICOMDIR file.
Returns
----------
WsiDicomFileSource
Source for files in DICOMDIR.
"""
dicomdir = dcmread(path)
if dicomdir.file_meta.MediaStorageSOPClassUID != MediaStorageDirectoryStorage:
raise ValueError()
fileset = FileSet(dicomdir)
files = [file.path for file in fileset]
return cls(files)

@staticmethod
def _open_file(file: Union[Path, BinaryIO]) -> Tuple[BinaryIO, Optional[Path]]:
"""Open stream if file is path. Return stream and optional filepath."""
Expand Down
27 changes: 27 additions & 0 deletions wsidicom/wsidicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,33 @@ def open_web(
)
return cls(source, label, True)

@classmethod
def open_dicomdir(
cls, path: Union[str, Path], label: Optional[Union[PILImage, str, Path]] = None
) -> "WsiDicom":
"""Open WSI DICOM files in DICOMDIR and return a WsiDicom object.
Parameters
----------
path: Union[str, Path]
Path to DICOMDIR file or directory with a DICOMDIR file.
label: Optional[Union[PILImage, str, Path]] = None
Optional label image to use instead of label found in path.
Returns
----------
WsiDicom
WsiDicom created from WSI DICOM files in DICOMDIR.
"""
if isinstance(path, str):
path = Path(path)
if path.is_dir():
path = path.joinpath("DICOMDIR")
if not path.is_file() or not path.exists():
raise FileNotFoundError(f"DICOMDIR file {path} not found.")
source = WsiDicomFileSource.open_dicomdir(path)
return cls(source, label, True)

def __enter__(self):
return self

Expand Down

0 comments on commit 83e939b

Please sign in to comment.