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

from aleph import Aleph
from rsd import RSD
Matic Perovšek's avatar
Matic Perovšek committed
9
from wordification import Wordification
Anze Vavpetic's avatar
Anze Vavpetic committed
10
from treeliker import TreeLiker
11
from security import check_input
Anze Vavpetic's avatar
Anze Vavpetic committed
12

Anze Vavpetic's avatar
Anze Vavpetic committed
13 14
from services.webservice import WebService

Anze Vavpetic's avatar
Anze Vavpetic committed
15 16 17 18 19 20 21
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']
22
    # Parse settings provided via file
Anze Vavpetic's avatar
Anze Vavpetic committed
23 24
    if settings:
        aleph.settingsAsFacts(settings)
25 26 27
    # 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))
28 29 30
    # Check for illegal predicates
    for pl_script in [b, pos, neg]:
        check_input(pl_script)
Anze Vavpetic's avatar
Anze Vavpetic committed
31
    # Run aleph
32 33
    result = aleph.induce(mode, pos, neg, b)
    return {'theory': result}
Anze Vavpetic's avatar
Anze Vavpetic committed
34 35 36 37 38 39 40 41

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']
42
    subgroups = input_dict['subgroups'] == 'true'
Anze Vavpetic's avatar
Anze Vavpetic committed
43 44 45
    # Parse settings
    if settings:
        rsd.settingsAsFacts(settings)
46 47 48
    # 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))
49 50 51
    # Check for illegal predicates
    for pl_script in [b, pos, neg, examples]:
        check_input(pl_script)
Anze Vavpetic's avatar
Anze Vavpetic committed
52 53 54
    # Run rsd
    features, arff, rules = rsd.induce(b, examples=examples, pos=pos, neg=neg, cn2sd=subgroups)
    return {'features' : features, 'arff' : arff, 'rules' : rules}
55 56

def ilp_sdmsegs_rule_viewer(input_dict):
Anze Vavpetic's avatar
Anze Vavpetic committed
57 58 59
    return {}

def ilp_sdmaleph(input_dict):
60
    import orange
61
    ws = WebService('http://vihar.ijs.si:8097', 3600)
62 63 64 65 66 67 68 69 70 71 72 73
    data = input_dict.get('examples')
    if isinstance(data, orange.ExampleTable):
        with tempfile.NamedTemporaryFile(suffix='.tab', delete=True) as f:
            data.save(f.name)
            examples = f.read()
    elif isinstance(data, list):
        examples = json.dumps(data)
    elif isinstance(data, str):
        examples = data
    else:
        raise Exception('Illegal examples format. \
                         Supported formats: str, list or Orange')
Anze Vavpetic's avatar
Anze Vavpetic committed
74
    response = ws.client.sdmaleph(
75
        examples=examples,
Anze Vavpetic's avatar
Anze Vavpetic committed
76 77 78 79 80 81 82 83 84 85
        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
    )
Matic Perovšek's avatar
Matic Perovšek committed
86 87 88 89 90 91 92
    return {'theory' : response['theory']}


def ilp_wordification(input_dict):
    target_table = input_dict.get('target_table',None)
    other_tables = input_dict.get('other_tables', None)
    context = input_dict.get('context', None)
93 94
    word_att_length = int(input_dict.get('f_ngram_size', 1))
    wordification = Wordification(target_table,other_tables,context,word_att_length)
Janez K's avatar
merge  
Janez K committed
95
    return {'corpus' : wordification.wordify()}
Anze Vavpetic's avatar
Anze Vavpetic committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111


def ilp_treeliker(input_dict):
    template = input_dict['template']
    dataset = input_dict['dataset']
    settings = {
        'algorithm': input_dict.get('algorithm'),
        'minimum_frequency': input_dict.get('minimum_frequency'),
        'covered_class': input_dict.get('covered_class'),
        'maximum_size': input_dict.get('maximum_size'),
        'use_sampling': input_dict.get('use_sampling'),
        'sample_size': input_dict.get('sample_size'),
        'max_degree': input_dict.get('max_degree')
    }
    arff = TreeLiker(dataset, template).run(settings=settings)
    return {'arff': arff}