diff --git a/.gitattributes b/.gitattributes index 48edf9969ff2af8c04445bf1e5e74f779606c169..57f1e7530704c75979ca042fe9cc3561ac4ee484 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4852,3 +4852,45 @@ documents/10.1056_nejmoa2302312/sap.pdf filter=lfs diff=lfs merge=lfs -text documents/10.1056_nejmoa2116596/sap.pdf filter=lfs diff=lfs merge=lfs -text documents/10.1016_s0140-6736(24)01498-3/sap.pdf filter=lfs diff=lfs merge=lfs -text documents/10.1056_nejmoa2210645/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1016_s0140-6736(17)33297-x/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1016_s0140-6736(17)33297-x/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1001_jama.2019.1660/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1001_jama.2019.0071/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1001_jama.2019.1660/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1016_s0140-6736(18)32321-3/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1016_s0140-6736(18)32321-3/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1001_jama.2019.0071/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1908655/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1908655/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1805104/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1816714/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1805104/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1816714/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1200_jco.2017.74.0720/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1200_jco.2017.74.0720/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1001_jama.2019.0557/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1001_jama.2019.0557/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1801601/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1801601/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1513248/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1513248/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1700456/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1700456/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1016_s0140-6736(19)31388-1/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1016_s0140-6736(19)31388-1/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1909953/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1001_jama.2019.12618/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1708131/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1909953/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1001_jama.2019.12618/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1200_jco.2017.75.6270/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1708131/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1716995/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1716995/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1200_jco.2017.75.6270/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1708454/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1016_s0140-6736(19)30772-x/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1016_s0140-6736(19)30772-x/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1708454/sap.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1056_nejmoa1903869/protocol.pdf filter=lfs diff=lfs merge=lfs -text +documents/10.1016_s0140-6736(19)31150-x/protocol.pdf filter=lfs diff=lfs merge=lfs -text diff --git a/SYNC_README.md b/SYNC_README.md new file mode 100644 index 0000000000000000000000000000000000000000..513bfee265d1812718014e9d929292deb7d0310b --- /dev/null +++ b/SYNC_README.md @@ -0,0 +1,89 @@ +# Sync Dataset + +`sync_dataset.py` pulls the latest rows from the master Google Sheet, downloads the protocol/SAP PDFs for any new rows, and pushes everything to the Hugging Face dataset at [`trialdesignbench/source`](https://huggingface.co/datasets/trialdesignbench/source). + +## What it does + +1. Downloads the Google Sheet as CSV, converts to Parquet → overwrites `data/tdr.parquet`. +2. Diffs against the previous `tdr.parquet` by the `#` column to find new rows. +3. For each new row, downloads: + - `Study Protocol Link` → `documents/<#>/protocol.pdf` + - `Protocol+SAP / SAP Link` → `documents/<#>/sap.pdf` + - Skips empty links. Skips files already on disk. Writes a `*.error.txt` on failure. +4. Uploads the updated `tdr.parquet` and any new PDFs to Hugging Face. + +## One-time setup + +```bash +# 1. Python deps +pip install -U "huggingface_hub[cli]" pandas pyarrow openpyxl +# pandas + pyarrow are required for the Parquet read/write. +# openpyxl is only needed if the source file is an uploaded .xlsx +# (htmlview URL) rather than a native Google Sheet. + +# 2. Authenticate to Hugging Face (write token from https://huggingface.co/settings/tokens) +hf auth login + +# 3. Make sure the Google Sheet is shared as "Anyone with the link: Viewer" +# Sheet URL: https://docs.google.com/spreadsheets/d/1Vb6U9Jzigtg5hLcn4R_5REW_G84cNVk6 +``` + +## Usage + +Run from the `source/` directory: + +```bash +cd /Users/tengjun2/Desktop/clinical/tdb/source + +# Preview new rows without changing anything +python3 sync_dataset.py --dry-run + +# Download new PDFs and update tdr.parquet locally, but don't upload +python3 sync_dataset.py --no-upload + +# Full sync: fetch sheet, download new PDFs, upload to Hugging Face +python3 sync_dataset.py +``` + +## Output layout + +``` +source/ +├── data/ +│ └── tdr.parquet # latest sheet content (snappy-compressed) +├── documents/ +│ └── / +│ ├── protocol.pdf +│ ├── sap.pdf +│ ├── protocol.error.txt # only present on download failure +│ └── sap.error.txt +└── sync_dataset.py +``` + +## Configuration + +Edit constants at the top of `sync_dataset.py` if any of these change: + +| Variable | Default | Meaning | +| ------------ | -------------------------------------------------- | ---------------------------------------- | +| `SHEET_ID` | `1Vb6U9Jzigtg5hLcn4R_5REW_G84cNVk6` | Google Sheet document ID | +| `SHEET_GID` | `0` | Sheet tab GID (find it in the sheet URL) | +| `HF_REPO` | `trialdesignbench/source` | Hugging Face dataset repo | +| `PARQUET_PATH` | `data/tdr.parquet` | Local Parquet path | +| `DOCS_DIR` | `documents/` | PDF output root | + +## Troubleshooting + +**"Google Sheet is not publicly shared"** — Open the sheet → Share → General access → "Anyone with the link" → Viewer. + +**A PDF row writes `*.error.txt`** — The source server rejected the request (often `HTTP 403` on paywalled publisher links like NEJM). The row stays in `tdr.parquet`; only the PDF is missing. Re-running won't retry automatically — delete the `.error.txt` and the partial file, then re-run, if you want to retry. + +**Upload is slow** — Throughput is bounded by your upstream bandwidth. For small incremental syncs (a handful of new files) `hf upload` is used per file. For > 200 new files the script falls back to `hf upload-large-folder` which parallelizes and is resumable. + +**Diff missed a row whose `#` was reused** — The script keys on the `#` column. If a row's content changed without `#` changing, it won't be detected as new. Inspect manually in that case. + +## Notes + +- The script only **adds** new rows. Rows deleted from the sheet are kept in the local Parquet file and on Hugging Face — clean those up by hand if needed. +- `tdr.parquet` is always overwritten with the latest sheet content, so edits in the sheet (typo fixes, etc.) propagate. +- PDFs are never re-downloaded if already present. Force a re-download by deleting the file. diff --git a/__pycache__/sync_dataset.cpython-313.pyc b/__pycache__/sync_dataset.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5784e93639f0d6baaeb73ec14f2dd85a42c2e074 Binary files /dev/null and b/__pycache__/sync_dataset.cpython-313.pyc differ diff --git a/delete_old_folders.py b/delete_old_folders.py new file mode 100644 index 0000000000000000000000000000000000000000..166a90cd2afc15d098d0f3a05a560a2b40dff567 --- /dev/null +++ b/delete_old_folders.py @@ -0,0 +1,42 @@ +"""Delete the old numeric-named documents/<#>/ folders from the HF dataset.""" + +from __future__ import annotations + +from huggingface_hub import HfApi + +REPO = "trialdesignbench/source" + + +def main() -> None: + api = HfApi() + files = api.list_repo_files(REPO, repo_type="dataset") + + old = [ + f for f in files + if f.startswith("documents/") + and len(f.split("/")) >= 2 + and f.split("/")[1].isdigit() + ] + print(f"Found {len(old)} files under numeric folders. First 5:") + for p in old[:5]: + print(" ", p) + + if not old: + return + + confirm = input(f"Delete all {len(old)} files? [y/N] ").strip().lower() + if confirm != "y": + print("Aborted.") + return + + api.delete_files( + delete_patterns=old, + repo_id=REPO, + repo_type="dataset", + commit_message="Remove old numeric-named document folders", + ) + print("Deleted.") + + +if __name__ == "__main__": + main() diff --git a/documents/10.1001_jama.2018.20579/sap.pdf b/documents/10.1001_jama.2018.20579/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..807c5b6687c15edbd1e61ddb08fb6a2e0eee66c4 Binary files /dev/null and b/documents/10.1001_jama.2018.20579/sap.pdf differ diff --git a/documents/10.1001_jama.2019.0071/protocol.pdf b/documents/10.1001_jama.2019.0071/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..a7f716235732ccbc0e7a6675b873db341c905791 --- /dev/null +++ b/documents/10.1001_jama.2019.0071/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5dfe5a197820387b23f1d4570a4a1519321918afbc21f3fa5bd8a27a9cb04cc0 +size 2754354 diff --git a/documents/10.1001_jama.2019.0071/sap.pdf b/documents/10.1001_jama.2019.0071/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..594cf9a523a6774ba2d596fe8795a2e905de735d --- /dev/null +++ b/documents/10.1001_jama.2019.0071/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6fd76358b47d339576b018881d70bada62c38e20b8a01c21e5686453ffadf4a4 +size 281032 diff --git a/documents/10.1001_jama.2019.0557/protocol.pdf b/documents/10.1001_jama.2019.0557/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..2ca77dcf9fa664aa5516db77ccd91562a66f1018 --- /dev/null +++ b/documents/10.1001_jama.2019.0557/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:08f104e10f8ca44d048262aab02bf11f63f0b59cae9d55f5fae8ff670ce79f27 +size 1209003 diff --git a/documents/10.1001_jama.2019.0557/sap.pdf b/documents/10.1001_jama.2019.0557/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..2ca77dcf9fa664aa5516db77ccd91562a66f1018 --- /dev/null +++ b/documents/10.1001_jama.2019.0557/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:08f104e10f8ca44d048262aab02bf11f63f0b59cae9d55f5fae8ff670ce79f27 +size 1209003 diff --git a/documents/10.1001_jama.2019.12618/protocol.pdf b/documents/10.1001_jama.2019.12618/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3cb9f9975b656a00144711682f1a5a70d8595373 --- /dev/null +++ b/documents/10.1001_jama.2019.12618/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b7e0d9fff5fb3b8681a408213bc82810107ea8689d4e2a7f0c5ddd060fc98193 +size 9456599 diff --git a/documents/10.1001_jama.2019.12618/sap.pdf b/documents/10.1001_jama.2019.12618/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3cb9f9975b656a00144711682f1a5a70d8595373 --- /dev/null +++ b/documents/10.1001_jama.2019.12618/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b7e0d9fff5fb3b8681a408213bc82810107ea8689d4e2a7f0c5ddd060fc98193 +size 9456599 diff --git a/documents/10.1001_jama.2019.1660/protocol.pdf b/documents/10.1001_jama.2019.1660/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..7836c4ea1fbcda1a626cd322df1273e1ff5e2d81 --- /dev/null +++ b/documents/10.1001_jama.2019.1660/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:96abe28b22bfb56939c150fc143d024d306d81fca255f54550ce8cb194e2f5cc +size 1215290 diff --git a/documents/10.1001_jama.2019.1660/sap.pdf b/documents/10.1001_jama.2019.1660/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..0fe788edae59c11284363705c7e7177cecf35452 --- /dev/null +++ b/documents/10.1001_jama.2019.1660/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1f9ba880010ccd6a2581715b5824c4759f437337d36372bf800b4c307245b549 +size 408047 diff --git a/documents/10.1016_s0140-6736(17)33297-x/protocol.pdf b/documents/10.1016_s0140-6736(17)33297-x/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..c0654b4513159c995bc2d46eab0d989e92b07191 --- /dev/null +++ b/documents/10.1016_s0140-6736(17)33297-x/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9f64a224e34e8b1ccab7161b025b8944ff05f0e06e15c4ba70d14b1713daccb3 +size 2491616 diff --git a/documents/10.1016_s0140-6736(17)33297-x/sap.pdf b/documents/10.1016_s0140-6736(17)33297-x/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..c0654b4513159c995bc2d46eab0d989e92b07191 --- /dev/null +++ b/documents/10.1016_s0140-6736(17)33297-x/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9f64a224e34e8b1ccab7161b025b8944ff05f0e06e15c4ba70d14b1713daccb3 +size 2491616 diff --git a/documents/10.1016_s0140-6736(18)32321-3/protocol.pdf b/documents/10.1016_s0140-6736(18)32321-3/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..93ea1b219bafcfd83e98edcb1cb48ee65101e600 --- /dev/null +++ b/documents/10.1016_s0140-6736(18)32321-3/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6f0c1405283c72390c92ca645b3f62502b18ba7b7aebd59ea6bf3f25e5e51395 +size 1079157 diff --git a/documents/10.1016_s0140-6736(18)32321-3/sap.pdf b/documents/10.1016_s0140-6736(18)32321-3/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..93ea1b219bafcfd83e98edcb1cb48ee65101e600 --- /dev/null +++ b/documents/10.1016_s0140-6736(18)32321-3/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6f0c1405283c72390c92ca645b3f62502b18ba7b7aebd59ea6bf3f25e5e51395 +size 1079157 diff --git a/documents/10.1016_s0140-6736(19)30772-x/protocol.pdf b/documents/10.1016_s0140-6736(19)30772-x/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..ee2f554ef0719aecfc07acac219223b58d74a9b1 --- /dev/null +++ b/documents/10.1016_s0140-6736(19)30772-x/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0098f38b6a5831341ce5f5ef1a8a2b27bebed3c5fee846ca099bb6ce690457ce +size 2004321 diff --git a/documents/10.1016_s0140-6736(19)30772-x/sap.pdf b/documents/10.1016_s0140-6736(19)30772-x/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3b304082a7e8adc788eab9004fa1babe023a393e --- /dev/null +++ b/documents/10.1016_s0140-6736(19)30772-x/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6f9d0f965bc28eafc1229d293318c6ab1b57112f55411ad092bbeb147b082a41 +size 795974 diff --git a/documents/10.1016_s0140-6736(19)31150-x/protocol.pdf b/documents/10.1016_s0140-6736(19)31150-x/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..e61663f5e4b1f95568026cd4dccfcf3d8ab2d8bf --- /dev/null +++ b/documents/10.1016_s0140-6736(19)31150-x/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2c379993f99707de410d645765a4282e966085eef5379847d029c6c592884c40 +size 918504 diff --git a/documents/10.1016_s0140-6736(19)31388-1/protocol.pdf b/documents/10.1016_s0140-6736(19)31388-1/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..94c381f3eeb6347df8c04e09c7162d808dd76d38 --- /dev/null +++ b/documents/10.1016_s0140-6736(19)31388-1/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:247060db1ac1a71eb649698297fde5ecf50dd927249822c001dafda788b95545 +size 1446328 diff --git a/documents/10.1016_s0140-6736(19)31388-1/sap.pdf b/documents/10.1016_s0140-6736(19)31388-1/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..d9fd5850a6957dd1bdfaf0a42d64ad8560cba452 --- /dev/null +++ b/documents/10.1016_s0140-6736(19)31388-1/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e0444958cf55da7f5d82d21ffc0809f728a7b77707d32c9a1257ac75e8c2f676 +size 1054189 diff --git a/documents/10.1016_s0140-6736(19)31793-3/sap.pdf b/documents/10.1016_s0140-6736(19)31793-3/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..0d1f888ff78fa00072677a06494f2bfad90b17a7 Binary files /dev/null and b/documents/10.1016_s0140-6736(19)31793-3/sap.pdf differ diff --git a/documents/10.1056_nejmoa1513248/protocol.pdf b/documents/10.1056_nejmoa1513248/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..d3794a14b36fa2e48829cfbe00ba10f99d8cda5f --- /dev/null +++ b/documents/10.1056_nejmoa1513248/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e8d22817a7019ded0b8998c9d8578e444ec91af75355592108b5a6b72f10cbc3 +size 872936 diff --git a/documents/10.1056_nejmoa1513248/sap.pdf b/documents/10.1056_nejmoa1513248/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..053cebd56aef904076ae4897551f5b36ac278c31 --- /dev/null +++ b/documents/10.1056_nejmoa1513248/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6a646ff761268b9cc00b6762529f91d4ea93c7b39af5e4f2b152b46f52a2d47b +size 672972 diff --git a/documents/10.1056_nejmoa1700456/protocol.pdf b/documents/10.1056_nejmoa1700456/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..4843753494b0b9bcf712f36594f672a98c9e2601 --- /dev/null +++ b/documents/10.1056_nejmoa1700456/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:37a4853dffc6a551580d6c14b40179c8ee8bff8fbbcd2f2280ebd9c9a253941b +size 13460546 diff --git a/documents/10.1056_nejmoa1700456/sap.pdf b/documents/10.1056_nejmoa1700456/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..4843753494b0b9bcf712f36594f672a98c9e2601 --- /dev/null +++ b/documents/10.1056_nejmoa1700456/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:37a4853dffc6a551580d6c14b40179c8ee8bff8fbbcd2f2280ebd9c9a253941b +size 13460546 diff --git a/documents/10.1056_nejmoa1708131/protocol.pdf b/documents/10.1056_nejmoa1708131/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..17effd444a8145040660474e1c1e315f91d9513a --- /dev/null +++ b/documents/10.1056_nejmoa1708131/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:764d9df5bee793e75b0251679b8585674b4913dd21ac4042210a14039537e194 +size 4316285 diff --git a/documents/10.1056_nejmoa1708131/sap.pdf b/documents/10.1056_nejmoa1708131/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..c200fb87aa988ce6845462755d6622e9627fe439 --- /dev/null +++ b/documents/10.1056_nejmoa1708131/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0aecd4b62ebda8a34ea4d8aebf0b3f689336f4205d02351c1a3fa892d5c8571d +size 260398 diff --git a/documents/10.1056_nejmoa1708454/protocol.pdf b/documents/10.1056_nejmoa1708454/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..c92659eae57485117db762b7b6308258b5d39cb6 --- /dev/null +++ b/documents/10.1056_nejmoa1708454/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:94d6d58fa1594954193d172da6b1ffc948b83decef55edcd9cf7e99a0ae91181 +size 1202771 diff --git a/documents/10.1056_nejmoa1708454/sap.pdf b/documents/10.1056_nejmoa1708454/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3f6920e7a1e15a36944eccb5441acff3c0214249 --- /dev/null +++ b/documents/10.1056_nejmoa1708454/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:02d15b9ea9d1a4913484e553801be2ca633ff02031f0be63d946a6d54c0915a1 +size 4557136 diff --git a/documents/10.1056_nejmoa1716995/protocol.pdf b/documents/10.1056_nejmoa1716995/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..33df75623e7079f545973c47bd932d8362685b62 --- /dev/null +++ b/documents/10.1056_nejmoa1716995/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:486a7c300a9a3c8a8a4d027f1d8bff151722e715d762c406230b32dcbd7c5aa5 +size 1195199 diff --git a/documents/10.1056_nejmoa1716995/sap.pdf b/documents/10.1056_nejmoa1716995/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..cf67fa2e042be67b71c18be47f0e1c255ea2fa9b --- /dev/null +++ b/documents/10.1056_nejmoa1716995/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f94679ff396d4becff0380b0c9b6977cf633d4a0167244355d4d29009afbde84 +size 851897 diff --git a/documents/10.1056_nejmoa1801601/protocol.pdf b/documents/10.1056_nejmoa1801601/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..859963cb3fd1fb4d6619aa67a08ad11ebe8dd02b --- /dev/null +++ b/documents/10.1056_nejmoa1801601/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d270558bb30306f8d965f5826d3debf14d087261934ecc74516e39b973332880 +size 1080077 diff --git a/documents/10.1056_nejmoa1801601/sap.pdf b/documents/10.1056_nejmoa1801601/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3cd1ff0ae923ddc3c0d5ed6cc2db4b51d04368a4 --- /dev/null +++ b/documents/10.1056_nejmoa1801601/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6b3276071d3ebd13e65f2d04b0d6bb3e4e9539963a6437d86bcc3aac6353a57f +size 166155 diff --git a/documents/10.1056_nejmoa1805104/protocol.pdf b/documents/10.1056_nejmoa1805104/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..58fb9e73ccfe301f5df3cc98e5443ed6cd66f3e7 --- /dev/null +++ b/documents/10.1056_nejmoa1805104/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d9ba3470bc0bb95641699f1fe08359ec229d72d25999738bee206c49f1be4005 +size 6501069 diff --git a/documents/10.1056_nejmoa1805104/sap.pdf b/documents/10.1056_nejmoa1805104/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..58fb9e73ccfe301f5df3cc98e5443ed6cd66f3e7 --- /dev/null +++ b/documents/10.1056_nejmoa1805104/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d9ba3470bc0bb95641699f1fe08359ec229d72d25999738bee206c49f1be4005 +size 6501069 diff --git a/documents/10.1056_nejmoa1816714/protocol.pdf b/documents/10.1056_nejmoa1816714/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..5f455dd15f42124593f96e363b210b491fe38e07 --- /dev/null +++ b/documents/10.1056_nejmoa1816714/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:14496801a4397f97ced0c949623550da93a5d8cb9a17982b9a8791cecce64915 +size 2578319 diff --git a/documents/10.1056_nejmoa1816714/sap.pdf b/documents/10.1056_nejmoa1816714/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..5f455dd15f42124593f96e363b210b491fe38e07 --- /dev/null +++ b/documents/10.1056_nejmoa1816714/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:14496801a4397f97ced0c949623550da93a5d8cb9a17982b9a8791cecce64915 +size 2578319 diff --git a/documents/10.1056_nejmoa1903869/protocol.pdf b/documents/10.1056_nejmoa1903869/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..04e49cf33dc1045dba4a322ddf0002a37522d3a3 --- /dev/null +++ b/documents/10.1056_nejmoa1903869/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a765ac16832c3638653c44c89cf033d8a5a55b80855af0818378e9bc9e2212fd +size 2184520 diff --git a/documents/10.1056_nejmoa1908655/protocol.pdf b/documents/10.1056_nejmoa1908655/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..2ceae60b2853fb208c2ae5e35577d8c351c7a47a --- /dev/null +++ b/documents/10.1056_nejmoa1908655/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c645095faa926776d2f9f9f564dd348d44e348d4c3105d36a20276d33ea904b7 +size 1231466 diff --git a/documents/10.1056_nejmoa1908655/sap.pdf b/documents/10.1056_nejmoa1908655/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..9562b6b7ed6892f4d8d8f8a8855d5d1790c440aa --- /dev/null +++ b/documents/10.1056_nejmoa1908655/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5590f7a09d944d30e6b5f31b78fa67e8982fda1c92d9eaedbca87c025b2bdb0f +size 601155 diff --git a/documents/10.1056_nejmoa1909953/protocol.pdf b/documents/10.1056_nejmoa1909953/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..a03e2636b505c57828125e5e749cfd0ea6b1ace6 --- /dev/null +++ b/documents/10.1056_nejmoa1909953/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d4fe3f2632832fb7695159422d10aff7a4df6d841777fe34bd644abd4e787ae +size 2297612 diff --git a/documents/10.1056_nejmoa1909953/sap.pdf b/documents/10.1056_nejmoa1909953/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3c1dde1a0d4870056fe0a59d503576e4cd38da3f --- /dev/null +++ b/documents/10.1056_nejmoa1909953/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fd81a8db29ff12599057679d1c59345d81c5455387385b65a310a7a006203315 +size 562980 diff --git a/documents/10.1200_jco.2017.74.0720/protocol.pdf b/documents/10.1200_jco.2017.74.0720/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..0ccce87fa8154f6b1718583f5882edf36dfefbb5 --- /dev/null +++ b/documents/10.1200_jco.2017.74.0720/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:511f2be5a0dde243fa66512139c1029e0a6032db84c4fbc5cecbed7a99f887a6 +size 478930 diff --git a/documents/10.1200_jco.2017.74.0720/sap.pdf b/documents/10.1200_jco.2017.74.0720/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..31429bb13154401bb1bd3c762d0a623fc60963a1 --- /dev/null +++ b/documents/10.1200_jco.2017.74.0720/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:003097bc03e2266b9476c5c48d1064e0b24809fa7c8019855358a13a26c2795a +size 359491 diff --git a/documents/10.1200_jco.2017.75.6270/protocol.pdf b/documents/10.1200_jco.2017.75.6270/protocol.pdf new file mode 100644 index 0000000000000000000000000000000000000000..4b1d2ccfbfb933fba79b787287b9d0c0351cbf71 --- /dev/null +++ b/documents/10.1200_jco.2017.75.6270/protocol.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a312bf6befdd59bcaf8edbce725b215636d075fb52437c59286a2c1e4a3f2ffa +size 3378050 diff --git a/documents/10.1200_jco.2017.75.6270/sap.pdf b/documents/10.1200_jco.2017.75.6270/sap.pdf new file mode 100644 index 0000000000000000000000000000000000000000..4b1d2ccfbfb933fba79b787287b9d0c0351cbf71 --- /dev/null +++ b/documents/10.1200_jco.2017.75.6270/sap.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a312bf6befdd59bcaf8edbce725b215636d075fb52437c59286a2c1e4a3f2ffa +size 3378050 diff --git a/download_docs.py b/download_docs.py index b6f6d33f9ec2f19018185ba55ef8b8824e4bbd8e..6355debcad20f086fc3f96baaa685a33bdc851ec 100644 --- a/download_docs.py +++ b/download_docs.py @@ -12,6 +12,7 @@ import time import urllib.error import urllib.request from pathlib import Path +from urllib.parse import urlparse CSV_PATH = Path(__file__).parent / "file.csv" OUT_ROOT = Path(__file__).parent / "downloads" @@ -25,6 +26,12 @@ UA = ( ) +def paper_link_slug(link: str) -> str: + path = urlparse((link or "").strip()).path.strip("/") + parts = [p for p in path.split("/") if p] + return "_".join(parts[-2:]) if len(parts) >= 2 else "" + + def download(url: str, dest: Path) -> tuple[bool, str]: if dest.exists() and dest.stat().st_size > 0: return True, "exists" @@ -64,7 +71,12 @@ def main(start_row: int = 3) -> None: print(f"[{num}] skip: no links") continue - row_dir = OUT_ROOT / str(num) + slug = paper_link_slug(row.get("Paper Link") or "") + if not slug: + print(f"[{num}] skip: no usable Paper Link") + continue + + row_dir = OUT_ROOT / slug row_dir.mkdir(exist_ok=True) if protocol: diff --git a/replace_doc.py b/replace_doc.py new file mode 100644 index 0000000000000000000000000000000000000000..65f5218157056e3cc21f849d426444c76d99d67f --- /dev/null +++ b/replace_doc.py @@ -0,0 +1,15 @@ +from huggingface_hub import HfApi + +api = HfApi() +# api.delete_folder( +# "documents", +# repo_id="trialdesignbench/source", +# repo_type="dataset", +# ) +api.upload_large_folder( + folder_path="/users/teng77/source", # parent — preserves "documents/" prefix + repo_id="trialdesignbench/source", + repo_type="dataset", + allow_patterns=["documents/**"], + ignore_patterns=[".cache/**", "**/.cache/**"], +) diff --git a/sync_dataset.py b/sync_dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..3cb2a2ef945174c8d210dc881f3451c95ab6afaa --- /dev/null +++ b/sync_dataset.py @@ -0,0 +1,323 @@ +"""Sync TrialDesignBench dataset from Google Sheets to local + Hugging Face. + +Steps: +1. Download the latest sheet as CSV from Google Sheets. +2. Diff against the existing tdr.parquet by the "#" column to find new rows. +3. Download protocol and SAP PDFs for new rows (skip if no link). +4. Overwrite tdr.parquet and upload the changed files to Hugging Face. + +Usage: + python sync_dataset.py # full sync + python sync_dataset.py --no-upload # local only + python sync_dataset.py --dry-run # show what would happen +""" + +from __future__ import annotations + +import argparse +import csv +import subprocess +import sys +import time +import urllib.error +import urllib.request +from pathlib import Path +from urllib.parse import urlparse + +SHEET_ID = "1Vb6U9Jzigtg5hLcn4R_5REW_G84cNVk6" +SHEET_GID = "0" +SHEET_CSV_URL = ( + f"https://docs.google.com/spreadsheets/d/{SHEET_ID}/export" + f"?format=csv&gid={SHEET_GID}" +) +# Fallback for uploaded .xlsx files (htmlview URLs) — downloads raw bytes +# and converts to CSV via openpyxl. +SHEET_XLSX_URL = f"https://docs.google.com/uc?export=download&id={SHEET_ID}" +SHEET_TAB_INDEX = 0 # 0 = first sheet; change if data is on another tab + +ROOT = Path(__file__).parent +PARQUET_PATH = ROOT / "data" / "tdr.parquet" +DOCS_DIR = ROOT / "documents" +HF_REPO = "trialdesignbench/source" + +UA = ( + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" +) +TIMEOUT = 60 +RETRIES = 2 + + +def fetch_sheet_csv() -> str: + """Try native-sheet CSV export first; fall back to xlsx download + convert.""" + try: + req = urllib.request.Request(SHEET_CSV_URL, headers={"User-Agent": UA}) + with urllib.request.urlopen(req, timeout=TIMEOUT) as resp: + final_url = resp.geturl() + if "accounts.google.com" in final_url or "ServiceLogin" in final_url: + raise RuntimeError( + "Google Sheet is not publicly shared — " + "enable 'Anyone with link: Viewer'." + ) + return resp.read().decode("utf-8") + except urllib.error.HTTPError as e: + if e.code != 400: + raise + print("CSV export returned 400 — falling back to .xlsx download.") + return _fetch_xlsx_as_csv() + + +def _fetch_xlsx_as_csv() -> str: + try: + from openpyxl import load_workbook + except ImportError as e: + msg = ( + "openpyxl is required to read uploaded .xlsx Drive files. " + "Install with: pip install openpyxl" + ) + raise RuntimeError(msg) from e + + import io + + req = urllib.request.Request(SHEET_XLSX_URL, headers={"User-Agent": UA}) + with urllib.request.urlopen(req, timeout=TIMEOUT) as resp: + final_url = resp.geturl() + if "accounts.google.com" in final_url or "ServiceLogin" in final_url: + raise RuntimeError( + "File is not publicly shared — enable 'Anyone with link: Viewer'." + ) + data = resp.read() + + wb = load_workbook(io.BytesIO(data), read_only=True, data_only=True) + ws = wb.worksheets[SHEET_TAB_INDEX] + + def _fmt(v: object) -> str: + if v is None: + return "" + # openpyxl returns whole-number cells as floats (1.0, 2.0, ...); + # collapse back to int so "#" / "Year" / "PMID" stay digit-like. + if isinstance(v, float) and v.is_integer(): + return str(int(v)) + return str(v) + + buf = io.StringIO() + writer = csv.writer(buf) + for row in ws.iter_rows(values_only=True): + writer.writerow([_fmt(v) for v in row]) + return buf.getvalue() + + +def read_existing_ids() -> set[str]: + if not PARQUET_PATH.exists(): + return set() + import pandas as pd + + df = pd.read_parquet(PARQUET_PATH, columns=["#"]) + out: set[str] = set() + for v in df["#"].dropna(): + # Normalize whole-number floats ("1.0") back to "1" so the diff + # matches sheet rows regardless of how the column was stored. + if isinstance(v, float) and v.is_integer(): + out.add(str(int(v))) + else: + s = str(v).strip() + if s: + out.add(s) + return out + + +def write_parquet(csv_text: str) -> None: + import io + + import pandas as pd + + df = pd.read_csv(io.StringIO(csv_text)) + PARQUET_PATH.parent.mkdir(parents=True, exist_ok=True) + df.to_parquet(PARQUET_PATH, index=False, compression="snappy") + + +def parse_rows(csv_text: str) -> list[dict[str, str]]: + reader = csv.DictReader(csv_text.splitlines()) + return [row for row in reader if (row.get("#") or "").strip().isdigit()] + + +def download_pdf(url: str, dest: Path) -> tuple[bool, str]: + if dest.exists() and dest.stat().st_size > 0: + return True, "exists" + req = urllib.request.Request(url, headers={"User-Agent": UA}) + last_err = "" + for attempt in range(RETRIES + 1): + try: + with urllib.request.urlopen(req, timeout=TIMEOUT) as resp: + data = resp.read() + dest.write_bytes(data) + return True, f"ok ({len(data)} bytes)" + except urllib.error.HTTPError as e: + last_err = f"HTTP {e.code}" + except (urllib.error.URLError, TimeoutError) as e: + last_err = f"network: {e}" + except Exception as e: # noqa: BLE001 + last_err = f"error: {e}" + time.sleep(1 + attempt) + return False, last_err + + +def paper_link_slug(link: str) -> str: + """Last two path segments of a Paper Link joined with '_'. + + Example: https://doi.org/10.1056/nejmoa2511478 -> 10.1056_nejmoa2511478 + """ + path = urlparse((link or "").strip()).path.strip("/") + parts = [p for p in path.split("/") if p] + return "_".join(parts[-2:]) if len(parts) >= 2 else "" + + +def _row_missing_pdfs(row: dict[str, str]) -> bool: + """A row needs work iff it has a usable slug + at least one link whose + target PDF is not already on disk.""" + slug = paper_link_slug(row.get("Paper Link") or "") + if not slug: + return False + protocol = (row.get("Study Protocol Link") or "").strip() + sap = (row.get("Protocol+SAP / SAP Link") or "").strip() + if not protocol and not sap: + return False + row_dir = DOCS_DIR / slug + if protocol and not (row_dir / "protocol.pdf").exists(): + return True + if sap and not (row_dir / "sap.pdf").exists(): + return True + return False + + +def download_for_row(row: dict[str, str]) -> list[Path]: + num = (row.get("#") or "").strip() + slug = paper_link_slug(row.get("Paper Link") or "") + protocol = (row.get("Study Protocol Link") or "").strip() + sap = (row.get("Protocol+SAP / SAP Link") or "").strip() + if not protocol and not sap: + print(f"[{num}] skip: no links") + return [] + if not slug: + print(f"[{num}] skip: no usable Paper Link for folder name") + return [] + + row_dir = DOCS_DIR / slug + row_dir.mkdir(parents=True, exist_ok=True) + new_files: list[Path] = [] + + if protocol: + dest = row_dir / "protocol.pdf" + existed = dest.exists() + ok, msg = download_pdf(protocol, dest) + print(f"[{num}/{slug}] protocol: {msg}") + if ok and not existed: + new_files.append(dest) + elif not ok: + (row_dir / "protocol.error.txt").write_text( + f"{protocol}\n{msg}\n", encoding="utf-8" + ) + + if sap: + dest = row_dir / "sap.pdf" + existed = dest.exists() + ok, msg = download_pdf(sap, dest) + print(f"[{num}/{slug}] sap: {msg}") + if ok and not existed: + new_files.append(dest) + elif not ok: + (row_dir / "sap.error.txt").write_text( + f"{sap}\n{msg}\n", encoding="utf-8" + ) + + return new_files + + +def _hf_cli() -> str: + # Resolve the `hf` CLI even when PATH doesn't include the active venv/conda env + # (e.g. when this script is launched from a non-interactive shell). + import shutil + + found = shutil.which("hf") + if found: + return found + candidate = Path(sys.executable).parent / "hf" + if candidate.exists(): + return str(candidate) + raise RuntimeError( + "`hf` CLI not found. Install with: pip install -U 'huggingface_hub[cli]'" + ) + + +def hf_upload(paths: list[Path]) -> None: + if not paths: + print("No files to upload.") + return + rels = [str(p.relative_to(ROOT)) for p in paths] + print(f"Uploading {len(rels)} files to {HF_REPO} ...") + hf = _hf_cli() + # Use hf upload for small incremental batches; switch to upload-large-folder + # if the new-row set is large. + if len(rels) > 200: + cmd = [ + hf, "upload-large-folder", HF_REPO, str(ROOT), + "--repo-type=dataset", "--num-workers=4", + ] + subprocess.run(cmd, check=True) + return + for rel in rels: + cmd = [hf, "upload", HF_REPO, rel, rel, "--repo-type=dataset"] + subprocess.run(cmd, check=True) + + +def main() -> None: + parser = argparse.ArgumentParser() + parser.add_argument("--no-upload", action="store_true", help="Skip HF upload.") + parser.add_argument( + "--dry-run", action="store_true", help="Show diff only, no downloads or upload." + ) + args = parser.parse_args() + + print(f"Fetching sheet ({SHEET_ID}, gid={SHEET_GID}) ...") + csv_text = fetch_sheet_csv() + new_rows_all = parse_rows(csv_text) + print(f"Sheet has {len(new_rows_all)} data rows.") + + existing_ids = read_existing_ids() + print(f"Local parquet has {len(existing_ids)} rows.") + + new_in_sheet = [r for r in new_rows_all if (r.get("#") or "").strip() not in existing_ids] + rows_needing_pdfs = [r for r in new_rows_all if _row_missing_pdfs(r)] + print(f"New rows in sheet (vs parquet #): {len(new_in_sheet)}") + print(f"Rows missing PDFs on disk: {len(rows_needing_pdfs)}") + for r in rows_needing_pdfs[:20]: + print(f" - #{r.get('#')}: {(r.get('Paper Title') or '')[:80]}") + if len(rows_needing_pdfs) > 20: + print(f" ... and {len(rows_needing_pdfs) - 20} more") + + if args.dry_run: + return + + write_parquet(csv_text) + print(f"Wrote {PARQUET_PATH}") + + DOCS_DIR.mkdir(exist_ok=True) + new_pdfs: list[Path] = [] + for row in rows_needing_pdfs: + new_pdfs.extend(download_for_row(row)) + + print(f"Downloaded {len(new_pdfs)} new PDFs.") + + if args.no_upload: + return + + hf_upload([PARQUET_PATH, *new_pdfs]) + print("Done.") + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + print("\nAborted.") + sys.exit(130)