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

Fix Bibtex rendering in reports #1001

Merged
merged 24 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
50d4743
Change @software for @misc
eurunuela Nov 22, 2023
ce0c976
Added function to convert bibtex to html
eurunuela Nov 22, 2023
a0e048e
Created new reports dependencies with APA for bibtex
eurunuela Nov 22, 2023
61b2d2b
Undo changes in pyproject.toml
eurunuela Nov 22, 2023
bfb7bdb
Add pybtex as a dependency
eurunuela Nov 22, 2023
5f1cdb5
Fix style issues
martaarbizu Nov 22, 2023
f24581f
remove import
martinezeguiluz Nov 22, 2023
b0be904
Merge pull request #1 from martaarbizu/Fix-style-issues
eurunuela Nov 22, 2023
0bed4f5
Merge pull request #2 from martinezeguiluz/Fix_style
eurunuela Nov 22, 2023
3157f5f
Fix_style_issues_2
martaarbizu Nov 22, 2023
1b08143
Merge pull request #3 from martaarbizu/Fix-style-issues
eurunuela Nov 22, 2023
0668d67
Tried to fix inline citations
eurunuela Jan 25, 2024
2233788
remove breakpoint
notZaki Jan 26, 2024
8a358dc
Update citekey for Hunter2007
notZaki Jan 26, 2024
50d4fc6
Allow underscores to be in cite keys
notZaki Jan 26, 2024
5109cfc
Substitute inline citations with str.replace
notZaki Jan 26, 2024
41b9c84
Remove duplicated curly brace from bokeh reference
notZaki Jan 26, 2024
e7f6565
Remove colon from Hunter citekey
eurunuela Jan 26, 2024
171868b
Update Hunter citekey in docs
eurunuela Jan 26, 2024
461403d
Generate bibliography as list instead of lines
notZaki Jan 26, 2024
820d8cc
Accommodate authors without last/family names in bib
notZaki Jan 26, 2024
2387717
Show bibliography as list in report
notZaki Jan 26, 2024
28af810
Inherit content styles for bibliography in report
notZaki Jan 26, 2024
ead5da7
Merge pull request #4 from notZaki/bibtex_fix
eurunuela Jan 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/outputs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ An example report
A two-stage masking procedure was applied, in which a liberal mask (including voxels with good data in at least the first echo) was used for optimal combination, T2*/S0 estimation, and denoising, while a more conservative mask (restricted to voxels with good data in at least the first three echoes) was used for the component classification procedure.
Multi-echo data were then optimally combined using the T2* combination method \\citep{posse1999enhancement}.
Next, components were manually classified as BOLD (TE-dependent), non-BOLD (TE-independent), or uncertain (low-variance).
This workflow used numpy \\citep{van2011numpy}, scipy \\citep{virtanen2020scipy}, pandas \\citep{mckinney2010data,reback2020pandas}, scikit-learn \\citep{pedregosa2011scikit}, nilearn, bokeh \\citep{bokehmanual}, matplotlib \\citep{Hunter:2007}, and nibabel \\citep{brett_matthew_2019_3233118}.
This workflow used numpy \\citep{van2011numpy}, scipy \\citep{virtanen2020scipy}, pandas \\citep{mckinney2010data,reback2020pandas}, scikit-learn \\citep{pedregosa2011scikit}, nilearn, bokeh \\citep{bokehmanual}, matplotlib \\citep{Hunter2007}, and nibabel \\citep{brett_matthew_2019_3233118}.
This workflow also used the Dice similarity index \\citep{dice1945measures,sorensen1948method}.

