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

Unify and clean code style and docstring #68

Merged
merged 35 commits into from
Aug 1, 2023
Merged

Conversation

dachengx
Copy link
Collaborator

@dachengx dachengx commented Jul 30, 2023

Every time we update the attributes of modules, like adding a module runner, we should run alea/docs/make_docs.sh in alea/docs folder to update alea/docs/reference.

But the docstring of template_source.py is not updated. It will be fixed in #69.

Reference:

Indentation: https://legacy.python.org/dev/peps/pep-0008/#indentation
Docstring format: https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html

This PR tries to apply hanging indentation as much as possible.

You can check the built docs at https://readthedocs.org/projects/alea/builds/ or https://alea--68.org.readthedocs.build/en/68/.

@github-actions
Copy link

github-actions bot commented Jul 30, 2023

Pull Request Test Coverage Report for Build 5717911329

  • 23 of 29 (79.31%) changed or added relevant lines in 5 files are covered.
  • 7 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+1.9%) to 63.193%

Changes Missing Coverage Covered Lines Changed/Added Lines %
alea/simulators.py 10 12 83.33%
alea/model.py 6 10 60.0%
Files with Coverage Reduction New Missed Lines %
alea/models/blueice_extended_model.py 1 94.04%
alea/utils.py 1 78.57%
alea/parameters.py 2 81.25%
alea/model.py 3 59.87%
Totals Coverage Status
Change from base Build 5696617155: 1.9%
Covered Lines: 570
Relevant Lines: 902

💛 - Coveralls

alea/model.py Outdated Show resolved Hide resolved
alea/model.py Outdated Show resolved Hide resolved
alea/model.py Show resolved Hide resolved
alea/simulators.py Outdated Show resolved Hide resolved
alea/simulators.py Outdated Show resolved Hide resolved
alea/simulators.py Outdated Show resolved Hide resolved
alea/simulators.py Outdated Show resolved Hide resolved
alea/models/blueice_extended_model.py Show resolved Hide resolved
alea/simulators.py Outdated Show resolved Hide resolved
alea/simulators.py Show resolved Hide resolved
alea/simulators.py Show resolved Hide resolved
alea/simulators.py Show resolved Hide resolved
alea/model.py Outdated Show resolved Hide resolved
alea/model.py Outdated Show resolved Hide resolved
@dachengx dachengx force-pushed the code_docstring_style branch from 7355bd4 to 2e4c06c Compare July 31, 2023 00:42
@dachengx dachengx marked this pull request as ready for review July 31, 2023 00:47
@dachengx dachengx requested review from hammannr and kdund July 31, 2023 06:11
@hammannr
Copy link
Collaborator

hammannr commented Jul 31, 2023

I just had a very brief look. Is there a particular benefit of choosing restructured text docstring? As far as I know, sphinx also supports Google fand NumPy-type docstrings. I generally find reStr text terrible to read and to maintain and if there is no clear point in favor of it I’d much prefer to go to one of the alternative standards.

@dachengx
Copy link
Collaborator Author

I just had a very brief look. Is there a particular benefit of choosing restructured text docstring? As far as I know, sphinx also supports Google fand NumPy-type docstrings. I generally find reStr text terrible to read and to maintain and if there is no clear point in favor of it I’d much prefer to go to one of the alternative standards.

OK, I will go to Google style docstring.

alea/model.py Outdated Show resolved Hide resolved
alea/model.py Outdated Show resolved Hide resolved
alea/model.py Outdated Show resolved Hide resolved
alea/model.py Outdated Show resolved Hide resolved
alea/model.py Show resolved Hide resolved
alea/model.py Show resolved Hide resolved
alea/model.py Show resolved Hide resolved
alea/model.py Outdated Show resolved Hide resolved
alea/model.py Outdated Show resolved Hide resolved
alea/model.py Outdated Show resolved Hide resolved
@dachengx dachengx requested a review from hammannr July 31, 2023 17:34
alea/model.py Show resolved Hide resolved
alea/model.py Show resolved Hide resolved
alea/model.py Show resolved Hide resolved
alea/model.py Show resolved Hide resolved
alea/model.py Show resolved Hide resolved
alea/simulators.py Show resolved Hide resolved
alea/examples/gaussian_model.py Show resolved Hide resolved
alea/examples/gaussian_model.py Show resolved Hide resolved
alea/examples/gaussian_model.py Show resolved Hide resolved
alea/examples/gaussian_model.py Show resolved Hide resolved
alea/parameters.py Show resolved Hide resolved
alea/parameters.py Show resolved Hide resolved
@hammannr
Copy link
Collaborator

hammannr commented Aug 1, 2023

They are here: https://alea--68.org.readthedocs.build/en/68/py-modindex.html @hammannr

Ah very nice! So do we need to make the linking to the things in the sidebar manually? I think this would be very useful!

Copy link
Collaborator

@hammannr hammannr left a comment

Choose a reason for hiding this comment

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

Two minor things and some of the previous discussions are still open but apart from that it looks great -- very nice to have such clean and structured docstrings! 🚀

