
    0-Ph                        d Z ddlZddlZddlZddlZddlZ ej        e          Z	ej
        dk    Zi dddddd	d
dddddddddddddddddddddd d!d"d#d$d%i d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGi dHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkiZdlZdmZdnZ edo e                                D                       Z edp e                                D                       Z G dq dre          Z G ds dte          Z G du dve          Z G dw dxe          Zdy Zd}d{Zd| ZdS )~z! Plugin for reading DICOM files.
    Nlittle)     )	PixelDataOB)       )	StudyDateDA)r   !   )
SeriesDater   )r   "   )AcquisitionDater   )r   #   )ContentDater   )r   0   )	StudyTimeTM)r   1   )
SeriesTimer   )r   2   )AcquisitionTimer   )r   3   )ContentTimer   )r   `   )ModalityCS)r   p   )ManufacturerLO)r      )InstitutionNamer    )r   0  )StudyDescriptionr    )r   i>  )SeriesDescriptionr    )r      )SOPClassUIDUI)r      )SOPInstanceUIDr(   )r	      )StudyInstanceUIDr(   )r	      )SeriesInstanceUIDr(   )r   i  )
ContextUIDr(   )r	      )SeriesNumberIS)r	      )AcquisitionNumberr2   )r	      )InstanceNumberr2   )r	      )IsotopeNumberr2   )r	      )PhaseNumberr2   )r	   r&   )IntervalNumberr2   )r	      )TimeSlotNumberr2   )r	   r)   )AngleNumberr2   )r	      )
ItemNumberr2   )r	   r	   )PatientOrientationr   )r	   r   )ImagePositionr   )r	   r   )ImagePositionPatientr   )r	   5   )ImageOrientationr   )r	   7   )ImageOrientationPatientr   )r   r   )PatientNamePN)r   r	   )	PatientIDr    )r   r   )PatientBirthDater   )r   @   )
PatientSexr   )r   i  )
PatientAgeAS)r   i   )PatientSizeDS)r   r#   )PatientWeightrQ   )(      )SamplesPerPixelUS)rS   r   )NumberOfFramesr2   )rS      )BitsAllocatedrV   )rS   i  )
BitsStoredrV   )rS   i  )HighBitrV   )rS   i  )PixelRepresentationrV   )rS   r   )RowsrV   )rS   r0   )ColumnsrV   )rS   iR  )RescaleInterceptrQ   )rS   iS  )RescaleSloperQ   )rS   r   )PixelSpacingrQ   )r)      )SliceSpacingrQ   )  i   )rd   i  )rd   i  c                     g | ]
}|d          S )r    ).0keys     V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/plugins/_dicom.py
<listcomp>rj   c   s    000c!f000    c                     g | ]
}|d          S )   rf   )rg   vals     ri   rj   rj   d   s    
/
/
/c3q6
/
/
/rk   c                       e Zd ZdS )NotADicomFileN__name__
__module____qualname__rf   rk   ri   rp   rp   g           Drk   rp   c                       e Zd ZdS )CompressedDicomNrq   rf   rk   ri   rw   rw   k   ru   rk   rw   c                       e Zd ZdZd Zed             Zd Zd Zd Z	d Z
d Zd	 ZddZd Zd Zd Zd Zd Zd Zd ZdS )SimpleDicomReadera@  
    This class provides reading of pixel data from DICOM files. It is
    focussed on getting the pixel data, not the meta info.

    To use, first create an instance of this class (giving it
    a file object or filename). Next use the info attribute to
    get a dict of the meta data. The loading of pixel data is
    deferred until get_numpy_array() is called.

    Comparison with Pydicom
    -----------------------

    This code focusses on getting the pixel data out, which allows some
    shortcuts, resulting in the code being much smaller.

    Since the processing of data elements is much cheaper (it skips a lot
    of tags), this code is about 3x faster than pydicom (except for the
    deflated DICOM files).

    This class does borrow some code (and ideas) from the pydicom
    project, and (to the best of our knowledge) has the same limitations
    as pydicom with regard to the type of files that it can handle.

    Limitations
    -----------

    For more advanced DICOM processing, please check out pydicom.

      * Only a predefined subset of data elements (meta information) is read.
      * This is a reader; it can not write DICOM files.
      * (just like pydicom) it can handle none of the compressed DICOM
        formats except for "Deflated Explicit VR Little Endian"
        (1.2.840.10008.1.2.1.99).

    c                 F    t          |t                    r| _        t          |d           _        nd _        | _        d  _        d _        d _        d _        i  _	         fd fd fd fd	d
 d d d d  fdd d _
                                          d S )Nrbz<unknown file>FT<c                 0                         d|           S )NH_unpackxselfs    ri   <lambda>z,SimpleDicomReader.__init__.<locals>.<lambda>       DLLa00 rk   c                 0                         d|           S )NLr   r   s    ri   r   z,SimpleDicomReader.__init__.<locals>.<lambda>   r   rk   c                 <                         | t          d          S N\_splitValuesfloatr   s    ri   r   z,SimpleDicomReader.__init__.<locals>.<lambda>       D--a== rk   c                 <                         | t          d          S r   )r   intr   s    ri   r   z,SimpleDicomReader.__init__.<locals>.<lambda>   s    D--ad;; rk   c                 T    |                      dd                              d          S Nasciiignore decodestripr   s    ri   r   z,SimpleDicomReader.__init__.<locals>.<lambda>   "    AHHWh77==fEE rk   c                 T    |                      dd                              d          S r   r   r   s    ri   r   z,SimpleDicomReader.__init__.<locals>.<lambda>   r   rk   c                 T    |                      dd                              d          S r   r   r   s    ri   r   z,SimpleDicomReader.__init__.<locals>.<lambda>   r   rk   c                 T    |                      dd                              d          S r   r   r   s    ri   r   z,SimpleDicomReader.__init__.<locals>.<lambda>   r   rk   c                 x    |                      dd                              d                                          S Nzutf-8r   r   r   r   rstripr   s    ri   r   z,SimpleDicomReader.__init__.<locals>.<lambda>   .    AHHWh77==fEELLNN rk   c                 <                         | t          d          S r   r   r   s    ri   r   z,SimpleDicomReader.__init__.<locals>.<lambda>   r   rk   c                 x    |                      dd                              d                                          S r   r   r   s    ri   r   z,SimpleDicomReader.__init__.<locals>.<lambda>   r   rk   )rV   ULrQ   r2   rO   r   r   r(   r    r   rI   )
isinstancestr	_filenameopen_file_pixel_data_locis_implicit_VRis_little_endian_unpackPrefix_info_converters_read)r   files   ` ri   __init__zSimpleDicomReader.__init__   s    dC   	!DNdD))DJJ-DNDJ## $ 
 10000000====;;;;EEEEEEEENN====NN
 
