Metadata-Version: 2.4
Name: sawmil
Version: 0.2.1
Summary: Open-source Python library providing a collection of Support Vector Machine (SVM) classifiers for multiple-instance learning (MIL): SVM, NSK, sMIL and sAwMIL.
Project-URL: Homepage, https://github.com/carlomarxdk/sawmil
Project-URL: Documentation, https://sawmil.readthedocs.io/en/latest/
Project-URL: Issues, https://github.com/carlomarxdk/sawmil/issues
Project-URL: Paper, https://arxiv.org/abs/2506.23921
Project-URL: DOI, https://doi.org/10.5281/zenodo.16990499
Project-URL: Citation, https://github.com/carlomarxdk/sawmil/blob/main/citation.cff
Author: Germans Savcisens, Tina Eliassi-Rad
License: MIT License
        
        Copyright (c) 2025 Germans Savcisens and Tina Eliassi-Rad
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: MIL,bag,kernels,multiple-instance-learning,sawmil,sparse,svm
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: numpy>=1.22
Requires-Dist: scikit-learn>=1.2
Requires-Dist: scipy>=1.16.1
Provides-Extra: daqp
Requires-Dist: daqp>=0.5; extra == 'daqp'
Provides-Extra: dev
Requires-Dist: build; extra == 'dev'
Requires-Dist: mypy; extra == 'dev'
Requires-Dist: pytest-cov; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: twine; extra == 'dev'
Provides-Extra: docs
Requires-Dist: mkdocs-bibtex; extra == 'docs'
Requires-Dist: mkdocs-gen-files>=0.5; extra == 'docs'
Requires-Dist: mkdocs-jupyter>=0.24; extra == 'docs'
Requires-Dist: mkdocs-literate-nav>=0.6; extra == 'docs'
Requires-Dist: mkdocs-material>=9.5; extra == 'docs'
Requires-Dist: mkdocs-section-index>=0.3; extra == 'docs'
Requires-Dist: mkdocs>=1.5; extra == 'docs'
Requires-Dist: mkdocstrings-python>=1.10; extra == 'docs'
Requires-Dist: mkdocstrings>=0.24; extra == 'docs'
Requires-Dist: ruff; extra == 'docs'
Provides-Extra: full
Requires-Dist: gurobipy>=12.0.3; extra == 'full'
Requires-Dist: osqp>=1.0.4; extra == 'full'
Provides-Extra: gurobi
Requires-Dist: gurobipy>=12.0.3; extra == 'gurobi'
Provides-Extra: osqp
Requires-Dist: osqp>=1.0.4; extra == 'osqp'
Description-Content-Type: text/markdown

# Multiple-Instance Support Vector Machines in Python

