LSST Applications g042eb84c57+730a74494b,g04e9c324dd+8c5ae1fdc5,g134cb467dc+1f1e3e7524,g199a45376c+0ba108daf9,g1fd858c14a+fa7d31856b,g210f2d0738+f66ac109ec,g262e1987ae+83a3acc0e5,g29ae962dfc+d856a2cb1f,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+a1e0c9f713,g47891489e3+0d594cb711,g4d44eb3520+c57ec8f3ed,g4d7b6aa1c5+f66ac109ec,g53246c7159+8c5ae1fdc5,g56a1a4eaf3+fd7ad03fde,g64539dfbff+f66ac109ec,g67b6fd64d1+0d594cb711,g67fd3c3899+f66ac109ec,g6985122a63+0d594cb711,g74acd417e5+3098891321,g786e29fd12+668abc6043,g81db2e9a8d+98e2ab9f28,g87389fa792+8856018cbb,g89139ef638+0d594cb711,g8d7436a09f+80fda9ce03,g8ea07a8fe4+760ca7c3fc,g90f42f885a+033b1d468d,g97be763408+a8a29bda4b,g99822b682c+e3ec3c61f9,g9d5c6a246b+0d5dac0c3d,ga41d0fce20+9243b26dd2,gbf99507273+8c5ae1fdc5,gd7ef33dd92+0d594cb711,gdab6d2f7ff+3098891321,ge410e46f29+0d594cb711,geaed405ab2+c4bbc419c6,gf9a733ac38+8c5ae1fdc5,w.2025.38
LSST Data Management Base Package
Loading...
Searching...
No Matches
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 1286 of file image.py.

1286def _operate_on_images(image1: Image, image2: Image | ScalarLike, op: Callable) -> Image:
1287 """Perform an operation on two images, that may or may not be spectrally
1288 and spatially aligned.
1289
1290 Parameters
1291 ----------
1292 image1:
1293 The image on the LHS of the operation
1294 image2:
1295 The image on the RHS of the operation
1296 op:
1297 The operation used to combine the images.
1298
1299 Returns
1300 -------
1301 image:
1302 The resulting combined image.
1303 """
1304 if type(image2) in ScalarTypes:
1305 return image1.copy_with(data=op(image1.data, image2))
1306 image2 = cast(Image, image2)
1307 if image1.bands == image2.bands and image1.bbox == image2.bbox:
1308 # The images perfectly overlap, so just combine their results
1309 with np.errstate(divide="ignore", invalid="ignore"):
1310 result = op(image1.data, image2.data)
1311 return Image(result, bands=image1.bands, yx0=image1.yx0)
1312
1313 if op != operator.add and op != operator.sub and image1.bands != image2.bands:
1314 msg = "Images with different bands can only be combined using addition and subtraction, "
1315 msg += f"got {op}, with bands {image1.bands}, {image2.bands}"
1316 raise ValueError(msg)
1317
1318 # Use all of the bands in the first image
1319 bands = image1.bands
1320 # Add on any bands from the second image not contained in the first image
1321 bands = bands + tuple(band for band in image2.bands if band not in bands)
1322 # Create a box that contains both images
1323 bbox = image1.bbox | image2.bbox
1324 # Create an image that will contain both images
1325 if len(bands) > 0:
1326 shape = (len(bands),) + bbox.shape
1327 else:
1328 shape = bbox.shape
1329
1330 if op == operator.add or op == operator.sub:
1331 dtype = get_combined_dtype(image1, image2)
1332 result = Image(np.zeros(shape, dtype=dtype), bands=bands, yx0=cast(tuple[int, int], bbox.origin))
1333 # Add the first image in place
1334 image1.insert_into(result, operator.add)
1335 # Use the operator to insert the second image
1336 image2.insert_into(result, op)
1337 else:
1338 # Project both images into the full bbox
1339 image1 = image1.project(bbox=bbox)
1340 image2 = image2.project(bbox=bbox)
1341 result = op(image1, image2)
1342 return result
1343
1344

◆ 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

◆ 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 1345 of file image.py.

1349) -> Image:
1350 """Insert one image into another image
1351
1352 Parameters
1353 ----------
1354 main_image:
1355 The image that will have `sub_image` insertd.
1356 sub_image:
1357 The image that is inserted into `main_image`.
1358 op:
1359 The operator to use for insertion
1360 (addition, subtraction, multiplication, etc.).
1361
1362 Returns
1363 -------
1364 main_image: Image
1365 The `main_image`, with the `sub_image` inserted in place.
1366 """
1367 if len(main_image.bands) == 0 and len(sub_image.bands) == 0:
1368 slices = sub_image.matched_slices(main_image.bbox)
1369 image_slices = slices[1]
1370 self_slices = slices[0]
1371 else:
1372 band_indices = sub_image.matched_spectral_indices(main_image)
1373 slices = sub_image.matched_slices(main_image.bbox)
1374 image_slices = (band_indices[0],) + slices[1] # type: ignore
1375 self_slices = (band_indices[1],) + slices[0] # type: ignore
1376
1377 main_image._data[image_slices] = op(main_image.data[image_slices], sub_image.data[self_slices])
1378 return main_image