JuliaArrays / AxisArrays.jl

Performant arrays where each dimension can have a named axis with values
http://JuliaArrays.github.io/AxisArrays.jl/latest/
Other
200 stars 41 forks source link

Enforce axis constraint in merge function #135

Open Balinus opened 6 years ago

Balinus commented 6 years ago

Hello!

I'm wondering if there is a way to easily keep one of the axis as a "sort" axis constraint in merge operations. Here's an example of what is the current behavior:

A = AxisArray(collect(1:11), Axis{:time}(0:0.1:1))
B = AxisArray(collect(12:22), Axis{:time}(1.1:0.1:2.1))
M = merge(B, A) # ideally, I would like to force the time axis to an ascending order

1-dimensional AxisArray{Int64,1,...} with axes:
    :time, [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0  …  0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
And data, a 22-element Array{Int64,1}:
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
  ⋮
  3
  4
  5
  6
  7
  8
  9
 10
 11

Now, trying sort(M) returns

sort(M)
1-dimensional AxisArray{Int64,1,...} with axes:
    :time, [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0  …  0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
And data, a 22-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
  ⋮
 14
 15
 16
 17
 18
 19
 20
 21
 22

Hence, the values are sorted, but not the time axis definition.

Ideally, I would like to enforce the :time axis to be the sorting constraint in merge function.

I coded a workaround in a custom merge function which consist of reverting the order of the argument (i.e. merge(B, A) -> merge(A, B)). But I suspect that it only works for 2-input call to merge without a more complex workaround.

Thanks for any help or hint on how it can be done!

Balinus commented 6 years ago

Any idea? Right now, I can do some custom checks in my package but I think this kind of behaviour should, in the end, reside upstream.

Cheers!

GordStephen commented 6 years ago

In theory, sorting should already be happening:

https://github.com/JuliaArrays/AxisArrays.jl/blob/702623f94a96f4f6cb3f5e8e8393eb72c2c05276/src/combine.jl#L42

Strange that it's not, maybe the axis isn't being recognized as a Dimensional for some reason... I would consider this a bug.