Skip to content

Carry dimension types through the axis interface: dimtype and a 6-arg yaxcreate - #71

Draft
bjarthur wants to merge 1 commit into
JuliaDataCubes:masterfrom
bjarthur:bja/dimtype
Draft

bjarthur wants to merge 1 commit into
JuliaDataCubes:masterfrom
bjarthur:bja/dimtype

Conversation

@bjarthur

@bjarthur bjarthur commented Oct 6, 2026

Copy link
Copy Markdown

This is the alternative to JuliaDataCubes/YAXArrays.jl#626 described in this comment there, implemented so that the two approaches to JuliaDataCubes/YAXArrays.jl#362 can be compared as code. One of the two should be taken, not both.

Problem

yaxconvert reduces a source array to dimension names and values and hands them to yaxcreate(T, data, dnames, dvals, attrs). A target that models dimensions as types, DimensionalData, has nothing to go on but the name, so it rebuilds every axis as Dim{name}(values): a DimArray with a user's @dim D1 converts to one with Dim{:D1}, and X, Ti become Dim{:X}, Dim{:Ti}.

Change

  • A new optional interface function dimtype(x, i): the dimension type of axis i, or nothing when the source only has a name (the fallback, so nothing changes for existing implementers). The DimensionalData extension defines it as DimensionalData.basetypeof(dims(x)[i]), which gives D1 for a D1, X for an X, Dim{:lon} for a Dim{:lon}.
  • yaxconvert now calls a six-argument yaxcreate(T, data, dnames, dtypes, dvals, attrs). Its default forwards to the five-argument method, so targets that do not model dimension types (AxisArrays, NamedDims, KeyedArray, NamedTuple) are untouched. The DimensionalData extension implements it as dtypes[i] === nothing ? Dim{dnames[i]}(dvals[i]) : dtypes[i](dvals[i]).
  • dimtype is exported alongside dimname and dimvals.

The YAXArrays side (defining dimtype(::YAXArray, i) and the six-argument yaxcreate(::Type{YAXArray}, ...)) is in JuliaDataCubes/YAXArrays.jl#TBD, which needs this released first.

Compared with the direct methods in YAXArrays#626

That PR adds yaxconvert(::Type{YAXArray}, ::AbstractDimArray) and yaxconvert(::Type{<:DimArray}, ::YAXArray) passing the Dimension objects through, in YAXArrays alone. This one extends the protocol so any source that can supply dimension types has them preserved through the shared pipeline. Trade-offs: the direct methods also keep lookups (Sampled vs Categorical, order, span) and metadata exactly, which this still rebuilds from values; this one touches the public interface and needs a release before YAXArrays can use it, but has no special case for a pair of types. Only DimensionalData-based sources carry dimension types today, so the two are equivalent in what they fix.

Metadata normalisation (NoMetadata to a Dict) is a separate defect handled by #70 either way.

Tests

The DimensionalData test item checks dimtype on a plain source (nothing) and on a DimArray (D1, X), that a DimArray round trip through yaxconvert keeps typeof(dims(...)), and that the plain-array conversion still produces Dim{:x} axes.

🤖 Generated with Claude Code

… yaxcreate

yaxconvert reduces a source array to dimension names and values, so a target
that models dimensions as types, DimensionalData, has to rebuild every axis as
Dim{name}(values): a DimArray with a user's `@dim D1` converts to one with
Dim{:D1} (JuliaDataCubes/YAXArrays.jl#362).

Add an optional interface function `dimtype(x, i)`, the dimension type of axis
i or `nothing` when the source has only a name (the fallback), and a
six-argument `yaxcreate(T, data, dnames, dtypes, dvals, attributes)` that
yaxconvert now calls; its default forwards to the five-argument method, so
targets that do not model dimension types are unchanged. The DimensionalData
extension defines dimtype(::DimArray, i) as basetypeof(dims(x)[i]) and builds
axes as dtypes[i](dvals[i]) when a type is given.

This is the interface-level alternative to the direct DimArray <-> YAXArray
methods proposed in JuliaDataCubes/YAXArrays.jl#626.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant