def deleteLabel(self,label=None,keepTY=0,keep=0,copy=0): """Removes solutions with the given labels or type names""" if label is None: label = [lab for lab in all_point_types if lab not in ["No Label", "RG", "UZ"]] if isinstance(label, (str, int)): label = [label] if copy: new = self.__class__(self) new.labels = Points.PointInfo(self.labels.by_index.copy()) if not self.__fullyParsed: new.__datalist = self.__datalist[:] else: new = self for idx in new.labels.getIndices(): ty_name,v = new._gettypelabel(idx) if "LAB" not in v: continue if ((not keep and (v["LAB"] in label or ty_name in label)) or (keep and not v["LAB"] in label and not ty_name in label)): if copy: new.labels[idx][ty_name] = v.copy() new.labels[idx][ty_name]["LAB"] = 0 if not keepTY: new.labels[idx][ty_name]["TY number"] = 0 if not new.__fullyParsed: new.__patchline(new.__datalist,idx,3,0) if not keepTY: new.__patchline(new.__datalist,idx,2,0) if v["TY number"] == 0: new.labels.remove(idx) if copy: return new
def relabel(self,old_label=1,new_label=None): """Relabels the first solution with the given label""" if new_label is None: label = old_label new = self.__class__(self) labels = {} if not self.__fullyParsed: new.__datalist = self.__datalist[:] for index in self.labels.getIndices(): labels[index] = self.labels[index].copy() ty_name,v = self._gettypelabel(index) if v.get("LAB",0) != 0: labels[index][ty_name] = v.copy() labels[index][ty_name]["LAB"] = label if not self.__fullyParsed: self.__patchline(new.__datalist,index,3,label) label = label + 1 new.labels = Points.PointInfo(labels) return new labels = self.labels if isinstance(old_label, int): old_label = [old_label] new_label = [new_label] for j in range(len(old_label)): for index in self.labels.getIndices(): v = self._gettypelabel(index)[1] if "LAB" not in v: continue if v["LAB"] == old_label[j]: v["LAB"] = new_label[j] if not self.__fullyParsed: self.__patchline(self.__datalist,index,3,new_label[j])
def getLabel(self,label): """Given a label, return the correct solution""" if label is None: return self if isinstance(label, int): for k in self.labels.getIndices(): v = self._gettypelabel(k)[1] if v.get("LAB",0) == label: return self.getIndex(k) raise KeyError("Label %s not found"%label) if isinstance(label, str) and len(label) > 2 and label[-1].isdigit(): j = 2 if not label[2].isdigit(): j = 3 number = int(label[j:]) i = 0 for k,v in self.labels.sortByIndex(): if label[:j] in v: i = i + 1 if i == number: return self.getIndex(k) raise KeyError("Label %s not found"%label) if not AUTOutil.isiterable(label): label = [label] labels = {} counts = [0]*len(label) for k,val in self.labels.sortByIndex(): ty_name,v = self._gettypelabel(k) if "LAB" not in v: continue for i in range(len(label)): lab = label[i] j = 2 if len(lab) > 2 and not lab[2].isdigit(): j = 3 if (isinstance(lab, str) and len(lab) > j and ty_name == lab[:j]): counts[i] = counts[i] + 1 if counts[i] == int(lab[j:]): labels[k] = val if v["LAB"] in label or ty_name in label: labels[k] = val continue new = self.__class__(self) new.labels = Points.PointInfo(labels) return new
def read(self,inputfile,prevline=None): # We now go through the file and read the branches. # read the branch header # A section is defined as a part of a fort.7 # file between "headers", i.e. those parts # of the fort.7 file which start with a 0 # and contain information about the branch # FIXME: I am not sure of this is the correct # fix to having multiple sections of a fort.7 # file with the same branch number. What it comes # dowm to is keeping the fort.7 and fort.8 files # in sync. I.e. I could make sure that # this branch numbers are unique here, but then # the fort.7 file will not match the fort.8 file. # Another way for a section to start is with a point number # equal to 1. self._lastline = None if hasattr(str,"split"): split = str.split else: import string split = string.split if prevline: line = prevline else: try: line = next(inputfile) except StopIteration: self.labels = None return headerlist = [] columns = split(line,None,2) if columns[0] == '0': headerlist.append(line) for line in inputfile: columns = split(line,None,2) if columns[0] != '0': self._lastline = line break headerlist.append(line) datalist = [] labels = {} if columns[0] != '0': self._lastline = None datalist = [line] if columns[2][0] != '0': #type columns = split(line,None,4) pt = int(columns[1]) if pt < 0: stab = 'S' else: stab = 'U' ty = int(columns[2]) lab = int(columns[3]) key = type_translation(ty)["short name"] labels[len(datalist)-1] = {key: {"LAB":lab,"TY number":ty}} for line in inputfile: datalist.append(line) columns = split(line,None,2) if (columns[0] == '0' or ((columns[1] == '-1' or columns[1] == '1') and self.__checknorotate(datalist))): self._lastline = datalist.pop() break if columns[2][0] != '0': #type # columns = split(datalist[-1],None,4) columns = splitB(datalist[-1]) pt = int(columns[1]) ty = int(columns[2]) lab = int(columns[3]) key = type_translation(ty)["short name"] labels[len(datalist)-1] = {key: {"LAB":lab,"TY number":ty}} self.labels = Points.PointInfo(labels) self.__datalist = datalist self.c = self.parseHeader(headerlist)
def __realinit(self, fort7_filename, fort8_filename, fort9_filename, constants): ioerrors = [] try: parseB.parseBR.__init__(self, fort7_filename) for i, d in enumerate(self): self[i] = bifDiagBranch(d) except IOError: ioerrors.append(str(sys.exc_info()[1])) parseB.parseBR.__init__(self) fort7_filename = None if isinstance(fort8_filename, parseS.AUTOSolution): fort8_filename = [fort8_filename] try: solution = parseS.parseS(fort8_filename) except IOError: ioerrors.append(str(sys.exc_info()[1])) solution = None if fort7_filename is None: raise AUTOExceptions.AUTORuntimeError('\n'.join(ioerrors)) if fort7_filename is None and fort8_filename is not None: # simulate a bifurcation diagram labels = {} for s in solution: br = s["Branch number"] if labels == {} or br != branch.BR: if labels != {}: branch.labels = Points.PointInfo(labels) branch = bifDiagBranch() self.append(branch) branch.BR = br branch.coordarray = [] branch.coordnames = [] branch.headernames = [] branch.headerlist = [] branch.c = constants labels = {} base = 0 i = 0 pt = s["PT"] lab = s["LAB"] ty = s["TY number"] if i >= base + pt - 1: # point numbers wrapped around base += 9999 i = base + pt - 1 labels[i] = {s["TY"]: {"LAB": lab, "TY number": ty, "PT": pt}} if labels != {}: branch.labels = Points.PointInfo(labels) if fort9_filename is not None and self.data != []: try: # for now just attach diagnostics information to first branch self[0].diagnostics = parseD.parseD(fort9_filename) except IOError: pass i = 0 if solution is not None: for d in self: if constants is None and d.c is not None: constants = d.c constants = parseC.parseC(constants) for k in constants: if k in self.nonekeys: constants[k] = None for ind in d.labels.getIndices(): if i >= len(solution): break x = d._gettypelabel(ind)[1] s = solution[i] if x.get("LAB", 0) != 0 or s["LAB"] == 0: i = i + 1 s = x["solution"] = parseS.AUTOSolution( s, constants=constants) if d.coordnames != []: s.b = d[ind]