def _read_bond(self, bo): b = bond(self.paper) b.order = gtml_to_bkchem_bond_order_remap.index( dom_ext.getAllTextFromElement(xpath.Evaluate("bond", bo)[0])) ids = [i.nodeValue for i in xpath.Evaluate("end/@idref", bo)] b.atoms = [self._atom_id_remap[i] for i in ids] return b
def add_atom_to( self, a1, bond_to_use=None, pos=None): """adds new atom bound to atom id with bond, the position of new atom can be specified in pos or is decided calling find_place(), if x, y is specified and matches already existing atom it will be used instead of creating new one """ if pos != None: x, y = pos else: if bond_to_use: x, y = self.find_place( a1, Screen.any_to_px( self.paper.standard.bond_length), added_order=bond_to_use.order) else: x, y = self.find_place( a1, Screen.any_to_px( self.paper.standard.bond_length)) a2 = None # the new atom if pos: # try if the coordinates are the same as of another atom for at in self.atoms: if abs( at.x - x) < 2 and abs( at.y - y) < 2 and not at == a1: a2 = at break if not a2: a2 = self.create_new_atom( x, y) b = bond_to_use or bond( self.paper.standard, order=1, type='n') self.add_edge( a1, a2, e=b) b.molecule = self b.draw() return a2, b
def add_atom_to(self, a1, bond_to_use=None, pos=None): """adds new atom bound to atom id with bond, the position of new atom can be specified in pos or is decided calling find_place(), if x, y is specified and matches already existing atom it will be used instead of creating new one """ if pos != None: x, y = pos else: if bond_to_use: x, y = self.find_place(a1, Screen.any_to_px( self.paper.standard.bond_length), added_order=bond_to_use.order) else: x, y = self.find_place( a1, Screen.any_to_px(self.paper.standard.bond_length)) a2 = None # the new atom if pos: # try if the coordinates are the same as of another atom for at in self.atoms: if abs(at.x - x) < 2 and abs(at.y - y) < 2 and not at == a1: a2 = at break if not a2: a2 = self.create_new_atom(x, y) b = bond_to_use or bond(self.paper.standard, order=1, type='n') self.add_edge(a1, a2, e=b) b.molecule = self b.draw() return a2, b
def read_package(self, package): """reads the dom element package and sets internal state according to it""" # the standard std = self.paper and self.paper.standard or None self.name = package.getAttribute('name') if package.getAttribute('id'): self.id = package.getAttribute('id') for name, cls in { 'atom': atom, 'group': group, 'text': textatom, 'query': queryatom }.iteritems(): for a in dom_extensions.simpleXPathSearch(package, name): self.insert_atom(cls(standard=std, package=a, molecule=self)) self._id_map = [a.id for a in self.atoms] for b in dom_extensions.simpleXPathSearch(package, 'bond'): bnd = bond(standard=std, package=b, molecule=self) self.add_edge(bnd.atom1, bnd.atom2, bnd) # template related attributes temp = package.getElementsByTagName('template') if temp: temp = temp[0] self.t_atom = Store.id_manager.get_object_with_id( temp.getAttribute('atom')) if temp.getAttribute('bond_first') and temp.getAttribute( 'bond_second'): self.t_bond_first = Store.id_manager.get_object_with_id( temp.getAttribute('bond_first')) self.t_bond_second = Store.id_manager.get_object_with_id( temp.getAttribute('bond_second')) self.next_to_t_atom = self.t_atom.neighbors[0] # display form df = package.getElementsByTagName('display-form') if df: df = df[0] self.display_form = ''.join([e.toxml() for e in df.childNodes ]).encode('utf-8') # fragments for fel in dom_extensions.simpleXPathSearch(package, "fragment"): f = fragment() try: f.read_package(fel) except bkchem_exceptions.bkchem_fragment_error: pass else: self.fragments.add(f) ud = dom_extensions.getChildrenNamed(package, "user-data") if ud: self.user_data = [u.cloneNode(True) for u in ud] # final check of atoms valecies [ a.raise_valency_to_senseful_value() for a in self.vertices if isinstance(a, atom) ]
def linkMutate(self): inNode = None outNode = None itrCount = 0 # make sure the while loop ends if no more connections can be made while (inNode is None or outNode is None) and itrCount <= len(self.nodes)**2: node1 = random.choice(self.nodes) node2 = random.choice(self.nodes) itrCount += 1 if node1.id != node2.id and self.compatibleNodes(node1, node2) == True: if node2.type == "i" or node1.type == "o": inNode = node2 outNode = node1 else: inNode = node1 outNode = node2 if inNode is not None: b = bond() b.init(inNode, outNode, random.uniform(-2, 2), True) b = self.createBondFromInnovation(b, False) self.bonds.append(b) # check that no loop exists if self.bondCreatesLoop([b.inNode.id], b.inNode, b.outNode): tempNode = b.inNode b.inNode = b.outNode b.outNode = tempNode # self.printBond(b) # increment inputs for out node b.outNode.inputs += 1 else: print("***Could not complete link mutation***")
def read_cdml(text): """returns the last molecule for now""" doc = dom.parseString(text) #if doc.childNodes()[0].nodeName == 'svg': # path = "/svg/cdml/molecule" #else: # path = "/cdml/molecule" path = "//molecule" do_not_continue_this_mol = 0 for mol_el in dom_ext.simpleXPathSearch(doc, path): atom_id_remap = {} mol = molecule() groups = [] for atom_el in dom_ext.simpleXPathSearch(mol_el, "atom"): name = atom_el.getAttribute('name') if not name: #print "this molecule has an invalid symbol" do_not_continue_this_mol = 1 break pos = dom_ext.simpleXPathSearch(atom_el, 'point')[0] x = cm_to_float_coord(pos.getAttribute('x')) y = cm_to_float_coord(pos.getAttribute('y')) z = cm_to_float_coord(pos.getAttribute('z')) if name in PT: # its really an atom a = atom(symbol=name, charge=atom_el.getAttribute('charge') and int(atom_el.getAttribute('charge')) or 0, coords=(x, y, z)) mol.add_vertex(v=a) elif name in cdml_to_smiles: # its a known group group = smiles.text_to_mol(cdml_to_smiles[name], calc_coords=0) a = group.vertices[0] a.x = x a.y = y a.z = z mol.insert_a_graph(group) atom_id_remap[atom_el.getAttribute('id')] = a if do_not_continue_this_mol: break for bond_el in dom_ext.simpleXPathSearch(mol_el, "bond"): type = bond_el.getAttribute('type') if type[1] == u'0': # we ignore bonds with order 0 continue v1 = atom_id_remap[bond_el.getAttribute('start')] v2 = atom_id_remap[bond_el.getAttribute('end')] e = bond(order=int(type[1]), type=type[0]) mol.add_edge(v1, v2, e=e) if mol.is_connected(): # this is here to handle diborane and similar weird things yield mol else: for comp in mol.get_disconnected_subgraphs(): yield comp
def read_cdml( text): """returns the last molecule for now""" doc = dom.parseString( text) #if doc.childNodes()[0].nodeName == 'svg': # path = "/svg/cdml/molecule" #else: # path = "/cdml/molecule" path = "//molecule" do_not_continue_this_mol = 0 for mol_el in dom_ext.simpleXPathSearch( doc, path): atom_id_remap = {} mol = molecule() groups = [] for atom_el in dom_ext.simpleXPathSearch( mol_el, "atom"): name = atom_el.getAttribute( 'name') if not name: #print "this molecule has an invalid symbol" do_not_continue_this_mol = 1 break pos = dom_ext.simpleXPathSearch( atom_el, 'point')[0] x = cm_to_float_coord( pos.getAttribute('x')) y = cm_to_float_coord( pos.getAttribute('y')) z = cm_to_float_coord( pos.getAttribute('z')) if name in PT: # its really an atom a = atom( symbol=name, charge=atom_el.getAttribute( 'charge') and int( atom_el.getAttribute( 'charge')) or 0, coords=( x, y, z)) mol.add_vertex( v=a) elif name in cdml_to_smiles: # its a known group group = smiles.text_to_mol( cdml_to_smiles[ name], calc_coords=0) a = group.vertices[0] a.x = x a.y = y a.z = z mol.insert_a_graph( group) atom_id_remap[ atom_el.getAttribute( 'id')] = a if do_not_continue_this_mol: break for bond_el in dom_ext.simpleXPathSearch( mol_el, "bond"): type = bond_el.getAttribute( 'type') if type[1] == u'0': # we ignore bonds with order 0 continue v1 = atom_id_remap[ bond_el.getAttribute( 'start')] v2 = atom_id_remap[ bond_el.getAttribute( 'end')] e = bond( order=int( type[1]), type=type[0]) mol.add_edge( v1, v2, e=e) if mol.is_connected(): # this is here to handle diborane and similar weird things yield mol else: for comp in mol.get_disconnected_subgraphs(): yield comp
def edges_to_bonds(self, edges, covalances): """ Converts list of edges into bong objects :param edges: list of pairs of nodes names :param covalances: list of covalance values for each link :type edge: List :type covalence: List """ for i, edge in enumerate(edges): key = self.edge_to_key(edge) self.edges[key] = bond.bond(covalances[i])
def bond_request_thread(country, results, position): bonds_in_country = investpy.bonds.get_bonds(country)["name"] bonds_list = [] for bond_name in bonds_in_country: bond_data = investpy.bonds.get_bond_information(bond_name) #print(bond_data["Maturity Date"][0]) #print(bond_data["Coupon"][0]) if bond_data["Maturity Date"][0] is not None and bond_data["Price"][0] is not None and datetime.strptime(bond_data["Maturity Date"][0], '%d/%m/%Y').date() > date.today(): if 50 < bond_data["Price"][0] and bond_data["Price"][0] < 150: bonds_list.append(bond(bond_data["Price"][0], bond_data["Maturity Date"][0], bond_data["Coupon"][0])) results[position] = bonds_list
def nodeMutate(self): # choose a random ENABLED bond and disable it b = None itrCount = 0 while (b == None) and itrCount <= len(self.nodes)**2: itrCount += 1 b = random.choice(self.bonds) if b.enabled == False: b = None if b == None: return b.enabled = False inNode = b.inNode outNode = b.outNode # create middle node middleNode = node() middleNode.init(0, self.idCount, "h") self.idCount += 1 middleNode.inputs = 1 self.nodes.append(middleNode) # create bond from inNode to middleNode b1 = bond() b1.init(inNode, middleNode, 1, True) b1 = self.createBondFromInnovation(b1, True) middleNode = b1.outNode self.bonds.append(b1) # create bond from middleNode to outNode b2 = bond() b2.init(middleNode, outNode, b.weight, True) b2 = self.createBondFromInnovation(b2, False) self.bonds.append(b2)
def add_if_not_when_issued_or_dup(self, security): ''' Add securities that satisfy specs to self.value, self.list_CUSIPs. ''' if len(security["interestRate"]) > 0 and not (security["cusip"] in self.list_CUSIPs): ust_mat_date = self.get_maturity_date(security) time_to_maturity = (ust_mat_date - date.today()).days / 365.25 time_to_maturity = str('%.2f' % time_to_maturity) price = self.get_bond_prices(security['cusip']) new_bond = bond.bond(ust_mat_date, self.settle_date, float(security['interestRate']) / 100, 2) self.value.append({'mat_date': ''.join([security["maturityDate"][0:10]]), 'int_rate': round(float(security["interestRate"]), 3), \ 'cpns_per_yr': 2, 'ttm': time_to_maturity, 'cusip': security["cusip"], 'issue_date': security['issueDate'][0:10], \ 'yield': new_bond.bond_yield(price), 'price': price}) self.list_CUSIPs.append(security["cusip"])
def read_package( self, package): """reads the dom element package and sets internal state according to it""" # the standard std = self.paper and self.paper.standard or None self.name = package.getAttribute( 'name') if package.getAttribute( 'id'): self.id = package.getAttribute( 'id') for name, cls in {'atom': atom, 'group': group, 'text': textatom, 'query': queryatom}.items(): for a in dom_extensions.simpleXPathSearch( package, name): self.insert_atom( cls( standard=std, package=a, molecule=self)) self._id_map = [a.id for a in self.atoms] for b in dom_extensions.simpleXPathSearch( package, 'bond'): bnd = bond( standard=std, package=b, molecule=self) self.add_edge( bnd.atom1, bnd.atom2, bnd) # template related attributes temp = package.getElementsByTagName('template') if temp: temp = temp[0] self.t_atom = Store.id_manager.get_object_with_id( temp.getAttribute( 'atom')) if temp.getAttribute('bond_first') and temp.getAttribute('bond_second'): self.t_bond_first = Store.id_manager.get_object_with_id( temp.getAttribute( 'bond_first')) self.t_bond_second = Store.id_manager.get_object_with_id( temp.getAttribute( 'bond_second')) self.next_to_t_atom = self.t_atom.neighbors[0] # display form df = package.getElementsByTagName('display-form') if df: df = df[0] self.display_form = ''.join( [e.toxml('utf-8') for e in df.childNodes]) # fragments for fel in dom_extensions.simpleXPathSearch( package, "fragment"): f = fragment() try: f.read_package( fel) except bkchem_exceptions.bkchem_fragment_error: pass else: self.fragments.add( f) ud = dom_extensions.getChildrenNamed( package, "user-data") if ud: self.user_data = [u.cloneNode( True) for u in ud] # final check of atoms valecies [a.raise_valency_to_senseful_value() for a in self.vertices if isinstance( a, atom)]
def init(self, inputs, outputs, rdm, innovations): # Create Input and Output nodes self.createNodeGroup(inputs, 'i') self.createNodeGroup(outputs, 'o') self.storedInnovations = innovations if rdm == False: return # Randomly make bonds from inputs to outputs or to hidden nodes while (len(self.bonds) == 0): for n in self.inputNodes: if random.randint(0, 1) == 1: output = random.choice(self.outputNodes) if not self.doesBondExist(n, output): output.inputs += 1 b = bond() b.init(n, output, random.uniform(-1, 1), True) b = self.createBondFromInnovation(b, False) self.bonds.append(b) return self.storedInnovations
def pybel_to_oasa_bond(self, pbond): obond = bond() obond.order = pbond.GetBondOrder() return obond
def main(): settle_date = date(2018, 7, 5) b1 = bond.bond(date(2020, 6, 30), settle_date, 0.025, 2) return b1.bond_yield() '''
def create_edge(self): std = self.paper and self.paper.standard or Store.app.paper.standard return bond(standard=std)
def oasa_bond_to_bkchem_bond(b, paper): bo = bond.bond(standard=paper.standard) bo.type = b.type bo.order = b.order return bo
def test_yield(self): settle_date = date(2018, 7, 5) b1 = bond.bond(date(2020, 6, 30), settle_date, 0.025, 2) self.assertEqual(b1.bond_yield(99.914), 2.545)
def create_edge(self): return bond()
def create_edge( self): return bond()
def oasa_bond_to_bkchem_bond( b, paper): bo = bond.bond( standard=paper.standard) bo.type = b.type bo.order = b.order return bo
def _read_bond(self, bo): b = bond(self.paper) b.order = gtml_to_bkchem_bond_order_remap.index(dom_ext.getAllTextFromElement(xpath.Evaluate("bond", bo)[0])) ids = [i.nodeValue for i in xpath.Evaluate("end/@idref", bo)] b.atoms = [self._atom_id_remap[i] for i in ids] return b
def pybel_to_oasa_bond( self, pbond): obond = bond() obond.order = pbond.GetBondOrder() return obond
def create_edge( self): std = self.paper and self.paper.standard or Store.app.paper.standard return bond( standard=std)