Fix numpy warnings: MaskedArray partition et Mean of empty slice

- Convert MaskedArray to ndarray avant np.percentile() via
  np.asarray(data.compressed()) et np.ma.filled(data, np.nan)
- Supprimer RuntimeWarning "Mean of empty slice" dans MSRM et
  ondelette avec warnings.catch_warnings()
- Ajout import warnings dans visualizations.py

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jacquin Antoine
2026-05-10 03:02:36 +02:00
parent e2845b9e6d
commit 569f2e680c
2 changed files with 15 additions and 6 deletions

View File

@ -204,7 +204,7 @@ def _apply_colormap(data, tif_file):
# Find matching colormap # Find matching colormap
for key, info in COLORMAPS.items(): for key, info in COLORMAPS.items():
if key in name: if key in name:
valid_data = np.asarray(data[~np.isnan(data)] if hasattr(data, 'compressed') else data.flatten()) valid_data = np.asarray(data.compressed() if hasattr(data, 'compressed') else data.flatten())
valid_data = valid_data[~np.isnan(valid_data)] valid_data = valid_data[~np.isnan(valid_data)]
vmin = vmax = None vmin = vmax = None
@ -239,7 +239,7 @@ def _apply_colormap(data, tif_file):
return data, info['cmap'], info['title'], legend, info['description'], False return data, info['cmap'], info['title'], legend, info['description'], False
# Default: terrain colormap with percentile stretch # Default: terrain colormap with percentile stretch
valid_data = np.asarray(data[~np.isnan(data)] if hasattr(data, 'compressed') else data.flatten()) valid_data = np.asarray(data.compressed() if hasattr(data, 'compressed') else data.flatten())
valid_data = valid_data[~np.isnan(valid_data)] valid_data = valid_data[~np.isnan(valid_data)]
p2, p98 = np.percentile(valid_data, (2, 98)) p2, p98 = np.percentile(valid_data, (2, 98))
data = np.clip((data - p2) / (p98 - p2), 0, 1) data = np.clip((data - p2) / (p98 - p2), 0, 1)
@ -324,6 +324,10 @@ def tif_to_png(tif_file, vis_dir, resolution):
valid_data = np.asarray(data.compressed() if hasattr(data, 'compressed') else data.flatten()) valid_data = np.asarray(data.compressed() if hasattr(data, 'compressed') else data.flatten())
valid_data = valid_data[~np.isnan(valid_data)] valid_data = valid_data[~np.isnan(valid_data)]
# Convert MaskedArray to plain ndarray (NaN-filled) to avoid numpy warnings
if isinstance(data, np.ma.MaskedArray):
data = np.ma.filled(data, np.nan)
# Apply colormap # Apply colormap
data, cmap, title, legend_label, description, is_rgb_result = _apply_colormap(data, tif_file) data, cmap, title, legend_label, description, is_rgb_result = _apply_colormap(data, tif_file)

View File

@ -6,6 +6,7 @@ parameters and returns the path to the output GeoTIFF file, or None on error.
import logging import logging
import time import time
import warnings
from pathlib import Path from pathlib import Path
import numpy as np import numpy as np
@ -391,7 +392,9 @@ def generate_mslrm(dem_file, basename, vis_dir, resolution):
lrm_stack.append(lrm_norm.astype(np.float32)) lrm_stack.append(lrm_norm.astype(np.float32))
lrm_array = np.array(lrm_stack) lrm_array = np.array(lrm_stack)
with np.errstate(invalid='ignore'): with np.errstate(invalid='ignore', divide='ignore'):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', message='Mean of empty slice')
mslrm = np.sqrt(np.nanmean(lrm_array ** 2, axis=0)) mslrm = np.sqrt(np.nanmean(lrm_array ** 2, axis=0))
mslrm[nan_mask] = np.nan mslrm[nan_mask] = np.nan
_save_tif(output, mslrm.astype(np.float32), transform, crs) _save_tif(output, mslrm.astype(np.float32), transform, crs)
@ -659,7 +662,9 @@ def generate_wavelet(dem_file, basename, vis_dir, resolution):
response = response / std_val response = response / std_val
wavelet_stack.append(response) wavelet_stack.append(response)
with np.errstate(invalid='ignore'): with np.errstate(invalid='ignore', divide='ignore'):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', message='Mean of empty slice')
combined = np.sqrt(np.nanmean(np.array(wavelet_stack) ** 2, axis=0)) combined = np.sqrt(np.nanmean(np.array(wavelet_stack) ** 2, axis=0))
combined[nan_mask] = np.nan combined[nan_mask] = np.nan