-
Notifications
You must be signed in to change notification settings - Fork 0
docs: initial SDK documentation #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matejpekar
wants to merge
14
commits into
main
Choose a base branch
from
mkdocs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a97f232
feat: 1st docs iteration
6d115d3
feat: add dependency group + CI pipelines
41353ec
fix: remove rules and pages
224a62e
fix: gitlab template
5df95db
Delete .gitlab-ci.yml
matejpekar ab170df
feat: mkdocs workflow
matejpekar 778ba50
fix: dir
matejpekar 8f614db
fix: update lock
matejpekar c835a9f
fix: update lock
matejpekar fcac854
fix: update type
matejpekar 3fd2b8b
feat: add init file
2b0f882
fix: change mkdocs site/repo url to github
aaa7789
fix: syntax error in docs
925be4d
fix: add RGB convert into README
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| name: MkDocs Build (RationAI Standard) | ||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
|
|
||
| jobs: | ||
| run: | ||
| uses: RationAI/.github/.github/workflows/mkdocs-build.yml@main |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # RationAI Python SDK | ||
|
|
||
| Python SDK for interacting with RationAI pathology image analysis services (classification, segmentation, and QC). | ||
|
|
||
| [Quick start](learn/get-started/quick-start.md) | ||
|
|
||
| [How it works](learn/how-it-works.md) | ||
|
|
||
| [API reference](reference/client.md) | ||
|
|
||
| ## What you can do | ||
|
|
||
| - Run image classification and segmentation via `client.models`. | ||
| - Run quality-control workflows via `client.qc`. | ||
| - Choose sync (`Client`) or async (`AsyncClient`) depending on your app. | ||
|
|
||
| ## Minimal examples | ||
|
|
||
| ### Model example | ||
|
|
||
| ```python | ||
| from PIL import Image | ||
|
|
||
| import rationai | ||
|
|
||
| image = Image.open("path/to/image.jpg").convert("RGB") | ||
|
|
||
| with rationai.Client() as client: | ||
| result = client.models.classify_image("model-name", image) | ||
| print(result) | ||
| ``` | ||
|
|
||
| ### QC example | ||
|
|
||
| ```python | ||
| import rationai | ||
|
|
||
| with rationai.Client() as client: | ||
| xopat_url = client.qc.check_slide( | ||
| wsi_path="/data/slides/slide.svs", | ||
| output_path="/data/qc-output/slide-001", | ||
| ) | ||
| print(xopat_url) | ||
| ``` |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| # Quick start | ||
|
|
||
| ## Sync vs Async clients | ||
|
|
||
| This SDK provides two clients: | ||
|
|
||
| - `rationai.Client` (sync): Uses blocking HTTP requests. Best for scripts, notebooks, CLIs, or when your code is already synchronous. | ||
| - `rationai.AsyncClient` (async): Uses non-blocking HTTP requests (`await`). Best when you already have an `asyncio` event loop (FastAPI, async workers) or you want to run many requests concurrently. | ||
|
|
||
| Both clients expose the same high-level resources: | ||
|
|
||
| - `client.models` for image classification/segmentation | ||
| - `client.qc` for quality control endpoints | ||
|
|
||
| ### What’s the actual difference? | ||
|
|
||
| - **Sync** calls (e.g. `client.models.classify_image(...)`) block the current thread until the request completes. | ||
| - **Async** calls (e.g. `await client.models.classify_image(...)`) yield control back to the event loop while the network request is in flight, so other tasks can run. | ||
|
|
||
| ### Lifecycle (important) | ||
|
|
||
| - Prefer using context managers so connections are closed: | ||
| - sync: `with rationai.Client(...) as client: ...` | ||
| - async: `async with rationai.AsyncClient(...) as client: ...` | ||
| - If you don’t use `with`, call `client.close()` (sync) / `await client.aclose()` (async). | ||
|
|
||
| For details on what is sent over the wire (compression, payloads), see: [How it works](../how-it-works.md). | ||
|
|
||
| ## API at a glance | ||
|
|
||
| ### Models | ||
|
|
||
| #### `client.models.classify_image` | ||
|
|
||
| Signature: | ||
|
|
||
| `classify_image(model: str, image: PIL.Image.Image | numpy.typing.NDArray[numpy.uint8], timeout=...) -> float | dict[str, float]` | ||
|
|
||
| - `model`: Model name / path appended to `models_base_url`. | ||
| - `image`: **uint8 RGB** image (PIL or NumPy array of shape `(H, W, 3)`). | ||
| - `timeout`: Optional request timeout (defaults to the client’s timeout). | ||
| - Returns: classification result from JSON (often `float` for binary, or `dict[class, prob]`). | ||
|
|
||
| #### `client.models.segment_image` | ||
|
|
||
| Signature: | ||
|
|
||
| `segment_image(model: str, image: PIL.Image.Image | numpy.typing.NDArray[numpy.uint8], timeout=...) -> numpy.typing.NDArray[numpy.float16]` | ||
|
|
||
| - `model`: Model name / path appended to `models_base_url`. | ||
| - `image`: **uint8 RGB** image (PIL or NumPy array of shape `(H, W, 3)`). | ||
| - `timeout`: Optional request timeout (defaults to the client’s timeout). | ||
| - Returns: `float16` NumPy array with shape `(num_classes, height, width)`. | ||
|
|
||
| ### Quality control (QC) | ||
|
|
||
| #### `client.qc.check_slide` | ||
|
|
||
| Signature: | ||
|
|
||
| `check_slide(wsi_path: os.PathLike[str] | str, output_path: os.PathLike[str] | str, config: SlideCheckConfig | None = None, timeout=3600) -> str` | ||
|
|
||
| - `wsi_path`: Path to a whole-slide image (evaluated by the QC service). | ||
| - `output_path`: Directory where the QC service should write masks (evaluated by the QC service). | ||
| - `config`: Optional `SlideCheckConfig` (see reference types). | ||
| - `timeout`: Request timeout (default is 3600 seconds). | ||
| - Returns: xOpat URL as plain text. | ||
|
|
||
| #### `client.qc.generate_report` | ||
|
|
||
| Signature: | ||
|
|
||
| `generate_report(backgrounds: Iterable[os.PathLike[str] | str], mask_dir: os.PathLike[str] | str, save_location: os.PathLike[str] | str, compute_metrics: bool = True, timeout=...) -> None` | ||
|
|
||
| - `backgrounds`: Iterable of slide/background image paths. | ||
| - `mask_dir`: Directory containing generated masks. | ||
| - `save_location`: Path where the report HTML should be written. | ||
| - `compute_metrics`: Whether to compute aggregated metrics (default: `True`). | ||
| - Returns: nothing. | ||
|
|
||
| ## Synchronous client | ||
|
|
||
| ```python | ||
| from PIL import Image | ||
| import rationai | ||
|
|
||
| image = Image.open("path/to/image.jpg").convert("RGB") | ||
|
|
||
| with rationai.Client() as client: | ||
| result = client.models.classify_image("model-name", image) | ||
| print(result) | ||
| ``` | ||
|
|
||
| ## Asynchronous client | ||
|
|
||
| ```python | ||
| import asyncio | ||
| from PIL import Image | ||
| import rationai | ||
|
|
||
| image = Image.open("path/to/image.jpg").convert("RGB") | ||
|
|
||
| async def main(): | ||
| async with rationai.AsyncClient() as client: | ||
| result = await client.models.classify_image("model-name", image) | ||
| print(result) | ||
|
|
||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| ### Concurrency with the async client | ||
|
|
||
| Use `asyncio` concurrency when you need to process many images. A semaphore is the simplest way to cap concurrency so you don’t overload the server. | ||
|
|
||
| ```python | ||
| import asyncio | ||
| from PIL import Image | ||
| import rationai | ||
|
|
||
| async def classify_many(paths: list[str], model: str, *, max_concurrent: int = 8) -> list[float | dict[str, float]]: | ||
| sem = asyncio.Semaphore(max_concurrent) | ||
|
|
||
| async def one(client: rationai.AsyncClient, path: str) -> float | dict[str, float]: | ||
| async with sem: | ||
| image = Image.open(path).convert("RGB") | ||
| return await client.models.classify_image(model, image) | ||
|
|
||
| async with rationai.AsyncClient() as client: | ||
| return await asyncio.gather(*(one(client, p) for p in paths)) | ||
| ``` | ||
|
|
||
| ## Common pitfalls | ||
|
|
||
| - **PIL image mode**: ensure RGB. | ||
|
|
||
| ```python | ||
| image = Image.open(path).convert("RGB") | ||
| ``` | ||
|
|
||
| - **NumPy dtype/shape**: the services expect `uint8` RGB images. | ||
|
|
||
| ```python | ||
| import numpy as np | ||
|
|
||
| assert arr.dtype == np.uint8 | ||
| assert arr.ndim == 3 and arr.shape[2] == 3 | ||
| ``` | ||
|
|
||
| - **Forgetting to close clients**: prefer `with ...` / `async with ...`. | ||
|
|
||
| - **Too much async concurrency**: cap with a semaphore (start small like 4–16) to avoid server overload/timeouts. | ||
|
|
||
| - **Timeouts**: segmentation/QC can take longer. Increase per-request timeout if needed. | ||
|
|
||
| ```python | ||
| result = client.models.segment_image("model", image, timeout=300) | ||
| ``` | ||
|
|
||
| - **QC paths are server-side**: `wsi_path` / `output_path` must exist where the QC service runs. | ||
|
|
||
| ## Configuration | ||
|
|
||
| You can override service URLs and timeouts: | ||
|
|
||
| ```python | ||
| from rationai import Client | ||
|
|
||
| client = Client( | ||
| models_base_url="http://localhost:8000", | ||
| qc_base_url="http://localhost:8001", | ||
| timeout=300, | ||
| ) | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.