lundi 5 juillet 2021

How to use if statement within a try/except?

I have a dataset as follow:

import numpy as np
import pandas as pd
import xarray as xr
size = (5, 4, 10)
pre_anomaly= 5 * np.random.uniform(-2, 2, size)
temp_anomaly = 10 * np.random.uniform(-2, 2, size)
time = pd.date_range("2014-01", periods=10, freq='MS')
ds = xr.Dataset(
    data_vars=dict(
        temperature=(["lat", "lon", "time"], temp_anomaly),
        precipitation=(["lat", "lon", "time"], pre_anomaly),
    ),
    coords={"lon": [25, 25.5, 26, 26.5], "lat": [42.5, 43, 43.5, 44, 44.5], "time": time},
    attrs=dict(description="Weather related data."),)

I have another set of data to be used as upper and lower bound thresholds with the same lon/lat of the above mentioned dataset:

x0= -10 * np.random.rand(5, 4)  # lower bound threshold
x1= 10 * np.random.rand(5, 4)  # upper bound threshold

x = xr.Dataset(
    data_vars=dict(
        lower_bound=(["lat", "lon"], x0),
        upper_bound=(["lat", "lon"], x1),
    ),
    coords={"lon": [25, 25.5, 26, 26.5], "lat": [42.5, 43, 43.5, 44, 44.5]},
    attrs=dict(description="thresholds"),)

Now I would like to use if statement within try/except to check outliers whether the dataset fall within the lower and upper bound thresholds or it exceed the thresholds. Even if one of the correspond values exceed the threshold it you raise an error.

I defined a function like this:

def outlier(dataset, var_name=""):
    try:
        for time in range(len(dataset[var_name]["time"])):
            if x.lower_bound.any() <= dataset[var_name][:, :, time].any() or dataset[var_name][:, :, time].any() <= x.upper_bound.any():
                print("outlier check --------> OK")

            elif x.lower_bound.any() > dataset[var_name][time, :, :].any() or dataset[var_name][time, :, :].any() >= x.upper_bound.any():

                raise ValueError("value is out of the threshold range")

    except ValueError as ve:
        print('ValueError', ve)

outlier(ds, var_name= "precipitation")

However, the outlier function seems sees all values within the thresholds (even when I visually checked them and found some variable values exceed the thresholds).

I tried both .any() and .all() but could not found the issue. And even I am not sure whether I should use try/except, or just if statement is enough.

Aucun commentaire:

Enregistrer un commentaire