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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
#!/usr/bin/env python3
r'''Tests the python parser
This is intended to work with both python2 and python3.
'''
from __future__ import print_function
import os
import sys
import numpy as np
sys.path[:0] = (os.path.abspath(os.path.dirname(sys.argv[0])) + "/../lib",)
import vnlog
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
inputstring = '''#! zxcv
#
#
#
## fdd
#time height
## qewr
1 2
## ff
3 4
# - 10
- 5 # abc
6 -
- -
7 8
'''
ref = r'''1 2
3 4
None 5
6 None
None None
7 8
'''
# Parsing manually
f = StringIO(inputstring)
parser = vnlog.vnlog()
resultstring = ''
for l in f:
parser.parse(l)
d = parser.values_dict()
if not d:
continue
resultstring += '{} {}\n'.format(d['time'],d['height'])
if resultstring != ref:
print("Expected '{}' but got '{}'".format(ref, resultstring))
print("Test failed!")
sys.exit(1)
# Iterating
f = StringIO(inputstring)
resultstring = ''
for d in vnlog.vnlog(f):
resultstring += '{} {}\n'.format(d['time'],d['height'])
if resultstring != ref:
print("Expected '{}' but got '{}'".format(ref, resultstring))
print("Test failed!")
sys.exit(1)
# Slurping
inputstring_noundef = r'''#! zxcv
# time height
## qewr
1 2
3 4 #fff
# - 10
7 8
'''
ref_noundef = np.array(((1,2),(3,4),(7,8)))
f = StringIO(inputstring_noundef)
arr,list_keys,dict_key_index = vnlog.slurp(f)
if np.linalg.norm((ref_noundef - arr).ravel()) > 1e-8:
raise Exception("Array mismatch: expected '{}' but got '{}". \
format(ref_noundef, arr))
if len(list_keys) != 2 or list_keys[0] != 'time' or list_keys[1] != 'height':
raise Exception("Key mismatch: expected '{}' but got '{}". \
format(('time','height'), list_keys))
if len(dict_key_index) != 2 or dict_key_index['time'] != 0 or dict_key_index['height'] != 1:
raise Exception("Key-dict mismatch: expected '{}' but got '{}". \
format({'time': 0, 'height': 1}, dict_key_index))
# Slurping with simple dtypes
f = StringIO(inputstring_noundef)
arr = vnlog.slurp(f, dtype=int)[0]
if arr.dtype != int: raise Exception("Unexpected dtype")
if np.linalg.norm((ref_noundef - arr).ravel()) > 1e-8:
raise Exception("Array mismatch")
f = StringIO(inputstring_noundef)
arr = vnlog.slurp(f, dtype=float)[0]
if arr.dtype != float: raise Exception("Unexpected dtype")
if np.linalg.norm((ref_noundef - arr).ravel()) > 1e-8:
raise Exception("Array mismatch")
f = StringIO(inputstring_noundef)
arr = vnlog.slurp(f, dtype=np.dtype(float))[0]
if arr.dtype != float: raise Exception("Unexpected dtype")
if np.linalg.norm((ref_noundef - arr).ravel()) > 1e-8:
raise Exception("Array mismatch")
# Slurping a single row should still produce a 2d result
inputstring = '''
## asdf
# x name y name2 z
1 2 3
'''
f = StringIO(inputstring)
arr = vnlog.slurp(f)[0]
if arr.shape != (1,3): raise Exception("Unexpected shape")
# Slurping with structured dtypes
inputstring = '''
## asdf
# x name y name2 z
1 a 2 zz2 3
4 fbb 5 qq2 6
'''
ref = np.array(((1,2,3),
(4,5,6),),)
dtype = np.dtype([ ('name', 'U16'),
('x y z', int, (3,)),
('name2', 'U16'), ])
f = StringIO(inputstring)
arr = vnlog.slurp(f, dtype=dtype)
if arr.shape != (2,): raise Exception("Unexpected structured array outer shape")
if arr['name' ].shape != (2,): raise Exception("Unexpected structured array inner shape")
if arr['name2'].shape != (2,): raise Exception("Unexpected structured array inner shape")
if arr['x y z'].shape != (2,3): raise Exception("Unexpected structured array inner shape")
if arr['x y z'].dtype != int: raise Exception("Unexpected structured array inner dtype")
if arr['name' ][0] != 'a': raise Exception("mismatch")
if arr['name2'][1] != 'qq2': raise Exception("mismatch")
if np.linalg.norm((ref - arr['x y z']).ravel()) > 1e-8:
raise Exception("Array mismatch")
# selecting a subset of the data
ref = np.array(((1,3),
(4,6),),)
dtype = np.dtype([ ('name2', 'U16'),
('x z', int, (2,)) ])
f = StringIO(inputstring)
arr = vnlog.slurp(f, dtype=dtype)
if arr['x z'].shape != (2,2): raise Exception("Unexpected structured array inner shape")
if arr['x z'].dtype != int: raise Exception("Unexpected structured array inner dtype")
if arr['name2'][1] != 'qq2': raise Exception("mismatch")
if np.linalg.norm((ref - arr['x z']).ravel()) > 1e-8:
raise Exception("Array mismatch")
dtype = np.dtype([ ('name', 'U16'),
('x yz', int, (3,)),
('name2', 'U16'), ])
f = StringIO(inputstring)
try: arr = vnlog.slurp(f, dtype=dtype)
except: pass
else: raise Exception("Bad dtype wasn't flagged")
dtype = np.dtype([ ('name', 'U16'),
('x yz', int, (2,)),
('name2', 'U16'), ])
f = StringIO(inputstring)
try: arr = vnlog.slurp(f, dtype=dtype)
except: pass
else: raise Exception("Bad dtype wasn't flagged")
dtype = np.dtype([ ('name', 'U16'),
('x y z w', int, (4,)),
('name2', 'U16'), ])
f = StringIO(inputstring)
try: arr = vnlog.slurp(f, dtype=dtype)
except: pass
else: raise Exception("Bad dtype wasn't flagged")
dtype = np.dtype([ ('name', 'U16'),
('x y z', int, (2,)),
('name2', 'U16'), ])
f = StringIO(inputstring)
try: arr = vnlog.slurp(f, dtype=dtype)
except: pass
else: raise Exception("Bad dtype wasn't flagged")
dtype = np.dtype([ ('name', 'U16'),
('x y z', int, (3,)),
('name 2', 'U16'), ])
f = StringIO(inputstring)
try: arr = vnlog.slurp(f, dtype=dtype)
except: pass
else: raise Exception("Bad dtype wasn't flagged")
# Slurping a single row with a structured dtype
inputstring = '''
## asdf
# x name y name2 z
4 fbb 5 qq2 6
'''
dtype = np.dtype([ ('name', 'U16'),
('x y z', int, (3,)),
('name2', 'U16'), ])
f = StringIO(inputstring)
arr = vnlog.slurp(f, dtype=dtype)
if arr.shape != (1,): raise Exception("Unexpected structured array outer shape")
if arr['name' ].shape != (1,): raise Exception("Unexpected structured array inner shape")
if arr['name2'].shape != (1,): raise Exception("Unexpected structured array inner shape")
if arr['x y z'].shape != (1,3): raise Exception("Unexpected structured array inner shape")
print("Test passed")
sys.exit(0);
|