|
1 | 1 | # Get started with the Python Library |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | ```{include} ../partials/_get_started_signup.md |
6 | 6 | ``` |
7 | 7 |
|
8 | 8 | ## Install the library |
9 | 9 |
|
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/): |
13 | 11 |
|
14 | 12 | ```shell |
15 | 13 | uv add aignostics |
| 14 | +# or |
| 15 | +pip install aignostics |
16 | 16 | ``` |
17 | 17 |
|
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`: |
19 | 30 |
|
20 | 31 | ```shell |
21 | | -pip install aignostics |
| 32 | +AIGNOSTICS_REFRESH_TOKEN=<your refresh token> |
22 | 33 | ``` |
23 | 34 |
|
24 | | -## Usage |
| 35 | +## Upload your slides |
25 | 36 |
|
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. |
27 | 38 |
|
28 | 39 | ```python |
29 | | -from aignostics import platform |
| 40 | +from pathlib import Path |
30 | 41 |
|
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"], |
53 | 51 | ) |
54 | | -# wait for the results and download incrementally as they become available |
55 | | -application_run.download_to_folder("path/to/download/folder") |
56 | | -``` |
57 | 52 |
|
58 | | -See the [library reference](https://aignostics.readthedocs.io/en/latest/lib_reference.html) for all classes and methods. |
59 | 53 |
|
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 | + |
61 | 59 |
|
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 | +``` |
66 | 62 |
|
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. |
68 | 64 |
|
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) |
76 | 70 | ``` |
77 | 71 |
|
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. |
79 | 73 |
|
80 | | -## Defining the input for an application run |
| 74 | +## Follow the analysis |
81 | 75 |
|
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) |
83 | 81 |
|
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 |
85 | 92 |
|
86 | 93 | ```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") |
104 | 95 | ``` |
105 | 96 |
|
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. |
107 | 98 |
|
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 |
109 | 100 |
|
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) |
111 | 104 |
|
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 | +``` |
113 | 109 |
|
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. |
115 | 111 |
|
116 | 112 | ## Where to go next |
117 | 113 |
|
118 | 114 | - {doc}`Invite your team <invite_your_team>` — add colleagues so they can run analyses too. |
119 | 115 | - {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