Skip to content

Commit

Permalink
Fix scaling and rounding issues
Browse files Browse the repository at this point in the history
Round all indices to 0 decimal places instead of 4 so that they are
correctly rounded to integer values.

For TVI, set scale factor to 0.01.

Fixes #37
Fixes #38
  • Loading branch information
chuckwondo committed Sep 19, 2024
1 parent a6c6d28 commit 2441a6b
Show file tree
Hide file tree
Showing 21 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.PHONY = build test
.DEFAULT_GOAL = test

build:
docker compose build
Expand Down
8 changes: 5 additions & 3 deletions hls_vi/generate_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ def savi(data: BandData) -> np.ma.masked_array:

def tvi(data: BandData) -> np.ma.masked_array:
g, r, nir = data[Band.G], data[Band.R], data[Band.NIR]
# We do NOT multiply by 10_000 like we do for other indices.
return (120 * (nir - g) - 200 * (r - g)) / 2 # pyright: ignore[reportReturnType]


Expand All @@ -328,7 +327,7 @@ class Index(Enum):
NDVI = ("Normalized Difference Vegetation Index",)
NDWI = ("Normalized Difference Water Index",)
SAVI = ("Soil-Adjusted Vegetation Index",)
TVI = ("Triangular Vegetation Index", 1.0)
TVI = ("Triangular Vegetation Index", 0.01)

def __init__(self, long_name: str, scale_factor: SupportsFloat = 0.0001) -> None:
function_name = self.name.lower()
Expand All @@ -343,7 +342,10 @@ def __init__(self, long_name: str, scale_factor: SupportsFloat = 0.0001) -> None

def __call__(self, data: BandData) -> np.ma.masked_array:
scaled_index = self.compute_index(data) / self.scale_factor
return np.ma.round(scaled_index, decimals=4).astype(np.int16)
# We need to round to whole numbers (i.e., 0 decimal places, which is
# the default for np.round) because we convert to integer values, but
# numpy's conversion to integer types performs truncation, not rounding.
return np.ma.round(scaled_index).astype(np.int16)


def parse_args() -> Tuple[Path, Path, str]:
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 2441a6b

Please sign in to comment.