Skip to content

Commit 4264225

Browse files
Saurabh PandeySaurabh Pandey
authored andcommitted
merged updated code
2 parents f23dd3f + b2be6b4 commit 4264225

File tree

1 file changed

+88
-60
lines changed

1 file changed

+88
-60
lines changed

PostProcessing/test_functions.py

Lines changed: 88 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import os
44
from pathlib import Path
55

6-
from typing import List
6+
from typing import List, Tuple
7+
78

89
from dataframes import repair_dropped_frames, compute_time_range, trim_into_interval
10+
from PostProcessVideos import scan_session_dir
11+
912

1013
import pandas as pd
1114

@@ -19,83 +22,108 @@
1922
RECSYNCH_SESSION_DIR = os.environ.get(RECSYNCH_SESSION_DIR_VAR)
2023
print("Data root set at '{}'.".format(RECSYNCH_SESSION_DIR))
2124

22-
#@pytest.fixture(autouse=True)
23-
#def client_dir() -> str:
24-
# if RECSYNCH_SESSION_DIR is None:
25-
# raise Exception(f"Environment variable {RECSYNCH_SESSION_DIR_VAR} not defined")
25+
26+
@pytest.fixture(scope="session", autouse=True)
27+
def session_data() -> Tuple[List[str], List[pd.DataFrame], List[str]]:
28+
29+
assert RECSYNCH_SESSION_DIR is not None, "Variable RECSYNCH_SESSION_DIR is None."
30+
assert os.path.exists(RECSYNCH_SESSION_DIR)
31+
assert os.path.isdir(RECSYNCH_SESSION_DIR)
32+
33+
clienIDs, dataframes, video_paths = scan_session_dir(Path(RECSYNCH_SESSION_DIR))
34+
35+
return clienIDs, dataframes, video_paths
36+
37+
38+
def test_session_data(session_data):
39+
40+
clienIDs, dataframes, video_paths = session_data
41+
42+
assert len(clienIDs) > 0
43+
assert len(clienIDs) == len(dataframes) == len(video_paths)
44+
45+
for df in dataframes:
46+
assert len(df) > 0
47+
assert len(df.columns) == 1
48+
49+
for vp in video_paths:
50+
assert os.path.exists(vp)
51+
assert os.path.isfile(vp)
52+
53+
54+
def session_data_list() -> List[Tuple[str, pd.DataFrame, str]]:
55+
56+
clienIDs, dataframes, video_paths = scan_session_dir(Path(RECSYNCH_SESSION_DIR))
57+
58+
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
2671

2772

28-
# def client_IDs() -> List[str]:
73+
def CSVs() -> List[str]:
2974

30-
# out = []
75+
out = []
3176

32-
# for p in Path(RECSYNCH_SESSION_DIR).iterdir():
33-
# print("-->", p.stem)
34-
# out.append(p)
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)
3585

36-
# return out
86+
return out
3787

3888

39-
# def CSVs() -> List[str]:
89+
@pytest.mark.parametrize("csv_file", CSVs())
90+
def test_df_reparation(csv_file):
4091

41-
# out = []
92+
# Load the test dataframes
93+
csv_path = Path(RECSYNCH_SESSION_DIR) / csv_file
94+
df = pd.read_csv(csv_path)
4295

43-
# clients = client_IDs()
44-
# for c in clients:
45-
# client_dir = Path(RECSYNCH_SESSION_DIR) / c
46-
# for csv_file in client_dir.glob("*.csv"):
47-
# print("==>", csv_file)
48-
# rel_filepath = str(csv_file.relative_to(RECSYNCH_SESSION_DIR))
49-
# print("++>", rel_filepath)
50-
# out.append(rel_filepath)
96+
assert len(df) >= 2
5197

52-
# return out
98+
repaired_df = repair_dropped_frames(df)
5399

100+
assert len(repaired_df) >= len(df)
54101

55-
# @pytest.mark.parametrize("csv_file", CSVs())
56-
# def test_df_reparation(csv_file):
102+
@pytest.mark.parametrize("client_data", session_data_list())
103+
def test_df_reparation(client_data):
57104

58-
# # Load the test dataframes
59-
# csv_path = Path(RECSYNCH_SESSION_DIR) / csv_file
60-
# df = pd.read_csv(csv_path)
105+
_, df, _ = client_data
61106

62-
# assert len(df) >= 2
107+
assert len(df) >= 2
63108

64-
# repaired_df = repair_dropped_frames(df)
109+
repaired_df = repair_dropped_frames(df)
65110

66-
# assert len(repaired_df) >= len(df)
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]
67114

68115

69-
def test_df_trim():
70-
# Create three sample dataframes with single column and no headers
71-
df1 = pd.DataFrame([9, 10, 11, 16])
72-
df2 = pd.DataFrame([7, 8, 12, 13])
73-
df3 = pd.DataFrame([12, 15, 16, 19])
116+
def test_df_trimming(session_data):
117+
_, dataframes, _ = session_data
74118

75-
# Add the dataframes to a list
76-
df_list = [df1, df2, df3]
119+
min_common, max_common = compute_time_range(dataframes)
120+
assert min_common <= max_common
77121

78-
# Compute time range and trim the dataframe
79-
min_val, max_val = compute_time_range(df_list)
80-
trimmed_df_list = trim_into_interval(df_list, min_val, max_val, 3)
122+
for df in dataframes:
123+
# Get the first element of the first column
124+
ts_start = df[0].iloc[0]
125+
assert ts_start <= min_common
81126

82-
# Create expected list of dataframes to compare with trimmed_df_list
83-
compare_list = [
84-
pd.DataFrame([9, 10, 11, 16]),
85-
pd.DataFrame([12, 13]),
86-
pd.DataFrame([12, 15, 16])
87-
]
88-
89-
# print(trimmed_df_list.to_string(index=False))
90-
# print(compare_list.to_string(index=False))
91-
# try:
92-
# for df1, df2 in zip(trimmed_df_list, compare_list):
93-
# df1_reset = df1.reset_index(drop=True)
94-
# df2_reset = df2.reset_index(drop=True)
95-
# pd_testing.assert_frame_equal(df1_reset, df2_reset)
96-
# print("All dataframes match")
97-
# except AssertionError as e:
98-
# print("Dataframes do not match:")
99-
# print(e)
100-
# Assert if all dataframes in df_list match the corresponding dataframes in compare_list
101-
assert trimmed_df_list == compare_list
127+
# Get the last element of the first column
128+
ts_end = df[0].iloc[-1]
129+
assert ts_end >= max_common

0 commit comments

Comments
 (0)