Skip to content

Commit 3ce2b47

Browse files
docs: rewrite Python Library guide to mirror the Console workflow
Restructure the guide as the same first analysis as the Console guide, one snippet per step: install, log in (browser or refresh token), upload slides with computed metadata, start Atlas H&E-TME, follow the run state and per-slide outcomes, download results, list/cancel/clean up. Replace the placeholder test-app snippet, and move notebooks to "Where to go next". Also drop the "Step N:" prefixes from the API guide's login headings for consistency with the Console guide. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent d15e166 commit 3ce2b47

2 files changed

Lines changed: 75 additions & 78 deletions

File tree

docs/partials/get_started_api.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The API never sees your password. It accepts a short-lived **access token** —
2020

2121
This is the standard OAuth 2.0 Device Authorization Grant ([RFC 8628](https://datatracker.ietf.org/doc/html/rfc8628)), so most languages have a library for the three steps below — you supply the endpoints and client ID.
2222

23-
### Step 1: start the login
23+
### Start the login
2424

2525
```shell
2626
CLIENT_ID=your-client-id
@@ -37,7 +37,7 @@ curl -s -X POST https://aignostics-platform.eu.auth0.com/oauth/device/code \
3737

3838
The response carries `verification_uri_complete` (the link for you), `user_code` (the code to compare), `device_code` (your program's secret handle), and `interval` (seconds between polls).
3939

40-
### Step 2: approve it, and collect the tokens
40+
### Approve it, and collect the tokens
4141

4242
Open `verification_uri_complete` in a browser, log in, and check the code shown matches the `user_code` your program printed — that comparison is what stops someone else's program from being approved with your account. Meanwhile, poll for the tokens every `interval` seconds while the response says `error: authorization_pending` (or `slow_down`, meaning you are asking too often):
4343

@@ -50,7 +50,7 @@ curl -s -X POST https://aignostics-platform.eu.auth0.com/oauth/token \
5050

5151
Once you approve, the same call returns `access_token` and `refresh_token`. Store the refresh token as a secret — it is what makes the next step possible — and never log or commit either token.
5252

53-
### Step 3: renew without a browser
53+
### Renew without a browser
5454

5555
This is what CI and long-running services do whenever a call returns `401`:
5656

@@ -136,7 +136,7 @@ Got an access token, and a refresh token to store as a secret (64 chars).
136136
}
137137
```
138138

139-
Keep the refresh token in your secret manager and later runs skip the browser entirely — Step 3 is the whole renewal.
139+
Keep the refresh token in your secret manager and later runs skip the browser entirely — the renewal call above is all they need.
140140

141141
## Find out what the application expects
142142

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,116 @@
11
# Get started with the Python Library
22

3-
The **Aignostics Python Library** lets you call the Aignostics Platform programmatically from your own scripts, notebooks, and applications. It is well suited to building custom analysis pipelines and processing large datasets in Python.
3+
The **Aignostics Python Library** lets you use the Aignostics Platform from your own scripts, notebooks, and pipelines. This guide takes you through the same first analysis as the Console guide — upload your slides, run [Atlas H&E-TME](https://www.aignostics.com/products/he-tme-profiling-product) on them, follow the analysis, and download the results — in Python.
44

55
```{include} ../partials/_get_started_signup.md
66
```
77

88
## Install the library
99

10-
Add the Aignostics Python SDK to your project.
11-
12-
**With [uv](https://docs.astral.sh/uv/):**
10+
Add the Aignostics Python SDK to your project with [uv](https://docs.astral.sh/uv/) or [pip](https://pip.pypa.io/en/stable/):
1311

1412
```shell
1513
uv add aignostics
14+
# or
15+
pip install aignostics
1616
```
1717

18-
**With [pip](https://pip.pypa.io/en/stable/):**
18+
## Log in
19+
20+
Create a client. The first time, your browser opens for you to log in with your email, password, and the six-digit code from your authenticator app; the login is cached for future sessions.
21+
22+
```python
23+
from aignostics import platform
24+
25+
client = platform.Client()
26+
print(client.me().user.email)
27+
```
28+
29+
For scripts that run without a browser — on a server or in CI — set a refresh token instead. Get one from the `Use in Python Notebooks` section of [your quick-start page in Console](https://platform.aignostics.com/getting-started/quick-start) and put it in the environment or in `~/.aignostics/.env`:
1930

2031
```shell
21-
pip install aignostics
32+
AIGNOSTICS_REFRESH_TOKEN=<your refresh token>
2233
```
2334

24-
## Usage
35+
## Upload your slides
2536

26-
The following snippet shows how to use the client to submit an application run:
37+
The platform reads each slide from your organization's private bucket, together with its checksum, size, resolution, staining method, tissue, and disease. The library computes the technical values from the files; the medical ones you set per slide — here the same for all slides in the folder.
2738

2839
```python
29-
from aignostics import platform
40+
from pathlib import Path
3041

31-
# initialize the client
32-
client = platform.Client()
33-
# submit an application run
34-
application_run = client.runs.submit(
35-
application_id="test-app",
36-
items=[
37-
platform.InputItem(
38-
external_id="slide-1",
39-
input_artifacts=[
40-
platform.InputArtifact(
41-
name="whole_slide_image",
42-
download_url="<a signed url to download the data>",
43-
metadata={
44-
"checksum_base64_crc32c": "AAAAAA==",
45-
"resolution_mpp": 0.25,
46-
"width_px": 1000,
47-
"height_px": 1000,
48-
},
49-
)
50-
],
51-
),
52-
],
42+
from aignostics.application import Service as ApplicationService
43+
44+
APPLICATION = "he-tme"
45+
slides = Path("my-slides")
46+
47+
metadata = ApplicationService.generate_metadata_from_source_directory(
48+
slides,
49+
APPLICATION,
50+
mappings=[".*:staining_method=H&E,tissue=LUNG,disease=LUNG_CANCER"],
5351
)
54-
# wait for the results and download incrementally as they become available
55-
application_run.download_to_folder("path/to/download/folder")
56-
```
5752

58-
See the [library reference](https://aignostics.readthedocs.io/en/latest/lib_reference.html) for all classes and methods.
5953

60-
## Example notebooks
54+
def remember_bucket_url(_bytes_uploaded: int, source: Path, bucket_url: str) -> None:
55+
for row in metadata:
56+
if row["external_id"] == str(source):
57+
row["platform_bucket_url"] = bucket_url
58+
6159

62-
> [!IMPORTANT]
63-
> Before you start, set up your authentication credentials if you have not done so. Visit
64-
> [your personal dashboard on the Aignostics Platform website](https://platform.aignostics.com/getting-started/quick-start)
65-
> and follow the steps in the `Use in Python Notebooks` section.
60+
ApplicationService.application_run_upload(APPLICATION, metadata, upload_progress_callable=remember_bucket_url)
61+
```
6662

67-
The SDK includes ready-to-use [Marimo](https://marimo.io/) notebooks that demonstrate platform interaction patterns — ideal for learning the API, prototyping workflows, and integrating with data science pipelines. They use the "Test Application" (free for all users):
63+
`mappings` match slide paths by regular expression, so a folder with mixed cases takes one mapping per group, for example `"lung/.*:tissue=LUNG,disease=LUNG_CANCER"`. If your slides are already in a cloud bucket, you can skip the upload and hand the platform signed URLs instead — see {doc}`Give the platform access to your slides <get_started_api>` in the API guide.
6864

69-
```shell
70-
# clone the python-sdk repository
71-
git clone https://github.com/aignostics/python-sdk.git
72-
# within the cloned repository, install the SDK and all dependencies
73-
uv sync --all-extras
74-
# open the example notebook in your browser
75-
uv run marimo edit examples/notebook.py
65+
## Start the analysis
66+
67+
```python
68+
run = ApplicationService().application_run_submit_from_metadata(APPLICATION, metadata, note="My first analysis")
69+
print(run.run_id)
7670
```
7771

78-
> 💡 You can also run a notebook inside the Aignostics Launchpad: select the run you want to inspect in the left sidebar and click **Marimo**.
72+
Keep the `run_id`: it is how you find the analysis again later, in Python and in Console.
7973

80-
## Defining the input for an application run
74+
## Follow the analysis
8175

82-
The following details apply to advanced use cases. These examples use the "Test Application" — a free application available to all users for testing and development.
76+
The analysis runs on Aignostics servers, so your script can exit and pick it up later with `client.run(run_id)`. The run's state goes `PENDING``PROCESSING``TERMINATED`; each slide has its own state and outcome.
77+
78+
```python
79+
details = run.details()
80+
print(details.state, details.termination_reason)
8381

84-
When creating a run, you specify the `application_id` and optionally the `application_version`. If you omit the version, the latest is used automatically. You then define the input items to process:
82+
s = details.statistics
83+
print(f"{s.item_succeeded_count} of {s.item_count} slides succeeded, {s.item_user_error_count + s.item_system_error_count} failed")
84+
85+
for item in run.results():
86+
print(item.external_id, item.state, item.termination_reason)
87+
```
88+
89+
The analysis also appears under **My Application Runs** in [Console](https://platform.aignostics.com), where you can review the results in the viewer.
90+
91+
## Download results
8592

8693
```python
87-
(
88-
platform.InputItem(
89-
external_id="1",
90-
input_artifacts=[
91-
platform.InputArtifact(
92-
name="whole_slide_image", # defined by the application version's input artifact schema
93-
download_url="<a signed url to download the data>",
94-
metadata={ # defined by the application version's input artifact schema
95-
"checksum_base64_crc32c": "N+LWCg==",
96-
"resolution_mpp": 0.46499982,
97-
"width_px": 3728,
98-
"height_px": 3640,
99-
},
100-
)
101-
],
102-
),
103-
)
94+
run.download_to_folder("results")
10495
```
10596

106-
For each item you process, provide a unique `external_id` string — it is used to match results back to your inputs. The `input_artifacts` field is a list of `InputArtifact` objects defining the data and metadata for each item. The required artifacts depend on the application version; for the test application there is a single artifact, named `whole_slide_image`.
97+
This waits for the analysis to finish and downloads each slide's results as soon as they are ready: the tissue regions, the classified cells, and a spreadsheet of measurements such as cell counts and densities. Results are kept for 30 days, so download what you want to keep.
10798

108-
The `download_url` is a signed URL that allows the Aignostics Platform to download the image data during processing.
99+
## List, cancel, or clean up
109100

110-
## Self-signed URLs for large files
101+
```python
102+
for r in client.runs.list(application_id=APPLICATION):
103+
print(r.run_id, r.details().state)
111104

112-
To make whole slide images available to the Aignostics Platform, you provide a signed URL the platform can download from. Signed URLs for files in Google Cloud Storage buckets can be generated with `generate_signed_url` ([code](https://github.com/aignostics/python-sdk/blob/main/src/aignostics/platform/_utils.py)).
105+
run = client.run("<run_id>")
106+
run.cancel() # stop an analysis that is still running
107+
run.delete() # remove a finished analysis and its results
108+
```
113109

114-
**You must provide the [required credentials](https://cloud.google.com/docs/authentication/application-default-credentials) for the Google Cloud Storage bucket.**
110+
Your slides stay in your bucket until you delete them; see {doc}`Clean up your bucket <get_started_console>` in the Console guide.
115111

116112
## Where to go next
117113

118114
- {doc}`Invite your team <invite_your_team>` — add colleagues so they can run analyses too.
119115
- {doc}`Library reference <lib_reference>` — all public classes and functions.
116+
- [Example notebooks](https://github.com/aignostics/python-sdk/tree/main/examples) — ready-to-use [Marimo](https://marimo.io/) and Jupyter notebooks in the repository.

0 commit comments

Comments
 (0)