Repository: IAMconsortium/pyam
Pull request #702 (2022-09-14 05:40:29)

Migrate `compute_bias()` to `compute` module
# Please confirm that this PR has done the following:

- ~Tests Added~
- ~Documentation Added~
- ~Name of contributors Added to AUTHORS.rst~
- [x] Description in RELEASE_NOTES.md Added

# Description of PR

To remove clutter from the core module, this PR moves the method `compute_bias()` to `compute` module (with a deprecation-warning until the next release).


codecov[bot]: # [Codecov](https://codecov.io/gh/IAMconsortium/pyam/pull/702?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium) Report
> Merging [#702](https://codecov.io/gh/IAMconsortium/pyam/pull/702?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium) (65921e2) into [main](https://codecov.io/gh/IAMconsortium/pyam/commit/759120f01453f2738df6d4126186bb0a21cab54b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium) (759120f) will **decrease** coverage by `0.0%`.
> The diff coverage is `75.0%`.

```diff
@@           Coverage Diff           @@
##            main    #702     +/-   ##
=======================================
- Coverage   94.8%   94.8%   -0.1%     
=======================================
  Files         58      58             
  Lines       5856    5858      +2     
=======================================
  Hits        5557    5557             
- Misses       299     301      +2     
```


| [Impacted Files](https://codecov.io/gh/IAMconsortium/pyam/pull/702?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium) | Coverage Δ | |
|---|---|---|
| [pyam/units.py](https://codecov.io/gh/IAMconsortium/pyam/pull/702/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium#diff-cHlhbS91bml0cy5weQ==) | `96.1% <ø> (-0.1%)` | :arrow_down: |
| [pyam/core.py](https://codecov.io/gh/IAMconsortium/pyam/pull/702/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium#diff-cHlhbS9jb3JlLnB5) | `94.6% <33.3%> (-0.3%)` | :arrow_down: |
| [pyam/compute.py](https://codecov.io/gh/IAMconsortium/pyam/pull/702/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium#diff-cHlhbS9jb21wdXRlLnB5) | `100.0% <100.0%> (ø)` | |
| [tests/test\_feature\_debiasing.py](https://codecov.io/gh/IAMconsortium/pyam/pull/702/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium#diff-dGVzdHMvdGVzdF9mZWF0dXJlX2RlYmlhc2luZy5weQ==) | `100.0% <100.0%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=IAMconsortium)


Patch:
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 6a4de3c..2cf84af 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -7,6 +7,7 @@ dependency for better performance.
 
 ## Individual updates
 
+- [#702](https://github.com/IAMconsortium/pyam/pull/702) Migrate `compute_bias()` to `compute` module
 - [#701](https://github.com/IAMconsortium/pyam/pull/701) Add **xlsxwriter** as dependency to improve `to_excel()` performance
 - [#699](https://github.com/IAMconsortium/pyam/pull/699) Add filter options to IIASA API `index()`, `meta()` and `properties()` methods
 - [#697](https://github.com/IAMconsortium/pyam/pull/697) Add warning if IIASA API returns empty result
diff --git a/pyam/compute.py b/pyam/compute.py
index dee80d2..be7989d 100644
--- a/pyam/compute.py
+++ b/pyam/compute.py
@@ -2,6 +2,7 @@ import math
 import pandas as pd
 from pyam.index import replace_index_values
 from pyam.timeseries import growth_rate
+from pyam._debiasing import _compute_bias
 from pyam.utils import remove_from_list
 
 
@@ -113,6 +114,52 @@ class IamComputeAccessor:
 
         return self._df._finalize(value, append=append, variable=name, unit="")
 
+    def bias(self, name, method, axis):
+        """Compute the bias weights and add to 'meta'
+
+        Parameters
+        ----------
+        name : str
+           Column name in the 'meta' dataframe
+        method : str
+            Method to compute the bias weights, see the notes
+        axis : str
+            Index dimensions on which to apply the `method`
+
+        Notes
+        -----
+
+        The following methods are implemented:
+
+        - "count": use the inverse of the number of scenarios grouped by `axis` names.
+
+          Using the following method on an IamDataFrame with three scenarios
+
+          .. code-block:: python
+
+              df.compute.bias(name="bias-weight", method="count", axis="scenario")
+
+          results in the following column to be added to *df.meta*:
+
+          .. list-table::
+             :header-rows: 1
+
+             * - model
+               - scenario
+               - bias-weight
+             * - model_a
+               - scen_a
+               - 0.5
+             * - model_a
+               - scen_b
+               - 1
+             * - model_b
+               - scen_a
+               - 0.5
+
+        """
+        _compute_bias(self._df, name, method, axis)
+
 
 def _compute_learning_rate(x, performance, experience):
     """Internal implementation for computing implicit learning rate from timeseries data
diff --git a/pyam/core.py b/pyam/core.py
index bd03f08..3299668 100755
--- a/pyam/core.py
+++ b/pyam/core.py
@@ -77,8 +77,7 @@ from pyam.index import (
     replace_index_values,
 )
 from pyam.time import swap_time_for_year, swap_year_for_time
-from pyam._debiasing import _compute_bias
-from pyam.logging import raise_data_error
+from pyam.logging import raise_data_error, deprecation_warning
 
 logger = logging.getLogger(__name__)
 
@@ -1012,50 +1011,10 @@ class IamDataFrame(object):
             return df.reset_index()
 
     def compute_bias(self, name, method, axis):
-        """Compute the bias weights and add to 'meta'
-
-        Parameters
-        ----------
-        name : str
-           Column name in the 'meta' dataframe
-        method : str
-            Method to compute the bias weights, see the notes
-        axis : str
-            Index dimensions on which to apply the `method`
-
-        Notes
-        -----
-
-        The following methods are implemented:
-
-        - "count": use the inverse of the number of scenarios grouped by `axis` names.
-
-          Using the following method on an IamDataFrame with three scenarios
-
-          .. code-block:: python
-
-              df.compute_bias(name="bias-weight", method="count", axis="scenario")
-
-          results in the following column to be added to *df.meta*:
-
-          .. list-table::
-             :header-rows: 1
-
-             * - model
-               - scenario
-               - bias-weight
-             * - model_a
-               - scen_a
-               - 0.5
-             * - model_a
-               - scen_b
-               - 1
-             * - model_b
-               - scen_a
-               - 0.5
-
-        """
-        _compute_bias(self, name, method, axis)
+        """DEPRECATED - please use :meth:`IamDataFrame.compute.bias()`"""
+        # TODO: deprecated, remove for release >= 1.7
+        deprecation_warning("Use `df.compute.bias()` instead.")
+        self.compute.bias(name, method, axis)
 
     def rename(
         self, mapping=None, inplace=False, append=False, check_duplicates=True, **kwargs
diff --git a/pyam/units.py b/pyam/units.py
index 3e5ac2d..18a5a1e 100644
--- a/pyam/units.py
+++ b/pyam/units.py
@@ -3,8 +3,6 @@ import re
 
 import iam_units
 import pint
-
-from pyam.logging import deprecation_warning
 from pyam.index import replace_index_values
 
 logger = logging.getLogger(__name__)


Test patch:
diff --git a/tests/test_feature_debiasing.py b/tests/test_feature_debiasing.py
index 590eac9..9f045da 100644
--- a/tests/test_feature_debiasing.py
+++ b/tests/test_feature_debiasing.py
@@ -13,7 +13,7 @@ def test_debiasing_count(test_pd_df, axis, exp):
     # modify the default test data to have three distinct scenarios
     test_pd_df.loc[1, "model"] = "model_b"
     df = IamDataFrame(test_pd_df)
-    df.compute_bias(method="count", name="bias", axis=axis)
+    df.compute.bias(method="count", name="bias", axis=axis)
 
     assert_array_equal(df["bias"].values, exp)
 
@@ -22,4 +22,4 @@ def test_debiasing_unknown_method(test_df_year):
     """Check computing bias weights counting the number of scenarios by scenario name"""
     msg = "Unknown method foo for computing bias weights!"
     with pytest.raises(ValueError, match=msg):
-        test_df_year.compute_bias(method="foo", name="bias", axis="scenario")
+        test_df_year.compute.bias(method="foo", name="bias", axis="scenario")