From 537906b355a1e3b3fefaf2b7ac8b424936767b85 Mon Sep 17 00:00:00 2001 From: ndaelman Date: Mon, 16 Dec 2024 17:04:14 +0100 Subject: [PATCH 1/6] Fix logic loophole when no inp files are recuperated --- electronicparsers/cp2k/parser.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/electronicparsers/cp2k/parser.py b/electronicparsers/cp2k/parser.py index 236f86a1..8f5bbeec 100644 --- a/electronicparsers/cp2k/parser.py +++ b/electronicparsers/cp2k/parser.py @@ -1570,25 +1570,24 @@ def parse(name, data, section): input_files = get_files( input_filename, self.filepath, self.mainfile, deep=False ) - if not input_files: - self.logger.warning( - 'Input *.inp file not found. We will attempt finding the restart file from ' - 'the project_name appending a -1, -1.restart.', - data={'project_name': project_name}, - ) - # Patch to check if the input is .restart and CP2K appended a -1 after the name + if not input_files: # ? what is the purpose of this recovery logic + self.logger.warning('Input *.inp file not found.') + if project_name: + self.logger.warning( + f' We will attempt finding the restart file from {project_name}-1.restart' + ) project_filename = f'{project_name}-1.restart' input_files = get_files( project_filename, self.filepath, self.mainfile, deep=False ) + elif len(input_files) > 1: + self.logger.warning( + f'Multiple input files found. We will parse the first file retrieved.' + ) # ! TODO: employ better heuristic OR parse all + self.inp_parser.mainfile = input_files[0] else: return - if len(input_files) > 1: - self.logger.warning( - f'Multiple input files found. We will parse the first read file.' - ) - self.inp_parser.mainfile = input_files[0] parse('x_cp2k_section_input', self.inp_parser.tree, self.archive.run[-1]) From 0c84b124f13f4131b4c35a4cad621d3e64c29991 Mon Sep 17 00:00:00 2001 From: ndaelman Date: Mon, 16 Dec 2024 17:32:04 +0100 Subject: [PATCH 2/6] Correct if-structure --- electronicparsers/cp2k/parser.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/electronicparsers/cp2k/parser.py b/electronicparsers/cp2k/parser.py index 8f5bbeec..83d16d20 100644 --- a/electronicparsers/cp2k/parser.py +++ b/electronicparsers/cp2k/parser.py @@ -1571,23 +1571,22 @@ def parse(name, data, section): input_filename, self.filepath, self.mainfile, deep=False ) if not input_files: # ? what is the purpose of this recovery logic - self.logger.warning('Input *.inp file not found.') - + self.logger.warning('Input (*.inp) file not found.') if project_name: + project_filename = f'{project_name}-1.restart' self.logger.warning( - f' We will attempt finding the restart file from {project_name}-1.restart' + f'Will attempt from restart file ({project_filename})' ) - project_filename = f'{project_name}-1.restart' input_files = get_files( project_filename, self.filepath, self.mainfile, deep=False ) - elif len(input_files) > 1: - self.logger.warning( - f'Multiple input files found. We will parse the first file retrieved.' - ) # ! TODO: employ better heuristic OR parse all - self.inp_parser.mainfile = input_files[0] - else: - return + if len(input_files) > 1: + self.logger.warning( + f'Multiple input files found. Will parse the first file retrieved.' + ) # ! TODO: employ better heuristic OR parse all + self.inp_parser.mainfile = input_files[0] + else: + return parse('x_cp2k_section_input', self.inp_parser.tree, self.archive.run[-1]) From 156b652de6fec9eac49af2ecc12f25b7b5942ecf Mon Sep 17 00:00:00 2001 From: ndaelman Date: Mon, 16 Dec 2024 18:26:26 +0100 Subject: [PATCH 3/6] Capture cases where no input files were passed on --- electronicparsers/cp2k/parser.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/electronicparsers/cp2k/parser.py b/electronicparsers/cp2k/parser.py index 83d16d20..47bfb2ac 100644 --- a/electronicparsers/cp2k/parser.py +++ b/electronicparsers/cp2k/parser.py @@ -1580,13 +1580,14 @@ def parse(name, data, section): input_files = get_files( project_filename, self.filepath, self.mainfile, deep=False ) - if len(input_files) > 1: - self.logger.warning( - f'Multiple input files found. Will parse the first file retrieved.' - ) # ! TODO: employ better heuristic OR parse all - self.inp_parser.mainfile = input_files[0] - else: - return + if len(input_files) == 0: + return + elif len(input_files) > 1: + self.logger.warning( + f'Multiple input files found. Will parse the first file retrieved.' + ) # ! TODO: employ better heuristic OR parse all + # cover single or multiple `input_files` + self.inp_parser.mainfile = input_files[0] parse('x_cp2k_section_input', self.inp_parser.tree, self.archive.run[-1]) From b31578d6154dccf751769cf5aa204e01b6ff82ef Mon Sep 17 00:00:00 2001 From: ndaelman Date: Mon, 16 Dec 2024 18:32:07 +0100 Subject: [PATCH 4/6] Fix ruff linting --- electronicparsers/castep/parser.py | 9 ++++++--- electronicparsers/quantumespresso/parser.py | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/electronicparsers/castep/parser.py b/electronicparsers/castep/parser.py index b9f00e77..c23dea5c 100644 --- a/electronicparsers/castep/parser.py +++ b/electronicparsers/castep/parser.py @@ -290,9 +290,12 @@ def add_unit(val): elif line and last_parameter: last_parameter[1].append(add_unit(line.strip())) - for key in title.keys(): - for sub_key, val in title[key].items(): - title[key][sub_key] = val[0] if len(val) == 1 else val + for key, val in title.items(): + for sub_key, sub_val in val.items(): + if len(sub_val) == 1: + title[key][sub_key] = sub_val[0] + else: + title[key][sub_key] = sub_val return title diff --git a/electronicparsers/quantumespresso/parser.py b/electronicparsers/quantumespresso/parser.py index 624974e4..9bf16134 100644 --- a/electronicparsers/quantumespresso/parser.py +++ b/electronicparsers/quantumespresso/parser.py @@ -3207,7 +3207,7 @@ def parse_configuration(calculation): 'damped_dynamics': 'geometry_optimization', 'vcs_wentzcovitch_damped_minimization': 'geometry_optimization', } - for method in methods: + for method in methods.values(): sampling = run.get(method) if sampling is not None: self.sampling_method = methods[method] From 417a9b2afa48f6f0d3dd837d6a637a98b42ea9ab Mon Sep 17 00:00:00 2001 From: ndaelman Date: Mon, 16 Dec 2024 18:32:33 +0100 Subject: [PATCH 5/6] Apply ruff format --- electronicparsers/wien2k/parser.py | 18 ++++++++++-------- tests/test_wien2kparser.py | 2 ++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/electronicparsers/wien2k/parser.py b/electronicparsers/wien2k/parser.py index daada6ee..9a1e8b34 100644 --- a/electronicparsers/wien2k/parser.py +++ b/electronicparsers/wien2k/parser.py @@ -1127,18 +1127,20 @@ def parse_method(self): atom_obj = AtomParameters() atom_obj.atom_index = atom_index atom_obj.core_hole = CoreHole( - j_quantum_number = j_quantum_number, - l_quantum_number = l_quantum_number, - n_quantum_number = n_quantum_number, - n_electrons_excited = electrons_excited, - occupation = occupancy, - dscf_state = 'final', + j_quantum_number=j_quantum_number, + l_quantum_number=l_quantum_number, + n_quantum_number=n_quantum_number, + n_electrons_excited=electrons_excited, + occupation=occupancy, + dscf_state='final', ) atom_par.append(atom_obj) break else: - self.logger.warning("inc file is missing, no corehole information " - "will be parsed if corehole present.") + self.logger.warning( + 'inc file is missing, no corehole information ' + 'will be parsed if corehole present.' + ) # basis if self.in1_parser.mainfile: self.in1_parser.parse() diff --git a/tests/test_wien2kparser.py b/tests/test_wien2kparser.py index ba05d7b1..fb20ba0b 100644 --- a/tests/test_wien2kparser.py +++ b/tests/test_wien2kparser.py @@ -26,6 +26,7 @@ CoreHole, ) + def approx(value, abs=0, rel=1e-6): return pytest.approx(value, abs=abs, rel=rel) @@ -125,6 +126,7 @@ def test_dos(parser, caplog): 2.7395685667470246e17 ) + def test_core_hole(parser, caplog): archive = EntryArchive() parser.parse('tests/data/wien2k/TiN-corehole/TiN-corehole.scf', archive, None) From 879802ab3b921ea359b47d0bc6b93080cfd680c5 Mon Sep 17 00:00:00 2001 From: ndaelman Date: Mon, 16 Dec 2024 18:41:13 +0100 Subject: [PATCH 6/6] Revert "Fix ruff linting" This reverts commit b31578d6154dccf751769cf5aa204e01b6ff82ef. --- electronicparsers/castep/parser.py | 9 +++------ electronicparsers/quantumespresso/parser.py | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/electronicparsers/castep/parser.py b/electronicparsers/castep/parser.py index c23dea5c..b9f00e77 100644 --- a/electronicparsers/castep/parser.py +++ b/electronicparsers/castep/parser.py @@ -290,12 +290,9 @@ def add_unit(val): elif line and last_parameter: last_parameter[1].append(add_unit(line.strip())) - for key, val in title.items(): - for sub_key, sub_val in val.items(): - if len(sub_val) == 1: - title[key][sub_key] = sub_val[0] - else: - title[key][sub_key] = sub_val + for key in title.keys(): + for sub_key, val in title[key].items(): + title[key][sub_key] = val[0] if len(val) == 1 else val return title diff --git a/electronicparsers/quantumespresso/parser.py b/electronicparsers/quantumespresso/parser.py index 9bf16134..624974e4 100644 --- a/electronicparsers/quantumespresso/parser.py +++ b/electronicparsers/quantumespresso/parser.py @@ -3207,7 +3207,7 @@ def parse_configuration(calculation): 'damped_dynamics': 'geometry_optimization', 'vcs_wentzcovitch_damped_minimization': 'geometry_optimization', } - for method in methods.values(): + for method in methods: sampling = run.get(method) if sampling is not None: self.sampling_method = methods[method]