Daniel@0: # Part of DML (Digital Music Laboratory) Daniel@0: # Copyright 2014-2015 Steven Hargreaves Daniel@0: Daniel@0: # This program is free software; you can redistribute it and/or Daniel@0: # modify it under the terms of the GNU General Public License Daniel@0: # as published by the Free Software Foundation; either version 2 Daniel@0: # of the License, or (at your option) any later version. Daniel@0: # Daniel@0: # This program is distributed in the hope that it will be useful, Daniel@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: # GNU General Public License for more details. Daniel@0: # Daniel@0: # You should have received a copy of the GNU General Public Daniel@0: # License along with this library; if not, write to the Free Software Daniel@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: Daniel@0: #!/usr/bin/env python Daniel@0: # -*- coding: utf-8 -*- Daniel@0: __author__="hargreavess" Daniel@0: Daniel@0: import ConfigParser Daniel@0: import logging Daniel@0: import sys Daniel@0: import os Daniel@0: import time Daniel@0: import shutil Daniel@0: import argparse Daniel@0: from os import walk Daniel@0: import rdflib Daniel@0: from rdflib import Graph, Namespace, BNode, Literal, RDF, RDFS Daniel@0: from RDFClosure import DeductiveClosure, OWLRL_Semantics Daniel@0: import transforms.keyTonicHistogram Daniel@0: import transforms.tuningFrequencyStatistics Daniel@0: import transforms.semitoneHistogram Daniel@0: import math Daniel@0: Daniel@0: cla = Namespace("http://dml.org/cla#") Daniel@0: Daniel@0: def print_status(msg): Daniel@0: sys.stderr.write(msg+'\n') Daniel@0: Daniel@0: def main(): Daniel@0: Daniel@0: # get config Daniel@0: # print_status("Reading configuration...") Daniel@0: # config = ConfigParser.ConfigParser() Daniel@0: # config.read('dml-analyser.cfg') Daniel@0: Daniel@0: # parse dmlcla ontolgy and input graph Daniel@0: input_graph = Graph() Daniel@0: # print_status("Reading DML CLA ontology...") Daniel@0: # input_graph.parse(config.get('Ontology', 'dmlclaOntology_URI'), format="n3") Daniel@0: # print_status("Reading input triples...") Daniel@0: input_graph.parse(sys.stdin, format="n3") Daniel@0: # print_status("------") Daniel@0: # input_graph.serialize(destination=sys.stderr,format='n3') Daniel@0: # print_status("------") Daniel@0: # print_status("Forward chaining OWL entailments...") Daniel@0: # DeductiveClosure(OWLRL_Semantics).expand(input_graph) Daniel@0: Daniel@0: # initialise output rdf graph Daniel@0: # bnode = BNode() Daniel@0: # print_status("Building output graph...") Daniel@0: output_graph = Graph() Daniel@0: output_graph.bind("dmlcla",cla) Daniel@0: Daniel@0: for comp in input_graph.subjects(cla.function,None): Daniel@0: func= input_graph.value(comp, cla.function) Daniel@0: inp = input_graph.value(comp, cla.input) Daniel@0: print_status("Got computation %s: %s(%s)" % (comp,func,inp)) Daniel@0: fn = eval(func) Daniel@0: output = fn(inp.value) Daniel@0: print_status("Result is %s" % output) Daniel@0: output_graph.set((comp,cla.output,Literal(output))) Daniel@0: # comps = input_graph.query( Daniel@0: # """prefix cla: Daniel@0: # SELECT ?comp ?function ?input Daniel@0: # WHERE { Daniel@0: # ?comp cla:function ?function . Daniel@0: # ?comp cla:input ?input Daniel@0: # }""") Daniel@0: Daniel@0: # for row in comps: Daniel@0: # print_status("Got computation %s: %s(%s)" % (row.comp,row.function,row.input)) Daniel@0: # fn = eval(row.function) Daniel@0: # output = fn(row.input) Daniel@0: # print_state("Result is %s" % output) Daniel@0: Daniel@0: Daniel@0: # # Determine which transforms are to be applied, and Daniel@0: # # the associated input files Daniel@0: # transforms = find_transforms_in_n3(input_graph) Daniel@0: Daniel@0: # # Apply the transform(s) to each file and create Daniel@0: # # rdf results graph Daniel@0: # execute_transforms(transforms, output_graph) Daniel@0: Daniel@0: # # Write output rdf to stdout Daniel@0: # print_status("Writing output triples...") Daniel@0: output_graph.serialize(destination=sys.stdout,format='n3') Daniel@0: Daniel@0: # # Loop through all transforms, process the corresponding Daniel@0: # # input files appropriately and add the (RDF) result to output_graph Daniel@0: # def execute_transforms(transforms, output_graph): Daniel@0: Daniel@0: # transform_iter = transforms.iterkeys() Daniel@0: # key_histogram = [] Daniel@0: Daniel@0: # for (transform, transform_type) in transforms: Daniel@0: Daniel@0: # input_f_files = transforms.get((transform, transform_type)) Daniel@0: Daniel@0: # # Add additional clauses to this if statement Daniel@0: # # for each transform type Daniel@0: # if transform_type == rdflib.term.URIRef(u'http://dml.org/dml/cla#CollectionLevelKeyTonic'): Daniel@0: Daniel@0: # transforms.keyTonicHistogram.run(transform,input_f_files, output_graph) Daniel@0: Daniel@0: # elif transform_type == rdflib.term.URIRef(u'http://dml.org/dml/cla#CollectionLevelTuningFrequencyStatistics'): Daniel@0: Daniel@0: # transforms.tuningFrequencyStatistics.run(transform,input_f_files, output_graph) Daniel@0: Daniel@0: # elif transform_type == rdflib.term.URIRef(u'http://dml.org/dml/cla#CollectionLevelSemitone'): Daniel@0: # transforms.semitoneHistogram.run(transform, input_f_files, output_graph) Daniel@0: Daniel@0: Daniel@0: # # Find all transforms, and their associated input files, Daniel@0: # # from rdf_graph Daniel@0: # def find_transforms_in_n3(rdf_graph): Daniel@0: Daniel@0: # q1 = rdf_graph.query( Daniel@0: # """prefix dml: Daniel@0: # SELECT ?comp ?function Daniel@0: # WHERE { Daniel@0: # ?comp a dml:Computation . Daniel@0: # ?comp dml:function ?function . Daniel@0: # }""") Daniel@0: Daniel@0: # for row in q1: Daniel@0: Daniel@0: # inputs = rdf_graph.query( Daniel@0: # """prefix dml: Daniel@0: # SELECT ?input Daniel@0: # WHERE { Daniel@0: # ?comp dml:input ?input . Daniel@0: # }""") Daniel@0: Daniel@0: # computations = dict() Daniel@0: Daniel@0: # for row in qres: Daniel@0: Daniel@0: # comp = row.comp Daniel@0: # input = row.input Daniel@0: # transform_type = row.transform_type Daniel@0: Daniel@0: # if transforms.has_key((transform_bnode, transform_type)): Daniel@0: Daniel@0: # transform_key = transforms.get((transform_bnode, transform_type)) Daniel@0: # transform_key.append(dml_input) Daniel@0: Daniel@0: # else: Daniel@0: Daniel@0: # transforms[(transform_bnode, transform_type)] = [dml_input] Daniel@0: Daniel@0: # return transforms Daniel@0: Daniel@0: # # Determine the mapping between feature file URIs and Daniel@0: # # their source audio file URIs Daniel@0: # def map_audio_to_feature_files(): Daniel@0: Daniel@0: # # Loop through audio files Daniel@0: # lines = [line.strip() for line in args.audio_files] Daniel@0: Daniel@0: # for audio_file in lines: Daniel@0: Daniel@0: # print "sonic-annotator -T " + args.transforms + " --rdf-basedir " + args.basedir + " <" + audio_file + ">" Daniel@0: Daniel@0: # audio_to_feature_file_dict = dict() Daniel@0: Daniel@0: # for (dirpath, dirnames, filenames) in walk(args.basedir): Daniel@0: # for file in filenames: Daniel@0: Daniel@0: # print "found file: " + file Daniel@0: Daniel@0: # if file.endswith(".n3"): Daniel@0: Daniel@0: # print "found n3 file: " + file Daniel@0: Daniel@0: # # open and parse n3 file Daniel@0: # rdf_graph = Graph() Daniel@0: # rdf_graph.parse(os.path.join(dirpath, file), format="n3") Daniel@0: Daniel@0: # # find subject in ?subject a mo:AudioFile Daniel@0: # qres = rdf_graph.query( Daniel@0: # """SELECT ?audio_file Daniel@0: # WHERE { Daniel@0: # ?audio_file a mo:AudioFile . Daniel@0: # }""") Daniel@0: Daniel@0: # print len(qres) Daniel@0: Daniel@0: # for row in qres: Daniel@0: Daniel@0: # print("audio file URI is %s" % row.audio_file.n3()) Daniel@0: # print("feature file URI is %s" % os.path.join(os.getcwd(), dirpath, file)) Daniel@0: # audio_to_feature_file_dict[row.audio_file.n3()] = os.path.join(os.getcwd(), dirpath, file) Daniel@0: Daniel@0: # # add full file URI, subject to dict Daniel@0: Daniel@0: # print audio_to_feature_file_dict Daniel@0: Daniel@0: if __name__ == "__main__": Daniel@0: Daniel@0: # parser = argparse.ArgumentParser() Daniel@0: Daniel@0: # # 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: # parser.add_argument("-b", "--basedir", help="the URI of the base output directory") Daniel@0: Daniel@0: # args = parser.parse_args() Daniel@0: Daniel@0: main() Daniel@0: