def __init__(self, G, coloring, name=None, check=True): from flexible_rigid_graph import FlexRiGraph if type(G) == FlexRiGraph or 'FlexRiGraph' in str( type(G)) or isinstance(G, FlexRiGraph): self._graph = G else: raise exceptions.TypeError('The graph G must be FlexRiGraph.') if type(coloring) in [list, Set] and len(coloring) == 2: self._red_edges = Set([Set(e) for e in coloring[0]]) self._blue_edges = Set([Set(e) for e in coloring[1]]) elif type(coloring) == dict: self._red_edges = Set( [Set(e) for e in coloring if coloring[e] == 'red']) self._blue_edges = Set( [Set(e) for e in coloring if coloring[e] == 'blue']) elif type(coloring) == NACcoloring or isinstance( coloring, NACcoloring) or 'NACcoloring' in str(type(coloring)): self._red_edges = copy(coloring._red_edges) self._blue_edges = copy(coloring._blue_edges) else: raise exceptions.TypeError( 'The coloring must be a dict, list consisting of two lists or an instance of NACcoloring.' ) self._check_edges() self._name = name if check and not self.is_NAC_coloring(): raise exceptions.ValueError('The coloring is not a NAC-coloring.')
def getCluster(self, feats): """Converts the feature vectors of the input into integers, which represent the cluster which each feature is most likelly a member of. Output will be the same form as the input, but with the features converted to integers. In the case of a feature matrix it will become a vector of integers. For a list of feature vectors it will become a list of integers. For a list of tuples with the last element a feature vector it will return a list of tuples where the last element has been replaced with an integer.""" if isinstance(feats, numpy.ndarray): # Numpy array - just pass through... assert (len(feats.shape) == 2) return self.doGetCluster(feats) elif isinstance(feats, list): if isinstance(feats[0], numpy.ndarray): # List of vectors - glue them all together to create a data matrix and pass on through... data = numpy.vstack(feats) asVec = self.doGetCluster(data) return map(lambda i: asVec[i], xrange(asVec.shape[0])) elif isinstance(feats[0], tuple): # List of tuples where the last item should be a numpy.array as a feature vector... vecs = map(lambda x: x[-1], feats) data = numpy.vstack(vecs) asVec = self.doGetCluster(data) return map(lambda i: feats[i][:-1] + (asVec[i], ), xrange(asVec.shape[0])) else: raise exceptions.TypeError( 'bad type for features - when given a list it must contain numpy.array vectors or tuples with the last element a vector' ) else: raise exceptions.TypeError( 'bad type for features - expects a numpy.array or a list')
def getCluster(self, feats): """Given a set of features returns for each feature the cluster with the highest probability of having generated it. Multiple input/output modes are supported. If a data matrix is input then the output will be an integer vector of cluster indices. If the input is a list of feature vectors the output will be a list of cluster indices. If the input is a list of tuples with the last elements a feature vector the output will be identical, but with the feature vectors replaced by cluster integers.""" if isinstance(feats, numpy.ndarray): # Numpy array - just pass through... assert (len(feats.shape) == 2) return self.doGetCluster(feats) elif isinstance(feats, list): if isinstance(feats[0], numpy.ndarray): # List of vectors - glue them all together to create a data matrix and pass on through... data = numpy.vstack(feats) asVec = self.doGetCluster(data) return map(lambda i: asVec[i], xrange(asVec.shape[0])) elif isinstance(feats[0], tuple): # List of tuples where the last item should be a numpy.array as a feature vector... vecs = map(lambda x: x[-1], feats) data = numpy.vstack(vecs) asVec = self.doGetCluster(data) return map(lambda i: feats[i][:-1] + (asVec[i], ), xrange(asVec.shape[0])) else: raise exceptions.TypeError( 'bad type for features - when given a list it must contain numpy.array vectors or tuples with the last element a vector' ) else: raise exceptions.TypeError( 'bad type for features - expects a numpy.array or a list')
def getWeight(self, feats): """Given a set of features returns for each feature the probability of having been generated by each mixture member - this vector will sum to one. Multiple input/output modes are supported. If a data matrix is input then the output will be a matrix where each row has been replaced by the mixing vector. If the input is a list of feature vectors the output will be a list of mixing vectors. If the input is a list of tuples with the last elements a feature vector the output will be identical, but with the feature vectors replaced with mixing vectors.""" if isinstance(feats, numpy.ndarray): # Numpy array - just pass through... assert (len(feats.shape) == 2) return self.doGetWeight(feats) elif isinstance(feats, list): if isinstance(feats[0], numpy.ndarray): # List of vectors - glue them all together to create a data matrix and pass on through... data = numpy.vstack(feats) asMat = self.doGetWeight(data) return map(lambda i: asMat[i, :], xrange(asMat.shape[0])) elif isinstance(feats[0], tuple): # List of tuples where the last item should be a numpy.array as a feature vector... vecs = map(lambda x: x[-1], feats) data = numpy.vstack(vecs) asMat = self.doGetWeight(data) return map(lambda i: feats[i][:-1] + (asMat[i, :], ), xrange(asMat.shape[0])) else: raise exceptions.TypeError( 'bad type for features - when given a list it must contain numpy.array vectors or tuples with the last element a vector' ) else: raise exceptions.TypeError( 'bad type for features - expects a numpy.array or a list')
def train(self, feats, clusters, *listArgs, **dictArgs): """Given the features and number of clusters, plus any implimentation specific arguments, this trains the model. Features can be provided as a data matrix, as a list of feature vectors or as a list of tuples where the last entry of each tuple is a feature vector.""" if isinstance(feats, numpy.ndarray): # Numpy array - just pass through... assert (len(feats.shape) == 2) self.doTrain(feats, clusters, *listArgs, **dictArgs) del listArgs, dictArgs elif isinstance(feats, list): if isinstance(feats[0], numpy.ndarray): # List of vectors - glue them all together to create a data matrix and pass on through... data = numpy.vstack(feats) self.doTrain(data, clusters, *listArgs, **dictArgs) del listArgs, dictArgs elif isinstance(feats[0], tuple): # List of tuples where the last item should be a numpy.array as a feature vector... vecs = map(lambda x: x[-1], feats) data = numpy.vstack(vecs) self.doTrain(data, clusters, *listArgs, **dictArgs) del listArgs, dictArgs else: raise exceptions.TypeError( 'bad type for features - when given a list it must contain numpy.array vectors or tuples with the last element a vector' ) else: raise exceptions.TypeError( 'bad type for features - expects a numpy.array or a list')
def __init__(self, graph): if not (isinstance(graph, FlexRiGraph) or 'FlexRiGraph' in str(type(graph))): raise exceptions.TypeError('The graph must be of the type FlexRiGraph.') self._graph = graph self._same_lengths = None self._active_NACs = None
def __init__(self, *params): """ Disk represents a raw disk Possible constructors: __init__(self, element, doc=None) type(element)=Node __init__(self, name, doc=None) type(name)==str """ if len(params)==1: doc=xml.dom.getDOMImplementation().createDocument(None, self.TAGNAME, None) elif len(params)==2: doc=params[1] else: raise exceptions.TypeError("""Wrong number of arguments to constructor of HostDisk. Either __init__(self, element, doc=None) or __init__(self, name, doc=None) are supported.""") if isinstance(params[0], basestring): element=doc.createElement(self.TAGNAME) element.setAttribute("name", params[0]) else: element=params[0] super(HostDisk, self).__init__(element, doc) self.log.debug("__init__") self.devicename=None self.stabilizedfile="/proc/partitions" self.stabilizedtype="hash" self.stabilizedgood=2
def check(self): if self[0] != 'Command': raise exceptions.TypeError( '%r not permitted as %r[0]. Has to be \'Command\'' % (self[0], self)) check_type("command", self.command(), self.TESTTYPE) check_type("id", self.id(), self.TESTTYPE) check_type("args", self.args(), dict)
def checkAndSetInteger(var=None, default=None): """ Utility to check if the passed value is a positive integer. @param var : The object to check. @param default : The default to use if no var is given. @return : The checked object or default. @throw : TypeError if check fails. """ if var is None: if not (isinstance( default, int) ): raise exceptions.TypeError("The default must be an integer.") return default if not isinstance(var, int): raise exceptions.TypeError("The parameter must be of type integer.") return var
def makeSizeList2(slist): """slist: ( ( w,h) , optional count , ( w2,h2) , optional count2 , ( w3,h3 ) , optional count3 , ... ( wn,hn ) ) """ rtn = [] for i, carg in zip(range(len(slist)), slist): if type(carg) is int: if type(slist[i - 1]) in (list, tuple) and type(slist[i + 1]) in (list, tuple): for j in range(1, carg): rtn.append((getProgress(slist[i - 1][0], slist[i + 1][0], float(j) / carg), getProgress(slist[i - 1][1], slist[i + 1][1], float(j) / carg)) ) else: raise exceptions.TypeError('invalid type in list ') elif type(carg) in (list, tuple): rtn.append(carg) else: raise exceptions.TypeError( 'invalid type %s type must list or tuple' % type(carg)) return rtn
def check(self): if self[0] != 'Event': raise exceptions.TypeError( '%r not permitted as %r[0]. Has to be \'Event\'' % (self[0])) check_type("event", self.event(), self.TESTTYPE) check_type("id", self.id(), self.TESTTYPE) check_type("origin", self.origin(), dict) check_type("args", self.args(), dict) check_type("timestamp", self.timestamp(), float, True)
def calculate_overlap(self, A, B): """ calculate overlap integral between fields A and B. A & B must be integers between 0-2 (0 = pump, 1 = signal, 3 = idler.)""" if (type(A) is not int) or (type(B) is not int): e = exceptions.TypeError('A & B must both be integers.') raise e if A < 0 or A > 3 or B < 0 or B > 3: e = exceptions.ValueError('A & B must be in range [0,3].') raise e return (self.waists[A] + self.waists[B]) / 2.0
def __init__(self, address, data, description = 'Generic memory.'): DebugOut('Memory.__init__(%s, data, %s)' % (address, description)) self.__description = description self.__address = address if (type(data) != types.StringType): raise exceptions.TypeError('Data must be a string.') self.__data = list(data)
def save_dense(self, s, xout, h): if self.count == self.kmax: self.resize() y = s.dense_out(xout, h) if self.ysave.dtype != y.dtype: errmsg = 'Integrand returns ', str( y.dtype), ' but workspaces are initialized to ', str( self.ysave.dtype), '!' raise exceptions.TypeError(errmsg) self.ysave[self.count, :] = y self.xsave[self.count] = xout self.count += 1
def checkAndSetInstance(cls, var=None, default=None): """ Utility to check if the passed object is an instance of the given class (or derived class). @param cls : The class against which to check. @param var : The object to check. @param default : The default to use if no var is given. @return : The checked object or default. @throw : TypeError if var is not an instance of cls or a derived class. """ if var is None: if default is None: return None elif not isinstance(default, cls): raise exceptions.TypeError("The default is not of correct type.") else: return default elif not isinstance(var, cls): raise exceptions.TypeError("The parameter 'var' is not of correct type.") return var
def save(self, x, y): if self.ysave.dtype != y.dtype: errmsg = 'Integrand returns ', str( y.dtype), ' but workspaces are initialized to ', str( self.ysave.dtype), '!' raise exceptions.TypeError(errmsg) if self.kmax <= 0: return if self.count == self.kmax: self.resize() self.ysave[self.count, :] = y self.xsave[self.count] = x self.count += 1
def train(self, feats, clusters, *listArgs, **dictArgs): """Accepts as input the features, how many clusters to use and any further implimentation specific variables. The features can be represented as a data matrix, a list of vectors or as a list of tuples where the last entry is a feature vector. Will then train the model on the given data set.""" if isinstance(feats, numpy.ndarray): # Numpy array - just pass through... assert (len(feats.shape) == 2) self.doTrain(feats, clusters, *listArgs, **dictArgs) elif isinstance(feats, list): if isinstance(feats[0], numpy.ndarray): # List of vectors - glue them all together to create a data matrix and pass on through... data = numpy.vstack(feats) self.doTrain(data, clusters, *listArgs, **dictArgs) elif isinstance(feats[0], tuple): # List of tuples where the last item should be a numpy.array as a feature vector... vecs = map(lambda x: x[-1], feats) data = numpy.vstack(vecs) self.doTrain(data, clusters, *listArgs, **dictArgs) else: raise exceptions.TypeError( 'bad type for features - when given a list it must contain numpy.array vectors or tuples with the last element a vector' ) else: raise exceptions.TypeError( 'bad type for features - expects a numpy.array or a list')
def getNLL(self, feats): """Given a set of features returns the negative log likelihood of the given features being generated by the model. Note that as the k-means model is not probabilistic this is technically wrong, but it hacks it by treating each cluster as a symmetric Gaussian with a standard deviation calculated using the provided features, with equal probability of selecting each cluster.""" if isinstance(feats, numpy.ndarray): # Numpy array - just pass through... assert (len(feats.shape) == 2) return self.doGetNLL(feats) elif isinstance(feats, list): if isinstance(feats[0], numpy.ndarray): # List of vectors - glue them all together to create a data matrix and pass on through... data = numpy.vstack(feats) return self.doGetNLL(data) elif isinstance(feats[0], tuple): # List of tuples where the last item should be a numpy.array as a feature vector... vecs = map(lambda x: x[-1], feats) data = numpy.vstack(vecs) return self.doGetNLL(data) else: raise exceptions.TypeError( 'bad type for features - when given a list it must contain numpy.array vectors or tuples with the last element a vector' ) else: raise exceptions.TypeError( 'bad type for features - expects a numpy.array or a list')
def check_type(name, value, type_, allows_none=False): """Check value is of type type_ or None if allows_none is True.""" if isinstance(value, type_): return if allows_none and value is None: return if isinstance(type_, tuple): types = list([t.__name__ for t in type_]) else: types = [type_.__name__] if allows_none: types.append("None") raise exceptions.TypeError('%r not permitted as %s. Has to be %s.' \ % (value, name, prettyprint_list(types)))
def getNLL(self, feats): """Given a set of features returns the negative log likelihood of the given features being generated by the model.""" if isinstance(feats, numpy.ndarray): # Numpy array - just pass through... assert (len(feats.shape) == 2) return self.doGetNLL(feats) elif isinstance(feats, list): if isinstance(feats[0], numpy.ndarray): # List of vectors - glue them all together to create a data matrix and pass on through... data = numpy.vstack(feats) return self.doGetNLL(data) elif isinstance(feats[0], tuple): # List of tuples where the last item should be a numpy.array as a feature vector... vecs = map(lambda x: x[-1], feats) data = numpy.vstack(vecs) return self.doGetNLL(data) else: raise exceptions.TypeError( 'bad type for features - when given a list it must contain numpy.array vectors or tuples with the last element a vector' ) else: raise exceptions.TypeError( 'bad type for features - expects a numpy.array or a list')
def setTransition(self, getfunc, inputs, nextState, action): """setTransition(getfunc, inputs, nextState, action) getfunc (function): converte function from input to "inputs" inputs (list): a set of conditions to invoke this rule. nextState (int): next state. action (string/function): execute when this rule is invoked. string: call shell function: call the function""" if not (type(inputs) == type([])): raise exceptions.TypeError("inputs must be list, but:" \ + str(type(inputs))) self.rules.append({ "input": inputs, "next": nextState, "action": action, "getfunc": getfunc })
def __getitem__(self, item): ## This WILL get cleaned up, but it does what I want for now x_vals = [] toReshape = None if isinstance(item, types.TupleType): if isinstance(item[0], types.FloatType) or \ isinstance(item[0], types.IntType): x_vals.append(float(item[0])) elif isinstance(item[0], na.ndarray): toReshape = item[0].shape x_vals = (item[0].ravel()) colsToReturn = [] for col in item[1:]: if isinstance(col, types.StringType): colsToReturn.append(self.cols[col]) else: colsToReturn.append(int(col)) elif isinstance(item, types.FloatType) or \ isinstance(item, types.IntType): x_vals.append(float(item)) colsToReturn = arange(1, len(self.cols)) elif isinstance(item, na.ndarray): toReshape = item.shape x_vals = item.ravel() colsToReturn = arange(1, len(self.cols)) elif isinstance(item, types.ListType): colsToReturn = arange(1, len(self.cols)) elif isinstance(item, types.StringType): return self.columns[:, self.cols[item]] else: raise exceptions.TypeError() colsToReturn = na.array(colsToReturn, 'int32') valsToReturn = na.zeros((len(x_vals), len(colsToReturn)), 'float64') x_axis = self.columns[:, 0] x_vals_arr = na.array(x_vals, 'float64') PointCombine.Interpolate(x_axis, self.columns, x_vals_arr, valsToReturn, colsToReturn) if toReshape != None: if len(colsToReturn == 1): valsToReturn = na.reshape(valsToReturn, toReshape) else: newShape = list(toReshape) newShape.append(len(colsToReturn)) valsToReturn = na.reshape(valsToReturn, newShape) return valsToReturn
def write(self, address, data): DebugOut('Memory.write(%s, data)' % (address)) 'Arguments are the address and a string.' if (type(data) != types.StringType): raise exceptions.TypeError('Data must be a string.') offset = address - self.__address DebugOut( 'self.__address: %s' % self.__address ) DebugOut( 'offset: %s' % offset ) DebugOut( 'len(data): %s' % len(data)) #DebugOut( 'len(self): %s' % len(self)) if (((offset + len(data) - 1) >= len(self)) or (offset < 0)): raise exceptions.ValueError('Request is not within memory boundary.') self.__data[offset : offset + len(data)] = list(data)
def __init__(self, seqname=None, start=None, end=None, strand="+", frame=None, location=None, alphabet=DEFAULT_ALPHABET): if not isinstance(seqname, (types.StringType, types.NoneType)): raise exceptions.TypeError("seqname needs to be string") self.frame = frame self.alphabet = alphabet if location is None: self.seqname = seqname self.start = start self.end = end self.strand = strand return else: self.seqname = location.seqname self.start = location.start() + 1 self.end = location.end() + 1 self.strand = location.complement
def __init__(self, writer=None, serializer=None, delim=None): self.writer = writer or stdout.write self.serializer = serializer or str self.delim = delim or '\n' if not callable(self.writer): raise exceptions.TypeError('%r is not callable.' % self.writer)
def RAISE(key, value): raise exceptions.TypeError("env[%s] = %r is not a string." % (key, value))
def proc_obj(self, obj): raise exceptions.TypeError("Not a list or a tuple", obj)
def __init__(self, graph, four_cycles=[], separator='', edges_ordered=[]): if not (isinstance(graph, FlexRiGraph) or 'FlexRiGraph' in str(type(graph))): raise exceptions.TypeError('The graph must be of the type FlexRiGraph.') self._graph = graph if four_cycles == []: self._four_cycles = self._graph.four_cycles(only_with_NAC=True) else: self._four_cycles = four_cycles if not self._graph.are_NAC_colorings_named(): self._graph.set_NAC_colorings_names() # -----Polynomial Ring for leading coefficients----- ws = [] zs = [] lambdas = [] ws_latex = [] zs_latex = [] lambdas_latex = [] if edges_ordered==[]: edges_ordered = self._graph.edges(labels=False) else: if (Set([self._edge2str(e) for e in edges_ordered]) != Set([self._edge2str(e) for e in self._graph.edges(labels=False)])): raise ValueError('The provided ordered edges do not match the edges of the graph.') for e in edges_ordered: ws.append('w' + self._edge2str(e)) zs.append('z' + self._edge2str(e)) lambdas.append('lambda' + self._edge2str(e)) ws_latex.append('w_{' + self._edge2str(e).replace('_', separator) + '}') zs_latex.append('z_{' + self._edge2str(e).replace('_', separator) + '}') lambdas_latex.append('\\lambda_{' + self._edge2str(e).replace('_', separator) + '}') self._ringLC = PolynomialRing(QQ, names=lambdas+ws+zs) #, order='lex') self._ringLC._latex_names = lambdas_latex + ws_latex + zs_latex self._ringLC_gens = self._ringLC.gens_dict() self._ring_lambdas = PolynomialRing(QQ, names=lambdas + ['u']) self._ring_lambdas._latex_names = lambdas_latex + ['u'] self._ring_lambdas_gens = self._ring_lambdas.gens_dict() self.aux_var = self._ring_lambdas_gens['u'] xs = [] ys = [] xs_latex = [] ys_latex = [] for v in self._graph.vertices(): xs.append('x' + str(v)) ys.append('y' + str(v)) xs_latex.append('x_{' + str(v) + '}') ys_latex.append('y_{' + str(v) + '}') self._ring_coordinates = PolynomialRing(QQ, names=lambdas+xs+ys) self._ring_coordinates._latex_names = lambdas_latex + xs_latex + ys_latex self._ring_coordinates_gens = self._ring_coordinates.gens_dict() # ----Ramification----- # if len(self._graph.NAC_colorings()) > 1: self._ring_ramification = PolynomialRing(QQ, [col.name() for col in self._graph.NAC_colorings()], len(self._graph.NAC_colorings())) # else: # self._ring_ramification = PolynomialRing(QQ, self._graph.NAC_colorings()[0].name()) self._ring_ramification_gens = self._ring_ramification.gens_dict() self._restriction_NAC_types = self.NAC_coloring_restrictions() # -----Graph of 4-cycles----- self._four_cycle_graph = Graph([self._four_cycles,[]], format='vertices_and_edges') for c1, c2 in Subsets(self._four_cycle_graph.vertices(), 2): intersection = self.cycle_edges(c1, sets=True).intersection(self.cycle_edges(c2, sets=True)) if len(intersection)>=2 and len(intersection[0].intersection(intersection[1]))==1: common_vert = intersection[0].intersection(intersection[1])[0] self._four_cycle_graph.add_edge(c1, c2, common_vert) # -----Cycle with orthogonal diagonals due to NAC----- self._orthogonal_diagonals = { delta.name(): [cycle for cycle in self._four_cycle_graph if delta.cycle_has_orthogonal_diagonals(cycle)] for delta in self._graph.NAC_colorings()}