1111import traceback
1212from collections import OrderedDict
1313from enum import Enum
14- from multiprocessing .pool import ThreadPool
1514from pathlib import Path
1615from typing import (
1716 Any ,
@@ -294,13 +293,7 @@ def __init__(self, fp: Union[str, Path, DicomFileLike]):
294293 'the path to a DICOM file stored on disk.'
295294 )
296295
297- # We use threads to read multiple frames in parallel. Since the
298- # operation is I/O rather CPU limited, we don't need to worry about
299- # the Global Interpreter Lock (GIL) and use lighweight threads
300- # instead of separate processes. However, that won't be as useful for
301- # decoding, since that operation is CPU limited.
302- self ._pool : Union [ThreadPool , None ] = None
303-
296+ # Those attributes will be set by the "open()"
304297 self ._metadata : Dataset = Dataset ()
305298 self ._is_open = False
306299 self ._as_float = False
@@ -510,7 +503,6 @@ def open(self) -> None:
510503 'Number of Frames.'
511504 )
512505
513- self ._pool = ThreadPool ()
514506 self ._is_open = True
515507
516508 def _assert_is_open (self ) -> None :
@@ -531,46 +523,10 @@ def metadata(self) -> Dataset:
531523
532524 def close (self ) -> None :
533525 """Close file."""
534- if self ._pool is not None :
535- self ._pool .close ()
536- self ._pool .terminate ()
537526 if self ._fp is not None :
538527 self ._fp .close ()
539528 self ._is_open = False
540529
541- def read_frames (
542- self ,
543- indices : Iterable [int ],
544- parallel : bool = False
545- ) -> List [bytes ]:
546- """Read the pixel data of one or more frame items.
547-
548- Parameters
549- ----------
550- indices: Iterable[int]
551- Zero-based frame indices
552- parallel: bool, optional
553- Whether frame items should be read in parallel using multiple
554- threads
555-
556- Returns
557- -------
558- List[bytes]
559- Pixel data of frame items encoded in the transfer syntax.
560-
561- Raises
562- ------
563- IOError
564- When frames could not be read
565-
566- """
567- self ._assert_is_open ()
568- if parallel :
569- func = self .read_frame
570- return self ._pool .map (func , indices ) # type: ignore
571- else :
572- return [self .read_frame (i ) for i in indices ]
573-
574530 def read_frame (self , index : int ) -> bytes :
575531 """Read the pixel data of an individual frame item.
576532
@@ -679,39 +635,6 @@ def decode_frame(self, index: int, value: bytes):
679635 ds .PixelData = value
680636 return ds .pixel_array
681637
682- def read_and_decode_frames (
683- self ,
684- indices : Iterable [int ],
685- parallel : bool = False
686- ) -> List [np .ndarray ]:
687- """Read and decode the pixel data of one or more frame items.
688-
689- Parameters
690- ----------
691- indices: Iterable[int]
692- Zero-based frame indices
693- parallel: bool, optional
694- Whether frame items should be read in parallel using multiple
695- threads
696-
697- Returns
698- -------
699- List[numpy.ndarray]
700- Pixel arrays of frame items
701-
702- Raises
703- ------
704- IOError
705- When frames could not be read
706-
707- """
708- self ._assert_is_open ()
709- if parallel :
710- func = self .read_and_decode_frame
711- return self ._pool .map (func , indices ) # type: ignore
712- else :
713- return [self .read_and_decode_frame (i ) for i in indices ]
714-
715638 def read_and_decode_frame (self , index : int ):
716639 """Read and decode the pixel data of an individual frame item.
717640
@@ -2927,10 +2850,9 @@ def retrieve_instance_frames(
29272850 frame_index = frame_number - 1
29282851 frame_indices .append (frame_index )
29292852
2930- frames = image_file_reader .read_frames (frame_indices , parallel = True )
2931-
29322853 reencoded_frames = []
2933- for frame in frames :
2854+ for frame_index in frame_indices :
2855+ frame = image_file_reader .read_frame (frame_index )
29342856 if not transfer_syntax_uid .is_encapsulated :
29352857 reencoded_frame = frame
29362858 else :
0 commit comments