Skip to content

Commit

Permalink
Linter fix
Browse files Browse the repository at this point in the history
  • Loading branch information
asyatrhl committed Feb 5, 2024
1 parent 59436ff commit 8219f0c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
27 changes: 16 additions & 11 deletions datasets/samplemotordatalimerick.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def sliding_windows_on_columns_of_2d(array, window_size, overlap_ratio):
for i in range(num_of_cols):
result_list[i, :, :] = SampleMotorDataLimerick.sliding_windows_1d(
array[:, i],
window_size, overlap_ratio
)
window_size, overlap_ratio
)

return result_list

Expand Down Expand Up @@ -162,6 +162,9 @@ def process_file_and_return_signal_windows(self, file_raw_data):

@staticmethod
def create_common_empty_df():
"""
Create empty dataframe
"""
df = pd.DataFrame(columns=SampleMotorDataLimerick.common_dataframe_columns)
return df

Expand All @@ -173,7 +176,9 @@ def parse_ADXL356C_and_return_common_df_row(file_full_path):
"""
df_raw = pd.read_csv(file_full_path, sep=';', header=None)
df_raw.rename(
columns={0: 'Time', 1: 'Voltage_x', 2: 'Voltage_y', 3: 'Voltage_z', 4: 'x', 5: 'y', 6: 'z'}, inplace=True
columns={0: 'Time', 1: 'Voltage_x', 2: 'Voltage_y',
3: 'Voltage_z', 4: 'x', 5: 'y', 6: 'z'},
inplace=True
)
ss_vibr_x1 = df_raw.iloc[0]['x']
ss_vibr_y1 = df_raw.iloc[0]['y']
Expand Down Expand Up @@ -285,8 +290,7 @@ def __create_pkl_files(self):

self.__gen_datasets()

@staticmethod
def normalize_signal(features):
def normalize_signal(self, features):
"""
Normalize signal with Local Min Max Normalization
"""
Expand All @@ -297,9 +301,10 @@ def normalize_signal(features):

for feature in range(features.shape[1]):
for signal in range(features.shape[2]):
features[instance, feature, signal] = \
(features[instance, feature, signal] - instance_min[feature]) / \
features[instance, feature, signal] = (
(features[instance, feature, signal] - instance_min[feature]) /
(instance_max[feature] - instance_min[feature])
)

return features

Expand Down Expand Up @@ -327,7 +332,7 @@ def __gen_datasets(self):
for file in os.listdir(data_dir):
full_path = os.path.join(data_dir, file)

if any(file.startswith(rpm_prefix + SampleMotorDataLimerick.healthy_file_identifier) \
if any(file.startswith(rpm_prefix + SampleMotorDataLimerick.healthy_file_identifier)
for rpm_prefix in selected_rpm_prefixes):
if self.sensor_selected == 'ADXL356C':
healthy_row = SampleMotorDataLimerick.parse_ADXL356C_and_return_common_df_row(
Expand Down Expand Up @@ -390,9 +395,9 @@ def __gen_datasets(self):

anomaly_features = np.asarray(anomaly_features)

train_features = normalize_signal(train_features)
test_normal_features = normalize_signal(test_normal_features)
anomaly_features = normalize_signal(anomaly_features)
train_features = self.normalize_signal(train_features)
test_normal_features = self.normalize_signal(test_normal_features)
anomaly_features = self.normalize_signal(anomaly_features)

# For eliminating filter effects
train_features[:, :, :SampleMotorDataLimerick.num_start_zeros] = 0.5
Expand Down
28 changes: 15 additions & 13 deletions utils/autoencoder_eval_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

DECAY_FACTOR = 1


def calc_model_size(model):
"""
Returns the model's weight anf bias number.
Expand Down Expand Up @@ -71,24 +72,24 @@ def extract_reconstructions_losses(model, dataloader, device):
decay_vector = np.tile(decay_vector, (loss_numpy.shape[0], loss_numpy.shape[1], 1))

decayed_loss = loss_numpy * decay_vector
losses.extend(decayed_loss.mean(axis=(1,2)))
losses.extend(decayed_loss.mean(axis=(1, 2)))
reconstructions.append(model_out)

return reconstructions, losses, inputs, labels


def plot_all_metrics(F1s, BalancedAccuracies, FPRs, Recalls, percentiles):
"""
F1, Balanced Accuracy, False Positive Rate metrics are plotted with respect to
threshold decided according to percentiles of training loss in percentile list.
F1, Balanced Accuracy, False Positive Rate metrics are plotted with respect to
threshold decided according to percentiles of training loss in percentile list.
"""
fontsize=22
fontsize = 22
linewidth = 4

fig, axs = plt.subplots(1, 4, figsize=(36, 11))

axs[0].plot(percentiles, F1s, '-o', linewidth=linewidth)
for i, xy in enumerate(zip(percentiles, F1s)): # pylint: disable=unused-variable
for i, xy in enumerate(zip(percentiles, F1s)): # pylint: disable=unused-variable
axs[0].annotate(f"{xy[1]: .3f}", xy=xy, fontsize=fontsize - 2)

axs[0].grid()
Expand Down Expand Up @@ -118,7 +119,6 @@ def plot_all_metrics(F1s, BalancedAccuracies, FPRs, Recalls, percentiles):
axs[2].tick_params(axis='both', which='both', labelsize=fontsize)
axs[2].legend(("FPR",), loc='lower left', fontsize=fontsize - 2)


axs[3].plot(percentiles, Recalls, '-o', linewidth=linewidth)
for i, xy in enumerate(zip(percentiles, Recalls)):
axs[3].annotate(f"{xy[1]: .3f}", xy=xy, fontsize=fontsize - 2)
Expand All @@ -137,28 +137,30 @@ def plot_all_metrics(F1s, BalancedAccuracies, FPRs, Recalls, percentiles):

def sweep_performance_metrics(thresholds, train_tuple, test_tuple):
"""
F1s, BalancedAccuracies, FPRs, Recalls are calculated
F1s, BalancedAccuracies, FPRs, Recalls are calculated
and returned based on different thresholds.
"""

train_reconstructions, train_losses, train_inputs, train_labels = train_tuple # pylint: disable=unused-variable
test_reconstructions, test_losses, test_inputs, test_labels = test_tuple # pylint: disable=unused-variable
train_reconstructions, train_losses, \
train_inputs, train_labels = train_tuple # pylint: disable=unused-variable
test_reconstructions, test_losses, \
test_inputs, test_labels = test_tuple # pylint: disable=unused-variable

FPRs = []
F1s = []
BalancedAccuracies = []
Recalls = []

for threshold in thresholds:
FPR, TNR, Recall, Precision, Accuracy, F1, BalancedAccuracy = calc_ae_perf_metrics( # pylint: disable=unused-variable
FPR, _, Recall, Precision, Accuracy, F1, BalancedAccuracy = calc_ae_perf_metrics(
test_reconstructions,
test_inputs,
test_labels,
threshold=threshold,
print_all=False
)

FPR_tr, TNR_tr, Rec_tr, Prec_tr, Accuracy_train, F1_tr, B_Acc_tr = calc_ae_perf_metrics( # pylint: disable=unused-variable
_, _, _, _, Accuracy_train, _, _ = calc_ae_perf_metrics(
train_reconstructions,
train_inputs,
train_labels,
Expand All @@ -181,7 +183,7 @@ def sweep_performance_metrics(thresholds, train_tuple, test_tuple):

def calc_ae_perf_metrics(reconstructions, inputs, labels, threshold, print_all=True):
"""
FPR, TNR, Recall, Precision, Accuracy, F1, BalancedAccuracy
FPR, TNR, Recall, Precision, Accuracy, F1, BalancedAccuracy
metrics of AutoEncoder are calculated and returned.
"""

Expand Down Expand Up @@ -214,7 +216,7 @@ def calc_ae_perf_metrics(reconstructions, inputs, labels, threshold, print_all=T
decayed_loss = loss_numpy * decay_vector
decayed_loss = torch.Tensor(decayed_loss).to(label_batch.device)

loss_batch = decayed_loss.mean(dim=(1,2))
loss_batch = decayed_loss.mean(dim=(1, 2))
prediction_batch = loss_batch > threshold

TN += torch.sum(torch.logical_and(torch.logical_not(prediction_batch),
Expand Down

0 comments on commit 8219f0c

Please sign in to comment.