Skip to content

Commit 74f8e05

Browse files
committed
Cleaned up tests. Fixed df repair header assumptions
1 parent d39bfc3 commit 74f8e05

File tree

3 files changed

+33
-62
lines changed

3 files changed

+33
-62
lines changed

PostProcessing/PostProcessVideos.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import os
2-
import csv
31
import argparse
42
from pathlib import Path
5-
from typing import List, Dict, Tuple
3+
from typing import List, Tuple
64

75
import pandas as pd
8-
import cv2
96
import re
107

118
from dataframes import compute_time_range, trim_into_interval, repair_dropped_frames

PostProcessing/dataframes.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
from typing import Tuple
55

6-
THRESHOLD_NS = 10 * 1000 * 1000 #10 milisecond
6+
THRESHOLD_NS = 10 * 1000 * 1000 # 10 milisecond
7+
78

89
def compute_time_step(video_timestamps: pd.DataFrame) -> float:
910
"""
@@ -16,15 +17,18 @@ def compute_time_step(video_timestamps: pd.DataFrame) -> float:
1617
float: The time step of the video time stamps.
1718
"""
1819

19-
time_step = (video_timestamps['timestamp'].diff()).dropna().value_counts().index[0]
20-
20+
first_col_name = video_timestamps.columns[0]
21+
time_step = (video_timestamps[first_col_name].diff()).dropna().value_counts().index[0]
2122

2223
return time_step
2324

2425

2526
def repair_dropped_frames(df: pd.DataFrame, time_step: float) -> pd.DataFrame:
26-
df['timestamp'] = pd.to_datetime(df['timestamp']).astype(np.int64)
27-
timestamps = df['timestamp']
27+
28+
first_col_name = df.columns[0]
29+
30+
df[first_col_name] = pd.to_datetime(df[first_col_name]).astype(np.int64)
31+
timestamps = df[first_col_name]
2832
repaired_rows = []
2933

3034
# Check for missing timestamps and generate them
@@ -47,7 +51,7 @@ def repair_dropped_frames(df: pd.DataFrame, time_step: float) -> pd.DataFrame:
4751

4852
print(len(repaired_rows))
4953
# Create a new DataFrame with repaired rows
50-
columns = ['timestamp', 'Generated']
54+
columns = ['timestamp', 'generated']
5155
output_df = pd.DataFrame(repaired_rows, columns=columns)
5256
output_df['timestamp'] = pd.to_datetime(output_df['timestamp']).astype(np.int64)
5357

PostProcessing/test_functions.py

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22

33
import os
44
from pathlib import Path
5-
65
from typing import List, Tuple
76

8-
9-
from dataframes import repair_dropped_frames, compute_time_range, trim_into_interval
10-
from PostProcessVideos import scan_session_dir
11-
12-
137
import pandas as pd
148

15-
import pandas.testing as pd_testing
9+
from dataframes import compute_time_step
10+
from dataframes import repair_dropped_frames, compute_time_range
11+
12+
from PostProcessVideos import scan_session_dir
1613

17-
import numpy as np
1814

1915
RECSYNCH_SESSION_DIR_VAR = "RECSYNCH_SESSION_DIR"
2016

@@ -56,61 +52,35 @@ def session_data_list() -> List[Tuple[str, pd.DataFrame, str]]:
5652
clienIDs, dataframes, video_paths = scan_session_dir(Path(RECSYNCH_SESSION_DIR))
5753

5854
for clientID, df, video_path in zip(clienIDs, dataframes, video_paths):
59-
yield (clientID, df, video_path)
60-
61-
62-
def client_IDs() -> List[Path]:
63-
64-
out = []
65-
66-
for p in Path(RECSYNCH_SESSION_DIR).iterdir():
67-
print("-->", p.stem)
68-
out.append(p)
69-
70-
return out
71-
55+
yield clientID, df, video_path
7256

73-
def CSVs() -> List[str]:
7457

75-
out = []
76-
77-
clients = client_IDs()
78-
for c in clients:
79-
client_dir = Path(RECSYNCH_SESSION_DIR) / c
80-
for csv_file in client_dir.glob("*.csv"):
81-
print("==>", csv_file)
82-
rel_filepath = str(csv_file.relative_to(RECSYNCH_SESSION_DIR))
83-
print("++>", rel_filepath)
84-
out.append(rel_filepath)
85-
86-
return out
87-
88-
89-
@pytest.mark.parametrize("csv_file", CSVs())
90-
def test_df_reparation(csv_file):
91-
92-
# Load the test dataframes
93-
csv_path = Path(RECSYNCH_SESSION_DIR) / csv_file
94-
df = pd.read_csv(csv_path)
58+
@pytest.mark.parametrize("client_data", session_data_list())
59+
def test_df_reparation(client_data):
9560

61+
_, df, _ = client_data
9662
assert len(df) >= 2
9763

98-
repaired_df = repair_dropped_frames(df)
64+
first_col_name = df.columns[0]
9965

100-
assert len(repaired_df) >= len(df)
66+
time_step = compute_time_step(df)
67+
assert time_step > 0
10168

102-
@pytest.mark.parametrize("client_data", session_data_list())
103-
def test_df_reparation(client_data):
69+
repaired_df = repair_dropped_frames(df, time_step)
10470

105-
_, df, _ = client_data
71+
assert len(repaired_df) > 2
10672

107-
assert len(df) >= 2
73+
assert len(repaired_df) >= len(df), "Dataframe size was reduced after reparation."
10874

109-
repaired_df = repair_dropped_frames(df)
75+
repaired_first_col_name = repaired_df.columns[0]
76+
repaired_second_col_name = repaired_df.columns[1]
77+
assert repaired_first_col_name == "timestamp"
78+
assert repaired_second_col_name == "generated"
11079

111-
assert len(repaired_df) >= len(df)
112-
assert df[0].iloc[0] == repaired_df[0].iloc[0]
113-
assert df[0].iloc[-1] == repaired_df[0].iloc[-1]
80+
assert df[first_col_name].iloc[0] == repaired_df[repaired_first_col_name].iloc[0],\
81+
"The first element changed after reparation"
82+
assert df[first_col_name].iloc[-1] == repaired_df[repaired_first_col_name].iloc[-1],\
83+
"The last element changed after reparation"
11484

11585

11686
def test_df_trimming(session_data):

0 commit comments

Comments
 (0)