Skip to content

Commit

Permalink
Merge pull request #27 from lsst/tickets/DM-39243
Browse files Browse the repository at this point in the history
DM-39243: Add a factory method for OwnedImagePlanes
  • Loading branch information
arunkannawadi committed Dec 20, 2023
2 parents 9faf7ed + 013bab8 commit 9e7d708
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 10 deletions.
1 change: 0 additions & 1 deletion .github/workflows/rebase_checker.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
name: Check that 'main' is not merged into the development branch

on: pull_request
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/cell_coadds/_cell_coadd_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from abc import ABCMeta, abstractmethod
from collections.abc import Iterable, Mapping
from typing import Any, ClassVar
from typing import Any, ClassVar, cast

import lsst.geom
import lsst.pex.config as pexConfig
Expand Down Expand Up @@ -265,7 +265,7 @@ def run( # type: ignore[override] # TODO: Remove after DM-34696 is fixed.
common = CommonComponents(
units=CoaddUnits.nJy,
wcs=patchInfo.wcs,
band=quantumDataId.get("band", None),
band=cast(str, quantumDataId.get("band", "")),
identifiers=PatchIdentifiers.from_data_id(quantumDataId),
)

Expand Down
5 changes: 5 additions & 0 deletions python/lsst/cell_coadds/_common_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class CoaddUnits(enum.Enum):
flux unit other than nJy).
"""

legacy = enum.auto()
"""Pixels in semi-arbitrary unit obtained by scaling the warps to a common
zeropoint (typically 27).
"""

nJy = enum.auto()
"""Pixels represent flux in nanojanskies.
"""
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/cell_coadds/_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def from_data_id(cls, data_id: DataCoordinate) -> PatchIdentifiers:
skymap=cast(str, data_id["skymap"]),
tract=cast(int, data_id["tract"]),
patch=Index2D(x=patch_record.cell_x, y=patch_record.cell_y),
band=data_id.get("band"),
band=cast(str, data_id.get("band")),
)


Expand Down Expand Up @@ -111,7 +111,7 @@ def from_data_id( # type: ignore [override]
skymap=cast(str, data_id["skymap"]),
tract=cast(int, data_id["tract"]),
patch=Index2D(x=patch_record.cell_x, y=patch_record.cell_y),
band=data_id.get("band"),
band=cast(str, data_id.get("band")),
cell=cell,
)

Expand Down
37 changes: 35 additions & 2 deletions python/lsst/cell_coadds/_image_planes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@

from abc import ABC, abstractmethod
from collections.abc import Callable, Sequence
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Self

from lsst.afw.image import MaskedImageF

if TYPE_CHECKING:
from lsst.afw.image import Mask
from lsst.afw.image import Mask, MaskedImage
from lsst.geom import Box2I

from .typing_helpers import ImageLike
Expand Down Expand Up @@ -116,6 +116,39 @@ def __init__(
self._mask_fractions = mask_fractions
self._noise_realizations = tuple(noise_realizations)

@classmethod
def from_masked_image(
cls,
masked_image: MaskedImage,
mask_fractions: ImageLike | None = None,
noise_realizations: Sequence[ImageLike] = (),
) -> Self:
"""Construct from an `lsst.afw.image.MaskedImage`.
Parameters
----------
masked_image : `~lsst.afw.image.MaskedImage`
The image to construct from. The image, mask and variance planes
of ``masked_image`` will be used as the image, mask and variance
planes of the constructed object.
mask_fractions : `ImageLike`, optional
The mask fractions image.
noise_realizations : `Sequence` [`ImageLike`], optional
The noise realizations.
Returns
-------
self : `OwnedImagePlanes`
An instance of OwnedImagePlanes.
"""
return cls(
image=masked_image.image,
mask=masked_image.mask,
variance=masked_image.variance,
mask_fractions=mask_fractions,
noise_realizations=noise_realizations,
)

@property
def bbox(self) -> Box2I:
# Docstring inherited.
Expand Down
13 changes: 11 additions & 2 deletions python/lsst/cell_coadds/_multiple_cell_coadd.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(
self._common = common
cells_builder = GridContainer[SingleCellCoadd](self._grid.shape)
self._mask_fraction_names: set[str] = set()

for cell in cells:
index = cell.identifiers.cell
cells_builder[index] = cell
Expand All @@ -97,9 +98,17 @@ def __init__(
if n_noise_realizations:
n_noise_realizations.add(self._n_noise_realizations)
raise ValueError(
f"Inconsistent number of noise realizations ({n_noise_realizations}) betwen cells."
f"Inconsistent number of noise realizations ({n_noise_realizations}) between cells."
)
max_inner_bbox = Box2I(self._cells.first.inner.bbox.getMin(), self._cells.last.inner.bbox.getMax())

# Finish the construction without relying on the first and last of
# self._cells so we can construct an instance with partial list.
indices = list(cells_builder.indices())
max_inner_bbox = Box2I(
grid.bbox_of(indices[0]).getMin(),
grid.bbox_of(indices[-1]).getMax(),
)

if inner_bbox is None:
inner_bbox = max_inner_bbox
elif not max_inner_bbox.contains(inner_bbox):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_coadds.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def setUpClass(cls) -> None:
np.random.seed(42)
data_id = test_utils.generate_data_id()
common = CommonComponents(
units=CoaddUnits.nJy,
units=CoaddUnits.legacy, # units here are arbitrary.
wcs=test_utils.generate_wcs(),
band=data_id["band"],
identifiers=PatchIdentifiers.from_data_id(data_id),
Expand Down

0 comments on commit 9e7d708

Please sign in to comment.