annotate pyspark/transforms/tonicHistogram.py @ 0:e34cf1b6fe09 tip

commit
author Daniel Wolff
date Sat, 20 Feb 2016 18:14:24 +0100
parents
children
rev   line source
Daniel@0 1 # Part of DML (Digital Music Laboratory)
Daniel@0 2 #
Daniel@0 3 # This program is free software; you can redistribute it and/or
Daniel@0 4 # modify it under the terms of the GNU General Public License
Daniel@0 5 # as published by the Free Software Foundation; either version 2
Daniel@0 6 # of the License, or (at your option) any later version.
Daniel@0 7 #
Daniel@0 8 # This program is distributed in the hope that it will be useful,
Daniel@0 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel@0 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Daniel@0 11 # GNU General Public License for more details.
Daniel@0 12 #
Daniel@0 13 # You should have received a copy of the GNU General Public
Daniel@0 14 # License along with this library; if not, write to the Free Software
Daniel@0 15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Daniel@0 16
Daniel@0 17 # -*- coding: utf-8 -*-
Daniel@0 18 __author__="hargreavess"
Daniel@0 19
Daniel@0 20 from rdflib import Graph, Namespace, BNode, RDF, Literal
Daniel@0 21 from n3Parser import get_rdf_graph_from_n3
Daniel@0 22 from csvParser import get_dict_from_csv, get_array_from_csv
Daniel@0 23
Daniel@0 24 dml_ns = Namespace("http://dml.org/dml/cla#")
Daniel@0 25
Daniel@0 26 # Add triples representing a 'tonic histogram' result to
Daniel@0 27 # an RDF graph
Daniel@0 28 def add_tonic_histogram_to_graph(tonic_histogram, output_rdf_graph, transform, sample_count, input_f_files):
Daniel@0 29
Daniel@0 30 output_bnode = BNode()
Daniel@0 31 output_rdf_graph.add((transform, dml_ns.output, output_bnode))
Daniel@0 32 for input_f_file in input_f_files:
Daniel@0 33 output_rdf_graph.add((transform, dml_ns.input, input_f_file))
Daniel@0 34 output_rdf_graph.add((output_bnode, RDF.type, dml_ns.TonicHistogram))
Daniel@0 35 output_rdf_graph.add((output_bnode, dml_ns.sample_count, Literal(sample_count)))
Daniel@0 36
Daniel@0 37 for tonic in tonic_histogram:
Daniel@0 38
Daniel@0 39 bin_bnode = BNode()
Daniel@0 40 output_rdf_graph.add((output_bnode, dml_ns.bin, bin_bnode))
Daniel@0 41 output_rdf_graph.add((bin_bnode, dml_ns.bin_number, Literal(tonic)))
Daniel@0 42 output_rdf_graph.add((bin_bnode, dml_ns.bin_value, Literal(tonic_histogram.get(tonic))))
Daniel@0 43
Daniel@0 44 return output_rdf_graph
Daniel@0 45
Daniel@0 46 # Parse the input_f_files n3 files, and generate
Daniel@0 47 # a tonic histogram
Daniel@0 48 def find_cla_tonic_histogram(input_f_files):
Daniel@0 49
Daniel@0 50 num_f_files = len(input_f_files)
Daniel@0 51 tonic_hist = dict()
Daniel@0 52
Daniel@0 53 for x in range(1,13):
Daniel@0 54
Daniel@0 55 tonic_hist[x] = 0
Daniel@0 56
Daniel@0 57 for input_f_file in input_f_files:
Daniel@0 58
Daniel@0 59 # tonic = find_last_key_in_piece(input_f_file)
Daniel@0 60 tonic = find_most_common_key_in_piece(input_f_file)
Daniel@0 61 tonic_hist[tonic] = tonic_hist.get(tonic) + 1
Daniel@0 62
Daniel@0 63 return (tonic_hist, num_f_files)
Daniel@0 64
Daniel@0 65 def find_most_common_key_in_piece(input_f_file):
Daniel@0 66
Daniel@0 67 tonic_hist = find_tonic_histogram(input_f_file)
Daniel@0 68 duration_of_tonic = max(tonic_hist.values())
Daniel@0 69 result = -1
Daniel@0 70
Daniel@0 71 for tonic in tonic_hist:
Daniel@0 72
Daniel@0 73 if tonic_hist[tonic] == duration_of_tonic:
Daniel@0 74 result = tonic
Daniel@0 75
Daniel@0 76 return result
Daniel@0 77
Daniel@0 78 # Parse the input_f_files n3 file, and generate
Daniel@0 79 # a tonic histogram
Daniel@0 80 def find_tonic_histogram(input_f_file):
Daniel@0 81
Daniel@0 82 tonic_hist = dict()
Daniel@0 83
Daniel@0 84 for x in range(1,13):
Daniel@0 85
Daniel@0 86 tonic_hist[x] = 0
Daniel@0 87
Daniel@0 88 if input_f_file.endswith('.csv'):
Daniel@0 89
Daniel@0 90 # ['time','keynr','label']
Daniel@0 91 csv_array = get_array_from_csv(input_f_file)
Daniel@0 92
Daniel@0 93 for idx in range(1, len(csv_array)):
Daniel@0 94
Daniel@0 95 tonic_duration = csv_array[idx][0] - csv_array[idx - 1][0]
Daniel@0 96 tonic = int(csv_array[idx - 1][1])
Daniel@0 97 tonic_hist[tonic] = tonic_hist.get(tonic) + tonic_duration
Daniel@0 98
Daniel@0 99 else:
Daniel@0 100
Daniel@0 101 # TODO - n3 version of tonic histogram
Daniel@0 102 # for now use last key in piece
Daniel@0 103 tonic = find_last_key_in_piece(input_f_file)
Daniel@0 104 tonic_hist[tonic] = tonic_hist.get(tonic) + 1
Daniel@0 105
Daniel@0 106 return (tonic_hist)
Daniel@0 107
Daniel@0 108 # Determine the last (temporally) key in the
Daniel@0 109 # input_f_file n3 file
Daniel@0 110 def find_last_key_in_piece(input_f_file):
Daniel@0 111
Daniel@0 112 max_time = 0
Daniel@0 113 last_key = 0
Daniel@0 114
Daniel@0 115 if input_f_file.endswith('.csv'):
Daniel@0 116
Daniel@0 117 csv_dict = get_dict_from_csv(input_f_file, columtype = ['time','keynr','label'])
Daniel@0 118
Daniel@0 119 for row in csv_dict:
Daniel@0 120
Daniel@0 121 tl_time = float(row['time'])
Daniel@0 122
Daniel@0 123 if tl_time > max_time:
Daniel@0 124
Daniel@0 125 max_time = tl_time
Daniel@0 126 last_key = row['keynr']
Daniel@0 127
Daniel@0 128
Daniel@0 129 else:
Daniel@0 130
Daniel@0 131 key_feature_graph = get_rdf_graph_from_n3(input_f_file)
Daniel@0 132
Daniel@0 133 qres = key_feature_graph.query(
Daniel@0 134 """prefix dml: <http://dml.org/dml/cla#>
Daniel@0 135 prefix event: <http://purl.org/NET/c4dm/event.owl#>
Daniel@0 136 prefix tl: <http://purl.org/NET/c4dm/timeline.owl#>
Daniel@0 137 prefix af: <http://purl.org/ontology/af/>
Daniel@0 138 SELECT ?event ?key ?tl_time
Daniel@0 139 WHERE {
Daniel@0 140 ?event event:time ?event_time .
Daniel@0 141 ?event_time tl:at ?tl_time .
Daniel@0 142 ?event af:feature ?key .
Daniel@0 143 }""")
Daniel@0 144
Daniel@0 145 for row in qres:
Daniel@0 146
Daniel@0 147 tl_time_str_len = len(row.tl_time)
Daniel@0 148 tl_time = float(row.tl_time[2:tl_time_str_len-1])
Daniel@0 149
Daniel@0 150 if tl_time > max_time:
Daniel@0 151
Daniel@0 152 max_time = tl_time
Daniel@0 153 last_key = row.key
Daniel@0 154
Daniel@0 155
Daniel@0 156 return int(last_key)