Open barentsen opened 9 years ago
imageutils
has been merged into Astropy core in https://github.com/astropy/astropy/pull/3201 but apparently the downsample
function wasn't ... as far as I can see it's currently not in Astropy core or photutils!?
@astrofrog @larrybradley Do we want to provide the downsample
function?
Do you think there's still a need for imageutils
or should all functionality go into Astropy core or photutils directly from now on?
@barentsen downsample
/upsample
are Cython
functions, which require strict data typing (here data must be a float64). I think allowing other types would require a python wrapper around the Cython function to recast the data type (or perhaps this could be done within the Cython code?).
@cdeil I think these functions weren't added to astropy
because there is a way to do this (downsample/upsample) using numpy broadcasting tricks (although that method is a bit slower). I'll have to go back through the long comments on that PR to refresh my memory.
Here is a one-line equivalent of downsample
:
data.reshape(data.shape[0] / factor, factor, data.shape[1] / factor, factor).sum(axis=(1, 3))
However, it will work only when the data
shape is an exact multiple of factor
. I think(?) the imageutils.downsample
function would handle this (essentially by trimming the data
).
The numpy one-liner is perhaps somewhat obscure -- I didn't think of it. I'd say the act of binning data is common enough to deserve a user-friendly function in astropy?
I agree (the one-line equivalent of upsample
is even more obscure). I'll submit an astropy
PR with the numpy one-line versions of both downsample
and upsample
(and adding the automatic trimming, if necessary).
Here is a version that works including the trimming of the data:
def downsample(image, factor):
nx_new = image.shape[1] // factor
ny_new = image.shape[0] // factor
nx_init = nx_new * factor
ny_init = ny_new * factor
return (image[:ny_init, :nx_init]
.reshape((ny_new, factor, nx_new, factor))
.mean(axis=3).mean(axis=1))
astropy
PR is here: https://github.com/astropy/astropy/pull/3453
It looks like
downsample
does not accept an array withdtype=int
. Given that many images are stored as ints, it would be nice if it did?Of course this works fine: