zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
U
P±drã@s4dZddlmZddlmZdgZGdd„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@sReZdZdZddd„Zdd„Zdd„Zd    d
„Zed d „ƒZ    ed d„ƒZ
dd„Z dS)ra¯
    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)
 
    NcCsF||_||_dd„|jDƒ|_dd„|jDƒ|_dd„|jDƒ|_dS)NcSsg|]}d‘qS©r©©Ú.0ZdimrrúMd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\numpy/lib/arrayterator.pyÚ
<listcomp>Xsz)Arrayterator.__init__.<locals>.<listcomp>cSsg|]}|‘qSrrrrrr    r
YscSsg|]}d‘qS)érrrrr    r
Zs)ÚvarÚbuf_sizeÚshapeÚstartÚstopÚstep)Úselfr r rrr    Ú__init__Ts
zArrayterator.__init__cCs t|j|ƒS©N)Úgetattrr )rÚattrrrr    Ú __getattr__\szArrayterator.__getattr__c CsNt|tƒs|f}g}t|ƒ|j}}|D]^}|tkrZ| tdƒg||d¡t|ƒ}q(t|tƒr|| t||ddƒ¡q(| |¡q(t|ƒ}t|ƒ|kr¶|tdƒf|t|ƒ7}|     |j
|j ¡}t t |j|j|j|ƒƒD]j\}\}}    }
}||jpød|j|<|
|jpd|j|<||jp(|    ||j|<t|    |j|ƒ|j|<qÞ|S)z-
        Return a new arrayterator.
 
        Nr r)Ú
isinstanceÚtupleÚlenÚndimÚEllipsisÚextendÚsliceÚintÚappendÚ    __class__r r Ú    enumerateÚziprrrÚmin) rÚindexZfixedÚlengthZdimsÚslice_ÚoutÚirrrrrr    Ú __getitem___s.
 
 
  ÿzArrayterator.__getitem__cCs*tdd„t|j|j|jƒDƒƒ}|j|S)z-
        Return corresponding data.
 
        css|]}t|ŽVqdSr©r©rÚtrrr    Ú    <genexpr>„sz)Arrayterator.__array__.<locals>.<genexpr>)rr#rrrr )rr'rrr    Ú    __array__s 
ÿ
zArrayterator.__array__ccs|D]}|jEdHqdS)aG
        A 1-D flat iterator for Arrayterator objects.
 
        This iterator returns elements of the array to be iterated over in
        `Arrayterator` one by one. It is similar to `flatiter`.
 
        See Also
        --------
        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Úblockrrr    r0ˆszArrayterator.flatcCs tdd„t|j|j|jƒDƒƒS)zk
        The shape of the array to be iterated over.
 
        For an example, see `Arrayterator`.
 
        css(|] \}}}||d|dVqdS)r Nr)rrrrrrr    r.¬sz%Arrayterator.shape.<locals>.<genexpr>)rr#rrr)rrrr    r¤sÿzArrayterator.shapec    cs’dd„|jDƒrdS|jdd…}|jdd…}|jdd…}|jj}|jpVtt|jƒ}d}t    |dddƒD]~}|dkrŠ||d||<n:||j|kr¶|||||||<|}n|j|||<t
|j|||ƒ||<||j|}qlt dd„t |||ƒDƒƒ}|j|V||||<t    |dddƒD]F}|||j|kr.|j|||<||d|j|d7<q.|d|jdkrFdSqFdS)NcSsg|]}|dkr|‘qSrrrrrr    r
±sz)Arrayterator.__iter__.<locals>.<listcomp>rr éÿÿÿÿcss|]}t|ŽVqdSrr+r,rrr    r.Ðsz(Arrayterator.__iter__.<locals>.<genexpr>) rrrrr rr rrÚranger$rr#)    rrrrZndimsÚcountZrundimr)r'rrr    Ú__iter__¯s4  "zArrayterator.__iter__)N) Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrr*r/Úpropertyr0rr5rrrr    rsC
     
 
 
N)r9ÚoperatorrÚ    functoolsrÚ__all__rrrrr    Ú<module>s