Spaces:
Sleeping
Sleeping
Fix overflow warnings and improve error handling
Browse files- Convert pixel values to int to prevent numpy overflow warnings
- Add defensive programming with .get() for safe dictionary access
- Add debug output to track analysis result structure
- Handle missing keys gracefully in analysis display
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
- app.py +12 -6
- radar_analyzer.py +10 -5
app.py
CHANGED
|
@@ -48,20 +48,26 @@ class RadarAnalysisApp:
|
|
| 48 |
# Analyze the radar image
|
| 49 |
result = self.analyzer.analyze_radar(radar_file, "radar_legend.png")
|
| 50 |
|
| 51 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
analysis_text = f"""
|
| 53 |
**Radar Analysis Results:**
|
| 54 |
|
| 55 |
-
- **Precipitation Pixels:** {
|
| 56 |
-
- **Total Image Pixels:** {
|
| 57 |
-
- **Coverage:** {
|
| 58 |
-
- **Regions Found:** {len(result
|
| 59 |
|
| 60 |
**Detected Precipitation Levels:**
|
| 61 |
"""
|
| 62 |
|
| 63 |
# Add intensity breakdown
|
| 64 |
-
|
|
|
|
| 65 |
if count > 0:
|
| 66 |
analysis_text += f"\n- {intensity}: {count:,} pixels"
|
| 67 |
|
|
|
|
| 48 |
# Analyze the radar image
|
| 49 |
result = self.analyzer.analyze_radar(radar_file, "radar_legend.png")
|
| 50 |
|
| 51 |
+
# Debug: Print what we actually got
|
| 52 |
+
print("Analysis result keys:", result.keys())
|
| 53 |
+
print("Analysis keys:", result['analysis'].keys())
|
| 54 |
+
|
| 55 |
+
# Create analysis summary with safe access
|
| 56 |
+
analysis = result['analysis']
|
| 57 |
analysis_text = f"""
|
| 58 |
**Radar Analysis Results:**
|
| 59 |
|
| 60 |
+
- **Precipitation Pixels:** {analysis.get('precipitation_pixels', 0):,}
|
| 61 |
+
- **Total Image Pixels:** {analysis.get('total_pixels', 0):,}
|
| 62 |
+
- **Coverage:** {analysis.get('precipitation_percentage', 0):.2f}%
|
| 63 |
+
- **Regions Found:** {len(result.get('regions', []))}
|
| 64 |
|
| 65 |
**Detected Precipitation Levels:**
|
| 66 |
"""
|
| 67 |
|
| 68 |
# Add intensity breakdown
|
| 69 |
+
intensity_levels = analysis.get('intensity_levels', analysis.get('pixel_statistics', {}))
|
| 70 |
+
for intensity, count in intensity_levels.items():
|
| 71 |
if count > 0:
|
| 72 |
analysis_text += f"\n- {intensity}: {count:,} pixels"
|
| 73 |
|
radar_analyzer.py
CHANGED
|
@@ -133,7 +133,10 @@ class CanadianRadarAnalyzer:
|
|
| 133 |
|
| 134 |
def color_distance(self, color1: Tuple[int, int, int], color2: Tuple[int, int, int]) -> float:
|
| 135 |
"""Calculate Euclidean distance between two RGB colors."""
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
def find_precipitation_value(self, rgb_color: Tuple[int, int, int]) -> Optional[ColorRange]:
|
| 139 |
"""
|
|
@@ -177,10 +180,11 @@ class CanadianRadarAnalyzer:
|
|
| 177 |
|
| 178 |
for y in range(height):
|
| 179 |
for x in range(width):
|
| 180 |
-
pixel_rgb = tuple(radar_image[y, x])
|
| 181 |
|
| 182 |
# Skip transparent/background pixels
|
| 183 |
-
|
|
|
|
| 184 |
continue
|
| 185 |
|
| 186 |
match = self.find_precipitation_value(pixel_rgb)
|
|
@@ -244,10 +248,11 @@ class CanadianRadarAnalyzer:
|
|
| 244 |
for y in range(height):
|
| 245 |
for x in range(width):
|
| 246 |
if not visited[y, x]:
|
| 247 |
-
pixel_rgb = tuple(radar_image[y, x])
|
| 248 |
|
| 249 |
# Skip background pixels
|
| 250 |
-
|
|
|
|
| 251 |
visited[y, x] = True
|
| 252 |
continue
|
| 253 |
|
|
|
|
| 133 |
|
| 134 |
def color_distance(self, color1: Tuple[int, int, int], color2: Tuple[int, int, int]) -> float:
|
| 135 |
"""Calculate Euclidean distance between two RGB colors."""
|
| 136 |
+
# Convert to int to prevent overflow
|
| 137 |
+
c1_int = [int(c) for c in color1]
|
| 138 |
+
c2_int = [int(c) for c in color2]
|
| 139 |
+
return np.sqrt(sum((c1 - c2) ** 2 for c1, c2 in zip(c1_int, c2_int)))
|
| 140 |
|
| 141 |
def find_precipitation_value(self, rgb_color: Tuple[int, int, int]) -> Optional[ColorRange]:
|
| 142 |
"""
|
|
|
|
| 180 |
|
| 181 |
for y in range(height):
|
| 182 |
for x in range(width):
|
| 183 |
+
pixel_rgb = tuple(int(c) for c in radar_image[y, x])
|
| 184 |
|
| 185 |
# Skip transparent/background pixels
|
| 186 |
+
pixel_sum = sum(pixel_rgb)
|
| 187 |
+
if pixel_sum < 30: # Very dark pixels are likely background
|
| 188 |
continue
|
| 189 |
|
| 190 |
match = self.find_precipitation_value(pixel_rgb)
|
|
|
|
| 248 |
for y in range(height):
|
| 249 |
for x in range(width):
|
| 250 |
if not visited[y, x]:
|
| 251 |
+
pixel_rgb = tuple(int(c) for c in radar_image[y, x])
|
| 252 |
|
| 253 |
# Skip background pixels
|
| 254 |
+
pixel_sum = sum(pixel_rgb)
|
| 255 |
+
if pixel_sum < 30:
|
| 256 |
visited[y, x] = True
|
| 257 |
continue
|
| 258 |
|