Advanced smoothing and interpolation
Here, we perform different smoothing and interpolation operations on different body-parts and animals using the simba.data_processors.interpolation_smoothing.AdvancedSmoother
and simba.data_processors.interpolation_smoothing.AdvancedInterpolator
.
See Example 1 for applying different smoothing and interpolation operations on different animals.
See Example 2 for applying different smoothing and interpolation operations on different body-parts within animals.
[28]:
from simba.data_processors.interpolation_smoothing import AdvancedSmoother
from simba.data_processors.interpolation_smoothing import AdvancedInterpolator
[29]:
## EXAMPLE 1: DATA DEFINITIONS
#The path to the SimBA project config
CONFIG_PATH = '/Users/simon/Desktop/envs/troubleshooting/two_black_animals_14bp/project_folder/project_config.ini'
#The path to the directory in a SimBA project that we want to interpolate and smooth
DATA_PATH = '/Users/simon/Desktop/envs/troubleshooting/two_black_animals_14bp/project_folder/csv/input_csv'
# If we define each interpolation rule per "animal" or per "body-part". Here we specify one interpolation/smooting rule
# per animal. If we want to specify different smoothing/interpolation rules per body-part, set TYPE = 'body-part' (see further examples below)
TYPE = 'animal'
# Dictionary defining the interpolation settings for the separate animals in the project.
# Acceptable values: 'linear', 'quadratic', 'nearest'.
# Note I: These interpolation settings will be applied on all body-parts belonging to specific animals. If you need to
# apply different interpolation settings for different body-parts WITHIN animals, see further examples below.
# Note II: To find the names of the animals in the project, check the project_config.ini file under the header [Multi animal IDs][id_list]
INTERPOLATION_SETTINGS = {'Simon': 'linear', 'JJ': 'quadratic'}
# Dictionary defining the smoothing settings for the separate animals in the project.
# Acceptable methods: 'Savitzky Golay', 'Gaussian'.
# Note I: These smoothing settings will be applied on all body-parts belonging to specific animals. If you need to
# apply different smoothing settings for different body-parts WITHIN animals, see further examples below.
# Note II: To find the names of the animals in the project, check the project_config.ini file under the header [Multi animal IDs][id_list]
SMOOTHING_SETTINGS = {'Simon': {'method': 'Savitzky Golay', 'time_window': 2000},
'JJ': {'method': 'Gaussian', 'time_window': 500}}
#If the data in the ``DATA_PATH`` contains multi-index dataframes, set to True. E.g., set to True of input data is the ``project_folder/csv/input_csv`` directory.
MULTI_INDEX_INPUT = True
# If True, overwrites the input data. If False, then saves a copy input data in datetime-stamped sub-directory of the ``DATA_PATH`` folder.
OVERWRITE = True
[30]:
# RUN INTERPOLATOR EXAMPLE 1
interpolator = AdvancedInterpolator(data_dir=DATA_PATH,
config_path=CONFIG_PATH,
type=TYPE,
settings=INTERPOLATION_SETTINGS,
initial_import_multi_index=MULTI_INDEX_INPUT,
overwrite=OVERWRITE)
interpolator.run()
Interpolating 9 Ear_left_1 body-parts in video Together_1...
Interpolating 2 Ear_right_1 body-parts in video Together_1...
Interpolating 7 Nose_1 body-parts in video Together_1...
Interpolating 0 Center_1 body-parts in video Together_1...
Interpolating 2 Lat_left_1 body-parts in video Together_1...
Interpolating 3 Lat_right_1 body-parts in video Together_1...
Interpolating 3 Tail_base_1 body-parts in video Together_1...
Interpolating 0 Tail_end_1 body-parts in video Together_1...
Interpolating 0 Ear_left_2 body-parts in video Together_1...
Interpolating 0 Ear_right_2 body-parts in video Together_1...
Interpolating 0 Nose_2 body-parts in video Together_1...
Interpolating 0 Center_2 body-parts in video Together_1...
Interpolating 0 Lat_left_2 body-parts in video Together_1...
Interpolating 2 Lat_right_2 body-parts in video Together_1...
Interpolating 2 Tail_base_2 body-parts in video Together_1...
Interpolating 0 Tail_end_2 body-parts in video Together_1...
SIMBA COMPLETE: Interpolation complete! (elapsed time: 0.4584s) complete
[31]:
# RUN SMOOTHING EXAMPLE 1
smoother = AdvancedSmoother(data_dir=DATA_PATH,
config_path=CONFIG_PATH,
type=TYPE,
settings=SMOOTHING_SETTINGS,
initial_import_multi_index=True,
overwrite=OVERWRITE)
smoother.run()
Smoothing data in video Together_1...
SIMBA COMPLETE: Smoothing complete complete! (elapsed time: 0.4014s) complete
[32]:
# EXAMPLE 2: IF WE INSTEAD WANT TO APPLY SMOOTHING AND INTERPOLATION RULED ON A BODYPART-BY-BODY-PART BASES, WE RE-DEFINE "TYPE" AND
# OUR INTERPOLATION AND SMOOTHING SETTINGS
TYPE = 'body-part'
# Note: To find the names of the body-parts in your SimBA project, see the `project_folder/logs/measures/pose_configs/bp_names/project_bp_names.csv` file in your SimBa project
INTERPOLATION_SETTINGS = {'Simon': {'Ear_left_1': 'nearest', 'Ear_right_1': 'linear', 'Nose_1': 'nearest', 'Lat_left_1': 'linear', 'Lat_right_1': 'linear', 'Center_1': 'nearest', 'Tail_base_1': 'quadratic'},
'JJ': {'Ear_left_2': 'linear', 'Ear_right_2': 'nearest', 'Nose_2': 'quadratic', 'Lat_left_2': 'quadratic', 'Lat_right_2': 'linear', 'Center_2': 'quadratic', 'Tail_base_2': 'quadratic'}}
SMOOTHING_SETTINGS = {'Simon': {'Ear_left_1': {'method': 'Savitzky Golay', 'time_window': 3500},
'Ear_right_1': {'method': 'Gaussian', 'time_window': 500},
'Nose_1': {'method': 'Savitzky Golay', 'time_window': 2000},
'Lat_left_1': {'method': 'Savitzky Golay', 'time_window': 2000},
'Lat_right_1': {'method': 'Gaussian', 'time_window': 2000},
'Center_1': {'method': 'Savitzky Golay', 'time_window': 2000},
'Tail_base_1': {'method': 'Gaussian', 'time_window': 500}},
'JJ': {'Ear_left_2': {'method': 'Savitzky Golay', 'time_window': 2000},
'Ear_right_2': {'method': 'Savitzky Golay', 'time_window': 500},
'Nose_2': {'method': 'Gaussian', 'time_window': 3500},
'Lat_left_2': {'method': 'Savitzky Golay', 'time_window': 500},
'Lat_right_2': {'method': 'Gaussian', 'time_window': 3500},
'Center_2': {'method': 'Gaussian', 'time_window': 2000},
'Tail_base_2': {'method': 'Savitzky Golay', 'time_window': 3500}}}
[33]:
# RUN INTERPOLATOR EXAMPLE 2
interpolator = AdvancedInterpolator(data_dir=DATA_PATH,
config_path=CONFIG_PATH,
type=TYPE,
settings=INTERPOLATION_SETTINGS,
initial_import_multi_index=MULTI_INDEX_INPUT,
overwrite=OVERWRITE)
interpolator.run()
Interpolating 9 Ear_left_1 body-parts in video Together_1...
Interpolating 2 Ear_right_1 body-parts in video Together_1...
Interpolating 7 Nose_1 body-parts in video Together_1...
Interpolating 2 Lat_left_1 body-parts in video Together_1...
Interpolating 3 Lat_right_1 body-parts in video Together_1...
Interpolating 0 Center_1 body-parts in video Together_1...
Interpolating 3 Tail_base_1 body-parts in video Together_1...
Interpolating 0 Ear_left_2 body-parts in video Together_1...
Interpolating 0 Ear_right_2 body-parts in video Together_1...
Interpolating 0 Nose_2 body-parts in video Together_1...
Interpolating 0 Lat_left_2 body-parts in video Together_1...
Interpolating 2 Lat_right_2 body-parts in video Together_1...
Interpolating 0 Center_2 body-parts in video Together_1...
Interpolating 2 Tail_base_2 body-parts in video Together_1...
SIMBA COMPLETE: Interpolation complete! (elapsed time: 0.5042s) complete
[34]:
# RUN SMOOTHING EXAMPLE 2
smoother = AdvancedSmoother(data_dir=DATA_PATH,
config_path=CONFIG_PATH,
type=TYPE,
settings=SMOOTHING_SETTINGS,
initial_import_multi_index=True,
overwrite=OVERWRITE)
smoother.run()
Smoothing data in video Together_1...
SIMBA COMPLETE: Smoothing complete complete! (elapsed time: 0.3942s) complete
[ ]: