path_to_dlc_files = 'example_data'
behaviors = ['pecking', 'shaking']
default_label = 'not_defined'
ass = LabelAssistant(behaviors, path_to_dlc_files, default_label)
ass.addLabel('pecking', 2, 5, 'coordinates.h5')
# Make sure a user does not have typos during labelling
def _missing_label(): ass.addLabel('peckking', 100, 200, 'coordinates.h5')
with ExceptionExpected(ex=ValueError, regex="Unknown behaviour: peckking"): _missing_label()
def _missing_file(): ass.addLabel('pecking', 100, 200, 'non_existent_file.h5')
with ExceptionExpected(ex=FileNotFoundError, regex="non_existent_file.h5"): _missing_file()
ass.addLabel('shaking', 10, 15, 'coordinates.h5')
df = ass.to_df()
df_expected = pd.DataFrame({'behavior': ['pecking', 'shaking'], 'start': [2, 10], 'end': [5, 15], 'file': ['coordinates.h5', 'coordinates.h5']})
pd.testing.assert_frame_equal(df, df_expected)
df_applied = ass.apply_labels()
display(df_applied)
df_c1 = df_applied[df_applied['file_name'] == 'coordinates.h5']
df_c1_4 = df_c1[df_c1['frame'] == 4]
df_c1_1 = df_c1[df_c1['frame'] == 1]
test_eq(df_c1_4['behavior'].iloc[0], 'pecking')
test_eq(df_c1_1['behavior'].iloc[0], default_label)
df_c1_12 = df_c1[df_c1['frame'] == 12]
test_eq(df_c1_12['behavior'].iloc[0], 'shaking')
# files that are not labelled are ignored in import
test_eq((df_applied[df_applied['file_name'] == 'coordinates2.h5']).empty, True)
It is then advised to save the labelled data for future processing.
df_applied.to_hdf('_tmp_labelled_behaviors.h5', key='labels')
display(pd.read_hdf('_tmp_labelled_behaviors.h5'))