o
    i,                     @   s  d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZmZ d	d
 e D Zde	e de	e dee fddZeeeeee f f Zeeef Zeeeeef f ZG dd dZG dd dZdd eD Z e !dd eD  ee Z"dS )z"Access code dictionary information    )chainN)castAny)KeysViewIterable)Code)concepts)name_for_cidcid_conceptsc                 C   s   i | ]\}}||qS  r   ).0kvr   r   I/mnt/sdb/aimis/docanh/lib/python3.10/site-packages/pydicom/sr/codedict.py
<dictcomp>   s    r   sourcefiltersreturnc                    s8    st t| S dd  D  t t fdd| D S )a  Return a sorted list of filtered str.

    Parameters
    ----------
    source : Iterable[str]
        The iterable of str to be filtered.
    filters : Iterable[str]
        An iterable containing patterns for which values are to be included
        in the results.

    Returns
    -------
    List[str]
        A sorted list of unique values from `source`, filtered by including
        case-insensitive partial or full matches against the values
        in `filters`.
    c                 S   s   g | ]}|  qS r   lowerr   fr   r   r   
<listcomp>'       z_filtered.<locals>.<listcomp>c                 3   s*    | ] t  fd dD r V  qdS )c                 3   s    | ]	}|   v V  qd S Nr   r   valr   r   	<genexpr>*       z&_filtered.<locals>.<genexpr>.<genexpr>N)any)r   r   r   r   r   *   s   ( z_filtered.<locals>.<genexpr>)sortedset)r   r   r   r    r   	_filtered   s   r#   c                   @   s   e Zd ZdZdZdeddfddZedeee	f fdd	Z
d
ee	B defddZdee fddZdedee fddZdede	fddZedefddZedefddZdefddZedefddZdefddZdee fddZdS ) 
CollectionzjInterface for a collection of concepts, such as SNOMED-CT, or a DICOM CID.

    .. versionadded:: 3.0
    z{} = {}namer   Nc                 C   sT   |  ds|| _t| | _nd|dd  | _tt|dd  | _i | _dS )a6  Create a new collection.

        Parameters
        ----------
        name : str
            The name of the collection, should either be a key in the
            ``sr._concepts_dict.concepts`` :class:`dict` or a CID name for
            a CID in ``sr._cid_dict.cid_concepts`` such as ``"CID1234"``.
        CID   N)	upper
startswith_nameCONCEPTS_scheme_dataCID_CONCEPTSint	_cid_data	_conceptsselfr%   r   r   r   __init__;   s   

zCollection.__init__c                    s$    j s fdd  D  _  j S )z1Return a :class:`dict` of {SR identifiers: codes}c                    s   i | ]}|t  |qS r   )getattr)r   r%   r2   r   r   r   V   s    z'Collection.concepts.<locals>.<dictcomp>)r0   dirr5   r   r5   r   r   R   s   zCollection.conceptsitemc                 C   s@   t |trzt| |}W n ty   Y dS w |}|| j v S )aZ  Checks whether a given code is a member of the collection.

        Parameters
        ----------
        item : pydicom.sr.coding.Code | str
            The code to check for as either the code or the corresponding
            keyword.

        Returns
        -------
        bool
            Whether the collection contains the `code`
        F)
isinstancestrr4   AttributeErrorr   values)r2   r7   coder   r   r   __contains__Z   s   
zCollection.__contains__c                 C   sT   dd t t| t jD }dd t t| t jD }t|  }t||B |B S )zReturn a list of available concept keywords.

        List of attributes is used, for example, in auto-completion in editors
        or command-line environments.
        c                 S      h | ]}|d  qS r   r   r   r   r   r   r   	<setcomp>x   r   z%Collection.__dir__.<locals>.<setcomp>c                 S   r>   r?   r   r@   r   r   r   rA   y   r   )inspect
getmemberstype	isroutineisdatadescriptorr"   r6   r!   )r2   methspropssr_namesr   r   r   __dir__r   s   zCollection.__dir__r   c                 G   s(   | j rtt| j |S t| j|S )a  Return an sorted list of concept keywords based on a partial match.

        Parameters
        ----------
        filters : str
            Zero or more string arguments to the function. Used for
            case-insensitive match to any part of the SR keyword.

        Returns
        -------
        list of str
            The matching keywords. If no `filters` are used then all
            keywords are returned.
        )is_cidr#   r   from_iterabler/   r;   r,   )r2   r   r   r   r   r6   ~   s   zCollection.dirc              	      s  | j drfdd| j D }|std d| j  t|dkr6td d| j  dd	| |d
 }tt	t
|  }t|dkrTt| d
 \}}n8t| j dd   fdd| D }t|dkrd	dd |D }td d| j  d| |d
 \}}t||d
 |dS z
tt	| j }	W n ty   td d| j  dw t|	dkrd	|	 }
td d| j  d|
 t|	 d
 }|	| \}}t||| j dS )a~  Return the :class:`~pydicom.sr.Code` corresponding to `name`.

        Parameters
        ----------
        name : str
            A camel case version of the concept's code meaning, such as
            ``"FontanelOfSkull" in the SCT coding scheme.

        Returns
        -------
        pydicom.sr.Code
            The :class:`~pydicom.sr.Code` corresponding to `name`.
        r&   c                    s   g | ]
\}} |v r|qS r   r   )r   schemekeywordsr%   r   r   r      s
    z*Collection.__getattr__.<locals>.<listcomp>zNo matching code for keyword 'z' in    z/Multiple schemes found to contain the keyword 'z: z, r   r'   Nc                    s$   g | ]\}} |d  v r||fqS )rP   r   )r   r<   r   )cidr   r   r      s    c                 s   s    | ]}|d  V  qdS r   Nr   r@   r   r   r   r          z)Collection.__getattr__.<locals>.<genexpr>z"Multiple codes found for keyword ')valuemeaningscheme_designatorz' in scheme ''z': )r%   r)   r/   itemsr:   lenRuntimeErrorjoinr   CIDValueTyper+   listr.   r   r,   KeyErrorkeys)r2   r%   matchesrM   identifiersr<   r   _matchescodesentriescode_valuesrU   cidsr   )rQ   r%   r   __getattr__   sd   

zCollection.__getattr__c                 C   s   | j dS )z:Return ``True`` if the collection is one of the DICOM CIDsr&   )r%   r)   r5   r   r   r   rK      s   zCollection.is_cidc                 C      | j S )z"Return the name of the collection.)r*   r5   r   r   r   r%         zCollection.namec                    s.    fdd j  D } j dd| S )z*Return a representation of the collection.c                    s   g | ]\}} j ||qS r   )repr_formatformatr   r%   conceptr5   r   r   r      s    z'Collection.__repr__.<locals>.<listcomp>
)r   rX   r%   r[   )r2   r   r   r5   r   __repr__   s   
zCollection.__repr__c                 C   rh   )z0Return the scheme designator for the collection.rO   r5   r   r   r   rV      ri   zCollection.scheme_designatorc                    s`  t dd | j D d }t dd | j D d }t dd | j D d }t |d}t |d}t |d}| jrud	| d
| d
| d | jg}| dddd | dddd |d fdd| j	 D  n6d	| d
| d d| j g}| ddd | ddd |d fdd| j	 D  d|S )z1Return a string representation of the collection.c                 s   s    | ]}t |V  qd S r   rY   )r   nr   r   r   r      rS   z%Collection.__str__.<locals>.<genexpr>   c                 s       | ]	}t |d  V  qdS rR   rp   r   cr   r   r   r      r   c                 s   rs   )rP   Nrp   rt   r   r   r   r      r            z{:z} {:z} {}	Attributer   SchemeMeaningz	---------z----z------z-------rn   c                 3   s&    | ]\}} j |g|R  V  qd S r   rk   rl   fmtr   r   r   	  s
    
zScheme: c                 3   s*    | ]\}}  ||d  |d V  qdS )r   rr   Nr|   rl   r}   r   r   r     s
    
)
maxr   r_   r;   rK   r%   appendrk   r[   rX   )r2   	len_names	len_codeslen_schemessr   r}   r   __str__   s4   



zCollection.__str__c                 C   s   t | S )zReturn a list of valid names for auto-completion code.

        Used in IPython, so that data element names can be found and offered
        for autocompletion on the IPython command line.
        )r6   r5   r   r   r   trait_names  s   zCollection.trait_names)__name__
__module____qualname____doc__rj   r9   r3   propertydictr   r   boolr=   r]   rJ   r6   rg   rK   r%   ro   rV   r   r   r   r   r   r   r$   3   s&    L	'r$   c                   @   sr   e Zd ZdZdee ddfddZedee	 fddZ
d	e	defd
dZdee	 fddZdee	 fddZdS )ConceptszWManagement class for the available concept collections.

    .. versionadded:: 3.0
    collectionsr   Nc                 C   s   dd |D | _ dS )zCreate a new concepts management class instance.

        Parameters
        ----------
        collections : list[Collection]
            A list of the available concept collections.
        c                 S   s   i | ]}|j |qS r   rO   rt   r   r   r   r   5  r   z%Concepts.__init__.<locals>.<dictcomp>N)_collections)r2   r   r   r   r   r3   -  s   zConcepts.__init__c                 C   s
   | j  S )z6Return the names of the available concept collections.r   r_   r5   r   r   r   r   7  s   
zConcepts.collectionsr%   c                 C   sP   |  drd|dd  }|| jv r| j| S tdt| j d| d)zReturn the concept collection corresponding to `name`.

        Parameters
        ----------
        name : str
            The scheme designator or CID name for the collection to be returned.
        r&   r'   NrW   z' object has no attribute ')r(   r)   r   r:   rD   r   r1   r   r   r   rg   <  s   

zConcepts.__getattr__c                 C      dd | j  D S )z/Return a list of available scheme designations.c                 S   s   g | ]	}| d s|qS r&   r)   rt   r   r   r   r   P      z$Concepts.schemes.<locals>.<listcomp>r   r5   r   r   r   schemesN     zConcepts.schemesc                 C   r   )z%Return a list of available CID names.c                 S   s   g | ]	}| d r|qS r   r   rt   r   r   r   r   T  r   z!Concepts.CIDs.<locals>.<listcomp>r   r5   r   r   r   CIDsR  r   zConcepts.CIDs)r   r   r   r   r]   r$   r3   r   r   r9   r   r   rg   r   r   r   r   r   r   r   '  s    
r   c                 C   s   g | ]}t |qS r   r$   )r   
designatorr   r   r   r   X  r   r   c                 c   s    | ]
}t d | V  qdS )r&   Nr   )r   rQ   r   r   r   r   Z  s    r   )#r   	itertoolsr   rB   typingr   r   collections.abcr   r   pydicom.sr.codingr   pydicom.sr._concepts_dictr   r+   pydicom.sr._cid_dictr	   r
   r-   rX   cid_for_namer9   r]   r#   r   tupler.   r\   ConceptsTypeSnomedMappingTyper$   r   r   extendrc   r   r   r   r   <module>   s&   " u1