Datasets:
Tasks:
Tabular Regression
Sub-tasks:
tabular-single-column-regression
Languages:
English
Size:
100K<n<1M
ArXiv:
License:
Upload 2 files
Browse files
data/util/prepare_mosaec_partial_structure.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import argparse
|
| 5 |
+
import shutil
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
DATASET_DIRS = ("charged", "neutral")
|
| 11 |
+
|
| 12 |
+
COLUMN_MAP = {
|
| 13 |
+
"density_g/cm3": "density_g_cm3",
|
| 14 |
+
"lcd_ang_H2": "lcd_ang_H2",
|
| 15 |
+
"pld_ang_H2": "pld_ang_H2",
|
| 16 |
+
"asa_m2/cm3_H2": "asa_m2_cm3_H2",
|
| 17 |
+
"asa_m2/g_H2": "asa_m2_g_H2",
|
| 18 |
+
"void_fraction_H2": "void_fraction_H2",
|
| 19 |
+
"av_ang3_H2": "av_ang3_H2",
|
| 20 |
+
"av_cm3/g_H2": "av_cm3_g_H2",
|
| 21 |
+
"void_fraction_probe-occupiable_H2": "void_fraction_probe-occupiable_H2",
|
| 22 |
+
"av_probe-occupiable_ang3_H2": "av_probe-occupiable_ang3_H2",
|
| 23 |
+
"av_probe-occupiable_cm3/g_H2": "av_probe-occupiable_cm3_g_H2",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
TARGET_COLUMNS = [
|
| 27 |
+
"cif",
|
| 28 |
+
"density_g_cm3",
|
| 29 |
+
"lcd_ang_H2",
|
| 30 |
+
"pld_ang_H2",
|
| 31 |
+
"asa_m2_cm3_H2",
|
| 32 |
+
"asa_m2_g_H2",
|
| 33 |
+
"void_fraction_H2",
|
| 34 |
+
"av_ang3_H2",
|
| 35 |
+
"av_cm3_g_H2",
|
| 36 |
+
"void_fraction_probe-occupiable_H2",
|
| 37 |
+
"av_probe-occupiable_ang3_H2",
|
| 38 |
+
"av_probe-occupiable_cm3_g_H2",
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def normalize_cif(series: pd.Series) -> pd.Series:
|
| 43 |
+
cif = series.astype(str).str.strip()
|
| 44 |
+
return cif.str.replace(r"\\.cif$", "", regex=True)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def load_properties_table(csv_path: Path) -> tuple[pd.DataFrame, set[str]]:
|
| 48 |
+
required = ["cif", *COLUMN_MAP.keys()]
|
| 49 |
+
df = pd.read_csv(csv_path)
|
| 50 |
+
|
| 51 |
+
missing = [c for c in required if c not in df.columns]
|
| 52 |
+
if missing:
|
| 53 |
+
raise ValueError(f"Missing required columns in CSV: {', '.join(missing)}")
|
| 54 |
+
|
| 55 |
+
df["cif"] = normalize_cif(df["cif"])
|
| 56 |
+
|
| 57 |
+
duplicated = df["cif"].duplicated(keep=False)
|
| 58 |
+
if duplicated.any():
|
| 59 |
+
dup_count = int(duplicated.sum())
|
| 60 |
+
print(
|
| 61 |
+
f"[WARN] Found {dup_count} duplicated cif rows in CSV. "
|
| 62 |
+
"Keeping the first occurrence for each cif."
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
props = df[["cif", *COLUMN_MAP.keys()]].copy()
|
| 66 |
+
props = props.rename(columns=COLUMN_MAP)
|
| 67 |
+
props = props.drop_duplicates(subset=["cif"], keep="first")
|
| 68 |
+
props = props[TARGET_COLUMNS]
|
| 69 |
+
|
| 70 |
+
return props, set(props["cif"])
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def process_subset(subset_dir: Path, valid_cif: set[str], props: pd.DataFrame) -> dict[str, int]:
|
| 74 |
+
raw_dir = subset_dir / "raw"
|
| 75 |
+
raw_dir.mkdir(exist_ok=True)
|
| 76 |
+
|
| 77 |
+
moved = 0
|
| 78 |
+
deleted = 0
|
| 79 |
+
|
| 80 |
+
# Move/delete CIFs from subset root
|
| 81 |
+
for cif_path in subset_dir.glob("*.cif"):
|
| 82 |
+
if cif_path.stem in valid_cif:
|
| 83 |
+
destination = raw_dir / cif_path.name
|
| 84 |
+
if destination.exists():
|
| 85 |
+
cif_path.unlink()
|
| 86 |
+
else:
|
| 87 |
+
shutil.move(str(cif_path), str(destination))
|
| 88 |
+
moved += 1
|
| 89 |
+
else:
|
| 90 |
+
cif_path.unlink()
|
| 91 |
+
deleted += 1
|
| 92 |
+
|
| 93 |
+
# Clean invalid CIFs inside raw/
|
| 94 |
+
for cif_path in raw_dir.glob("*.cif"):
|
| 95 |
+
if cif_path.stem not in valid_cif:
|
| 96 |
+
cif_path.unlink()
|
| 97 |
+
deleted += 1
|
| 98 |
+
|
| 99 |
+
raw_cif = sorted(p.stem for p in raw_dir.glob("*.cif") if p.stem in valid_cif)
|
| 100 |
+
|
| 101 |
+
props_indexed = props.set_index("cif")
|
| 102 |
+
available = [c for c in raw_cif if c in props_indexed.index]
|
| 103 |
+
missing_in_csv = [c for c in raw_cif if c not in props_indexed.index]
|
| 104 |
+
|
| 105 |
+
if missing_in_csv:
|
| 106 |
+
print(
|
| 107 |
+
f"[WARN] {subset_dir.name}: {len(missing_in_csv)} files in raw/ "
|
| 108 |
+
"not found in CSV; skipped in id_prop.csv"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
id_prop = props_indexed.loc[available].reset_index()
|
| 112 |
+
id_prop_path = subset_dir / "id_prop.csv"
|
| 113 |
+
id_prop.to_csv(id_prop_path, index=False)
|
| 114 |
+
|
| 115 |
+
return {
|
| 116 |
+
"moved": moved,
|
| 117 |
+
"deleted": deleted,
|
| 118 |
+
"raw_count": len(raw_cif),
|
| 119 |
+
"id_prop_rows": len(id_prop),
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def parse_args() -> argparse.Namespace:
|
| 124 |
+
parser = argparse.ArgumentParser(
|
| 125 |
+
description=(
|
| 126 |
+
"Prepare MOSAEC-DB full/charged and full/neutral datasets: "
|
| 127 |
+
"move valid .cif files into raw/ and create id_prop.csv"
|
| 128 |
+
)
|
| 129 |
+
)
|
| 130 |
+
parser.add_argument(
|
| 131 |
+
"--csv",
|
| 132 |
+
default="mosaec-db.csv",
|
| 133 |
+
help="Path to source CSV (default: mosaec-db.csv)",
|
| 134 |
+
)
|
| 135 |
+
parser.add_argument(
|
| 136 |
+
"--root",
|
| 137 |
+
default=".",
|
| 138 |
+
help="Root directory containing charged and neutral folders (default: current directory)",
|
| 139 |
+
)
|
| 140 |
+
return parser.parse_args()
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def main() -> None:
|
| 144 |
+
args = parse_args()
|
| 145 |
+
root = Path(args.root).resolve()
|
| 146 |
+
csv_path = (root / args.csv).resolve() if not Path(args.csv).is_absolute() else Path(args.csv)
|
| 147 |
+
|
| 148 |
+
if not csv_path.exists():
|
| 149 |
+
raise FileNotFoundError(f"CSV file not found: {csv_path}")
|
| 150 |
+
|
| 151 |
+
props, valid_cif = load_properties_table(csv_path)
|
| 152 |
+
|
| 153 |
+
print(f"Loaded {len(valid_cif)} unique cif values from: {csv_path}")
|
| 154 |
+
print()
|
| 155 |
+
|
| 156 |
+
for subset_name in DATASET_DIRS:
|
| 157 |
+
subset_dir = root / subset_name
|
| 158 |
+
if not subset_dir.exists() or not subset_dir.is_dir():
|
| 159 |
+
print(f"[WARN] Skip {subset_name}: folder not found at {subset_dir}")
|
| 160 |
+
continue
|
| 161 |
+
|
| 162 |
+
stats = process_subset(subset_dir, valid_cif, props)
|
| 163 |
+
print(
|
| 164 |
+
f"{subset_name}: moved={stats['moved']}, deleted={stats['deleted']}, "
|
| 165 |
+
f"raw_files={stats['raw_count']}, id_prop_rows={stats['id_prop_rows']}"
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
if __name__ == "__main__":
|
| 170 |
+
main()
|