Commit 0f878d16 authored by Anze Vavpetic's avatar Anze Vavpetic

added option to dump full database to prolog to db to rsd and db to aleph

parent 05eb4f93
......@@ -36,7 +36,7 @@ def ilp_rsd(input_dict):
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
subgroups = input_dict['subgroups'] == 'true'
# Parse settings
if settings:
rsd.settingsAsFacts(settings)
......
......@@ -27,6 +27,8 @@ class ILP_Converter(Converter):
def __init__(self, *args, **kwargs):
self.settings = kwargs.pop('settings', {}) if kwargs else {}
self.discr_intervals = kwargs.pop('discr_intervals', {}) if kwargs else {}
self.dump = kwargs.pop('dump', False) if kwargs else False
print self.dump
Converter.__init__(self, *args, **kwargs)
def user_settings(self):
......@@ -94,6 +96,24 @@ class ILP_Converter(Converter):
return ['%s_%s(%s, %s) :-' % (table, att, var_table, var_att),
values_goal] + discretize_goals
@staticmethod
def numeric(val):
for num_type in [int, float, long, complex]:
try:
num_type(val)
return True
except:
pass
return False
def dump_tables(self):
dump = []
fmt_cols = lambda cols: ','.join([("%s" % col) if ILP_Converter.numeric(col) else ("'%s'" % col) for col in cols])
for table in self.db.tables:
attributes = self.db.cols[table]
dump.append('\n'.join(["%s(%s)." % (table, fmt_cols(cols)) for cols in self.db.rows(table, attributes)]))
return dump
class RSD_Converter(ILP_Converter):
'''
Converts the database context to RSD inputs.
......@@ -118,7 +138,11 @@ class RSD_Converter(ILP_Converter):
modeslist.append(self.mode('%s_%s' % (table, att), [('+', table), ('-', att)]))
modeslist.append(self.mode('instantiate', [('+', att)]))
getters.extend(self.attribute_clause(table, att))
return '\n'.join(self.db_connection() + modeslist + getters + self.user_settings())
if not self.dump:
b = '\n'.join(self.db_connection() + modeslist + getters + self.user_settings())
else:
b = '\n'.join(modeslist + getters + self.user_settings() + self.dump_tables())
return b
class Aleph_Converter(ILP_Converter):
'''
......@@ -171,7 +195,11 @@ class Aleph_Converter(ILP_Converter):
types.extend(self.constant_type_def(table, att))
getters.extend(self.attribute_clause(table, att))
local_copies = [self.local_copy(table) for table in self.db.tables]
return '\n'.join(self.db_connection() + local_copies + self.user_settings() + modeslist + determinations + types + getters)
if not self.dump:
b = '\n'.join(self.db_connection() + local_copies + self.user_settings() + modeslist + determinations + types + getters)
else:
b = '\n'.join(self.user_settings() + modeslist + determinations + types + getters + self.dump_tables())
return b
def concept_type_def(self, table):
var_pk = self.db.pkeys[table].capitalize()
......@@ -282,8 +310,8 @@ if __name__ == '__main__':
intervals = {'cars': {'position' : [1, 3]}}
import cPickle
cPickle.dump(intervals, open('intervals.pkl','w'))
rsd = RSD_Converter(context, discr_intervals=intervals)
aleph = Aleph_Converter(context, target_att_val='east', discr_intervals=intervals)
rsd = RSD_Converter(context, discr_intervals=intervals, dump=True)
aleph = Aleph_Converter(context, target_att_val='east', discr_intervals=intervals, dump=True)
print rsd.background_knowledge()
print aleph.background_knowledge()
#orange = Orange_Converter(context)
......
......@@ -128,6 +128,24 @@
"description": "Target attribute value to be used as the positive class"
}
},
{
"pk": 660,
"model": "workflows.abstractinput",
"fields": {
"widget": 191,
"name": "dump full database",
"short_name": "dmp",
"uid": "fb5fc5f5-aa8f-4667-b96a-2b3f9a7672c7",
"default": "false",
"required": false,
"multi": false,
"parameter_type": "checkbox",
"variable": "dump",
"parameter": true,
"order": 1,
"description": "dump full database to prolog"
}
},
{
"pk": 644,
"model": "workflows.abstractinput",
......@@ -135,14 +153,14 @@
"widget": 191,
"name": "discretization intervals",
"short_name": "itr",
"uid": "02c30ce3-482b-4cc7-a9f2-08cbdb925105",
"uid": "e20c7a7c-d2f5-4ab6-9ef1-b3d244140d88",
"default": "",
"required": false,
"multi": false,
"parameter_type": null,
"variable": "discr_intervals",
"parameter": false,
"order": 1,
"order": 2,
"description": "dictionary of intervals for discretization"
}
},
......@@ -160,7 +178,7 @@
"parameter_type": null,
"variable": "context",
"parameter": false,
"order": 2,
"order": 3,
"description": "Database context object"
}
},
......@@ -315,6 +333,24 @@
"description": "Database context object"
}
},
{
"pk": 659,
"model": "workflows.abstractinput",
"fields": {
"widget": 193,
"name": "dump full database",
"short_name": "dmp",
"uid": "3c1375b1-10ed-400d-abfe-261115723c2f",
"default": "false",
"required": false,
"multi": false,
"parameter_type": "checkbox",
"variable": "dump",
"parameter": true,
"order": 1,
"description": "dump full database"
}
},
{
"pk": 645,
"model": "workflows.abstractinput",
......@@ -322,14 +358,14 @@
"widget": 193,
"name": "discretization intervals",
"short_name": "itr",
"uid": "1449645a-4e32-41eb-8c8b-7b494cefd04b",
"uid": "de698509-3bdc-482f-b5f9-9c6b3916426b",
"default": "",
"required": false,
"multi": false,
"parameter_type": null,
"variable": "discr_intervals",
"parameter": false,
"order": 1,
"order": 2,
"description": "dictionary of intervals for discretization"
}
},
......
......@@ -26,11 +26,13 @@ def mysql_db_context_finished(postdata, input_dict, output_dict):
return {'context' : context}
def mysql_rsd_converter(input_dict):
rsd = RSD_Converter(input_dict['context'], discr_intervals=input_dict.get('discr_intervals', {}))
dump = input_dict['dump'] == 'true'
rsd = RSD_Converter(input_dict['context'], discr_intervals=input_dict.get('discr_intervals', {}), dump=dump)
return {'examples' : rsd.all_examples(), 'bk' : rsd.background_knowledge()}
def mysql_aleph_converter(input_dict):
aleph = Aleph_Converter(input_dict['context'], target_att_val=input_dict['target_att_val'], discr_intervals=input_dict.get('discr_intervals', {}))
dump = input_dict['dump'] == 'true'
aleph = Aleph_Converter(input_dict['context'], target_att_val=input_dict['target_att_val'], discr_intervals=input_dict.get('discr_intervals', {}), dump=dump)
return {'pos_examples' : aleph.positive_examples(), 'neg_examples' : aleph.negative_examples(), 'bk' : aleph.background_knowledge()}
def mysql_query_to_odt(input_dict):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment