aisp 0.1.33__tar.gz → 0.1.35__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.
@@ -1,15 +1,15 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: aisp
3
- Version: 0.1.33
3
+ Version: 0.1.35
4
4
  Summary: Package with techniques of artificial immune systems.
5
5
  Author-email: João Paulo da Silva Barros <jpsilvabarr@gmail.com>
6
6
  Maintainer-email: Alison Zille Lopes <alisonzille@gmail.com>
7
- License: LGPL-3.0 license
7
+ License-Expression: LGPL-3.0-only
8
8
  Project-URL: Homepage, https://ais-package.github.io/
9
9
  Project-URL: Documentation, https://ais-package.github.io/docs/intro
10
10
  Project-URL: Source Code, https://github.com/AIS-Package/aisp
11
11
  Project-URL: Tracker, https://github.com/AIS-Package/aisp/issues
12
- Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
12
+ Keywords: Artificial Immune Systems,classification,Natural computing,machine learning,artificial intelligence
13
13
  Classifier: Operating System :: OS Independent
14
14
  Classifier: Programming Language :: Python
15
15
  Classifier: Programming Language :: Python :: 3
@@ -23,6 +23,7 @@ License-File: LICENSE
23
23
  Requires-Dist: numpy>=1.22.4
24
24
  Requires-Dist: scipy>=1.8.1
25
25
  Requires-Dist: tqdm>=4.64.1
26
+ Dynamic: license-file
26
27
 
27
28
  <div align = center>
28
29
 
@@ -38,7 +39,7 @@ Requires-Dist: tqdm>=4.64.1
38
39
  <div class='language-options'>
39
40
 
