comparison output_report.py @ 0:76733d05d8ef draft

"planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/packages/spyboat commit 5c60a414c785246371beac23ce52d8bbab3602d1"
author iuc
date Sat, 28 Nov 2020 13:45:34 +0000
parents
children 639d2031d998
comparison
equal deleted inserted replaced
-1:000000000000 0:76733d05d8ef
1 """ Produces plots and a summary html 'headless' """
2 import logging
3 import os
4
5 import matplotlib
6 import matplotlib.pyplot as ppl
7 import spyboat.plotting as spyplot
8
9 ppl.switch_backend('Agg')
10 matplotlib.rcParams["text.usetex"] = False
11 logger = logging.getLogger(__name__)
12
13 # figure resolution
14 DPI = 250
15
16
17 def produce_snapshots(input_movie, results, frame, Wkwargs, img_path="."):
18 """
19 Takes the *input_movie* and the *results* dictionary
20 from spyboat.processing.run_parallel and produces phase,
21 period and amplitude snapshot png's.
22
23 For the period snapshot also the period range is needed,
24 hence the analysis dictionary 'Wkwargs' also gets passed.
25
26 The output files name pattern is:
27 [input, phase, period, amplitude]_frame{frame}.png
28 and the storage location in *img_path*.
29
30 These get picked up by 'create_html'
31 """
32
33 spyplot.input_snapshot(input_movie[frame])
34 fig = ppl.gcf()
35 out_path = os.path.join(img_path, f"input_frame{frame}.png")
36 fig.savefig(out_path, dpi=DPI)
37 ppl.close(fig)
38
39 spyplot.phase_snapshot(results["phase"][frame])
40 fig = ppl.gcf()
41 out_path = os.path.join(img_path, f"phase_frame{frame}.png")
42 fig.savefig(out_path, dpi=DPI)
43 ppl.close(fig)
44
45 spyplot.period_snapshot(
46 results["period"][frame], Wkwargs["Tmin"],
47 Wkwargs["Tmax"], time_unit="a.u."
48 )
49
50 fig = ppl.gcf()
51 out_path = os.path.join(img_path, f"period_frame{frame}.png")
52 fig.savefig(out_path, dpi=DPI)
53 ppl.close(fig)
54
55 spyplot.amplitude_snapshot(results["amplitude"][frame])
56 fig = ppl.gcf()
57 out_path = os.path.join(img_path, f"amplitude_frame{frame}.png")
58 fig.savefig(out_path, dpi=DPI)
59 ppl.close(fig)
60
61 logger.info(f"Produced 4 snapshots for frame {frame}..")
62
63
64 def produce_distr_plots(results, Wkwargs, img_path="."):
65 """
66 Output file names are:
67
68 period_distr.png, power_distr.png and phase_distr.png
69 """
70
71 spyplot.period_distr_dynamics(results["period"], Wkwargs)
72 fig = ppl.gcf()
73 out_path = os.path.join(img_path, "period_distr.png")
74 fig.savefig(out_path, dpi=DPI)
75
76 spyplot.power_distr_dynamics(results["power"], Wkwargs)
77 fig = ppl.gcf()
78 out_path = os.path.join(img_path, "power_distr.png")
79 fig.savefig(out_path, dpi=DPI)
80
81 spyplot.phase_coherence_dynamics(results["phase"], Wkwargs)
82 fig = ppl.gcf()
83 out_path = os.path.join(img_path, "phase_distr.png")
84 fig.savefig(out_path, dpi=DPI)
85
86 logger.info("Produced 3 distribution plots..")
87
88
89 def create_html(frame_nums, html_fname="OutputReport.html"):
90 """
91 The html generated assumes the respective png's
92 have been created with 'produce_snapshots' and 'produce_distr_plots'
93 and can be found at the cwd (that's how Galaxy works..)
94 """
95
96 # -- create a gallery for every frame in frame_nums --
97
98 galleries = ""
99 for frame_num in frame_nums:
100 new_gal = f"""
101 <div class="FrameSlides">
102 <h3 style="text-align:center; color=#363333">
103 Frame Nr. {frame_num} </h3>
104
105 <div class="snapshot_gallery">
106
107 <figure class=”snapshot_gallery__item
108 snapshot_gallery__item--1">
109 <img src="input_frame{frame_num}.png" alt="The Input"
110 class="snapshot_gallery__img">
111 </figure>
112
113 <figure class=”snapshot_gallery__item
114 snapshot_gallery__item--2">
115 <img src="phase_frame{frame_num}.png" alt="Phase"
116 class="snapshot_gallery__img">
117 </figure>
118
119 <figure class=”snapshot_gallery__item
120 snapshot_gallery__item--3">
121 <img src="period_frame{frame_num}.png"
122 alt="Period" class="snapshot_gallery__img">
123 </figure>
124
125 <figure class=”snapshot_gallery__item
126 snapshot_gallery__item--4">
127 <img src="amplitude_frame{frame_num}.png"
128 alt="Amplitude" class="snapshot_gallery__img">
129 </figure>
130 </div>
131 </div>
132 """
133 galleries += new_gal
134
135 html_string = f"""
136 <html>
137 <!-- this file got automatically created by 'output_report.py' -->
138 <title>SpyBOAT Output Report</title>
139 <head>
140 <!-- that doesn't work with galaxy.. -->
141 <!--link rel="stylesheet" href="styles.css"-->
142 <style type="text/css">
143 body{{ margin:10 100; background:whitesmoke; }}
144 /*body{{ margin:10 100; background:darkslategrey; }}*/
145 .center{{
146 text-align: center;
147 display: block;
148 margin-left: auto;
149 margin-right: auto;
150 width: 100%;}}
151
152 /* matplotlib output at 1600x1200 */
153 .distr_gallery {{
154 display: grid;
155 margin: 0 auto;
156 text-align: center;
157 /* border: 1px dashed rgba(4, 4, 4, 0.35); */
158 grid-template-columns: repeat(3,1fr);
159 grid-template-rows: 20vw;
160 grid-gap: 0px;
161 column-gap: 0px
162 }}
163 .distr_gallery__img {{
164 width: 100%;
165 height: 100%;
166 object-fit: contain;
167 }}
168
169
170 /* matplotlib output at 1600x1200 */
171 .snapshot_gallery {{
172 display: grid;
173 margin: 0 auto;
174 border: 1px dashed rgba(4, 4, 4, 0.35);
175 text-align: center;
176 grid-template-columns: repeat(2,1fr);
177 grid-template-rows: repeat(2,20vw);
178 grid-gap: 5px;
179 }}
180 .snapshot_gallery__img {{
181 width: 100%;
182 height: 100%;
183 object-fit: contain;
184 }}
185 .subheader{{
186 text-align:center;
187 font-size: 160%;
188 color:#363333;}}
189 </style>
190 </head>
191 <body>
192 <h1 style="text-align:center; color:#363333">SpyBOAT Results Report</h1>
193 <hr style="width:50%">
194 <h1 class="subheader"> Distribution Dynamics </h1>
195 <div class="distr_gallery">
196 <figure class=”distr_gallery__item distr_gallery__item--1">
197 <img src="period_distr.png" alt="Period" class="distr_gallery__img">
198 </figure>
199
200 <figure class=”distr_gallery__item distr_gallery__item--2">
201 <img src="power_distr.png" alt="Power" class="distr_gallery__img">
202 </figure>
203
204 <figure class=”distr_gallery__item distr_gallery__item--3">
205 <img src="phase_distr.png" alt="Phase" class="distr_gallery__img">
206 </figure>
207
208 </div>
209
210 <h1 class="subheader"> Output Movie Snapshots </h1>
211
212 <!-- trigger the javascript at the end--->
213 <div class="center">
214 <button class="w3-button" onclick="plusDivs(-1)">&#10094; Prev</button>
215 <button class="w3-button" onclick="plusDivs(1)">Next &#10095;</button>
216 </div>
217
218 <!-- defines all elements of the "FrameSlides" class --->
219 {galleries}
220 </div>
221
222 <!-- javascript with escaped '{{'--->
223 <script>
224 var slideIndex = 1;
225 showDivs(slideIndex);
226
227 function plusDivs(n) {{
228 showDivs(slideIndex += n);
229 }}
230
231 function showDivs(n) {{
232 var i;
233 var x = document.getElementsByClassName("FrameSlides");
234 if (n > x.length) {{slideIndex = 1}}
235 if (n < 1) {{slideIndex = x.length}} ;
236 for (i = 0; i < x.length; i++) {{
237 x[i].style.display = "none";
238 }}
239 x[slideIndex-1].style.display = "block";
240 }}
241 </script>
242 </body>
243 </html>
244 """
245
246 with open(html_fname, "w") as OUT:
247 OUT.write(html_string)
248
249 logger.info("Created html report")
250 return html_string
251
252 # for local testing
253 # create_html([0,20,40,60,80])