Metadata-Version: 2.3
Name: dr-datacube
Version: 0.1.1
Summary: Access to the Dynamic Routing datacube: consolidated NWB tables as polars LazyFrames, with standard session filters.
Author: Ben Hardcastle
Author-email: Ben Hardcastle <ben.hardcastle@alleninstitute.org>
Requires-Dist: lazynwb>=1.0.0
Requires-Dist: npc-session>=0.1.41
Requires-Dist: polars>=1.43.2
Requires-Dist: pydantic-settings
Requires-Dist: universal-pathlib
Requires-Dist: aind-session>=0.3.26 ; extra == 'co'
Requires-Dist: marimo ; extra == 'nb'
Requires-Python: >=3.10
Project-URL: Repository, https://github.com/AllenNeuralDynamics/dr-datacube
Project-URL: Issues, https://github.com/AllenNeuralDynamics/dr-datacube/issues
Provides-Extra: co
Provides-Extra: nb
Description-Content-Type: text/markdown



### Loading data

Use `get_lf` to read a consolidated table as a `polars.LazyFrame`; apply `.select(...)` to choose specific columns and
`.collect()` to materialize as a `DataFrame`.

Pass `session_id` to restrict the result to one session. By default, parquet
results are restricted to behavior-passing brainwide sessions in the published
datacube session list. Use `session_type` to select `"brainwide"`, `"naive"`, or
`"templeton"` sessions, pass a collection to select multiple types, or pass
`session_type=None` and `with_behavior_filter=False` to include all sessions.

Pass `nwb=True` to read directly from NWB files. With `session_id`, this reads
that session's NWB file; without it, it reads all NWB sources available from the
configured data source. 

`get_lf("units")` always loads the full units table,
including spike times, and can be slow. Use `get_lf("unit_metrics")` for the table without spike times and other large array-like columns. The spike-inclusive
parquet files are only available in the cache; when using the datacube asset,
`units` is automatically read from NWB.

Pass `only_in_data_asset=False` to bypass the published session list, for
example to load another session available in the cache.

```python
import polars as pl
from dr_datacube import get_lf

performance = get_lf("performance").collect()
session_units = (
    get_lf("units", session_id="123456_2024-01-01")
    # filter to reduce the number of rows fetched:
    .filter(
        'is_qc_pass', 
        pl.col('structure') == 'MOs',
    )
    # drop large columns that aren't needed to save time/memory:
    .drop('spike_amplitudes', 'waveform_mean', 'waveform_std')
    # alternatively, select the columns you need directly:
    .select('spike_times', 'unit_id') 
    .collect()
)

# Load all behavior-passing sessions from multiple standard session sets:
ephys = get_lf(
    "unit_metrics",
    session_type=["brainwide", "templeton"],
).collect()
```

Use `get_session_table` for the standard session sets. By default it applies
the behavior filter and keeps only sessions in the configured data asset:

```python
from dr_datacube import get_session_table

brainwide = get_session_table(["brainwide", "templeton"])
all_sessions = get_session_table(None, with_behavior_filter=False)
```

### Datacube session IDs
A list of standard ephys sets is published in
[`assets/datacube_sessions.csv`](assets/datacube_sessions.csv). The table contains the session and
subject IDs, the standard session type (`brainwide`, `naive`, and
`templeton`), and whether the session passed the relevant behavior filter for its type. The table is generated by
[`scripts/dump_datacube_sessions.py`](scripts/dump_datacube_sessions.py) and updated automatically in GitHub Actions when changes are made to the filtering logic or the datacube default version (as it requires CO and AWS credentials).

Read it directly from GitHub with pandas:

```python
import pandas as pd

url = "https://raw.githubusercontent.com/allenneuraldynamics/dr-datacube/main/assets/datacube_sessions.csv"
session_ids = pd.read_csv(url).query("is_behavior_pass and session_type == 'brainwide'")["session_id"].tolist()
```

Or with Polars:

```python
import polars as pl

url = "https://raw.githubusercontent.com/allenneuraldynamics/dr-datacube/main/assets/datacube_sessions.csv"
session_ids = (
    pl.read_csv(url)
    .filter(pl.col("is_behavior_pass") & (pl.col("session_type") == "brainwide"))["session_id"]
    .to_list()
)
```

This package also provides a convenience function to get the list of session IDs directly:

```python
from dr_datacube import get_session_ids_from_github

good_session_ids = get_session_ids_from_github("brainwide")
all_session_ids = get_session_ids_from_github(["brainwide", "templeton"], with_behavior_filter=False)
```


### Data source and configuration

The datacube version and data source are controlled by `dr_datacube.config`.
Set configuration near the top of your script or notebook before loading data:

```python
import dr_datacube

dr_datacube.config.version = "v0.0.301"
```

By default, the package uses a matching local `dynamicrouting_datacube` asset
when running on Code Ocean. If no matching local asset is available, it streams
from the datacube asset directory on S3 through fsspec/boto. The S3 asset
directory is currently private, so credentials are required for this fallback.

The main configuration options are:

| Option | Effect |
|---|---|
| `version` | Select the datacube version. |
| `use_cache` | Use the public scratch bucket (`aind-scratch-data`) for both NWB files and parquet tables. |
| `disable_asset_streaming` | Disable streaming the datacube asset through fsspec/boto; a matching asset must be attached locally in Code Ocean. |
| `anon` | Use anonymous access: AWS credentials are not required and are not used, even if available. |
| `nwb_only` | Disable access to parquet tables, so reads use NWB data only. |

To use the public scratch-bucket cache instead, enable the cache:

```python
dr_datacube.config.use_cache = True
```

If credentials are not available, enable anonymous access as well:

```python
dr_datacube.config.anon = True
```

### Temporary configuration

Use `config.override(...)` when an operation needs different settings without
changing the module-level `config`. The temporary configuration is restored
when the context exits, even if an exception is raised:

```python
from dr_datacube import config, get_lf

with config.override(use_cache=True):
    units = get_lf("units")
```
