def testGetInfoset(self): # fold>> graph = Graph.Graph() coords = Infoset.Infoset(graph, InfosetType.getCoordsType()) coords2 = Infoset.Infoset(graph, InfosetType.getCoordsType()) labels = Infoset.Infoset(graph, InfosetType.getAtomLabelType()) bondtypes = Infoset.Infoset(graph, InfosetType.getBondTypeType()) angles = Infoset.Infoset(graph, InfosetType.getAngleType()) energy = Infoset.Infoset(graph, InfosetType.getHFEnergyType()) self.assertEqual(len(graph.getInfosets()), 6) self.assertEqual(len(graph.getInfosets(dimensionality=0)), 1) self.assertEqual(len(graph.getInfosets(dimensionality=1)), 3) self.assertEqual(len(graph.getInfosets(dimensionality=2)), 1) self.assertEqual(len(graph.getInfosets(dimensionality=3)), 1) self.assertEqual(len(graph.getInfosets(dimensionality=4)), 0) self.assertEqual(len(graph.getInfosets(uuid=coords.uuid())), 1) self.assertEqual(len(graph.getInfosets(uuid=labels.uuid())), 1) self.assertEqual(len(graph.getInfosets(uuid=uuid.uuid4())), 0) self.assertEqual( len(graph.getInfosets(infoset_type=InfosetType.getCoordsType())), 2) self.assertEqual( len(graph.getInfosets(infoset_type=InfosetType.getHFEnergyType())), 1) self.assertEqual(len(graph.getInfosets(infoset_type=list)), 0) self.assertEqual( len(graph.getInfosets(uuid=labels.uuid(), dimensionality=1)), 1) self.assertEqual( len(graph.getInfosets(uuid=labels.uuid(), dimensionality=2)), 0)
def testSearch(self): reinitdb() db = DBAccess.DBAccess(os.path.join(moduleDir(), "testdb")) for i in xrange(1, 90): graph = Graph.Graph() coords = Infoset.Infoset(graph, InfosetType.getCoordsType()) labels = Infoset.Infoset(graph, InfosetType.getAtomLabelType()) bondtypes = Infoset.Infoset(graph, InfosetType.getBondTypeType()) energy = Infoset.Infoset(graph, InfosetType.getHFEnergyType()) angles = Infoset.Infoset(graph, InfosetType.getAngleType()) node1 = graph.createEntity() node2 = graph.createEntity() node3 = graph.createEntity() link = graph.createEntity((node1, node2)) angle = graph.createEntity((node1, node2, node3)) coords.setValue(node1, (10.0, 0.0, 0.0)) coords.setValue(node2, (10.0, 2.0, 0.0)) coords.setValue(node3, (10.0, 2.0, 3.0)) bondtypes.setValue(link, 2) angles.setValue(angle, Measure.Measure(float(i), Units.degrees)) db.store(graph)
def testStoreAndRetrieve(self): reinitdb() graph = Graph.Graph() coords = Infoset.Infoset(graph, InfosetType.getCoordsType()) labels = Infoset.Infoset(graph, InfosetType.getAtomLabelType()) bondtypes = Infoset.Infoset(graph, InfosetType.getBondTypeType()) energy = Infoset.Infoset(graph, InfosetType.getHFEnergyType()) angles = Infoset.Infoset(graph, InfosetType.getAngleType()) node1 = graph.createEntity() node2 = graph.createEntity() node3 = graph.createEntity() link = graph.createEntity((node1, node2)) angle = graph.createEntity((node1, node2, node3)) coords.setValue(node1, (10.0, 0.0, 0.0)) coords.setValue(node2, (10.0, 2.0, 0.0)) coords.setValue(node3, (10.0, 2.0, 3.0)) bondtypes.setValue(link, 2) angles.setValue(angle, Measure.Measure(45, Units.degrees)) db = DBAccess.DBAccess(os.path.join(moduleDir(), "testdb")) db.store(graph) molecule_list = db.retrieveAll() self.assertEqual(len(molecule_list), 1)
def testGraphInfoset(self): # fold>> graph = Graph.Graph() energy = Infoset.Infoset(graph, InfosetType.getHFEnergyType()) energy.setValue(None, Measure.Measure(10.4, Units.hartree)) self.assertEqual(energy.dimensionality(), 0) self.assertEqual(energy.size(), 1) self.assertEqual(energy.value(None).value(), 10.4) self.assertEqual(energy.value(None).unit(), Units.hartree)
def testSearch(self): reinitdb() db = DBAccess.DBAccess(os.path.join(moduleDir(), "testdb")) for i in xrange(1, 90): graph = Graph.Graph() coords = Infoset.Infoset(graph, InfosetType.getCoordsType()) labels = Infoset.Infoset(graph, InfosetType.getAtomLabelType()) bondtypes = Infoset.Infoset(graph, InfosetType.getBondTypeType()) energy = Infoset.Infoset(graph, InfosetType.getHFEnergyType()) angles = Infoset.Infoset(graph, InfosetType.getAngleType()) node1 = graph.createEntity() node2 = graph.createEntity() node3 = graph.createEntity() link = graph.createEntity((node1, node2)) angle = graph.createEntity((node1, node2, node3)) coords.setValue(node1, (10.0, 0.0, 0.0)) coords.setValue(node2, (10.0, 2.0, 0.0)) coords.setValue(node3, (10.0, 2.0, 3.0)) bondtypes.setValue(link, 2) angles.setValue(angle, Measure.Measure(float(i), Units.degrees)) db.store(graph) class Search: def __init__(self, angle): self._angle = angle def satisfiedBy(self, graph): infoset = graph.getInfosets( infoset_type=InfosetType.getAngleType())[0] if infoset is None: return False #if infoset.value(angle).value() > 45.0 and infoset.value(angle).value() < 48.0: # return True if infoset.value(angle).value() % 2 == 0: return True return False results = db.search(Search(angle)) #self.assertEqual(len(results), 2) for i in results: print i.getInfosets( infoset_type=InfosetType.getAngleType())[0].value(angle)
def browsedb(request,dbname): try: db = DBAccess.DBAccess(os.path.join(settings.DB_PATH, dbname)) except: raise Http404 molecule_list = [] all = db.retrieveAll() molecule_list = [] for graph in all: molecule={} code = graph.getInfosets(infoset_type=InfosetType.getMoleculeCodeType()) if len(code) == 0: continue else: molecule["code"] = code[0].value(graph) name = graph.getInfosets(infoset_type=InfosetType.getConventionalMoleculeNameType()) if len(name) == 0: molecule["name"] = molecule["code"] else: molecule["name"] = name[0].value(graph) elements = graph.getInfosets(infoset_type=InfosetType.getElementType()) if len(elements) == 0: continue else: molecule["num_of_atoms"] = elements[0].size() molecule["brute_formula"] = Utilities.hillFormula(map(lambda x: x[1], elements[0].allValues())) hf_energy = graph.getInfosets(infoset_type=InfosetType.getHFEnergyType()) if len(hf_energy) == 0: continue else: molecule["hf_energy"] = hf_energy[0].value(graph) #molecule["uuid"] = graph.uuid() molecule_list.append(molecule) database = { "name": dbname } return render_to_response("application/browsedb.html", { "database" : database, "molecule_list" : molecule_list })
def searchdb(request): if request.method == "POST" and request.POST: db_name = request.POST["db_name"] page = int(request.POST['page']) rows_per_page = int(request.POST['rp']) sortname = request.POST['sortname'] sortorder = request.POST['sortorder'] else: raise Http404 try: db = DBAccess.DBAccess(os.path.join(settings.DB_PATH, db_name)) except: raise Http404 all = db.retrieveAll() json_response = {"page": page, "total": len(all), "rows": []} rows = [] for id, graph in enumerate(all): molecule = {} code = graph.getInfosets( infoset_type=InfosetType.getMoleculeCodeType()) if len(code) == 0: continue else: # FIXME possible danger of injection of db_name by the user molecule["code"] = "<a href=\"/browsedb/" + db_name + "/" + code[ 0].value(graph) + "\">" + code[0].value(graph) + "</a>" name = graph.getInfosets( infoset_type=InfosetType.getConventionalMoleculeNameType()) if len(name) == 0: molecule["name"] = molecule["code"] else: molecule["name"] = name[0].value(graph) elements = graph.getInfosets(infoset_type=InfosetType.getElementType()) if len(elements) == 0: continue else: molecule["num_of_atoms"] = elements[0].size() molecule["brute_formula"] = Utilities.hillFormula( map(lambda x: x[1], elements[0].allValues())) hf_energy = graph.getInfosets( infoset_type=InfosetType.getHFEnergyType()) if len(hf_energy) == 0: molecule["hf_energy"] = "" else: molecule["hf_energy"] = hf_energy[0].value(graph).value() row = {} row["id"] = molecule["code"] row["cell"] = [ molecule["code"], molecule["name"], molecule["brute_formula"], molecule["num_of_atoms"], molecule["hf_energy"], "" ] rows.append(row) json_response["rows"].extend( sortAndTrim(rows, page, rows_per_page, sortname, sortorder)) json_response = { "total": 3, "page": 1, "rows": [{ "cell": ["foo", "bar"] }, { "cell": ["foo", "bar"] }, { "cell": ["foo", "bar"] }] } return HttpResponse(simplejson.dumps(json_response), mimetype='application/javascript')