Skip to main content

ASDF - Advanced Scientific Data Format

CI Status https://readthedocs.org/projects/asdf/badge/?version=latest https://codecov.io/gh/asdf-format/asdf/branch/main/graphs/badge.svg https://zenodo.org/badge/18112754.svg https://img.shields.io/pypi/l/asdf.svg

The Advanced Scientific Data Format (ASDF) is a next-generation interchange format for scientific data. This package contains the Python implementation of the ASDF specification. More information on the ASDF file format including the specification can be found here.

The ASDF format has the following features:

  • Hierarchical and human-readable metadata in YAML format

  • Efficient binary array storage with support for memory mapping and flexible compression.

  • Content validation using schemas (using JSON Schema)

  • Native and transparent support for most basic Python data types, with an extension API to add support for any custom Python object.

ASDF is under active development on github. More information on contributing can be found below.

Overview

This section outlines basic use cases of the ASDF package for creating and reading ASDF files.

Creating a file

We’re going to store several numpy arrays and other data to an ASDF file. We do this by creating a “tree”, which is simply a dict, and we provide it as input to the constructor of AsdfFile:

import asdf
import numpy as np

# Create some data
sequence = np.arange(100)
squares = sequence**2
random = np.random.random(100)

# Store the data in an arbitrarily nested dictionary
tree = {
    "foo": 42,
    "name": "Monty",
    "sequence": sequence,
    "powers": {"squares": squares},
    "random": random,
}

# Create the ASDF file object from our data tree
af = asdf.AsdfFile(tree)

# Write the data to a new file
af.write_to("example.asdf")

If we open the newly created file’s metadata section, we can see some of the key features of ASDF on display:

#ASDF 1.0.0
#ASDF_STANDARD 1.2.0
%YAML 1.1
%TAG ! tag:stsci.edu:asdf/
--- !core/asdf-1.1.0
asdf_library: !core/software-1.0.0 {author: The ASDF Developers, homepage: 'http://github.com/asdf-format/asdf',
  name: asdf, version: 2.0.0}
history:
  extensions:
  - !core/extension_metadata-1.0.0
    extension_class: asdf.extension.BuiltinExtension
    software: {name: asdf, version: 2.0.0}
foo: 42
name: Monty
powers:
  squares: !core/ndarray-1.0.0
    source: 1
    datatype: int64
    byteorder: little
    shape: [100]
random: !core/ndarray-1.0.0
  source: 2
  datatype: float64
  byteorder: little
  shape: [100]
sequence: !core/ndarray-1.0.0
  source: 0
  datatype: int64
  byteorder: little
  shape: [100]
...

The metadata in the file mirrors the structure of the tree that was stored. It is hierarchical and human-readable. Notice that metadata has been added to the tree that was not explicitly given by the user. Notice also that the numerical array data is not stored in the metadata tree itself. Instead, it is stored as binary data blocks below the metadata section (not shown above).

It is possible to compress the array data when writing the file:

af.write_to("compressed.asdf", all_array_compression="zlib")

The built-in compression algorithms are 'zlib', and 'bzp2'. The 'lz4' algorithm becomes available when the lz4 package is installed. Other compression algorithms may be available via extensions.

Reading a file

To read an existing ASDF file, we simply use the top-level open function of the asdf package:

import asdf

af = asdf.open("example.asdf")

The open function also works as a context handler:

with asdf.open("example.asdf") as af:
    ...

To get a quick overview of the data stored in the file, use the top-level AsdfFile.info() method:

>>> import asdf
>>> af = asdf.open("example.asdf")
>>> af.info()
root (AsdfObject)
├─asdf_library (Software)
│ ├─author (str): The ASDF Developers
│ ├─homepage (str): http://github.com/asdf-format/asdf
│ ├─name (str): asdf
│ └─version (str): 2.8.0
├─history (dict)
│ └─extensions (list)
│   └─[0] (ExtensionMetadata)
│     ├─extension_class (str): asdf.extension.BuiltinExtension
│     └─software (Software)
│       ├─name (str): asdf
│       └─version (str): 2.8.0
├─foo (int): 42
├─name (str): Monty
├─powers (dict)
│ └─squares (NDArrayType): shape=(100,), dtype=int64
├─random (NDArrayType): shape=(100,), dtype=float64
└─sequence (NDArrayType): shape=(100,), dtype=int64

