Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document/Unit Test gempyor.statistics #304

Merged
merged 24 commits into from
Oct 18, 2024

Conversation

TimothyWillard
Copy link
Contributor

This PR:

  • Documents the gempyor.statistics module and its only class Statistic,
  • Adds unit tests for all of the methods of the Statistic class,
  • Regulates the exports of gempyor.statistics using the __all__ dunder, and
  • Correct incorrect type annotations.

This PR does not:

  • Change the functionality contained in this module or refactor the code contained (beyond style).

In the process of this PR come across some issues that I've noted on the main issue, GH-300.

This PR derives from the branch in GH-277 because I needed the confuse related testing utilities, so it would be much easier to review this PR after reviewing/merging GH-277.

* Wrote draft documentation for the `statistics` module in Google style
  guide.
* Added missing type annotations and corrected already existing ones.
* Applied black formatter to the file, including manually correcting
  some line-length issues.
* Rearranged dunder methods.
…docs

Merging the 'unit-test-gempyor-parameters' branch into
'GH-300/statistics-unit-tests-docs' to get the confuse related testing
utilities.
* Created initial unit testing infrastructure for the `Statistic` class
  from `gempyor.statistics`, starting with invalid regularization name
  value error.
* Added default to `getattr` call to make unsupported regularization
  value error reachable. Should obsolete with better documentation.
* Added a test fixture to test the attributes of the `Statistic` class.
* Removed unnecessary `tmp_path` pytest fixture dependency.
* Improved documentation on the `Statistic` class' attributes and added
  a raises section for the constructor.
Added a test fixture for the result of calling `str` and `repr` on an
instance of the `Statistic` class.
The return of `Statistic.llik` is actually an xarray DataArray instead
of a float, but summed along the date dimension.
Added the initial unit tests for the regularization methods,
`_forecast_regularize` and `_allsubpop_regularize`, of the `Statistic`
class. The tests are general and do not make claims about correctness
for now.
Added unit tests for the `apply_resample` method, including creating a
new factory, `simple_valid_resample_factory`, which hits the "resample
config present" of the code path.
Added unit tests for `apply_scale` method including a new factory that
produces an input set with a 'scale' config. Fixed a bug where the scale
function was not applied even if provided. This is a *breaking* change,
but doesn't affect currently existing test suite, need to see if this
affects any currently existing config files.
Added unit tests for the `apply_transforms` method of `Statistic`,
including making a new factory that includes both resampling and scaling
configuration.
Created global `all_valid_factories` that can be passed directly to the
`pytest.mark.parametrize` decorator to test methods of the `Statistic`
class against many configurations.
Added unit tests for the `llik` method of the `Statistic` class. Had to
change factories to use RMSE by default for likelihood distribution
since the poisson distribution only has integer support.
Was previously using `xarray.DataArray` for `model_data` and `gt_data`
in unit testing the `Statistic` class since that is what many methods
expect. It seems though the main entry to the class, `compute_logloss`
takes an `xarray.DataSet` that the class splices into
`xarray.DataArray`s. The unit tests now more accurately reflect this.
* Created initial unit tests on the `compute_logloss` method of
  `Statistic`, checking for structure but not correctness.
* Updated documentation for `compute_logloss` to reflect the possible
  `ValueError` and the correct input types expected.
* Changed internal variable of that method to a float to get a
  consistent float return for the second tuple entry from
  `compute_logloss`.
Added a test fixture that confirms the `ValueError` raised when model
data and ground truth data do not have the same shapes in
`Statistic.compute_logloss`.
There were entries in the mock configs, modeled on existing configs,
that are not considered by the `Statistic` class at all. Removed for
clarity.
emprzy
emprzy previously approved these changes Aug 23, 2024
@TimothyWillard TimothyWillard dismissed emprzy’s stale review September 13, 2024 12:09

The merge-base changed after approval.

@TimothyWillard
Copy link
Contributor Author

I think this got lost at some point after GH-277 was merged in, but this should be ready to review. I will address these issues from GH-300 in a follow-up PR after this one (want to establish a baseline of testing for the class first):

I'm not certain what this means, maybe the Statistic.llik method is supposed to make guarantees about the subpopulation order in the outputted data?

        likelihood = xr.DataArray(likelihood, coords=gt_data.coords, dims=gt_data.dims)

        # TODO: check the order of the arguments
        return likelihood

Yes, that's on the date. Not necessarily adding a check but at least making sure the code is correct

and

s RMSE used in practice for statistics? Would there be consequences for fixing this bug?

Very good find for the bug, the python inference has been used a few time on production already, I think only with poisson llik for now. But this is important and a priority.

@TimothyWillard TimothyWillard marked this pull request as ready for review October 10, 2024 19:58
@TimothyWillard TimothyWillard requested a review from emprzy October 10, 2024 19:58
Copy link
Collaborator

@jcblemai jcblemai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All look good to me as a new baseline. The bug you've found for the llik is fixed in emcee_batch (which contains also the most recent "statistics" code) in case.

@TimothyWillard
Copy link
Contributor Author

The bug you've found for the llik is fixed in emcee_batch

Would you mind referencing that commit on the main issue so that fact doesn't get lost? Thanks!

@jcblemai
Copy link
Collaborator

The bug you've found for the llik is fixed in emcee_batch

Would you mind referencing that commit on the main issue so that fact doesn't get lost? Thanks!

isn't it already cause it contains the issue number ? I'll add a comment in case

@TimothyWillard
Copy link
Contributor Author

isn't it already cause it contains the issue number ? I'll add a comment in case

Ah, you're right, I missed that.

@TimothyWillard TimothyWillard added bug Defects or errors in the code. enhancement Request for improvement or addition of new feature(s). gempyor Concerns the Python core. docstring Relating to in-code documentation. labels Oct 15, 2024
@TimothyWillard TimothyWillard merged commit 2af7da5 into main Oct 18, 2024
1 check passed
@TimothyWillard TimothyWillard deleted the GH-300/statistics-unit-tests-docs branch October 18, 2024 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Defects or errors in the code. docstring Relating to in-code documentation. enhancement Request for improvement or addition of new feature(s). gempyor Concerns the Python core.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: gempyor.statistics Missing Documentation/Unit Tests
4 participants