$ 	

rk   c                     | j         S Nr   r   s    ri   infozSimpleDicomReader.info   
    zrk   c                     |                     d                              d          }	 ||v r.t          fd|                    |          D                       S  |          S # t          $ r |cY S w xY w)Nr   r   c                 N    g | ]!}|                                  |          "S rf   )r   )rg   vtypes     ri   rj   z2SimpleDicomReader._splitValues.<locals>.<listcomp>   s.    NNN!AGGIINdd1ggNNNrk   )r   r   tuplesplit
ValueError)r   r   r   splitterss     `  ri   r   zSimpleDicomReader._splitValues   s    HHW##F++	1}}NNNNqwwx/@/@NNNOOOtAww 	 	 	HHH	s   1A( 
A( (A76A7c                 H    t          j        | j        |z   |          d         S )Nr   )structunpackr   )r   fmtvalues      ri   r   zSimpleDicomReader._unpack   s!    }T/#5u==a@@rk   c                 N    t          | j                                                  S r   )iterr   keysr   s    ri   __iter__zSimpleDicomReader.__iter__   s    DJOO%%&&&rk   c                     t                               | d          }||v r||         S t                               | |          S )Nr   )object__getattribute__)r   rh   r   s      ri   __getattr__zSimpleDicomReader.__getattr__   s@    &&tW55$;;9&&tS111rk   c                    | j         }|                    d           |                    d          dk    rt          d          |                                  |                                  |                                  t          j        	                    | j
                  r"| j                                          d | _         d S d S )Nr!      s   DICMzNot a valid DICOM file.)r   seekreadrp   _read_header_read_data_elements_get_shape_and_samplingospathisfiler   close)r   fs     ri   r   zSimpleDicomReader._read   s    J	s66!99 9:::  """$$&&&7>>$.)) 	JDJJJ	 	rk   c                    | j         }|                     d|                    d                    }|                     d|                    d                    }| j        r*|                     d|                    d                    }n|                    d          }|dv r?|                    d          }|                     d|                    d                    }n)|                     d|                    d                    }|dk    r@|dk    r:|                                }||f| _        |                    ||z              ||dfS |d	k    r|                                 }n|                    |          }|||fS )
Nr~   rT   Ir   )s   OBs   OWs   SQs   UNr   r   s   Deferred loading of pixel data    )r   r   r   r   tellr   r   _read_undefined_length_value)	r   r   groupelementvlvrreservedherer   s	            ri   _readDataElementz"SimpleDicomReader._readDataElement   s]   JS!&&)),,,,sAFF1II.. 	2c166!99--BBB11166!99\\#qvvayy11\\#qvvayy11F??w&006688D#'8D FF4"9'#DDDZ99;;r

'5((rk   r!   c                 p   | j         }d}t          j        | j        dz   t          d         t          d                   }d}g }|s^|                                }|                    |          }t          |          |k     rL|                    |t          |          z
            }	||	z  }t          |          |k     rt          d          |	                    |          }
|
dk    rpd}|
                    |d	|
                    |                    ||
z   d
z              |                    d
          }|dk    rt                              d           nH|                    |                                |z
             |
                    |d	|                     |^d                    |          S )zOCopied (in compacted form) from PyDicom
        Copyright Darcy Mason.
           HHr   rm   Fz4End of file reached before sequence delimiter found.TNr   s       z6Expected 4 zero bytes after undefined length delimiterrk   )r   r   packr   SequenceDelimiterTagr   r   lenEOFErrorfindappendr   loggerwarningjoin)r   	read_sizefpsearch_rewindbytes_to_findfoundvalue_chunkschunk_start
bytes_read	new_bytesindexlengths               ri   r   z.SimpleDicomReader._read_undefined_length_value   s    Z%';A'>@TUV@W
 
  	A''))K++J:** GGIJ$?@@	i'
z??Y.."Q   OOM22E{{##Jvv$6777e+a/000[((NNS   		M1222##J$?@@@3  	A8 xx%%%rk   c                    | j         }d }	 	 |                                }|                                 \  }}}|dk    r5|dk    r.|dk    r(|                    d                              d          }n|                    |           n~n)# t          t          j        f$ r t          d          w xY w|| j
        d<   |d\  }}n|d	k    rd\  }}n|d
k    rd\  }}ny|dk    rd\  }}nm|dk    rd\  }}|                                  nM|d}
}	d|	cxk    rdk     rn nd}
d|	cxk    rdk     rn nd}
|	dk    rd}
|	dk    rd}
t          d|	|
d          || _        || _        d|         | _        d S )NTrT   r   r   r   z*End of file reached while still in header.TransferSyntaxUID)FTz1.2.840.10008.1.2.1z1.2.840.10008.1.2.2FFz1.2.840.10008.1.2)TTz1.2.840.10008.1.2.1.99 z1.2.840.10008.1.2.4.50z1.2.840.10008.1.2.4.99z (JPEG)z1.2.840.10008.1.2.4.90z (JPEG 2000)z1.2.840.10008.1.2.5z (RLE)z1.2.840.10008.1.2.6.1z (RFC 2557)zHThe dicom reader can only read files with uncompressed image data - not z7. You can try using dcmtk or gdcm to convert the image.z><)r   r   r   r   r   r   r   r   errorRuntimeErrorr   _inflaterw   r   r   r   )r   r   r  fp_saver   r   r   r   r   t
extra_infos              ri   r   zSimpleDicomReader._read_header*  s-   J 	M&&(((,(=(=(?(?%wD==}}D,1LL,A,A,G,G,O,O) FF7OOO &,' 	M 	M 	MKLLL	M +<
&'$/:,N,,"777/:,N,,"777/;,N,,"555/9,N,,":::/:,N,MMOOOO .rzA'1GGGG/GGGGGG&
'1GGGG/GGGGGG+
)))%
+++*
!/ Azzz+   - 0!"23s   A?B &B1c                 .   | j         }	 	 |                                 \  }}}|t          v rN||f}t                              |d          \  }}|r*| j                            |d           } ||          ||<   p# t          t          j        f$ r Y d S w xY w)NT)NNc                     | S r   rf   r   s    ri   r   z7SimpleDicomReader._read_data_elements.<locals>.<lambda>v  s    q rk   )	r   r   GROUPSMINIDICTgetr   r   r   r  )	r   r   r   r   r   rh   namer   	converters	            ri   r   z%SimpleDicomReader._read_data_elementsi  s    z	6(,(=(=(?(?%wF?? '*C'||C>>HD" 6$($4$8$8[[$I$I	%.Yu%5%5T
6 &,' 	 	 	DD	s   A1A: :BBc                 R   d| vrt          d          | j        rt          | j                  dk     rd}| j        d}t          | j        d          | _        | j                            | j        d                    | j        d	         d
k    r|                                 }n%| j        	                    | j        d	                   }|r | j        
                                 d| _        || j        d<   |                                 }|                     |          }d| j        d<   |S )zsGet numpy arra for this DICOM file, with the correct shape,
        and pixel values scaled appropriately.
        r   $No pixel data found in this dataset.d   FNTr{   r   rm   r   sB   Data converted to numpy array, raw data removed to preserve memory)	TypeErrorr   r   r   r   r   r   r   r   r   r   r   _pixel_data_numpy_apply_slope_and_offset)r   
close_filer   datas       ri   get_numpy_arrayz!SimpleDicomReader.get_numpy_array{  s/   
 d""BCCC  	,C$7$7#$=$=Jz!!
!$.$77
JOOD03444#A&*4499;;
(<Q(?@@ "
  """!
&+DJ{# %%''++D11
 X 	
; rk   c                    d| v rF| j         dk    r;| j        dk    r| j        | j         | j        | j        f}nq| j         | j        | j        f}n\d| v rI| j        dk    r/| j        dk    r| j        | j        | j        f}n-t          d          | j        | j        f}nt          d          d| v r5t          | j        d                   t          | j        d                   f}nd	}d
| v rt          | j
                  f|z   }dt          |          t          |          z
  z  |t          |           d         z   }|| j        d<   || j        d<   dS )zGet shape and sampling without actuall using the pixel data.
        In this way, the user can get an idea what's inside without having
        to load it.
        rW   rm   rU   r   zCDICOM plugin only handles SamplesPerPixel > 1 if Bits Allocated = 8z=DICOM file has no SamplesPerPixel (perhaps this is a report?)ra   r   )      ?r  rc   )r  Nshapesampling)rW   rU   r]   r^   rY   NotImplementedErrorr  r   ra   absrc   r   r   )r   r  r  s      ri   r   z)SimpleDicomReader._get_shape_and_sampling  s    t##(;a(?(?#a''('IL	 +TYD$&&#a''%** 0$)T\IEE-(   	4</R  
 T!!T.q122E$:KA:N4O4OOHHHT!!D-..08;H SZZ#h--788SZZKMM;RR $
7!)
:rk   c                    d| vrt          d          | j        t          k    }dd| j                 | j        fz  }	 t          j        |          }n-# t           $ r  t          d|| j        | j        fz            w xY wt          j        | j        |          	                                }|r|
                    d            |j        | j        d          }|S )z'Return a NumPy array of the pixel data.r   r  z%sint%d)ur  zYData type not understood by NumPy: format='%s',  PixelRepresentation=%d, BitsAllocated=%dTr  )r  r   sys_is_little_endianr\   rY   npdtype
frombufferr   copybyteswapreshaper   )r   need_byteswap
format_strnumpy_formatarrs        ri   r  z#SimpleDicomReader._pixel_data_numpy  s   
 d""BCCC -1EE d./"
 

	8J//LL 	 	 	<!94;MNO  	 mDNL99>>@@  	LL ck4:g./
s   A *A;c                    d\  }}d\  }}d| v r	d}| j         }d| v r	d}| j        }t          |          |k    st          |          |k    rd}|st          |          t          |          }}|r|j        t          j        t          j        fv rnp|r!|                    t          j                  }nM|                                	                                |
                                	                                }}t          |||z  |z   ||z  |z   g          }t          |||z  |z   ||z  |z   g          }d}|dk     rXt          | |g          }|dk     rt          j        }nx|d	k     rt          j        }ne|d
k     rt          j        }nRt          j        }nE|dk     rt          j        }n2|dk     rt          j        }n|dk     rt          j        }nt          j        }||j        k    r|                    |          }||z  }||z  }|S )z
        If RescaleSlope and RescaleIntercept are present in the data,
        apply them. The data type of the data is changed if necessary.
        )rm   r   r  r`   Tr_   Nr   r!   i   l        rX   i   l        )r`   r_   r   r$  r#  float32float64astypeminitemmaxint8int16int32)	r   r  slopeoffset
needFloatsneedApplySlopeOffsetminReqmaxReqr$  s	            ri   r  z)SimpleDicomReader._apply_slope_and_offset  s%    v+7(
(T!!#' %E%%#' *Fu::#f++"7"7J 	4JJF6E   )	zbj"*555 !.{{2:.. "&!2!2DHHJJOO4E4Effunv&=v~PV?VWXXffunv&=v~PV?VWXX A:: 6'6!233F}} "% "% " "
 }} "% "% " "
DJ&&;;u--D EMDFND rk   c                     dd l }ddlm} | j                                        }|                    ||j                   } ||          | _        d S )Nr   )BytesIO)zlibior>  r   r   
decompress	MAX_WBITS)r   r?  r>  zippedunzippeds        ri   r  zSimpleDicomReader._inflate<  s`     	 "" ??6DN?;;WX&&


