aisp 0.1.41__tar.gz → 0.2.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.
Files changed (33) hide show
  1. {aisp-0.1.41 → aisp-0.2.0}/PKG-INFO +9 -16
  2. {aisp-0.1.41 → aisp-0.2.0}/README.md +8 -15
  3. aisp-0.2.0/aisp/__init__.py +4 -0
  4. {aisp-0.1.41 → aisp-0.2.0}/aisp/base/__init__.py +1 -0
  5. aisp-0.2.0/aisp/base/_classifier.py +116 -0
  6. aisp-0.2.0/aisp/base/mutation.py +86 -0
  7. aisp-0.2.0/aisp/csa/__init__.py +9 -0
  8. aisp-0.2.0/aisp/csa/_ai_immune_recognition_sys.py +506 -0
  9. aisp-0.2.0/aisp/csa/_base.py +119 -0
  10. aisp-0.2.0/aisp/csa/_cell.py +47 -0
  11. {aisp-0.1.41 → aisp-0.2.0}/aisp/exceptions.py +9 -8
  12. {aisp-0.1.41 → aisp-0.2.0}/aisp/nsa/__init__.py +2 -2
  13. {aisp-0.1.41 → aisp-0.2.0}/aisp/nsa/_base.py +35 -27
  14. {aisp-0.1.41 → aisp-0.2.0}/aisp/nsa/_negative_selection.py +177 -156
  15. {aisp-0.1.41 → aisp-0.2.0}/aisp/nsa/_ns_core.py +49 -39
  16. {aisp-0.1.41 → aisp-0.2.0}/aisp/utils/__init__.py +1 -1
  17. aisp-0.2.0/aisp/utils/_multiclass.py +35 -0
  18. aisp-0.2.0/aisp/utils/distance.py +233 -0
  19. {aisp-0.1.41 → aisp-0.2.0}/aisp/utils/metrics.py +10 -8
  20. aisp-0.2.0/aisp/utils/sanitizers.py +64 -0
  21. {aisp-0.1.41 → aisp-0.2.0}/aisp.egg-info/PKG-INFO +9 -16
  22. {aisp-0.1.41 → aisp-0.2.0}/aisp.egg-info/SOURCES.txt +5 -0
  23. {aisp-0.1.41 → aisp-0.2.0}/pyproject.toml +9 -2
  24. aisp-0.1.41/aisp/__init__.py +0 -4
  25. aisp-0.1.41/aisp/base/_classifier.py +0 -110
  26. aisp-0.1.41/aisp/utils/_multiclass.py +0 -29
  27. aisp-0.1.41/aisp/utils/distance.py +0 -215
  28. aisp-0.1.41/aisp/utils/sanitizers.py +0 -55
  29. {aisp-0.1.41 → aisp-0.2.0}/LICENSE +0 -0
  30. {aisp-0.1.41 → aisp-0.2.0}/aisp.egg-info/dependency_links.txt +0 -0
  31. {aisp-0.1.41 → aisp-0.2.0}/aisp.egg-info/requires.txt +0 -0
  32. {aisp-0.1.41 → aisp-0.2.0}/aisp.egg-info/top_level.txt +0 -0
  33. {aisp-0.1.41 → aisp-0.2.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aisp
3
- Version: 0.1.41
3
+ Version: 0.2.0
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>
@@ -79,7 +79,8 @@ Artificial Immune Systems (AIS) are inspired by the vertebrate immune system, cr
79
79
  ##### Algorithms implemented:
80
80
 
81
81
  > - [x] [**Negative Selection.**](https://ais-package.github.io/docs/aisp-techniques/Negative%20Selection/)
82
- > - [ ] *Clonal Selection Algorithms.*
82
+ > - [x] **Clonal Selection Algorithms.**
83
+ > * [AIRS - Artificial Immune Recognition System](https://ais-package.github.io/docs/aisp-techniques/Clonal%20Selection%20Algorithms/)
83
84
  > - [ ] *Dendritic Cells.*
84
85
  > - [ ] *Immune Network Theory.*
85
86
 
@@ -126,22 +127,14 @@ pip install aisp
126
127
 
127
128
  ---
128
129
 
129
- ##### Example using the negative selection technique (**nsa**):
130
+ Explore the example notebooks available in the [AIS-Package/aisp repository](https://github.com/AIS-Package/aisp/tree/main/examples).
131
+ These notebooks demonstrate how to utilize the package's functionalities in various scenarios, including applications of the RNSA,
132
+ BNSA and AIRS algorithms on datasets such as Iris, Geyser, and Mushrooms.
130
133
 
131
- In the example present in this [notebook](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/example_with_randomly_generated_dataset-en.ipynb), **500** random samples were generated, arranged in two groups, one for each class.
134
+ You can run the notebooks directly in your browser without any local installation using Binder:
132
135
 
133
- Below are some examples that use a database for classification with the [Jupyter notebook](https://jupyter.org/) tool.
134
-
135
-
136
- ##### **Negative Selection:**
137
-
138
- + **RNSA** Application of negative selection techniques for classification using the Iris family flower database and Old Faithful Geyser:
139
- + [iris_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/iris_dataBase_example_en.ipynb)
140
- + [geyser_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/geyser_dataBase_example_en.ipynb)
141
- + **BNSA**
142
- + [mushrooms_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/BNSA/mushrooms_dataBase_example_en.ipynb)
143
-
144
- ---
136
+ [![Launch on Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/AIS-Package/aisp/HEAD?labpath=%2Fexamples)
145
137
 
138
+ > 💡 **Tip**: Binder may take a few minutes to load the environment, especially on the first launch.
146
139
  </section>
147
140
  </section>
@@ -49,7 +49,8 @@ Artificial Immune Systems (AIS) are inspired by the vertebrate immune system, cr
49
49
  ##### Algorithms implemented:
50
50
 
51
51
  > - [x] [**Negative Selection.**](https://ais-package.github.io/docs/aisp-techniques/Negative%20Selection/)
52
- > - [ ] *Clonal Selection Algorithms.*
52
+ > - [x] **Clonal Selection Algorithms.**
53
+ > * [AIRS - Artificial Immune Recognition System](https://ais-package.github.io/docs/aisp-techniques/Clonal%20Selection%20Algorithms/)
53
54
  > - [ ] *Dendritic Cells.*
54
55
  > - [ ] *Immune Network Theory.*
55
56
 
@@ -96,22 +97,14 @@ pip install aisp
96
97
 
97
98
  ---
98
99
 
99
- ##### Example using the negative selection technique (**nsa**):
100
+ Explore the example notebooks available in the [AIS-Package/aisp repository](https://github.com/AIS-Package/aisp/tree/main/examples).
101
+ These notebooks demonstrate how to utilize the package's functionalities in various scenarios, including applications of the RNSA,
102
+ BNSA and AIRS algorithms on datasets such as Iris, Geyser, and Mushrooms.
100
103
 
101
- In the example present in this [notebook](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/example_with_randomly_generated_dataset-en.ipynb), **500** random samples were generated, arranged in two groups, one for each class.
104
+ You can run the notebooks directly in your browser without any local installation using Binder:
102
105
 
103
- Below are some examples that use a database for classification with the [Jupyter notebook](https://jupyter.org/) tool.
104
-
105
-
106
- ##### **Negative Selection:**
107
-
108
- + **RNSA** Application of negative selection techniques for classification using the Iris family flower database and Old Faithful Geyser:
109
- + [iris_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/iris_dataBase_example_en.ipynb)
110
- + [geyser_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/RNSA/geyser_dataBase_example_en.ipynb)
111
- + **BNSA**
112
- + [mushrooms_dataBase_example](https://github.com/AIS-Package/aisp/blob/main/examples/BNSA/mushrooms_dataBase_example_en.ipynb)
113
-
114
- ---
106
+ [![Launch on Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/AIS-Package/aisp/HEAD?labpath=%2Fexamples)
115
107
 
108
+ > 💡 **Tip**: Binder may take a few minutes to load the environment, especially on the first launch.
116
109
  </section>
117
110
  </section>
@@ -0,0 +1,4 @@
1
+ """Artificial Immune Systems Package."""
2
+
3
+ __author__ = "João Paulo da Silva Barros"
4
+ __version__ = "0.1.42"
@@ -1,4 +1,5 @@
1
1
  """Base class modules."""
2
+
2
3
  from ._classifier import BaseClassifier
3
4
 
4
5
  __all__ = ['BaseClassifier']
@@ -0,0 +1,116 @@
1
+ """Base class for classification algorithm."""
2
+
3
+ from abc import ABC, abstractmethod
4
+ from typing import Optional, Union
5
+
6
+ import numpy.typing as npt
7
+
8
+ from ..utils import slice_index_list_by_class
9
+ from ..utils.metrics import accuracy_score
10
+
11
+
12
+ class BaseClassifier(ABC):
13
+ """Base class for classification algorithms.
14
+
15
+ Defines the abstract methods ``fit`` and ``predict``, and implements the ``score``,
16
+ ``get_params`` method.
17
+ """
18
+
19
+ classes: Optional[Union[npt.NDArray, list]] = None
20
+
21
+ @abstractmethod
22
+ def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True):
23
+ """
24
+ Train the model using the input data X and corresponding labels y.
25
+
26
+ This abstract method is implemented by the class that inherits it.
27
+
28
+ Parameters
29
+ ----------
30
+ X : npt.NDArray
31
+ Input data used for training the model.
32
+ y : npt.NDArray
33
+ Corresponding labels or target values for the input data.
34
+ verbose : bool, default=True
35
+ Flag to enable or disable detailed output during training.
36
+
37
+ Returns
38
+ -------
39
+ self : BaseClassifier
40
+ Returns the instance of the class that implements this method.
41
+ """
42
+
43
+ @abstractmethod
44
+ def predict(self, X) -> Optional[npt.NDArray]:
45
+ """
46
+ Generate predictions based on the input data X.
47
+
48
+ This abstract method is implemented by the class that inherits it.
49
+
50
+ Parameters
51
+ ----------
52
+ X : npt.NDArray
53
+ Input data for which predictions will be generated.
54
+
55
+ Returns
56
+ -------
57
+ Predictions : Optional[npt.NDArray]
58
+ Predicted values for each input sample, or ``None`` if the prediction fails.
59
+ """
60
+
61
+ def score(self, X: npt.NDArray, y: list) -> float:
62
+ """
63
+ Score function calculates forecast accuracy.
64
+
65
+ Details
66
+ -------
67
+ This function performs the prediction of X and checks how many elements are equal
68
+ between vector y and y_predicted. This function was added for compatibility with some
69
+ scikit-learn functions.
70
+
71
+ Parameters
72
+ ----------
73
+ X : np.ndarray
74
+ Feature set with shape (n_samples, n_features).
75
+ y : np.ndarray
76
+ True values with shape (n_samples,).
77
+
78
+ Returns
79
+ -------
80
+ accuracy : float
81
+ The accuracy of the model.
82
+ """
83
+ if len(y) == 0:
84
+ return 0
85
+ y_pred = self.predict(X)
86
+ return accuracy_score(y, y_pred)
87
+
88
+ def _slice_index_list_by_class(self, y: npt.NDArray) -> dict:
89
+ """Separate the indices of the lines according to the output class.
90
+
91
+ Loop through the sample array only in positions where the output matches the class
92
+ being trained.
93
+
94
+ Parameters
95
+ ----------
96
+ y : npt.NDArray
97
+ Receives a y [``N sample``] array with the output classes of the ``X`` sample array.
98
+
99
+ Returns
100
+ -------
101
+ dict: dict
102
+ A dictionary with the list of array positions(``y``), with the classes as key.
103
+ """
104
+ return slice_index_list_by_class(self.classes, y)
105
+
106
+ def get_params(self, deep: bool = True) -> dict: # pylint: disable=W0613
107
+ """
108
+ Return a dictionary with the object's main parameters.
109
+
110
+ This method is required to ensure compatibility with scikit-learn functions.
111
+ """
112
+ return {
113
+ key: value
114
+ for key, value in self.__dict__.items()
115
+ if not key.startswith("_")
116
+ }
@@ -0,0 +1,86 @@
1
+ """
2
+ The functions perform utilize Numba decorators for Just-In-Time compilation.
3
+
4
+ Contains functions that generate sets of mutated clones from continuous or binary vectors,
5
+ simulating the clonal expansion process in artificial immune systems.
6
+ """
7
+
8
+ import numpy as np
9
+ import numpy.typing as npt
10
+ from numba import njit, types
11
+
12
+
13
+ @njit([(types.float64[:], types.int64)], cache=True)
14
+ def clone_and_mutate_continuous(
15
+ vector: npt.NDArray[np.float64],
16
+ n: int
17
+ ) -> npt.NDArray[np.float64]:
18
+ """
19
+ Generate a set of mutated clones from a cell represented by a continuous vector.
20
+
21
+ This function creates `n` clones of the input vector and applies random mutations to each of
22
+ them, simulating the process of clonal expansion in artificial immune systems. Each clone
23
+ will have a random number of mutations applied in distinct positions of the original vector.
24
+
25
+ Parameters
26
+ ----------
27
+ vector : npt.NDArray[np.float64]
28
+ The original immune cell with continuous values to be cloned and mutated.
29
+ n : int
30
+ The number of mutated clones to be generated.
31
+
32
+ Returns
33
+ -------
34
+ clone_set : npt.NDArray
35
+ An Array(n, len(vector)) containing the `n` mutated clones of the original vector.
36
+ """
37
+ n_features = vector.shape[0]
38
+ clone_set = np.empty((n, n_features), dtype=np.float64)
39
+ for i in range(n):
40
+ n_mutations = np.random.randint(1, n_features)
41
+ clone = vector.copy()
42
+ position_mutations = np.random.permutation(n_features)[:n_mutations]
43
+ for j in range(n_mutations):
44
+ idx = position_mutations[j]
45
+ clone[idx] = np.float64(np.random.random())
46
+ clone_set[i] = clone
47
+
48
+ return clone_set
49
+
50
+
51
+ @njit([(types.boolean[:], types.int64)], cache=True)
52
+ def clone_and_mutate_binary(
53
+ vector: npt.NDArray[np.bool_],
54
+ n: int
55
+ ) -> npt.NDArray[np.bool_]:
56
+ """
57
+ Generate a set of mutated clones from a cell represented by a binary vector.
58
+
59
+ This function creates `n` clones of the input vector and applies random mutations to each of
60
+ them, changing some bits randomly. The process simulates clonal expansion in artificial
61
+ immune systems with discrete representations.
62
+
63
+ Parameters
64
+ ----------
65
+ vector : npt.NDArray[np.bool_]
66
+ The original immune cell with binary values to be cloned and mutated.
67
+ n : int
68
+ The number of mutated clones to be generated.
69
+
70
+ Returns
71
+ -------
72
+ clone_set : npt.NDArray[np.bool_]
73
+ An Array(n, len(vector)) containing the `n` mutated clones of the original vector.
74
+ """
75
+ n_features = vector.shape[0]
76
+ clone_set = np.empty((n, n_features), dtype=np.bool_)
77
+ for i in range(n):
78
+ n_mutations = np.random.randint(1, n_features)
79
+ clone = vector.copy()
80
+ position_mutations = np.random.permutation(n_features)[:n_mutations]
81
+ for j in range(n_mutations):
82
+ idx = position_mutations[j]
83
+ clone[idx] = np.bool_(np.random.randint(0, 2))
84
+ clone_set[i] = clone
85
+
86
+ return clone_set
@@ -0,0 +1,9 @@
1
+ """Module (CSA) Clonal Selection Algorithm.
2
+
3
+ CSAs are inspired by the process of antibody proliferation upon detecting an antigen, during which
4
+ the generated antibodies undergo mutations in an attempt to enhance pathogen recognition.
5
+ """
6
+ from ._ai_immune_recognition_sys import AIRS
7
+
8
+ __author__ = 'João Paulo da Silva Barros'
9
+ __all__ = ['AIRS']