def run(self, scope, storage): """Save trajectory quantities.""" filename = self.arguments['filename'] box = scope['box'] box = garnett.trajectory.Box(Lx=box[0], Ly=box[1], Lz=box[2], xy=box[3], xz=box[4], yz=box[5]) positions = scope['position'] if 'orientation' in scope: orientations = scope['orientation'] else: orientations = np.tile([(1, 0, 0, 0)], (len(positions), 1)) types = scope['type'] if 'type_names' not in scope: type_names = [chr(ord('A') + t) for t in sorted(set(types))] else: type_names = scope['type_names'] the_frame = FakeFrame(box, positions, orientations, type_names, types, None, None, len(positions)) mode = ('w' if any(filename.endswith(suf) for suf in ['.pos', '.cif']) else 'wb') with storage.open(self.arguments['filename'], mode=mode, on_filesystem=True) as f: garnett.write([the_frame], f.name)
def make_gsd(job): try: with garnett.read(job.fn('dump.pos')) as traj: garnett.write(traj, job.fn('dump.gsd')) except garnett.errors.ParserError as e: print(e) job.doc.failed = True
def test_write_pos(self): with self.create_tmp_and_traj() as (tmp_dir, traj): tmp_name = os.path.join(tmp_dir, 'test.pos') garnett.write(traj, tmp_name) # Read back the file and check if it is the same as the original with garnett.read(tmp_name) as traj2: self.assertEqual(len(traj), len(traj2))
def test_write_format(self): with self.create_tmp_and_traj() as (tmp_dir, traj): # No suffix is given to the temp file, so no format is detected tmp_name = os.path.join(tmp_dir, 'test') garnett.write(traj, tmp_name, fmt='pos') # Read back the file and check if it is the same as the original with garnett.read(tmp_name, fmt='pos') as traj2: self.assertEqual(len(traj), len(traj2))
def test_write_io(self): with self.create_tmp_and_traj() as (tmp_dir, traj): tmp_name = os.path.join(tmp_dir, 'test_io.gsd') with open(tmp_name, 'wb') as gsdfile: garnett.write(traj, gsdfile) # Read back the file and check if it is the same as the original with garnett.read(tmp_name) as traj2: self.assertEqual(len(traj), len(traj2))
def test_default(self): with TemporaryDirectory() as tmp_dir: gsdfile = os.path.join(tmp_dir, 'testfile.gsd') posfile = os.path.join(tmp_dir, 'testfile.pos') with open(gsdfile, "wb") as f: f.write(base64.b64decode(garnett.samples.GSD_BASE64)) with garnett.read(gsdfile) as traj: with self.assertRaises(AttributeError): traj[-1].shapedef garnett.write(traj, posfile) with garnett.read(posfile) as traj: for frame in traj: for name in frame.shapedef.keys(): self.assertEqual(frame.shapedef[name], DEFAULT_SHAPE_DEFINITION)
def main(args): with garnett.read(args.infile) as traj: traj_centered = garnett.trajectory.Trajectory( (center(frame) for frame in traj)) garnett.write(traj_centered, args.outfile) return 0
def write(traj, *args, **kwargs): trajectory = [getattr(frame, '_garnett_frame', frame) for frame in traj] garnett.write(trajectory, *args, **kwargs)
def main(args): with garnett.read(args.infile) as traj: garnett.write(traj, args.outfile) return 0
def test_write_unsupported(self): with self.create_tmp_and_traj() as (tmp_dir, traj): # No suffix is given to the temp file, so no format is detected tmp_name = os.path.join(tmp_dir, 'test') with self.assertRaises(NotImplementedError): garnett.write(traj, tmp_name)