rk   N)r!   )rr   rs   rt   __doc__r   propertyr   r   r   r   r   r   r   r   r   r   r  r   r  r  r  rf   rk   ri   ry   ry   o   s-       " "H# # #J   X  A A A' ' '2 2 2  ) ) )8)& )& )& )&V=4 =4 =4~  $% % %N.* .* .*`& & &P@ @ @D' ' ' ' 'rk   ry   c                       e Zd ZdZd Zd Zd Zd Zed             Z	ed             Z
ed             Zed	             Zed
             Zd Zd Zd Zd Zd ZdS )DicomSerieszDicomSeries
    This class represents a serie of dicom files (SimpleDicomReader
    objects) that belong together. If these are multiple files, they
    represent the slices of a volume (like for CT or MRI).
    c                 >    g | _         || _        i | _        || _        d S r   )_entries_suidr   _progressIndicator)r   suidprogressIndicators      ri   r   zDicomSeries.__init__U  s'     

"3rk   c                 *    t          | j                  S r   )r   rJ  r   s    ri   __len__zDicomSeries.__len__^  s    4=!!!rk   c                 *    t          | j                  S r   )r   rJ  r   s    ri   r   zDicomSeries.__iter__a  s    DM"""rk   c                     | j         |         S r   )rJ  )r   r   s     ri   __getitem__zDicomSeries.__getitem__d  s    }U##rk   c                     | j         S r   )rK  r   s    ri   rM  zDicomSeries.suidg  r   rk   c                     | j         d         S )z#The shape of the data (nz, ny, nx).r  r   r   s    ri   r  zDicomSeries.shapek  s     z'""rk   c                     | j         d         S )z8The sampling (voxel distances) of the data (dz, dy, dx).r  r   r   s    ri   r  zDicomSeries.samplingp  s     z*%%rk   c                     | j         S )z~A dictionary containing the information as present in the
        first dicomfile of this serie. None if there are no entries.r   r   s    ri   r   zDicomSeries.infou  s     zrk   c                    | j         }|sdt          |           z  S g }d|v r|                    d|d         z              | j        r9d | j        D             }|                    d                    |                     d|v r!|                    d|d         z   dz              d|v r!|                    d|d         z   dz              d	                    |          S )
