LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
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