-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
45 lines (38 loc) · 1.26 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from contextlib import contextmanager
from xml.dom import minidom
import sys, os
import numpy as np
@contextmanager
def hidden_prints():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
@contextmanager
def hidden_errors():
with open(os.devnull, "w") as devnull:
old_stderr = sys.stderr
sys.stderr = devnull
try:
yield
finally:
sys.stderr = old_stderr
def normalize_minmax(x):
return (x - x.min()) / (x.max() - x.min())
def read_inviwo_tf(fn):
xmldoc = minidom.parse(str(fn))
def parse_point(point):
pos = float(point.getElementsByTagName('pos')[0].getAttribute('content'))
opacity = float(point.getElementsByTagName('rgba')[0].getAttribute('w'))
return pos, opacity
points = sorted(map(parse_point, xmldoc.getElementsByTagName('Point')))
l, r = points[0][1], points[-1][1]
xp, yp = zip(*points)
def apply_tf(x, normalize=False):
if normalize: x = normalize_minmax(x)
return np.interp(x, xp, yp, left=l, right=r)
return apply_tf
__all__ = ['hidden_prints', 'hidden_errors', 'read_inviwo_tf', 'normalize_minmax']