Created
May 31, 2025 18:39
-
-
Save thierrymoudiki/2cba38ca1a6155a3ff4ecef493d86648 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # first, install jq: on macOS, brew install jq | |
| wget https://raw.githubusercontent.com/Techtonique/datasets/refs/heads/main/tabular/survival/gbsg2_2.csv | |
| # === CONFIG === | |
| JSON_FILE="survival_curves.json" | |
| CSV_FILE="survival_curves.csv" | |
| PLOT_FILE="survival_plot.png" | |
| # Prompt for auth token | |
| read -p "Enter your Bearer token: " AUTH_TOKEN | |
| # === 1. Submit requests for multiple patients === | |
| python3 <<END | |
| import subprocess | |
| import sys | |
| try: | |
| # Use --user to install in the user's site-packages if direct install is restricted (PEP 668) | |
| subprocess.run([sys.executable, '-m', 'pip', 'install', 'requests'], check=True) | |
| except subprocess.CalledProcessError: | |
| print("Failed to install requests package") | |
| sys.exit(1) | |
| import requests | |
| import json | |
| headers = {'Authorization': 'Bearer $AUTH_TOKEN'} | |
| responses = [] | |
| for patient_id in range(4): | |
| params = {'method': 'RidgeCV', 'patient_id': str(patient_id)} | |
| files = {'file': ('gbsg2_2.csv', open('gbsg2_2.csv', 'rb'), 'text/csv')} | |
| print(f"Sending request for patient {patient_id}") | |
| response = requests.post('https://www.techtonique.net/survivalcurve', params=params, headers=headers, files=files) | |
| print(response) | |
| responses.append(response.json()) | |
| with open('$JSON_FILE', 'w') as f: | |
| json.dump(responses, f) | |
| END | |
| # === 2. Convert JSON to CSV === | |
| jq -r '.[] | .times as $t | .survival_probabilities as $s | range(0; $t|length) | "\(.),\($t[.]),\($s[.])"' "$JSON_FILE" > "$CSV_FILE" | |
| echo "patient_id,time,survival" > temp.csv && tail -n +2 "$CSV_FILE" >> temp.csv && mv temp.csv "$CSV_FILE" | |
| # === 3. Plot with rush === | |
| rush plot \ | |
| --x time \ | |
| --y survival \ | |
| --group patient_id \ | |
| --color patient_id \ | |
| --title "Patient Survival Curves" \ | |
| "$CSV_FILE" > "$PLOT_FILE" | |
| echo "✅ Final plot saved to: $PLOT_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment