@@ -42,7 +42,8 @@ def test_eval_mixin_perfect_match(metric_fn, kwargs, storage, config, start_stor
4242 da1 = DocumentArray .empty (10 )
4343 da1 .embeddings = np .random .random ([10 , 256 ])
4444 da1_index = DocumentArray (da1 , storage = storage , config = config )
45- da1 .match (da1_index , exclude_self = True )
45+ with da1_index :
46+ da1 .match (da1_index , exclude_self = True )
4647 r = da1 .evaluate (ground_truth = da1 , metrics = [metric_fn ], strict = False , ** kwargs )[
4748 metric_fn
4849 ]
@@ -80,7 +81,8 @@ def test_eval_mixin_perfect_match_multiple_metrics(storage, config, start_storag
8081 da1 = DocumentArray .empty (10 )
8182 da1 .embeddings = np .random .random ([10 , 256 ])
8283 da1_index = DocumentArray (da1 , storage = storage , config = config )
83- da1 .match (da1_index , exclude_self = True )
84+ with da1_index :
85+ da1 .match (da1_index , exclude_self = True )
8486 r = da1 .evaluate (ground_truth = da1 , metrics = metric_fns , strict = False , ** kwargs )
8587 for metric_fn in metric_fns :
8688 assert metric_fn in r
@@ -123,7 +125,8 @@ def test_eval_mixin_perfect_match_labeled(
123125 d .tags = {'label' : 'A' }
124126 da1 .embeddings = np .random .random ([10 , 256 ])
125127 da1_index = DocumentArray (da1 , storage = storage , config = config )
126- da1 .match (da1_index , exclude_self = True )
128+ with da1_index :
129+ da1 .match (da1_index , exclude_self = True )
127130 r = da1 .evaluate (metrics = [metric_fn ], ** kwargs )[metric_fn ]
128131 assert isinstance (r , float )
129132 assert r == 1.0
@@ -166,7 +169,8 @@ def test_eval_mixin_zero_labeled(storage, config, metric_fn, start_storage, kwar
166169 for d in da2 :
167170 d .tags = {'label' : 'B' }
168171 da1_index = DocumentArray (da2 , storage = storage , config = config )
169- da1 .match (da1_index , exclude_self = True )
172+ with da1_index :
173+ da1 .match (da1_index , exclude_self = True )
170174 r = da1 .evaluate ([metric_fn ], ** kwargs )[metric_fn ]
171175 assert isinstance (r , float )
172176 assert r == 0.0
@@ -264,9 +268,10 @@ def test_eval_mixin_zero_match(storage, config, metric_fn, start_storage, kwargs
264268 da2 = copy .deepcopy (da1 )
265269 da2 .embeddings = np .random .random ([10 , 256 ])
266270 da2_index = DocumentArray (da2 , storage = storage , config = config )
267- da2 .match (da2_index , exclude_self = True )
271+ with da2_index :
272+ da2 .match (da2_index , exclude_self = True )
268273
269- r = da1 .evaluate (ground_truth = da2 , metrics = [metric_fn ], ** kwargs )[metric_fn ]
274+ r = da1 .evaluate (ground_truth = da2 , metrics = [metric_fn ], ** kwargs )[metric_fn ]
270275 assert isinstance (r , float )
271276 assert r == 1.0
272277 for d in da1 :
@@ -337,17 +342,20 @@ def test_same_hash_same_len_fun_should_work(storage, config, start_storage):
337342 da1 = DocumentArray .empty (10 )
338343 da1 .embeddings = np .random .random ([10 , 3 ])
339344 da1_index = DocumentArray (da1 , storage = storage , config = config )
340- da1 .match (da1_index )
345+ with da1_index :
346+ da1 .match (da1_index )
341347 da2 = DocumentArray .empty (10 )
342348 da2 .embeddings = np .random .random ([10 , 3 ])
343349 da2_index = DocumentArray (da1 , storage = storage , config = config )
344- da2 .match (da2_index )
345- with pytest .raises (ValueError ):
346- da1 .evaluate (ground_truth = da2 , metrics = ['precision_at_k' ])
347- for d1 , d2 in zip (da1 , da2 ):
348- d1 .id = d2 .id
350+ with da2_index :
351+ da2 .match (da2_index )
352+ with da1_index , da2_index :
353+ with pytest .raises (ValueError ):
354+ da1 .evaluate (ground_truth = da2 , metrics = ['precision_at_k' ])
355+ for d1 , d2 in zip (da1 , da2 ):
356+ d1 .id = d2 .id
349357
350- da1 .evaluate (ground_truth = da2 , metrics = ['precision_at_k' ])
358+ da1 .evaluate (ground_truth = da2 , metrics = ['precision_at_k' ])
351359
352360
353361@pytest .mark .parametrize (
@@ -368,7 +376,8 @@ def test_adding_noise(storage, config, start_storage):
368376
369377 da .embeddings = np .random .random ([10 , 3 ])
370378 da_index = DocumentArray (da , storage = storage , config = config )
371- da .match (da_index , exclude_self = True )
379+ with da_index :
380+ da .match (da_index , exclude_self = True )
372381
373382 da2 = copy .deepcopy (da )
374383
@@ -410,17 +419,18 @@ def test_adding_noise(storage, config, start_storage):
410419def test_diff_match_len_in_gd (storage , config , metric_fn , start_storage , kwargs ):
411420 da1 = DocumentArray .empty (10 )
412421 da1 .embeddings = np .random .random ([10 , 128 ])
413- da1_index = DocumentArray (da1 , storage = storage , config = config )
422+ # da1_index = DocumentArray(da1, storage=storage, config=config)
414423 da1 .match (da1 , exclude_self = True )
415424
416425 da2 = copy .deepcopy (da1 )
417426 da2 .embeddings = np .random .random ([10 , 128 ])
418427 da2_index = DocumentArray (da2 , storage = storage , config = config )
419- da2 .match (da2_index , exclude_self = True )
420- # pop some matches from first document
421- da2 [0 ].matches .pop (8 )
428+ with da2_index :
429+ da2 .match (da2_index , exclude_self = True )
430+ # pop some matches from first document
431+ da2 [0 ].matches .pop (8 )
422432
423- r = da1 .evaluate (ground_truth = da2 , metrics = [metric_fn ], ** kwargs )[metric_fn ]
433+ r = da1 .evaluate (ground_truth = da2 , metrics = [metric_fn ], ** kwargs )[metric_fn ]
424434 assert isinstance (r , float )
425435 np .testing .assert_allclose (r , 1.0 , rtol = 1e-2 ) #
426436 for d in da1 :
@@ -486,7 +496,8 @@ def test_useless_groundtruth_warning_should_raise(storage, config, start_storage
486496 d .tags = {'label' : 'A' }
487497 da1 .embeddings = np .random .random ([10 , 256 ])
488498 da1_index = DocumentArray (da1 , storage = storage , config = config )
489- da1 .match (da1_index , exclude_self = True )
499+ with da1_index :
500+ da1 .match (da1_index , exclude_self = True )
490501 da2 = DocumentArray .empty (10 )
491502 with pytest .warns (UserWarning ):
492503 da1 .evaluate (ground_truth = da2 , metrics = ['precision_at_k' ])
@@ -518,13 +529,14 @@ def test_embed_and_evaluate_single_da(storage, config, start_storage):
518529 dummy_embed_function (gt )
519530 gt .match (gt , limit = 3 )
520531
521- res = queries_da .embed_and_evaluate (
522- ground_truth = gt ,
523- metrics = ['precision_at_k' , 'reciprocal_rank' ],
524- embed_funcs = dummy_embed_function ,
525- match_batch_size = 1 ,
526- limit = 3 ,
527- )
532+ with queries_da :
533+ res = queries_da .embed_and_evaluate (
534+ ground_truth = gt ,
535+ metrics = ['precision_at_k' , 'reciprocal_rank' ],
536+ embed_funcs = dummy_embed_function ,
537+ match_batch_size = 1 ,
538+ limit = 3 ,
539+ )
528540 assert all ([v == 1.0 for v in res .values ()])
529541
530542
@@ -601,15 +613,16 @@ def test_embed_and_evaluate_two_das(storage, config, sample_size, start_storage)
601613 dummy_embed_function (gt_index )
602614 gt_queries .match (gt_index , limit = 3 )
603615
604- res = queries_da .embed_and_evaluate (
605- ground_truth = gt_queries ,
606- index_data = index_da ,
607- metrics = ['precision_at_k' , 'reciprocal_rank' ],
608- embed_funcs = dummy_embed_function ,
609- match_batch_size = 1 ,
610- limit = 3 ,
611- query_sample_size = sample_size ,
612- )
616+ with index_da :
617+ res = queries_da .embed_and_evaluate (
618+ ground_truth = gt_queries ,
619+ index_data = index_da ,
620+ metrics = ['precision_at_k' , 'reciprocal_rank' ],
621+ embed_funcs = dummy_embed_function ,
622+ match_batch_size = 1 ,
623+ limit = 3 ,
624+ query_sample_size = sample_size ,
625+ )
613626 assert all ([v == 1.0 for v in res .values ()])
614627
615628
@@ -681,25 +694,26 @@ def emb_func(da):
681694 da1 = DocumentArray ([Document (text = str (i ), tags = {label_tag : i }) for i in range (3 )])
682695 da2 = DocumentArray (da1 , storage = storage , config = config , copy = True )
683696
684- if (
685- use_index
686- ): # query and index da are distinct # (different embeddings are generated)
687- res = da1 .embed_and_evaluate (
688- index_data = da2 ,
689- metrics = metric_fns ,
690- embed_funcs = emb_func ,
691- match_batch_size = 1 ,
692- limit = 3 ,
693- label_tag = label_tag ,
694- )
695- else : # query and index are the same (embeddings of both das are equal)
696- res = da2 .embed_and_evaluate (
697- metrics = metric_fns ,
698- embed_funcs = emb_func ,
699- match_batch_size = 1 ,
700- limit = 3 ,
701- label_tag = label_tag ,
702- )
697+ with da2 :
698+ if (
699+ use_index
700+ ): # query and index da are distinct # (different embeddings are generated)
701+ res = da1 .embed_and_evaluate (
702+ index_data = da2 ,
703+ metrics = metric_fns ,
704+ embed_funcs = emb_func ,
705+ match_batch_size = 1 ,
706+ limit = 3 ,
707+ label_tag = label_tag ,
708+ )
709+ else : # query and index are the same (embeddings of both das are equal)
710+ res = da2 .embed_and_evaluate (
711+ metrics = metric_fns ,
712+ embed_funcs = emb_func ,
713+ match_batch_size = 1 ,
714+ limit = 3 ,
715+ label_tag = label_tag ,
716+ )
703717 for key in metric_fns :
704718 assert key in res
705719 assert abs (res [key ] - expected [key ]) < 1e-4
@@ -799,9 +813,10 @@ def test_embed_and_evaluate_with_embed_model(
799813 [Document (text = f'some text { i } ' , tags = {'label' : str (i )}) for i in range (5 )]
800814 )
801815 da = DocumentArray (da , storage = storage , config = config )
802- res = da .embed_and_evaluate (
803- metrics = ['precision_at_k' ], embed_models = model , collate_fns = collate_fn
804- )
816+ with da :
817+ res = da .embed_and_evaluate (
818+ metrics = ['precision_at_k' ], embed_models = model , collate_fns = collate_fn
819+ )
805820 assert res
806821 assert res ['precision_at_k' ] == 0.2
807822
@@ -873,12 +888,13 @@ def emb_func(da):
873888 )
874889 da2 = DocumentArray (da1 , storage = storage , config = config , copy = True )
875890
876- res = da1 .embed_and_evaluate (
877- index_data = da2 ,
878- metrics = metric_fns ,
879- embed_funcs = emb_func ,
880- query_sample_size = sample_size ,
881- )
891+ with da2 :
892+ res = da1 .embed_and_evaluate (
893+ index_data = da2 ,
894+ metrics = metric_fns ,
895+ embed_funcs = emb_func ,
896+ query_sample_size = sample_size ,
897+ )
882898 expected_size = (
883899 sample_size if sample_size and (sample_size < len (da1 )) else len (da1 )
884900 )
0 commit comments