B <ÓFdDÉã@sdZddlmZmZmZddlZdgZd3dd„Zdd „Z d4d d „Z d5d d„Z d6dd„Z d7dd„Z d8dd„Zd9dd„Zd:dd„Zd;dd„Zddd „Zd?d!d"„Zd@d#d$„ZdAd%d&„ZdBd'd(„ZdCd)d*„ZdDd+d,„ZdEd.d/„Zd0d1„Zd2d„ZdS)Fzl The arraypad module contains a group of functions to pad values onto the edges of an n-dimensional array. é)ÚdivisionÚabsolute_importÚprint_functionNÚpadFcsŠt‡‡fdd„t|jƒDƒƒ}|s8t dˆˆd¡}nt ˆˆdd¡}| |¡}x0tˆƒD]$\}}|j||kr^|j||d}q^W|S)a— Create an ndarray of `shape` with increments along specified `axis` Parameters ---------- arr : ndarray Input array of arbitrary shape. shape : tuple of ints Shape of desired array. Should be equivalent to `arr.shape` except `shape[axis]` which may have any positive value. axis : int Axis to increment along. reverse : bool If False, increment in a positive fashion from 1 to `shape[axis]`, inclusive. If True, the bounds are the same but the order reversed. Returns ------- padarr : ndarray Output array sized to pad `arr` along `axis`, with linear range from 1 to `shape[axis]` along specified `axis`. Notes ----- The range is deliberately 1-indexed for this specific use case. Think of this algorithm as broadcasting `np.arange` to a single `axis` of an arbitrarily shaped ndarray. c3s&|]\}}|ˆkrdnˆˆVqdS)éN©)Ú.0ÚiÚx)ÚaxisÚshaperúE/opt/alt/python37/lib64/python3.7/site-packages/numpy/lib/arraypad.pyú 0sz"_arange_ndarray..rréÿÿÿÿ)r )ÚtupleÚ enumerater ÚnpZarangeÚreshapeÚrepeat)Úarrr r ÚreverseZ initshapeZpadarrr Zdimr)r r r Ú_arange_ndarrays rcCst |tj¡r|j|ddS)z Rounds arr inplace if destination dtype is integer. Parameters ---------- arr : ndarray Input array. dtype : dtype The dtype of the destination array. )ÚoutN)rZ issubdtypeZintegerÚround)rÚdtyperrr Ú_round_ifneeded=s rrcsxˆdkr |St‡‡fdd„t|jƒDƒƒ}|dkrPtjtj||jd|fˆdStjt |¡| |j¡|fˆdSdS)a Prepend constant `val` along `axis` of `arr`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to prepend. val : scalar Constant value to use. For best results should be of type `arr.dtype`; if not `arr.dtype` will be cast to `arr.dtype`. axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` constant `val` prepended along `axis`. rc3s"|]\}}|ˆkr|nˆVqdS)Nr)rr r )r Úpad_amtrr resz!_prepend_const..)r)r N)rrr rÚ concatenateÚzerosrÚastype)rrÚvalr Úpadshaper)r rr Ú_prepend_constMsr"csxˆdkr |St‡‡fdd„t|jƒDƒƒ}|dkrPtj|tj||jdfˆdStj|t |¡| |j¡fˆdSdS)a Append constant `val` along `axis` of `arr`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to append. val : scalar Constant value to use. For best results should be of type `arr.dtype`; if not `arr.dtype` will be cast to `arr.dtype`. axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` constant `val` appended along `axis`. rc3s"|]\}}|ˆkr|nˆVqdS)Nr)rr r )r rrr r‡sz _append_const..)r)r N)rrr rrrrr)rrr r r!r)r rr Ú _append_constosr#csn|dkr |St‡fdd„t|jƒDƒƒ}t‡fdd„t|jƒDƒƒ}|| |¡}tj|j|ˆd|fˆdS)a Prepend `pad_amt` to `arr` along `axis` by extending edge values. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to prepend. axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, extended by `pad_amt` edge values appended along `axis`. rc3s&|]\}}|ˆkrtdƒndVqdS)Nr)Úslice)rr r )r rr r§sz _prepend_edge..c3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr r«s)r )rrr rrrr)rrr Ú edge_sliceÚ pad_singletonÚedge_arrr)r r Ú _prepend_edge‘s  r(csp|dkr ˆSt‡‡fdd„tˆjƒDƒƒ}t‡fdd„tˆjƒDƒƒ}ˆ| |¡}tjˆ|j|ˆdfˆdS)a— Append `pad_amt` to `arr` along `axis` by extending edge values. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to append. axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, extended by `pad_amt` edge values prepended along `axis`. rc3s0|](\}}|ˆkrtdƒn ˆjˆdVqdS)Nr)r$r )rr r )rr rr rÉsz_append_edge..c3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr rÍs)r )rrr rrrr)rrr r%r&r'r)rr r Ú _append_edge²s r)c sÖˆdkr |St‡‡fdd„t|jƒDƒƒ}t||ˆdd tj¡}t‡fdd„t|jƒDƒƒ}t‡fdd„t|jƒDƒƒ}|| |¡ ˆˆ¡}||t ˆƒ} || }||7}t ||j ƒtj | |j ¡|fˆdS) aF Prepend linear ramp along `axis`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to prepend. end : scalar Constal value to use. For best results should be of type `arr.dtype`; if not `arr.dtype` will be cast to `arr.dtype`. axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` values prepended along `axis`. The prepended region ramps linearly from the edge value to `end`. rc3s"|]\}}|ˆkr|nˆVqdS)Nr)rr r )r rrr rïsz _prepend_ramp..T)rc3s&|]\}}|ˆkrtdƒndVqdS)Nr)r$)rr r )r rr r÷sc3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr rûs)r ) rrr rrrÚfloat64rrÚfloatrrr) rrÚendr r!Úramp_arrr%r&Úedge_padÚsloper)r rr Ú _prepend_rampÔs    r0c sÖˆdkr |St‡‡fdd„t|jƒDƒƒ}t||ˆdd tj¡}t‡fdd„t|jƒDƒƒ}t‡fdd„t|jƒDƒƒ}|| |¡ ˆˆ¡}||t ˆƒ} || }||7}t ||j ƒtj || |j ¡fˆdS) aB Append linear ramp along `axis`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to append. end : scalar Constal value to use. For best results should be of type `arr.dtype`; if not `arr.dtype` will be cast to `arr.dtype`. axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` values appended along `axis`. The appended region ramps linearly from the edge value to `end`. rc3s"|]\}}|ˆkr|nˆVqdS)Nr)rr r )r rrr r&sz_append_ramp..F)rc3s&|]\}}|ˆkrtdƒndVqdS)Nr)r$)rr r )r rr r.sc3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr r2s)r ) rrr rrrr*rrr+rrr) rrr,r r!r-r%r&r.r/r)r rr Ú _append_ramp s    r1cs¦|dkr |Sˆdkr t||ˆƒSˆdk r:ˆ|jˆkr:d‰t‡‡fdd„t|jƒDƒƒ}t‡fdd„t|jƒDƒƒ}||jˆd |¡}tj|j|ˆd|fˆdS)aS Prepend `pad_amt` maximum values along `axis`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to prepend. num : int Depth into `arr` along `axis` to calculate maximum. Range: [1, `arr.shape[axis]`] or None (entire axis) axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` values appended along `axis`. The prepended region is the maximum of the first `num` values along `axis`. rrNc3s*|]"\}}|ˆkrtdƒntˆƒVqdS)N)r$)rr r )r Únumrr rgsz_prepend_max..c3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr rks)r ) r(r rrÚmaxrrrr)rrr2r Ú max_slicer&Ú max_chunkr)r r2r Ú _prepend_maxBs  r6csÔ|dkr |Sˆdkr t||ˆƒSˆdk r:ˆ|jˆkr:d‰|jˆd‰ˆdk rrt‡‡‡fdd„t|jƒDƒƒ}ntdd„|jDƒƒ}t‡fdd„t|jƒDƒƒ}||jˆd |¡}tj||j|ˆdfˆdS) a_ Pad one `axis` of `arr` with the maximum of the last `num` elements. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to append. num : int Depth into `arr` along `axis` to calculate maximum. Range: [1, `arr.shape[axis]`] or None (entire axis) axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` values appended along `axis`. The appended region is the maximum of the final `num` values along `axis`. rrNc3s2|]*\}}|ˆkrtdƒntˆˆˆdƒVqdS)Nr)r$)rr r )r r,r2rr rsz_append_max..css|]}tdƒVqdS)N)r$)rr rrr r sc3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr r£s)r ) r)r rrr3rrrr)rrr2r r4r&r5r)r r,r2r Ú _append_maxvs$  r7cs¶|dkr |Sˆdkr t||ˆƒSˆdk r:ˆ|jˆkr:d‰t‡‡fdd„t|jƒDƒƒ}t‡fdd„t|jƒDƒƒ}|| ˆ¡ |¡}t||jƒtj |  |ˆ¡  |j¡|fˆdS)aC Prepend `pad_amt` mean values along `axis`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to prepend. num : int Depth into `arr` along `axis` to calculate mean. Range: [1, `arr.shape[axis]`] or None (entire axis) axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` values prepended along `axis`. The prepended region is the mean of the first `num` values along `axis`. rrNc3s*|]"\}}|ˆkrtdƒntˆƒVqdS)N)r$)rr r )r r2rr rÒsz _prepend_mean..c3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr rÖs)r ) r(r rrÚmeanrrrrrrr)rrr2r Ú mean_slicer&Ú mean_chunkr)r r2r Ú _prepend_mean®s   r;csæ|dkr |Sˆdkr t||ˆƒSˆdk r:ˆ|jˆkr:d‰|jˆd‰ˆdk rrt‡‡‡fdd„t|jƒDƒƒ}ntdd„|jDƒƒ}t‡fdd„t|jƒDƒƒ}||jˆd |¡}t||jƒtj ||  |ˆ¡  |j¡fˆdS) aB Append `pad_amt` mean values along `axis`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to append. num : int Depth into `arr` along `axis` to calculate mean. Range: [1, `arr.shape[axis]`] or None (entire axis) axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` values appended along `axis`. The appended region is the maximum of the final `num` values along `axis`. rrNc3s2|]*\}}|ˆkrtdƒntˆˆˆdƒVqdS)Nr)r$)rr r )r r,r2rr r sz_append_mean..css|]}tdƒVqdS)N)r$)rr rrr r sc3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr rs)r ) r)r rrr8rrrrrrr)rrr2r r9r&r:r)r r,r2r Ú _append_meanâs&   r<csº|dkr |Sˆdkr t||ˆƒSˆdk r:ˆ|jˆkr:d‰t‡‡fdd„t|jƒDƒƒ}t‡fdd„t|jƒDƒƒ}tj||ˆd |¡}t||jƒtj |  |ˆ¡  |j¡|fˆdS)aI Prepend `pad_amt` median values along `axis`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to prepend. num : int Depth into `arr` along `axis` to calculate median. Range: [1, `arr.shape[axis]`] or None (entire axis) axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` values prepended along `axis`. The prepended region is the median of the first `num` values along `axis`. rrNc3s*|]"\}}|ˆkrtdƒntˆƒVqdS)N)r$)rr r )r r2rr r?sz_prepend_med..c3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr rCs)r ) r(r rrrÚmedianrrrrrr)rrr2r Ú med_slicer&Ú med_chunkr)r r2r Ú _prepend_meds   r@csè|dkr |Sˆdkr t||ˆƒSˆdk r:ˆ|jˆkr:d‰|jˆd‰ˆdk rrt‡‡‡fdd„t|jƒDƒƒ}ntdd„|jDƒƒ}t‡fdd„t|jƒDƒƒ}tj||ˆd |¡}t||jƒtj ||  |ˆ¡  |j¡fˆdS) aE Append `pad_amt` median values along `axis`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to append. num : int Depth into `arr` along `axis` to calculate median. Range: [1, `arr.shape[axis]`] or None (entire axis) axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` values appended along `axis`. The appended region is the median of the final `num` values along `axis`. rrNc3s2|]*\}}|ˆkrtdƒntˆˆˆdƒVqdS)Nr)r$)rr r )r r,r2rr rvsz_append_med..css|]}tdƒVqdS)N)r$)rr rrr rysc3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr r|s)r ) r)r rrrr=rrrrrr)rrr2r r>r&r?r)r r,r2r Ú _append_medOs&   rAcs¦|dkr |Sˆdkr t||ˆƒSˆdk r:ˆ|jˆkr:d‰t‡‡fdd„t|jƒDƒƒ}t‡fdd„t|jƒDƒƒ}||jˆd |¡}tj|j|ˆd|fˆdS)aT Prepend `pad_amt` minimum values along `axis`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to prepend. num : int Depth into `arr` along `axis` to calculate minimum. Range: [1, `arr.shape[axis]`] or None (entire axis) axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` values prepended along `axis`. The prepended region is the minimum of the first `num` values along `axis`. rrNc3s*|]"\}}|ˆkrtdƒntˆƒVqdS)N)r$)rr r )r r2rr r­sz_prepend_min..c3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr r±s)r ) r(r rrÚminrrrr)rrr2r Ú min_slicer&Ú min_chunkr)r r2r Ú _prepend_minˆs  rEcsÔ|dkr |Sˆdkr t||ˆƒSˆdk r:ˆ|jˆkr:d‰|jˆd‰ˆdk rrt‡‡‡fdd„t|jƒDƒƒ}ntdd„|jDƒƒ}t‡fdd„t|jƒDƒƒ}||jˆd |¡}tj||j|ˆdfˆdS) aG Append `pad_amt` median values along `axis`. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : int Amount of padding to append. num : int Depth into `arr` along `axis` to calculate minimum. Range: [1, `arr.shape[axis]`] or None (entire axis) axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt` values appended along `axis`. The appended region is the minimum of the final `num` values along `axis`. rrNc3s2|]*\}}|ˆkrtdƒntˆˆˆdƒVqdS)Nr)r$)rr r )r r,r2rr rãsz_append_min..css|]}tdƒVqdS)N)r$)rr rrr ræsc3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr rés)r ) r)r rrrBrrrr)rrr2r rCr&rDr)r r,r2r Ú _append_min¼s$  rFc sšˆddkrˆddkr|St‡‡fdd„t|jƒDƒƒ}||}t‡fdd„t|jƒDƒƒ}ˆddkrt| |¡}d|krÀˆddkrÀt‡fdd„t|jƒDƒƒ}|| |¡}d||}~|jˆˆdd‰|jˆd‰t‡‡‡fd d„t|jƒDƒƒ}t‡fd d„t|jƒDƒƒ} ||| } ˆddkrD|  |¡} d|kr†t‡fd d„t|jƒDƒƒ} ||  |¡}d|| } ~tj||| fˆd S) aÙ Pad `axis` of `arr` by reflection. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : tuple of ints, length 2 Padding to (prepend, append) along `axis`. method : str Controls method of reflection; options are 'even' or 'odd'. axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt[0]` values prepended and `pad_amt[1]` values appended along `axis`. Both regions are padded with reflected values from the original array. Notes ----- This algorithm does not pad with repetition, i.e. the edges are not repeated in the reflection. For that behavior, use `mode='symmetric'`. The modes 'reflect', 'symmetric', and 'wrap' must be padded with a single function, lest the indexing tricks in non-integer multiples of the original shape would violate repetition in the final iteration. rrc3s2|]*\}}|ˆkrtdƒntˆdddƒVqdS)Nrr)r$)rr r )r rrr rsz_pad_ref..c3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr r"sÚoddc3s&|]\}}|ˆkrtdƒndVqdS)Nr)r$)rr r )r rr r)séc3s,|]$\}}|ˆkrtdƒntˆˆƒVqdS)N)r$)rr r )r r,Ústartrr r5sc3s.|]&\}}|ˆkrtdƒn tdddƒVqdS)Nr)r$)rr r )r rr r7sc3s&|]\}}|ˆkrtdƒndVqdS)Nr)r$)rr r )r rr r?s)r )rrr rrr) rrÚmethodr Z ref_sliceZ ref_chunk1r&Ú edge_slice1Ú edge_chunkÚrev_idxZ ref_chunk2Ú edge_slice2r)r r,rrIr Ú_pad_refôs>!           rOc s–ˆddkrˆddkr|St‡‡fdd„t|jƒDƒƒ}t‡fdd„t|jƒDƒƒ}|||}t‡fdd„t|jƒDƒƒ}ˆddkr”| |¡}d|kràˆddkràt‡fdd„t|jƒDƒƒ}|| |¡} d | |}~ |jˆˆd‰|jˆ‰t‡‡‡fd d„t|jƒDƒƒ}|||} ˆddkr@|  |¡} d|kr‚t‡fd d„t|jƒDƒƒ} ||  |¡} d | | } ~ tj||| fˆd S) aÊ Pad `axis` of `arr` by symmetry. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : tuple of ints, length 2 Padding to (prepend, append) along `axis`. method : str Controls method of symmetry; options are 'even' or 'odd'. axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt[0]` values prepended and `pad_amt[1]` values appended along `axis`. Both regions are padded with symmetric values from the original array. Notes ----- This algorithm DOES pad with repetition, i.e. the edges are repeated. For padding without repeated edges, use `mode='reflect'`. The modes 'reflect', 'symmetric', and 'wrap' must be padded with a single function, lest the indexing tricks in non-integer multiples of the original shape would violate repetition in the final iteration. rrc3s0|](\}}|ˆkrtdƒn tdˆdƒVqdS)Nr)r$)rr r )r rrr rqsz_pad_sym..c3s.|]&\}}|ˆkrtdƒn tdddƒVqdS)Nr)r$)rr r )r rr rssc3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr rxsrGc3s&|]\}}|ˆkrtdƒndVqdS)Nr)r$)rr r )r rr rsrHc3s,|]$\}}|ˆkrtdƒntˆˆƒVqdS)N)r$)rr r )r r,rIrr r‹sc3s&|]\}}|ˆkrtdƒndVqdS)Nr)r$)rr r )r rr r“s)r )rrr rrr) rrrJr Z sym_slicerMZ sym_chunk1r&rKrLZ sym_chunk2rNr)r r,rrIr Ú_pad_symIs>!             rPcsâˆddkrˆddkr|S|jˆˆd‰|jˆ‰t‡‡‡fdd„t|jƒDƒƒ}||}t‡fdd„t|jƒDƒƒ}ˆddkr’| |¡}t‡‡fdd„t|jƒDƒƒ}||}ˆddkrÎ| |¡}tj|||fˆdS)a1 Pad `axis` of `arr` via wrapping. Parameters ---------- arr : ndarray Input array of arbitrary shape. pad_amt : tuple of ints, length 2 Padding to (prepend, append) along `axis`. axis : int Axis along which to pad `arr`. Returns ------- padarr : ndarray Output array, with `pad_amt[0]` values prepended and `pad_amt[1]` values appended along `axis`. Both regions are padded wrapped values from the opposite end of `axis`. Notes ----- This method of padding is also known as 'tile' or 'tiling'. The modes 'reflect', 'symmetric', and 'wrap' must be padded with a single function, lest the indexing tricks in non-integer multiples of the original shape would violate repetition in the final iteration. rrc3s,|]$\}}|ˆkrtdƒntˆˆƒVqdS)N)r$)rr r )r r,rIrr rÄsz_pad_wrap..c3s"|]\}}|ˆkr|ndVqdS)rNr)rr r )r rr rÉsc3s0|](\}}|ˆkrtdƒn tdˆdƒVqdS)Nrr)r$)rr r )r rrr rÒs)r )r rrrrr)rrr Z wrap_sliceZ wrap_chunk1r&Z wrap_chunk2r)r r,rrIr Ú _pad_wraps"      rQTcCsŠ|j}|dkrd|St |¡}yt ||df¡}Wn&tk rZd}t||fƒ‚YnX|dkrtt |¡ t¡}tdd„|  ¡DƒƒS)aZ Private function which does some checks and normalizes the possibly much simpler representations of 'pad_width', 'stat_length', 'constant_values', 'end_values'. Parameters ---------- narray : ndarray Input ndarray shape : {sequence, array_like, float, int}, optional The width of padding (pad_width), the number of elements on the edge of the narray used for statistics (stat_length), the constant value(s) to use when filling padded regions (constant_values), or the endpoint target(s) for linear ramps (end_values). ((before_1, after_1), ... (before_N, after_N)) unique number of elements for each axis where `N` is rank of `narray`. ((before, after),) yields same before and after constants for each axis. (constant,) or val is a shortcut for before = after = constant for all axes. cast_to_int : bool, optional Controls if values in ``shape`` will be rounded and cast to int before being returned. Returns ------- normalized_shape : tuple of tuples val => ((val, val), (val, val), ...) [[val1, val2], [val3, val4], ...] => ((val1, val2), (val3, val4), ...) ((val1, val2), (val3, val4), ...) => no change [[val1, val2], ] => ((val1, val2), (val1, val2), ...) ((val1, val2), ) => ((val1, val2), (val1, val2), ...) [[val , ], ] => ((val, val), (val, val), ...) ((val , ), ) => ((val, val), (val, val), ...) N))NNrHz/Unable to create correctly shaped tuple from %sTcss|]}t|ƒVqdS)N)r)rr rrr rsz#_normalize_shape..) ÚndimrÚasarrayZ broadcast_toÚ ValueErrorrrÚintrÚtolist)Zndarrayr Ú cast_to_intZndimsZ shape_arrÚfmtrrr Ú_normalize_shapeÝs% rYcCsbt||ƒ}xR|D]J}dd„|Dƒ}dd„|Dƒ}|ddksH|ddkrd}t||fƒ‚qW|S)aÜ Private function which does some checks and reformats pad_width and stat_length using _normalize_shape. Parameters ---------- narray : ndarray Input ndarray number_elements : {sequence, int}, optional The width of padding (pad_width) or the number of elements on the edge of the narray used for statistics (stat_length). ((before_1, after_1), ... (before_N, after_N)) unique number of elements for each axis. ((before, after),) yields same before and after constants for each axis. (constant,) or int is a shortcut for before = after = constant for all axes. Returns ------- _validate_lengths : tuple of tuples int => ((int, int), (int, int), ...) [[int1, int2], [int3, int4], ...] => ((int1, int2), (int3, int4), ...) ((int1, int2), (int3, int4), ...) => no change [[int1, int2], ] => ((int1, int2), (int1, int2), ...) ((int1, int2), ) => ((int1, int2), (int1, int2), ...) [[int , ], ] => ((int, int), (int, int), ...) ((int , ), ) => ((int, int), (int, int), ...) cSsg|]}|dkrdn|‘qS)Nrr)rr rrr ú :sz%_validate_lengths..cSsg|]}|dkrdnd‘qS)rrrr)rr rrr rZ;srrz"%s cannot contain negative values.)rYrT)ÚnarrayZnumber_elementsZnormshpr ZchkrXrrr Ú_validate_lengthss  r\c s–t ˆ¡jjdkstdƒ‚t |¡‰tˆˆƒ‰dggdgdgdgdgdgdgdggdœ }dd d d d œ}t|tjj ƒrx,|D]$}|||kr|t d |||fƒ‚q|Wx ||D]}|  |||¡q®WxÐ|D]<}|dkrêtˆ||ƒ||<|d krÌt ˆ||dd||<qÌWnŠ|} t tˆjƒƒ} ‡fdd„| Dƒ} ‡‡fdd„| Dƒ} t ˆj¡| } t | ˆj¡}ˆ|| <x&| D]}t | ||ˆ|||¡qrW|Sˆ ¡}|dkrøxHttˆ|dƒƒD]2\}\\}}\}}t||||ƒ}t||||ƒ}q¾Wnš|dkr:x2tˆƒD]&\}\}}t|||ƒ}t|||ƒ}q WnX|dkr’xHttˆ|dƒƒD]2\}\\}}\}}t||||ƒ}t||||ƒ}qXWn|dkrêxHttˆ|dƒƒD]2\}\\}}\}}t||||ƒ}t||||ƒ}q°Wn¨|dkrBxHttˆ|dƒƒD]2\}\\}}\}}t||||ƒ}t||||ƒ}qWnP|dkršxHttˆ|dƒƒD]2\}\\}}\}}t ||||ƒ}t!||||ƒ}q`Wnø|dkròxHttˆ|dƒƒD]2\}\\}}\}}t"||||ƒ}t#||||ƒ}q¸Wn |dkr,x’tˆƒD]\}\}}ˆj|d krL|d ks:|d krt d $|¡ƒ‚q|d ks`|d krŒ|j|dkrŒt|||ƒ}t|||ƒ}q|d}|j|d}xl||ks¸||krt%||||ƒ}t%||||ƒ}t&|||f||ƒ}||8}||8}|||7}q¤Wt&|||f||ƒ}qWnf|dkræx¬tˆƒD] \}\}}|d}|j|}xl||kst||krÊt%||||ƒ}t%||||ƒ}t'|||f||ƒ}||8}||8}|||7}q`Wt'|||f||ƒ}q@Wn¬|dkr’x tˆƒD]”\}\}}|j|}xj||ks&||krzt%||||ƒ}t%||||ƒ}t(|||f|ƒ}||8}||8}|||7}qWt(|||f|ƒ}qúW|S)a¹ Pads an array. Parameters ---------- array : array_like of rank N Input array pad_width : {sequence, array_like, int} Number of values padded to the edges of each axis. ((before_1, after_1), ... (before_N, after_N)) unique pad widths for each axis. ((before, after),) yields same before and after pad for each axis. (pad,) or int is a shortcut for before = after = pad width for all axes. mode : str or function One of the following string values or a user supplied function. 'constant' Pads with a constant value. 'edge' Pads with the edge values of array. 'linear_ramp' Pads with the linear ramp between end_value and the array edge value. 'maximum' Pads with the maximum value of all or part of the vector along each axis. 'mean' Pads with the mean value of all or part of the vector along each axis. 'median' Pads with the median value of all or part of the vector along each axis. 'minimum' Pads with the minimum value of all or part of the vector along each axis. 'reflect' Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis. 'symmetric' Pads with the reflection of the vector mirrored along the edge of the array. 'wrap' Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning. Padding function, see Notes. stat_length : sequence or int, optional Used in 'maximum', 'mean', 'median', and 'minimum'. Number of values at edge of each axis used to calculate the statistic value. ((before_1, after_1), ... (before_N, after_N)) unique statistic lengths for each axis. ((before, after),) yields same before and after statistic lengths for each axis. (stat_length,) or int is a shortcut for before = after = statistic length for all axes. Default is ``None``, to use the entire axis. constant_values : sequence or int, optional Used in 'constant'. The values to set the padded values for each axis. ((before_1, after_1), ... (before_N, after_N)) unique pad constants for each axis. ((before, after),) yields same before and after constants for each axis. (constant,) or int is a shortcut for before = after = constant for all axes. Default is 0. end_values : sequence or int, optional Used in 'linear_ramp'. The values used for the ending value of the linear_ramp and that will form the edge of the padded array. ((before_1, after_1), ... (before_N, after_N)) unique end values for each axis. ((before, after),) yields same before and after end values for each axis. (constant,) or int is a shortcut for before = after = end value for all axes. Default is 0. reflect_type : {'even', 'odd'}, optional Used in 'reflect', and 'symmetric'. The 'even' style is the default with an unaltered reflection around the edge value. For the 'odd' style, the extented part of the array is created by subtracting the reflected values from two times the edge value. Returns ------- pad : ndarray Padded array of rank equal to `array` with shape increased according to `pad_width`. Notes ----- .. versionadded:: 1.7.0 For an array with rank greater than 1, some of the padding of later axes is calculated from padding of previous axes. This is easiest to think about with a rank 2 array where the corners of the padded array are calculated by using padded values from the first axis. The padding function, if used, should return a rank 1 array equal in length to the vector argument with padded values replaced. It has the following signature:: padding_func(vector, iaxis_pad_width, iaxis, **kwargs) where vector : ndarray A rank 1 array already padded with zeros. Padded values are vector[:pad_tuple[0]] and vector[-pad_tuple[1]:]. iaxis_pad_width : tuple A 2-tuple of ints, iaxis_pad_width[0] represents the number of values padded at the beginning of vector where iaxis_pad_width[1] represents the number of values padded at the end of vector. iaxis : int The axis currently being calculated. kwargs : misc Any keyword arguments the function requires. Examples -------- >>> a = [1, 2, 3, 4, 5] >>> np.lib.pad(a, (2,3), 'constant', constant_values=(4, 6)) array([4, 4, 1, 2, 3, 4, 5, 6, 6, 6]) >>> np.lib.pad(a, (2, 3), 'edge') array([1, 1, 1, 2, 3, 4, 5, 5, 5, 5]) >>> np.lib.pad(a, (2, 3), 'linear_ramp', end_values=(5, -4)) array([ 5, 3, 1, 2, 3, 4, 5, 2, -1, -4]) >>> np.lib.pad(a, (2,), 'maximum') array([5, 5, 1, 2, 3, 4, 5, 5, 5]) >>> np.lib.pad(a, (2,), 'mean') array([3, 3, 1, 2, 3, 4, 5, 3, 3]) >>> np.lib.pad(a, (2,), 'median') array([3, 3, 1, 2, 3, 4, 5, 3, 3]) >>> a = [[1, 2], [3, 4]] >>> np.lib.pad(a, ((3, 2), (2, 3)), 'minimum') array([[1, 1, 1, 2, 1, 1, 1], [1, 1, 1, 2, 1, 1, 1], [1, 1, 1, 2, 1, 1, 1], [1, 1, 1, 2, 1, 1, 1], [3, 3, 3, 4, 3, 3, 3], [1, 1, 1, 2, 1, 1, 1], [1, 1, 1, 2, 1, 1, 1]]) >>> a = [1, 2, 3, 4, 5] >>> np.lib.pad(a, (2, 3), 'reflect') array([3, 2, 1, 2, 3, 4, 5, 4, 3, 2]) >>> np.lib.pad(a, (2, 3), 'reflect', reflect_type='odd') array([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]) >>> np.lib.pad(a, (2, 3), 'symmetric') array([2, 1, 1, 2, 3, 4, 5, 5, 4, 3]) >>> np.lib.pad(a, (2, 3), 'symmetric', reflect_type='odd') array([0, 1, 1, 2, 3, 4, 5, 5, 6, 7]) >>> np.lib.pad(a, (2, 3), 'wrap') array([4, 5, 1, 2, 3, 4, 5, 1, 2, 3]) >>> def padwithtens(vector, pad_width, iaxis, kwargs): ... vector[:pad_width[0]] = 10 ... vector[-pad_width[1]:] = 10 ... return vector >>> a = np.arange(6) >>> a = a.reshape((2, 3)) >>> np.lib.pad(a, 2, padwithtens) array([[10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10], [10, 10, 0, 1, 2, 10, 10], [10, 10, 3, 4, 5, 10, 10], [10, 10, 10, 10, 10, 10, 10], [10, 10, 10, 10, 10, 10, 10]]) r z%`pad_width` must be of integral type.Úconstant_valuesÚ end_valuesÚ stat_lengthÚ reflect_type) ÚconstantÚedgeÚ linear_rampÚmaximumr8r=ÚminimumÚreflectÚ symmetricÚwrapNrZeven)r_r]r^r`z%%s keyword not in allowed keywords %s)r^r]F)rWcsg|]}t ˆ|¡‘qSr)rÚsum)rr )Ú pad_widthrr rZ>szpad..cs0g|](}tˆ|dˆ|dˆj|ƒ‘qS)r)r$r )rr )r[rjrr rZ?srarbrcrdr8r=rerfz:There aren't any elements to reflect in axis {} of `array`rrgrh))rrSrZkindÚ TypeErrorÚarrayr\Ú isinstanceÚcompatZ basestringrTÚ setdefaultrYÚlistÚrangerRr rZapply_along_axisÚcopyrÚzipr"r#r(r)r0r1r6r7r;r<r@rArErFÚformatrBrOrPrQ)rlrjÚmodeÚkwargsZ allowedkwargsZ kwdefaultsÚkeyÚkwr ZfunctionZrankZtotal_dim_increaseZ offset_slicesZ new_shapeZnewmatZiaxisr Z pad_beforeZ pad_afterZ before_valZ after_valZ chunk_beforeZ chunk_afterrJZsafe_padZ pad_iter_bZ pad_iter_ar)r[rjr rFsF          &   & & & & &              )F)r)r)r)r)r)r)r)r)r)r)r)r)r)r)r)r)r)T)Ú__doc__Z __future__rrrZnumpyrÚ__all__rrr"r#r(r)r0r1r6r7r;r<r@rArErFrOrPrQrYr\rrrrr Ús2 + " " ! " 7 7 4 8 4 9 4 9 4 8 U T @ <-