Skip to content

Fix safe_average() function in Measurements.py

An obscure error popped up while testing the Operations app:

{
    "detail": "1 validation error for AverageMeasurement\nangle\n  Input should be a valid number [type=float_type, input_value=None, input_type=NoneType]\n    For further information visit https://errors.pydantic.dev/2.9/v/float_type"
}

This probably arises because Measurement.py's save_average function can't handle a list that is all zeros:

def safe_average(l: List[Optional[float]]):
    values = l and [f for f in l if f] or None
    return values and numpy.nanmean(values) or None

Something like the following should fix this, but it seems like there must be a better way to NOT treat zero as a boolean here:

def safe_average(l: List[Optional[float]]):
    values = l and [f for f in l if (isinstance(f, (int, float)) and not isinstance(f, bool))] or None
    return values and numpy.nanmean(values) or None