forked from prob-ml/bliss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_figures.py
More file actions
350 lines (286 loc) · 11.2 KB
/
binary_figures.py
File metadata and controls
350 lines (286 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""Script to create detection encoder related figures."""
import math
from copy import deepcopy
import numpy as np
import torch
from einops import rearrange, reduce
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
from tqdm import tqdm
from bliss.catalog import FullCatalog, TileCatalog
from bliss.datasets.io import load_dataset_npz
from bliss.encoders.binary import BinaryEncoder
from bliss.plotting import CLR_CYCLE, BlissFigure
from bliss.render_tiles import get_n_padded_tiles_hw
from bliss.reporting import get_residual_measurements
def _get_metrics_per_bin(tbools, ebools, snrs, snr_bins):
tp_per_bin = []
nt_per_bin = []
p_per_bin = []
for ii in range(len(snr_bins) - 1):
snr1 = snr_bins[ii]
snr2 = snr_bins[ii + 1]
_mask = (snrs > snr1) * (snrs < snr2)
_tp = (ebools == tbools) * (tbools == 1) * (ebools == 1) * _mask
_p = (ebools == 1) * _mask
_nt = (tbools == 1) * _mask
tp_per_bin.append(_tp.sum())
p_per_bin.append(_p.sum())
nt_per_bin.append(_nt.sum())
tp = np.array(tp_per_bin)
p = np.array(p_per_bin)
nt = np.array(nt_per_bin)
precision = tp / p
recall = tp / nt
f1 = 2 / (precision**-1 + recall**-1)
return precision, recall, f1
def _get_equally_spaced_bins(
bools: np.ndarray,
snrs: np.ndarray,
*,
min_snr: float = 10.0,
max_snr: float = 1000.0,
n_bins: int = 10,
):
mask = (snrs > min_snr) * (snrs <= max_snr) * bools.astype(bool)
_log_snr = np.log10(snrs[mask])
qs = np.linspace(0, 1, n_bins)
snr_bins = 10 ** np.quantile(_log_snr, qs)
snr_middle = (snr_bins[1:] + snr_bins[:-1]) / 2
return snr_bins, snr_middle
class BinaryFigures(BlissFigure):
def __init__(
self, *, figdir, cachedir, suffix, overwrite=False, img_format="png", aperture=5.0
):
super().__init__(
figdir=figdir,
cachedir=cachedir,
suffix=suffix,
overwrite=overwrite,
img_format=img_format,
)
self.aperture = aperture
@property
def all_rcs(self) -> dict:
return {
"binary_scatter": {"fontsize": 34},
"binary_curves": {"fontsize": 36, "major_tick_size": 12, "minor_tick_size": 7},
"binary_contours": {"fontsize": 34},
}
@property
def cache_name(self) -> str:
return "binary"
@property
def fignames(self) -> tuple[str, ...]:
return ("binary_scatter", "binary_curves", "binary_contours")
def compute_data(self, ds_path: str, binary: BinaryEncoder):
# metadata
bp = binary.bp
tile_slen = binary.tile_slen
ptile_slen = binary.ptile_slen
# read dataset
dataset = load_dataset_npz(ds_path)
images = dataset["images"]
paddings = dataset["paddings"]
uncentered_sources = dataset["uncentered_sources"]
star_bools = dataset["star_bools"]
size = images.shape[-1]
slen = size - 2 * bp
# paddings include stars for convenience, but we don't want to remove them in this case
# we want to include snr of stars
only_stars = uncentered_sources * rearrange(star_bools, "b n 1 -> b n 1 1 1").float()
all_stars = reduce(only_stars, "b n c h w -> b c h w", "sum")
new_paddings = paddings - all_stars
# get truth catalog
exclude = ("images", "uncentered_sources", "centered_sources", "noiseless", "paddings")
true_cat_dict = {p: q for p, q in dataset.items() if p not in exclude}
_truth = FullCatalog(slen, slen, true_cat_dict)
# get snrs through sep
meas_dict = get_residual_measurements(
_truth,
images,
paddings=new_paddings,
sources=uncentered_sources,
bp=bp,
r=self.aperture,
)
snr = meas_dict["snr"]
# add parameters to truth
_truth["snr"] = snr
# we ignore double counting source and pick the brightest one for comparisons
# these ensures results later are all aligned
truth_tile_cat = _truth.to_tile_params(tile_slen, ignore_extra_sources=True)
truth = truth_tile_cat.to_full_params()
# get source is on
b = truth.n_sources.shape[0]
ms = truth.max_n_sources
source_is_on = torch.zeros((b, ms))
for jj in range(b):
n = truth.n_sources[jj]
source_is_on[jj, :n] = 1.0
is_on_mask = source_is_on.flatten().bool()
# run binary encoder on true locations
batch_size = 100
n_images = images.shape[0]
n_batches = math.ceil(n_images / batch_size)
nth, ntw = get_n_padded_tiles_hw(size, size, tile_slen=tile_slen, ptile_slen=ptile_slen)
tile_galaxy_probs = []
for ii in tqdm(range(n_batches)):
start, end = ii * batch_size, (ii + 1) * batch_size
bimages = images[start:end].to(binary.device)
btile_locs = truth_tile_cat.locs[start:end].to(binary.device)
tile_gprob_flat = binary.forward(bimages, btile_locs).to("cpu")
tile_gprob = rearrange(
tile_gprob_flat, "(n nth ntw) -> n nth ntw 1", n=batch_size, nth=nth, ntw=ntw
)
tile_galaxy_probs.append(tile_gprob)
tile_galaxy_probs = torch.concatenate(tile_galaxy_probs, axis=0)
# create new catalog with these booleans and prob
out = {}
thresholds = (0.5, 0.75, 0.9)
for tsh in thresholds:
est_tiled = deepcopy(truth_tile_cat.to_dict())
n_sources_flat = est_tiled["n_sources"].float().unsqueeze(-1)
est_tiled["galaxy_bools"] = tile_galaxy_probs.ge(tsh) * n_sources_flat
est_tiled["star_bools"] = tile_galaxy_probs.le(1 - tsh) * n_sources_flat
est_tiled["galaxy_probs"] = tile_galaxy_probs * n_sources_flat
est_tiled_cat = TileCatalog(tile_slen, est_tiled)
est = est_tiled_cat.to_full_params()
# get flat list of truth, predicted bools, probs, and snr
egbools = est["galaxy_bools"].flatten()[is_on_mask]
esbools = est["star_bools"].flatten()[is_on_mask]
probs = est["galaxy_probs"].flatten()[is_on_mask]
out[tsh] = {
"egbools": egbools,
"esbools": esbools,
}
out["probs"] = probs # always the same
snr = truth["snr"].flatten()[is_on_mask]
tgbools = truth["galaxy_bools"].flatten()[is_on_mask]
tsbools = truth["star_bools"].flatten()[is_on_mask]
out["snr"] = snr
out["tgbools"] = tgbools
out["tsbools"] = tsbools
return out
def _get_binary_scatter_figure(self, data: dict):
# first we make two scatter plot figures
# useful for sanity checking
fig, ax = plt.subplots(1, 1, figsize=(12, 10))
snr = data["snr"]
probs = data["probs"]
tgbools = data["tgbools"]
tsbools = data["tsbools"]
galaxy_mask = tgbools.astype(bool)
star_mask = tsbools.astype(bool)
c1 = CLR_CYCLE[0]
c2 = CLR_CYCLE[1]
# scatter plot of probabilities
ax.scatter(
snr[galaxy_mask],
probs[galaxy_mask],
marker="o",
s=5,
alpha=0.35,
color=c1,
label=r"\rm Galaxy",
)
ax.scatter(
snr[star_mask],
probs[star_mask],
marker="o",
s=5,
alpha=0.35,
color=c2,
label=r"\rm Star",
)
ax.set_xscale("log")
# ax.set_xticks([1e-2, 1e-1, 1, 10, 100, 1000, 10000, 100_000])
ax.legend(markerscale=6, fontsize=28)
ax.set_xlabel(r"\rm SNR")
ax.set_ylabel(r"\rm Galaxy Classification Probability")
ax.set_xlim(1, 1e3)
return fig
def _get_binary_curves(self, data: dict):
snr = data["snr"]
tgbools = data["tgbools"]
tsbools = data["tsbools"]
egbools = data[0.5]["egbools"]
esbools = data[0.5]["esbools"]
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 20))
c1 = CLR_CYCLE[0]
c2 = CLR_CYCLE[1]
c3 = CLR_CYCLE[2]
# precision and recall for galaxies
snr_bins, snr_middle = _get_equally_spaced_bins(egbools, snr, n_bins=10)
prec, rec, f1 = _get_metrics_per_bin(tgbools, egbools, snr, snr_bins)
ax1.plot(snr_middle, prec, "-o", label=r"\rm precision", color=c1)
ax1.plot(snr_middle, rec, "-o", label=r"\rm recall", color=c2)
ax1.plot(snr_middle, f1, "-o", label="$F_{1}$", color=c3)
ax1.set_xlabel(r"\rm SNR")
ax1.set_xscale("log")
ax1.set_title(r"\rm Galaxies")
ax1.set_ylabel(r"\rm Metric", fontsize=36)
ax1.set_xlim(10, 1000)
ax1.legend()
# precision and recall for stars
snr_bins, snr_middle = _get_equally_spaced_bins(esbools, snr, n_bins=10)
prec, rec, f1 = _get_metrics_per_bin(tsbools, esbools, snr, snr_bins)
ax2.plot(snr_middle, prec, "-o", color=c1)
ax2.plot(snr_middle, rec, "-o", color=c2)
ax2.plot(snr_middle, f1, "-o", color=c3)
ax2.set_xlabel(r"\rm SNR")
ax2.set_xscale("log")
ax2.set_title(r"\rm Stars")
ax2.set_xlim(10, 1000)
plt.tight_layout()
return fig
def _get_binary_contours(self, data: dict):
snr = data["snr"]
probs = data["probs"]
tgbools = data["tgbools"]
tsbools = data["tsbools"]
galaxy_mask = tgbools.astype(bool) & (snr > 0)
star_mask = tsbools.astype(bool) & (snr > 0)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10), sharey=True)
ax1.hist2d(
np.log10(snr[galaxy_mask]),
probs[galaxy_mask],
bins=20,
range=[[0, 3], [0, 1]],
cmap="PuBu",
norm="log",
vmin=1,
vmax=2e4,
)
_xticks = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
_xticks_labels = [f"$10^{int(x)}$" if x in [0.0, 1.0, 2.0, 3.0] else "" for x in _xticks]
ax1.set_xticks(ticks=_xticks, labels=_xticks_labels)
ax1.set_xlabel(r"\rm SNR")
ax1.set_ylabel(r"\rm Galaxy Classification Probability")
ax1.set_title(r"\rm Galaxies")
_, _, _, pcm = ax2.hist2d(
np.log10(snr[star_mask]),
probs[star_mask],
bins=20,
range=[[0, 3], [0, 1]],
cmap="PuBu",
norm="log",
vmin=1,
vmax=2e4,
)
ax2.set_xticks(ticks=_xticks, labels=_xticks_labels)
ax2.set_xlabel(r"\rm SNR")
ax2.set_title(r"\rm Stars")
divider = make_axes_locatable(ax2)
cax = divider.append_axes("right", size="5%", pad=0.05)
fig.colorbar(pcm, cax=cax, orientation="vertical")
plt.tight_layout()
return fig
def create_figure(self, fname: str, data):
if fname == "binary_scatter":
return self._get_binary_scatter_figure(data)
if fname == "binary_curves":
return self._get_binary_curves(data)
if fname == "binary_contours":
return self._get_binary_contours(data)
raise ValueError(f"Unknown figure name: {fname}")