nothing@0
|
1 import rdflib, os
|
nothing@0
|
2 from rdflib import plugin, OWL, URIRef, RDF, RDFS, Literal
|
nothing@0
|
3 from rdflib.graph import Graph
|
nothing@0
|
4 from rdflib.namespace import Namespace
|
nothing@0
|
5
|
nothing@0
|
6 plugin.register(
|
nothing@0
|
7 'sparql', rdflib.query.Processor,
|
nothing@0
|
8 'rdfextras.sparql.processor', 'Processor')
|
nothing@0
|
9 plugin.register(
|
nothing@0
|
10 'sparql', rdflib.query.Result,
|
nothing@0
|
11 'rdfextras.sparql.query', 'SPARQLQueryResult')
|
nothing@0
|
12
|
nothing@0
|
13 afuri = "http://sovarr.c4dm.eecs.qmul.ac.uk/features/"
|
nothing@0
|
14 cataloguePath = '/Users/alo/MusicOntology/features/af-catalogue.rdf'
|
nothing@0
|
15
|
nothing@0
|
16 graph = Graph()
|
nothing@0
|
17 graph.parse(cataloguePath)
|
nothing@0
|
18
|
nothing@0
|
19 execfile('/Users/alo/MusicOntology/features/fca/writeHTML.py')
|
nothing@0
|
20
|
nothing@0
|
21 def getFeaturesByTool( name ):
|
nothing@0
|
22 qry = 'SELECT DISTINCT ?feature WHERE { ?x af:computedIn "' + name + '" . ?x af:feature ?feature} ORDER BY ?feature'
|
nothing@0
|
23
|
nothing@0
|
24 return graph.query(qry,
|
nothing@0
|
25 initNs=dict(
|
nothing@0
|
26 af=Namespace(afuri))
|
nothing@0
|
27 )
|
nothing@0
|
28
|
nothing@0
|
29 def getBaseFeatures():
|
nothing@0
|
30 features = []
|
nothing@0
|
31 ns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn')
|
nothing@0
|
32 for su in graph.subjects(RDF.type, OWL.Class):
|
nothing@0
|
33 count = sum(1 for _ in graph.objects(su,ns))
|
nothing@0
|
34 if count > 1:
|
nothing@0
|
35 features.append(su.split('/')[-1])
|
nothing@0
|
36
|
nothing@0
|
37 return features.sort()
|
nothing@0
|
38
|
nothing@0
|
39 def getTools():
|
nothing@0
|
40 return graph.query(
|
nothing@0
|
41 """SELECT DISTINCT ?tool
|
nothing@0
|
42 WHERE {
|
nothing@0
|
43 ?x local:computedIn ?tool .
|
nothing@0
|
44 ?x local:feature ?feature
|
nothing@0
|
45 }
|
nothing@0
|
46 ORDER BY ?tool""",
|
nothing@0
|
47 initNs=dict(
|
nothing@0
|
48 af=Namespace(afuri))
|
nothing@0
|
49 )
|
nothing@0
|
50
|
nothing@0
|
51 def constructMatrix( name, path ):
|
nothing@0
|
52 rows = []
|
nothing@0
|
53 columns = []
|
nothing@0
|
54 matrix = []
|
nothing@0
|
55
|
nothing@0
|
56 cat = ["appdomain", "complexity", "domain", "level", "temporalscale", "dimensions"]
|
nothing@0
|
57 catdict = {}
|
nothing@0
|
58
|
nothing@0
|
59 for nm in cat:
|
nothing@0
|
60 catdict[nm] = []
|
nothing@0
|
61
|
nothing@0
|
62 for ns, value in graph.namespaces():
|
nothing@0
|
63 if ns == 'local':
|
nothing@0
|
64 local = value
|
nothing@0
|
65
|
nothing@0
|
66 for it in getFeaturesByTool(name):
|
nothing@0
|
67 rows.append(it[0])
|
nothing@0
|
68 for su, pr, ob in graph.triples((URIRef(local+it[0]), None, None)):
|
nothing@0
|
69 col = pr.split('/')[-1]
|
nothing@0
|
70 if cat.count(col) != 0:
|
nothing@0
|
71 if catdict[col].count(ob) == 0:
|
nothing@0
|
72 columns.append(col+"-"+ob)
|
nothing@0
|
73 catdict[col].append(ob)
|
nothing@0
|
74
|
nothing@0
|
75 rows.sort()
|
nothing@0
|
76 columns.sort()
|
nothing@0
|
77
|
nothing@0
|
78 for i in range(len(rows)):
|
nothing@0
|
79 row = []
|
nothing@0
|
80 for j in range(len(columns)):
|
nothing@0
|
81 row.append(0)
|
nothing@0
|
82 matrix.append(row)
|
nothing@0
|
83
|
nothing@0
|
84 index = 0
|
nothing@0
|
85 for feature in rows:
|
nothing@0
|
86 for s, p, o in graph.triples((URIRef(local+feature), None, None)):
|
nothing@0
|
87 col = p.split('/')[-1]
|
nothing@0
|
88 if cat.count(col) != 0:
|
nothing@0
|
89 matrix[index][columns.index(col+"-"+o)] = 1
|
nothing@0
|
90
|
nothing@0
|
91 index += 1
|
nothing@0
|
92
|
nothing@0
|
93 writeCXT(path+name+'.cxt', matrix, columns, rows)
|
nothing@0
|
94 print ("wrote cxt data to "+path+name+".cxt")
|
nothing@0
|
95 name = name.replace(' ', '\ ')
|
nothing@0
|
96 os.system("fcastone -bc "+path+name+".cxt "+path+name+".dot")
|
nothing@0
|
97
|
nothing@0
|
98 def constructBaseMatrix( name, path ):
|
nothing@0
|
99 rows = []
|
nothing@0
|
100 columns = []
|
nothing@0
|
101 matrix = []
|
nothing@0
|
102
|
nothing@0
|
103 cat = ["domain", "level", "temporalscale", "dimensions", "output", "tag"]
|
nothing@0
|
104 catdict = {}
|
nothing@0
|
105
|
nothing@0
|
106 for nm in cat:
|
nothing@0
|
107 catdict[nm] = []
|
nothing@0
|
108
|
nothing@0
|
109 for ns, value in graph.namespaces():
|
nothing@0
|
110 if ns == 'af':
|
nothing@0
|
111 local = value
|
nothing@0
|
112
|
nothing@0
|
113 features = []
|
nothing@0
|
114 ns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn')
|
nothing@0
|
115 for su in graph.subjects(RDF.type, OWL.Class):
|
nothing@0
|
116 count = sum(1 for _ in graph.objects(su,ns))
|
nothing@0
|
117 if count > 1:
|
nothing@0
|
118 features.append(su.split('/')[-1])
|
nothing@0
|
119
|
nothing@0
|
120 for it in features:
|
nothing@0
|
121 rows.append(it)
|
nothing@0
|
122 for su, pr, ob in graph.triples((URIRef(local+it), None, None)):
|
nothing@0
|
123 col = pr.split('/')[-1]
|
nothing@0
|
124 if cat.count(col) != 0:
|
nothing@0
|
125 if catdict[col].count(ob) == 0:
|
nothing@0
|
126 columns.append(ob.replace(' ', '-')+"-"+col.replace(' ', '-'))
|
nothing@0
|
127 catdict[col].append(ob)
|
nothing@0
|
128
|
nothing@0
|
129 rows.sort()
|
nothing@0
|
130 columns.sort()
|
nothing@0
|
131
|
nothing@0
|
132 for i in range(len(rows)):
|
nothing@0
|
133 row = []
|
nothing@0
|
134 for j in range(len(columns)):
|
nothing@0
|
135 row.append(0)
|
nothing@0
|
136 matrix.append(row)
|
nothing@0
|
137
|
nothing@0
|
138 index = 0
|
nothing@0
|
139 for feature in rows:
|
nothing@0
|
140 for s, p, o in graph.triples((URIRef(local+feature), None, None)):
|
nothing@0
|
141 col = p.split('/')[-1]
|
nothing@0
|
142 if cat.count(col) != 0:
|
nothing@0
|
143 matrix[index][columns.index(o.replace(' ', '-')+"-"+col.replace(' ', '-'))] = 1
|
nothing@0
|
144
|
nothing@0
|
145 index += 1
|
nothing@0
|
146
|
nothing@0
|
147 writeCXT(path+name+'.cxt', matrix, columns, rows)
|
nothing@0
|
148 print ("wrote cxt data to "+path+name+".cxt")
|
nothing@0
|
149 name = name.replace(' ', '\ ')
|
nothing@0
|
150 os.system("fcastone -bc "+path+name+".cxt "+path+name+".dot")
|