[![PyPI - Version](https://img.shields.io/pypi/v/sawmil?style=flat-square)](https://pypi.org/project/sawmil/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sawmil?style=flat-square)](https://pypi.org/project/sawmil/)
[![PyPI - Status](https://img.shields.io/pypi/status/sawmil?style=flat-square)](https://pypi.org/project/sawmil/)
[![GitHub License](https://img.shields.io/github/license/carlomarxdk/sawmil?style=flat-square)](https://github.com/carlomarxdk/sawmil/blob/main/LICENSE)
[![Documentation](https://readthedocs.org/projects/sawmil/badge/?version=latest)](https://sawmil.readthedocs.io/en/latest/?badge=latest)
[![DOI](https://zenodo.org/badge/1046623935.svg)](https://doi.org/10.5281/zenodo.16990499)

`sAwMIL` (**S**parse **Aw**are **M**ultiple-**I**nstance **L**earning) is an open-source Python library providing a collection of Support Vector Machine (SVM) classifiers for multiple-instance learning (MIL). It builds upon ideas from the earlier [misvm](https://github.com/garydoranjr/misvm) package, adapting it for the latest Python version, as well as introducing new models.

In **Single-Instance Learning** (SIL), the dataset consists of pairs of an instance and a label:

$$
\langle \mathbf{x}_i, y_i \rangle \text{ , where } \mathbf{x}_i \in \mathbb{R}^{d} \text{ and } y_i \in \mathcal{Y}.
$$

In binary settings, the label is $y \in \{0,1\}$.
To solve this problem, we can use a standard [SVM](https://sawmil.readthedocs.io/en/latest/models/svm/) model.

In **Multiple-Instance Learning** (MIL), the dataset consists of *bags* of instances paired with a single bag-level label:

$$
\langle \mathbf{X}_i, y_i \rangle \text{ , where } \mathbf{X}_i = \{ \mathbf{x}_{1}, \mathbf{x}_{2}, ..., \mathbf{x}_{n_i} \}, \mathbf{x}_j \in \mathbb{R}^{d} \text{ and } y_i \in \mathcal{Y}.
$$

To solve this problem, we can use [NSK](https://sawmil.readthedocs.io/en/latest/models/nsk/) or [sMIL](https://sawmil.readthedocs.io/en/latest/models/sMIL/) models.

In some cases, each bag, along with the instances and a label, could contain a **intra-bag mask** that specifies which items are likely to contain the signal related to $y$. In that case, we have a triplet of $\langle \mathbf{X}_i, \mathbf{M}_i, y_i \rangle$, where

$$
 \mathbf{M}_i = \{m_1, m_1,... m_{n_i}\}, \text{ where } m_j \in \{0,1\}.
$$

To solve this problem, one can use the [sAwMIL](https://sawmil.readthedocs.io/en/latest/models/sAwMIL/) model.

## Installation

`sawmil` supports three QP backends:

* [Gurobi](https://gurobi.com)
* [OSQP](https://osqp.org/)
* [DAQP](https://darnstrom.github.io/daqp/)
  
By default, the base package installs **without** any solver; pick one (or both) via extras.

### Base package (no solver)

```bash
pip install sawmil
# it installs numpy>=1.22 and scikit-learn>=1.7.0
```

### Option 1: `Gurobi` backend

> Gurobi is commercial software. You’ll need a valid license (academic or commercial), refer to the [official website](https://gurobi.com).

```bash
pip install "sawmil[gurobi]"
# in additionl to the base packages, it install gurobi>12.0.3
```

### Option 2: `OSQP` backend

```bash
pip install "sawmil[osqp]"
# in additionl to the base packages, it installs osqp>=1.0.4 and scipy>=1.16.1
```

### Option 3: `DAQP` backend

```bash
pip install "sawmil[daqp]"
# in additionl to the base packages, it installs daqp>=0.5 and scipy>=1.16.1
```

### Option 4 — All supported solvers

```bash
pip install "sawmil[full]"
```


### Picking the solver in code

```python
from sawmil import SVM, RBF

k = RBF(gamma = 0.1)
# solver= "osqp" (default is "gurobi")
# SVM is for single-instances 
clf = SVM(C=1.0, 
          kernel=k, 
          solver="osqp").fit(X, y)
```

## Quick start

### 1. Generate Dummy Data

``` python
from sawmil.data import generate_dummy_bags
import numpy as np
rng = np.random.default_rng(0)

ds = generate_dummy_bags(
    n_pos=300, n_neg=100, inst_per_bag=(5, 15), d=2,
    pos_centers=((+2,+1), (+4,+3)),
    neg_centers=((-1.5,-1.0), (-3.0,+0.5)),
    pos_scales=((2.0, 0.6), (1.2, 0.8)),
    neg_scales=((1.5, 0.5), (2.5, 0.9)),
    pos_intra_rate=(0.25, 0.85),
    ensure_pos_in_every_pos_bag=True,
    neg_pos_noise_rate=(0.00, 0.05),
    pos_neg_noise_rate=(0.00, 0.20),
    outlier_rate=0.1,
    outlier_scale=8.0,
    random_state=42,
)
```

### 2. Fit `NSK` with RBF Kernel

**Load a kernel:**

```python
from sawmil.kernels import get_kernel, RBF
k1 = get_kernel("rbf", gamma=0.1)
k2 = RBF(gamma=0.1)
# k1 == k2

```

**Fit NSK Model:**

```python
from sawmil.nsk import NSK

clf = NSK(C=1, kernel=k, 
          # bag kernel settings
          normalizer='average',
          # solver params
          scale_C=True, 
          tol=1e-8, 
          verbose=False).fit(ds, None)
y = ds.y
print("Train acc:", clf.score(ds, y))
```

### 3. Fit `sMIL` Model with Linear Kernel

```python
from sawmil.smil import sMIL

k = get_kernel("linear") # base (single-instance kernel)
clf = sMIL(C=0.1, 
           kernel=k, 
           scale_C=True, 
           tol=1e-8, 
           verbose=False).fit(ds, None)
```

See more examples in the [`example.ipynb`](https://github.com/carlomarxdk/sawmil/blob/main/example.ipynb) notebook.

### 4. Fit `sAwMIL` with Combined Kernels

```python
from sawmil.kernels import Product, Polynomial, Linear, RBF, Sum, Scale
from sawmil.sawmil import sAwMIL

k = Sum(Linear(), 
        Scale(0.5, 
              Product(Polynomial(degree=2), RBF(gamma=1.0))))

clf = sAwMIL(C=0.1, 
             kernel=k,
             solver="gurobi", 
             eta=0.95) # here eta is high, since all items in the bag are relevant
clf.fit(ds)
print("Train acc:", clf.score(ds, ds.y))
```

## Citation

If you use `sawmil` package in academic work, please cite:

Savcisens, G. & Eliassi-Rad, T. *sAwMIL: Python package for Sparse Multiple-Instance Learning* (2025).

```bibtex
@software{savcisens2025sawmil,
  author = {Savcisens, Germans and Eliassi-Rad, Tina},
  title = {sAwMIL: Python package for Sparse Multiple-Instance Learning},
  year = {2025},
  doi = {10.5281/zenodo.16990499},
  url = {https://github.com/carlomarxdk/sawmil}
}
```

If you want to reference a specific version of the package, find the [correct DOI here](https://doi.org/10.5281/zenodo.16990499).
