Debian Codegraph @

tests/test_physical_inversion.py

raw
1 import pytest
2 import xarray as xr
3
4 from polsarpro.physical_inversion import dubois_surface_inversion
5
6
7 @pytest.mark.parametrize(
8 "synthetic_poldata",
9 [{"poltypes": ["S", "C3", "T3", "C4", "T4"], "size": 16, "chunk_size": 4}],
10 indirect=True,
11 )
12 def test_dubois_surface_inversion(synthetic_poldata):
13 for _, ds in synthetic_poldata.items():
14 ds = ds.copy()
15 first_var = next(iter(ds.data_vars))
16 ds[first_var] = ds[first_var].where(~((ds.y == 0) & (ds.x == 0)))
17
18 incidence_angle = ds[first_var].real.astype("float32") * 0 + 35.0
19
20 res = dubois_surface_inversion(
21 input_data=ds,
22 incidence_angle=incidence_angle,
23 freq_ghz=5.3,
24 thresh1=3.0,
25 thresh2=3.0,
26 calibration_coeff=1.0,
27 )
28
29 expected_vars = {
30 "dubois_ks",
31 "dubois_er",
32 "dubois_mv",
33 "dubois_mask_out",
34 "dubois_mask_in",
35 "dubois_mask_valid_in_out",
36 }
37 assert expected_vars.issubset(set(res.data_vars))
38
39 shp = ds[first_var].shape
40 for name in expected_vars:
41 assert res[name].shape == shp
42 assert res[name].dtype == "float32"
43
44 original_non_nan_mask = ds.to_array().notnull().all("variable")
45 expected_mask = (
46 res["dubois_mask_in"]
47 * res["dubois_mask_out"]
48 * original_non_nan_mask.astype("float32")
49 ).astype("float32")
50 xr.testing.assert_allclose(res["dubois_mask_valid_in_out"], expected_mask)
51
52
53 @pytest.mark.parametrize(
54 "kwargs, exc_type",
55 [
56 (dict(freq_ghz=0.0), ValueError),
57 (dict(freq_ghz="bad"), TypeError),
58 (dict(thresh1="bad"), TypeError),
59 (dict(thresh2="bad"), TypeError),
60 (dict(calibration_coeff="bad"), TypeError),
61 (dict(calibration_coeff=0.0), ValueError),
62 ],
63 )
64 @pytest.mark.parametrize(
65 "synthetic_poldata",
66 [{"poltypes": ["C3"], "size": 8, "chunk_size": 4}],
67 indirect=True,
68 )
69 def test_dubois_surface_inversion_invalid_scalars(synthetic_poldata, kwargs, exc_type):
70 ds = synthetic_poldata["C3"]
71 first_var = next(iter(ds.data_vars))
72 incidence_angle = ds[first_var].real.astype("float32") * 0 + 35.0
73
74 base_kwargs = dict(
75 input_data=ds,
76 incidence_angle=incidence_angle,
77 freq_ghz=5.3,
78 thresh1=3.0,
79 thresh2=3.0,
80 calibration_coeff=1.0,
81 )
82 base_kwargs.update(kwargs)
83
84 with pytest.raises(exc_type):
85 dubois_surface_inversion(**base_kwargs)
86
87
88 @pytest.mark.parametrize(
89 "synthetic_poldata",
90 [{"poltypes": ["C3"], "size": 8, "chunk_size": 4}],
91 indirect=True,
92 )
93 def test_dubois_surface_inversion_invalid_incidence_angle_type(synthetic_poldata):
94 ds = synthetic_poldata["C3"]
95
96 with pytest.raises(TypeError):
97 dubois_surface_inversion(
98 input_data=ds,
99 incidence_angle=ds.m11.values,
100 freq_ghz=5.3,
101 thresh1=3.0,
102 thresh2=3.0,
103 )