def test_no_events(self):
     pj = json.loads(open("files/test.boris").read())
     r = utilities.get_current_points_by_subject(point_behaviors_codes=["p"],
                               events=pj["observations"]["observation #1"]["events"],
                               subjects=pj["subjects_conf"],
                               time=Decimal("3"),
                               tolerance=Decimal("3"))
     assert r == {'0': [], '1': []}
Exemple #2
0
 def test_no_events(self):
     pj = json.loads(open("files/test.boris").read())
     r = utilities.get_current_points_by_subject(point_behaviors_codes=["p"],
                               events=pj[config.OBSERVATIONS]["observation #1"]["events"],
                               subjects=pj["subjects_conf"],
                               time=Decimal("3"),
                               tolerance=Decimal("3"))
     assert r == {'0': [], '1': []}
 def test_events(self):
     pj_float = json.loads(open("files/test.boris").read())
     pj = utilities.convert_time_to_decimal(pj_float)
     r = utilities.get_current_points_by_subject(point_behaviors_codes=["p"],
                               events=pj["observations"]["offset positif"]["events"],
                               subjects={"0":{"key":"", "name": "", "description":"no focal subject"}},
                               time=Decimal("22.6"),
                               tolerance=Decimal("1"))
     assert r == {'0': [['p', '']]}
Exemple #4
0
 def test_events(self):
     pj_float = json.loads(open("files/test.boris").read())
     pj = utilities.convert_time_to_decimal(pj_float)
     r = utilities.get_current_points_by_subject(point_behaviors_codes=["p"],
                               events=pj["observations"]["offset positif"]["events"],
                               subjects={"0":{"key":"", "name": "", "description":"no focal subject"}},
                               time=Decimal("22.6"),
                               tolerance=Decimal("1"))
     assert r == {'0': [['p', '']]}
Exemple #5
0
 def test_events_without_modifiers2(self):
     pj_float = json.loads(open("files/test.boris").read())
     pj = utilities.convert_time_to_decimal(pj_float)
     r = utilities.get_current_points_by_subject(point_behaviors_codes=["p"],
                               events=pj[config.OBSERVATIONS]["modifiers"]["events"],
                               subjects={"0":{"key":"", "name": "", "description":"no focal subject"}},
                               time=Decimal("8.000"),
                               tolerance=Decimal("5"),
                               include_modifiers=True)
     assert r == {'0': []}
Exemple #6
0
 def test_events_with_modifiers3(self):
     # no events should correspond to selected behavior
     pj_float = json.loads(open("files/test.boris").read())
     pj = utilities.convert_time_to_decimal(pj_float)
     r = utilities.get_current_points_by_subject(point_behaviors_codes=["q"],
                               events=pj[config.OBSERVATIONS]["modifiers"][config.EVENTS],
                               subjects={"0":{"key":"", "name": "", "description":"no focal subject"}},
                               time=Decimal("8.000"),
                               tolerance=Decimal("5"),
                               include_modifiers=True)
     assert r == {'0': [('q', 'm1'), ('q', 'm2')]}
def create_behavior_binary_table(pj: dict, selected_observations: list,
                                 parameters_obs: dict,
                                 time_interval: float) -> dict:
    """
    create behavior binary table

    Args:
        pj (dict): project dictionary
        selected_observations (list): list of selected observations
        parameters_obs (dict): dcit of parameters
        time_interval (float): time interval (in seconds)

    Returns:
        dict: dictionary of tablib dataset

    """

    results_df = {}

    state_behavior_codes = [
        x for x in utilities.state_behavior_codes(pj[ETHOGRAM])
        if x in parameters_obs[SELECTED_BEHAVIORS]
    ]
    point_behavior_codes = [
        x for x in utilities.point_behavior_codes(pj[ETHOGRAM])
        if x in parameters_obs[SELECTED_BEHAVIORS]
    ]
    if not state_behavior_codes and not point_behavior_codes:
        return {"error": True, "msg": "No state events selected"}

    for obs_id in selected_observations:

        if obs_id not in results_df:
            results_df[obs_id] = {}

        for subject in parameters_obs[SELECTED_SUBJECTS]:

            # extract tuple (behavior, modifier)
            behav_modif_list = [
                (idx[2], idx[3]) for idx in pj[OBSERVATIONS][obs_id][EVENTS]
                if idx[1] == (subject if subject != NO_FOCAL_SUBJECT else "")
                and idx[2] in parameters_obs[SELECTED_BEHAVIORS]
            ]

            # extract observed subjects NOT USED at the moment
            observed_subjects = [
                event[EVENT_SUBJECT_FIELD_IDX]
                for event in pj[OBSERVATIONS][obs_id][EVENTS]
            ]

            # add selected behavior if not found in (behavior, modifier)
            if not parameters_obs[EXCLUDE_BEHAVIORS]:
                #for behav in state_behavior_codes:
                for behav in parameters_obs[SELECTED_BEHAVIORS]:
                    if behav not in [x[0] for x in behav_modif_list]:
                        behav_modif_list.append((behav, ""))

            behav_modif_set = set(behav_modif_list)

            if parameters_obs[INCLUDE_MODIFIERS]:
                results_df[obs_id][subject] = tablib.Dataset(
                    headers=["time"] + [
                        f"{x[0]}" + f" ({x[1]})" * (x[1] != "")
                        for x in sorted(behav_modif_set)
                    ])
            else:
                results_df[obs_id][subject] = tablib.Dataset(
                    headers=["time"] + [x[0] for x in sorted(behav_modif_set)])

            if subject == NO_FOCAL_SUBJECT:
                sel_subject_dict = {"": {SUBJECT_NAME: ""}}
            else:
                sel_subject_dict = dict([
                    (idx, pj[SUBJECTS][idx]) for idx in pj[SUBJECTS]
                    if pj[SUBJECTS][idx][SUBJECT_NAME] == subject
                ])

            row_idx = 0
            t = parameters_obs[START_TIME]
            while t < parameters_obs[END_TIME]:

                # state events
                current_states = utilities.get_current_states_modifiers_by_subject(
                    state_behavior_codes,
                    pj[OBSERVATIONS][obs_id][EVENTS],
                    sel_subject_dict,
                    t,
                    include_modifiers=parameters_obs[INCLUDE_MODIFIERS])

                # point events
                current_point = utilities.get_current_points_by_subject(
                    point_behavior_codes,
                    pj[OBSERVATIONS][obs_id][EVENTS],
                    sel_subject_dict,
                    t,
                    time_interval,
                    include_modifiers=parameters_obs[INCLUDE_MODIFIERS])

                cols = [float(t)]  # time

                for behav in results_df[obs_id][subject].headers[
                        1:]:  # skip time
                    if behav in state_behavior_codes:
                        cols.append(
                            int(behav in current_states[list(
                                current_states.keys())[0]]))

                    if behav in point_behavior_codes:
                        cols.append(current_point[list(
                            current_point.keys())[0]].count(behav))
                        #int(behav in current_point[list(current_point.keys())[0]]))

                results_df[obs_id][subject].append(cols)

                t += time_interval
                row_idx += 1

    return results_df