Datasets:
Tasks:
Tabular Regression
Sub-tasks:
tabular-single-column-regression
Languages:
English
Size:
100K<n<1M
ArXiv:
License:
| #!/usr/bin/env python3 | |
| from __future__ import annotations | |
| import argparse | |
| import shutil | |
| from pathlib import Path | |
| import pandas as pd | |
| DATASET_DIRS = ("charged", "neutral") | |
| COLUMN_MAP = { | |
| "density_g/cm3": "density_g_cm3", | |
| "lcd_ang_H2": "lcd_ang_H2", | |
| "pld_ang_H2": "pld_ang_H2", | |
| "asa_m2/cm3_H2": "asa_m2_cm3_H2", | |
| "asa_m2/g_H2": "asa_m2_g_H2", | |
| "void_fraction_H2": "void_fraction_H2", | |
| "av_ang3_H2": "av_ang3_H2", | |
| "av_cm3/g_H2": "av_cm3_g_H2", | |
| "void_fraction_probe-occupiable_H2": "void_fraction_probe-occupiable_H2", | |
| "av_probe-occupiable_ang3_H2": "av_probe-occupiable_ang3_H2", | |
| "av_probe-occupiable_cm3/g_H2": "av_probe-occupiable_cm3_g_H2", | |
| } | |
| TARGET_COLUMNS = [ | |
| "cif", | |
| "density_g_cm3", | |
| "lcd_ang_H2", | |
| "pld_ang_H2", | |
| "asa_m2_cm3_H2", | |
| "asa_m2_g_H2", | |
| "void_fraction_H2", | |
| "av_ang3_H2", | |
| "av_cm3_g_H2", | |
| "void_fraction_probe-occupiable_H2", | |
| "av_probe-occupiable_ang3_H2", | |
| "av_probe-occupiable_cm3_g_H2", | |
| ] | |
| def normalize_cif(series: pd.Series) -> pd.Series: | |
| cif = series.astype(str).str.strip() | |
| return cif.str.replace(r"\\.cif$", "", regex=True) | |
| def load_properties_table(csv_path: Path) -> tuple[pd.DataFrame, set[str]]: | |
| required = ["cif", *COLUMN_MAP.keys()] | |
| df = pd.read_csv(csv_path) | |
| missing = [c for c in required if c not in df.columns] | |
| if missing: | |
| raise ValueError(f"Missing required columns in CSV: {', '.join(missing)}") | |
| df["cif"] = normalize_cif(df["cif"]) | |
| duplicated = df["cif"].duplicated(keep=False) | |
| if duplicated.any(): | |
| dup_count = int(duplicated.sum()) | |
| print( | |
| f"[WARN] Found {dup_count} duplicated cif rows in CSV. " | |
| "Keeping the first occurrence for each cif." | |
| ) | |
| props = df[["cif", *COLUMN_MAP.keys()]].copy() | |
| props = props.rename(columns=COLUMN_MAP) | |
| props = props.drop_duplicates(subset=["cif"], keep="first") | |
| props = props[TARGET_COLUMNS] | |
| return props, set(props["cif"]) | |
| def process_subset(subset_dir: Path, valid_cif: set[str], props: pd.DataFrame) -> dict[str, int]: | |
| raw_dir = subset_dir / "raw" | |
| raw_dir.mkdir(exist_ok=True) | |
| moved = 0 | |
| deleted = 0 | |
| # Move/delete CIFs from subset root | |
| for cif_path in subset_dir.glob("*.cif"): | |
| if cif_path.stem in valid_cif: | |
| destination = raw_dir / cif_path.name | |
| if destination.exists(): | |
| cif_path.unlink() | |
| else: | |
| shutil.move(str(cif_path), str(destination)) | |
| moved += 1 | |
| else: | |
| cif_path.unlink() | |
| deleted += 1 | |
| # Clean invalid CIFs inside raw/ | |
| for cif_path in raw_dir.glob("*.cif"): | |
| if cif_path.stem not in valid_cif: | |
| cif_path.unlink() | |
| deleted += 1 | |
| raw_cif = sorted(p.stem for p in raw_dir.glob("*.cif") if p.stem in valid_cif) | |
| props_indexed = props.set_index("cif") | |
| available = [c for c in raw_cif if c in props_indexed.index] | |
| missing_in_csv = [c for c in raw_cif if c not in props_indexed.index] | |
| if missing_in_csv: | |
| print( | |
| f"[WARN] {subset_dir.name}: {len(missing_in_csv)} files in raw/ " | |
| "not found in CSV; skipped in id_prop.csv" | |
| ) | |
| id_prop = props_indexed.loc[available].reset_index() | |
| id_prop_path = subset_dir / "id_prop.csv" | |
| id_prop.to_csv(id_prop_path, index=False) | |
| return { | |
| "moved": moved, | |
| "deleted": deleted, | |
| "raw_count": len(raw_cif), | |
| "id_prop_rows": len(id_prop), | |
| } | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser( | |
| description=( | |
| "Prepare MOSAEC-DB full/charged and full/neutral datasets: " | |
| "move valid .cif files into raw/ and create id_prop.csv" | |
| ) | |
| ) | |
| parser.add_argument( | |
| "--csv", | |
| default="mosaec-db.csv", | |
| help="Path to source CSV (default: mosaec-db.csv)", | |
| ) | |
| parser.add_argument( | |
| "--root", | |
| default=".", | |
| help="Root directory containing charged and neutral folders (default: current directory)", | |
| ) | |
| return parser.parse_args() | |
| def main() -> None: | |
| args = parse_args() | |
| root = Path(args.root).resolve() | |
| csv_path = (root / args.csv).resolve() if not Path(args.csv).is_absolute() else Path(args.csv) | |
| if not csv_path.exists(): | |
| raise FileNotFoundError(f"CSV file not found: {csv_path}") | |
| props, valid_cif = load_properties_table(csv_path) | |
| print(f"Loaded {len(valid_cif)} unique cif values from: {csv_path}") | |
| print() | |
| for subset_name in DATASET_DIRS: | |
| subset_dir = root / subset_name | |
| if not subset_dir.exists() or not subset_dir.is_dir(): | |
| print(f"[WARN] Skip {subset_name}: folder not found at {subset_dir}") | |
| continue | |
| stats = process_subset(subset_dir, valid_cif, props) | |
| print( | |
| f"{subset_name}: moved={stats['moved']}, deleted={stats['deleted']}, " | |
| f"raw_files={stats['raw_count']}, id_prop_rows={stats['id_prop_rows']}" | |
| ) | |
| if __name__ == "__main__": | |
| main() | |