Skip to content

Commit

Permalink
feat(JAQPOT-380): new doa methods (#131)
Browse files Browse the repository at this point in the history
* chore: updated domain example

* feat: mahalanobis and kernel density DOA

* feat: Implemented CityBlock DOA class

* chrore: added cityblock in doa example

* fix: Stabilized matrix inversion

* test: Implemented tests for the three new DOA classes

* fix: Connection to API
  • Loading branch information
periklis91 authored Dec 3, 2024
1 parent 830984d commit 5f8200c
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions jaqpotpy/doa/doa.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,9 @@ def predict(self, new_data: Union[np.array, pd.DataFrame]) -> Iterable[Any]:

for nd in new_data:
distance = self.calculate_distance(nd)
in_ad = distance <= self._threshold
in_ad = False
if distance <= self._threshold:
in_ad = True

doa = {
"mahalanobisDistance": distance,
Expand All @@ -475,7 +477,7 @@ def get_attributes(self):
"""
mahalanobis_data = MahalanobisDoa(
mean_vector=self._mean_vector,
cov_matrix=self._cov_matrix,
cov_matrix=self._inv_cov_matrix,
threshold=self._threshold,
)
return mahalanobis_data.to_dict()
Expand All @@ -494,7 +496,7 @@ def __name__(self):

def __init__(
self,
kernel_type="gaussian",
kernel_type="GAUSSIAN",
threshold_method="percentile",
threshold_percentile=5,
sigma=None,
Expand All @@ -517,7 +519,7 @@ def __init__(
Raises:
ValueError: If parameters do not meet specified constraints.
"""
valid_kernel_types = ["gaussian", "rbf", "laplacian"]
valid_kernel_types = ["GAUSSIAN", "RBF", "LAPLACIAN"]
if kernel_type not in valid_kernel_types:
raise ValueError(
f"Invalid kernel type. Must be one of {valid_kernel_types}"
Expand Down Expand Up @@ -655,6 +657,8 @@ def predict(self, new_data: Union[np.ndarray, pd.DataFrame]) -> Iterable[dict]:
kernel_func = self._select_kernel()

doa_results = []
if isinstance(self._data, list):
self._data = np.array(self._data) # Convert to NumPy array
for point in new_data:
# Compute kernel distances between the new point and all training data points
point_distances = [
Expand All @@ -664,12 +668,14 @@ def predict(self, new_data: Union[np.ndarray, pd.DataFrame]) -> Iterable[dict]:

# Calculate average kernel distance
avg_distance = np.mean(point_distances)

in_ad = False
if avg_distance >= self._threshold:
in_ad = True
doa_results.append(
{
"kernelDistance": avg_distance,
"threshold": self._threshold,
"inDoa": avg_distance >= self._threshold,
"inDoa": in_ad,
}
)

Expand All @@ -682,7 +688,15 @@ def get_attributes(self):
Returns:
KernelDOAAttributes: Attributes of the Kernel DOA.
"""
kernel_data = KernelBasedDoa(sigma=self._sigma, threshold=self._threshold)
kernel_data = KernelBasedDoa(
sigma=self._sigma,
gamma=self._gamma,
threshold=self._threshold,
kernel_type=self._kernel_type,
threshold_method=self._threshold_method,
threshold_percentile=self._threshold_percentile,
data_points=self._data,
)
return kernel_data.to_dict()


Expand Down Expand Up @@ -769,7 +783,9 @@ def predict(self, new_data: Union[np.array, pd.DataFrame]) -> Iterable[Any]:

for nd in new_data:
distance = self.calculate_distance(nd)
in_ad = distance <= self._threshold
in_ad = False
if distance <= self._threshold:
in_ad = True

doa = {
"cityBlockDistance": distance,
Expand Down

0 comments on commit 5f8200c

Please sign in to comment.