<jupyter_start><jupyter_text>The data used for this demonstration comes from the Urban Sounds Dataset. This dataset and its taxonomy is presented in <NAME>, <NAME> and <NAME>, A Dataset and Taxonomy for Urban Sound Research, 22nd ACM International Conference on Multimedia, Orlando USA, Nov. 2014.For simplicity the dataset is sampled and a subset of 20 audio clips from two categories are used - air conditioner (AC) and drill.<jupyter_code>def __iter__(self): return 0

#
# INSERT YOUR CONNECTION TO audio_data.zip HERE
# USE Insert StreamingBody object
import types
import pandas as pd
from botocore.client import Config
import ibm_boto3

def __iter__(self): return 0

streaming_body_3 = client_338b0f796bb44a8daaeb8a05ee9a918b.get_object(Bucket='cwt-donotdelete-pr-7m2e3cpmjjj2vm', Key='dataset.zip')['Body']
# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(streaming_body_3, "__iter__"): streaming_body_3.__iter__ = types.MethodType( __iter__, streaming_body_3 ) 
#

data_bytes = BytesIO(streaming_body_3.read())
zip_file = ZipFile(data_bytes)
ZipFile.namelist(zip_file)
#reading data files informationn
audio_data = []
labels = []
sampling_rate = []
file_names = []

for file_name in ZipFile.namelist(zip_file):
    # Skip directories
    if not os.path.basename(file_name):
        continue
    
    audio_file = None
    if file_name.startswith("dataset/pain/"):
        labels.append(0)
        audio_file = zip_file.open(file_name)
    elif file_name.startswith("dataset/discomfortb/"):
        labels.append(1)
        audio_file = zip_file.open(file_name)
    else:
        print("Unknown file class. Skipping.")

    if audio_file is not None:
        file_names.append(file_name)
        tmp = BytesIO(audio_file.read())
        data, samplerate = sf.read(tmp)
        audio_data.append(data)
        sampling_rate.append(samplerate)   
#reaspmling all data into one sapling rate for next process(for this part librosa=0.6.3 can run,if you already have processed data no need for this step)
for index in range(len(audio_data)):
    
    if (sampling_rate[index] == 48000):
        audio_data[index] = librosa.resample(audio_data[index], 48000, 44100)
        sampling_rate[index] = 44100

#tranforme data .wav into mono channel because we can not use els 
def to_mono(data):
    if data.ndim > 1:
        data = np.mean(data, axis=1)
    return data

for index in range(len(audio_data)):
    audio_data[index] = to_mono(audio_data[index])
#plot sample 1 from subfile 1
fig = plt.figure(figsize=(14,6))
plt.plot(audio_data[1])
#plot sapmle 4 from subfile 2
fig = plt.figure(figsize=(14,6))
plt.plot(audio_data[4])
# scalling data to use only the needed informations for extracting CWT coefficients (one part'first')
scales = np.arange(1, 101)
coeff1, freqs1 = pywt.cwt(audio_data[1][:25000], scales, 'morl')
coeff2, freqs2 = pywt.cwt(audio_data[4][:25000], scales, 'morl')
#plot CWT energy
plt.figure(1, figsize=(20,10))
plt.subplot(121)
plt.imshow(coeff1, cmap='coolwarm', aspect='auto')  
plt.subplot(122)
plt.imshow(coeff2, cmap='coolwarm', aspect='auto')  

plt.show() 
# 3D plot to visualise the power from all angles 
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np


fig = plt.figure(figsize=(40,15))

ax1 = fig.add_subplot(1, 2, 1, projection='3d')

Y = np.arange(1, 101, 1)
X = np.arange(1, 25001, 1)

X, Y = np.meshgrid(X, Y)

ax1.plot_surface(X, Y, coeff1, cmap=cm.coolwarm, linewidth=0, antialiased=True)

ax1.set_xlabel("Time", fontsize=20)
ax1.set_ylabel("Scale", fontsize=20)
ax1.set_zlabel("Amplitude", fontsize=20)
ax1.set_zlim3d(-1,1)

ax2 = fig.add_subplot(1, 2, 2, projection='3d')

ax2.plot_surface(X, Y, coeff2, cmap=cm.coolwarm, linewidth=0, antialiased=True)


ax2.set_xlabel("Time", fontsize=20)
ax2.set_ylabel("Scale", fontsize=20)
ax2.set_zlabel("Amplitude", fontsize=20)
ax2.set_zlim3d(-1,1)

plt.show()
# principal coponent analyse reducing vector dimensio 
from sklearn.decomposition import PCA

pca = PCA(n_components=1)

features = np.empty((0,100))

for ind in range(len(audio_data)):
    print('.', end='')
    coeff, freqs = pywt.cwt(audio_data[ind][:25000], scales, 'morl')    
    features = np.vstack([features, pca.fit_transform(coeff).flatten()])

#split data to (20% test,80% train)
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.20, random_state=1234)
#calssification with SVM
clf = svm.SVC()
clf.fit(X_train, y_train)
#predictiong and learning accuracy
y_pred = clf.predict(X_test)
print("Accuracy : %.2f%%" % (accuracy_score(y_test, y_pred) * 100))<jupyter_output>Accuracy : 100.00%
{'fraction_non_alphanumeric': 0.12022207986333547, 'fraction_numerical': 0.04270766602605167, 'mean_word_length': 4.877038895859473, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '543723', 'license': 'MIT', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1707, 'n_words': 430, 'path': 'test.ipynb', 'repo_name': 'BNMEZR/svm-and-cwt', 'size': '4695'}