Loading [MathJax]/extensions/tex2jax.js
LSST Applications g0d33ba9806+b932483eba,g0fba68d861+d53f2a615d,g1e78f5e6d3+1e869f36eb,g1ec0fe41b4+f536777771,g1fd858c14a+d5f4961c99,g35bb328faa+fcb1d3bbc8,g4af146b050+2e821d8f6b,g4d2262a081+b02c98aa00,g53246c7159+fcb1d3bbc8,g5a012ec0e7+b20b785ecb,g60b5630c4e+b932483eba,g67b6fd64d1+4086c0989b,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g87b7deb4dc+7d8c31d03d,g8852436030+a639f189fc,g89139ef638+4086c0989b,g9125e01d80+fcb1d3bbc8,g94187f82dc+b932483eba,g989de1cb63+4086c0989b,g9f33ca652e+898eabdf38,g9f7030ddb1+b068313d7a,ga2b97cdc51+b932483eba,ga44b1db4f6+2bd830756e,gabe3b4be73+1e0a283bba,gabf8522325+fa80ff7197,gb1101e3267+f4f1608365,gb58c049af0+f03b321e39,gb89ab40317+4086c0989b,gcf25f946ba+a639f189fc,gd6cbbdb0b4+af3c3595f5,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+4078fef7e5,ge278dab8ac+d65b3c2b70,ge410e46f29+4086c0989b,gf67bdafdda+4086c0989b,gfe06eef73a+6e83fc67a4,v29.0.0.rc5
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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

◆ 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