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.parameters #277

Merged
merged 36 commits into from
Sep 13, 2024

Conversation

TimothyWillard
Copy link
Contributor

This PR:

  • Documents the gempyor.parameters module and its only class Parameters,
  • Adds unit tests for (almost) all of the methods of the Parameters class,
  • Regulates the exports of gempyor.parameters using the __all__ dunder`.

This PR does not:

  • Change any of the functionality contained in this module,
  • Refactor any of the code in this module for readability/performance,
  • Correct the type hint issues with this module (namely list/dict types not being fully specified),
  • Test the parameters_reduce method of the Parameters class.

This is the first in one or two more PRs to address GH-276. I didn't implement any tests for parameters_reduce yet because I still need to get a handle on the NPI objects. However, I think it would be best to get this first pass done to set ground for the next pass.

First draft of documentation for the `gempyor.parameters` module, most
importantly containing the `Parameters` class. Also added `__all__`
dunder to regulate exports and converted TODO comments to GitHub issues,
see GH-274, GH-275.
Wrote, documented, and unit tested `create_confuse_subview_from_dict`
and `create_confuse_rootview_from_dict` functions to easily create
confuse package objects from dicts for use in parametrized unit tests.
Initialized the unit tests for the `gempyor.parameters.Parameters` class
with fixtures for the exceptions raised by the class' constructor. Left
a note about the inability to reach one of the `ValueError`s, will have
to revisit, either with a way to reach that exception or refactor the
dead code.
Wrote a unit test fixture for correctly instantiating the `Parameters`
class as well as checking its documented attributes. Fixed typo in
attributes documentation.
Added `partials_are_similar` to `gempyor.testing` for testing if
`functools.partial` objects are similar enough for unit testing purposes
along with corresponding documentation and unit tests.
Switch from manually testing partials to using `partials_are_similar` in
the `gempyor.parameters.Paramters` unit tests.
Added test fixtures for the `picklable_lamda_alpha/sigma` methods of
`gempyor.parameters.Parameters`. Also added some light inline comments
for the unit tests.
Added a test fixture for the `get_pnames2pindex` method of
`gempyor.parameters.Parameters`.
Added a test fixture for `parameters_quick_draw` including coverage for
the shape issues with time series parameters. Added a note to the
documentation for `Parameters.parameters_quick_draw` about time series
parameters.
Added a test fixture for `Parameters.parameters_load` as well as add
some to the fixture for `parameters_quick_draw`. Light documentation
updates to the corresponding method to clarify conforming sizes with
time series parameters.
Added a test fixutre for the `getParametersDF` method of
`gempyor.parameters.Parameters`.
Did not implement a test fixture for the `parameters_reduce` method of
`gempyor.parameters.Parameters` for now. Left a note explaining the
blocker of getting a handle on the `NPI` module.
@TimothyWillard TimothyWillard linked an issue Aug 2, 2024 that may be closed by this pull request
@TimothyWillard TimothyWillard added documentation Relating to ReadMEs / gitbook / vignettes / etc. gempyor Concerns the Python core. labels Aug 2, 2024
Copy link
Contributor

@pearsonca pearsonca left a comment

Choose a reason for hiding this comment

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

Seems like a reasonable start. The tests need some DRYing and elimination of magic constants.

Seems worthwhile to tail off a few issues for refactoring as well - e.g. extracting sub-parser methods / object definitions for timeseries and distros; pruning no-longer-used methods

corresponds to the `npar` attribute of this class.

Note:
If any of the parameters are 'timeseries' type parameters then `n_days` and
Copy link
Contributor

Choose a reason for hiding this comment

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

must they? doesn't look like any errors raised if not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you get an exception raised by numpy about shapes not being compatible on this line:

param_arr[idx] = self.pdata[pn]["ts"].values

I suppose you could provide a single date time series and it would be fine since numpy could make those shapes compatible (although, the constructor doesn't allow missing dates for time series params). I decided to place this info under the Notes (I need to fix typo) section instead of the Raises since this method doesn't raise the exception itself.

flepimop/gempyor_pkg/src/gempyor/parameters.py Outdated Show resolved Hide resolved
Parameterized
`test_timeseries_parameter_has_insufficient_dates_value_error` test
fixture by moving custom DataFrame to shared `MockData` class.
Export the `partials_are_similar` function from the `gempyor.testing`
module using the `__all__` dunder.
Added a function with the same core logic as
`gempyor.utils.as_random_distribution` that is decoupled from
the `confuse.ConfigView` class along with corresponding documentation
and tests.
Decoupled the `test_parameters_instance_attributes` test fixture from a
specific set of inputs and generalized it to accept arbitrary factories
that generate valid input sets for `Parameters`.
Parametrized the `test_picklable_lamda_alpha`,
`test_picklable_lamda_sigma`, and `test_get_pnames2pindex` test fixtures
to accept arbitrary valid inputs.
@TimothyWillard
Copy link
Contributor Author

No need to review yet, still WIP per the suggestions above.

Added a function to `gempyor.testing` to determine if a given value is
supported by a given distribution and its paramters. It does not test if
said sample is plausible or not.
Parametrized the test fixture for the `Parameters.parameters_quick_draw`
method. Also developed a generic `MockParametersInput` class that can be
used to easily facilitate the parameterization of
`gempyor.parameters.Parameters` unit tests.
Copy link
Contributor

@pearsonca pearsonca left a comment

Choose a reason for hiding this comment

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

Going to separately have a close look at test_parameters, but other comments in gathered now.

Combined `create_confuse_rootview_from_dict` and
`create_confuse_subview_from_dict` into one
`create_confuse_configview_from_dict` function in `gempyor.testing`.
Updated unit tests for `gempyor.parameters.Parameters` to use the new
`create_confuse_configview_from_dict` helper instead of the two prior
versions of this function.
jcblemai
jcblemai previously approved these changes Sep 11, 2024
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.

This is incredibly helpful and well structured, thanks. Good to merge.

@TimothyWillard
Copy link
Contributor Author

@saraloo, @emprzy, or @pearsonca is there any chance we could get this review and merged soon?

emprzy
emprzy previously approved these changes Sep 11, 2024
saraloo
saraloo previously approved these changes Sep 11, 2024
Resolved conflicts in the `gempyor.utils` module related to the `typing`
module imports.
Added a brief description to the example for the
`create_confuse_configview_from_dict` function that also shows the
corresponding yaml that is represented by the example.
Copy link
Contributor

@pearsonca pearsonca left a comment

Choose a reason for hiding this comment

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

Works for me; one meta question, which if it needs addressing can be done in separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

@TimothyWillard checking my understanding here: this file effectively establishes a module within gempyor, corresponding to testing support functions, yes?

if so, what are the benefits of making those functions part of the public interface of gempyor? i'm familiar with the downsides, and am more accustomed to seeing this sort of thing as internal to the test suite, not exported. But I can imagine possible benefits?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that understanding is correct.

The unit tests are defined outside of the package so they need to be able to import these functions from gempyor. I could set __all__ = [] anyways to exclude these from the "public interface", there are ways to easily get around the __all__ restrictions. Other options would be either 1) moving these functions into an appropriate place in the tests folder or 2) moving the tests into gempyor.

Copy link
Contributor

Choose a reason for hiding this comment

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

got it - this should be a next-issue-problem and seems like the right answer is 2).

if we're thinking of gempyor as its own little capability / package, it should have its own internal tests.

flepimop/gempyor_pkg/src/gempyor/testing.py Show resolved Hide resolved
Copy link
Collaborator

@emprzy emprzy left a comment

Choose a reason for hiding this comment

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

Looks good

@TimothyWillard TimothyWillard merged commit 40eb2ae into main Sep 13, 2024
1 check passed
@TimothyWillard TimothyWillard deleted the unit-test-gempyor-parameters branch September 13, 2024 12:04
jcblemai pushed a commit that referenced this pull request Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Relating to ReadMEs / gitbook / vignettes / etc. gempyor Concerns the Python core.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document, Test, Repair gempyor.parameters Module
5 participants