The AsdfFile behaves like a Python dict, and nodes are accessed like any other dictionary entry:

>>> af["name"]
'Monty'
>>> af["powers"]
{'squares': <array (unloaded) shape: [100] dtype: int64>}

Array data remains unloaded until it is explicitly accessed:

>>> af["powers"]["squares"]
array([   0,    1,    4,    9,   16,   25,   36,   49,   64,   81,  100,
        121,  144,  169,  196,  225,  256,  289,  324,  361,  400,  441,
        484,  529,  576,  625,  676,  729,  784,  841,  900,  961, 1024,
       1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849,
       1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916,
       3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225,
       4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776,
       5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569,
       7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604,
       9801])

>>> import numpy as np
>>> expected = [x**2 for x in range(100)]
>>> np.equal(af["powers"]["squares"], expected).all()
True

Memory mapping can be enabled by providing memmap=True to open:

af = asdf.open("example.asdf", memmap=True)

For more information and for advanced usage examples, see the documentation.

Extending ASDF

Out of the box, the asdf package automatically serializes and deserializes native Python types. It is possible to extend asdf by implementing custom tags that correspond to custom user types. More information on extending ASDF can be found in the official documentation.

Installation

Stable releases of the ASDF Python package are registered at PyPi. The latest stable version can be installed using pip:

$ pip install asdf

The latest development version of ASDF is available from the main branch on github. To clone the project:

$ git clone https://github.com/asdf-format/asdf

To install:

$ cd asdf
$ pip install .

To install in development mode:

$ pip install -e .

Testing

To install the test dependencies from a source checkout of the repository:

$ pip install -e ".[tests]"

To run the unit tests from a source checkout of the repository:

$ pytest

It is also possible to run the test suite from an installed version of the package.

$ pip install "asdf[tests]"
$ pytest --pyargs asdf

It is also possible to run the tests using nox.

$ pip install nox

To list all available sessions:

$ nox -l

To run a specific session:

$ nox -s "core-3.12"  # Run core ASDF tests in Python 3.12

Groups of sessions can also be specified by tag:

$ nox -t downstream  # Run all downstream sessions

Documentation

More detailed documentation on this software package can be found here.

More information on the ASDF file format itself can be found here.

If you are looking for the Adaptable Seismic Data Format, information can be found here.

License

ASDF is licensed under a BSD 3-clause style license. See LICENSE for the licenses folder for licenses for any included software.

Contributing

We welcome feedback and contributions to the project. Contributions of code, documentation, or general feedback are all appreciated. Please follow the contributing guidelines to submit an issue or a pull request.

We strive to provide a welcoming community to all of our users by abiding to the Code of Conduct.

Metadata

Release files for asdf 6.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asdf 6.0.0
File Size Uploaded Provenance Actions
asdf-6.0.0.tar.gz 923.3 kB Without provenance Details for asdf-6.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for asdf 6.0.0
File Interpreter ABI Platform Actions
asdf-6.0.0-py3-none-any.whl Python 3 none any Details for asdf-6.0.0-py3-none-any.whl

Total release size: 1.9 MB

Release files / asdf-6.0.0.tar.gz

Download URL asdf-6.0.0.tar.gz
Size 923.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c78c59dc7e5ec93b23479ee0b45193d1ea5a9661b8a1512a4f5a4daeeceeb7c1
BLAKE2b-256 checksum
How to use checksums
17735f03dadd18e7046492c193508971d9a630f9ddd88b58d6b700ed5d0eef06
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / asdf-6.0.0-py3-none-any.whl

Download URL asdf-6.0.0-py3-none-any.whl
Size 984.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
152d49f9b2a2cd3ac0321b3ae6d2c30e2cb83d944ec00b8205aebf827fa38a7f
BLAKE2b-256 checksum
How to use checksums
6cf7c0ce6f1152897b23263d8b87742b2fc1532d53bd4647a1c03a667962db56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for asdf 6.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

6.0.0 This release

2 release files No provenance

1.0.2

0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page