aisp 0.1.41__py3-none-any.whl → 0.1.42__py3-none-any.whl
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.
- aisp/__init__.py +2 -2
- aisp/base/__init__.py +1 -0
- aisp/base/_classifier.py +38 -32
- aisp/exceptions.py +9 -8
- aisp/nsa/__init__.py +2 -2
- aisp/nsa/_base.py +35 -27
- aisp/nsa/_negative_selection.py +177 -156
- aisp/nsa/_ns_core.py +49 -39
- aisp/utils/__init__.py +1 -1
- aisp/utils/_multiclass.py +18 -12
- aisp/utils/distance.py +96 -78
- aisp/utils/metrics.py +10 -8
- aisp/utils/sanitizers.py +27 -18
- {aisp-0.1.41.dist-info → aisp-0.1.42.dist-info}/METADATA +1 -1
- aisp-0.1.42.dist-info/RECORD +18 -0
- {aisp-0.1.41.dist-info → aisp-0.1.42.dist-info}/WHEEL +1 -1
- aisp-0.1.41.dist-info/RECORD +0 -18
- {aisp-0.1.41.dist-info → aisp-0.1.42.dist-info}/licenses/LICENSE +0 -0
- {aisp-0.1.41.dist-info → aisp-0.1.42.dist-info}/top_level.txt +0 -0
aisp/nsa/_negative_selection.py
CHANGED
@@ -22,58 +22,64 @@ from ._base import BaseNSA, Detector
|
|
22
22
|
|
23
23
|
|
24
24
|
class RNSA(BaseNSA):
|
25
|
-
"""
|
26
|
-
|
27
|
-
|
25
|
+
"""Real-Valued Negative Selection Algorithm (RNSA) for classification and anomaly detection.
|
26
|
+
|
27
|
+
Uses the self and non-self method to identify anomalies.
|
28
28
|
|
29
29
|
Parameters
|
30
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
|
-
|
31
|
+
N : int, default=100
|
32
|
+
Number of detectors.
|
33
|
+
r : float, default=0.05
|
34
|
+
Radius of the detector.
|
35
|
+
r_s : float, default=0.0001
|
36
|
+
rₛ Radius of the ``X`` own samples.
|
37
|
+
k : int, default=1
|
38
|
+
Number of neighbors near the randomly generated detectors to perform the distance average
|
39
|
+
calculation.
|
40
|
+
metric: str, default='euclidean'
|
41
|
+
Way to calculate the distance between the detector and the sample:
|
42
|
+
|
43
|
+
+ ``'Euclidean'`` ➜ The calculation of the distance is given by the expression:
|
44
|
+
√( (x₁ – x₂)² + (y₁ – y₂)² + ... + (yn – yn)²).
|
45
|
+
+ ``'minkowski'`` ➜ The calculation of the distance is given by the expression:
|
46
|
+
( |X₁ – Y₁|p + |X₂ – Y₂|p + ... + |Xn – Yn|p) ¹/ₚ.
|
47
|
+
+ ``'manhattan'`` ➜ The calculation of the distance is given by the expression:
|
48
|
+
( |x₁ – x₂| + |y₁ – y₂| + ... + |yn – yn|) .
|
49
|
+
max_discards : int, default=1000
|
50
|
+
This parameter indicates the maximum number of consecutive detector discards, aimed at
|
51
|
+
preventing a possible infinite loop in case a radius is defined that cannot generate
|
52
|
+
non-self detectors.
|
53
|
+
seed : int, default=None
|
54
|
+
Seed for the random generation of values in the detectors.
|
55
|
+
algorithm : str, default='default-NSA'
|
56
|
+
Set the algorithm version:
|
57
|
+
|
58
|
+
+ ``'default-NSA'``: Default algorithm with fixed radius.
|
59
|
+
+ ``'V-detector'``: This algorithm is based on the article Ji & Dasgupta (2004) [1]_
|
60
|
+
and uses a variable radius for anomaly detection in feature spaces.
|
61
|
+
|
62
|
+
**kwargs : dict
|
63
|
+
Parâmetros adicionais. Os seguintes argumentos são reconhecidos:
|
64
|
+
|
65
|
+
+ non_self_label : str, default='non-self'
|
66
|
+
This variable stores the label that will be assigned when the data has only one
|
67
|
+
output class, and the sample is classified as not belonging to that class.
|
68
|
+
+ cell_bounds : bool, default=False
|
69
|
+
If set to ``True``, this option limits the generation of detectors to the space
|
70
|
+
within the plane between 0 and 1. This means that any detector whose radius exceeds
|
71
|
+
this limit is discarded, this variable is only used in the ``V-detector`` algorithm.
|
72
|
+
+ p : float, default=2
|
73
|
+
This parameter stores the value of ``p`` used in the Minkowski distance. The default
|
74
|
+
is ``2``, which represents Euclidean distance. Different values of p lead
|
75
|
+
to different variants of the Minkowski Distance.
|
76
|
+
|
77
|
+
References
|
72
78
|
----------
|
73
|
-
[1]
|
74
|
-
|
75
|
-
|
76
|
-
|
79
|
+
.. [1] Ji, Z.; Dasgupta, D. (2004).
|
80
|
+
Real-Valued Negative Selection Algorithm with Variable-Sized Detectors.
|
81
|
+
In *Lecture Notes in Computer Science*, vol. 3025.
|
82
|
+
https://doi.org/10.1007/978-3-540-24854-5_30
|
77
83
|
"""
|
78
84
|
|
79
85
|
def __init__(
|
@@ -117,25 +123,30 @@ class RNSA(BaseNSA):
|
|
117
123
|
|
118
124
|
def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
|
119
125
|
"""
|
120
|
-
|
121
|
-
method negative selection method(``NegativeSelect``).
|
126
|
+
Perform training according to X and y, using the negative selection method (NegativeSelect).
|
122
127
|
|
123
128
|
Parameters
|
124
129
|
----------
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
130
|
+
X : npt.NDArray
|
131
|
+
Training array, containing the samples and their characteristics, [``N samples`` (
|
132
|
+
rows)][``N features`` (columns)].
|
133
|
+
y : npt.NDArray
|
134
|
+
Array of target classes of ``X`` with [``N samples`` (lines)].
|
135
|
+
verbose: bool, default=True
|
136
|
+
Feedback from detector generation to the user.
|
129
137
|
|
130
138
|
Raises
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
139
|
+
------
|
140
|
+
TypeError
|
141
|
+
If X or y are not ndarrays or have incompatible shapes.
|
142
|
+
MaxDiscardsReachedError
|
143
|
+
The maximum number of detector discards was reached during maturation. Check the
|
144
|
+
defined radius value and consider reducing it.
|
135
145
|
|
136
146
|
Returns
|
137
|
-
|
138
|
-
|
147
|
+
-------
|
148
|
+
self : RNSA
|
149
|
+
Returns the instance itself.
|
139
150
|
"""
|
140
151
|
progress = None
|
141
152
|
super()._check_and_raise_exceptions_fit(X, y)
|
@@ -197,29 +208,26 @@ class RNSA(BaseNSA):
|
|
197
208
|
|
198
209
|
def predict(self, X: npt.NDArray) -> Optional[npt.NDArray]:
|
199
210
|
"""
|
200
|
-
|
201
|
-
created after training.
|
211
|
+
Prediction of classes based on detectors created after training.
|
202
212
|
|
203
213
|
Parameters
|
204
214
|
----------
|
205
|
-
|
206
|
-
Array with input samples with [``
|
207
|
-
|
215
|
+
X : npt.NDArray
|
216
|
+
Array with input samples with [``N_samples`` (Lines)] and [``N_characteristics``
|
217
|
+
(Columns)]
|
208
218
|
|
209
219
|
Raises
|
210
|
-
|
211
|
-
|
220
|
+
------
|
221
|
+
TypeError
|
212
222
|
If X is not an ndarray or list.
|
213
|
-
|
223
|
+
FeatureDimensionMismatch
|
214
224
|
If the number of features in X does not match the expected number.
|
215
225
|
|
216
226
|
Returns
|
217
|
-
|
218
|
-
|
227
|
+
-------
|
228
|
+
C : npt.NDArray or None
|
219
229
|
an ndarray of the form ``C`` [``N samples``], containing the predicted classes
|
220
|
-
for ``X``.
|
221
|
-
* ``None``
|
222
|
-
If there are no detectors for the prediction.
|
230
|
+
for ``X``. Returns `None` if no detectors are available for prediction.
|
223
231
|
"""
|
224
232
|
# If there are no detectors, Returns None.
|
225
233
|
if self._detectors is None:
|
@@ -260,21 +268,22 @@ class RNSA(BaseNSA):
|
|
260
268
|
return np.array(c)
|
261
269
|
|
262
270
|
def __checks_valid_detector(
|
263
|
-
self, x_class: npt.NDArray
|
271
|
+
self, x_class: npt.NDArray, vector_x: npt.NDArray
|
264
272
|
) -> Union[bool, tuple[bool, float]]:
|
265
273
|
"""
|
266
|
-
|
274
|
+
Check if the detector has a valid non-proper r radius for the class.
|
267
275
|
|
268
276
|
Parameters
|
269
277
|
----------
|
270
|
-
|
278
|
+
x_class : npt.NDArray
|
271
279
|
Array ``x_class`` with the samples per class.
|
272
|
-
|
280
|
+
vector_x : npt.NDArray
|
273
281
|
Randomly generated vector x candidate detector with values between[0, 1].
|
274
282
|
|
275
283
|
Returns
|
276
|
-
|
277
|
-
|
284
|
+
-------
|
285
|
+
Validity : bool
|
286
|
+
Returns whether the detector is valid or not.
|
278
287
|
"""
|
279
288
|
# If any of the input arrays have zero size, Returns false.
|
280
289
|
if np.size(x_class) == 0 or np.size(vector_x) == 0:
|
@@ -282,7 +291,7 @@ class RNSA(BaseNSA):
|
|
282
291
|
# If self.k > 1, uses the k nearest neighbors (kNN); otherwise, checks the detector
|
283
292
|
# without considering kNN.
|
284
293
|
if self.k > 1:
|
285
|
-
knn_list =
|
294
|
+
knn_list = []
|
286
295
|
for x in x_class:
|
287
296
|
# Calculates the distance between the two vectors and adds it to the kNN list if
|
288
297
|
# the distance is smaller than the largest distance in the list.
|
@@ -317,20 +326,21 @@ class RNSA(BaseNSA):
|
|
317
326
|
self, knn: npt.NDArray, distance: float
|
318
327
|
) -> npt.NDArray:
|
319
328
|
"""
|
320
|
-
|
321
|
-
if the distance of the new sample is less, replace it and sort in ascending order.
|
329
|
+
Compare the k-nearest neighbor distance at position k=1 in the list knn.
|
322
330
|
|
331
|
+
If the distance of the new sample is less, replace it and sort in ascending order.
|
323
332
|
|
324
333
|
Parameters
|
325
334
|
----------
|
326
|
-
|
335
|
+
knn : npt.NDArray
|
327
336
|
List of k-nearest neighbor distances.
|
328
|
-
|
337
|
+
distance : float
|
329
338
|
Distance to check.
|
330
339
|
|
331
340
|
Returns
|
332
|
-
|
333
|
-
|
341
|
+
-------
|
342
|
+
knn : npt.NDArray
|
343
|
+
Updated and sorted nearest neighbor list.
|
334
344
|
"""
|
335
345
|
# If the number of distances in kNN is less than k, adds the distance.
|
336
346
|
if len(knn) < self.k:
|
@@ -346,18 +356,20 @@ class RNSA(BaseNSA):
|
|
346
356
|
|
347
357
|
return knn
|
348
358
|
|
349
|
-
def __compare_sample_to_detectors(self, line: npt.NDArray):
|
359
|
+
def __compare_sample_to_detectors(self, line: npt.NDArray) -> Optional[str]:
|
350
360
|
"""
|
351
|
-
|
361
|
+
Compare a sample with the detectors, verifying if the sample is proper.
|
352
362
|
|
353
363
|
Parameters
|
354
364
|
----------
|
355
|
-
|
365
|
+
line : npt.NDArray
|
366
|
+
vector with N-features
|
356
367
|
|
357
368
|
Returns
|
358
|
-
|
359
|
-
|
360
|
-
|
369
|
+
-------
|
370
|
+
possible_classes : Optional[str]
|
371
|
+
Returns the predicted class with the detectors or None if the sample does not qualify
|
372
|
+
for any class.
|
361
373
|
"""
|
362
374
|
# List to store the classes and the average distance between the detectors and the sample.
|
363
375
|
possible_classes = []
|
@@ -391,37 +403,45 @@ class RNSA(BaseNSA):
|
|
391
403
|
|
392
404
|
def __distance(self, u: npt.NDArray, v: npt.NDArray) -> float:
|
393
405
|
"""
|
394
|
-
|
406
|
+
Calculate the distance between two points by the chosen ``metric``.
|
395
407
|
|
396
408
|
Parameters
|
397
409
|
----------
|
398
|
-
|
399
|
-
|
410
|
+
u : npt.NDArray
|
411
|
+
Coordinates of the first point.
|
412
|
+
v : npt.NDArray
|
413
|
+
Coordinates of the second point.
|
400
414
|
|
401
415
|
Returns
|
402
|
-
|
403
|
-
|
416
|
+
-------
|
417
|
+
Distance : float
|
418
|
+
between the two points.
|
404
419
|
"""
|
405
420
|
return compute_metric_distance(u, v, get_metric_code(self.metric), self.p)
|
406
421
|
|
407
422
|
def __detector_is_valid_to_vdetector(
|
408
423
|
self, distance: float, vector_x: npt.NDArray
|
409
424
|
) -> Union[bool, tuple[bool, float]]:
|
410
|
-
"""
|
411
|
-
|
412
|
-
|
425
|
+
"""Validate the detector against the vdetector.
|
426
|
+
|
427
|
+
Check if the distance between the detector and the samples, minus the radius of the
|
428
|
+
samples, is greater than the minimum radius.
|
413
429
|
|
414
430
|
Parameters
|
415
431
|
----------
|
416
|
-
|
417
|
-
|
418
|
-
|
432
|
+
distance : float
|
433
|
+
minimum distance calculated between all samples.
|
434
|
+
vector_x : np.ndarray
|
435
|
+
randomly generated candidate detector vector x with values between 0 and 1.
|
419
436
|
|
420
437
|
Returns
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
438
|
+
-------
|
439
|
+
valid : bool
|
440
|
+
|
441
|
+
- ``False`` if the calculated radius is smaller than the minimum distance or exceeds
|
442
|
+
the edge of the space, if this option is enabled.
|
443
|
+
|
444
|
+
- ``True`` and the distance minus the radius of the samples, if the radius is valid.`
|
425
445
|
"""
|
426
446
|
new_detector_r = float(distance - self.r_s)
|
427
447
|
if self.r >= new_detector_r:
|
@@ -437,27 +457,33 @@ class RNSA(BaseNSA):
|
|
437
457
|
|
438
458
|
|
439
459
|
class BNSA(BaseNSA):
|
440
|
-
"""
|
441
|
-
|
442
|
-
identification purposes of anomalies through the self and not
|
460
|
+
"""BNSA (Binary Negative Selection Algorithm).
|
461
|
+
|
462
|
+
Class is for classification and identification purposes of anomalies through the self and not
|
463
|
+
self method.
|
443
464
|
|
444
465
|
Parameters
|
445
466
|
----------
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
467
|
+
N : int, default=100
|
468
|
+
Number of detectors.
|
469
|
+
aff_thresh : float, default=0.1
|
470
|
+
The variable represents the percentage of similarity between the T cell and the own
|
471
|
+
samples. The default value is 10% (0.1), while a value of 1.0 represents 100% similarity.
|
472
|
+
max_discards : int, default=1000
|
473
|
+
This parameter indicates the maximum number of detector discards in sequence, which aims
|
474
|
+
to avoid a possible infinite loop if a radius is defined that it is not possible to
|
475
|
+
generate non-self detectors.
|
476
|
+
seed : Optional[int], default=None
|
477
|
+
Seed for the random generation of values in the detectors.
|
478
|
+
no_label_sample_selection : str, default="max_average_difference"
|
479
|
+
Method for selecting labels for samples designated as non-self by all detectors.
|
480
|
+
Available method types:
|
481
|
+
|
482
|
+
- max_average_difference - Selects the class with the highest average difference among the
|
483
|
+
detectors.
|
484
|
+
|
485
|
+
- max_nearest_difference - Selects the class with the highest difference between the
|
486
|
+
nearest and farthest detector from the sample.
|
461
487
|
"""
|
462
488
|
|
463
489
|
def __init__(
|
@@ -470,8 +496,6 @@ class BNSA(BaseNSA):
|
|
470
496
|
"max_average_difference", "max_nearest_difference"
|
471
497
|
] = "max_average_difference",
|
472
498
|
):
|
473
|
-
super().__init__()
|
474
|
-
|
475
499
|
self.N: int = sanitize_param(N, 100, lambda x: x > 0)
|
476
500
|
self.aff_thresh: float = sanitize_param(aff_thresh, 0.1, lambda x: 0 < x < 1)
|
477
501
|
self.max_discards: float = sanitize_param(max_discards, 1000, lambda x: x > 0)
|
@@ -497,22 +521,22 @@ class BNSA(BaseNSA):
|
|
497
521
|
return self._detectors
|
498
522
|
|
499
523
|
def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
|
500
|
-
"""
|
501
|
-
The function ``fit(...)``, performs the training according to ``X`` and ``y``, using the
|
502
|
-
method negative selection method(``NegativeSelect``).
|
524
|
+
"""Training according to X and y, using the method negative selection method.
|
503
525
|
|
504
526
|
Parameters
|
505
527
|
----------
|
506
|
-
|
507
|
-
Training array, containing the samples and their characteristics,
|
508
|
-
|
509
|
-
|
528
|
+
X : npt.NDArray
|
529
|
+
Training array, containing the samples and their characteristics, [``N samples`` (
|
530
|
+
rows)][``N features`` (columns)].
|
531
|
+
y : npt.NDArray
|
510
532
|
Array of target classes of ``X`` with [``N samples`` (lines)].
|
511
|
-
|
533
|
+
verbose : bool, default=True
|
534
|
+
Feedback from detector generation to the user.
|
512
535
|
|
513
536
|
Returns
|
514
|
-
|
515
|
-
|
537
|
+
-------
|
538
|
+
self : BNSA
|
539
|
+
Returns the instance it self.
|
516
540
|
"""
|
517
541
|
super()._check_and_raise_exceptions_fit(X, y, "BNSA")
|
518
542
|
|
@@ -574,20 +598,19 @@ class BNSA(BaseNSA):
|
|
574
598
|
return self
|
575
599
|
|
576
600
|
def predict(self, X: npt.NDArray) -> Optional[npt.NDArray]:
|
577
|
-
"""
|
578
|
-
Function to perform the prediction of classes based on detectors
|
579
|
-
created after training.
|
601
|
+
"""Prediction of classes based on detectors created after training.
|
580
602
|
|
581
603
|
Parameters
|
582
604
|
----------
|
583
|
-
|
584
|
-
[``
|
605
|
+
X : npt.NDArray
|
606
|
+
Array with input samples with [``N_samples`` (Lines)] and [``N_characteristics``(
|
607
|
+
Columns)]
|
585
608
|
|
586
609
|
Returns
|
587
|
-
|
588
|
-
|
589
|
-
containing the predicted classes for
|
590
|
-
|
610
|
+
-------
|
611
|
+
c : Optional[npt.NDArray]
|
612
|
+
an ndarray of the form ``C`` [``N samples``], containing the predicted classes for
|
613
|
+
``X``. Returns``None``: If there are no detectors for the prediction.
|
591
614
|
"""
|
592
615
|
# If there are no detectors, Returns None.
|
593
616
|
if self._detectors is None:
|
@@ -629,19 +652,17 @@ class BNSA(BaseNSA):
|
|
629
652
|
return np.array(c)
|
630
653
|
|
631
654
|
def __assign_class_to_non_self_sample(self, line: npt.NDArray, c: list):
|
632
|
-
"""
|
633
|
-
|
634
|
-
|
635
|
-
|
655
|
+
"""Determine the class of a sample when all detectors classify it as "non-self".
|
656
|
+
|
657
|
+
Classification is performed using the ``max_average_difference`` and
|
658
|
+
``max_nearest_difference`` methods.
|
636
659
|
|
637
660
|
Parameters
|
638
661
|
----------
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
----------
|
644
|
-
* list: The list of predictions `c` updated with the class assigned to the sample.
|
662
|
+
line : npt.NDArray
|
663
|
+
Sample to be classified.
|
664
|
+
c : list
|
665
|
+
List of predictions to be updated with the new classification.
|
645
666
|
"""
|
646
667
|
class_differences: dict = {}
|
647
668
|
for _class_ in self.classes:
|
aisp/nsa/_ns_core.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
-
"""
|
1
|
+
"""nsa - Negative Selection.
|
2
2
|
|
3
3
|
The functions perform detector checks and utilize Numba decorators for Just-In-Time compilation
|
4
4
|
"""
|
5
5
|
|
6
|
+
import numpy as np
|
6
7
|
import numpy.typing as npt
|
7
8
|
from numba import njit, types
|
8
9
|
|
@@ -18,25 +19,29 @@ from ..utils.distance import compute_metric_distance, hamming
|
|
18
19
|
cache=True
|
19
20
|
)
|
20
21
|
def check_detector_bnsa_validity(
|
21
|
-
x_class: npt.NDArray,
|
22
|
-
vector_x: npt.NDArray,
|
22
|
+
x_class: npt.NDArray[np.bool_],
|
23
|
+
vector_x: npt.NDArray[np.bool_],
|
23
24
|
aff_thresh: float
|
24
25
|
) -> bool:
|
25
26
|
"""
|
26
|
-
|
27
|
-
|
28
|
-
in ``x_class`` is less than or
|
27
|
+
Check the validity of a candidate detector using the Hamming distance.
|
28
|
+
|
29
|
+
A detector is considered INVALID if its distance to any sample in ``x_class`` is less than or
|
30
|
+
equal to ``aff_thresh``.
|
29
31
|
|
30
32
|
Parameters
|
31
33
|
----------
|
32
|
-
|
33
|
-
(n_samples, n_features).
|
34
|
-
|
35
|
-
|
34
|
+
x_class : npt.NDArray[np.bool_]
|
35
|
+
Array containing the class samples. Expected shape: (n_samples, n_features).
|
36
|
+
vector_x : npt.NDArray[np.bool_]
|
37
|
+
Array representing the detector. Expected shape: (n_features,).
|
38
|
+
aff_thresh : float
|
39
|
+
Affinity threshold.
|
36
40
|
|
37
41
|
Returns
|
38
|
-
|
39
|
-
|
42
|
+
-------
|
43
|
+
valid : bool
|
44
|
+
True if the detector is valid, False otherwise.
|
40
45
|
"""
|
41
46
|
n = x_class.shape[1]
|
42
47
|
if n != vector_x.shape[0]:
|
@@ -58,24 +63,25 @@ def check_detector_bnsa_validity(
|
|
58
63
|
cache=True
|
59
64
|
)
|
60
65
|
def bnsa_class_prediction(
|
61
|
-
features: npt.NDArray,
|
62
|
-
class_detectors: npt.NDArray,
|
66
|
+
features: npt.NDArray[np.bool_],
|
67
|
+
class_detectors: npt.NDArray[np.bool_],
|
63
68
|
aff_thresh: float
|
64
69
|
) -> int:
|
65
|
-
"""
|
66
|
-
Defines the class of a sample from the non-self detectors.
|
70
|
+
"""Define the class of a sample from the non-self detectors.
|
67
71
|
|
68
72
|
Parameters
|
69
73
|
----------
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
74
|
+
features : npt.NDArray[np.bool_]
|
75
|
+
binary sample to be classified (shape: [n_features]).
|
76
|
+
class_detectors : npt.NDArray[np.bool_]
|
77
|
+
Array containing the detectors of all classes (shape: [n_classes, n_detectors, n_features]).
|
78
|
+
aff_thresh : float
|
79
|
+
Affinity threshold that determines whether a detector recognizes the sample as non-self.
|
75
80
|
|
76
81
|
Returns
|
77
|
-
|
78
|
-
|
82
|
+
-------
|
83
|
+
best_class_index : int
|
84
|
+
Index of the predicted class. Returns -1 if it is non-self for all classes.
|
79
85
|
"""
|
80
86
|
n_classes, n_detectors, _ = class_detectors.shape
|
81
87
|
best_class_idx = -1
|
@@ -116,31 +122,35 @@ def bnsa_class_prediction(
|
|
116
122
|
cache=True
|
117
123
|
)
|
118
124
|
def check_detector_rnsa_validity(
|
119
|
-
x_class: npt.NDArray,
|
120
|
-
vector_x: npt.NDArray,
|
125
|
+
x_class: npt.NDArray[np.float64],
|
126
|
+
vector_x: npt.NDArray[np.float64],
|
121
127
|
threshold: float,
|
122
128
|
metric: int,
|
123
129
|
p: float
|
124
130
|
) -> bool:
|
125
|
-
"""
|
126
|
-
|
127
|
-
|
128
|
-
|
131
|
+
"""Check the validity of a candidate detector using the Hamming distance.
|
132
|
+
|
133
|
+
A detector is considered INVALID if its distance to any sample in ``x_class`` is less than
|
134
|
+
or equal to ``aff_thresh``.
|
129
135
|
|
130
136
|
Parameters
|
131
137
|
----------
|
132
|
-
|
133
|
-
(n_samples, n_features).
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
138
|
+
x_class : npt.NDArray[np.float64]
|
139
|
+
Array containing the class samples. Expected shape: (n_samples, n_features).
|
140
|
+
vector_x : npt.NDArray[np.float64]
|
141
|
+
Array representing the detector. Expected shape: (n_features,).
|
142
|
+
threshold : float
|
143
|
+
threshold.
|
144
|
+
metric : int
|
145
|
+
Distance metric to be used. Available options: [0 (Euclidean), 1 (Manhattan),
|
146
|
+
2 (Minkowski)].
|
147
|
+
p : float
|
148
|
+
Parameter for the Minkowski distance (used only if `metric` is "minkowski").
|
140
149
|
|
141
150
|
Returns
|
142
|
-
|
143
|
-
|
151
|
+
-------
|
152
|
+
valid : bool
|
153
|
+
True if the detector is valid, False otherwise.
|
144
154
|
"""
|
145
155
|
n = x_class.shape[1]
|
146
156
|
if n != vector_x.shape[0]:
|