From fb6075a30b2d26a369e0e8f64b0a78313eccb66c Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:27:48 +0530 Subject: [PATCH 01/13] Using bibtexparser instead of pybtex Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- pybamm/citations.py | 128 +++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 85 deletions(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index ca260c5cfd..c61f8ba6bf 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -1,29 +1,19 @@ -# -# Bibliographical information for PyBaMM -# Inspired by firedrake/PETSc citation workflow -# https://firedrakeproject.org/citing.html -# import pybamm -import os +from pybamm.util import have_optional_dependency import warnings +import os from sys import _getframe -from pybamm.util import have_optional_dependency class Citations: - - """Entry point to citations management. + """ + Entry point to citations management. This object may be used to record BibTeX citation information and then register that a particular citation is relevant for a particular simulation. Citations listed in `pybamm/CITATIONS.bib` can be registered with their citation key. For all other works provide a BibTeX Citation to :meth:`register`. - Examples - -------- - >>> pybamm.citations.register("Sulzer2021") - >>> pybamm.citations.register("@misc{Newton1687, title={Mathematical...}}") - >>> pybamm.print_citations("citations.txt") """ def __init__(self): @@ -73,29 +63,32 @@ def read_citations(self): """Reads the citations in `pybamm.CITATIONS.bib`. Other works can be cited by passing a BibTeX citation to :meth:`register`. """ - parse_file = have_optional_dependency("pybtex.database", "parse_file") - citations_file = os.path.join(pybamm.root_dir(), "pybamm", "CITATIONS.bib") - bib_data = parse_file(citations_file, bib_format="bibtex") - for key, entry in bib_data.entries.items(): - self._add_citation(key, entry) + citations_file = citations_file = os.path.join(pybamm.root_dir(), "pybamm", "CITATIONS.bib") + parse_file = have_optional_dependency("bibtexparser", "parse_file") + bib_data = parse_file(citations_file) + entries = bib_data.entries + for entry in entries: + # print(entry.key) + self._add_citation(entry.key, entry) def _add_citation(self, key, entry): """Adds `entry` to `self._all_citations` under `key`, warning the user if a previous entry is overwritten """ - Entry = have_optional_dependency("pybtex.database", "Entry") # Check input types are correct + Entry = have_optional_dependency("bibtexparser.model", "Entry") if not isinstance(key, str) or not isinstance(entry, Entry): raise TypeError() # Warn if overwriting a previous citation - new_citation = entry.to_string("bibtex") + new_citation = entry if key in self._all_citations and new_citation != self._all_citations[key]: warnings.warn(f"Replacing citation for {key}") # Add to database self._all_citations[key] = new_citation + # print(new_citation) def _add_citation_tag(self, key, entry): """Adds a tag for a citation key in the dict, which represents the name of the @@ -150,23 +143,22 @@ def _parse_citation(self, key): key: str A BibTeX formatted citation """ - PybtexError = have_optional_dependency("pybtex.scanner", "PybtexError") - parse_string = have_optional_dependency("pybtex.database", "parse_string") + + # Parse string as a bibtex citation, and check that a citation was found try: - # Parse string as a bibtex citation, and check that a citation was found - bib_data = parse_string(key, bib_format="bibtex") + parse_string = have_optional_dependency("bibtexparser", "parse_string") + bib_data = parse_string(key) if not bib_data.entries: - raise PybtexError("no entries found") + print("no entries found") # Add and register all citations - for key, entry in bib_data.entries.items(): + for entry in bib_data.entries: # Add to _all_citations dictionary - self._add_citation(key, entry) + self._add_citation(entry.key, entry) # Add to _papers_to_cite set - self._papers_to_cite.add(key) + self._papers_to_cite.add(entry.key) return - except PybtexError: - # Unable to parse / unknown key + except: raise KeyError(f"Not a bibtex citation or known citation: {key}") def _tag_citations(self): @@ -178,47 +170,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): - """Print all citations that were used for running simulations. The verbose - option is provided to print tags for citations in the output such that it can - can be seen where the citations were registered due to the use of PyBaMM models - and solvers in the code. - - .. note:: - If a citation is registered manually, it will not be tagged. - - .. warning:: - This function will notify the user if a citation that has been previously - registered is invalid or cannot be parsed. - - Parameters - ---------- - filename : str, optional - Filename to which to print citations. If None, citations are printed - to the terminal. - verbose: bool, optional - If True, prints the citation tags for the citations that have been - registered. An example of the output is shown below. + def print(self, filename=None, verbose=False): - Examples - -------- - .. code-block:: python - - pybamm.lithium_ion.SPM() - pybamm.Citations.print(verbose=True) or pybamm.print_citations(verbose=True) - - will append the following at the end of the list of citations: - - .. code-block:: - - Citations registered: - - Marquis2019 was cited due to the use of SPM - - """ # Parse citations that were not known keys at registration, but do not # fail if they cannot be parsed - pybtex = have_optional_dependency("pybtex") try: for key in self._unknown_citations: self._parse_citation(key) @@ -230,28 +185,31 @@ def print(self, filename=None, output_format="text", verbose=False): # delete the invalid citation from the set self._unknown_citations.remove(key) - 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'." - ) + citations = self._cited if filename is None: - print(citations) + # print(citations) + for entry in citations: + self._string_formatting(entry) if verbose: self._tag_citations() # pragma: no cover else: with open(filename, "w") as f: f.write(citations) + def _string_formatting(self, entry): + txt_format = ' ' + for key, value in entry.items(): + if key != "ID" and key != "ENTRYTYPE": + txt_format = txt_format + " " + str(value) + print(f" {txt_format} \n") + + @property + def citation_err_msg(self): + return self._citation_err_msg + -def print_citations(filename=None, output_format="text", verbose=False): +def print_citations(filename=None, verbose=False): """See :meth:`Citations.print`""" if citations._citation_err_msg is not None: raise ImportError( @@ -260,7 +218,7 @@ def print_citations(filename=None, output_format="text", verbose=False): "https://bitbucket.org/pybtex-devs/pybtex/issues/148/. " "Please manually cite all the references." "\nError encountered -\n" - f"{citations._citation_err_msg}" + f"{citations.citation_err_msg}" ) else: if verbose: # pragma: no cover @@ -269,9 +227,9 @@ def print_citations(filename=None, output_format="text", verbose=False): "Verbose output is available only for the terminal and not for printing to files", ) else: - citations.print(filename, output_format, verbose=True) + citations.print(filename, verbose=True) else: - pybamm.citations.print(filename, output_format) + citations.print(filename) citations = Citations() From 9e3d7ed128c59123adb830b4ca1fdaa16d275f4f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 17 Jan 2024 09:02:51 +0000 Subject: [PATCH 02/13] style: pre-commit fixes --- pybamm/citations.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index c61f8ba6bf..2f93801cca 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -63,7 +63,9 @@ def read_citations(self): """Reads the citations in `pybamm.CITATIONS.bib`. Other works can be cited by passing a BibTeX citation to :meth:`register`. """ - citations_file = citations_file = os.path.join(pybamm.root_dir(), "pybamm", "CITATIONS.bib") + citations_file = citations_file = os.path.join( + pybamm.root_dir(), "pybamm", "CITATIONS.bib" + ) parse_file = have_optional_dependency("bibtexparser", "parse_file") bib_data = parse_file(citations_file) entries = bib_data.entries @@ -171,7 +173,6 @@ def _tag_citations(self): print(f"{key} was cited due to the use of {entry}") def print(self, filename=None, verbose=False): - # Parse citations that were not known keys at registration, but do not # fail if they cannot be parsed try: @@ -198,7 +199,7 @@ def print(self, filename=None, verbose=False): f.write(citations) def _string_formatting(self, entry): - txt_format = ' ' + txt_format = " " for key, value in entry.items(): if key != "ID" and key != "ENTRYTYPE": txt_format = txt_format + " " + str(value) From 210bfc770fe5d12aef8c716dc8683ba36e7b5380 Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:36:14 +0530 Subject: [PATCH 03/13] Adding back wrongly removed docstring Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- pybamm/citations.py | 50 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index c61f8ba6bf..c3c5fe21e5 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -1,3 +1,8 @@ +# +# Bibliographical information for PyBaMM +# Inspired by firedrake/PETSc citation workflow +# https://firedrakeproject.org/citing.html +# import pybamm from pybamm.util import have_optional_dependency import warnings @@ -6,14 +11,18 @@ class Citations: - """ - Entry point to citations management. + """Entry point to citations management. This object may be used to record BibTeX citation information and then register that a particular citation is relevant for a particular simulation. Citations listed in `pybamm/CITATIONS.bib` can be registered with their citation key. For all other works provide a BibTeX Citation to :meth:`register`. + Examples + -------- + >>> pybamm.citations.register("Sulzer2021") + >>> pybamm.citations.register("@misc{Newton1687, title={Mathematical...}}") + >>> pybamm.print_citations("citations.txt") """ def __init__(self): @@ -171,6 +180,43 @@ def _tag_citations(self): print(f"{key} was cited due to the use of {entry}") def print(self, filename=None, verbose=False): + """Print all citations that were used for running simulations. The verbose + option is provided to print tags for citations in the output such that it can + can be seen where the citations were registered due to the use of PyBaMM models + and solvers in the code. + + .. note:: + If a citation is registered manually, it will not be tagged. + + .. warning:: + This function will notify the user if a citation that has been previously + registered is invalid or cannot be parsed. + + Parameters + ---------- + filename : str, optional + Filename to which to print citations. If None, citations are printed + to the terminal. + verbose: bool, optional + If True, prints the citation tags for the citations that have been + registered. An example of the output is shown below. + + Examples + -------- + .. code-block:: python + + pybamm.lithium_ion.SPM() + pybamm.Citations.print(verbose=True) or pybamm.print_citations(verbose=True) + + will append the following at the end of the list of citations: + + .. code-block:: + + Citations registered: + + Marquis2019 was cited due to the use of SPM + + """ # Parse citations that were not known keys at registration, but do not # fail if they cannot be parsed From b67b9c29d079e8dfebba2277ee84a912e83ad60e Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Wed, 17 Jan 2024 14:42:28 +0530 Subject: [PATCH 04/13] Adding back wrongly removed docstring Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- pybamm/citations.py | 54 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index 2f93801cca..56f9339329 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -1,3 +1,8 @@ +# +# Bibliographical information for PyBaMM +# Inspired by firedrake/PETSc citation workflow +# https://firedrakeproject.org/citing.html +# import pybamm from pybamm.util import have_optional_dependency import warnings @@ -6,14 +11,18 @@ class Citations: - """ - Entry point to citations management. + """Entry point to citations management. This object may be used to record BibTeX citation information and then register that a particular citation is relevant for a particular simulation. Citations listed in `pybamm/CITATIONS.bib` can be registered with their citation key. For all other works provide a BibTeX Citation to :meth:`register`. + Examples + -------- + >>> pybamm.citations.register("Sulzer2021") + >>> pybamm.citations.register("@misc{Newton1687, title={Mathematical...}}") + >>> pybamm.print_citations("citations.txt") """ def __init__(self): @@ -70,7 +79,6 @@ def read_citations(self): bib_data = parse_file(citations_file) entries = bib_data.entries for entry in entries: - # print(entry.key) self._add_citation(entry.key, entry) def _add_citation(self, key, entry): @@ -90,7 +98,6 @@ def _add_citation(self, key, entry): # Add to database self._all_citations[key] = new_citation - # print(new_citation) def _add_citation_tag(self, key, entry): """Adds a tag for a citation key in the dict, which represents the name of the @@ -173,8 +180,43 @@ def _tag_citations(self): print(f"{key} was cited due to the use of {entry}") def print(self, filename=None, verbose=False): - # Parse citations that were not known keys at registration, but do not - # fail if they cannot be parsed + """Print all citations that were used for running simulations. The verbose + option is provided to print tags for citations in the output such that it can + can be seen where the citations were registered due to the use of PyBaMM models + and solvers in the code. + + .. note:: + If a citation is registered manually, it will not be tagged. + + .. warning:: + This function will notify the user if a citation that has been previously + registered is invalid or cannot be parsed. + + Parameters + ---------- + filename : str, optional + Filename to which to print citations. If None, citations are printed + to the terminal. + verbose: bool, optional + If True, prints the citation tags for the citations that have been + registered. An example of the output is shown below. + + Examples + -------- + .. code-block:: python + + pybamm.lithium_ion.SPM() + pybamm.Citations.print(verbose=True) or pybamm.print_citations(verbose=True) + + will append the following at the end of the list of citations: + + .. code-block:: + + Citations registered: + + Marquis2019 was cited due to the use of SPM + + """ try: for key in self._unknown_citations: self._parse_citation(key) From 07306e3d8b44a7df3238687d501d359a49d26382 Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Tue, 23 Jan 2024 20:44:10 +0530 Subject: [PATCH 05/13] PassCing test cases Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- pybamm/citations.py | 22 ++++++++++------------ pyproject.toml | 3 ++- tests/unit/test_citations.py | 17 ++++++----------- tests/unit/test_util.py | 10 +++++----- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index 56f9339329..5a42369f5e 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -9,7 +9,6 @@ import os from sys import _getframe - class Citations: """Entry point to citations management. This object may be used to record BibTeX citation information and then register that @@ -72,7 +71,7 @@ def read_citations(self): """Reads the citations in `pybamm.CITATIONS.bib`. Other works can be cited by passing a BibTeX citation to :meth:`register`. """ - citations_file = citations_file = os.path.join( + citations_file = os.path.join( pybamm.root_dir(), "pybamm", "CITATIONS.bib" ) parse_file = have_optional_dependency("bibtexparser", "parse_file") @@ -158,7 +157,7 @@ def _parse_citation(self, key): parse_string = have_optional_dependency("bibtexparser", "parse_string") bib_data = parse_string(key) if not bib_data.entries: - print("no entries found") + raise Exception("no entries found") # Add and register all citations for entry in bib_data.entries: @@ -167,7 +166,7 @@ def _parse_citation(self, key): # Add to _papers_to_cite set self._papers_to_cite.add(entry.key) return - except: + except Exception: raise KeyError(f"Not a bibtex citation or known citation: {key}") def _tag_citations(self): @@ -182,7 +181,7 @@ def _tag_citations(self): def print(self, filename=None, verbose=False): """Print all citations that were used for running simulations. The verbose option is provided to print tags for citations in the output such that it can - can be seen where the citations were registered due to the use of PyBaMM models + be seen where the citations were registered due to the use of PyBaMM models and solvers in the code. .. note:: @@ -231,21 +230,22 @@ def print(self, filename=None, verbose=False): citations = self._cited if filename is None: - # print(citations) for entry in citations: - self._string_formatting(entry) + print(self._string_formatting(entry)) if verbose: self._tag_citations() # pragma: no cover else: with open(filename, "w") as f: - f.write(citations) + for entry in citations: + f.write(self._string_formatting(entry)) + def _string_formatting(self, entry): txt_format = " " for key, value in entry.items(): if key != "ID" and key != "ENTRYTYPE": txt_format = txt_format + " " + str(value) - print(f" {txt_format} \n") + return f" {txt_format} \n" @property def citation_err_msg(self): @@ -256,9 +256,7 @@ def print_citations(filename=None, verbose=False): """See :meth:`Citations.print`""" if citations._citation_err_msg is not None: raise ImportError( - 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." "Please manually cite all the references." "\nError encountered -\n" f"{citations.citation_err_msg}" diff --git a/pyproject.toml b/pyproject.toml index 69fb9bfc1e..c39114700f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -80,7 +80,8 @@ plot = [ ] # For the Citations class cite = [ - "pybtex>=0.24.0", + "pybtex>=0.24.0", + "bibtexparser>=2.0.0b5", ] # To generate LaTeX strings latexify = [ diff --git a/tests/unit/test_citations.py b/tests/unit/test_citations.py index d8c1de3718..12508581eb 100644 --- a/tests/unit/test_citations.py +++ b/tests/unit/test_citations.py @@ -7,7 +7,7 @@ import unittest import contextlib import warnings -from pybtex.database import Entry +from bibtexparser.model import Entry from tempfile import NamedTemporaryFile @@ -47,15 +47,10 @@ def test_citations(self): def test_print_citations(self): pybamm.citations._reset() - # Text Style - with temporary_filename() as filename: - pybamm.print_citations(filename, "text") - with open(filename) as f: - self.assertTrue(len(f.readlines()) > 0) # Bibtext Style with temporary_filename() as filename: - pybamm.print_citations(filename, "bibtex") + pybamm.print_citations(filename) with open(filename) as f: self.assertTrue(len(f.readlines()) > 0) @@ -64,11 +59,11 @@ def test_print_citations(self): with contextlib.redirect_stdout(f): pybamm.print_citations() self.assertTrue( - "Python Battery Mathematical Modelling (PyBaMM)." in f.getvalue() + " {Python Battery Mathematical Modelling (PyBaMM)}" in f.getvalue() ) - with self.assertRaisesRegex(pybamm.OptionError, "'text' or 'bibtex'"): - pybamm.print_citations("test_citations.txt", "bad format") + # with self.assertRaisesRegex(pybamm.OptionError, "'text' or 'bibtex'"): + # pybamm.print_citations("test_citations.txt", "bad format") pybamm.citations._citation_err_msg = "Error" with self.assertRaisesRegex(ImportError, "Error"): @@ -111,7 +106,7 @@ def test_input_validation(self): """Test type validation of ``_add_citation``""" pybamm.citations.register(1) - with self.assertRaises(TypeError): + with self.assertRaises(KeyError): pybamm.citations._parse_citation(1) with self.assertRaises(TypeError): diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py index 730e4cc08d..d4839e95b3 100644 --- a/tests/unit/test_util.py +++ b/tests/unit/test_util.py @@ -92,9 +92,9 @@ def test_git_commit_info(self): self.assertEqual(git_commit_info[:2], "v2") def test_have_optional_dependency(self): - with self.assertRaisesRegex(ModuleNotFoundError, "Optional dependency pybtex is not available."): - pybtex = sys.modules['pybtex'] - sys.modules['pybtex'] = None + with self.assertRaisesRegex(ModuleNotFoundError, "Optional dependency bibtexparser is not available."): + bibtexparser = sys.modules['bibtexparser'] + sys.modules['bibtexparser'] = None pybamm.print_citations() with self.assertRaisesRegex(ModuleNotFoundError, "Optional dependency anytree is not available."): with TemporaryDirectory() as dir_name: @@ -106,8 +106,8 @@ def test_have_optional_dependency(self): sym = pybamm.div(c * pybamm.grad(c)) + (c / d + c - d) ** 5 sym.visualise(test_name) - sys.modules['pybtex'] = pybtex - pybamm.util.have_optional_dependency("pybtex") + sys.modules['bibtexparser'] = bibtexparser + pybamm.util.have_optional_dependency("bibtexparser") pybamm.print_citations() From 4c3ed062a174b235ba4876d82d83521439e304e4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 23 Jan 2024 15:18:55 +0000 Subject: [PATCH 06/13] style: pre-commit fixes --- pybamm/citations.py | 6 ++---- tests/unit/test_citations.py | 1 - tests/unit/test_util.py | 10 ++++++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index 5a42369f5e..c926449076 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -9,6 +9,7 @@ import os from sys import _getframe + class Citations: """Entry point to citations management. This object may be used to record BibTeX citation information and then register that @@ -71,9 +72,7 @@ def read_citations(self): """Reads the citations in `pybamm.CITATIONS.bib`. Other works can be cited by passing a BibTeX citation to :meth:`register`. """ - citations_file = os.path.join( - pybamm.root_dir(), "pybamm", "CITATIONS.bib" - ) + citations_file = os.path.join(pybamm.root_dir(), "pybamm", "CITATIONS.bib") parse_file = have_optional_dependency("bibtexparser", "parse_file") bib_data = parse_file(citations_file) entries = bib_data.entries @@ -239,7 +238,6 @@ def print(self, filename=None, verbose=False): for entry in citations: f.write(self._string_formatting(entry)) - def _string_formatting(self, entry): txt_format = " " for key, value in entry.items(): diff --git a/tests/unit/test_citations.py b/tests/unit/test_citations.py index 12508581eb..7d38b600c5 100644 --- a/tests/unit/test_citations.py +++ b/tests/unit/test_citations.py @@ -47,7 +47,6 @@ def test_citations(self): def test_print_citations(self): pybamm.citations._reset() - # Bibtext Style with temporary_filename() as filename: pybamm.print_citations(filename) diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py index 713f0af86d..4d26939ad2 100644 --- a/tests/unit/test_util.py +++ b/tests/unit/test_util.py @@ -93,9 +93,11 @@ def test_git_commit_info(self): self.assertEqual(git_commit_info[:2], "v2") def test_have_optional_dependency(self): - with self.assertRaisesRegex(ModuleNotFoundError, "Optional dependency bibtexparser is not available."): - bibtexparser = sys.modules['bibtexparser'] - sys.modules['bibtexparser'] = None + with self.assertRaisesRegex( + ModuleNotFoundError, "Optional dependency bibtexparser is not available." + ): + bibtexparser = sys.modules["bibtexparser"] + sys.modules["bibtexparser"] = None pybamm.print_citations() with self.assertRaisesRegex( ModuleNotFoundError, "Optional dependency anytree is not available." @@ -109,7 +111,7 @@ def test_have_optional_dependency(self): sym = pybamm.div(c * pybamm.grad(c)) + (c / d + c - d) ** 5 sym.visualise(test_name) - sys.modules['bibtexparser'] = bibtexparser + sys.modules["bibtexparser"] = bibtexparser pybamm.util.have_optional_dependency("bibtexparser") pybamm.print_citations() From 5aa706b6dbe590804db89738cb2ef47b0283d835 Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Wed, 24 Jan 2024 10:14:59 +0530 Subject: [PATCH 07/13] Checking for bbtexparser optional dependency Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- pybamm/citations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pybamm/citations.py b/pybamm/citations.py index 5a42369f5e..9d21b58193 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -216,6 +216,7 @@ def print(self, filename=None, verbose=False): Marquis2019 was cited due to the use of SPM """ + bibtexparser = have_optional_dependency("bibtexparser") try: for key in self._unknown_citations: self._parse_citation(key) From d25cde0179f6a1648547e3fc78ca5ebfd7589648 Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Wed, 24 Jan 2024 14:41:50 +0530 Subject: [PATCH 08/13] Fixing pre-commit Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- pybamm/citations.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index daf121a89d..17b35d4a5f 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -215,7 +215,6 @@ def print(self, filename=None, verbose=False): Marquis2019 was cited due to the use of SPM """ - bibtexparser = have_optional_dependency("bibtexparser") try: for key in self._unknown_citations: self._parse_citation(key) @@ -240,6 +239,9 @@ def print(self, filename=None, verbose=False): f.write(self._string_formatting(entry)) def _string_formatting(self, entry): + bibtexparser = have_optional_dependency("bibtexparser") + if not isinstance(entry, bibtexparser.model.Entry): + raise TypeError() txt_format = " " for key, value in entry.items(): if key != "ID" and key != "ENTRYTYPE": From fe5dc3b957967c239ce007e854094d87d405c75b Mon Sep 17 00:00:00 2001 From: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> Date: Tue, 30 Jan 2024 00:06:41 +0530 Subject: [PATCH 09/13] Better error handeling Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com> --- pybamm/citations.py | 2 +- pyproject.toml | 1 - tests/unit/test_citations.py | 5 +++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index 17b35d4a5f..d86a69121a 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -241,7 +241,7 @@ def print(self, filename=None, verbose=False): def _string_formatting(self, entry): bibtexparser = have_optional_dependency("bibtexparser") if not isinstance(entry, bibtexparser.model.Entry): - raise TypeError() + raise TypeError("Input for string formatting must be an instance of bibtexparser.model.Entry") txt_format = " " for key, value in entry.items(): if key != "ID" and key != "ENTRYTYPE": diff --git a/pyproject.toml b/pyproject.toml index 4be291d670..316b305ddb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -84,7 +84,6 @@ plot = [ ] # For the Citations class cite = [ - "pybtex>=0.24.0", "bibtexparser>=2.0.0b5", ] # To generate LaTeX strings diff --git a/tests/unit/test_citations.py b/tests/unit/test_citations.py index 7d38b600c5..5bb5a5a425 100644 --- a/tests/unit/test_citations.py +++ b/tests/unit/test_citations.py @@ -114,6 +114,11 @@ def test_input_validation(self): with self.assertRaises(TypeError): pybamm.citations._add_citation(1001, Entry("misc")) + def test_string_formatting(self): + """Test type validation of ``_string_formatting``""" + with self.assertRaisesRegex(TypeError, "Input for string formatting"): + pybamm.citations._string_formatting("NotAEntry") + def test_andersson_2019(self): citations = pybamm.citations citations._reset() From f8dce396aa97d99014c954b0588cd7865afa4212 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Jan 2024 18:37:37 +0000 Subject: [PATCH 10/13] style: pre-commit fixes --- pybamm/citations.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index d86a69121a..7792eab942 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -241,7 +241,9 @@ def print(self, filename=None, verbose=False): def _string_formatting(self, entry): bibtexparser = have_optional_dependency("bibtexparser") if not isinstance(entry, bibtexparser.model.Entry): - raise TypeError("Input for string formatting must be an instance of bibtexparser.model.Entry") + raise TypeError( + "Input for string formatting must be an instance of bibtexparser.model.Entry" + ) txt_format = " " for key, value in entry.items(): if key != "ID" and key != "ENTRYTYPE": From a71ce1da1f8ee38475bb59614904d324d9f1d037 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 7 Mar 2024 05:58:38 +0000 Subject: [PATCH 11/13] style: pre-commit fixes --- pybamm/citations.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index a62c428c69..690ba5577e 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -7,7 +7,6 @@ import warnings import os from sys import _getframe -from pybamm.util import import_optional_dependency class Citations: From 98121fcc0c855f3f27f71d67af42100be2efd5af Mon Sep 17 00:00:00 2001 From: Arjun Verma Date: Thu, 7 Mar 2024 11:36:09 +0530 Subject: [PATCH 12/13] Apply suggestions from code review Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com> --- pybamm/citations.py | 8 ++++---- tests/unit/test_citations.py | 2 +- tests/unit/test_util.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pybamm/citations.py b/pybamm/citations.py index 690ba5577e..8512f0f62a 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -73,7 +73,7 @@ def read_citations(self): by passing a BibTeX citation to :meth:`register`. """ citations_file = os.path.join(pybamm.root_dir(), "pybamm", "CITATIONS.bib") - parse_file = have_optional_dependency("bibtexparser", "parse_file") + parse_file = import_optional_dependency("bibtexparser", "parse_file") bib_data = parse_file(citations_file) entries = bib_data.entries for entry in entries: @@ -85,7 +85,7 @@ def _add_citation(self, key, entry): """ # Check input types are correct - Entry = have_optional_dependency("bibtexparser.model", "Entry") + Entry = import_optional_dependency("bibtexparser.model", "Entry") if not isinstance(key, str) or not isinstance(entry, Entry): raise TypeError() @@ -153,7 +153,7 @@ def _parse_citation(self, key): # Parse string as a bibtex citation, and check that a citation was found try: - parse_string = have_optional_dependency("bibtexparser", "parse_string") + parse_string = import_optional_dependency("bibtexparser", "parse_string") bib_data = parse_string(key) if not bib_data.entries: raise Exception("no entries found") @@ -239,7 +239,7 @@ def print(self, filename=None, verbose=False): f.write(self._string_formatting(entry)) def _string_formatting(self, entry): - bibtexparser = have_optional_dependency("bibtexparser") + bibtexparser = import_optional_dependency("bibtexparser") if not isinstance(entry, bibtexparser.model.Entry): raise TypeError( "Input for string formatting must be an instance of bibtexparser.model.Entry" diff --git a/tests/unit/test_citations.py b/tests/unit/test_citations.py index 5bb5a5a425..f18377681e 100644 --- a/tests/unit/test_citations.py +++ b/tests/unit/test_citations.py @@ -117,7 +117,7 @@ def test_input_validation(self): def test_string_formatting(self): """Test type validation of ``_string_formatting``""" with self.assertRaisesRegex(TypeError, "Input for string formatting"): - pybamm.citations._string_formatting("NotAEntry") + pybamm.citations._string_formatting("NotAnEntry") def test_andersson_2019(self): citations = pybamm.citations diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py index f2c9deebac..5864e13416 100644 --- a/tests/unit/test_util.py +++ b/tests/unit/test_util.py @@ -119,7 +119,7 @@ def test_import_optional_dependency(self): sym.visualise(test_name) sys.modules["bibtexparser"] = bibtexparser - pybamm.util.have_optional_dependency("bibtexparser") + pybamm.util.import_optional_dependency("bibtexparser") pybamm.print_citations() From c43805db4c5cfc4d82dd33c54e36b2f8d87317c1 Mon Sep 17 00:00:00 2001 From: Arjun Verma Date: Thu, 7 Mar 2024 11:38:01 +0530 Subject: [PATCH 13/13] Update pybamm/citations.py --- pybamm/citations.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pybamm/citations.py b/pybamm/citations.py index 8512f0f62a..cf7da3aad3 100644 --- a/pybamm/citations.py +++ b/pybamm/citations.py @@ -7,6 +7,7 @@ import warnings import os from sys import _getframe +from pybamm.util import import_optional_dependency class Citations: