-
Notifications
You must be signed in to change notification settings - Fork 34
/
13_arraycommon.jl
2217 lines (1954 loc) · 84.5 KB
/
13_arraycommon.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module TestArrayCommon
using Test
using Missings
using Future: copy!
using CategoricalArrays, DataAPI
using CategoricalArrays: DefaultRefType, pool
using PooledArrays
using JSON3
using StructTypes
using RecipesBase
using Plots
using SentinelArrays
const ≅ = isequal
const ≇ = !isequal
@testset "mergelevels()" begin
# Test that mergelevels handles mutually compatible orderings
@test CategoricalArrays.mergelevels(true, [6, 2, 4, 8], [2, 3, 5, 4], [2, 4, 8]) ==
([6, 2, 3, 5, 4, 8], true)
@test CategoricalArrays.mergelevels(true, ["A", "B", "D"], ["C", "D", "E", "F"], ["B", "C", "E"]) ==
(["A", "B", "C", "D", "E", "F"], true)
@test CategoricalArrays.mergelevels(true, ["A", "B", "D"], ["C", "D", "E", "F"], ["A", "B", "C", "F"]) ==
(["A", "B", "C", "D", "E", "F"], true)
@test CategoricalArrays.mergelevels(true, ["B", "C", "D"], ["A", "B"]) ==
(["A", "B", "C", "D"], true)
@test CategoricalArrays.mergelevels(true, ["B", "C", "D"], []) ==
(["B", "C", "D"], true)
@test CategoricalArrays.mergelevels(true, [], ["A", "C"]) ==
(["A", "C"], true)
@test CategoricalArrays.mergelevels(true, [], []) ==
([], true)
@test CategoricalArrays.mergelevels(true, ["A", "B", "C"], [], ["A", "C", "D"]) ==
(["A", "B", "C", "D"], true)
@test CategoricalArrays.mergelevels(true, [], ["A", "B", "C"], ["A", "C", "D"]) ==
(["A", "B", "C", "D"], true)
@test CategoricalArrays.mergelevels(true, ["A", "C", "D"], ["A", "B", "C"], []) ==
(["A", "B", "C", "D"], true)
@test CategoricalArrays.mergelevels(true, [], ["A", "C", "D"], []) ==
(["A", "C", "D"], true)
@test CategoricalArrays.mergelevels(true, ["A", "C", "D"], [], []) ==
(["A", "C", "D"], true)
@test CategoricalArrays.mergelevels(true, [], [], ["A", "C", "D"]) ==
(["A", "C", "D"], true)
@test CategoricalArrays.mergelevels(true, [], [], []) ==
([], true)
@test CategoricalArrays.mergelevels(true, ["A", "C", "D"], ["A", "B", "C"], ["A", "D", "E"], ["C", "D"]) ==
(["A", "B", "C", "D", "E"], true)
# Test that mergelevels handles mutually incompatible orderings
# by giving priority to first sets of levels
@test CategoricalArrays.mergelevels(true, [6, 3, 4, 7], [2, 3, 6, 5, 4], [2, 4, 8]) ==
([2, 6, 3, 5, 4, 7, 8], false)
@test CategoricalArrays.mergelevels(true, ["A", "C", "D"], ["D", "C"], []) ==
(["A", "C", "D"], false)
@test CategoricalArrays.mergelevels(true, ["A", "D", "C"], ["A", "B", "C"], ["A", "D", "E"], ["C", "D"]) ==
(["A", "B", "D", "C", "E"], false)
# Test that mergelevels handles incomplete orderings
@test CategoricalArrays.mergelevels(true, ["B", "C", "D"], ["A", "C"]) ==
(["B", "A", "C", "D"], false)
@test CategoricalArrays.mergelevels(true, ["A", "B", "D"], ["C", "D", "E", "F"]) ==
(["A", "B", "C", "D", "E", "F"], false)
@test CategoricalArrays.mergelevels(true, ["A", "B", "E", "G"], ["C", "D", "E", "F"]) ==
(["A", "B", "C", "D", "E", "G", "F"], false)
@test CategoricalArrays.mergelevels(true, ["A", "B", "D"], ["C", "D", "E", "F"], ["A", "B", "D", "F"]) ==
(["A", "B", "C", "D", "E", "F"], false)
# Test with ordered=false (simpler, almost a subset of code paths tested above)
@test CategoricalArrays.mergelevels(false, ["A", "B", "E", "G"], ["C", "D", "E", "F"]) ==
(["A", "B", "C", "D", "E", "G", "F"], false)
end
@testset "Testing $T" for T in (Union{}, Missing)
@testset "vcat()" begin
# Test that vcat of compress arrays uses a reftype that doesn't overflow
a1 = 3:200
a2 = 300:-1:100
ca1 = CategoricalArray{Union{T, Int}}(a1)
ca2 = CategoricalArray{Union{T, Int}}(a2)
cca1 = compress(ca1)
cca2 = compress(ca2)
r = vcat(cca1, cca2)
@test r == vcat(a1, a2)
@test isa(cca1, CategoricalArray{Union{T, Int}, 1, UInt8})
@test isa(cca2, CategoricalArray{Union{T, Int}, 1, UInt8})
@test isa(r, CategoricalArray{Union{T, Int}, 1, CategoricalArrays.DefaultRefType})
@test isa(vcat(cca1, ca2), CategoricalArray{Union{T, Int}, 1, CategoricalArrays.DefaultRefType})
@test isordered(r) == false
@test levels(r) == collect(3:300)
# Test vcat of multidimensional arrays
a1 = Array{Int}(undef, 2, 3, 4, 5)
a2 = Array{Int}(undef, 3, 3, 4, 5)
a1[1:end] = (length(a1):-1:1) .+ 2
a2[1:end] = (1:length(a2)) .+ 10
ca1 = CategoricalArray{Union{T, Int}}(a1)
ca2 = CategoricalArray{Union{T, Int}}(a2)
cca1 = compress(ca1)
cca2 = compress(ca2)
r = vcat(cca1, cca2)
@test r == vcat(a1, a2)
@test isa(r, CategoricalArray{Union{T, Int}, 4, CategoricalArrays.DefaultRefType})
@test isordered(r) == false
@test levels(r) == collect(3:length(a2)+10)
# Test concatenation of mutually compatible levels
a1 = ["Young", "Middle"]
a2 = ["Middle", "Old"]
ca1 = CategoricalArray{Union{T, String}}(a1, ordered=true)
ca2 = CategoricalArray{Union{T, String}}(a2, ordered=true)
@test levels!(ca1, ["Young", "Middle"]) === ca1
@test levels!(ca2, ["Middle", "Old"]) === ca2
r = vcat(ca1, ca2)
@test r == vcat(a1, a2)
@test levels(r) == ["Young", "Middle", "Old"]
@test isordered(r) == true
# Test concatenation of conflicting ordering. This drops the ordering
a1 = ["Old", "Young", "Young"]
a2 = ["Old", "Young", "Middle", "Young"]
ca1 = CategoricalArray{Union{T, String}}(a1, ordered=true)
ca2 = CategoricalArray{Union{T, String}}(a2, ordered=true)
levels!(ca1, ["Young", "Middle", "Old"])
# ca2 has another order
r = vcat(ca1, ca2)
@test r == vcat(a1, a2)
@test levels(r) == ["Young", "Middle", "Old"]
@test isordered(r) == false
end
@testset "undef constructors preserve reftype" begin
x = CategoricalArray{Union{T, CategoricalValue{Int, UInt8}}}(undef, 3)
@test x isa CategoricalArray{Union{T, Int}, 1, UInt8}
x = CategoricalArray{Union{T, CategoricalValue{Int, UInt8}}, 1}(undef, 3)
@test x isa CategoricalArray{Union{T, Int}, 1, UInt8}
end
@testset "similar()" begin
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
y = similar(x)
@test typeof(x) === typeof(y)
@test size(y) == size(x)
if T === Missing
@test all(ismissing, y)
else
@test !any(isassigned(y, i) for i in 1:length(y))
end
y = similar(x, 3)
@test typeof(x) === typeof(y)
@test size(y) == (3,)
if T === Missing
@test all(ismissing, y)
else
@test !any(isassigned(y, i) for i in 1:length(y))
end
y = similar(x, Int)
@test isa(y, Vector{Int})
@test size(y) == size(x)
y = similar(x, Int, 3, 2)
@test isa(y, Matrix{Int})
@test size(y) == (3, 2)
x = CategoricalArray{Union{T, String}, 1, UInt8}(["Old", "Young", "Middle", "Young"])
y = similar(x, Union{T, CategoricalValue{String}})
@test typeof(x) === typeof(y)
@test size(y) == size(x)
T === Missing && @test all(ismissing, y)
y = similar(x, Union{T, CategoricalValue{String, UInt32}})
@test isa(y, CategoricalVector{Union{T, String}, UInt32})
@test size(y) == size(x)
T === Missing && @test all(ismissing, y)
y = similar(x, Union{Missing, CategoricalValue{String}}, 3, 2)
@test isa(y, CategoricalMatrix{Union{String, Missing}, UInt8})
@test size(y) == (3, 2)
@test all(ismissing, y)
y = similar(x, CategoricalValue{String})
@test isa(y, CategoricalVector{String, UInt8})
@test size(y) == size(x)
@test !any(isassigned(y, i) for i in 1:length(y))
y = similar(x, CategoricalValue{String, UInt32})
@test isa(y, CategoricalVector{String, UInt32})
@test size(y) == size(x)
@test !any(isassigned(y, i) for i in 1:length(y))
y = similar(x, CategoricalValue{Int, UInt32})
@test isa(y, CategoricalVector{Int, UInt32})
@test size(y) == size(x)
y = similar([], Union{CategoricalValue{Int}, T}, (3,))
@test isa(y, CategoricalVector{Union{Int, T}, UInt32})
@test size(y) == (3,)
y = similar([], Union{CategoricalValue{String}, T}, (3,))
@test isa(y, CategoricalVector{Union{String, T}, UInt32})
@test size(y) == (3,)
y = similar([], Union{CategoricalValue{Int, UInt8}, T}, (3,))
@test isa(y, CategoricalVector{Union{Int, T}, UInt8})
@test size(y) == (3,)
y = similar([], Union{CategoricalValue{String, UInt8}, T}, (3,))
@test isa(y, CategoricalVector{Union{String, T}, UInt8})
@test size(y) == (3,)
y = similar(Vector{Union{CategoricalValue{Int}, T}}, (3,))
@test isa(y, CategoricalVector{Union{Int, T}, UInt32})
@test size(y) == (3,)
y = similar(Vector{Union{CategoricalValue{String}, T}}, (3,))
@test isa(y, CategoricalVector{Union{String, T}, UInt32})
@test size(y) == (3,)
y = similar(Vector{Union{CategoricalValue{Int, UInt8}, T}}, (3,))
@test isa(y, CategoricalVector{Union{Int, T}, UInt8})
@test size(y) == (3,)
y = similar(Vector{Union{CategoricalValue{String, UInt8}, T}}, (3,))
@test isa(y, CategoricalVector{Union{String, T}, UInt8})
@test size(y) == (3,)
y = similar(Vector{T}, (3,))
@test isa(y, Vector{T})
@test size(y) == (3,)
y = similar(1:1, Union{CategoricalValue{String, UInt8}, T})
@test isa(y, CategoricalVector{Union{String, T}, UInt8})
@test size(y) == (1,)
y = similar(1:1, Union{CategoricalValue{String, UInt8}, T}, (3,))
@test isa(y, CategoricalVector{Union{String, T}, UInt8})
@test size(y) == (3,)
end
@testset "copy" begin
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
ordered!(x, true)
y = copy(x)
@test y == x
levels!(y, ["Z", "Middle", "Old", "Young"])
y[1] = "Z"
@test x == ["Old", "Young", "Middle", "Young"]
@test levels(x) == ["Young", "Middle", "Old"]
# Test with missing values
if T === Missing
x[3] = missing
@test copy(x) ≅ x
end
end
@testset "copy! and copyto!" for ordered in (false, true)
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
ordered!(x, ordered)
y = CategoricalArray{Union{T, String}}(["X", "Z", "Y", "X"])
for copyf! in (copy!, copyto!)
x2 = copy(x)
@test copyf!(x2, y) === x2
@test x2 == y
@test levels(x2) == ["Young", "Middle", "Old", "X", "Y", "Z"]
@test !isordered(x2)
end
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
ordered!(x, ordered)
x2 = copy(x)
y = CategoricalArray{Union{T, String}}(["X", "Z", "Y", "X"])
a = Union{String, Missing}["Z", "Y", "X", "Young"]
# Test with missing values
if T === Missing
x[3] = x2[3] = missing
y[3] = a[2] = missing
end
@test copyto!(x2, 1, y, 2) === x2
@test x2 ≅ a
@test levels(x2) == ["Young", "Middle", "Old", "X", "Y", "Z"]
@test !isordered(x2)
@testset "0-length copy!/copyto!" begin
# 0-length copy!/copyto! does nothing (including bounds checks) except setting levels
u = x[1:0]
v = y[1:0]
x2 = copy(x)
@test copyto!(x2, 1, y, 3, 0) === x2
@test levels(x2) == ["Young", "Middle", "Old", "X", "Y", "Z"]
@test !isordered(x2)
@test x2 ≅ x
x2 = copy(x)
@test copyto!(x2, 1, y, 5, 0) === x2
@test levels(x2) == ["Young", "Middle", "Old", "X", "Y", "Z"]
@test !isordered(x2)
@test x2 ≅ x
u2 = copy(u)
@test copyto!(u2, -5, v, 2, 0) === u2
@test levels(u2) == ["Young", "Middle", "Old", "X", "Y", "Z"]
@test !isordered(x2)
@test isempty(u2)
x2 = copy(x)
@test copyto!(x2, -5, v, 2, 0) === x2
@test levels(x2) == ["Young", "Middle", "Old", "X", "Y", "Z"]
@test !isordered(x2)
@test x2 ≅ x
u2 = copy(u)
@test copyto!(u2, v) === u2
@test levels(u2) == ["Young", "Middle", "Old", "X", "Y", "Z"]
@test !isordered(x2)
@test isempty(u2)
x2 = copy(x)
@test copyto!(x2, v) === x2
@test levels(x2) == ["Young", "Middle", "Old", "X", "Y", "Z"]
@test !isordered(x2)
@test x2 ≅ x
u2 = copy(u)
@test copy!(u2, v) === u2
@test levels(u2) == ["Young", "Middle", "Old", "X", "Y", "Z"]
@test !isordered(x2)
@test isempty(u2)
x2 = copy(x)
@test copy!(x2, v) === x2
@test levels(x2) == ["Young", "Middle", "Old", "X", "Y", "Z"]
@test !isordered(x2)
@test x2 ≅ x
# test with zero-levels source
x2 = copy(x)
@test copy!(x2, categorical(String[])) === x2
@test levels(x2) == ["Young", "Middle", "Old"]
@test isordered(x2) === ordered
@test x2 ≅ x
# test with zero-levels destination
for ordered2 in (true, false)
x2 = CategoricalArray{String}(undef, 2, ordered=ordered2)
@test copy!(x2, u) === x2
@test levels(x2) == ["Young", "Middle", "Old"]
@test isordered(x2) === ordered
@test length(x2) == 2
@test !any(isassigned(x2, i) for i in eachindex(x2))
end
# test with zero-levels source and destination
for ordered2 in (true, false)
x2 = CategoricalArray{String}(undef, 2, ordered=ordered2)
@test copy!(x2, categorical(String[], ordered=ordered)) === x2
@test isempty(levels(x2))
@test isordered(x2) === ordered2
@test length(x2) == 2
@test !any(isassigned(x2, i) for i in eachindex(x2))
end
end
@testset "nonzero-length copy!/copyto! into/from empty array throws bounds error" begin
u = x[1:0]
v = y[1:0]
x2 = copy(x)
@test_throws BoundsError copy!(u, x)
@test u ≅ v
@test_throws BoundsError copyto!(u, x)
@test u ≅ v
@test_throws BoundsError copyto!(u, 1, v, 1, 1)
@test u ≅ v
@test_throws BoundsError copyto!(x2, 1, v, 1, 1)
@test x2 ≅ x
end
@testset "no corruption happens in case of bounds error" begin
@test_throws BoundsError copyto!(x2, 10, y, 2)
@test x2 ≅ x
@test_throws BoundsError copyto!(x2, 1, y, 10)
@test x2 ≅ x
@test_throws BoundsError copyto!(x2, 10, y, 20)
@test x2 ≅ x
@test_throws BoundsError copyto!(x2, 10, y, 2)
@test x2 ≅ x
@test_throws BoundsError copyto!(x2, 1, y, 2, 10)
@test x2 ≅ x
@test_throws BoundsError copyto!(x2, 4, y, 1, 2)
@test x2 ≅ x
@test_throws BoundsError copyto!(x2, 1, y, 4, 2)
@test x2 ≅ x
end
end
@testset "$copyf!" for copyf! in (copy!, copyto!)
@testset "copy src not supporting missings into dest not supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector(v), reverse(v))
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
end
@testset "copy src supporting missings into dest not supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector{Union{Missing, String}}(v), reverse(v))
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
end
@testset "copy src not supporting missings into dest supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector(v), reverse(v))
dest = CategoricalVector{Union{String, Missing}}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
end
@testset "copy src supporting missings into dest supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector{Union{String, Missing}}(v), reverse(v))
dest = CategoricalVector{Union{String, Missing}}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
end
@testset "copy viewed src not supporting missings into dest not supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector(v), reverse(v))
vsrc = view(src, 1:length(src))
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, vsrc)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
end
@testset "copy viewed src supporting missings into dest not supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector{Union{String, Missing}}(v), reverse(v))
vsrc = view(src, 1:length(src))
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, vsrc)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
end
@testset "copy viewed src not supporting missings into dest supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector(v), reverse(v))
vsrc = view(src, 1:length(src))
dest = CategoricalVector{Union{String, Missing}}(undef, 3)
copyf!(dest, vsrc)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
end
@testset "copy viewed src supporting missings into dest supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector{Union{String, Missing}}(v), reverse(v))
vsrc = view(src, 1:length(src))
dest = CategoricalVector{Union{String, Missing}}(undef, 3)
copyf!(dest, vsrc)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
end
@testset "copy a viewed subset of src into dest" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector(v), reverse(v))
vsrc = view(src, 1:2)
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, vsrc)
@test dest[1:2] == src[1:2]
@test levels(dest) == levels(src)
vsrc = view(src, 1:2)
dest = CategoricalVector{String}(undef, 2)
copyf!(dest, vsrc)
@test dest == src[1:2]
@test levels(dest) == levels(src)
end
@testset "copy a src into viewed dest" begin
v = ["a", "b"]
src = levels!(CategoricalVector(v), reverse(v))
dest = CategoricalVector{String}(["e", "f", "g"])
vdest = view(dest, 1:2)
copyf!(vdest, src)
@test dest[1:2] == src[1:2]
@test levels(dest) == levels(vdest) == ["e", "f", "g", "b", "a"]
dest = CategoricalVector{String}(["e", "f"])
vdest = view(dest, 1:2)
copyf!(vdest, src)
@test vdest == src[1:2]
@test levels(dest) == levels(vdest) == ["e", "f", "b", "a"]
# Destination without any levels should be marked as ordered
src = levels!(CategoricalVector(v, ordered=true), reverse(v))
dest = CategoricalVector{Union{String,Missing}}([missing, missing])
vdest = view(dest, 1:2)
copyf!(vdest, src)
@test dest ≅ src
@test levels(vdest) == levels(dest)
@test isordered(dest)
end
@testset "copy a src into viewed dest and breaking orderedness" begin
v = ["a", "b"]
src = levels!(CategoricalVector(v), reverse(v))
dest = CategoricalVector{String}(["e", "f", "g"], ordered=true)
vdest = view(dest, 1:2)
copyf!(vdest, src)
@test dest[1:2] == ["a", "b"]
@test levels(dest) == levels(vdest) == ["e", "f", "g", "b", "a"]
@test !isordered(dest) && !isordered(vdest)
dest = CategoricalVector{String}(["e", "f"], ordered=true)
vdest = view(dest, 1:2)
copyf!(vdest, src)
@test dest == ["a", "b"]
@test levels(dest) == levels(vdest) == ["e", "f", "b", "a"]
@test !isordered(dest) && !isordered(vdest)
end
@testset "viable mixed src and dest types" begin
v = ["a", "b", "c"]
src = CategoricalVector{Union{eltype(v), Missing}}(v)
levels!(src, reverse(v))
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
vsrc = view(src, 1:length(src))
copyf!(dest, vsrc)
@test dest == vsrc
@test levels(dest) == levels(vsrc) == reverse(v)
src = CategoricalVector{AbstractString}(v)
levels!(src, reverse(v))
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
src = CategoricalVector{String}(v)
levels!(src, reverse(v))
dest = CategoricalVector{AbstractString}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
end
@testset "inviable mixed src and dest types" begin
v = ["a", "b", missing]
src = CategoricalVector(v)
dest = CategoricalVector{String}(undef, 3)
@test_throws MissingException copyf!(dest, src)
vsrc = view(src, 1:length(src))
@test_throws MissingException copyf!(dest, vsrc)
v = Integer[-1, -2, -3]
src = CategoricalVector(v)
dest = CategoricalVector{UInt}(undef, 3)
@test_throws InexactError copyf!(dest, src)
end
@testset "partial copyto! between arrays with identical levels" begin
v = ["a", "b", "c"]
src = CategoricalVector{Union{eltype(v), Missing}}(v)
levels!(src, reverse(v))
dest = CategoricalVector{Union{eltype(v), Missing}}(fill(missing, 6))
levels!(dest, levels(src))
copyto!(dest, 3, src)
@test dest ≅ [missing, missing, "a", "b", "c", missing]
@test levels(dest) == levels(src) == reverse(v)
copyto!(dest, 3, src, 2, 1)
@test dest ≅ [missing, missing, "b", "b", "c", missing]
@test levels(dest) == levels(src) == reverse(v)
end
end
@testset "assigning into array with empty levels uses orderedness of source" begin
# destination is marked as ordered when source is ordered
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
ordered!(x, true)
for copyf! in (copyto!, copy!)
y = CategoricalArray{Union{T, String}}(undef, 4)
copyf!(y, x)
@test isordered(y)
@test levels(y) == levels(x)
if T >: Missing
y = CategoricalArray{Union{T, String}}(fill(missing, 4))
copyf!(y, x)
@test isordered(y)
@test levels(y) == levels(x)
end
end
y = CategoricalArray{Union{T, String}}(undef, 4)
y[1] = x[1]
@test isordered(y)
@test levels(y) == levels(x)
if T >: Missing
y = CategoricalArray{Union{T, String}}(fill(missing, 4))
y[1] = x[1]
@test isordered(y)
@test levels(y) == levels(x)
end
# destination is marked as unordered when source is unordered
ordered!(x, false)
for copyf! in (copyto!, copy!)
y = CategoricalArray{Union{T, String}}(undef, 4)
ordered!(y, true)
copyf!(y, x)
@test !isordered(y)
@test levels(y) == levels(x)
if T >: Missing
y = CategoricalArray{Union{T, String}}(fill(missing, 4))
ordered!(y, true)
copyf!(y, x)
@test !isordered(y)
@test levels(y) == levels(x)
end
end
y = CategoricalArray{Union{T, String}}(undef, 4)
y[1] = x[1]
@test !isordered(y)
@test levels(y) == levels(x)
if T >: Missing
y = CategoricalArray{Union{T, String}}(fill(missing, 4))
y[1] = x[1]
@test !isordered(y)
@test levels(y) == levels(x)
end
end
@testset "resize!()" begin
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
@test resize!(x, 3) === x
@test x == ["Old", "Young", "Middle"]
@test resize!(x, 4) === x
if T === Missing
@test x ≅ ["Old", "Young", "Middle", missing]
else
@test x[1:3] == ["Old", "Young", "Middle"]
@test !isassigned(x, 4)
end
end
@testset "$copyf! and conflicting orders sstart=$sstart dstart=$dstart n=$n" for
(sstart, dstart, n) in ((1, 1, 4), (1, 2, 3)),
copyf! in (copy!, copyto!)
# Conflicting orders: check that the destination wins and that result is not ordered
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
ordered!(x, true)
y = CategoricalArray{Union{T, String}}(["Middle", "Middle", "Old", "Young"])
levels!(y, ["Old", "Middle", "Young"])
ordered!(x, true)
@test copyf!(x, y) === x
@test x == y
@test levels(x) == ["Young", "Middle", "Old"]
@test !isordered(x)
# Destination ordered, but not origin: check that destination wins
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
ordered!(x, true)
y = CategoricalArray{Union{T, String}}(["Middle", "Middle", "Old", "Young"])
levels!(y, ["Young", "Middle", "Old"])
@test copyf!(x, y) === x
@test x == y
@test levels(x) == ["Young", "Middle", "Old"]
@test isordered(x)
# Origin ordered, but not destination: check that destination wins
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
y = CategoricalArray{Union{T, String}}(["Middle", "Middle", "Old", "Young"])
ordered!(y, true)
levels!(y, ["Young", "Middle", "Old"])
@test copyf!(x, y) === x
@test x == y
@test levels(x) == ["Young", "Middle", "Old"]
@test !isordered(x)
# Origin ordered, destination ordered with no levels: check that result is ordered
y = CategoricalArray{Union{T, String}}(["Middle", "Middle", "Old", "Young"])
ordered!(y, true)
levels!(y, ["Young", "Middle", "Old"])
x = similar(x)
ordered!(x, true)
@test copyf!(x, y) === x
@test x == y
@test levels(x) == ["Young", "Middle", "Old"]
@test isordered(x)
# Destination ordered, but not origin, and new levels: check that result is unordered
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
ordered!(x, true)
y = CategoricalArray{Union{T, String}}(["Middle", "Middle", "Old", "Young"])
levels!(y, ["X", "Young", "Middle", "Old"])
copyf!(x, y)
@test levels(x) == ["X", "Young", "Middle", "Old"]
@test !isordered(x)
end
@testset "fill!()" begin
x = CategoricalArray{Union{String, T}}(["a", "b", "c"])
x2 = copy(x)
@test fill!(x2, "a") === x2
@test x2 == ["a", "a", "a"]
@test levels(x2) == ["a", "b", "c"]
@test fill!(x2, x[2]) == ["b", "b", "b"]
@test levels(x2) == ["a", "b", "c"]
x2 = copy(x)
@test_throws MethodError fill!(x2, 3)
@test x2 == x
if T === Missing
x2 = fill!(copy(x), missing)
@test all(ismissing, x2)
else
@test_throws MethodError fill!(x2, missing)
@test x2 == x
end
fill!(x2, "c")
@test x2 == ["c", "c", "c"]
@test levels(x2) == ["a", "b", "c"]
fill!(x2, "0")
@test x2 == ["0", "0", "0"]
@test levels(x2) == ["a", "b", "c", "0"]
y = categorical(["d", "e"])
ordered!(y, true)
levels!(y, ["0", "e", "d"])
fill!(x2, y[1])
@test x2 == ["d", "d", "d"]
@test levels(x2) == ["a", "b", "c", "0", "e", "d"]
x3 = similar(x2)
fill!(x3, y[1])
@test x3 == ["d", "d", "d"]
@test levels(x3) == ["0", "e", "d"]
@test isordered(x3)
end
@testset "overflow of reftype is detected and doesn't corrupt data and levels" begin
res = @test_throws LevelsException{Int, UInt8} CategoricalArray{Union{T, Int}, 1, UInt8}(256:-1:1)
@test res.value.levels == [1]
@test sprint(showerror, res.value) == "cannot store level(s) 1 since reference type UInt8 can only hold 255 levels. Use the decompress function to make room for more levels."
x = CategoricalArray{Union{T, Int}, 1, UInt8}(254:-1:1)
x[1] = 1000
res = @test_throws LevelsException{Int, UInt8} x[1] = 1001
@test res.value.levels == [1001]
@test sprint(showerror, res.value) == "cannot store level(s) 1001 since reference type UInt8 can only hold 255 levels. Use the decompress function to make room for more levels."
@test x == vcat(1000, 253:-1:1)
@test levels(x) == vcat(1:254, 1000)
x = CategoricalArray{Union{T, Int}, 1, UInt8}(1:254)
res = @test_throws LevelsException{Int, UInt8} x[1:2] = 1000:1001
@test res.value.levels == [1001]
@test sprint(showerror, res.value) == "cannot store level(s) 1001 since reference type UInt8 can only hold 255 levels. Use the decompress function to make room for more levels."
@test x == vcat(1000, 2:254)
@test levels(x) == vcat(1:254, 1000)
x = CategoricalArray{Union{T, Int}, 1, UInt8}([1, 3, 256])
res = @test_throws LevelsException{Int, UInt8} levels!(x, collect(1:256))
@test res.value.levels == [256]
@test sprint(showerror, res.value) == "cannot store level(s) 256 since reference type UInt8 can only hold 255 levels. Use the decompress function to make room for more levels."
x = CategoricalArray{Union{T, Int}}(30:2:131115)
res = @test_throws LevelsException{Int, UInt16} CategoricalVector{Int, UInt16}(x)
@test res.value.levels == collect(131100:2:131114)
@test sprint(showerror, res.value) == "cannot store level(s) 131100, 131102, 131104, 131106, 131108, 131110, 131112 and 131114 since reference type UInt16 can only hold 65535 levels. Use the decompress function to make room for more levels."
x = CategoricalArray{Union{T, String}, 1, UInt8}(string.(Char.(65:318)))
res = @test_throws LevelsException{String, UInt8} levels!(x, vcat(levels(x), "az", "bz", "cz"))
@test res.value.levels == ["bz", "cz"]
@test sprint(showerror, res.value) == "cannot store level(s) \"bz\" and \"cz\" since reference type UInt8 can only hold 255 levels. Use the decompress function to make room for more levels."
@test x == string.(Char.(65:318))
lev = copy(levels(x))
levels!(x, vcat(lev, "az"))
@test levels(x) == vcat(lev, "az")
end
@testset "compress()/decompress()" begin
x = compress(CategoricalArray{Union{T, Int}}([1, 3, 736251]))
ux = decompress(x)
@test x == ux
@test isa(x, CategoricalArray{Union{T, Int}, 1, UInt8})
@test isa(ux, CategoricalArray{Union{T, Int}, 1, CategoricalArrays.DefaultRefType})
end
@testset "reshape()" begin
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
ordered!(x, true)
y = reshape(x, 1, 4)
@test isa(y, CategoricalArray{Union{T, String}, 2, CategoricalArrays.DefaultRefType})
@test y == ["Old" "Young" "Middle" "Young"]
@test levels(x) == levels(y)
@test isordered(x)
y = reshape(x, 2, 2)
@test isa(y, CategoricalArray{Union{T, String}, 2, CategoricalArrays.DefaultRefType})
@test y == ["Old" "Middle"; "Young" "Young"]
@test levels(x) == levels(y)
@test isordered(x)
# Test with missing values
if T === Missing
x[3] = missing
y = reshape(x, 1, 4)
@test isa(y, CategoricalArray{Union{T, String}, 2, CategoricalArrays.DefaultRefType})
@test y ≅ ["Old" "Young" missing "Young"]
@test levels(x) == levels(y)
@test isordered(x)
end
end
@testset "sort" begin
x = CategoricalArray{Union{T, String}}(["Old", "Young", "Middle", "Young"])
levels!(x, ["Young", "Middle", "Old"])
@test sort(x) == ["Young", "Young", "Middle", "Old"]
ordered!(x, true)
@test sort(x) == ["Young", "Young", "Middle", "Old"]
@test sort!(x) === x
@test x == ["Young", "Young", "Middle", "Old"]
if T !== Missing
v = rand(["a", "b", "c", "d"], 1000)
else
v = rand(["a", "b", "c", "d", missing], 1000)
end
for rev in (true, false)
cv = categorical(v)
sv = sort(v, rev=rev)
@test sort(cv, rev=rev) ≅ sv
@test sort!(cv, rev=rev) === cv ≅ sv
cv = categorical(v)
levels!(cv, ["b", "a", "c", "d"])
@test sort(cv, rev=rev) ≅
["b", "a", "c", "d", missing][sort([5; 1:4][cv.refs .+ 1], rev=rev)]
levels!(cv, ["x", "z", "b", "a", "y", "c", "d", "0"])
@test sort(cv, rev=rev) ≅
["x", "z", "b", "a", "y", "c", "d", "0", missing][sort([9; 1:8][cv.refs .+ 1], rev=rev)]
cv = categorical(v)
@test sort(cv, rev=rev, lt=(x, y) -> isless(y, x)) ≅
sort(cv, rev=!rev) ≅
sort(cv, order=Base.Sort.ord((x, y) -> isless(y, x), identity, rev))
# Function changing order
byf1 = x -> ismissing(x) ? "c" : (x == "a" ? "z" : "b")
@test sort(cv, rev=rev, by=byf1) ≅ sort(cv, rev=rev, by=byf1)
# Check that by function is not called on unused levels/missing
byf2 = x -> (@assert get(x) != "b"; x)
replace!(cv, missing=>"a", "b"=>"a")
@test sort(cv, rev=rev, by=byf2) ≅ sort(cv, rev=rev, by=byf2)
end
end
end
@testset "constructors and convert() between categorical arrays, ordered_orig=$ordered_orig, ordered=$ordered, R=$R, T=$T, CVM2=$CVM2, N=$N" for ordered_orig in (true, false),
ordered in (true, false),
R in (DefaultRefType, UInt8, UInt, Int8, Int),
T in (String, Union{String, Missing}),
(CVM2, N) in ((CategoricalVector, 1), (CategoricalMatrix, 2))
# check that ctors/converters preserve levels and ordering by default even across types,
# do not modify original array when changing ordering, and make copies
# only when necessary (though for construcors the pool is always copied because of ordered)
if CVM2 <: AbstractVector
x = CategoricalArray{T, N, R}(["A", "B"], ordered=ordered_orig)
else
x = CategoricalArray{T, N, R}(["A" "B"], ordered=ordered_orig)
end
# Do not use lexicographic order in order to catch bugs about order preservation
levels!(x, ["B", "A"])
for y in (CategoricalArray(x),
CategoricalArray{T}(x),
CategoricalArray{T, N}(x),
CategoricalArray{T, N, R}(x),
CategoricalArray{T, N, DefaultRefType}(x),
CategoricalArray{T, N, UInt8}(x),
CVM2(x),
CVM2{T}(x),
CVM2{T, R}(x),
CVM2{T, DefaultRefType}(x),
CVM2{T, UInt8}(x))
@test isa(y, CVM2{T})
@test isordered(y) === isordered(x)
@test isordered(x) === ordered_orig
@test y.refs == x.refs
@test levels(y) == levels(x)
@test y.refs !== x.refs
@test y.pool !== x.pool
end
for y in (@inferred(categorical(x)),
categorical(x, compress=false),
categorical(x, compress=true))
@test isa(y, CategoricalArray{T, N})
@test isordered(y) === isordered(x)
@test isordered(x) === ordered_orig
@test y.refs == x.refs
@test levels(y) == levels(x)
@test y.refs !== x.refs
@test y.pool !== x.pool
end
for y in (CategoricalArray(x, ordered=ordered),
CategoricalArray{T}(x, ordered=ordered),
CategoricalArray{T, N}(x, ordered=ordered),
CategoricalArray{T, N, R}(x, ordered=ordered),
CategoricalArray{T, N, DefaultRefType}(x, ordered=ordered),
CategoricalArray{T, N, UInt8}(x, ordered=ordered),
CVM2(x, ordered=ordered),
CVM2{T}(x, ordered=ordered),
CVM2{T, R}(x, ordered=ordered),
CVM2{T, DefaultRefType}(x, ordered=ordered),
CVM2{T, UInt8}(x, ordered=ordered))
@test isa(y, CVM2{T})
@test isordered(y) === ordered
@test isordered(x) === ordered_orig
@test y.refs == x.refs
@test levels(y) == levels(x)
@test y.refs !== x.refs
@test y.pool !== x.pool
end
for y in (@inferred(categorical(x, ordered=ordered)),
categorical(x, compress=false, ordered=ordered),
categorical(x, compress=true, ordered=ordered))
@test isa(y, CategoricalArray{T, N})
@test isordered(y) === ordered
@test isordered(x) === ordered_orig
@test y.refs == x.refs
@test levels(y) == levels(x)
@test y.refs !== x.refs
@test y.pool !== x.pool
end
for y in (convert(CategoricalArray, x),
convert(CategoricalArray{T}, x),
convert(CategoricalArray{T, N}, x),
convert(CategoricalArray{T, N, R}, x),
convert(CategoricalArray{T, N, DefaultRefType}, x),
convert(CategoricalArray{T, N, UInt8}, x))
@test isa(y, CVM2{T})
@test isordered(y) === isordered(x)
@test isordered(x) === ordered_orig
@test y.refs == x.refs
@test levels(y) == levels(x)
@test (y.refs === x.refs) == (eltype(x.refs) === eltype(y.refs))
@test (y.pool === x.pool) == (eltype(x.refs) === eltype(y.refs))
end
end
@testset "levels argument to constructors" begin
for T in (String, Union{String, Missing}),
ord in (false, true),
levs in (nothing, [], ["a"], ["b", "c", "a"])
for (U, x) in ((String, CategoricalArray(undef, 2, levels=levs, ordered=ord)),
(T, CategoricalArray{T}(undef, 2, levels=levs, ordered=ord)),
(T, CategoricalArray{T, 1}(undef, 2, levels=levs, ordered=ord)),