|
2 | 2 |
|
3 | 3 | A new [Advanced Scientific Data Format (ASDF)](https://asdf-standard.readthedocs.io/en/latest/index.html) package, written in Julia. |
4 | 4 |
|
5 | | -## Quickstart |
| 5 | +## Introduction |
6 | 6 |
|
7 | | -### Installation |
| 7 | +The ASDF file format is based on the human-readable [YAML](http://yaml.org/) standard, extended with efficient binary blocks to store array data. Basic arithmetic types (`Bool`, `Int`, `Float`, `Complex`) and `String` types are supported out of the box. Other types (structures) need to be declared to be supported. |
| 8 | + |
| 9 | +ASDF supports arbitrary array strides, both C (Python) and Fortran (Julia) memory layouts, as well as compression. The YAML metadata can contain arbitrary information corresponding to scalars, arrays, or dictionaries. |
| 10 | + |
| 11 | +The ASDF file format targets a similar audience as the [HDF5](https://www.hdfgroup.org/solutions/hdf5/) format. |
| 12 | + |
| 13 | +## Installation |
8 | 14 |
|
9 | 15 | ```julia-repl |
10 | 16 | pkg> add ASDF, OrderedCollections |
11 | 17 | ``` |
12 | 18 |
|
13 | | -### Usage |
14 | | - |
15 | | -```@example quickstart |
| 19 | +```@repl intro |
16 | 20 | using ASDF, OrderedCollections |
| 21 | +``` |
17 | 22 |
|
18 | | -doc = OrderedDict( |
19 | | - "field_1" => [5, 6, 7, 8], |
20 | | - "field_2" => ["up", "down", "left", "right"], |
21 | | - "field_3" => OrderedDict( |
22 | | - "field_3a" => ["apple", "orange", "pear"], |
23 | | - "field_3b" => [1.0, 2.0, 3.0], |
24 | | - ) |
25 | | -) |
| 23 | +!!! note |
| 24 | + We use an `OrderedDict` from [OrderedCollections.jl](https://github.com/JuliaCollections/OrderedCollections.jl) to preserve the order of data on write and load, and to maximize compatibility with YAML.jl. |
26 | 25 |
|
27 | | -save("example.asdf", doc) |
| 26 | +## Getting started |
| 27 | + |
| 28 | +ASDF files are initially created as a dictionary with arbitrarily nested data: |
| 29 | + |
| 30 | +```@example intro |
| 31 | +af_payload = OrderedDict("field_1" => [5, 6, 7, 8], "field_2" => ["up", "down", "left", "right"], "field_3" => OrderedDict("field_3a" => ["apple", "orange", "pear"], "field_3b" => [1.0, 2.0, 3.0])) |
28 | 32 | ``` |
29 | 33 |
|
30 | | -```@example quickstart |
31 | | -af = load("example.asdf") |
| 34 | +ASDF.jl is registered with [FileIO.jl](https://juliaio.github.io/FileIO.jl/stable/), so this data can be written to the ASDF file format with the generic [`save`](@ref) function: |
| 35 | + |
| 36 | +```@example intro |
| 37 | +save("intro.asdf", af_payload) |
32 | 38 | ``` |
33 | 39 |
|
34 | | -```@example quickstart |
| 40 | +The saved file contains the following human-readable contents: |
| 41 | + |
| 42 | +!!! details "View file" |
| 43 | + ```@example intro |
| 44 | + read("intro.asdf", String) |> print |
| 45 | + ``` |
| 46 | + |
| 47 | +which can be loaded back with FileIO.jl's generic [`load`](@ref) function: |
| 48 | + |
| 49 | +```@example intro |
| 50 | +af = load("intro.asdf") |
| 51 | +``` |
| 52 | + |
| 53 | +This is stored as an [`ASDF.ASDFFile`](@ref). To change the number of rows shown, pass this object to [`ASDF.info`](@ref): |
| 54 | + |
| 55 | +```@example intro |
35 | 56 | ASDF.info(af; max_rows = 3) |
36 | 57 | ``` |
| 58 | + |
| 59 | +It contains a `metadata` field, which is a new dictionary that merges information about this library (stored under the `asdf_library` key) with the original user-defined `af_payload` dictionary. For convenience, `af.metadata[<key>]` can be accessed directly as `af[key]`. Since the underlying data is a dictionary, it can be modified in the standard way: |
| 60 | + |
| 61 | +```@example intro |
| 62 | +af["field_1"] = [50, 60, 70, 80] |
| 63 | +``` |
| 64 | + |
| 65 | +The convenience syntax can also be used to save the modified `ASDF.ASDFFile` object directly: |
| 66 | + |
| 67 | +```@example intro |
| 68 | +save("intro_modified.asdf", af) |
| 69 | +``` |
| 70 | + |
| 71 | +!!! details "View file" |
| 72 | + ```@example intro |
| 73 | + read("intro_modified.asdf", String) |> print |
| 74 | + ``` |
| 75 | + |
| 76 | +## Array storage |
| 77 | + |
| 78 | +By default, array data is written inline as a literal to the ASDF file. This can be stored and later accessed more efficiently by wrapping your data in an [`ASDF.NDArrayWrapper`](@ref). This allows for your data to be stored as a binary via the `inline = false` keyword (default), which can be further optimized by specifying a supported [compression algorithm](@ref ASDF.Compression) to use via the `compression` keyword: |
| 79 | + |
| 80 | +```@example intro |
| 81 | +af_payload = OrderedDict("meta" => OrderedDict("my" => OrderedDict("nested" => "metadata")), "data" => ASDF.NDArrayWrapper([1, 2, 3, 4]; compression = ASDF.C_Bzip2)) |
| 82 | +
|
| 83 | +save("intro_compressed.asdf", af_payload) |
| 84 | +
|
| 85 | +af = load("intro_compressed.asdf") |
| 86 | +``` |
| 87 | + |
| 88 | +!!! details "View file" |
| 89 | + ```julia-repl |
| 90 | + julia> read("intro_compressed.asdf", String) |> print |
| 91 | + #ASDF 1.0.0 |
| 92 | + #ASDF_STANDARD 1.6.0 |
| 93 | + # This is an ASDF file <https://asdf-standard.readthedocs.io/> |
| 94 | + %YAML 1.1 |
| 95 | + %TAG ! tag:stsci.edu:asdf/ |
| 96 | + --- |
| 97 | + !core/asdf-1.1.0 |
| 98 | + meta: |
| 99 | + my: |
| 100 | + nested: "metadata" |
| 101 | + data: !core/ndarray-1.1.0 |
| 102 | + source: 0 |
| 103 | + shape: |
| 104 | + - 4 |
| 105 | + datatype: "int64" |
| 106 | + byteorder: "little" |
| 107 | + asdf_library: !core/software-1.0.0 |
| 108 | + name: "ASDF.jl" |
| 109 | + author: "Erik Schnetter <schnetter@gmail.com>" |
| 110 | + homepage: "https://github.com/JuliaAstro/ASDF.jl" |
| 111 | + version: "2.0.0" |
| 112 | + ... |
| 113 | + �BLK0 f�0xj�sq���r#ASDF BLOCK INDEX |
| 114 | + %YAML 1.1 |
| 115 | + --- |
| 116 | + [463,] |
| 117 | + ... |
| 118 | + ``` |
| 119 | + |
| 120 | +Using `NDArrayWrapper` allows for the wrapped data to be lazily accessed as a strided view. To access the underlying data, use the `[]` (dereference) syntax: |
| 121 | + |
| 122 | +```@example intro |
| 123 | +af["data"][] == [1, 2, 3, 4] |
| 124 | +``` |
| 125 | + |
| 126 | +## Tagged objects |
| 127 | + |
| 128 | +Come back soon to see how custom Julia objects can be handled in ASDF.jl. |
0 commit comments