Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silence "converting a masked element to nan" warning #924

Merged
merged 11 commits into from
Oct 24, 2023
4 changes: 1 addition & 3 deletions api/app/database/user_info_model.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Alert database model."""
"""UserInfo database model."""
import datetime
import json
import logging

from sqlalchemy import JSON, TIMESTAMP, Column, DateTime, Identity, Integer, String
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
from sqlalchemy.sql.sqltypes import Boolean

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

Base = declarative_base()
Expand Down
6 changes: 5 additions & 1 deletion api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
from .geotiff_from_stac_api import get_geotiff
from .models import AlertsModel, StatsModel, UserInfoPydanticModel

logging.basicConfig(level=logging.DEBUG)
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.DEBUG,
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger(__name__)

# silence boto3 logging to avoid spamming the logs
Expand Down
26 changes: 26 additions & 0 deletions api/app/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import requests
from fastapi import HTTPException

Expand All @@ -12,3 +14,27 @@ def forward_http_error(resp: requests.Response, excluded_codes: list[int]) -> No
obj = resp.json()
detail = obj.get("detail", "Unknown error")
raise HTTPException(status_code=resp.status_code, detail=detail)


class WarningsFilter(logging.Filter):
"""Custom logging filter for warnings."""

def __init__(self):
super().__init__()
self.max_warnings = 1
self.warning_count = 0

def filter(self, record):
if (
self.warning_count >= self.max_warnings
and record.levelno == logging.WARNING
and "converting a masked element to nan" in record.getMessage()
):
return True
if (
record.levelno == logging.WARNING
and "converting a masked element to nan" in record.getMessage()
):
self.warning_count += 1

return False
5 changes: 5 additions & 0 deletions api/app/zonal_stats.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Calulate zonal statistics and return a json or a geojson."""
import logging
import warnings
from collections import defaultdict
from datetime import datetime
from json import dump, load
Expand All @@ -20,6 +21,7 @@
)
from app.raster_utils import calculate_pixel_area, gdal_calc, reproj_match
from app.timer import timed
from app.utils import WarningsFilter
from app.validation import VALID_OPERATORS
from fastapi import HTTPException
from rasterio.warp import Resampling
Expand All @@ -29,6 +31,9 @@

logger = logging.getLogger(__name__)

# Add custom filter to silence 'converting a masked element to nan' warnings
logger.addFilter(WarningsFilter())


DEFAULT_STATS = ["min", "max", "mean", "median", "sum", "std", "nodata", "count"]

Expand Down
Loading