nothing@0: import rdflib, os nothing@0: from rdflib import plugin, OWL, URIRef, RDF, RDFS, Literal nothing@0: from rdflib.graph import Graph nothing@0: from rdflib.namespace import Namespace nothing@0: nothing@0: plugin.register( nothing@0: 'sparql', rdflib.query.Processor, nothing@0: 'rdfextras.sparql.processor', 'Processor') nothing@0: plugin.register( nothing@0: 'sparql', rdflib.query.Result, nothing@0: 'rdfextras.sparql.query', 'SPARQLQueryResult') nothing@0: nothing@0: afuri = "http://sovarr.c4dm.eecs.qmul.ac.uk/features/" nothing@0: cataloguePath = '/Users/alo/MusicOntology/features/af-catalogue.rdf' nothing@0: nothing@0: graph = Graph() nothing@0: graph.parse(cataloguePath) nothing@0: nothing@0: execfile('/Users/alo/MusicOntology/features/fca/writeHTML.py') nothing@0: nothing@0: def getFeaturesByTool( name ): nothing@0: qry = 'SELECT DISTINCT ?feature WHERE { ?x af:computedIn "' + name + '" . ?x af:feature ?feature} ORDER BY ?feature' nothing@0: nothing@0: return graph.query(qry, nothing@0: initNs=dict( nothing@0: af=Namespace(afuri)) nothing@0: ) nothing@0: nothing@0: def getBaseFeatures(): nothing@0: features = [] nothing@0: ns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn') nothing@0: for su in graph.subjects(RDF.type, OWL.Class): nothing@0: count = sum(1 for _ in graph.objects(su,ns)) nothing@0: if count > 1: nothing@0: features.append(su.split('/')[-1]) nothing@0: nothing@0: return features.sort() nothing@0: nothing@15: def getOnlyBaseFeatures(): nothing@15: features = [] nothing@15: cns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn') nothing@15: dns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/domain') nothing@15: for su in graph.subjects(RDF.type, OWL.Class): nothing@15: compcount = sum(1 for _ in graph.objects(su,cns)) nothing@15: domcount = sum(1 for _ in graph.objects(su,dns)) nothing@15: if compcount > 0 and domcount > 0: nothing@15: features.append(su.split('/')[-1]) nothing@15: nothing@15: return features.sort() nothing@15: nothing@15: nothing@0: def getTools(): nothing@0: return graph.query( nothing@0: """SELECT DISTINCT ?tool nothing@0: WHERE { nothing@0: ?x local:computedIn ?tool . nothing@0: ?x local:feature ?feature nothing@0: } nothing@0: ORDER BY ?tool""", nothing@0: initNs=dict( nothing@0: af=Namespace(afuri)) nothing@0: ) nothing@0: nothing@0: def constructMatrix( name, path ): nothing@0: rows = [] nothing@0: columns = [] nothing@0: matrix = [] nothing@0: nothing@4: #cat = ["appdomain", "complexity", "domain", "level", "temporalscale", "dimensions"] nothing@4: cat = ["output", "domain", "level", "temporalscale", "dimensionality", "model"] nothing@0: catdict = {} nothing@0: nothing@0: for nm in cat: nothing@0: catdict[nm] = [] nothing@0: nothing@0: for ns, value in graph.namespaces(): nothing@4: if ns == 'af': nothing@0: local = value nothing@0: nothing@0: for it in getFeaturesByTool(name): nothing@0: rows.append(it[0]) nothing@0: for su, pr, ob in graph.triples((URIRef(local+it[0]), None, None)): nothing@0: col = pr.split('/')[-1] nothing@0: if cat.count(col) != 0: nothing@0: if catdict[col].count(ob) == 0: nothing@0: columns.append(col+"-"+ob) nothing@0: catdict[col].append(ob) nothing@0: nothing@0: rows.sort() nothing@0: columns.sort() nothing@0: nothing@0: for i in range(len(rows)): nothing@0: row = [] nothing@0: for j in range(len(columns)): nothing@0: row.append(0) nothing@0: matrix.append(row) nothing@0: nothing@0: index = 0 nothing@0: for feature in rows: nothing@0: for s, p, o in graph.triples((URIRef(local+feature), None, None)): nothing@0: col = p.split('/')[-1] nothing@0: if cat.count(col) != 0: nothing@0: matrix[index][columns.index(col+"-"+o)] = 1 nothing@0: nothing@0: index += 1 nothing@0: nothing@0: writeCXT(path+name+'.cxt', matrix, columns, rows) nothing@0: print ("wrote cxt data to "+path+name+".cxt") nothing@0: name = name.replace(' ', '\ ') nothing@0: os.system("fcastone -bc "+path+name+".cxt "+path+name+".dot") nothing@0: nothing@0: def constructBaseMatrix( name, path ): nothing@0: rows = [] nothing@0: columns = [] nothing@0: matrix = [] nothing@0: nothing@4: #cat = ["domain", "level", "temporalscale", "dimensions", "output", "tag"] nothing@15: cat = ["domain", "level", "temporalscale", "dimensionality", "model", "complexity"] nothing@0: catdict = {} nothing@0: nothing@0: for nm in cat: nothing@0: catdict[nm] = [] nothing@0: nothing@0: for ns, value in graph.namespaces(): nothing@0: if ns == 'af': nothing@0: local = value nothing@0: nothing@0: features = [] nothing@15: cns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn') nothing@15: dns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/temporalscale') nothing@0: for su in graph.subjects(RDF.type, OWL.Class): nothing@15: compcount = sum(1 for _ in graph.objects(su,cns)) nothing@15: domcount = sum(1 for _ in graph.objects(su,dns)) nothing@15: if compcount > 0 and domcount > 0 and su.find('MPEG7') == -1: nothing@0: features.append(su.split('/')[-1]) nothing@15: nothing@15: #features = features.sort() nothing@0: nothing@0: for it in features: nothing@0: rows.append(it) nothing@0: for su, pr, ob in graph.triples((URIRef(local+it), None, None)): nothing@0: col = pr.split('/')[-1] nothing@0: if cat.count(col) != 0: nothing@0: if catdict[col].count(ob) == 0: nothing@0: columns.append(ob.replace(' ', '-')+"-"+col.replace(' ', '-')) nothing@0: catdict[col].append(ob) nothing@0: nothing@0: rows.sort() nothing@0: columns.sort() nothing@0: nothing@0: for i in range(len(rows)): nothing@0: row = [] nothing@0: for j in range(len(columns)): nothing@0: row.append(0) nothing@0: matrix.append(row) nothing@0: nothing@0: index = 0 nothing@0: for feature in rows: nothing@0: for s, p, o in graph.triples((URIRef(local+feature), None, None)): nothing@0: col = p.split('/')[-1] nothing@0: if cat.count(col) != 0: nothing@0: matrix[index][columns.index(o.replace(' ', '-')+"-"+col.replace(' ', '-'))] = 1 nothing@0: nothing@0: index += 1 nothing@0: nothing@0: writeCXT(path+name+'.cxt', matrix, columns, rows) nothing@0: print ("wrote cxt data to "+path+name+".cxt") nothing@0: name = name.replace(' ', '\ ') nothing@0: os.system("fcastone -bc "+path+name+".cxt "+path+name+".dot")