comparison fca/fcaDotGen.py @ 0:62d2c72e4223

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