Serialize feature_stats for JSON storage Returns: Dictionary that can be JSON-serialized
(self)
| 2252 | return min(1.0, max(0.0, scaled)) |
| 2253 | |
| 2254 | def _serialize_feature_stats(self) -> Dict[str, Any]: |
| 2255 | """ |
| 2256 | Serialize feature_stats for JSON storage |
| 2257 | |
| 2258 | Returns: |
| 2259 | Dictionary that can be JSON-serialized |
| 2260 | """ |
| 2261 | serialized = {} |
| 2262 | for feature_name, stats in self.feature_stats.items(): |
| 2263 | # Convert to JSON-serializable format |
| 2264 | serialized_stats = {} |
| 2265 | for key, value in stats.items(): |
| 2266 | if key == "values": |
| 2267 | # Limit size to prevent excessive memory usage |
| 2268 | # Keep only the most recent 100 values for percentile calculations |
| 2269 | if isinstance(value, list) and len(value) > 100: |
| 2270 | serialized_stats[key] = value[-100:] |
| 2271 | else: |
| 2272 | serialized_stats[key] = value |
| 2273 | else: |
| 2274 | # Convert numpy types to Python native types |
| 2275 | if hasattr(value, "item"): # numpy scalar |
| 2276 | serialized_stats[key] = value.item() |
| 2277 | else: |
| 2278 | serialized_stats[key] = value |
| 2279 | serialized[feature_name] = serialized_stats |
| 2280 | return serialized |
| 2281 | |
| 2282 | def _deserialize_feature_stats( |
| 2283 | self, stats_dict: Dict[str, Any] |