Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimension mismatch: DimSelectors with alphabetized labels #798

Closed
ggebbie opened this issue Sep 7, 2024 · 4 comments
Closed

Dimension mismatch: DimSelectors with alphabetized labels #798

ggebbie opened this issue Sep 7, 2024 · 4 comments

Comments

@ggebbie
Copy link

ggebbie commented Sep 7, 2024

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().
=#
@rafaqz
Copy link
Owner

rafaqz commented Sep 7, 2024

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.

@rafaqz
Copy link
Owner

rafaqz commented Sep 7, 2024

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))

@ggebbie
Copy link
Author

ggebbie commented Sep 7, 2024

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!

@rafaqz
Copy link
Owner

rafaqz commented Oct 1, 2024

This works now in the latest version

@rafaqz rafaqz closed this as completed Oct 1, 2024
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

No branches or pull requests

2 participants