Skip to content

Implement GLM-based firing rate estimation#709

Open
abuzarmahmood wants to merge 5 commits into
katzlabbrandeis:masterfrom
abuzarmahmood:feature/glm-firing-rate-estimation
Open

Implement GLM-based firing rate estimation#709
abuzarmahmood wants to merge 5 commits into
katzlabbrandeis:masterfrom
abuzarmahmood:feature/glm-firing-rate-estimation

Conversation

@abuzarmahmood
Copy link
Copy Markdown
Member

@abuzarmahmood abuzarmahmood commented Jan 18, 2026

Closes #708

Summary

Adds utils/glm/ module for GLM-based firing rate estimation using the nemos library.

Architecture

Due to dependency conflicts between nemos and blech_clust, this module uses a two-environment architecture:

infer_glm_rates.py (orchestrator)
        │
        ├── _glm_extract_data.py  →  runs in blech_clust conda env
        │                             (uses ephys_data for spike extraction)
        │
        └── _glm_fit_models.py    →  runs in separate nemos venv
                                      (fits GLM, generates plots, writes HDF5)

Data is passed between environments via temporary numpy files in utils/glm/_temp/.

Features

  • Spike history modeling: Raised cosine log basis functions for autoregressive effects
  • Stimulus features: Optional stimulus timing input
  • Neural coupling: Optional coupling between simultaneously recorded neurons
  • Bits per spike metric: Model comparison against baseline Poisson
  • Flexible grouping: Process by taste and/or brain region

Setup

  1. blech_clust conda environment (existing)
  2. Create nemos venv:
    python -m venv ~/nemos_env
    source ~/nemos_env/bin/activate
    pip install nemos matplotlib pandas tables

Usage

python utils/glm/infer_glm_rates.py <data_dir> [options]

The script auto-detects environments or prompts if not found.

Output

  • Predicted firing rates in HDF5 under /glm_output/regions/
  • Bits per spike summary CSV
  • Individual neuron prediction plots
  • Pickled GLM models

Implements issue katzlabbrandeis#708: GLM-based firing rate inference using nemos library.

Features:
- Spike history modeling with raised cosine basis functions
- Optional stimulus timing features
- Optional coupling between simultaneously recorded neurons
- Bits per spike metric for model comparison vs binned rates
- Separate processing by taste and/or region

Co-authored-by: Ona <no-reply@ona.com>
@abuzarmahmood
Copy link
Copy Markdown
Member Author

Implementation Details

GLM Model Structure

The script fits a Poisson GLM for each neuron with the following components:

  1. Spike History Filter: Captures autoregressive effects using nemos.basis.RaisedCosineLogConv. The log-spaced basis functions efficiently represent both short and long timescale dependencies with fewer parameters than raw history.

  2. Stimulus Features (optional): Models the effect of stimulus onset using a convolved indicator function. This captures post-stimulus response dynamics.

  3. Neural Coupling (optional): Models how activity in other neurons affects the target neuron's firing. Uses the same raised cosine basis but with reduced dimensionality.

Bits Per Spike Metric

The bits per spike metric quantifies model performance as information gain over a baseline:

bits_per_spike = (LL_model - LL_baseline) / (n_spikes * log(2))

Where:

  • LL_model: Log-likelihood under the fitted GLM
  • LL_baseline: Log-likelihood under a homogeneous Poisson model (mean rate)

Positive values indicate the GLM captures structure beyond the mean rate. Typical values for well-fit models range from 0.1-1.0 bits/spike.

Comparison to RNN Approach

This GLM approach complements the existing infer_rnn_rates.py:

Aspect GLM (nemos) RNN (blechRNN)
Interpretability High - coefficients map to features Low - latent dynamics
Computation Fast - convex optimization Slow - gradient descent
Flexibility Structured features Learns representations
Dependencies nemos (pip) blechRNN (external)

Future Extensions

Per issue #708, potential additions:

  • Cross-validation for regularization strength
  • Model selection across different feature sets
  • Comparison plots between GLM and RNN predictions

abuzarmahmood and others added 4 commits January 19, 2026 17:22
Move GLM scripts to utils/glm/ subdirectory with two-environment architecture:
- infer_glm_rates.py: Main orchestrator that detects environments
- _glm_extract_data.py: Runs in blech_clust conda env for data extraction
- _glm_fit_models.py: Runs in nemos venv for GLM fitting

Data is passed between environments via temporary numpy files.
Remove nemos from requirements-optional.txt since it runs in separate venv.

Co-authored-by: Ona <no-reply@ona.com>
Co-authored-by: Ona <no-reply@ona.com>
…perations

- Omitted regularizer strength parameter to default to `Ridge`.
- Temporarily commented out model saving and bits per spike distribution plotting to streamline current execution. These tweaks are intended to improve performance for now.
…ting

- Introduced a `test_mode` parameter to enable running tests without actual command-line arguments, facilitating easier code testing.
- Enhanced `find_conda_env` to efficiently parse the conda environment list, providing the Python path for better environment management.
- Improved the script's feedback with inline comments and print statements when operating in test mode.
- Adjusted the default behavior of `sys.exit` to allow for graceful test termination and better debugging.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Glm-based firing rate estimation

1 participant