Which patient is going to survive longer? Another guide to using techtonique dot net’s API (with R + Python + the command line) for survival analysis
[This article was first published on T. Moudiki's Webpage - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In today’s post, we’ll see how to use rush and the probabilistic survival analysis API provided by techtonique.net (along with R and Python) to plot survival curves . Note that the web app also contains a page for plotting these curves, in 1 click. You can also read this post for more Python examples.
First, you’d need to install rush. Here is how I did it:
cd /Users/t/Documents/Python_Packages git clone https://github.com/jeroenjanssens/rush.git export PATH="/Users/t/Documents/Python_Packages/rush/exec:$PATH" source ~/.zshrc # or source ~/.bashrc rush --help # check if rush is installed
Now, download and save the following script in your current directory (note that there’s nothing malicious in it). Replace AUTH_TOKEN below by a token that can be found at techtonique.net/token:
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" |
Then, at the command line, run:
./2025-05-31-survival.sh
The result plot can be found in your current directory as a PNG file.
To leave a comment for the author, please follow the link and comment on their blog: T. Moudiki's Webpage - R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.