
    Kh                     :    S r SSKJr  SSKJr  S/r " S S5      rg)a$  
A buffered iterator for big arrays.

This module solves the problem of iterating over a big file-based array
without having to read it into memory. The `Arrayterator` class wraps
an array object, and when iterated it will return sub-arrays with at most
a user-specified number of elements.

    )mul)reduceArrayteratorc                   b    \ rS rSrSrSrSS jrS rS rSS jr\	S	 5       r
\	S
 5       rS rSrg)r      a  
Buffered iterator for big arrays.

`Arrayterator` creates a buffered iterator for reading big arrays in small
contiguous blocks. The class is useful for objects stored in the
file system. It allows iteration over the object *without* reading
everything in memory; instead, small blocks are read and iterated over.

`Arrayterator` can be used with any object that supports multidimensional
slices. This includes NumPy arrays, but also variables from
Scientific.IO.NetCDF or pynetcdf for example.

Parameters
----------
var : array_like
    The object to iterate over.
buf_size : int, optional
    The buffer size. If `buf_size` is supplied, the maximum amount of
    data that will be read into memory is `buf_size` elements.
    Default is None, which will read as many element as possible
    into memory.

Attributes
----------
var
buf_size
start
stop
step
shape
flat

See Also
--------
numpy.ndenumerate : Multidimensional array iterator.
numpy.flatiter : Flat array iterator.
numpy.memmap : Create a memory-map to an array stored
               in a binary file on disk.

Notes
-----
The algorithm works by first finding a "running dimension", along which
the blocks will be extracted. Given an array of dimensions
``(d1, d2, ..., dn)``, e.g. if `buf_size` is smaller than ``d1``, the
first dimension will be used. If, on the other hand,
``d1 < buf_size < d1*d2`` the second dimension will be used, and so on.
Blocks are extracted along this dimension, and when the last block is
returned the process continues from the next dimension, until all
elements have been read.

Examples
--------
>>> import numpy as np
>>> a = np.arange(3 * 4 * 5 * 6).reshape(3, 4, 5, 6)
>>> a_itor = np.lib.Arrayterator(a, 2)
>>> a_itor.shape
(3, 4, 5, 6)

Now we can iterate over ``a_itor``, and it will return arrays of size
two. Since `buf_size` was smaller than any dimension, the first
dimension will be iterated over first:

>>> for subarr in a_itor:
...     if not subarr.all():
...         print(subarr, subarr.shape) # doctest: +SKIP
>>> # [[[[0 1]]]] (1, 1, 1, 2)

z	numpy.libNc                     Xl         X l        UR                   Vs/ s H  nSPM     snU l        [	        UR                  5      U l        UR                   Vs/ s H  nSPM     snU l        g s  snf s  snf )Nr      )varbuf_sizeshapestartliststopstep)selfr
   r   dims       N/var/www/html/env/lib/python3.13/site-packages/numpy/lib/_arrayterator_impl.py__init__Arrayterator.__init__X   sW     #&99-9Ca9-