40
41
  * [English.](#english)
41
- * [Português.](#português)
42
+ * [Português.](https://ais-package.github.io/pt-br/docs/intro)
42
43
 
43
44
  </div>
44
45
 
@@ -51,11 +52,6 @@ Requires-Dist: tqdm>=4.64.1
51
52
  ---
52
53
 
53
54
  <section id='english'>
54
- <div align = center>
55
-
56
- ## English
57
-
58
- </div>
59
55
 
60
56
  #### Summary:
61
57
 
@@ -143,104 +139,5 @@ Below are some examples that use a database for classification with the [Jupyter
143
139
 
144
140
  ---
145
141
 
146
-
147
- </section>
148
- </section>
149
-
150
- ---
151
-
152
- <section id='português'>
153
- <div align = center>
154
-
155
- ## Português
156
-
157
- </div>
158
-
159
- #### Sumário:
160
-
161
- > 1. [Introdução.](#introdução)
162
- > 2. [Instalação.](#instalação)
163
- > 1. [Dependências](#dependências)
164
- > 2. [Instalação do usuário](#instalação-do-usuário)
165
- > 3. [Exemplos.](#exemplos)
166
-
167
- ---
168
- <section id='introdução'>
169
-
170
- #### Introdução
171
-
172
- O **AISP** é um pacote python que implementa as técnicas dos sistemas imunológicos artificiais, distribuído sob a licença GNU Lesser General Public License v3.0 (LGPLv3).
173
-
174
- O pacote teve início no ano de **2022** como um pacote de pesquisa no instituto federal do norte de minas gerais - campus salinas (**IFNMG - Salinas**).
175
-
176
- Os sistemas imunológicos artificiais (SIA) inspiram-se no sistema imunológico dos vertebrados, criando metáforas que aplicam a capacidade de reconhecer e catalogar os patógenos, entre outras características desse sistema.
177
-
178
- ##### Algoritmos implementados:
179
-
180
- > - [x] [**Seleção Negativa.**](https://ais-package.github.io/docs/aisp-techniques/Negative%20Selection/)
181
- > - [ ] *Algoritmos de Seleção Clonal.*
182
- > - [ ] *Células Dendríticas.*
183
- > - [ ] *Teoria da Rede Imune.*
184
-
185
- </section>
186
-
187
- <section id='introdução'>
188
-
189
- #### **Instalação**
190
-
191
-
192
- O módulo requer a instalação do [python 3.8.10](https://www.python.org/downloads/) ou superior.
193
-
194
- <section id='dependências'>
195
-
196
- ##### **Dependências:**
197
- <div align = center>
198
-
199
- | Pacotes | Versão |
200
- |:-------------:|:-------------:|
201
- | numpy | ≥ 1.22.4 |
202
- | scipy | ≥ 1.8.1 |
203
- | tqdm | ≥ 4.64.1 |
204
-
205
- </div>
206
- </section>
207
-
208
- <section id='instalação-do-usuário'>
209
-
210
- ##### **Instalação do usuário**
211
-
212
- A maneira mais simples de instalação do AISP é utilizando o ``pip``:
213
-
214
- ```Bash
215
- pip install aisp
216
- ```
217
-
218
- </section>
219
-
220
- </section>
221
- <section id='exemplos'>
222
-
223
- #### Exemplos:
224
-
225
- ---
226
-
227
- ##### Exemplo utilizando a técnica de seleção negativa (**nsa**):
228
-
229
- No exemplo presente nesse [notebook](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/example_with_randomly_generated_dataset-pt.ipynb), gerando **500** amostras aleatórias dispostas em dois grupos um para cada classe.
230
-
231
- A seguir alguns exemplos que utiliza-se de base de dados para classificação com a ferramenta [Jupyter notebook](https://jupyter.org/).
232
-
233
- #### **Seleção Negativa:**
234
-
235
- + **RNSA** Aplicação das tecnica de seleção negativa para classificação utilizando a base de dados de flores da família Iris e Old Faithful Geyser:
236
- + [iris_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/iris_dataBase_example_pt-br.ipynb)
237
- + [geyser_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/geyser_dataBase_example_pt-br.ipynb)
238
-
239
- + **BNSA**
240
- + [mushrooms_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/BNSA/mushrooms_dataBase_example_en.ipynb)
241
-
242
-
243
- ---
244
-
245
142
  </section>
246
143
  </section>
@@ -12,7 +12,7 @@
12
12
  <div class='language-options'>
13
13
 
14
14
  * [English.](#english)
15
- * [Português.](#português)
15
+ * [Português.](https://ais-package.github.io/pt-br/docs/intro)
16
16
 
17
17
  </div>
18
18
 
@@ -25,11 +25,6 @@
25
25
  ---
26
26
 
27
27
  <section id='english'>
28
- <div align = center>
29
-
30
- ## English
31
-
32
- </div>
33
28
 
34
29
  #### Summary:
35
30
 
@@ -117,104 +112,5 @@ Below are some examples that use a database for classification with the [Jupyter
117
112
 
118
113
  ---
119
114
 
120
-
121
- </section>
122
- </section>
123
-
124
- ---
125
-
126
- <section id='português'>
127
- <div align = center>
128
-
129
- ## Português
130
-
131
- </div>
132
-
133
- #### Sumário:
134
-
135
- > 1. [Introdução.](#introdução)
136
- > 2. [Instalação.](#instalação)
137
- > 1. [Dependências](#dependências)
138
- > 2. [Instalação do usuário](#instalação-do-usuário)
139
- > 3. [Exemplos.](#exemplos)
140
-
141
- ---
142
- <section id='introdução'>
143
-
144
- #### Introdução
145
-
146
- O **AISP** é um pacote python que implementa as técnicas dos sistemas imunológicos artificiais, distribuído sob a licença GNU Lesser General Public License v3.0 (LGPLv3).
147
-
148
- O pacote teve início no ano de **2022** como um pacote de pesquisa no instituto federal do norte de minas gerais - campus salinas (**IFNMG - Salinas**).
149
-
150
- Os sistemas imunológicos artificiais (SIA) inspiram-se no sistema imunológico dos vertebrados, criando metáforas que aplicam a capacidade de reconhecer e catalogar os patógenos, entre outras características desse sistema.
151
-
152
- ##### Algoritmos implementados:
153
-
154
- > - [x] [**Seleção Negativa.**](https://ais-package.github.io/docs/aisp-techniques/Negative%20Selection/)
155
- > - [ ] *Algoritmos de Seleção Clonal.*
156
- > - [ ] *Células Dendríticas.*
157
- > - [ ] *Teoria da Rede Imune.*
158
-
159
- </section>
160
-
161
- <section id='introdução'>
162
-
163
- #### **Instalação**
164
-
165
-
166
- O módulo requer a instalação do [python 3.8.10](https://www.python.org/downloads/) ou superior.
167
-
168
- <section id='dependências'>
169
-
170
- ##### **Dependências:**
171
- <div align = center>
172
-
173
- | Pacotes | Versão |
174
- |:-------------:|:-------------:|
175
- | numpy | ≥ 1.22.4 |
176
- | scipy | ≥ 1.8.1 |
177
- | tqdm | ≥ 4.64.1 |
178
-
179
- </div>
180
- </section>
181
-
182
- <section id='instalação-do-usuário'>
183
-
184
- ##### **Instalação do usuário**
185
-
186
- A maneira mais simples de instalação do AISP é utilizando o ``pip``:
187
-
188
- ```Bash
189
- pip install aisp
190
- ```
191
-
192
- </section>
193
-
194
- </section>
195
- <section id='exemplos'>
196
-
197
- #### Exemplos:
198
-
199
- ---
200
-
201
- ##### Exemplo utilizando a técnica de seleção negativa (**nsa**):
202
-
203
- No exemplo presente nesse [notebook](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/example_with_randomly_generated_dataset-pt.ipynb), gerando **500** amostras aleatórias dispostas em dois grupos um para cada classe.
204
-
205
- A seguir alguns exemplos que utiliza-se de base de dados para classificação com a ferramenta [Jupyter notebook](https://jupyter.org/).
206
-
207
- #### **Seleção Negativa:**
208
-
209
- + **RNSA** Aplicação das tecnica de seleção negativa para classificação utilizando a base de dados de flores da família Iris e Old Faithful Geyser:
210
- + [iris_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/iris_dataBase_example_pt-br.ipynb)
211
- + [geyser_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/geyser_dataBase_example_pt-br.ipynb)
212
-
213
- + **BNSA**
214
- + [mushrooms_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/BNSA/mushrooms_dataBase_example_en.ipynb)
215
-
216
-
217
- ---
218
-
219
115
  </section>
220
116
  </section>
@@ -0,0 +1,4 @@
1
+ """Artificial Immune Systems Package"""
2
+
3
+ __author__ = "João Paulo da Silva Barros"
4
+ __version__ = "0.1.35"
@@ -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)
@@ -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"
@@ -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
+ """