# Quickstart

## Install heartKIT

We provide several installation methods including pip, uv, and Docker. Install __heartKIT__ via pip/uv for the latest stable release or by cloning the GitHub repo for the most up-to-date. Additionally, a [VSCode Dev Container](https://code.visualstudio.com/docs/devcontainers/containers) is available and defined in [./.devcontainer](https://github.com/AmbiqAI/heartkit/tree/main/.devcontainer) to run in an isolated Docker environment.

Clone the repository if you are interested in contributing to the development or wish to experiment with the latest source code. After cloning, navigate into the directory and install the package. In this mode, uv is recommended.

```bash title="Terminal"
# Clone the repository
git clone https://github.com/AmbiqAI/heartkit.git

# Navigate to the cloned directory
cd heartkit

# Install the package in editable mode for development
uv sync
```

When using editable mode via uv, be sure to activate the python environment: `source .venv/bin/activate`.
On Windows using Powershell, use `.venv\Scripts\activate`.

Install the heartKIT package using pip or uv.
Visit the Python Package Index (PyPI) for more details on the package: [https://pypi.org/project/heartkit/](https://pypi.org/project/heartkit/)

```bash title="Terminal"
# Install with pip
pip install heartkit
```

Or, if you prefer to use uv, you can install the package with the following command:

```bash title="Terminal"
# Install with uv
uv add heartkit
```

Alternatively, you can install the latest development version directly from the GitHub repository. Make sure to have the Git command-line tool installed on your system. The @main command installs the main branch and may be modified to another branch, i.e. @canary.

```bash title="Terminal"
pip install git+https://github.com/AmbiqAI/heartkit.git@main
```

Or, using uv:

```bash title="Terminal"
uv add git+https://github.com/AmbiqAI/heartkit.git@main
```

## Requirements

* [Python 3.12–3.13](https://www.python.org)
* [uv ^0.7.10+](https://docs.astral.sh/uv/getting-started/installation/)

Check the project's [pyproject.toml](https://github.com/AmbiqAI/heartkit/blob/main/pyproject.toml) file for a list of up-to-date Python dependencies. Note that the installation methods above install all required dependencies. The following are optional dependencies only needed when running `demo` command using Ambiq's evaluation board (`EVB`) backend:

* [Arm GNU Toolchain ^12.2](https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads)
* [Segger J-Link ^7.92](https://www.segger.com/downloads/jlink/)

Once installed, __heartKIT__ can be used as either a CLI-based tool or as a Python package to perform advanced experimentation.

---

## Use heartKIT with CLI

The heartKIT command line interface (CLI) allows for simple single-line commands to download datasets, train models, evaluate performance, and export models. The CLI requires no customization or Python code. You can simply run all the built-in tasks from the terminal with the __heartkit__ command. Check out the [CLI Guide](https://ambiqai.github.io/heartkit/usage/cli/) to learn more about available options.

Heartkit commands use the following syntax:

```bash title="Terminal"
heartkit --mode [MODE] --task [TASK] --config [CONFIG]
```

Or using short flags:

```bash title="Terminal"
heartkit -m [MODE] -t [TASK] -c [CONFIG]
```

Where:

* `MODE` is one of `download`, `train`, `evaluate`, `export`, or `demo`
* `TASK` is one of `segmentation`, `rhythm`, `beat`, or `denoise`
* `CONFIG` is configuration as JSON content or file path

Download datasets specified in the configuration file.

```bash title="Terminal"
heartkit -m download -c ./download-datasets.json
```

Train a rhythm model using the supplied configuration file.

```bash title="Terminal"
heartkit -m train -t rhythm -c ./configuration.json
```

Evaluate the trained rhythm model using the supplied configuration file.

```bash title="Terminal"
heartkit -m evaluate -t rhythm  -c ./configuration.json
```

Run demo on trained rhythm model using the supplied configuration file.

```bash title="Terminal"
heartkit -m demo -t rhythm -c ./configuration.json
```

## Use heartKIT with Python

The __heartKIT__ Python package allows for more fine-grained control and customization. You can use the package to train, evaluate, and deploy models for a variety of tasks. You can create custom datasets, models, and tasks and register them with corresponding factories and use them like built-in tasks.

For example, you can create a custom task, train it, evaluate its performance on a validation set, and even export a quantized TensorFlow Lite model for deployment. Check out the [Python Guide](https://ambiqai.github.io/heartkit/usage/python/) to learn more about using heartKIT as a Python package.

```py title="Python example" linenums="1"

import heartkit as hk

params = hk.HKTaskParams(...)

task = hk.TaskFactory.get("rhythm")

task.download(params)  # Download dataset(s)

task.train(params)  # Train the model

task.evaluate(params)  # Evaluate the model

task.export(params)  # Export to TFLite

```

**Configuration parameters**
<ConfigExample code= lang="python" filename="quickstart-13.py" download="/heartkit/examples/quickstart-13.py">

```py title="quickstart-13.py"

hk.HKTaskParams(
    name="arr-2-eff-sm",
    project="hk-rhythm-2",
    job_dir="./results/arr-2-eff-sm",
    verbose=2,
    datasets=[hk.NamedParams(
        name="ptbxl",
        params=dict(
            path="./datasets/ptbxl"
        )
    )],
    num_classes=2,
    class_map={
        "0": 0,
        "7": 1,
        "8": 1
    },
    class_names=[
        "NORMAL", "AFIB/AFL"
    ],
    class_weights="balanced",
    sampling_rate=100,
    frame_size=512,
    samples_per_patient=[10, 10],
    val_samples_per_patient=[5, 5],
    test_samples_per_patient=[5, 5],
    val_patients=0.20,
    val_size=20000,
    test_size=20000,
    batch_size=256,
    buffer_size=20000,
    epochs=100,
    steps_per_epoch=50,
    val_metric="loss",
    lr_rate=1e-3,
    lr_cycles=1,
    threshold=0.75,
    val_metric_threshold=0.98,
    tflm_var_name="g_rhythm_model",
    tflm_file="rhythm_model_buffer.h",
    backend="pc",
    demo_size=896,
    display_report=True,
    quantization=hk.QuantizationParams(
        qat=False,
        format="INT8",
        io_type="int8",
        conversion="CONCRETE",
        debug=False
    ),
    preprocesses=[
        hk.NamedParams(
            name="layer_norm",
            params=dict(
                epsilon=0.01,
                name="znorm"
            )
        )
    ],
    augmentations=[
    ],
    model_file="model.keras",
    use_logits=False,
    architecture=hk.NamedParams(
        name="efficientnetv2",
        params=dict(
            input_filters=16,
            input_kernel_size=[1, 9],
            input_strides=[1, 2],
            blocks=[
                {"filters": 24, "depth": 2, "kernel_size": [1, 9], "strides": [1, 2], "ex_ratio": 1,  "se_ratio": 2},
                {"filters": 32, "depth": 2, "kernel_size": [1, 9], "strides": [1, 2], "ex_ratio": 1,  "se_ratio": 2},
                {"filters": 40, "depth": 2, "kernel_size": [1, 9], "strides": [1, 2], "ex_ratio": 1,  "se_ratio": 2},
                {"filters": 48, "depth": 1, "kernel_size": [1, 9], "strides": [1, 2], "ex_ratio": 1,  "se_ratio": 2}
            ],
            output_filters=0,
            include_top=True,
            use_logits=True
        )
    }
)
```

---
