def testList(self): a = HashSet(['a', 'b', 'c', 'd', 'e']) l = a.list() l.sort() self.assertEqual(l, ['a', 'b', 'c', 'd', 'e'], "incorrect list") l = [] self.assertTrue('e' in a, "set rep exposure")
def remove_node(self, node): """Removes node and all edges connected to it.""" if node not in self.__adjacency_list: raise ValueError("Unknown node: " + str(node)) # remove node (and all out-edges) from adjacency list del self.__adjacency_list[node] # remove all in-edges from adjacency list for n in self.__adjacency_list.keys(): self.__adjacency_list[n] = HashSet( filter(lambda x, node=node: x is not node, self.__adjacency_list[n].list())) # remove all refering pairs in label map for label in self.__label_map.keys(): lm = HashSet(filter(lambda x,node=node: \ (x[0] is not node) and (x[1] is not node), self.__label_map[label].list())) # remove the entry completely if the label is now unused if lm.empty(): del self.__label_map[label] else: self.__label_map[label] = lm # remove all refering entries in edge map for edge in self.__edge_map.keys(): if edge[0] is node or edge[1] is node: del self.__edge_map[edge]
def __init__(self, nodes=[]): """Initializes a new Graph object.""" self.__adjacency_list = {} # maps parent -> set of child objects for n in nodes: self.__adjacency_list[n] = HashSet() self.__label_map = {} # maps label -> set of (parent, child) pairs self.__edge_map = {} # maps (parent, child) pair -> label
def testLen(self): a = HashSet() self.assertEqual(len(a), 0, "incorrect default size") a.add('a') a.add('b') self.assertEqual(len(a), 2, "incorrect size") a.remove('b') self.assertEqual(len(a), 1, "incorrect size after removal") a.add('a') self.assertEqual(len(a), 1, "incorrect size after duplicate add")
def testLen(self): a = HashSet() self.failUnless(len(a) == 0, "incorrect default size") a.add('a') a.add('b') self.failUnless(len(a) == 2, "incorrect size") a.remove('b') self.failUnless(len(a) == 1, "incorrect size after removal") a.add('a') self.failUnless(len(a) == 1, "incorrect size after duplicate add")
def __init__(self, reactants={}, catalysts=[], reversible=0, data=None): """Initializes a new Reaction object.""" # enforce invariants on reactants: self.reactants = reactants.copy() for r in self.reactants.keys(): if self.reactants[r] == 0: del self.reactants[r] self.catalysts = HashSet(catalysts).list() self.data = data self.reversible = reversible
def testContains(self): n = HashSet() self.assertTrue('a' not in n, "element in empty set") self.assertTrue(not n.contains('a'), "element in empty set (2)") a = HashSet(['a','b','c','d']) self.assertTrue('a' in a, "contained element not found") self.assertTrue('d' in a, "contained element not found") self.assertTrue('e' not in a, "not contained element found") self.assertTrue(68 not in a, "not contained element found") self.assertTrue(a.contains('a'), "contained element not found (2)") self.assertTrue(a.contains('d'), "contained element not found (2)") self.assertTrue(not a.contains('e'), "not contained element found (2)") self.assertTrue(not a.contains(68), "not contained element found (2)")
def remove_node(self, node): """Removes node and all edges connected to it.""" if node not in self.__adjacency_list: raise ValueError("Unknown node: " + str(node)) # remove node (and all out-edges) from adjacency list del self.__adjacency_list[node] # remove all in-edges from adjacency list for n in self.__adjacency_list: self.__adjacency_list[n] = HashSet(filter(lambda x,node=node: x[0] is not node, self.__adjacency_list[n].list())) # remove all refering pairs in label map for label in self.__label_map.keys(): lm = HashSet(filter(lambda x,node=node: \ (x[0] is not node) and (x[1] is not node), self.__label_map[label].list())) # remove the entry completely if the label is now unused if lm.empty(): del self.__label_map[label] else: self.__label_map[label] = lm
def add_edge(self, source, to, label=None): """Adds an edge to this graph.""" if source not in self.__adjacency_list: raise ValueError("Unknown <from> node: " + str(source)) if to not in self.__adjacency_list: raise ValueError("Unknown <to> node: " + str(to)) edge = (to, label) self.__adjacency_list[source].add(edge) if label not in self.__label_map: self.__label_map[label] = HashSet() self.__label_map[label].add((source, to))
def __init__(self, reactants={}, catalysts=[], reversible=0, data=None): """Initializes a new Reaction object.""" # enforce invariants on reactants: self.reactants = reactants.copy() # loop over original, edit the copy for r, value in reactants.iteritems(): if value == 0: del self.reactants[r] self.catalysts = HashSet(catalysts).list() self.data = data self.reversible = reversible
def testSetOps(self): n = HashSet() a = HashSet(['a', 'b', 'c']) b = HashSet(['a', 'd', 'e', 'f']) c = HashSet(['g', 'h']) # union self.assertEqual(a.union(b), HashSet(['a','b','c','d','e','f']), "incorrect union") self.assertEqual(a.union(n), a, "incorrect union with empty set") # intersection self.assertEqual(a.intersection(b), HashSet(['a']), "incorrect intersection") self.assertEqual(a.intersection(c), HashSet(), "incorrect intersection") self.assertEqual(a.intersection(n), HashSet(), "incorrect intersection with empty set") # difference self.assertEqual(a.difference(b), HashSet(['b','c']), "incorrect difference") self.assertEqual(a.difference(c), HashSet(['a','b','c']), "incorrect difference") self.assertEqual(b.difference(a), HashSet(['d','e','f']), "incorrect difference") # cartesian product self.assertEqual(a.cartesian(c), HashSet([('a','g'),('a','h'), ('b','g'),('b','h'), ('c','g'),('c','h')]), "incorrect cartesian product") self.assertEqual(a.cartesian(n), HashSet(), "incorrect cartesian product")
class System: """Abstraction for a collection of reactions. This class is used in the Bio.Pathway framework to represent an arbitrary collection of reactions without explicitly defined links. Attributes: None """ def __init__(self, reactions = []): """Initializes a new System object.""" self.__reactions = HashSet(reactions) def __repr__(self): """Returns a debugging string representation of self.""" return "System(" + ",".join(map(repr,self.__reactions.list())) + ")" def __str__(self): """Returns a string representation of self.""" return "System of " + str(len(self.__reactions)) + \ " reactions involving " + str(len(self.species())) + \ " species" def add_reaction(self, reaction): """Adds reaction to self.""" self.__reactions.add(reaction) def remove_reaction(self, reaction): """Removes reaction from self.""" self.__reactions.remove(reaction) def reactions(self): """Returns a list of the reactions in this system.""" return self.__reactions.list() def species(self): """Returns a list of the species in this system.""" s = HashSet(reduce(lambda s,x: s + x, [x.species() for x in self.reactions()], [])) return s.list() def stochiometry(self): """Computes the stoichiometry matrix for self. Returns (species, reactions, stoch) where species = ordered list of species in this system reactions = ordered list of reactions in this system stoch = 2D array where stoch[i][j] is coef of the jth species in the ith reaction, as defined by species and reactions above """ # Note: This an inefficient and ugly temporary implementation. # To be practical, stochiometric matrices should probably # be implemented by sparse matrices, which would require # NumPy dependencies. # # PS: We should implement automatic checking for NumPy here. species = self.species() reactions = self.reactions() stoch = [] * len(reactions) for i in range(len(reactions)): stoch[i] = 0 * len(species) for s in reactions[i].species(): stoch[species.index(s)] = reactions[i].reactants[s] return (species, reactions, stoch)
def __init__(self, reactions=[]): """Initializes a new System object.""" self.__reactions = HashSet(reactions)
def children(self, parent): """Returns a list of unique children for parent.""" s = HashSet([x[0] for x in self.child_edges(parent)]) return s.list()
def species(self): """Returns a list of the species in this system.""" s = HashSet( reduce(lambda s, x: s + x, [x.species() for x in self.reactions()], [])) return s.list()
def parents(self, child): """Returns a list of unique parents for child.""" s = HashSet([x[0] for x in self.parent_edges(child)]) return s.list()
def testEquals(self): self.assertEqual(HashSet(['a','b']), HashSet(['b','a']), "not equal to similar") self.assertEqual(HashSet(), HashSet(), "empty set not equal to similar") self.assertNotEqual(HashSet(['a','b','c']), HashSet(), "non-empty equal to empty") self.assertNotEqual(HashSet(['a','b']), HashSet(['a','b','c']), "equal to superset") self.assertNotEqual(HashSet(['a','b']), HashSet(['a']), "equal to subset")
def add_node(self, node): """Adds a node to this graph.""" if node not in self.__adjacency_list: self.__adjacency_list[node] = HashSet()
def species(self): """Returns a list of the species in this system.""" s = HashSet(reduce(lambda s,x: s + x, [x.species() for x in self.reactions()], [])) return s.list()
def __init__(self, reactions = []): """Initializes a new System object.""" self.__reactions = HashSet(reactions)
class System: """Abstraction for a collection of reactions. This class is used in the Bio.Pathway framework to represent an arbitrary collection of reactions without explicitly defined links. Attributes: None """ def __init__(self, reactions=[]): """Initializes a new System object.""" self.__reactions = HashSet(reactions) def __repr__(self): """Returns a debugging string representation of self.""" return "System(" + ",".join(map(repr, self.__reactions.list())) + ")" def __str__(self): """Returns a string representation of self.""" return "System of " + str(len(self.__reactions)) + \ " reactions involving " + str(len(self.species())) + \ " species" def add_reaction(self, reaction): """Adds reaction to self.""" self.__reactions.add(reaction) def remove_reaction(self, reaction): """Removes reaction from self.""" self.__reactions.remove(reaction) def reactions(self): """Returns a list of the reactions in this system.""" return self.__reactions.list() def species(self): """Returns a list of the species in this system.""" s = HashSet( reduce(lambda s, x: s + x, [x.species() for x in self.reactions()], [])) return s.list() def stochiometry(self): """Computes the stoichiometry matrix for self. Returns (species, reactions, stoch) where species = ordered list of species in this system reactions = ordered list of reactions in this system stoch = 2D array where stoch[i][j] is coef of the jth species in the ith reaction, as defined by species and reactions above """ # Note: This an inefficient and ugly temporary implementation. # To be practical, stochiometric matrices should probably # be implemented by sparse matrices, which would require # NumPy dependencies. # # PS: We should implement automatic checking for NumPy here. species = self.species() reactions = self.reactions() stoch = [] * len(reactions) for i in range(len(reactions)): stoch[i] = 0 * len(species) for s in reactions[i].species(): stoch[species.index(s)] = reactions[i].reactants[s] return (species, reactions, stoch)