Skip to content

Commit d617396

Browse files
authored
Pair similarity regularization (#273)
* added component cross-entropy regularizer * added per-band responsibility option * added Responsibility class to collect options * added PairSimilarity loss as more specific regularizer * example uses of new regularizer * more accurate similarity calculation * made similarity weight relative to NLL; updated notebooks * removed cross-entropy responsibility penalty; faster similarity calc * reduced iterations for LSBG fit to avoid RTD timeout
1 parent f127789 commit d617396

4 files changed

Lines changed: 401 additions & 53 deletions

File tree

docs/0-example.ipynb

Lines changed: 104 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
{
1313
"cell_type": "code",
1414
"execution_count": null,
15-
"metadata": {},
15+
"metadata": {
16+
"ExecuteTime": {
17+
"end_time": "2026-05-06T03:30:43.079757Z",
18+
"start_time": "2026-05-06T03:30:41.963960Z"
19+
}
20+
},
1621
"outputs": [],
1722
"source": [
1823
"# Import Packages and setup\n",
@@ -35,7 +40,12 @@
3540
{
3641
"cell_type": "code",
3742
"execution_count": null,
38-
"metadata": {},
43+
"metadata": {
44+
"ExecuteTime": {
45+
"end_time": "2026-05-06T03:30:43.322047Z",
46+
"start_time": "2026-05-06T03:30:43.080885Z"
47+
}
48+
},
3949
"outputs": [],
4050
"source": [
4151
"from huggingface_hub import hf_hub_download\n",
@@ -84,7 +94,12 @@
8494
{
8595
"cell_type": "code",
8696
"execution_count": null,
87-
"metadata": {},
97+
"metadata": {
98+
"ExecuteTime": {
99+
"end_time": "2026-05-06T03:30:44.358652Z",
100+
"start_time": "2026-05-06T03:30:43.323163Z"
101+
}
102+
},
88103
"outputs": [],
89104
"source": [
90105
"obs = sc2.Observation(data, weights=weights, psf=psf, channels=channels, name=\"HSC\")"
@@ -114,7 +129,12 @@
114129
{
115130
"cell_type": "code",
116131
"execution_count": null,
117-
"metadata": {},
132+
"metadata": {
133+
"ExecuteTime": {
134+
"end_time": "2026-05-06T03:30:44.928542Z",
135+
"start_time": "2026-05-06T03:30:44.382265Z"
136+
}
137+
},
118138
"outputs": [],
119139
"source": [
120140
"norm = sc2.plot.AsinhAutomaticNorm(obs)\n",
@@ -132,7 +152,12 @@
132152
{
133153
"cell_type": "code",
134154
"execution_count": null,
135-
"metadata": {},
155+
"metadata": {
156+
"ExecuteTime": {
157+
"end_time": "2026-05-06T03:30:46.307450Z",
158+
"start_time": "2026-05-06T03:30:44.941167Z"
159+
}
160+
},
136161
"outputs": [],
137162
"source": [
138163
"model_frame = sc2.Frame.from_observations(obs)\n",
@@ -159,7 +184,12 @@
159184
{
160185
"cell_type": "code",
161186
"execution_count": null,
162-
"metadata": {},
187+
"metadata": {
188+
"ExecuteTime": {
189+
"end_time": "2026-05-06T03:30:47.519240Z",
190+
"start_time": "2026-05-06T03:30:46.321171Z"
191+
}
192+
},
163193
"outputs": [],
164194
"source": [
165195
"obs.match(model_frame)\n",
@@ -180,7 +210,12 @@
180210
{
181211
"cell_type": "code",
182212
"execution_count": null,
183-
"metadata": {},
213+
"metadata": {
214+
"ExecuteTime": {
215+
"end_time": "2026-05-06T03:30:50.770854Z",
216+
"start_time": "2026-05-06T03:30:47.529312Z"
217+
}
218+
},
184219
"outputs": [],
185220
"source": [
186221
"with sc2.Scene(model_frame) as scene:\n",
@@ -201,7 +236,12 @@
201236
{
202237
"cell_type": "code",
203238
"execution_count": null,
204-
"metadata": {},
239+
"metadata": {
240+
"ExecuteTime": {
241+
"end_time": "2026-05-06T03:30:51.111693Z",
242+
"start_time": "2026-05-06T03:30:50.789270Z"
243+
}
244+
},
205245
"outputs": [],
206246
"source": [
207247
"# we know source 0 is a star:\n",
@@ -223,7 +263,12 @@
223263
{
224264
"cell_type": "code",
225265
"execution_count": null,
226-
"metadata": {},
266+
"metadata": {
267+
"ExecuteTime": {
268+
"end_time": "2026-05-06T03:30:52.309994Z",
269+
"start_time": "2026-05-06T03:30:51.112795Z"
270+
}
271+
},
227272
"outputs": [],
228273
"source": [
229274
"sc2.plot.scene(\n",
@@ -261,7 +306,12 @@
261306
{
262307
"cell_type": "code",
263308
"execution_count": null,
264-
"metadata": {},
309+
"metadata": {
310+
"ExecuteTime": {
311+
"end_time": "2026-05-06T03:30:52.628502Z",
312+
"start_time": "2026-05-06T03:30:52.347148Z"
313+
}
314+
},
265315
"outputs": [],
266316
"source": [
267317
"scene_array = scene() # evaluate the model\n",
@@ -278,7 +328,12 @@
278328
{
279329
"cell_type": "code",
280330
"execution_count": null,
281-
"metadata": {},
331+
"metadata": {
332+
"ExecuteTime": {
333+
"end_time": "2026-05-06T03:30:52.673113Z",
334+
"start_time": "2026-05-06T03:30:52.637963Z"
335+
}
336+
},
282337
"outputs": [],
283338
"source": [
284339
"print(\"Avg. chi^2:\", obs.goodness_of_fit(scene_array))"
@@ -294,7 +349,12 @@
294349
{
295350
"cell_type": "code",
296351
"execution_count": null,
297-
"metadata": {},
352+
"metadata": {
353+
"ExecuteTime": {
354+
"end_time": "2026-05-06T03:30:53.573610Z",
355+
"start_time": "2026-05-06T03:30:52.675168Z"
356+
}
357+
},
298358
"outputs": [],
299359
"source": [
300360
"from functools import partial\n",
@@ -345,7 +405,12 @@
345405
{
346406
"cell_type": "code",
347407
"execution_count": null,
348-
"metadata": {},
408+
"metadata": {
409+
"ExecuteTime": {
410+
"end_time": "2026-05-06T03:31:06.350088Z",
411+
"start_time": "2026-05-06T03:30:59.216421Z"
412+
}
413+
},
349414
"outputs": [],
350415
"source": [
351416
"maxiter = 1000\n",
@@ -365,10 +430,15 @@
365430
{
366431
"cell_type": "code",
367432
"execution_count": null,
368-
"metadata": {},
433+
"metadata": {
434+
"ExecuteTime": {
435+
"end_time": "2026-05-06T03:31:07.790719Z",
436+
"start_time": "2026-05-06T03:31:07.711428Z"
437+
}
438+
},
369439
"outputs": [],
370440
"source": [
371-
"plt.semilogy(scene_.fit_info['losses'])\n",
441+
"plt.semilogy(scene_.fit_info['loss'])\n",
372442
"plt.ylabel('Loss')\n",
373443
"plt.xlabel('Iteration')\n",
374444
"plt.show()\n",
@@ -387,7 +457,12 @@
387457
{
388458
"cell_type": "code",
389459
"execution_count": null,
390-
"metadata": {},
460+
"metadata": {
461+
"ExecuteTime": {
462+
"end_time": "2026-05-06T03:31:09.561022Z",
463+
"start_time": "2026-05-06T03:31:09.189173Z"
464+
}
465+
},
391466
"outputs": [],
392467
"source": [
393468
"sc2.plot.scene(\n",
@@ -426,7 +501,12 @@
426501
{
427502
"cell_type": "code",
428503
"execution_count": null,
429-
"metadata": {},
504+
"metadata": {
505+
"ExecuteTime": {
506+
"end_time": "2026-05-06T03:31:12.759044Z",
507+
"start_time": "2026-05-06T03:31:12.531067Z"
508+
}
509+
},
430510
"outputs": [],
431511
"source": [
432512
"# Get models for source 1\n",
@@ -458,7 +538,12 @@
458538
{
459539
"cell_type": "code",
460540
"execution_count": null,
461-
"metadata": {},
541+
"metadata": {
542+
"ExecuteTime": {
543+
"end_time": "2026-05-06T03:31:18.908174Z",
544+
"start_time": "2026-05-06T03:31:16.632802Z"
545+
}
546+
},
462547
"outputs": [],
463548
"source": [
464549
"sc2.plot.sources(\n",
@@ -479,7 +564,7 @@
479564
"metadata": {},
480565
"source": [
481566
"The model of object 0 assumes the simple Gaussian shape of the model PSF, which is the internal representation of a point source.\n",
482-
"But it's noticeable that all other sources show pixel patterns that don't seem right. Some of them pick up flux from other sources (especially source 1) or noise (sources 4-6). What we see here is the limitation of non-parametric models: they can fit anything, but the likelihood may not have enough information to constrain all of these degrees of freedom. More/stronger constraints or priors can help, and we will describe their use in [a separate tutorial](howto/priors).\n",
567+
"But it's noticeable that all other sources show pixel patterns that don't seem right. Some of them pick up flux from other sources (especially source 1) or noise (sources 4-6). What we see here is the limitation of non-parametric models: they can fit anything, but the likelihood may not have enough information to constrain all of these degrees of freedom. More/stronger constraints or priors can help, and we will describe their use in [a separate tutorial](howto/priors). Another option is the addition of a model-free regularizer, e.g. {py:class}`~scarlet2.PairSimilarity`, which we demonstrate in the [LSBG tutorial](howto/lsb).\n",
483568
"\n",
484569
"### Source Measurements\n",
485570
"\n",

0 commit comments

Comments
 (0)