create_training_data.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import pickle
00004 import sys
00005 
00006 sys.setrecursionlimit(100000)
00007 restypescnt = {}
00008 
00009 
00010 def recurse(f, indiv_class, individual, level = 0):
00011     strLine = ""
00012     
00013     strLine += indiv_class
00014     strLine += ", " + str(level)
00015     
00016     for indiv in individual["individuals"]:
00017         strLineIndividual = strLine
00018         
00019         if individual["individuals"][indiv]["failure"] == "":
00020             strLineIndividual += ", Success"
00021         else:
00022             strLineIndividual += ", " + individual["individuals"][indiv]["failure"]
00023         
00024         params_found = False
00025         for param in model["parameters"]:
00026             if param in individual["individuals"][indiv]["parameters"]:
00027                 params_found = True
00028                 strLineIndividual += ", " + individual["individuals"][indiv]["parameters"][param]
00029             else:
00030                 strLineIndividual += ", ?"
00031         
00032         strLineIndividual += "\n"
00033         
00034         if params_found:
00035             f.write(strLineIndividual)
00036     
00037     for subtype in individual["subTypes"]:
00038         recurse(f, subtype, individual["subTypes"][subtype], level + 1)
00039 
00040 
00041 def collectTaskTypes(task):
00042     types = []
00043     
00044     for curtype in task:
00045         subtypes = collectTaskTypes(task[curtype]["subTypes"])
00046         
00047         for subtype in subtypes:
00048             if not subtype in types:
00049                 types.append(subtype)
00050         
00051         if not curtype in types:
00052             types.append(curtype)
00053     
00054     return types
00055 
00056 def collectResults(task):
00057     results = []
00058     
00059     for curtype in task:
00060         curresults = collectResults(task[curtype]["subTypes"])
00061         
00062         for indiv in task[curtype]["individuals"]:
00063             if task[curtype]["individuals"][indiv]["failure"] != "":
00064                 fail = task[curtype]["individuals"][indiv]["failure"]
00065                 if not fail in results:
00066                     results.append(fail)
00067                 
00068                 if not fail in restypescnt:
00069                     restypescnt[fail] = 0
00070             
00071                 restypescnt[fail] += 1
00072         
00073         for curresult in curresults:
00074             if not curresult in results:
00075                 results.append(curresult)
00076     
00077     return results
00078 
00079 with open("generalized_model.pkl", "r") as f:
00080     model = pickle.load(f)
00081 
00082 with open("training_data.arff", "wb") as f:
00083     f.write("@relation TaskInformation\n\n")
00084     
00085     dicTaskTypes = collectTaskTypes(model["model"])
00086     f.write("@attribute Task {")
00087     
00088     strline = "";
00089     for tasktype in dicTaskTypes:
00090         if strline != "":
00091             strline += ", "
00092         
00093         strline += tasktype
00094     
00095     strline += "}\n"
00096     f.write(strline)
00097     
00098     f.write("@attribute Level NUMERIC\n")
00099 
00100     dicResultTypes = collectResults(model["model"])
00101     dicResultTypes.append("Success");
00102     
00103     f.write("@attribute Result {")
00104     
00105     strline = "";
00106     for tasktype in dicResultTypes:
00107         if strline != "":
00108             strline += ", "
00109         
00110         strline += tasktype
00111     
00112     strline += "}\n"
00113     f.write(strline)
00114     
00115     for param in model["parameters"]:
00116         if param == "TAGNAME":
00117             f.write("@attribute " + param + " {PICK, PERCEIVE, NAVIGATE, GRASP, PLACE}\n")
00118         else:
00119             f.write("@attribute " + param + " NUMERIC\n")
00120 
00121     f.write("\n@data\n")
00122     
00123     indiv_class = model["model"].keys()[0]
00124     recurse(f, indiv_class, model["model"][indiv_class])
00125     
00126     print restypescnt


beliefstate
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 22:30:15