-
Notifications
You must be signed in to change notification settings - Fork 47
Enhancement: Improve NDWI stability and clean redundant imports in create_masks.py #87
Description
Summary
This issue proposes several small improvements to data_preprocessing/create_masks.py to improve code quality and numerical stability during NDWI computation.
Proposed Improvements
1. Remove duplicate imports
Currently the file contains redundant imports such as:
from rasterio import mask
from rasterio.mask import mask
and duplicated os imports.
Cleaning these improves readability and avoids confusion.
2. Improve NDWI numerical stability
The NDWI calculation currently uses:
ndwi = (green - nir) / (green + nir)
If (green + nir) == 0, this can cause division-by-zero warnings and generate invalid values.
Suggested improvement:
ndwi = (green - nir) / (green + nir + 1e-8)
This avoids numerical instability while keeping NDWI values in the same range.
3. Improve NDWI scaling to 8-bit
Currently NDWI values are converted using:
(out_image * 127) + 128
Since NDWI ranges from [-1, 1], a clearer scaling approach would be:
((ndwi + 1) * 127.5).astype(np.uint8)
This explicitly maps the NDWI range [-1,1] → [0,255].
Benefits
- Improves numerical robustness of NDWI computation
- Reduces redundant imports
- Clarifies NDWI scaling logic
- Improves maintainability of preprocessing code
If this enhancement is acceptable, I would be happy to submit a pull request implementing these improvements.