MCPcopy Create free account
hub / github.com/PolymathicAI/AstroCLIP / ToRGB

Class ToRGB

astroclip/astrodino/data/augmentations.py:132–183  ·  view source on GitHub ↗

Transformation from raw image data (nanomaggies) to the rgb values displayed at the legacy viewer https://www.legacysurvey.org/viewer Code copied from https://github.com/legacysurvey/imagine/blob/master/map/views.py

Source from the content-addressed store, hash-verified

130
131
132class ToRGB:
133 """
134 Transformation from raw image data (nanomaggies) to the rgb values displayed
135 at the legacy viewer https://www.legacysurvey.org/viewer
136
137 Code copied from
138 https://github.com/legacysurvey/imagine/blob/master/map/views.py
139 """
140
141 def __init__(self, scales=None, m=0.03, Q=20, bands=["g", "r", "z"]):
142 rgb_scales = {
143 "u": (2, 1.5),
144 "g": (2, 6.0),
145 "r": (1, 3.4),
146 "i": (0, 1.0),
147 "z": (0, 2.2),
148 }
149 if scales is not None:
150 rgb_scales.update(scales)
151
152 self.rgb_scales = rgb_scales
153 self.m = m
154 self.Q = Q
155 self.bands = bands
156 self.axes, self.scales = zip(*[rgb_scales[bands[i]] for i in range(len(bands))])
157
158 # rearange scales to correspond to image channels after swapping
159 self.scales = [self.scales[i] for i in self.axes]
160
161 def __call__(self, imgs):
162 # Check image shape and set to C x H x W
163 if imgs.shape[0] != len(self.bands):
164 imgs = np.transpose(imgs, (2, 0, 1))
165
166 I = 0
167 for img, band in zip(imgs, self.bands):
168 plane, scale = self.rgb_scales[band]
169 img = np.maximum(0, img * scale + self.m)
170 I = I + img
171 I /= len(self.bands)
172
173 Q = 20
174 fI = np.arcsinh(Q * I) / np.sqrt(Q)
175 I += (I == 0.0) * 1e-6
176 H, W = I.shape
177 rgb = np.zeros((H, W, 3), np.float32)
178 for img, band in zip(imgs, self.bands):
179 plane, scale = self.rgb_scales[band]
180 rgb[:, :, plane] = (img * scale + self.m) * fI / I
181
182 rgb = np.clip(rgb, 0, 1)
183 return rgb
184
185
186class GaussianNoise:

Callers 3

_get_imagesFunction · 0.90
__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected