Page MenuHomec4science

Features PSD.py
No OneTemporary

File Metadata

Created
Thu, Feb 13, 00:37

Features PSD.py

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 29 23:08:34 2021
@author: srpv
contact: vigneashwara.solairajapandiyan@empa.ch
The codes in this following script will be used for the publication of the following work
"Dynamics of in-situ alloying of Ti6Al4V-Fe by means of acoustic emission monitoring
supported by operando synchrotron X-ray diffraction"
@any reuse of this code should be authorized by the first owner, code author
"""
# %%
# Librariies to import
import numpy as np
from Utils_PSD import *
from Utils_PSD_Viz import *
import os
'''
This is a custom script to compute the PSD and visualize it'''
# %%
# point to the folder based on Dataprep_Whole.py
dataset = 'CM'
path = r'C:\Users\srpv\Desktop\Sinergia Offline\Reza\Code\Pre_processed Dataset'
sample_rate = 1500000 # rate at which the acquistion happend.
band_size = 7 # Number equal sized windows to be computed. --> its 0- sampling_rate//2
frequency_bandwidth = 15000 # frequency window length interested
band_max_size = (band_size+1) * 10000
r = ['Ti64', 'Ti64_3Fe', 'Ti64_6Fe'] # --> Total number of classes/ Categories
for sensor in r:
dataset_name = path+'/'+str(dataset)+'-'+str(sensor)+'_raw.npy'
print(dataset_name)
data = np.load(dataset_name)
featurelist = PSD_function(data, sample_rate, band_size, band_max_size)
Featurespace = np.asarray(featurelist)
Featurespace = Featurespace.astype(np.float64)
data_filename = str(dataset)+'-'+str(sensor)+'_PSD'+'.npy'
np.save(os.path.join(path, data_filename), Featurespace, allow_pickle=True)
# %%
Featurespace = np.zeros((0, band_size)) # size of the PSD features computed
classspace = []
'''
Looping all the PSD files corresponding to respective classes into a single data frame
'''
for sensor in r:
dataset_name = path+'/'+str(dataset)+'-'+str(sensor)+'_PSD'+'.npy'
print(dataset_name)
data = np.load(dataset_name)
Featurespace = np.concatenate((Featurespace, data), axis=0)
print(Featurespace.shape)
# Featurespace.append(data)
dataset_label = path+'/'+'Classlabel_'+str(dataset)+'-'+str(sensor)+'.npy'
print(dataset_label)
label = np.load(dataset_label)
classspace = np.concatenate((classspace, label), axis=0)
print(classspace.shape)
'''
Mapping all the PSD feature of each window with their respective classes labels
'''
Featurespace = pd.DataFrame(Featurespace)
classspace = pd.DataFrame(classspace)
classspace.columns = ['Categorical']
data = pd.concat([Featurespace, classspace], axis=1)
minval = min(data.Categorical.value_counts())
data = pd.concat([data[data.Categorical == cat].head(minval) for cat in data.Categorical.unique()])
Featurespace = data.iloc[:, :-1]
classspace = data.iloc[:, -1]
values, counts = np.unique(classspace, return_counts=True)
print(values, counts)
classspace = classspace.to_numpy()
Featurespace = Featurespace.to_numpy()
''' Using the Help function script..
---> Utils_PSD_Viz.py '''
Energybands = boxcomparisonplots(Featurespace, classspace, band_size)
plt.rcParams.update(plt.rcParamsDefault)
plt.figure(figsize=(12, 15))
sns.set(font_scale=3.5)
sns.set_style("whitegrid", {'axes.grid': False})
ax = sns.catplot(x="Frequency Bands", y="Cumulative energy value", hue="Categorical", kind="bar", errorbar=("pi", 95), data=Energybands, height=9,
aspect=2.8, palette={"Ti64": "blue", "Ti64_3Fe": "green", "Ti64_6Fe": "red"})
ax.set_xticklabels(fontsize=30)
ax._legend.remove()
plt.ylabel("Cumulative energy value \n (dB/Hz)", labelpad=20)
plt.title('Dataset Ti-Fe', fontsize=40)
plt.legend(loc='upper right', frameon=False, fontsize=30)
plt.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
plotname = 'Dataset Ti-Fe'+'_'+"0-700kHZ "+'_Energyband_Comparison_FFT.png'
plt.savefig(plotname, dpi=800, bbox_inches='tight')
plt.show()

Event Timeline