importtempfileimporturllib.requestimportzipfilefrompathlibimportPathimportase.ioimportzntrackfromaseimportunitsimportipsuiteasipsfromipsuiteimportfieldsdefmodify_xyz_file(file_path,target_string,replacement_string):new_file_path=file_path.with_name(file_path.stem+"_mod"+file_path.suffix)withfile_path.open("r")asinput_file,new_file_path.open("w")asoutput_file:forlineininput_file:# Replace all occurrences of the target string with the replacement stringmodified_line=line.replace(target_string,replacement_string)output_file.write(modified_line)returnnew_file_pathdefdownload_data(url:str,data_path:Path):url_path=Path(urllib.parse.urlparse(url).path)zip_path=data_path/url_path.stemfile_path=zip_path.with_suffix(".xyz")urllib.request.urlretrieve(url,zip_path)withzipfile.ZipFile(zip_path,"r")aszip_ref:zip_ref.extractall(data_path)file_path=modify_xyz_file(file_path,target_string="Energy",replacement_string="energy")returnfile_path
[docs]defrun(self):tmpdir=tempfile.TemporaryDirectory()raw_data_dir=Path(tmpdir.name)/"raw_data"raw_data_dir.mkdir(parents=True,exist_ok=True)ifself.datasetnotinself.datasets.keys():raiseFileNotFoundError(f"Dataset {self.dataset} is not known. Key has top be in {self.datasets}")url=self.datasets[self.dataset]file_path=download_data(url,raw_data_dir)self.frames=ase.io.read(file_path,":")foratomsinself.frames:atoms.calc.results["energy"]*=units.kcal/units.molatoms.calc.results["forces"]*=units.kcal/units.moltmpdir.cleanup()