From c954803afbcb2fb7ceccc1ace10a8f32286127ed Mon Sep 17 00:00:00 2001 From: Sean Colby Date: Wed, 8 Jan 2025 17:06:31 -0800 Subject: [PATCH] Separate `finish` and `parse` functionality --- isicle/mobility.py | 24 +++++++++++++--- isicle/parse.py | 72 ++++------------------------------------------ 2 files changed, 26 insertions(+), 70 deletions(-) diff --git a/isicle/mobility.py b/isicle/mobility.py index d5e20a7..7d4cec7 100644 --- a/isicle/mobility.py +++ b/isicle/mobility.py @@ -1,3 +1,4 @@ +import glob import os import shutil import subprocess @@ -116,10 +117,10 @@ def _configure_mobcal(self, i2=5013489, buffer_gas='helium', 'IMP': imp, 'NUM_THREADS': processes} - self.mobcal_params = os.path.join(self.temp_dir, + self._mobcal_params = os.path.join(self.temp_dir, 'mobcal.params') - with open(self.mobcal_params, 'w') as f: + with open(self._mobcal_params, 'w') as f: f.write('\n'.join(['{} {}'.format(k, v) for k, v in d.items()])) f.write('\n') @@ -175,13 +176,28 @@ def finish(self): # Result container result = {} + # Enumerate output files + for outfile in outfiles: + # Split name and extension + basename, ext = os.path.basename(outfile).rsplit('.', 1) + + # Read output content + with open(outfile, 'rb') as f: + contents = f.read() + + # Attempt utf-8 decode + try: + result[ext] = contents.decode('utf-8') + except UnicodeDecodeError: + result[ext] = contents + # Assign to attribute self.result = result return self.result def parse(self): """ - Parse ORCA simulation results. + Parse Mobcal simulation results. Returns ------- @@ -191,7 +207,7 @@ def parse(self): """ if self.result is None: - raise RuntimeError("Must complete ORCA simulation.") + raise RuntimeError("Must complete Mobcal simulation.") parser = isicle.parse.MobcalParser(data=self.result) diff --git a/isicle/parse.py b/isicle/parse.py index 01d21d5..2f88926 100644 --- a/isicle/parse.py +++ b/isicle/parse.py @@ -353,9 +353,6 @@ def parse(self): return result - def save(self, path): - isicle.io.save_pickle(path, self.result) - class NWChemParser(FileParserInterface): """Extract information from NWChem simulation output files.""" @@ -748,9 +745,6 @@ def parse(self): return result - def save(self, path): - isicle.io.save_pickle(path, self.result) - class ImpactParser(FileParserInterface): """ @@ -812,41 +806,26 @@ def parse(self): self.result = result return result # TODO: return CCS? - def save(self, path: str, sep="\t"): - """ - Write parsed object to file - """ - pd.DataFrame(self.result).to_csv(path, sep=sep, index=False) - return - class MobcalParser(FileParserInterface): """ Extract text from a MOBCAL mobility calculation output file. """ - def __init__(self): - """ - Add docstring - """ - self.contents = None - self.result = {} + def __init__(self, data=None): + self.data = data - def load(self, path: str): - """ - Load in the data file - """ - with open(path, "r") as f: - self.contents = f.readlines() + self.result = {} - return self.contents + def load(self, path): + self.data = isicle.io.load_pickle(path) def parse(self): """ Extract relevant information from data """ done = False - for line in self.contents: + for line in self.data["out"]: # if "average (second order) TM mobility" in line: # m_mn = float(line.split('=')[-1]) if "average TM cross section" in line: @@ -860,37 +839,6 @@ def parse(self): return self.result - def save(self, path: str, sep="\t"): - """ - Write parsed object to file - """ - pd.DataFrame(self.result).to_csv(path, sep=sep, index=False) - return - - -class SanderParser(FileParserInterface): - """ - Extract text from an Sander simulated annealing simulation output file. - """ - - def load(self, path: str): - """ - Load in the data file - """ - raise NotImplementedError - - def parse(self): - """ - Extract relevant information from data - """ - raise NotImplementedError - - def save(self, path: str): - """ - Write parsed object to file - """ - raise NotImplementedError - class XTBParser(FileParserInterface): """ @@ -1160,11 +1108,3 @@ def parse(self): result["energy"] = self._crest_energy() return result - - def save(self, path): - """ - Add docstring - """ - with open(path, "wb") as f: - pickle.dump(self, f) - return