deduplicate-lib 0.0.5__tar.gz → 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {deduplicate_lib-0.0.5/src/deduplicate_lib.egg-info → deduplicate_lib-0.1.0}/PKG-INFO +1 -1
- deduplicate_lib-0.1.0/src/deduplicate_lib/__init__.py +1 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/core/duplicate_detection_algorithm.py +37 -10
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/core/tolerance_calculator.py +3 -3
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/plugins/duplicate_detection_algorithms/distance_matrix.py +5 -6
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/plugins/duplicate_detection_algorithms/multi_hashing.py +30 -8
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/plugins/tolerance_calculators/natural_tolerance_plateau_probe.py +36 -11
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/plugins/tolerance_calculators/perturbed_dataset_reclustering.py +2 -1
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0/src/deduplicate_lib.egg-info}/PKG-INFO +1 -1
- deduplicate_lib-0.0.5/src/deduplicate_lib/__init__.py +0 -1
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/LICENSE +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/README.md +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/pyproject.toml +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/setup.cfg +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/core/__init__.py +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/core/plugin_registry.py +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/plugins/__init__.py +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/plugins/duplicate_detection_algorithms/__init__.py +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/plugins/tolerance_calculators/__init__.py +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib.egg-info/SOURCES.txt +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib.egg-info/dependency_links.txt +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib.egg-info/requires.txt +0 -0
- {deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
@@ -81,6 +81,8 @@ class DuplicateDetectionAlgorithm(ABC):
|
|
|
81
81
|
self.distance_metric = distance_metric
|
|
82
82
|
self.unique_vector_indices = unique_vector_indices
|
|
83
83
|
self.max_vector_array_size = max_vector_array_size
|
|
84
|
+
self._dirty = True # auxiliary structures (distance matrix, hash dict, ...) need (re)building
|
|
85
|
+
self.vector_count = 0
|
|
84
86
|
|
|
85
87
|
self.set_dataset_array(dataset_array) if dataset_array.size > 0 else None
|
|
86
88
|
|
|
@@ -145,14 +147,27 @@ class DuplicateDetectionAlgorithm(ABC):
|
|
|
145
147
|
self.distance_matrix[:, self.vector_count] = np.pad(new_distances, (0, self.max_vector_array_size - len(new_distances)), constant_values=0)
|
|
146
148
|
|
|
147
149
|
|
|
148
|
-
def pre_dda_processing(
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
150
|
+
def pre_dda_processing(self, *args, **kwargs) -> None:
|
|
151
|
+
"""Ensure the dataset array and any algorithm-specific auxiliary structures (e.g. distance matrix, hash dictionary) are preallocated and up to date before duplication checks.
|
|
152
|
+
|
|
153
|
+
This is idempotent: auxiliary structures are only rebuilt via `_rebuild_auxiliary_structures()` if the dataset has changed (`_dirty`) since they were last built. Child classes should implement `_rebuild_auxiliary_structures()` rather than overriding this method.
|
|
154
|
+
"""
|
|
155
|
+
self.preinitialize_dataset_array()
|
|
156
|
+
if self._dirty:
|
|
157
|
+
self._rebuild_auxiliary_structures()
|
|
158
|
+
self._dirty = False
|
|
159
|
+
|
|
160
|
+
def _rebuild_auxiliary_structures(self) -> None:
|
|
161
|
+
"""Rebuild any algorithm-specific auxiliary structures from the current dataset array. Overridden by child classes."""
|
|
152
162
|
pass
|
|
153
163
|
|
|
154
164
|
def add_input_vector_to_dda(self) -> None:
|
|
155
|
-
"""Add the input vector to the dataset array and update
|
|
165
|
+
"""Add the input vector to the dataset array and update any auxiliary structures accordingly."""
|
|
166
|
+
self.pre_dda_processing()
|
|
167
|
+
self._append_vector_to_structures()
|
|
168
|
+
|
|
169
|
+
def _append_vector_to_structures(self) -> None:
|
|
170
|
+
"""Append `input_vector` to the dataset array and incrementally update any auxiliary structures. Overridden by child classes."""
|
|
156
171
|
pass
|
|
157
172
|
|
|
158
173
|
def get_unique_vector_indices(self) -> np.ndarray:
|
|
@@ -182,24 +197,30 @@ class DuplicateDetectionAlgorithm(ABC):
|
|
|
182
197
|
def set_dataset_array(self, new_dataset_array: np.ndarray) -> None:
|
|
183
198
|
"""Create new dataset array with correct shape and vector count
|
|
184
199
|
if the input vector and dataset are both empty then the vector length cannot be determined, so we initialize an empty dataset array"""
|
|
200
|
+
current_dataset_array = self._dataset_array[: self.vector_count]
|
|
201
|
+
if new_dataset_array.shape == current_dataset_array.shape and np.array_equal(
|
|
202
|
+
new_dataset_array, current_dataset_array
|
|
203
|
+
):
|
|
204
|
+
return
|
|
205
|
+
|
|
185
206
|
if new_dataset_array.shape[0] > self.max_vector_array_size:
|
|
186
207
|
raise ValueError("New dataset array size exceeds maximum allowed size.")
|
|
187
208
|
|
|
188
209
|
self.vector_count = new_dataset_array.shape[0]
|
|
189
210
|
if new_dataset_array.size == 0 and self.input_vector.size > 0:
|
|
190
211
|
self.initialize_dataset_array(len(self.input_vector))
|
|
191
|
-
elif new_dataset_array.size > 0:
|
|
212
|
+
elif new_dataset_array.size > 0:
|
|
192
213
|
self.initialize_dataset_array(new_dataset_array.shape[1])
|
|
193
214
|
self._dataset_array[: self.vector_count] = new_dataset_array
|
|
194
215
|
else:
|
|
195
216
|
self._dataset_array = new_dataset_array
|
|
217
|
+
|
|
218
|
+
self._dirty = True
|
|
196
219
|
|
|
197
220
|
def get_vector_length(self) -> int:
|
|
198
221
|
has_input = self.input_vector.size > 0
|
|
199
222
|
has_dataset = self._dataset_array.size > 0
|
|
200
|
-
|
|
201
|
-
print(f"input_vector: {self.input_vector}, dataset_array: {self._dataset_array}")
|
|
202
|
-
|
|
223
|
+
|
|
203
224
|
if has_input and has_dataset:
|
|
204
225
|
print(self.input_vector.shape, self._dataset_array.shape)
|
|
205
226
|
if self.input_vector.shape[0] != self._dataset_array.shape[1]:
|
|
@@ -209,6 +230,8 @@ class DuplicateDetectionAlgorithm(ABC):
|
|
|
209
230
|
vector_length = self.input_vector.shape[0]
|
|
210
231
|
elif has_dataset:
|
|
211
232
|
vector_length = self._dataset_array.shape[1]
|
|
233
|
+
elif has_input:
|
|
234
|
+
vector_length = len(self.input_vector)
|
|
212
235
|
else:
|
|
213
236
|
raise ValueError(
|
|
214
237
|
"Cannot determine vector length from input vector or dataset array. "
|
|
@@ -219,10 +242,14 @@ class DuplicateDetectionAlgorithm(ABC):
|
|
|
219
242
|
|
|
220
243
|
def preinitialize_dataset_array(self) -> None:
|
|
221
244
|
vector_length=self.get_vector_length()
|
|
222
|
-
|
|
245
|
+
|
|
223
246
|
if self._dataset_array.shape[0] > self.max_vector_array_size:
|
|
224
247
|
raise ValueError("Dataset array size exceeds maximum allowed size.")
|
|
225
248
|
|
|
249
|
+
if self._dataset_array.size == 0:
|
|
250
|
+
self.initialize_dataset_array(vector_length)
|
|
251
|
+
self.vector_count = 0
|
|
252
|
+
|
|
226
253
|
if self._dataset_array.shape[0] != self.max_vector_array_size:
|
|
227
254
|
existing_data = self._dataset_array.copy()
|
|
228
255
|
self.vector_count = existing_data.shape[0]
|
{deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib/core/tolerance_calculator.py
RENAMED
|
@@ -29,7 +29,7 @@ class ToleranceCalculator(ABC):
|
|
|
29
29
|
yield
|
|
30
30
|
finally:
|
|
31
31
|
setattr(obj, attr, original)
|
|
32
|
-
|
|
32
|
+
|
|
33
33
|
def binary_search_tolerance(
|
|
34
34
|
self,
|
|
35
35
|
target_unique_vectors: int,
|
|
@@ -65,7 +65,7 @@ class ToleranceCalculator(ABC):
|
|
|
65
65
|
):
|
|
66
66
|
|
|
67
67
|
unique_vectors = self.duplicate_detection_algorithm_object.get_dataset_unique_structures()
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
|
|
70
70
|
results_dict[tolerance] = unique_vectors
|
|
71
71
|
if unique_vectors < target_unique_vectors: # loosen tolerance
|
|
@@ -84,7 +84,7 @@ class ToleranceCalculator(ABC):
|
|
|
84
84
|
best_min = tolerance
|
|
85
85
|
else:
|
|
86
86
|
best_max = tolerance
|
|
87
|
-
|
|
87
|
+
print(f"Tolerance: {tolerance}, Unique Vectors: {unique_vectors}")
|
|
88
88
|
tolerance = (best_min + best_max) / 2
|
|
89
89
|
|
|
90
90
|
# reset the dataset_array to the original dataset array after the search is complete
|
|
@@ -22,6 +22,7 @@ class DistanceMatrix(DuplicateDetectionAlgorithm):
|
|
|
22
22
|
return f"DistanceMatrix(tolerance={self.tolerance}, distance_metric={self.distance_metric})"
|
|
23
23
|
|
|
24
24
|
def duplicate_check(self) -> bool:
|
|
25
|
+
self.preinitialize_dataset_array()
|
|
25
26
|
return bool(
|
|
26
27
|
np.any(
|
|
27
28
|
self.get_new_distance_matrix_column(self.dataset_array) < self.tolerance
|
|
@@ -29,6 +30,7 @@ class DistanceMatrix(DuplicateDetectionAlgorithm):
|
|
|
29
30
|
)
|
|
30
31
|
|
|
31
32
|
def get_dataset_unique_structures(self) -> int:
|
|
33
|
+
self.pre_dda_processing()
|
|
32
34
|
self.unique_vector_indices = np.zeros(self.vector_count, dtype=bool)
|
|
33
35
|
self.unique_vector_indices[0] = True # the first vector is always unique
|
|
34
36
|
for i in range(1, self.vector_count):
|
|
@@ -37,14 +39,11 @@ class DistanceMatrix(DuplicateDetectionAlgorithm):
|
|
|
37
39
|
self.unique_vector_indices[i] = True
|
|
38
40
|
return np.sum(self.unique_vector_indices)
|
|
39
41
|
|
|
40
|
-
def
|
|
41
|
-
self.preinitialize_dataset_array()
|
|
42
|
-
self.initialize_distance_matrix()
|
|
42
|
+
def _rebuild_auxiliary_structures(self) -> None:
|
|
43
43
|
self.compute_distance_matrix(self.dataset_array)
|
|
44
|
-
|
|
45
|
-
def
|
|
44
|
+
|
|
45
|
+
def _append_vector_to_structures(self) -> None:
|
|
46
46
|
"""Add the input vector to the dataset array and update the distance matrix accordingly."""
|
|
47
47
|
self.add_new_vector_to_distance_matrix(self.dataset_array)
|
|
48
48
|
self._dataset_array[self.vector_count] = self.input_vector
|
|
49
49
|
self.vector_count += 1
|
|
50
|
-
|
|
@@ -65,6 +65,8 @@ class MultiHashing(DuplicateDetectionAlgorithm):
|
|
|
65
65
|
self.perturbation_array = np.abs(
|
|
66
66
|
rng.normal(loc=0.0, scale=1.0, size=self.perturbations)
|
|
67
67
|
)
|
|
68
|
+
# self.perturbation_array = rng.normal(loc=0.0, scale=1.0, size=self.perturbations)
|
|
69
|
+
|
|
68
70
|
|
|
69
71
|
def _ensure_perturbation_array(self):
|
|
70
72
|
|
|
@@ -97,6 +99,7 @@ class MultiHashing(DuplicateDetectionAlgorithm):
|
|
|
97
99
|
input_vector = self.input_vector
|
|
98
100
|
self._ensure_perturbation_array()
|
|
99
101
|
rounded_and_perturbed = fast_round_and_perturb(input_vector, self.perturbation_array, self.tolerance)
|
|
102
|
+
rounded_and_perturbed = np.where(rounded_and_perturbed == 0, 0.0, rounded_and_perturbed) # ensure that any -0.0 values are converted to 0.0 otherwise they hash differently
|
|
100
103
|
hash_vector = np.zeros(self.perturbations, dtype=int)
|
|
101
104
|
for i in range(self.perturbations):
|
|
102
105
|
hash_vector[i] = hash(rounded_and_perturbed[i].tobytes())
|
|
@@ -112,7 +115,7 @@ class MultiHashing(DuplicateDetectionAlgorithm):
|
|
|
112
115
|
|
|
113
116
|
return self.hash_dict
|
|
114
117
|
|
|
115
|
-
def add_input_vector_hashes_to_dictionary(self, input_vector: np.ndarray | None = None, input_index: int = None) -> None:
|
|
118
|
+
def add_input_vector_hashes_to_dictionary(self, input_vector: np.ndarray | None = None, input_index: int | None = None) -> None:
|
|
116
119
|
if input_vector is None:
|
|
117
120
|
input_vector = self.input_vector
|
|
118
121
|
if input_index is None:
|
|
@@ -123,9 +126,9 @@ class MultiHashing(DuplicateDetectionAlgorithm):
|
|
|
123
126
|
|
|
124
127
|
for hash_index, hash_value in enumerate(hash_vector):
|
|
125
128
|
if hash_value not in self.hash_dict[hash_index]:
|
|
126
|
-
self.hash_dict[hash_index][hash_value] =
|
|
129
|
+
self.hash_dict[hash_index][hash_value] = {input_index: True} # if first time seeing this hash value, add index with True (indicating a unique structure for this perturbation)
|
|
127
130
|
else:
|
|
128
|
-
self.hash_dict[hash_index][hash_value]
|
|
131
|
+
self.hash_dict[hash_index][hash_value][input_index] = False # if hash value already exists, add index with False (indicating a clash for this perturbation)
|
|
129
132
|
|
|
130
133
|
def warn_if_vector_dict_mismatch(self, hash_vector: np.ndarray):
|
|
131
134
|
if len(self.hash_dict) != len(hash_vector):
|
|
@@ -138,6 +141,7 @@ class MultiHashing(DuplicateDetectionAlgorithm):
|
|
|
138
141
|
Returns:
|
|
139
142
|
bool: True if a duplicate is detected, False otherwise.
|
|
140
143
|
"""
|
|
144
|
+
self.pre_dda_processing()
|
|
141
145
|
hash_vector = self.create_hash_vector()
|
|
142
146
|
self.warn_if_vector_dict_mismatch(hash_vector)
|
|
143
147
|
|
|
@@ -152,6 +156,7 @@ class MultiHashing(DuplicateDetectionAlgorithm):
|
|
|
152
156
|
return bool(duplicate_structure)
|
|
153
157
|
|
|
154
158
|
def get_uniqueness_score(self) -> float:
|
|
159
|
+
self.pre_dda_processing()
|
|
155
160
|
hash_vector = self.create_hash_vector()
|
|
156
161
|
self.warn_if_vector_dict_mismatch(hash_vector)
|
|
157
162
|
clash_vector = np.zeros(self.perturbations, dtype=bool)
|
|
@@ -169,10 +174,11 @@ class MultiHashing(DuplicateDetectionAlgorithm):
|
|
|
169
174
|
Returns:
|
|
170
175
|
int: The number of unique structures in the dataset.
|
|
171
176
|
"""
|
|
172
|
-
self.
|
|
177
|
+
self.pre_dda_processing()
|
|
173
178
|
u_list_of_lists = []
|
|
174
179
|
for i in range(self.perturbations):
|
|
175
|
-
|
|
180
|
+
u_list_of_dicts = [self.hash_dict[i][hash_value] for hash_value in self.hash_dict[i]]
|
|
181
|
+
u_list = [unique_index for d in u_list_of_dicts for unique_index, is_unique in d.items() if is_unique]
|
|
176
182
|
u_list_of_lists.append(u_list)
|
|
177
183
|
|
|
178
184
|
# count the number of times each index appears across all perturbations
|
|
@@ -194,12 +200,28 @@ class MultiHashing(DuplicateDetectionAlgorithm):
|
|
|
194
200
|
|
|
195
201
|
return np.sum(self.unique_vector_indices)
|
|
196
202
|
|
|
203
|
+
def _perturbation_array_stale(self) -> bool:
|
|
204
|
+
return (
|
|
205
|
+
self.perturbation_array.shape[0] != self.perturbations
|
|
206
|
+
or self.seed != getattr(self, "_hash_dict_seed", None)
|
|
207
|
+
)
|
|
208
|
+
|
|
197
209
|
def pre_dda_processing(self, *args, **kwargs) -> None:
|
|
198
|
-
|
|
210
|
+
if (
|
|
211
|
+
self.tolerance != getattr(self, "_hash_dict_tolerance", None)
|
|
212
|
+
or self._perturbation_array_stale()
|
|
213
|
+
):
|
|
214
|
+
self._dirty = True
|
|
215
|
+
super().pre_dda_processing(*args, **kwargs)
|
|
216
|
+
|
|
217
|
+
def _rebuild_auxiliary_structures(self) -> None:
|
|
218
|
+
if self._perturbation_array_stale():
|
|
219
|
+
self.set_perturbation_array()
|
|
199
220
|
self.compute_hash_vector_dictionary()
|
|
200
|
-
|
|
221
|
+
self._hash_dict_tolerance = self.tolerance
|
|
222
|
+
self._hash_dict_seed = self.seed
|
|
201
223
|
|
|
202
|
-
def
|
|
224
|
+
def _append_vector_to_structures(self) -> None:
|
|
203
225
|
"""Add the input vector to the dataset array and update the hash vector array accordingly."""
|
|
204
226
|
self._dataset_array[self.vector_count] = self.input_vector
|
|
205
227
|
self.add_input_vector_hashes_to_dictionary(self.input_vector)
|
|
@@ -100,18 +100,43 @@ class NaturalTolerancePlateauProbe(ToleranceCalculator):
|
|
|
100
100
|
sorted_tols,
|
|
101
101
|
tolerance_results,
|
|
102
102
|
)
|
|
103
|
-
|
|
103
|
+
print(f'plateau log: {plateau_log}')
|
|
104
|
+
|
|
104
105
|
plateau_lengths = []
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
106
|
+
starts=[]
|
|
107
|
+
ends=[]
|
|
108
|
+
# find starts
|
|
109
|
+
for i in range(1, len(plateau_log)):
|
|
110
|
+
if plateau_log[i] and not plateau_log[i - 1]:
|
|
111
|
+
starts.append(i)
|
|
112
|
+
|
|
113
|
+
# find ends
|
|
114
|
+
for i in range(1, len(plateau_log)):
|
|
115
|
+
if not plateau_log[i] and plateau_log[i - 1]:
|
|
116
|
+
ends.append(i)
|
|
117
|
+
|
|
118
|
+
# handle if starts and ends are mismatched due to starting or ending in a plateau
|
|
119
|
+
if len(starts) > len(ends):
|
|
120
|
+
ends.append(len(plateau_log))
|
|
121
|
+
elif len(ends) > len(starts):
|
|
122
|
+
starts.insert(0, 0)
|
|
123
|
+
|
|
124
|
+
# find lengths
|
|
125
|
+
for start, end in zip(starts, ends):
|
|
126
|
+
plateau_lengths.append(
|
|
127
|
+
(sorted_tols[start], sorted_tols[end], end - start)
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
# for i in range(len(plateau_log)):
|
|
131
|
+
# if plateau_log[i]:
|
|
132
|
+
# if not plateau_log[i - 1]:
|
|
133
|
+
# start = i
|
|
134
|
+
# else:
|
|
135
|
+
# if plateau_log[i - 1]:
|
|
136
|
+
# end = i
|
|
137
|
+
# plateau_lengths.append(
|
|
138
|
+
# (sorted_tols[start], sorted_tols[end], end - start)
|
|
139
|
+
# )
|
|
115
140
|
|
|
116
141
|
return plateau_lengths
|
|
117
142
|
|
|
@@ -56,7 +56,8 @@ class PerturbedDatasetReclustering(ToleranceCalculator):
|
|
|
56
56
|
Returns:
|
|
57
57
|
float: The calculated tolerance value.
|
|
58
58
|
"""
|
|
59
|
-
self.
|
|
59
|
+
self.duplicate_detection_algorithm_object.pre_dda_processing()
|
|
60
|
+
print(self.duplicate_detection_algorithm_object.vector_count)
|
|
60
61
|
if self.target_unique_vectors_threshold == "average":
|
|
61
62
|
low_tolerance = self.binary_search_tolerance(
|
|
62
63
|
target_unique_vectors=self.target_unique_vectors,
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.0.5"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{deduplicate_lib-0.0.5 → deduplicate_lib-0.1.0}/src/deduplicate_lib.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|