From 46be2c84e8f39854360cb542e891728dd0018729 Mon Sep 17 00:00:00 2001 From: Maciej Bartkowiak Date: Fri, 31 Jan 2025 14:36:21 +0000 Subject: [PATCH 1/5] Handle more errors when reading H5MD --- .../Src/MDANSE/Trajectory/H5MDTrajectory.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py b/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py index 2684e54bf..77ad86fe5 100644 --- a/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py +++ b/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py @@ -63,7 +63,13 @@ def __init__(self, h5_filename): self._chemical_system = ChemicalSystem( os.path.splitext(os.path.basename(self._h5_filename))[0] ) - self._chemical_system.initialise_atoms(chemical_elements) + try: + self._chemical_system.initialise_atoms(chemical_elements) + except Exception: + LOG.error( + "It was not possible to read chemical element information from an H5MD file." + ) + return # Load all the unit cells self._load_unit_cells() @@ -75,7 +81,7 @@ def __init__(self, h5_filename): except: conv_factor = 1.0 else: - if pos_unit == "Ang": + if pos_unit == "Ang" or pos_unit == "Angstrom": pos_unit = "ang" conv_factor = measure(1.0, pos_unit).toval("nm") coords *= conv_factor @@ -94,6 +100,7 @@ def file_is_right(self, filename): temp["h5md"] except KeyError: result = False + temp.close() return result def close(self): @@ -117,7 +124,7 @@ def __getitem__(self, frame): except: conv_factor = 1.0 else: - if pos_unit == "Ang": + if pos_unit == "Ang" or pos_unit == "Angstrom": pos_unit = "ang" conv_factor = measure(1.0, pos_unit).toval("nm") configuration = {} @@ -195,7 +202,7 @@ def coordinates(self, frame): except: conv_factor = 1.0 else: - if pos_unit == "Ang": + if pos_unit == "Ang" or pos_unit == "Angstrom": pos_unit = "ang" conv_factor = measure(1.0, pos_unit).toval("nm") @@ -248,7 +255,7 @@ def _load_unit_cells(self): except: conv_factor = 1.0 else: - if box_unit == "Ang": + if box_unit == "Ang" or box_unit == "Angstrom": box_unit = "ang" conv_factor = measure(1.0, box_unit).toval("nm") try: @@ -371,7 +378,7 @@ def read_com_trajectory( except: conv_factor = 1.0 else: - if pos_unit == "Ang": + if pos_unit == "Ang" or pos_unit == "Angstrom": pos_unit = "ang" conv_factor = measure(1.0, pos_unit).toval("nm") @@ -469,7 +476,7 @@ def read_atomic_trajectory( except: conv_factor = 1.0 else: - if pos_unit == "Ang": + if pos_unit == "Ang" or pos_unit == "Angstrom": pos_unit = "ang" conv_factor = measure(1.0, pos_unit).toval("nm") coords = grp[first:last:step, index, :].astype(np.float64) * conv_factor From 4d7578302b169b6762974dbea7a7a0323048e4c0 Mon Sep 17 00:00:00 2001 From: Maciej Bartkowiak Date: Mon, 3 Feb 2025 15:24:37 +0000 Subject: [PATCH 2/5] Improve H5MD loading based on new input files --- .../Src/MDANSE/Trajectory/H5MDTrajectory.py | 53 +++++++++++++------ 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py b/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py index 77ad86fe5..351de1208 100644 --- a/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py +++ b/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py @@ -52,14 +52,26 @@ def __init__(self, h5_filename): self._h5_filename = h5_filename self._h5_file = h5py.File(self._h5_filename, "r") - - # Load the chemical system - try: + particle_types = self._h5_file["/particles/all/species"] + particle_lookup = h5py.check_enum_dtype( + self._h5_file["/particles/all/species"].dtype + ) + if particle_lookup is None: + # Load the chemical system + try: + symbols = self._h5_file["/parameters/atom_symbols"] + except KeyError: + LOG.error( + f"No information about chemical elements in {self._h5_filename}" + ) + return + else: + chemical_elements = [byte.decode() for byte in symbols] + else: + reverse_lookup = {item: key for key, item in particle_lookup.items()} chemical_elements = [ - byte.decode() for byte in self._h5_file["/parameters/atom_symbols"] + reverse_lookup[type_number] for type_number in particle_types ] - except KeyError: - chemical_elements = self._h5_file["/particles/all/species"] self._chemical_system = ChemicalSystem( os.path.splitext(os.path.basename(self._h5_filename))[0] ) @@ -70,7 +82,6 @@ def __init__(self, h5_filename): "It was not possible to read chemical element information from an H5MD file." ) return - # Load all the unit cells self._load_unit_cells() @@ -78,7 +89,7 @@ def __init__(self, h5_filename): coords = self._h5_file["/particles/all/position/value"][0, :, :] try: pos_unit = self._h5_file["/particles/all/position/value"].attrs["unit"] - except: + except Exception: conv_factor = 1.0 else: if pos_unit == "Ang" or pos_unit == "Angstrom": @@ -252,8 +263,8 @@ def _load_unit_cells(self): self._unit_cells = [] try: box_unit = self._h5_file["/particles/all/box/edges/value"].attrs["unit"] - except: - conv_factor = 1.0 + except (AttributeError, KeyError): + conv_factor = 0.1 else: if box_unit == "Ang" or box_unit == "Angstrom": box_unit = "ang" @@ -265,9 +276,16 @@ def _load_unit_cells(self): else: if len(cells.shape) > 1: for cell in cells: - temp_array = np.array( - [[cell[0], 0.0, 0.0], [0.0, cell[1], 0.0], [0.0, 0.0, cell[2]]] - ) + if cell.shape == (3, 3): + temp_array = np.array(cell) + else: + temp_array = np.array( + [ + [cell[0], 0.0, 0.0], + [0.0, cell[1], 0.0], + [0.0, 0.0, cell[2]], + ] + ) uc = UnitCell(temp_array) self._unit_cells.append(uc) else: @@ -279,14 +297,17 @@ def _load_unit_cells(self): def time(self): try: time_unit = self._h5_file["/particles/all/position/time"].attrs["unit"] - except: + except KeyError: conv_factor = 1.0 else: conv_factor = measure(1.0, time_unit).toval("ps") try: time = self._h5_file["/particles/all/position/time"] * conv_factor - except: - time = [] + except TypeError: + try: + time = self._h5_file["/particles/all/position/time"][:] * conv_factor + except Exception: + time = [] return time def unit_cell(self, frame): From 9e4086c7fef043ed3210994a17b0b8e62920f18e Mon Sep 17 00:00:00 2001 From: Maciej Bartkowiak Date: Fri, 7 Feb 2025 16:35:18 +0000 Subject: [PATCH 3/5] Make code more ruff-compatible --- .../Src/MDANSE/Trajectory/H5MDTrajectory.py | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py b/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py index 8b1ec0f8f..f547427b5 100644 --- a/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py +++ b/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py @@ -91,7 +91,7 @@ def __init__(self, h5_filename: Union[Path, str]): except Exception: conv_factor = 1.0 else: - if pos_unit == "Ang" or pos_unit == "Angstrom": + if pos_unit in ("Ang", "Angstrom"): pos_unit = "ang" conv_factor = measure(1.0, pos_unit).toval("nm") coords *= conv_factor @@ -128,13 +128,12 @@ def __getitem__(self, frame): :rtype: dict of ndarray """ - grp = self._h5_file["/particles/all/position/value"] try: pos_unit = self._h5_file["/particles/all/position/value"].attrs["unit"] - except: + except Exception: conv_factor = 1.0 else: - if pos_unit == "Ang" or pos_unit == "Angstrom": + if pos_unit in ("Ang", "Angstrom"): pos_unit = "ang" conv_factor = measure(1.0, pos_unit).toval("nm") configuration = {} @@ -144,12 +143,12 @@ def __getitem__(self, frame): try: try: vel_unit = self._h5_file["/particles/all/velocity/value"].attrs["unit"] - except: + except Exception: vel_unit = "ang/fs" configuration["velocities"] = self._h5_file[ "/particles/all/velocity/value" ][frame, :, :] * measure(1.0, vel_unit).toval("nm/ps") - except: + except Exception: pass configuration["time"] = self.time()[frame] @@ -186,7 +185,7 @@ def charges(self, frame): except KeyError: LOG.debug(f"No charge information in trajectory {self._h5_filename}") charge = np.zeros(self._chemical_system.number_of_atoms) - except: + except Exception: try: charge = self._h5_file["/particles/all/charge"][:] except KeyError: @@ -209,10 +208,10 @@ def coordinates(self, frame): raise IndexError(f"Invalid frame number: {frame}") try: pos_unit = self._h5_file["/particles/all/position/value"].attrs["unit"] - except: + except Exception: conv_factor = 1.0 else: - if pos_unit == "Ang" or pos_unit == "Angstrom": + if pos_unit in ("Ang", "Angstrom"): pos_unit = "ang" conv_factor = measure(1.0, pos_unit).toval("nm") @@ -243,7 +242,7 @@ def configuration(self, frame): if k not in self._variables_to_skip: try: variables[k] = self.variable(k)[frame, :, :].astype(np.float64) - except: + except Exception: self._variables_to_skip.append(k) coordinates = self.coordinates(frame) @@ -395,10 +394,10 @@ def read_com_trajectory( grp = self._h5_file["/particles/all/position/value"] try: pos_unit = self._h5_file["/particles/all/position/value"].attrs["unit"] - except: + except Exception: conv_factor = 1.0 else: - if pos_unit == "Ang" or pos_unit == "Angstrom": + if pos_unit in ("Ang", "Angstrom"): pos_unit = "ang" conv_factor = measure(1.0, pos_unit).toval("nm") @@ -493,10 +492,10 @@ def read_atomic_trajectory( grp = self._h5_file["/particles/all/position/value"] try: pos_unit = self._h5_file["/particles/all/position/value"].attrs["unit"] - except: + except Exception: conv_factor = 1.0 else: - if pos_unit == "Ang" or pos_unit == "Angstrom": + if pos_unit in ("Ang", "Angstrom"): pos_unit = "ang" conv_factor = measure(1.0, pos_unit).toval("nm") coords = grp[first:last:step, index, :].astype(np.float64) * conv_factor From 7d7693333d140ae3ce4ce3e3b03752c2fc0743e1 Mon Sep 17 00:00:00 2001 From: Maciej Bartkowiak <108934199+MBartkowiakSTFC@users.noreply.github.com> Date: Tue, 11 Feb 2025 14:43:20 +0000 Subject: [PATCH 4/5] Update MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py Co-authored-by: Jacob Wilkins <46597752+oerc0122@users.noreply.github.com> Signed-off-by: Maciej Bartkowiak <108934199+MBartkowiakSTFC@users.noreply.github.com> --- MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py b/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py index f547427b5..116aafd74 100644 --- a/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py +++ b/MDANSE/Src/MDANSE/Trajectory/H5MDTrajectory.py @@ -264,7 +264,7 @@ def _load_unit_cells(self): except (AttributeError, KeyError): conv_factor = 0.1 else: - if box_unit == "Ang" or box_unit == "Angstrom": + if box_unit in ("Ang", "Angstrom"): box_unit = "ang" conv_factor = measure(1.0, box_unit).toval("nm") try: From 14219ecaf16840ecf012c181bd5d416c28a6990b Mon Sep 17 00:00:00 2001 From: Maciej Bartkowiak Date: Tue, 11 Feb 2025 14:54:50 +0000 Subject: [PATCH 5/5] Correct text info for H5MD --- MDANSE/Src/MDANSE/Framework/InputData/HDFTrajectoryInputData.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MDANSE/Src/MDANSE/Framework/InputData/HDFTrajectoryInputData.py b/MDANSE/Src/MDANSE/Framework/InputData/HDFTrajectoryInputData.py index fa10a77c2..b70042b64 100644 --- a/MDANSE/Src/MDANSE/Framework/InputData/HDFTrajectoryInputData.py +++ b/MDANSE/Src/MDANSE/Framework/InputData/HDFTrajectoryInputData.py @@ -61,7 +61,7 @@ def info(self): val.append("Number of steps:") val.append(f"{self._data}\n") val.append("Configuration:") - val.append(f"\tIs periodic: {'unit_cell' in self._data.file}\n") + val.append(f"\tIs periodic: {self._data.unit_cell(0) is not None}\n") try: val.append(f"First unit cell (nm):\n{self._data.unit_cell(0)._unit_cell}\n") except Exception: