Skip to content

Commit

Permalink
Merge pull request #1442 from girder/dicom-multi-file-guard
Browse files Browse the repository at this point in the history
Don't scan as many files when checking for multi file dicom
  • Loading branch information
manthey authored Jan 23, 2024
2 parents 65eba30 + 2e167db commit b02a177
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### Improvements
- Read config values from the environment variables ([#1422](../../pull/1422))
- Optimizing when reading arrays rather than images from tiff files ([#1423](../../pull/1423))
- Better filter DICOM adjacent files to ensure they share series instance IDs ([#1424](../../pull/1424), [#1436](../../pull/1436))
- Better filter DICOM adjacent files to ensure they share series instance IDs ([#1424](../../pull/1424), [#1436](../../pull/1436), [#1442](../../pull/1442))
- Optimizing small getRegion calls and some tiff tile fetches ([#1427](../../pull/1427))
- Started adding python types to the core library ([#1432](../../pull/1432), [#1433](../../pull/1433), [#1437](../../pull/1437), [#1438](../../pull/1438), [#1439](../../pull/1439))
- Use parallelism in computing tile frames ([#1434](../../pull/1434))
Expand Down
7 changes: 7 additions & 0 deletions sources/dicom/large_image_source_dicom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def _lazyImportPydicom():

if pydicom is None:
import pydicom
return pydicom


def dicom_to_dict(ds, base=None):
Expand Down Expand Up @@ -138,6 +139,12 @@ def __init__(self, path, **kwargs):
if not os.path.isfile(path):
raise TileSourceFileNotFoundError(path) from None
root = os.path.dirname(path) or '.'
try:
_lazyImportPydicom()
pydicom.filereader.dcmread(path, stop_before_pixels=True)
except Exception as exc:
msg = f'File cannot be opened via dicom tile source ({exc}).'
raise TileSourceError(msg)
self._largeImagePath = [
os.path.join(root, entry) for entry in os.listdir(root)
if os.path.isfile(os.path.join(root, entry)) and
Expand Down
10 changes: 8 additions & 2 deletions sources/dicom/large_image_source_dicom/girder_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from girder.models.folder import Folder
from girder.models.item import Item
from girder.utility import assetstore_utilities
from large_image.exceptions import TileSourceError

from . import DICOMFileTileSource
from . import DICOMFileTileSource, _lazyImportPydicom
from .assetstore import DICOMWEB_META_KEY


Expand Down Expand Up @@ -46,8 +47,13 @@ def _getFilesystemLargeImagePath(self):
filelist = [
File().getLocalFilePath(file) for file in Item().childFiles(self.item)
if self._pathMightBeDicom(file['name'])]
if len(filelist) > 1:
if len(filelist) != 1:
return filelist
try:
_lazyImportPydicom().filereader.dcmread(filelist[0], stop_before_pixels=True)
except Exception as exc:
msg = f'File cannot be opened via dicom tile source ({exc}).'
raise TileSourceError(msg)
filelist = []
folder = Folder().load(self.item['folderId'], force=True)
for item in Folder().childItems(folder):
Expand Down

0 comments on commit b02a177

Please sign in to comment.