Coverage for ase/io/bundlemanipulate.py: 5.71%
105 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1"""Functions for in-place manipulation of bundletrajectories.
3This module defines a number of functions that can be used to
4extract and delete data from BundleTrajectories directly on
5disk. The functions are intended for large-scale MD output,
6so they avoid copying the potentially large amounts of data.
7Instead, data is either directly deleted in-place; or copies
8are made by creating a new directory structure, but hardlinking
9the data files. Hard links makes it possible to delete the
10original data without invalidating the copy.
12Usage from command line:
14python -m ase.io.bundlemanipulate inbundle outbundle [start [end [step]]]
15"""
17import json
18import os
20import numpy as np
22from ase.io.bundletrajectory import UlmBundleBackend
25def copy_frames(inbundle, outbundle, start=0, end=None, step=1, verbose=False):
26 """Copies selected frame from one bundle to the next."""
27 if not (
28 isinstance(start, int)
29 and (isinstance(end, int) or end is None)
30 and isinstance(step, int)
31 ):
32 raise TypeError('copy_frames: start, end and step must be integers.')
33 metadata, nframes = read_bundle_info(inbundle)
35 if metadata['backend'] == 'ulm':
36 backend = UlmBundleBackend(True, metadata['ulm.singleprecision'])
37 elif metadata['backend'] == 'pickle':
38 raise OSError(
39 "Input BundleTrajectory uses the 'pickle' backend. "
40 + 'This is not longer supported for security reasons'
41 )
42 else:
43 raise OSError("Unknown backend type '{}'".format(metadata['backend']))
45 if start < 0:
46 start += nframes
47 if end is None:
48 end = nframes
49 if end < 0:
50 end += nframes
51 if start < 0 or (start > nframes - 1 and end > 0):
52 raise ValueError('copy_frames: Invalid start value.')
53 if end < 0 or (end > nframes - 1 and end < 0):
54 raise ValueError('copy_frames: Invalid end value.')
55 if step == 0:
56 raise ValueError('copy_frames: Invalid step value (zero)')
57 frames = list(range(start, end, step))
58 if verbose:
59 print('Copying the frames', frames)
61 # Make the new bundle directory
62 os.mkdir(outbundle)
63 with open(os.path.join(outbundle, 'metadata.json'), 'w') as fd:
64 json.dump(metadata, fd, indent=2)
66 for nout, nin in enumerate(frames):
67 if verbose:
68 print('F%i -> F%i' % (nin, nout))
69 indir = os.path.join(inbundle, 'F' + str(nin))
70 outdir = os.path.join(outbundle, 'F' + str(nout))
71 os.mkdir(outdir)
72 names = os.listdir(indir)
73 for name in names:
74 fromfile = os.path.join(indir, name)
75 tofile = os.path.join(outdir, name)
76 os.link(fromfile, tofile)
77 if nout == 0 and nin != 0:
78 if verbose:
79 print('F0 -> F0 (supplemental)')
80 # The smalldata.ulm stuff must be updated.
81 # At the same time, check if the number of fragments
82 # has not changed.
83 data0 = backend.read_small(os.path.join(inbundle, 'F0'))
84 data1 = backend.read_small(indir)
85 split_data = metadata['subtype'] == 'split'
86 if split_data:
87 fragments0 = data0['fragments']
88 fragments1 = data1['fragments']
90 data0.update(data1) # Data in frame overrides data from frame 0.
91 backend.write_small(outdir, data0)
93 # If data is written in split mode, it must be reordered
94 firstnames = os.listdir(os.path.join(inbundle, 'F0'))
95 if not split_data:
96 # Simple linking
97 for name in firstnames:
98 if name not in names:
99 if verbose:
100 print(' ', name, ' (linking)')
101 fromfile = os.path.join(inbundle, 'F0', name)
102 tofile = os.path.join(outdir, name)
103 os.link(fromfile, tofile)
104 else:
105 # Must read and rewrite data
106 # First we read the ID's from frame 0 and N
107 assert 'ID_0.ulm' in firstnames and 'ID_0.ulm' in names
108 backend.nfrag = fragments0
109 f0_id, _dummy = backend.read_split(
110 os.path.join(inbundle, 'F0'), 'ID'
111 )
112 backend.nfrag = fragments1
113 fn_id, fn_sizes = backend.read_split(indir, 'ID')
114 for name in firstnames:
115 # Only look at each array, not each file
116 if '_0.' not in name:
117 continue
118 if name not in names:
119 # We need to load this array
120 arrayname = name.split('_')[0]
121 print(' Reading', arrayname)
122 backend.nfrag = fragments0
123 f0_data, _dummy = backend.read_split(
124 os.path.join(inbundle, 'F0'), arrayname
125 )
126 # Sort data
127 f0_data[f0_id] = np.array(f0_data)
128 # Unsort with new ordering
129 f0_data = f0_data[fn_id]
130 # Write it
131 print(' Writing reshuffled', arrayname)
132 pointer = 0
133 backend.nfrag = fragments1
134 for i, s in enumerate(fn_sizes):
135 segment = f0_data[pointer : pointer + s]
136 pointer += s
137 backend.write(outdir, f'{arrayname}_{i}', segment)
138 # Finally, write the number of frames
139 with open(os.path.join(outbundle, 'frames'), 'w') as fd:
140 fd.write(str(len(frames)) + '\n')
143# Helper functions
144def read_bundle_info(name):
145 """Read global info about a bundle.
147 Returns (metadata, nframes)
148 """
149 if not os.path.isdir(name):
150 raise OSError(f"No directory (bundle) named '{name}' found.")
152 metaname = os.path.join(name, 'metadata.json')
154 if not os.path.isfile(metaname):
155 if os.path.isfile(os.path.join(name, 'metadata')):
156 raise OSError(
157 'Found obsolete metadata in unsecure Pickle format. '
158 'Refusing to load.'
159 )
160 else:
161 raise OSError(
162 "'{}' does not appear to be a BundleTrajectory (no {})".format(
163 name, metaname
164 )
165 )
167 with open(metaname) as fd:
168 mdata = json.load(fd)
170 if 'format' not in mdata or mdata['format'] != 'BundleTrajectory':
171 raise OSError(f"'{name}' does not appear to be a BundleTrajectory")
172 if mdata['version'] != 1:
173 raise OSError(
174 'Cannot manipulate BundleTrajectories with version '
175 'number %s' % (mdata['version'],)
176 )
177 with open(os.path.join(name, 'frames')) as fd:
178 nframes = int(fd.read())
179 if nframes == 0:
180 raise OSError(f"'{name}' is an empty BundleTrajectory")
181 return mdata, nframes
184if __name__ == '__main__':
185 import sys
187 if len(sys.argv) < 3:
188 print(__doc__)
189 sys.exit()
190 inname, outname = sys.argv[1:3]
191 if len(sys.argv) > 3:
192 start = int(sys.argv[3])
193 else:
194 start = 0
195 if len(sys.argv) > 4:
196 end: int | None = int(sys.argv[4])
197 else:
198 end = None
199 if len(sys.argv) > 5:
200 step = int(sys.argv[5])
201 else:
202 step = 1
203 copy_frames(inname, outname, start, end, step, verbose=1)