starmoth


Namestarmoth JSON
Version 0.10.1 PyPI version JSON
download
home_pageNone
SummaryA small wrapper library to help test systems using STAR
upload_time2024-09-24 13:07:34
maintainerNone
docs_urlNone
authorGudjon Magnusson
requires_python<4,>=3.7
licenseproprietary
keywords fraunhofer star testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# MOdel Test Harness (Moth)

A simple way to interrogate your AI model from a separate testing application.

# Client

## Simple classification model client.
``` python
from moth import Moth
from moth.message import ImagePromptMsg, ClassificationResultMsg, HandshakeTaskTypes

moth = Moth("my-ai", task_type=HandshakeTaskTypes.CLASSIFICATION)

@moth.prompt
def on_prompt(prompt: ImagePromptMsg):
    # TODO: Do smart AI here
    return ClassificationResultMsg(prompt_id=prompt.id, class_name="cat") # Most pictures are cat pictures 

moth.run()
```

ClassificationResultMsg can optionally include a confidence value

``` python
ClassificationResultMsg(prompt_id=prompt.id, class_name="cat", confidence=0.9)
```

## Simple object detection model client.
``` python
from moth import Moth
from moth.message import (
    ImagePromptMsg,
    ObjectDetectionResultMsg,
    ObjectDetectionResult,
    HandshakeTaskTypes,
)

moth = Moth("my-ai", task_type=HandshakeTaskTypes.OBJECT_DETECTION)


@moth.prompt
def on_prompt(prompt: ImagePromptMsg):
    # TODO: Do smart AI here
    # Make a list of ObjectDetectionResults
    results = []
    results.append(
        ObjectDetectionResult(
            0,
            0,
            50,
            50,
            class_name="cat",
            class_index=0,
            confidence=0.9,  # Optional confidence
        )
    )
    results.append(
        ObjectDetectionResult(
            10,
            10,
            50,
            35,
            class_name="dog",
            class_index=1,
            confidence=0.1,  # Optional confidence
        )
    )
    return ObjectDetectionResultMsg(
        prompt_id=prompt.id, object_detection_results=results
    )


moth.run()

```

## Simple segmentation model client.
``` python
from moth import Moth
from moth.message import (
    ImagePromptMsg,
    SegmentationResultMsg,
    SegmentationResult,
    HandshakeTaskTypes,
)

moth = Moth("my-ai", task_type=HandshakeTaskTypes.SEGMENTATION)


@moth.prompt
def on_prompt(prompt: ImagePromptMsg):
    # TODO: Do smart AI here
    # Make a list of ObjectDetectionResults
    results = []
    results.append(
        SegmentationResult(
            [0, 0, 50, 50, 20, 20, 0, 0],  # The predicted polygon
            class_name="cat",
            class_index=0,
            confidence=0.9,  # Optional confidence
        )
    )
    results.append(
        SegmentationResult(
            [0, 0, 50, 50, 13, 20, 0, 0],  # The predicted polygon
            class_name="dog",
            class_index=1,
            confidence=0.1,  # Optional confidence
        )
    )
    return SegmentationResultMsg(prompt_id=prompt.id, results=results)


moth.run()
```

## Mask to polygon conversion
Easily convert a binary mask to a polygon using the convert_mask_to_contour function from the `moth.utils` module.

### Usage
1. Import the function:
    ``` python
    from moth.utils import convert_mask_to_contour
    ```
2. Prepare Your Mask: Ensure your mask is a 2D NumPy array where regions of interest are marked with 1s (or 255 for 8-bit images) and the background is 0.
3. Convert the mask:
    ``` python
    polygon = convert_mask_to_contour(mask)
    ```
### Example
``` python
from moth.utils import convert_mask_to_contour
import numpy as np

# Example binary mask
mask = np.array([
    [0, 0, 0, 0, 0],
    [0, 1, 1, 1, 0],
    [0, 1, 1, 1, 0],
    [0, 0, 0, 0, 0]
], dtype=np.uint8)

# Convert the mask to a polygon
polygon = convert_mask_to_contour(mask)

# Output the polygon
print(polygon)
```

## Client Output Classes
Define the set of output classes that your model can predict. This information is sent to the server so it knows the possible prediction classes of the model. This is recommended to ensure the model is not penalized for classes it cannot output:
``` python
moth = Moth("my-ai", task_type=HandshakeTaskTypes.CLASSIFICATION, output_classes=["cat", "dog"])
```
By specifying these output classes, the server can accurately assess the model's performance based on its intended capabilities, preventing incorrect evaluation against classes it is not designed to predict.


# Server

## Simple server.
``` python
from moth.server import Server
from moth.message import HandshakeMsg

class ModelDriverImpl(ModelDriver):
    # TODO: Implement your model driver here
    pass

server = Server(7171)

@server.driver_factory
def handle_handshake(handshake: HandshakeMsg) -> ModelDriver
    return ModelDriverImpl()

server.start()
```
## Subscribe to model changes
Track changes to the list of connected models:
``` python
from moth.server import Model

@server.on_model_change
def handle_model_change(model_list: List[Model]):
    print(f"Connected models: {model_list}")
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "starmoth",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.7",
    "maintainer_email": null,
    "keywords": "Fraunhofer, STAR, testing",
    "author": "Gudjon Magnusson",
    "author_email": "gmagnusson@fraunhofer.org",
    "download_url": "https://files.pythonhosted.org/packages/b5/42/dfab24811c5127a9ba4b1eef7db78b74ec8e6c6c78123a43ddc8175a0cab/starmoth-0.10.1.tar.gz",
    "platform": null,
    "description": "\n# MOdel Test Harness (Moth)\n\nA simple way to interrogate your AI model from a separate testing application.\n\n# Client\n\n## Simple classification model client.\n``` python\nfrom moth import Moth\nfrom moth.message import ImagePromptMsg, ClassificationResultMsg, HandshakeTaskTypes\n\nmoth = Moth(\"my-ai\", task_type=HandshakeTaskTypes.CLASSIFICATION)\n\n@moth.prompt\ndef on_prompt(prompt: ImagePromptMsg):\n    # TODO: Do smart AI here\n    return ClassificationResultMsg(prompt_id=prompt.id, class_name=\"cat\") # Most pictures are cat pictures \n\nmoth.run()\n```\n\nClassificationResultMsg can optionally include a confidence value\n\n``` python\nClassificationResultMsg(prompt_id=prompt.id, class_name=\"cat\", confidence=0.9)\n```\n\n## Simple object detection model client.\n``` python\nfrom moth import Moth\nfrom moth.message import (\n    ImagePromptMsg,\n    ObjectDetectionResultMsg,\n    ObjectDetectionResult,\n    HandshakeTaskTypes,\n)\n\nmoth = Moth(\"my-ai\", task_type=HandshakeTaskTypes.OBJECT_DETECTION)\n\n\n@moth.prompt\ndef on_prompt(prompt: ImagePromptMsg):\n    # TODO: Do smart AI here\n    # Make a list of ObjectDetectionResults\n    results = []\n    results.append(\n        ObjectDetectionResult(\n            0,\n            0,\n            50,\n            50,\n            class_name=\"cat\",\n            class_index=0,\n            confidence=0.9,  # Optional confidence\n        )\n    )\n    results.append(\n        ObjectDetectionResult(\n            10,\n            10,\n            50,\n            35,\n            class_name=\"dog\",\n            class_index=1,\n            confidence=0.1,  # Optional confidence\n        )\n    )\n    return ObjectDetectionResultMsg(\n        prompt_id=prompt.id, object_detection_results=results\n    )\n\n\nmoth.run()\n\n```\n\n## Simple segmentation model client.\n``` python\nfrom moth import Moth\nfrom moth.message import (\n    ImagePromptMsg,\n    SegmentationResultMsg,\n    SegmentationResult,\n    HandshakeTaskTypes,\n)\n\nmoth = Moth(\"my-ai\", task_type=HandshakeTaskTypes.SEGMENTATION)\n\n\n@moth.prompt\ndef on_prompt(prompt: ImagePromptMsg):\n    # TODO: Do smart AI here\n    # Make a list of ObjectDetectionResults\n    results = []\n    results.append(\n        SegmentationResult(\n            [0, 0, 50, 50, 20, 20, 0, 0],  # The predicted polygon\n            class_name=\"cat\",\n            class_index=0,\n            confidence=0.9,  # Optional confidence\n        )\n    )\n    results.append(\n        SegmentationResult(\n            [0, 0, 50, 50, 13, 20, 0, 0],  # The predicted polygon\n            class_name=\"dog\",\n            class_index=1,\n            confidence=0.1,  # Optional confidence\n        )\n    )\n    return SegmentationResultMsg(prompt_id=prompt.id, results=results)\n\n\nmoth.run()\n```\n\n## Mask to polygon conversion\nEasily convert a binary mask to a polygon using the convert_mask_to_contour function from the `moth.utils` module.\n\n### Usage\n1. Import the function:\n    ``` python\n    from moth.utils import convert_mask_to_contour\n    ```\n2. Prepare Your Mask: Ensure your mask is a 2D NumPy array where regions of interest are marked with 1s (or 255 for 8-bit images) and the background is 0.\n3. Convert the mask:\n    ``` python\n    polygon = convert_mask_to_contour(mask)\n    ```\n### Example\n``` python\nfrom moth.utils import convert_mask_to_contour\nimport numpy as np\n\n# Example binary mask\nmask = np.array([\n    [0, 0, 0, 0, 0],\n    [0, 1, 1, 1, 0],\n    [0, 1, 1, 1, 0],\n    [0, 0, 0, 0, 0]\n], dtype=np.uint8)\n\n# Convert the mask to a polygon\npolygon = convert_mask_to_contour(mask)\n\n# Output the polygon\nprint(polygon)\n```\n\n## Client Output Classes\nDefine the set of output classes that your model can predict. This information is sent to the server so it knows the possible prediction classes of the model. This is recommended to ensure the model is not penalized for classes it cannot output:\n``` python\nmoth = Moth(\"my-ai\", task_type=HandshakeTaskTypes.CLASSIFICATION, output_classes=[\"cat\", \"dog\"])\n```\nBy specifying these output classes, the server can accurately assess the model's performance based on its intended capabilities, preventing incorrect evaluation against classes it is not designed to predict.\n\n\n# Server\n\n## Simple server.\n``` python\nfrom moth.server import Server\nfrom moth.message import HandshakeMsg\n\nclass ModelDriverImpl(ModelDriver):\n    # TODO: Implement your model driver here\n    pass\n\nserver = Server(7171)\n\n@server.driver_factory\ndef handle_handshake(handshake: HandshakeMsg) -> ModelDriver\n    return ModelDriverImpl()\n\nserver.start()\n```\n## Subscribe to model changes\nTrack changes to the list of connected models:\n``` python\nfrom moth.server import Model\n\n@server.on_model_change\ndef handle_model_change(model_list: List[Model]):\n    print(f\"Connected models: {model_list}\")\n```\n",
    "bugtrack_url": null,
    "license": "proprietary",
    "summary": "A small wrapper library to help test systems using STAR",
    "version": "0.10.1",
    "project_urls": null,
    "split_keywords": [
        "fraunhofer",
        " star",
        " testing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9157a420becd9c3da2af8a604a13374c38d730c93cb5602717c7b0be82dbd867",
                "md5": "17badd604d7e40c37afc1da8e5f84ced",
                "sha256": "ce7d5d62c9d674ffa73c46c4a3475fb11c6f7bf1325b45dfec977ab131e0ba4d"
            },
            "downloads": -1,
            "filename": "starmoth-0.10.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "17badd604d7e40c37afc1da8e5f84ced",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.7",
            "size": 12331,
            "upload_time": "2024-09-24T13:07:33",
            "upload_time_iso_8601": "2024-09-24T13:07:33.797685Z",
            "url": "https://files.pythonhosted.org/packages/91/57/a420becd9c3da2af8a604a13374c38d730c93cb5602717c7b0be82dbd867/starmoth-0.10.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b542dfab24811c5127a9ba4b1eef7db78b74ec8e6c6c78123a43ddc8175a0cab",
                "md5": "2f4ef8792da3435f3656a58d1c91fd30",
                "sha256": "c880659b59bff971fa138d02909af7728c8659cb54525c7f58285c86e440ab8e"
            },
            "downloads": -1,
            "filename": "starmoth-0.10.1.tar.gz",
            "has_sig": false,
            "md5_digest": "2f4ef8792da3435f3656a58d1c91fd30",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.7",
            "size": 11925,
            "upload_time": "2024-09-24T13:07:34",
            "upload_time_iso_8601": "2024-09-24T13:07:34.709329Z",
            "url": "https://files.pythonhosted.org/packages/b5/42/dfab24811c5127a9ba4b1eef7db78b74ec8e6c6c78123a43ddc8175a0cab/starmoth-0.10.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-24 13:07:34",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "starmoth"
}
        
Elapsed time: 0.34441s