-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrdb.py
286 lines (260 loc) · 8.8 KB
/
rdb.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
This module contains code useful for reading, writing, and manipulating RDB
database tables (i.e., tab-delimited data fields with a header and format line)
"""
from __future__ import division, with_statement, print_function
import array
from collections import OrderedDict
from functools import wraps
from math import sqrt
import re
from sys import stderr
import warnings
try:
from numpy import sqrt
import numpy as np
_HAS_NUMPY = True
except ImportError:
np = None
_HAS_NUMPY = False
def checksize(fcn):
@wraps(fcn)
def newfcn(self, other):
if len(self) != len(other):
raise SizeError('Vector sizes incompatible')
return fcn(self, other)
return newfcn
def check_vector_or_scalar(fcn):
@wraps(fcn)
def newfcn(self, other):
scalar = False
if len(other) == 1: # Scalar
scalar = True
elif len(self) != len(other):
raise SizeError('Vector sizes incompatible')
return fcn(self, other)
return newfcn
class Vector(array.array):
# Make vector addition, subtraction work the way it's expected to
@checksize
def __add__(self, other):
return Vector(self.typecode,
[self[i] + other[i] for i in range(len(self))])
@checksize
def __sub__(self, other):
return Vector(self.typecode,
[self[i] - other[i] for i in range(len(self))])
@check_vector_or_scalar
def __mul__(self, other):
global scalar
if scalar: return Vector([i*other for i in self])
return Vector(self.typecode,
[self[i] * other[i] for i in range(len(self))])
@check_vector_or_scalar
def __div__(self, other):
global scalar
if scalar: return Vector([i/other for i in self])
return Vector(self.typecode,
[self[i] / other[i] for i in range(len(self))])
@checksize
def __iadd__(self, other):
for i in range(len(self)):
self[i] += other[i]
return self
@checksize
def __isub__(self, other):
for i in range(len(self)):
self[i] -= other[i]
return self
@check_vector_or_scalar
def __idiv__(self, other):
global scalar
if scalar:
for i in range(len(self)): self[i] /= other
else:
for i in range(len(self)): self[i] /= other[i]
return self
@check_vector_or_scalar
def __imul__(self, other):
global scalar
if scalar:
for i in range(len(self)): self[i] *= other
else:
for i in range(len(self)): self[i] *= other[i]
return self
def mean(self):
""" Return the mean of this list """
return sum(self) / len(self)
def std(self):
""" Return the standard deviation of this list """
sum = sum2 = 0
for v in self:
sum += v
sum2 += v * v
avg = sum / len(self)
return sqrt(abs(sum2 / len(self) - avg * avg))
# Python 3 support
try:
unicode
str = unicode
except NameError:
pass # Nothing to do
# Exception hierarchy
class RDBError(Exception): pass
class SizeError(RDBError): pass
# Decorators
def _ensurefile(mode, clsmeth=False):
"""
Makes sure that the first object passed to the function is an open file
with the requested mode
"""
def wrapper(fcn):
@wraps(fcn)
def newfcn(self, f, *args, **kwargs):
if not hasattr(f, 'write') or not hasattr(f, 'read'):
# Pretend it's a string
with open(f, mode) as _f:
return fcn(self, _f, *args, **kwargs)
return fcn(self, f, *args, **kwargs)
return newfcn
return wrapper
class RDB(OrderedDict):
""" RDB data """
_fmtre = re.compile(r'%(?:[0-9-\.]*)([sgfdi])')
def __init__(self, *args, **kwargs):
super(RDB, self).__init__(*args, **kwargs)
self.formats = []
self._types = []
self._format = {str : '%s', float : '%g', int : '%d'}
@property
def format(self):
return self._format
@format.setter
def format(self, fmt):
rematch = RDB._fmtre.match(fmt)
if rematch is None:
raise RDBError('Invalid format statement!')
typecode = rematch.groups()[0]
if typecode == 's': self._format[str] = fmt
if typecode in ('i', 'd'): self._format[int] = fmt
if typecode in ('f', 'g'): self._format[float] = fmt
@_ensurefile('w')
def write_to_file(self, f):
""" Writes the RDB table to a file """
if not self._types:
# Set our types
self._set_types()
print('\t'.join(self), file=f)
print('\t'.join(self.formats), file=f)
mylen = len(self[self.keys()[0]])
for key in self:
if len(self[key]) != mylen:
raise RDBError('data length mismatch')
for i in range(mylen):
print('\t'.join(
[self._format[self._types[j]] % (self[key][i]) for j, key in
enumerate(self)]),
file=f
)
def _set_types(self):
""" Determine the type of each of our data sets """
global _HAS_NUMPY
for key in self:
if all([isinstance(val, int) for val in self[key]]):
self._types.append(int)
elif all([isinstance(val, float) for val in self[key]]):
self._types.append(float)
elif _HAS_NUMPY:
if isinstance(self[key], np.ndarray):
# Now see if it is any of the float types
if str(self[key].dtype).startswith('float'):
self._types.append(float)
elif str(self[key].dtype).startswith('int'):
self._types.append(int)
else:
self._types.append(str)
else:
self._types.append(str)
else:
self._types.append(str)
@classmethod
def load_from_dict(cls, data):
""" Load this from another dict class """
inst = cls()
for key in data: inst[key] = data[key]
def check(self, reporter=stderr):
""" Checks that everything is valid. Return 0 for OK; 1 for not """
retval = 0
same = all([len(self[key]) == len(self[self.keys()[0]]) for key in self])
if not same:
reporter.write('Length mismatch:\n\t')
reporter.write('\n\t'.join(['%s: %d' % (key, len(self[key]))
for key in self]))
reporter.write('\n')
retval = 1
return retval
@classmethod
@_ensurefile('r', clsmeth=True)
def load_from_file(cls, f, use_numpy=True):
""" Loads the data structure from an RDB file """
global _HAS_NUMPY
if use_numpy and not _HAS_NUMPY:
warnings.warn('numpy could not be found. Using array.')
use_numpy = False
inst = RDB()
for key in f.readline()[:-1].split('\t'):
inst[key] = []
fmtline = f.readline().strip()
if fmtline:
inst.formats = fmtline.split('\t')
if len(inst.formats) != len(inst):
raise RDBError('badly-formed formats line. wrong number of formats')
line = f.readline()[:-1]
# determine initial types
data = line.split('\t')
for i, d in enumerate(data):
try:
inst[inst.keys()[i]].append(int(d))
inst._types.append(int)
except ValueError:
# Not integer, try float
try:
inst[inst.keys()[i]].append(float(d))
inst._types.append(float)
except ValueError:
# Not integer, store as unicode string
inst[inst.keys()[i]].append(str(d))
inst._types.append(str)
for line in f:
data = line[:-1].split('\t')
for i, d in enumerate(data):
try:
inst[inst.keys()[i]].append(inst._types[i](d))
except ValueError:
try:
if inst._types[i] is int:
inst[inst.keys()[i]].append(float(d))
inst._types[i] = float
else:
# Originally a float, now make it a string
inst[inst.keys()[i]].append(str(d))
inst._types[i] = str
except ValueError:
# Only hit here if original type was int and current is string
inst[inst.keys()[i]].append(str(d))
inst._types[i] = str
if use_numpy:
# Convert data to numpy arrays
for i, key in enumerate(inst):
if inst._types[i] is int:
inst[key] = np.asarray(inst[key], dtype=np.int)
elif inst._types[i] is float:
inst[key] = np.asarray(inst[key], dtype=np.float)
else:
# Convert data to Vector arrays
for i, key in enumerate(inst):
if inst._types[i] is int:
inst[key] = Vector('i', inst[key])
elif inst._types[i] is float:
inst[key] = Vector('d', inst[key])
return inst