diff fca/fcaDotGen.py @ 0:62d2c72e4223

initial commit
author nothing@tehis.net
date Mon, 25 Feb 2013 14:40:54 +0000
parents
children 8bd8453e0551
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fca/fcaDotGen.py	Mon Feb 25 14:40:54 2013 +0000
@@ -0,0 +1,150 @@
+import rdflib, os
+from rdflib import plugin, OWL, URIRef, RDF, RDFS, Literal
+from rdflib.graph import Graph
+from rdflib.namespace import Namespace
+
+plugin.register(
+    'sparql', rdflib.query.Processor,
+    'rdfextras.sparql.processor', 'Processor')
+plugin.register(
+    'sparql', rdflib.query.Result,
+    'rdfextras.sparql.query', 'SPARQLQueryResult')
+
+afuri = "http://sovarr.c4dm.eecs.qmul.ac.uk/features/"
+cataloguePath = '/Users/alo/MusicOntology/features/af-catalogue.rdf'
+
+graph = Graph()
+graph.parse(cataloguePath)
+
+execfile('/Users/alo/MusicOntology/features/fca/writeHTML.py')
+
+def getFeaturesByTool( name ):
+    qry = 'SELECT DISTINCT ?feature WHERE { ?x af:computedIn "' + name + '" . ?x af:feature ?feature} ORDER BY ?feature'
+    
+    return graph.query(qry,
+        initNs=dict(
+            af=Namespace(afuri))
+    )
+
+def getBaseFeatures():
+    features = []
+    ns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn')
+    for su in graph.subjects(RDF.type, OWL.Class):
+        count = sum(1 for _ in graph.objects(su,ns))
+        if count > 1:
+            features.append(su.split('/')[-1])
+            
+    return features.sort()
+    
+def getTools():
+    return graph.query(
+        """SELECT DISTINCT ?tool
+           WHERE {
+    	   	?x local:computedIn ?tool .
+    		?x local:feature ?feature 
+           }
+    	   ORDER BY ?tool""",
+        initNs=dict(
+            af=Namespace(afuri))
+    )
+
+def constructMatrix( name, path ):
+    rows = []
+    columns = []
+    matrix = []
+
+    cat = ["appdomain", "complexity", "domain", "level", "temporalscale", "dimensions"]
+    catdict = {}
+
+    for nm in cat:
+        catdict[nm] = []
+
+    for ns, value in graph.namespaces():
+        if ns == 'local':
+            local = value
+
+    for it in getFeaturesByTool(name):
+        rows.append(it[0])
+        for su, pr, ob in graph.triples((URIRef(local+it[0]), None, None)):
+            col = pr.split('/')[-1]
+            if cat.count(col) != 0:
+                if catdict[col].count(ob) == 0:
+                    columns.append(col+"-"+ob)
+                    catdict[col].append(ob)
+
+    rows.sort()
+    columns.sort()
+    
+    for i in range(len(rows)):
+        row = []
+        for j in range(len(columns)):
+            row.append(0)
+        matrix.append(row)
+
+    index = 0
+    for feature in rows:
+        for s, p, o in graph.triples((URIRef(local+feature), None, None)):
+            col = p.split('/')[-1]
+            if cat.count(col) != 0:
+                matrix[index][columns.index(col+"-"+o)] = 1
+        
+        index += 1
+    
+    writeCXT(path+name+'.cxt', matrix, columns, rows)
+    print ("wrote cxt data to "+path+name+".cxt")
+    name = name.replace(' ', '\ ')
+    os.system("fcastone -bc "+path+name+".cxt "+path+name+".dot")
+    
+def constructBaseMatrix( name, path ):
+    rows = []
+    columns = []
+    matrix = []
+
+    cat = ["domain", "level", "temporalscale", "dimensions", "output", "tag"]
+    catdict = {}
+
+    for nm in cat:
+        catdict[nm] = []
+
+    for ns, value in graph.namespaces():
+        if ns == 'af':
+            local = value
+
+    features = []
+    ns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn')
+    for su in graph.subjects(RDF.type, OWL.Class):
+        count = sum(1 for _ in graph.objects(su,ns))
+        if count > 1:
+            features.append(su.split('/')[-1])
+
+    for it in features:
+        rows.append(it)
+        for su, pr, ob in graph.triples((URIRef(local+it), None, None)):
+            col = pr.split('/')[-1]
+            if cat.count(col) != 0:
+                if catdict[col].count(ob) == 0:
+                    columns.append(ob.replace(' ', '-')+"-"+col.replace(' ', '-'))
+                    catdict[col].append(ob)
+
+    rows.sort()
+    columns.sort()
+    
+    for i in range(len(rows)):
+        row = []
+        for j in range(len(columns)):
+            row.append(0)
+        matrix.append(row)
+
+    index = 0
+    for feature in rows:
+        for s, p, o in graph.triples((URIRef(local+feature), None, None)):
+            col = p.split('/')[-1]
+            if cat.count(col) != 0:
+                matrix[index][columns.index(o.replace(' ', '-')+"-"+col.replace(' ', '-'))] = 1
+        
+        index += 1
+    
+    writeCXT(path+name+'.cxt', matrix, columns, rows)
+    print ("wrote cxt data to "+path+name+".cxt")
+    name = name.replace(' ', '\ ')
+    os.system("fcastone -bc "+path+name+".cxt "+path+name+".dot")