Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion aisp/base/core/_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ def get_report(self) -> str:
return "".join(report_parts)

@abstractmethod
def optimize(self, max_iters: int = 50, n_iter_no_change=10, verbose: bool = True) -> Any:
def optimize(
self,
max_iters: int = 50,
n_iter_no_change: int = 10,
verbose: bool = True
) -> Any:
"""Execute the optimization process.

This abstract method must be implemented by the subclass, defining
Expand Down
5 changes: 4 additions & 1 deletion aisp/csa/_ai_recognition_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,10 @@ def predict(self, X: Union[npt.NDArray, list]) -> npt.NDArray:
)

def _refinement_arb(
self, ai: npt.NDArray, c_match_stimulation: float, arb_list: List[_ARB]
self,
ai: npt.NDArray,
c_match_stimulation: float,
arb_list: List[_ARB]
) -> _ARB:
"""
Refine the ARB set until the average stimulation exceeds the defined threshold.
Expand Down
2 changes: 1 addition & 1 deletion aisp/ina/_ai_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def fit(self, X: Union[npt.NDArray, list], verbose: bool = True) -> AiNet:

Parameters
----------
X : npt.NDArray
X : Union[npt.NDArray, list]
Input data used for training the model.
verbose : bool, default=True
Feedback from the progress bar showing current training interaction details.
Expand Down
4 changes: 3 additions & 1 deletion aisp/nsa/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

@njit([(types.boolean[:, :], types.boolean[:], types.float64)], cache=True)
def check_detector_bnsa_validity(
x_class: npt.NDArray[np.bool_], vector_x: npt.NDArray[np.bool_], aff_thresh: float
x_class: npt.NDArray[np.bool_],
vector_x: npt.NDArray[np.bool_],
aff_thresh: float
) -> bool:
"""
Check the validity of a candidate detector using the Hamming distance.
Expand Down
10 changes: 7 additions & 3 deletions aisp/nsa/_negative_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ def predict(self, X: Union[npt.NDArray, list]) -> npt.NDArray:
return np.array(c)

def _checks_valid_detector(
self, x_class: npt.NDArray, vector_x: npt.NDArray
self,
x_class: npt.NDArray,
vector_x: npt.NDArray
) -> Union[bool, tuple[bool, float]]:
"""
Check if the detector has a valid non-proper r radius for the class.
Expand Down Expand Up @@ -344,7 +346,7 @@ def _compare_knearest_neighbors_list(self, knn: list, distance: float) -> None:

Parameters
----------
knn : npt.NDArray
knn : list
List of k-nearest neighbor distances.
distance : float
Distance to check.
Expand Down Expand Up @@ -426,7 +428,9 @@ def _distance(self, u: npt.NDArray, v: npt.NDArray) -> float:
return compute_metric_distance(u, v, get_metric_code(self.metric), self.p)

def _detector_is_valid_to_vdetector(
self, distance: float, vector_x: npt.NDArray
self,
distance: float,
vector_x: npt.NDArray
) -> Union[bool, tuple[bool, float]]:
"""Validate the detector against the vdetector.

Expand Down
15 changes: 10 additions & 5 deletions aisp/utils/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def hamming(u: npt.NDArray[np.bool_], v: npt.NDArray[np.bool_]) -> float64:
if n == 0:
return float64(0.0)

return np.float64(np.sum(u != v) / n)
return float64(np.sum(u != v) / n)


@njit()
Expand Down Expand Up @@ -84,7 +84,9 @@ def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64:

@njit()
def minkowski(
u: npt.NDArray[float64], v: npt.NDArray[float64], p: float = 2.0
u: npt.NDArray[float64],
v: npt.NDArray[float64],
p: float = 2.0
) -> float64:
"""Calculate the normalized Minkowski distance between two points.

Expand All @@ -99,8 +101,8 @@ def minkowski(
p : float
The p parameter defines the type of distance to be calculated:

- p = 1: **Manhattan** distance sum of absolute differences.
- p = 2: **Euclidean** distance sum of squared differences (square root).
- p = 1: **Manhattan** distance - sum of absolute differences.
- p = 2: **Euclidean** distance - sum of squared differences (square root).
- p > 2: **Minkowski** distance with an increasing penalty as p increases.

Returns
Expand All @@ -117,7 +119,10 @@ def minkowski(

@njit([(types.float64[:], types.float64[:], types.int32, types.float64)], cache=True)
def compute_metric_distance(
u: npt.NDArray[float64], v: npt.NDArray[float64], metric: int, p: float = 2.0
u: npt.NDArray[float64],
v: npt.NDArray[float64],
metric: int,
p: float = 2.0
) -> float64:
"""Calculate the distance between two points by the chosen metric.

Expand Down
3 changes: 2 additions & 1 deletion aisp/utils/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@


def accuracy_score(
y_true: Union[npt.NDArray, list], y_pred: Union[npt.NDArray, list]
y_true: Union[npt.NDArray, list],
y_pred: Union[npt.NDArray, list]
) -> float:
"""Calculate the accuracy score based on true and predicted labels.

Expand Down
42 changes: 28 additions & 14 deletions docs/en/advanced-guides/Core/Negative Selection.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ The functions perform detector checks and utilize Numba decorators for Just-In-T
### Function check_detector_bnsa_validity(...)

```python
@njit([(types.boolean[:, :], types.boolean[:], types.float64)], cache=True)
def check_detector_bnsa_validity(
x_class: npt.NDArray,
vector_x: npt.NDArray,
x_class: npt.NDArray[np.bool_],
vector_x: npt.NDArray[np.bool_],
aff_thresh: float
) -> bool:
```
Expand All @@ -16,8 +17,8 @@ Checks the validity of a candidate detector (vector_x) against samples from a cl

**Parameters:**

* **x_class** (``npt.NDArray``): Array containing the class samples. Expected shape: (n_samples, n_features).
* **vector_x** (``npt.NDArray``): Array representing the detector. Expected shape: (n_features,).
* **x_class** (``npt.NDArray[np.bool_]``): Array containing the class samples. Expected shape: (n_samples, n_features).
* **vector_x** (``npt.NDArray[np.bool_]``): Array representing the detector. Expected shape: (n_features,).
* **aff_thresh** (``float``): Affinity threshold.

**returns**:
Expand All @@ -29,19 +30,20 @@ Checks the validity of a candidate detector (vector_x) against samples from a cl
### Function bnsa_class_prediction(...)

```python
@njit([(types.boolean[:], types.boolean[:, :, :], types.float64)], cache=True)
def bnsa_class_prediction(
features: npt.NDArray,
class_detectors: npt.NDArray,
aff_thresh: float
features: npt.NDArray[np.bool_],
class_detectors: npt.NDArray[np.bool_],
aff_thresh: float,
) -> int:
```

Defines the class of a sample from the non-self detectors.

**Parameters:**

* **features** (``npt.NDArray``): binary sample to be classified (shape: [n_features]).
* **class_detectors** (``npt.NDArray``): Array containing the detectors of all classes
* **features** (``npt.NDArray[np.bool_]``): binary sample to be classified (shape: [n_features]).
* **class_detectors** (``npt.NDArray[np.bool_]``): Array containing the detectors of all classes
Shape: (n_classes, n_detectors, n_features).
* **aff_thresh** (``float``): Affinity threshold that determines whether a detector recognizes the sample as non-self.

Expand All @@ -54,21 +56,33 @@ Shape: (n_classes, n_detectors, n_features).
### Function check_detector_rnsa_validity(...)

```python
@njit(
[
(
types.float64[:, :],
types.float64[:],
types.float64,
types.int32,
types.float64,
)
],
cache=True,
)
def check_detector_rnsa_validity(
x_class: npt.NDArray,
vector_x: npt.NDArray,
x_class: npt.NDArray[np.float64],
vector_x: npt.NDArray[np.float64],
threshold: float,
metric: int,
p: float
p: float,
) -> bool:
```

Checks the validity of a candidate detector (vector_x) against samples from a class (x_class) using the Hamming distance. A detector is considered INVALID if its distance to any sample in ``x_class`` is less than or equal to ``aff_thresh``.

**Parameters:**

* **x_class** (``npt.NDArray``): Array containing the class samples. Expected shape: (n_samples, n_features).
* **vector_x** (``npt.NDArray``): Array representing the detector. Expected shape: (n_features,).
* **x_class** (``npt.NDArray[np.float64]``): Array containing the class samples. Expected shape: (n_samples, n_features).
* **vector_x** (``npt.NDArray[np.float64]``): Array representing the detector. Expected shape: (n_features,).
* **threshold** (``float``): threshold.
* **metric** (``int``): Distance metric to be used. Available options: 0 (Euclidean), 1 (Manhattan), 2 (Minkowski)
* **p** (``float``): Parameter for the Minkowski distance (used only if `metric` is "minkowski").
Expand Down
2 changes: 1 addition & 1 deletion docs/en/advanced-guides/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Provides the `get_params` and `set_params` methods for compatibility with the sc
### Method `set_params(...)`

```python
def set_params(self, **params)
def set_params(self, **params) -> Base:
```

Set the parameters of the instance. Ensures compatibility with scikit-learn functions.
Expand Down
19 changes: 14 additions & 5 deletions docs/en/advanced-guides/base/classifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ Base class for classification algorithms, defining the abstract methods ``fit``
### Method `score(...)`

```python
def score(self, X: npt.NDArray, y: list) -> float
def score(
self,
X: Union[npt.NDArray, list],
y: Union[npt.NDArray, list]
) -> float:
```

Score function calculates forecast accuracy.
Expand All @@ -17,8 +21,8 @@ This function was added for compatibility with some scikit-learn functions.

**Parameters:**

* **X** (`npt.NDArray`): Feature set with shape (n_samples, n_features).
* **y** (`list`): True values with shape (n_samples,).
* **X** (`Union[npt.NDArray, list]`): Feature set with shape (n_samples, n_features).
* **y** (`Union[npt.NDArray, list]`): True values with shape (n_samples,).

**Returns**:

Expand All @@ -44,7 +48,12 @@ Returns a dictionary with the classes as key and the indices in ``X`` of the sam

```python
@abstractmethod
def fit(self, X: npt.NDArray, y: npt.NDArray, verbose: bool = True) -> BaseClassifier:
def fit(
self,
X: Union[npt.NDArray, list],
y: Union[npt.NDArray, list],
verbose: bool = True
) -> BaseClassifier:
```

Fit the model to the training data.
Expand All @@ -59,7 +68,7 @@ Implementation:

```python
@abstractmethod
def predict(self, X) -> Optional[npt.NDArray]:
def predict(self, X: Union[npt.NDArray, list]) -> npt.NDArray:
```

Performs label prediction for the given data.
Expand Down
16 changes: 8 additions & 8 deletions docs/en/advanced-guides/base/clusterer.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ the implementation of the **`fit`** and **`predict`** methods in all derived cla

```python
@abstractmethod
def fit(self, X: npt.NDArray, verbose: bool = True) -> BaseClusterer:
def fit(self, X: Union[npt.NDArray, list], verbose: bool = True) -> BaseClusterer:
```

Fit the model to the training data.
This abstract method must be implemented by subclasses.

**Parameters**:

* **X** (`npt.NDArray`): Input data used for training the model.
* **X** (`Union[npt.NDArray, list]`): Input data used for training the model.
* **verbose** (`bool`, default=True): Flag to enable or disable detailed output during training.

**Returns**:
Expand All @@ -36,19 +36,19 @@ This abstract method must be implemented by subclasses.

```python
@abstractmethod
def predict(self, X: npt.NDArray) -> Optional[npt.NDArray]:
def predict(self, X: Union[npt.NDArray, list]) -> npt.NDArray:
```

Generate predictions based on the input data.
This abstract method must be implemented by subclasses.

**Parameters**:

* **X** (`npt.NDArray`): Input data for which predictions will be generated.
* **X** (`Union[npt.NDArray, list]`): Input data for which predictions will be generated.

**Returns**:

* **predictions** (`Optional[npt.NDArray]`): Predicted cluster labels for each input sample, or `None` if prediction is not possible.
* **predictions** (`npt.NDArray`): Predicted cluster labels for each input sample.

**Implementation**:

Expand All @@ -59,16 +59,16 @@ This abstract method must be implemented by subclasses.
### Method `fit_predict(...)`

```python
def fit_predict(self, X: npt.NDArray, verbose: bool = True) -> Optional[npt.NDArray]
def fit_predict(self, X: Union[npt.NDArray, list], verbose: bool = True) -> npt.NDArray:
```

Convenience method that combines `fit` and `predict` in a single call.

**Parameters**:

* **X** (`npt.NDArray`): Input data for which predictions will be generated.
* **X** (`Union[npt.NDArray, list]`): Input data for which predictions will be generated.
* **verbose** (`bool`, default=True): Flag to enable or disable detailed output during training.

**Returns**:

* **predictions**: `Optional[npt.NDArray]` - Predicted cluster labels for each input sample, or `None` if prediction is not possible.
* **predictions**: `npt.NDArray` - Predicted cluster labels for each input sample.
7 changes: 6 additions & 1 deletion docs/en/advanced-guides/base/optimizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ Reset the object's internal state, clearing history and resetting values.

```python
@abstractmethod
def optimize(self, max_iters: int = 50, n_iter_no_change=10, verbose: bool = True) -> Any
def optimize(
self,
max_iters: int = 50,
n_iter_no_change: int = 10,
verbose: bool = True
) -> Any:
```

Execute the optimization process. This method must be implemented by the subclass to define how the optimization strategy explores the search space.
Expand Down
Loading