LSST Applications g0603fd7c41+501e3db9f9,g0aad566f14+23d8574c86,g0dd44d6229+a1a4c8b791,g2079a07aa2+86d27d4dc4,g2305ad1205+a62672bbc1,g2bbee38e9b+047b288a59,g337abbeb29+047b288a59,g33d1c0ed96+047b288a59,g3a166c0a6a+047b288a59,g3d1719c13e+23d8574c86,g487adcacf7+cb7fd919b2,g4be5004598+23d8574c86,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+4a9e435310,g63cd9335cc+585e252eca,g858d7b2824+23d8574c86,g88963caddf+0cb8e002cc,g99cad8db69+43388bcaec,g9ddcbc5298+9a081db1e4,ga1e77700b3+a912195c07,gae0086650b+585e252eca,gb0e22166c9+60f28cb32d,gb2522980b2+793639e996,gb3a676b8dc+b4feba26a1,gb4b16eec92+63f8520565,gba4ed39666+c2a2e4ac27,gbb8dafda3b+a5d255a82e,gc120e1dc64+d820f8acdb,gc28159a63d+047b288a59,gc3e9b769f7+f4f1cc6b50,gcf0d15dbbd+a1a4c8b791,gdaeeff99f8+f9a426f77a,gdb0af172c8+b6d5496702,ge79ae78c31+047b288a59,w.2024.19
LSST Data Management Base Package
Loading...
Searching...
No Matches
Classes | Functions
lsst.scarlet.lite.image Namespace Reference

Classes

class  Image
 
class  MismatchedBandsError
 
class  MismatchedBoxError
 

Functions

list[DTypeLike] get_dtypes (*np.ndarray|Image|ScalarLike data)
 
DTypeLike get_combined_dtype (*np.ndarray|Image|ScalarLike data)
 
Image _operate_on_images (Image image1, Image|ScalarLike image2, Callable op)
 
Image insert_image (Image main_image, Image sub_image, Callable op=operator.add)
 

Function Documentation

◆ _operate_on_images()

Image lsst.scarlet.lite.image._operate_on_images ( Image image1,
Image | ScalarLike image2,
Callable op )
protected
Perform an operation on two images, that may or may not be spectrally
and spatially aligned.

Parameters
----------
image1:
    The image on the LHS of the operation
image2:
    The image on the RHS of the operation
op:
    The operation used to combine the images.

Returns
-------
image:
    The resulting combined image.

Definition at line 1234 of file image.py.

1234def _operate_on_images(image1: Image, image2: Image | ScalarLike, op: Callable) -> Image:
1235 """Perform an operation on two images, that may or may not be spectrally
1236 and spatially aligned.
1237
1238 Parameters
1239 ----------
1240 image1:
1241 The image on the LHS of the operation
1242 image2:
1243 The image on the RHS of the operation
1244 op:
1245 The operation used to combine the images.
1246
1247 Returns
1248 -------
1249 image:
1250 The resulting combined image.
1251 """
1252 if type(image2) in ScalarTypes:
1253 return image1.copy_with(data=op(image1.data, image2))
1254 image2 = cast(Image, image2)
1255 if image1.bands == image2.bands and image1.bbox == image2.bbox:
1256 # The images perfectly overlap, so just combine their results
1257 with np.errstate(divide="ignore", invalid="ignore"):
1258 result = op(image1.data, image2.data)
1259 return Image(result, bands=image1.bands, yx0=image1.yx0)
1260
1261 if op != operator.add and op != operator.sub and image1.bands != image2.bands:
1262 msg = "Images with different bands can only be combined using addition and subtraction, "
1263 msg += f"got {op}, with bands {image1.bands}, {image2.bands}"
1264 raise ValueError(msg)
1265
1266 # Use all of the bands in the first image
1267 bands = image1.bands
1268 # Add on any bands from the second image not contained in the first image
1269 bands = bands + tuple(band for band in image2.bands if band not in bands)
1270 # Create a box that contains both images
1271 bbox = image1.bbox | image2.bbox
1272 # Create an image that will contain both images
1273 if len(bands) > 0:
1274 shape = (len(bands),) + bbox.shape
1275 else:
1276 shape = bbox.shape
1277
1278 if op == operator.add or op == operator.sub:
1279 dtype = get_combined_dtype(image1, image2)
1280 result = Image(np.zeros(shape, dtype=dtype), bands=bands, yx0=cast(tuple[int, int], bbox.origin))
1281 # Add the first image in place
1282 image1.insert_into(result, operator.add)
1283 # Use the operator to insert the second image
1284 image2.insert_into(result, op)
1285 else:
1286 # Project both images into the full bbox
1287 image1 = image1.project(bbox=bbox)
1288 image2 = image2.project(bbox=bbox)
1289 result = op(image1, image2)
1290 return result
1291
1292

