|
1 | | -from __future__ import with_statement |
| 1 | +# -*- coding: utf-8 -*- |
2 | 2 |
|
3 | | -__author__ = 'Tom Schaul, tom@idsia.ch; Justin Bayer, bayerj@in.tum.de' |
| 3 | +from __future__ import with_statement |
4 | 4 |
|
5 | 5 | import gc |
6 | 6 | import pickle |
|
15 | 15 |
|
16 | 16 | from scipy import where, array, exp, zeros, size, mat, median |
17 | 17 |
|
| 18 | +__author__ = 'Tom Schaul, tom@idsia.ch; Justin Bayer, bayerj@in.tum.de' |
| 19 | + |
| 20 | + |
18 | 21 | # file extension for load/save protocol mapping |
19 | 22 | known_extensions = { |
20 | 23 | 'mat': 'matlab', |
21 | 24 | 'txt': 'ascii', |
22 | 25 | 'svm': 'libsvm', |
23 | 26 | 'pkl': 'pickle', |
24 | | - 'nc' : 'netcdf' } |
| 27 | + 'nc': 'netcdf' |
| 28 | +} |
25 | 29 |
|
26 | 30 |
|
27 | 31 | def abstractMethod(): |
@@ -149,42 +153,48 @@ class Serializable(object): |
149 | 153 | supported. |
150 | 154 | """ |
151 | 155 |
|
152 | | - def saveToFileLike(self, flo, format=None, **kwargs): |
| 156 | + def saveToFileLike(self, flo, formatObj=None, **kwargs): |
153 | 157 | """Save the object to a given file like object in the given format. |
154 | 158 | """ |
155 | | - format = 'pickle' if format is None else format |
156 | | - save = getattr(self, "save_%s" % format, None) |
| 159 | + formatObj = 'pickle' if formatObj is None else formatObj |
| 160 | + save = getattr(self, "save_%s" % formatObj, None) |
| 161 | + |
157 | 162 | if save is None: |
158 | | - raise ValueError("Unknown format '%s'." % format) |
| 163 | + raise ValueError("Unknown format '%s'." % formatObj) |
| 164 | + |
159 | 165 | save(flo, **kwargs) |
160 | 166 |
|
161 | 167 | @classmethod |
162 | | - def loadFromFileLike(cls, flo, format=None): |
| 168 | + def loadFromFileLike(cls, flo, formatObj=None): |
163 | 169 | """Load the object to a given file like object with the given protocol. |
164 | 170 | """ |
165 | | - format = 'pickle' if format is None else format |
166 | | - load = getattr(cls, "load_%s" % format, None) |
| 171 | + formatObj = 'pickle' if formatObj is None else formatObj |
| 172 | + load = getattr(cls, "load_%s" % formatObj, None) |
| 173 | + |
167 | 174 | if load is None: |
168 | | - raise ValueError("Unknown format '%s'." % format) |
| 175 | + raise ValueError("Unknown format '%s'." % formatObj) |
| 176 | + |
169 | 177 | return load(flo) |
170 | 178 |
|
171 | | - def saveToFile(self, filename, format=None, **kwargs): |
| 179 | + def saveToFile(self, filename, formatObj=None, **kwargs): |
172 | 180 | """Save the object to file given by filename.""" |
173 | | - if format is None: |
| 181 | + if formatObj is None: |
174 | 182 | # try to derive protocol from file extension |
175 | | - format = formatFromExtension(filename) |
176 | | - with file(filename, 'wb') as fp: |
177 | | - self.saveToFileLike(fp, format, **kwargs) |
| 183 | + formatObj = formatFromExtension(filename) |
| 184 | + |
| 185 | + with open(filename, 'wb') as fp: |
| 186 | + self.saveToFileLike(fp, formatObj, **kwargs) |
178 | 187 |
|
179 | 188 | @classmethod |
180 | | - def loadFromFile(cls, filename, format=None): |
| 189 | + def loadFromFile(cls, filename, formatObj=None): |
181 | 190 | """Return an instance of the class that is saved in the file with the |
182 | 191 | given filename in the specified format.""" |
183 | | - if format is None: |
| 192 | + if formatObj is None: |
184 | 193 | # try to derive protocol from file extension |
185 | | - format = formatFromExtension(filename) |
186 | | - with file(filename, 'rbU') as fp: |
187 | | - obj = cls.loadFromFileLike(fp, format) |
| 194 | + formatObj = formatFromExtension(filename) |
| 195 | + |
| 196 | + with open(filename, 'rbU') as fp: |
| 197 | + obj = cls.loadFromFileLike(fp, formatObj) |
188 | 198 | obj.filename = filename |
189 | 199 | return obj |
190 | 200 |
|
@@ -485,8 +495,11 @@ def crossproduct(ss, row=None, level=0): |
485 | 495 | if row is None: |
486 | 496 | row = [] |
487 | 497 | if len(ss) > 1: |
488 | | - return reduce(operator.add, |
489 | | - [crossproduct(ss[1:], row + [i], level + 1) for i in ss[0]]) |
| 498 | + import functools |
| 499 | + return functools.reduce( |
| 500 | + operator.add, |
| 501 | + [crossproduct(ss[1:], row + [i], level + 1) for i in ss[0]] |
| 502 | + ) |
490 | 503 | else: |
491 | 504 | return [row + [i] for i in ss[0]] |
492 | 505 |
|
@@ -539,7 +552,7 @@ def permuteToBlocks2d(arr, blockheight, blockwidth): |
539 | 552 | _height, width = arr.shape |
540 | 553 | arr = arr.flatten() |
541 | 554 | new = zeros(size(arr)) |
542 | | - for i in xrange(size(arr)): |
| 555 | + for i in range(size(arr)): |
543 | 556 | blockx = (i % width) / blockwidth |
544 | 557 | blocky = i / width / blockheight |
545 | 558 | blockoffset = blocky * width / blockwidth + blockx |
@@ -785,4 +798,3 @@ def weightedUtest(g1, w1, g2, w2): |
785 | 798 | z = (u1 - mu) / sigu |
786 | 799 | conf = norm.cdf(z) |
787 | 800 | return conf |
788 | | - |
|
0 commit comments