annotate rdfpy/writeTimbreToolboxOnto.py @ 18:d5012016bf64 tip

added rdfpy and rdfonto directories
author nothing@tehis.net
date Tue, 23 Apr 2013 11:49:20 +0100
parents
children
rev   line source
nothing@18 1 import rdflib, os, fnmatch, urllib2
nothing@18 2 from rdflib import Graph, RDF, RDFS, plugin, URIRef, Literal, OWL, XSD, Namespace
nothing@18 3
nothing@18 4 graph = Graph()
nothing@18 5
nothing@18 6 af = Namespace('http://sovarr.c4dm.eecs.qmul.ac.uk/features/')
nothing@18 7 graph.bind('af', af)
nothing@18 8
nothing@18 9 dc = Namespace('http://purl.org/dc/elements/1.1/')
nothing@18 10 graph.bind('dc', dc)
nothing@18 11
nothing@18 12 owl = Namespace('http://www.w3.org/2002/07/owl#')
nothing@18 13 graph.bind('owl', owl)
nothing@18 14
nothing@18 15 xsd = Namespace('http://www.w3.org/2001/XMLSchema#')
nothing@18 16 graph.bind('xsd', xsd)
nothing@18 17
nothing@18 18
nothing@18 19 input = {
nothing@18 20 'au': "Audio Signal",
nothing@18 21 'tee': "Temporal Energy Envelope",
nothing@18 22 'stft': "Short-term Fourier Transform",
nothing@18 23 'erb': "Auditory model",
nothing@18 24 'harm': "Harmonic"
nothing@18 25 }
nothing@18 26
nothing@18 27 graph.add((
nothing@18 28 URIRef(af+"AudioDescriptor"),
nothing@18 29 RDF.type,
nothing@18 30 OWL.Class
nothing@18 31 ))
nothing@18 32 graph.add((
nothing@18 33 URIRef(af+"AudioDescriptor"),
nothing@18 34 RDFS.label,
nothing@18 35 Literal("Timbre Toolbox Audio Descriptor", lang="en")
nothing@18 36 ))
nothing@18 37
nothing@18 38
nothing@18 39 graph.add((
nothing@18 40 URIRef(af+"InputSignal"),
nothing@18 41 RDF.type,
nothing@18 42 OWL.Class
nothing@18 43 ))
nothing@18 44 graph.add((
nothing@18 45 URIRef(af+"InputSignal"),
nothing@18 46 RDFS.label,
nothing@18 47 Literal("Timbre Toolbox Input Representation", lang="en")
nothing@18 48 ))
nothing@18 49
nothing@18 50 graph.add((
nothing@18 51 URIRef(af+"inputRepresentation"),
nothing@18 52 RDF.type,
nothing@18 53 OWL.ObjectProperty
nothing@18 54 ))
nothing@18 55 graph.add((
nothing@18 56 URIRef(af+"inputRepresentation"),
nothing@18 57 RDFS.domain,
nothing@18 58 URIRef(af+"AudioDescriptor")
nothing@18 59 ))
nothing@18 60 graph.add((
nothing@18 61 URIRef(af+"inputRepresentation"),
nothing@18 62 RDFS.range,
nothing@18 63 URIRef(af+"InputSignal")
nothing@18 64 ))
nothing@18 65
nothing@18 66
nothing@18 67
nothing@18 68 for inkey in input.keys():
nothing@18 69 id = af+input[inkey].replace(" ", "")
nothing@18 70 graph.add((
nothing@18 71 URIRef(id),
nothing@18 72 RDF.type,
nothing@18 73 OWL.Class
nothing@18 74 ))
nothing@18 75 graph.add((
nothing@18 76 URIRef(id),
nothing@18 77 RDFS.label,
nothing@18 78 Literal(input[inkey], lang="en")
nothing@18 79 ))
nothing@18 80 graph.add((
nothing@18 81 URIRef(id),
nothing@18 82 RDFS.subClassOf,
nothing@18 83 URIRef(af+"InputSignal")
nothing@18 84 ))
nothing@18 85
nothing@18 86 features = {
nothing@18 87 'Global Descriptors': [
nothing@18 88 {"Attack": "tee"},
nothing@18 89 {"Decay": "tee"},
nothing@18 90 {"Release": "tee"},
nothing@18 91 {"Log-Attack Time": "tee"},
nothing@18 92 {"Attack Slope": "tee"},
nothing@18 93 {"DecreaseSlope": "tee"},
nothing@18 94 {"Temporal Centroid": "tee"},
nothing@18 95 {"Effective Duration": "tee"},
nothing@18 96 {"Frequency of Energy Modulation": "tee"},
nothing@18 97 {"Amplitude of Energy Modulation": "tee"}
nothing@18 98 ],
nothing@18 99
nothing@18 100 'Time-Varying Descriptors': [
nothing@18 101 {"Autocorrelation": 'au'},
nothing@18 102 {"Zero Crossing Rate": 'au'},
nothing@18 103 {"RMS-Energy Envelope": 'tee'},
nothing@18 104 {"Spectral Centroid": 'stft,erb,harm'},
nothing@18 105 {"Spectral Spread": 'stft,erb,harm'},
nothing@18 106 {"Spectral Skewness": 'stft,erb,harm'},
nothing@18 107 {"Spectral Kurtosis": 'stft,erb,harm'},
nothing@18 108 {"Spectral Slope": 'stft,erb,harm'},
nothing@18 109 {"Spectral Decrease": 'stft,erb,harm'},
nothing@18 110 {"Spectral Rolloff": 'stft,erb,harm'},
nothing@18 111 {"Spectro-temporal Variation": 'stft,erb,harm'},
nothing@18 112 {"Spectral Flatness": 'stft,erb'},
nothing@18 113 {"Spectral Crest": 'stft,erb'},
nothing@18 114 {"Harmonic Energy": 'harm'},
nothing@18 115 {"Noise Energy": 'harm'},
nothing@18 116 {"Noisiness": 'harm'},
nothing@18 117 {"Fundamental Frequency": 'harm'},
nothing@18 118 {"Inharmonicity": 'harm'},
nothing@18 119 {"Tristimulus": 'harm'},
nothing@18 120 {"Harmonic Spectral Deviation": 'harm'},
nothing@18 121 {"Odd to even harmonic ratio": 'harm'}
nothing@18 122 ]
nothing@18 123 }
nothing@18 124
nothing@18 125 for temporal in features.keys():
nothing@18 126 auid = af + temporal[:-1].replace("-", "").replace(" ", "")
nothing@18 127 graph.add((
nothing@18 128 URIRef(auid),
nothing@18 129 RDF.type,
nothing@18 130 OWL.Class
nothing@18 131 ))
nothing@18 132 graph.add((
nothing@18 133 URIRef(auid),
nothing@18 134 RDFS.subClassOf,
nothing@18 135 URIRef(af+"AudioDescriptor")
nothing@18 136 ))
nothing@18 137
nothing@18 138 for feature in features[temporal]:
nothing@18 139 name = feature.keys()[0]
nothing@18 140 id = af + name.replace(" ", "")
nothing@18 141 #graph.add((
nothing@18 142 # URIRef(id),
nothing@18 143 # RDF.type,
nothing@18 144 # OWL.Class
nothing@18 145 #))
nothing@18 146 graph.add((
nothing@18 147 URIRef(id),
nothing@18 148 RDF.type,
nothing@18 149 URIRef(af+"AudioFeature")
nothing@18 150 ))
nothing@18 151 graph.add((
nothing@18 152 URIRef(id),
nothing@18 153 RDFS.label,
nothing@18 154 Literal(name, lang="en")
nothing@18 155 ))
nothing@18 156 graph.add((
nothing@18 157 URIRef(id),
nothing@18 158 RDFS.subClassOf,
nothing@18 159 URIRef(auid)
nothing@18 160 ))
nothing@18 161 for inp in feature[name].split(","):
nothing@18 162 graph.add((
nothing@18 163 URIRef(id),
nothing@18 164 URIRef(af+"inputRepresentation"),
nothing@18 165 URIRef(af+input[inp].replace(" ", ""))
nothing@18 166 ))
nothing@18 167
nothing@18 168
nothing@18 169 graph.serialize('/Users/alo/MusicOntology/features/rdfonto/TimbreToolbox-onto.rdf')
nothing@18 170 graph.serialize('/Users/alo/MusicOntology/features/rdfonto/TimbreToolbox-onto.n3', format="n3")
nothing@18 171