-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2022_0226_output_gt2k_dataset.py
189 lines (141 loc) · 4.5 KB
/
2022_0226_output_gt2k_dataset.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
# %%
# import
import csv
import os
import pickle
import numpy as np
from sklearn import preprocessing
from sklearn.decomposition import PCA
from tqdm import tqdm
#%%
npz = np.load("naoki_2328_old_dataset.npz", allow_pickle=True)
print(npz.files)
print(npz["label"])
print(len(npz["label"]))
print(len(npz["data"]))
# %%
# Making data without PCA.
# 書き込み
folder_path = "gt2k_raw"
visualization_path = "gt2k_raw_visualization"
try:
os.mkdir(folder_path)
except:
pass
#try:
# os.mkdir(visualization_path)
# except:
# pass
try:
os.mkdir(folder_path + "/data")
os.mkdir(folder_path + "/label")
except:
pass
count = 0
for i in tqdm(range(len(npz["label"]))):
smartpalate_data = npz["data"][i]
script = npz["label"][i].lower()
# smartpalate_data = [[float(y) for y in x] for x in [row[0] for row in smartpalate_data]]
# fig = plt.figure()
# plt.xlim([0,500])
data_frames_np = np.array(smartpalate_data)
# im = plt.imshow(data_frames_np.T)
scaled_data_frames_np = data_frames_np
# plt.show()
# print(scaled_data_frames_np.shape)
filename = str(i) + script
try:
# plt.savefig(visualization_path + "/" +str(filename) +".png")
outF = open(folder_path + "/data/" + str(filename), "w")
for line in scaled_data_frames_np:
outF.write(str(list(line)).strip("[]").replace(",", " "))
outF.write("\n")
outF.close()
# write label
label = open(folder_path + "/label/" + str(filename) + ".lab", "w")
test_line = script.split()
label.write("sil" + "\n")
# for i in test_line:
for countt, scr in enumerate(test_line):
if countt == len(test_line) - 1:
for j in list(scr):
label.write(j + "\n")
break
else:
for j in list(scr):
label.write(j + "\n")
label.write("_" + "\n")
label.write("sil")
label.close()
count += 1
except Exception as e:
print(e)
# %% making PCA
def make_pca_model(data, dimentions):
# dataはnumpy?
pca = PCA(n_components=dimentions)
pca.fit(data)
print(pca.explained_variance_ratio_)
return pca
# Load the data and concatenate them as "data."
# This process is needed to make PCA model.
data = []
for sample in npz["data"]:
data.extend(sample)
data_frames_np = np.array(data)
print(data_frames_np.shape)
# %%
# You can add the different dimensions like:
# for pca_dimention in [4,8,16, 32]:
for pca_dimention in [16]:
pca = make_pca_model(data_frames_np, pca_dimention)
pickle.dump(pca, open(str(pca_dimention) + "pca.pkl", "wb"))
reduced_all_data_frames_np = pca.transform(data_frames_np)
features_mmscaler = preprocessing.MinMaxScaler() # インスタンスの作
features_mmscaler.fit(reduced_all_data_frames_np)
pickle.dump(features_mmscaler, open(str(pca_dimention) + "mmscaler.pkl", "wb"))
# 書き込み
folder_path = "gt2k_2328_" + str(pca_dimention)
try:
os.mkdir(folder_path)
except:
pass
try:
os.mkdir(folder_path + "/data")
os.mkdir(folder_path + "/label")
except:
pass
count = 0
for i in tqdm(range(len(npz["label"]))):
smartpalate_data = npz["data"][i]
script = npz["label"][i].lower()
data_frames_np = np.array(smartpalate_data)
scaled_data_frames_np = features_mmscaler.transform(
pca.transform(data_frames_np)
)
filename = str(i) + script
try:
outF = open(folder_path + "/data/" + str(filename), "w")
for line in scaled_data_frames_np:
outF.write(str(list(line)).strip("[]").replace(",", " "))
outF.write("\n")
outF.close()
# write label
label = open(folder_path + "/label/" + str(filename) + ".lab", "w")
test_line = script.split()
label.write("sil" + "\n")
# for i in test_line:
for countt, scr in enumerate(test_line):
if countt == len(test_line) - 1:
for j in list(scr):
label.write(j + "\n")
break
else:
for j in list(scr):
label.write(j + "\n")
label.write("_" + "\n")
label.write("sil")
label.close()
count += 1
except Exception as e:
print(e)