Commit b001609f authored by lucas_miranda's avatar lucas_miranda
Browse files

Added test_deepof for testing functions with pytest and hypothesis

parent e9f79f58
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%load_ext autoreload %load_ext autoreload
%autoreload 2 %autoreload 2
   
import warnings import warnings
warnings.filterwarnings("ignore") warnings.filterwarnings("ignore")
``` ```
   
%% Output %% Output
   
The autoreload extension is already loaded. To reload it, use: The autoreload extension is already loaded. To reload it, use:
%reload_ext autoreload %reload_ext autoreload
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
#from source.utils import * #from source.utils import *
from source.preprocess import * from source.preprocess import *
from source.model_utils import * from source.model_utils import *
import pickle import pickle
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import pandas as pd import pandas as pd
from collections import defaultdict from collections import defaultdict
from tqdm import tqdm_notebook as tqdm from tqdm import tqdm_notebook as tqdm
``` ```
   
%% Cell type:code id: tags:parameters %% Cell type:code id: tags:parameters
   
``` python ``` python
path = "../../Desktop/DLC_social_1/" path = "../../Desktop/DLC_social_1/"
path2 = "../../Desktop/DLC_social_2/" path2 = "../../Desktop/DLC_social_2/"
``` ```
   
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
   
# Set up and design the project # Set up and design the project
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
with open('{}DLC_social_1_exp_conditions.pickle'.format(path), 'rb') as handle: with open('{}DLC_social_1_exp_conditions.pickle'.format(path), 'rb') as handle:
Treatment_dict = pickle.load(handle) Treatment_dict = pickle.load(handle)
``` ```
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
Treatment_dict["WT+NS"] Treatment_dict["WT+NS"]
``` ```
   
%% Output %% Output
   
['Test 6DLC', ['Test 6DLC',
'Test 15DLC', 'Test 15DLC',
'Test 24DLC', 'Test 24DLC',
'Test 29DLC', 'Test 29DLC',
'Test 38DLC', 'Test 38DLC',
'Test 47DLC', 'Test 47DLC',
'Day2Test8DLC', 'Day2Test8DLC',
'Day2Test13DLC', 'Day2Test13DLC',
'Day2Test22DLC', 'Day2Test22DLC',
'Day2Test31DLC', 'Day2Test31DLC',
'Day2Test40DLC'] 'Day2Test40DLC']
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
#Which angles to compute? #Which angles to compute?
bp_dict = {'B_Nose':['B_Left_ear','B_Right_ear'], bp_dict = {'B_Nose':['B_Left_ear','B_Right_ear'],
'B_Left_ear':['B_Nose','B_Right_ear','B_Center','B_Left_flank'], 'B_Left_ear':['B_Nose','B_Right_ear','B_Center','B_Left_flank'],
'B_Right_ear':['B_Nose','B_Left_ear','B_Center','B_Right_flank'], 'B_Right_ear':['B_Nose','B_Left_ear','B_Center','B_Right_flank'],
'B_Center':['B_Left_ear','B_Right_ear','B_Left_flank','B_Right_flank','B_Tail_base'], 'B_Center':['B_Left_ear','B_Right_ear','B_Left_flank','B_Right_flank','B_Tail_base'],
'B_Left_flank':['B_Left_ear','B_Center','B_Tail_base'], 'B_Left_flank':['B_Left_ear','B_Center','B_Tail_base'],
'B_Right_flank':['B_Right_ear','B_Center','B_Tail_base'], 'B_Right_flank':['B_Right_ear','B_Center','B_Tail_base'],
'B_Tail_base':['B_Center','B_Left_flank','B_Right_flank']} 'B_Tail_base':['B_Center','B_Left_flank','B_Right_flank']}
``` ```
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%%time %%time
DLC_social_1 = project(path=path,#Path where to find the required files DLC_social_1 = project(path=path,#Path where to find the required files
smooth_alpha=0.5, #Alpha value for exponentially weighted smoothing smooth_alpha=0.5, #Alpha value for exponentially weighted smoothing
distances=['B_Center','B_Nose','B_Left_ear','B_Right_ear','B_Left_flank', distances=['B_Center','B_Nose','B_Left_ear','B_Right_ear','B_Left_flank',
'B_Right_flank','B_Tail_base'], 'B_Right_flank','B_Tail_base'],
ego=False, ego=False,
angles=True, angles=True,
connectivity=bp_dict, connectivity=bp_dict,
arena='circular', #Type of arena used in the experiments arena='circular', #Type of arena used in the experiments
arena_dims=[380], #Dimensions of the arena. Just one if it's circular arena_dims=[380], #Dimensions of the arena. Just one if it's circular
subset_condition="B", subset_condition="B",
video_format='.mp4', video_format='.mp4',
table_format='.h5', table_format='.h5',
exp_conditions=Treatment_dict) exp_conditions=Treatment_dict)
``` ```
   
%% Output %% Output
   
CPU times: user 2.73 s, sys: 809 ms, total: 3.54 s CPU times: user 2.72 s, sys: 846 ms, total: 3.57 s
Wall time: 1.37 s Wall time: 1.45 s
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%%time %%time
DLC_social_2 = project(path=path2,#Path where to find the required files DLC_social_2 = project(path=path2,#Path where to find the required files
smooth_alpha=0.5, #Alpha value for exponentially weighted smoothing smooth_alpha=0.5, #Alpha value for exponentially weighted smoothing
distances=['B_Center','B_Nose','B_Left_ear','B_Right_ear','B_Left_flank', distances=['B_Center','B_Nose','B_Left_ear','B_Right_ear','B_Left_flank',
'B_Right_flank','B_Tail_base'], 'B_Right_flank','B_Tail_base'],
ego=False, ego=False,
angles=True, angles=True,
connectivity=bp_dict, connectivity=bp_dict,
arena='circular', #Type of arena used in the experiments arena='circular', #Type of arena used in the experiments
arena_dims=[380], #Dimensions of the arena. Just one if it's circular arena_dims=[380], #Dimensions of the arena. Just one if it's circular
subset_condition="B", subset_condition="B",
video_format='.mp4', video_format='.mp4',
table_format='.h5') table_format='.h5')
``` ```
   
%% Output %% Output
   
CPU times: user 6.67 s, sys: 1.02 s, total: 7.69 s CPU times: user 6.56 s, sys: 1.05 s, total: 7.62 s
Wall time: 1.65 s Wall time: 1.73 s
   
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
   
# Run project # Run project
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%%time %%time
DLC_social_1_coords = DLC_social_1.run(verbose=True) DLC_social_1_coords = DLC_social_1.run(verbose=True)
print(DLC_social_1_coords) print(DLC_social_1_coords)
type(DLC_social_1_coords) type(DLC_social_1_coords)
``` ```
   
%% Output %% Output
   
Loading trajectories... Loading trajectories...
Smoothing trajectories... Smoothing trajectories...
Computing distances... Computing distances...
Computing angles... Computing angles...
Done! Done!
Coordinates of 47 videos across 4 conditions Coordinates of 47 videos across 4 conditions
CPU times: user 5.22 s, sys: 714 ms, total: 5.93 s CPU times: user 5.54 s, sys: 1.09 s, total: 6.63 s
Wall time: 6.67 s Wall time: 13.1 s
   
source.preprocess.coordinates source.preprocess.coordinates
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%%time %%time
DLC_social_2_coords = DLC_social_2.run(verbose=True) DLC_social_2_coords = DLC_social_2.run(verbose=True)
print(DLC_social_2_coords) print(DLC_social_2_coords)
type(DLC_social_2_coords) type(DLC_social_2_coords)
``` ```
   
%% Output %% Output
   
Loading trajectories... Loading trajectories...
Smoothing trajectories... Smoothing trajectories...
Computing distances... Computing distances...
Computing angles... Computing angles...
Done! Done!
DLC analysis of 31 videos DLC analysis of 31 videos
CPU times: user 3.14 s, sys: 326 ms, total: 3.47 s CPU times: user 3.04 s, sys: 262 ms, total: 3.31 s
Wall time: 3.64 s Wall time: 6.14 s
   
source.preprocess.coordinates source.preprocess.coordinates
   
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
   
# Generate coords # Generate coords
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%%time %%time
ptest = DLC_social_1_coords.get_coords(center="B_Center", polar=False, speed=0, length='00:10:00') ptest = DLC_social_1_coords.get_coords(center="B_Center", polar=False, speed=0, length='00:10:00')
ptest._type ptest._type
   
ptest2 = DLC_social_2_coords.get_coords(center="B_Center", polar=False, speed=0, length='00:10:00') ptest2 = DLC_social_2_coords.get_coords(center="B_Center", polar=False, speed=0, length='00:10:00')
ptest2._type ptest2._type
``` ```
   
%% Output %% Output
   
CPU times: user 1.26 s, sys: 74.7 ms, total: 1.33 s CPU times: user 1.16 s, sys: 41.1 ms, total: 1.2 s
Wall time: 1.37 s Wall time: 1.24 s
   
'coords' 'coords'
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
ptest['Test 13DLC'].columns.levels ptest['Test 13DLC'].columns.levels
``` ```
   
%% Output %% Output
   
FrozenList([['B_Center', 'B_Left_ear', 'B_Left_flank', 'B_Nose', 'B_Right_ear', 'B_Right_flank', 'B_Tail_base'], ['x', 'y']]) FrozenList([['B_Center', 'B_Left_ear', 'B_Left_flank', 'B_Nose', 'B_Right_ear', 'B_Right_flank', 'B_Tail_base'], ['x', 'y']])
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%%time %%time
dtest = DLC_social_1_coords.get_distances(speed=0, length='00:10:00') dtest = DLC_social_1_coords.get_distances(speed=0, length='00:10:00')
dtest._type dtest._type
   
dtest2 = DLC_social_2_coords.get_distances(speed=0, length='00:10:00') dtest2 = DLC_social_2_coords.get_distances(speed=0, length='00:10:00')
dtest2._type dtest2._type
``` ```
   
%% Output %% Output
   
CPU times: user 89.9 ms, sys: 77.2 ms, total: 167 ms CPU times: user 92.1 ms, sys: 62 ms, total: 154 ms
Wall time: 209 ms Wall time: 297 ms
   
'dists' 'dists'
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%%time %%time
atest = DLC_social_1_coords.get_angles(degrees=True, speed=0, length='00:10:00') atest = DLC_social_1_coords.get_angles(degrees=True, speed=0, length='00:10:00')
atest._type atest._type
   
atest2 = DLC_social_2_coords.get_angles(degrees=True, speed=0, length='00:10:00') atest2 = DLC_social_2_coords.get_angles(degrees=True, speed=0, length='00:10:00')
atest2._type atest2._type
``` ```
   
%% Output %% Output
   
CPU times: user 232 ms, sys: 157 ms, total: 388 ms CPU times: user 288 ms, sys: 281 ms, total: 569 ms
Wall time: 419 ms Wall time: 626 ms
   
'angles' 'angles'
   
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
   
# Visualization playground # Visualization playground
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
# ptest.plot_heatmaps(['B_Nose'], i=2) # ptest.plot_heatmaps(['B_Nose'], i=2)
``` ```
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
ptest['Day2Test13DLC']['B_Nose'].iloc[:5000] ptest['Day2Test13DLC']['B_Nose'].iloc[:5000]
``` ```
   
%% Output %% Output
   
coords x y coords x y
00:00:00 19.370636 42.280235 00:00:00 19.370636 42.280235
00:00:00.040000 19.370636 42.280235 00:00:00.040000 19.370636 42.280235
00:00:00.080000 19.500778 42.243202 00:00:00.080000 19.500778 42.243202
00:00:00.120000 19.207077 42.388008 00:00:00.120000 19.207077 42.388008
00:00:00.160000 19.395851 42.293156 00:00:00.160000 19.395851 42.293156
... ... ... ... ... ...
00:03:19.800000 -35.108001 -15.180417 00:03:19.800000 -35.108001 -15.180417
00:03:19.840000 -36.197332 -15.403811 00:03:19.840000 -36.197332 -15.403811
00:03:19.880000 -37.723694 -13.514239 00:03:19.880000 -37.723694 -13.514239
00:03:19.920000 -39.470217 -13.346349 00:03:19.920000 -39.470217 -13.346349
00:03:19.960000 -40.692077 -11.919606 00:03:19.960000 -40.692077 -11.919606
[5000 rows x 2 columns] [5000 rows x 2 columns]
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
#Plot animation of trajectory over time with different smoothings #Plot animation of trajectory over time with different smoothings
# plt.plot(ptestb['Day2Test13DLC']['B_Nose'].iloc[:50]['x'], # plt.plot(ptestb['Day2Test13DLC']['B_Nose'].iloc[:50]['x'],
# ptestb['Day2Test13DLC']['B_Nose'].iloc[:50]['y'], label='alpha=0.95') # ptestb['Day2Test13DLC']['B_Nose'].iloc[:50]['y'], label='alpha=0.95')
   
# plt.plot(ptestd['Day2Test13DLC']['B_Nose'].iloc[:50]['x'], # plt.plot(ptestd['Day2Test13DLC']['B_Nose'].iloc[:50]['x'],
# ptestd['Day2Test13DLC']['B_Nose'].iloc[:50]['y'], label='alpha=0.65') # ptestd['Day2Test13DLC']['B_Nose'].iloc[:50]['y'], label='alpha=0.65')
   
# plt.xlabel('x') # plt.xlabel('x')
# plt.ylabel('y') # plt.ylabel('y')
# plt.title('Mouse Center Trajectory using different exponential smoothings') # plt.title('Mouse Center Trajectory using different exponential smoothings')
# plt.legend() # plt.legend()
# plt.show() # plt.show()
``` ```
   
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
   
# Dimensionality reduction playground # Dimensionality reduction playground
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
#pca = ptest.pca(4, 1000) #pca = ptest.pca(4, 1000)
``` ```
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
#plt.scatter(*pca[0].T) #plt.scatter(*pca[0].T)
#plt.show() #plt.show()
``` ```
   
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
   
# Preprocessing playground # Preprocessing playground
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
mtest = merge_tables( mtest = merge_tables(
DLC_social_1_coords.get_coords(center="B_Center", polar=False, length='00:10:00', align='B_Nose') DLC_social_1_coords.get_coords(center="B_Center", polar=False, length='00:10:00', align='B_Nose')
#DLC_social_1_coords.get_distances(speed=0, length='00:10:00'), #DLC_social_1_coords.get_distances(speed=0, length='00:10:00'),
#DLC_social_1_coords.get_angles(degrees=True, speed=0, length='00:10:00'), #DLC_social_1_coords.get_angles(degrees=True, speed=0, length='00:10:00'),
) )
``` ```
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
mtest2 = merge_tables( mtest2 = merge_tables(
DLC_social_2_coords.get_coords(center="B_Center", polar=False, length='00:10:00', align='B_Nose'), DLC_social_2_coords.get_coords(center="B_Center", polar=False, length='00:10:00', align='B_Nose'),
#DLC_social_2_coords.get_distances(speed=0, length='00:10:00'), #DLC_social_2_coords.get_distances(speed=0, length='00:10:00'),
#DLC_social_2_coords.get_angles(degrees=True, speed=0, length='00:10:00'), #DLC_social_2_coords.get_angles(degrees=True, speed=0, length='00:10:00'),
) )
``` ```
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%%time %%time
pttest = mtest.preprocess(window_size=13, window_step=10, filter=None, sigma=55, pttest = mtest.preprocess(window_size=13, window_step=10, filter=None, sigma=55,
shift=0, scale='standard', align='center', shuffle=True, test_videos=0) shift=0, scale='standard', align='center', shuffle=True, test_videos=0)
print(pttest.shape) print(pttest.shape)
#print(pttrain.shape) #print(pttrain.shape)
``` ```
   
%% Output %% Output
   
(70504, 13, 12) (70504, 13, 12)
CPU times: user 2 s, sys: 153 ms, total: 2.16 s (70504, 13, 12)
Wall time: 2.19 s CPU times: user 2.09 s, sys: 130 ms, total: 2.22 s
Wall time: 2.3 s
   
%% Cell type:code id: tags: %% Cell type:code id: tags:
   
``` python ``` python
%%time %%time
pttest2 = mtest2.preprocess(window_size=13, window_step=1, filter=None, sigma=55, pttest2 = mtest2.preprocess(window_size=13, window_step=1, filter=None, sigma=55,
shift=0, scale="standard", align='all', shuffle=False) shift=0, scale="standard", align='all', shuffle=False)
pttest2.shape pttest2.shape
``` ```
   
%% Output %% Output
   
CPU times: user 9.33 s, sys: 90 ms, total: 9.42 s CPU times: user 9.95 s, sys: 82.6 ms, total: 10 s
Wall time: 9.42 s