Skip to content

Commit 838b9b9

Browse files
committed
fix(py2): drop python 2 compatibility
1 parent 4c0a753 commit 838b9b9

File tree

1 file changed

+10
-75
lines changed

1 file changed

+10
-75
lines changed

niworkflows/viz/utils.py

Lines changed: 10 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
# -*- coding: utf-8 -*-
21
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
32
# vi: set ft=python sts=4 ts=4 sw=4 et:
43
"""Helper tools for visualization purposes"""
5-
from __future__ import absolute_import, division, print_function, unicode_literals
6-
7-
import os
84
import os.path as op
5+
from shutil import which
96
import subprocess
107
import base64
118
import re
@@ -22,83 +19,25 @@
2219
from svgutils.transform import SVGFigure
2320
from seaborn import color_palette
2421

25-
from .. import NIWORKFLOWS_LOG
2622
from nipype.utils import filemanip
27-
28-
try:
29-
from shutil import which
30-
except ImportError:
31-
32-
def which(cmd):
33-
"""
34-
A homemade which command
35-
36-
>>> from niworkflows.viz.utils import which
37-
>>> which('ls')
38-
True
39-
>>> which('madeoutcommand')
40-
False
41-
42-
"""
43-
44-
try:
45-
subprocess.run([cmd], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
46-
close_fds=True)
47-
except OSError as e:
48-
from errno import ENOENT
49-
if e.errno == ENOENT:
50-
return False
51-
raise e
52-
return True
23+
from .. import NIWORKFLOWS_LOG
5324

5425

5526
SVGNS = "http://www.w3.org/2000/svg"
5627
PY3 = version_info[0] > 2
5728

58-
# Patch subprocess in python 2
59-
if not hasattr(subprocess, 'DEVNULL'):
60-
setattr(subprocess, 'DEVNULL', -3)
61-
62-
if not hasattr(subprocess, 'run'):
63-
def _run(args, input=None, stdout=None, stderr=None, shell=False, check=False,
64-
close_fds=False):
65-
from collections import namedtuple
66-
67-
devnull = open(os.devnull, 'r+')
68-
stdin = subprocess.PIPE if input is not None else None
6929

70-
if stdout == subprocess.DEVNULL:
71-
stdout = devnull
72-
73-
if stderr == subprocess.DEVNULL:
74-
stderr = devnull
75-
76-
proc = subprocess.Popen(args, stdout=stdout, shell=shell, stdin=stdin,
77-
close_fds=close_fds)
78-
result = namedtuple('CompletedProcess', 'stdout stderr')
79-
res = result(*proc.communicate(input=input))
80-
81-
devnull.close()
82-
83-
if check and proc.returncode != 0:
84-
raise subprocess.CalledProcessError(proc.returncode, args)
85-
86-
return res
87-
setattr(subprocess, 'run', _run)
88-
89-
90-
def robust_set_limits(data, plot_params):
30+
def robust_set_limits(data, plot_params, percentiles=(15, 99.8)):
31+
"""Set (vmax, vmin) based on percentiles of the data."""
9132
plot_params['vmin'] = plot_params.get(
92-
'vmin', np.percentile(data, 15))
33+
'vmin', np.percentile(data, percentiles[0]))
9334
plot_params['vmax'] = plot_params.get(
94-
'vmax', np.percentile(data, 99.8))
35+
'vmax', np.percentile(data, percentiles[1]))
9536
return plot_params
9637

9738

9839
def svg_compress(image, compress='auto'):
99-
''' takes an image as created by nilearn.plotting and returns a blob svg.
100-
Performs compression (can be disabled). A bit hacky. '''
101-
40+
"""Generate a blob SVG from a matplotlib figure, may perform compression."""
10241
# Check availability of svgo and cwebp
10342
has_compress = all((which('svgo'), which('cwebp')))
10443
if compress is True and not has_compress:
@@ -161,9 +100,7 @@ def svg_compress(image, compress='auto'):
161100

162101

163102
def svg2str(display_object, dpi=300):
164-
"""
165-
Serializes a nilearn display object as a string
166-
"""
103+
"""Serialize a nilearn display object to string."""
167104
from io import StringIO
168105
image_buf = StringIO()
169106
display_object.frame_axes.figure.savefig(
@@ -173,9 +110,7 @@ def svg2str(display_object, dpi=300):
173110

174111

175112
def extract_svg(display_object, dpi=300, compress='auto'):
176-
"""
177-
Removes the preamble of the svg files generated with nilearn
178-
"""
113+
"""Remove the preamble of the svg files generated with nilearn."""
179114
image_svg = svg2str(display_object, dpi)
180115
if compress is True or compress == 'auto':
181116
image_svg = svg_compress(image_svg, compress)
@@ -197,7 +132,7 @@ def extract_svg(display_object, dpi=300, compress='auto'):
197132

198133

199134
def cuts_from_bbox(mask_nii, cuts=3):
200-
"""Finds equi-spaced cuts for presenting images"""
135+
"""Find equi-spaced cuts for presenting images."""
201136
from nibabel.affines import apply_affine
202137

203138
mask_data = np.asanyarray(mask_nii.dataobj) > 0.0

0 commit comments

Comments
 (0)