Skip to content

Commit

Permalink
Upgraded some libraries like numpy,pandas and small refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
selimfirat committed Oct 6, 2023
1 parent e06c010 commit 9e4f15f
Show file tree
Hide file tree
Showing 30 changed files with 111 additions and 110 deletions.
3 changes: 2 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
v<0.0.0>, <08/15/2020> -- Initial release.
v<0.1.0>, <08/15/2020> -- Initial release.
v<0.2.0> <06/10/2023> -- Upgrade numpy version and small refactorings
2 changes: 1 addition & 1 deletion examples/example_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
ensembler = AverageScoreEnsembler() # Ensembler module.

for X, y in tqdm(iterator.iter(X_all, y_all)): # Iterate over examples.
model_scores = np.empty(len(models), dtype=np.float)
model_scores = np.empty(len(models), dtype=np.float64)

# Fit & Score via for each model.
for i, model in enumerate(models):
Expand Down
22 changes: 11 additions & 11 deletions pysad/core/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def fit_partial(self, X, y=None):
"""Fits the model to next instance.
Args:
X (np.float array of shape (num_features,)): The instance to fit.
X (np.float64 array of shape (num_features,)): The instance to fit.
y (int): The label of the instance (Optional for unsupervised models, default=None).
Returns:
Expand All @@ -25,7 +25,7 @@ def score_partial(self, X):
"""Scores the anomalousness of the next instance.
Args:
X (np.float array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
X (np.float64 array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
Returns:
float: The anomalousness score of the input instance.
Expand All @@ -36,7 +36,7 @@ def fit_score_partial(self, X, y=None):
"""Applies fit_partial and score_partial to the next instance, respectively.
Args:
X (np.float array of shape (num_features,)): The instance to fit and score.
X (np.float64 array of shape (num_features,)): The instance to fit and score.
y (int): The label of the instance (Optional for unsupervised models, default=None).
Returns:
Expand All @@ -48,7 +48,7 @@ def fit(self, X, y=None):
"""Fits the model to all instances in order.
Args:
X (np.float array of shape (num_instances, num_features)): The instances in order to fit.
X (np.float64 array of shape (num_instances, num_features)): The instances in order to fit.
y (int): The labels of the instances in order to fit (Optional for unsupervised models, default=None).
Returns:
Expand All @@ -63,12 +63,12 @@ def score(self, X):
"""Scores all instaces via score_partial iteratively.
Args:
X (np.float array of shape (num_instances, num_features)): The instances in order to score.
X (np.float64 array of shape (num_instances, num_features)): The instances in order to score.
Returns:
np.float array of shape (num_instances,): The anomalousness scores of the instances in order.
np.float64 array of shape (num_instances,): The anomalousness scores of the instances in order.
"""
y_pred = np.empty(X.shape[0], dtype=np.float)
y_pred = np.empty(X.shape[0], dtype=np.float64)
for i, (xi, _) in enumerate(_iterate(X)):
y_pred[i] = self.score_partial(xi)

Expand All @@ -78,13 +78,13 @@ def fit_score(self, X, y=None):
"""This helper method applies fit_score_partial to all instances in order.
Args:
X (np.float array of shape (num_instances, num_features)): The instances in order to fit.
y (np.int array of shape (num_instances, )): The labels of the instances in order to fit (Optional for unsupervised models, default=None).
X (np.float64 array of shape (num_instances, num_features)): The instances in order to fit.
y (np.int32 array of shape (num_instances, )): The labels of the instances in order to fit (Optional for unsupervised models, default=None).
Returns:
np.float array of shape (num_instances,): The anomalousness scores of the instances in order.
np.float64 array of shape (num_instances,): The anomalousness scores of the instances in order.
"""
y_pred = np.zeros(X.shape[0], dtype=np.float)
y_pred = np.zeros(X.shape[0], dtype=np.float64)
for i, (xi, yi) in enumerate(_iterate(X, y)):
y_pred[i] = self.fit_score_partial(xi, yi)

Expand Down
14 changes: 7 additions & 7 deletions pysad/core/base_postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def transform(self, scores):
"""Shortcut method that iteratively applies transform_partial to all instances in order.
Args:
np.float array of shape (num_instances,): Input scores.
np.float64 array of shape (num_instances,): Input scores.
Returns:
np.float array of shape (num_instances,): Processed scores.
np.float64 array of shape (num_instances,): Processed scores.
"""
processed_scores = np.empty(scores.shape[0], dtype=np.float)
processed_scores = np.empty(scores.shape[0], dtype=np.float64)
for i, (score, _) in enumerate(_iterate(scores)):
processed_scores[i] = self.transform_partial(score)

Expand All @@ -61,7 +61,7 @@ def fit(self, scores):
"""Shortcut method that iteratively applies fit_partial to all instances in order.
Args:
np.float array of shape (num_instances,): Input scores.
np.float64 array of shape (num_instances,): Input scores.
Returns:
object: self.
Expand All @@ -75,12 +75,12 @@ def fit_transform(self, scores):
"""Shortcut method that iteratively applies fit_transform_partial to all instances in order.
Args:
np.float array of shape (num_instances,): Input scores.
np.float64 array of shape (num_instances,): Input scores.
Returns:
np.float array of shape (num_instances,): Processed scores.
np.float64 array of shape (num_instances,): Processed scores.
"""
processed_scores = np.empty(scores.shape[0], dtype=np.float)
processed_scores = np.empty(scores.shape[0], dtype=np.float64)
for i, (score, _) in enumerate(_iterate(scores)):
processed_scores[i] = self.fit_transform_partial(score)

Expand Down
24 changes: 12 additions & 12 deletions pysad/core/base_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def fit_partial(self, X):
"""Fits particular (next) timestep's features to train the transformer.
Args:
X (np.float array of shape (num_components,)): Input feature vector.
X (np.float64 array of shape (num_components,)): Input feature vector.
Returns:
object: self.
"""
Expand All @@ -26,35 +26,35 @@ def transform_partial(self, X):
"""Transforms particular (next) timestep's vector.
Args:
X (np.float array of shape (num_features,)): Input feature vector.
X (np.float64 array of shape (num_features,)): Input feature vector.
Returns:
transformed_X (np.float array of shape (num_components,)): Projected feature vector.
transformed_X (np.float64 array of shape (num_components,)): Projected feature vector.
"""
pass

def fit_transform_partial(self, X):
"""Shortcut method that iteratively applies fit_partial and transform_partial, respectively.
Args:
X (np.float array of shape (num_components,)): Input feature vector.
X (np.float64 array of shape (num_components,)): Input feature vector.
Returns:
transformed_X (np.float array of shape (num_components,)): Projected feature vector.
transformed_X (np.float64 array of shape (num_components,)): Projected feature vector.
"""
return self.fit_partial(X).transform_partial(X)

def transform(self, X):
"""Shortcut method that iteratively applies transform_partial to all instances in order.
Args:
X (np.float array of shape (num_instances, num_features)): Input feature vectors.
X (np.float64 array of shape (num_instances, num_features)): Input feature vectors.
Returns:
np.float array of shape (num_instances, num_components): Projected feature vectors.
np.float64 array of shape (num_instances, num_components): Projected feature vectors.
"""
output_dims = self.output_dims if self.output_dims > 0 else X.shape[1]
transformed_X = np.empty((X.shape[0], output_dims), dtype=np.float)
transformed_X = np.empty((X.shape[0], output_dims), dtype=np.float64)
for i, (xi, _) in enumerate(_iterate(X)):
transformed_X[i] = self.transform_partial(xi)

Expand All @@ -64,7 +64,7 @@ def fit(self, X):
"""Shortcut method that iteratively applies fit_partial to all instances in order.
Args:
X (np.float array of shape (num_instances, num_features)): Input feature vectors.
X (np.float64 array of shape (num_instances, num_features)): Input feature vectors.
Returns:
object: The fitted transformer
Expand All @@ -78,13 +78,13 @@ def fit_transform(self, X):
"""Shortcut method that iteratively applies fit_transform_partial to all instances in order.
Args:
X (np.float array of shape (num_instances, num_components)): Input feature vectors.
X (np.float64 array of shape (num_instances, num_components)): Input feature vectors.
Returns:
np.float array of shape (num_instances, num_components): Projected feature vectors.
np.float64 array of shape (num_instances, num_components): Projected feature vectors.
"""
output_dims = self.output_dims if self.output_dims > 0 else X.shape[1]
transformed_X = np.empty((X.shape[0], output_dims), dtype=np.float)
transformed_X = np.empty((X.shape[0], output_dims), dtype=np.float64)
for i, (xi, _) in enumerate(_iterate(X)):
transformed_X[i] = self.fit_transform_partial(xi)

Expand Down
4 changes: 2 additions & 2 deletions pysad/models/exact_storm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def fit_partial(self, X, y=None):
"""Fits the model to next instance. Simply, adds the instance to the window.
Args:
X (np.float array of shape (num_features,)): The instance to fit.
X (np.float64 array of shape (num_features,)): The instance to fit.
y (int): Ignored since the model is unsupervised (Default=None).
Returns:
Expand All @@ -36,7 +36,7 @@ def score_partial(self, X):
"""Scores the anomalousness of the next instance.
Args:
X (np.float array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
X (np.float64 array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
Returns:
float: The anomalousness score of the input instance.
Expand Down
10 changes: 5 additions & 5 deletions pysad/models/half_space_trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class HalfSpaceTrees(BaseModel):
"""Half-Space Trees method :cite:`tan2011fast`.
Args:
feature_mins (np.float array of shape (num_features,)): Minimum boundary of the features.
feature_maxes (np.float array of shape (num_features,)): Maximum boundary of the features.
feature_mins (np.float64 array of shape (num_features,)): Minimum boundary of the features.
feature_maxes (np.float64 array of shape (num_features,)): Maximum boundary of the features.
window_size (int): The size of the window (Default=100).
num_trees (int): The number of treesint (Default=25).
max_depth (int): Maximum depth of the trees (Default=15).
initial_window_X (np.float array of shape (num_initial_instances,num_features)): The initial window to fit for initial calibration period. If not `None`, we simply apply fit to these instances (Default=None).
initial_window_X (np.float64 array of shape (num_initial_instances,num_features)): The initial window to fit for initial calibration period. If not `None`, we simply apply fit to these instances (Default=None).
"""

def __init__(
Expand Down Expand Up @@ -109,7 +109,7 @@ def fit_partial(self, X, y=None):
"""Fits the model to next instance.
Args:
X (np.float array of shape (num_features,)): The instance to fit.
X (np.float64 array of shape (num_features,)): The instance to fit.
y (int): Ignored since the model is unsupervised (Default=None).
Returns:
Expand Down Expand Up @@ -138,7 +138,7 @@ def score_partial(self, X):
"""Scores the anomalousness of the next instance.
Args:
X (np.float array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
X (np.float64 array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
Returns:
float: The anomalousness score of the input instance.
Expand Down
2 changes: 1 addition & 1 deletion pysad/models/iforest_asd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class IForestASD(ReferenceWindowModel):
"""An Anomaly Detection Approach Based on Isolation Forest Algorithm for Streaming Data using Sliding Window :cite:`ding2013anomaly`. Note that concept drift is not implemented since it is a part of the simulation. See Algorithm 2 in "An Anomaly Detection Approach Based on Isolation Forest Algorithm for Streaming Data using Sliding Window" paper. This method is unsupervised so it is not needed to give y as parameter.
Args:
initial_window_X (np.float array of shape (num_initial_instances,num_features)): The initial window to fit for initial calibration period. We simply apply fit to these instances (Default=None).
initial_window_X (np.float64 array of shape (num_initial_instances,num_features)): The initial window to fit for initial calibration period. We simply apply fit to these instances (Default=None).
window_size (int): The size of the reference window and its sliding (Default=2048).
"""

Expand Down
6 changes: 3 additions & 3 deletions pysad/models/integrations/one_fit_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class OneFitModel(PYODModel):
Args:
model_cls (class): The model class to be instantiated.
initial_X (np.float array of shape (num_initial_instances, num_features)): Initial instances to fit.
initial_y (np.int array of shape (num_initial_instances,): Initial window's ground truth labels. Used if not None. Needs to be `None` for the unsupervised `model_cls` models. (Default=None).
initial_X (np.float64 array of shape (num_initial_instances, num_features)): Initial instances to fit.
initial_y (np.int32 array of shape (num_initial_instances,): Initial window's ground truth labels. Used if not None. Needs to be `None` for the unsupervised `model_cls` models. (Default=None).
**kwargs (Keyword arguments): Keyword arguments that are passed to the `model_cls`.
"""

Expand Down Expand Up @@ -43,7 +43,7 @@ def score_partial(self, X):
"""Scores the anomalousness of the next instance.
Args:
X (np.float array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
X (np.float64 array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
Returns:
float: The anomalousness score of the input instance.
Expand Down
8 changes: 4 additions & 4 deletions pysad/models/integrations/reference_window_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class ReferenceWindowModel(PYODModel):
model_cls (class): The model class to be instantiated.
window_size (int): The size of each window.
sliding_size (int): The sliding length of the windows.
initial_X (np.float array of shape (num_initial_instances, num_features)): Initial instances to fit.
initial_y (np.int array of shape (num_initial_instances,)): Initial window's ground truth labels. Used if not None. Needs to be `None` for the unsupervised `model_cls` models. (Default=None).
initial_X (np.float64 array of shape (num_initial_instances, num_features)): Initial instances to fit.
initial_y (np.int32 array of shape (num_initial_instances,)): Initial window's ground truth labels. Used if not None. Needs to be `None` for the unsupervised `model_cls` models. (Default=None).
**kwargs (Keyword arguments): Keyword arguments that is passed to the `model_cls`.
"""

Expand Down Expand Up @@ -52,7 +52,7 @@ def fit_partial(self, X, y=None):
"""Fits the model to next instance.
Args:
X (np.float array of shape (num_features,)): The instance to fit.
X (np.float64 array of shape (num_features,)): The instance to fit.
y (int): The label of the instance (Optional for unsupervised models, default=None).
Returns:
Expand Down Expand Up @@ -97,7 +97,7 @@ def score_partial(self, X):
"""Scores the anomalousness of the next instance.
Args:
X (np.float array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
X (np.float64 array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
Returns:
float: The anomalousness score of the input instance.
Expand Down
4 changes: 2 additions & 2 deletions pysad/models/kitnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def fit_partial(self, X, y=None):
"""Fits the model to next instance. Simply, adds the instance to the window.
Args:
X (np.float array of shape (num_features,)): The instance to fit.
X (np.float64 array of shape (num_features,)): The instance to fit.
y (int): Ignored since the model is unsupervised (Default=None).
Returns:
Expand All @@ -57,7 +57,7 @@ def score_partial(self, X):
"""Scores the anomalousness of the next instance.
Args:
X (np.float array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
X (np.float64 array of shape (num_features,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
Returns:
float: The anomalousness score of the input instance.
Expand Down
4 changes: 2 additions & 2 deletions pysad/models/knn_cad.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def fit_partial(self, X, y=None):
"""Fits the model to next instance. Note that this model is univariate.
Args:
X (np.float array of shape (1,)): The instance to fit.
X (np.float64 array of shape (1,)): The instance to fit.
y (int): Ignored since the model is unsupervised (Default=None).
Returns:
Expand Down Expand Up @@ -82,7 +82,7 @@ def score_partial(self, X):
"""Scores the anomalousness of the next instance.
Args:
X (np.float array of shape (1,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
X (np.float64 array of shape (1,)): The instance to score. Higher scores represent more anomalous instances whereas lower scores correspond to more normal instances.
Returns:
float: The anomalousness score of the input instance.
Expand Down
Loading

0 comments on commit 9e4f15f

Please sign in to comment.