-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
172 lines (150 loc) · 5.06 KB
/
stack.py
File metadata and controls
172 lines (150 loc) · 5.06 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
import torch
from caskade import Module, forward, Param
from torch.nn.functional import avg_pool2d
from utils import meshgrid, Pixelated
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
DTYPE = torch.float64
class Cutout(Module):
def __init__(
self,
data,
mask,
variance,
upsample,
psf,
starf_mode="param",
center=None,
starf=None,
sky=None,
variance_scale=1.0,
anneal=0.0,
psf_upsample=4,
**kwargs,
):
super().__init__(**kwargs)
self.data = torch.as_tensor(data, device=DEVICE, dtype=DTYPE)
self.mask = torch.as_tensor(mask, device=DEVICE, dtype=torch.bool)
self.variance = torch.as_tensor(variance, device=DEVICE, dtype=DTYPE)
self.upsample = upsample
self.psf = Pixelated(
name="PSF",
x0=0.0,
y0=0.0,
image=psf,
pixelscale=1.0 / psf_upsample,
)
self.center = Param(f"center", center, shape=(2,))
self.starf = Param(f"starf", starf)
self.sky = Param("sky", sky)
self.thx, self.thy = meshgrid(
1 / upsample,
data.shape[1] * upsample,
data.shape[0] * upsample,
device=DEVICE,
dtype=DTYPE,
)
self.starf_mode = starf_mode
self.variance_scale = variance_scale
self.anneal = anneal
self.center_mask = False
self.meta.bad_obj_mask = torch.zeros_like(self._mask)
def to(self, device, dtype):
super().to(device, dtype)
self.data = self.data.to(device, dtype)
self._mask = self._mask.to(device, torch.bool)
self.meta.bad_obj_mask = self.meta.bad_obj_mask.to(device, torch.bool)
self.variance = self.variance.to(device, dtype)
self.thx = self.thx.to(device, dtype)
self.thy = self.thy.to(device, dtype)
return self
@property
def mask(self):
mask = self._mask | self.meta.bad_obj_mask
if self.center_mask:
center_mask = torch.zeros_like(self._mask)
center_mask[: int(center_mask.shape[0] // 4)] = True
center_mask[-int(center_mask.shape[0] // 4) :] = True
center_mask[:, : int(center_mask.shape[0] // 4)] = True
center_mask[:, -int(center_mask.shape[0] // 4) :] = True
return mask | center_mask
return mask
@mask.setter
def mask(self, value):
self._mask = value
@property
def starf_mode(self):
return self._starf_mode
@starf_mode.setter
def starf_mode(self, value):
self._starf_mode = value
if value == "exact":
self.starf = torch.ones((), device=self.data.device, dtype=self.data.dtype)
@forward
def _sample(self, center=None, sky=None, model_perturb=None):
sample = avg_pool2d(
self.psf.brightness(
self.thx + center[0],
self.thy + center[1],
padding_mode="clamp",
)[
None
][None],
self.upsample,
).squeeze()
return sample
@forward
def max_starf(self, sky=None):
return ((~self.mask) * (self.data - sky)).amax()
@forward
def exact_starf(self, sample=None, sky=None):
if sample is None:
sample = self._sample()
data = self.data - sky
starf = ((~self.mask) * sample * data / self.variance).sum() / (
(~self.mask) * sample**2 / self.variance
).sum()
return starf
@forward
def sample(self, sample=None, starf=None, sky=None):
if sample is None:
sample = self._sample()
if self.starf_mode == "exact":
starf = self.exact_starf(sample=sample, sky=sky)
return sample * starf + sky
@forward
def residuals(self, sample=None):
if sample is None:
sample = self.sample()
return (self.data - sample) / self.variance.sqrt()
@forward
def set_prior_bounds(self, center=None, size=3):
self.prior_center = center.detach()
self.prior_size = size
@forward
def log_prior(self, center=None):
ob = torch.tensor(-torch.inf, device=center.device)
ib = torch.tensor(0.0, device=center.device)
return torch.where(
torch.any((center - self.prior_center).abs() > self.prior_size),
ob,
ib,
)
@forward
def log_likelihood(self, sample=None, anneal=0.0):
if sample is None:
sample = self.sample()
if not isinstance(anneal, (float, int)):
assert sample.shape == anneal.shape
return (
-0.5
* (
(~self.mask)
* (sample - self.data) ** 2
/ (self.variance * self.variance_scale + self.anneal + anneal)
).sum()
)
@forward
def log_posterior(self, sample=None, anneal=0.0):
lp = self.log_prior()
ll = self.log_likelihood(sample=sample, anneal=anneal)
return lp + ll