Datamonger for Julia · 0.1.0

<a id="an-offline-data-workflow-in-julia"></a>

# An offline data workflow in Julia

A reproducible data workflow needs more than a dataset name—it needs an exact version, the records that describe that version, and a check that the retrieved bytes decode into the expected values. This tutorial follows those steps and shows how to inspect and clean its cache. 

<a id="inspect-the-available-data"></a>

## Inspect the available data

Load Datamonger and choose a private cache directory for the walkthrough: 

```julia
using Datamonger
using DataFrames
using SparseArrays
using SHA
using JSON3

cache = mktempdir()
registry = BUNDLED_REGISTRY
available = list_data(; registry, cache_dir=cache, offline=true)
sort([info.dataset_id for info in available])
```

```text
57-element Vector{String}:
 "datamonger:brca1@1"
 "datamonger:godlewski2023@1"
 "datamonger:guyon2004-gisette@2"
 "datamonger:guyon2004-madelon@1"
 "datamonger:koussounadis2014@1"
 "datamonger:rhee2006@1"
 "datamonger:scheetz2006@1"
 "datamonger:sharma2026-california@1"
 "datamonger:sharma2026-delaware@1"
 "datamonger:sharma2026-texas@1"
 ⋮
 "libsvm:yearpredictionmsd@1"
 "uci:abalone@1"
 "uci:breast-cancer-wisconsin-diagnostic@1"
 "uci:covertype@1"
 "uci:iris@1"
 "uci:p53-mutants@1"
 "uci:wholesale-customers@1"
 "uci:wine-quality-red@1"
 "uci:wine-quality@1"
```

Before deciding to retrieve its data, you may want to inspect a dataset's metadata: 

```julia
iris_info = data_info("uci:iris@1"; registry, cache_dir=cache, offline=true)
(
    title=iris_info.title,
    license=iris_info.license["identifier"],
    provider=iris_info.provenance["provider"],
    decoder=iris_info.representation["decoder"],
)
```

```text
(title = "Iris", license = "CC-BY-4.0", provider = "UCI Machine Learning Repository", decoder = "delimited-text")
```

The record includes the source's provenance, license information, tasks, expected components, and verification records. A missing or unknown license is useful information to investigate; registry inclusion itself grants no usage rights. 

<a id="prepare-an-offline-example"></a>

## Prepare an offline example

The repository includes a tiny CSV with floating-point measurements, integer counts, text labels, and Boolean flags. Its registry record is part of the shared conformance corpus used by the language clients. Find the fixtures relative to the installed checkout: 

```julia
fixtures = joinpath(pkgdir(Datamonger), "test", "fixtures")
selector = JSON3.read(
    read(joinpath(fixtures, "registry", "selector.json")),
    Dict{String,Any},
)
example_registry = Registry(
    selector["release"],
    selector["index_sha256"],
    selector["index_url"],
)
```

An offline fetch needs both the selected index and its artifacts in the cache. For this self-contained demonstration, copy the checked-in files to their content-addressed locations. Normal online calls populate these locations for you; manual cache preparation is only needed for this fixture walkthrough. 

```julia
registry_directory = joinpath(cache, "registries", "sha256")
artifact_directory = joinpath(cache, "objects", "sha256")
mkpath(registry_directory)
mkpath(artifact_directory)
cp(
    joinpath(fixtures, "registry", "index.json"),
    joinpath(registry_directory, example_registry.index_sha256),
)
for path in readdir(joinpath(fixtures, "conformance", "artifacts"); join=true)
    digest = bytes2hex(sha256(read(path)))
    cp(path, joinpath(artifact_directory, digest))
end
```

The filenames are SHA-256 digests. Copying a file into the cache does not make it trusted: Datamonger still verifies its bytes before using it. The fixture registry's download addresses are placeholders, and `offline=true` forbids any attempt to contact them. 

<a id="fetch-and-verify-a-table"></a>

## Fetch and verify a table

Request both the decoded data and its reproducibility metadata: 

```julia
result = fetch_data(
    "conformance:mixed_csv@1";
    registry=example_registry,
    cache_dir=cache,
    offline=true,
    return_info=true,
)
table = result.data
@assert table isa DataFrame
@assert size(table) == (5, 4)
table
```

```text
5×4 DataFrame
 Row │ measurement  count    label          enabled
     │ Float64?     Int64?   String?        Bool?
─────┼──────────────────────────────────────────────
   1 │         1.5        1  alpha             true
   2 │        -0.0  missing  quoted, value    false
   3 │   missing          3  says "hello"   missing
   4 │        20.0        4  missing           true
   5 │         0.0        5  omega            false
```

The table has five rows and four columns. The result separates the analysis object, `result.data`, from the fetch record, `result.info`. Delimited data becomes a `DataFrame`, and column names come from the registry recipe. The CSV decoder handles quoting, escaped quotes, and missing fields according to that recipe. 

```julia
(
    dataset=result.info.dataset_id,
    release=result.info.registry_release,
    verification=result.info.verification,
    canonical_digest=result.info.canonical_digest,
)
```

```text
(dataset = "conformance:mixed_csv@1", release = "test-0002", verification = :decoded, canonical_digest = "e25d27e8b0008332d778cd48429a7c4f7af59411884092e52f120da63f26e726")
```

The `:decoded` verification status means that artifact hashes, decoded shapes, and the canonical logical digest all passed. The canonical digest describes the logical values rather than a language's in-memory representation. 

`verify_decoded=false` skips shape and canonical verification and reports `:artifact` instead. It still verifies artifact bytes. Keep the default unless you have a specific reason to skip the decoded checks. 

<a id="work-with-missing-values-and-exact-integers"></a>

## Work with missing values and exact integers

Datamonger preserves column types. A column with missing values uses `Union{Missing,T}` rather than converting everything to text or floating point: 

```julia
[(name, eltype(table[!, name])) for name in names(table)]
```

```text
4-element Vector{Tuple{String, Union}}:
 ("measurement", Union{Missing, Float64})
 ("count", Union{Missing, Int64})
 ("label", Union{Missing, String})
 ("enabled", Union{Missing, Bool})
```

Use Julia's usual missing-value operations when computing summaries. For example, the measurement column has four observed values: 

```julia
measurements = collect(skipmissing(table.measurement))
@assert length(measurements) == 4
sum(measurements) / length(measurements)
```

```text
5.375
```

Counts remain exact `Int64` values. Boolean values remain Boolean, and quoted commas and quotation marks remain part of the decoded strings. Datamonger does not impute missing observations or transform variables for a model. 

<a id="retrieve-sparse-data-and-traintest-splits"></a>

## Retrieve sparse data and train/test splits

LIBSVM and SVMLight representations return a `SparseDataset` with `features` and `response` fields: 

```julia
sparse_data = fetch_data(
    "conformance:small_libsvm@1";
    registry=example_registry,
    cache_dir=cache,
    offline=true,
)
@assert sparse_data isa SparseDataset
(
    shape=size(sparse_data.features),
    stored_values=nnz(sparse_data.features),
    response=sparse_data.response,
)
```

```text
(shape = (2, 4), stored_values = 3, response = [1, -1])
```

The feature matrix normally uses `SparseMatrixCSC`. Exceptionally wide data uses the read-only `CSRMatrix` type to avoid allocating an impractical number of column pointers. Both provide matrix indexing, `size`, and `nnz`; check the concrete type before assuming a downstream solver supports it. Responses keep the recipe's exact integer or floating-point type. 

A registry can also describe an explicit training and test pair: 

```julia
split = fetch_data(
    "conformance:small_libsvm_split@1";
    registry=example_registry,
    cache_dir=cache,
    offline=true,
)
@assert split isa SparseDatasetSplit
(train=size(split.train.features), test=size(split.test.features))
```

```text
(train = (2, 4), test = (2, 4))
```

A `SparseDatasetSplit` preserves the published split. Datamonger does not shuffle observations or choose a new split for you. 

<a id="inspect-artifact-bytes-and-cache-contents"></a>

## Inspect artifact bytes and cache contents

Use `fetch_artifact` when another tool needs the source file rather than a Julia object. It returns a path to verified cached bytes: 

```julia
artifact_path = fetch_artifact(
    "conformance:mixed_csv@1";
    registry=example_registry,
    cache_dir=cache,
    offline=true,
)
read(artifact_path, String)
```

```text
"measurement,count,label,enabled\n1.5,1,alpha,true\n-0.0,,\"quoted, value\",false\n,3,\"says \"\"hello\"\"\",\n2e1,4,,true\n0,5,omega,false\n"
```

Treat that path as cache-owned. Copy it elsewhere if it needs to outlive cache cleaning, and avoid editing its contents in place. 

`cache_info` inventories entries and verifies their stored hashes without accessing the network: 

```julia
inventory = cache_info(; cache_dir=cache)
@assert all(entry.valid for entry in inventory.entries)
(
    entries=length(inventory.entries),
    bytes=inventory.total_size,
    all_valid=all(entry.valid for entry in inventory.entries),
)
```

```text
(entries = 25, bytes = 10071, all_valid = true)
```

Each entry identifies its kind, digest, size, path, modification time, validity, and known dataset associations. The tutorial copied extra conformance files, including malformed examples used by tests. A valid cache entry means its bytes match its filename; it does not mean every file is a decodable dataset. 

Eviction is manual. Select a dataset to remove its associated inactive artifacts: 

```julia
removed = cache_clean(; dataset="conformance:mixed_csv@1", cache_dir=cache)
@assert !isempty(removed.removed)
(bytes_removed=removed.bytes_removed, entries_removed=length(removed.removed))
```

```text
(bytes_removed = 126, entries_removed = 1)
```

Files shared with another representation refer to the same cached object, so removing one can affect later offline calls for those representations too. The `older_than` filter can restrict eviction by age, for example `cache_clean(; older_than=Day(30), cache_dir=cache)` after `using Dates`. When both filters are supplied, they intersect. Active readers and publishers are skipped. Calling `cache_clean` without filters selects the whole cache. 

<a id="recognize-an-offline-cache-miss"></a>

## Recognize an offline cache miss

The CSV artifact has now been removed. Repeating its offline fetch produces an `OfflineError` rather than silently accessing the network: 

```julia
try
    fetch_data(
        "conformance:mixed_csv@1";
        registry=example_registry,
        cache_dir=cache,
        offline=true,
    )
    error("Expected an offline cache miss")
catch exception
    @assert exception isa OfflineError
    println(nameof(typeof(exception)))
end
rm(cache; recursive=true)
```

```text
OfflineError
```

A missing registry index raises `RegistryOfflineError`. Integrity failures, unsupported decoders, and unknown datasets have distinct exception types too. Catch the types your application can handle, and let unexpected failures propagate. The [client guide](<https://github.com/jolars/datamonger/blob/main/packages/julia/README.md#error-taxonomy-and-cache-management>) summarizes the error categories. 

<a id="use-the-same-workflow-with-public-datasets"></a>

## Use the same workflow with public datasets

For a real analysis, use the bundled stable registry or another recorded strong selector. These calls require access to upstream hosts when artifacts are not cached: 

```julia
using Datamonger

registry = BUNDLED_REGISTRY
result = fetch_data("uci:iris@1"; registry, return_info=true)
iris = result.data
heart = fetch_data("libsvm:heart_scale@1"; registry)
```

Keep `result.info.dataset_id`, `result.info.registry_release`, `result.info.registry_index_sha256`, and `result.info.canonical_digest` with the analysis. Later, reuse the same dataset reference and selector; use `offline=true` when the run must rely entirely on previously cached inputs. See [Registries and reproducibility](<reproducibility>) for project selectors, selection precedence, and the limits of catalog discovery.
