LSST Applications g0603fd7c41+022847dfd1,g0aad566f14+f45185db35,g180d380827+40e913b07a,g2079a07aa2+86d27d4dc4,g2305ad1205+696e5f3872,g2bbee38e9b+047b288a59,g337abbeb29+047b288a59,g33d1c0ed96+047b288a59,g3a166c0a6a+047b288a59,g3d1719c13e+f45185db35,g3de15ee5c7+5201731f0d,g487adcacf7+19f9b77d7d,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+248b16177b,g63cd9335cc+585e252eca,g858d7b2824+f45185db35,g88963caddf+0cb8e002cc,g991b906543+f45185db35,g99cad8db69+1747e75aa3,g9b9dfce982+78139cbddb,g9ddcbc5298+9a081db1e4,ga1e77700b3+a912195c07,gae0086650b+585e252eca,gb0e22166c9+60f28cb32d,gb3a676b8dc+b4feba26a1,gb4b16eec92+f82f04eb54,gba4ed39666+c2a2e4ac27,gbb8dafda3b+215b19b0ab,gc120e1dc64+b0284b5341,gc28159a63d+047b288a59,gc3e9b769f7+dcad4ace9a,gcf0d15dbbd+78139cbddb,gdaeeff99f8+f9a426f77a,ge79ae78c31+047b288a59,w.2024.19
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Attributes | List of all members
lsst.scarlet.lite.component.FactorizedComponent Class Reference
Inheritance diagram for lsst.scarlet.lite.component.FactorizedComponent:
lsst.scarlet.lite.component.Component lsst.scarlet.lite.models.free_form.FreeFormComponent

Public Member Functions

 __init__ (self, tuple bands, Parameter|np.ndarray spectrum, Parameter|np.ndarray morph, Box bbox, tuple[int, int]|None peak=None, np.ndarray|None bg_rms=None, float|None bg_thresh=0.25, float floor=1e-20, Monotonicity|None monotonicity=None, int padding=5)
 
tuple[int, int]|None peak (self)
 
tuple[int, int]|None component_center (self)
 
np.ndarray spectrum (self)
 
np.ndarray morph (self)
 
tuple shape (self)
 
Image get_model (self)
 
 grad_spectrum (self, np.ndarray input_grad, np.ndarray spectrum, np.ndarray morph)
 
 grad_morph (self, np.ndarray input_grad, np.ndarray morph, np.ndarray spectrum)
 
np.ndarray prox_spectrum (self, np.ndarray spectrum)
 
np.ndarray prox_morph (self, np.ndarray morph)
 
bool resize (self, Box model_box)
 
 update (self, int it, np.ndarray input_grad)
 
None parameterize (self, Callable parameterization)
 
 __str__ (self)
 
 __repr__ (self)
 

Public Attributes

 bg_rms
 
 bg_thresh
 
 floor
 
 monotonicity
 
 padding
 
 morph
 

Protected Attributes

 _spectrum
 
 _morph
 
 _peak
 
 _bbox
 

Detailed Description

A component that can be factorized into spectrum and morphology
parameters.

Parameters
----------
bands:
    The bands of the spectral dimension, in order.
spectrum:
    The parameter to store and update the spectrum.
morph:
    The parameter to store and update the morphology.
peak:
    Location of the peak for the source.
bbox:
    The `Box` in the `model_bbox` that contains the source.
bg_rms:
    The RMS of the background used to threshold, grow,
    and shrink the component.
floor:
    Minimum value of the spectrum or center morphology pixel
    (depending on which is normalized).
monotonicity:
    The monotonicity operator to use for making the source monotonic.
    If this parameter is `None`, the source will not be made monotonic.

Definition at line 115 of file component.py.

Constructor & Destructor Documentation

◆ __init__()

lsst.scarlet.lite.component.FactorizedComponent.__init__ ( self,
tuple bands,
Parameter | np.ndarray spectrum,
Parameter | np.ndarray morph,
Box bbox,
tuple[int, int] | None peak = None,
np.ndarray | None bg_rms = None,
float | None bg_thresh = 0.25,
float floor = 1e-20,
Monotonicity | None monotonicity = None,
int padding = 5 )

