-
Notifications
You must be signed in to change notification settings - Fork 12
/
generate_matches_remote.py
53 lines (42 loc) · 1.91 KB
/
generate_matches_remote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import pandas as pd
from pickle import loads
from winnow.utils.matches import get_summarized_matches, unique
import os
OUTPUT_FILENAME = "match_df.csv"
OUTPUT_FOLDER = "data"
OUTPUT_FP = os.path.join(OUTPUT_FOLDER, OUTPUT_FILENAME)
DISTANCE = 0.75
# Mappings between source ids and connection strings
connections_dict = dict(
partner_a="postgres:https://postgres:admin@localhost:5432/videodeduplicationdb",
partner_b="postgres:https://postgres:admin@localhost:5432/videodeduplicationdb",
central_repo="postgres:https://postgres:admin@localhost:5432/videodeduplicationdb",
)
dfs = []
for identifier, connstring in connections_dict.items():
print(f"Loading data from {identifier}")
sample_df = pd.read_sql_query('select * from "signatures"', con=connstring)
sample_df["identifier"] = identifier
dfs.append(sample_df)
df = pd.concat(dfs)
del dfs
# Extract relevant information from the unified DataFrame
ids = np.array(df["file_id"])
source_identifiers = np.array(df["identifier"])
video_signatures = np.array([loads(x) for x in df["signature"].values])
# Get a summary of the matches of the input signatures as a DataFrame
match_df = get_summarized_matches(video_signatures, distance=DISTANCE)
# Enrich match data with information about the input files and source
match_df["query_video"] = ids[match_df["query"]]
match_df["query_video_source_id"] = source_identifiers[match_df["query"]]
match_df["match_video"] = ids[match_df["match"]]
match_df["match_video_source_id"] = source_identifiers[match_df["match"]]
match_df["self_match"] = match_df["query_video"] == match_df["match_video"]
# Remove self matches
match_df = match_df.loc[~match_df["self_match"], :]
# Removes duplicated entries (eg if A matches B, we don't need B matches A)
match_df["unique_index"] = match_df.apply(unique, axis=1)
match_df = match_df.drop_duplicates(subset=["unique_index"])
print(f"Saved matches to {OUTPUT_FP}")
match_df.to_csv(OUTPUT_FP)