LSST Applications g00d0e8bbd7+edbf708997,g03191d30f7+9ce8016dbd,g1955dfad08+0bd186d245,g199a45376c+5137f08352,g1fd858c14a+a888a50aa2,g262e1987ae+45f9aba685,g29ae962dfc+1c7d47a24f,g2cef7863aa+73c82f25e4,g35bb328faa+edbf708997,g3fd5ace14f+eed17d2c67,g47891489e3+6dc8069a4c,g53246c7159+edbf708997,g64539dfbff+c4107e45b5,g67b6fd64d1+6dc8069a4c,g74acd417e5+f452e9c21a,g786e29fd12+af89c03590,g7ae74a0b1c+a25e60b391,g7aefaa3e3d+2025e9ce17,g7cc15d900a+2d158402f9,g87389fa792+a4172ec7da,g89139ef638+6dc8069a4c,g8d4809ba88+c4107e45b5,g8d7436a09f+e96c132b44,g8ea07a8fe4+db21c37724,g98df359435+aae6d409c1,ga2180abaac+edbf708997,gac66b60396+966efe6077,gb632fb1845+88945a90f8,gbaa8f7a6c5+38b34f4976,gbf99507273+edbf708997,gca7fc764a6+6dc8069a4c,gd7ef33dd92+6dc8069a4c,gda68eeecaf+7d1e613a8d,gdab6d2f7ff+f452e9c21a,gdbb4c4dda9+c4107e45b5,ge410e46f29+6dc8069a4c,ge41e95a9f2+c4107e45b5,geaed405ab2+e194be0d2b,w.2025.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
source.py
Go to the documentation of this file.
1# This file is part of scarlet_lite.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22from __future__ import annotations
23
24__all__ = ["Source"]
25
26from abc import ABC, abstractmethod
27from typing import TYPE_CHECKING, Any, Callable
28
29from .bbox import Box
30from .component import Component
31from .image import Image
32
33if TYPE_CHECKING:
34 from .io import ScarletSourceBaseData, ScarletSourceData
35
36
37class SourceBase(ABC):
38 """Base class for a source
39
40 This is primarily to allow `isinstance` checks
41 without importing the full `Source` class.
42 """
43
44 metadata: dict[str, Any] | None = None
45
46 @abstractmethod
47 def to_data(self) -> ScarletSourceBaseData:
48 """Convert to a `ScarletSourceBaseData` for serialization
49
50 Returns
51 -------
52 source_data:
53 The `ScarletSourceData` representation of this source.
54 """
55
56
58 """A container for components associated with the same astrophysical object
59
60 A source can have a single component, or multiple components,
61 and each can be contained in different bounding boxes.
62
63 Parameters
64 ----------
65 components:
66 The components contained in the source.
67 """
68
69 def __init__(self, components: list[Component], metadata: dict | None = None):
70 self.components = components
71 self.flux_weighted_image: Image | None = None
72 self.metadata = metadata
73
74 @property
75 def n_components(self) -> int:
76 """The number of components in this source"""
77 return len(self.components)
78
79 @property
80 def center(self) -> tuple[int, int] | None:
81 """The center of the source in the full Blend."""
82 if not self.is_null and hasattr(self.components[0], "peak"):
83 return self.components[0].peak # type: ignore
84 return None
85
86 @property
87 def source_center(self) -> tuple[int, int] | None:
88 """The center of the source in its local bounding box."""
89 _center = self.center
90 _origin = self.bbox.origin
91 if _center is not None:
92 center = (
93 _center[0] - _origin[0],
94 _center[1] - _origin[1],
95 )
96 return center
97 return None
98
99 @property
100 def is_null(self) -> bool:
101 """True if the source does not have any components"""
102 return self.n_components == 0
103
104 @property
105 def bbox(self) -> Box:
106 """The minimal bounding box to contain all of this sources components
107
108 Null sources have a bounding box with shape `(0,0,0)`
109 """
110 if self.n_components == 0:
111 return Box((0, 0))
112 bbox = self.components[0].bbox
113 for component in self.components[1:]:
114 bbox = bbox | component.bbox
115 return bbox
116
117 @property
118 def bands(self) -> tuple:
119 """The bands in the full source model."""
120 if self.is_null:
121 return ()
122 return self.components[0].bands
123
124 def get_model(self, use_flux: bool = False) -> Image:
125 """Build the model for the source
126
127 This is never called during optimization and is only used
128 to generate a model of the source for investigative purposes.
129
130 Parameters
131 ----------
132 use_flux:
133 Whether to use the re-distributed flux associated with the source
134 instead of the component models.
135
136 Returns
137 -------
138 model:
139 The full-color model.
140 """
141 if self.n_components == 0:
142 return 0 # type: ignore
143
144 if use_flux:
145 # Return the redistributed flux
146 # (calculated by scarlet.lite.measure.weight_sources)
147 return self.flux_weighted_image # type: ignore
148
149 model = self.components[0].get_model()
150 for component in self.components[1:]:
151 model = model + component.get_model()
152 return model
153
154 def parameterize(self, parameterization: Callable):
155 """Convert the component parameter arrays into Parameter instances
156
157 Parameters
158 ----------
159 parameterization:
160 A function to use to convert parameters of a given type into
161 a `Parameter` in place. It should take a single argument that
162 is the `Component` or `Source` that is to be parameterized.
163 """
164 for component in self.components:
165 component.parameterize(parameterization)
166
167 def to_data(self) -> ScarletSourceData:
168 """Convert to a `ScarletSourceData` for serialization
169
170 Returns
171 -------
172 source_data:
173 The `ScarletSourceData` representation of this source.
174 """
175 from .io import ScarletSourceData
176
177 component_data = [c.to_data() for c in self.components]
178 return ScarletSourceData(components=component_data, metadata=self.metadata)
179
180 def __str__(self):
181 return f"Source<{len(self.components)}>"
182
183 def __repr__(self):
184 return f"Source(components={repr(self.components)})>"
ScarletSourceBaseData to_data(self)
Definition source.py:47
__init__(self, list[Component] components, dict|None metadata=None)
Definition source.py:69
tuple[int, int]|None center(self)
Definition source.py:80
Image get_model(self, bool use_flux=False)
Definition source.py:124
tuple[int, int]|None source_center(self)
Definition source.py:87
ScarletSourceData to_data(self)
Definition source.py:167
parameterize(self, Callable parameterization)
Definition source.py:154