Metadata-Version: 2.4
Name: neuroencoder
Version: 2.0.0
Summary: EEG embeddings with EPI-300K: hosted API client and local model
Author-email: Neuroencoder <contact@neuroencoder.com>
License: Apache-2.0
Project-URL: Homepage, https://neuroencoder.com
Project-URL: Documentation, https://docs.neuroencoder.com
Project-URL: API reference, https://docs.neuroencoder.com/api
Keywords: eeg,embeddings,foundation-model,neuroscience,sleep-staging,seizure-detection,mne,brain
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Healthcare Industry
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: numpy
Requires-Dist: httpx>=0.25
Provides-Extra: local
Requires-Dist: torch>=2.1; extra == "local"
Requires-Dist: scipy; extra == "local"
Requires-Dist: mne>=1.5; extra == "local"
Provides-Extra: viz
Requires-Dist: matplotlib; extra == "viz"
Requires-Dist: umap-learn; extra == "viz"
Requires-Dist: scikit-learn; extra == "viz"
Requires-Dist: pandas; extra == "viz"
Requires-Dist: embedding-atlas>=0.13; extra == "viz"
Requires-Dist: anywidget; extra == "viz"
Provides-Extra: all
Requires-Dist: neuroencoder[local,viz]; extra == "all"
Provides-Extra: dev
Requires-Dist: neuroencoder[local,viz]; extra == "dev"
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: respx; extra == "dev"

# neuroencoder

EEG embeddings from EPI-300K, an EEG foundation model. Each 30 s window of a recording becomes one
768-d L2-normalised vector. Hosted API client and local model in one package, OpenAI-style interface.

## Install

```bash
pip install neuroencoder              # hosted API client (numpy + httpx only)
pip install "neuroencoder[local]"     # + local model and EDF/FIF reading (torch, scipy, mne)
pip install "neuroencoder[viz]"       # + ne.plot / ne.explore / ne.serve / ne.export
```

Python 3.10+.

## Hosted quickstart

Get a key (a single-use link is emailed to you; opening it shows the key once):

```bash
curl -X POST https://api.neuroencoder.com/v1/signup -d '{"email":"you@lab.org"}'
export NEUROENCODER_API_KEY=ne_...
```

```python
from neuroencoder import embeddings

r = embeddings(eeg, sfreq=256, channel_names=["Fp1", "Fp2", "C3", "C4", "O1", "O2"])  # eeg: [channels, samples]
r = embeddings("night.edf")            # EDF/BDF/FIF/NPY path or open file, or an mne Raw
r.to_numpy()                           # [n_windows, 768]
r.t_start                              # window start times in seconds
r.channels_used                        # per input channel: label, region it was mapped to (or None), bipolar flag
r.usage                                # seconds_of_eeg, windows, month_seconds_of_eeg
```

One window every `stride_seconds` (default 30, non-overlapping; minimum 0.5 for a sliding window).
The explicit client is `Neuroencoder(api_key=...).embeddings.create(...)` with the same arguments.
Errors carry `code`, `param` and a `fix` string.

## Local model

```python
from neuroencoder import EPI
model = EPI.from_checkpoint("epi300k.pt", device="cpu")
E = model.embed(eeg, sfreq=256, channel_names=ch_names)                 # numpy [n_windows, 768]
```

The weights are released with the paper and are not downloadable yet; until then use the hosted API.
If you have `epi300k.pt`, set `NEUROENCODER_WEIGHTS=/path/to/epi300k.pt` (or put it at
`~/.cache/neuroencoder/epi300k.pt`) and `embeddings(...)` runs locally whenever `NEUROENCODER_API_KEY`
is unset. On CUDA the local model runs in bf16; the hosted API runs fp32 on CPU (on an 8 h night: at most
3.3e-3 per component, cosine at least 0.9998).

## Channel names

Channels are averaged into six scalp regions (frontal, central, left/right temporal, parietal,
occipital) by their 10-20/10-10 labels, so any montage works. Recognised forms:

| Input | Mapped as |
| --- | --- |
| `Fp1`, `C3`, `O2`, `EEG C3`, `POL Fp1` | the electrode |
| `FP1-REF`, `C3-LE`, `C3-AVG`, `C3-M2`, `O1-A1` | the electrode before the reference |
| `FP1-F7`, `EEG Fpz-Cz` (bipolar) | region of the first electrode, flagged `bipolar` |
| `EOG ...`, `ECG` | recorded as EOG/ECG; not used by the model |
| `EMG ...`, anything else | ignored, with a warning |

If no channel maps to a scalp region the call raises `InvalidInputError` naming the labels. Check
`r.channels_used` to see what was used. Without `channel_names`, an 8-row array is taken as the
regions in order (Frontal, Central, Temporal L, Temporal R, Parietal, Occipital, EOG, ECG).

## Limits

- `sfreq` 1 to 20000 Hz, resampled to 250 Hz; 1-100 Hz bandpass and 50/100 Hz notch unless `filter=False`.
- Input must be finite (NaN/inf raise) and at least 0.5 s long. Inputs shorter than 30 s give one
  embedding of the available signal; trailing data shorter than one window is not embedded.
- Hosted: the server takes at most `neuroencoder.MAX_WINDOWS_PER_REQUEST` (1024) windows per request. The client
  sends longer inputs as consecutive requests of at most 256 windows and 32 MiB each and merges them, so one call
  returns the same windows, `t_start` and `usage` as a single request would. Pieces are embedded independently:
  the window on each side of a boundary differs slightly from a single request (cosine 0.9986 or higher on two
  Sleep-EDF recordings; other windows are unchanged).
  Each piece is billed as it completes; if one fails after retries, the error says how many requests and windows
  were already billed. EDF/BDF/FIF files over 32 MiB are read locally with mne (`pip install mne`, or the `local` extra) and split;
  without mne they are uploaded whole. `neuroencoder.n_windows(n_samples, sfreq, stride_seconds)` gives the window
  count in advance. Monthly quota is in seconds of EEG (30 s per window).
- Retries (`max_retries=2`): connection failures, 429 `rate_limit_exceeded` and 503, honouring `Retry-After` up to
  60 s. Every embeddings request carries an `Idempotency-Key`, so timeouts, connections lost after sending and
  500/502/504 are retried too, with the same key; the server does not bill a repeated request twice.
  Quota errors and other 4xx are not retried.

## Migrating from 1.x

2.0 replaces the 1.x image model. `MRL.from_pretrained()`, `predict(images, dim=...)`, Matryoshka
dimensions and HuggingFace gated access are gone. Use `embeddings(...)` (hosted or local) or
`EPI.from_checkpoint(...).embed(...)`; embeddings are 768-d. `MRL` remains as an alias of `EPI`.
`preprocess()` now returns non-overlapping 30 s windows by default (`stride_seconds=30`; previously 1).
