Skip to content

Commit 370f03a

Browse files
v0.3.21
Adjusts cloning function by range and related notebooks
1 parent 7eb0e8b commit 370f03a

File tree

23 files changed

+85
-75
lines changed

23 files changed

+85
-75
lines changed

aisp/base/mutation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def clone_and_mutate_ranged(
122122
position_mutations = np.random.permutation(n_features)[:n_mutations]
123123
for j in range(n_mutations):
124124
idx = position_mutations[j]
125-
min_limit = bounds[idx, 0]
126-
max_limit = bounds[idx, 1]
125+
min_limit = bounds[0][idx]
126+
max_limit = bounds[1][idx]
127127
clone[idx] = np.random.uniform(min_limit, max_limit)
128128
clone_set[i] = clone
129129

aisp/csa/_ai_recognition_sys.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ class AIRS(BaseAIRS):
111111
Way to calculate the distance between the detector and the sample:
112112
113113
* ``'Euclidean'`` ➜ The calculation of the distance is given by the expression:
114-
√( (x₁ x₂)² + (y₁ y₂)² + ... + (yn yn)²).
114+
√( (x₁ - x₂)² + (y₁ - y₂)² + ... + (yn - yn)²).
115115
116116
* ``'minkowski'`` ➜ The calculation of the distance is given by the expression:
117-
( |X₁ Y₁|p + |X₂ Y₂|p + ... + |Xn Yn|p) ¹/ₚ.
117+
( |X₁ - Y₁|p + |X₂ - Y₂|p + ... + |Xn - Yn|p) ¹/ₚ.
118118
119119
* ``'manhattan'`` ➜ The calculation of the distance is given by the expression:
120-
( |x₁ x₂| + |y₁ y₂| + ... + |yn yn|).
120+
( |x₁ - x₂| + |y₁ - y₂| + ... + |yn - yn|).
121121
122122
seed : int
123123
Seed for the random generation of detector values. Defaults to None.
@@ -139,7 +139,7 @@ class AIRS(BaseAIRS):
139139
140140
References
141141
----------
142-
.. [1] Brabazon, A., ONeill, M., & McGarraghy, S. (2015). Natural Computing Algorithms. In
142+
.. [1] Brabazon, A., O'Neill, M., & McGarraghy, S. (2015). Natural Computing Algorithms. In
143143
Natural Computing Series. Springer Berlin Heidelberg.
144144
https://doi.org/10.1007/978-3-662-43631-8
145145
@@ -195,6 +195,7 @@ def __init__(
195195
self.affinity_threshold = 0.0
196196
self.classes = []
197197
self._bounds: Optional[npt.NDArray[np.float64]] = None
198+
self._n_features: Optional[int] = None
198199

199200
@property
200201
def cells_memory(self) -> Optional[Dict[str, list[Cell]]]:
@@ -235,6 +236,7 @@ def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True) -> AIRS:
235236
self._bounds = np.vstack([np.min(X, axis=0), np.max(X, axis=0)])
236237

237238
self.classes = np.unique(y)
239+
self._n_features = X.shape[1]
238240
sample_index = self._slice_index_list_by_class(y)
239241
progress = tqdm(
240242
total=len(y),
@@ -330,11 +332,11 @@ def predict(self, X: npt.NDArray) -> Optional[npt.NDArray]:
330332
An ndarray of the form ``C`` [``N samples``], containing the predicted classes for
331333
``X``. or ``None``: If there are no detectors for the prediction.
332334
"""
333-
if self._all_class_cell_vectors is None:
335+
if self._all_class_cell_vectors is None or self._n_features is None:
334336
return None
335337

336338
super()._check_and_raise_exceptions_predict(
337-
X, len(self._cells_memory[self.classes[0]][0].vector), self._feature_type
339+
X, self._n_features, self._feature_type
338340
)
339341

340342
c: list = []
@@ -380,7 +382,7 @@ def _refinement_arb(
380382
381383
References
382384
----------
383-
.. [1] Brabazon, A., ONeill, M., & McGarraghy, S. (2015).
385+
.. [1] Brabazon, A., O'Neill, M., & McGarraghy, S. (2015).
384386
Natural Computing Algorithms. Natural Computing Series.
385387
Springer Berlin Heidelberg. https://doi.org/10.1007/978-3-662-43631-8
386388
"""
@@ -441,7 +443,7 @@ def _cells_affinity_threshold(self, antigens_list: npt.NDArray):
441443
distances = pdist(antigens_list, metric="hamming")
442444
else:
443445
metric_kwargs = {'p': self.p} if self.metric == 'minkowski' else {}
444-
distances = pdist(antigens_list, metric=self.metric, **metric_kwargs)
446+
distances = pdist(antigens_list, metric=self.metric, **metric_kwargs) # type: ignore
445447

446448
n = antigens_list.shape[0]
447449
sum_affinity = np.sum(1.0 - (distances / (1.0 + distances)))

aisp/csa/tests/test_ai_recognition_sys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pylint: disable=redefined-outer-name
2-
"""Tests for classes implementing negative selection."""
2+
"""Tests for classes implementing Artificial Immune Recognition System."""
33

44
import numpy as np
55
import pytest

aisp/ina/_ai_network.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class AiNet(BaseAiNet):
2929
clustering and data compression tasks. The aiNet algorithm uses principles from immune
3030
network theory, clonal selection, and affinity maturation to compress high-dimensional
3131
datasets. [1]_
32-
For clustering, the class uses SciPys implementation of the **Minimum Spanning Tree**
32+
For clustering, the class uses SciPy's implementation of the **Minimum Spanning Tree**
3333
(MST) to remove the most distant nodes and separate the groups. [2]_
3434
3535
Parameters
@@ -58,13 +58,13 @@ class AiNet(BaseAiNet):
5858
Way to calculate the distance between the detector and the sample:
5959
6060
* ``'Euclidean'`` ➜ The calculation of the distance is given by the expression:
61-
√( (x₁ x₂)² + (y₁ y₂)² + ... + (yn yn)²).
61+
√( (x₁ - x₂)² + (y₁ - y₂)² + ... + (yn - yn)²).
6262
6363
* ``'minkowski'`` ➜ The calculation of the distance is given by the expression:
64-
( |X₁ Y₁|p + |X₂ Y₂|p + ... + |Xn Yn|p) ¹/ₚ.
64+
( |X₁ - Y₁|p + |X₂ - Y₂|p + ... + |Xn - Yn|p) ¹/ₚ.
6565
6666
* ``'manhattan'`` ➜ The calculation of the distance is given by the expression:
67-
( |x₁ x₂| + |y₁ y₂| + ... + |yn yn|).
67+
( |x₁ - x₂| + |y₁ - y₂| + ... + |yn - yn|).
6868
6969
seed : Optional[int]
7070
Seed for the random generation of detector values. Defaults to None.
@@ -455,7 +455,7 @@ def _calculate_affinities(self, u: npt.NDArray, v: npt.NDArray) -> npt.NDArray:
455455
"""
456456
u = np.reshape(u, (1, -1))
457457
v = np.atleast_2d(v)
458-
distances = cdist(u, v, metric=self.metric, **self._metric_params)[0]
458+
distances = cdist(u, v, metric=self.metric, **self._metric_params)[0] # type: ignore
459459

460460
return 1 - (distances / (1 + distances))
461461

@@ -523,6 +523,8 @@ def update_clusters(self, mst_inconsistency_factor: Optional[float] = None):
523523
------
524524
ValueError
525525
If the Minimum Spanning Tree (MST) has not yet been created
526+
If Population of antibodies is empty
527+
If MST statistics (mean or std) are not available.
526528
527529
Updates
528530
-------
@@ -534,6 +536,12 @@ def update_clusters(self, mst_inconsistency_factor: Optional[float] = None):
534536
if self._mst_structure is None:
535537
raise ValueError("The Minimum Spanning Tree (MST) has not yet been created.")
536538

539+
if self._population_antibodies is None or len(self._population_antibodies) == 0:
540+
raise ValueError("Population of antibodies is empty")
541+
542+
if self._mst_mean_distance is None or self._mst_std_distance is None:
543+
raise ValueError("MST statistics (mean or std) are not available.")
544+
537545
if mst_inconsistency_factor is not None:
538546
self.mst_inconsistency_factor = mst_inconsistency_factor
539547

aisp/nsa/_binary_negative_selection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,4 @@ def __assign_class_to_non_self_sample(self, line: npt.NDArray, c: list):
237237
else:
238238
class_differences[_class_] = distances.sum() / self.N
239239

240-
c.append(max(class_differences, key=class_differences.get))
240+
c.append(max(class_differences, key=class_differences.get)) # type: ignore

aisp/nsa/_negative_selection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ class RNSA(BaseNSA):
4040
Way to calculate the distance between the detector and the sample:
4141
4242
+ ``'Euclidean'`` ➜ The calculation of the distance is given by the expression:
43-
√( (x₁ x₂)² + (y₁ y₂)² + ... + (yn yn)²).
43+
√( (x₁ - x₂)² + (y₁ - y₂)² + ... + (yn - yn)²).
4444
+ ``'minkowski'`` ➜ The calculation of the distance is given by the expression:
45-
( |X₁ Y₁|p + |X₂ Y₂|p + ... + |Xn Yn|p) ¹/ₚ.
45+
( |X₁ - Y₁|p + |X₂ - Y₂|p + ... + |Xn - Yn|p) ¹/ₚ.
4646
+ ``'manhattan'`` ➜ The calculation of the distance is given by the expression:
47-
( |x₁ x₂| + |y₁ y₂| + ... + |yn yn|) .
47+
( |x₁ - x₂| + |y₁ - y₂| + ... + |yn - yn|) .
4848
max_discards : int, default=1000
4949
This parameter indicates the maximum number of consecutive detector discards, aimed at
5050
preventing a possible infinite loop in case a radius is defined that cannot generate
@@ -260,7 +260,7 @@ def predict(self, X: npt.NDArray) -> Optional[npt.NDArray]:
260260
average_distance[_class_] = np.average(
261261
[self.__distance(detector, line) for detector in detectores]
262262
)
263-
c.append(max(average_distance, key=average_distance.get))
263+
c.append(max(average_distance, key=average_distance.get)) # type: ignore
264264
return np.array(c)
265265

266266
def __checks_valid_detector(

aisp/utils/distance.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def hamming(u: npt.NDArray[np.bool_], v: npt.NDArray[np.bool_]) -> float64:
4040
def euclidean(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> float64:
4141
"""Calculate the normalized Euclidean distance between two points.
4242
43-
√( (x₁ x₂)² + (y₁ y₂)² + ... + (yn yn)²)
43+
√( (x₁ - x₂)² + (y₁ - y₂)² + ... + (yn - yn)²)
4444
4545
Parameters
4646
----------
@@ -61,7 +61,7 @@ def euclidean(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> float64
6161
def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64:
6262
"""Calculate the normalized Manhattan distance between two points.
6363
64-
(|x₁ x₂| + |y₁ y₂| + ... + |yn yn|) / n
64+
(|x₁ - x₂| + |y₁ - y₂| + ... + |yn - yn|) / n
6565
6666
Parameters
6767
----------
@@ -86,7 +86,7 @@ def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64:
8686
def minkowski(u: npt.NDArray[float64], v: npt.NDArray[float64], p: float = 2.0) -> float64:
8787
"""Calculate the normalized Minkowski distance between two points.
8888
89-
(( |X₁ Y₁|p + |X₂ Y₂|p + ... + |Xn Yn|p) ¹/ₚ.) / n
89+
(( |X₁ - Y₁|p + |X₂ - Y₂|p + ... + |Xn - Yn|p) ¹/ₚ.) / n
9090
9191
Parameters
9292
----------
@@ -124,7 +124,7 @@ def compute_metric_distance(
124124
u: npt.NDArray[float64],
125125
v: npt.NDArray[float64],
126126
metric: int,
127-
p: float64 = 2.0
127+
p: float = 2.0
128128
) -> float64:
129129
"""Calculate the distance between two points by the chosen metric.
130130

aisp/utils/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
MetricType : Literal["manhattan", "minkowski", "euclidean"]
1313
Specifies the distance metric to use for calculations. Possible values:
1414
- "manhattan": The calculation of the distance is given by the expression:
15-
√( (x₁ x₂)² + (y₁ y₂)² + ... + (yn yn)²).
15+
√( (x₁ - x₂)² + (y₁ - y₂)² + ... + (yn - yn)²).
1616
- "minkowski": The calculation of the distance is given by the expression:
17-
( |X₁ Y₁|p + |X₂ Y₂|p + ... + |Xn Yn|p) ¹/ₚ.
17+
( |X₁ - Y₁|p + |X₂ - Y₂|p + ... + |Xn - Yn|p) ¹/ₚ.
1818
- "euclidean": The calculation of the distance is given by the expression:
19-
( |x₁ x₂| + |y₁ y₂| + ... + |yn yn|).
19+
( |x₁ - x₂| + |y₁ - y₂| + ... + |yn - yn|).
2020
"""
2121

2222

docs/en/advanced-guides/utils.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def euclidean(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> np.floa
136136

137137
Function to calculate the normalized Euclidean distance between two points.
138138

139-
$√( (x₁ x₂)² + (y₁ y₂)² + ... + (yn yn)²)$
139+
$√( (x₁ - x₂)² + (y₁ - y₂)² + ... + (yn - yn)²)$
140140

141141

142142

@@ -157,7 +157,7 @@ def cityblock(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> np.floa
157157

158158
Function to calculate the normalized Manhattan distance between two points.
159159

160-
$(|x₁ x₂| + |y₁ y₂| + ... + |yn yn|) / n$
160+
$(|x₁ - x₂| + |y₁ - y₂| + ... + |yn - yn|) / n$
161161

162162

163163
**Parameters:**
@@ -177,7 +177,7 @@ def minkowski(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64], p: float =
177177

178178
Function to calculate the normalized Minkowski distance between two points.
179179

180-
$(( |X₁ Y₁|p + |X₂ Y₂|p + ... + |Xn Yn|p) ¹/ₚ) / n$
180+
$(( |X₁ - Y₁|p + |X₂ - Y₂|p + ... + |Xn - Yn|p) ¹/ₚ) / n$
181181

182182

183183
**Parameters:**

docs/en/classes/Clonal Selection Algorithms/AIRS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ Related and noteworthy works: access here [2](#ref2).
2323
* **resource_amplified** (``float``): Resource consumption amplifier is multiplied with the incentive to subtract resources. Defaults to 1.0 without amplification.
2424
* **metric** (Literal["manhattan", "minkowski", "euclidean"]): Way to calculate the distance between the detector and the sample:
2525
* ``'Euclidean'`` ➜ The calculation of the distance is given by the expression:
26-
√( (x₁ x₂)² + (y₁ y₂)² + ... + (yn yn)²).
26+
√( (x₁ - x₂)² + (y₁ - y₂)² + ... + (yn - yn)²).
2727
* ``'minkowski'`` ➜ The calculation of the distance is given by the expression:
28-
( |X₁ Y₁|p + |X₂ Y₂|p + ... + |Xn Yn|p) ¹/ₚ.
28+
( |X₁ - Y₁|p + |X₂ - Y₂|p + ... + |Xn - Yn|p) ¹/ₚ.
2929
* ``'manhattan'`` ➜ The calculation of the distance is given by the expression:
30-
( |x₁ x₂| + |y₁ y₂| + ... + |yn yn|).
30+
( |x₁ - x₂| + |y₁ - y₂| + ... + |yn - yn|).
3131
Defaults to "Euclidean".
3232

3333
* **seed** (int): Seed for the random generation of detector values. Defaults to None.

0 commit comments

Comments
 (0)