o
    ^i'                     @   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u r'tj	||jd}n|j|kr8t
d| d|j d	z	| }d
|_W n tyU   t
d|j d|j d	w |j| jdd}|j|jdd}|| ||| |S )a   Map values from input array from input_vals to output_vals.

    Parameters
    ----------
    input_arr : array of int, shape (M[, ...])
        The input label image.
    input_vals : array of int, shape (K,)
        The values to map from.
    output_vals : array, shape (K,)
        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.

    Notes
    -----
    If `input_arr` contains values that aren't covered by `input_vals`, they
    are set to 0.

    Examples
    --------
    >>> import numpy as np
    >>> import skimage as ski
    >>> ski.util.map_array(
    ...    input_arr=np.array([[0, 2, 2, 0], [3, 4, 5, 0]]),
    ...    input_vals=np.array([1, 2, 3, 4, 6]),
    ...    output_vals=np.array([6, 7, 8, 9, 10]),
    ... )
    array([[0, 7, 7, 0],
           [8, 9, 0, 0]])
       )
_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)_remapr   np
issubdtyper   integer	TypeErrorshapereshapeempty
ValueErrorviewAttributeErrorstridesastype)	input_arr
input_valsoutput_valsoutr   
orig_shapeout_view r   R/var/www/html/RAG/RAG_venv/lib/python3.10/site-packages/skimage/util/_map_array.py	map_array   s>   %


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 (K,)
        The source values from which to map.
    out_values : array, shape (K,)
        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__z   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__   s   zArrayMap.__len__Nc                 C   s:   |du 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   zerosr(   r"   )r&   r   r	   outputr   r   r   	__array__   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r"tt  j}ddg fdd|D  }|S 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                    (   g | ]}d  j |  d j|  qS z  u    → r"   r#   .0ir)   r   r   
<listcomp>      ( z$ArrayMap.__str__.<locals>.<listcomp>r      c                    r3   r4   r5   r6   r)   r   r   r9      r:   z  ...c                    r3   r4   r5   r6   r)   r   r   r9      r:   )lenr"   r$   rangejoinlist)r&   rowsstringrows0rows1r   r)   r   __str__   s(   zArrayMap.__str__c                 C   s
   |  |S r.   )__getitem__)r&   arrr   r   r   __call__   s   
zArrayMap.__call__c                 C   s   t |}|rt |g}n t|tr.|jpd}|jd ur |jnt| }|j}t 	|||}|j
tkr8t |}t|| jj|j
dd| j}|rL|d }|S )Nr   Fr   )r   isscalararray
isinstanceslicestartstopr<   steparanger   boolflatnonzeror   r"   r   r#   )r&   indexscalarrL   rM   rN   r   r   r   r   rE      s$   




zArrayMap.__getitem__c                 C   s>   | j d u r
|  | _ || j |< t| j | _| j | j | _d S r.   )r%   r-   r   rQ   r"   r#   )r&   indicesvaluesr   r   r   __setitem__   s
   


zArrayMap.__setitem__)NN)__name__
__module____qualname____doc__r'   r*   r-   propertyr   r1   rD   rG   rE   rV   r   r   r   r   r    L   s    -

r    r.   )numpyr   r   r    r   r   r   r   <module>   s    
H