/
getFolds.py
61 lines (48 loc) · 1.82 KB
/
getFolds.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
# coding: utf-8
import sys
sys.path.insert(0, '../ModeTonicEstimation/')
import json
import os
from extras import foldGeneration
from extras import fileOperations as fo
import numpy as np
from sklearn import cross_validation
# I/O
base_dir = '../../experiments/raag-recognition/'
data_dir = os.path.join(base_dir,'data')
experiments_dir = os.path.join(base_dir, 'experiments')
modes = fo.getModeNames(data_dir)
n_exp = 20
n_folds = 12
# get the data into appropriate format
[pitch_paths, pitch_base, pitch_fname] = fo.getFileNamesInDir(data_dir, '.pitch')
tonic_paths = [os.path.splitext(p)[0] + '.tonic' for p in pitch_paths]
mode_labels = []
for p in pitch_base:
for r in modes:
if r in p:
mode_labels.append(r)
# make the data a single dictionary for housekeeping
data = []
for p, f, t, r in zip(pitch_paths, pitch_fname, tonic_paths, mode_labels):
data.append({'file':p, 'name':os.path.splitext(f)[0],
'tonic':float(np.loadtxt(t)), 'mode':r})
# create 20 stratified 12 fold
mode_idx = [modes.index(m) for m in [d['mode'] for d in data]]
for nn in xrange(0,n_exp):
skf = cross_validation.StratifiedKFold(mode_idx, n_folds=n_folds,
shuffle=True, random_state=nn)
folds = dict()
for ff, fold in enumerate(skf):
folds['fold' + str(ff)] = {'train': [], 'test': []}
for tr_idx in fold[0]:
folds['fold' + str(ff)]['train'].append(data[tr_idx])
for te_idx in fold[1]:
folds['fold' + str(ff)]['test'].append(data[te_idx])
exp_dir = os.path.join(experiments_dir, 'exp' + str(nn))
if not os.path.exists(exp_dir):
os.makedirs(exp_dir)
fold_savefile = os.path.join(exp_dir, 'folds.json')
with open(fold_savefile, 'w') as f:
json.dump(folds, f, indent=2)
print "Created the folds for Experiment " + str(nn)