annotate fca/fcaDotGen.py @ 18:d5012016bf64 tip

added rdfpy and rdfonto directories
author nothing@tehis.net
date Tue, 23 Apr 2013 11:49:20 +0100
parents 53069717108c
children
rev   line source
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@15 39 def getOnlyBaseFeatures():
nothing@15 40 features = []
nothing@15 41 cns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn')
nothing@15 42 dns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/domain')
nothing@15 43 for su in graph.subjects(RDF.type, OWL.Class):
nothing@15 44 compcount = sum(1 for _ in graph.objects(su,cns))
nothing@15 45 domcount = sum(1 for _ in graph.objects(su,dns))
nothing@15 46 if compcount > 0 and domcount > 0:
nothing@15 47 features.append(su.split('/')[-1])
nothing@15 48
nothing@15 49 return features.sort()
nothing@15 50
nothing@15 51
nothing@0 52 def getTools():
nothing@0 53 return graph.query(
nothing@0 54 """SELECT DISTINCT ?tool
nothing@0 55 WHERE {
nothing@0 56 ?x local:computedIn ?tool .
nothing@0 57 ?x local:feature ?feature
nothing@0 58 }
nothing@0 59 ORDER BY ?tool""",
nothing@0 60 initNs=dict(
nothing@0 61 af=Namespace(afuri))
nothing@0 62 )
nothing@0 63
nothing@0 64 def constructMatrix( name, path ):
nothing@0 65 rows = []
nothing@0 66 columns = []
nothing@0 67 matrix = []
nothing@0 68
nothing@4 69 #cat = ["appdomain", "complexity", "domain", "level", "temporalscale", "dimensions"]
nothing@4 70 cat = ["output", "domain", "level", "temporalscale", "dimensionality", "model"]
nothing@0 71 catdict = {}
nothing@0 72
nothing@0 73 for nm in cat:
nothing@0 74 catdict[nm] = []
nothing@0 75
nothing@0 76 for ns, value in graph.namespaces():
nothing@4 77 if ns == 'af':
nothing@0 78 local = value
nothing@0 79
nothing@0 80 for it in getFeaturesByTool(name):
nothing@0 81 rows.append(it[0])
nothing@0 82 for su, pr, ob in graph.triples((URIRef(local+it[0]), None, None)):
nothing@0 83 col = pr.split('/')[-1]
nothing@0 84 if cat.count(col) != 0:
nothing@0 85 if catdict[col].count(ob) == 0:
nothing@0 86 columns.append(col+"-"+ob)
nothing@0 87 catdict[col].append(ob)
nothing@0 88
nothing@0 89 rows.sort()
nothing@0 90 columns.sort()
nothing@0 91
nothing@0 92 for i in range(len(rows)):
nothing@0 93 row = []
nothing@0 94 for j in range(len(columns)):
nothing@0 95 row.append(0)
nothing@0 96 matrix.append(row)
nothing@0 97
nothing@0 98 index = 0
nothing@0 99 for feature in rows:
nothing@0 100 for s, p, o in graph.triples((URIRef(local+feature), None, None)):
nothing@0 101 col = p.split('/')[-1]
nothing@0 102 if cat.count(col) != 0:
nothing@0 103 matrix[index][columns.index(col+"-"+o)] = 1
nothing@0 104
nothing@0 105 index += 1
nothing@0 106
nothing@0 107 writeCXT(path+name+'.cxt', matrix, columns, rows)
nothing@0 108 print ("wrote cxt data to "+path+name+".cxt")
nothing@0 109 name = name.replace(' ', '\ ')
nothing@0 110 os.system("fcastone -bc "+path+name+".cxt "+path+name+".dot")
nothing@0 111
nothing@0 112 def constructBaseMatrix( name, path ):
nothing@0 113 rows = []
nothing@0 114 columns = []
nothing@0 115 matrix = []
nothing@0 116
nothing@4 117 #cat = ["domain", "level", "temporalscale", "dimensions", "output", "tag"]
nothing@15 118 cat = ["domain", "level", "temporalscale", "dimensionality", "model", "complexity"]
nothing@0 119 catdict = {}
nothing@0 120
nothing@0 121 for nm in cat:
nothing@0 122 catdict[nm] = []
nothing@0 123
nothing@0 124 for ns, value in graph.namespaces():
nothing@0 125 if ns == 'af':
nothing@0 126 local = value
nothing@0 127
nothing@0 128 features = []
nothing@15 129 cns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn')
nothing@15 130 dns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/temporalscale')
nothing@0 131 for su in graph.subjects(RDF.type, OWL.Class):
nothing@15 132 compcount = sum(1 for _ in graph.objects(su,cns))
nothing@15 133 domcount = sum(1 for _ in graph.objects(su,dns))
nothing@15 134 if compcount > 0 and domcount > 0 and su.find('MPEG7') == -1:
nothing@0 135 features.append(su.split('/')[-1])
nothing@15 136
nothing@15 137 #features = features.sort()
nothing@0 138
nothing@0 139 for it in features:
nothing@0 140 rows.append(it)
nothing@0 141 for su, pr, ob in graph.triples((URIRef(local+it), None, None)):
nothing@0 142 col = pr.split('/')[-1]
nothing@0 143 if cat.count(col) != 0:
nothing@0 144 if catdict[col].count(ob) == 0:
nothing@0 145 columns.append(ob.replace(' ', '-')+"-"+col.replace(' ', '-'))
nothing@0 146 catdict[col].append(ob)
nothing@0 147
nothing@0 148 rows.sort()
nothing@0 149 columns.sort()
nothing@0 150
nothing@0 151 for i in range(len(rows)):
nothing@0 152 row = []
nothing@0 153 for j in range(len(columns)):
nothing@0 154 row.append(0)
nothing@0 155 matrix.append(row)
nothing@0 156
nothing@0 157 index = 0
nothing@0 158 for feature in rows:
nothing@0 159 for s, p, o in graph.triples((URIRef(local+feature), None, None)):
nothing@0 160 col = p.split('/')[-1]
nothing@0 161 if cat.count(col) != 0:
nothing@0 162 matrix[index][columns.index(o.replace(' ', '-')+"-"+col.replace(' ', '-'))] = 1
nothing@0 163
nothing@0 164 index += 1
nothing@0 165
nothing@0 166 writeCXT(path+name+'.cxt', matrix, columns, rows)
nothing@0 167 print ("wrote cxt data to "+path+name+".cxt")
nothing@0 168 name = name.replace(' ', '\ ')
nothing@0 169 os.system("fcastone -bc "+path+name+".cxt "+path+name+".dot")