rdr-py / code / addRule.py
addRule.py
Raw
"""
*******************************************************
Owner: Ashutosh Jha
For: APPCAIR BITS Pilani Goa Campus, Reflexis Systems
Function Name: addRule
Takes head of Tree() class, last_true node, no Of nodes
current row, target column, column name list, maximal
set and row no as input. Adds value to head, like name,
predicted class, name.
*******************************************************
"""
from chooseRule import chooseRule
from addChosenRule import addChosenRule
from addCombination import addCombination
from addCombinationRules import addCombinationRules
from common_data import common_data

def addRule(head,last_true,noOfNodes,cur_row,target_col,col_list,maximal_set,z):
    #checks whether a last_true node exists
    if(last_true == None):
        head.name = "c" + str(noOfNodes-1)
        head.row_no = z
        if(target_col != None):
            head.pred_label = cur_row[target_col]
        rule_list,feature_used,isstr = addCombination(col_list,cur_row,target_col)
        if(rule_list != None):
            addCombinationRules(head,rule_list,cur_row,isstr)
            if(target_col == None):
                head.pred_label = input("Since this dataset has no target column, which can be used to indicate conclusion" +
                        " from a rule, kindly assign a class value as conclusion if this rule is satisfied:")
            head.f_used = feature_used
        else:
            j=1
            for i in range(0,len(col_list)):
                if(col_list[i] != target_col):
                    if(type(cur_row[col_list[i]])== str):
                        maximal_set[j] = [str(col_list[i]) , str(cur_row[col_list[i]]), "str"]
                        j = j+1
                    else:
                        maximal_set[j] = [str(col_list[i]) , str(cur_row[col_list[i]]), "nstr"]
                        j = j+1
            rule_no = chooseRule(j,maximal_set)
            #print(maximal_set[rule_no])
            #print(noOfNodes)
            addChosenRule(head,maximal_set[rule_no])
            if(target_col == None):
                head.pred_label = input("Since this dataset has no target column, which can be used to indicate conclusion" +
                      " from a rule, kindly assign a class value as conclusion if this rule is satisfied:")            
            head.f_used.append(maximal_set[rule_no][0])
            maximal_set.clear()
    else:
        head.name = "c" + str(noOfNodes-1)
        head.row_no = z
        if(target_col != None):
            head.pred_label = cur_row[target_col]
        rule_list,feature_used,isstr = addCombination(col_list,cur_row,target_col)
        if(rule_list != None):
            #Check whether features used are present in last_true node
            #and if the input condition of last node is satisfied by current instance
            if((common_data(feature_used,last_true.f_used) == True) and (eval(last_true.inp_cond)==True)):
                print("Can't add this rule as it violates RDR implementation." +
                    " The instance uses feature common to last true node." +
                    " And also satisfies the last true node.")
                return
            else:
                addCombinationRules(head,rule_list,cur_row,isstr)
                if(target_col == None):
                    head.pred_label = input("Since this dataset has no target column, which can be used to indicate conclusion" +
                      " from a rule, kindly assign a class value as conclusion if this rule is satisfied:")
                head.f_used = feature_used
        else:
            j=1
            for i in range(0,len(col_list)):
                if(col_list[i] != target_col):
                    if((col_list[i] in last_true.f_used) and 
                       (eval(last_true.inp_cond) == True)):
                        continue
                    else:
                        if(type(cur_row[col_list[i]])== str):
                            maximal_set[j] = [str(col_list[i]) , str(cur_row[col_list[i]]), "str"]
                            j = j+1
                        else:
                            maximal_set[j] = [str(col_list[i]) , str(cur_row[col_list[i]]), "nstr"]
                            j = j+1
            rule_no = chooseRule(j,maximal_set)
            #print(maximal_set[rule_no])
            #print(noOfNodes)
            addChosenRule(head,maximal_set[rule_no])
            if(target_col == None):
                head.pred_label = input("Since this dataset has no target column, which can be used to indicate conclusion" +
                      " from a rule, kindly assign a class value as conclusion if this rule is satisfied:")
            head.f_used.append(maximal_set[rule_no][0])
            maximal_set.clear()
    print("\nThe condition added by you:")
    print(head.name,":",head.inp_cond)