Skip to content

Commit e5e804b

Browse files
authored
Merge pull request #64 from Luke2Sky/LucasMilitao2
PR to verify disorders on input structure and improve error handling
2 parents 72c74d7 + 8ba409f commit e5e804b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

scripts/multi_component_hydrogen_bond_propensity/multi_component_hydrogen_bond_propensity_report.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,18 @@ def make_mc_report(identifier, results, directory, diagram_file, chart_file):
314314
launch_word_processor(output_file)
315315

316316

317-
def main(structure, work_directory, failure_directory, library, csdrefcode):
317+
def main(structure, work_directory, failure_directory, library, csdrefcode, force_run):
318318
# This loads up the CSD if a refcode is requested, otherwise loads the structural file supplied
319319
if csdrefcode:
320320
try:
321321
crystal = io.CrystalReader('CSD').crystal(structure)
322322
except RuntimeError:
323323
print('Error! %s is not in the database!' % structure)
324324
quit()
325+
if io.CrystalReader('CSD').entry(structure).has_disorder and not force_run:
326+
raise RuntimeError("Disorder can cause undefined behaviour. It is not advisable to run this "
327+
"script on disordered entries.\n To force this script to run on disordered entries"
328+
" use the flag --force_run_disordered.")
325329
else:
326330
crystal = io.CrystalReader(structure)[0]
327331

@@ -358,10 +362,12 @@ def main(structure, work_directory, failure_directory, library, csdrefcode):
358362
tdata = get_mc_scores(propensities, crystal.identifier)
359363
json.dump(tdata, file)
360364
mc_dictionary[coformer_name] = get_mc_scores(propensities, crystal.identifier)
361-
except RuntimeError:
365+
except RuntimeError as error_message:
362366
print("Propensity calculation failure for %s!" % coformer_name)
367+
error_string = f"{coformer_name}: {error_message}"
368+
warnings.warn(error_string)
363369
mc_dictionary[coformer_name] = ["N/A", "N/A", "N/A", "N/A", "N/A", crystal.identifier]
364-
failures.append(coformer_name)
370+
failures.append(error_string)
365371

366372
# Make sense of the outputs of all the calculations
367373
mc_hbp_screen = sorted(mc_dictionary.items(), key=lambda e: 0 if e[1][0] == 'N/A' else e[1][0], reverse=True)
@@ -411,6 +417,9 @@ def main(structure, work_directory, failure_directory, library, csdrefcode):
411417
parser.add_argument('-f', '--failure_directory', type=str,
412418
help='The location where the failures file should be generated')
413419

420+
parser.add_argument('--force_run_disordered', action="store_true",
421+
help='Forces running the script on disordered entries. (NOT RECOMMENDED)', default=False)
422+
414423
args = parser.parse_args()
415424
refcode = False
416425
args.directory = os.path.abspath(args.directory)
@@ -424,4 +433,5 @@ def main(structure, work_directory, failure_directory, library, csdrefcode):
424433
if not os.path.isdir(args.coformer_library):
425434
parser.error('%s - library not found.' % args.coformer_library)
426435

427-
main(args.input_structure, args.directory, args.failure_directory, args.coformer_library, refcode)
436+
main(args.input_structure, args.directory, args.failure_directory, args.coformer_library, refcode,
437+
args.force_run_disordered)

0 commit comments

Comments
 (0)