zA description of the dicom series. Used fields are
        PatientName, shape of the data, SeriesDescription, and
        ImageComments.
        z DicomSeries containing %i imagesrH   r  c                 ,    g | ]}t          |          S rf   )r   )rg   ds     ri   rj   z+DicomSeries.description.<locals>.<listcomp>  s    ...a3q66...rk   r   r%   'ImageComments )r   r   r   r  r   )r   r   fieldstmps       ri   descriptionzDicomSeries.description{  s     y  	B5D		AAD  MM"tM22333: 	)..4:...CMM#((3--((($&&MM#%8 99C?@@@d""MM#_ 55;<<< xxrk   c                     t          t          |                                                     }dt          |           |fz  S )Nz"<DicomSeries with %i images at %s>)hexidupperr   )r   adrs     ri   __repr__zDicomSeries.__repr__  s5    "T((mm!!##3s4yy#6FFFrk   c                    t          |           dk    rt          d          t          |           dk    r| d                                         S | j        t	          d          | d                                         }t          j        | j        |j                  }||d<   | j	        
                    ddt          |                      t          dt          |                     D ]<}| |                                         ||<   | j	                            |dz              =| j	                                         ddl}|                                 |S )	zGet (load) the data that this DicomSeries represents, and return
        it as a numpy array. If this serie contains multiple images, the
        resulting array is 3D, otherwise it's 2D.
        r   z!Serie does not contain any files.rm   Nz,Cannot return volume if series not finished.)r$  zloading datar  )r   r   r  r   r  r#  zerosr  r$  rL  startrangeset_progressfinishgccollect)r   slicevolzrm  s        ri   r  zDicomSeries.get_numpy_array  sC    t99>>@AAAYY!^^7**,,, 9MNNN Q''))htz555A 	%%nb#d))DDDq#d))$$ 	8 	8A!W,,..CF#00Q7777&&((( 				



