Commit 2fd96511 authored by Finzel, Bettina's avatar Finzel, Bettina
Browse files

Animal example

parent bd54b450
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 01 14:28:02 2021
@author: Bettina Finzel
"""
import pexpect
import os
data_set = "semanticnet"
aleph_path = os.getcwd()
aleph_call = "yap -l aleph.pl -g 'read_all(" + data_set + ").'"
child_process = pexpect.spawn(aleph_call, timeout = 500, maxread = 200000)
child_process.sendline('induce.')
index = child_process.expect(["[total clauses constructed]", pexpect.TIMEOUT])
if index == 0:
child_process.sendline('write_rules.')
index2 = child_process.expect(["yes", pexpect.TIMEOUT])
if index2 == 0:
child_process.sendline('halt.')
index3 = child_process.expect(["yes", pexpect.TIMEOUT])
if index3 == 0:
child_process.terminate()
else:
child_process.terminate()
else:
child_process.terminate()
else:
child_process.terminate()
### You can adapt the files in this repository with respect to your use case. ###
In ILP_model_learner.py change the data_set field according to the name of your Aleph input files,
written in Prolog. Per default it is set to "semanticnet". The script loads all Aleph files,
which are located in the same directory as the aleph.pl file and induces a model.
Aleph needs three input files: the background file *.b, the file with positive example *.f,
and the file with negative examples *.n.
Check out the Aleph manual for more information about how to set up the files:
https://www.cs.ox.ac.uk/activities/programinduction/Aleph/aleph.html
In our code, Aleph is setup such that it produces output files for each run ("Record.txt"),
and for the learned model ("Result.txt"). The result file is stored by using the command "write_rules.".
### In order to run the aleph.pl file with our code, you will need an installation of Yap ###
The installation is currently only available for Linux operating systems and can be simply downloaded
using the command "apt-get install yap".
\ No newline at end of file
[settings]
abduce=false
cache_clauselength=3
caching=false
check_good=false
check_redundant=false
check_useless=false
clauselength=10
condition=false
confidence=0.95
construct_bottom=saturation
depth=10
evalfn=coverage
explore=false
good=false
greedy=true
gsamplesize=100
i=10
interactive=false
language=(+inf)
lazy_negs=false
lazy_on_contradiction=false
lazy_on_cost=false
lookahead=1
max_abducibles=2
max_features=(+inf)
minacc=0.0
mingain=0.05
minpos=2
minposfrac=0
minscore=0
moves=5
newvars=(+inf)
nodes=180000
noise=0
nreduce_bottom=false
openlist=(+inf)
optimise_clauses=false
permute_bottom=false
portray_examples=false
portray_hypothesis=false
portray_literals=false
portray_search=false
print=4
prior=[0.5-pos,0.5-neg]
proof_strategy=restricted_sld
prooftime=(+inf)
prune_tree=false
record=true
recordfile=Record
recordfile_stream=$stream(4)
refine=false
refineop=false
resample=1
rls_type=gsat
rulefile=Result
samplesize=0
search=bf
searchstrat=bf
searchtime=(+inf)
skolemvars=10000
splitvars=false
stage=command
store_bottom=false
subsample=false
subsamplesize=(+inf)
threads=1
train_neg=semanticnet.n
train_pos=semanticnet.f
tries=10
typeoverlap=0.95
uniform_sample=false
updateback=true
verbosity=2
version=5
[sat] [1]
[clauses constructed] [180000]
[search time] [19.261]
[best clause]
tracks_down(A,B) :-
is_(B,plant), is_(A,herbivore).
[positive examples left] [3]
[estimated time to finish (secs)] [11.5566]
[sat] [3]
[clauses constructed] [598]
[search time] [0.0210000000000008]
[best clause]
tracks_down(A,B) :-
is_(B,herbivore), is_(A,carnivore).
[positive examples left] [0]
[estimated time to finish (secs)] [0.0]
[theory]
[Rule 1] [Pos cover = 5 Neg cover = 0]
tracks_down(A,B) :-
is_(B,plant), is_(A,herbivore).
[positive examples covered]
tracks_down(bobby,dandelion).
tracks_down(fluffy,clover).
tracks_down(bobby,parsley).
tracks_down(fluffy,rosemary).
tracks_down(tipsie,rosemary).
[negative examples covered]
[Rule 2] [Pos cover = 3 Neg cover = 0]
tracks_down(A,B) :-
is_(B,herbivore), is_(A,carnivore).
[positive examples covered]
tracks_down(samson,fluffy).
tracks_down(bella,bobby).
tracks_down(bella,tipsie).
[negative examples covered]
[Training set performance]
Actual
+ -
+ 8 0 8
Pred
- 0 8 8
8 8 16
Accuracy = 1.0
[Training set summary] [[8,0,0,8]]
[time taken] [20.522]
[total clauses constructed] [180598]
tracks_down(A,B) :-
is_(B,plant), is_(A,herbivore).
tracks_down(A,B) :-
is_(B,herbivore), is_(A,carnivore).
This diff is collapsed.
% Head declaration for target class
:- modeh(1, stage_t2(+tissue,+tissue)).
% Body declarations for relations
:- modeb(*, is_(+tissue,+tissue)).
:- modeb(*, is_(+tissue,-tissue)).
:- modeb(*, is_(-tissue,+tissue)).
:- modeb(*, is_(+tissue,#tissue)).
:- modeb(*, is_(#tissue,+tissue)).
:- modeb(*, is_(-tissue,#tissue)).
:- modeb(*, is_(#tissue,-tissue)).
:- modeb(*, is_(#tissue,#tissue)).
%:- modeb(*, has(+tissue,+tissue)).
%:- modeb(*, has(+tissue,-tissue)).
%:- modeb(*, has(-tissue,+tissue)).
%:- modeb(*, has(+tissue,#tissue)).
%:- modeb(*, has(#tissue,+tissue)).
%:- modeb(*, has(-tissue,#tissue)).
%:- modeb(*, has(#tissue,-tissue)).
%:- modeb(*, has(#tissue,#tissue)).
:- modeb(*, contains(+tissue,+tissue)).
:- modeb(*, contains(+tissue,-tissue)).
:- modeb(*, contains(-tissue,+tissue)).
:- modeb(*, contains(+tissue,#tissue)).
:- modeb(*, contains(#tissue,+tissue)).
:- modeb(*, contains(-tissue,#tissue)).
:- modeb(*, contains(#tissue,-tissue)).
:- modeb(*, contains(#tissue,#tissue)).
:- modeb(*, invades(+tissue,+tissue)).
:- modeb(*, invades(+tissue,-tissue)).
:- modeb(*, invades(-tissue,+tissue)).
:- modeb(*, invades(+tissue,#tissue)).
:- modeb(*, invades(#tissue,+tissue)).
:- modeb(*, invades(-tissue,#tissue)).
:- modeb(*, invades(#tissue,-tissue)).
:- modeb(*, invades(#tissue,#tissue)).
:- modeb(*, intersects(+tissue,+tissue)).
:- modeb(*, intersects(+tissue,-tissue)).
:- modeb(*, intersects(-tissue,+tissue)).
:- modeb(*, intersects(+tissue,#tissue)).
:- modeb(*, intersects(#tissue,+tissue)).
:- modeb(*, intersects(-tissue,#tissue)).
:- modeb(*, intersects(#tissue,-tissue)).
:- modeb(*, intersects(#tissue,#tissue)).
% Determinations
:- determination(stage_t2/2, is_/2).
:- determination(stage_t2/2, contains/2).
:- determination(stage_t2/2, invades/2).
%:- determination(stage_t2/2, has/2).
% Aleph settings
:- set(i, 10).
:- set(clauselength, 10).
:- set(minpos, 2).
:- set(minscore, 0).
:- set(verbosity, 2).
:- set(noise, 0).
:- set(nodes, 180000).
:- set(rulefile,'Result').
:- set(record, true).
:- set(recordfile, 'Record').
% semantic concepts
is_a(mucosa,tissue).
is_a(submucosa,tissue).
is_a(muscle,tissue).
is_a(fat,tissue).
% background of examples
% T2
contains(scan_0708,tissue_1718).
contains(scan_0708,tissue_1818).
contains(scan_0708,tissue_1918).
contains(scan_0708,tissue_1118).
contains(scan_0001,tissue_1615).
contains(scan_0001,tissue_1715).
contains(scan_0001,tissue_1815).
contains(scan_0001,tissue_1115).
contains(scan_0002,tissue_1413).
contains(scan_0002,tissue_1513).
contains(scan_0002,tissue_1613).
contains(scan_0002,tissue_1113).
is_a(tissue_1718,tumor).
is_a(tissue_1615,tumor).
is_a(tissue_1413,tumor).
is_a(tissue_1818,muscle).
is_a(tissue_1715,muscle).
is_a(tissue_1513,muscle).
is_a(tissue_1918,submucosa).
is_a(tissue_1815,submucosa).
is_a(tissue_1613,submucosa).
is_a(tissue_1118,mucosa).
is_a(tissue_1115,mucosa).
is_a(tissue_1113,mucosa).
intersects(tissue_1718,tissue_1818).
intersects(tissue_1615,tissue_1715).
intersects(tissue_1413,tissue_1513).
intersects(tissue_1718,tissue_1918).
intersects(tissue_1615,tissue_1815).
intersects(tissue_1413,tissue_1613).
% Contrast class
contains(scan_2728,tissue_2718).
contains(scan_2728,tissue_2818).
contains(scan_2728,tissue_2918).
contains(scan_2728,tissue_2118).
contains(scan_0003,tissue_2615).
contains(scan_0003,tissue_2715).
contains(scan_0003,tissue_2815).
contains(scan_0003,tissue_2115).
contains(scan_0004,tissue_2413).
contains(scan_0004,tissue_2513).
contains(scan_0004,tissue_2613).
contains(scan_0004,tissue_2113).
is_a(tissue_2718,tumor).
is_a(tissue_2615,tumor).
is_a(tissue_2413,tumor).
is_a(tissue_2818,muscle).
is_a(tissue_2715,muscle).
is_a(tissue_2513,muscle).
is_a(tissue_2918,submucosa).
is_a(tissue_2815,submucosa).
is_a(tissue_2613,submucosa).
is_a(tissue_2118,mucosa).
is_a(tissue_2115,mucosa).
is_a(tissue_2113,mucosa).
intersects(tissue_2718,tissue_2918).
intersects(tissue_2615,tissue_2815).
intersects(tissue_2413,tissue_2613).
intersects(tissue_2718,tissue_2118).
intersects(tissue_2615,tissue_2115).
intersects(tissue_2413,tissue_2113).
% reasoning rules
is_(A,B) :- is_a(A,B).
is_(A,B) :- is_a(A,C), is_(C,B).
invades(A,B) :- intersects(A,B).
%has(A,X) :- has_p(A,X).
%has(X,Z) :- has_p(X,Y), has(Y,Z).
%has(A,X) :- is_(A,B), has(B,X).
%has(A,X) :- has_p(A,Y), is_(Y,X).
stage_t2(scan_0708).
stage_t2(scan_0001).
stage_t2(scan_0002).
stage_t2(scan_2728).
stage_t2(scan_0003).
stage_t2(scan_0004).
% Head declaration for target class
:- modeh(1, tracks_down(+being,+being)).
% Body declarations for relations
:- modeb(*, is_(+being,+being)).
:- modeb(*, is_(+being,-being)).
:- modeb(*, is_(-being,+being)).
:- modeb(*, is_(+being,#being)).
:- modeb(*, is_(#being,+being)).
:- modeb(*, is_(-being,#being)).
:- modeb(*, is_(#being,-being)).
:- modeb(*, is_(#being,#being)).
:- modeb(*, has(+being,+being)).
:- modeb(*, has(+being,-being)).
:- modeb(*, has(-being,+being)).
:- modeb(*, has(+being,#being)).
:- modeb(*, has(#being,+being)).
:- modeb(*, has(-being,#being)).
:- modeb(*, has(#being,-being)).
:- modeb(*, has(#being,#being)).
%:- modeb(*, is_a(+being,+being)).
%:- modeb(*, is_a(+being,-being)).
%:- modeb(*, is_a(-being,+being)).
%:- modeb(*, is_a(+being,#being)).
%:- modeb(*, is_a(#being,+being)).
%:- modeb(*, is_a(-being,#being)).
%:- modeb(*, is_a(#being,-being)).
%:- modeb(*, is_a(#being,#being)).
%:- modeb(*, has_p(+being,+being)).
%:- modeb(*, has_p(+being,-being)).
%:- modeb(*, has_p(-being,+being)).
%:- modeb(*, has_p(+being,#being)).
%:- modeb(*, has_p(#being,+being)).
%:- modeb(*, has_p(-being,#being)).
%:- modeb(*, has_p(#being,-being)).
%:- modeb(*, has_p(#being,#being)).
% Determinations
:- determination(tracks_down/2, is_/2).
:- determination(tracks_down/2, has/2).
%:- determination(tracks_down/2, is_a/2).
%:- determination(tracks_down/2, has_p/2).
% Aleph settings
:- set(i, 10).
:- set(clauselength, 10).
:- set(minpos, 2).
:- set(minscore, 0).
:- set(verbosity, 2).
:- set(noise, 0).
:- set(nodes, 180000).
:- set(rulefile,'Result').
:- set(record, true).
:- set(recordfile, 'Record').
% semantic concepts
is_a(plant,being).
is_a(animal,being).
is_a(flower,plant).
is_a(clover,flower).
is_a(dandelion,flower).
is_a(fish,animal).
is_a(bird,animal).
is_a(mammal,animal).
is_a(herbivore,mammal).
is_a(carnivore,mammal).
is_a(rabbit,herbivore).
is_a(fox,carnivore).
is_a(dog,carnivore).
is_a(stomach,organ).
has_p(being,metabolism).
has_p(animal,stomach).
has_p(fish,gills).
has_p(bird,feathers).
has_p(mammal,fur).
is_a(herb,plant).
is_a(parsley,herb).
is_a(rosemary,herb).
is_a(mouse,herbivore).
% background of examples
is_a(bobby,rabbit).
is_a(fluffy,rabbit).
is_a(tweety,bird).
is_a(blubbly,fish).
is_a(bello,dog).
is_a(samson,dog).
is_a(argo,dog).
is_a(bella,fox).
is_a(tipsie,mouse).
% reasoning rules
is_(A,B) :- is_a(A,B).
is_(A,B) :- is_a(A,C), is_(C,B).
has(A,X) :- has_p(A,X).
has(X,Z) :- has_p(X,Y), has(Y,Z).
has(A,X) :- is_(A,B), has(B,X).
has(A,X) :- has_p(A,Y), is_(Y,X).
tracks_down(bobby,dandelion).
tracks_down(fluffy,clover).
tracks_down(samson,fluffy).
tracks_down(bella,bobby).
tracks_down(bella,tipsie).
tracks_down(bobby,parsley).
tracks_down(fluffy,rosemary).
tracks_down(tipsie,rosemary).
tracks_down(argo,argo).
tracks_down(dandelion,bobby).
tracks_down(bobby,bobby).
tracks_down(blubbly,samson).
tracks_down(fluffy,argo).
tracks_down(clover,clover).
tracks_down(tipsie,bella).
tracks_down(rosemary,tipsie).
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 20 12:26:27 2021
@author: Bettina Finzel
"""
# Imports
import os
from os import path
import pexpect
from shutil import *
import regex as re
data_set = "semanticnet"
target_class = "tracks_down"
# Create a rule file for every rule from the learned model
print(os.getcwd())
tree_path = os.getcwd()
path_parent = os.path.dirname(tree_path)
os.chdir(path_parent)
model_path = os.getcwd() + "/1_Model"
model_file = model_path + "/Result"
# record the lines of the model file
count = len(open(model_file).readlines())
print("The file has " + str(count) + " lines.")
list_of_rules = []
lines = open(model_file).readlines()
line_index = 0
while line_index < count:
head_check = lines[line_index]
if ":-" in head_check:
# add the head to the rule
concatenated_rule = head_check
# skip the head and iterate over lines to check, when the fullstop comes.
index = line_index + 1
while index < count:
if ")." in lines[index]:
concatenated_rule = concatenated_rule + lines[index]
index = index + 1
list_of_rules.append(concatenated_rule)
break
else:
concatenated_rule = concatenated_rule + lines[index]
index = index + 1
line_index = index
# if a rule has no body, add it directly
else:
rule = lines[line_index]
line_index = line_index + 1
list_of_rules.append(rule)
# write the rule files
file_index = 1
for rule in list_of_rules:
print("Rule: " + rule)
rule_file = tree_path + "/Rule" + str(file_index) + ".pl"
with open(rule_file, 'w') as outfile:
outfile.write(rule)
file_index = file_index + 1
# Proof each rule and save the proof
print("RULES: " + str(len(list_of_rules)))
file_index = len(list_of_rules)
while file_index > 0:
proof_files = []
background = tree_path + "/background_without_settings.pl"
# rest of the files
prover = tree_path + "/prover.pl"
rule = tree_path + "/Rule" + str(file_index) + ".pl"
print("Rule File")
print(rule)
proof_files.append(background)
proof_files.append(prover)
proof_files.append(rule)
### folder for output
##if not os.path.exists("Proves"):
## os.mkdir("Proves")
# create a big proof and proof file
proof_writer_input = tree_path + "/proof_writer_input.pl"
print(proof_writer_input)
with open(proof_writer_input, 'w') as outfile:
for file_name in proof_files:
with open(file_name) as infile:
outfile.write(infile.read())
selected_examples_pos = []
pos_examples = open(model_path + "/" + data_set + ".f", "rt")
lines = pos_examples.readlines()
print("POS")
print(lines)
for line in lines:
example_predicate = line
print(example_predicate)
result = re.findall(r"\((.*?)\)", example_predicate)
print(result)
selected_examples_pos.append(result[0])
print(selected_examples_pos)
print("NEG")
selected_examples_neg = []
neg_examples = open(model_path + "/" + data_set + ".n", "rt")
lines = neg_examples.readlines()
print(lines)
for line in lines:
example_predicate = line
print(example_predicate)
result = re.findall(r"\((.*?)\)", example_predicate)
print(result)