Skip to content

Commit

Permalink
Separate finish and parse functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
smcolby committed Jan 9, 2025
1 parent 7c3c996 commit c954803
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 70 deletions.
24 changes: 20 additions & 4 deletions isicle/mobility.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import glob
import os
import shutil
import subprocess
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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
-------
Expand All @@ -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)

Expand Down
72 changes: 6 additions & 66 deletions isicle/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -748,9 +745,6 @@ def parse(self):

return result

def save(self, path):
isicle.io.save_pickle(path, self.result)


class ImpactParser(FileParserInterface):
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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

0 comments on commit c954803

Please sign in to comment.