DimensionalData.jl
DimensionalData.jl copied to clipboard
Dimension mismatch: `DimSelectors` with alphabetized labels
I have been using Dimensions that use English-word labels. When using DimSelectors, the alphabetical order of the labels may change, leading to a change in the "order" of the Dimension. Some algebraic functions, like subtraction (below), then no longer work. Here's a MWE. Any advice is appreciated!
using DimensionalData
using DimensionalData: @dim
@dim Vertical "vertical"
# full data
A_locations = [:low, :high]
A = DimArray(rand(length(A_locations)),Vertical(A_locations))
# data at a subset of locations
B_locations = [:low]
B = DimArray(rand(length(B_locations)),Vertical(B_locations))
A[DimSelectors(B)] # works
B - A[DimSelectors(B)] # would be nice to compare full data to subsetted data at subset locations, but gives error
#= error:
DimensionMismatch: Lookups do not all have the same order: DimensionalData.Dimensions.Lookups.ForwardOrdered(), DimensionalData.Dimensions.Lookups.ReverseOrdered().
=#
Ok right interesting use case. We should use the order from DimSelectors rather than the original.
You can probably specifically set the order to Unordered :
Vertical(A_locations; order=DimensionalData.Unordered())
And it will work.
But, maybe Categorical lookups should be Unordered by default, and make order opt-in? Ordered things are faster so we usually go with ordered if we can, but it can be kind of confusing.
We should also ignore the order if the length is one. I'm changing how some of these correctness checks work in the next breaking release, so we can fix this at the same time.
Looks like this is no longer broken after the other changes that are coming, but by using Vertical(NoLookup()). But you probably really want it to return something with the dimension Vertical(Categorical([:low], Unordered()))
Aldo FYI you can define a DimArray like this:
A = rand(Vertical(A_locations))
Great advice, thank you. I confirm that the Vertical(Categorical([:low], Unordered())) approach works for me.
I forgot about the streamlined way to make a random DimArray. Nice!
I see Categorical lookups as mostly being used in small-ish problems where custom labels make sense. The slowdown from using an Unordered Dimension wouldn't matter much in these cases. So I see the Unordered solution as being a good one for my test case. In bigger problems, some other kind of numerical, Ordered Dimension would be used instead.
Also, I agree that the order doesn't make much sense when there's just one element.
It was pretty nifty to submit an issue at night and instantaneously have a solution in the morning -- cheers!
This works now in the latest version