The ln-model package provides a complete framework for fitting separable Linear-Nonlinear (LN) models to neural data, with support for multiple model types, cross-validation, and synthetic data generation. It is designed for electrophysiology data analysis with a focus on receptive field estimation using separable temporal and spatial filters in particular.
- π§ Multiple Model Types: Support for Poisson, linear, and logistic regression models
- π― Separable Filters: Efficient temporal-spatial decomposition for receptive field estimation
- π Cross-Validation: Automated hyperparameter selection with
LNModelCV
-
Add the ln-model directory to your MATLAB path:
addpath('path/to/ln-model'); -
Or place the
+lnModelpackage directory in your working directory.
% Create and fit a basic LN model
% Note: stimulus matrix should be [nTimePoints x nSpatialPositions]
model = lnModel.LNModel(20, 'poireg'); % 20 temporal lags, adaptive Poisson model
model.fit(stimulus, responses); % Fit to your data
predictions = model.predict(testStimulus); % Generate predictions
% Access learned filters
filters = model.getSeparableFilters(); % {temporal, spatial} cell array
combinedFilter = model.getCombinedFilter(); % Full spatiotemporal filter% Automatic regularization parameter selection
modelCV = lnModel.LNModelCV(20, 'poireglin', ...
'regCValues', logspace(-4, 2, 10), 'scoring', 'objective');
modelCV.fit(stimulus, responses);
fprintf('Best regularization: %.2e\n', modelCV.BestRegC);
% Make predictions with optimized model
predictions = modelCV.predict(testStimulus);% Available model types
model1 = lnModel.LNModel(20, 'poireglin'); % Adaptive link Poisson (default)
model2 = lnModel.LNModel(20, 'poireg'); % Standard Poisson GLM
model3 = lnModel.LNModel(20, 'linreg'); % Linear regression
model4 = lnModel.LNModel(20, 'logreg'); % Logistic regression
% With custom regularization
model.fit(stimulus, responses, 'regC', 1e-3);
% With baseline handling
model.fit(stimulus, responses, baseline); % Fixed baseline
model.fit(stimulus, responses, []); % Learned bias (default)The package supports four model types: 'poireglin', 'poireg', and 'logreg' are intended for spike data, whereas 'logreg' can be used for input currents.
% For multi-repetition data (each column is a repetition)
responses = [rep1, rep2, rep3, rep4, rep5]; % [nTimePoints x nReps]
modelCV = lnModel.LNModelCV(20, 'poireg');
% Each repetition becomes a CV fold automatically
modelCV.fit(stimulus, responses);% Enable detailed debugging
model = lnModel.LNModel(20, 'poireglin');
model.DebugLevel = 2; % Enables gradient validation
model.fit(stimulus, responses);
% Debug levels:
% 0 - Silent (default)
% 1 - Basic convergence info
% 2 - Gradient validation (errors on failure)
% 3 - Full diagnostic output+lnModel/ % Main package namespace
βββ LNModel.m % Primary model class with fit/predict interface
βββ LNModelCV.m % Cross-validated model with hyperparameter selection
βββ +datagen/ % Synthetic data generation utilities
β βββ generateSyntheticData.m % Core synthetic response generation
β βββ +stimulus/ % Stimulus generation functions
β βββ whiteNoise.m % White noise stimulus generator
βββ +models/ % Model type implementations
β βββ BaseModel.m % Abstract base class for all model types
β βββ PoiRegLinModel.m % Adaptive link Poisson model
β βββ PoiRegModel.m % Standard Poisson GLM
β βββ LinRegModel.m % Linear regression model
β βββ LogRegModel.m % Logistic regression model
β βββ +optimization/ % Mathematical optimization methods
β βββ NewtonMethod.m % Newton's method implementation
β βββ LeastSquares.m % Closed-form least squares
βββ +rf/ % Receptive field estimation components
β βββ ReceptiveField.m % Receptive field state management
β βββ RFEstimator.m % Alternating optimization engine
scripts/ % Usage examples and tutorials
βββ exampleUsage.m % Comprehensive tutorial with synthetic RGC data
tests/ % Validation and testing utilities
βββ testLinkFunctions.m % Link function validation tests
docs/ % Documentation and guidelines
βββ STYLEGUIDE.md % Code style conventions
- LNModel: Main interface for model creation, fitting, and prediction
- LNModelCV: Cross-validated version with automatic hyperparameter selection
- ReceptiveField: Manages separable filter representations and computations
- RFEstimator: Implements alternating minimization optimization algorithm
Stimulus β Padding β Filter Convolution β Link Function β Predictions
β β β β β
[nΓp] β [(n+k-1)Γp] β [nΓ1] β Link(z) β Response [nΓ1]
% Run the comprehensive tutorial
run('scripts/exampleUsage.m');This tutorial demonstrates:
- Synthetic RGC data generation with biphasic temporal and center-surround spatial filters
- Model fitting with cross-validation
- Performance evaluation and filter recovery
- Complete visualization of results
- Detailed explanation of matrix padding and convolution operations
All models implement the separable LN framework:
z = (w_temporal β w_spatial) * X + baseline + bias
y = invLink(z) + noise
- PoiRegLinModel:
g(y) = log(y + k) - log(k)for adaptive scaling - PoiRegModel:
g(y) = log(y + Ξ΅)for standard Poisson - LinRegModel:
g(y) = yfor linear relationship - LogRegModel:
g(y) = logit(y)for binary data
Models automatically select appropriate optimization:
- Newton's Method: Nonlinear models (Poisson, logistic)
- Closed-Form: Linear models with analytical solutions
- Alternating Minimization: Coordinate descent for separable filters
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Follow the style guidelines
- Add tests for new functionality
- Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is released under the MIT License.
