-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4178d1c
commit f182f7b
Showing
6 changed files
with
149 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import Tuple | ||
|
||
import numpy as np | ||
from dask.array import Array as DaskArray | ||
from numpy.typing import ArrayLike | ||
|
||
GAIN_8 = 0x0000 | ||
GAIN_2 = 0x8000 | ||
GAIN_1 = 0xC000 | ||
BAD_PIXEL = 0x2000 | ||
PIXEL_MASK = 0x1FFF | ||
|
||
|
||
def correct_images(images: DaskArray, dark: ArrayLike, flat: ArrayLike, gain: Tuple[float, float, float]): | ||
"""_summary_ | ||
Parameters | ||
---------- | ||
images : DaskArray | ||
Input array of images to correct of shape (N, y, x) where N is the | ||
number of images and x and y are the image size. | ||
dark : ArrayLike | ||
Input array of dark images. This should be of shape (3, y, x). | ||
dark[0] is the gain 8 (most sensitive setting) dark image with | ||
dark[2] being the gain 1 (least sensitive) dark image. | ||
flat : ArrayLike | ||
Input array for the flatfield correction. This should be of shape | ||
(y, x) | ||
gain : Tuple[float, float, float] | ||
These are the gain multiplication factors for the three different | ||
gain settings | ||
// Note GAIN_1 is the least sensitive setting which means we need to multiply the | ||
// measured values by 8. Conversly GAIN_8 is the most sensitive and therefore only | ||
// does not need a multiplier | ||
""" | ||
|
||
# Shape checking: | ||
if dark.ndim != 3: | ||
raise ValueError(f"Expected 3D array, got {dark.ndim}D array for darks") | ||
if dark.shape[0] != 3: | ||
raise ValueError(f"Expected 3 dark images, got {dark.shape[0]}") | ||
if dark.shape[-2:] != images.shape[-2]: | ||
raise ValueError(f"Dark images shape {dark.shape[-2:]} does not match images shape {images.shape[-2]}") | ||
if flat.shape != images.shape[-2:]: | ||
raise ValueError(f"Flatfield shape {flat.shape} does not match images shape {images.shape[-2]}") | ||
|
||
corrected = np.where(images & BAD_PIXEL, np.NaN, images) | ||
corrected = np.where(images & GAIN_1, flat * gain[-1] * (corrected - dark[-1, ...]), corrected) | ||
corrected = np.where(images & GAIN_2, flat * gain[-2] * (corrected - dark[-2, ...]), corrected) | ||
corrected = np.where(images & GAIN_8, flat * gain[-3] * (corrected - dark[-3, ...]), corrected) | ||
|
||
return corrected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Literal, Union | ||
|
||
import dask.array as da | ||
from dask.array import Array as DaskArray | ||
|
||
|
||
def rotate90(images: DaskArray, sense: Union[Literal["cw"], Literal["ccw"]] = "cw") -> DaskArray: | ||
""" | ||
Rotate images by 90 degrees using Dask. | ||
This whole function is a moot wrapper around `da.rot90` from Dask, but written | ||
explicitly to match the old C code. | ||
Parameters | ||
---------- | ||
images : da.Array | ||
Input Dask array of images to rotate of shape (N, y, x), | ||
where N is the number of images and y, x are the image dimensions. | ||
sense : str, optional | ||
'cw' to rotate clockwise, 'ccw' to rotate anticlockwise. | ||
Default is 'cw'. | ||
Returns | ||
------- | ||
da.Array | ||
The rotated images as a Dask array. | ||
""" | ||
# Rotate images. The axes (1, 2) specify the plane of rotation (y-x plane for each image). | ||
# k controls the direction and repetitions of the rotation. | ||
if sense == "ccw": | ||
k = 1 | ||
elif sense == "cw": | ||
k = -1 | ||
else: | ||
raise ValueError("sense must be 'cw' or 'ccw'") | ||
rotated_images = da.rot90(images, k=k, axes=(-2, -1)) | ||
return rotated_images |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters