Download scripts/build_notebook.py from efe-T/nasa-same-model-c: direct link, hf CLI and curl.
- Browser
- Download file 7.14 kB
-
https://huggingface.co/efe-T/nasa-same-model-c/resolve/main/scripts/build_notebook.py
- Command line
-
hf download hf://efe-T/nasa-same-model-c/scripts/build_notebook.py
-
curl -L -o build_notebook.py https://huggingface.co/efe-T/nasa-same-model-c/resolve/main/scripts/build_notebook.py
7.14 kB
| """Build a credential-free notebook with its importable implementation embedded.""" | |
| import base64 | |
| import io | |
| from pathlib import Path | |
| import zipfile | |
| import nbformat as nbf | |
| root = Path(__file__).resolve().parents[1] | |
| buffer = io.BytesIO() | |
| with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as bundle: | |
| for folder in ["model_c", "tests"]: | |
| for path in sorted((root / folder).glob("*.py")): | |
| bundle.write(path, str(path.relative_to(root))) | |
| bundle.write(root / "requirements.txt", "requirements.txt") | |
| encoded = base64.b64encode(buffer.getvalue()).decode() | |
| notebook = nbf.v4.new_notebook() | |
| notebook.metadata["kernelspec"] = {"display_name": "Python 3", "language": "python", "name": "python3"} | |
| notebook.cells = [ | |
| nbf.v4.new_markdown_cell("# Model C — SAME / SAME-R ISS detector threshold response\nRun all on a standard CPU Colab runtime. No paid API, Hub account or dataset credentials are required. All source modules and tests are embedded below; NASA files are downloaded and checked against pinned SHA256 values. The target is a **derived recorded-scatter ≥2 V proxy**, not reported operational alarm ground truth. Material plus controlled airflow, resistance ratio, heating, aging and mixing durations are the inputs. Models never receive measured outcomes.\n\nSequence: acquisition → integrity and preprocessing → EDA → grouped nested baseline/model selection → training/evaluation → checks/export → standalone CPU inference → dashboard table → full tests. The primary audit is in the accompanying research report; scientific sources: [PSI SAME](https://psi.nasa.gov/physci/repo/data/investigations/PSI-102), [PSI SAME-R](https://psi.nasa.gov/physci/repo/data/investigations/PSI-101), [Meyer2018 §3.2 Figure9](https://pmc.ncbi.nlm.nih.gov/articles/PMC7918997/)."), | |
| nbf.v4.new_code_cell("# @title Setup pinned environment and importable project\nimport sys, subprocess, pathlib, base64, io, zipfile, os\nproject = pathlib.Path('/content/model_c_project') if pathlib.Path('/content').exists() else pathlib.Path.cwd()\nproject.mkdir(parents=True, exist_ok=True)\nos.chdir(project)\nsource_zip = base64.b64decode(" + repr(encoded) + ")\nwith zipfile.ZipFile(io.BytesIO(source_zip)) as z:\n z.extractall(project)\nsubprocess.run([sys.executable, '-m', 'pip', 'install', '-q', '-r', 'requirements.txt'], check=True)\nsys.path.insert(0, str(project))\nos.environ['MPLCONFIGDIR'] = '/tmp/model-c-matplotlib'\nprint('Project:', project)"), | |
| nbf.v4.new_code_cell("# @title Acquire both investigations and verify original checksums\nfrom model_c.data import acquire, preprocess\npath = acquire()\nobservations = preprocess(path)\nimport pandas as pd, json\nprint(path)\ndisplay(pd.read_json('data/raw/source_manifest.json')[['study', 'path', 'sha256']])\nprint('No missing signal or detection-limit value is encoded as a new zero.')"), | |
| nbf.v4.new_code_cell("# @title EDA, independent units, eligibility and labels\nprint(observations.groupby('campaign').agg(rows=('test_id','size'), groups=('group_id','nunique'), primary=('training_eligible','sum')))\nprint(observations.groupby(['campaign','material','training_eligible']).size())\nprint(observations.loc[~observations.training_eligible, 'exclusion_reasons'].value_counts())\nprint('Derived label:', observations.label_rule.iloc[0])\nprint('AW saturation per observation is unresolved; finite high voltages are used only as threshold evidence.')\ndisplay(observations[['material','campaign','airflow_cm_s','resistance_ratio','heating_s','aging_s','mixing_s','iss_scatter_v','alarm_level_reached']].head())"), | |
| nbf.v4.new_code_cell("# @title Nested grouped baseline, candidate selection, final training and export\nfrom model_c.train import run\nmetrics = run()\nprint(json.dumps(metrics['headline_selection_unbiased_nested_cv'], indent=2))\nprint('Pooled out-of-fold:', metrics['pooled_oof'])\nprint('Per-material:', json.dumps(metrics['per_material'], indent=2))\nprint('Cross-campaign:', json.dumps(metrics['cross_campaign'], indent=2))"), | |
| nbf.v4.new_code_cell("# @title Validation, failure cases, external formula checks\nfrom IPython.display import display, Image, Markdown\ndisplay(Image(filename='reports/validation.png'))\ndisplay(pd.read_csv('reports/failure_cases.csv')[['test_id','material','campaign','iss_scatter_v','reference','oof_predicted']])\ndisplay(pd.read_csv('reports/external_checks.csv'))\ndisplay(Markdown(pathlib.Path('reports/evaluation.md').read_text()))"), | |
| nbf.v4.new_code_cell("# @title Notebook-free inference from the exported standalone module\nimport importlib.util\nspec = importlib.util.spec_from_file_location('standalone_inference', 'artifacts/model_c/inference.py')\nstandalone = importlib.util.module_from_spec(spec)\nspec.loader.exec_module(standalone)\nrequest = json.loads(pathlib.Path('reports/sample_inference.json').read_text())['request']\nprint(json.dumps(standalone.predict_model_c(request), indent=2))\nprint('Accepted OOD example:', pathlib.Path('reports/ood_example.json').read_text())"), | |
| nbf.v4.new_code_cell("# @title Precomputed dashboard and complete automated tests\ndashboard = pd.read_csv('data/processed/dashboard_table.csv')\nprint('Dashboard rows:', len(dashboard))\ndisplay(dashboard[['test_id','material','iss_scatter_v','alarm_level_reached','predicted_alarm_level_reached','oof_predicted','ood','prediction_scope']].head())\nresult = subprocess.run([sys.executable, '-m', 'pytest', '-q'], text=True, capture_output=True)\npathlib.Path('logs').mkdir(exist_ok=True)\npathlib.Path('logs/pytest-colab.txt').write_text(result.stdout + result.stderr)\nprint(result.stdout, result.stderr)\nresult.check_returncode()"), | |
| nbf.v4.new_code_cell("# @title Package verified outputs\nimport zipfile\nwith zipfile.ZipFile('/content/model_c_results.zip' if pathlib.Path('/content').exists() else 'model_c_results.zip','w',zipfile.ZIP_DEFLATED) as z:\n for directory in ['artifacts','data/processed','reports','logs']:\n for p in pathlib.Path(directory).rglob('*'):\n if p.is_file(): z.write(p, str(p))\nprint('Exported model_c_results.zip. Download this plus the notebook; inference needs only artifacts/model_c and its pinned dependencies.')"), | |
| nbf.v4.new_markdown_cell("## Interpretation and limits\nBalanced accuracy above is nested out-of-fold performance of model selection; final full-data candidate comparison is not an independent estimate. Inputs exclude detector/moment measurements. Reused samples and aged readings share validation groups. Secondary aging rows remain descriptive and have no model prediction. Both campaigns use source-reduced ISS channel AW; STS and MPASS are not pooled. Saturation states and electronic detection limits are not reconstructed per row. No uncertainty interval, probability calibration, new-material generalization, real alarm timing or safety guarantee is claimed. The final forest is a small CPU classifier; GPU use would not help this84-row task.") | |
| ] | |
| (root / "notebooks").mkdir(exist_ok=True) | |
| nbf.write(notebook, root / "notebooks/model_c.ipynb") | |
| print("Built notebooks/model_c.ipynb") | |