Skip to content

Commit c82854c

Browse files
committed
Make import pydicom optional
1 parent be6bdcd commit c82854c

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

lib/galaxy/datatypes/images.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import mrcfile
1818
import numpy as np
1919
import png
20-
import pydicom
2120
import tifffile
2221
from typing_extensions import Literal
2322

@@ -27,6 +26,11 @@
2726
except ImportError:
2827
PIL = None # type: ignore[assignment, unused-ignore]
2928

29+
try:
30+
import pydicom
31+
except ImportError:
32+
pydicom = None # type: ignore[assignment, unused-ignore]
33+
3034
from galaxy.datatypes.binary import Binary
3135
from galaxy.datatypes.metadata import (
3236
FileParameter,
@@ -541,11 +545,16 @@ def sniff(self, filename: str) -> bool:
541545
"""
542546
Determine if the file is in DICOM format.
543547
"""
544-
try:
545-
pydicom.dcmread(filename, stop_before_pixels=True)
546-
return True
547-
except pydicom.errors.InvalidDicomError:
548-
return False
548+
if pydicom:
549+
try:
550+
pydicom.dcmread(filename, stop_before_pixels=True)
551+
return True
552+
except pydicom.errors.InvalidDicomError:
553+
return False
554+
else:
555+
with open(filename, "rb") as fh:
556+
fh.seek(128)
557+
return fh.read(4) == b"DICM"
549558

550559
def get_mime(self) -> str:
551560
"""
@@ -555,6 +564,15 @@ def get_mime(self) -> str:
555564

556565
def set_meta(
557566
self, dataset: DatasetProtocol, overwrite: bool = True, metadata_tmp_files_dir: Optional[str] = None, **kwd
567+
) -> None:
568+
"""
569+
Populate the metadata of the DICOM file using the pydicom library, if available.
570+
"""
571+
if pydicom:
572+
self._set_meta_with_pydicom(dataset)
573+
574+
def _set_meta_with_pydicom(
575+
self, dataset: DatasetProtocol, overwrite: bool = True, metadata_tmp_files_dir: Optional[str] = None, **kwd
558576
) -> None:
559577
"""
560578
Populate the metadata of the DICOM file using the pydicom library.

0 commit comments

Comments
 (0)