|
3 | 3 | import os
|
4 | 4 | from pathlib import Path
|
5 | 5 |
|
6 |
| -from typing import List |
| 6 | +from typing import List, Tuple |
| 7 | + |
7 | 8 |
|
8 | 9 | from dataframes import repair_dropped_frames, compute_time_range, trim_into_interval
|
| 10 | +from PostProcessVideos import scan_session_dir |
| 11 | + |
9 | 12 |
|
10 | 13 | import pandas as pd
|
11 | 14 |
|
|
19 | 22 | RECSYNCH_SESSION_DIR = os.environ.get(RECSYNCH_SESSION_DIR_VAR)
|
20 | 23 | print("Data root set at '{}'.".format(RECSYNCH_SESSION_DIR))
|
21 | 24 |
|
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 |
26 | 71 |
|
27 | 72 |
|
28 |
| -# def client_IDs() -> List[str]: |
| 73 | +def CSVs() -> List[str]: |
29 | 74 |
|
30 |
| -# out = [] |
| 75 | + out = [] |
31 | 76 |
|
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) |
35 | 85 |
|
36 |
| -# return out |
| 86 | + return out |
37 | 87 |
|
38 | 88 |
|
39 |
| -# def CSVs() -> List[str]: |
| 89 | +@pytest.mark.parametrize("csv_file", CSVs()) |
| 90 | +def test_df_reparation(csv_file): |
40 | 91 |
|
41 |
| -# out = [] |
| 92 | + # Load the test dataframes |
| 93 | + csv_path = Path(RECSYNCH_SESSION_DIR) / csv_file |
| 94 | + df = pd.read_csv(csv_path) |
42 | 95 |
|
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 |
51 | 97 |
|
52 |
| -# return out |
| 98 | + repaired_df = repair_dropped_frames(df) |
53 | 99 |
|
| 100 | + assert len(repaired_df) >= len(df) |
54 | 101 |
|
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): |
57 | 104 |
|
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 |
61 | 106 |
|
62 |
| -# assert len(df) >= 2 |
| 107 | + assert len(df) >= 2 |
63 | 108 |
|
64 |
| -# repaired_df = repair_dropped_frames(df) |
| 109 | + repaired_df = repair_dropped_frames(df) |
65 | 110 |
|
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] |
67 | 114 |
|
68 | 115 |
|
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 |
74 | 118 |
|
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 |
77 | 121 |
|
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 |
81 | 126 |
|
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