def test_time_budget1(self): pj = json.loads(open("files/test.boris").read()) ethogram = pj[ETHOGRAM] selected_observations = ["observation #1"] parameters = { "selected subjects": ["subject1", "subject2"], "selected behaviors": ["p", "s"], INCLUDE_MODIFIERS: False, EXCLUDE_BEHAVIORS: False, "start time": 0, "end time": 100.0 } cursor = db_functions.load_events_in_db(pj, parameters[SELECTED_SUBJECTS], selected_observations, parameters[SELECTED_BEHAVIORS]) out, categories = time_budget_functions.time_budget_analysis( ethogram, cursor, selected_observations, parameters, by_category=False) # open("files/test_time_budget1new.json", "w").write(json.dumps(out)) assert json.loads(open("files/test_time_budget1.json").read()) == out
def test_1(self): pj = json.loads(open("files/test.boris").read()) cursor = db_functions.load_events_in_db(pj, ["subject1"], ["observation #1"], ["s"]) cursor.execute("SELECT occurence FROM events WHERE observation = ? AND subject = ? AND code = ?", ("observation #1", "subject1", "s")) out = "" for r in cursor.fetchall(): out += '{}\n'.format(r[0]) REF = """3.3\n7.75\n9.9\n16.2\n18.35\n24.475\n""" assert out == REF
def test_time_budget2(self): VERIF = [{ "subject": "subject1", "behavior": "p", "modifiers": "", "duration": 0, "duration_mean": 0, "duration_stdev": "NA", "number": "0", "inter_duration_mean": "NA", "inter_duration_stdev": "NA" }, { "subject": "subject2", "behavior": "p", "modifiers": "", "duration": 0, "duration_mean": 0, "duration_stdev": 0, "number": 2, "inter_duration_mean": 0.0, "inter_duration_stdev": "NA" }] pj = json.loads(open("files/test.boris").read()) ethogram = pj[ETHOGRAM] selected_observations = ["observation #2"] parameters = { SELECTED_SUBJECTS: ["subject1", "subject2"], SELECTED_BEHAVIORS: ["p"], INCLUDE_MODIFIERS: False, EXCLUDE_BEHAVIORS: False, "start time": 0, "end time": 100.0 } cursor = db_functions.load_events_in_db(pj, parameters[SELECTED_SUBJECTS], selected_observations, parameters[SELECTED_BEHAVIORS]) out, categories = time_budget_functions.time_budget_analysis( ethogram, cursor, selected_observations, parameters, by_category=False) # open("files/test_time_budget2.json", "w").write(json.dumps(out)) assert out == VERIF
def test_time_budget3(self): VERIF = [{ "subject": "No focal subject", "behavior": "s", "modifiers": "", "duration": 59.625, "duration_mean": 9.937, "duration_stdev": 8.395, "number": 6, "inter_duration_mean": 49.268, "inter_duration_stdev": 84.012 }, { "subject": "No focal subject", "behavior": "p", "modifiers": "", "duration": 0, "duration_mean": 0, "duration_stdev": "NA", "number": 6, "inter_duration_mean": 54.083, "inter_duration_stdev": 117.79 }] pj = json.loads(open("files/test.boris").read()) ethogram = pj[ETHOGRAM] selected_observations = ["observation #2"] parameters = { SELECTED_SUBJECTS: ["No focal subject"], SELECTED_BEHAVIORS: ["s", "p"], INCLUDE_MODIFIERS: True, EXCLUDE_BEHAVIORS: True, "start time": 0, "end time": 320.0 } cursor = db_functions.load_events_in_db(pj, parameters[SELECTED_SUBJECTS], selected_observations, parameters[SELECTED_BEHAVIORS]) out, categories = time_budget_functions.time_budget_analysis( ethogram, cursor, selected_observations, parameters, by_category=False) # open("files/test_time_budget3.json", "w").write(json.dumps(out)) assert out == VERIF
def test_time_budget4(self): VERIF = [{ "subject": "No focal subject", "behavior": "s", "modifiers": "", "duration": "UNPAIRED", "duration_mean": "UNPAIRED", "duration_stdev": "UNPAIRED", "number": "UNPAIRED", "inter_duration_mean": "UNPAIRED", "inter_duration_stdev": "UNPAIRED" }, { "subject": "No focal subject", "behavior": "p", "modifiers": "", "duration": "NA", "duration_mean": "NA", "duration_stdev": "NA", "number": 5, "inter_duration_mean": 3.272, "inter_duration_stdev": 0.309 }] pj = json.loads(open("files/test.boris").read()) ethogram = pj[ETHOGRAM] selected_observations = ["live not paired"] parameters = { SELECTED_SUBJECTS: ["No focal subject"], SELECTED_BEHAVIORS: ["s", "p"], INCLUDE_MODIFIERS: True, EXCLUDE_BEHAVIORS: True, "start time": 0, "end time": 26.862 } cursor = db_functions.load_events_in_db(pj, parameters[SELECTED_SUBJECTS], selected_observations, parameters[SELECTED_BEHAVIORS]) out, categories = time_budget_functions.time_budget_analysis( ethogram, cursor, selected_observations, parameters, by_category=False) # open("/tmp/test_time_budget4.json", "w").write(json.dumps(out)) assert out == VERIF
def test_2(self): pj = json.loads(open("files/test.boris").read()) cursor = db_functions.load_events_in_db(pj, ["subject2"], ["live"], ["s", "p"]) cursor.execute("SELECT occurence FROM events WHERE observation = ? AND subject = ? AND code = ?", ("live", "subject2", "s")) out = "" for r in cursor.fetchall(): out += '{}\n'.format(r[0]) REF = """12.509\n20.685\n""" assert out == REF
def test_1(self): pj = json.loads(open("files/test.boris").read()) cursor = db_functions.load_events_in_db(pj, ["subject1"], ["observation #1"], ["s"]) cursor.execute( "SELECT occurence FROM events WHERE observation = ? AND subject = ? AND code = ?", ("observation #1", "subject1", "s")) out = "" for r in cursor.fetchall(): out += '{}\n'.format(r[0]) REF = """3.3\n7.75\n9.9\n16.2\n18.35\n24.475\n""" assert out == REF
def test_2(self): pj = json.loads(open("files/test.boris").read()) cursor = db_functions.load_events_in_db(pj, ["subject2"], ["live"], ["s", "p"]) cursor.execute( "SELECT occurence FROM events WHERE observation = ? AND subject = ? AND code = ?", ("live", "subject2", "s")) out = "" for r in cursor.fetchall(): out += '{}\n'.format(r[0]) REF = """12.509\n20.685\n""" assert out == REF
def test_3(self): """ no focal subject, observation with not paired events """ pj = json.loads(open("files/test.boris").read()) cursor = db_functions.load_events_in_db(pj, ["No focal subject"], ["live not paired"], ["s", "p"]) cursor.execute("SELECT occurence FROM events WHERE observation = ? AND subject = ? AND code = ?", ("live not paired", "No focal subject", "s")) out = "" for r in cursor.fetchall(): out += '{}\n'.format(r[0]) REF = """2.718\n10.478\n12.926\n17.47\n19.502\n24.318\n26.862\n""" assert out == REF
def test_3(self): """ no focal subject, observation with not paired events """ pj = json.loads(open("files/test.boris").read()) cursor = db_functions.load_events_in_db(pj, ["No focal subject"], ["live not paired"], ["s", "p"]) cursor.execute( "SELECT occurence FROM events WHERE observation = ? AND subject = ? AND code = ?", ("live not paired", "No focal subject", "s")) out = "" for r in cursor.fetchall(): out += '{}\n'.format(r[0]) REF = """2.718\n10.478\n12.926\n17.47\n19.502\n24.318\n26.862\n""" assert out == REF
def export_aggregated_events(pj, parameters, obsId): """ export aggregated events Args: pj (dict): BORIS project parameters (dict): subjects, behaviors obsId (str): observation id Returns: tablib.Dataset: """ data = tablib.Dataset() observation = pj[OBSERVATIONS][obsId] duration1 = [] # in seconds if observation[TYPE] in [MEDIA]: try: for mediaFile in observation[FILE][PLAYER1]: if "media_info" in observation: duration1.append( observation["media_info"]["length"][mediaFile]) except: duration1 = [] total_length = "{0:.3f}".format( project_functions.observation_total_length(observation)) cursor = db_functions.load_events_in_db(pj, parameters["selected subjects"], [obsId], parameters["selected behaviors"]) for subject in parameters["selected subjects"]: for behavior in parameters["selected behaviors"]: cursor.execute( "SELECT occurence, modifiers, comment FROM events WHERE observation = ? AND subject = ? AND code = ? ORDER by occurence", (obsId, subject, behavior)) rows = list(cursor.fetchall()) for idx, row in enumerate(rows): if observation[TYPE] in [MEDIA]: if duration1: mediaFileIdx = [ idx1 for idx1, x in enumerate(duration1) if row["occurence"] >= sum(duration1[0:idx1]) ][-1] mediaFileString = observation[FILE][PLAYER1][ mediaFileIdx] fpsString = observation["media_info"]["fps"][ observation[FILE][PLAYER1][mediaFileIdx]] else: mediaFileString = "-" fpsString = "NA" if observation[TYPE] in [LIVE]: mediaFileString = "LIVE" fpsString = "NA" if POINT in project_functions.event_type( behavior, pj[ETHOGRAM]): row_data = [] row_data.extend([ obsId, observation["date"].replace("T", " "), mediaFileString, total_length, fpsString ]) # independent variables if INDEPENDENT_VARIABLES in pj: for idx_var in utilities.sorted_keys( pj[INDEPENDENT_VARIABLES]): if pj[INDEPENDENT_VARIABLES][idx_var][ "label"] in observation[ INDEPENDENT_VARIABLES]: row_data.append( observation[INDEPENDENT_VARIABLES][ pj[INDEPENDENT_VARIABLES][idx_var] ["label"]]) else: row_data.append("") row_data.extend([ subject, behavior, row["modifiers"].strip(), POINT, "{0:.3f}".format(row["occurence"]), # start "NA", # stop "NA", # duration row["comment"], "" ]) data.append(row_data) if STATE in project_functions.event_type( behavior, pj[ETHOGRAM]): if idx % 2 == 0: row_data = [] row_data.extend([ obsId, observation["date"].replace("T", " "), mediaFileString, total_length, fpsString ]) # independent variables if INDEPENDENT_VARIABLES in pj: for idx_var in utilities.sorted_keys( pj[INDEPENDENT_VARIABLES]): if pj[INDEPENDENT_VARIABLES][idx_var][ "label"] in observation[ INDEPENDENT_VARIABLES]: row_data.append( observation[INDEPENDENT_VARIABLES][ pj[INDEPENDENT_VARIABLES][idx_var] ["label"]]) else: row_data.append("") row_data.extend([ subject, behavior, row["modifiers"].strip(), STATE, "{0:.3f}".format(row["occurence"]), "{0:.3f}".format(rows[idx + 1]["occurence"]), "{0:.3f}".format(rows[idx + 1]["occurence"] - row["occurence"]), row["comment"], rows[idx + 1]["comment"] ]) data.append(row_data) return data
def test_time_budget5(self): """ test time budget with modifiers """ VERIF = [{ "subject": "No focal subject", "behavior": "q", "modifiers": "m1", "duration": 0, "duration_mean": 0, "duration_stdev": "NA", "number": 1, "inter_duration_mean": "NA", "inter_duration_stdev": "NA" }, { "subject": "No focal subject", "behavior": "q", "modifiers": "m2", "duration": 0, "duration_mean": 0, "duration_stdev": "NA", "number": 1, "inter_duration_mean": "NA", "inter_duration_stdev": "NA" }, { "subject": "No focal subject", "behavior": "q", "modifiers": "m3", "duration": 0, "duration_mean": 0, "duration_stdev": "NA", "number": 1, "inter_duration_mean": "NA", "inter_duration_stdev": "NA" }, { "subject": "No focal subject", "behavior": "r", "modifiers": "m1", "duration": 8.775, "duration_mean": 8.775, "duration_stdev": "NA", "number": 1, "inter_duration_mean": "NA", "inter_duration_stdev": "NA" }, { "subject": "No focal subject", "behavior": "r", "modifiers": "None", "duration": 10.4, "duration_mean": 10.4, "duration_stdev": "NA", "number": 1, "inter_duration_mean": "NA", "inter_duration_stdev": "NA" }] pj = json.loads(open("files/test.boris").read()) ethogram = pj[ETHOGRAM] selected_observations = ["modifiers"] parameters = { SELECTED_SUBJECTS: ["No focal subject"], SELECTED_BEHAVIORS: ["q", "r"], INCLUDE_MODIFIERS: True, EXCLUDE_BEHAVIORS: False, "start time": 0, "end time": 180 } cursor = db_functions.load_events_in_db( pj, parameters[SELECTED_SUBJECTS], selected_observations, parameters[SELECTED_BEHAVIORS], time_interval=TIME_ARBITRARY_INTERVAL) out, categories = time_budget_functions.time_budget_analysis( ethogram, cursor, selected_observations, parameters, by_category=False) # open("files/1.json", "w").write(json.dumps(out)) assert out == VERIF