aisp 0.1.34__py3-none-any.whl → 0.1.35__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 ADDED
@@ -0,0 +1,4 @@
1
+ """Artificial Immune Systems Package"""
2
+
3
+ __author__ = "João Paulo da Silva Barros"
4
+ __version__ = "0.1.35"
aisp/exceptions.py ADDED
@@ -0,0 +1,42 @@
1
+ """Custom warnings and errors"""
2
+
3
+
4
+ class MaxDiscardsReachedError(Exception):
5
+ """Exception thrown when the maximum number of detector discards is reached."""
6
+
7
+ def __init__(self, _class_, message=None):
8
+ if message is None:
9
+ message = (
10
+ "An error has been identified:\n"
11
+ f"the maximum number of discards of detectors for the {_class_} class "
12
+ "has been reached.\nIt is recommended to check the defined radius and "
13
+ "consider reducing its value."
14
+ )
15
+
16
+ super().__init__(message)
17
+
18
+
19
+ class FeatureDimensionMismatch(Exception):
20
+ """
21
+ Exception raised when the number of input features does not match the expected number
22
+ required by the model for prediction
23
+ """
24
+
25
+ def __init__(
26
+ self,
27
+ expected: int,
28
+ received: int,
29
+ variable_name: str = None
30
+ ):
31
+ parts = []
32
+ if variable_name:
33
+ parts.append(f"In variable '{variable_name}'")
34
+
35
+ parts.append("feature dimension mismatch")
36
+
37
+ message = (
38
+ f"{' '.join(parts)}: expected {expected} features, but received {received}. "
39
+ "Please ensure the input data has the correct number of features "
40
+ "and matches the expected shape for the model."
41
+ )
42
+ super().__init__(message)
aisp/nsa/__init__.py ADDED
@@ -0,0 +1,11 @@
1
+ """nsa: Module (NSA) Negative Selection Algorithm
2
+
3
+ NSAs simulate the maturation process of T-cells in the immune system, where these cells learn to
4
+ distinguish between self and non-self. Only T-cells capable of recognizing non-self elements are
5
+ preserved.
6
+ """
7
+ from ._negative_selection import BNSA, RNSA
8
+
9
+ __author__ = "João Paulo da Silva Barros"
10
+ __all__ = ["RNSA", "BNSA"]
11
+ __version__ = "0.1.35"
aisp/nsa/_base.py ADDED
@@ -0,0 +1,212 @@
1
+ """Base Class for Negative Selection Algorithm."""
2
+ from abc import abstractmethod
3
+ from typing import Literal, Optional
4
+
5
+ import numpy as np
6
+ import numpy.typing as npt
7
+ from scipy.spatial.distance import cityblock, euclidean, minkowski
8
+
9
+ from ..exceptions import FeatureDimensionMismatch
10
+ from ..utils.metrics import accuracy_score
11
+ from ..utils.sanitizers import sanitize_choice
12
+
13
+
14
+ class Base:
15
+ """
16
+ The base class contains functions that are used by more than one class in the package, and
17
+ therefore are considered essential for the overall functioning of the system.
18
+
19
+ Parameters
20
+ ----------
21
+ * metric (``str``): Way to calculate the distance between the detector and the sample:
22
+ * ``'Euclidean'`` ➜ The calculation of the distance is given by the expression: \
23
+ √( (x₁ – x₂)² + (y₁ – y₂)² + ... + (yn – yn)²).
24
+ * ``'minkowski'`` ➜ The calculation of the distance is given by the expression: \
25
+ ( |X₁ – Y₁|p + |X₂ – Y₂|p + ... + |Xn – Yn|p) ¹/ₚ.
26
+ * ``'manhattan'`` ➜ The calculation of the distance is given by the expression: \
27
+ ( |x₁ – x₂| + |y₁ – y₂| + ... + |yn – yn|) .
28
+ * p (``float``): This parameter stores the value of ``p`` used in the Minkowski distance.\
29
+ The default is ``2``, which represents normalized Euclidean distance. Different \
30
+ values of p lead to different variants of the [Minkowski Distance][1].
31
+
32
+ Notes
33
+ ----------
34
+ [1]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.minkowski_distance.html
35
+ """
36
+
37
+ def __init__(self, metric: str = "euclidean", p: float = 2):
38
+ self.metric = sanitize_choice(metric, ["manhattan", "minkowski"], "euclidean")
39
+ self.p: float = p
40
+
41
+ def _distance(self, u: npt.NDArray, v: npt.NDArray):
42
+ """
43
+ Function to calculate the distance between two points by the chosen ``metric``.
44
+
45
+ Parameters
46
+ ----------
47
+ * u (``npt.NDArray``): Coordinates of the first point.
48
+ * v (``npt.NDArray``): Coordinates of the second point.
49
+
50
+ returns
51
+ ----------
52
+ * Distance (``double``) between the two points.
53
+ """
54
+ if self.metric == "manhattan":
55
+ return cityblock(u, v)
56
+ if self.metric == "minkowski":
57
+ return minkowski(u, v, self.p)
58
+
59
+ return euclidean(u, v)
60
+
61
+ @staticmethod
62
+ def _check_and_raise_exceptions_fit(
63
+ X: npt.NDArray = None,
64
+ y: npt.NDArray = None,
65
+ _class_: Literal["RNSA", "BNSA"] = "RNSA",
66
+ ) -> None:
67
+ """
68
+ Function responsible for verifying fit function parameters and throwing exceptions if the
69
+ verification is not successful.
70
+
71
+ Parameters
72
+ ----------
73
+ * X (``npt.NDArray``) Training array, containing the samples and their
74
+ characteristics, [``N samples`` (rows)][``N features`` (columns)].
75
+ * y (``npt.NDArray``) Array of target classes of ``X`` with [``N samples`` (lines)].
76
+ * _class_ (``Literal[RNSA, BNSA], optional``) Current class. Defaults to 'RNSA'.
77
+
78
+ Raises
79
+ ----------
80
+ * TypeError: If X or y are not ndarrays or have incompatible shapes.
81
+ * ValueError: If _class_ is BNSA and X contains values that are not composed only of
82
+ 0 and 1.
83
+ """
84
+ if isinstance(X, list):
85
+ X = np.array(X)
86
+ if isinstance(y, list):
87
+ y = np.array(y)
88
+
89
+ if not isinstance(X, np.ndarray):
90
+ raise TypeError("X is not an ndarray or list.")
91
+ if not isinstance(y, np.ndarray):
92
+ raise TypeError("y is not an ndarray or list.")
93
+
94
+ if X.shape[0] != y.shape[0]:
95
+ raise TypeError(
96
+ "X does not have the same amount of sample for the output classes in y."
97
+ )
98
+
99
+ if _class_ == "BNSA" and not np.isin(X, [0, 1]).all():
100
+ raise ValueError(
101
+ "The array X contains values that are not composed only of 0 and 1."
102
+ )
103
+
104
+ @staticmethod
105
+ def _check_and_raise_exceptions_predict(
106
+ X: npt.NDArray = None,
107
+ expected: int = 0,
108
+ _class_: Literal["RNSA", "BNSA"] = "RNSA",
109
+ ) -> None:
110
+ """
111
+ Function responsible for verifying predict function parameters and throwing exceptions if
112
+ the verification is not successful.
113
+
114
+ Parameters
115
+ ----------
116
+ * X (``npt.NDArray``)
117
+ Input array for prediction, containing the samples and their characteristics,
118
+ [``N samples`` (rows)][``N features`` (columns)].
119
+ * expected (``int``)
120
+ Expected number of features per sample (columns in X).
121
+ * _class_ (``Literal[RNSA, BNSA], optional``)
122
+ Current class. Defaults to 'RNSA'.
123
+
124
+ Raises
125
+ ----------
126
+ * TypeError
127
+ If X is not an ndarray or list.
128
+ * FeatureDimensionMismatch
129
+ If the number of features in X does not match the expected number.
130
+ * ValueError
131
+ If _class_ is BNSA and X contains values that are not composed only of 0 and 1.
132
+ """
133
+ if not isinstance(X, (np.ndarray, list)):
134
+ raise TypeError("X is not an ndarray or list")
135
+ if expected != len(X[0]):
136
+ raise FeatureDimensionMismatch(
137
+ expected,
138
+ len(X[0]),
139
+ "X"
140
+ )
141
+
142
+ if _class_ != "BNSA":
143
+ return
144
+
145
+ # Checks if matrix X contains only binary samples. Otherwise, raises an exception.
146
+ if not np.isin(X, [0, 1]).all():
147
+ raise ValueError(
148
+ "The array X contains values that are not composed only of 0 and 1."
149
+ )
150
+
151
+ def score(self, X: npt.NDArray, y: list) -> float:
152
+ """
153
+ Score function calculates forecast accuracy.
154
+
155
+ Details
156
+ ----------
157
+ This function performs the prediction of X and checks how many elements are equal
158
+ between vector y and y_predicted. This function was added for compatibility with some
159
+ scikit-learn functions.
160
+
161
+ Parameters
162
+ ----------
163
+ * X (``np.ndarray``):
164
+ Feature set with shape (n_samples, n_features).
165
+ * y (``np.ndarray``):
166
+ True values with shape (n_samples,).
167
+
168
+ Returns
169
+ ----------
170
+ * accuracy (``float``): The accuracy of the model.
171
+ """
172
+ if len(y) == 0:
173
+ return 0
174
+ y_pred = self.predict(X)
175
+ return accuracy_score(y, y_pred)
176
+
177
+ @abstractmethod
178
+ def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
179
+ """
180
+ Function to train the model using the input data ``X`` and corresponding labels ``y``.
181
+
182
+ This abstract method is implemented by the class that inherits it.
183
+
184
+ Parameters
185
+ ----------
186
+ * X (``npt.NDArray``): Input data used for training the model, previously normalized to the
187
+ range [0, 1].
188
+ * y (``npt.NDArray``): Corresponding labels or target values for the input data.
189
+ * verbose (``bool``, optional): Flag to enable or disable detailed output during training.
190
+ Default is ``True``.
191
+
192
+ Returns
193
+ ----------
194
+ * self: Returns the instance of the class that implements this method.
195
+ """
196
+
197
+ @abstractmethod
198
+ def predict(self, X) -> Optional[npt.NDArray]:
199
+ """
200
+ Function to generate predictions based on the input data ``X``.
201
+
202
+ This abstract method is implemented by the class that inherits it.
203
+
204
+ Parameters
205
+ ----------
206
+ * X (``npt.NDArray``): Input data for which predictions will be generated.
207
+
208
+ Returns
209
+ ----------
210
+ * Predictions (``Optional[npt.NDArray]``): Predicted values for each input sample, or
211
+ ``None`` if the prediction fails.
212
+ """