Commit c3f0d930 authored by Anze Vavpetic's avatar Anze Vavpetic

added ilp package

parent c8e6b316
from aleph import Aleph
\ No newline at end of file
This diff is collapsed.
#
# Python interface to Aleph.
#
# author: Anze Vavpetic <anze.vavpetic@ijs.si>, 2011
#
import os.path
import shutil
import logging
import re
import tempfile
from stat import S_IREAD, S_IEXEC
from subprocess import Popen, PIPE
DEBUG = True
# Setup a logger
logger = logging.getLogger("Aleph [Python]")
logger.setLevel(logging.DEBUG if DEBUG else logging.INFO)
ch = logging.StreamHandler()
formatter = logging.Formatter("%(name)s %(levelname)s: %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
class Aleph(object):
# The aleph source file is presumed to be in the same dir as this file.
THIS_DIR = os.path.dirname(__file__) if os.path.dirname(__file__) else '.'
ALEPH_FN = 'aleph.pl'
YAP = '/usr/local/bin/yap'
RULES_SUFFIX = 'Rules'
SCRIPT = 'run_aleph.pl'
def __init__(self, verbosity=logging.NOTSET):
"""
Creates an Aleph object.
@param logging can be DEBUG, INFO or NOTSET (default). This controls the verbosity of the output.
"""
self.tmpdir = tempfile.mkdtemp()
self.aleph_script = '%s/%s' % (self.tmpdir, Aleph.ALEPH_FN)
self.postGoal = None
self.postScript = None
# Dictionary of non-default settings
self.settings = dict()
logger.setLevel(verbosity)
shutil.copy("%s/%s" % (Aleph.THIS_DIR, Aleph.ALEPH_FN), self.aleph_script)
def set(self, name, value):
"""
Sets the value of setting 'name' to 'value'.
"""
self.settings[name] = value
def settingsAsFacts(self, settings):
"""
Parses a string of settings in the form set(name1, val1), set(name2, val2)...
"""
pattern = re.compile('set\(([a-zA-Z0-9_]+),(\[a-zA-Z0-9_]+)\)')
pairs = pattern.findall(settings)
for name, val in pairs:
self.set(name, val)
def setPostScript(self, goal, script):
"""
After learning call the given script using 'goal'.
"""
self.postGoal = goal
self.postScript = script
def induce(self, mode, pos, neg, b, filestem='default'):
"""
Induce a theory in 'mode'.
@param filestem The base name of this experiment.
@param mode In which mode to induce rules.
@param pos String of positive examples.
@param neg String of negative examples.
@param b String with background knowledge.
"""
# Write the inputs to appropriate files.
self.__prepare(filestem, pos, neg, b)
# Make a script to run aleph (with appropriate settings, stack/heap sizes, ...).
self.__script(mode, filestem)
logger.info("Running aleph...")
# Run the aleph script.
p = Popen(['./' + Aleph.SCRIPT], cwd=self.tmpdir, stdout=PIPE)
stdout_str, stderr_str = p.communicate()
logger.debug(stdout_str)
logger.debug(stderr_str)
logger.info("Done.")
# Return the rules written in the output file.
rules = open('%s/%s' % (self.tmpdir, filestem + Aleph.RULES_SUFFIX)).read()
#shutil.copy('%s/%s.py' % (self.tmpdir, filestem), '/home/anzev/programiranje/sdm/results/')
# Cleanup.
self.__cleanup()
return rules
def __prepare(self, filestem, pos, neg, b):
"""
Prepares the needed files.
"""
posFile = open('%s/%s.f' % (self.tmpdir, filestem), 'w')
negFile = open('%s/%s.n' % (self.tmpdir, filestem), 'w')
bFile = open('%s/%s.b' % (self.tmpdir, filestem), 'w')
posFile.write(pos)
negFile.write(neg)
bFile.write(b)
posFile.close()
negFile.close()
bFile.close()
def __cleanup(self):
"""
Cleans up all the temporary files.
"""
try:
shutil.rmtree(self.tmpdir)
except:
logger.info('Problem removing temporary files. The files are probably in use.')
def __script(self, mode, filestem):
"""
Makes the script file to be run by yap.
"""
scriptPath = '%s/%s' % (self.tmpdir, Aleph.SCRIPT)
script = open(scriptPath, 'w')
#print scriptPath
# Permit the owner to execute and read this script
os.chmod(scriptPath, S_IREAD | S_IEXEC)
cat = lambda x: script.write(x + '\n')
cat("#!%s -L -s50000 -h200000" % Aleph.YAP)
cat(":- initialization(run_aleph).")
cat("run_aleph :- ")
cat("consult(aleph),")
cat("read_all('%s')," % filestem)
# Cat all the non-default settings
for setting, value in self.settings.items():
cat("set(%s, %s)," % (setting, value))
cat("%s," % mode)
cat("write_rules('%s')%s" % (filestem + Aleph.RULES_SUFFIX, ',' if self.postScript else '.'))
if self.postScript:
cat(self.postGoal + ".")
cat(self.postScript)
script.close()
\ No newline at end of file
[
{
"pk": 72,
"model": "workflows.category",
"fields": {
"uid": "ed859be5-cc13-46b9-b249-c8f026732c1c",
"parent": null,
"workflow": null,
"user": null,
"order": 1,
"name": "ILP"
}
},
{
"pk": 300,
"model": "workflows.abstractwidget",
"fields": {
"category": 72,
"treeview_image": "",
"name": "Aleph",
"is_streaming": false,
"uid": "b8289304-389e-414a-98ab-273b16478cee",
"interaction_view": "",
"image": "",
"package": "ilp",
"static_image": "ilp.png",
"post_interact_action": "",
"user": null,
"visualization_view": "",
"action": "ilp_aleph",
"wsdl_method": "",
"wsdl": "",
"interactive": false,
"has_progress_bar": false,
"order": 1,
"description": ""
}
},
{
"pk": 988,
"model": "workflows.abstractinput",
"fields": {
"widget": 300,
"name": "settings",
"short_name": "set",
"uid": "bfe16b08-89b1-4835-875c-6b0302fb1da3",
"default": "",
"required": false,
"multi": false,
"parameter_type": null,
"variable": "settings",
"parameter": false,
"order": 1,
"description": "settings facts"
}
},
{
"pk": 989,
"model": "workflows.abstractinput",
"fields": {
"widget": 300,
"name": "mode",
"short_name": "mod",
"uid": "00b174f3-250d-4c47-9a60-b515e5260e0c",
"default": "induce",
"required": true,
"multi": false,
"parameter_type": "select",
"variable": "mode",
"parameter": true,
"order": 2,
"description": "induction mode"
}
},
{
"pk": 101,
"model": "workflows.abstractoption",
"fields": {
"uid": "8e5631e5-6497-4720-9e26-5936619caf5c",
"abstract_input": 989,
"value": "induce",
"name": "induce"
}
},
{
"pk": 105,
"model": "workflows.abstractoption",
"fields": {
"uid": "92f66b17-02fd-4bb6-b4cb-03bb54b403f6",
"abstract_input": 989,
"value": "induce_constraints",
"name": "induce_constraints"
}
},
{
"pk": 102,
"model": "workflows.abstractoption",
"fields": {
"uid": "ce82b005-81e4-4997-80c3-05731651869a",
"abstract_input": 989,
"value": "induce_cover",
"name": "induce_cover"
}
},
{
"pk": 107,
"model": "workflows.abstractoption",
"fields": {
"uid": "968d079a-871c-4cae-b15e-c8e7b61f3b8c",
"abstract_input": 989,
"value": "induce_features",
"name": "induce_features"
}
},
{
"pk": 103,
"model": "workflows.abstractoption",
"fields": {
"uid": "0103c466-0e2e-47fe-b182-0a034beb7eed",
"abstract_input": 989,
"value": "induce_max",
"name": "induce_max"
}
},
{
"pk": 106,
"model": "workflows.abstractoption",
"fields": {
"uid": "78cb938d-16b8-4c7c-b712-b59e27aa837d",
"abstract_input": 989,
"value": "induce_modes",
"name": "induce_modes"
}
},
{
"pk": 104,
"model": "workflows.abstractoption",
"fields": {
"uid": "45f5cc84-b097-439a-9363-d2299c540913",
"abstract_input": 989,
"value": "induce_tree",
"name": "induce_tree"
}
},
{
"pk": 990,
"model": "workflows.abstractinput",
"fields": {
"widget": 300,
"name": "pos",
"short_name": "pos",
"uid": "03788382-f3b9-4580-8e85-b600874ef643",
"default": "",
"required": true,
"multi": false,
"parameter_type": null,
"variable": "pos",
"parameter": false,
"order": 3,
"description": "positive examples"
}
},
{
"pk": 991,
"model": "workflows.abstractinput",
"fields": {
"widget": 300,
"name": "neg",
"short_name": "neg",
"uid": "01529349-3300-41c5-8585-3f7a90175120",
"default": "",
"required": true,
"multi": false,
"parameter_type": null,
"variable": "neg",
"parameter": false,
"order": 4,
"description": "negative examples"
}
},
{
"pk": 992,
"model": "workflows.abstractinput",
"fields": {
"widget": 300,
"name": "b",
"short_name": "b",
"uid": "2ec81e9e-d911-46a1-a95d-2eaf349443a8",
"default": "",
"required": true,
"multi": false,
"parameter_type": null,
"variable": "b",
"parameter": false,
"order": 5,
"description": "background knowledge"
}
},
{
"pk": 355,
"model": "workflows.abstractoutput",
"fields": {
"widget": 300,
"name": "theory",
"short_name": "the",
"variable": "theory",
"uid": "d1d8839b-a2f1-4b82-b759-7c6c7d7d57e5",
"order": 1,
"description": "theory"
}
},
{
"pk": 301,
"model": "workflows.abstractwidget",
"fields": {
"category": 72,
"treeview_image": "",
"name": "RSD",
"is_streaming": false,
"uid": "dabcb4ed-ae86-4f3e-8d3e-bb0306ff11f8",
"interaction_view": "",
"image": "",
"package": "ilp",
"static_image": "ilp.png",
"post_interact_action": "",
"user": null,
"visualization_view": "",
"action": "ilp_rsd",
"wsdl_method": "",
"wsdl": "",
"interactive": false,
"has_progress_bar": false,
"order": 1,
"description": "Relational subgroup discovery by Zelezny et al."
}
},
{
"pk": 993,
"model": "workflows.abstractinput",
"fields": {
"widget": 301,
"name": "settings",
"short_name": "set",
"uid": "a1660c9f-952d-4f29-9f23-9b0be5fe8cea",
"default": "",
"required": false,
"multi": false,
"parameter_type": null,
"variable": "settings",
"parameter": false,
"order": 1,
"description": "settings"
}
},
{
"pk": 994,
"model": "workflows.abstractinput",
"fields": {
"widget": 301,
"name": "pos",
"short_name": "pos",
"uid": "676ef826-7dd9-415a-8a8a-d82146dcc95a",
"default": "",
"required": false,
"multi": false,
"parameter_type": null,
"variable": "pos",
"parameter": false,
"order": 2,
"description": "positive examples"
}
},
{
"pk": 995,
"model": "workflows.abstractinput",
"fields": {
"widget": 301,
"name": "neg",
"short_name": "neg",
"uid": "0b46921d-50fe-46b1-a1e4-67c918160a7f",
"default": "",
"required": false,
"multi": false,
"parameter_type": null,
"variable": "neg",
"parameter": false,
"order": 3,
"description": "negative examples"
}
},
{
"pk": 996,
"model": "workflows.abstractinput",
"fields": {
"widget": 301,
"name": "examples",
"short_name": "ex",
"uid": "817a82dc-2264-4181-a31a-6a7ff2b6c882",
"default": "",
"required": false,
"multi": false,
"parameter_type": null,
"variable": "examples",
"parameter": false,
"order": 4,
"description": "classified examples (pos+neg in one file)"
}
},
{
"pk": 997,
"model": "workflows.abstractinput",
"fields": {
"widget": 301,
"name": "b",
"short_name": "b",
"uid": "746dac3a-bcf9-4546-b0cb-ccfea866707a",
"default": "",
"required": true,
"multi": false,
"parameter_type": null,
"variable": "b",
"parameter": false,
"order": 5,
"description": "background knowledge"
}
},
{
"pk": 998,
"model": "workflows.abstractinput",
"fields": {
"widget": 301,
"name": "subgroups",
"short_name": "sg",
"uid": "1c982c21-0b97-40d1-a35e-30ba16342b00",
"default": "",
"required": true,
"multi": false,
"parameter_type": "checkbox",
"variable": "subgroups",
"parameter": true,
"order": 6,
"description": "find subgroups as well"
}
},
{
"pk": 356,
"model": "workflows.abstractoutput",
"fields": {
"widget": 301,
"name": "features",
"short_name": "frs",
"variable": "features",
"uid": "1097065e-83f9-4dfa-89b8-775d500caa89",
"order": 1,
"description": "features as prolog facts"
}
},
{
"pk": 357,
"model": "workflows.abstractoutput",
"fields": {
"widget": 301,
"name": "arff",
"short_name": "arf",
"variable": "arff",
"uid": "20b53049-5a43-46f8-9d1d-b49d8db2b1bc",
"order": 2,
"description": "arff file of the propositionalized data"
}
},
{
"pk": 358,
"model": "workflows.abstractoutput",
"fields": {
"widget": 301,
"name": "rules",
"short_name": "rul",
"variable": "rules",
"uid": "3f70b720-fd68-4770-936b-68512046a12e",
"order": 3,
"description": "rules as prolog facts"
}
}
]
\ No newline at end of file
from django.shortcuts import render
import re
from string import ascii_lowercase as chars
from random import choice
from aleph import Aleph
from rsd import RSD
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']
# Parse settings
if settings:
aleph.settingsAsFacts(settings)
# 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)
# Run rsd
features, arff, rules = rsd.induce(b, examples=examples, pos=pos, neg=neg, cn2sd=subgroups)
return {'features' : features, 'arff' : arff, 'rules' : rules}
from rsd import RSD
\ No newline at end of file
:-modeh(1,train(+train)). % main key
:-modeb(1,hasCar(+train,-car)).
:-modeb(1,carshape(+car,-car_shape)).
:-modeb(1,instantiate(+car_shape)). % a constant value should be considered for this var when generating features
:-modeb(1,carlength(+car,-car_length)).
:-modeb(1,instantiate(+car_length)). % a constant value should be considered for this var when generating features
:-modeb(1,has_sides(+car,-sides)).
:-modeb(1,instantiate(+sides)). % a constant value should be considered for this var when generating features
:-modeb(1,has_roof(+car,-roof)).
:-modeb(1,instantiate(+roof)). % a constant value should be considered for this var when generating features
:-modeb(1,has_wheels(+car,-wheels)).
:-modeb(1,instantiate(+wheels)). % a constant value should be considered for this var when generating features
:-modeb(1,has_load(+car,-load)).
:-modeb(1,loadshape(+load,-load_shape)).
:-modeb(1,instantiate(+load_shape)). % a constant value should be considered for this var when generating features
:-modeb(1,loadnum(+load,-load_num)).
:-modeb(1,instantiate(+load_num)). % a constant value should be considered for this var when generating features
%:-modeb(1,notSame(+car,+car)).
% settings considered by "featurize.pl":
% ... none - all default
% settings considered by "process.pl"
% ... none - all default
% settings considered by "rules.pl"
:-set(eval_threshold,0.01).
:-set(sig_threshold,0). % chi^2 value threshold does not make sense with 20 train instances -> therefore 0
hasCar(Train,Car):-
my_member(Car,Train).
carshape(c(_,S,_,_,_,_,_),S).
carlength(c(_,_,LE,_,_,_,_),LE).
has_sides(c(_,_,_,SD,_,_,_),SD).
has_roof(c(_,_,_,_,R,_,_),R).
has_wheels(c(_,_,_,_,_,W,_),W).
has_load(c(_,_,_,_,_,_,L),L).
loadshape(l(LS,_),LS).
loadnum(l(_,LN),LN).
% 20 Eastbound and Westbound trains
train(east,[c(1,rectangle,short,not_double,none,2,l(circle,1)),c(2,rectangle,
long,not_double,none,3,l(hexagon,1)),c(3,rectangle,short,
not_double,peaked,2,l(triangle,1)),c(4,rectangle,long,
not_double,none,2,l(rectangle,3))]).
train(east,[c(1,rectangle,short,not_double,flat,2,l(circle,2)),c(2,bucket,
short,not_double,none,2,l(rectangle,1)),c(3,u_shaped,
short,not_double,none,2,l(triangle,1))]).
train(east,[c(1,rectangle,long,not_double,flat,3,l(utriangle,1)),c(2,hexagon,
short,not_double,flat,2,l(triangle,1)),c(3,rectangle,
short,not_double,none,2,l(circle,1))]).
train(east,[c(1,rectangle,short,not_double,none,2,l(rectangle,1)),c(2,ellipse,
short,not_double,arc,2,l(diamond,1)),c(3,rectangle,short,
double,none,2,l(triangle,1)),c(4,bucket,short,not_double,
none,2,l(triangle,1))]).
train(east,[c(1,rectangle,short,not_double,flat,2,l(circle,1)),c(2,rectangle,
long,not_double,flat,3,l(rectangle,1)),c(3,rectangle,
short,double,none,2,l(triangle,1))]).
train(east,[c(1,rectangle,long,not_double,jagged,3,l(rectangle,1)),c(2,hexagon,
short,not_double,flat,2,l(circle,1)),c(3,rectangle,short,
not_double,none,2,l(triangle,1)),c(4,rectangle,long,not_double,
jagged,2,l(rectangle,0))]).
train(east,[c(1,rectangle,long,not_double,none,2,l(hexagon,1)),c(2,rectangle,
short,not_double,none,2,l(rectangle,1)),c(3,rectangle,
short,not_double,flat,2,l(triangle,1))]).
train(east,[c(1,rectangle,short,not_double,peaked,2,l(rectangle,1)),c(2,
bucket,short,not_double,none,2,l(rectangle,1)),c(3,rectangle,
long,not_double,flat,2,l(circle,1)),c(4,rectangle,short,
not_double,none,2,l(rectangle,1))]).
train(east,[c(1,rectangle,long,not_double,none,2,l(rectangle,3)),c(2,rectangle,
short,not_double,none,2,l(circle,1)),c(3,rectangle,long,
not_double,jagged,3,l(hexagon,1)),c(4,u_shaped,short,
not_double,none,2,l(triangle,1))]).
train(east,[c(1,bucket,short,not_double,none,2,l(triangle,1)),c(2,u_shaped,
short,not_double,none,2,l(circle,1)),c(3,rectangle,short,
not_double,none,2,l(triangle,1)),c(4,rectangle,short,
not_double,none,2,l(triangle,1))]).
train(west,[c(1,rectangle,short,not_double,none,2,l(triangle,1)),
c(2,rectangle, long,not_double,flat,2,l(circle,3))]).
train(west,[c(1,rectangle,long,not_double,jagged,2,l(circle,0)),c(2,u_shaped,
short,not_double,none,2,l(triangle,1)),c(3,rectangle,short,
double,none,2,l(circle,1))]).
train(west,[c(1,u_shaped,short,not_double,none,2,l(circle,1)),c(2,rectangle,
long,not_double,flat,3,l(rectangle,1))]).
train(west,[c(1,bucket,short,not_double,none,2,l(circle,1)),c(2,rectangle,
short,not_double,none,2,l(rectangle,1)),c(3,rectangle,
long,not_double,jagged,3,l(rectangle,1)),c(4,bucket,short,
not_double,none,2,l(circle,1))]).
train(west,[c(1,rectangle,long,not_double,none,2,l(rectangle,2)),c(2,u_shaped,