3
3
"""Helper tools for visualization purposes"""
4
4
from pathlib import Path
5
5
from shutil import which
6
+ from tempfile import TemporaryDirectory
6
7
import subprocess
7
8
import base64
8
9
import re
@@ -327,10 +328,15 @@ def plot_registration(anat_nii, div_id, plot_params=None,
327
328
328
329
329
330
def compose_view (bg_svgs , fg_svgs , ref = 0 , out_file = 'report.svg' ):
330
- """
331
- Composes the input svgs into one standalone svg and inserts
332
- the CSS code for the flickering animation
333
- """
331
+ """Compose the input svgs into one standalone svg with CSS flickering animation."""
332
+ out_file = Path (out_file ).absolute ()
333
+ out_file .write_text ("\n " .join (
334
+ _compose_view (bg_svgs , fg_svgs , ref = ref )
335
+ ))
336
+ return str (out_file )
337
+
338
+
339
+ def _compose_view (bg_svgs , fg_svgs , ref = 0 ):
334
340
335
341
if fg_svgs is None :
336
342
fg_svgs = []
@@ -379,11 +385,12 @@ def compose_view(bg_svgs, fg_svgs, ref=0, out_file='report.svg'):
379
385
fig .root .attrib .pop ("width" )
380
386
fig .root .attrib .pop ("height" )
381
387
fig .root .set ("preserveAspectRatio" , "xMidYMid meet" )
382
- out_file = Path (out_file ).absolute ()
383
- fig .save (str (out_file ))
384
388
385
- # Post processing
386
- svg = out_file .read_text ().splitlines ()
389
+ with TemporaryDirectory () as tmpdirname :
390
+ out_file = Path (tmpdirname ) / "tmp.svg"
391
+ fig .save (str (out_file ))
392
+ # Post processing
393
+ svg = out_file .read_text ().splitlines ()
387
394
388
395
# Remove <?xml... line
389
396
if svg [0 ].startswith ("<?xml" ):
@@ -398,8 +405,7 @@ def compose_view(bg_svgs, fg_svgs, ref=0, out_file='report.svg'):
398
405
.foreground-svg:hover { animation-play-state: running;}
399
406
</style>""" % tuple ([uuid4 ()] * 2 ))
400
407
401
- out_file .write_text ("\n " .join (svg ))
402
- return str (out_file )
408
+ return svg
403
409
404
410
405
411
def transform_to_2d (data , max_axis ):
0 commit comments