Intro to NEMS

This notebook covers some basic overview of how NEMS works, form loading data to creating a modelspec of a model and evaluating it.

[1]:
import pickle
from pathlib import Path

import nems0.analysis.api
import nems0.initializers
import nems0.recording as recording
import nems0.uri
from nems0.fitters.api import scipy_minimize
from nems0.signal import RasterizedSignal
[nems.configs.defaults INFO] Saving log messages to /tmp/nems\NEMS 2020-05-28 133134.log

Configuration

Raw data is usually saved in the recording directory in NEMS. You can specify a custom save data location in your NEMS config file.

[2]:
# get the data and results paths
signals_dir = Path(nems.NEMS_PATH) / 'recordings'
[3]:
# download some demo data
recording.get_demo_recordings(signals_dir)
datafile = signals_dir / 'TAR010c-18-1.pkl'

Load and Format Recording Data

[4]:
with open(datafile, 'rb') as f:
    cellid, rec_name, fs, X, Y, epochs = pickle.load(f)

A recording object contains a set of signal objects, along with some associated metadata. signal objects can be of various types depending on your use case. signal data is stored as channel x time arrays.

[5]:
# create NEMS formatted signal objects from the raw data
resp = RasterizedSignal(fs, Y, 'resp', rec_name, epochs=epochs, chans=[cellid])
stim = RasterizedSignal(fs, X, 'stim', rec_name, epochs=epochs)
[6]:
# create the recording object from the signals
signals = {'resp': resp, 'stim': stim}
rec = recording.Recording(signals)

To validate the model after training, the data is split into est and val datasets.

[7]:
est, val = rec.split_using_epoch_occurrence_counts(epoch_regex="^STIM_")

Initialize the Modelspec

Modelspec names are shorthand keyword strings. The below one defines a model, which constrains the spectral tuning to be gaussian and adds a static output non linearity.

[8]:
modelspec_name = 'wc.18x2.g-fir.2x15-lvl.1-dexp.1'
[9]:
# record some meta data for display and saving
meta = {'cellid': cellid,
        'batch': 271,
        'modelname': modelspec_name,
        'recording': cellid
       }

modelspec = nems.initializers.from_keywords(modelspec_name, meta=meta)
[nems.initializers INFO] kw: wc.18x2.g
[nems.initializers INFO] kw: fir.2x15
[nems.initializers INFO] kw: lvl.1
[nems.initializers INFO] kw: dexp.1
[nems.initializers INFO] Setting modelspec[0] input to stim

Run an Analysis

The model is fit to the data using a basic gradient approach.

[10]:
modelspec = nems.analysis.api.fit_basic(est, modelspec, fitter=scipy_minimize)
[nems.analysis.fit_basic INFO] Data len pre-mask: 206974
[nems.analysis.fit_basic INFO] Data len post-mask: 148500
[nems.modelspec INFO] Freezing fast rec at start=0
[nems.fitters.fitter INFO] options {'ftol': 1e-07, 'maxiter': 1000, 'maxfun': 10000}
[nems.fitters.fitter INFO] Start sigma: [ 0.4167  0.5833  0.3192  0.3192  0.      0.1    -0.05    0.      0.
  0.      0.      0.      0.      0.      0.      0.      0.      0.
  0.      0.      0.1    -0.05    0.      0.      0.      0.      0.
  0.      0.      0.      0.      0.      0.      0.      0.      1.
  0.      1.      0.    ]
[nems.analysis.cost_functions INFO] Eval #100. E=1.103888
[nems.analysis.cost_functions INFO] Eval #200. E=1.051702
[nems.analysis.cost_functions INFO] Eval #300. E=1.000810
[nems.analysis.cost_functions INFO] Eval #400. E=0.998919
[nems.analysis.cost_functions INFO] Eval #500. E=0.985356
[nems.analysis.cost_functions INFO] Eval #600. E=0.979484
[nems.analysis.cost_functions INFO] Eval #700. E=0.973421
[nems.analysis.cost_functions INFO] Eval #800. E=1.509854
[nems.analysis.cost_functions INFO] Eval #900. E=0.957021
[nems.analysis.cost_functions INFO] Eval #1000. E=0.947317
[nems.analysis.cost_functions INFO] Eval #1100. E=0.951219
[nems.analysis.cost_functions INFO] Eval #1200. E=0.945415
[nems.analysis.cost_functions INFO] Eval #1300. E=0.944702
[nems.analysis.cost_functions INFO] Eval #1400. E=1.009904
[nems.analysis.cost_functions INFO] Eval #1500. E=0.943614
[nems.analysis.cost_functions INFO] Eval #1600. E=0.943349
[nems.analysis.cost_functions INFO] Eval #1700. E=0.942525
[nems.analysis.cost_functions INFO] Eval #1800. E=0.942367
[nems.analysis.cost_functions INFO] Eval #1900. E=0.941636
[nems.analysis.cost_functions INFO] Eval #2000. E=0.940703
[nems.analysis.cost_functions INFO] Eval #2100. E=0.939995
[nems.analysis.cost_functions INFO] Eval #2200. E=0.939768
[nems.analysis.cost_functions INFO] Eval #2300. E=0.940184
[nems.analysis.cost_functions INFO] Eval #2400. E=0.939562
[nems.analysis.cost_functions INFO] Eval #2500. E=0.939343
[nems.analysis.cost_functions INFO] Eval #2600. E=0.939294
[nems.analysis.cost_functions INFO] Eval #2700. E=0.939260
[nems.analysis.cost_functions INFO] Eval #2800. E=0.939626
[nems.analysis.cost_functions INFO] Eval #2900. E=0.939196
[nems.analysis.cost_functions INFO] Eval #3000. E=0.939138
[nems.analysis.cost_functions INFO] Eval #3100. E=0.939115
[nems.analysis.cost_functions INFO] Eval #3200. E=0.939088
[nems.analysis.cost_functions INFO] Eval #3300. E=0.939086
[nems.analysis.cost_functions INFO] Eval #3400. E=0.939047
[nems.analysis.cost_functions INFO] Eval #3500. E=0.939098
[nems.analysis.cost_functions INFO] Eval #3600. E=0.939004
[nems.analysis.cost_functions INFO] Eval #3700. E=0.938968
[nems.analysis.cost_functions INFO] Eval #3800. E=0.938932
[nems.analysis.cost_functions INFO] Eval #3900. E=0.938921
[nems.analysis.cost_functions INFO] Eval #4000. E=0.938892
[nems.analysis.cost_functions INFO] Eval #4100. E=0.938927
[nems.analysis.cost_functions INFO] Eval #4200. E=0.938838
[nems.analysis.cost_functions INFO] Eval #4300. E=0.938823
[nems.analysis.cost_functions INFO] Eval #4400. E=0.938815
[nems.analysis.cost_functions INFO] Eval #4500. E=0.938810
[nems.analysis.cost_functions INFO] Eval #4600. E=0.938805
[nems.analysis.cost_functions INFO] Eval #4700. E=0.938800
[nems.analysis.cost_functions INFO] Eval #4800. E=0.938793
[nems.analysis.cost_functions INFO] Eval #4900. E=0.938784
[nems.analysis.cost_functions INFO] Eval #5000. E=0.938779
[nems.analysis.cost_functions INFO] Eval #5100. E=0.938774
[nems.analysis.cost_functions INFO] Eval #5200. E=0.938766
[nems.analysis.cost_functions INFO] Eval #5300. E=0.938759
[nems.analysis.cost_functions INFO] Eval #5400. E=0.938756
[nems.analysis.cost_functions INFO] Eval #5500. E=0.938750
[nems.analysis.cost_functions INFO] Eval #5600. E=0.938748
[nems.analysis.cost_functions INFO] Eval #5700. E=0.938744
[nems.analysis.cost_functions INFO] Eval #5800. E=0.938740
[nems.analysis.cost_functions INFO] Eval #5900. E=0.938738
[nems.analysis.cost_functions INFO] Eval #6000. E=0.938736
[nems.analysis.cost_functions INFO] Eval #6100. E=0.938731
[nems.analysis.cost_functions INFO] Eval #6200. E=0.938726
[nems.analysis.cost_functions INFO] Eval #6300. E=0.938720
[nems.analysis.cost_functions INFO] Eval #6400. E=0.938718
[nems.analysis.cost_functions INFO] Eval #6500. E=0.938715
[nems.analysis.cost_functions INFO] Eval #6600. E=0.938729
[nems.analysis.cost_functions INFO] Eval #6700. E=0.938712
[nems.analysis.cost_functions INFO] Eval #6800. E=0.938711
[nems.fitters.fitter INFO] Starting error: 1.126462 -- Final error: 0.938711
[nems.fitters.fitter INFO] Final sigma: [ 0.1127  1.01    0.0878  0.429   0.0385  0.3507  0.0848 -0.0516  0.0173
 -0.0399 -0.0922 -0.0377 -0.0823 -0.0094 -0.0021  0.0392 -0.0257 -0.0082
 -0.0495  0.0242  0.0226  0.0768  0.0919  0.1434 -0.1538 -0.1551 -0.0634
  0.0328  0.0106 -0.0735 -0.1639 -0.0261  0.0469 -0.1385 -0.3081  0.7584
  0.1432  0.9374  0.3081]
[nems.analysis.fit_basic INFO] Delta error: 1.126462 - 0.938711 = -1.877514e-01

Generate Summary Statistics

[11]:
# generate predictions
est, val = nems.analysis.api.generate_prediction(est, val, modelspec)
[12]:
# evaluate prediction accuracy
modelspec = nems.analysis.api.standard_correlation(est, val, modelspec)
[13]:
# results
print("Performance: r_fit={0:.3f} r_test={1:.3f}".format(
        modelspec.meta['r_fit'][0][0],
        modelspec.meta['r_test'][0][0]))
Performance: r_fit=0.346 r_test=0.353

Genenrate Plots

[14]:
fig = modelspec.quickplot(rec=est)
[nems.modelspec INFO] Quickplot: no epoch specified, falling back to "TRIAL"
[nems.modelspec WARNING] Quickplot: no valid epochs matching TRIAL. Will not subset data.
[nems.modelspec INFO] plotting row 1/6
[nems.modelspec INFO] plotting row 2/6
[nems.modelspec INFO] plotting row 3/6
[nems.modelspec INFO] plotting row 4/6
[nems.modelspec INFO] plotting row 5/6
bin range: 0-500
[nems.modelspec INFO] plotting row 6/6
[nems.modelspec INFO] Quickplot: generated fig with title "Cell: TAR010c-18-1, Batch: 271, None #0 wc.18x2.g-fir.2x15-lvl.1-dexp.1"
../_images/demos_demo_nems_26_3.png