Skip to content

Commit

Permalink
Actually use name set/get
Browse files Browse the repository at this point in the history
  • Loading branch information
oerc0122 committed Jan 30, 2025
1 parent dbb5223 commit b5fade0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
12 changes: 6 additions & 6 deletions MDANSE/Src/MDANSE/Chemistry/ChemicalSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, name: str = "", trajectory=None):
:type name: str
"""

self._name = name
self.name = str(name)
self._database = ATOMS_DATABASE
if trajectory is not None:
self._database = trajectory
Expand All @@ -55,7 +55,7 @@ def __init__(self, name: str = "", trajectory=None):
self._unique_elements = set()

def __str__(self):
return f"ChemicalSystem {self._name} consisting of {len(self._atom_types)} atoms in {len(self._clusters)} molecules"
return f"ChemicalSystem {self.name} consisting of {len(self._atom_types)} atoms in {len(self._clusters)} molecules"

def initialise_atoms(self, element_list: List[str], name_list: List[str] = None):
self._atom_indices = [
Expand Down Expand Up @@ -191,7 +191,7 @@ def copy(self) -> "ChemicalSystem":
:return: Copy of the ChemicalSystem instance
:rtype: MDANSE.Chemistry.ChemicalSystem.ChemicalSystem
"""
cs = ChemicalSystem(self._name)
cs = ChemicalSystem(self.name)

for attribute_name, attribute_value in self.__dict__.items():
if attribute_name in ["rdkit_mol", "_configuration"]:
Expand Down Expand Up @@ -248,7 +248,7 @@ def serialize(self, h5_file: h5py.File) -> None:
string_dt = h5py.special_dtype(vlen=str)

grp = h5_file.create_group("/composition")
grp.attrs["name"] = self._name
grp.attrs["name"] = self.name

try:
grp.create_dataset("atom_types", data=self._atom_types, dtype=string_dt)
Expand Down Expand Up @@ -292,7 +292,7 @@ def load(self, trajectory: str):
self.rdkit_mol = Chem.RWMol()

grp = source["/composition"]
self._name = grp.attrs["name"]
self.name = grp.attrs["name"]

atom_types = [binary.decode("utf-8") for binary in grp["atom_types"][:]]
atom_names = None
Expand Down Expand Up @@ -326,7 +326,7 @@ def legacy_load(self, trajectory: str):
self.rdkit_mol = Chem.RWMol()

grp = source["/chemical_system"]
self._name = grp.attrs["name"]
self.name = grp.attrs["name"]
atoms = grp["atoms"]
element_list = [line[0].decode("utf-8").strip("'") for line in atoms]
self.initialise_atoms(element_list)
Expand Down
2 changes: 1 addition & 1 deletion MDANSE/Src/MDANSE/Core/Platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def application_directory(self) -> Path:
basedir = Path(os.environ["APPDATA"]) / "mdanse"

# If the application directory does not exist, create it.
os.makedirs(basedir)
basedir.mkdir(parents=True, exist_ok=True)

return basedir

Expand Down
14 changes: 6 additions & 8 deletions MDANSE/Src/MDANSE/Framework/Converters/Gromacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#

import collections
from pathlib import Path

import numpy as np
from mdtraj.formats.xtc import XTCTrajectoryFile
Expand Down Expand Up @@ -77,16 +78,13 @@ def initialize(self):

data_to_be_written = ["configuration", "time"]

filename = Path(self.configuration["xtc_file"]["filename"])
# Create XTC or TRR object depending on which kind of trajectory was loaded
if self.configuration["xtc_file"]["filename"].suffix == ".xtc":
self._xdr_file = XTCTrajectoryFile(
self.configuration["xtc_file"]["filename"], "r"
)
if filename.suffix == ".xtc":
self._xdr_file = XTCTrajectoryFile(bytes(filename), "r")
self._xtc = True
elif self.configuration["xtc_file"]["filename"].suffix == ".trr":
self._xdr_file = TRRTrajectoryFile(
self.configuration["xtc_file"]["filename"], "r"
)
elif filename.suffix == ".trr":
self._xdr_file = TRRTrajectoryFile(bytes(filename), "r")
self._xtc = False

# Extract information about whether velocities and forces are present in the TRR file
Expand Down
3 changes: 2 additions & 1 deletion MDANSE/Src/MDANSE/Framework/Formats/TextFormat.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def write(
:type header: str
"""

filename = Path(filename).with_suffix(".tar")
filename = Path(filename)
filename = filename.parent / (filename.stem + "_text.tar")

PLATFORM.create_directory(filename.parent)
tf = tarfile.open(filename, "w")
Expand Down
8 changes: 4 additions & 4 deletions MDANSE/Src/MDANSE/MolecularDynamics/Trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from typing import TYPE_CHECKING, Any, Dict, List
import operator
from typing import TYPE_CHECKING, Any, Collection, Dict, List, Union

if TYPE_CHECKING:
from MDANSE.Chemistry.Databases import AtomsDatabase

import math
import operator
from collections.abc import Collection
from pathlib import Path
from typing import Union

import h5py
import numpy as np
from MDANSE import PLATFORM
from MDANSE.Chemistry import ATOMS_DATABASE
from MDANSE.Chemistry.ChemicalSystem import ChemicalSystem
from MDANSE.MolecularDynamics.Configuration import RealConfiguration
from MDANSE.Trajectory.H5MDTrajectory import H5MDTrajectory
from MDANSE.Trajectory.MdanseTrajectory import MdanseTrajectory

available_formats = {
"MDANSE": MdanseTrajectory,
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Trajectory/MdanseTrajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(self, h5_filename: Union[Path, str]):

# Load the chemical system
self._chemical_system = ChemicalSystem(self._h5_filename.stem, self)
self._chemical_system.load(self._h5_filename)
self._chemical_system.load(self._h5_file)

# Load all the unit cells
self._load_unit_cells()
Expand All @@ -69,7 +69,7 @@ def file_is_right(self, filename: Union[Path, str]):
else:
try:
temp_cs = ChemicalSystem(filename.stem)
temp_cs.load(filename)
temp_cs.load(file_object)
except Exception:
LOG.warning(
f"Could not load ChemicalSystem from {filename}. MDANSE will try to read it as H5MD next."
Expand Down
5 changes: 3 additions & 2 deletions MDANSE/Tests/UnitTests/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import json
from pathlib import Path
import unittest
from unittest.mock import patch, mock_open, ANY

Expand Down Expand Up @@ -101,9 +102,9 @@ def test__load_user_database(self):
}
),
) as m:
with patch("Path.exists", spec=True):
with patch("pathlib.Path.exists", spec=True):
ATOMS_DATABASE._load("user.json")
m.assert_called_with("user.json", "r")
m.assert_called_with(Path("user.json"), "r")
self.assertDictEqual({"family": "str"}, ATOMS_DATABASE._properties)
self.assertDictEqual(
{"H": {"family": "non-metal"}}, ATOMS_DATABASE._data
Expand Down

0 comments on commit b5fade0

Please sign in to comment.