imports <SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">as</SPAN> np
<SPAN class="keyword token">from</SPAN> numpy<SPAN class="punctuation token">.</SPAN>lib<SPAN class="punctuation token">.</SPAN>stride_tricks <SPAN class="keyword token">import</SPAN> as_strided
<SPAN class="keyword token">from</SPAN> functools <SPAN class="keyword token">import</SPAN> wraps
<SPAN class="keyword token">from</SPAN> textwrap <SPAN class="keyword token">import</SPAN> dedent
np<SPAN class="punctuation token">.</SPAN>set_printoptions<SPAN class="punctuation token">(</SPAN>edgeitems<SPAN class="operator token">=</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN>linewidth<SPAN class="operator token">=</SPAN><SPAN class="number token">80</SPAN><SPAN class="punctuation token">,</SPAN>precision<SPAN class="operator token">=</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN>
suppress<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN>threshold<SPAN class="operator token">=</SPAN><SPAN class="number token">100</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> slide (stride) and block functions <SPAN class="comment token">#---- functions ----</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">_check</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> r_c<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Performs the array checks necessary for stride and block.
: a - Array or list.
: r_c - tuple/list/array of rows x cols.
:Attempts will be made to
: produce a shape at least (1*c). For a scalar, the
: minimum shape will be (1*r) for 1D array or (1*c) for 2D
: array if r<c. Be aware
"""</SPAN>
<SPAN class="keyword token">if</SPAN> isinstance<SPAN class="punctuation token">(</SPAN>r_c<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN>int<SPAN class="punctuation token">,</SPAN> float<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
r_c <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> int<SPAN class="punctuation token">(</SPAN>r_c<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
r<SPAN class="punctuation token">,</SPAN> c <SPAN class="operator token">=</SPAN> r_c
a <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>atleast_2d<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN>
shp <SPAN class="operator token">=</SPAN> a<SPAN class="punctuation token">.</SPAN>shape
r<SPAN class="punctuation token">,</SPAN> c <SPAN class="operator token">=</SPAN> r_c <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN> min<SPAN class="punctuation token">(</SPAN>r<SPAN class="punctuation token">,</SPAN> a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> min<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">,</SPAN> shp<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">)</SPAN>
a <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>ascontiguousarray<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> a<SPAN class="punctuation token">,</SPAN> shp<SPAN class="punctuation token">,</SPAN> r<SPAN class="punctuation token">,</SPAN> c<SPAN class="punctuation token">,</SPAN> tuple<SPAN class="punctuation token">(</SPAN>r_c<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">stride</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> r_c<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Provide a 2D sliding/moving view of an array.
: There is no edge correction for outputs.
:
:Requires
:--------
: a - array or list, usually a 2D array. Assumes rows is >=1,
: it is corrected as is the number of columns.
: r_c - tuple/list/array of rows x cols. Attempts to
: produce a shape at least (1*c). For a scalar, the
: minimum shape will be (1*r) for 1D array or 2D
: array if r<c. Be aware
"""</SPAN>
a<SPAN class="punctuation token">,</SPAN> shp<SPAN class="punctuation token">,</SPAN> r<SPAN class="punctuation token">,</SPAN> c<SPAN class="punctuation token">,</SPAN> r_c <SPAN class="operator token">=</SPAN> _check<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> r_c<SPAN class="punctuation token">)</SPAN>
shape <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">-</SPAN> r <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">-</SPAN> c <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> r_c
strides <SPAN class="operator token">=</SPAN> a<SPAN class="punctuation token">.</SPAN>strides <SPAN class="operator token">*</SPAN> <SPAN class="number token">2</SPAN>
a_s <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>as_strided<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> shape<SPAN class="operator token">=</SPAN>shape<SPAN class="punctuation token">,</SPAN> strides<SPAN class="operator token">=</SPAN>strides<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>squeeze<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> a_s
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">block</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> r_c<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""See _check and/or stride for documentation. This function
: moves in increments of the block size, rather than sliding
: by one row and column
:
"""</SPAN>
a<SPAN class="punctuation token">,</SPAN> shp<SPAN class="punctuation token">,</SPAN> r<SPAN class="punctuation token">,</SPAN> c<SPAN class="punctuation token">,</SPAN> r_c <SPAN class="operator token">=</SPAN> _check<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> r_c<SPAN class="punctuation token">)</SPAN>
shape <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">/</SPAN>r<SPAN class="punctuation token">,</SPAN> a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="operator token">/</SPAN>c<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> r_c
strides <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>r<SPAN class="operator token">*</SPAN>a<SPAN class="punctuation token">.</SPAN>strides<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> c<SPAN class="operator token">*</SPAN>a<SPAN class="punctuation token">.</SPAN>strides<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> a<SPAN class="punctuation token">.</SPAN>strides
a_b <SPAN class="operator token">=</SPAN> as_strided<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> shape<SPAN class="operator token">=</SPAN>shape<SPAN class="punctuation token">,</SPAN> strides<SPAN class="operator token">=</SPAN>strides<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>squeeze<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> a_b
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> timing decorator, decorated stat function and time test <SPAN class="keyword token">def</SPAN> <SPAN class="token function">delta_time</SPAN><SPAN class="punctuation token">(</SPAN>func<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""simple timing decorator function"""</SPAN>
<SPAN class="keyword token">import</SPAN> time
@wraps<SPAN class="punctuation token">(</SPAN>func<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">wrapper</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>args<SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">**</SPAN>kwargs<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Timing function for... {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>func<SPAN class="punctuation token">.</SPAN>__name__<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
t0 <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>perf_counter<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># start time</SPAN>
result <SPAN class="operator token">=</SPAN> func<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>args<SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">**</SPAN>kwargs<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># ... run the function ...</SPAN>
t1 <SPAN class="operator token">=</SPAN> time<SPAN class="punctuation token">.</SPAN>perf_counter<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># end time</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Results for... {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>func<SPAN class="punctuation token">.</SPAN>__name__<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">" time taken ...{:12.9e} sec."</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>t1<SPAN class="operator token">-</SPAN>t0<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#print("\n {}".format(result)) # print within wrapper</SPAN>
<SPAN class="keyword token">return</SPAN> result <SPAN class="comment token"># return result</SPAN>
<SPAN class="keyword token">return</SPAN> wrapperdelta_time
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">array_mean</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""change the func"""</SPAN>
a_mean <SPAN class="operator token">=</SPAN> a<SPAN class="punctuation token">.</SPAN>mean<SPAN class="punctuation token">(</SPAN>axis<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> None
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">time_test</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""time test for block and sliding raster"""</SPAN>
N <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">100</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">500</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5000</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> n <SPAN class="keyword token">in</SPAN> N<SPAN class="punctuation token">:</SPAN>
a <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>arange<SPAN class="punctuation token">(</SPAN>n<SPAN class="operator token">*</SPAN>n<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>reshape<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">,</SPAN> n<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#a0 = stride(a, block=(3, 3)) # uncomment this or below</SPAN>
a0 <SPAN class="operator token">=</SPAN> block<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> block<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\nArray size... {0}x{0}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>n<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
a_stat <SPAN class="operator token">=</SPAN> array_mean<SPAN class="punctuation token">(</SPAN>a0<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># time function</SPAN>
<SPAN class="keyword token">return</SPAN> None
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>main section <SPAN class="keyword token">if</SPAN> __name__<SPAN class="operator token">==</SPAN><SPAN class="string token">"__main__"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""comparison between block and slide view"""</SPAN>
prn<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN>
time_test<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |