관리-도구
편집 파일: arrayterator.cpython-311.pyc
� �܋f� � �B � d Z ddlmZ ddlmZ dgZ G d� d� � ZdS )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 �^ � e Zd ZdZd d�Zd� Zd� Zd� Zed� � � Z ed� � � Z d � ZdS )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 -------- ndenumerate : Multidimensional array iterator. flatiter : Flat array iterator. 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 -------- >>> 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) Nc � � || _ || _ d� |j D � � | _ d� |j D � � | _ d� |j D � � | _ d S )Nc � � g | ]}d ��S �r � ��.0�dims �M/opt/cloudlinux/venv/lib64/python3.11/site-packages/numpy/lib/arrayterator.py� <listcomp>z)Arrayterator.__init__.<locals>.<listcomp>X s � �-�-�-�C�a�-�-�-� c � � g | ]}|��S r r r s r r z)Arrayterator.__init__.<locals>.<listcomp>Y s � �.�.�.�S�S�.�.�.r c � � g | ]}d ��S )� r r s r r z)Arrayterator.__init__.<locals>.<listcomp>Z s � �,�,�,�3�Q�,�,�,r )�var�buf_size�shape�start�stop�step)�selfr r s r �__init__zArrayterator.__init__T sY � ���� �� �-�-�3�9�-�-�-�� �.�.�C�I�.�.�.�� �,�,�#�)�,�,�,�� � � r c �, � t | j |� � S �N)�getattrr )r �attrs r �__getattr__zArrayterator.__getattr__\ s � ��t�x��&�&�&r c � � t |t � � s|f}g }t |� � | j }}|D ]�}|t u r<|� t d� � g||z dz z � � t |� � }�Gt |t � � r(|� t ||dz d� � � � ��|� |� � ��t |� � }t |� � |k r&|t d� � f|t |� � z z z }| � | j | j � � }t t | j | j | j |� � � � D ]l\ }\ }} } }||j pdz |j |<