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:
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.
Summary
This issue proposes several small improvements to
data_preprocessing/create_masks.pyto improve code quality and numerical stability during NDWI computation.Proposed Improvements
1. Remove duplicate imports
Currently the file contains redundant imports such as:
and duplicated
osimports.Cleaning these improves readability and avoids confusion.
2. Improve NDWI numerical stability
The NDWI calculation currently uses:
If
(green + nir) == 0, this can cause division-by-zero warnings and generate invalid values.Suggested improvement:
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:
Since NDWI ranges from [-1, 1], a clearer scaling approach would be:
This explicitly maps the NDWI range
[-1,1] → [0,255].Benefits
If this enhancement is acceptable, I would be happy to submit a pull request implementing these improvements.