alea/model.py Outdated Show resolved Hide resolved
alea/model.py Outdated Show resolved Hide resolved
@dachengx
Copy link
Collaborator Author

dachengx commented Aug 1, 2023

They are here: https://alea--68.org.readthedocs.build/en/68/py-modindex.html @hammannr

Ah very nice! So do we need to make the linking to the things in the sidebar manually? I think this would be very useful!

Maybe later. This PR only makes docstring. :)

@dachengx dachengx requested a review from hammannr August 1, 2023 08:46
Copy link
Collaborator

@hammannr hammannr left a comment

Choose a reason for hiding this comment

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

Looks great to me now -- thank you very much for this contribution @dachengx ! 😊

mu (float, optional (default=None)): mean of the gaussian,
if None, the nominal value is used
sigma (float, optional (default=None)): standard deviation of the gaussian,
if None, the nominal value is used
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we warn that no nominal values are set here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

But if you properly call the ll via .ll instead of ._ll it should parse the defaults or not

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not entirely sure but I think it's possible to run into a useless error if you don't define the defaults and then mu and sigma stay None and are parsed to stats.norm.. Maybe it would make sense to catch this case and provide a more useful error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We do catch it-- but it is nice to point out this stumbling block in the documentation, in particular since this is a tutorial model, that fails if you use it naively

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@kdund
Copy link
Collaborator

kdund commented Aug 1, 2023 via email

@hammannr
Copy link
Collaborator

hammannr commented Aug 1, 2023

Ah, right @kdund we catch it in the Parameters class. Then I think everything is fine or not? Probably I'm just missing your point

Copy link
Collaborator

@kdund kdund left a comment

Choose a reason for hiding this comment

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

Hi! Few minor changes-- I think we should warn that the gaussian model must have nominal values set manually, or fitting will not work (i.e. write it in the docstring) and we should label the likelihood likelihood not logpdf in the example placeholder

alea/model.py Outdated
raise NotImplementedError(
"You must write a data-generation method (_generate_data) for your statistical model"
" or use a subclass where it is written for you")

@_needs_data
def ll(self, **kwargs) -> float:
"""
Likelihod function, returns the loglikelihood for the given parameters.
Likelihod function, return the loglikelihood for the given parameters.
Copy link
Collaborator

Choose a reason for hiding this comment

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

returns is correct

alea/model.py Outdated
Make a function that can be passed to Minuit

Args:
minus (bool, optional (default=True)): if True, the function is multiplied by -1
Copy link
Collaborator

Choose a reason for hiding this comment

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

why have minus here if we never use the option?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do you mean that we drop this minus and always use True? Maybe @hammannr can comment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, good point -- let's drop it. It's a remnant from blueice, we don't need this flexibility here I think.

alea/model.py Outdated
"""
Generate data for the given parameters.
The parameters are passed as keyword arguments, positional arguments are not possible.
If a parameter is not given, the default value is used.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I would keep singular to emphasize that it is the default value singular for that parameter (i.e. not for all parameters)

mu (float, optional (default=None)): mean of the gaussian,
if None, the nominal value is used
sigma (float, optional (default=None)): standard deviation of the gaussian,
if None, the nominal value is used
Copy link
Collaborator

Choose a reason for hiding this comment

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

We do catch it-- but it is nice to point out this stumbling block in the documentation, in particular since this is a tutorial model, that fails if you use it naively

r_data = np.zeros(np.sum(n_sources), dtype=self.dtype)
i_write = 0
for i, n_source in enumerate(n_sources):
if 0 < n_source: #dont generate if 0
if n_source > 0: # dont generate if 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the wrong way, no? Always better to go smaller->larger

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

PEP8's WPS309 prefer putting variable first: #68 (comment)

Copy link
Collaborator

@kdund kdund Aug 1, 2023

Choose a reason for hiding this comment

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

In that case, I want us to ignore WPS309 errors.
To me this is a significant readability issue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see your reasoning Knut but I have to say I'm so used to it that writing 0 < n_sources takes me a few seconds to understand 😄 Though I think it's a small detail and I can live with either solution.

@@ -42,8 +41,7 @@ def adapt_likelihood_config_for_blueice(

Args:
likelihood_config (dict): likelihood config dict
template_folder_list (list): list of possible base folders.
Ordered by priority.
template_folder_list (list): list of possible base folders. Ordered by priority.
Copy link
Collaborator

Choose a reason for hiding this comment

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

which order? Try first one first or last one first?

Copy link
Collaborator

Choose a reason for hiding this comment

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

While "logPDF" is correct in principle here, I would label it likelihood instead since it is used as a function of mu, rather than the observation.

Copy link
Collaborator

Choose a reason for hiding this comment

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

actually, so it is not correct in principle, but in practise :)

@dachengx dachengx requested a review from kdund August 1, 2023 16:15
@dachengx dachengx merged commit 58bf303 into main Aug 1, 2023
@dachengx dachengx deleted the code_docstring_style branch August 1, 2023 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants