| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import os |
| 21 |
import sys |
| 22 |
from auto import AUTOExceptions |
| 23 |
from auto import AUTOutil |
| 24 |
from auto.AUTOutil import format19_10E3 |
| 25 |
try: |
| 26 |
from UserList import UserList |
| 27 |
except ImportError: |
| 28 |
from collections import UserList |
| 29 |
from auto import Points |
| 30 |
|
| 31 |
try: |
| 32 |
next |
| 33 |
except NameError: |
| 34 |
def next(it): |
| 35 |
return it.next() |
| 36 |
|
| 37 |
type_translation_dict = { |
| 38 |
0: {"long name" : "No Label","short name" : "No Label"}, |
| 39 |
1: {"long name" : "Branch point (algebraic problem)","short name" : "BP"}, |
| 40 |
-2: {"long name" : "Cusp (algebraic problem)","short name" : "CP"}, |
| 41 |
2: {"long name" : "Fold (algebraic problem)","short name" : "LP"}, |
| 42 |
3: {"long name" : "Hopf bifurcation (algebraic problem)","short name" : "HB"}, |
| 43 |
-3: {"long name" : "Zero-Hopf bifurcation (algebraic problem)","short name" : "ZH"}, |
| 44 |
-1: {"long name" : "Bogdanov-Takens bifurcation (algebraic problem)","short name" : "BT"}, |
| 45 |
-32: {"long name" : "Generalized Hopf bifurcation (algebraic problem)","short name" : "GH"}, |
| 46 |
4: {"long name" : "Regular point (every NPR steps)","short name" : "RG"}, |
| 47 |
-4: {"long name" : "User requested point","short name" : "UZ"}, |
| 48 |
5: {"long name" : "Fold (ODE)","short name" : "LP"}, |
| 49 |
6: {"long name" : "Bifurcation point (ODE)","short name" : "BP"}, |
| 50 |
7: {"long name" : "Period doubling bifurcation (ODE)","short name" : "PD"}, |
| 51 |
8: {"long name" : "Bifurcation to invariant torus (ODE)","short name" : "TR"}, |
| 52 |
-5: {"long name" : "1:1 Resonance bifurcation (ODE)","short name" : "R1"}, |
| 53 |
-6: {"long name" : "1:2 Resonance bifurcation (ODE)","short name" : "R2"}, |
| 54 |
-7: {"long name" : "1:3 Resonance bifurcation (ODE)","short name" : "R3"}, |
| 55 |
-8: {"long name" : "1:4 Resonance bifurcation (ODE)","short name" : "R4"}, |
| 56 |
23: {"long name" : "Fold-torus bifurcation (maps)","short name" :"LTR"}, |
| 57 |
77: {"long name" : "Flip-torus bifurcation (maps)","short name" :"PTR"}, |
| 58 |
28: {"long name" : "Fold-flip bifurcation (maps)","short name" :"LPD"}, |
| 59 |
88: {"long name" : "Torus-torus bifurcation (maps)","short name" :"TTR"}, |
| 60 |
9: {"long name" : "Normal begin or end","short name" : "EP"}, |
| 61 |
-9: {"long name" : "Abnormal termination","short name" : "MX"}} |
| 62 |
|
| 63 |
all_point_types = ["No Label", |
| 64 |
"BP","LP","HB","BT","RG","UZ","PD","TR","EP","MX","ZH", |
| 65 |
"CP","GH","R1","R2","R3","R4","LPD","LTR","PTR","TTR"] |
| 66 |
|
| 67 |
def type_translation(type): |
| 68 |
"""A little dictionary to transform types to human readable strings""" |
| 69 |
if type in [28,78]: |
| 70 |
type=28 |
| 71 |
elif type in [23,83]: |
| 72 |
type=23 |
| 73 |
elif type in [77,87]: |
| 74 |
type=77 |
| 75 |
elif type not in [-32,88]: |
| 76 |
if type>=0: |
| 77 |
type=type%10 |
| 78 |
else: |
| 79 |
type=-((-type)%10) |
| 80 |
return type_translation_dict.get(type, |
| 81 |
{"long name" : "Unknown type", |
| 82 |
"short name" : "Unknown type"}) |
| 83 |
|
| 84 |
def reverse_type_translation(type): |
| 85 |
"""A little dictionary to transform human readable strings to types""" |
| 86 |
for k,v in type_translation_dict.items(): |
| 87 |
if v["short name"] == type or v["long name"] == type: |
| 88 |
return k |
| 89 |
return type |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
class parseBMixin(object): |
| 106 |
def __str__(self): |
| 107 |
return self.summary() |
| 108 |
|
| 109 |
def __getitem__(self,index): |
| 110 |
return self.getIndex(index) |
| 111 |
|
| 112 |
def __call__(self,label=None): |
| 113 |
return self.getLabel(label) |
| 114 |
|
| 115 |
def dsp(self,label=None): |
| 116 |
"""Removes solutions with the given labels or type names""" |
| 117 |
return self.deleteLabel(label,copy=1) |
| 118 |
|
| 119 |
def ksp(self,label=None): |
| 120 |
"""Keeps solutions with the given labels or type names""" |
| 121 |
return self.deleteLabel(label,keep=1,copy=1) |
| 122 |
|
| 123 |
def dlb(self,label=None): |
| 124 |
"""Removes solutions with the given labels or type names""" |
| 125 |
return self.deleteLabel(label,keepTY=1,copy=1) |
| 126 |
|
| 127 |
def klb(self,label=None): |
| 128 |
"""Keeps solutions with the given labels or type names""" |
| 129 |
return self.deleteLabel(label,keepTY=1,keep=1,copy=1) |
| 130 |
|
| 131 |
def readFilename(self,filename): |
| 132 |
inputfile = AUTOutil.openFilename(filename,"r") |
| 133 |
self.read(inputfile) |
| 134 |
inputfile.close() |
| 135 |
|
| 136 |
def writeFilename(self,filename,append=False): |
| 137 |
if append: |
| 138 |
output = open(filename,"a") |
| 139 |
else: |
| 140 |
output = open(filename,"w") |
| 141 |
self.write(output) |
| 142 |
output.close() |
| 143 |
|
| 144 |
def writeRawFilename(self,filename): |
| 145 |
output = open(filename,"w") |
| 146 |
self.writeRaw(output) |
| 147 |
output.flush() |
| 148 |
output.close() |
| 149 |
|
| 150 |
def writeScreen(self): |
| 151 |
sys.stdout.write(self.summary()) |
| 152 |
|
| 153 |
|
| 154 |
class BDPointData(UserList): |
| 155 |
def __init__(self, branch=None, idx=None): |
| 156 |
self.branch = branch |
| 157 |
self.idx = idx |
| 158 |
def __getattr__(self, attr): |
| 159 |
if attr == 'data': |
| 160 |
return [d[self.idx] for d in self.branch.coordarray] |
| 161 |
return super(BDPointData, self).__getattribute__(attr) |
| 162 |
def __setitem__(self, i, item): |
| 163 |
self.branch.coordarray[i][self.idx] = item |
| 164 |
def __str__(self): |
| 165 |
return str(self.data) |
| 166 |
class BDPoint(Points.Point): |
| 167 |
def __init__(self, p, branch=None, idx=None, pt=None): |
| 168 |
if isinstance(p['coordarray'], list): |
| 169 |
|
| 170 |
self.__fullyParsed = False |
| 171 |
self.p = p |
| 172 |
self.coordnames = p['coordnames'] |
| 173 |
self.name_map = dict(zip(p['coordnames'], |
| 174 |
range(len(p['coordnames'])))) |
| 175 |
else: |
| 176 |
self.__fullyParsed = True |
| 177 |
Points.Point.__init__(self, p) |
| 178 |
self.idx = idx |
| 179 |
self.pt = pt |
| 180 |
self.branch = branch |
| 181 |
self.section = 0 |
| 182 |
self.index = idx |
| 183 |
|
| 184 |
def __getattr__(self, attr): |
| 185 |
if not self.__fullyParsed: |
| 186 |
p = self.p |
| 187 |
p['coordtype'] = type(p['coordarray'][0]) |
| 188 |
del self.name_map, self.p |
| 189 |
Points.Point.__init__(self, p) |
| 190 |
self.__fullyParsed = True |
| 191 |
return super(BDPoint, self).__getattribute__(attr) |
| 192 |
|
| 193 |
def __contains__(self, key): |
| 194 |
if (key in ["TY","TY name","data","BR","PT","section","index"] or |
| 195 |
Points.Point.has_key(self,key)): |
| 196 |
return True |
| 197 |
for k,v in self.labels.items(): |
| 198 |
if k in all_point_types: |
| 199 |
return key in v |
| 200 |
return False |
| 201 |
|
| 202 |
def has_key(self, key): |
| 203 |
return self.__contains__(key) |
| 204 |
|
| 205 |
def __setitem__(self, ixarg, val): |
| 206 |
"""Change coordinate array values.""" |
| 207 |
if type(ixarg) == type(""): |
| 208 |
if ixarg == "BR": |
| 209 |
self.branch["BR"] = val |
| 210 |
return |
| 211 |
elif ixarg == "PT": |
| 212 |
if not self.__fullyParsed: |
| 213 |
self.pt = val |
| 214 |
|
| 215 |
return |
| 216 |
elif ixarg == "section": |
| 217 |
self.section = val |
| 218 |
return |
| 219 |
elif ixarg == "index": |
| 220 |
self.index = val |
| 221 |
return |
| 222 |
for k,v in self.labels.items(): |
| 223 |
if "LAB" in v: |
| 224 |
v[ixarg] = val |
| 225 |
return |
| 226 |
Points.Point.__setitem__(self, ixarg, val) |
| 227 |
|
| 228 |
def __getitem__(self, coords): |
| 229 |
if type(coords) == type(""): |
| 230 |
if not self.__fullyParsed: |
| 231 |
try: |
| 232 |
return self.p['coordarray'][self.name_map[coords]] |
| 233 |
except KeyError: |
| 234 |
pass |
| 235 |
if coords == "BR": |
| 236 |
return self.branch["BR"] |
| 237 |
elif coords == "PT": |
| 238 |
if self.__fullyParsed: |
| 239 |
pt = self.idx+1 |
| 240 |
for p in self.branch.stability(): |
| 241 |
if abs(p) >= pt: |
| 242 |
if p < 0: |
| 243 |
pt = -pt |
| 244 |
break |
| 245 |
if pt < 0: |
| 246 |
pt = -((-pt-1) % 9999) - 1 |
| 247 |
else: |
| 248 |
pt = ((pt-1) % 9999) + 1 |
| 249 |
else: |
| 250 |
pt = self.pt |
| 251 |
return pt |
| 252 |
elif coords == "section": |
| 253 |
return self.section |
| 254 |
elif coords == "index": |
| 255 |
return self.index |
| 256 |
for k,v in self.labels.items(): |
| 257 |
if k in all_point_types: |
| 258 |
if coords in v: |
| 259 |
return v[coords] |
| 260 |
elif coords in ("TY name", "TY"): |
| 261 |
return k |
| 262 |
if coords == "data": |
| 263 |
return BDPointData(self.branch, self.idx) |
| 264 |
return Points.Point.__getitem__(self, coords) |
| 265 |
|
| 266 |
def __str__(self): |
| 267 |
for k,v in self.labels.items(): |
| 268 |
if k in all_point_types: |
| 269 |
ty_name = k |
| 270 |
label = v |
| 271 |
break |
| 272 |
return str({"BR": self["BR"], |
| 273 |
"PT": self["PT"], |
| 274 |
"TY number": label["TY number"], |
| 275 |
"TY name": ty_name, |
| 276 |
"LAB": label["LAB"], |
| 277 |
"data": list(self.coordarray), |
| 278 |
"section": self.section, |
| 279 |
"index": self.index}) |
| 280 |
|
| 281 |
__repr__ = __str__ |
| 282 |
|
| 283 |
|
| 284 |
class AUTOBranch(parseBMixin, Points.Pointset): |
| 285 |
def __init__(self,input=None,prevline=None,coordnames=[]): |
| 286 |
self.__fullyParsed = True |
| 287 |
if isinstance(input,AUTOBranch): |
| 288 |
for k,v in input.__dict__.items(): |
| 289 |
self.__dict__[k] = v |
| 290 |
elif input is not None: |
| 291 |
self.read(input,prevline) |
| 292 |
self.__fullyParsed = False |
| 293 |
|
| 294 |
def __getattr__(self,attr): |
| 295 |
if self.__fullyParsed or attr == "__del__": |
| 296 |
raise AttributeError |
| 297 |
self.__parse() |
| 298 |
return super(AUTOBranch, self).__getattribute__(attr) |
| 299 |
|
| 300 |
def _gettypelabel(self,idx): |
| 301 |
for k,v in self.labels[idx].items(): |
| 302 |
if "LAB" in v: |
| 303 |
return k,v |
| 304 |
return 'No Label', {} |
| 305 |
|
| 306 |
def __parse(self): |
| 307 |
if self.__fullyParsed: |
| 308 |
return |
| 309 |
global N |
| 310 |
if not Points.numpyimported: |
| 311 |
Points.importnumpy() |
| 312 |
self.__fullyParsed = True |
| 313 |
fromstring = Points.fromstring |
| 314 |
N = Points.N |
| 315 |
datalist = self.__datalist |
| 316 |
del self.__datalist |
| 317 |
line0 = datalist[0].split() |
| 318 |
self.BR = int(line0[0]) |
| 319 |
ncolumns = len(line0) |
| 320 |
nrows = len(datalist) |
| 321 |
datalist = "".join(datalist) |
| 322 |
if fromstring: |
| 323 |
data = [] |
| 324 |
if "D" not in datalist: |
| 325 |
data = fromstring(datalist, dtype=float, sep=' ') |
| 326 |
if len(data) != nrows * ncolumns: |
| 327 |
data = N.array(map(AUTOatof,datalist.split()), 'd') |
| 328 |
else: |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
data[-1] = AUTOatof(datalist[datalist.rfind(' ')+1:].strip()) |
| 333 |
else: |
| 334 |
datalist = datalist.split() |
| 335 |
try: |
| 336 |
data = N.array(map(float, datalist), 'd') |
| 337 |
except ValueError: |
| 338 |
data = N.array(map(AUTOatof, datalist), 'd') |
| 339 |
data.shape = (-1,ncolumns) |
| 340 |
coordarray = N.transpose(data[:,4:]).copy() |
| 341 |
points = data[:,1] |
| 342 |
if hasattr(N,"concatenate"): |
| 343 |
stability = self.__parsenumpy(points) |
| 344 |
else: |
| 345 |
stability = self.__parsearray(points) |
| 346 |
|
| 347 |
branchtype = type_translation(self.TY)["short name"] |
| 348 |
for i in stability: |
| 349 |
if i < 0: |
| 350 |
stab = "S" |
| 351 |
else: |
| 352 |
stab = "U" |
| 353 |
self.labels.update(abs(i)-1, branchtype, {"stab": stab}) |
| 354 |
|
| 355 |
for i in range(len(self.coordnames)): |
| 356 |
name = self.coordnames[i] |
| 357 |
if self.coordnames.count(name) > 1: |
| 358 |
for j in range(i+1,len(self.coordnames)): |
| 359 |
if self.coordnames[j] == name: |
| 360 |
self.coordnames[j] = name + ' ' |
| 361 |
Points.Pointset.__init__(self,{ |
| 362 |
"coordarray": coordarray, |
| 363 |
"coordnames": self.coordnames, |
| 364 |
"labels": self.labels, |
| 365 |
}) |
| 366 |
|
| 367 |
def __parsenumpy(self,points): |
| 368 |
global N |
| 369 |
|
| 370 |
|
| 371 |
stab = N.concatenate((N.flatnonzero(N.less(points[:-1]*points[1:],0)), |
| 372 |
[len(points)-1])) |
| 373 |
points = N.less(N.take(points,stab),0) |
| 374 |
stab = stab + 1 |
| 375 |
stability = N.where(points,-stab,stab) |
| 376 |
return stability |
| 377 |
|
| 378 |
def __parsearray(self,points): |
| 379 |
global N |
| 380 |
|
| 381 |
stability = [] |
| 382 |
prevpt = points[0] |
| 383 |
for p, pt in enumerate(points): |
| 384 |
if pt * prevpt < 0: |
| 385 |
if prevpt < 0: |
| 386 |
p = -p |
| 387 |
stability.append(p) |
| 388 |
prevpt = pt |
| 389 |
p = len(points) |
| 390 |
if pt < 0: |
| 391 |
p = -p |
| 392 |
stability.append(p) |
| 393 |
return stability |
| 394 |
|
| 395 |
def __setitem__(self, item, value): |
| 396 |
if item in ("BR", "TY", "TY number") and item not in self.coordnames: |
| 397 |
if item == "BR": |
| 398 |
br = self["BR"] |
| 399 |
if value != br: |
| 400 |
self.__parse() |
| 401 |
self.BR = value |
| 402 |
else: |
| 403 |
if item == "TY": |
| 404 |
value = reverse_type_translation(value) |
| 405 |
self.TY = value |
| 406 |
|
| 407 |
for k,x in map(self._gettypelabel, self.labels.getIndices()): |
| 408 |
v = x["TY number"] |
| 409 |
if v>=0: |
| 410 |
v=v%10 |
| 411 |
else: |
| 412 |
v=-((-v)%10) |
| 413 |
x["TY number"] = value*10 + v |
| 414 |
else: |
| 415 |
Points.Pointset.__setitem__(self, item, value) |
| 416 |
|
| 417 |
def __len__(self): |
| 418 |
if not self.__fullyParsed: |
| 419 |
return len(self.__datalist) |
| 420 |
if len(self.coordarray) == 0: |
| 421 |
return 0 |
| 422 |
return Points.Pointset.__len__(self) |
| 423 |
|
| 424 |
def deleteLabel(self,label=None,keepTY=0,keep=0,copy=0): |
| 425 |
"""Removes solutions with the given labels or type names""" |
| 426 |
if label is None: |
| 427 |
label = [lab for lab in all_point_types if lab not in |
| 428 |
["No Label", "RG", "UZ"]] |
| 429 |
if isinstance(label, (str, int)): |
| 430 |
label = [label] |
| 431 |
if copy: |
| 432 |
new = self.__class__(self) |
| 433 |
new.labels = Points.PointInfo(self.labels.by_index.copy()) |
| 434 |
if not self.__fullyParsed: |
| 435 |
new.__datalist = self.__datalist[:] |
| 436 |
else: |
| 437 |
new = self |
| 438 |
for idx in new.labels.getIndices(): |
| 439 |
ty_name,v = new._gettypelabel(idx) |
| 440 |
if "LAB" not in v: |
| 441 |
continue |
| 442 |
if ((not keep and (v["LAB"] in label or ty_name in label)) or |
| 443 |
(keep and not v["LAB"] in label and not ty_name in label)): |
| 444 |
if copy: |
| 445 |
new.labels[idx][ty_name] = v.copy() |
| 446 |
new.labels[idx][ty_name]["LAB"] = 0 |
| 447 |
if not keepTY: |
| 448 |
new.labels[idx][ty_name]["TY number"] = 0 |
| 449 |
if not new.__fullyParsed: |
| 450 |
new.__patchline(new.__datalist,idx,3,0) |
| 451 |
if not keepTY: |
| 452 |
new.__patchline(new.__datalist,idx,2,0) |
| 453 |
if v["TY number"] == 0: |
| 454 |
new.labels.remove(idx) |
| 455 |
if copy: |
| 456 |
return new |
| 457 |
|
| 458 |
def relabel(self,old_label=1,new_label=None): |
| 459 |
"""Relabels the first solution with the given label""" |
| 460 |
if new_label is None: |
| 461 |
label = old_label |
| 462 |
new = self.__class__(self) |
| 463 |
labels = {} |
| 464 |
if not self.__fullyParsed: |
| 465 |
new.__datalist = self.__datalist[:] |
| 466 |
for index in self.labels.getIndices(): |
| 467 |
labels[index] = self.labels[index].copy() |
| 468 |
ty_name,v = self._gettypelabel(index) |
| 469 |
if v.get("LAB",0) != 0: |
| 470 |
labels[index][ty_name] = v.copy() |
| 471 |
labels[index][ty_name]["LAB"] = label |
| 472 |
if not self.__fullyParsed: |
| 473 |
self.__patchline(new.__datalist,index,3,label) |
| 474 |
label = label + 1 |
| 475 |
new.labels = Points.PointInfo(labels) |
| 476 |
return new |
| 477 |
labels = self.labels |
| 478 |
if isinstance(old_label, int): |
| 479 |
old_label = [old_label] |
| 480 |
new_label = [new_label] |
| 481 |
for j in range(len(old_label)): |
| 482 |
for index in self.labels.getIndices(): |
| 483 |
v = self._gettypelabel(index)[1] |
| 484 |
if "LAB" not in v: |
| 485 |
continue |
| 486 |
if v["LAB"] == old_label[j]: |
| 487 |
v["LAB"] = new_label[j] |
| 488 |
if not self.__fullyParsed: |
| 489 |
self.__patchline(self.__datalist,index,3,new_label[j]) |
| 490 |
|
| 491 |
def uniquelyLabel(self,label=1): |
| 492 |
"""Make all labels in the file unique and sequential""" |
| 493 |
for index in self.labels.getIndices(): |
| 494 |
v = self._gettypelabel(index)[1] |
| 495 |
if v.get("LAB",0) != 0: |
| 496 |
v["LAB"] = label |
| 497 |
if not self.__fullyParsed: |
| 498 |
self.__patchline(self.__datalist,index,3,label) |
| 499 |
label = label + 1 |
| 500 |
|
| 501 |
def getLabel(self,label): |
| 502 |
"""Given a label, return the correct solution""" |
| 503 |
if label is None: |
| 504 |
return self |
| 505 |
if isinstance(label, int): |
| 506 |
for k in self.labels.getIndices(): |
| 507 |
v = self._gettypelabel(k)[1] |
| 508 |
if v.get("LAB",0) == label: |
| 509 |
return self.getIndex(k) |
| 510 |
raise KeyError("Label %s not found"%label) |
| 511 |
if isinstance(label, str) and len(label) > 2 and label[-1].isdigit(): |
| 512 |
j = 2 |
| 513 |
if not label[2].isdigit(): |
| 514 |
j = 3 |
| 515 |
number = int(label[j:]) |
| 516 |
i = 0 |
| 517 |
for k,v in self.labels.sortByIndex(): |
| 518 |
if label[:j] in v: |
| 519 |
i = i + 1 |
| 520 |
if i == number: |
| 521 |
return self.getIndex(k) |
| 522 |
raise KeyError("Label %s not found"%label) |
| 523 |
if not AUTOutil.isiterable(label): |
| 524 |
label = [label] |
| 525 |
labels = {} |
| 526 |
counts = [0]*len(label) |
| 527 |
for k,val in self.labels.sortByIndex(): |
| 528 |
ty_name,v = self._gettypelabel(k) |
| 529 |
if "LAB" not in v: |
| 530 |
continue |
| 531 |
for i in range(len(label)): |
| 532 |
lab = label[i] |
| 533 |
j = 2 |
| 534 |
if len(lab) > 2 and not lab[2].isdigit(): |
| 535 |
j = 3 |
| 536 |
if (isinstance(lab, str) and len(lab) > j and |
| 537 |
ty_name == lab[:j]): |
| 538 |
counts[i] = counts[i] + 1 |
| 539 |
if counts[i] == int(lab[j:]): |
| 540 |
labels[k] = val |
| 541 |
if v["LAB"] in label or ty_name in label: |
| 542 |
labels[k] = val |
| 543 |
continue |
| 544 |
new = self.__class__(self) |
| 545 |
new.labels = Points.PointInfo(labels) |
| 546 |
return new |
| 547 |
|
| 548 |
def getIndex(self,index): |
| 549 |
"""Return a parseB style line item; if given a string, return the |
| 550 |
relevant column""" |
| 551 |
if isinstance(index,tuple) and len(index)==2 and not self.__fullyParsed: |
| 552 |
|
| 553 |
i, j = index |
| 554 |
if isinstance(j, str): |
| 555 |
j = self.coordnames.index(j) |
| 556 |
return AUTOatof(self.__datalist[i].split()[4+j]) |
| 557 |
if index in ("BR", "TY", "TY number") and index not in self.coordnames: |
| 558 |
if index == "BR": |
| 559 |
if self.__fullyParsed: |
| 560 |
return self.BR |
| 561 |
else: |
| 562 |
return int(self.__datalist[0].split(None,1)[0]) |
| 563 |
elif index == "TY": |
| 564 |
return type_translation(self.TY)["short name"] |
| 565 |
else: |
| 566 |
return self.TY |
| 567 |
if isinstance(index, int) and index < 0: |
| 568 |
index = len(self) + index |
| 569 |
if self.__fullyParsed or not isinstance(index, int): |
| 570 |
if not Points.numpyimported: |
| 571 |
Points.importnumpy() |
| 572 |
ret = Points.Pointset.__getitem__(self,index) |
| 573 |
if isinstance(ret, Points.Pointset): |
| 574 |
r = self.__class__(self) |
| 575 |
r.coordarray = ret.coordarray |
| 576 |
r.labels = ret.labels |
| 577 |
|
| 578 |
if isinstance(index, slice): |
| 579 |
prev = 0 |
| 580 |
stability = [] |
| 581 |
for p in self.stability(): |
| 582 |
if p < 0: |
| 583 |
stab = "S" |
| 584 |
else: |
| 585 |
stab = "U" |
| 586 |
stability.append((abs(p)-prev)*stab) |
| 587 |
prev = abs(p) |
| 588 |
stability = "".join(stability)[index] |
| 589 |
branchtype = type_translation(self.TY)["short name"] |
| 590 |
prev = stability[0] |
| 591 |
for i, stab in enumerate(stability): |
| 592 |
if stab != prev: |
| 593 |
r.labels.update(i-1, branchtype, {"stab": prev}) |
| 594 |
prev = stab |
| 595 |
r.labels.update(len(r)-1, branchtype,{"stab":stability[-1]}) |
| 596 |
return r |
| 597 |
if not isinstance(ret, Points.Point): |
| 598 |
return ret |
| 599 |
labels = ret.labels |
| 600 |
coordnames = ret.coordnames |
| 601 |
coordarray = ret.coordarray |
| 602 |
pt = None |
| 603 |
else: |
| 604 |
labels = self.labels[index] |
| 605 |
coordnames = self.coordnames |
| 606 |
coordarray = self.__datalist[index].split() |
| 607 |
pt = int(coordarray[1]) |
| 608 |
coordarray = list(map(AUTOatof, coordarray[4:])) |
| 609 |
label = None |
| 610 |
for v in labels.values(): |
| 611 |
if "LAB" in v: |
| 612 |
label = v |
| 613 |
break |
| 614 |
if label is None: |
| 615 |
label = {"TY number": 0, "LAB": 0} |
| 616 |
if "No Label" in labels: |
| 617 |
labels["No Label"].update(label) |
| 618 |
else: |
| 619 |
labels["No Label"] = label |
| 620 |
return BDPoint({'coordarray': coordarray, |
| 621 |
'coordnames': coordnames, |
| 622 |
'labels': labels},self,index) |
| 623 |
|
| 624 |
def getLabels(self): |
| 625 |
"""Get all the labels from the solution""" |
| 626 |
labels = [] |
| 627 |
for index in self.labels.getIndices(): |
| 628 |
x = self._gettypelabel(index)[1] |
| 629 |
if x.get("LAB",0) != 0: |
| 630 |
labels.append(x["LAB"]) |
| 631 |
return labels |
| 632 |
|
| 633 |
def stability(self): |
| 634 |
"""Returns a list of point numbers where the stability |
| 635 |
changes: the end point of each part is stored.""" |
| 636 |
if not self.__fullyParsed: |
| 637 |
self.__parse() |
| 638 |
stab = [] |
| 639 |
prevpt = 0 |
| 640 |
branchtype = type_translation(self.TY)["short name"] |
| 641 |
for idx,val in self.labels.sortByIndex(): |
| 642 |
if branchtype in val and "stab" in val[branchtype]: |
| 643 |
if val[branchtype]["stab"] == "U": |
| 644 |
pt = idx+1 |
| 645 |
else: |
| 646 |
pt = -idx-1 |
| 647 |
if pt * prevpt <= 0: |
| 648 |
stab.append(pt) |
| 649 |
else: |
| 650 |
stab[-1] = pt |
| 651 |
prevpt = pt |
| 652 |
return stab |
| 653 |
|
| 654 |
def subtract(self,other,ref,pt=None): |
| 655 |
"""Subtracts branch branches using interpolation with respect to other |
| 656 |
with monotonically increasing or decreasing reference coordinate ref, |
| 657 |
and starting point pt""" |
| 658 |
global N |
| 659 |
if not Points.numpyimported: |
| 660 |
Points.importnumpy() |
| 661 |
N = Points.N |
| 662 |
if pt is None: |
| 663 |
index = 0 |
| 664 |
elif type(pt) == type(1): |
| 665 |
index = abs(pt) - 1 |
| 666 |
else: |
| 667 |
index = pt["index"] |
| 668 |
coordarray = N.array(self.coordarray) |
| 669 |
if not isinstance(other,Points.Pointset): |
| 670 |
other = other[0] |
| 671 |
b0 = other[ref] |
| 672 |
if b0[index] > b0[index+1]: |
| 673 |
|
| 674 |
k = index+1 |
| 675 |
while k < len(b0) and b0[k] <= b0[k-1]: |
| 676 |
k = k + 1 |
| 677 |
b0 = b0[:k] |
| 678 |
else: |
| 679 |
k = index+1 |
| 680 |
while k < len(b0) and b0[k] >= b0[k-1]: |
| 681 |
k = k + 1 |
| 682 |
b0 = b0[:k] |
| 683 |
r = 0 |
| 684 |
for i in range(len(other.coordnames)): |
| 685 |
if other.coordnames[i] == ref: |
| 686 |
r = i |
| 687 |
a0 = self.coordarray[r] |
| 688 |
k = index+1 |
| 689 |
for j in range(len(a0)): |
| 690 |
if b0[index+1] > b0[index]: |
| 691 |
|
| 692 |
if a0[j] > b0[k]: |
| 693 |
while k < len(b0)-1 and a0[j] > b0[k]: |
| 694 |
k = k + 1 |
| 695 |
elif b0[k-1] >= a0[j]: |
| 696 |
while k > index+1 and b0[k-1] >= a0[j]: |
| 697 |
k = k - 1 |
| 698 |
else: |
| 699 |
|
| 700 |
if a0[j] < b0[k]: |
| 701 |
while k < len(b0)-1 and a0[j] < b0[k]: |
| 702 |
k = k + 1 |
| 703 |
elif b0[k-1] <= a0[j]: |
| 704 |
while k > index+1 and b0[k-1] <= a0[j]: |
| 705 |
k = k - 1 |
| 706 |
|
| 707 |
for i in range(len(self.coordnames)): |
| 708 |
if i == r or i >= len(other.coordarray): |
| 709 |
continue |
| 710 |
a = coordarray[i] |
| 711 |
b = other.coordarray[i] |
| 712 |
a[j]=a[j]-b[k-1]-(a0[j]-b0[k-1])*(b[k]-b[k-1])/(b0[k]-b0[k-1]) |
| 713 |
new = self.__class__(self) |
| 714 |
Points.Pointset.__init__(new, coordarray = coordarray, |
| 715 |
coordnames = self.coordnames, |
| 716 |
labels = self.labels) |
| 717 |
return new |
| 718 |
|
| 719 |
def toArray(self): |
| 720 |
return list(map(list,zip(*self.coordarray))) |
| 721 |
|
| 722 |
def writeRaw(self,output): |
| 723 |
data = self.toArray() |
| 724 |
output.write("\n".join(["".join(["%24.15E"%v for v in d]) for d in data])+"\n") |
| 725 |
|
| 726 |
def write(self, output, columnlen=19): |
| 727 |
if columnlen == 19 and not self.__fullyParsed: |
| 728 |
output.writelines(self.headerlist) |
| 729 |
output.writelines(self.__datalist) |
| 730 |
return |
| 731 |
format = "%"+str(-columnlen)+"s" |
| 732 |
if self.headerlist != []: |
| 733 |
for l in self.headerlist: |
| 734 |
if " PT " not in l: |
| 735 |
output.write(l) |
| 736 |
if self.headernames != []: |
| 737 |
output_line = [" 0 PT TY LAB "] |
| 738 |
for name in self.headernames: |
| 739 |
output_line.append(format%name) |
| 740 |
output.writelines(output_line) |
| 741 |
output.write('\n') |
| 742 |
br = self.BR |
| 743 |
data = self.coordarray |
| 744 |
istab = 0 |
| 745 |
format = "%"+str(columnlen)+"."+str(columnlen-9)+"E" |
| 746 |
stability = self.stability() |
| 747 |
for i in range(len(data[0])): |
| 748 |
pt = i+1 |
| 749 |
if stability[istab] < 0: |
| 750 |
pt = -pt |
| 751 |
tynumber = 0 |
| 752 |
lab = 0 |
| 753 |
if i in self.labels.by_index: |
| 754 |
for k,label in self.labels[i].items(): |
| 755 |
if "LAB" in label: |
| 756 |
tynumber = label["TY number"] |
| 757 |
lab = label["LAB"] |
| 758 |
break |
| 759 |
if pt == stability[istab]: |
| 760 |
istab = istab + 1 |
| 761 |
if pt < 0: |
| 762 |
pt = -((-pt-1) % 9999) - 1 |
| 763 |
else: |
| 764 |
pt = ((pt-1) % 9999) + 1 |
| 765 |
output_line = "%4d%6d%4d%5d"%(br,pt,tynumber,lab) |
| 766 |
if columnlen == 19: |
| 767 |
for j in range(len(data)): |
| 768 |
output_line = output_line + format19_10E3(data[j,i]) |
| 769 |
else: |
| 770 |
for j in range(len(data)): |
| 771 |
output_line = output_line + format%data[j,i] |
| 772 |
output.write(output_line+"\n") |
| 773 |
|
| 774 |
def writeShort(self): |
| 775 |
self.write(sys.stdout, columnlen=14) |
| 776 |
|
| 777 |
def summary(self): |
| 778 |
slist = [] |
| 779 |
if self.__fullyParsed: |
| 780 |
data = self.coordarray |
| 781 |
first = True |
| 782 |
for index,l in self.labels.sortByIndex(): |
| 783 |
label = {} |
| 784 |
for k,v in l.items(): |
| 785 |
if "LAB" in v: |
| 786 |
label = v |
| 787 |
break |
| 788 |
ty_number = label.get("TY number",0) |
| 789 |
if ty_number == 0 or label["LAB"] == 0: |
| 790 |
continue |
| 791 |
if first: |
| 792 |
slist.append("".join( |
| 793 |
["\n BR PT TY LAB "] + |
| 794 |
["%-14s"%name for name in self.headernames])) |
| 795 |
first = False |
| 796 |
ty_name = type_translation(ty_number)["short name"] |
| 797 |
if ty_name=='RG': |
| 798 |
ty_name = ' ' |
| 799 |
|
| 800 |
if self.__fullyParsed: |
| 801 |
linelist = (["%4d%6d%4s%5d"%(abs(self.BR),(index % 9999) + 1, |
| 802 |
ty_name,label["LAB"])]+ |
| 803 |
["%14.5E"%d[index] for d in data]) |
| 804 |
else: |
| 805 |
linedata = self.__datalist[index].split() |
| 806 |
linelist = ( |
| 807 |
["%4d%6d%4s%5d"%(abs(int(linedata[0])),abs(int(linedata[1])), |
| 808 |
ty_name,label["LAB"])] + |
| 809 |
["%14.5E"%AUTOatof(d) for d in linedata[4:]]) |
| 810 |
slist.append("".join(linelist)) |
| 811 |
return "\n".join(slist) |
| 812 |
|
| 813 |
def __patchline(self,datalist,lineno,column,new): |
| 814 |
|
| 815 |
newsp = 0 |
| 816 |
line = datalist[lineno] |
| 817 |
l = len(line) |
| 818 |
for i in range(column+1): |
| 819 |
oldsp = newsp |
| 820 |
start = l - len(line[oldsp:].lstrip()) |
| 821 |
newsp = line.find(' ',start) |
| 822 |
datalist[lineno] = (line[:oldsp]+ |
| 823 |
'%'+str(newsp-oldsp)+'d'+line[newsp:])%new |
| 824 |
|
| 825 |
def __checknorotate(self,datalist): |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
prevpt = int(datalist[-2].split(None,2)[1]) |
| 831 |
if prevpt not in [9999,-9999,9997,-9997,0]: |
| 832 |
return True |
| 833 |
|
| 834 |
if prevpt in [-9997,9997]: |
| 835 |
self.__patchline(datalist,-1,1,-9998) |
| 836 |
elif prevpt == 0: |
| 837 |
self.__patchline(datalist,-2,1,-9999) |
| 838 |
return False |
| 839 |
|
| 840 |
def read(self,inputfile,prevline=None): |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
self._lastline = None |
| 857 |
if hasattr(str,"split"): |
| 858 |
split = str.split |
| 859 |
else: |
| 860 |
import string |
| 861 |
split = string.split |
| 862 |
if prevline: |
| 863 |
line = prevline |
| 864 |
else: |
| 865 |
try: |
| 866 |
line = next(inputfile) |
| 867 |
except StopIteration: |
| 868 |
self.labels = None |
| 869 |
return |
| 870 |
headerlist = [] |
| 871 |
columns = split(line,None,2) |
| 872 |
if columns[0] == '0': |
| 873 |
headerlist.append(line) |
| 874 |
for line in inputfile: |
| 875 |
columns = split(line,None,2) |
| 876 |
if columns[0] != '0': |
| 877 |
self._lastline = line |
| 878 |
break |
| 879 |
headerlist.append(line) |
| 880 |
datalist = [] |
| 881 |
labels = {} |
| 882 |
if columns[0] != '0': |
| 883 |
self._lastline = None |
| 884 |
datalist = [line] |
| 885 |
if columns[2][0] != '0': |
| 886 |
columns = split(line,None,4) |
| 887 |
pt = int(columns[1]) |
| 888 |
if pt < 0: |
| 889 |
stab = 'S' |
| 890 |
else: |
| 891 |
stab = 'U' |
| 892 |
ty = int(columns[2]) |
| 893 |
lab = int(columns[3]) |
| 894 |
key = type_translation(ty)["short name"] |
| 895 |
labels[len(datalist)-1] = {key: {"LAB":lab,"TY number":ty}} |
| 896 |
for line in inputfile: |
| 897 |
datalist.append(line) |
| 898 |
columns = split(line,None,2) |
| 899 |
if (columns[0] == '0' or |
| 900 |
((columns[1] == '-1' or columns[1] == '1') and |
| 901 |
self.__checknorotate(datalist))): |
| 902 |
self._lastline = datalist.pop() |
| 903 |
break |
| 904 |
if columns[2][0] != '0': |
| 905 |
columns = split(datalist[-1],None,4) |
| 906 |
pt = int(columns[1]) |
| 907 |
ty = int(columns[2]) |
| 908 |
lab = int(columns[3]) |
| 909 |
key = type_translation(ty)["short name"] |
| 910 |
labels[len(datalist)-1] = {key: {"LAB":lab,"TY number":ty}} |
| 911 |
self.labels = Points.PointInfo(labels) |
| 912 |
self.__datalist = datalist |
| 913 |
self.c = self.parseHeader(headerlist) |
| 914 |
|
| 915 |
def parseHeader(self,headerlist): |
| 916 |
from auto import parseC |
| 917 |
self.headerlist = headerlist |
| 918 |
if hasattr(str,"split"): |
| 919 |
split = str.split |
| 920 |
else: |
| 921 |
import string |
| 922 |
split = string.split |
| 923 |
line = "" |
| 924 |
if headerlist != []: |
| 925 |
line = headerlist[-1] |
| 926 |
ncolumns = len(split(self.__datalist[0])) - 4 |
| 927 |
self.headernames = [] |
| 928 |
self.coordnames = [] |
| 929 |
self.TY = 0 |
| 930 |
if " PT " in line: |
| 931 |
linelen = len(self.__datalist[0]) |
| 932 |
columnlen = (linelen - 19) // ncolumns |
| 933 |
n = linelen - columnlen * ncolumns |
| 934 |
for i in range(ncolumns): |
| 935 |
self.headernames.append(line[n:n+columnlen].rstrip()) |
| 936 |
self.coordnames.append(line[n:n+columnlen].strip()) |
| 937 |
n = n + columnlen |
| 938 |
if self.coordnames == []: |
| 939 |
self.coordnames = ["COLUMN(%d)"%(i+1) for i in range(ncolumns)] |
| 940 |
dict = parseC.parseC() |
| 941 |
i = 0 |
| 942 |
if len(headerlist) == 0: |
| 943 |
return |
| 944 |
words = split(headerlist[0]) |
| 945 |
if len(words) < 5: |
| 946 |
return |
| 947 |
for key in ["RL0","RL1","A0","A1"]: |
| 948 |
i = i + 1 |
| 949 |
dict[key] = AUTOatof(words[i]) |
| 950 |
key = "" |
| 951 |
userspec = False |
| 952 |
for line in headerlist[1:]: |
| 953 |
words = split(line) |
| 954 |
if len(words) < 2: |
| 955 |
break |
| 956 |
if words[-1] in ["constants:", "above:"]: |
| 957 |
userspec = True |
| 958 |
continue |
| 959 |
if words[1] == 'Branch': |
| 960 |
self.TY = int(words[3]) |
| 961 |
if words[1] in ["User-specified", "Active"]: |
| 962 |
line = line.replace("s:",":") |
| 963 |
for ind in range(2,len(words)): |
| 964 |
if words[ind] in ["parameter:","parameters:"]: |
| 965 |
index = ind + 1 |
| 966 |
break |
| 967 |
if words[1][0] == "U": |
| 968 |
key = "ICP" |
| 969 |
else: |
| 970 |
key = "Active ICP" |
| 971 |
d = [] |
| 972 |
for w in words[index:]: |
| 973 |
try: |
| 974 |
w = int(w) |
| 975 |
except ValueError: |
| 976 |
if ((w[0] == "'" and w[-1] == "'") or |
| 977 |
(w[0] == '"' and w[-1] == '"')): |
| 978 |
w = w[1:-1] |
| 979 |
d.append(w) |
| 980 |
dict[key] = d |
| 981 |
continue |
| 982 |
dict.parseline(" ".join(words[1:]),userspec) |
| 983 |
return dict |
| 984 |
|
| 985 |
class parseBR(parseBMixin, UserList): |
| 986 |
def __init__(self,filename=None): |
| 987 |
if isinstance(filename, str): |
| 988 |
UserList.__init__(self) |
| 989 |
self.readFilename(filename) |
| 990 |
else: |
| 991 |
UserList.__init__(self,filename) |
| 992 |
|
| 993 |
|
| 994 |
def deleteLabel(self,label=None,keepTY=0,keep=0,copy=0): |
| 995 |
if copy: |
| 996 |
data = [] |
| 997 |
for d in self.data: |
| 998 |
data.append(d.deleteLabel(label,keepTY,keep,copy)) |
| 999 |
return self.__class__(data) |
| 1000 |
for d in self.data: |
| 1001 |
d.deleteLabel(label,keepTY,keep) |
| 1002 |
|
| 1003 |
|
| 1004 |
def relabel(self,old_label=None,new_label=None): |
| 1005 |
if new_label is None: |
| 1006 |
label = 1 |
| 1007 |
new = [] |
| 1008 |
for d in self.data: |
| 1009 |
newd = d.relabel(label) |
| 1010 |
for idx,val in newd.labels.sortByIndex(): |
| 1011 |
for k,v in val.items(): |
| 1012 |
if v.get("LAB",0) != 0: |
| 1013 |
label = v["LAB"] |
| 1014 |
new.append(newd) |
| 1015 |
label = label + 1 |
| 1016 |
return self.__class__(new) |
| 1017 |
for d in self.data: |
| 1018 |
d.relabel(old_label,new_label) |
| 1019 |
|
| 1020 |
|
| 1021 |
def uniquelyLabel(self): |
| 1022 |
label = 1 |
| 1023 |
for d in self.data: |
| 1024 |
d.uniquelyLabel(label) |
| 1025 |
for idx,val in d.labels.sortByIndex(): |
| 1026 |
for k,v in val.items(): |
| 1027 |
if v.get("LAB",0) != 0: |
| 1028 |
label = v["LAB"] + 1 |
| 1029 |
|
| 1030 |
|
| 1031 |
def getLabel(self,label): |
| 1032 |
if isinstance(label, int): |
| 1033 |
i = 0 |
| 1034 |
section = 0 |
| 1035 |
for d in self.data: |
| 1036 |
l = len(d.coordarray[0]) |
| 1037 |
try: |
| 1038 |
item = d.getLabel(label) |
| 1039 |
item["index"] = item["index"] + i |
| 1040 |
item["section"] = section |
| 1041 |
return item |
| 1042 |
except (KeyError, IndexError): |
| 1043 |
pass |
| 1044 |
i = i + l |
| 1045 |
section = section + 1 |
| 1046 |
raise KeyError("Label %s not found"%label) |
| 1047 |
elif isinstance(label, str) and len(label) > 2 and label[-1].isdigit(): |
| 1048 |
j = 2 |
| 1049 |
if not label[2].isdigit(): |
| 1050 |
j = 3 |
| 1051 |
number = int(label[j:]) |
| 1052 |
for d in self.data: |
| 1053 |
l = len(d.getLabel(label[:j]).getLabels()) |
| 1054 |
if number <= l: |
| 1055 |
return d.getLabel(label[:j]+str(number)) |
| 1056 |
number -= l |
| 1057 |
raise KeyError("Label %s not found"%label) |
| 1058 |
new = [] |
| 1059 |
label = label[:] |
| 1060 |
for d in self.data: |
| 1061 |
newbranch = d.getLabel(label) |
| 1062 |
if newbranch: |
| 1063 |
new.append(newbranch) |
| 1064 |
for i, lab in enumerate(label): |
| 1065 |
if isinstance(lab, str) and len(lab) > 2 and lab[-1].isdigit(): |
| 1066 |
j = 2 |
| 1067 |
if not lab[2].isdigit(): |
| 1068 |
j = 3 |
| 1069 |
number = int(lab[j:]) |
| 1070 |
l = len(d.getLabel(lab[:j]).getLabels()) |
| 1071 |
if number >= l: |
| 1072 |
number -= l |
| 1073 |
label[i] = lab[:j]+str(number) |
| 1074 |
return self.__class__(new) |
| 1075 |
|
| 1076 |
def __getitem__(self,index): |
| 1077 |
try: |
| 1078 |
return UserList.__getitem__(self,index) |
| 1079 |
except TypeError: |
| 1080 |
return self.data[0][index] |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
def getIndex(self,index): |
| 1085 |
if type(index) != type(0): |
| 1086 |
return self.data[0].getIndex(index) |
| 1087 |
section = 0 |
| 1088 |
i = index |
| 1089 |
for d in self.data: |
| 1090 |
l = len(d) |
| 1091 |
if i < l: |
| 1092 |
item = d.getIndex(i) |
| 1093 |
item["section"] = section |
| 1094 |
item["index"] = index |
| 1095 |
return item |
| 1096 |
i = i - l |
| 1097 |
section = section + 1 |
| 1098 |
raise IndexError |
| 1099 |
|
| 1100 |
|
| 1101 |
def getLabels(self): |
| 1102 |
labels = [] |
| 1103 |
for d in self.data: |
| 1104 |
labels.extend(d.getLabels()) |
| 1105 |
return labels |
| 1106 |
|
| 1107 |
|
| 1108 |
def merge(self): |
| 1109 |
fw = None |
| 1110 |
data = [] |
| 1111 |
for bw in self.data: |
| 1112 |
data.append(bw) |
| 1113 |
if fw is None or fw.BR != bw.BR or (fw.c is not bw.c and |
| 1114 |
fw.c["DS"] * bw.c["DS"] > 0): |
| 1115 |
fw = bw |
| 1116 |
continue |
| 1117 |
f0 = fw[0] |
| 1118 |
b0 = bw[0] |
| 1119 |
if len(f0) != len(b0): |
| 1120 |
fw = bw |
| 1121 |
continue |
| 1122 |
cont = False |
| 1123 |
for i in range(len(f0)): |
| 1124 |
if f0[i] != b0[i]: |
| 1125 |
fw = bw |
| 1126 |
cont = True |
| 1127 |
break |
| 1128 |
if cont: |
| 1129 |
continue |
| 1130 |
|
| 1131 |
|
| 1132 |
new = bw[::-1] |
| 1133 |
new.extend(fw[1:]) |
| 1134 |
new.c = fw.c |
| 1135 |
new.headerlist = fw.headerlist |
| 1136 |
data[-1] = new |
| 1137 |
|
| 1138 |
|
| 1139 |
lab = min(fw.getLabels()+bw.getLabels()) |
| 1140 |
for idx,val in new.labels.sortByIndex(): |
| 1141 |
for k,v in val.items(): |
| 1142 |
if v.get("LAB",0) > 0: |
| 1143 |
val[k] = v.copy() |
| 1144 |
val[k]["LAB"] = lab |
| 1145 |
lab = lab+1 |
| 1146 |
|
| 1147 |
if hasattr(fw,"diagnostics"): |
| 1148 |
if hasattr(bw,"diagnostics"): |
| 1149 |
new.diagnostics = fw.diagnostics + bw.diagnostics |
| 1150 |
else: |
| 1151 |
new.diagnostics = fw.diagnostics |
| 1152 |
|
| 1153 |
del data[-2] |
| 1154 |
|
| 1155 |
fw = None |
| 1156 |
return self.__class__(data) |
| 1157 |
|
| 1158 |
def subtract(self,other,ref,pt=None): |
| 1159 |
"""Subtracts branch branches using interpolation with respect to other |
| 1160 |
with monotonically increasing or decreasing reference coordinate ref, |
| 1161 |
and starting point pt""" |
| 1162 |
new = [] |
| 1163 |
for d in self.data: |
| 1164 |
new.append(d.subtract(other,ref,pt)) |
| 1165 |
return self.__class__(new) |
| 1166 |
|
| 1167 |
def toArray(self): |
| 1168 |
array = [] |
| 1169 |
for d in self.data: |
| 1170 |
array.extend(d.toArray()) |
| 1171 |
return array |
| 1172 |
|
| 1173 |
def writeRaw(self,output): |
| 1174 |
for d in self.data: |
| 1175 |
d.writeRaw(output) |
| 1176 |
output.write("\n") |
| 1177 |
|
| 1178 |
def write(self,output): |
| 1179 |
for d in self.data: |
| 1180 |
d.write(output) |
| 1181 |
|
| 1182 |
def writeShort(self): |
| 1183 |
for d in self.data: |
| 1184 |
d.writeShort() |
| 1185 |
|
| 1186 |
def summary(self): |
| 1187 |
slist = [] |
| 1188 |
for branch in self.data: |
| 1189 |
s = branch.__str__() |
| 1190 |
if s != "": |
| 1191 |
slist.append(s) |
| 1192 |
return "\n".join(slist)+"\n" |
| 1193 |
|
| 1194 |
def __repr__(self): |
| 1195 |
slist = [] |
| 1196 |
for branch in self.data: |
| 1197 |
slist.append(branch.__repr__()) |
| 1198 |
return "\n".join(slist)+"\n" |
| 1199 |
|
| 1200 |
def read(self,inputfile): |
| 1201 |
|
| 1202 |
prevline = None |
| 1203 |
coordnames = [] |
| 1204 |
lastc = None |
| 1205 |
lastTY = 0 |
| 1206 |
while True: |
| 1207 |
branch = AUTOBranch(inputfile,prevline,coordnames) |
| 1208 |
if branch.labels is None: |
| 1209 |
break |
| 1210 |
prevline = branch._lastline |
| 1211 |
coordnames = branch.coordnames |
| 1212 |
self.data.append(branch) |
| 1213 |
if branch.c is None: |
| 1214 |
|
| 1215 |
branch.c = lastc |
| 1216 |
branch.TY = lastTY |
| 1217 |
else: |
| 1218 |
lastc = branch.c |
| 1219 |
lastTY = branch.TY |
| 1220 |
if prevline is None: |
| 1221 |
break |
| 1222 |
|
| 1223 |
class parseB(parseBMixin): |
| 1224 |
|
| 1225 |
def __init__(self,filename=None): |
| 1226 |
self.branches = parseBR(filename) |
| 1227 |
if len(self.branches) > 0: |
| 1228 |
self.coordnames = self.branches[0].coordnames |
| 1229 |
self.deleteLabel = self.branches.deleteLabel |
| 1230 |
self.relabel = self.branches.relabel |
| 1231 |
self.uniquelyLabel = self.branches.uniquelyLabel |
| 1232 |
self.getIndex = self.branches.getIndex |
| 1233 |
self.getLabels = self.branches.getLabels |
| 1234 |
self.toArray = self.branches.toArray |
| 1235 |
self.writeRaw = self.branches.writeRaw |
| 1236 |
self.write = self.branches.write |
| 1237 |
self.writeShort = self.branches.writeShort |
| 1238 |
self.summary = self.branches.summary |
| 1239 |
def __len__(self): |
| 1240 |
l = 0 |
| 1241 |
for d in self.branches: |
| 1242 |
l += len(d) |
| 1243 |
return l |
| 1244 |
def getLabel(self,label): |
| 1245 |
if isinstance(label, int) or ( |
| 1246 |
isinstance(label, str) and len(label) > 2 and label[-1].isdigit()): |
| 1247 |
return self.branches.getLabel(label) |
| 1248 |
return self.__class__(self.branches.getLabel(label)) |
| 1249 |
def read(self,inputfile): |
| 1250 |
self.branches.read(inputfile) |
| 1251 |
if len(self.branches) > 0: |
| 1252 |
self.coordnames = self.branches[0].coordnames |
| 1253 |
def __repr__(self): |
| 1254 |
return self.branches.__repr__() |
| 1255 |
|
| 1256 |
def AUTOatof(input_string): |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
try: |
| 1262 |
return float(input_string) |
| 1263 |
except ValueError: |
| 1264 |
try: |
| 1265 |
if not isinstance(input_string, str): |
| 1266 |
input_string = input_string.decode('ascii') |
| 1267 |
if input_string[-1] == "E": |
| 1268 |
|
| 1269 |
return float(input_string.strip()[0:-1]) |
| 1270 |
if len(input_string) >= 5: |
| 1271 |
if input_string[-4] in ["-","+"]: |
| 1272 |
|
| 1273 |
|
| 1274 |
return float(input_string[:-4]+'E'+input_string[-4:]) |
| 1275 |
if input_string[-4] == "D": |
| 1276 |
|
| 1277 |
|
| 1278 |
return float(input_string[:-4]+'E'+input_string[-3:]) |
| 1279 |
input_string = input_string.replace("D","E") |
| 1280 |
input_string = input_string.replace("d","e") |
| 1281 |
try: |
| 1282 |
return float(input_string) |
| 1283 |
except ValueError: |
| 1284 |
i = input_string.find("-", 1) |
| 1285 |
if i == -1: |
| 1286 |
i = input_string.find("+", 1) |
| 1287 |
if i != -1 and input_string[i-1] not in ["e", "E"]: |
| 1288 |
|
| 1289 |
return float(input_string[:i]+'E'+input_string[i:]) |
| 1290 |
print("Encountered value I don't understand") |
| 1291 |
print(input_string) |
| 1292 |
print("Setting to 0") |
| 1293 |
return 0.0 |
| 1294 |
except ValueError: |
| 1295 |
print("Encountered value which raises an exception while processing!!!") |
| 1296 |
print(input_string) |
| 1297 |
print("Setting to 0") |
| 1298 |
return 0.0 |
| 1299 |
|
| 1300 |
|
| 1301 |
def pointtest(a,b): |
| 1302 |
if "TY name" not in a: |
| 1303 |
raise AUTOExceptions.AUTORegressionError("No TY label") |
| 1304 |
if "TY number" not in a: |
| 1305 |
raise AUTOExceptions.AUTORegressionError("No TY label") |
| 1306 |
if "BR" not in a: |
| 1307 |
raise AUTOExceptions.AUTORegressionError("No BR label") |
| 1308 |
if "data" not in a: |
| 1309 |
raise AUTOExceptions.AUTORegressionError("No data label") |
| 1310 |
if "PT" not in a: |
| 1311 |
raise AUTOExceptions.AUTORegressionError("No PT label") |
| 1312 |
if "LAB" not in a: |
| 1313 |
raise AUTOExceptions.AUTORegressionError("No LAB label") |
| 1314 |
if len(a["data"]) != len(b["data"]): |
| 1315 |
raise AUTOExceptions.AUTORegressionError("Data sections have different lengths") |
| 1316 |
|
| 1317 |
def test(): |
| 1318 |
print("Testing reading from a filename") |
| 1319 |
foo = parseB() |
| 1320 |
foo.readFilename("test_data/fort.7") |
| 1321 |
if len(foo) != 150: |
| 1322 |
raise AUTOExceptions.AUTORegressionError("File length incorrect") |
| 1323 |
pointtest(foo.getIndex(0),foo.getIndex(57)) |
| 1324 |
|
| 1325 |
print("Testing reading from a stream") |
| 1326 |
foo = parseB() |
| 1327 |
fp = open("test_data/fort.7","r") |
| 1328 |
foo.read(fp) |
| 1329 |
if len(foo) != 150: |
| 1330 |
raise AUTOExceptions.AUTORegressionError("File length incorrect") |
| 1331 |
pointtest(foo.getIndex(0),foo.getIndex(57)) |
| 1332 |
|
| 1333 |
|
| 1334 |
print("Testing label manipulation") |
| 1335 |
labels = foo.getLabels() |
| 1336 |
foo.relabel(labels[0],57) |
| 1337 |
labels = foo.getLabels() |
| 1338 |
if labels[0] != 57: |
| 1339 |
raise AUTOExceptions.AUTORegressionError("Error in either relabel or getLabel") |
| 1340 |
foo.deleteLabel(labels[1]) |
| 1341 |
new_labels = foo.getLabels() |
| 1342 |
if len(labels) != len(new_labels) + 1: |
| 1343 |
raise AUTOExceptions.AUTORegressionError("Error in label deletion") |
| 1344 |
|
| 1345 |
bar = foo.relabel() |
| 1346 |
old_labels = foo.getLabels() |
| 1347 |
new_labels = bar.getLabels() |
| 1348 |
if old_labels[0] != 57 or new_labels[0] != 1: |
| 1349 |
raise AUTOExceptions.AUTORegressionError("Error in relabelling") |
| 1350 |
print("parseB passed all tests") |
| 1351 |
|
| 1352 |
if __name__ == '__main__' : |
| 1353 |
test() |
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|