library.py 2.47 KB
Newer Older
Anze Vavpetic's avatar
Anze Vavpetic committed
1 2 3 4 5 6 7
import re
from string import ascii_lowercase as chars
from random import choice

from aleph import Aleph
from rsd import RSD

Anze Vavpetic's avatar
Anze Vavpetic committed
8 9
from services.webservice import WebService

Anze Vavpetic's avatar
Anze Vavpetic committed
10 11 12 13 14 15 16
def ilp_aleph(input_dict):
    aleph = Aleph()
    settings = input_dict['settings']
    mode = input_dict['mode']
    pos = input_dict['pos']
    neg = input_dict['neg']
    b = input_dict['b']
17
    # Parse settings provided via file
Anze Vavpetic's avatar
Anze Vavpetic committed
18 19
    if settings:
        aleph.settingsAsFacts(settings)
20 21 22
    # Parse settings provided as parameters (these have higher priority)
    for setting, def_val in Aleph.ESSENTIAL_PARAMS.items():
        aleph.set(setting, input_dict.get(setting, def_val))
Anze Vavpetic's avatar
Anze Vavpetic committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    # Run aleph
    theory = aleph.induce(mode, pos, neg, b)
    return {'theory' : theory}

def ilp_rsd(input_dict):
    rsd = RSD()
    settings = input_dict.get('settings',None)
    pos = input_dict.get('pos', None)
    neg = input_dict.get('neg', None)
    examples = input_dict.get('examples', None)
    b = input_dict['b']
    subgroups = True if input_dict['subgroups'] == 'true' else False
    # Parse settings
    if settings:
        rsd.settingsAsFacts(settings)
38 39 40
    # Parse settings provided as parameters (these have higher priority)
    for setting, def_val in RSD.ESSENTIAL_PARAMS.items():
        rsd.set(setting, input_dict.get(setting, def_val))
Anze Vavpetic's avatar
Anze Vavpetic committed
41 42 43
    # Run rsd
    features, arff, rules = rsd.induce(b, examples=examples, pos=pos, neg=neg, cn2sd=subgroups)
    return {'features' : features, 'arff' : arff, 'rules' : rules}
44 45

def ilp_sdmsegs_rule_viewer(input_dict):
Anze Vavpetic's avatar
Anze Vavpetic committed
46 47 48
    return {}

def ilp_sdmaleph(input_dict):
Anze Vavpetic's avatar
Anze Vavpetic committed
49
    ws = WebService('http://workflow.ijs.si:8081', 3600)
Anze Vavpetic's avatar
Anze Vavpetic committed
50 51 52 53 54 55 56 57 58 59 60 61 62
    response = ws.client.sdmaleph(
        examples=input_dict.get('examples'),
        mapping=input_dict.get('mapping'),
        ontologies=[{'ontology' : ontology} for ontology in input_dict.get('ontology')],
        relations=[{'relation' : relation} for relation in input_dict.get('relation')],
        posClassVal=input_dict.get('posClassVal') if input_dict.get('posClassVal') != '' else None,
        cutoff=input_dict.get('cutoff') if input_dict.get('cutoff') != '' else None,
        minPos=input_dict.get('minPos') if input_dict.get('minPos') != '' else None,
        noise=input_dict.get('noise') if input_dict.get('noise') != '' else None,
        clauseLen=input_dict.get('clauseLen') if input_dict.get('clauseLen') != '' else None,
        dataFormat=input_dict.get('dataFormat') if input_dict.get('dataFormat') != '' else None
    )
    return {'theory' : response['theory']}