3
8ßL](<  ã               @   s¬   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZmZmZ dadad	d
„ ZG dd„ de	ƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )z1 Trait definitions related to the numpy library.
é    )Úabsolute_importNé   )ÚSequenceTypes)Ú
TraitError)Ú	TraitTypeÚOBJECT_IDENTITY_COMPARE)ÚStrÚAnyÚIntÚFloatc             C   sJ   ddl }| j|jd krtS | j|jd kr0tS | jd dkrBtS tS dS )z4 Get the corresponding trait for a numpy dtype.
    r   Nr   Z
AllIntegerÚS)ÚnumpyÚcharZ	typecodesÚTFloatÚTIntr   r	   )Údtyper   © r   ú8/tmp/pip-install-5sebvq2j/traits/traits/trait_numeric.pyÚdtype2trait.   s    r   c                   sR   e Zd ZdZd‡ fdd„	Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
‡  ZS )ÚAbstractArrayz: Abstract base class for defining numpy-based arrays.
    NFc       	         s®  yddl }W n tk
r(   tdƒ‚Y nX ddl mama d|d< |jdtƒ |dk	r€tjdt	ƒ |dk	r|||kr|td	ƒ‚n|}|dk	r¸y|j
|ƒ}W n  tk
r¶   td
| ƒ‚Y nX |dk	�rnt|tƒ�rfxž|D ]Œ}|dksÔt|ƒtksÔt|tƒ�rXt|ƒdk�rXt|d ƒtk�rX|d dk�rX|d dksÔt|d ƒtk�rX|d |d k�rXqÔtdƒ‚qÔW ntdƒ‚|dk�r„| j||ƒ}|| _
|| _|| _tt| ƒj|f|Ž dS )z) Returns an AbstractArray trait.
        r   NzMUsing Array or CArray trait types requires the numpy package to be installed.)ÚasarrayÚndarrayTÚarrayÚcomparison_modez4typecode is a deprecated argument; use dtype insteadzHInconsistent usage of the dtype and typecode arguments; use dtype alone.z%could not convert %r to a numpy dtypeé   r   zshape should be a list or tuple)r   ÚImportErrorr   r   r   Ú
setdefaultr   ÚwarningsÚwarnÚDeprecationWarningr   Ú	TypeErrorÚ
isinstancer   ÚtypeÚintÚlenÚ_default_for_dtype_and_shapeÚshapeÚcoerceÚsuperr   Ú__init__)	Úselfr   r&   Úvaluer'   ÚtypecodeÚmetadatar   Úitem)Ú	__class__r   r   r)   J   sV    


zAbstractArray.__init__c       
   
   C   s:  �yt |ƒ}t|tƒsNt|tƒs.| j|||ƒ | jdk	rFt|| jƒ}nt|ƒ}| jdk	r„|j| jkr„| jrx|j| jƒ}nt|| jƒ}| j	}|dkr–|S |j	}t
|ƒt
|ƒk�rxht|ƒD ]X\}}|| }	|	dk	r¸t |	ƒtkrè||	kræP q¸||	d k �s|	d dk	r¸||	d kr¸P q¸W |S W n   Y nX | j|||ƒ dS )z4 Validates that the value is a valid array.
        Nr   r   )r"   r!   r   r   Úerrorr   r   r'   Zastyper&   r$   Ú	enumerater#   )
r*   ÚobjectÚnamer+   Z
type_valueZtrait_shapeZvalue_shapeÚiZdimr.   r   r   r   Úvalidate¡   s<    


zAbstractArray.validatec             C   sš   d }}| j dk	rzg }xT| j D ]J}|dkr0d}n.t|ƒtk	r^|d dkrVd|d  }nd| }|j|ƒ qW dt|ƒf }| jdk	rŽd	| j }d
||f S )z: Returns descriptive information about the trait.
        Ú NÚ*r   z%d..r   z%d..%dz with shape %sz of %s valueszan array%s%s)r&   r"   r#   ÚappendÚtupler   )r*   r   r&   r.   r   r   r   ÚinfoÐ   s    


zAbstractArray.infoc             C   sš   d}d}| j dkrd}| jpd}| jdk	rRt| jƒdkrRddlm} |||d�}nDddlm} | jdkrnt}n
t	| jƒ}||| j
p„g | jpŒd	||d
�}|S )z6 Returns the default UI editor for the trait.
        NFTr   r   )ÚArrayEditor)Úauto_setÚ	enter_set)ÚTupleEditorr   )ÚtypesÚlabelsÚcolsr<   r=   )r<   r=   r&   r$   Ztraitsui.apir;   r>   r   r	   r   r@   rA   )r*   Úeditorr<   r=   r;   r>   r?   r   r   r   Úcreate_editorè   s&    



zAbstractArray.create_editorc             C   s   d| j | jdd| jƒfdffS )zh Returns the default value constructor for the type (called from the
            trait factory.
        é   N)Úcopy_default_valuer5   Údefault_value)r*   r   r   r   Úget_default_value  s    zAbstractArray.get_default_valuec             C   s   |j ƒ S )z‡ Returns a copy of the default value (called from the C code on
            first reference to a trait with no current value).
        )Úcopy)r*   r+   r   r   r   rE     s    z AbstractArray.copy_default_valuec             C   s~   ddl m} |dkrt}n|}|dkr2|d|ƒ}nHg }x8|D ]0}|dkrNd}nt|ƒtkrb|d }|j|ƒ q<W |||ƒ}|S )z> Invent a suitable default value for a given dtype and shape. r   )ÚzerosNr   )r   )r   rI   r#   r"   r   r8   )r*   r   r&   rI   Údtr+   Úsizer.   r   r   r   r%     s    

z*AbstractArray._default_for_dtype_and_shape)NNNFN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   r5   r:   rC   rG   rE   r%   Ú__classcell__r   r   )r/   r   r   F   s       Q/ r   c                   s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚArrayz8 Defines a trait whose value must be a numpy array.
    Nc                s&   t t| ƒj|||dfd|i|—Ž dS )a1   Returns an Array trait.

        Parameters
        ----------
        dtype : a numpy dtype (e.g., int32)
            The type of elements in the array; if omitted, no type-checking is
            performed on assigned values.
        shape : a tuple
            Describes the required shape of any assigned value. Wildcards and
            ranges are allowed. The value None within the *shape* tuple means
            that the corresponding dimension is not checked. (For example,
            ``shape=(None,3)`` means that the first dimension can be any size,
            but the second must be 3.) A two-element tuple within the *shape*
            tuple means that the dimension must be in the specified range. The
            second element can be None to indicate that there is no upper
            bound. (For example, ``shape=((3,5),(2,None))`` means that the
            first dimension must be in the range 3 to 5 (inclusive), and the
            second dimension must be at least 2.)
        value : numpy array
            A default value for the array.

        Default Value
        -------------
        *value* or ``zeros(min(shape))``, where ``min(shape)`` refers to the
        minimum shape allowed by the array. If *shape* is not specified, the
        minimum shape is (0,).

        Description
        -----------
        An Array trait allows only upcasting of assigned values that are
        already numpy arrays. It automatically casts tuples and lists of the
        right shape to the specified *dtype* (just like numpy's **array**
        does).
        Fr,   N)r(   rQ   r)   )r*   r   r&   r+   r,   r-   )r/   r   r   r)   =  s    %
zArray.__init__)NNNN)rL   rM   rN   rO   r)   rP   r   r   )r/   r   rQ   9  s   rQ   c                   s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚCArrayzV Defines a trait whose value must be a numpy array, with casting
        allowed.
    Nc                s&   t t| ƒj|||dfd|i|—Ž dS )aF   Returns a CArray trait.

        Parameters
        ----------
        dtype : a numpy dtype (e.g., int32)
            The type of elements in the array.
        shape : a tuple
            Describes the required shape of any assigned value. Wildcards and
            ranges are allowed. The value None within the *shape* tuple means
            that the corresponding dimension is not checked. (For example,
            ``shape=(None,3)`` means that the first dimension can be any size,
            but the second must be 3.) A two-element tuple within the *shape*
            tuple means that the dimension must be in the specified range. The
            second element can be None to indicate that there is no upper
            bound. (For example, ``shape=((3,5),(2,None))`` means that the
            first dimension must be in the range 3 to 5 (inclusive), and the
            second dimension must be at least 2.)
        value : numpy array
            A default value for the array.

        Default Value
        -------------
        *value* or ``zeros(min(shape))``, where ``min(shape)`` refers to the
        minimum shape allowed by the array. If *shape* is not specified, the
        minimum shape is (0,).

        Description
        -----------
        The trait returned by CArray() is similar to that returned by Array(),
        except that it allows both upcasting and downcasting of assigned values
        that are already numpy arrays. It automatically casts tuples and
        lists of the right shape to the specified *dtype* (just like
        numpy's **array** does).
        Tr,   N)r(   rR   r)   )r*   r   r&   r+   r,   r-   )r/   r   r   r)   q  s    %
zCArray.__init__)NNNN)rL   rM   rN   rO   r)   rP   r   r   )r/   r   rR   l  s   rR   c                   s<   e Zd ZdZ‡ fdd„Z‡ fdd„Zdd„ Zdd	„ Z‡  ZS )
ÚArrayOrNonezr A trait whose value may be either a NumPy array or None, with
        casting allowed.  The default is None.
    c                s"   |j dtƒ tt| ƒj||Ž d S )Nr   )r   r   r(   rS   r)   )r*   Úargsr-   )r/   r   r   r)   ¥  s    zArrayOrNone.__init__c                s    |d kr|S t t| ƒj|||ƒS )N)r(   rS   r5   )r*   r2   r3   r+   )r/   r   r   r5   ª  s    zArrayOrNone.validatec             C   s6   | j }|d krd|fS d| j| jd d |ƒfd ffS d S )Nr   rD   )rF   rE   r5   )r*   Zdvr   r   r   rG   ¯  s    zArrayOrNone.get_default_valuec             C   s   d S )Nr   )r*   r   r&   r   r   r   r%   ½  s    z(ArrayOrNone._default_for_dtype_and_shape)	rL   rM   rN   rO   r)   r5   rG   r%   rP   r   r   )r/   r   rS      s
   rS   )rO   Ú
__future__r   r   Z
trait_baser   Ztrait_errorsr   Ztrait_handlersr   r   Ztrait_typesr   r	   r
   r   r   r   r   r   r   r   rQ   rR   rS   r   r   r   r   Ú<module>   s    t34