| 1 |
|
| 2 |
try: |
| 3 |
from ConfigParser import ConfigParser |
| 4 |
except ImportError: |
| 5 |
from configparser import ConfigParser |
| 6 |
import os |
| 7 |
import array |
| 8 |
import gzip |
| 9 |
import sys |
| 10 |
from auto import AUTOExceptions |
| 11 |
N = array |
| 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 |
try: |
| 78 |
unicode |
| 79 |
except NameError: |
| 80 |
basestring = (str,bytes) |
| 81 |
|
| 82 |
def isiterable(value): |
| 83 |
|
| 84 |
if isinstance(value, basestring): return False |
| 85 |
try: |
| 86 |
iter(value) |
| 87 |
return True |
| 88 |
except TypeError: |
| 89 |
return False |
| 90 |
|
| 91 |
def findBaseClass(inputClass,baseClass): |
| 92 |
try: |
| 93 |
for base in inputClass.__bases__: |
| 94 |
if base == baseClass: |
| 95 |
return 1 |
| 96 |
elif findBaseClass(base,baseClass) == 1: |
| 97 |
return 1 |
| 98 |
return 0 |
| 99 |
|
| 100 |
except AttributeError: |
| 101 |
return 0 |
| 102 |
|
| 103 |
def getAUTORC(section=None): |
| 104 |
try: |
| 105 |
parser = ConfigParser(strict=False) |
| 106 |
except TypeError: |
| 107 |
parser = ConfigParser() |
| 108 |
if section is not None: |
| 109 |
parser.add_section(section) |
| 110 |
path = os.path.expandvars("$AUTO_DIR/.autorc") |
| 111 |
if os.path.exists(path): |
| 112 |
parser.read(path) |
| 113 |
path = os.path.expandvars("$HOME/.autorc") |
| 114 |
if os.path.exists(path): |
| 115 |
parser.read(path) |
| 116 |
if os.path.exists("./autorc"): |
| 117 |
parser.read("./autorc") |
| 118 |
elif os.path.exists("./.autorc"): |
| 119 |
parser.read("./.autorc") |
| 120 |
return parser |
| 121 |
|
| 122 |
def openFilename(filename,mode): |
| 123 |
try: |
| 124 |
inputfile = open(filename,mode) |
| 125 |
except IOError: |
| 126 |
s = sys.exc_info()[1] |
| 127 |
try: |
| 128 |
import gzip |
| 129 |
inputfile = gzip.open(filename+".gz",mode) |
| 130 |
inputfile.name = filename |
| 131 |
except IOError: |
| 132 |
raise IOError(s) |
| 133 |
return inputfile |
| 134 |
|
| 135 |
def getstatusoutput(cmd, shell=False): |
| 136 |
try: |
| 137 |
import subprocess, os |
| 138 |
if not shell: |
| 139 |
for i in range(len(cmd)): |
| 140 |
cmd[i] = cmd[i].replace("'","") |
| 141 |
p = subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE, |
| 142 |
stderr=subprocess.STDOUT) |
| 143 |
output = p.communicate() |
| 144 |
s = output[0].decode('ascii') |
| 145 |
if len(s) > 0 and s[-1] == '\n': |
| 146 |
s = s[:-1] |
| 147 |
return p.returncode, s |
| 148 |
except ImportError: |
| 149 |
import commands |
| 150 |
if isiterable(cmd): |
| 151 |
cmd = " ".join(cmd) |
| 152 |
return commands.getstatusoutput(cmd) |
| 153 |
|
| 154 |
def format19_10E3(x): |
| 155 |
s = "%19.10E"%x |
| 156 |
if s[-4] == 'E': |
| 157 |
s = s[1:-2] + '0' + s[-2:] |
| 158 |
return s |
| 159 |
|
| 160 |
try: |
| 161 |
import __builtin__ |
| 162 |
except ImportError: |
| 163 |
import builtins as __builtin__ |
| 164 |
|
| 165 |
try: |
| 166 |
raw_input |
| 167 |
except NameError: |
| 168 |
__builtin__.raw_input = input |
| 169 |
|
| 170 |
try: |
| 171 |
all |
| 172 |
except NameError: |
| 173 |
|
| 174 |
def all(iterable): |
| 175 |
""" |
| 176 |
Returns True if all elements of the iterable are true. |
| 177 |
""" |
| 178 |
for element in iterable: |
| 179 |
if not element: |
| 180 |
return False |
| 181 |
return True |
| 182 |
__builtin__.all = all |
| 183 |
all = all |
| 184 |
|
| 185 |
try: |
| 186 |
any |
| 187 |
except NameError: |
| 188 |
|
| 189 |
def any(iterable): |
| 190 |
""" |
| 191 |
Returns True if any element of the iterable is true. |
| 192 |
""" |
| 193 |
for element in iterable: |
| 194 |
if element: |
| 195 |
return True |
| 196 |
return False |
| 197 |
__builtin__.any = any |
| 198 |
any = any |
| 199 |
|
| 200 |
try: reversed |
| 201 |
except NameError: |
| 202 |
def reversed(seq): |
| 203 |
"""Iterate over x in reverse order. |
| 204 |
>>> list(reversed([1,2,3])) |
| 205 |
[3, 2, 1] |
| 206 |
""" |
| 207 |
if hasattr(seq, 'keys'): |
| 208 |
raise ValueError("mappings do not support reverse iteration") |
| 209 |
i = len(seq) |
| 210 |
while i > 0: |
| 211 |
i -= 1 |
| 212 |
yield seq[i] |
| 213 |
__builtin__.reversed = reversed |
| 214 |
|
| 215 |
|
| 216 |
try: sorted |
| 217 |
except NameError: |
| 218 |
import copy |
| 219 |
def sorted(seq, cmp=None, key=None, reverse=False): |
| 220 |
"""Copy seq and sort and return it. |
| 221 |
>>> sorted([3, 1, 2]) |
| 222 |
[1, 2, 3] |
| 223 |
""" |
| 224 |
seq2 = copy.copy(seq) |
| 225 |
if key: |
| 226 |
if cmp == None: |
| 227 |
cmp = __builtins__.cmp |
| 228 |
seq2.sort(lambda x,y: cmp(key(x), key(y))) |
| 229 |
else: |
| 230 |
if cmp == None: |
| 231 |
seq2.sort() |
| 232 |
else: |
| 233 |
seq2.sort(cmp) |
| 234 |
if reverse: |
| 235 |
seq2.reverse() |
| 236 |
return seq2 |
| 237 |
__builtin__.sorted = sorted |
| 238 |
|
| 239 |
try: |
| 240 |
set, frozenset |
| 241 |
except NameError: |
| 242 |
import sets |
| 243 |
set, frozenset = sets.Set, sets.ImmutableSet |
| 244 |
__builtin__.set = set |
| 245 |
__builtin__.frozenset = frozenset |
| 246 |
|
| 247 |
|
| 248 |
class array(object): |
| 249 |
def __init__(self, l, code=None, copy=True): |
| 250 |
self.base = 0 |
| 251 |
if code is None: |
| 252 |
if isinstance(l, array): |
| 253 |
code = l.typecode |
| 254 |
else: |
| 255 |
code = 'd' |
| 256 |
if isinstance(l, array): |
| 257 |
if not copy: |
| 258 |
self.base = l.base |
| 259 |
self.typecode = l.typecode |
| 260 |
self.data = l.data |
| 261 |
self.__shape = l.shape |
| 262 |
self.strides = l.strides |
| 263 |
return |
| 264 |
l = l.tolist() |
| 265 |
if (isinstance(l, list) and len(l) > 0 and |
| 266 |
isinstance(l[0], (array, list))): |
| 267 |
a2 = [] |
| 268 |
for a1 in l: |
| 269 |
if isinstance(a1, array): |
| 270 |
a1 = a1.tolist() |
| 271 |
a2.extend(a1) |
| 272 |
self.data = N.array(code, a2) |
| 273 |
self.__shape = len(l), len(l[0]) |
| 274 |
self.strides = len(l[0]), 1 |
| 275 |
else: |
| 276 |
if not isiterable(l): |
| 277 |
l=[l] |
| 278 |
self.data = N.array(code, l) |
| 279 |
self.__shape = len(self.data), |
| 280 |
self.strides = 1, |
| 281 |
self.typecode = code |
| 282 |
|
| 283 |
def __copy__(self): |
| 284 |
return array(self) |
| 285 |
|
| 286 |
copy = __copy__ |
| 287 |
|
| 288 |
def __eq__(self, other): |
| 289 |
return array([a == b for a, b in zip(self, other)], 'B') |
| 290 |
|
| 291 |
def __ne__(self, other): |
| 292 |
return array([a != b for a, b in zip(self, other)], 'B') |
| 293 |
|
| 294 |
def __gt__(self, other): |
| 295 |
return array([a > b for a, b in zip(self, other)], 'B') |
| 296 |
|
| 297 |
def __lt__(self, other): |
| 298 |
return array([a < b for a, b in zip(self, other)], 'B') |
| 299 |
|
| 300 |
def __ge__(self, other): |
| 301 |
return array([a >= b for a, b in zip(self, other)], 'B') |
| 302 |
|
| 303 |
def __le__(self, other): |
| 304 |
return array([a <= b for a, b in zip(self, other)], 'B') |
| 305 |
|
| 306 |
def __add__(self, other): |
| 307 |
return array([a + other for a in self], self.typecode) |
| 308 |
|
| 309 |
def __mul__(self, other): |
| 310 |
return array([a * other for a in self], self.typecode) |
| 311 |
|
| 312 |
def __setattr__(self, attr, value): |
| 313 |
if attr == 'shape': |
| 314 |
length = 1 |
| 315 |
for idx in self.__shape: |
| 316 |
length *= idx |
| 317 |
j = None |
| 318 |
for i, idx in enumerate(value): |
| 319 |
if idx == -1: |
| 320 |
j = i |
| 321 |
elif idx != 0: |
| 322 |
length //= idx |
| 323 |
if j is not None: |
| 324 |
value = value[:j] + (length,) + value[j+1:] |
| 325 |
self.__shape = value |
| 326 |
if len(value) == 2: |
| 327 |
self.strides = value[1], 1 |
| 328 |
else: |
| 329 |
self.strides = 1, |
| 330 |
else: |
| 331 |
object.__setattr__(self, attr, value) |
| 332 |
|
| 333 |
def __getattr__(self, attr): |
| 334 |
if attr == 'shape': |
| 335 |
return self.__shape |
| 336 |
return object.__getattribute__(self, attr) |
| 337 |
|
| 338 |
def __subarray(self, i): |
| 339 |
if not isinstance(i, tuple): |
| 340 |
i = i, |
| 341 |
if len(i) < len(self.__shape): |
| 342 |
i = i + (slice(None),) |
| 343 |
base = self.base |
| 344 |
strides = [] |
| 345 |
shape = [] |
| 346 |
for j, idx in enumerate(i): |
| 347 |
if isinstance(idx, slice): |
| 348 |
idx, stop, step = idx.indices(self.__shape[j]) |
| 349 |
strides.append(step*self.strides[j]) |
| 350 |
shape.append(abs(stop-idx+step-1)//abs(step)) |
| 351 |
else: |
| 352 |
if idx < 0: |
| 353 |
idx += self.__shape[j] |
| 354 |
if idx < 0 or idx >= self.__shape[j]: |
| 355 |
raise IndexError |
| 356 |
base += idx*self.strides[j] |
| 357 |
return base, strides, shape |
| 358 |
|
| 359 |
def __setitem__(self, i, v): |
| 360 |
base, strides, shape = self.__subarray(i) |
| 361 |
if len(strides) == 0: |
| 362 |
self.data[base] = v |
| 363 |
elif len(strides) == 1: |
| 364 |
stop = base+strides[0]*shape[0] |
| 365 |
if stop < 0: |
| 366 |
stop = None |
| 367 |
if isinstance(v,array): |
| 368 |
self.data[base:stop:strides[0]] = v._arrayslice() |
| 369 |
else: |
| 370 |
self.data[base:stop:strides[0]] = N.array(self.typecode, v) |
| 371 |
else: |
| 372 |
raise AUTOExceptions.AUTORuntimeError("2D updates only " |
| 373 |
"supported with numpy.") |
| 374 |
|
| 375 |
def __getitem__(self, i): |
| 376 |
base, strides, shape = self.__subarray(i) |
| 377 |
if len(strides) == 0: |
| 378 |
return self.data[base] |
| 379 |
new = array(self, copy=False) |
| 380 |
new.base = base |
| 381 |
new.shape = tuple(shape) |
| 382 |
new.strides = tuple(strides) |
| 383 |
return new |
| 384 |
|
| 385 |
def __len__(self): |
| 386 |
return self.shape[0] |
| 387 |
|
| 388 |
def _arrayslice(self): |
| 389 |
stop = self.base+self.shape[0]*self.strides[0] |
| 390 |
if stop < 0: |
| 391 |
stop = None |
| 392 |
return self.data[self.base:stop:self.strides[0]] |
| 393 |
|
| 394 |
def __str__(self): |
| 395 |
return array2string(self) |
| 396 |
|
| 397 |
def tolist(self): |
| 398 |
if len(self.shape) == 1: |
| 399 |
return self._arrayslice().tolist() |
| 400 |
return [e.tolist() for e in self] |
| 401 |
|
| 402 |
def rank(a): |
| 403 |
return len(a.shape) |
| 404 |
|
| 405 |
def reshape(a,shape): |
| 406 |
new = array(a, copy=False) |
| 407 |
if isinstance(shape,int): |
| 408 |
shape = shape, |
| 409 |
new.shape = shape |
| 410 |
return new |
| 411 |
|
| 412 |
def transpose(a): |
| 413 |
new = array(a, copy=False) |
| 414 |
new.shape = a.shape[::-1] |
| 415 |
new.strides = a.strides[::-1] |
| 416 |
return new |
| 417 |
|
| 418 |
def take(a, idx, axis=0): |
| 419 |
try: |
| 420 |
if axis == 1: |
| 421 |
return array([[j[i] for i in idx] for j in a]) |
| 422 |
return array([a[i] for i in idx]) |
| 423 |
except TypeError: |
| 424 |
raise IndexError |
| 425 |
|
| 426 |
def array2string(a,precision=0,format=None): |
| 427 |
indent = 1 |
| 428 |
if a.typecode == 'd' and format is None: |
| 429 |
indent = 0 |
| 430 |
absa = [abs(e) for e in ravel(a) if e != 0] |
| 431 |
if absa != []: |
| 432 |
minval = min(absa) |
| 433 |
maxval = max(absa) |
| 434 |
else: |
| 435 |
maxval = minval = 1 |
| 436 |
if minval < 0.0001 or maxval > minval * 1000: |
| 437 |
maxlen = 16 + (0 < minval < 1e-99 or maxval >= 1e100) |
| 438 |
format = "%%%s.8e"%maxlen |
| 439 |
else: |
| 440 |
precision = 0 |
| 441 |
for e in absa: |
| 442 |
s = "%.8f"%e |
| 443 |
precision = max(precision, 8 - (len(s) - len(s.rstrip("0")))) |
| 444 |
maxlen = len(str(int(maxval))) + precision + 2 |
| 445 |
format = "%%#%s.%sf"%(maxlen, precision) |
| 446 |
if len(a.shape) == 2: |
| 447 |
return "["+"\n ".join([array2string(e,format=format) |
| 448 |
for e in a])+"]" |
| 449 |
if a.typecode == 'B': |
| 450 |
return '[ '+" ".join([str(e == 1) for e in a])+']' |
| 451 |
if a.typecode == 'd': |
| 452 |
ss = [format%e for e in a] |
| 453 |
if format[-1] == 'f': |
| 454 |
for i, s in enumerate(ss): |
| 455 |
t = s.rstrip("0") |
| 456 |
ss[i] = t + (len(s)-len(t))*" " |
| 457 |
if len(ss) > 0: |
| 458 |
itemsperline = 77//(1+len(ss[0])) |
| 459 |
for i in range(itemsperline-1, len(ss)-1, itemsperline): |
| 460 |
ss[i] = ss[i]+"\n"+" "*indent |
| 461 |
return '['+" ".join(ss)+']' |
| 462 |
return '[ '+" ".join(map(str, a))+']' |
| 463 |
|
| 464 |
def shape(a): |
| 465 |
try: |
| 466 |
return a.shape |
| 467 |
except AttributeError: |
| 468 |
return array(a).shape |
| 469 |
|
| 470 |
def zeros(dim,code): |
| 471 |
if len(dim) == 1: |
| 472 |
return array(dim[0]*[0.0],code) |
| 473 |
return array([array(dim[1]*[0.0],code) for i in range(dim[0])],code) |
| 474 |
|
| 475 |
def less(a, val): |
| 476 |
return array([v < val for v in a], a.typecode) |
| 477 |
|
| 478 |
def ravel(a): |
| 479 |
if len(a.shape) == 1: |
| 480 |
return a |
| 481 |
new = array([]) |
| 482 |
new.data = a.data |
| 483 |
new.shape = a.shape[0]*a.shape[1], |
| 484 |
new.strides = a.strides[1], |
| 485 |
new.base = a.base |
| 486 |
return new |
| 487 |
|
| 488 |
ArrayType = array |
| 489 |
|
| 490 |
def test(): |
| 491 |
a=array([1,2,3,4]) |
| 492 |
print("%s"%a[::-1]) |
| 493 |
a=array([[1,2],[3,4]]) |
| 494 |
print("%s %s"%(a[:,1],a[1,:])) |
| 495 |
print("%s"%a[:,::-1]) |
| 496 |
print("%s"%a[::-1,:]) |
| 497 |
print("%s"%a[::-1]) |
| 498 |
print("%s"%a[-1,-1]) |
| 499 |
print("%s"%a[::-1,::-1]) |
| 500 |
b=array(a) |
| 501 |
b[0][0] = 4 |
| 502 |
print("%s,%s"%(a,b)) |
| 503 |
b[0,:]=[7,8] |
| 504 |
print("%s"%(b)) |
| 505 |
|
| 506 |
if __name__ == "__main__": |
| 507 |
test() |