davidavdav / NamedArrays.jl

Julia type that implements a drop-in replacement of Array with named dimensions
Other
120 stars 20 forks source link

getindex should return a NamedArray (even when using n[:]) #57

Closed s-celles closed 6 years ago

s-celles commented 6 years ago

Hello,

I noticed that

In[3]: n = NamedArray(rand(2,4,3))
Out[3]:
2×4×3 Named Array{Float64,3}

[:, :, C=1] =
A ╲ B │         1          2          3          4
──────┼───────────────────────────────────────────
1     │  0.746684   0.352576   0.859066  0.0463972
2     │  0.500173    0.32667   0.146571   0.796928

[:, :, C=2] =
A ╲ B │        1         2         3         4
──────┼───────────────────────────────────────
1     │ 0.306856  0.242292  0.630703  0.548638
2     │ 0.499724  0.184323  0.407832  0.684586

[:, :, C=3] =
A ╲ B │         1          2          3          4
──────┼───────────────────────────────────────────
1     │  0.841066  0.0542213   0.715639  0.0993138
2     │  0.312623   0.492007     0.5172   0.410809

In[4]: n[2,:,:]
Out[4]:
4×3 Named Array{Float64,2}
B ╲ C │        1         2         3
──────┼─────────────────────────────
1     │ 0.500173  0.499724  0.312623
2     │  0.32667  0.184323  0.492007
3     │ 0.146571  0.407832    0.5172
4     │ 0.796928  0.684586  0.410809

but

In[5]: n[:]
Out[5]:
24-element Array{Float64,1}:
 0.746684 
 0.500173 
 0.352576 
 0.32667  
 0.859066 
 0.146571 
 0.0463972
 0.796928 
 0.306856 
 0.499724 
 0.242292 
 0.184323 
 0.630703 
 0.407832 
 0.548638 
 0.684586 
 0.841066 
 0.312623 
 0.0542213
 0.492007 
 0.715639 
 0.5172   
 0.0993138
 0.410809 

I was expecting a NamedArray to be returned

Kind regards

s-celles commented 6 years ago

Flattening a NamedArray could help to convert it to other kind of datastructure (DataFrame...) see https://github.com/davidanthoff/IterableTables.jl/issues/65#issuecomment-345538256

nalimilan commented 6 years ago

That's consistent with Array:

julia> x = [1 2; 3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

julia> x[:]
4-element Array{Int64,1}:
 1
 3
 2
 4
davidavdav commented 6 years ago

My question would be: what would the names be in such case?

s-celles commented 6 years ago

The names (or value) of indexes.

davidavdav commented 6 years ago

...which are, for your example?

s-celles commented 6 years ago

Something like

24-element Named Array{Float64,1}
A  │ B │ C │ 
───┼───┼───┼──────────
1  │ 1 │ 1 │ ...
...
davidavdav commented 6 years ago

You have to be more explicit. What is the result you would have wanted?

s-celles commented 6 years ago

A, B, C columns should looks like

using IterTools
collect(product(1:2,1:4,1:3))
Out[4]:
24-element Array{Tuple{Int64,Int64,Int64},1}:
 (1, 1, 1)
 (2, 1, 1)
 (1, 2, 1)
 (2, 2, 1)
 (1, 3, 1)
 (2, 3, 1)
 (1, 4, 1)
 (2, 4, 1)
 (1, 1, 2)
 (2, 1, 2)
 (1, 2, 2)
 (2, 2, 2)
 (1, 3, 2)
 (2, 3, 2)
 (1, 4, 2)
 (2, 4, 2)
 (1, 1, 3)
 (2, 1, 3)
 (1, 2, 3)
 (2, 2, 3)
 (1, 3, 3)
 (2, 3, 3)
 (1, 4, 3)
 (2, 4, 3)

and forth column (because we had a 3 dimensions NamedArray) should contains values

nalimilan commented 6 years ago

That kind of makes sense. But note that there's no type-stability issue here stricto sensu.

s-celles commented 6 years ago

ok so I'm changing title

davidavdav commented 6 years ago

This would also apply to any subset-of-dimensions-preserving reshaping then, I suppose.

s-celles commented 6 years ago

yes but you should noticed that n[2, :] raises

WARNING: Partial linear indexing is deprecated. Use `reshape(A, Val{2})` to make the dimensionality of the array match the number of indices.
s-celles commented 6 years ago

Thanks @davidavdav

davidavdav commented 6 years ago

Ah---I could have used product() from IterTools---I missed that, couldn't find product. collect(product(names(n)...)).

s-celles commented 6 years ago

Sorry I edited my post