Download tools/data_processing/quality_control/clean_excel.py from Kaphathy/Dataset: direct link, hf CLI and curl.
- Browser
- Download file 2.13 kB
-
https://huggingface.co/datasets/Kaphathy/Dataset/resolve/main/tools/data_processing/quality_control/clean_excel.py
- Command line
-
hf download hf://datasets/Kaphathy/Dataset/tools/data_processing/quality_control/clean_excel.py
-
curl -L -o clean_excel.py https://huggingface.co/datasets/Kaphathy/Dataset/resolve/main/tools/data_processing/quality_control/clean_excel.py
2.13 kB
| import os | |
| import pandas as pd | |
| def main(): | |
| # --- 1. 路径配置 --- | |
| original_excel_path = "/data/team/huchengwei/fundus/fundus_csv/paired_fundus_image_dramdrvo.xlsx" | |
| missing_log_path = "/data/team/huchengwei/fundus/fundus_csv/missing_images_dramdrvo.txt" # 上一步脚本自动生成的缺失名单 | |
| # 新的干净表格保存路径 | |
| new_excel_path = "/data/team/huchengwei/fundus/fundus_csv/dramdrvo_cleaned.xlsx" | |
| # --- 2. 加载缺失黑名单 --- | |
| if not os.path.exists(missing_log_path): | |
| raise FileNotFoundError(f"找不到 {missing_log_path},请确认上一步脚本是否正确生成了该文件。") | |
| with open(missing_log_path, "r", encoding="utf-8") as f: | |
| # 去除换行符并存入哈希集合 (Set) 以获得 O(1) 的查询速度 | |
| missing_basenames = set(line.strip() for line in f if line.strip()) | |
| print(f"[*] 成功加载缺失名单,共计 {len(missing_basenames)} 个目标。") | |
| # --- 3. 读取原始 DataFrame --- | |
| print(f"[*] 正在读取原始 Excel 表格...") | |
| df = pd.read_excel(original_excel_path) | |
| original_len = len(df) | |
| # --- 4. 核心清洗逻辑 --- | |
| # 定义过滤条件:提取 image_name 的无后缀基础名,判断其是否在黑名单中 | |
| def is_valid_row(image_name): | |
| basename = os.path.splitext(str(image_name))[0] | |
| return basename not in missing_basenames | |
| # 应用掩码 (Boolean Mask) 过滤 | |
| mask = df['image_name'].apply(is_valid_row) | |
| df_cleaned = df[mask] | |
| cleaned_len = len(df_cleaned) | |
| removed_count = original_len - cleaned_len | |
| # --- 5. 校验与保存 --- | |
| print(f"[*] 清洗完成!") | |
| print(f" -> 原始数据行数: {original_len}") | |
| print(f" -> 清洗后数据行数: {cleaned_len} (正好对应你成功复制的 5505 张图)") | |
| print(f" -> 实际排除行数: {removed_count}") | |
| # 保存新的 Excel | |
| df_cleaned.to_excel(new_excel_path, index=False) | |
| print(f"[*] 新的干净表格已生成并保存至:\n {new_excel_path}") | |
| if __name__ == "__main__": | |
| main() |