◆ get_combined_dtype()

DTypeLike lsst.scarlet.lite.image.get_combined_dtype ( *np.ndarray | Image | ScalarLike data)
Get the combined dtype for a collection of arrays to prevent loss
of precision.

Parameters
----------
data:
    The arrays to use for calculating the dtype

Returns
-------
result: np.dtype
    The resulting dtype.

Definition at line 66 of file image.py.

66def get_combined_dtype(*data: np.ndarray | Image | ScalarLike) -> DTypeLike:
67 """Get the combined dtype for a collection of arrays to prevent loss
68 of precision.
69
70 Parameters
71 ----------
72 data:
73 The arrays to use for calculating the dtype
74
75 Returns
76 -------
77 result: np.dtype
78 The resulting dtype.
79 """
80 dtypes = get_dtypes(*data)
81 return max(dtypes) # type: ignore
82
83
int max

◆ get_dtypes()

list[DTypeLike] lsst.scarlet.lite.image.get_dtypes ( *np.ndarray | Image | ScalarLike data)
Get a list of dtypes from a list of arrays, images, or scalars

Parameters
----------
data:
    The arrays to use for calculating the dtype

Returns
-------
result:
    A list of datatypes.

Definition at line 44 of file image.py.

44def get_dtypes(*data: np.ndarray | Image | ScalarLike) -> list[DTypeLike]:
45 """Get a list of dtypes from a list of arrays, images, or scalars
46
47 Parameters
48 ----------
49 data:
50 The arrays to use for calculating the dtype
51
52 Returns
53 -------
54 result:
55 A list of datatypes.
56 """
57 dtypes: list[DTypeLike] = [None] * len(data)
58 for d, element in enumerate(data):
59 if hasattr(element, "dtype"):
60 dtypes[d] = cast(np.ndarray, element).dtype
61 else:
62 dtypes[d] = np.dtype(type(element))
63 return dtypes
64
65

◆ insert_image()

Image lsst.scarlet.lite.image.insert_image ( Image main_image,
Image sub_image,
Callable op = operator.add )
Insert one image into another image

Parameters
----------
main_image:
    The image that will have `sub_image` insertd.
sub_image:
    The image that is inserted into `main_image`.
op:
    The operator to use for insertion
    (addition, subtraction, multiplication, etc.).

Returns
-------
main_image: Image
    The `main_image`, with the `sub_image` inserted in place.

Definition at line 1293 of file image.py.

1297) -> Image:
1298 """Insert one image into another image
1299
1300 Parameters
1301 ----------
1302 main_image:
1303 The image that will have `sub_image` insertd.
1304 sub_image:
1305 The image that is inserted into `main_image`.
1306 op:
1307 The operator to use for insertion
1308 (addition, subtraction, multiplication, etc.).
1309
1310 Returns
1311 -------
1312 main_image: Image
1313 The `main_image`, with the `sub_image` inserted in place.
1314 """
1315 if len(main_image.bands) == 0 and len(sub_image.bands) == 0:
1316 slices = sub_image.matched_slices(main_image.bbox)
1317 image_slices = slices[1]
1318 self_slices = slices[0]
1319 else:
1320 band_indices = sub_image.matched_spectral_indices(main_image)
1321 slices = sub_image.matched_slices(main_image.bbox)
1322 image_slices = (band_indices[0],) + slices[1] # type: ignore
1323 self_slices = (band_indices[1],) + slices[0] # type: ignore
1324
1325 main_image._data[image_slices] = op(main_image.data[image_slices], sub_image.data[self_slices])
1326 return main_image