-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.py
128 lines (95 loc) · 4.63 KB
/
parser.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#/usr/local/bin/python
from utils import Utils
import numpy as np
import re
LOWER_BOUND = 'lower_bound'
UPPER_BOUND = 'upper_bound'
GRID_INFO_STRING = r'# Grid information'
POLYNOMIAL_INFO_STRING = r'# Polynomial information'
FUNCTION_INFO_STRING = r'# Function information'
DERIVATIVE_INFO_STRING = r'# Derivative information'
RANDOM_HEIGHTS_STRING = r'# Random heights'
FLOAT_NUMBER_REGEX = r'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?'
DIMENSION_REGEX = r'# Dimension: (\d+)'
class Parser:
@staticmethod
def init_dimension(input_file):
with open(input_file, 'r') as f:
return int(re.search(DIMENSION_REGEX, f.readline()).group(1))
@staticmethod
def init_grid_info(input_file, dimension):
grid_list = []
with open(input_file, 'r') as f:
for line in f:
if line.startswith(GRID_INFO_STRING):
grid_list = [list(map(float, next(f).split())) for x in range(dimension)]
return np.array(grid_list)
@staticmethod
def init_no_points_per_axis(grid_info):
return [len(grid_info[axis]) for axis in range(len(grid_info))]
@staticmethod
def init_random_heights(input_file, dimension, no_points_per_axis):
flat_random_heights = []
with open(input_file, 'r') as f:
for line in f:
if line.startswith(RANDOM_HEIGHTS_STRING):
for line in f:
if line.startswith(FUNCTION_INFO_STRING):
break
if line.startswith('#') or line.startswith('\n'):
continue
flat_random_heights.extend(line.rstrip().split(' '))
return np.array(flat_random_heights, dtype=float).reshape(no_points_per_axis)
@staticmethod
def init_function_info(input_file, dimension, no_points_per_axis):
function_info_lines = []
with open(input_file, 'r') as f:
for line in f:
if line.startswith(FUNCTION_INFO_STRING):
for line in f:
if line.startswith(DERIVATIVE_INFO_STRING):
break
function_info_lines.append(line.rstrip())
return Parser.parse_tuples_info(function_info_lines, dimension, no_points_per_axis, True)
@staticmethod
def init_derivative_info(input_file, dimension, no_points_per_axis):
derivative_info_lines = []
with open(input_file, 'r') as f:
for line in f:
if line.startswith(DERIVATIVE_INFO_STRING):
for line in f:
derivative_info_lines.append(line.rstrip())
return Parser.parse_tuples_info(derivative_info_lines, dimension, no_points_per_axis, False)
@staticmethod
def build_tuples_regex_for_dimension(d):
return r'\((?P<' + LOWER_BOUND + r'_' + str(d) + r'>' + FLOAT_NUMBER_REGEX + r'),\s*' + \
r'(?P<' + UPPER_BOUND + r'_' + str(d) + r'>' + FLOAT_NUMBER_REGEX + r')\)'
@staticmethod
def build_tuples_regex(n, is_function_info):
# Constructs a regex to match either (x, y) - for function information, or
# ((a, b), (c, d), ...) - for derivative information.
if is_function_info:
return Parser.build_tuples_regex_for_dimension(1)
return r'\(' + r',\s*'.join([Parser.build_tuples_regex_for_dimension(d + 1)
for d in range(n)]) + r'\)'
@staticmethod
def build_tuple_match(n, match, is_function_info):
if is_function_info:
return tuple([match.group(LOWER_BOUND + '_1'), match.group(UPPER_BOUND + '_1')])
return tuple([(match.group(LOWER_BOUND + '_%d' % (d + 1)), \
match.group(UPPER_BOUND + '_%d' % (d + 1))) for d in range(n)])
@staticmethod
def parse_tuples_info(lines, dimension, no_points_per_axis, is_function_info):
flat_nd_list = []
regex = Parser.build_tuples_regex(dimension, is_function_info)
for line in lines:
# Ignore possible comments in the input lines.
if line.startswith('#'):
continue
# Append the pairs/tuples of lower and upper bounds to the flat list.
for match in re.finditer(regex, line):
flat_nd_list.append(Parser.build_tuple_match(dimension, match, is_function_info))
# Finally, convert to the shape of an n-dimensional array from the given points.
return np.array(flat_nd_list, \
dtype=Utils.get_dtype(dimension, is_function_info)).reshape(
no_points_per_axis)