Skip to content

Commit

Permalink
rename all .atoms properties to .frames (#349)
Browse files Browse the repository at this point in the history
* rename all `.atoms` properties to `.frames`

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* remove ReadData node

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* rename some more .atoms to .frames

* update some more tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix test?

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
PythonFZ and pre-commit-ci[bot] authored Nov 28, 2024
1 parent 125b0da commit 844afc7
Show file tree
Hide file tree
Showing 44 changed files with 212 additions and 258 deletions.
3 changes: 1 addition & 2 deletions ipsuite/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ from .configuration_selection import (
)

# Data
from .data_loading import AddData, AddDataH5MD, ReadData
from .data_loading import AddData, AddDataH5MD

# Datasets
from .datasets import MD22Dataset
Expand Down Expand Up @@ -131,7 +131,6 @@ __all__ = [
# Data
"AddData",
"AddDataH5MD",
"ReadData",
# Datasets
"MD22Dataset",
# Bootstrap
Expand Down
4 changes: 2 additions & 2 deletions ipsuite/analysis/bond_stretch.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run(self):
e_uncertainties = []
f_uncertainties = []

self.atoms = []
self.frames = []
for i in range(self.n_steps):
struct.set_distance(self.idxs[0], self.idxs[1], bond_lengths[i], mask=mask)
ens_energies.append(struct.get_total_energy())
Expand All @@ -77,7 +77,7 @@ def run(self):
if "forces_uncertainty" in struct.calc.results:
f_uncertainties.append(struct.calc.results.get("forces_uncertainty"))

self.atoms.append(struct.copy())
self.frames.append(struct.copy())

results = {
"energy": np.asarray(ens_energies),
Expand Down
18 changes: 9 additions & 9 deletions ipsuite/analysis/model/dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ def run(self):

energies = []

self.atoms = []
self.frames = []

for stdev in tqdm.tqdm(stdev_space, ncols=70):
atoms.positions = reference.positions
atoms.rattle(stdev=stdev, seed=self.seed)
energies.append(atoms.get_potential_energy())
self.atoms.append(freeze_copy_atoms(atoms))
self.frames.append(freeze_copy_atoms(atoms))

self.energies = pd.DataFrame({"y": energies, "x": stdev_space})

Expand Down Expand Up @@ -134,7 +134,7 @@ def run(self):
original_atoms.calc = self.model.get_calculator(directory=self.model_outs)

energies = []
self.atoms = []
self.frames = []
if self.mapping is None:
scaling_atoms = original_atoms
else:
Expand All @@ -151,15 +151,15 @@ def run(self):
eval_atoms.calc = original_atoms.calc

energies.append(eval_atoms.get_potential_energy())
self.atoms.append(freeze_copy_atoms(eval_atoms))
self.frames.append(freeze_copy_atoms(eval_atoms))

self.energies = pd.DataFrame({"y": energies, "x": scale_space})

if "energy_uncertainty" in self.atoms[0].calc.results:
if "energy_uncertainty" in self.frames[0].calc.results:
fig, ax, _ = plot_with_uncertainty(
{
"std": np.std(
[a.calc.results["energy_uncertainty"] for a in self.atoms]
[a.calc.results["energy_uncertainty"] for a in self.frames]
),
"mean": self.energies["y"],
},
Expand Down Expand Up @@ -246,7 +246,7 @@ def run(self):
temperature, total_energy = utils.ase_sim.get_energy(atoms)

energy = []
self.atoms = []
self.frames = []

with tqdm.trange(
self.steps,
Expand All @@ -269,7 +269,7 @@ def run(self):
f" {self.max_temperature} K. Simulation was stopped."
)
break
self.atoms.append(freeze_copy_atoms(atoms))
self.frames.append(freeze_copy_atoms(atoms))

self.flux_data = pd.DataFrame(
energy, columns=["meassured_temp", "energy", "set_temp"]
Expand Down Expand Up @@ -375,7 +375,7 @@ class MDStability(base.IPSNode):
stable_steps_df: pd.DataFrame = zntrack.plots()

@property
def atoms(self) -> typing.List[ase.Atoms]:
def frames(self) -> typing.List[ase.Atoms]:
with self.state.fs.open(self.traj_file, "rb") as f:
with h5py.File(f) as file:
return znh5md.IO(file_handle=file)[:]
Expand Down
4 changes: 2 additions & 2 deletions ipsuite/analysis/model/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Prediction(base.ProcessAtoms):
model: models.MLModel = zntrack.deps()

def run(self):
self.atoms = []
self.frames = []
calc = self.model.get_calculator()

for configuration in tqdm.tqdm(self.get_data(), ncols=70):
Expand All @@ -58,7 +58,7 @@ def run(self):
): # required for nequip, GAP
pass

self.atoms.append(freeze_copy_atoms(atoms))
self.frames.append(freeze_copy_atoms(atoms))


class PredictionMetrics(base.ComparePredictions):
Expand Down
2 changes: 0 additions & 2 deletions ipsuite/analysis/sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ def get_values(self, data, item):
def run(self):
values = []

self.data = [x.atoms if isinstance(x, zntrack.Node) else x for x in self.data]

for atoms, sim in zip(self.data, self.sim_list):
radius = sim.constraints[0].radius
atom_id = sim.constraints[0].get_selected_atom_id(atoms[0])
Expand Down
10 changes: 5 additions & 5 deletions ipsuite/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class ProcessAtoms(IPSNode):
----------
data: list[ase.Atoms]
The atoms data to process. This must be an input to the Node
atoms: list[ase.Atoms]
frames: list[ase.Atoms]
The processed atoms data. This is an output of the Node.
It does not have to be 'field.Atoms' but can also be e.g. a 'property'.
"""

data: list[ase.Atoms] = zntrack.deps()
atoms: list[ase.Atoms] = fields.Atoms()
frames: list[ase.Atoms] = fields.Atoms()

def get_data(self) -> list[ase.Atoms]:
"""Get the atoms data to process."""
Expand All @@ -47,7 +47,7 @@ class ProcessSingleAtom(IPSNode):
data_id: int | None
The id of the atoms object to process. If None, the first
atoms object is used. Only relevant if 'data' is a list.
atoms: list[ase.Atoms]
frames: list[ase.Atoms]
The processed atoms data. This is an output of the Node.
It does not have to be 'field.Atoms' but can also be e.g. a 'property'.
Although, we only process a single atoms object, we return a list.
Expand All @@ -58,7 +58,7 @@ class ProcessSingleAtom(IPSNode):
data: typing.List[ase.Atoms] = zntrack.deps()
data_id: int = zntrack.params(-1)

atoms: typing.List[ase.Atoms] = fields.Atoms()
frames: typing.List[ase.Atoms] = fields.Atoms()

def get_data(self) -> ase.Atoms:
"""Get the atoms object to process given the 'data' and 'data_id'.
Expand Down Expand Up @@ -129,4 +129,4 @@ class Flatten(ProcessAtoms):
"""Flattens list[list[ase.Atoms]] to list[ase.Atoms]"""

def run(self):
self.atoms = sum(self.data, [])
self.frames = sum(self.data, [])
4 changes: 2 additions & 2 deletions ipsuite/base/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class HasAtoms(typing.Protocol):
"""Protocol for objects that have an atoms attribute."""

atoms: list[ase.Atoms]
frames: list[ase.Atoms]


class HasSelectedConfigurations(typing.Protocol):
Expand All @@ -30,7 +30,7 @@ class ProcessAtoms(typing.Protocol):
"""

data: list[ase.Atoms]
atoms: list[ase.Atoms]
frames: list[ase.Atoms]


# Collection of complex type hints
Expand Down
10 changes: 5 additions & 5 deletions ipsuite/bootstrap/random_displacements.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ def run(self) -> None:
(self.model_outs / "outs.txt").write_text("Lorem Ipsum")
if self.model is not None:
calculator = self.model.get_calculator(directory=self.model_outs)
self.atoms = []
self.frames = []
for atoms in tqdm.tqdm(atoms_list, ncols=120, desc="Applying model"):
atoms.calc = calculator
atoms.get_potential_energy()
self.atoms.append(freeze_copy_atoms(atoms))
self.frames.append(freeze_copy_atoms(atoms))
else:
self.atoms = atoms_list
self.frames = atoms_list

def bootstrap_configs(sefl, atoms: ase.Atoms, rng):
raise NotImplementedError
Expand Down Expand Up @@ -101,7 +101,7 @@ def bootstrap_configs(self, atoms, rng):
else:
atoms_list = []

mapping = ips.geometry.BarycenterMapping()
mapping = ips.BarycenterMapping()

_, molecules = mapping.forward_mapping(atoms)
for _ in range(self.n_configurations):
Expand Down Expand Up @@ -139,7 +139,7 @@ def bootstrap_configs(self, atoms, rng):
if self.maximum > 2 * np.pi:
log.warning("Setting maximum to 2 Pi.")

mapping = ips.geometry.BarycenterMapping()
mapping = ips.BarycenterMapping()

_, molecules = mapping.forward_mapping(atoms.copy())
for _ in range(self.n_configurations):
Expand Down
2 changes: 1 addition & 1 deletion ipsuite/bootstrap/surface_mods.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def run(self) -> None:
)
atoms_list[-1].extend(extension)

self.atoms = atoms_list
self.frames = atoms_list


class SurfaceRasterMetrics(analysis.PredictionMetrics):
Expand Down
2 changes: 1 addition & 1 deletion ipsuite/calculators/ase_geoopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def get_atoms(self) -> ase.Atoms:
return atoms

@property
def atoms(self) -> typing.List[ase.Atoms]:
def frames(self) -> typing.List[ase.Atoms]:
with self.state.fs.open(self.traj_file, "rb") as f:
with h5py.File(f) as file:
return znh5md.IO(file_handle=file)[:]
4 changes: 2 additions & 2 deletions ipsuite/calculators/ase_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ def get_atoms(self, method="run") -> ase.Atoms | typing.List[ase.Atoms]:
return atoms

@property
def atoms(self) -> typing.List[ase.Atoms]:
def frames(self) -> typing.List[ase.Atoms]:
with self.state.fs.open(self.traj_file, "rb") as f:
with h5py.File(f) as file:
return znh5md.IO(file_handle=file)[:]
Expand Down Expand Up @@ -727,7 +727,7 @@ def map(self): # noqa: A003
for atoms in structures:
metrics, current_step = self.run_md(atoms=atoms)
metrics_list.append(metrics)
self.structures.append(self.atoms[-current_step:])
self.structures.append(self.frames[-current_step:])

# Flatten metrics dictionary
flattened_metrics = {}
Expand Down
4 changes: 2 additions & 2 deletions ipsuite/calculators/cp2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def _run_cp2k(self, atoms, cp2k_input_script):
raise RuntimeError(f"CP2K failed with return code {proc.returncode}:")

@property
def atoms(self):
def frames(self):
"""Return the atoms object."""
# TODO this is for single point, what about MD
data = {}
Expand Down Expand Up @@ -217,7 +217,7 @@ def run(self):
file.unlink()

@property
def atoms(self) -> typing.List[ase.Atoms]:
def frames(self) -> typing.List[ase.Atoms]:
with self.state.fs.open(self.output_file, "rb") as f:
with h5py.File(f) as file:
return znh5md.IO(file_handle=file)[:]
Expand Down
2 changes: 1 addition & 1 deletion ipsuite/calculators/lammps.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,6 @@ def get_template(
yaml.dump({"sim_parameters": params}, file)

@property
def atoms(self) -> list[ase.Atoms]:
def frames(self) -> list[ase.Atoms]:
with self.state.fs.open(self.dump_file, mode="r") as file:
return list(ase.io.iread(file, format="lammps-dump-text"))
2 changes: 1 addition & 1 deletion ipsuite/calculators/orca.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def run(self):
db.append(atoms)

@property
def atoms(self):
def frames(self):
with self.state.fs.open(self.output_file, "rb") as f:
with h5py.File(f) as file:
return znh5md.IO(file_handle=file)[:]
Expand Down
4 changes: 2 additions & 2 deletions ipsuite/calculators/xtb.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class xTBSinglePoint(base.ProcessAtoms):
method: str = zntrack.params("GFN1-xTB")

def run(self):
self.atoms = []
self.frames = []

calculator = self.get_calculator()

for atom in tqdm.tqdm(self.get_data(), ncols=70):
atom.calc = calculator
atom.get_potential_energy()
self.atoms.append(freeze_copy_atoms(atom))
self.frames.append(freeze_copy_atoms(atom))

def get_calculator(self, **kwargs):
"""Get an xtb ase calculator."""
Expand Down
2 changes: 1 addition & 1 deletion ipsuite/configuration_generation/gmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def __post_init__(self):
self.mdp_files = [pathlib.Path(mdp_file) for mdp_file in self.mdp_files]

@property
def atoms(self):
def frames(self):
with self.state.fs.open(self.traj_file, "rb") as f:
with h5py.File(f) as file:
return znh5md.IO(file_handle=file)[:]
Expand Down
10 changes: 5 additions & 5 deletions ipsuite/configuration_generation/packmol.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Packmol(base.IPSNode):
box: list = zntrack.params(None)
density: float = zntrack.params(None)
structures: pathlib.Path = zntrack.outs_path(zntrack.nwd / "packmol")
atoms: list[ase.Atoms] = fields.Atoms()
frames: list[ase.Atoms] = fields.Atoms()
pbc: bool = zntrack.params(True)

def __post_init__(self):
Expand Down Expand Up @@ -96,15 +96,15 @@ def run(self):
if self.pbc:
atoms.cell = self.box
atoms.pbc = True
self.atoms = [atoms]
self.frames = [atoms]

def _get_box_from_molar_volume(self):
"""Get the box size from the molar volume"""
self.box = get_box_from_density(self.data, self.count, self.density)
log.info(f"estimated box size: {self.box}")

def view(self) -> view:
return view(self.atoms, viewer="x3d")
return view(self.frames, viewer="x3d")


class MultiPackmol(Packmol):
Expand Down Expand Up @@ -145,7 +145,7 @@ class MultiPackmol(Packmol):

def run(self):
np.random.seed(self.seed)
self.atoms = []
self.frames = []

if self.density is not None:
self._get_box_from_molar_volume()
Expand Down Expand Up @@ -187,4 +187,4 @@ def run(self):
atoms.cell = self.box
atoms.pbc = True

self.atoms.append(atoms)
self.frames.append(atoms)
6 changes: 3 additions & 3 deletions ipsuite/configuration_generation/smiles_to_atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def view(self) -> view:


class SmilesToConformers(base.IPSNode):
atoms: list[ase.Atoms] = fields.Atoms()
frames: list[ase.Atoms] = fields.Atoms()

smiles: str = zntrack.params()
numConfs: int = zntrack.params()
Expand All @@ -62,7 +62,7 @@ def run(self):
randomSeed=self.seed,
maxAttempts=self.maxAttempts,
)
self.atoms = []
self.frames = []
for conf in mol.GetConformers():
atoms = ase.Atoms(
positions=conf.GetPositions(),
Expand All @@ -74,4 +74,4 @@ def run(self):
atoms.set_cell([self.cell, self.cell, self.cell])
atoms.center()

self.atoms.append(atoms)
self.frames.append(atoms)
Loading

0 comments on commit 844afc7

Please sign in to comment.