Skip to content

Commit

Permalink
Improve API notebook example (#70)
Browse files Browse the repository at this point in the history
* add data config path to the server cli

* Improve api example and set more restrictive pinning on cupy

* Drop `DataConfig` in favor of `MainConfig`

* Update tests for ultrack server cli

* fix error of pickling a local object on Windows CI

* minor changes to notebook text

---------

Co-authored-by: Jordao Bragantini <[email protected]>
  • Loading branch information
ilan-theodoro and JoOkuma authored Jun 27, 2024
1 parent 4d200ca commit 8f10963
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 151,170 deletions.
151,355 changes: 228 additions & 151,127 deletions examples/api/api_example.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/api/environment_gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ channels:
dependencies:
- coin-or-cbc
- cucim
- cupy!=13.0.*
- cupy!=13.*
- gurobi
- jupyter
- pip
Expand Down
2 changes: 1 addition & 1 deletion examples/flow_field_3d/environment_gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ channels:
dependencies:
- coin-or-cbc
- cucim
- cupy!=13.0.*
- cupy!=13.*
- gurobi
- jupyter
- napari
Expand Down
2 changes: 1 addition & 1 deletion examples/micro_sam/environment_gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ channels:
dependencies:
- coin-or-cbc
- cucim
- cupy!=13.0.*
- cupy!=13.*
- gurobi
- jupyter
- pip
Expand Down
2 changes: 1 addition & 1 deletion examples/multi_color_ensemble/environment_gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies:
- cellpose
- coin-or-cbc
- cucim
- cupy!=13.0.*
- cupy!=13.*
- gurobi
- jupyter
- pip
Expand Down
2 changes: 1 addition & 1 deletion examples/neuromast_plantseg/environment_gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ channels:
dependencies:
- coin-or-cbc
- cucim
- cupy!=13.0.*
- cupy!=13.*
- gurobi
- jupyter
- pyqt
Expand Down
2 changes: 1 addition & 1 deletion examples/stardist_2d/environment_gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
- conda-forge::cudatoolkit=11.2
- conda-forge::cudnn=8.4.1
- conda-forge::tensorflow-gpu=2.11.0
- conda-forge::cupy!=13.0.*
- conda-forge::cupy!=13.*
- coin-or-cbc
- gurobi
- jupyter
Expand Down
2 changes: 1 addition & 1 deletion examples/zebrahub/environment_gpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ channels:
dependencies:
- coin-or-cbc
- cucim
- cupy!=13.0.*
- cupy!=13.*
- gurobi
- jupyter
- pip
Expand Down
5 changes: 2 additions & 3 deletions ultrack/api/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os
from multiprocessing import Process
from pathlib import Path
from threading import Thread
from typing import Union

import uvicorn

from ultrack import MainConfig
from ultrack.api import app
from ultrack.config import DataConfig


def _in_notebook():
Expand All @@ -23,7 +22,7 @@ def _in_notebook():

def start_server(
api_results_path: Union[Path, str, None] = None,
ultrack_data_config: Union[DataConfig, None] = None,
ultrack_data_config: Union[MainConfig, None] = None,
host: str = "0.0.0.0",
port: int = 8000,
):
Expand Down
47 changes: 22 additions & 25 deletions ultrack/cli/_test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from ultrack.utils.data import make_config_content


def _run_server(instance_config_path: str):
_run_command(["server", "--port", "54123", "-cfg", instance_config_path])

def _run_command(command_and_args: List[str]) -> None:
try:
main(command_and_args)
Expand Down Expand Up @@ -161,6 +164,25 @@ def test_clear_database(self, instance_config_path: str, mode: str) -> None:
]
)

def test_server(self, instance_config_path: str) -> None:
# Start server in a background thread
process = Process(target=_run_server, args=(instance_config_path,))
process.start()

# Wait for server to start
import time

time.sleep(10)

response = requests.get("http://127.0.0.1:54123")
print(response.content)

assert process.is_alive()
assert response.status_code == 200

process.terminate()
process.join()


def test_create_config(tmp_path: Path) -> None:
_run_command(["create_config", str(tmp_path / "config.toml")])
Expand All @@ -178,28 +200,3 @@ def test_labels_to_contours(zarr_dataset_paths: List[str], tmp_path: Path) -> No

def test_check_gurobi() -> None:
_run_command(["check_gurobi"])


def _run_server():
_run_command(["server", "--port", "54123"])


def test_server() -> None:

# Start server in a background thread
process = Process(target=_run_server)
process.start()

# Wait for server to start
import time

time.sleep(10)

response = requests.get("http://127.0.0.1:54123")
print(response.content)

assert process.is_alive()
assert response.status_code == 200

process.terminate()
process.join()
19 changes: 11 additions & 8 deletions ultrack/cli/server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
from typing import Optional

import click
import uvicorn
import toml

from ultrack.api.app import app
from ultrack import MainConfig
from ultrack.api import start_server
from ultrack.cli.utils import config_option
from ultrack.config import DataConfig


@click.command("server")
Expand All @@ -22,9 +25,9 @@
default=None,
show_default=True,
)
def server_cli(host: str, port: int, api_results_path: str) -> None:
@config_option()
def server_cli(
host: str, port: int, api_results_path: Optional[str], config: MainConfig
) -> None:
"""Start the websockets ultrack API."""
if api_results_path is not None:
os.environ["API_RESULTS_PATH"] = str(api_results_path)

uvicorn.run(app, host=host, port=port)
start_server(api_results_path, ultrack_data_config=config, host=host, port=port)

0 comments on commit 8f10963

Please sign in to comment.