Reimplemented from lsst.scarlet.lite.component.Component.

Reimplemented in lsst.scarlet.lite.models.free_form.FreeFormComponent.

Definition at line 142 of file component.py.

154 ):
155 # Initialize all of the base attributes
156 super().__init__(
157 bands=bands,
158 bbox=bbox,
159 )
160 self._spectrum = parameter(spectrum)
161 self._morph = parameter(morph)
162 self._peak = peak
163 self.bg_rms = bg_rms
164 self.bg_thresh = bg_thresh
165
166 self.floor = floor
167 self.monotonicity = monotonicity
168 self.padding = padding
169

Member Function Documentation

◆ __repr__()

lsst.scarlet.lite.component.FactorizedComponent.__repr__ ( self)

Reimplemented in lsst.scarlet.lite.models.free_form.FreeFormComponent.

Definition at line 340 of file component.py.

340 def __repr__(self):
341 return self.__str__()
342
343

◆ __str__()

lsst.scarlet.lite.component.FactorizedComponent.__str__ ( self)

Reimplemented in lsst.scarlet.lite.models.free_form.FreeFormComponent.

Definition at line 333 of file component.py.

333 def __str__(self):
334 result = (
335 f"FactorizedComponent<\n bands={self.bands},\n center={self.peak},\n "
336 f"spectrum={self.spectrum},\n morph_shape={self.morph.shape}\n>"
337 )
338 return result
339

◆ component_center()

tuple[int, int] | None lsst.scarlet.lite.component.FactorizedComponent.component_center ( self)
The center of the component in its bounding box

This is likely to be different than `Component.center`,
since `Component.center` is the center of the component in the
full model, whereas `component_center` is the center of the component
inside its bounding box.

Returns
-------
center:
    The center of the component in its bounding box

Definition at line 182 of file component.py.

182 def component_center(self) -> tuple[int, int] | None:
183 """The center of the component in its bounding box
184
185 This is likely to be different than `Component.center`,
186 since `Component.center` is the center of the component in the
187 full model, whereas `component_center` is the center of the component
188 inside its bounding box.
189
190 Returns
191 -------
192 center:
193 The center of the component in its bounding box
194 """
195 _center = self.peak
196 if _center is None:
197 return None
198 center = (
199 _center[0] - self.bbox.origin[-2],
200 _center[1] - self.bbox.origin[-1],
201 )
202 return center
203

◆ get_model()

Image lsst.scarlet.lite.component.FactorizedComponent.get_model ( self)
Build the model from the spectrum and morphology

Reimplemented from lsst.scarlet.lite.component.Component.

Definition at line 219 of file component.py.

219 def get_model(self) -> Image:
220 """Build the model from the spectrum and morphology"""
221 # The spectrum and morph might be Parameters,
222 # so cast them as arrays in the model.
223 spectrum = self.spectrum
224 morph = self.morph
225 model = spectrum[:, None, None] * morph[None, :, :]
226 return Image(model, bands=self.bands, yx0=cast(tuple[int, int], self.bbox.origin))
227

◆ grad_morph()

lsst.scarlet.lite.component.FactorizedComponent.grad_morph ( self,
np.ndarray input_grad,
np.ndarray morph,
np.ndarray spectrum )
Gradient of the morph wrt. the component model

Definition at line 232 of file component.py.

232 def grad_morph(self, input_grad: np.ndarray, morph: np.ndarray, spectrum: np.ndarray):
233 """Gradient of the morph wrt. the component model"""
234 return np.einsum("i,i...", spectrum, input_grad)
235

◆ grad_spectrum()

lsst.scarlet.lite.component.FactorizedComponent.grad_spectrum ( self,
np.ndarray input_grad,
np.ndarray spectrum,
np.ndarray morph )
Gradient of the spectrum wrt. the component model

Definition at line 228 of file component.py.

228 def grad_spectrum(self, input_grad: np.ndarray, spectrum: np.ndarray, morph: np.ndarray):
229 """Gradient of the spectrum wrt. the component model"""
230 return np.einsum("...jk,jk", input_grad, morph)
231