O	"%)),)3Q),	 .,s   A,A1c                 .    [        U R                  U5      $ N)getattrr
   )r   attrs     r   __getattr__Arrayterator.__getattr__`   s    txx&&    c                    [        U[        5      (       d  U4n/ n[        U5      U R                  pCU H  nU[        L a0  UR                  [        S5      /XC-
  S-   -  5        [        U5      nM<  [        U[        5      (       a   UR                  [        XUS-   S5      5        Mq  UR                  U5        M     [        U5      n[        U5      U:  a  U[        S5      4U[        U5      -
  -  -  nU R                  U R                  U R                  5      n[        [        U R                  U R                  U R                   U5      5       H  u  nu  ppXR                  =(       d    S-   UR                  U'   XR                   =(       d    S-  UR                   U'   XR                  =(       d    X-
  -   UR                  U'   [#        XR                  U   5      UR                  U'   M     U$ )z
Return a new arrayterator.

Nr	   r   )
isinstancetuplelenndimEllipsisextendsliceintappend	__class__r
   r   	enumeratezipr   r   r   min)r   indexfixedlengthdimsslice_outir   r   r   s              r   __getitem__Arrayterator.__getitem__c   s    %''HE5z499F!eDk]dk!m<=UFC((U6!8Q78V$  eu:eDk^tCJ77E nnTXXt}}5.7DJJ		499e</>*A*T LL$5A6CIIaL++"23CHHQK;;#<$*=CHHQKdHHQK0CHHQK/> 
r   c                     [        S [        U R                  U R                  U R                  5       5       5      nU R
                  U   $ )z
Return corresponding data.

c              3   2   #    U  H  n[        U6 v   M     g 7fr   r$   .0ts     r   	<genexpr>)Arrayterator.__array__.<locals>.<genexpr>   s      3 +2Quay +2   )r   r)   r   r   r   r
   )r   dtypecopyr/   s       r   	__array__Arrayterator.__array__   sA    
  3#

DIItyy+2 3 3xxr   c              #   J   #    U  H  nUR                    Sh  vN   M     g N	7f)a  
A 1-D flat iterator for Arrayterator objects.

This iterator returns elements of the array to be iterated over in
`~lib.Arrayterator` one by one.
It is similar to `flatiter`.

See Also
--------
lib.Arrayterator
flatiter

Examples
--------
>>> a = np.arange(3 * 4 * 5 * 6).reshape(3, 4, 5, 6)
>>> a_itor = np.lib.Arrayterator(a, 2)

>>> for subarr in a_itor.flat:
...     if not subarr:
...         print(subarr, type(subarr))
...
0 <class 'numpy.int64'>

N)flat)r   blocks     r   rB   Arrayterator.flat   s!     4 Ezz!! !s   #!
#c                 x    [        S [        U R                  U R                  U R                  5       5       5      $ )zS
The shape of the array to be iterated over.

For an example, see `Arrayterator`.

c              3   B   #    U  H  u  po2U-
  S -
  U-  S -   v   M     g7f)r	   N )r8   r   r   r   s       r   r:   %Arrayterator.shape.<locals>.<genexpr>   s,      75 3D%tEz!|d*1,5s   )r   r)   r   r   r   )r   s    r   r   Arrayterator.shape   s3      7DJJ		49957 7 	7r   c              #     #    U R                    Vs/ s H  oS::  d  M
  UPM     sn(       a  g U R                  S S  nU R                  S S  nU R                  S S  nU R                  R
                  n U R                  =(       d    [        [        U R                   5      nSn[        US-
  SS5       Hu  nUS:X  a
  X(   S-   X8'   O4X`R                   U   ::  a  X(   XdU   -  -   X8'   UnOU R                  U   X8'   [        U R                  U   X8   5      X8'   X`R                   U   -  nMw     [        S [        X#U5       5       5      n	U R                  U	   v   X7   X''   [        US-
  SS5       HJ  nX(   U R                  U   :  d  M  U R                  U   X('   X(S-
  ==   U R                  US-
     -  ss'   ML     US   U R                  S   :  a  g GMc  s  snf 7f)Nr   r	   c              3   2   #    U  H  n[        U6 v   M     g 7fr   r6   r7   s     r   r:   (Arrayterator.__iter__.<locals>.<genexpr>   s     E.D5!9.Dr<   )r   r   r   r   r
   r!   r   r   r   ranger*   r   r)   )
r   r   r   r   r   ndimscountrundimr1   r/   s
             r   __iter__Arrayterator.__iter__   s    ::2:CC:2

1yy|yy|MM<VC%<E
 F57B+ A:#hqjDGjjm+#hAw6DGF #iilDGdiilDG4zz!}, ,  Ec%t.DEEF((6"" !LEM57Ar*8tyy|+#zz!}EHA#J$))AaC.0J + Qx499Q<'E  3s   G	GGEGAG)r   r   r   r   r
   r   )NN)__name__
__module____qualname____firstlineno____doc__r   r   r2   r?   propertyrB   r   rR   __static_attributes__rG   r   r   r   r      sR    CJ J-'@  " "8 7 7,r   N)rX   operatorr   	functoolsr   __all__r   rG   r   r   <module>r^      s&     
P Pr   