Ejemplo n.º 1
0
    def get_latest_checkpoint_file(directory_name):
        """
        Yields the file with the most up-to-date checkpoint in it in terms of the 'step'.
        :param directory_name: the directory in which to look for checkpoints
        :return: the latest checkpoint or raise if directory is empty
        """
        from readdy import Trajectory

        checkpoint_files = Simulation.list_checkpoint_files(directory_name)
        if len(checkpoint_files) == 0:
            raise ValueError(
                "No checkpoints found in {}".format(directory_name))
        latest = None
        latest_step = None
        for filename in checkpoint_files:

            traj = Trajectory(filename)
            ckpt = traj.list_checkpoints()

            # print(f"GetLatestCheckpointFile: File {filename} checkpoints {ckpt}")

            latest_ckpt_step = 0
            for c in ckpt:
                latest_ckpt_step = c['step'] if latest_step is None else (
                    c['step'] if c['step'] > latest_step else latest_step)
            if latest is None:
                latest = (filename, ckpt)
                latest_step = latest_ckpt_step
                # print(f"  -> got new latest {latest}")
            else:
                if latest_ckpt_step > latest_step:
                    latest = (filename, ckpt)
                    latest_step = latest_ckpt_step
                    # print(f"  -> got new latest {latest}")
        return latest[0]
Ejemplo n.º 2
0
 def list_checkpoints(file_name):
     """
     Returns a list of checkpoints in a trajectory file.
     :param file_name: the trajectory file
     """
     from readdy import Trajectory
     traj = Trajectory(file_name)
     return traj.list_checkpoints()
Ejemplo n.º 3
0
 def list_checkpoints(file_name):
     """
     Returns a list of checkpoints in a trajectory file.
     :param file_name: the trajectory file
     """
     from readdy import Trajectory
     traj = Trajectory(file_name)
     return traj.list_checkpoints()
Ejemplo n.º 4
0
 def list_checkpoint_files(directory_name):
     import os
     from glob import glob
     from readdy import Trajectory
     files = glob(os.path.join(directory_name, '*.h5'))
     result = []
     for f in files:
         try:
             traj = Trajectory(f)
             traj.list_checkpoints()
             result.append(f)
         except:
             pass
     return result
Ejemplo n.º 5
0
    def load_particles_from_checkpoint(self, file_name, n=None):
        """
        Adds particles to the simulation as contained in the n-th checkpoint of a trajectory file.
        :param file_name: the trajectory file
        :param n: if n is None, retrieve configuration from latest checkpoint, otherwise use 'n-th' checkpoint, n >= 0
        """
        import numpy as _np
        from readdy import Trajectory
        from readdy.api.trajectory import _CKPT
        from readdy.util.io_utils import get_particle_types
        checkpoints = self.list_checkpoints(file_name)
        if n is None:
            n = len(checkpoints) - 1
        else:
            assert n < len(
                checkpoints
            ), f"n={n} is out of bounds, only have {len(checkpoints)} checkpoints"
        assert n >= 0, f"n must be positive but was {n}"

        # group particle types by flavor (NORMAL, TOPOLOGY)
        ptypes = get_particle_types(filename=file_name)
        normal_types = []
        topology_types = []
        for t in ptypes.values():
            if t['flavor'] == 'TOPOLOGY':
                topology_types.append(t['type_id'])
            if t['flavor'] == 'NORMAL':
                normal_types.append(t['type_id'])

        # load frame into memory
        traj = Trajectory(file_name)
        n_particles_per_frame, positions, types, ids = traj.to_numpy(
            start=n, stop=n + 1, name=_CKPT.POSITIONS_CKPT)

        # add particles with flavor NORMAL
        for normal_type in normal_types:
            tixs = _np.argwhere(types[0] == normal_type)
            pos = positions[0][tixs].squeeze()
            if len(pos) > 0:
                self.add_particles(traj.species_name(normal_type),
                                   _np.atleast_2d(pos))

        # add topologies
        time, topology_records = traj.read_observable_topologies(
            start=n, stop=n + 1, data_set_name=_CKPT.TOPOLOGY_CKPT)
        assert len(topology_records) == 1
        for topology in topology_records[0]:
            particle_types = [
                traj.species_name(types[0, i]) for i in topology.particles
            ]
            pos = _np.atleast_2d(
                _np.array([positions[0, i] for i in topology.particles]))
            top = self.add_topology(traj.topology_type_name(topology.type),
                                    particle_types, pos)
            for e in topology.edges:
                top.graph.add_edge(e[0], e[1])
Ejemplo n.º 6
0
    def load_particles_from_checkpoint(self, file_name, n=None):
        """
        Adds particles to the simulation as contained in the n-th checkpoint of a trajectory file.
        :param file_name: the trajectory file
        :param n: if n is None, retrieve configuration from latest checkpoint, otherwise use 'n-th' checkpoint, n >= 0
        """
        import numpy as _np
        from readdy import Trajectory
        from readdy.api.trajectory import _CKPT
        from readdy.util.io_utils import get_particle_types
        checkpoints = self.list_checkpoints(file_name)
        if n is None:
            n = len(checkpoints)-1
        else:
            assert n < len(checkpoints), f"n={n} is out of bounds, only have {len(checkpoints)} checkpoints"
        assert n >= 0, f"n must be positive but was {n}"

        # group particle types by flavor (NORMAL, TOPOLOGY)
        ptypes = get_particle_types(filename=file_name)
        normal_types = []
        topology_types = []
        for t in ptypes.values():
            if t['flavor'] == 'TOPOLOGY':
                topology_types.append(t['type_id'])
            if t['flavor'] == 'NORMAL':
                normal_types.append(t['type_id'])

        # load frame into memory
        traj = Trajectory(file_name)
        n_particles_per_frame, positions, types, ids = traj.to_numpy(start=n, stop=n+1, name=_CKPT.POSITIONS_CKPT)

        # add particles with flavor NORMAL
        for normal_type in normal_types:
            tixs = _np.argwhere(types[0] == normal_type)
            pos = positions[0][tixs].squeeze()
            if len(pos) > 0:
                self.add_particles(traj.species_name(normal_type), _np.atleast_2d(pos))

        # add topologies
        time, topology_records = traj.read_observable_topologies(start=n, stop=n+1, data_set_name=_CKPT.TOPOLOGY_CKPT)
        assert len(topology_records) == 1
        for topology in topology_records[0]:
            particle_types = [traj.species_name(types[0, i]) for i in topology.particles]
            pos = _np.atleast_2d(_np.array([positions[0, i] for i in topology.particles]))
            top = self.add_topology(traj.topology_type_name(topology.type), particle_types, pos)
            for e in topology.edges:
                top.graph.add_edge(e[0], e[1])