-
-
Notifications
You must be signed in to change notification settings - Fork 588
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
Using bibtexparser instead of pybtex for bibtex. #3740
Conversation
Signed-off-by: Pradyot Ranjan <[email protected]>
Signed-off-by: Pradyot Ranjan <[email protected]>
…tation-parser # Conflicts: # pybamm/citations.py
Signed-off-by: Pradyot Ranjan <[email protected]>
Note: the |
sciunto-org/python-bibtexparser#433 |
Signed-off-by: Pradyot Ranjan <[email protected]>
@agriyakhetarpal I don't understand the failing test case here : https://github.com/pybamm-team/PyBaMM/actions/runs/7627904153/job/20779262646?pr=3740#step:11:2390:~:text=AssertionError%3A%20ModuleNotFoundError%20not%20raised This is the only failing test case. Can you help me with this? |
bibtexparser = sys.modules["bibtexparser"] | ||
sys.modules["bibtexparser"] = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did have to think a bit about this because it wasn't really prominent in the first read. We are using the assertRaisesRegex
context manager clause to test the ModuleNotFoundError
message, and pybamm.print_citations
used to call the have_optional_dependency
utility function for pybtex
, which was removed – which means that the function is not called and subsequently an error was not raised.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is the case indeed. If you feel this should be changed to some other stable optional dependency then it can also be looked into.
Signed-off-by: Pradyot Ranjan <[email protected]>
Signed-off-by: Pradyot Ranjan <[email protected]>
I've added changes. Now only thing there is is changing output format. I might raise a PR in |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #3740 +/- ##
========================================
Coverage 99.60% 99.60%
========================================
Files 259 259
Lines 21270 21279 +9
========================================
+ Hits 21186 21195 +9
Misses 84 84 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @prady0t, changes looks nice.
Some comments below -
pyproject.toml
Outdated
@@ -84,7 +84,8 @@ plot = [ | |||
] | |||
# For the Citations class | |||
cite = [ | |||
"pybtex>=0.24.0", | |||
"pybtex>=0.24.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be removed now
return | ||
except PybtexError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does bibtexparser
also have some attribute corresponding to PybtexError
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it doesn't. The only thing close to exception handling in bibtexparser is this.
pybamm/citations.py
Outdated
def _string_formatting(self, entry): | ||
bibtexparser = have_optional_dependency("bibtexparser") | ||
if not isinstance(entry, bibtexparser.model.Entry): | ||
raise TypeError() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage is failing as this is not covered by tests, would be nice to add some test case covering this part too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, and a helpful error message would be nice as well – a TypeError
in itself is not descriptive enough
Signed-off-by: Pradyot Ranjan <[email protected]>
Why is this test case failing? https://github.com/pybamm-team/PyBaMM/actions/runs/7700868778/job/20989315835?pr=3740 |
Unrelated error, I triggered a re-run which should fix it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @prady0t – I haven't looked at the change in functionality in detail as of now, but this PR should – in addition, perform the following changes:
- Replace
pybtex
withbibtexparser
inasv.conf.json
and in the optional dependencies section/table of the installation guide - Replace instances of
pybtex
inCONTRIBUTING.md
and comments innoxfile.py
- Remove any remaining comments referencing
pybtex
inpybamm/citations.py
Also, this is the output from pybamm.print_citations()
, currently:
{Array programming with NumPy} Harris, Charles R. and Millman, K. Jarrod and van der Walt, St{\'{e}}fan J. and Gommers, Ralf and Virtanen, Pauli and Cournapeau, David and Wieser, Eric and Taylor, Julian and Berg, Sebastian and Smith, Nathaniel J. and others Nature 585 7825 357--362 2020 Nature Publishing Group 10.1038/s41586-020-2649-2
{Python Battery Mathematical Modelling (PyBaMM)} Sulzer, Valentin and Marquis, Scott G. and Timms, Robert and Robinson, Martin and Chapman, S. Jon 10.5334/jors.309 Journal of Open Research Software Software Sustainability Institute 9 1 14 2021
which doesn't suggest that the string parsing is working very well at this moment – I know it is unsupported functionality in bibtexparser
, but we can't compromise on losing proper formatting (missing commas and periods, unneeded curly brackets, missing diacritics) – it is essential for the NumPy and PyBaMM papers, and no less for any others.
Another solution is to use something like diffrax
's autocitation functionality that I had suggested before and hardcode citation strings inside __init__
methods for model and solver classes, which would mean losing out on plaintext functionality, but brings us towards having clearer outputs in standard BibTeX – which is something I prefer. @brosaplanella and @kratman, what do you think about this?
@@ -178,10 +177,10 @@ def _tag_citations(self): | |||
for key, entry in self._citation_tags.items(): | |||
print(f"{key} was cited due to the use of {entry}") | |||
|
|||
def print(self, filename=None, output_format="text", verbose=False): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should remove this option currently. Is there a way to convert to BibTeX, and if there isn't, is it possible that such a way be devised?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we can convert it into bibtex.
if output_format == "text": | ||
citations = pybtex.format_from_strings( | ||
self._cited, style="plain", output_backend="plaintext" | ||
) | ||
elif output_format == "bibtex": | ||
citations = "\n".join(self._cited) | ||
else: | ||
raise pybamm.OptionError( | ||
f"Output format {output_format} not recognised." | ||
"It should be 'text' or 'bibtex'." | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not be removed unless it is necessary to do so since this counts as a breaking change. I don't mind it a lot though since this is not a commonly used feature
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing that we can do now is having an additional option like filtered_output
along with bibtex
. This option can print the essential hardcoded values (inside init) which might lead to data loss but we can provide much cleaner output if that's what user is more concerned about. Additionally we can also have a option raw
which outputs the parsed bibtex as is (which is out only current output).
With this we can solve both problems. Not having data loss and also providing cleaner output.
f"Citations could not be registered. If you are on Google Colab - " | ||
"pybtex does not work with Google Colab due to a known bug - " | ||
"https://bitbucket.org/pybtex-devs/pybtex/issues/148/. " | ||
f"Citations could not be registered." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if we can test this – pybtex
is quite inactive and did not work on Google Colab, but bibtexparser
might work well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try this out.
@agriyakhetarpal How should we move forward with this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixing some conflicts raised with #3866
Co-authored-by: Agriya Khetarpal <[email protected]>
Tagging @Saransh-cpp @kratman since they were involved in the discussions where we discussed replacing P.S. to let go of the nox.options.default_venv_backend = "virtualenv" i.e., just the one line in |
Hmm, using We could maybe create a feature request in |
Nice!
Yes, @prady0t has created a feature request upstream earlier: sciunto-org/python-bibtexparser#433 So, yes – we can keep using Thanks for contributing and trying this out, @prady0t – sorry for the mess that this causes 🙂 Let's hope we can have |
Or, you could close this PR without deleting the branch so that we have some working |
Glad to see progress here. I'll close this PR here and open a new one with suggested changes. |
Do I have to replace : Line 8 in bc31cac
with
|
We should keep both |
Fixes #3648
Still in progress, Opened a PR so that it's easier to communicate changes and progress.