Skip to content

Commit

Permalink
New subarray function
Browse files Browse the repository at this point in the history
  • Loading branch information
andykee committed Nov 26, 2024
1 parent 5f471c0 commit 4cd160a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/ref/util.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Array manipulation
lentil.boundary
lentil.centroid
lentil.pad
lentil.subarray
lentil.window
lentil.rebin
lentil.rescale
Expand Down
1 change: 1 addition & 0 deletions lentil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from lentil.util import (
centroid,
pad,
subarray,
window,
boundary,
rebin,
Expand Down
37 changes: 37 additions & 0 deletions lentil/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,43 @@ def pad(array, shape):
return padded


def subarray(a, shape, shift=(0,0)):
"""Extract a contiguous subarray from a larger array.
The subarray is extracted about the center of the source array unless
a shift is specified.
Parameters
----------
a : array_like
Source array
shape : array_like of ints
Shape of subarray array in ``(nrows, ncols)``.
shift : array_like of ints
Relative shift of the center of the subarray in ``(row, col)``.
Returns
-------
out : ndarray
Subarray extracted from the source array.
"""

a = np.asarray(a)
shape = np.asarray(shape)

rmin = a.shape[0]//2 - shape[0]//2 + shift[0]
cmin = a.shape[1]//2 - shape[1]//2 + shift[1]
rmax = rmin + shape[0]
cmax = cmin + shape[1]

print(rmin, rmax, cmin, cmax)

if any((rmin<0, cmin<0, rmax>a.shape[0], cmax>a.shape[1])):
raise ValueError('window lies outside of array')

return a[rmin:rmax, cmin:cmax]


def window(img, shape=None, slice=None):
"""Extract an appropriately sized, potentially windowed array
Expand Down

0 comments on commit 4cd160a

Please sign in to comment.