AxisArrayConversion.jl allows converting between AxisArrays.jl like packages.
- AxisArrays.AxisArray
- AxisKeys.KeyedArray
- DimensionalData.DimArray
- AxisArrayConversion.SimpleAxisArray
- Base.NamedTuple
Conversions between two array types is provided by using the path
MyArray -> NamedTuple -> OtherArray
This has multiple advantages:
- For
N
array types, only2N
methods must be implemented. - Conversion can be implemented between packages that don't know about each other.
In order to support MyArray
, the following must be implemented:
function AxisArrayConversion.namedtuple(arr::MyArray)
...
return (axes=..., values=...)
end
function AxisArrayConversion.from_namedtuple(::Type{MyArray}, nt::NamedTuple)
... = nt.axes
... = nt.values
return MyArray(...)
end
And now any fancy conversion should work
using MyArrays, OtherArrays
using AxisArrayConversion: to
using Test
ma = MyArrays(...)
oa = to(OtherArray, ma)
@test oa isa OtherArrays.OtherArray
@test ma == to(MyArray, oa)