Skip to content

Commit

Permalink
Merge pull request #187 from mikapfl/new-pint
Browse files Browse the repository at this point in the history
fix: works for latest pint
  • Loading branch information
mikapfl authored Dec 12, 2023
2 parents d5e5d91 + 77f96ef commit 1c42e29
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions changelog_unreleased/new-pint.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Fixed compatibility with latest `pint` and `pint-xarray` libraries.
14 changes: 8 additions & 6 deletions docs/usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@
"import primap2.tests\n",
"\n",
"ds = primap2.tests.examples.opulent_ds()\n",
"ds[\"CO2\"].pr.loc[{\"product\": \"milk\"}] = np.nan\n",
"ds[\"CO2\"].pr.loc[{\"product\": \"milk\"}].pint.magnitude[:] = np.nan\n",
"\n",
"ds.pr.coverage(\"product\", \"entity\", \"area\")"
]
Expand Down Expand Up @@ -966,7 +966,7 @@
"temp = da.sum(dim=\"area (ISO3)\")\n",
"temp = temp.expand_dims({\"area (ISO3)\": [\"LATAM\"]})\n",
"# delete data from the country level for the years 2005-2009 (inclusive)\n",
"da.loc[{\"time\": slice(\"2005\", \"2009\")}] = np.nan\n",
"da.loc[{\"time\": slice(\"2005\", \"2009\")}].pint.magnitude[:] = np.nan\n",
"# add regional data to the array\n",
"da = xr.concat([da, temp], dim=\"area (ISO3)\")\n",
"da"
Expand Down Expand Up @@ -1130,7 +1130,9 @@
"source": [
"# delete all data about the years 2005-2009 (inclusive) from the\n",
"# KYOTOGHG data\n",
"ds[\"KYOTOGHG\"].loc[{\"time\": slice(\"2005\", \"2009\")}] = np.nan\n",
"ds[\"KYOTOGHG\"].loc[{\"time\": slice(\"2005\", \"2009\")}].pint.magnitude[\n",
" :\n",
"] = np.nan\n",
"ds[\"KYOTOGHG\"]"
]
},
Expand Down Expand Up @@ -1176,9 +1178,9 @@
"source": [
"# delete all data about the years 2005-2009 from the individual gas data\n",
"sel = {\"time\": slice(\"2005\", \"2009\")}\n",
"ds[\"CO2\"].loc[sel] = np.nan\n",
"ds[\"SF6\"].loc[sel] = np.nan\n",
"ds[\"CH4\"].loc[sel] = np.nan\n",
"ds[\"CO2\"].loc[sel].pint.magnitude[:] = np.nan\n",
"ds[\"SF6\"].loc[sel].pint.magnitude[:] = np.nan\n",
"ds[\"CH4\"].loc[sel].pint.magnitude[:] = np.nan\n",
"ds"
]
},
Expand Down
4 changes: 2 additions & 2 deletions primap2/tests/test_downscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

def test_downscale_gas_timeseries(empty_ds):
for key in empty_ds:
empty_ds[key][:] = np.nan
empty_ds[key].pint.magnitude[:] = np.nan
empty_ds["CO2"].loc[{"time": "2002"}] = 1 * ureg("Gg CO2 / year")
empty_ds["SF6"].loc[{"time": "2002"}] = 1 * ureg("Gg SF6 / year")
empty_ds["CH4"].loc[{"time": "2002"}] = 1 * ureg("Gg CH4 / year")
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_downscale_gas_timeseries(empty_ds):

def test_downscale_timeseries(empty_ds):
for key in empty_ds:
empty_ds[key][:] = np.nan
empty_ds[key].pint.magnitude[:] = np.nan
t = empty_ds.loc[{"area (ISO3)": "BOL"}].copy()
t["area (ISO3)"] = ["CAMB"] # here, the sum of COL, ARG, MEX, and BOL
ds = xr.concat([empty_ds, t], dim="area (ISO3)")
Expand Down
10 changes: 5 additions & 5 deletions primap2/tests/test_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_array_empty(empty_ds):

def test_array_coverage(empty_ds):
da = empty_ds["CO2"]
da[:] = np.nan
da.pint.magnitude[:] = np.nan
da.name = None

da.pr.loc[{"time": "2001", "area": "COL"}] = 12.0 * ureg("Gg CO2 / year")
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_array_coverage(empty_ds):
def test_array_coverage_multidim(opulent_ds):
da = opulent_ds["CO2"]

da.pr.loc[{"product": "milk"}] = np.nan
da.pr.loc[{"product": "milk"}].pint.magnitude[:] = np.nan

expected = pd.DataFrame(
index=da.pr["animal"].values,
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_array_coverage_error(opulent_ds):

def test_set_coverage(opulent_ds):
ds = opulent_ds
ds["CO2"].pr.loc[{"product": "milk"}] = np.nan
ds["CO2"].pr.loc[{"product": "milk"}].pint.magnitude[:] = np.nan

expected = pd.DataFrame(
index=ds.pr["product"].values,
Expand All @@ -135,7 +135,7 @@ def test_set_coverage(opulent_ds):

def test_set_coverage_entity(opulent_ds):
ds = opulent_ds
ds["CO2"].pr.loc[{"product": "milk"}] = np.nan
ds["CO2"].pr.loc[{"product": "milk"}].pint.magnitude[:] = np.nan

expected = pd.DataFrame(
index=list(ds.keys()),
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_set_coverage_boolean(opulent_ds):
def test_set_coverage_entity_other_dim_not_existing(opulent_ds):
ds = opulent_ds

ds["CO2"].pr.loc[{"product": "milk"}] = np.nan
ds["CO2"].pr.loc[{"product": "milk"}].pint.magnitude[:] = np.nan

entites_expected = [x for x in ds.keys() if x != "population"]

Expand Down
12 changes: 7 additions & 5 deletions primap2/tests/test_setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_exists_default_error(
def test_exists_empty_default(
self, da: xr.DataArray, ts: np.ndarray, co2: pint.Unit, new
):
da.loc[{"area (ISO3)": "COL"}] = np.nan
da.loc[{"area (ISO3)": "COL"}].pint.magnitude[:] = np.nan
actual = da.pr.set("area", "COL", ts * co2, **new)
expected = da
expected.loc[{"area (ISO3)": "COL"}] = ts[..., np.newaxis] * co2
Expand All @@ -88,7 +88,7 @@ def test_exists_empty_default(
def test_exists_somena_default(
self, da: xr.DataArray, ts: np.ndarray, co2: pint.Unit, new
):
da.loc[{"area (ISO3)": "COL"}] = np.nan
da.loc[{"area (ISO3)": "COL"}].pint.magnitude[:] = np.nan
da.loc[{"area (ISO3)": "COL", "time": "2001"}] = 2 * co2
with pytest.raises(
ValueError,
Expand Down Expand Up @@ -125,7 +125,7 @@ def test_exists_fillna(self, da: xr.DataArray, ts: np.ndarray, co2: pint.Unit, n
assert_aligned_equal(actual, expected)

def test_mixed_default(self, da: xr.DataArray, ts: np.ndarray, co2: pint.Unit):
da.loc[{"area (ISO3)": "COL"}] = np.nan
da.loc[{"area (ISO3)": "COL"}].pint.magnitude[:] = np.nan
actual = da.pr.set(
"area",
["COL", "CUB"],
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_mixed_from_data_array_overwrite(
"area", ["CUB", "COL"], 2 * da.pr.loc[{"area": "COL"}], existing="overwrite"
)
expected = da.reindex({"area (ISO3)": [*da["area (ISO3)"].values, "CUB"]})
expected.loc[{"area (ISO3)": "COL"}] = np.nan
expected.loc[{"area (ISO3)": "COL"}].pint.magnitude[:] = np.nan
expected = expected.fillna(da.loc[{"area (ISO3)": "COL"}] * 2)
assert_aligned_equal(actual, expected)

Expand Down Expand Up @@ -428,7 +428,9 @@ def test_existing_overwrite(self, minimal_ds: xr.Dataset, new):
assert_ds_aligned_equal(actual, expected)

def test_existing_fillna(self, minimal_ds: xr.Dataset, new):
minimal_ds["CO2"].pr.loc[{"area": "COL", "time": "2001"}] = np.nan
minimal_ds["CO2"].pr.loc[{"area": "COL", "time": "2001"}].pint.magnitude[
:
] = np.nan
actual = minimal_ds.pr.set(
"area", "COL", minimal_ds.pr.loc[{"area": "MEX"}], existing="fillna", **new
)
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ setup_requires =
setuptools_scm
install_requires =
xarray
pint>=0.20.1
pint_xarray>=0.2
pint>=0.23
pint_xarray>=0.3
numpy
pandas
openscm_units>=0.5.1
Expand Down

0 comments on commit 1c42e29

Please sign in to comment.