LSST Applications g0f08755f38+9c285cab97,g1635faa6d4+13f3999e92,g1653933729+a8ce1bb630,g1a0ca8cf93+bf6eb00ceb,g28da252d5a+0829b12dee,g29321ee8c0+5700dc9eac,g2bbee38e9b+9634bc57db,g2bc492864f+9634bc57db,g2cdde0e794+c2c89b37c4,g3156d2b45e+41e33cbcdc,g347aa1857d+9634bc57db,g35bb328faa+a8ce1bb630,g3a166c0a6a+9634bc57db,g3e281a1b8c+9f2c4e2fc3,g414038480c+077ccc18e7,g41af890bb2+fde0dd39b6,g5fbc88fb19+17cd334064,g781aacb6e4+a8ce1bb630,g80478fca09+55a9465950,g82479be7b0+d730eedb7d,g858d7b2824+9c285cab97,g9125e01d80+a8ce1bb630,g9726552aa6+10f999ec6a,ga5288a1d22+2a84bb7594,gacf8899fa4+c69c5206e8,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+9634bc57db,gcf0d15dbbd+4b7d09cae4,gda3e153d99+9c285cab97,gda6a2b7d83+4b7d09cae4,gdaeeff99f8+1711a396fd,ge2409df99d+5e831397f4,ge79ae78c31+9634bc57db,gf0baf85859+147a0692ba,gf3967379c6+41c94011de,gf3fb38a9a8+8f07a9901b,gfb92a5be7c+9c285cab97,w.2024.46
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