source: main/trunk/model-sites-dev/mars/collect/deam/MERGE-CSV-FEATURES-AND-AV-FILES.py@ 34438

Last change on this file since 34438 was 34438, checked in by davidb, 4 years ago

Next step of coding development; using OrderedDict to store the values by key, but still in order they appear in the CSV file

  • Property svn:executable set to *
File size: 2.6 KB
Line 
1#!/usr/bin/env python
2
3import csv
4import re
5from collections import OrderedDict
6
7def csv_features_to_dict(csv_filename):
8 csv_features_file = open(csv_filename)
9 csv_features_reader = csv.reader(csv_features_file, delimiter=',', quotechar='"')
10
11 csv_features_dict = {}
12
13 line_count = 0
14 header_row = None
15
16 for row in csv_features_reader:
17 if line_count == 0:
18 row.pop(0)
19 header_row = row
20 else:
21 full_file_id = row[0]
22
23 pat = re.compile('^import/(\\d+).json$')
24 mat = pat.match(full_file_id)
25 song_id = int(mat.group(1))
26
27 row.pop(0)
28 row_ordered_dict = OrderedDict()
29
30 for i in range(0, len(header_row)):
31 field = header_row[i];
32 value = row[i];
33
34 row_ordered_dict[field] = value
35
36 csv_features_dict[song_id] = row_ordered_dict
37
38 line_count += 1
39
40 return csv_features_dict
41
42def csv_groundtruth_to_dict(csv_filename):
43
44 csv_file = open(csv_filename)
45 csv_reader = csv.reader(csv_file, delimiter=',')
46
47 csv_gt_dict = {}
48
49 line_count = 0
50 header_row = None
51
52 for row in csv_reader:
53 if line_count == 0:
54 row.pop(0)
55 header_row = row
56 else:
57 song_id = int(row[0])
58 row.pop(0)
59
60 row_ordered_dict = OrderedDict()
61
62 for i in range(0, len(row)):
63 field = header_row[i];
64 value = row[i];
65
66 row_ordered_dict[field] = value
67
68 csv_gt_dict[song_id] = row_ordered_dict
69
70 line_count += 1
71
72 return csv_gt_dict
73
74
75csv_features_dict = csv_features_to_dict('etc/deam-essentia-features-collated.csv')
76
77print("Essentia Features (sample of first 3 ids):")
78i = 1
79for song_id_key, ordered_feature_vals in sorted(csv_features_dict.items()):
80# print(str(song_id_key) + "\n" + str(ordered_feature_vals))
81 if i >= 3:
82 break
83 i = i + 1
84
85
86groundtruth_dir = 'prepare/annotations/annotations averaged per song/dynamic (per second annotations)'
87arousal_csv_filename = groundtruth_dir + '/arousal.csv'
88valence_csv_filename = groundtruth_dir + '/valence.csv'
89
90
91arousal_groundtruth_dict = csv_groundtruth_to_dict(arousal_csv_filename)
92valence_groundtruth_dict = csv_groundtruth_to_dict(valence_csv_filename)
93
94print("Arousal Ground-truth (sample of first 3 ids):")
95i = 1
96for song_id_key, ordered_feature_vals in sorted(arousal_groundtruth_dict.items()):
97 print(str(song_id_key) + "\n" + str(ordered_feature_vals))
98 if i >= 3:
99 break
100 i = i + 1
Note: See TracBrowser for help on using the repository browser.