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.
@@ -22,58 +22,64 @@ from ._base import BaseNSA, Detector
22
22
 
23
23
 
24
24
  class RNSA(BaseNSA):
25
- """
26
- The ``RNSA`` (Real-Valued Negative Selection Algorithm) class is for classification and
27
- identification purposes. of anomalies through the self and not self method.
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
- * N (``int``): Number of detectors. Defaults to ``100``.
32
- * r (``float``): Radius of the detector. Defaults to ``0.05``.
33
- * r_s (``float``): rₛ Radius of the ``X`` own samples. Defaults to ``0.0001``.
34
- * k (``int``): Number of neighbors near the randomly generated detectors to perform the
35
- distance average calculation. Defaults to ``1``.
36
- * metric (``str``): Way to calculate the distance between the detector and the sample:
37
- + ``'Euclidean'`` The calculation of the distance is given by the expression:
38
- √( (x₁ x₂)² + (y₁ y₂)² + ... + (yn – yn)²).
39
- + ``'minkowski'`` ➜ The calculation of the distance is given by the expression:
40
- ( |X₁ – Y₁|p + |X₂ – Y₂|p + ... + |Xn – Yn|p) ¹/ₚ.
41
- + ``'manhattan'`` The calculation of the distance is given by the expression:
42
- ( |x₁ – x₂| + |y₁ – y₂| + ... + |yn – yn|) .
43
-
44
- Defaults to ``'euclidean'``.
45
- * max_discards (``int``): This parameter indicates the maximum number of consecutive
46
- detector discards, aimed at preventing a possible infinite loop in case a radius
47
- is defined that cannot generate non-self detectors. Defaults to ``1000``.
48
- * seed (``int``): Seed for the random generation of values in the detectors. Defaults to
49
- ``None``.
50
- * algorithm(``str``), Set the algorithm version:
51
- * ``'default-NSA'``: Default algorithm with fixed radius.
52
- * ``'V-detector'``: This algorithm is based on the article \
53
- [Real-Valued Negative Selection Algorithm with Variable-Sized Detectors][2], \
54
- by Ji, Z., Dasgupta, D. (2004), and uses a variable radius for anomaly \
55
- detection in feature spaces.
56
-
57
- Defaults to ``'default-NSA'``.
58
-
59
- * ``**kwargs``:
60
- - non_self_label (``str``): This variable stores the label that will be assigned \
61
- when the data has only one output class, and the sample is classified as not \
62
- belonging to that class. Defaults to ``'non-self'``.
63
- - cell_bounds (``bool``): If set to ``True``, this option limits the generation \
64
- of detectors to the space within the plane between 0 and 1. This means that \
65
- any detector whose radius exceeds this limit is discarded, this variable is \
66
- only used in the ``V-detector`` algorithm. Defaults to ``False``.
67
- - p (``float``): This parameter stores the value of ``p`` used in the Minkowski \
68
- distance. The default is ``2``, which represents normalized Euclidean distance.\
69
- Different values of p lead to different variants of the [Minkowski Distance][1].
70
-
71
- Notes
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] https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.minkowski_distance.html
74
-
75
- [2] https://doi.org/10.1007/978-3-540-24854-5_30
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
- The function ``fit(...)``, performs the training according to ``X`` and ``y``, using the
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
- * X (``npt.NDArray``): Training array, containing the samples and their \
126
- characteristics, [``N samples`` (rows)][``N features`` (columns)].
127
- * y (``npt.NDArray``): Array of target classes of ``X`` with [``N samples`` (lines)].
128
- verbose (``bool``): Feedback from detector generation to the user.
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
- * TypeError: If X or y are not ndarrays or have incompatible shapes.
133
- * MaxDiscardsReachedError: The maximum number of detector discards was reached during
134
- maturation. Check the defined radius value and consider reducing it.
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
- * (``self``): Returns the instance itself.
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
- Function to perform the prediction of classes based on detectors
201
- created after training.
211
+ Prediction of classes based on detectors created after training.
202
212
 
203
213
  Parameters
204
214
  ----------
205
- * X (``npt.NDArray``)
206
- Array with input samples with [``N samples`` (Lines)] and
207
- [``N characteristics``(Columns)]
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
- * TypeError
220
+ ------
221
+ TypeError
212
222
  If X is not an ndarray or list.
213
- * FeatureDimensionMismatch
223
+ FeatureDimensionMismatch
214
224
  If the number of features in X does not match the expected number.
215
225
 
216
226
  Returns
217
- ----------
218
- * C (``npt.NDArray``)
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 = None, vector_x: npt.NDArray = None
271
+ self, x_class: npt.NDArray, vector_x: npt.NDArray
264
272
  ) -> Union[bool, tuple[bool, float]]:
265
273
  """
266
- Function to check if the detector has a valid non-proper ``r`` radius for the class.
274
+ Check if the detector has a valid non-proper r radius for the class.
267
275
 
268
276
  Parameters
269
277
  ----------
270
- * x_class (``npt.NDArray``)
278
+ x_class : npt.NDArray
271
279
  Array ``x_class`` with the samples per class.
272
- * vector_x (``npt.NDArray``)
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
- * Validity (``bool``): Returns whether the detector is valid or not.
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 = np.empty(shape=0)
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
- Compares the k-nearest neighbor distance at position ``k-1`` in the list ``knn``,
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
- * knn (``npt.NDArray``)
335
+ knn : npt.NDArray
327
336
  List of k-nearest neighbor distances.
328
- * distance (``float``)
337
+ distance : float
329
338
  Distance to check.
