@@ -66,43 +66,6 @@ def test_pca_epsilon(self):
66
66
self .assertTrue (np .all (np .isfinite (y )))
67
67
68
68
69
- class TestRevSwigPtr (unittest .TestCase ):
70
-
71
- def test_rev_swig_ptr (self ):
72
-
73
- index = faiss .IndexFlatL2 (4 )
74
- xb0 = np .vstack ([
75
- i * 10 + np .array ([1 , 2 , 3 , 4 ], dtype = 'float32' )
76
- for i in range (5 )])
77
- index .add (xb0 )
78
- xb = faiss .rev_swig_ptr (index .get_xb (), 4 * 5 ).reshape (5 , 4 )
79
- self .assertEqual (np .abs (xb0 - xb ).sum (), 0 )
80
-
81
-
82
- class TestException (unittest .TestCase ):
83
-
84
- def test_exception (self ):
85
-
86
- index = faiss .IndexFlatL2 (10 )
87
-
88
- a = np .zeros ((5 , 10 ), dtype = 'float32' )
89
- b = np .zeros (5 , dtype = 'int64' )
90
-
91
- # an unsupported operation for IndexFlat
92
- self .assertRaises (
93
- RuntimeError ,
94
- index .add_with_ids , a , b
95
- )
96
- # assert 'add_with_ids not implemented' in str(e)
97
-
98
- def test_exception_2 (self ):
99
- self .assertRaises (
100
- RuntimeError ,
101
- faiss .index_factory , 12 , 'IVF256,Flat,PQ8'
102
- )
103
- # assert 'could not parse' in str(e)
104
-
105
-
106
69
class TestMapLong2Long (unittest .TestCase ):
107
70
108
71
def test_maplong2long (self ):
@@ -380,7 +343,6 @@ def test_rand_vector(self):
380
343
self .assertLess (ninter , 460 )
381
344
382
345
383
-
384
346
class TestPairwiseDis (unittest .TestCase ):
385
347
386
348
def test_L2 (self ):
@@ -418,142 +380,6 @@ def test_IP(self):
418
380
dis [i ], np .dot (x [ix [i ]], y [iy [i ]]))
419
381
420
382
421
- class TestSWIGWrap (unittest .TestCase ):
422
- """ various regressions with the SWIG wrapper """
423
-
424
- def test_size_t_ptr (self ):
425
- # issue 1064
426
- index = faiss .IndexHNSWFlat (10 , 32 )
427
-
428
- hnsw = index .hnsw
429
- index .add (np .random .rand (100 , 10 ).astype ('float32' ))
430
- be = np .empty (2 , 'uint64' )
431
- hnsw .neighbor_range (23 , 0 , faiss .swig_ptr (be ), faiss .swig_ptr (be [1 :]))
432
-
433
- def test_id_map_at (self ):
434
- # issue 1020
435
- n_features = 100
436
- feature_dims = 10
437
-
438
- features = np .random .random ((n_features , feature_dims )).astype (np .float32 )
439
- idx = np .arange (n_features ).astype (np .int64 )
440
-
441
- index = faiss .IndexFlatL2 (feature_dims )
442
- index = faiss .IndexIDMap2 (index )
443
- index .add_with_ids (features , idx )
444
-
445
- [index .id_map .at (int (i )) for i in range (index .ntotal )]
446
-
447
- def test_downcast_Refine (self ):
448
-
449
- index = faiss .IndexRefineFlat (
450
- faiss .IndexScalarQuantizer (10 , faiss .ScalarQuantizer .QT_8bit )
451
- )
452
-
453
- # serialize and deserialize
454
- index2 = faiss .deserialize_index (
455
- faiss .serialize_index (index )
456
- )
457
-
458
- assert isinstance (index2 , faiss .IndexRefineFlat )
459
-
460
- def do_test_array_type (self , dtype ):
461
- """ tests swig_ptr and rev_swig_ptr for this type of array """
462
- a = np .arange (12 ).astype (dtype )
463
- ptr = faiss .swig_ptr (a )
464
- a2 = faiss .rev_swig_ptr (ptr , 12 )
465
- np .testing .assert_array_equal (a , a2 )
466
-
467
- def test_all_array_types (self ):
468
- self .do_test_array_type ('float32' )
469
- self .do_test_array_type ('float64' )
470
- self .do_test_array_type ('int8' )
471
- self .do_test_array_type ('uint8' )
472
- self .do_test_array_type ('int16' )
473
- self .do_test_array_type ('uint16' )
474
- self .do_test_array_type ('int32' )
475
- self .do_test_array_type ('uint32' )
476
- self .do_test_array_type ('int64' )
477
- self .do_test_array_type ('uint64' )
478
-
479
- def test_int64 (self ):
480
- # see https://github.com/facebookresearch/faiss/issues/1529
481
- v = faiss .Int64Vector ()
482
-
483
- for i in range (10 ):
484
- v .push_back (i )
485
- a = faiss .vector_to_array (v )
486
- assert a .dtype == 'int64'
487
- np .testing .assert_array_equal (a , np .arange (10 , dtype = 'int64' ))
488
-
489
- # check if it works in an IDMap
490
- idx = faiss .IndexIDMap (faiss .IndexFlatL2 (32 ))
491
- idx .add_with_ids (
492
- np .random .rand (10 , 32 ).astype ('float32' ),
493
- np .random .randint (1000 , size = 10 , dtype = 'int64' )
494
- )
495
- faiss .vector_to_array (idx .id_map )
496
-
497
-
498
- class TestNNDescentKNNG (unittest .TestCase ):
499
-
500
- def test_knng_L2 (self ):
501
- self .subtest (32 , 10 , faiss .METRIC_L2 )
502
-
503
- def test_knng_IP (self ):
504
- self .subtest (32 , 10 , faiss .METRIC_INNER_PRODUCT )
505
-
506
- def subtest (self , d , K , metric ):
507
- metric_names = {faiss .METRIC_L1 : 'L1' ,
508
- faiss .METRIC_L2 : 'L2' ,
509
- faiss .METRIC_INNER_PRODUCT : 'IP' }
510
-
511
- nb = 1000
512
- _ , xb , _ = get_dataset_2 (d , 0 , nb , 0 )
513
-
514
- _ , knn = faiss .knn (xb , xb , K + 1 , metric )
515
- knn = knn [:, 1 :]
516
-
517
- index = faiss .IndexNNDescentFlat (d , K , metric )
518
- index .nndescent .S = 10
519
- index .nndescent .R = 32
520
- index .nndescent .L = K + 20
521
- index .nndescent .iter = 5
522
- index .verbose = True
523
-
524
- index .add (xb )
525
- graph = index .nndescent .final_graph
526
- graph = faiss .vector_to_array (graph )
527
- graph = graph .reshape (nb , K )
528
-
529
- recalls = 0
530
- for i in range (nb ):
531
- for j in range (K ):
532
- for k in range (K ):
533
- if graph [i , j ] == knn [i , k ]:
534
- recalls += 1
535
- break
536
- recall = 1.0 * recalls / (nb * K )
537
- assert recall > 0.99
538
-
539
- def test_small_nndescent (self ):
540
- """ building a too small graph used to crash, make sure it raises
541
- an exception instead.
542
- TODO: build the exact knn graph for small cases
543
- """
544
- d = 32
545
- K = 10
546
- index = faiss .IndexNNDescentFlat (d , K , faiss .METRIC_L2 )
547
- index .nndescent .S = 10
548
- index .nndescent .R = 32
549
- index .nndescent .L = K + 20
550
- index .nndescent .iter = 5
551
- index .verbose = True
552
-
553
- xb = np .zeros ((78 , d ), dtype = 'float32' )
554
- self .assertRaises (RuntimeError , index .add , xb )
555
-
556
-
557
383
class TestResultHeap (unittest .TestCase ):
558
384
559
385
def test_keep_min (self ):
@@ -663,6 +489,7 @@ def test_bucket_sort_inplace_int64(self):
663
489
def test_bucket_sort_inplace_parallel_int64 (self ):
664
490
self .do_test_bucket_sort_inplace (4 , dtype = 'int64' )
665
491
492
+
666
493
class TestMergeKNNResults (unittest .TestCase ):
667
494
668
495
def do_test (self , ismax , dtype ):
0 commit comments