Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 18, 2024
1 parent 06e4f1d commit c28a127
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 52 deletions.
5 changes: 3 additions & 2 deletions phys2bids/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,16 @@ def _get_parser():
default=False,
)
optional.add_argument(
"-report", "--report",
"-report",
"--report",
dest="make_report",
action="store_true",
help="Generate a report with the data and generated folder structure. "
"Default is False.",
default=False,
)
optional.add_argument("-v", "--version", action="version", version=("%(prog)s " + __version__))

parser._action_groups.append(optional)

return parser
Expand Down
2 changes: 1 addition & 1 deletion phys2bids/reporting/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"""Visual reporting tools for inspecting phys2bids workflow outputs."""
"""Visual reporting tools for inspecting phys2bids workflow outputs."""
2 changes: 1 addition & 1 deletion phys2bids/reporting/assets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ html, body {
.main{
margin-top: 100px;
margin-left: 100px;
}
}
99 changes: 53 additions & 46 deletions phys2bids/reporting/html_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
from os.path import join
from pathlib import Path
from string import Template
from bokeh.plotting import figure, ColumnDataSource

from bokeh.embed import components
from bokeh.layouts import gridplot
from bokeh.plotting import ColumnDataSource, figure

from phys2bids import _version

Expand All @@ -33,14 +34,17 @@ def _save_as_html(log_html_path, log_content, qc_html_path):
Saves the html file
"""
resource_path = Path(__file__).resolve().parent
head_template_name = 'report_log_template.html'
head_template_name = "report_log_template.html"
head_template_path = resource_path.joinpath(head_template_name)
with open(str(head_template_path), 'r') as head_file:
with open(str(head_template_path), "r") as head_file:
head_tpl = Template(head_file.read())

html = head_tpl.substitute(version=_version.get_versions()['version'],
log_html_path=log_html_path, log_content=log_content,
qc_html_path=qc_html_path)
html = head_tpl.substitute(
version=_version.get_versions()["version"],
log_html_path=log_html_path,
log_content=log_content,
qc_html_path=qc_html_path,
)
return html


Expand All @@ -67,16 +71,18 @@ def _update_fpage_template(tree_string, bokeh_id, bokeh_js, log_html_path, qc_ht
"""
resource_path = Path(__file__).resolve().parent

body_template_name = 'report_plots_template.html'
body_template_name = "report_plots_template.html"
body_template_path = resource_path.joinpath(body_template_name)
with open(str(body_template_path), 'r') as body_file:
with open(str(body_template_path), "r") as body_file:
body_tpl = Template(body_file.read())
body = body_tpl.substitute(tree=tree_string,
content=bokeh_id,
javascript=bokeh_js,
version=_version.get_versions()['version'],
log_html_path=log_html_path,
qc_html_path=qc_html_path)
body = body_tpl.substitute(
tree=tree_string,
content=bokeh_id,
javascript=bokeh_js,
version=_version.get_versions()["version"],
log_html_path=log_html_path,
qc_html_path=qc_html_path,
)
return body


Expand All @@ -94,13 +100,13 @@ def _generate_file_tree(out_dir):
tree_string: String with the tree of files in directory
"""
# prefix components:
space = ' '
branch = ''
space = " "
branch = ""
# pointers:
tee = '├── '
last = '└── '
tee = "├── "
last = "└── "

def tree(dir_path: Path, prefix: str = ''):
def tree(dir_path: Path, prefix: str = ""):
"""Generate tree structure.
Given a directory Path object
Expand All @@ -119,9 +125,9 @@ def tree(dir_path: Path, prefix: str = ''):
# i.e. space because last, └── , above so no more |
yield from tree(path, prefix=prefix + extension)

tree_string = ''
tree_string = ""
for line in tree(Path(out_dir)):
tree_string += line + '<br>'
tree_string += line + "<br>"
return tree_string


Expand All @@ -145,7 +151,7 @@ def _generate_bokeh_plots(phys_in, figsize=(250, 500)):
--------
https://phys2bids.readthedocs.io/en/latest/howto.html
"""
colors = ['#ff7a3c', '#008eba', '#ff96d3', '#3c376b', '#ffd439']
colors = ["#ff7a3c", "#008eba", "#ff96d3", "#3c376b", "#ffd439"]

time = phys_in.timeseries.T[0] # assumes first phys_in.timeseries is time
ch_num = len(phys_in.ch_name)
Expand All @@ -158,29 +164,30 @@ def _generate_bokeh_plots(phys_in, figsize=(250, 500)):
# build a data source for each plot, with only the data + index (time)
# for the purpose of reporting, data is downsampled 10x
# doesn't make much of a difference to the naked eye, fine for reports
source = ColumnDataSource(data=dict(
x=time[::downsample],
y=timeser[::downsample]))
source = ColumnDataSource(data=dict(x=time[::downsample], y=timeser[::downsample]))

i = row + 1

tools = ['wheel_zoom,pan,reset']
q = figure(plot_height=figsize[0], plot_width=figsize[1],
tools=tools,
title=f' Channel {i}: {phys_in.ch_name[i]}',
sizing_mode='stretch_both')
q.line('x', 'y', color=colors[i - 1], alpha=0.9, source=source)
q.xaxis.axis_label = 'Time (s)'
tools = ["wheel_zoom,pan,reset"]
q = figure(
plot_height=figsize[0],
plot_width=figsize[1],
tools=tools,
title=f" Channel {i}: {phys_in.ch_name[i]}",
sizing_mode="stretch_both",
)
q.line("x", "y", color=colors[i - 1], alpha=0.9, source=source)
q.xaxis.axis_label = "Time (s)"
# hovertool commented for posterity because I (KB) will be triumphant
# eventually
# q.add_tools(HoverTool(tooltips=[
# (phys_in.ch_name[i], '@y{0.000} ' + phys_in.units[i]),
# ('HELP', '100 :D')
# ], mode='vline'))
plot_list.append([q])
p = gridplot(plot_list, toolbar_location='right',
plot_height=250, plot_width=750,
merge_tools=True)
p = gridplot(
plot_list, toolbar_location="right", plot_height=250, plot_width=750, merge_tools=True
)
script, div = components(p)
return script, div

Expand All @@ -207,27 +214,27 @@ def generate_report(out_dir, log_path, phys_in):
https://phys2bids.readthedocs.io/en/latest/howto.html
"""
# Copy assets into output folder
pkgdir = sys.modules['phys2bids'].__path__[0]
assets_path = join(pkgdir, 'reporting', 'assets')
copy_tree(assets_path, join(out_dir, 'assets'))
pkgdir = sys.modules["phys2bids"].__path__[0]
assets_path = join(pkgdir, "reporting", "assets")
copy_tree(assets_path, join(out_dir, "assets"))

# Read log
with open(log_path, 'r') as f:
with open(log_path, "r") as f:
log_content = f.read()

log_content = log_content.replace('\n', '<br>')
log_html_path = join(out_dir, 'phys2bids_report_log.html')
qc_html_path = join(out_dir, 'phys2bids_report.html')
log_content = log_content.replace("\n", "<br>")
log_html_path = join(out_dir, "phys2bids_report_log.html")
qc_html_path = join(out_dir, "phys2bids_report.html")

html = _save_as_html(log_html_path, log_content, qc_html_path)

with open(log_html_path, 'wb') as f:
f.write(html.encode('utf-8'))
with open(log_html_path, "wb") as f:
f.write(html.encode("utf-8"))

# Read in output directory structure & create tree
tree_string = _generate_file_tree(out_dir)
bokeh_js, bokeh_div = _generate_bokeh_plots(phys_in, figsize=(250, 750))
html = _update_fpage_template(tree_string, bokeh_div, bokeh_js, log_html_path, qc_html_path)

with open(qc_html_path, 'wb') as f:
f.write(html.encode('utf-8'))
with open(qc_html_path, "wb") as f:
f.write(html.encode("utf-8"))
2 changes: 1 addition & 1 deletion phys2bids/reporting/report_log_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
<p>$log_content</p>
</div>
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion phys2bids/reporting/report_plots_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ <h2>phys2BIDS Output Directory</h2>
</body>
</html>

$javascript
$javascript

0 comments on commit c28a127

Please sign in to comment.