Paths fixed... need to double check the path exclusion format to feed api
This commit is contained in:
+45
-58
@@ -15,73 +15,60 @@
|
||||
|
||||
import pandas as pd
|
||||
import os
|
||||
from itertools import chain
|
||||
import ast
|
||||
import re
|
||||
|
||||
def split_path(path):
|
||||
parts = []
|
||||
while True:
|
||||
head, tail = os.path.split(path)
|
||||
if tail:
|
||||
parts.insert(0, tail)
|
||||
path = head
|
||||
else:
|
||||
if head:
|
||||
parts.insert(0, head)
|
||||
break
|
||||
return parts
|
||||
import os
|
||||
import pandas as pd
|
||||
|
||||
def local_common_pass(paths, min_parts=3):
|
||||
results = {}
|
||||
paths_sorted = sorted(paths)
|
||||
for i, path in enumerate(paths_sorted):
|
||||
candidates = []
|
||||
import os
|
||||
import pandas as pd
|
||||
|
||||
if i > 0:
|
||||
try:
|
||||
candidates.append(os.path.commonpath([path, paths_sorted[i-1]]))
|
||||
except ValueError:
|
||||
# different drives, skip
|
||||
pass
|
||||
if i < len(paths_sorted) - 1:
|
||||
try:
|
||||
candidates.append(os.path.commonpath([path, paths_sorted[i+1]]))
|
||||
except ValueError:
|
||||
# different drives, skip
|
||||
pass
|
||||
def split_filepaths_grouped(df, col="filename", group_parts=3, min_parts=3):
|
||||
def clean_split(path):
|
||||
parts = os.path.normpath(path).split(os.sep)
|
||||
# Remove leading empty strings caused by UNC paths
|
||||
parts = [p for p in parts if p]
|
||||
return parts
|
||||
|
||||
best = path
|
||||
best_len = 0
|
||||
for c in candidates:
|
||||
parts = split_path(c)
|
||||
if len(parts) >= min_parts and len(parts) > best_len:
|
||||
best = c
|
||||
best_len = len(parts)
|
||||
results[path] = best
|
||||
return results
|
||||
df = df.copy()
|
||||
split_paths = df[col].apply(clean_split)
|
||||
|
||||
def add_longest_common_two_local(df, col="filename_x", new_col="longestcfp", min_parts=3):
|
||||
dirs_series = df[col].astype(str).apply(os.path.dirname)
|
||||
first_pass = local_common_pass(dirs_series.tolist(), min_parts)
|
||||
second_pass = local_common_pass(list(first_pass.values()), min_parts)
|
||||
df[new_col] = dirs_series.map(lambda d: second_pass[first_pass[d]])
|
||||
return df
|
||||
# Filter out paths with fewer than `min_parts` components
|
||||
df = df[split_paths.apply(lambda parts: len(parts) >= min_parts)].copy()
|
||||
split_paths = split_paths[df.index] # Update split_paths to match filtered df
|
||||
|
||||
def export_groups_for_review(df, col, group_col, min_number_in_group, path_length_constant):
|
||||
"""
|
||||
Compute longest common paths, group filepaths, write CSV for review.
|
||||
"""
|
||||
df = df.drop_duplicates(subset=[col], keep='first')
|
||||
df = add_longest_common_two_local(df, col=col, new_col=group_col)
|
||||
grouped = df.groupby(group_col)[col].apply(list).reset_index()
|
||||
grouped = grouped.sort_values(by=col)
|
||||
print("Before filtering:", len(grouped))
|
||||
grouped = grouped[grouped[col].apply(lambda x: len(x) >= min_number_in_group)]
|
||||
filtered = grouped[grouped[group_col].apply(lambda x: len(os.path.normpath(x).split(os.sep)) >= path_length_constant)]
|
||||
print("After filtering:", len(grouped))
|
||||
df["group_key"] = split_paths.apply(lambda parts: os.sep.join(parts[:group_parts]))
|
||||
grouped = df.groupby("group_key")
|
||||
new_rows = []
|
||||
|
||||
return filtered, df
|
||||
for _, group_df in grouped:
|
||||
paths = group_df[col].tolist()
|
||||
split_parts = [clean_split(p) for p in paths]
|
||||
|
||||
def longest_common_prefix(paths):
|
||||
if not paths:
|
||||
return []
|
||||
prefix = paths[0]
|
||||
for path in paths[1:]:
|
||||
prefix = [a for a, b in zip(prefix, path) if a == b]
|
||||
if not prefix:
|
||||
break
|
||||
return prefix
|
||||
|
||||
common_prefix = longest_common_prefix(split_parts)
|
||||
prefix_str = os.sep.join(common_prefix)
|
||||
|
||||
for i, parts in enumerate(split_parts):
|
||||
filename = parts[-1]
|
||||
middle = os.sep.join(parts[len(common_prefix):-1]) if len(parts) > len(common_prefix) + 1 else ""
|
||||
row = group_df.iloc[i].copy()
|
||||
row["longestcfp"] = prefix_str
|
||||
row["middle"] = middle
|
||||
row["filename_only"] = filename
|
||||
new_rows.append(row)
|
||||
|
||||
return pd.DataFrame(new_rows).drop(columns=["group_key"])
|
||||
|
||||
def mask_from_csv(df, csv_path, filepath_col):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user