Target data Writers¶
The main entry point for writing target information is
- metatrain.utils.data.writers.get_writer(filename: str | Path, capabilities: ModelCapabilities | None = None, append: bool | None = None, fileformat: str | None = None) Writer[source]¶
Selects the appropriate writer based on the file extension.
For certain file suffixes, the systems will also be written (i.e
xyz).A path ending in a path separator (e.g.
predictions/) is treated as a memory-mappedMemmapDatasetdirectory rather than a single file. Since nothing exists on disk yet when a writer is selected, the trailing separator is the write-side equivalent of that check.The capabilities of the model are used to infer the type (physical quantity) of the predictions. In this way, for example, position gradients of energies can be saved as forces.
For the moment, strain gradients of the energy are saved as stresses (and not as virials).
- Parameters:
filename (str | Path) – name of the file to write, or a directory path ending in a path separator for a memmap dataset
capabilities (ModelCapabilities | None) – capabilities of the model
append (bool | None) – if
True, the data will be appended to the file, if it exists. IfFalse, the file will be overwritten. IfNone, the default behavior of the writer is used.fileformat (str | None) – format of the target value file. If
Nonethe format is determined from the file extension.
- Returns:
a
Writerinstance.- Raises:
ValueError – if
filenameboth ends in a path separator and has a recognized file suffix (e.g."predictions.zip/"), since it is then ambiguous whether a memmap directory or a file of that format was intended.- Return type:
Based on the provided filename the writer choses which child writer to use. The mapping which writer is used for which file type is stored in
- metatrain.utils.data.writers.PREDICTIONS_WRITERS: Dict[str, WriterFactory] = {'.mts': <function _make_factory.<locals>.factory>, '.xyz': <function _make_factory.<locals>.factory>, '.zip': <function _make_factory.<locals>.factory>}¶
dict: dictionary mapping file suffixes to a prediction writer
Implemented Writers¶
Writer Abstract Class¶
- class metatrain.utils.data.writers.Writer(filename: str | Path, capabilities: ModelCapabilities | None = None, append: bool | None = None)[source]¶
Bases:
ABC- Parameters:
capabilities (ModelCapabilities | None)
append (bool | None)
Available Implementations¶
The available implementations listed below represent concrete writers that inherit from
the Writer abstract class.
- class metatrain.utils.data.writers.ASEWriter(filename: str | Path, capabilities: ModelCapabilities | None = None, append: bool | None = False)[source]¶
Bases:
WriterWrite systems and predictions to an ASE-compatible XYZ file.
Systems and predictions are converted and written to disk immediately in each
write()call, avoiding unbounded memory growth for large datasets.- Parameters:
capabilities (ModelCapabilities | None) – Model capabilities (unused, but matches base signature).
append (bool | None) – If True, append to the existing file, otherwise overwrite.
- class metatrain.utils.data.writers.DiskDatasetWriter(path: str | Path, capabilities: ModelCapabilities | None = None, append: bool | None = False)[source]¶
Bases:
WriterWrite systems and predictions to a zip file, each system in a separate folder inside the zip.
Every finished zip carries a complete
metadata/atom_counts.npymember (the atom count of each entry, in entry order), which is what makes the dataset usable with atom-count-based sampling. Themetadata/folder is reserved for such dataset-level informative files.- Parameters:
capabilities (ModelCapabilities | None) – Model capabilities.
append (bool | None) – If True, open the zip file in append mode.
- write(systems: List[System], predictions: Dict[str, TensorMap]) None[source]¶
Write a single (system, predictions) into the zip under a new folder “<index>/”.
The stored TensorMaps’
"system"sample labels always hold the entry number, whether the systems arrive batched or one at a time. Distinct labels are required to read the dataset back for training (per-sample maps are joined at collation).
- class metatrain.utils.data.writers.MetatensorWriter(filename: str | Path, capabilities: ModelCapabilities | None = None, append: bool | None = False)[source]¶
Bases:
WriterWrite systems and predictions to Metatensor files (.mts).
Each
write()call saves the batch predictions to temporary files on disk, avoiding unbounded memory growth.finish()loads the temp files, concatenates them with correct system label offsets, and writes the final output.- Parameters:
filename (str | Path) – Base filename for the output files. Each target will be saved in a separate file with the target name appended.
capabilities (ModelCapabilities | None) – Model capabilities.
append (bool | None) – Whether to append to existing files, unused here but kept for compatibility with the base class.
- class metatrain.utils.data.writers.MemmapWriter(path: str | Path, capabilities: ModelCapabilities | None = None, append: bool | None = False)[source]¶
Bases:
WriterWrite systems and predictions to the on-disk layout consumed by
metatrain.utils.data.dataset.MemmapDataset: a directory containingns.npy,na.npy,x.bin,a.bin,c.bin, and one<target>.binfile per target (plus<target>_forces.bin/<target>_stress.binfor position/strain gradients). The resulting directory can be passed straight back tometatrainas asystems: read_from:dataset, which makes this format well suited for very large evaluation runs (e.g. >1M structures) that should not be re-read as a single in-memory XYZ file.The final number of structures/atoms is only known once evaluation has finished, so every array is streamed to its
.binfile (append-only, on everywrite()call) rather than buffered in memory or in a temporary directory. This keeps memory use and disk I/O to a single pass, at the cost of not knowing final shapes untilfinish(), where the smallns.npy/na.npyindex files (which require the final structure/atom counts) are written and all files are closed.- Parameters:
path (str | Path) – Directory path to write into (e.g.
predictions/), used as-is (unlike the other writers, no filename manipulation is performed). The directory is created if it does not exist yet. If it already exists and already contains any file, aFileExistsErroris raised immediately rather than writing into (or overwriting) a possibly unrelated directory: this check happens up front, before any (potentially very long) evaluation runs, instead of lazily discovering a conflict only when the colliding file would have been written.capabilities (ModelCapabilities | None) – Model capabilities (unused, but matches base signature).
append (bool | None) – Not supported for memmap datasets.