Coder Social home page Coder Social logo

Creating Frames in Python about garnett HOT 5 CLOSED

bdice avatar bdice commented on August 23, 2024
Creating Frames in Python

from garnett.

Comments (5)

bdice avatar bdice commented on August 23, 2024

Original comment by Carl Simon Adorf (Bitbucket: csadorf, GitHub: csadorf).


Can you elaborate on the second proposal? Maybe also with an example use case? I don't quite understand what you mean.

from garnett.

bdice avatar bdice commented on August 23, 2024

Original comment by Matthew Spellings (Bitbucket: mspells, GitHub: klarh).


Something like,

list_of_frames = [Frame(), ...]
writer = PosFileWriter()
output = open('dump.pos', 'w')

for frame in list_of_frames:
    writer.write_frame(frame, output)

for the use case where you don't have an actual list of frames sitting around to make a Trajectory out of, but are generating them on-the-fly (i.e. what Kyle is doing with his plato.viz module).

from garnett.

bdice avatar bdice commented on August 23, 2024

Original comment by Carl Simon Adorf (Bitbucket: csadorf, GitHub: csadorf).


What about:

#!python

for frame in generated_frames():
    writer.write([frame], output)

or alternatively:

#!python

writer.write(generated_frames(), output)

The reason why I'm hesitant to directly implement a write_frame() method is, that it requires the assumption that it is actually possible to write the trajectory frame-by-frame, which may not be true for all formats.

In the first case, you ignore this complication and force the writer to simply write length-1 trajectories which may be possible or not based on the format. In this case it's the user's responsibility to make sure that this is ok. For POS-files that's ok.

In the second case we give the writer more freedom in arranging the frame data in a way that is suitable for the format. That might mean for example to cache them all in memory until there is knowledge about how many frames to write.

See also: http://glotzerlab.engin.umich.edu/glotzformats/quickstart.html#efficient-modification-of-trajectories

from garnett.

bdice avatar bdice commented on August 23, 2024

Original comment by Matthew Spellings (Bitbucket: mspells, GitHub: klarh).


That's probably fine, and like you say, it works fine for pos files, at least. If you're using file objects, you can't do this for GSD files for instance, though, right? Another option would be to throw an error for file backends that don't support such an api...

from garnett.

bdice avatar bdice commented on August 23, 2024

Original comment by Carl Simon Adorf (Bitbucket: csadorf, GitHub: csadorf).


No, this works also with file-objects, here is a demonstration:

#!python

#!/usr/bin/env python
import sys
import argparse
import logging

import glotzformats as gf
import numpy as np
from tqdm import tqdm


logger = logging.getLogger(__name__)


def map_to_center(frame):
    frame.positions -= np.mean(frame.positions, axis=0)
    return frame


def main(args):
    pos_reader = gf.reader.PosFileReader()
    pos_writer = gf.writer.PosFileWriter()
    with open(args.posfile) as posfile:
        with open(args.out, 'w') as outfile:
            traj = pos_reader.read(posfile)
            N = len(traj)
            traj = (map_to_center(f) for f in traj)
            for frame in tqdm(traj, total=N):
                pos_writer.write([frame], outfile)
    return 0


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description="")
    parser.add_argument('posfile')
    parser.add_argument('out')
    args = parser.parse_args()
    logging.basicConfig(level=logging.WARNING)
    sys.exit(main(args))

from garnett.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.