view 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
line wrap: on
line source
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 getOnlyBaseFeatures():
    features = []
    cns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn')
    dns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/domain')
    for su in graph.subjects(RDF.type, OWL.Class):
        compcount = sum(1 for _ in graph.objects(su,cns))
        domcount = sum(1 for _ in graph.objects(su,dns))
        if compcount > 0 and domcount > 0:
            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"]
    cat = ["output", "domain", "level", "temporalscale", "dimensionality", "model"]
    catdict = {}

    for nm in cat:
        catdict[nm] = []

    for ns, value in graph.namespaces():
        if ns == 'af':
            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"]
    cat = ["domain", "level", "temporalscale", "dimensionality", "model", "complexity"]
    catdict = {}

    for nm in cat:
        catdict[nm] = []

    for ns, value in graph.namespaces():
        if ns == 'af':
            local = value

    features = []
    cns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/computedIn')
    dns = URIRef(u'http://sovarr.c4dm.eecs.qmul.ac.uk/features/temporalscale')
    for su in graph.subjects(RDF.type, OWL.Class):
        compcount = sum(1 for _ in graph.objects(su,cns))
        domcount = sum(1 for _ in graph.objects(su,dns))
        if compcount > 0 and domcount > 0 and su.find('MPEG7') == -1:
            features.append(su.split('/')[-1])
            
    #features = features.sort()

    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")