def test_to_dict_from_etree(self): vh_xml_tree = ElementTree.parse(self.vh_xml_file) col_xml_tree = ElementTree.parse(self.col_xml_file) xml_dict = self.vh_schema.to_dict(vh_xml_tree) self.assertNotEqual(xml_dict, VEHICLES_DICT) xml_dict = self.vh_schema.to_dict(vh_xml_tree, namespaces=self.vh_namespaces) self.assertEqual(xml_dict, VEHICLES_DICT) xml_dict = xmlschema.to_dict(vh_xml_tree, self.vh_schema.url, namespaces=self.vh_namespaces) self.assertEqual(xml_dict, VEHICLES_DICT) xml_dict = self.col_schema.to_dict(col_xml_tree) self.assertNotEqual(xml_dict, COLLECTION_DICT) xml_dict = self.col_schema.to_dict(col_xml_tree, namespaces=self.col_namespaces) self.assertEqual(xml_dict, COLLECTION_DICT) xml_dict = xmlschema.to_dict(col_xml_tree, self.col_schema.url, namespaces=self.col_namespaces) self.assertEqual(xml_dict, COLLECTION_DICT)
def test_to_dict_from_string(self): with open(os.path.join(self.test_dir, 'examples/vehicles/vehicles.xml')) as f: vh_xml_string = f.read() with open( os.path.join(self.test_dir, 'examples/collection/collection.xml')) as f: col_xml_string = f.read() xml_dict = self.vh_schema.to_dict(vh_xml_string, namespaces=self.namespaces) self.assertEqual(xml_dict, _VEHICLES_DICT) xml_dict = xmlschema.to_dict(vh_xml_string, self.vh_schema.url, namespaces=self.namespaces) self.assertEqual(xml_dict, _VEHICLES_DICT) xml_dict = self.col_schema.to_dict(col_xml_string, namespaces=self.namespaces) self.assertTrue(xml_dict, _COLLECTION_DICT) xml_dict = xmlschema.to_dict(col_xml_string, self.col_schema.url, namespaces=self.namespaces) self.assertTrue(xml_dict, _COLLECTION_DICT)
def test_to_dict_from_etree(self): vh_xml_tree = _ElementTree.parse( os.path.join(self.test_dir, 'examples/vehicles/vehicles.xml')) col_xml_tree = _ElementTree.parse( os.path.join(self.test_dir, 'examples/collection/collection.xml')) xml_dict = self.vh_schema.to_dict(vh_xml_tree) self.assertNotEqual(xml_dict, _VEHICLES_DICT) # XSI namespace unmapped xml_dict = self.vh_schema.to_dict(vh_xml_tree, namespaces=self.namespaces) self.assertEqual(xml_dict, _VEHICLES_DICT) xml_dict = xmlschema.to_dict(vh_xml_tree, self.vh_schema.url, namespaces=self.namespaces) self.assertEqual(xml_dict, _VEHICLES_DICT) xml_dict = self.col_schema.to_dict(col_xml_tree) self.assertNotEqual(xml_dict, _COLLECTION_DICT) xml_dict = self.col_schema.to_dict(col_xml_tree, namespaces=self.namespaces) self.assertEqual(xml_dict, _COLLECTION_DICT) xml_dict = xmlschema.to_dict(col_xml_tree, self.col_schema.url, namespaces=self.namespaces) self.assertEqual(xml_dict, _COLLECTION_DICT)
def read_results(self): filename = self.label + '.xml' self.input = xmlschema.to_dict(filename, schema=self.schema, path=None)['input'] self.output = xmlschema.to_dict(filename, schema=self.schema, path=None)['output'] self.atoms = get_atoms_from_xml_output(filename, output=self.output) self.results['energy'] = float( self.output["total_energy"]["etot"]) * units.Ry
def read(self, xmlfile, schema=None): self._data = {} data = xmlschema.to_dict(xmlfile, schema) try: self['pseudodir'] = data["input"]["control_variables"][ "pseudo_dir"] except KeyError: self[ 'pseudodir'] = './' # DB: ['./', os.path.dirname(filename)] ... alternatives? self["prefix"] = data["input"]["control_variables"]["prefix"] self["outdir"] = data["input"]["control_variables"]["outdir"] dout = data["output"] self["ecutwfc"] = dout["basis_set"]["ecutwfc"] self["ecutrho"] = dout["basis_set"]["ecutrho"] self["alat"] = dout["atomic_structure"]["@alat"] self["ibrav"] = dout["atomic_structure"]["@bravais_index"] self['a'] = np.array([ np.array(dout["atomic_structure"]["cell"]["a1"]), np.array(dout["atomic_structure"]["cell"]["a2"]), np.array(dout["atomic_structure"]["cell"]["a3"]) ]) self['b'] = np.array([ np.array(dout["basis_set"]["reciprocal_lattice"]["b1"]), np.array(dout["basis_set"]["reciprocal_lattice"]["b2"]), np.array(dout["basis_set"]["reciprocal_lattice"]["b3"]) ]) self["functional"] = np.array(dout["dft"]["functional"]) self["nat"] = (dout["atomic_structure"]["@nat"]) # for subsequent loops it is important to have always lists for atomic_positions # and atomic_species. If this is not, convert a_p = dout["atomic_structure"]["atomic_positions"]["atom"] if isinstance(a_p, list): self["atomic_positions"] = a_p else: self["atomic_positions"] = [a_p] a_s = dout["atomic_species"]["species"] if isinstance(a_s, list): self["atomic_species"] = a_s else: self["atomic_species"] = [a_s] self["ntyp"] = dout["atomic_species"]["@ntyp"] self["lsda"] = dout["magnetization"]["lsda"] self["noncolin"] = dout["magnetization"]["noncolin"] self['nr'] = np.array([ dout["basis_set"]["fft_grid"]["@nr1"], dout["basis_set"]["fft_grid"]["@nr2"], dout["basis_set"]["fft_grid"]["@nr3"] ]) self['nr_smooth'] = np.array([ dout["basis_set"]["fft_smooth"]["@nr1"], dout["basis_set"]["fft_smooth"]["@nr2"], dout["basis_set"]["fft_smooth"]["@nr3"] ])
def test_lxml(self): vh_xml_tree = _lxml_etree.parse( os.path.join(self.test_dir, 'cases/examples/vehicles/vehicles.xml')) self.assertEqual(self.vh_schema.to_dict(vh_xml_tree), _VEHICLES_DICT) self.assertEqual(xmlschema.to_dict(vh_xml_tree, self.vh_schema.url), _VEHICLES_DICT)
def test_to_dict_from_string(self): with open(self.vh_xml_file) as f: vh_xml_string = f.read() with open(self.col_xml_file) as f: col_xml_string = f.read() xml_dict = self.vh_schema.to_dict(vh_xml_string, namespaces=self.vh_namespaces) self.assertEqual(xml_dict, VEHICLES_DICT) xml_dict = xmlschema.to_dict(vh_xml_string, self.vh_schema.url, namespaces=self.vh_namespaces) self.assertEqual(xml_dict, VEHICLES_DICT) xml_dict = self.col_schema.to_dict(col_xml_string, namespaces=self.col_namespaces) self.assertTrue(xml_dict, COLLECTION_DICT) xml_dict = xmlschema.to_dict(col_xml_string, self.col_schema.url, namespaces=self.col_namespaces) self.assertTrue(xml_dict, COLLECTION_DICT)
def read_results(self): # FIXME: compose XML output filename from parameters filename = self.directory + '/temp/pwscf.xml' self.output = xmlschema.to_dict(filename, schema=self.schema, path="./output") self.atoms = get_atoms_from_xml_output(filename, output=self.output) self.results['energy'] = float( self.output["total_energy"]["etot"]) * units.Ry
def validate_XSD_file(self, xml_fileinput): """| Usage | This keyword is used to validate am xml file against the XSD | Arguments | 'xml_fileinput' = Output xml file path. |XSD Validator | ${xml_fileinput} """ if not os.path.exists(xml_fileinput): raise AssertionError("File not found error :" + xml_fileinput + " doesnot exist") pprint(xmlschema.to_dict(xml_fileinput)) print(xmlschema.validate(xml_fileinput))
def get_atoms_from_xml_output(filename, schema=None, output=None): """ Returns an Atoms object constructed from an XML QE file (according to the schema). :param filename: Name of the XML file to read from or a file descriptor. :param schema: Optional XML Schema file to use (for default schema is \ found using the schemaLocation attribute of the XML root). :param output: Optional dictionary containing the output tree of the XML file. \ If provided skips file access and builds Atoms object directly from output dictionary. :return: An Atoms object. """ if output is None: output = xmlschema.to_dict(filename, schema=schema, path="./qes:espresso/output") a1 = np.array(output["atomic_structure"]["cell"]["a1"]) a2 = np.array(output["atomic_structure"]["cell"]["a2"]) a3 = np.array(output["atomic_structure"]["cell"]["a3"]) a_p = (output["atomic_structure"]["atomic_positions"]["atom"]) atoms = Atoms() # First define the unit cell from a1, a2, a3 and alat cell = np.zeros((3, 3)) cell[0] = a1 cell[1] = a2 cell[2] = a3 atoms.set_cell(cell) # Now the atoms in the unit cell for atomx in a_p: # TODO: extend to all possible cases the symbol splitting (for now, only numbering up to 9 work). Not a very common case... symbol = split_atomic_symbol(atomx['@name'])[0] x = float(atomx['$'][0]) y = float(atomx['$'][1]) z = float(atomx['$'][2]) atoms.append(Atom(symbol, (x, y, z))) return atoms
def save_converted_xml_to_csv(dataset_etree, csv_file, prefix=None, filename=None): transform = etree.XSLT(etree.parse(str(this_dir / "iati-activities.xsl"))) schema = xmlschema.XMLSchema( str(pathlib.Path() / "__iatikitcache__/standard/schemas/203/iati-activities-schema.xsd") ) schama_dict = get_sorted_schema_dict() for activity in dataset_etree.findall("iati-activity"): version = dataset_etree.get("version", "1.01") activities = etree.Element("iati-activities", version=version) activities.append(activity) if version.startswith("1"): activities = transform(activities).getroot() sort_iati_element(activities.getchildren()[0], schama_dict) activity, error = xmlschema.to_dict(activities, schema=schema, validation="lax", decimal_type=float) activity = activity.get("iati-activity", [{}])[0] csv_file.writerow([ prefix, filename, str(error) if error else "", version, json.dumps(activity), ])
def test_lxml(self): vh_xml_tree = lxml_etree.parse(self.vh_xml_file) self.assertEqual(self.vh_schema.to_dict(vh_xml_tree), VEHICLES_DICT) self.assertEqual(xmlschema.to_dict(vh_xml_tree, self.vh_schema.url), VEHICLES_DICT)
def to_dict(self): return xmlschema.to_dict(self._document, self.schema)
def test_lxml(self): vh_xml_tree = _lxml_etree.parse('examples/vehicles/vehicles.xml') self.assertEqual(self.vh_schema.to_dict(vh_xml_tree), _VEHICLES_DICT) self.assertEqual(xmlschema.to_dict(vh_xml_tree, self.vh_schema.url), _VEHICLES_DICT)
def read(self, xmlfile, schema=None): self._data = xmlschema.to_dict(xmlfile, schema)
2.0 * pi * complex(0.0, 1.0) * (j * deltax * (e2[0] * G[x, y, z, 0] + e2[1] * G[x, y, z, 1] + e2[2] * G[x, y, z, 2]))) for i in range(0, nx): for j in range(0, ny): temp[i, j] += fft_charge[x, y, z] * eigx[i] * eigy[j] Z = temp / (nr[0] * nr[1] * nr[2]) return X, Y, Z if __name__ == "__main__": pw_output = xmlschema.to_dict("../../examples/example5/Ni.xml", "../../postqe/schemas/qes.xsd")['output'] nr = np.array([ pw_output["basis_set"]["fft_grid"]["@nr1"], pw_output["basis_set"]["fft_grid"]["@nr2"], pw_output["basis_set"]["fft_grid"]["@nr3"] ]) a1 = np.array(pw_output["atomic_structure"]["cell"]["a1"]) a2 = np.array(pw_output["atomic_structure"]["cell"]["a2"]) a3 = np.array(pw_output["atomic_structure"]["cell"]["a3"]) a = np.array([a1, a2, a3]) b1 = np.array(pw_output["basis_set"]["reciprocal_lattice"]["b1"]) b2 = np.array(pw_output["basis_set"]["reciprocal_lattice"]["b2"]) b3 = np.array(pw_output["basis_set"]["reciprocal_lattice"]["b3"]) b = np.array([b1, b2, b3]) ecutrho = (pw_output["basis_set"]["ecutrho"])