◆ morph()

np.ndarray lsst.scarlet.lite.component.FactorizedComponent.morph ( self)
The array of morphology values

Definition at line 210 of file component.py.

210 def morph(self) -> np.ndarray:
211 """The array of morphology values"""
212 return self._morph.x
213

◆ parameterize()

None lsst.scarlet.lite.component.FactorizedComponent.parameterize ( self,
Callable parameterization )
Convert the component parameter arrays into Parameter instances

Parameters
----------
parameterization: Callable
    A function to use to convert parameters of a given type into
    a `Parameter` in place. It should take a single argument that
    is the `Component` or `Source` that is to be parameterized.

Reimplemented from lsst.scarlet.lite.component.Component.

Definition at line 315 of file component.py.

315 def parameterize(self, parameterization: Callable) -> None:
316 """Convert the component parameter arrays into Parameter instances
317
318 Parameters
319 ----------
320 parameterization: Callable
321 A function to use to convert parameters of a given type into
322 a `Parameter` in place. It should take a single argument that
323 is the `Component` or `Source` that is to be parameterized.
324 """
325 # Update the spectrum and morph in place
326 parameterization(self)
327 # update the parameters
328 self._spectrum.grad = self.grad_spectrum
329 self._spectrum.prox = self.prox_spectrum
330 self._morph.grad = self.grad_morph
331 self._morph.prox = self.prox_morph
332

◆ peak()

tuple[int, int] | None lsst.scarlet.lite.component.FactorizedComponent.peak ( self)
The peak of the component

Returns
-------
peak:
    The peak of the component

Definition at line 171 of file component.py.

171 def peak(self) -> tuple[int, int] | None:
172 """The peak of the component
173
174 Returns
175 -------
176 peak:
177 The peak of the component
178 """
179 return self._peak
180

◆ prox_morph()

np.ndarray lsst.scarlet.lite.component.FactorizedComponent.prox_morph ( self,
np.ndarray morph )
Apply a prox-like update to the morphology

Reimplemented in lsst.scarlet.lite.models.free_form.FreeFormComponent.

Definition at line 243 of file component.py.

