Metadata-Version: 2.4
Name: cicada-nwb
Version: 0.5.0
Summary: NWB data access layer for CICADA — lazy read-only wrappers with no GUI or analysis dependencies
Project-URL: Documentation, https://cicada-analysis.readthedocs.io/
Project-URL: Repository, https://gitlab.com/cossartlab/cicada_nwb
Project-URL: Issues, https://gitlab.com/cossartlab/cicada_nwb/-/issues
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pynwb<5,>=3.1
Requires-Dist: numpy<3,>=2.0
Requires-Dist: pandas<4,>=2.2.2
Requires-Dist: h5py<4,>=3.11
Requires-Dist: scikit-image<0.27,>=0.24
Requires-Dist: remfile<0.2,>=0.1.13
Requires-Dist: requests<3,>=2.28
Requires-Dist: platformdirs<5,>=3.6
Requires-Dist: packaging>=23.0
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Dynamic: license-file

# CICADA NWB

[![Documentation](https://readthedocs.org/projects/cicada-analysis/badge/?version=latest)](https://cicada-analysis.readthedocs.io/en/latest/nwb/)

<!-- --8<-- [start:intro] -->
`cicada-nwb` reads NWB files (Neurodata Without Borders) from Python. It is the data access layer of
[CICADA](https://cicada-analysis.readthedocs.io/en/latest/) ([BiorXiv preprint](https://www.biorxiv.org/content/10.64898/2026.07.03.736318v1)),
and it works on its own: no GUI and no analysis package needed.

A file opens through submodules: `calcium_imaging`, `electrophysiology`, `behavior`, `widefield`, `optogenetic` and
`metadata` (lab packages can add more). It is read-only: it never changes a file. A file on your disk or on the
[DANDI archive](https://dandiarchive.org/) is opened the same way. The file stays open while the session object
exists, so only the data you ask for is read. Use a `with` block to close it.
<!-- --8<-- [end:intro] -->

## Install

<!-- --8<-- [start:install-user] -->
From PyPI:
```bash
conda create -n cicada_env python=3.11
conda activate cicada_env
pip install cicada-nwb
```

Or from GitLab, a given release (tag):
```bash
pip install "git+https://gitlab.com/cossartlab/cicada_nwb.git@0.5.0"
```
<!-- --8<-- [end:install-user] -->

<details>
<summary>Contributor: install from a clone</summary>

<!-- --8<-- [start:install-dev] -->
```bash
git clone https://gitlab.com/cossartlab/cicada_nwb.git /path/to/cicada_nwb
conda create -n cicada_env python=3.11
conda activate cicada_env
pip install -e "/path/to/cicada_nwb[dev]"
```
<!-- --8<-- [end:install-dev] -->
</details>

## Quick start

<!-- --8<-- [start:quickstart] -->
A getter returns `None`, or an empty list or dict, for what is missing: check before you read.

```python
from cicada_nwb import NWBSession

path = "my_file.nwb"    # a local .nwb file, or a DANDI address: dandi://000728/sub-.../sub-..._ophys.nwb

with NWBSession(path) as session:
    print(session.identifier, session.subject_id, session.age, session.age_unit)

    # Calcium imaging: a series is addressed by [module, interface, series name]
    series = session.calcium_imaging.get_roi_response_series()      # {module: {interface: [names]}}
    if series:
        module = next(iter(series))
        interface = next(iter(series[module]))
        keys = [module, interface, series[module][interface][0]]
        traces = session.calcium_imaging.get_roi_response_serie_data(keys)        # (n_cells, n_frames)
        times = session.calcium_imaging.get_roi_response_serie_timestamps(keys)   # (n_frames,) in s
    # Or pick series by the kind of their values: "traces", or rasters, spike counts, event amplitudes
    traces_series = session.calcium_imaging.get_roi_response_series_by_kind("traces")   # [[module, interface, name], ...]
    raster_series = session.calcium_imaging.get_raster_like_series()

    # Electrophysiology
    if session.electrophysiology.has_units():
        spike_trains = session.electrophysiology.get_spike_times("spike_times")   # one array per unit

    # Behavior
    trial_table = session.behavior.get_trial_table()                  # DataFrame, or None without trials
    events = session.behavior.get_behavioral_events_names()           # [] without events
    if events:
        event_times = session.behavior.get_behavioral_events_times(events[0])     # (2, n_events): start, stop

    # Widefield: an acquisition is addressed by [module, name]
    acquisitions = session.widefield.get_wf_acquisition_dict()        # {module: [names]}
    if acquisitions:
        module = next(iter(acquisitions))
        wf_times = session.widefield.get_widefield_timestamps([module, acquisitions[module][0]])
```

Complete script: [template](https://gitlab.com/cossartlab/cicada_nwb/-/blob/master/templates/cicada_nwb_only_analysis_template.py).
For computations (PSTH, assemblies), see the
[cicada-tools quick start](https://cicada-analysis.readthedocs.io/en/latest/analysis/cicada_tools_quickstart/).
<!-- --8<-- [end:quickstart] -->

## The submodules

<!-- --8<-- [start:submodules] -->
| Submodule                 | Accessed via                | Key methods                                                                                         |
|---------------------------|-----------------------------|-----------------------------------------------------------------------------------------------------|
| 2P calcium imaging        | `session.calcium_imaging`   | `get_roi_response_series()`, `get_roi_response_serie_data()`, `get_cell_indices_by_cell_type()`     |
| Electrophysiology         | `session.electrophysiology` | `get_units_table()`, `get_spike_times()`, `get_units_location()`, `get_electrical_series_data()`    |
| Behavior                  | `session.behavior`          | `get_trial_table()`, `get_behavioral_epochs_times()`, `get_behavioral_events_times()`               |
| Widefield calcium imaging | `session.widefield`         | `get_widefield_timestamps()`, `get_widefield_dff0()`                                                |
| Optogenetic stimulation   | `session.optogenetic`       | `has_opto_stimulation()`, `get_opto_stimulation_times()`                                            |
| Metadata                  | `session.metadata`          | `get_sessions_info()`                                                                               |
<!-- --8<-- [end:submodules] -->

## Lab-specific submodules

<!-- --8<-- [start:lab] -->
Lab packages add `session.<lab>` submodules, for example `session.petersen`
([cicada-petersen](https://github.com/LSENS-BMI-EPFL/cicada_petersen)) and `session.cossart`
([cicada-cossart](https://gitlab.com/cossartlab/cicada_cossart)).
Example: [template](https://gitlab.com/cossartlab/cicada_nwb/-/blob/master/templates/new_lab_submodule_template.py).
<!-- --8<-- [end:lab] -->

## Documentation

Quick start, user guide and API reference: [cicada-analysis.readthedocs.io](https://cicada-analysis.readthedocs.io/en/latest/nwb/).

<details>
<summary>What's inside the package</summary>

<!-- --8<-- [start:inside] -->
```
cicada_nwb/
├── nwb_session.py     # NWBSession: opens one NWB file (local or DANDI), holds the submodules
├── submodules/        # metadata, calcium_imaging, electrophysiology, behavior, widefield,
│                      # optogenetic (getters and question methods)
├── lab_submodules/    # NWBLabSubmodule: base of the lab submodules of the lab packages
├── dandi.py           # DANDI addresses, streaming and download
├── cli.py             # the cicada-dandi command
└── testing/           # Synthetic NWB files for tests
```
<!-- --8<-- [end:inside] -->
</details>
