Skip to content

Commit

Permalink
added AbstractArrayInterface for the CubeAxis (#145)
Browse files Browse the repository at this point in the history
* added AbstractArrayInterface for the CubeAxis

* changed the setindex method to work only on CubeAxis

* changed tests to work with new behaviour
  • Loading branch information
Sonicious authored Jul 5, 2022
1 parent 1195ceb commit 923e04a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
28 changes: 19 additions & 9 deletions src/Cubes/Axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@ export CubeAxis, RangeAxis, CategoricalAxis, getAxis
abstract type CubeAxis{T,S}
Supertype of all axes. Every `CubeAxis` is an 1D Cube itself and can be passed
to mapCube operations.
to mapCube operations. In detail CubeAxis is an `AbstractArray{Int, 1}`
"""
abstract type CubeAxis{T,S} end

Base.size(x::CubeAxis) = (length(x.values),)
Base.size(x::CubeAxis, i) =
i == 1 ? length(x.values) : error("Axis has only a single dimension")
Base.ndims(x::CubeAxis) = 1
Base.hash(ax::CubeAxis{<:Any,S}, h::UInt) where {S} = hash(S, hash(ax.values, h))
abstract type CubeAxis{T,S} <: AbstractArray{T, 1} end

"""
struct CategoricalAxis{T,S,RT}
Expand Down Expand Up @@ -132,7 +126,23 @@ function getAxis(desc, axlist::VecOrTuple{CubeAxis})
end
end

#Implement yaxarray interface
# Implement interfaces

# Basics: AbstractArray and more
Base.size(x::CubeAxis) = (length(x.values),)
Base.size(x::CubeAxis, i) =
i == 1 ? length(x.values) : error("Axis has only a single dimension")
Base.ndims(x::CubeAxis) = 1
Base.hash(ax::CubeAxis{<:Any,S}, h::UInt) where {S} = hash(S, hash(ax.values, h))
Base.IndexStyle(x::CubeAxis) = IndexLinear()
function Base.getindex(x::CubeAxis, i::Int)
return x.values[i]
end
function Base.setindex!(x::CategoricalAxis, value, i::Int)
x.values[i] = value
end

# YAXArrayBase
YAXArrayBase.dimname(x::CubeAxis, _) = axname(x)
YAXArrayBase.dimvals(x::CubeAxis, _) = x.values
YAXArrayBase.iscontdim(::RangeAxis, _) = true
Expand Down
30 changes: 18 additions & 12 deletions test/Cubes/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
RangeAxis,
"IrregularTimeAxis",
[DateTime("2016-10-03T10:12:28"), DateTime("2016-10-15T10:12:28"),
DateTime("2016-10-27T10:12:28"), DateTime("2016-11-08T10:12:28"),
DateTime("2016-11-20T10:12:28"), DateTime("2016-12-02T10:12:28"),
DateTime("2016-12-14T10:12:27"), DateTime("2016-12-26T10:12:27"),
DateTime("2017-01-07T10:12:25"), DateTime("2017-01-19T10:12:25"),
DateTime("2017-01-31T10:12:25"), DateTime("2017-02-24T10:12:24")],
DateTime("2016-10-27T10:12:28"), DateTime("2016-11-08T10:12:28"),
DateTime("2016-11-20T10:12:28"), DateTime("2016-12-02T10:12:28"),
DateTime("2016-12-14T10:12:27"), DateTime("2016-12-26T10:12:27"),
DateTime("2017-01-07T10:12:25"), DateTime("2017-01-19T10:12:25"),
DateTime("2017-01-31T10:12:25"), DateTime("2017-02-24T10:12:24")],
Day(1)
),
(RangeAxis, "PureArray", [0.0, 0.2, 0.4, 0.6], 0.2),
(CategoricalAxis, "CatAxis", ["One", "Two", "Three", "Four", "Five",
"Six", "Seven", "Eight", "Nine", "Ten", "Eleven"], nothing),
]
"Six", "Seven", "Eight", "Nine", "Ten", "Eleven"], nothing),
]

for (axt, axn, axv, axstep) in axestotest
ax = axt(axn, axv)
Expand All @@ -62,7 +62,7 @@
if ax isa CategoricalAxis
for (i, v) in enumerate(axv)
@test axVal2Index(ax, v) == i
@test axVal2Index(ax, v[1:2], fuzzy = true) == i
@test axVal2Index(ax, v[1:2], fuzzy=true) == i
end
@test iscontdim(ax, 1) == false
else
Expand All @@ -77,6 +77,11 @@
ax4 = renameaxis(ax, :Test)
@test axname(ax3) == "Test"
@test axsym(ax3) == :Test

# test getindex method
for i in eachindex(axv)
@test ax[i] == axv[i]
end
end
axlist = map(i -> i[1](i[2], i[3]), axestotest)
@test findAxis("Int", axlist) == 2
Expand All @@ -89,12 +94,13 @@
multiplevalax = CategoricalAxis("MultVal", ["One", "Two", "One"])
@test_throws ErrorException axVal2Index(multiplevalax, "one", fuzzy=true)
@testset "Hashtests" begin
catax = CategoricalAxis("Catax", [1,2])
catay = CategoricalAxis("Catay", [1,2])
catax2 = CategoricalAxis("Catax", [2,2])
cataxfloat = CategoricalAxis("Catax", [1.0,2.0])
catax = CategoricalAxis("Catax", [1, 2])
catay = CategoricalAxis("Catay", [1, 2])
catax2 = CategoricalAxis("Catax", [2, 2])
cataxfloat = CategoricalAxis("Catax", [1.0, 2.0])
@test hash(catax) == hash(cataxfloat)
@test hash(catax) != hash(catay)
@test hash(catax) != hash(catax2)
end

end
2 changes: 1 addition & 1 deletion test/Cubes/transformedcubes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
props = [Dict("att$i" => i) for i = 1:3]
singlecubes = [YAXArray(axlist, data[i], props[i]) for i = 1:3]
newcube = concatenatecubes(singlecubes, CategoricalAxis("ZVals", ["A", "B", "C"]))
@test caxes(newcube) == [axlist; CategoricalAxis("ZVals", ["A", "B", "C"])]
@test caxes(newcube) == [RangeAxis("XVals", 1.0:4.0), CategoricalAxis("YVals", [1, 2, 3, 4, 5]), CategoricalAxis("ZVals", ["A", "B", "C"])]
@test ndims(newcube) == 3
@test size(newcube) == (4, 5, 3)
@test newcube[:, :, :] == cat(data..., dims = 3)
Expand Down

0 comments on commit 923e04a

Please sign in to comment.