243 def prox_morph(self, morph: np.ndarray) -> np.ndarray:
244 """Apply a prox-like update to the morphology"""
245 # monotonicity
246 if self.monotonicity is not None:
247 morph = self.monotonicity(morph, cast(tuple[int, int], self.component_center))
248
249 if self.bg_thresh is not None and self.bg_rms is not None:
250 bg_thresh = self.bg_rms * self.bg_thresh
251 # Enforce background thresholding
252 model = self.spectrum[:, None, None] * morph[None, :, :]
253 morph[np.all(model < bg_thresh[:, None, None], axis=0)] = 0
254 else:
255 # enforce positivity
256 morph[morph < 0] = 0
257
258 # prevent divergent morphology
259 shape = morph.shape
260 if self.peak is None:
261 peak = (shape[0] // 2, shape[1] // 2)
262 else:
263 peak = (
264 self.peak[0] - self.bbox.origin[-2],
265 self.peak[1] - self.bbox.origin[-1],
266 )
267 morph[peak] = np.max([morph[peak], self.floor])
268
269 # Ensure that the morphology is finite
270 morph[~np.isfinite(morph)] = 0
271
272 # Normalize the morphology
273 max_value = np.max(morph)
274 if max_value > 0:
275 morph[:] = morph / max_value
276 return morph
277

◆ prox_spectrum()

np.ndarray lsst.scarlet.lite.component.FactorizedComponent.prox_spectrum ( self,
np.ndarray spectrum )
Apply a prox-like update to the spectrum

Reimplemented in lsst.scarlet.lite.models.free_form.FreeFormComponent.

Definition at line 236 of file component.py.

236 def prox_spectrum(self, spectrum: np.ndarray) -> np.ndarray:
237 """Apply a prox-like update to the spectrum"""
238 # prevent divergent spectrum
239 spectrum[spectrum < self.floor] = self.floor
240 spectrum[~np.isfinite(spectrum)] = self.floor
241 return spectrum
242

◆ resize()

bool lsst.scarlet.lite.component.FactorizedComponent.resize ( self,
Box model_box )
Test whether or not the component needs to be resized

Reimplemented from lsst.scarlet.lite.component.Component.

Reimplemented in lsst.scarlet.lite.models.free_form.FreeFormComponent.

Definition at line 278 of file component.py.

278 def resize(self, model_box: Box) -> bool:
279 """Test whether or not the component needs to be resized"""
280 # No need to resize if there is no size threshold.
281 # To allow box sizing but no thresholding use `bg_thresh=0`.
282 if self.bg_thresh is None or self.bg_rms is None:
283 return False
284
285 model = self.spectrum[:, None, None] * self.morph[None, :, :]
286 bg_thresh = self.bg_rms * self.bg_thresh
287 significant = np.any(model >= bg_thresh[:, None, None], axis=0)
288 if np.sum(significant) == 0:
289 # There are no significant pixels,
290 # so make a small box around the center
291 center = self.peak
292 if center is None:
293 center = (0, 0)
294 new_box = Box((1, 1), center).grow(self.padding) & model_box
295 else:
296 new_box = (
297 Box.from_data(significant, threshold=0).grow(self.padding) + self.bbox.origin
298 ) & model_box
299 if new_box == self.bbox:
300 return False
301
302 old_box = self.bbox
303 self._bbox = new_box
304 self._morph.resize(old_box, new_box)
305 return True
306

◆ shape()

tuple lsst.scarlet.lite.component.FactorizedComponent.shape ( self)
Shape of the resulting model image

Definition at line 215 of file component.py.

215 def shape(self) -> tuple:
216 """Shape of the resulting model image"""
217 return self.spectrum.shape + self.morph.shape
218

◆ spectrum()

np.ndarray lsst.scarlet.lite.component.FactorizedComponent.spectrum ( self)
The array of spectrum values

Definition at line 205 of file component.py.

205 def spectrum(self) -> np.ndarray:
206 """The array of spectrum values"""
207 return self._spectrum.x
208

◆ update()

lsst.scarlet.lite.component.FactorizedComponent.update ( self,
int it,
np.ndarray input_grad )
Update the spectrum and morphology parameters

Reimplemented from lsst.scarlet.lite.component.Component.

Definition at line 307 of file component.py.

307 def update(self, it: int, input_grad: np.ndarray):
308 """Update the spectrum and morphology parameters"""
309 # Store the input spectrum so that the morphology can
310 # have a consistent update
311 spectrum = self.spectrum.copy()
312 self._spectrum.update(it, input_grad, self.morph)
313 self._morph.update(it, input_grad, spectrum)
314

Member Data Documentation

◆ _bbox

lsst.scarlet.lite.component.FactorizedComponent._bbox
protected

Definition at line 303 of file component.py.

◆ _morph

lsst.scarlet.lite.component.FactorizedComponent._morph
protected

Definition at line 161 of file component.py.

◆ _peak

lsst.scarlet.lite.component.FactorizedComponent._peak
protected

Definition at line 162 of file component.py.

◆ _spectrum

lsst.scarlet.lite.component.FactorizedComponent._spectrum
protected

Definition at line 160 of file component.py.

◆ bg_rms

lsst.scarlet.lite.component.FactorizedComponent.bg_rms

Definition at line 163 of file component.py.

◆ bg_thresh

lsst.scarlet.lite.component.FactorizedComponent.bg_thresh

Definition at line 164 of file component.py.

◆ floor

lsst.scarlet.lite.component.FactorizedComponent.floor

Definition at line 166 of file component.py.

◆ monotonicity

lsst.scarlet.lite.component.FactorizedComponent.monotonicity

Definition at line 167 of file component.py.

◆ morph

lsst.scarlet.lite.component.FactorizedComponent.morph

Definition at line 312 of file component.py.

◆ padding

lsst.scarlet.lite.component.FactorizedComponent.padding

Definition at line 168 of file component.py.


The documentation for this class was generated from the following file: