-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze_data.py
253 lines (222 loc) · 9.19 KB
/
analyze_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#import numpy and pandas (for data) and NearestNeighbors (for neighbor calculations)
import numpy as np
import scipy as sp
import random
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import json
import pprint
import time
import sys
import os
import math
import warnings
import itertools
from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler, QuantileTransformer, PowerTransformer, KBinsDiscretizer
#our modules
import helper
import prodplay
import spotify
import plot
import algos
import testing
from songdataset import SongDataset, SegmentDataset
def analyze_dataset(dataset, dirname, verbose=0):
helper.makeDir(dirname)
feats = dataset.feat_df
va = dataset.va_df
df = pd.merge(va, feats, left_index=True, right_index=True)
# df = feats
if verbose >= 2: print(df.info())
## Basic descriptive stats.
desc_stats = pd.DataFrame({
# 'Missing Values': df.isnull().sum(),
'Mean': df.mean(),
'Std': df.std(),
# 'Mode': df.mode().iloc[0],
'Min': df.min(),
'Median': df.median(),
'Max': df.max(),
}).round(4)
if verbose >= 2: print(desc_stats)
desc_stats.to_csv("{}/mmm.csv".format(dirname))
helper.csvToLatex("{}/mmm.csv".format(dirname), "{}/info.tex".format(dirname))
## Full descriptive stats.
description = df.describe().T.round(4)
if verbose >= 2: print(description)
description.to_csv("{}/description.csv".format(dirname))
helper.csvToLatex("{}/description.csv".format(dirname), "{}/description.tex".format(dirname))
## Full boxplot.
# plot.snsplot(sns.boxenplot, df, None, None, figheight=2, figwidth=6)
fig, ax = plt.subplots(dpi=600)
fig.set_figwidth(9)
fig.set_figheight(5)
df.boxplot(figsize = (6, 3), ax=ax)
ax.set_xticklabels(df.columns, rotation=90, ha='right')
plt.tight_layout()
plt.savefig("{}/all-boxes.png".format(dirname))
plt.close()
# # Plot bounding boxes of each feature.
# helper.makeDir(f"{dirname}/feats")
# if verbose >= 1: print("\nMaking individual plots for")
# for col in df.columns:
# fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12.8, 4.8))
# if verbose >= 1: print("... {}".format(col))
# sns.boxenplot(data=df[[col]], ax=ax1)
# ax1.set_title(f"Boxplot for {col}")
# sns.histplot(data=df[[col]], ax=ax2)
# ax2.set_title(f"Histogram for {col}")
# plt.savefig(f"{dirname}/feats/{col}.png")
# plt.close()
# Correlation matrix and heatmap.
correlation = df.corr().round(2)
correlation.to_csv("{}/correlation.csv".format(dirname), float_format="%.6f")
plt.figure(figsize=(len(correlation)*0.6 + 1, len(correlation)*0.4 + 0.75))
sns.heatmap(correlation, annot=True, center=0, cmap='RdYlBu')
plt.tight_layout()
plt.savefig("{}/heatmap.png".format(dirname))
plt.close()
# # Arousal-Valence circle plot.
# mms = MinMaxScaler(feature_range=(-1,1))
# valence = mms.fit_transform(df[["valence"]])
# arousal = mms.fit_transform(df[["arousal"]])
# plot.av_circle(
# valence, arousal,
# title=f"Spread of {dataset.name}",
# file="{}/circle.png".format(dirname)
# )
return df
def discretize(df, columns, maxcoef=5.0, verbose=2):
badfeats = []
catfeats = [
"sp_time_sig", "sp_explicit", "sp_mode",
"MSD_key", "MSD_mode", "MSD_time_signature"
]
if verbose >= 2: print("Analyzing...")
for col in columns:
if col in catfeats: continue
iqr = df[col].quantile(0.75) - df[col].quantile(0.25)
goodmin = df[col].quantile(0.25) - (maxcoef * iqr)
goodmax = df[col].quantile(0.75) + (maxcoef * iqr)
realmin = df[col].quantile(0)
realmax = df[col].quantile(1)
iqrfmin = abs((df[col].quantile(.25) - realmin) / iqr)
iqrfmax = abs((df[col].quantile(.75) - realmax) / iqr)
if verbose >= 2:
print(col)
# print(df[col].describe([.01, .05, .1, .2, .25, .5, .75, .8, .9, .95, .99]))
print(" - iqr:", iqr, " iqrfmin:", iqrfmin, " iqrfmax:", iqrfmax)
if realmax > goodmax or realmin < goodmin:
if verbose >= 2:
print(" - Min: good -", goodmin, ", actual -", realmin, ', distcoef -', iqrfmin)
print(" - Max: good -", goodmax, ", actual -", realmax, ', distcoef -', iqrfmax)
print(" - Time to discretize!")
badfeats.append(col)
# elif verbose >= 2:
# print(" - All good here :)")
kbd = KBinsDiscretizer(n_bins = 20, encode='ordinal', strategy='quantile')
if verbose >= 1: print("\n\nDiscretizing:")
for col in badfeats:
if verbose >= 1: print(f"... {col}")
df[[col]] = kbd.fit_transform(df[[col]])
if verbose >= 1: print("\n")
return badfeats
if __name__ == "__main__":
helper.makeDir("data/_analysis")
info = helper.loadConfig("config.json")
datasets = [
# SongDataset(
# name="Deezer",
# cols=info["cols"]["deezer"],
# path=testing.DEEZER_SPO_MSD,
# feat_index = 3
# ),
SongDataset(
name="Deezer+Spotify",
cols=info["cols"]["deezer"] + info["cols"]["spotify"],
path="data/deezer/deezer-std-all.csv",
),
# SongDataset(
# name="old-Deezer",
# cols=["dzr_sng_id","MSD_sng_id","MSD_track_id","valence","arousal"],
# path="data/deezer/original-info/all.csv",
# feat_index = 2, valence=2, arousal=3
# ),
# SongDataset(
# name="old-Deezer+Spotify",
# cols=info["cols"]["deezer"] + info["cols"]["spotify"],
# path="./data/deezer/deezer-spotify.csv",
# ),
# SongDataset(
# name="old-Deezer+MSD",
# cols=info["cols"]["deezer"] + info["cols"]["msd"],
# path="./data/deezer/deezer-spotify+msd.csv",
# ),
# SongDataset(
# name="Deezer+MSD",
# cols=info["cols"]["deezer"] + info["cols"]["msd"],
# path="data/deezer/deezer-std-all.csv",
# ),
# SongDataset(
# name="PCA-Deezer+Spotify",
# path="data/deezer/deezer-pca-spotify.csv",
# ),
# SongDataset(
# name="PCA-Deezer+MSD",
# path="data/deezer/deezer-pca-msd.csv",
# ),
# SongDataset(
# name="PCA-Deezer+Spotify+MSD",
# path="data/deezer/deezer-pca-all.csv",
# ),
# SegmentDataset(
# name="Deezer+Segments-100cnt",
# cols=info["cols"]["deezer"] + info["cols"]["segments"],
# path="data/deezer/segments/cnt100.csv",
# ),
# SegmentDataset(
# name="Deezer+Segments-030sec",
# cols=info["cols"]["deezer"] + info["cols"]["segments"],
# path="data/deezer/segments/dur030.csv",
# )
]
mms = MinMaxScaler(feature_range=(-1,1))
scalers = [
# {"name": "stdscl", "func": StandardScaler()},
# {"name": "minmax", "func": MinMaxScaler(feature_range=(-1,1))},
# {"name": "robust", "func": RobustScaler(quantile_range=(25,75))},
# {"name": "qtunif", "func": QuantileTransformer(output_distribution='uniform')},
# {"name": "qtnorm", "func": QuantileTransformer(output_distribution='normal')},
{"name": "powert", "func": PowerTransformer(method='yeo-johnson', standardize=True)}
]
for dataset in datasets:
## Grab features and point data and analyze.
dirname = "out/thesis-plots/{}".format(dataset.name)
df = analyze_dataset(dataset, dirname)
discretes = {}
for scaler in scalers:
print(f'\n\nUsing {scaler["name"]} scaler')
df_scale = dataset.full_df.copy()
## Scale all the columns to the specific scaler.
# print("\nIndividually scaling")
for col in df.columns:
# print("... {}".format(col))
df_scale[[col]] = scaler["func"].fit_transform(df[[col]])
# Discretize outlier columns.
discretes[scaler["name"]] = discretize(df_scale, df.columns)
print("\nMin Max Scaling to (-1,1)")
for col in df.columns:
df_scale[[col]] = mms.fit_transform(df_scale[[col]])
## Output the scaled dataset to scaler folder.
dirscaled = f'{dirname}/scaled/{scaler["name"]}'
helper.makeDir(dirscaled)
df_scale.to_csv(f'{dirscaled}/data.csv')
scaled_dataset = SongDataset(
name=f'{scaler["name"]}-{dataset.name}',
cols=dataset.cols,
path=f'{dirscaled}/data.csv',
feat_index = dataset.feat_index,
)
analyze_dataset(scaled_dataset, dirscaled)
helper.jsonout(discretes, f"data/_analysis/{dataset.name}/discretes.json")