Skip to content

Commit 317f972

Browse files
Merge pull request #44 from Luke2Sky/LucasMilitao
Improvements to the way failures are handled and state saving
2 parents 18eb3f3 + 0ec9d26 commit 317f972

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

scripts/multi_component_hydrogen_bond_propensity/multi_component_hydrogen_bond_propensity_report.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import argparse
2121
import tempfile
2222
import subprocess
23+
import json
2324

2425
import matplotlib
2526

@@ -313,7 +314,7 @@ def make_mc_report(identifier, results, directory, diagram_file, chart_file):
313314
launch_word_processor(output_file)
314315

315316

316-
def main(structure, work_directory, library, csdrefcode):
317+
def main(structure, work_directory, failure_directory, library, csdrefcode):
317318
# This loads up the CSD if a refcode is requested, otherwise loads the structural file supplied
318319
if csdrefcode:
319320
try:
@@ -334,6 +335,7 @@ def main(structure, work_directory, library, csdrefcode):
334335
coformer_files = glob.glob(os.path.join(library, '*.mol2'))
335336
tempdir = tempfile.mkdtemp()
336337
mc_dictionary = {}
338+
failures = []
337339

338340
# for each coformer in the library, make a pair file for the api/coformer and run a HBP calculation
339341
for i, f in enumerate(coformer_files):
@@ -343,22 +345,33 @@ def main(structure, work_directory, library, csdrefcode):
343345
crystal = crystal_reader[0]
344346

345347
directory = os.path.join(os.path.abspath(work_directory), crystal.identifier)
346-
347-
try:
348-
propensities, donors, acceptors = propensity_calc(crystal, directory)
349-
coordination_scores = coordination_scores_calc(crystal, directory)
350-
pair_output(crystal.identifier, propensities, donors, acceptors, coordination_scores, directory)
351-
mc_dictionary[coformer_name] = get_mc_scores(propensities, crystal.identifier)
352-
353-
except RuntimeError:
354-
print("Propensity calculation failure for %s!" % coformer_name)
355-
mc_dictionary[coformer_name] = ["N/A", "N/A", "N/A", "N/A", "N/A", crystal.identifier]
348+
if os.path.exists(os.path.join(directory, "success.json")):
349+
with open(os.path.join(directory, "success.json"), "r") as file:
350+
tloaded = json.load(file)
351+
mc_dictionary[coformer_name] = tloaded
352+
else:
353+
try:
354+
propensities, donors, acceptors = propensity_calc(crystal, directory)
355+
coordination_scores = coordination_scores_calc(crystal, directory)
356+
pair_output(crystal.identifier, propensities, donors, acceptors, coordination_scores, directory)
357+
with open(os.path.join(directory, "success.json"), "w") as file:
358+
tdata = get_mc_scores(propensities, crystal.identifier)
359+
json.dump(tdata, file)
360+
mc_dictionary[coformer_name] = get_mc_scores(propensities, crystal.identifier)
361+
print(get_mc_scores(propensities, crystal.identifier))
362+
except RuntimeError:
363+
print("Propensity calculation failure for %s!" % coformer_name)
364+
mc_dictionary[coformer_name] = ["N/A", "N/A", "N/A", "N/A", "N/A", crystal.identifier]
365+
failures.append(coformer_name)
356366

357367
# Make sense of the outputs of all the calculations
358368
mc_hbp_screen = sorted(mc_dictionary.items(), key=lambda e: 0 if e[1][0] == 'N/A' else e[1][0], reverse=True)
359369
diagram_file = make_diagram(api_molecule, work_directory)
360370
chart_file = make_mc_chart(mc_hbp_screen, directory, api_molecule)
361371
make_mc_report(structure, mc_hbp_screen, work_directory, diagram_file, chart_file)
372+
if failure_directory is not None:
373+
with open(os.path.join(failure_directory, 'failures.txt'), 'w', encoding='utf-8', newline='') as file:
374+
file.write('\n'.join(map(str, failures)))
362375

363376

364377
if __name__ == '__main__':
@@ -396,9 +409,10 @@ def main(structure, work_directory, library, csdrefcode):
396409
parser.add_argument('-c', '--coformer_library', type=str,
397410
help='the directory of the desired coformer library',
398411
default=ccdc_coformers_dir)
412+
parser.add_argument('-f', '--failure_directory', type=str,
413+
help='The location where the failures file should be generated')
399414

400415
args = parser.parse_args()
401-
402416
refcode = False
403417

404418
if not os.path.isfile(args.input_structure):
@@ -413,4 +427,4 @@ def main(structure, work_directory, library, csdrefcode):
413427
if not os.path.isdir(args.coformer_library):
414428
parser.error('%s - library not found.' % args.coformer_library)
415429

416-
main(args.input_structure, args.directory, args.coformer_library, refcode)
430+
main(args.input_structure, args.directory, args.failure_directory, args.coformer_library, refcode)

0 commit comments

Comments
 (0)