|
| 1 | +"""How to cite ArviZ and its methods.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | + |
| 6 | + |
| 7 | +def citations(methods=None, filepath=None, format_type="bibtex"): |
| 8 | + """ |
| 9 | + List citations for ArviZ and the methods implemented in ArviZ. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + methods : List |
| 14 | + Methods implemented in ArviZ from which to retrieve citations. |
| 15 | + filepath : str, optional |
| 16 | + Specifies the location to save the file with the citations. |
| 17 | + If ``None``, the result is returned as a string. |
| 18 | + format_type : str |
| 19 | + Specifies in which format the references will be displayed. |
| 20 | + Currently, only "bibtex" is supported. |
| 21 | +
|
| 22 | + Examples |
| 23 | + -------- |
| 24 | + >>> from arviz_base import citations |
| 25 | + >>> from arviz_stats import rhat |
| 26 | + >>> citations(methods=[rhat]) # Returns how to cite ArviZ and rhat |
| 27 | + >>> citations() # Returns how to cite ArviZ |
| 28 | + """ |
| 29 | + method_citations = [{"doi": "10.21105/joss.XXXXX"}] |
| 30 | + if methods is not None: |
| 31 | + for method in methods: |
| 32 | + _extract_ids_per_entry(method_citations, method.__doc__) |
| 33 | + |
| 34 | + if format_type == "bibtex": |
| 35 | + header = _get_header(methods) |
| 36 | + citation_text = _find_bibtex_entries(header, method_citations) |
| 37 | + if filepath: |
| 38 | + with open(filepath, "w") as fw: |
| 39 | + fw.write(citation_text) |
| 40 | + else: |
| 41 | + return citation_text |
| 42 | + else: |
| 43 | + raise ValueError("Invalid value for format_type. Use 'bibtex'.") |
| 44 | + |
| 45 | + |
| 46 | +def _extract_ids_per_entry(data, text): |
| 47 | + entries = re.split(r"\n\s*\.\. \[\d+\] ", text.strip()) |
| 48 | + |
| 49 | + doi_pattern = re.compile(r"https?://doi\.org/(\S+)", re.IGNORECASE) |
| 50 | + url_pattern = re.compile(r"https?://(?!doi\.org)(\S+)", re.IGNORECASE) |
| 51 | + |
| 52 | + for entry in entries: |
| 53 | + doi_match = doi_pattern.search(entry) |
| 54 | + if doi_match: |
| 55 | + doi = doi_match.group(1).rstrip(".") |
| 56 | + data.append({"doi": doi}) |
| 57 | + else: |
| 58 | + urls = [url.rstrip(".") for url in url_pattern.findall(entry)] |
| 59 | + if urls: |
| 60 | + data.append({"urls": urls}) |
| 61 | + return data |
| 62 | + |
| 63 | + |
| 64 | +def _find_bibtex_entries(header, data): |
| 65 | + ref_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "references.bib") |
| 66 | + with open(ref_path, encoding="utf-8") as fr: |
| 67 | + bibtex_text = fr.read() |
| 68 | + |
| 69 | + entries = re.split(r"\n(?=@)", bibtex_text) |
| 70 | + |
| 71 | + doi_field_pattern = re.compile(r'doi\s*=\s*[{"]([^}"]+)[}"]', re.IGNORECASE) |
| 72 | + url_field_pattern = re.compile(r'url\s*=\s*[{"]([^}"]+)[}"]', re.IGNORECASE) |
| 73 | + |
| 74 | + references = [header] |
| 75 | + for identifier in data: |
| 76 | + found_entry = "" |
| 77 | + for entry in entries: |
| 78 | + bib_dois = doi_field_pattern.findall(entry) |
| 79 | + bib_urls = url_field_pattern.findall(entry) |
| 80 | + |
| 81 | + if "doi" in identifier and any(identifier["doi"] in doi for doi in bib_dois): |
| 82 | + found_entry = entry.strip() |
| 83 | + break |
| 84 | + |
| 85 | + if "urls" in identifier and any( |
| 86 | + any(url in b_url or b_url in url for b_url in bib_urls) |
| 87 | + for url in identifier["urls"] |
| 88 | + ): |
| 89 | + found_entry = entry.strip() |
| 90 | + break |
| 91 | + if found_entry: |
| 92 | + if found_entry not in references: |
| 93 | + references.append(found_entry) |
| 94 | + |
| 95 | + return "\n\n".join(references) |
| 96 | + |
| 97 | + |
| 98 | +def _get_header(methods=None): |
| 99 | + references = "Bibtex format citations for ArviZ paper\n" |
| 100 | + |
| 101 | + if methods is not None: |
| 102 | + methods_str = ", ".join([method.__name__ for method in methods]) |
| 103 | + references = references.strip() + f", and\nfor the following methods: {methods_str}" |
| 104 | + |
| 105 | + return references |
0 commit comments