4
4
from os .path import join
5
5
from pathlib import Path
6
6
from string import Template
7
- from bokeh . plotting import figure , ColumnDataSource
7
+
8
8
from bokeh .embed import components
9
9
from bokeh .layouts import gridplot
10
+ from bokeh .plotting import ColumnDataSource , figure
10
11
11
12
from phys2bids import _version
12
13
@@ -33,14 +34,17 @@ def _save_as_html(log_html_path, log_content, qc_html_path):
33
34
Saves the html file
34
35
"""
35
36
resource_path = Path (__file__ ).resolve ().parent
36
- head_template_name = ' report_log_template.html'
37
+ head_template_name = " report_log_template.html"
37
38
head_template_path = resource_path .joinpath (head_template_name )
38
- with open (str (head_template_path ), 'r' ) as head_file :
39
+ with open (str (head_template_path ), "r" ) as head_file :
39
40
head_tpl = Template (head_file .read ())
40
41
41
- html = head_tpl .substitute (version = _version .get_versions ()['version' ],
42
- log_html_path = log_html_path , log_content = log_content ,
43
- qc_html_path = qc_html_path )
42
+ html = head_tpl .substitute (
43
+ version = _version .get_versions ()["version" ],
44
+ log_html_path = log_html_path ,
45
+ log_content = log_content ,
46
+ qc_html_path = qc_html_path ,
47
+ )
44
48
return html
45
49
46
50
@@ -67,16 +71,18 @@ def _update_fpage_template(tree_string, bokeh_id, bokeh_js, log_html_path, qc_ht
67
71
"""
68
72
resource_path = Path (__file__ ).resolve ().parent
69
73
70
- body_template_name = ' report_plots_template.html'
74
+ body_template_name = " report_plots_template.html"
71
75
body_template_path = resource_path .joinpath (body_template_name )
72
- with open (str (body_template_path ), 'r' ) as body_file :
76
+ with open (str (body_template_path ), "r" ) as body_file :
73
77
body_tpl = Template (body_file .read ())
74
- body = body_tpl .substitute (tree = tree_string ,
75
- content = bokeh_id ,
76
- javascript = bokeh_js ,
77
- version = _version .get_versions ()['version' ],
78
- log_html_path = log_html_path ,
79
- qc_html_path = qc_html_path )
78
+ body = body_tpl .substitute (
79
+ tree = tree_string ,
80
+ content = bokeh_id ,
81
+ javascript = bokeh_js ,
82
+ version = _version .get_versions ()["version" ],
83
+ log_html_path = log_html_path ,
84
+ qc_html_path = qc_html_path ,
85
+ )
80
86
return body
81
87
82
88
@@ -94,13 +100,13 @@ def _generate_file_tree(out_dir):
94
100
tree_string: String with the tree of files in directory
95
101
"""
96
102
# prefix components:
97
- space = '  '
98
- branch = ' │ '
103
+ space = "  "
104
+ branch = " │ "
99
105
# pointers:
100
- tee = ' ├── '
101
- last = ' └── '
106
+ tee = " ├── "
107
+ last = " └── "
102
108
103
- def tree (dir_path : Path , prefix : str = '' ):
109
+ def tree (dir_path : Path , prefix : str = "" ):
104
110
"""Generate tree structure.
105
111
106
112
Given a directory Path object
@@ -119,9 +125,9 @@ def tree(dir_path: Path, prefix: str = ''):
119
125
# i.e. space because last, └── , above so no more |
120
126
yield from tree (path , prefix = prefix + extension )
121
127
122
- tree_string = ''
128
+ tree_string = ""
123
129
for line in tree (Path (out_dir )):
124
- tree_string += line + ' <br>'
130
+ tree_string += line + " <br>"
125
131
return tree_string
126
132
127
133
@@ -145,7 +151,7 @@ def _generate_bokeh_plots(phys_in, figsize=(250, 500)):
145
151
--------
146
152
https://phys2bids.readthedocs.io/en/latest/howto.html
147
153
"""
148
- colors = [' #ff7a3c' , ' #008eba' , ' #ff96d3' , ' #3c376b' , ' #ffd439' ]
154
+ colors = [" #ff7a3c" , " #008eba" , " #ff96d3" , " #3c376b" , " #ffd439" ]
149
155
150
156
time = phys_in .timeseries .T [0 ] # assumes first phys_in.timeseries is time
151
157
ch_num = len (phys_in .ch_name )
@@ -158,29 +164,30 @@ def _generate_bokeh_plots(phys_in, figsize=(250, 500)):
158
164
# build a data source for each plot, with only the data + index (time)
159
165
# for the purpose of reporting, data is downsampled 10x
160
166
# doesn't make much of a difference to the naked eye, fine for reports
161
- source = ColumnDataSource (data = dict (
162
- x = time [::downsample ],
163
- y = timeser [::downsample ]))
167
+ source = ColumnDataSource (data = dict (x = time [::downsample ], y = timeser [::downsample ]))
164
168
165
169
i = row + 1
166
170
167
- tools = ['wheel_zoom,pan,reset' ]
168
- q = figure (plot_height = figsize [0 ], plot_width = figsize [1 ],
169
- tools = tools ,
170
- title = f' Channel { i } : { phys_in .ch_name [i ]} ' ,
171
- sizing_mode = 'stretch_both' )
172
- q .line ('x' , 'y' , color = colors [i - 1 ], alpha = 0.9 , source = source )
173
- q .xaxis .axis_label = 'Time (s)'
171
+ tools = ["wheel_zoom,pan,reset" ]
172
+ q = figure (
173
+ plot_height = figsize [0 ],
174
+ plot_width = figsize [1 ],
175
+ tools = tools ,
176
+ title = f" Channel { i } : { phys_in .ch_name [i ]} " ,
177
+ sizing_mode = "stretch_both" ,
178
+ )
179
+ q .line ("x" , "y" , color = colors [i - 1 ], alpha = 0.9 , source = source )
180
+ q .xaxis .axis_label = "Time (s)"
174
181
# hovertool commented for posterity because I (KB) will be triumphant
175
182
# eventually
176
183
# q.add_tools(HoverTool(tooltips=[
177
184
# (phys_in.ch_name[i], '@y{0.000} ' + phys_in.units[i]),
178
185
# ('HELP', '100 :D')
179
186
# ], mode='vline'))
180
187
plot_list .append ([q ])
181
- p = gridplot (plot_list , toolbar_location = 'right' ,
182
- plot_height = 250 , plot_width = 750 ,
183
- merge_tools = True )
188
+ p = gridplot (
189
+ plot_list , toolbar_location = "right" , plot_height = 250 , plot_width = 750 , merge_tools = True
190
+ )
184
191
script , div = components (p )
185
192
return script , div
186
193
@@ -207,27 +214,27 @@ def generate_report(out_dir, log_path, phys_in):
207
214
https://phys2bids.readthedocs.io/en/latest/howto.html
208
215
"""
209
216
# Copy assets into output folder
210
- pkgdir = sys .modules [' phys2bids' ].__path__ [0 ]
211
- assets_path = join (pkgdir , ' reporting' , ' assets' )
212
- copy_tree (assets_path , join (out_dir , ' assets' ))
217
+ pkgdir = sys .modules [" phys2bids" ].__path__ [0 ]
218
+ assets_path = join (pkgdir , " reporting" , " assets" )
219
+ copy_tree (assets_path , join (out_dir , " assets" ))
213
220
214
221
# Read log
215
- with open (log_path , 'r' ) as f :
222
+ with open (log_path , "r" ) as f :
216
223
log_content = f .read ()
217
224
218
- log_content = log_content .replace (' \n ' , ' <br>' )
219
- log_html_path = join (out_dir , ' phys2bids_report_log.html' )
220
- qc_html_path = join (out_dir , ' phys2bids_report.html' )
225
+ log_content = log_content .replace (" \n " , " <br>" )
226
+ log_html_path = join (out_dir , " phys2bids_report_log.html" )
227
+ qc_html_path = join (out_dir , " phys2bids_report.html" )
221
228
222
229
html = _save_as_html (log_html_path , log_content , qc_html_path )
223
230
224
- with open (log_html_path , 'wb' ) as f :
225
- f .write (html .encode (' utf-8' ))
231
+ with open (log_html_path , "wb" ) as f :
232
+ f .write (html .encode (" utf-8" ))
226
233
227
234
# Read in output directory structure & create tree
228
235
tree_string = _generate_file_tree (out_dir )
229
236
bokeh_js , bokeh_div = _generate_bokeh_plots (phys_in , figsize = (250 , 750 ))
230
237
html = _update_fpage_template (tree_string , bokeh_div , bokeh_js , log_html_path , qc_html_path )
231
238
232
- with open (qc_html_path , 'wb' ) as f :
233
- f .write (html .encode (' utf-8' ))
239
+ with open (qc_html_path , "wb" ) as f :
240
+ f .write (html .encode (" utf-8" ))
0 commit comments