References
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ dependencies = [
"nilearn>=0.7",
"numpy>=1.16",
"pandas>=2.0",
"pybtex",
"pybtex-apa-style",
"scikit-learn>=0.21",
"scipy>=1.2.0",
"threadpoolctl",
Expand All @@ -48,6 +50,7 @@ doc = [
"sphinx-argparse",
"sphinxcontrib-bibtex",
]

tests = [
"codecov",
"coverage",
Expand Down
6 changes: 3 additions & 3 deletions tedana/bibtex.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ def find_citations(description):
all_citations : :obj:`list` of :obj:`str`
A list of all identifiers for citations.
"""
paren_citations = re.findall(r"\\citep{([a-zA-Z0-9,/\.]+)}", description)
intext_citations = re.findall(r"\\cite{([a-zA-Z0-9,/\.]+)}", description)
inparen_citations = re.findall(r"\\citealt{([a-zA-Z0-9,/\.]+)}", description)
paren_citations = re.findall(r"\\citep{([a-zA-Z0-9,_/\.]+)}", description)
intext_citations = re.findall(r"\\cite{([a-zA-Z0-9,_/\.]+)}", description)
inparen_citations = re.findall(r"\\citealt{([a-zA-Z0-9,_/\.]+)}", description)
all_citations = ",".join(paren_citations + intext_citations + inparen_citations)
all_citations = all_citations.split(",")
all_citations = sorted(list(set(all_citations)))
Expand Down
11 changes: 9 additions & 2 deletions tedana/reporting/data/html/report_body_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
width: 80%;
}

.references ul {
line-height: 150%;
}

.carpet-wrapper {
margin-top: 30px;
}
Expand Down Expand Up @@ -177,9 +181,12 @@ <h1>Info</h1>
<div class="about">
<h1>About tedana</h1>
$about

</div>
<div class="content references">
<h1>References</h1>
$references
<ul>
$references
</ul>
</div>
</div>

Expand Down
66 changes: 64 additions & 2 deletions tedana/reporting/html_report.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""Build HTML reports for tedana."""
import logging
import os
import re
from os.path import join as opj
from pathlib import Path
from string import Template

import pandas as pd
from bokeh import __version__ as bokehversion
from bokeh import embed, layouts, models
from pybtex.database.input import bibtex
from pybtex.plugin import find_plugin

from tedana import __version__
from tedana.io import load_json
Expand All @@ -16,6 +19,60 @@
LGR = logging.getLogger("GENERAL")


APA = find_plugin("pybtex.style.formatting", "apa")()
HTML = find_plugin("pybtex.backends", "html")()


def _bib2html(bibliography):
parser = bibtex.Parser()
bibliography = parser.parse_file(bibliography)
formatted_bib = APA.format_bibliography(bibliography)
bibliography_str = "".join(f"<li>{entry.text.render(HTML)}</li>" for entry in formatted_bib)
return bibliography_str, bibliography


def _cite2html(bibliography, citekey):
# Make a list of citekeys and separete double citations
citekey_list = citekey.split(",") if "," in citekey else [citekey]

for idx, key in enumerate(citekey_list):
# Get first author
first_author = bibliography.entries[key].persons["author"][0]

# Keep surname only (whatever is before the comma, if there is a comma)
if "," in str(first_author):
first_author = str(first_author).split(",")[0]

# Get publication year
pub_year = bibliography.entries[key].fields["year"]

# Return complete citation
if idx == 0:
citation = f"{first_author} et al. {pub_year}"
else:
citation += f", {first_author} et al. {pub_year}"

return citation


def _inline_citations(text, bibliography):
# Find all \citep
matches = re.finditer(r"\\citep{(.*?)}", text)
citations = [(match.start(), match.group(1)) for match in matches]

updated_text = text

for citation in citations:
citekey = citation[1]
matched_string = "\\citep{" + citekey + "}"

# Convert citation form latex to html
html_citation = f"({_cite2html(bibliography, citekey)})"
updated_text = updated_text.replace(matched_string, html_citation, 1)

return updated_text


def _generate_buttons(out_dir, io_generator):
resource_path = Path(__file__).resolve().parent.joinpath("data", "html")

Expand Down Expand Up @@ -85,6 +142,12 @@ def _update_template_bokeh(bokeh_id, info_table, about, prefix, references, boke
# Initial carpet plot (default one)
initial_carpet = f"./figures/{prefix}carpet_optcom.svg"

# Convert bibtex to html
references, bibliography = _bib2html(references)

# Update inline citations
about = _inline_citations(about, bibliography)

body_template_name = "report_body_template.html"
body_template_path = resource_path.joinpath(body_template_name)
with open(str(body_template_path)) as body_file:
Expand Down Expand Up @@ -273,8 +336,7 @@ def get_elbow_val(elbow_prefix):
with open(opj(io_generator.out_dir, f"{io_generator.prefix}report.txt"), "r+") as f:
about = f.read()

with open(opj(io_generator.out_dir, f"{io_generator.prefix}references.bib")) as f:
references = f.read()
references = opj(io_generator.out_dir, f"{io_generator.prefix}references.bib")

# Read info table
data_descr_path = io_generator.get_name("data description json")
Expand Down
Loading