-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadrants.py
114 lines (94 loc) · 3.58 KB
/
quadrants.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
#import numpy and pandas (for data) and NearestNeighbors (for neighbor calculations)
import numpy as np
import random
import pandas as pd
from sklearn.neighbors import NearestNeighbors
from sklearn.preprocessing import MinMaxScaler, StandardScaler
import matplotlib.pyplot as plt
import json
import pprint
import time
import sys
import os
import math
#our modules
import helper
import prodplay
import algos
import plot
import testing
from songdataset import SongDataset
info = helper.loadConfig("config.json")
songdata = SongDataset(
name="Deezer",
cols=info["cols"]["deezer"],
path=testing.DEEZER_SPO_MSD, knn=True, verbose=True,
feat_index = 3, arousal = 4, valence = 3,
)
songdata.make_knn()
test_time = str(time.strftime("%y-%m-%d_%H%M"))
helper.makeDir("./quadrants")
scalers = [
('mms', MinMaxScaler(feature_range=(-1,1))),
('std', StandardScaler()),
('no', None)
]
def make_scale_quadrants(id, scaler):
dirname = "./quadrants/{}-{}".format(id, test_time)
helper.makeDir(dirname)
scaled_points = np.array(songdata.unique_points)
if scaler is not None:
scaled_points = scaler.fit_transform(pd.DataFrame(songdata.unique_points).iloc[:, 0:2])
scaled_to_og = {}
print("\nUsing {} scaler, data in directory {}".format(id, dirname))
# Get a way to correspond from our scaled points back to the original points.
for i in range(songdata.unique_size):
origin_point = songdata.unique_points[i]
scaled_point = scaled_points[i]
scaled_string = helper.arr2stringPoint(scaled_point)
scaled_to_og[scaled_string] = origin_point
# Quadrants array: (-v,-a), (+v,-a), (-v,+a), (+v,+a)
quadrant_names = ["BL", "BR", "TL", "TR"]
quadrants = { "BL": [], "BR": [], "TL": [], "TR": [] }
# Put the points in the quadrants.
for point in scaled_points:
v, a = point[0], point[1]
index = 0
if v >= 0: index += 1
if a >= 0: index += 2
quadrants[quadrant_names[index]].append([v, a])
# Print sizes out for sanity check.
print("Total points:", songdata.unique_size)
print("Quadrants:\n{}\t{}\n{}\t{}".format(
len(quadrants["TL"]), len(quadrants["TR"]),
len(quadrants["BL"]), len(quadrants["BR"])
))
# Grab random sample points from each quadrant.
samples = { "BL": [], "BR": [], "TL": [], "TR": [] }
for name in quadrant_names:
samples[name] = random.sample(quadrants[name], 100)
# Plot all of the samples.
allsamples = np.vstack((samples["TL"], samples["TR"], samples["BL"], samples["BR"]))
if id != 'mms':
allsamples = MinMaxScaler(feature_range=(-1,1)).fit_transform(allsamples)
allsamples = np.transpose(allsamples)
plot.av_circle(
allsamples[0], allsamples[1],
title="random sample using {}".format(id),
file="{}/cir.png".format(dirname)
)
# Grab songs from sample points.
samplesongs = { "BL": [], "BR": [], "TL": [], "TR": [] }
for name in quadrant_names:
for p in samples[name]:
pstring = helper.arr2stringPoint(p)
origpoint = scaled_to_og[pstring]
songid = songdata.get_random_song(origpoint)
samplesongs[name].append(str(songid))
# Print the points into a JSON file.
json_samples = json.dumps(samplesongs, indent=4)
with open("{}/songs.json".format(dirname), "w") as f:
f.write(json_samples)
if __name__ == "__main__":
for id, scaler in scalers:
make_scale_quadrants(id, scaler)