rk   c                 :    | j                             |           d S r   )rJ  r   )r   dcms     ri   _appendzDicomSeries._append  s    S!!!!!rk   c                 >    | j                             d            d S )Nc                 N    | j         t          | d          r| j        d         nd fS )NrC   rT   )r6   hasattrrC   )ks    ri   r   z#DicomSeries._sort.<locals>.<lambda>  s4      q"899A*1-- rk   rh   )rJ  sortr   s    ri   _sortzDicomSeries._sort  s5      	 		
 		
 		
 		
 		
rk   c                 j   | j         }t          |          dk    rdS t          |          dk    r|d         j        | _        dS |d         }d}|j        |j        f}|j        d         dd         }t          t          |                    D ]}||         }t          |j        d                   }t          |j        d                   }	|t          ||	z
            z  }|j        |j        f}
|j        d         dd         }||
k    rt          d          ||k    r| j                            d           |}|t          |          dz
  z  }|d         j                                        | _        t          |          f|j        d	         z   | j        d	<   |f|j        d         z   | j        d<   dS )
a  
        Evaluate the series of dicom files. Together they should make up
        a volumetric dataset. This means the files should meet certain
        conditions. Also some additional information has to be calculated,
        such as the distance between the slices. This method sets the
        attributes for "shape", "sampling" and "info".

        This method checks:
          * that there are no missing files
          * that the dimensions of all images match
          * that the pixel spacing of all images match
        r   Nrm   g        r  rT   z$Dimensions of slices does not match.zWarn: sampling does not match.r  )rJ  r   r   r   r]   r^   rj  r   rC   r  r   rL  writer&  )r   r   ds1distance_sum
dimensionsr  r   ds2pos1pos2dimensions2	sampling2distance_means                ri   _finishzDicomSeries._finish  s    Mq66Q;;FVVq[[1DJF dXs{*
8J'+3q66]] 	 	E E(C1!455D1!455DCt,,,L(CK/K,RaR0I[(( !GHHH9$$'--.NOOOCC %A
3 qTY^^%%
  #1vvi#(7*;;
7"/!1CHZ4H!H
:rk   N)rr   rs   rt   rE  r   rP  r   rS  rF  rM  r  r  r   r`  rf  r  rt  r{  r  rf   rk   ri   rH  rH  N  s5        4 4 4" " "# # #$ $ $   X # # X# & & X&   X
     X 8G G G     D" " "

 

 

?I ?I ?I ?I ?Irk   rH  c                 <   t          j        |          D ]}t           j                            ||          }t           j                            |          rt          | |           Rt           j                            |          r|                     |           dS )z-List all files in the directory, recursively.N)r   listdirr   r   isdir
list_filesr   r   )filesr   r2  s      ri   r  r    s    
4    w||D$''7== 	ud####W^^D!! 	LL rk   Fc                 `   t           j                            | j                  r| j        }nXt           j                            | j                  r%t           j                            | j                  }nt          d          g }t          ||           i }d}|                    ddt          |                     |D ]}|dz  }|
                    |           |                    d          r2	 t          |          }nD# t          $ r Y Ot          $ r,}	|                    t!          |	                     Y d}	~	d}	~	ww xY w	 |j        }
