### Install and Load Compression Filter Packages Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/compression.md To use compression filters beyond the built-in ones, install and load the corresponding packages using Pkg. ```julia using Pkg # For other compression algorithms Pkg.add("JLD2Lz4") using JLD2, JLD2Lz4 # Load the package you need ``` -------------------------------- ### Filter Configuration Examples (Zstd and Bzip2) Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/compression.md Demonstrates configuring Zstd with different compression levels and Bzip2 with a custom block size. These configurations can be applied when writing data using `jldopen` and `write`. ```julia using JLD2, JLD2Lz4, JLD2Bzip2 # Zstd with different compression levels zstd_fast = ZstdFilter(level=1) # Fast compression zstd_best = ZstdFilter(level=22) # Best compression # Bzip2 with custom block size bzip2_filter = Bzip2Filter(blocksize100k=4) # Example usage jldopen("example.jld2", "w") do f write(f, "fast_data", zeros(UInt8, 10000); compress=zstd_fast) write(f, "small_data", randn(10000); compress=zstd_best) write(f, "archive_data", randn(1000); compress=bzip2_filter) end ``` -------------------------------- ### Pretty-Print JLD2 File Structure with display Source: https://context7.com/juliaio/jld2.jl/llms.txt Use the `display` function to get a formatted, human-readable representation of the JLD2 file's structure. This provides a convenient overview. ```julia using JLD2 f = jldopen("inspect.jld2") display(f) # Pretty-prints file structure close(f) ``` -------------------------------- ### Simpler Custom Serialization Example Source: https://context7.com/juliaio/jld2.jl/llms.txt A simplified example of custom serialization where a struct is serialized as a single primitive type (Float64). Demonstrates `writeas`, `wconvert`, and `rconvert`. ```julia struct Wrapper value::Float64 end JLD2.writeas(::Type{Wrapper}) = Float64 JLD2.wconvert(::Type{Float64}, w::Wrapper) = w.value JLD2.rconvert(::Type{Wrapper}, x::Float64) = Wrapper(x) # Array of Wrappers stored as Vector{Float64} arr = [Wrapper(rand()) for _ in 1:1000] @save "wrappers.jld2" arr ``` -------------------------------- ### Print Header Messages for an Array Dataset Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/troubleshooting.md This example shows how to print header messages for an array dataset ('b') in a JLD2 file. It highlights differences from scalar datasets, such as dataspace dimensions and contiguous data layout. ```julia JLD2.print_header_messages(f, "b") ``` -------------------------------- ### Inspect JLD2 File Contents with Keys and Length Source: https://context7.com/juliaio/jld2.jl/llms.txt Get the dataset names (keys) and the total number of datasets in a JLD2 file. Also, check for the existence of a specific dataset. ```julia using JLD2 f = jldopen("inspect.jld2") println(keys(f)) # Dataset names println(length(f)) # Number of datasets println(haskey(f, "scalar")) # Check existence ``` -------------------------------- ### Save Struct with Original Definition Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/advanced.md Saves a struct `A` with an `Int` field. This example sets up data that will later be loaded with a potentially different struct definition. ```julia using JLD2 struct A x::Int end jldsave("example.jld2"; a = A(42)) ``` -------------------------------- ### Demonstrate JLD2 Object Caching Behavior Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/troubleshooting.md This example shows how JLD2 caches objects. Editing a loaded array modifies the cached version, not the original file. A new copy is loaded after the cache is cleared. ```julia using JLD2 jldsave("demo.jld2", a=zeros(2)) f = jldopen("demo.jld2") a = f["a"] # bind loaded array to name `a` a[1] = 42; # editing the underlying array f["a"] a = nothing # remove all references to the loaded array GC.gc(true) # call GC to remove the cache f["a"] # a new copy is loaded from the file close(f) ``` -------------------------------- ### Serialize Custom Type as Built-in Type Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/customserialization.md This example demonstrates serializing a custom struct `B` directly as a `Float64` using `JLD2.writeas`, `JLD2.wconvert`, and `JLD2.rconvert`. The array of `B` structs is converted to a `Vector{Float64}` before saving. ```julia struct B x::Float64 end JLD2.writeas(::Type{B}) = Float64 JLD2.wconvert(::Type{Float64}, b::B) = b.x JLD2.rconvert(::Type{B}, x::Float64) = B(x) arr = [B(rand()) for i in 1:10] @save "test.jld2" arr ``` -------------------------------- ### Upgrade Structs with JLD2.rconvert Source: https://context7.com/juliaio/jld2.jl/llms.txt Define a conversion function for upgrading old struct types to new ones when loading data. This example shows how to add a default value for a new field. ```julia # Old struct # struct OldConfig # learning_rate::Float64 # epochs::Int # end # New struct with additional field struct NewConfig learning_rate::Float64 epochs::Int batch_size::Int # New field end # Define conversion from old format (loaded as NamedTuple) to new struct JLD2.rconvert(::Type{NewConfig}, nt::NamedTuple) = NewConfig(nt.learning_rate, nt.epochs, 32) # Default batch_size # Load with type upgrade config = load("old_config.jld2", "config"; typemap=Dict("Main.OldConfig" => JLD2.Upgrade(NewConfig))) ``` -------------------------------- ### Enable Default Compression with jldopen() Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/compression.md Use `jldopen` in write mode with `compress=true` to enable default compression when writing data. ```julia jldopen("example.jld2", "w"; compress = true) do f f["large_array"] = zeros(10000) end ``` -------------------------------- ### Enable Default Compression with jldsave() Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/compression.md Alternatively, use `jldsave` with the second argument set to `true` to enable default compression. ```julia jldsave("example.jld2", true; large_array=zeros(10000)) ``` -------------------------------- ### FileIO Interface - Load Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Demonstrates loading data from a JLD2 file using the FileIO interface, including loading all datasets, a single dataset, or multiple datasets. ```APIDOC ## GET /load ### Description Loads data from a JLD2 file using the FileIO interface. Can load all datasets, a specific dataset, or multiple datasets. ### Method GET (conceptual, as JLD2 uses functions) ### Endpoint `load(filename)` or `load(filename, dataset_name)` or `load(filename, dataset_name1, dataset_name2, ...)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - `filename` (string) - The name of the JLD2 file to load from. - `dataset_name` (string) - The name of the dataset to load. ### Request Example ```json { "example": "using JLD2\n# Load all datasets\nload(\"example.jld2\")\n# Load a single dataset\nload(\"example.jld2\", \"hello\")\n# Load multiple datasets\nload(\"example.jld2\", \"foo\", \"hello\")" } ``` ### Response #### Success Response (200) - Returns a `Dict` containing all datasets if no dataset name is specified. - Returns the content of the specified dataset if a single dataset name is provided. - Returns a tuple of contents if multiple dataset names are provided. #### Response Example ```json { "example": "// For load(\"example.jld2\")\nDict(\"hello\" => \"world\", \"foo\" => :bar)\n// For load(\"example.jld2\", \"hello\")\n\"world\"\n// For load(\"example.jld2\", \"foo\", \"hello\")\n(:bar, \"world\")" } ``` ``` -------------------------------- ### Enable Default Compression with save() Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/compression.md Use the `compress=true` argument with the `save` function to enable default compression for arrays. ```julia using JLD2 save("example.jld2", "large_array", zeros(10000); compress = true) ``` -------------------------------- ### Write and read data using file handles Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Interact with JLD2 files using a file-like interface with `jldopen`. Data can be written using `write` or bracket assignment, and read using `read` or bracket access. Remember to `close` the file handle when done. ```julia using JLD2 jldopen("example.jld2", "w") do f write(f, "variant1", 1.0) f["variant2"] = (rand(5), rand(Bool, 3)) end f = jldopen("example.jld2") v1 = read(f, "variant1") v2 = f["variant2"] close(f) v1, v2 ``` -------------------------------- ### Use Specific Compression Filter (Lz4) Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/compression.md Pass an instance of a specific filter, like `Lz4Filter()`, to the `compress` argument in `jldopen` to use that compression algorithm. ```julia using JLD2, JLD2Lz4 # Using Lz4 compression jldopen("example.jld2", "w"; compress = Lz4Filter()) do f f["large_array"] = zeros(10000) end ``` -------------------------------- ### File Handles - jldopen, write, read, close Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Interacting with JLD2 files using a file-like interface with `jldopen`, `write`, `read`, and `close` functions. ```APIDOC ## File Handle Operations (jldopen, write, read, close) ### Description Provides a file-like interface for interacting with JLD2 files. Allows opening files in various modes, writing data, reading data, and closing the file handle. ### Method - `jldopen`: Opens a file (similar to `open`). - `write`: Writes data to the opened file handle. - `read`: Reads data from the opened file handle. - `close`: Closes the file handle. ### Endpoint `jldopen(filename, mode)` `write(f, name, data)` or `f[name] = data` `read(f, name)` or `f[name]` `close(f)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - `filename` (string) - The name of the JLD2 file. - `mode` (string) - How the file should be opened (e.g., \"w\" for write, \"r\" for read, \"a\" for append). - `f` - The file handle returned by `jldopen`. - `name` (string) - The name of the dataset within the file. - `data` - The data to be written. ### Request Example ```json { "example": "using JLD2\njldopen(\"example.jld2\", \"w\") do f\n write(f, \"variant1\", 1.0)\n f[\"variant2\"] = (rand(5), rand(Bool, 3))\nend\n\nf = jldopen(\"example.jld2\")\nv1 = read(f, \"variant1\")\nv2 = f[\"variant2\"]\nclose(f)" } ``` ### Response #### Success Response (200) - `jldopen`: Returns a file handle `f`. - `write`: Data is written to the file. - `read`: Returns the requested data. - `close`: Closes the file handle. #### Response Example ```json { "example": "// For read operations\nv1 = 1.0\nv2 = (rand(5), rand(Bool, 3))" } ``` ``` -------------------------------- ### Pack and unpack data using UnPack.jl extension Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Integrate with UnPack.jl to use `@pack!` and `@unpack` macros for saving and loading variables directly to/from the file handle. This simplifies the process of storing and retrieving multiple variables. ```julia using JLD2, UnPack file = jldopen("example.jld2", "w") x, y = rand(2) @pack! file = x, y # equivalent to file["x"] = x; file["y"] = y @unpack x, y = file # equivalent to x = file["x"]; y = file["y"] close(file) ``` -------------------------------- ### FileIO Interface - Save Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Demonstrates saving data to a JLD2 file using the FileIO interface with both dictionary and argument-based methods. ```APIDOC ## POST /save ### Description Saves data to a JLD2 file using the FileIO interface. Data can be provided as a dictionary or as key-value arguments. ### Method POST (conceptual, as JLD2 uses functions) ### Endpoint `save(filename, data)` or `save(filename, key1, value1, key2, value2, ...)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - `filename` (string) - The name of the JLD2 file to save. - `data` (AbstractDict) - A dictionary where keys are dataset names (strings) and values are the data to be saved. - `key1`, `key2`, ... (string) - Dataset names. - `value1`, `value2`, ... - Data corresponding to the key names. ### Request Example ```json { "example": "using JLD2\nsave(\"example.jld2\", Dict(\"hello\" => \"world\", \"foo\" => :bar))\n# or\nsave(\"example.jld2\", \"hello\", \"world\", \"foo\", :bar)" } ``` ### Response #### Success Response (200) Data is saved to the specified file. #### Response Example None (operation is file-based) ``` -------------------------------- ### Combine Compression Filters (Shuffle + Deflate) Source: https://context7.com/juliaio/jld2.jl/llms.txt Applies multiple compression filters sequentially, such as Shuffle for preprocessing and Deflate for compression. Shuffle can improve compression ratios for numeric data. ```julia jldopen("shuffled.jld2", "w"; compress=[Shuffle(), Deflate()]) do f # Shuffle reorders bytes for better compression of numeric data f["integers"] = rand(UInt32, 100000) end ``` -------------------------------- ### Combine Multiple Filters (Shuffle and Deflate) Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/compression.md Chain multiple filters by providing a vector of filter instances to the `compress` argument. Preprocessing filters like `Shuffle()` should typically precede compression filters. ```julia using JLD2 # Combine Shuffle preprocessing with Deflate compression filters = [Shuffle(), Deflate()] jldopen("example.jld2", "w"; compress = filters) do f # Benefits from byte shuffling # Only the lowest byte of each element is non-zero # Shuffle() reorders the bytes of all elements from e.g. # [123123123] to [111222333] # where each digit refers to the nth byte of an array element. f["numeric_data"] = UInt.(rand(UInt8, 10000)) end ``` -------------------------------- ### Create a Sample JLD2 File for Inspection Source: https://context7.com/juliaio/jld2.jl/llms.txt Save various types of Julia data (scalar, array, nested tuple, dictionary) to a JLD2 file. This is a prerequisite for inspecting file contents. ```julia using JLD2 # Create a sample file jldsave("inspect.jld2"; scalar = 42, array = [1, 2, 3, 4, 5], nested = (a=1, b=2.0), group_data = Dict("x" => 1, "y" => 2) ) ``` -------------------------------- ### Enable Default Compression (Deflate) Source: https://context7.com/juliaio/jld2.jl/llms.txt Saves data to a JLD2 file with default compression enabled using the `compress=true` argument. This uses the Deflate filter. ```julia using JLD2 # Enable default compression (Deflate) save("compressed.jld2", "data", zeros(100000); compress=true) ``` -------------------------------- ### Upgrade Structs on Load with `rconvert` and `typemap` Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/advanced.md Demonstrates upgrading an old struct (`OldStructVersion`) to a new one (`UpdatedStruct`) during loading. It defines a custom `rconvert` method and uses `JLD2.Upgrade` within the `typemap` to handle the conversion. ```julia using JLD2 ### new session # This is the new version of your struct struct UpdatedStruct x::Float64 # no longer int y::Float64 z::Float64 # = x*y end # When upgrading a struct, JLD2 will load the fields of the old struct into a `NamedTuple` # and call `rconvert` on it. Here we implement a conversion method that returns an `UpdatedStruct` JLD2.rconvert(::Type{UpdatedStruct}, nt::NamedTuple) = UpdatedStruct(Float64(nt.x), nt.y, nt.x*nt.y) # Here we provide the `typemap` keyword argument. It is a dictionary mapping the stored struct name # to an `Upgrade` instance with the new struct. load("test.jld2", "data"; typemap=Dict("Main.OldStructVersion" => JLD2.Upgrade(UpdatedStruct))) ``` -------------------------------- ### Full Control with jldopen File Handle Interface Source: https://context7.com/juliaio/jld2.jl/llms.txt The jldopen function provides a file handle interface for full control over JLD2 files. It supports multiple modes: 'r', 'r+', 'w', and 'a'. The do-block syntax is recommended for writing. ```julia using JLD2 # Write mode with do-block (recommended) jldopen("example.jld2", "w") do f f["scalar"] = 42 f["array"] = rand(100, 100) f["tuple"] = (1, 2.0, "three") f["dict"] = Dict("a" => 1, "b" => 2) end ``` -------------------------------- ### Migrate Compression Filter Specification Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/compression.md Update compression filter specification from the old API (e.g., `ZlibCompressor()`) to the new API (e.g., `Deflate()`). The `compress=true` option remains functional. ```julia # Old API # using JLD2, CodecZlib # jldopen("file.jld2", "w"; compress = ZlibCompressor()) do f # New API using JLD2 jldopen("file.jld2", "w"; compress = Deflate()) do f # ... end ``` -------------------------------- ### Save data using FileIO interface with arguments Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Alternatively, pass dataset names and contents directly as arguments to the `save` function for a more concise syntax. ```julia using JLD2 save("example.jld2", "hello", "world", "foo", :bar) ``` -------------------------------- ### Load Plain Types Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/advanced.md Demonstrates loading data with and without the `plain` option. Use `plain=true` to load data as plain Julia types, bypassing custom type reconstruction. ```julia using JLD2 jldsave("test.jld2"; z= 1.0 + im * 2.0) load("test.jld2", "z") load("test.jld2", "z"; plain=true) @__MODULE__ ``` -------------------------------- ### Create Groups Implicitly using Path Syntax Source: https://context7.com/juliaio/jld2.jl/llms.txt Saves data to a JLD2 file using path-like strings (e.g., 'group/subgroup/key') to implicitly create groups. This is a convenient way to structure data. ```julia save("grouped.jld2", "experiment2/parameters", Dict("lr" => 0.001), "experiment2/results", [0.88, 0.92], "experiment2/metadata/notes", "second run" ) ``` -------------------------------- ### File Handles - Groups Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Demonstrates creating and accessing nested groups within JLD2 files, both explicitly and implicitly. ```APIDOC ## Groups in JLD2 Files ### Description JLD2 files support nesting datasets into groups for better organization. Groups can be created explicitly using `JLD2.Group` or implicitly by using path-like names for datasets. ### Method POST (conceptual for creation), GET (conceptual for access) ### Endpoint - Explicit: `JLD2.Group(parent, \"group_name\")` - Implicit: `save(filename, \"group/dataset\", data)` or `load(filename, \"group/dataset\")` - Access: `file[\"group\"][\"dataset\"]` or `file[\"group/dataset\"]` ### Parameters #### Path Parameters None #### Query Parameters - `nested` (boolean) - Optional. If `true`, loads nested groups as nested dictionaries. Defaults to `false` (unrolling paths). #### Request Body - `filename` (string) - The name of the JLD2 file. - `parent` - The parent group or file handle. - `group_name` (string) - The name of the group to create. - `dataset_name` (string) - The name of the dataset. - `data` - The data to be saved. ### Request Example ```json { "example": "using JLD2\n# Explicit group creation\njldopen(\"example.jld2\", \"w\") do file\n mygroup = JLD2.Group(file, \"mygroup\")\n mygroup[\"mystuff\"] = 42\nend\n\n# Implicit group creation\nsave(\"example.jld2\", \"anothergroup/data\", [1, 2, 3])\n\n# Accessing groups\njldopen(\"example.jld2\") do file\n println(file[\"mygroup\"][\"mystuff\"])\n println(file[\"anothergroup/data\"])\nend\n\n# Loading with nested option\nall_data = load(\"example.jld2\")\nnested_data = load(\"example.jld2\"; nested=true)" } ``` ### Response #### Success Response (200) - Data within groups is saved or loaded correctly. - `load` function returns data, potentially as nested dictionaries if `nested=true`. #### Response Example ```json { "example": "// For load(\"example.jld2\")\nDict(\"mygroup/mystuff\" => 42, \"anothergroup/data\" => [1, 2, 3])\n// For load(\"example.jld2\"; nested=true)\nDict(\"mygroup\" => Dict(\"mystuff\" => 42), \"anothergroup\" => Dict(\"data\" => [1, 2, 3]))" } ``` ``` -------------------------------- ### Write Data with Compression Source: https://context7.com/juliaio/jld2.jl/llms.txt Opens a JLD2 file in write mode ('w') with compression enabled. Compression is useful for reducing file size, especially for large arrays. ```julia jldopen("compressed.jld2", "w"; compress=true) do f f["large_array"] = zeros(100000) end ``` -------------------------------- ### Load Data with JLD2 Source: https://github.com/juliaio/jld2.jl/blob/master/README.md Use `load` to retrieve data from a JLD2 file. Specify the filename and the key for the data. ```julia load(filename, "data") ``` -------------------------------- ### Use Specific Compression Filter (Zstd) Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/compression.md Configure Zstd compression with a specific level by passing `ZstdFilter(level)` to the `compress` argument. ```julia using JLD2 # Zstd with non-standard compression level jldopen("example.jld2", "w"; compress = ZstdFilter(9)) do f f["large_array"] = zeros(10000) end ``` -------------------------------- ### Load all datasets from a JLD2 file Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Call `load` with only the filename to retrieve all datasets from the JLD2 file into a `Dict`. Ensure the filename has the ".jld2" suffix for automatic JLD2 detection. ```julia using JLD2 load("example.jld2") ``` -------------------------------- ### Load a single dataset from a JLD2 file Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md To load a specific dataset, provide its name as an argument to the `load` function. The filename must have the ".jld2" suffix. ```julia using JLD2 load("example.jld2", "hello") ``` -------------------------------- ### UnPack Extension Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Utilizing UnPack.jl's `@unpack` and `@pack!` macros with JLD2's file-like interface for convenient data saving and loading. ```APIDOC ## UnPack Extension for JLD2 ### Description When UnPack.jl is loaded, its `@unpack` and `@pack!` macros can be used with JLD2's file handle interface to streamline the process of saving and loading variables. ### Method POST (conceptual for packing), GET (conceptual for unpacking) ### Endpoint `@pack! file = var1, var2, ...` `@unpack var1, var2, ... = file` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - `file` - The JLD2 file handle. - `var1`, `var2`, ... - Variables to be packed into the file or unpacked from the file. ### Request Example ```json { "example": "using JLD2, UnPack\nfile = jldopen(\"example.jld2\", \"w\")\nx, y = rand(2)\n\n@pack! file = x, y # Equivalent to file[\"x\"] = x; file[\"y\"] = y\n@unpack x, y = file # Equivalent to x = file[\"x\"]; y = file[\"y\"]\nclose(file)" } ``` ### Response #### Success Response (200) Variables are efficiently packed into or unpacked from the JLD2 file using the specified macros. #### Response Example None (operation is file-based and variable manipulation) ``` -------------------------------- ### Per-Dataset Compression Control Source: https://context7.com/juliaio/jld2.jl/llms.txt Demonstrates controlling compression on a per-dataset basis within a single JLD2 file. You can override the file-level compression setting for individual writes. ```julia jldopen("mixed.jld2", "w"; compress=ZstdFilter()) do f # Uses file-level compression write(f, "compressible", zeros(10000)) # Override: no compression for random data write(f, "random", rand(10000); compress=false) # Override: use different filter write(f, "deflated", zeros(10000); compress=Deflate()) end ``` -------------------------------- ### Save and Load Data using FileIO Interface Source: https://context7.com/juliaio/jld2.jl/llms.txt The save and load functions from FileIO provide a simple interface for JLD2 files. Data can be saved using a Dict or name/value pairs, and loaded as a Dict or specific variables. ```julia using JLD2 # Save using Dict save("example.jld2", Dict("hello" => "world", "numbers" => [1,2,3], "flag" => true)) # Save using alternating name/value pairs save("example.jld2", "hello", "world", "numbers", [1,2,3], "flag", true) # Load all data as Dict data = load("example.jld2") # Dict{String, Any} with 3 entries: # "hello" => "world" # "numbers" => [1, 2, 3] # "flag" => true # Load single dataset hello = load("example.jld2", "hello") # "world" # Load multiple datasets as tuple numbers, flag = load("example.jld2", "numbers", "flag") # ([1, 2, 3], true) # Load with nested groups as nested dicts data = load("example.jld2"; nested=true) ``` -------------------------------- ### Load multiple datasets from a JLD2 file Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Load multiple datasets by specifying their names as arguments to the `load` function. The function returns the contents of the specified datasets as a tuple. ```julia using JLD2 load("example.jld2", "foo", "hello") ``` -------------------------------- ### Manually Select Compression for Datasets Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/compression.md Override file compression settings for specific datasets using the `compress` keyword argument in the `write` function. Use `false` to disable compression or specify a filter like `Deflate()` or `ZstdFilter()` with custom levels. ```julia using JLD2 jldopen("example.jld2", "w"; compress=ZstdFilter()) do f # This gets compressed with the ZstdFilter write(f, "default_array", zeros(10000)) # Don't compress this write(f, "random_array", rand(10000); compress=false) # Override the above compression filter and use a different one write(f, "zlib_array", zeros(10000); compress=Deflate()) # Alternatively, use the same filter but with different configuration write(f, "fast_compressed", rand(10000); compress=ZstdFilter(level= -20)) end ``` -------------------------------- ### Use Specific Compression Filters (Zstd) Source: https://context7.com/juliaio/jld2.jl/llms.txt Opens a JLD2 file with a specific compression filter, ZstdFilter, in write mode. This allows for different compression algorithms. ```julia jldopen("zstd.jld2", "w"; compress=ZstdFilter()) do f f["data"] = rand(100000) end ``` -------------------------------- ### Full Control over Type Reconstruction with Custom Mapping Function Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/advanced.md Provides a custom mapping function to `load` for advanced type remapping and reconstruction. This function receives the `JLDFile` object, type path, and parameters, allowing conditional mapping and custom conversions using `JLD2.Upgrade`. ```julia struct OldStruct{T} x::T end old_int = OldStruct(42) old_float = OldStruct(3.14) jldsave("test.jld2"; old_int, old_float, inttype=OldStruct{Int}, floattype=OldStruct{Float64}) struct NormalStruct{T} x::T end struct SquaredStruct{T} xsquared::T end JLD2.rconvert(::Type{SquaredStruct{T}}, nt) where T = SquaredStruct{T}(nt.x^2) typemap = function(f::JLD2.JLDFile, typepath::String, params::Vector) if typepath == "Main.OldStruct" if params[1] == Int @info "Mapping an OldStruct{Int} to SquaredStruct{Int} with conversion" # If the type param is Int, map to squared struct # and wrap in `Upgrade` to trigger custom conversion with `rconvert` return JLD2.Upgrade(SquaredStruct{Int}) else @info "Mapping an OldStruct{T} to NormalStruct{T} without conversion" # All other OldStructs just get updated to NormalStruct return NormalStruct{params...} end end # This typemap functino is called for every single type that is decoded. # All types that do not need special handling should be forwarded to the default # implementation. @info "Forwarding $typepath with parameters $params to default type mapping" return JLD2.default_typemap(f, typepath, params) end load("test.jld2"; typemap) ``` -------------------------------- ### Zstd Compression with Level Source: https://context7.com/juliaio/jld2.jl/llms.txt Configures Zstd compression with a specific compression level. Higher levels provide better compression but take longer. ```julia jldopen("zstd_best.jld2", "w"; compress=ZstdFilter(level=19)) do f f["data"] = rand(100000) end ``` -------------------------------- ### Save Data with JLD2 Source: https://github.com/juliaio/jld2.jl/blob/master/README.md Use `jldsave` for basic data saving. Specify the filename and the data to be saved. ```julia jldsave(filename; data) ``` -------------------------------- ### Load nested group data using path delimiters Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Load data from nested groups by specifying the full path with slashes as delimiters in the `load` function. ```julia using JLD2 load("example.jld2", "mygroup/mystuff") ``` -------------------------------- ### Save data using FileIO interface with Dict Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Use the `save` function from FileIO to store key-value pairs in a JLD2 file. The keys are strings representing dataset names, and values are their contents. ```julia using JLD2 save("example.jld2", Dict("hello" => "world", "foo" => :bar)) ``` -------------------------------- ### Store and Retrieve Single Object with save_object/load_object Source: https://context7.com/juliaio/jld2.jl/llms.txt Use save_object and load_object for storing and retrieving a single object, ideal for caching or simple data persistence. Compression is supported. ```julia using JLD2 # Define a complex structure struct ModelState weights::Matrix{Float64} bias::Vector{Float64} epoch::Int end state = ModelState(rand(100, 50), rand(50), 42) # Save single object save_object("model_state.jld2", state) # Load single object loaded_state = load_object("model_state.jld2") println(loaded_state.epoch) # Output: 42 # Works with any Julia type save_object("array.jld2", rand(1000, 1000)) big_array = load_object("array.jld2") # With compression save_object("compressed_state.jld2", state; compress=true) ``` -------------------------------- ### Print Header Messages for a Shared Datatype Dataset Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/troubleshooting.md This snippet illustrates printing header messages for a dataset ('c') with a shared datatype. It shows how to reference and inspect header messages of a shared datatype using its offset. ```julia JLD2.print_header_messages(f, "c") ``` ```julia JLD2.print_header_messages(f, JLD2.RelOffset(4520)) ``` -------------------------------- ### Print Header Messages for a Scalar Dataset Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/troubleshooting.md This snippet demonstrates how to print header messages for a scalar dataset ('a') in a JLD2 file using `JLD2.print_header_messages`. This reveals information about the data type, dataspace, and datalayout. ```julia using JLD2 jldsave("test.jld2"; a = 42, b = [1,2,3,4,5], c = (1,2), ) f = jldopen("test.jld2") JLD2.print_header_messages(f, "a") ``` -------------------------------- ### Work with IOBuffer in JLD2 Source: https://context7.com/juliaio/jld2.jl/llms.txt Use JLD2 with an IOBuffer for in-memory serialization and deserialization. This is an alternative to using raw byte vectors. ```julia using JLD2 # Work with IOBuffer io = IOBuffer() jldopen(io, "w") do f f["data"] = rand(100) end seekstart(io) # Reset position for reading jldopen(io, "r") do f data = f["data"] println("Loaded $(length(data)) elements") end ``` -------------------------------- ### Create nested groups implicitly using path delimiters Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Save data into nested groups by using slashes in the dataset name, which act as path delimiters. This provides an implicit way to create group structures. ```julia using JLD2 save("example.jld2", "mygroup/mystuff", 42) ``` -------------------------------- ### Load variables from JLD2 file Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/legacy.md Use the @load macro to retrieve specified variables from a JLD2 file into the current scope. The variables must exist in the file. ```julia @load "example.jld2" hello foo ``` -------------------------------- ### Save and Load Julia Data with JLD2 Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/index.md Use the `@save` macro to store Julia variables to a JLD2 file and `@load` to retrieve them. This is useful for persisting model states, simulation results, or any complex Julia data structures. ```julia using JLD2 # Some sample data model = (name = "Transformer", layers = 12, params = 300_000_000) scores = [0.91, 0.87, 0.93] # Save to a file @save "model_state.jld2" model scores # Load back later @load "model_state.jld2" model scores println(model.name) # Output: Transformer ``` ```julia @save "data.jld2" a=1 b=[1,2,3] c="hi" @load "data.jld2" b c ``` -------------------------------- ### Create Explicit Groups in JLD2 Source: https://context7.com/juliaio/jld2.jl/llms.txt Demonstrates creating hierarchical groups within a JLD2 file explicitly using `JLD2.Group`. This allows for better organization of related data. ```julia using JLD2 # Create groups explicitly jldopen("grouped.jld2", "w") do f # Create a group mygroup = JLD2.Group(f, "experiment1") mygroup["parameters"] = Dict("lr" => 0.01, "batch" => 32) mygroup["results"] = [0.91, 0.93, 0.95] # Create nested groups subgroup = JLD2.Group(mygroup, "metadata") subgroup["timestamp"] = "2024-01-15" end ``` -------------------------------- ### Save variables with compression option Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/legacy.md Enable compression when saving variables to a JLD2 file by passing options within curly braces. This can reduce file size. ```julia @save "example.jld2" {compress=true} hello bar=foo ``` -------------------------------- ### Print Detailed Header Messages for a Dataset in JLD2 Source: https://context7.com/juliaio/jld2.jl/llms.txt Display detailed header information for a specific dataset within a JLD2 file, including dataspace, datatype, and layout. Useful for debugging. ```julia using JLD2 f = jldopen("inspect.jld2") JLD2.print_header_messages(f, "array") # Shows dataspace, datatype, and layout information ``` -------------------------------- ### Access Grouped Data in JLD2 Source: https://context7.com/juliaio/jld2.jl/llms.txt Shows how to access data stored within groups in a JLD2 file, either by direct path or by navigating through group objects. ```julia jldopen("grouped.jld2") do f # Direct path access params = f["experiment1/parameters"] # Navigate through groups exp1 = f["experiment1"] results = exp1["results"] # List group contents println(keys(f["experiment1"])) # ["parameters", "results", "metadata"] end ``` -------------------------------- ### Load nested and unrolled groups Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md By default, `load` unrolls nested groups into paths. Use the `nested=true` keyword argument to load groups as nested dictionaries, preserving the hierarchical structure. ```julia using JLD2 load("example.jld2") load("example.jld2"; nested=true) ``` -------------------------------- ### jldsave Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Saves data using Julia's keyword argument syntax, useful when variable names match desired dataset names. ```APIDOC ## POST /jldsave ### Description Saves data to a JLD2 file using Julia's keyword argument syntax. This is convenient when the variable names in your Julia session directly correspond to the desired names of the datasets in the file. ### Method POST (conceptual, as JLD2 uses functions) ### Endpoint `jldsave(file; variable1=value1, variable2=value2, ...)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - `file` (string) - The name of the JLD2 file to save. - `variable1`, `variable2`, ... - Keyword arguments where the argument name becomes the dataset name and the argument value is the data to be saved. ### Request Example ```json { "example": "using JLD2\nmy_data = [1, 2, 3]\njldsave(\"data.jld2\", my_data=my_data)" } ``` ### Response #### Success Response (200) Data is saved to the specified file with dataset names matching the keyword argument names. #### Response Example None (operation is file-based) ``` -------------------------------- ### Read Data from JLD2 File Source: https://context7.com/juliaio/jld2.jl/llms.txt Opens a JLD2 file in read mode to access its contents. Use 'r' for read mode. Ensure the file is closed after use. ```julia jldopen("example.jld2", "r") do f println(keys(f)) # ["scalar", "array", "tuple", "dict"] println(f["scalar"]) # 42 println(haskey(f, "array")) # true data = read(f, "array") # Alternative to f["array"] end ``` -------------------------------- ### Append Data to JLD2 File Source: https://context7.com/juliaio/jld2.jl/llms.txt Opens an existing JLD2 file in append mode ('a') to add new data without overwriting existing content. Ensure the file is closed after use. ```julia jldopen("example.jld2", "a") do f f["new_data"] = "appended value" end ``` -------------------------------- ### Define Custom Serialization with JLD2 Methods Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/customserialization.md Alternatively, use JLD2's `wconvert` and `rconvert` methods for custom serialization. This is useful when you don't own the type or want to avoid extending `Base`. Ensure the serialization type is distinct from built-in types. ```julia JLD2.wconvert(::Type{ASerialization}, a::A) = ASerialization([a.x]) JLD2.rconvert(::Type{A}, a::ASerialization) = A(only(a.x)) ``` -------------------------------- ### Define Custom Serialization with Base.convert Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/customserialization.md Define a serialization type and overload `Base.convert` to specify how JLD2 should handle custom types during saving and loading. Always use a wrapper type for serialization to prevent conflicts with built-in types. ```julia struct A x::Int end struct ASerialization x::Vector{Int} end JLD2.writeas(::Type{A}) = ASerialization Base.convert(::Type{ASerialization}, a::A) = ASerialization([a.x]) Base.convert(::Type{A}, a::ASerialization) = A(only(a.x)) ``` -------------------------------- ### Manual File Open/Close in JLD2 Source: https://context7.com/juliaio/jld2.jl/llms.txt Manually opens a JLD2 file in read mode and explicitly closes it. Remember to always close the file handle to prevent data corruption. ```julia f = jldopen("example.jld2", "r") scalar = f["scalar"] close(f) ``` -------------------------------- ### Save Data with jldsave Function Source: https://context7.com/juliaio/jld2.jl/llms.txt The jldsave function uses keyword arguments for saving data, with variable names becoming dataset names. It supports optional compression. ```julia using JLD2 # Data to save a = 1 b = [1, 2, 3] c = "hello" config = Dict("learning_rate" => 0.001, "epochs" => 100) # Save using keyword syntax - variable names become dataset names jldsave("data.jld2"; a, b, c, config) # Save with compression enabled jldsave("compressed.jld2", true; a, b, c) # Save with explicit Zstd compression jldsave("zstd_compressed.jld2", ZstdFilter(3); a, b, c) # Equivalent to: # jldopen("data.jld2", "w") do f # f["a"] = a # f["b"] = b # f["c"] = c # f["config"] = config # end ``` -------------------------------- ### Single Object Storage Source: https://github.com/juliaio/jld2.jl/blob/master/docs/src/basic_usage.md Provides functions for saving and loading a single object to/from a JLD2 file. ```APIDOC ## POST /save_object & GET /load_object ### Description Functions to save a single object to a file or load a single object from a file. ### Method POST (save_object), GET (load_object) (conceptual) ### Endpoint `save_object(filename, object)` `load_object(filename)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - `filename` (string) - The name of the JLD2 file. - `object` - The data object to save. ### Request Example ```json { "example": "using JLD2\nmy_object = Dict(\"a\" => 1)\nsave_object(\"single_object.jld2\", my_object)\nloaded_object = load_object(\"single_object.jld2\")" } ``` ### Response #### Success Response (200) - `save_object`: Object is saved to the file. - `load_object`: Returns the loaded object. #### Response Example ```json { "example": "// For load_object\nDict(\"a\" => 1)" } ``` ``` -------------------------------- ### Define Custom Serialization for Types Source: https://context7.com/juliaio/jld2.jl/llms.txt Shows how to define custom serialization for user-defined types by specifying a serialization type and conversion methods (`wconvert`, `rconvert`). This allows JLD2 to handle complex objects. ```julia using JLD2 # Original type that may have complex internals struct DatabaseConnection host::String port::Int pool_size::Int # Imagine this has non-serializable fields like actual connections end # Serialization type - stores only what's needed struct DatabaseConnectionSerialized host::String port::Int pool_size::Int end # Tell JLD2 how to serialize JLD2.writeas(::Type{DatabaseConnection}) = DatabaseConnectionSerialized # Define conversions using wconvert/rconvert (preferred over Base.convert) JLD2.wconvert(::Type{DatabaseConnectionSerialized}, db::DatabaseConnection) = DatabaseConnectionSerialized(db.host, db.port, db.pool_size) JLD2.rconvert(::Type{DatabaseConnection}, s::DatabaseConnectionSerialized) = DatabaseConnection(s.host, s.port, s.pool_size) # Usage conn = DatabaseConnection("localhost", 5432, 10) @save "config.jld2" conn @load "config.jld2" conn println(conn.host) # Output: localhost ```