Skip to content

Commit

Permalink
GTC-2916 Switch to float values
Browse files Browse the repository at this point in the history
  • Loading branch information
manukala6 committed Oct 10, 2024
1 parent 4fa29c0 commit 7938c11
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
26 changes: 17 additions & 9 deletions lambdas/raster_tiler/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,28 @@ def apply_annual_loss_filter(
year <= _end_year - 2000
)

red: ndarray = np.ones(intensity.shape).astype("uint8") * 228
# Construct RGBA bands with floating point values for precision
red: np.ndarray = np.ones(intensity.shape).astype("float32") * 228
green: ndarray = (
np.ones(intensity.shape) * 102
np.ones(intensity.shape).astype("float32") * 102
+ (72 - zoom)
- (scaled_intensity * (3 / max(zoom, 1)))
).astype("uint8")
)
blue: ndarray = (
np.ones(intensity.shape) * 153 + (33 - zoom) - (intensity / max(zoom, 1))
).astype("uint8")
np.ones(intensity.shape).astype("float32") * 153
+ (33 - zoom)
- (intensity / max(zoom, 1))
)
alpha: ndarray = (
(scaled_intensity if zoom < 13 else intensity) * start_year_mask * end_year_mask
).astype("uint8")
).astype("float32")

return np.array([red, green, blue, alpha])
# Ensure GBA values are in [0, 255] range
green = np.clip(green, 0, 255).astype("uint8")
blue = np.clip(blue, 0, 255).astype("uint8")
alpha = np.clip(alpha, 0, 255).astype("uint8")

return np.array([red.astype("uint8"), green, blue, alpha])


##############################
Expand Down Expand Up @@ -352,15 +360,15 @@ def read_tile_cache(dataset, version, implementation, x, y, z, **kwargs) -> ndar

arr = np.array(png)

return seperate_bands(arr)
return separat_bands(arr)


##########################
# Array functions
##########################


def seperate_bands(arr: ndarray) -> ndarray:
def separat_bands(arr: ndarray) -> ndarray:

logger.debug("Store bands in separate arrays")
# convert data from (height, width, bands) to (bands, height, width)
Expand Down
8 changes: 4 additions & 4 deletions tests/lambda/test_array_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
from lambdas.raster_tiler.lambda_function import (
array_to_img,
combine_bands,
seperate_bands,
separat_bands,
)


def test_seperate_bands():
def test_separat_bands():
data = np.array([[[1, 2, 3]], [[1, 2, 3]], [[1, 2, 3]]])

flip = seperate_bands(data)
flip = separat_bands(data)
assert flip.shape == (3, 1, 3)
assert np.all(flip[0] == 1)
assert np.all(flip[1] == 2)
assert np.all(flip[2] == 3)

data = np.array([[[1, 2, 3, 4]], [[1, 2, 3, 4]], [[1, 2, 3, 4]]])

flip = seperate_bands(data)
flip = separat_bands(data)
assert flip.shape == (4, 1, 3)
assert np.all(flip[0] == 1)
assert np.all(flip[1] == 2)
Expand Down

0 comments on commit 7938c11

Please sign in to comment.