def testFrame1Light(self): start_frame = 2311 end_frame = 2442 fluent = 'light' source = 'origdata' orig_dict = xml_stuff.queryXMLForAnswersBetweenFrames(self.orig_xml, fluent, start_frame, end_frame, source, not source.endswith('smrt') ) desired_orig_dict = {'light_action_2311_act_pushbutton': 0, 'light_2311_off_on': 0, 'light_action_2311_act_no_pushbutton': 100, 'light_2311_on': 0, 'light_2311_on_off': 300, 'light_2311_off': 0} assert orig_dict == desired_orig_dict, "\ntest: {}\ntruth: {}".format(orig_dict, desired_orig_dict) source = 'causaldata' causal_dict = xml_stuff.queryXMLForAnswersBetweenFrames(self.causal_xml, fluent, start_frame, end_frame, source, not source.endswith('smrt') ) desired_causal_dict = {'light_action_2311_act_pushbutton': 0, 'light_2311_off_on': 0, 'light_action_2311_act_no_pushbutton': 100, 'light_2311_on': 100, 'light_2311_on_off': 0, 'light_2311_off': 0} assert causal_dict == desired_causal_dict, "\n{}\n{}".format(causal_dict, desired_causal_dict)
def testFrame1Door(self): start_frame = 261 end_frame = 314 fluent = 'door' source = 'origdata' orig_dict = xml_stuff.queryXMLForAnswersBetweenFrames(self.orig_xml, fluent, start_frame, end_frame, source, not source.endswith('smrt') ) desired_orig_dict = {'door_261_closed': 0, 'door_261_closed_open': 100, 'door_261_open_closed': 100, 'door_action_261_act_opened': 0, 'door_261_open': 0, 'door_action_261_act_not_opened_closed': 100, 'door_action_261_act_closed': 0} assert orig_dict == desired_orig_dict, "{} != {}".format(orig_dict, desired_orig_dict) source = 'causaldata' causal_dict = xml_stuff.queryXMLForAnswersBetweenFrames(self.causal_xml, fluent, start_frame, end_frame, source, not source.endswith('smrt') ) desired_causal_dict = {'door_261_closed': 0, 'door_261_closed_open': 0, 'door_261_open_closed': 0, 'door_action_261_act_opened': 0, 'door_261_open': 100, 'door_action_261_act_not_opened_closed': 100, 'door_action_261_act_closed': 0} assert causal_dict == desired_causal_dict, "{} != {}".format(causal_dict, desired_causal_dict)
def testFrame1Screen(self): start_frame = 2311 end_frame = 2442 fluent = 'screen' source = 'origdata' orig_dict = xml_stuff.queryXMLForAnswersBetweenFrames(self.orig_xml, fluent, start_frame, end_frame, source, not source.endswith('smrt') ) desired_orig_dict = {'screen_2311_off_on': 100, 'screen_action_2311_act_no_mousekeyboard': 0, 'screen_action_2311_act_mousekeyboard': 100, 'screen_2311_on_off': 100, 'screen_2311_on': 0, 'screen_2311_off': 0} assert orig_dict == desired_orig_dict, "{} != {}".format(orig_dict, desired_orig_dict) source = 'causaldata' causal_dict = xml_stuff.queryXMLForAnswersBetweenFrames(self.causal_xml, fluent, start_frame, end_frame, source, not source.endswith('smrt') ) desired_causal_dict = {'screen_2311_off_on': 0, 'screen_action_2311_act_no_mousekeyboard': 0, 'screen_action_2311_act_mousekeyboard': 100, 'screen_2311_on_off': 0, 'screen_2311_on': 100, 'screen_2311_off': 0} assert causal_dict == desired_causal_dict, "{} != {}".format(causal_dict, desired_causal_dict)
def getAnswersForFrames(self, frame1, frame2): return xml_stuff.queryXMLForAnswersBetweenFrames(self.parsexml, "phone", frame1, frame2, "causalgrammar", dumb=True)
def getAnswersForFrames(self, xml, name, oject, frame1, frame2): return xml_stuff.queryXMLForAnswersBetweenFrames(xml, oject, frame1, frame2, name, dumb=True)
def testFrame2Door(self): start_frame = 314 end_frame = 349 fluent = 'door' """ origdb: 314-349: shuts x 2, stand_start, stand_end causal: 314-349: shuts, stand_start """ source = 'origdata' orig_dict = xml_stuff.queryXMLForAnswersBetweenFrames(self.orig_xml, fluent, start_frame, end_frame, source, not source.endswith('smrt') ) desired_orig_dict = {'door_314_closed': 0, 'door_314_closed_open': 0, 'door_314_open_closed': 200, 'door_action_314_act_opened': 100, 'door_314_open': 0, 'door_action_314_act_not_opened_closed': 0, 'door_action_314_act_closed': 100} assert orig_dict == desired_orig_dict, "\ntest: {}\ntruth: {}".format(orig_dict, desired_orig_dict) source = 'causaldata' causal_dict = xml_stuff.queryXMLForAnswersBetweenFrames(self.causal_xml, fluent, start_frame, end_frame, source, not source.endswith('smrt') ) desired_causal_dict = {'door_314_closed': 0, 'door_314_closed_open': 0, 'door_314_open_closed': 100, 'door_action_314_act_opened': 0, 'door_314_open': 0, 'door_action_314_act_not_opened_closed': 0, 'door_action_314_act_closed': 100} assert causal_dict == desired_causal_dict, "\n{}\n{}".format(causal_dict, desired_causal_dict)
def uploadComputerResponseToDB(example, fluent_and_action_xml, source, connType, conn=False): import causal_grammar_summerdata # sets up causal_forest debugQuery = False parsedExampleName = example.split('_') # for db lookup, remove "room" at end, and munge _'s away exampleNameForDB = "".join(parsedExampleName[:-1]) # for cutpoints, just remove "room" at end exampleNameForCutpoints = "_".join(parsedExampleName[:-1]) m = hashlib.md5(exampleNameForDB) tableName = TBLPFX + m.hexdigest() leaveconn = True if not conn: leaveconn = False conn = getDB(connType) allColumns = getColumns(conn, connType, tableName, exampleNameForDB) if not allColumns: return # get cutpoints and objects from our columns; we'll build the actions and fluents back up manually from lookups print("{}".format(tableName)) cutpoint_lookup = videoCutpoints.cutpoints[exampleNameForCutpoints] cutpoints = [] ojects = [] sqlStatement = "SELECT " for singleColumn in allColumns: column = singleColumn[0] if column not in ("act_made_call", "act_unlock", "name", "stamp", "hash"): # sqlStatement += singleColumn[0] + ", " # print("COLUMN: {}".format(column)) if column.count("_") < 3: oject, frame, tmp = singleColumn[0].split("_", 2) else: oject, frame, tmp, rest = singleColumn[0].split("_", 3) if frame == "action": frame = tmp if oject == "dispense": continue ojects.append(oject, ) cutpoints.append(int(frame)) ojects = list(set(ojects)) cutpoints = sorted(list(set(cutpoints))) if str(cutpoints[-1]) in cutpoint_lookup: cutpoints.append(cutpoint_lookup[str(cutpoints[-1])]) else: print("WARNING: {} failed to be found in cutpoints for {}".format( str(cutpoints[-1]), exampleNameForCutpoints)) last_cutpoint = sorted(int(x) for x in cutpoint_lookup)[-1] cutpoints.append(cutpoint_lookup[str(last_cutpoint)]) # let's make sure we know how to work on all of these objects known_ojects = kKnownObjects if not all(map(lambda x: x in known_ojects, ojects)): difference = set(ojects).difference(known_ojects) print("skipping {} due to unknown: {}".format(example, ", ".join(difference))) return print("WORKING ON------{}".format(ojects)) # for each of our objects, figure out what we think went on at each cutpoint #print("objects: {}".format(ojects)) #print("frames: {}".format(cutpoints)) insertion_object = {"name": source, "hash": kInsertionHash} if debugQuery: print minidom.parseString(fluent_and_action_xml).toprettyxml( indent="\t") fluent_and_action_xml_xml = ET.fromstring(fluent_and_action_xml) for oject in ojects: prev_frame = cutpoints[0] for frame in cutpoints[1:]: #print("{} - {}".format(oject, frame)) answers = xml_stuff.queryXMLForAnswersBetweenFrames( fluent_and_action_xml_xml, oject, prev_frame, frame, source, not source.endswith('smrt')) if source in ( "random", "origdata", ): framestr = "{}".format(prev_frame) things = dict() for key in answers: thing, choice = key.split(framestr) if not thing in things: things[thing] = 1 else: things[thing] += 1 for key in answers: thing, choice = key.split(framestr) # {door, light, screen} are only detectable fluents. So if it's not door, light, screen, it should be "random" for origdata. which means (tested) ~ we're setting to "random": trash {same, less, more}; phone {off_active, active_off, off, active}; cup {same, less, more}; thirst {not, thirsty, thirsty_not, not_thirsty}; waterstream {water_on, water_off}; if source in ("origdata", ): if choice.startswith("_act_"): continue elif thing in (x + "_" for x in causal_grammar_summerdata. detectable_fluents): # this is horribly, horribly hardcoded here.... # if there's no detection but it's detectable, choice is herein split: # when no detection: # door_{}_closed_open: 0, door_{}_open_closed: 0, door_{}_closed: 50, door_{}_open: 50 # light_{}_off_on: 0, light_{}_on_off: 0, light_{}_off: 50, light_{}_on: 50 # screen_{}_off_on: 0, screen_{}_on_off: 0, screen_{}_off: 50, screen_{}_on: 50 dothisthing = False if thing == "door_" and choice == "_closed_open": dothisthing = True off_on = "_".join( ("door", str(prev_frame), "closed_open")) on_off = "_".join( ("door", str(prev_frame), "open_closed")) on = "_".join( ("door", str(prev_frame), "open")) off = "_".join( ("door", str(prev_frame), "closed")) elif thing == "light_" and choice == "_off_on": dothisthing = True off_on = "_".join( ("light", str(prev_frame), "off_on")) on_off = "_".join( ("light", str(prev_frame), "on_off")) on = "_".join(("light", str(prev_frame), "on")) off = "_".join( ("light", str(prev_frame), "off")) elif thing == "screen_" and choice == "_off_on": dothisthing = True off_on = "_".join( ("screen", str(prev_frame), "off_on")) on_off = "_".join( ("screen", str(prev_frame), "on_off")) on = "_".join( ("screen", str(prev_frame), "on")) off = "_".join( ("screen", str(prev_frame), "off")) if dothisthing: keys = [off_on, on_off, on, off] # we say there's no detection if everything is even, or if we've got 0 for off_on and on_off if len(set(answers[k] for k in keys)) == 1 or ( answers[off_on] == 0 and answers[on_off] == 0): answers[off_on] = 0 answers[on_off] = 0 answers[on] = 50 answers[off] = 50 print("FOO+ {} {}".format( example, {k: answers[k] for k in keys})) else: print("FOO- {} {}".format( example, {k: answers[k] for k in keys})) continue answers[key] = int(100 / things[thing]) insertion_object.update(answers) prev_frame = frame print("INSERT: {}".format(insertion_object)) # http://stackoverflow.com/a/9336427/856925 for key in insertion_object.keys(): if type(insertion_object[key]) is str: insertion_object[key] = "'{}'".format(insertion_object[key]) qry = "INSERT INTO %s (%s) VALUES (%s)" % (tableName, ", ".join( insertion_object.keys()), ", ".join(map(str, insertion_object.values()))) if not globalDryRun: cursor = conn.cursor() cursor.execute("DELETE FROM %s WHERE name IN ('%s')" % (tableName, source)) cursor.execute(qry) conn.commit() cursor.close() if not leaveconn: conn.close() return True