n# t$          $ r Y w xY w|
|vrt'          |
|          ||
<   ||
                             |           t+          |                                          }|                    d 	           t1          d
 |D                       D ]}t3          |||           g }t5          t          |                    D ]q}	 ||                                          |                    ||                    9# t          $ r,}|                    t!          |                     Y d}~jd}~ww xY w|                    dt          |          z             |S )a  
    Reads dicom files and returns a list of DicomSeries objects, which
    contain information about the data, and can be used to load the
    image or volume data.

    if readPixelData is True, the pixel data of all series is read. By
    default the loading of pixeldata is deferred until it is requested
    using the DicomSeries.get_pixel_array() method. In general, both
    methods should be equally fast.
    z<Dicom plugin needs a valid filename to examine the directoryr   zexamining filesr  rm   DICOMDIRNc                     | j         S r   )rM  r   s    ri   r   z#process_directory.<locals>.<lambda>Q  s    af rk   ry  c                     g | ]}|S rf   rf   )rg   series     ri   rj   z%process_directory.<locals>.<listcomp>T  s    555U5555rk   zFound %i correct series.)r   r   r  filenamer   dirnamer   r  ri  r   rk  country   rp   	Exceptionr}  r   r.   AttributeErrorrH  rt  listvaluesrz  reversedsplitSerieIfRequiredrj  r  r   rl  )requestrN  readPixelDatar   r  seriesr  r  rs  whyrM  r  series_ierrs                  ri   process_directoryr    s    
w}}W%&& Y	(	)	) Yww/00WXXX Eud FE-wE

CCC " "
&&u--->>*%% 		#H--CC 	 	 	H 	 	 	##CHH---HHHH		(DD 	 	 	H	v&t->??F4LtS!!!! &--//""F
KK$$K%%% 55f55566 ? ?UF,=>>>> G3v;;  	1INN6!9%%%% 	 	 	##CHH---DDDD	 7#g,,FGGG NsH   4D
E	E"E  E	E
EE5I
J"JJc                 R   |                                   | j        }|d         }d|vrdS |gg}d}t          dt          |                    D ]}||         }t	          |j        d                   }	t	          |j        d                   }
t          |	|
z
            }|r!|d|z  k    r|                    g            d}n*|r&|d|z  k    r|                    d|j	        z             |}|d	                             |           |}t          |          dk    r|
                    |           }g }|D ]3}t          | j        |          }||_        |                    |           4t          |          D ]}|                    ||           |                    |            dS dS )
a  
    Split the serie in multiple series if this is required. The choice
    is based on examing the image position relative to the previous
    image. If it differs too much, it is assumed that there is a new
    dataset. This can happen for example in unspitted gated CT data.
    r   rC   Nrm   rT   g @g      ?zWarning: missing file after %rr   )r{  rJ  rj  r   r   rC   r  r   r}  r   r   rH  rM  r  insertremove)r  r  rN  r   r~  L2distancer   r  r  r  newDistr  series2insertnewSeries                  ri   r  r  h  s    
KKMMMA
A$CS((%BHq#a&&!!  hS-a011S-a011dTk""  		#.00IIbMMMHH  GcHn44!''4s}D   H
2c 2ww{{LL 	+ 	+A"5:/@AAH !H  **** // 	' 	'HMM!X&&&&e {rk   )F)rE  sysr   r   loggingnumpyr#  	getLoggerrr   r   	byteorderr"  r  ItemTagItemDelimiterTagr   setr   r  r  VRSr  rp   r  rw   r   ry   rH  r  r  r  rf   rk   ri   <module>r     s    


 				       
	8	$	$ }0 =)= )= *	=
 /= += )= *= /= += (= ,= /=  0!=" 1#=& +'=( .)=* 0+= =, 1-=. */=2 ,3=4 15=6 .7=8 -9=: +;=< .==> .?=@ +A=B *C=D 2E=F -G=H 4I=J 0K=L 7M=P +Q= = =R )S=T 0U=V *W=X *Y=Z +[=\ -]=` /a=b .c=d -e=f *g=h 'i=j 3k=l nm=n 'o=p 0q=r ,s=v ,w= =x ,y= =B # '  
00000	1	1	c
/
/X__..
/
/
/00	 	 	 	 	I 	 	 		 	 	 	 	l 	 	 	\' \' \' \' \' \' \' \'~~I ~I ~I ~I ~I& ~I ~I ~IB  L L L L^< < < < <rk   