nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: dirty-cat/dirty_cat
Pull request #148 (2020-10-26 17:36:29)

regression test for cache overflow
Regression test for cache overflow problem (rows are encoded with -1s). 
Fixes: #146 


Patch:
diff --git a/dirty_cat/minhash_encoder.py b/dirty_cat/minhash_encoder.py
index ca597ae..e1c8f8f 100644
--- a/dirty_cat/minhash_encoder.py
+++ b/dirty_cat/minhash_encoder.py
@@ -73,6 +73,7 @@ class MinHashEncoder(BaseEstimator, TransformerMixin):
         self.minmax_hash = minmax_hash
         self.count = 0
         self.handle_missing = handle_missing
+        self._capacity = 2**10
 
     def get_unique_ngrams(self, string, ngram_range):
         """ Return the set of unique n-grams of a string.
@@ -158,7 +159,7 @@ class MinHashEncoder(BaseEstimator, TransformerMixin):
         self
             The fitted MinHashEncoder instance.
         """
-        self.hash_dict = LRUDict(capacity=2**10)
+        self.hash_dict = LRUDict(capacity=self._capacity)
         return self
 
     def transform(self, X):


Test patch:
diff --git a/dirty_cat/test/test_minhash_encoder.py b/dirty_cat/test/test_minhash_encoder.py
index b780e65..b1afa02 100644
--- a/dirty_cat/test/test_minhash_encoder.py
+++ b/dirty_cat/test/test_minhash_encoder.py
@@ -1,4 +1,6 @@
 import time
+import random
+from string import ascii_lowercase
 import numpy as np
 import pytest
 
@@ -124,6 +126,21 @@ def test_missing_values(input_type, missing, hashing):
     return
 
 
+def test_cache_overflow():
+    # Regression test for cache overflow resulting in -1s in encoding
+    def get_random_string(length):
+        letters = ascii_lowercase
+        result_str = ''.join(random.choice(letters) for i in range(length))
+        return result_str
+
+    encoder = MinHashEncoder(n_components=3)
+    capacity = encoder._capacity
+    raw_data = [get_random_string(10) for x in range(capacity + 1)]
+    y = encoder.fit_transform(raw_data)
+
+    assert len(y[y == -1.0]) == 0
+
+
 if __name__ == '__main__':
     print('start test')
     test_MinHashEncoder()