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

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

In progress implementation of code to merge CSV feature file and Ground-truth files together

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