annotate dml-cla/python/rdf_wrapper.py @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
rev   line source
Daniel@0 1 # Part of DML (Digital Music Laboratory)
Daniel@0 2 # Copyright 2014-2015 Steven Hargreaves
Daniel@0 3
Daniel@0 4 # This program is free software; you can redistribute it and/or
Daniel@0 5 # modify it under the terms of the GNU General Public License
Daniel@0 6 # as published by the Free Software Foundation; either version 2
Daniel@0 7 # of the License, or (at your option) any later version.
Daniel@0 8 #
Daniel@0 9 # This program is distributed in the hope that it will be useful,
Daniel@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Daniel@0 12 # GNU General Public License for more details.
Daniel@0 13 #
Daniel@0 14 # You should have received a copy of the GNU General Public
Daniel@0 15 # License along with this library; if not, write to the Free Software
Daniel@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Daniel@0 17
Daniel@0 18 #!/usr/bin/env python
Daniel@0 19 # -*- coding: utf-8 -*-
Daniel@0 20 __author__="hargreavess"
Daniel@0 21
Daniel@0 22 import ConfigParser
Daniel@0 23 import logging
Daniel@0 24 import sys
Daniel@0 25 import os
Daniel@0 26 import time
Daniel@0 27 import shutil
Daniel@0 28 import argparse
Daniel@0 29 from os import walk
Daniel@0 30 import rdflib
Daniel@0 31 from rdflib import Graph, Namespace, BNode, Literal, RDF, RDFS
Daniel@0 32 from RDFClosure import DeductiveClosure, OWLRL_Semantics
Daniel@0 33 import transforms.keyTonicHistogram
Daniel@0 34 import transforms.tuningFrequencyStatistics
Daniel@0 35 import transforms.semitoneHistogram
Daniel@0 36 import math
Daniel@0 37
Daniel@0 38 cla = Namespace("http://dml.org/cla#")
Daniel@0 39
Daniel@0 40 def print_status(msg):
Daniel@0 41 sys.stderr.write(msg+'\n')
Daniel@0 42
Daniel@0 43 def main():
Daniel@0 44
Daniel@0 45 # get config
Daniel@0 46 # print_status("Reading configuration...")
Daniel@0 47 # config = ConfigParser.ConfigParser()
Daniel@0 48 # config.read('dml-analyser.cfg')
Daniel@0 49
Daniel@0 50 # parse dmlcla ontolgy and input graph
Daniel@0 51 input_graph = Graph()
Daniel@0 52 # print_status("Reading DML CLA ontology...")
Daniel@0 53 # input_graph.parse(config.get('Ontology', 'dmlclaOntology_URI'), format="n3")
Daniel@0 54 # print_status("Reading input triples...")
Daniel@0 55 input_graph.parse(sys.stdin, format="n3")
Daniel@0 56 # print_status("------")
Daniel@0 57 # input_graph.serialize(destination=sys.stderr,format='n3')
Daniel@0 58 # print_status("------")
Daniel@0 59 # print_status("Forward chaining OWL entailments...")
Daniel@0 60 # DeductiveClosure(OWLRL_Semantics).expand(input_graph)
Daniel@0 61
Daniel@0 62 # initialise output rdf graph
Daniel@0 63 # bnode = BNode()
Daniel@0 64 # print_status("Building output graph...")
Daniel@0 65 output_graph = Graph()
Daniel@0 66 output_graph.bind("dmlcla",cla)
Daniel@0 67
Daniel@0 68 for comp in input_graph.subjects(cla.function,None):
Daniel@0 69 func= input_graph.value(comp, cla.function)
Daniel@0 70 inp = input_graph.value(comp, cla.input)
Daniel@0 71 print_status("Got computation %s: %s(%s)" % (comp,func,inp))
Daniel@0 72 fn = eval(func)
Daniel@0 73 output = fn(inp.value)
Daniel@0 74 print_status("Result is %s" % output)
Daniel@0 75 output_graph.set((comp,cla.output,Literal(output)))
Daniel@0 76 # comps = input_graph.query(
Daniel@0 77 # """prefix cla: <http://dml.org/cla#>
Daniel@0 78 # SELECT ?comp ?function ?input
Daniel@0 79 # WHERE {
Daniel@0 80 # ?comp cla:function ?function .
Daniel@0 81 # ?comp cla:input ?input
Daniel@0 82 # }""")
Daniel@0 83
Daniel@0 84 # for row in comps:
Daniel@0 85 # print_status("Got computation %s: %s(%s)" % (row.comp,row.function,row.input))
Daniel@0 86 # fn = eval(row.function)
Daniel@0 87 # output = fn(row.input)
Daniel@0 88 # print_state("Result is %s" % output)
Daniel@0 89
Daniel@0 90
Daniel@0 91 # # Determine which transforms are to be applied, and
Daniel@0 92 # # the associated input files
Daniel@0 93 # transforms = find_transforms_in_n3(input_graph)
Daniel@0 94
Daniel@0 95 # # Apply the transform(s) to each file and create
Daniel@0 96 # # rdf results graph
Daniel@0 97 # execute_transforms(transforms, output_graph)
Daniel@0 98
Daniel@0 99 # # Write output rdf to stdout
Daniel@0 100 # print_status("Writing output triples...")
Daniel@0 101 output_graph.serialize(destination=sys.stdout,format='n3')
Daniel@0 102
Daniel@0 103 # # Loop through all transforms, process the corresponding
Daniel@0 104 # # input files appropriately and add the (RDF) result to output_graph
Daniel@0 105 # def execute_transforms(transforms, output_graph):
Daniel@0 106
Daniel@0 107 # transform_iter = transforms.iterkeys()
Daniel@0 108 # key_histogram = []
Daniel@0 109
Daniel@0 110 # for (transform, transform_type) in transforms:
Daniel@0 111
Daniel@0 112 # input_f_files = transforms.get((transform, transform_type))
Daniel@0 113
Daniel@0 114 # # Add additional clauses to this if statement
Daniel@0 115 # # for each transform type
Daniel@0 116 # if transform_type == rdflib.term.URIRef(u'http://dml.org/dml/cla#CollectionLevelKeyTonic'):
Daniel@0 117
Daniel@0 118 # transforms.keyTonicHistogram.run(transform,input_f_files, output_graph)
Daniel@0 119
Daniel@0 120 # elif transform_type == rdflib.term.URIRef(u'http://dml.org/dml/cla#CollectionLevelTuningFrequencyStatistics'):
Daniel@0 121
Daniel@0 122 # transforms.tuningFrequencyStatistics.run(transform,input_f_files, output_graph)
Daniel@0 123
Daniel@0 124 # elif transform_type == rdflib.term.URIRef(u'http://dml.org/dml/cla#CollectionLevelSemitone'):
Daniel@0 125 # transforms.semitoneHistogram.run(transform, input_f_files, output_graph)
Daniel@0 126
Daniel@0 127
Daniel@0 128 # # Find all transforms, and their associated input files,
Daniel@0 129 # # from rdf_graph
Daniel@0 130 # def find_transforms_in_n3(rdf_graph):
Daniel@0 131
Daniel@0 132 # q1 = rdf_graph.query(
Daniel@0 133 # """prefix dml: <http://dml.org/dml/cla#>
Daniel@0 134 # SELECT ?comp ?function
Daniel@0 135 # WHERE {
Daniel@0 136 # ?comp a dml:Computation .
Daniel@0 137 # ?comp dml:function ?function .
Daniel@0 138 # }""")
Daniel@0 139
Daniel@0 140 # for row in q1:
Daniel@0 141
Daniel@0 142 # inputs = rdf_graph.query(
Daniel@0 143 # """prefix dml: <http://dml.org/dml/cla#>
Daniel@0 144 # SELECT ?input
Daniel@0 145 # WHERE {
Daniel@0 146 # ?comp dml:input ?input .
Daniel@0 147 # }""")
Daniel@0 148
Daniel@0 149 # computations = dict()
Daniel@0 150
Daniel@0 151 # for row in qres:
Daniel@0 152
Daniel@0 153 # comp = row.comp
Daniel@0 154 # input = row.input
Daniel@0 155 # transform_type = row.transform_type
Daniel@0 156
Daniel@0 157 # if transforms.has_key((transform_bnode, transform_type)):
Daniel@0 158
Daniel@0 159 # transform_key = transforms.get((transform_bnode, transform_type))
Daniel@0 160 # transform_key.append(dml_input)
Daniel@0 161
Daniel@0 162 # else:
Daniel@0 163
Daniel@0 164 # transforms[(transform_bnode, transform_type)] = [dml_input]
Daniel@0 165
Daniel@0 166 # return transforms
Daniel@0 167
Daniel@0 168 # # Determine the mapping between feature file URIs and
Daniel@0 169 # # their source audio file URIs
Daniel@0 170 # def map_audio_to_feature_files():
Daniel@0 171
Daniel@0 172 # # Loop through audio files
Daniel@0 173 # lines = [line.strip() for line in args.audio_files]
Daniel@0 174
Daniel@0 175 # for audio_file in lines:
Daniel@0 176
Daniel@0 177 # print "sonic-annotator -T " + args.transforms + " --rdf-basedir " + args.basedir + " <" + audio_file + ">"
Daniel@0 178
Daniel@0 179 # audio_to_feature_file_dict = dict()
Daniel@0 180
Daniel@0 181 # for (dirpath, dirnames, filenames) in walk(args.basedir):
Daniel@0 182 # for file in filenames:
Daniel@0 183
Daniel@0 184 # print "found file: " + file
Daniel@0 185
Daniel@0 186 # if file.endswith(".n3"):
Daniel@0 187
Daniel@0 188 # print "found n3 file: " + file
Daniel@0 189
Daniel@0 190 # # open and parse n3 file
Daniel@0 191 # rdf_graph = Graph()
Daniel@0 192 # rdf_graph.parse(os.path.join(dirpath, file), format="n3")
Daniel@0 193
Daniel@0 194 # # find subject in ?subject a mo:AudioFile
Daniel@0 195 # qres = rdf_graph.query(
Daniel@0 196 # """SELECT ?audio_file
Daniel@0 197 # WHERE {
Daniel@0 198 # ?audio_file a mo:AudioFile .
Daniel@0 199 # }""")
Daniel@0 200
Daniel@0 201 # print len(qres)
Daniel@0 202
Daniel@0 203 # for row in qres:
Daniel@0 204
Daniel@0 205 # print("audio file URI is %s" % row.audio_file.n3())
Daniel@0 206 # print("feature file URI is %s" % os.path.join(os.getcwd(), dirpath, file))
Daniel@0 207 # audio_to_feature_file_dict[row.audio_file.n3()] = os.path.join(os.getcwd(), dirpath, file)
Daniel@0 208
Daniel@0 209 # # add full file URI, subject to dict
Daniel@0 210
Daniel@0 211 # print audio_to_feature_file_dict
Daniel@0 212
Daniel@0 213 if __name__ == "__main__":
Daniel@0 214
Daniel@0 215 # parser = argparse.ArgumentParser()
Daniel@0 216
Daniel@0 217 # # parser.add_argument("-T", "--transforms", help="the URI of an n3 (RDF) file describing one or more transforms, and the files to which they should be applied")
Daniel@0 218 # parser.add_argument("-b", "--basedir", help="the URI of the base output directory")
Daniel@0 219
Daniel@0 220 # args = parser.parse_args()
Daniel@0 221
Daniel@0 222 main()
Daniel@0 223