Metadata-Version: 2.4
Name: scentience
Version: 0.2.0
Summary: Python client for Scentience olfaction instruments over BLE Bluetooth
Project-URL: Documentation, https://scentience.github.io/docs-api/ble-api
Project-URL: Repository, https://github.com/scentience/scentience-pypi
License: MIT License
        
        Copyright (c) 2026 Scentience
        
        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: ble,bluetooth,chemical-sensing,olfaction,scentience,sensors
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
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.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: System :: Hardware
Requires-Python: >=3.8
Requires-Dist: bleak>=0.21.0
Provides-Extra: models
Requires-Dist: huggingface-hub>=0.16.0; extra == 'models'
Requires-Dist: numpy>=1.24.0; extra == 'models'
Requires-Dist: pillow>=9.0.0; extra == 'models'
Requires-Dist: torch>=2.0.0; extra == 'models'
Requires-Dist: torchvision>=0.15.0; extra == 'models'
Requires-Dist: transformers>=4.30.0; extra == 'models'
Description-Content-Type: text/markdown

# scentience

Python client for [Scentience](https://scentience.github.io) olfaction instruments over BLE Bluetooth.

## Installation

BLE only:
```bash
pip install scentience
```

BLE + COLIP embedding models:
```bash
pip install "scentience[models]"
```

## Requirements

- Python 3.8+
- Bluetooth 4.0+ adapter
- A Scentience developer API key (obtain from the Scentience portal)
- For embedding models: `torch`, `torchvision`, `transformers`, `huggingface-hub`, `Pillow` (installed via `[models]` extra)

## Quick start

```python
import scentience as scn

# Initialize with your developer API key
device = scn.ScentienceDevice(api_key="YOUR_API_KEY")

# Connect via BLE (scans for the first available Scentience device)
device.connect_ble(char_uuid="YOUR_CHAR_UUID")

# Take a single reading
data = device.sample_ble()
print(data)

# Start continuous streaming (non-blocking)
device.stream_ble(callback=lambda d: print(d))

# Stop streaming and disconnect
device.stop_stream()
device.disconnect()
```

## Targeting a specific device

Pass the device UID (serial number or BLE address) to connect to a particular instrument:

```python
device.connect_ble(char_uuid="YOUR_CHAR_UUID", device_uid="YOUR_DEVICE_UID")
```

## Context manager

```python
with scn.ScentienceDevice(api_key="YOUR_API_KEY") as device:
    device.connect_ble(char_uuid="YOUR_CHAR_UUID")
    data = device.sample_ble()
```

## Response payload

`sample_ble()` and the streaming callback receive a dict that may contain:

| Key | Description |
|-----|-------------|
| `UID` | Device serial number |
| `TIMESTAMP` | Reading timestamp |
| `ENV_temperatureC` | Ambient temperature (°C) |
| `ENV_humidity` | Relative humidity (%) |
| `ENV_pressureHpa` | Barometric pressure (hPa) |
| `BATT_health` | Battery health |
| `BATT_v` | Battery voltage |
| `BATT_charge` | Battery charge (%) |
| `BATT_time` | Estimated battery time remaining |
| `STATUS_opuA` | Operational status |
| `CO2`, `NH3`, `NO`, `NO2`, `CO`, `C2H5OH` | Chemical compounds (non-zero only) |
| `H2`, `CH4`, `C3H8`, `C4H10`, `H2S`, `HCHO`, `SO2`, `VOC` | Chemical compounds (non-zero only) |

## Logging and export

All readings from `sample_ble()` and `stream_ble()` are automatically buffered in memory.

```python
# Access the in-memory log at any time
print(device.log)          # list of dicts

# Export to JSON
device.export_json("readings.json")

# Export to CSV
device.export_csv("readings.csv")

# Clear the buffer
device.clear_log()
```

The CSV uses `UID` and `TIMESTAMP` as the first columns (when present), followed by all other fields in alphabetical order.  Readings that are missing a field are written with an empty value for that column.

## COLIP embedding models

`ColipModel` integrates the [Olfaction-Vision-Language Embeddings](https://huggingface.co/kordelfrance/Olfaction-Vision-Language-Embeddings) to produce joint embeddings across olfaction, vision, and language modalities.

Four variants are available:

| Variant | Embed dim | Architecture | Best for |
|---------|-----------|--------------|----------|
| `colip-small-base` | 512 | base GNN | Fast inference / edge devices |
| `colip-small-gat`  | 512 | GAT        | Higher accuracy on edge devices |
| `colip-large-base` | 2048 | base GNN | Accuracy-critical tasks |
| `colip-large-gat`  | 2048 | GAT        | Highest accuracy, slower inference |

Weights are downloaded from HuggingFace Hub and cached locally on first use.

### Example — small base model

```python
import scentience as scn
from PIL import Image

# Load the small base model (512-dim embeddings, fastest)
model = scn.ColipModel.from_pretrained("colip-small-base")
print(model)
# ColipModel(variant='colip-small-base', embed_dim=512, device='cpu')

image   = Image.open("scene.jpg")
olf_vec = [0.0] * 138          # 138-dimensional olfactory descriptor

# Returns a torch.Tensor of shape (512,)
embedding = model.embed(image, olf_vec)

# Or as a NumPy array
arr = model.embed_numpy(image, olf_vec)
```

### Example — large GAT model

```python
import scentience as scn
from PIL import Image

# Load the large GAT model (2048-dim embeddings, highest accuracy)
model = scn.ColipModel.from_pretrained("colip-large-gat")

image   = Image.open("scene.jpg")
olf_vec = [0.0] * 138

# Returns a torch.Tensor of shape (2048,)
embedding = model.embed(image, olf_vec)
```

### Example — combined BLE streaming + real-time embedding

```python
import scentience as scn
from PIL import Image

model  = scn.ColipModel.from_pretrained("colip-small-base")
device = scn.ScentienceDevice(api_key="YOUR_API_KEY")
device.connect_ble(char_uuid="YOUR_CHAR_UUID")

scene = Image.open("scene.jpg")

def on_sample(data):
    olf_vec   = [data.get(k, 0.0) for k in sorted(data.keys()) if k not in ("UID", "TIMESTAMP")]
    embedding = model.embed(scene, olf_vec)
    print(f"embedding shape: {embedding.shape}, norm: {embedding.norm():.4f}")

device.stream_ble(callback=on_sample)
```

### Targeting a specific device or compute backend

```python
# Target GPU explicitly
model = scn.ColipModel.from_pretrained("colip-large-base", device="cuda")

# Or use the constructor directly
model = scn.ColipModel(variant="colip-small-gat", device="mps")
```

## API reference

See the full [BLE API documentation](https://scentience.github.io/docs-api/ble-api).

## License

MIT
