amep.evaluate.Function#

class amep.evaluate.Function(traj, func, skip=0.0, nav=10, **kwargs)#

Bases: BaseEvaluation

Apply a user-defined function to a trajectory.

__init__(traj, func, skip=0.0, nav=10, **kwargs)#

Calculate a given function for a trajectory.

Parameters:
  • traj (BaseTrajectory) – Trajectory object with simulation data.

  • func (Function) – Function to be used for analysis. Its signature should be func(frame, **kwargs)->float

  • skip (float, default=0.0) – Skip this fraction at the beginning of the trajectory.

  • nav (int, default=10) – number of frames to consider for the time average.

  • **kwargs (Keyword Arguments) – General python keyword arguments to be forwarded to the function f.

Examples

>>> import amep
>>> traj = amep.load.traj("../examples/data/lammps.h5amep")
>>> def msd(frame, start=None):
...     vec = start.unwrapped_coords() - frame.unwrapped_coords()
...     return (vec ** 2).sum(axis=1).mean()
>>> msd_eval = amep.evaluate.Function(
...     traj, msd, nav=traj.nframes, start=traj[0]
... )
>>> msd_eval.name = "msd"
>>> msd_eval.save("./eval/msd_eval.h5")
>>> fig, axs = amep.plot.new()
>>> axs.plot(traj.times, msd_eval.frames)
>>> axs.set_xlabel(r"$t$")
>>> axs.set_ylabel(r"MSD")
>>> axs.loglog()
>>> fig.savefig("./figures/evaluate/evaluate-Function.png")
../_images/evaluate-Function.png
>>> def polarization(frame, start=None):
...     mean_orientation = frame.orientations().mean(axis=0)
...     return np.sqrt(np.sum(mean_orientation**2))
>>> pol_eval = amep.evaluate.Function(
...     traj, polarization, nav=traj.nframes
... )
>>> pol_eval.name = "global polarization"
>>> pol_eval.save("./eval/global_polarization.h5")
>>> print("Time average: ", pol_eval.avg)
Time average:  0.0091200555
>>>

Methods

__init__(traj, func[, skip, nav])

Calculate a given function for a trajectory.

items()

keys()

The keys to the evaluation object.

save(path[, backup, database, name])

Stores the evaluation result in an HDF5 file.

values()

Attributes

avg

Time-averaged function value (averaged over the given number of frames).

frames

Function value for each frame.

indices

Indices of all frames for which the function has been evaluated.

name

times

Times at which the function is evaluated.

property avg#

Time-averaged function value (averaged over the given number of frames).

Returns:

Time-averaged function value.

Return type:

various

property frames#

Function value for each frame.

Returns:

Function value for each frame.

Return type:

np.ndarray

property indices#

Indices of all frames for which the function has been evaluated.

Returns:

Frame indices.

Return type:

np.ndarray

keys() list[str]#

The keys to the evaluation object.

Used so Evaluation-objects can be used as dictionaries.

save(path: str, backup: bool = True, database: bool = False, name: str | None = None) None#

Stores the evaluation result in an HDF5 file.

Parameters:
  • path (str) – Path of the ‘.h5’ file in which the data should be stored. If only a directory is given, the filename is chosen as self.name. Raises an error if the given directory does not exist or if the file extension is not ‘.h5’.

  • backup (bool, optional) – If True, an already existing file is backed up and not overwritten. This keyword is ignored if database=True. The default is True.

  • database (bool, optional) – If True, the results are appended to the given ‘.h5’ file if it already exists. If False, a new file is created and the old is backed up. If False and the given ‘.h5’ file contains multiple evaluation results, an error is raised. In this case, database has to be set to True. The default is False.

  • name (str or None, optional) – Name under which the data should be stored in the HDF5 file. If None, self.name is used. The default is None.

Return type:

None.

property times#

Times at which the function is evaluated.

Returns:

Times at which the function is evaluated.

Return type:

np.ndarray