Skip to content

Commit b6c2c73

Browse files
authored
ResamplingRenderer (#230)
* k-space resampling (as I understand it but not working that well) * insert_into as standalone function * added Lanczos renderer (test is not good yet); reversed coordinate convention for resample2d * added Moments.convolve (+test); increased ProfileMorphology shape to 15*size * fixed import for Lanczos resampler * undid my changes to resampling renderer * added Moment methods; redefined center shift for ProfileMorphology accordingly * introduced translation to ResamplingRenderer; whole slew of minor corrections of Frame/WCS * use jacobian only for resample_fourier * made PSF (de)convolution optional in ResamplingRenderer * switched test_renderer to Moment-based tests * minor fix * minor fixes; removed source validation checks * import scarlet2 as sc2
1 parent dd83017 commit b6c2c73

File tree

15 files changed

+826
-488
lines changed

15 files changed

+826
-488
lines changed

docs/howto/multiresolution.ipynb

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
"from astropy.table import Table\n",
2626
"from astropy.wcs import WCS\n",
2727
"\n",
28-
"import scarlet2\n",
28+
"import scarlet2 as sc2\n",
2929
"\n",
30-
"scarlet2.set_validation(False)"
30+
"sc2.set_validation(False)"
3131
]
3232
},
3333
{
@@ -114,14 +114,14 @@
114114
"outputs": [],
115115
"source": [
116116
"# Scarlet Observations\n",
117-
"obs_hst = scarlet2.Observation(\n",
118-
" data_hst, wcs=wcs_hst, psf=scarlet2.ArrayPSF(psf_hst_data), channels=[\"F814W\"], weights=obs_hst_weights\n",
117+
"obs_hst = sc2.Observation(\n",
118+
" data_hst, wcs=wcs_hst, psf=sc2.ArrayPSF(psf_hst_data), channels=[\"F814W\"], weights=obs_hst_weights\n",
119119
")\n",
120120
"\n",
121-
"obs_hsc = scarlet2.Observation(\n",
121+
"obs_hsc = sc2.Observation(\n",
122122
" data_hsc,\n",
123123
" wcs=wcs_hsc,\n",
124-
" psf=scarlet2.ArrayPSF(psf_hsc_data),\n",
124+
" psf=sc2.ArrayPSF(psf_hsc_data),\n",
125125
" channels=[\"g\", \"r\", \"i\", \"z\", \"y\"],\n",
126126
" weights=obs_hsc_weights,\n",
127127
")"
@@ -149,7 +149,7 @@
149149
},
150150
"outputs": [],
151151
"source": [
152-
"model_frame = scarlet2.Frame.from_observations(\n",
152+
"model_frame = sc2.Frame.from_observations(\n",
153153
" observations=[obs_hst, obs_hsc],\n",
154154
" coverage=\"union\", # or \"intersection\"\n",
155155
")"
@@ -182,13 +182,13 @@
182182
"metadata": {},
183183
"outputs": [],
184184
"source": [
185-
"norm_hst = scarlet2.plot.AsinhAutomaticNorm(obs_hst)\n",
186-
"norm_hsc = scarlet2.plot.AsinhAutomaticNorm(obs_hsc)\n",
185+
"norm_hst = sc2.plot.AsinhAutomaticNorm(obs_hst)\n",
186+
"norm_hsc = sc2.plot.AsinhAutomaticNorm(obs_hsc)\n",
187187
"\n",
188-
"scarlet2.plot.observation(\n",
188+
"sc2.plot.observation(\n",
189189
" obs_hst, norm=norm_hst, sky_coords=ra_dec, show_psf=True, label_kwargs={\"color\": \"red\"}\n",
190190
")\n",
191-
"scarlet2.plot.observation(obs_hsc, norm=norm_hsc, sky_coords=ra_dec, show_psf=True);"
191+
"sc2.plot.observation(obs_hsc, norm=norm_hsc, sky_coords=ra_dec, show_psf=True);"
192192
]
193193
},
194194
{
@@ -206,16 +206,15 @@
206206
"metadata": {},
207207
"outputs": [],
208208
"source": [
209-
"import scarlet2.init as init\n",
210209
"\n",
211-
"with scarlet2.Scene(model_frame) as scene:\n",
210+
"with sc2.Scene(model_frame) as scene:\n",
212211
" for i, center in enumerate(ra_dec):\n",
213212
" try:\n",
214-
" spectrum, morph = init.from_gaussian_moments([obs_hst, obs_hsc], center, min_corr=0.99)\n",
213+
" spectrum, morph = sc2.init.from_gaussian_moments([obs_hst, obs_hsc], center, min_corr=0.99)\n",
215214
" except ValueError:\n",
216-
" spectrum = init.pixel_spectrum([obs_hst, obs_hsc], center)\n",
217-
" morph = init.compact_morphology()\n",
218-
" scarlet2.Source(center, spectrum, morph)"
215+
" spectrum = sc2.init.pixel_spectrum([obs_hst, obs_hsc], center)\n",
216+
" morph = sc2.init.compact_morphology()\n",
217+
" sc2.Source(center, spectrum, morph)"
219218
]
220219
},
221220
{
@@ -225,7 +224,7 @@
225224
"metadata": {},
226225
"outputs": [],
227226
"source": [
228-
"scarlet2.plot.scene(\n",
227+
"sc2.plot.scene(\n",
229228
" scene,\n",
230229
" observation=obs_hst,\n",
231230
" show_rendered=True,\n",
@@ -235,7 +234,7 @@
235234
" add_boxes=True,\n",
236235
" label_kwargs={\"color\": \"red\"},\n",
237236
")\n",
238-
"scarlet2.plot.scene(\n",
237+
"sc2.plot.scene(\n",
239238
" scene,\n",
240239
" observation=obs_hsc,\n",
241240
" show_rendered=True,\n",
@@ -246,16 +245,6 @@
246245
");"
247246
]
248247
},
249-
{
250-
"cell_type": "code",
251-
"execution_count": null,
252-
"id": "5af1c3e91450403a",
253-
"metadata": {},
254-
"outputs": [],
255-
"source": [
256-
"obs_hsc.renderer"
257-
]
258-
},
259248
{
260249
"cell_type": "markdown",
261250
"id": "5765f3385e73e562",
@@ -290,20 +279,19 @@
290279
"source": [
291280
"from numpyro.distributions import constraints\n",
292281
"from functools import partial\n",
293-
"from scarlet2.module import relative_step\n",
294282
"\n",
295-
"spec_step = partial(relative_step, factor=0.05)\n",
296-
"morph_step = partial(relative_step, factor=1e-3)\n",
283+
"spec_step = partial(sc2.relative_step, factor=0.05)\n",
284+
"morph_step = partial(sc2.relative_step, factor=1e-3)\n",
297285
"\n",
298-
"with scarlet2.Parameters(scene) as parameters:\n",
286+
"with sc2.Parameters(scene) as parameters:\n",
299287
" for i in range(len(scene.sources)):\n",
300-
" scarlet2.Parameter(\n",
288+
" sc2.Parameter(\n",
301289
" scene.sources[i].spectrum,\n",
302290
" name=f\"spectrum.{i}\",\n",
303291
" constraint=constraints.positive,\n",
304292
" stepsize=spec_step,\n",
305293
" )\n",
306-
" scarlet2.Parameter(\n",
294+
" sc2.Parameter(\n",
307295
" scene.sources[i].morphology,\n",
308296
" name=f\"morph.{i}\",\n",
309297
" constraint=constraints.unit_interval,\n",
@@ -346,7 +334,7 @@
346334
"metadata": {},
347335
"outputs": [],
348336
"source": [
349-
"scarlet2.plot.scene(\n",
337+
"sc2.plot.scene(\n",
350338
" scene_,\n",
351339
" observation=obs_hst,\n",
352340
" show_rendered=True,\n",
@@ -358,7 +346,7 @@
358346
" box_kwargs={\"edgecolor\": \"red\", \"facecolor\": \"none\"},\n",
359347
" label_kwargs={\"color\": \"red\"},\n",
360348
")\n",
361-
"scarlet2.plot.scene(\n",
349+
"sc2.plot.scene(\n",
362350
" scene_,\n",
363351
" observation=obs_hsc,\n",
364352
" show_rendered=True,\n",

src/scarlet2/bbox.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,6 @@ def contains(self, p):
8989
return False
9090
return True
9191

92-
def insert_into(self, image, sub):
93-
"""Insert `sub` into `image` according to this bbox
94-
95-
Inverse operation to :func:`~scarlet.bbox.Box.extract_from`.
96-
97-
Parameters
98-
----------
99-
image: array
100-
Full image
101-
sub: array
102-
Extracted sub-image
103-
104-
Returns
105-
-------
106-
image: array
107-
"""
108-
imbox = Box(image.shape)
109-
110-
im_slices, sub_slices = overlap_slices(imbox, self)
111-
image[im_slices] = sub[sub_slices]
112-
return image
113-
11492
def get_extent(self):
11593
"""Return the start and end coordinates."""
11694
return [self.start[-1], self.stop[-1], self.start[-2], self.stop[-2]]
@@ -283,3 +261,31 @@ def overlap_slices(bbox1, bbox2, return_boxes=False):
283261
_bbox2.slices,
284262
)
285263
return slices
264+
265+
266+
def insert_into(image, sub, bbox):
267+
"""Insert `sub` into `image` according to this bbox
268+
269+
Inverse operation to :func:`~scarlet.bbox.Box.extract_from`.
270+
271+
Parameters
272+
----------
273+
image: array
274+
Full image
275+
sub: array
276+
Smaller sub-image
277+
bbox: Box
278+
Bounding box that describes the shape and position of `sub` in the pixel coordinates of `image`.
279+
Returns
280+
-------
281+
image: array
282+
Image with `sub` inserted at `bbox`.
283+
"""
284+
imbox = Box(image.shape)
285+
286+
im_slices, sub_slices = overlap_slices(imbox, bbox)
287+
try:
288+
image[im_slices] = sub[sub_slices] # numpy arrays
289+
except TypeError:
290+
image = image.at[im_slices].set(sub[sub_slices]) # jax arrays
291+
return image

0 commit comments

Comments
 (0)