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

Add validator to check whether anonymous and reduced formulae are reduced #914

Merged
merged 2 commits into from Aug 30, 2021

Conversation

ml-evs
Copy link
Member

@ml-evs ml-evs commented Aug 27, 2021

Closes #913.

@codecov
Copy link

codecov bot commented Aug 27, 2021

Codecov Report

Merging #914 (63f8875) into master (7d25322) will increase coverage by 0.02%.
The diff coverage is 100.00%.

❗ Current head 63f8875 differs from pull request most recent head 3195c5b. Consider uploading reports for the commit 3195c5b to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #914      +/-   ##
==========================================
+ Coverage   92.77%   92.80%   +0.02%     
==========================================
  Files          67       67              
  Lines        3765     3780      +15     
==========================================
+ Hits         3493     3508      +15     
  Misses        272      272              
Flag Coverage Δ
project 92.80% <100.00%> (+0.02%) ⬆️
validator 92.80% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
optimade/models/structures.py 96.19% <100.00%> (+0.21%) ⬆️
optimade/validator/__init__.py 100.00% <100.00%> (ø)
optimade/validator/validator.py 82.07% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7d25322...3195c5b. Read the comment docs.

@ml-evs ml-evs force-pushed the ml-evs/extra_chemform_validation branch from 63f8875 to 64cfd88 Compare August 27, 2021 14:17
@ml-evs ml-evs marked this pull request as ready for review August 27, 2021 14:19
Comment on lines 912 to 913
# Can replace with just `math.gcd(numbers)` in Python 3.9+
_gcd = reduce(gcd, numbers)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe make a special import that takes care of this then and you can import so the functionality becomes gcd?

Does it make sense? Aka. did I make sense?

Copy link
Member Author

Choose a reason for hiding this comment

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

You make sense, but I'm not sure it is any less confusing? If I read _gcd (or gcd_) I understand it as avoiding a nameclash in some sense. I think _gcd = reduce(gcd, numbers) is clearer than from math import gcd as _gcd; gcd = reduce(_gcd, numbers).

Copy link
Member Author

Choose a reason for hiding this comment

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

Unless you mean a versioned Python import such that it calls math.gcd directly for 3.9 and reduce(gcd, numbers) for <3.9... cba with that really.

Copy link
Member

@CasperWA CasperWA Aug 30, 2021

Choose a reason for hiding this comment

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

Unless you mean a versioned Python import such that it calls math.gcd directly for 3.9 and reduce(gcd, numbers) for <3.9... cba with that really.

This ! :) (So I didn't make sense - I just got lucky that you eventually thought of the same thing 😅)

Copy link
Member Author

Choose a reason for hiding this comment

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

Reworked it slightly to address both my interpretations of your question...

Now for >=3.9 it calls math.gcd(numbers) directly and the variable is now just gcd, not gcd_.

optimade/models/structures.py Outdated Show resolved Hide resolved
optimade/validator/__init__.py Show resolved Hide resolved
@CasperWA
Copy link
Member

Oh yeah, wanted to add a comment that it might be nice to use some other variable names than v and n (although, I think the first one is used throughout, but I think value should work as well at least - then we can change it more throughout the code later).

@ml-evs
Copy link
Member Author

ml-evs commented Aug 27, 2021

Oh yeah, wanted to add a comment that it might be nice to use some other variable names than v and n (although, I think the first one is used throughout, but I think value should work as well at least - then we can change it more throughout the code later).

For some reason I had mentally added v to the list of variable names enforced by pydantic validators (alongside field, values etc.), but this is not the case. I can change it to value here. Do you really dislike n that much as a loop variable?

@ml-evs ml-evs force-pushed the ml-evs/extra_chemform_validation branch from 64cfd88 to ad1870b Compare August 27, 2021 18:28
@ml-evs ml-evs force-pushed the ml-evs/extra_chemform_validation branch from ad1870b to 14027c2 Compare August 29, 2021 17:33
@ml-evs
Copy link
Member Author

ml-evs commented Aug 29, 2021

This should be good to go @CasperWA and @JPBergsma.

There's now some duplication between validators (i.e. we parse out numbers and elements) for various fields, but there are some subtleties that make this way clearer than doing it all in one go.

JPBergsma
JPBergsma previously approved these changes Aug 29, 2021
Copy link
Contributor

@JPBergsma JPBergsma left a comment

Choose a reason for hiding this comment

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

Everything looks good to me.
I still made a minor point about line 903 in structures.py, but this is mostly me playing code golf, and it is not important.

optimade/models/structures.py Show resolved Hide resolved
@CasperWA
Copy link
Member

For some reason I had mentally added v to the list of variable names enforced by pydantic validators (alongside field, values etc.), but this is not the case. I can change it to value here.

🎉

Do you really dislike n that much as a loop variable?

I'm a sucker for pylint's variable naming - what can I say?
It could be reduced_number or something descriptive? It's nice with descriptive variable names - but it's not a deal breaker; just a pet peeve.

Copy link
Member

@CasperWA CasperWA left a comment

Choose a reason for hiding this comment

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

Two total comments on my side (this plus the old one) - after that this is good to go for me.

optimade/validator/__init__.py Outdated Show resolved Hide resolved
@ml-evs
Copy link
Member Author

ml-evs commented Aug 30, 2021

I'm a sucker for pylint's variable naming - what can I say?

Huh, my pylint doesn't complain about loop variables...

It could be reduced_number or something descriptive? It's nice with descriptive variable names - but it's not a deal breaker; just a pet peeve.

I think we might be talking about outdated changes or something now, unless you want to replace _gcd with reduced_number?

@ml-evs ml-evs force-pushed the ml-evs/extra_chemform_validation branch from b8cb8f9 to 9c19e9c Compare August 30, 2021 10:40
…uced

- Also explicitly check for 0 chemical proportion in formulae
- Add --version flag for the validator
- Do 'minimal' tests first, as this is where actual model deserialization occurs
@ml-evs ml-evs force-pushed the ml-evs/extra_chemform_validation branch from 9c19e9c to 3195c5b Compare August 30, 2021 10:44
@CasperWA
Copy link
Member

I'm a sucker for pylint's variable naming - what can I say?

Huh, my pylint doesn't complain about loop variables...

Ah - it might have changed then.

I think we might be talking about outdated changes or something now, unless you want to replace _gcd with reduced_number?

Nah, it was just a suggestion for what to name n - but no matter.

@ml-evs ml-evs merged commit 4adbaa6 into master Aug 30, 2021
@ml-evs ml-evs deleted the ml-evs/extra_chemform_validation branch August 30, 2021 11:05
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.

Add validation that anonymous/reduced chemical formulae are in fact reduced
3 participants