Welcome to the tutorial on creating BioEngine Apps! In this guide, we'll walk you through the process of developing and submitting both UI and Compute Apps for the BioEngine platform. Whether you're building a user interface or a computational backend, this tutorial will help you get started and contribute your app to the BioEngine ecosystem.
Before you begin, ensure you have the following prerequisites:
- Basic Programming Skills: Familiarity with Python, JavaScript, and web development concepts.
- Development Environment:
- Python 3.7 or higher installed on your machine.
- A modern web browser (e.g., Chrome, Firefox).
- Git installed for version control.
- Tools and Libraries:
- Install the necessary Python packages:
pip install hypha-rpc pip install ray
- Install the necessary Python packages:
-
Clone the BioEngine Repository:
- Start by cloning the BioEngine GitHub repository to your local machine:
git clone https://github.com/bioimage-io/bioengine.git cd bioengine/bioimageio/engine
- Start by cloning the BioEngine GitHub repository to your local machine:
-
Create a New Folder for Your App:
- Inside the
bioengine/bioimageio/engine
directory, create a new folder for your app:mkdir my-bioengine-app cd my-bioengine-app
- Inside the
Compute Apps are the backend services that perform computations. In this example, we'll create a simple Compute App using Cellpose for cell segmentation.
-
Create the
__init__.py
File:- This file will contain the main logic for your Compute App. Here’s an example using the Cellpose model:
from hypha_rpc import api import numpy as np class CellposeModel: def __init__(self): from cellpose import core self.use_GPU = core.use_gpu() print('>>> GPU activated? %d' % self.use_GPU) self.cached_model_type = None self.model = None def _load_model(self, model_type): from cellpose import models if self.model is None or model_type != self.cached_model_type: print(f'Loading model: {model_type}') self.model = models.Cellpose(gpu=self.use_GPU, model_type=model_type) self.cached_model_type = model_type else: print(f'Reusing cached model: {model_type}') return self.model def predict(self, images: list[np.ndarray], channels=None, diameter=None, flow_threshold=None, model_type='cyto3'): model = self._load_model(model_type) if channels is None: channels = [[2, 3]] * len(images) masks, flows, styles, diams = model.eval(images, diameter=diameter, flow_threshold=flow_threshold, channels=channels) results = { 'masks': [mask.tolist() for mask in masks], 'diameters': diams } return results def train(self, images, labels, config): raise NotImplementedError("Training functionality not implemented yet") api.export(CellposeModel)
- This file will contain the main logic for your Compute App. Here’s an example using the Cellpose model:
-
Create the
manifest.yaml
File:- This file contains metadata and configuration for your app:
name: Cellpose id: cellpose description: Cellpose is a generalist algorithm for cell and nucleus segmentation runtime: ray entrypoint: __init__.py ray_serve_config: ray_actor_options: num_gpus: 1 runtime_env: pip: - opencv-python-headless==4.2.0.34 - cellpose==3.0.11 - torch==2.3.1 - torchvision==0.18.1 autoscaling_config: downscale_delay_s: 1 min_replicas: 0 max_replicas: 2
- This file contains metadata and configuration for your app:
UI Apps are web-based interfaces that interact with the Compute Apps. In this example, we’ll create a simple ImJoy plugin for image visualization and interaction with the Compute App.
-
Create a New File for the UI App:
- In the
my-bioengine-app
folder, create a file namedimage_viewer.imjoy.html
:<config lang="json"> { "name": "Image Viewer", "type": "window", "version": "0.1.0" } </config> <script lang="javascript"> class ImJoyPlugin { async setup() { await api.log("Plugin initialized"); } async run() { await api.alert("Hello from ImJoy!"); } } api.export(new ImJoyPlugin()); </script> <window> <div> <h1>Hello, World!</h1> <p>Welcome to your first ImJoy plugin.</p> </div> </window>
- In the
-
Connect the UI App to the Compute App:
- Modify the
image_viewer.imjoy.html
file to connect to the Cellpose Compute App:<script lang="javascript"> class ImJoyPlugin { async setup() { this.server = await hyphaWebsocketClient.connectToServer({"server_url": "https://ai.imjoy.io"}); this.cellposeService = await this.server.getService("cellpose"); await api.log("Plugin initialized"); } async run() { const result = await this.cellposeService.predict({images: [/* image data */]}); await api.alert("Segmentation result: " + JSON.stringify(result)); } } api.export(new ImJoyPlugin()); </script>
- Modify the
-
Run the Compute App:
- If you're running the Compute App locally:
python __init__.py
- If you're running the Compute App locally:
-
Test the UI App:
- Open the ImJoy web app: ImJoy.io
- Drag and drop the
image_viewer.imjoy.html
file into the ImJoy interface. - Run the plugin and check the console for outputs.
-
Prepare Your Files for Submission:
- Ensure that your
__init__.py
andmanifest.yaml
files are complete and tested.
- Ensure that your
-
Create a Pull Request:
- Push your changes to a new branch in your forked GitHub repository.
- Create a pull request (PR) to the main BioEngine repository with your new app folder.
-
Submit Your PR:
- Provide a detailed description of your app, including its functionality, dependencies, and any special instructions.
- The BioEngine team will review your submission and provide feedback or merge it into the main repository.
Congratulations! You've created and submitted a BioEngine App. This tutorial has guided you through the development of both UI and Compute Apps, from setup to submission. By contributing to BioEngine, you’re helping to expand a powerful platform for bioimage analysis and AI-driven research.
If you have any questions or need further assistance, feel free to reach out to the BioEngine community on GitHub or through our support channels. Happy coding!