File size: 1,706 Bytes
95d9557 | 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 54 | #!/usr/bin/env python3
"""Create the PhaseFlow missing-count CSV views next to a source phase table."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import pandas as pd
PHASE_COLUMNS = [f"group_{row}{column}" for row in range(1, 5) for column in range(1, 5)]
PACKAGE_ROOT = Path(__file__).resolve().parents[1]
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--input", type=Path, default=PACKAGE_ROOT / "data/raw/phase_diagram_original_scale.csv")
parser.add_argument("--output-dir", type=Path, default=PACKAGE_ROOT / "data/raw/by_missing")
return parser.parse_args()
def main() -> None:
args = parse_args()
frame = pd.read_csv(args.input)
required = {"AminoAcidSequence", *PHASE_COLUMNS}
missing = sorted(required.difference(frame.columns))
if missing:
raise ValueError(f"Missing required columns: {missing}")
args.output_dir.mkdir(parents=True, exist_ok=True)
missing_count = frame[PHASE_COLUMNS].isna().sum(axis=1)
counts: dict[str, int] = {}
for count in range(16):
subset = frame.loc[missing_count == count]
subset.to_csv(args.output_dir / f"missing_{count}.csv", index=False)
counts[str(count)] = int(len(subset))
report = {
"input": str(args.input),
"rows": int(len(frame)),
"phase_columns": PHASE_COLUMNS,
"missing_count_rows": counts,
}
(args.output_dir / "missing_split_report.json").write_text(
json.dumps(report, indent=2, sort_keys=True) + "\n"
)
print(json.dumps(report, sort_keys=True))
if __name__ == "__main__":
main()
|