3
d:                 @   s   d Z ddlZddlmZmZ ddlmZ ddlmZ ddl	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.
    N   )ComparisonModeDefaultValue)SequenceTypes)
TraitError)	TraitType)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char	typecodesTFloatTIntr   r	   )dtyper    r   6/tmp/pip-build-7vycvbft/traits/traits/trait_numeric.pydtype2trait   s    r   c                   sX   e Zd ZdZdd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.
    NFunsafe)castingc      
         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j |d k	rtj	dt
 |d k	r~||kr~tdn|}|d k	ry|j|}W n  tk
r   td	| Y nX |d k	rpt|trhx|D ]}	|	d kst|	tkst|	trZt|	d
krZt|	d tkrZ|	d dkrZ|	d d kst|	d tkrZ|	d |	d krZqtdqW ntd|d kr| j||}|| _|| _|| _|| _t j|f| d S )Nr   zMUsing 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   identitywarningswarnDeprecationWarningr   	TypeError
isinstancer   typeintlen_default_for_dtype_and_shapeshapecoercer   super__init__)
selfr   r*   valuer+   typecoder   metadatar   item)	__class__r   r   r-   3   sX    


zAbstractArray.__init__c       	   
   C   s"  y t |tsFt |ts&| j||| | jdk	r>t|| j}nt|}| jdk	rn|j| jkrn|j| j| jd}| j}|dkr|S |j}t	|t	|kr xft
|D ]V\}}|| }|dk	rt|tkr||krP q||d k s|d dk	r||d krP qW |S W n   Y nX | j||| dS )z4 Validates that the value is a valid array.
        N)r   r   r   )r%   r   r   errorr   r   Zastyper   r*   r(   	enumerater&   r'   )	r.   objectnamer/   Ztrait_shapeZvalue_shapeiZdimr2   r   r   r   validate   s6    


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	rd	| 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*   r2   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
pg | jpd	||d
}|S )z6 Returns the default UI editor for the trait.
        NFTr   r   )ArrayEditor)auto_set	enter_set)TupleEditorr   )typeslabelscolsr@   rA   )r@   rA   r*   r(   Ztraitsui.apir?   rB   r   r	   r   rD   rE   )r.   Zeditorr@   rA   r?   rB   rC   r   r   r   create_editor   s&    



zAbstractArray.create_editorc             C   s    t j| 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)r   callable_and_argscopy_default_valuer9   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   rH      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   rL   r'   r&   r   r<   )r.   r   r*   rL   dtr/   sizer2   r   r   r   r)      s    

z*AbstractArray._default_for_dtype_and_shape)NNNFN)__name__
__module____qualname____doc__r-   r9   r>   rF   rJ   rH   r)   __classcell__r   r   )r3   r   r   /   s       P* r   c                   s(   e Zd ZdZddd fddZ  ZS )Arraya   A trait type whose value must be a NumPy array.

    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).

    The default value is either the *value* argument 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,).

    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.
    casting : str
        Casting rule for the array's dtype. If ``dtype`` is set, a value can
        only be assigned if it passes the casting rule. Values can be:

        - "no": No casting is allowed
        - "equiv": Only byte-order changes are allowed
        - "safe": Only allow casting that fully preserves values (e.g.
          "float32" to "float64")
        - "same-kind": Only safe casts or casts within a kind (e.g. "float64"
          to "float32") are allowed
        - "unsafe": Any casting is allowed

        Default is "unsafe".
    Nr   )r   c               s$   t  j|||df||d| d S )NF)r0   r   )r,   r-   )r.   r   r*   r/   r0   r   r1   )r3   r   r   r-   G  s    
zArray.__init__)NNNN)rO   rP   rQ   rR   r-   rS   r   r   )r3   r   rT     s   ,   rT   c                   s(   e Zd ZdZddd fddZ  ZS )CArraya   A coercing trait type whose value is a NumPy array.

    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).

    The default value is either the *value* argument 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,).

    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.
    casting : str
        Casting rule for the array's dtype. If ``dtype`` is set, a value can
        only be assigned if it passes the casting rule. Values can be:

        - "no": No casting is allowed
        - "equiv": Only byte-order changes are allowed
        - "safe": Only allow casting that fully preserves values (e.g.
          "float32" to "float64")
        - "same-kind": Only safe casts or casts within a kind (e.g. "float64"
          to "float32") are allowed
        - "unsafe": Any casting is allowed

        Default is "unsafe".
    Nr   )r   c               s$   t  j|||df||d| d S )NT)r0   r   )r,   r-   )r.   r   r*   r/   r0   r   r1   )r3   r   r   r-     s    
zCArray.__init__)NNNN)rO   rP   rQ   rR   r-   rS   r   r   )r3   r   rU   \  s   ,   rU   c                   s<   e Zd ZdZ fddZ fddZdd Zdd	 Z  ZS )
ArrayOrNonez A coercing trait whose value may be either a NumPy array or None.

    This trait is designed to avoid the comparison issues with numpy arrays
    that can arise from the use of constructs like Union(None, Array).

    The default value is None.
    c                s    |j dtj t j|| d S )Nr   )r   r   r    r,   r-   )r.   argsr1   )r3   r   r   r-     s    zArrayOrNone.__init__c                s   |d kr|S t  j|||S )N)r,   r9   )r.   r6   r7   r/   )r3   r   r   r9     s    zArrayOrNone.validatec             C   s:   | j }|d krtj|fS tj| j| jd d |fd ffS d S )N)rI   r   ZconstantrG   rH   r9   )r.   Zdvr   r   r   rJ     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)	rO   rP   rQ   rR   r-   r9   rJ   r)   rS   r   r   )r3   r   rV     s
   rV   )rR   r!   	constantsr   r   Z
trait_baser   Ztrait_errorsr   Z
trait_typer   Ztrait_typesr   r	   r
   r   r   r   r   r   r   r   rT   rU   rV   r   r   r   r   <module>   s    kCC