o
    iO!                     @   s   d Z ddlmZmZ ddlmZ zddlZdZW n e	y#   dZY nw er,ddl
mZ dZd	d
iZddddddddddd
ZdefddZ	d!dddeded fddZd!dddededdfdd ZdS )"a  Use the `numpy <https://numpy.org/>`_ package to convert supported *Waveform
Data* to a :class:`numpy.ndarray`.

.. versionadded:: 2.1

**Supported data**

The numpy handler supports the conversion of data in the (5400,0100)
*Waveform Sequence* element to an :class:`~numpy.ndarray` provided the
related :dcm:`Waveform<part03/sect_C.10.9.html>` module elements have values
given in the table below.

+-------------+------------------------------+------+-------------------------+
| Element                                           | Supported               |
+-------------+------------------------------+------+ values                  |
| Tag         | Keyword                      | Type |                         |
+=============+==============================+======+=========================+
| (003A,0005) | NumberOfWaveformChannels     | 1    | N > 0                   |
+-------------+------------------------------+------+-------------------------+
| (003A,0010) | NumberOfWaveformSamples      | 1    | N > 0                   |
+-------------+------------------------------+------+-------------------------+
| (5400,1004) | WaveformBitsAllocated        | 1    | 8, 16, 32, 64           |
+-------------+------------------------------+------+-------------------------+
| (5400,1006) | WaveformSampleInterpretation | 1    | SB, UB, MB, AB, SS, US, |
|             |                              |      | SL, UL, SV, UV          |
+-------------+------------------------------+------+-------------------------+

    )TYPE_CHECKINGcast)	GeneratorNTF)DatasetzNumpy Waveformnumpy)zhttps://numpy.org/NumPyint8uint8int16uint16int32uint32int64uint64)
)   SB)r   UB)r   MB)r   AB)   SS)r   US)    SL)r   UL)@   SV)r   UVreturnc                   C   s   t S )zXReturn ``True`` if the handler has its dependencies met.

    .. versionadded:: 2.1
    )HAVE_NP r    r    U/mnt/sdb/aimis/docanh/lib/python3.10/site-packages/pydicom/waveforms/numpy_handler.pyis_available=   s   r"   dsr   as_raw)
np.ndarrayNNc                 #   sn   d| vr	t dtttd | jD ]\} g d} fdd|D }|r3t d| dd	| tt jd
 }tt j}tt j	}tt j}tt
 j}	|| | }
t||	f }tjtt jd|
 |d}t|||}|s|d}ttd  j}t|D ]&\}}|dd}|dd}|dd}|d|f | | | |d|f< q|V  qdS )a  Yield an :class:`~numpy.ndarray` for each multiplex group in the
    *Waveform Sequence*.

    .. versionadded:: 2.1

    Parameters
    ----------
    ds : pydicom.dataset.Dataset
        The :class:`Dataset` containing a :dcm:`Waveform
        <part03/sect_C.10.9.html>` module and the *Waveform Sequence* to be
        converted.
    as_raw : bool, optional
        If ``True`` (default), then yield the raw unitless waveform data. If
        ``False`` then attempt to convert the raw data for each channel to the
        quantity specified by the corresponding (003A,0210) *Channel
        Sensitivity* unit.

    Yields
    ------
    np.ndarray
        The waveform data for a multiplex group as an :class:`~numpy.ndarray`
        with shape (samples, channels).
    WaveformSequence=No (5400,0100) Waveform Sequence element found in the datasetr   NumberOfWaveformChannelsNumberOfWaveformSamplesWaveformBitsAllocatedWaveformSampleInterpretationWaveformDatac                       g | ]}| vr|qS r    r    .0elemitemr    r!   
<listcomp>l       z&generate_multiplex.<locals>.<listcomp>:Unable to convert the waveform multiplex group with index H as the following required elements are missing from the sequence item: , r   NdtypefloatChannelBaseline        ChannelSensitivity      ?"ChannelSensitivityCorrectionFactor.)AttributeError	enumerater   listr&   joinintr+   r*   r)   strr,   WAVEFORM_DTYPESnp
frombufferbytesr-   copyreshapeastypeChannelDefinitionSequenceget)r#   r$   iirequired_elementsmissingbytes_per_sample
nr_samplesnr_channelsbits_allocatedsample_interpretationexpected_lenr:   arrseqjjchbaselinesensitivity
correctionr    r2   r!   generate_multiplexE   sF   
"r`   indexr%   c                    sb  d| vrt dttd | j|  g d} fdd|D }|r.t d| dd	| tt jd
 }tt j}tt j}tt j}tt	 j
}	|| | }
t||	f }tjtt jd|
 |d}t|||}|s|d}ttd  j}t|D ]&\}}|dd}|dd}|dd}|d|f | | | |d|f< qtd|S )aW  Return an :class:`~numpy.ndarray` for the multiplex group in the
    *Waveform Sequence* at `index`.

    .. versionadded:: 2.1

    Parameters
    ----------
    ds : pydicom.dataset.Dataset
        The :class:`Dataset` containing a :dcm:`Waveform
        <part03/sect_C.10.9.html>` module and the *Waveform Sequence* to be
        converted.
    index : int
        The index of the multiplex group to return.
    as_raw : bool, optional
        If ``True`` (default), then return the raw unitless waveform data. If
        ``False`` then attempt to convert the raw data for each channel to the
        quantity specified by the corresponding (003A,0210) *Channel
        Sensitivity* unit.

    Returns
    -------
    np.ndarray
        The waveform data for a multiplex group as an :class:`~numpy.ndarray`
        with shape (samples, channels).
    r&   r'   r   r(   c                    r.   r    r    r/   r2   r    r!   r4      r5   z#multiplex_array.<locals>.<listcomp>r6   r7   r8   r   Nr9   r;   r<   r=   r>   r?   r@   .r%   )rA   r   rC   r&   rD   rE   r+   r*   r)   rF   r,   rG   rH   rI   rJ   r-   rK   rL   rM   rN   rB   rO   )r#   ra   r$   rQ   rR   rS   rT   rU   rV   rW   rX   r:   rY   rZ   r[   r\   r]   r^   r_   r    r2   r!   multiplex_array   sB   
"
rb   )T)__doc__typingr   r   collections.abcr   r   rH   r   ImportErrorpydicom.datasetr   HANDLER_NAMEDEPENDENCIESrG   boolr"   r`   rE   rb   r    r    r    r!   <module>   sD   	
 K