-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
59 lines (49 loc) · 1.93 KB
/
utils.py
File metadata and controls
59 lines (49 loc) · 1.93 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
import equadratures as eq
import urllib.parse
import re
def strip_tree(polytree):
'''
Recurse tree and strip all unnecessary attributes to reduce its size.
'''
def _recurse(node):
if node is None:
return
# Strip attributes
node.pop("data", None)
# would be more elegent to loop through with delattr, but hard code with del instead for speed
del node['poly'].inputs, node['poly'].outputs, node['poly'].subsampling_algorithm_function, node['poly'].subsampling_algorithm_name,\
node['poly'].sampling_args, node['poly'].quadrature, node['poly'].sampling_ratio,\
node['poly'].solver, node['poly'].solver_args, node['poly'].variable, node['poly'].output_variances
# Go to children
_recurse(node["children"]["left"])
_recurse(node["children"]["right"])
# Kick us off...
_recurse(polytree.tree)
return polytree
# FOR DEBUGGING
def print_tree(polytree):
'''
Recurse tree and print attributes.
'''
def _recurse(node):
if node is None:
return
# Strip attributes
print(node)
print(dir(node['poly']))
# Go to children
_recurse(node["children"]["left"])
_recurse(node["children"]["right"])
# Kick us off...
_recurse(polytree.tree)
def convert_latex(text):
def toimage(x):
if x[1] and x[-2] == r'$':
x = x[2:-2]
img = "\n<img src='https://math.now.sh?from={}&color=black&alternateColor=black' style='display: block; margin: 0.5em auto;'/>\n"
return img.format(urllib.parse.quote_plus(x))
else:
x = x[1:-1]
return r''.format(urllib.parse.quote_plus(x))
return re.sub(r'\${2}([^$]+)\${2}|\$(.+?)\$', lambda x: toimage(x.group()), text)