330
339
 
331
340
  Returns
332
- ----------
333
- * ``npt.NDArray``: Updated and sorted nearest neighbor list.
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
- Function to compare a sample with the detectors, verifying if the sample is proper.
361
+ Compare a sample with the detectors, verifying if the sample is proper.
352
362
 
353
363
  Parameters
354
364
  ----------
355
- * line (``npt.NDArray``): vector with N-features
365
+ line : npt.NDArray
366
+ vector with N-features
356
367
 
357
368
  Returns
358
- ----------
359
- * Returns the predicted class with the detectors or None if the sample does not qualify
360
- for any class.
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
- Function to calculate the distance between two points by the chosen ``metric``.
406
+ Calculate the distance between two points by the chosen ``metric``.
395
407
 
396
408
  Parameters
397
409
  ----------
398
- * u (``npt.NDArray``): Coordinates of the first point.
399
- * v (``npt.NDArray``): Coordinates of the second point.
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
- * Distance (``float``): between the two points.
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
- Check if the distance between the detector and the samples, minus the radius of the samples,
412
- is greater than the minimum radius.
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
- * distance (``float``): minimum distance calculated between all samples.
417
- * vector_x (``numpy.ndarray``): randomly generated candidate detector vector x with
418
- values between 0 and 1.
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
- * ``False`` if the calculated radius is smaller than the minimum distance or exceeds the
423
- edge of the space, if this option is enabled.
424
- * ``True`` and the distance minus the radius of the samples, if the radius is valid.`
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
- The ``BNSA`` (Binary Negative Selection Algorithm) class is for classification and
442
- identification purposes of anomalies through the self and not self method.
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
- * N (``int``): Number of detectors. Defaults to ``100``.
447
- * aff_thresh (``float``): The variable represents the percentage of similarity
448
- between the T cell and the own samples. The default value is 10% (0.1), while a value of
449
- 1.0 represents 100% similarity.
450
- * max_discards (``int``): This parameter indicates the maximum number of detector discards in
451
- sequence, which aims to avoid a possible infinite loop if a radius is defined that it is
452
- not possible to generate non-self detectors. Defaults to ``1000``.
453
- * seed (``int``): Seed for the random generation of values in the detectors. Defaults to
454
- ``None``.
455
- * no_label_sample_selection (``str``): Method for selecting labels for samples designated as
456
- non-self by all detectors. Available method types:
457
- - (``max_average_difference``): Selects the class with the highest average difference
458
- among the detectors.
459
- - (``max_nearest_difference``): Selects the class with the highest difference between
460
- the nearest and farthest detector from the sample.
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
- * X (``npt.NDArray``):
507
- Training array, containing the samples and their characteristics,
508
- [``N samples`` (rows)][``N features`` (columns)].
509
- * y (``npt.NDArray``):
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
- verbose (``bool``): Feedback from detector generation to the user.
533
+ verbose : bool, default=True
534
+ Feedback from detector generation to the user.
512
535
 
513
536
  Returns
514
- ----------
515
- * (``self``): Returns the instance itself.
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
- * X (``npt.NDArray``): Array with input samples with [``N samples`` (Lines)] and
584
- [``N characteristics``(Columns)]
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
- * c (``npt.NDArray``): an ndarray of the form ``C`` [``N samples``],
589
- containing the predicted classes for ``X``
590
- * ``None``: If there are no detectors for the prediction.
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
- This function determines the class of a sample when all detectors classify it
634
- as "non-self". Classification is performed using the ``max_average_difference``
635
- and ``max_nearest_difference`` methods.
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
- * line (list): Sample to be classified.
640
- * c (list): List of predictions to be updated with the new classification.
641
-
642
- Returns
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
- """ns: Negative Selection
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
- Checks the validity of a candidate detector (vector_x) against samples from a class (x_class)
27
- using the Hamming distance. A detector is considered INVALID if its distance to any sample
28
- in ``x_class`` is less than or equal to ``aff_thresh``.
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
- * x_class (``npt.NDArray``): Array containing the class samples. Expected shape:
33
- (n_samples, n_features).
34
- * vector_x (``npt.NDArray``): Array representing the detector. Expected shape: (n_features,).
35
- * aff_thresh (``float``): Affinity threshold.
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
- * True if the detector is valid, False otherwise.
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
- * features (``npt.NDArray``): binary sample to be classified (shape: [n_features]).
71
- * class_detectors (``npt.NDArray``): Array containing the detectors of all classes
72
- (shape: [n_classes, n_detectors, n_features]).
73
- * aff_thresh (``float``): Affinity threshold that determines whether a detector recognizes the
74
- sample as non-self.
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
- * int: Index of the predicted class. Returns -1 if it is non-self for all classes.
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
- Checks the validity of a candidate detector (vector_x) against samples from a class (x_class)
127
- using the Hamming distance. A detector is considered INVALID if its distance to any sample
128
- in ``x_class`` is less than or equal to ``aff_thresh``.
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
- * x_class (``npt.NDArray``): Array containing the class samples. Expected shape:
133
- (n_samples, n_features).
134
- * vector_x (``npt.NDArray``): Array representing the detector. Expected shape: (n_features,).
135
- * threshold (``float``): threshold.
136
- * metric (``int``): Distance metric to be used. Available options:
137
- [0 (Euclidean), 1 (Manhattan), 2 (Minkowski)].
138
- * p (``float``): Parameter for the Minkowski distance (used only if `metric`
139
- is "minkowski").
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
- * True if the detector is valid, False otherwise.
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]: