Commit 999e2399 authored by lucas_miranda's avatar lucas_miranda
Browse files

Added tests for preprocess.py

parent 4abebccc
......@@ -506,6 +506,7 @@ class table_dict(dict):
{k: value for k, value in self.items() if k in keys}, self._type
)
# noinspection PyTypeChecker
def plot_heatmaps(self, bodyparts, save=False, i=0):
if self._type != "coords" or self._polar:
......@@ -549,6 +550,7 @@ class table_dict(dict):
return X_train, X_test
# noinspection PyTypeChecker,PyGlobalUndefined
def preprocess(
self,
window_size=1,
......@@ -565,6 +567,7 @@ class table_dict(dict):
"""Builds a sliding window. If specified, splits train and test and
Z-scores the data using sklearn's standard scaler"""
global g
X_train, X_test = self.get_training_set(test_videos)
if scale:
......@@ -616,7 +619,7 @@ class table_dict(dict):
]
)
g /= np.max(g)
X_train = X_train * g.reshape(1, window_size, 1)
X_train = X_train * g.reshape([1, window_size, 1])
if test_videos:
......@@ -629,7 +632,7 @@ class table_dict(dict):
X_test = align_trajectories(X_test, align)
if conv_filter == "gaussian":
X_test = X_test * g.reshape(1, window_size, 1)
X_test = X_test * g.reshape([1, window_size, 1])
if shuffle:
X_train = X_train[
......@@ -650,7 +653,7 @@ class table_dict(dict):
def random_projection(self, n_components=None, sample=1000):
X = self.get_training_set()
X = self.get_training_set()[0]
X = X[np.random.choice(X.shape[0], sample, replace=False), :]
rproj = random_projection.GaussianRandomProjection(n_components=n_components)
......@@ -660,7 +663,7 @@ class table_dict(dict):
def pca(self, n_components=None, sample=1000, kernel="linear"):
X = self.get_training_set()
X = self.get_training_set()[0]
X = X[np.random.choice(X.shape[0], sample, replace=False), :]
pca = KernelPCA(n_components=n_components, kernel=kernel)
......@@ -670,7 +673,7 @@ class table_dict(dict):
def tsne(self, n_components=None, sample=1000, perplexity=30):
X = self.get_training_set()
X = self.get_training_set()[0]
X = X[np.random.choice(X.shape[0], sample, replace=False), :]
tsne = TSNE(n_components=n_components, perplexity=perplexity)
......
......@@ -16,7 +16,7 @@ from scipy import spatial
from scipy import stats
from sklearn import mixture
from tqdm import tqdm
from typing import Tuple, Any, List, Union, Dict, NewType
from typing import Tuple, Any, List, Union, NewType
# DEFINE CUSTOM ANNOTATED TYPES #
......@@ -936,7 +936,34 @@ def rule_based_tagging(
huddle_spine: int = 10,
huddle_speed: int = 1,
) -> pd.DataFrame:
"""Outputs a dataframe with the motives registered per frame."""
"""Outputs a dataframe with the registered motives per frame. If specified, produces a labeled
video displaying the information in real time
Parameters:
- tracks (list):
- videos (list):
- coordinates (deepof.preprocessing.coordinates):
- vid_index (int):
- animal_ids (list):
- show (bool):
- save (bool):
- fps (float):
- speed_pause (int):
- frame_limit (float):
- recog_limit (int):
- path (str):
- arena_type (str):
- close_contact_tol (int):
- side_contact_tol (int):
- follow_frames (int):
- follow_tol (int):
- huddle_forward (int):
- huddle_spine (int):
- huddle_speed (int):
Returns:
- tag_df (pandas.DataFrame): table with traits as columns and frames as rows. Each
value is a boolean indicating trait detection at a given time"""
vid_name = re.findall("(.*?)_", tracks[vid_index])[0]
......
......@@ -206,7 +206,7 @@ def test_get_table_dicts(nodes, ego, sampler):
else:
align = False
table.preprocess(
prep = table.preprocess(
window_size=11,
window_step=1,
scale=sampler.draw(st.one_of(st.just("standard"), st.just("minmax"))),
......@@ -218,3 +218,14 @@ def test_get_table_dicts(nodes, ego, sampler):
shuffle=sampler.draw(st.booleans()),
align=align,
)
assert (type(prep) == np.ndarray or type(prep) == tuple)
if type(prep) == tuple:
assert type(prep[0]) == np.ndarray
# deepof dimensionality reduction testing
assert type(table.random_projection(n_components=2, sample=50)) == tuple
assert type(table.pca(n_components=2, sample=50)) == tuple
assert type(table.tsne(n_components=2, sample=50)) == tuple
......@@ -260,6 +260,7 @@ def test_smooth_boolean_array(a):
smooth = smooth_boolean_array(a)
def trans(x):
"""In situ function for computing boolean transitions"""
return sum([i + 1 != i for i in range(x.shape[0] - 1)])
assert trans(a) >= trans(smooth)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment