Skip to content

Commit 89e8fa5

Browse files
author
Chris Bridge
committed
Add read functions
1 parent a8a45e6 commit 89e8fa5

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

src/highdicom/legacy/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
LegacyConvertedEnhancedCTImage,
66
LegacyConvertedEnhancedMRImage,
77
LegacyConvertedEnhancedPETImage,
8+
lcectimread,
9+
lcepetimread,
10+
lcemrimread,
811
)
912

1013
SOP_CLASS_UIDS = {
@@ -17,4 +20,7 @@
1720
'LegacyConvertedEnhancedCTImage',
1821
'LegacyConvertedEnhancedMRImage',
1922
'LegacyConvertedEnhancedPETImage',
23+
'lcectimread',
24+
'lcepetimread',
25+
'lcemrimread',
2026
]

src/highdicom/legacy/sop.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,3 +2205,90 @@ class LegacyConvertedEnhancedMRImage(_CommonLegacyConvertedEnhancedImage):
22052205
_MODALITY_NAME = "MR"
22062206
_LEGACY_SOP_CLASS_UID = "1.2.840.10008.5.1.4.1.1.4"
22072207
_ENHANCED_SOP_CLASS_UID = "1.2.840.10008.5.1.4.1.1.4.4"
2208+
2209+
2210+
def lcectimread(
2211+
fp: str | bytes | PathLike | BinaryIO,
2212+
lazy_frame_retrieval: bool = False
2213+
) -> LegacyConvertedEnhancedCTImage:
2214+
"""Read an LegacyConvertedEnhancedCTImage from DICOM file.
2215+
2216+
Parameters
2217+
----------
2218+
fp: Union[str, bytes, os.PathLike]
2219+
Any file-like object representing a DICOM file containing an
2220+
Legacy Converted Enhanced CT Image.
2221+
lazy_frame_retrieval: bool
2222+
If True, the returned image will retrieve frames from the file as
2223+
requested, rather than loading in the entire object to memory
2224+
initially. This may be a good idea if file reading is slow and you are
2225+
likely to need only a subset of the frames in the image.
2226+
2227+
Returns
2228+
-------
2229+
highdicom.legacy.LegacyConvertedEnhancedCTImage:
2230+
Image read from the file.
2231+
2232+
"""
2233+
return LegacyConvertedEnhancedCTImage.from_file(
2234+
fp,
2235+
lazy_frame_retrieval=lazy_frame_retrieval
2236+
)
2237+
2238+
2239+
def lcemrimread(
2240+
fp: str | bytes | PathLike | BinaryIO,
2241+
lazy_frame_retrieval: bool = False
2242+
) -> LegacyConvertedEnhancedMRImage:
2243+
"""Read an LegacyConvertedEnhancedMRImage from DICOM file.
2244+
2245+
Parameters
2246+
----------
2247+
fp: Union[str, bytes, os.PathLike]
2248+
Any file-like object representing a DICOM file containing an
2249+
Legacy Converted Enhanced MR Image.
2250+
lazy_frame_retrieval: bool
2251+
If True, the returned image will retrieve frames from the file as
2252+
requested, rather than loading in the entire object to memory
2253+
initially. This may be a good idea if file reading is slow and you are
2254+
likely to need only a subset of the frames in the image.
2255+
2256+
Returns
2257+
-------
2258+
highdicom.legacy.LegacyConvertedEnhancedMRImage:
2259+
Image read from the file.
2260+
2261+
"""
2262+
return LegacyConvertedEnhancedMRImage.from_file(
2263+
fp,
2264+
lazy_frame_retrieval=lazy_frame_retrieval
2265+
)
2266+
2267+
2268+
def lcepetimread(
2269+
fp: str | bytes | PathLike | BinaryIO,
2270+
lazy_frame_retrieval: bool = False
2271+
) -> LegacyConvertedEnhancedPETImage:
2272+
"""Read an LegacyConvertedEnhancedPETImage from DICOM file.
2273+
2274+
Parameters
2275+
----------
2276+
fp: Union[str, bytes, os.PathLike]
2277+
Any file-like object representing a DICOM file containing an
2278+
Legacy Converted Enhanced PET Image.
2279+
lazy_frame_retrieval: bool
2280+
If True, the returned image will retrieve frames from the file as
2281+
requested, rather than loading in the entire object to memory
2282+
initially. This may be a good idea if file reading is slow and you are
2283+
likely to need only a subset of the frames in the image.
2284+
2285+
Returns
2286+
-------
2287+
highdicom.legacy.LegacyConvertedEnhancedPETImage:
2288+
Image read from the file.
2289+
2290+
"""
2291+
return LegacyConvertedEnhancedPETImage.from_file(
2292+
fp,
2293+
lazy_frame_retrieval=lazy_frame_retrieval
2294+
)

tests/test_legacy.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from concurrent.futures import ProcessPoolExecutor
22
from copy import deepcopy
3+
from io import BytesIO
34
from datetime import datetime, timedelta
45
import enum
56
import re
@@ -20,6 +21,9 @@
2021
LegacyConvertedEnhancedCTImage,
2122
LegacyConvertedEnhancedMRImage,
2223
LegacyConvertedEnhancedPETImage,
24+
lcectimread,
25+
lcemrimread,
26+
lcepetimread,
2327
)
2428
from highdicom import UID
2529

@@ -1374,3 +1378,42 @@ def test_from_dataset_wrong_modality(modality: Modality) -> None:
13741378
WrongLegacyConverterClass.from_dataset(
13751379
write_and_read_dataset(converted)
13761380
)
1381+
1382+
1383+
@pytest.mark.parametrize("lazy", [True, False])
1384+
def test_read_function(modality: Modality, lazy: bool) -> None:
1385+
LegacyConverterClass = MODALITY_CLASS_MAP[modality]
1386+
data_generator = DicomGenerator(5)
1387+
legacy_datasets = data_generator.generate_mixed_framesets(
1388+
modality, 1, True, True
1389+
)
1390+
1391+
output_series_uid = UID()
1392+
output_sop_uid = UID()
1393+
output_series_number = 23
1394+
output_instance_number = 2
1395+
series_description = 'Converted Series'
1396+
1397+
converted = LegacyConverterClass(
1398+
legacy_datasets,
1399+
series_instance_uid=output_series_uid,
1400+
sop_instance_uid=output_sop_uid,
1401+
series_number=output_series_number,
1402+
instance_number=output_instance_number,
1403+
series_description=series_description,
1404+
)
1405+
1406+
read_fn = {
1407+
Modality.PT: lcepetimread,
1408+
Modality.CT: lcectimread,
1409+
Modality.MR: lcemrimread,
1410+
}[modality]
1411+
1412+
with BytesIO() as fp:
1413+
converted.save_as(fp)
1414+
fp.seek(0)
1415+
reread = read_fn(fp, lazy_frame_retrieval=lazy)
1416+
1417+
assert isinstance(reread, LegacyConverterClass)
1418+
assert ('PixelData' in reread) == (not lazy)
1419+
assert converted.pixel_array.shape == (5, 2, 2)

0 commit comments

Comments
 (0)