Changeset 34440

Show
Ignore:
Timestamp:
05.10.2020 11:54:07 (3 weeks ago)
Author:
davidb
Message:

Version that outputs the merged CSV data into a new file

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/model-sites-dev/mars/collect/deam/MERGE-CSV-FEATURES-AND-AV-FILES.py

    r34438 r34440  
    11#!/usr/bin/env python 
     2 
     3from __future__ import absolute_import 
     4from __future__ import division 
     5from __future__ import print_function 
    26 
    37import csv 
     
    610 
    711def csv_features_to_dict(csv_filename): 
    8     csv_features_file = open(csv_filename) 
     12    csv_features_file = open(csv_filename,"r") 
    913    csv_features_reader = csv.reader(csv_features_file, delimiter=',', quotechar='"') 
    1014 
     
    7377 
    7478 
    75 csv_features_dict = csv_features_to_dict('etc/deam-essentia-features-collated.csv') 
     79def print_songkey_dict(message,songkey_dict,limit=3): 
     80    print() 
     81    print("****") 
     82    print("* " + message + " (Sample limit = " + str(limit) + "):") 
     83    print("****") 
    7684 
    77 print("Essentia Features (sample of first 3 ids):") 
    78 i = 1 
    79 for 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 
     85    i = 1 
     86    for song_id_key, ordered_vals in sorted(songkey_dict.items()):  
     87        print("Song id " + str(song_id_key) + ": \n" + str(ordered_vals)) 
     88        if i >= limit: 
     89            break 
     90        i = i + 1 
    8491 
     92def add_groundtruth_to_features(songkey_features_dict,gt_label,songkey_gt_dict,gt_field): 
     93    new_features_label = gt_label + "_" + gt_field 
     94 
     95    # Doesn't need to be sorted, but tidier this way  
     96    for song_id_key, ordered_feature_vals in sorted(songkey_features_dict.items()):  
     97        songkey_features_dict[song_id_key][new_features_label] = songkey_gt_dict[song_id_key][gt_field] 
     98     
     99 
     100def ordered_dict_features_to_array(songkey_combined_dict): 
     101     
     102    features_array = [] 
     103 
     104    header = [] 
     105 
     106    # Set up the header labels 
     107    first_key = next(iter(songkey_combined_dict)) 
     108    first_ordered_dict = songkey_combined_dict[first_key] 
     109 
     110    for feature_key in first_ordered_dict: 
     111        header.append(feature_key) 
     112 
     113    features_array.append(header) 
     114 
     115    # Now move on to processing each of the song_key_id entries 
     116    # 
     117    # Doesn't need to be sorted, but tidier this way  
     118     
     119    for song_id_key, ordered_combined_vals in sorted(songkey_combined_dict.items()):  
     120 
     121        row = [] 
     122 
     123        for feature_key in ordered_combined_vals: 
     124            feature_val = ordered_combined_vals[feature_key] 
     125 
     126            row.append(feature_val) 
     127 
     128 
     129        features_array.append(row) 
     130 
     131    return features_array 
     132         
     133 
     134def csv_save_combined_features(csv_filename,combined_features_array): 
     135    csv_features_file = open(csv_filename,"w",newline="") 
     136    csv_features_writer = csv.writer(csv_features_file, delimiter=',', quotechar='"') 
     137 
     138    csv_features_writer.writerows(combined_features_array); 
     139 
     140 
     141 
     142csv_input_filename  = 'etc/deam-essentia-features-collated.csv' 
     143csv_output_filename = 'etc/deam-essentia-features-arousal-valence.csv' 
    85144 
    86145groundtruth_dir = 'prepare/annotations/annotations averaged per song/dynamic (per second annotations)' 
     
    88147valence_csv_filename = groundtruth_dir + '/valence.csv' 
    89148 
     149# Essentia Features extracted: 21 seconds in, for 6 second block 
     150# This equates to DEAM ground-trugh date values: 
     151#   [sample_21000ms,sample_27000ms) 
     152# 
     153# => Take the ground-trugh value at 'sample_26500' as the culmination of 
     154#    the affect the music had on users participating in the experiment 
     155 
     156gt_field='sample_26500ms' 
     157 
     158 
     159csv_features_dict = csv_features_to_dict(csv_input_filename) 
    90160 
    91161arousal_groundtruth_dict = csv_groundtruth_to_dict(arousal_csv_filename) 
    92162valence_groundtruth_dict = csv_groundtruth_to_dict(valence_csv_filename) 
    93163 
    94 print("Arousal Ground-truth (sample of first 3 ids):") 
    95 i = 1 
    96 for 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 
     164print_songkey_dict("Arousal Ground-truth",arousal_groundtruth_dict) 
     165print_songkey_dict("Valence Ground-truth",valence_groundtruth_dict) 
     166 
     167add_groundtruth_to_features(csv_features_dict,"arousal",arousal_groundtruth_dict,gt_field) 
     168add_groundtruth_to_features(csv_features_dict,"valence",valence_groundtruth_dict,gt_field) 
     169 
     170print_songkey_dict("Essentia Features",csv_features_dict,1) 
     171 
     172combined_features_array = ordered_dict_features_to_array(csv_features_dict) 
     173 
     174 
     175print() 
     176print("****") 
     177print("* Saving combined CSV data to: " + csv_output_filename) 
     178print("****") 
     179csv_save_combined_features(csv_output_filename,combined_features_array) 
     180