Ejemplo n.º 1
0
def convert(inp, out, fout, force=True, fields=None,
            exclude=None, include=None, steps=None):
    """
    Convert trajectory into a different format.

    `inp`: input trajectory object
    `out`: output trajectory class
    `fout`: output file

    If `out` is a string, we look for a matching trajectory format
    else we assume out is a trajectory class.
    If `out` is None, we rely on the factory guessing the format
    from the filename suffix.

    Return: name of converted trajectory file
    """
    from atooms.trajectory import Trajectory
    from atooms.trajectory.base import canonicalize_fields
    if isinstance(out, str):
        out_class = Trajectory.formats[out]
    else:
        out_class = out

    if fields is None and len(inp.fields) > 0 and (include is None or len(include) == 0):
        # We automatically include all fields from the input trajectory
        # Since the output trajectory may have extra fields, we do should not overwrite them
        include = canonicalize_fields(inp.fields)

    if fout != '/dev/stdout' and (os.path.exists(fout) and not force):
        print('File exists, conversion skipped')
    else:
        # Make sure parent folder exists
        from atooms.core.utils import mkdir
        mkdir(os.path.dirname(fout))
        with out_class(fout, 'w') as conv:
            modify_fields(conv, fields, include, exclude)
            conv.precision = inp.precision
            conv.timestep = inp.timestep
            conv.block_size = inp.block_size
            # In python 3, zip returns a generator so this is ok
            #
            # for system, step in zip(inp, inp.steps):
            #     conv.write(system, step)
            #
            # In python 2, zipping t and t.steps will load everything
            # in RAM. In this case, it is better to use enumerate()
            if steps is None:
                for i, system in progress(enumerate(inp), total=len(inp)):
                    conv.write(system, inp.steps[i])
            else:
                # Only include requested steps (useful to prune
                # non-periodic trajectories)
                for step in steps:
                    idx = inp.steps.index(step)
                    conv.write(inp[idx], step)

    return fout
Ejemplo n.º 2
0
def modify_fields(trajectory, fields=None, include=None, exclude=None):
    """
    Modify fields of a trajectory.

    Either provide a new list of fields, such as ['id', 'x', 'y'], or
    specify explicit patterns to exclude or include.
    """
    from atooms.trajectory.base import canonicalize_fields
    if fields is not None:
        # Reset the output format
        trajectory.fields = fields
    else:
        canonicalize_fields(trajectory.fields)
        # Exclude and/or include lists of patterns from output format
        if exclude is not None:
            canonicalize_fields(exclude)
            for pattern in exclude:
                if pattern in trajectory.fields:
                    trajectory.fields.remove(pattern)
        if include is not None:
            canonicalize_fields(include)
            for pattern in include:
                if pattern not in trajectory.fields:
                    trajectory.fields.append(pattern)

    return trajectory
Ejemplo n.º 3
0
    def __init__(self, filename, mode='r', fields=None):
        super(TrajectoryGSD, self).__init__(filename, mode)
        self._fields_default = ['id', 'pos']
        self.fields = copy(self._fields_default) if fields is None else fields
        self.fields = canonicalize_fields(self.fields)

        # self.mode can be 'w' or 'r', but gsd is a binary format, so it only accepts 'wb' or 'rb'.
        file_mode = self.mode + "b"
        # Trajectory file handle
        self.trajectory = gsd.hoomd.open(name=self.filename, mode=file_mode)
        # When reading, we must define the steps.
        if self.mode == 'r':
            self.steps = [snap.configuration.step for snap in self.trajectory]
Ejemplo n.º 4
0
    def __init__(self, filename, mode='r', fields=None):
        super(TrajectoryLerner, self).__init__(filename, mode)

        # Trajectory file handle
        self.mode = mode
        # Filename is actually a folder, in which we'll save snaps in separate files. 
        self.trajectory = open(self.filename)
        if mode == "w":
            os.makedirs(self.trajectory, exist_ok=True)
        # Since there is no timestep information in the Lerner format, we just enumerate the frames in the trajectory, i.e. we create 0.dat, 1.dat, etc.
        self.current_step = 0

        if mode == 'w':
            self.fields = copy(self.fields_default) if fields is None else fields
        if mode == 'r':
            self._read_header()
            self._read_data(filename)
            self._detect_fields()
            # When reading, we must define the steps. There is always only a single snapshot in this trajectory format, with no timestep information.
            self.steps = [0]

        self.fields = canonicalize_fields(self.fields)