U
    ?h                     @   s$   d dl ZdddZG dd dZdS )    Nc              	   C   s   ddl m} t| jtjs$td| j}| d} |dkrNtj	||jd}n"|j|krpt
d| d|j d	z| }d
|_W n. tk
r   t
d|j d|j d	Y nX |j| jdd}|j|jdd}|| ||| |S )aH  Map values from input array from input_vals to output_vals.

    Parameters
    ----------
    input_arr : array of int, shape (M[, N][, P][, ...])
        The input label image.
    input_vals : array of int, shape (N,)
        The values to map from.
    output_vals : array, shape (N,)
        The values to map to.
    out: array, same shape as `input_arr`
        The output array. Will be created if not provided. It should
        have the same dtype as `output_vals`.

    Returns
    -------
    out : array, same shape as `input_arr`
        The array of mapped values.
       )
_map_arrayz7The dtype of an array to be remapped should be integer.NdtypezbIf out array is provided, it should have the same shape as the input array. Input array has shape z", provided output array has shape .)r   z`If out array is provided, it should be either contiguous or 1-dimensional. Got array with shape z and strides Fcopy)Z_remapr   npZ
issubdtyper   integer	TypeErrorshapeZreshapeempty
ValueErrorviewAttributeErrorstridesastype)Z	input_arrZ
input_valsZoutput_valsoutr   Z
orig_shapeZout_view r   I/var/www/html/venv/lib/python3.8/site-packages/skimage/util/_map_array.py	map_array   s0    



r   c                   @   s^   e Zd ZdZdd Zdd ZdddZed	d
 Zdd Z	dd Z
dd Zdd Zdd ZdS )ArrayMapu  Class designed to mimic mapping by NumPy array indexing.

    This class is designed to replicate the use of NumPy arrays for mapping
    values with indexing:

    >>> values = np.array([0.25, 0.5, 1.0])
    >>> indices = np.array([[0, 0, 1], [2, 2, 1]])
    >>> values[indices]
    array([[0.25, 0.25, 0.5 ],
           [1.  , 1.  , 0.5 ]])

    The issue with this indexing is that you need a very large ``values``
    array if the values in the ``indices`` array are large.

    >>> values = np.array([0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.0])
    >>> indices = np.array([[0, 0, 10], [0, 10, 10]])
    >>> values[indices]
    array([[0.25, 0.25, 1.  ],
           [0.25, 1.  , 1.  ]])

    Using this class, the approach is similar, but there is no need to
    create a large values array:

    >>> in_indices = np.array([0, 10])
    >>> out_values = np.array([0.25, 1.0])
    >>> values = ArrayMap(in_indices, out_values)
    >>> values
    ArrayMap(array([ 0, 10]), array([0.25, 1.  ]))
    >>> print(values)
    ArrayMap:
      0 → 0.25
      10 → 1.0
    >>> indices = np.array([[0, 0, 10], [0, 10, 10]])
    >>> values[indices]
    array([[0.25, 0.25, 1.  ],
           [0.25, 1.  , 1.  ]])

    Parameters
    ----------
    in_values : array of int, shape (N,)
        The source values from which to map.
    out_values : array, shape (N,)
        The destination values from which to map.
    c                 C   s   || _ || _d| _d | _d S )N   )	in_values
out_values_max_str_lines_array)selfr   r   r   r   r   __init__j   s    zArrayMap.__init__c                 C   s   t | jd S )z<Return one more than the maximum label value being remapped.r   )r
   maxr   r   r   r   r   __len__p   s    zArrayMap.__len__Nc                 C   s:   |dkr| j j}tjt| jd |d}| j || j< |S )zReturn an array that behaves like the arraymap when indexed.

        This array can be very large: it is the size of the largest value
        in the ``in_vals`` array, plus one.
        Nr   r   )r   r   r
   Zzerosr    r   )r   r   outputr   r   r   	__array__t   s
    zArrayMap.__array__c                 C   s   | j jS N)r   r   r!   r   r   r   r      s    zArrayMap.dtypec                 C   s   dt | j dt | j dS )Nz	ArrayMap(z, ))reprr   r   r!   r   r   r   __repr__   s    zArrayMap.__repr__c                    s   t  j jd krBtt  j}ddg fdd|D  }n`ttd jd }tt j d d}ddg fdd|D  d	g  fd
d|D  }|S )Nr   
z	ArrayMap:c                    s(   g | ] }d  j |  d j|  qS z  u    → r   r   .0ir!   r   r   
<listcomp>   s     z$ArrayMap.__str__.<locals>.<listcomp>r      c                    s(   g | ] }d  j |  d j|  qS r*   r+   r,   r!   r   r   r/      s   z  ...c                    s(   g | ] }d  j |  d j|  qS r*   r+   r,   r!   r   r   r/      s   )lenr   r   rangejoinlist)r   rowsstringZrows0Zrows1r   r!   r   __str__   s.    

zArrayMap.__str__c                 C   s
   |  |S r%   )__getitem__)r   Zarrr   r   r   __call__   s    zArrayMap.__call__c                 C   s   t |}|rt |g}n@t|tr\|jp.d}|jd k	r@|jnt| }|j}t 	|||}|j
tkrpt |}t|| jj|j
dd| j}|r|d }|S )Nr   Fr   )r
   Zisscalararray
isinstanceslicestartstopr1   stepZaranger   boolflatnonzeror   r   r   r   )r   indexZscalarr=   r>   r?   r   r   r   r   r8      s*    




zArrayMap.__getitem__c                 C   s>   | j d kr|  | _ || j |< t| j | _| j | j | _d S r%   )r   r$   r
   rA   r   r   )r   indicesvaluesr   r   r   __setitem__   s
    


zArrayMap.__setitem__)N)__name__
__module____qualname____doc__r   r"   r$   propertyr   r(   r7   r9   r8   rE   r   r   r   r   r   =   s   ,

r   )N)numpyr
   r   r   r   r   r   r   <module>   s   
9