NamedArrays.jl icon indicating copy to clipboard operation
NamedArrays.jl copied to clipboard

Indexing with Not(i::AbstractVector{Bool}) gives the incorrect result

Open tomhaber opened this issue 4 years ago • 2 comments

The indexing code with Not(bools) treats the booleans as indices 0 and 1 and thus captures all indices except 1.

For example:

julia> X = NamedArray(rand(10,5))
10×5 Named Array{Float64,2}
A ╲ B │         1          2          3          4          5
──────┼──────────────────────────────────────────────────────
1     │  0.818328   0.840304   0.473106   0.283691   0.245174
2     │  0.915175    0.95662   0.347404   0.113388   0.463306
3     │  0.749376    0.21087   0.634752    0.12749   0.548102
4     │   0.21976   0.225479   0.092484  0.0164488   0.990245
5     │  0.918221   0.106789   0.540831  0.0722173   0.779579
6     │  0.196055   0.692281   0.895311   0.348337   0.242474
7     │  0.551477   0.834262   0.945084   0.214824   0.522671
8     │ 0.0311555   0.934718   0.100138   0.720378   0.886131
9     │  0.997415  0.0865824  0.0703392   0.260739   0.518249
10    │  0.990844    0.39621   0.509232   0.829048   0.397102

julia> X[:, Not([true, false, true, false, true])]
10×4 Named Array{Float64,2}
A ╲ B │         2          3          4          5
──────┼───────────────────────────────────────────
1     │  0.840304   0.473106   0.283691   0.245174
2     │   0.95662   0.347404   0.113388   0.463306
3     │   0.21087   0.634752    0.12749   0.548102
4     │  0.225479   0.092484  0.0164488   0.990245
5     │  0.106789   0.540831  0.0722173   0.779579
6     │  0.692281   0.895311   0.348337   0.242474
7     │  0.834262   0.945084   0.214824   0.522671
8     │  0.934718   0.100138   0.720378   0.886131
9     │ 0.0865824  0.0703392   0.260739   0.518249
10    │   0.39621   0.509232   0.829048   0.397102

tomhaber avatar Nov 06 '20 16:11 tomhaber

Hello,

I didn't make the package that provides Not (I think, I used to have one but we moved to a common Not), but I don't think this is the intended usage of Not. For your case, use

X[:, .![true, false, true, false, true]]

davidavdav avatar Nov 07 '20 15:11 davidavdav

The InvertedIndices package actually does support the use of Not in this way. There is even special code to deal with this case specifically.

For example: same thing without the NamedArray

using InvertedIndices
julia> X = rand(10,5)
10×5 Array{Float64,2}:
 0.290501   0.0675099  0.0576696  0.935896   0.936226
 0.537722   0.622467   0.956746   0.483789   0.886105
 0.352586   0.841013   0.741045   0.625766   0.635618
 0.123232   0.0590852  0.449366   0.579803   0.61804
 0.0465393  0.34072    0.488103   0.284071   0.812611
 0.109587   0.674603   0.404623   0.111767   0.417153
 0.9696     0.189503   0.803316   0.418196   0.560244
 0.37363    0.845999   0.904044   0.361739   0.329037
 0.940953   0.865926   0.0521284  0.0375961  0.694446
 0.538775   0.399408   0.415636   0.509889   0.232651

julia> X[:, Not([true, false, true, false, true])]
10×2 Array{Float64,2}:
 0.0675099  0.935896
 0.622467   0.483789
 0.841013   0.625766
 0.0590852  0.579803
 0.34072    0.284071
 0.674603   0.111767
 0.189503   0.418196
 0.845999   0.361739
 0.865926   0.0375961
 0.399408   0.509889

The reason I would like to have the same behaviour, is to allow the user to provide an array of names, indices or booleans without having to special case the code based on the type of array passed.

tomhaber avatar Nov 07 '20 15:11 tomhaber