Skip to content

Commit cfd2556

Browse files
author
Saurabh Pandey
committed
compute and trim function work in progress
1 parent 5c1a721 commit cfd2556

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

PostProcessing/PostProcessVideos.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ def main(input_dir: Path, output_dir: Path):
8585
# Will be filled with key=clientID:str, data=Tuple[csv:DataFrame, videofile:str]
8686
clients_data: Dict[str, Tuple[pd.DataFrame, str]] = dict()
8787

88-
for cID in clientIDs:
88+
df_list = []
89+
90+
for cID in clientIDs[1:]:
8991
client_dir = input_dir / cID
9092
CSVs = list(client_dir.glob("*.csv"))
9193
MP4s = list(client_dir.glob("*.mp4"))
92-
9394
#
9495
# Consistency check. Each clientID folder must have exactly 1 CSV and 1 mp4.
9596
if len(CSVs) != 1:
@@ -99,36 +100,37 @@ def main(input_dir: Path, output_dir: Path):
99100
raise Exception(f"Expecting 1 MP4 file for client {cID}. Found {len(MP4s)}.")
100101

101102
csv_file = CSVs[0]
102-
mp4_file = MP4s[1]
103+
mp4_file = MP4s[0]
103104

104105
df: pd.DataFrame = pd.read_csv(csv_file, header=None)
105106

106107
clients_data[cID] = (df, str(mp4_file))
107-
108+
df_list.append(df)
108109

109110
# Define the path to the directory containing the CSV files
110111
# csv_path = "/Users/tbc/Desktop/test_data/"
111112

112113
#
113114
# Repair CSVs (TODO - Mina)
114-
repaired_client_data = dict()
115-
for cID, (df, mp4) in clients_data:
116-
repaired_df = repair_dropped_frames(df)
117-
repaired_client_data[cID] = repaired_df, mp4
115+
# repaired_client_data = dict()
116+
# for cID, (df, mp4) in clients_data:
117+
# repaired_df = repair_dropped_frames(df)
118+
# repaired_client_data[cID] = repaired_df, mp4
119+
118120

119121
#
120122
# Find time ranges (Saurabh, To test better)
121123
# Compute the time range
122-
dfs = [df for k, (df, _) in clients_data]
123-
min_common, max_common = compute_time_range(dfs)
124+
#dfs = [df for k, (df, _) in clients_data]
125+
min_common, max_common = compute_time_range(df_list)
124126

125127
#
126128
# Trim CSVs (TODO)
127129
# Trim the data frames to the time range and save to new CSV files
128130
csv_path = output_dir / "test"
129131
# TODO -- actually, we don't need to save them. We could just return them as DataFrame instances
130-
trim_into_interval(csv_path, dfs, min_common, max_common)
131-
132+
trimmed_dataframes = trim_into_interval(df_list, min_common, max_common, THRESHOLD_NS)
133+
132134

133135
#
134136
# Extract the frames from the original videos

PostProcessing/dataframes.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,40 @@
44
def repair_dropped_frames(df: pd.DataFrame) -> pd.DataFrame:
55
pass
66

7-
7+
# Function to find the largest value in the first entry of all dataframes
8+
def find_largest_first_entry(dfs):
9+
largest_value = float('-inf')
10+
for df in dfs:
11+
first_entry = df.iloc[0, 0]
12+
if first_entry > largest_value:
13+
largest_value = first_entry
14+
return largest_value
15+
16+
# Function to find the smallest value in the last entry of selected dataframes
17+
def find_smallest_last_entry(dfs):
18+
smallest_value = float('inf')
19+
for df in dfs:
20+
last_entry = df.iloc[-1, 0]
21+
if last_entry < smallest_value:
22+
smallest_value = last_entry
23+
return smallest_value
24+
25+
# Function to find the largest & smallest value in the first and last entry of dataframes
826
def compute_time_range(dfs):
927
# Find the lowest and highest numbers in all the data frames
10-
min_common = max(df.iloc[:,0].min() for df in dfs)
11-
max_common = min(df.iloc[:,0].max() for df in dfs)
12-
13-
# Print the results
14-
print(f"The lowest common number is {min_common}")
15-
print(f"The highest common number is {max_common}")
16-
17-
return (min_common, max_common)
18-
19-
20-
def trim_into_interval(csv_path, dfs, min_common, max_common):
21-
# Trim each data frame to the min_common and max_common interval and save to a new file
22-
for i, df in enumerate(dfs):
23-
df_trimmed = df[(df.iloc[:,0] >= min_common) & (df.iloc[:,0] <= max_common)]
24-
df_trimmed.to_csv(f"{csv_path}trimmed_df_{i+1}.csv", header=False, index=False)
25-
26-
# Print the results
27-
print(f"{len(dfs)} data frames trimmed and saved to {csv_path}")
28+
lower_value = find_largest_first_entry(dfs)
29+
higher_value = find_smallest_last_entry(dfs)
30+
31+
# return the results
32+
return (lower_value, higher_value)
33+
34+
# Function to trim dataframes based on specified values
35+
def trim_into_interval(dfs, min_common, max_common, threshold):
36+
trimmed_dataframes = []
37+
# import pdb;pdb.set_trace()
38+
for df in dfs:
39+
start = df[(df.iloc[:, 0] >= min_common - threshold) & (df.iloc[:, 0] <= min_common + threshold)]
40+
end = df[(df.iloc[:, 0] >= max_common - threshold) & (df.iloc[:, 0] <= max_common + threshold)]
41+
trimmed_df = df[(df.iloc[:, 0] >= start.iloc[0, 0]) & (df.iloc[:, 0] <= end.iloc[0, 0])].reset_index(drop=True)
42+
trimmed_dataframes.append(trimmed_df)
43+
return trimmed_dataframes

0 commit comments

Comments
 (0)