xforms Demo
This notebook demonstrates the use of xforms for generating models that are easy to reload.
[1]:
import logging
from pathlib import Path
import nems0.db as db
import nems0.modelspec as ms
import nems0.recording as recording
import nems0.uri
import nems0.xforms as xforms
[nems.configs.defaults INFO] Saving log messages to /tmp/nems\NEMS 2020-05-28 141002.log
Configuration
[2]:
# get the data and results paths
results_dir = nems.get_setting('NEMS_RESULTS_DIR')
signals_dir = nems.get_setting('NEMS_RECORDINGS_DIR')
[3]:
# download some demo data
recording.get_demo_recordings(signals_dir)
datafile = Path(signals_dir) / 'TAR010c-18-1.pkl'
Data Loading and Preprocessing
[4]:
load_command = 'nems.demo.loaders.demo_loader'
expt_id = 'TAR010c'
batch = 271
cell_id = 'TAR010c-18-1'
[5]:
modelspec_name = 'dlog-wc.18x1.g-fir.1x15-lvl.1-dexp.1'
Generate the Modelspec
Each item in the xform spec is a call to a function along with the arguments for that function.
[6]:
xfspec = []
[7]:
# load from external format
xfspec.append(['nems.xforms.load_recording_wrapper',
{'load_command': load_command,
'exptid': expt_id,
'datafile': str(datafile)
}])
[8]:
# split the data into est and val
xfspec.append(['nems.xforms.split_by_occurrence_counts',
{'epoch_regex': '^STIM_'}])
[9]:
xfspec.append(['nems.xforms.average_away_stim_occurrences', {}])
[10]:
meta = {'cellid': cell_id, 'batch': batch, 'modelname': modelspec_name, 'recording': expt_id}
xfspec.append(['nems.xforms.init_from_keywords',
{'keywordstring': modelspec_name,
'meta': meta
}])
[11]:
# init, then fit
xfspec.append(['nems.xforms.fit_basic_init', {}])
xfspec.append(['nems.xforms.fit_basic', {'tolerance': 1e-6}])
[12]:
xfspec.append(['nems.xforms.predict', {}])
[13]:
# test prediction then visualize
xfspec.append(['nems.analysis.api.standard_correlation', {},
['est', 'val', 'modelspec', 'rec'], ['modelspec']])
xfspec.append(['nems.xforms.plot_summary', {}])
Run the Analysis
[14]:
ctx = {}
for xfa in xfspec:
ctx = xforms.evaluate_step(xfa, ctx)
[nems.xforms INFO] Evaluating: nems.xforms.load_recording_wrapper
[nems.xforms INFO] Loading cached file C:\Users\Alex\PycharmProjects\NEMS\recordings\TAR010c_afb264b3db970ec890e04c727e612c1cbfaced62.tgz
[nems.xforms INFO] Evaluating: nems.xforms.split_by_occurrence_counts
[nems.xforms INFO] Evaluating: nems.xforms.average_away_stim_occurrences
[nems.xforms INFO] Evaluating: nems.xforms.init_from_keywords
[nems.initializers INFO] kw: dlog
[nems.initializers INFO] kw: wc.18x1.g
[nems.initializers INFO] kw: fir.1x15
[nems.initializers INFO] kw: lvl.1
[nems.initializers INFO] kw: dexp.1
[nems.initializers INFO] Setting modelspec[0] input to stim
[nems.xforms INFO] Evaluating: nems.xforms.fit_basic_init
[nems.initializers INFO] prefit_LN parameters: tol=3.16e-06 max_iter=1500
[nems.initializers INFO] target_module: ['levelshift', 'relu'] found at modelspec[3].
[nems.initializers INFO] Mod 3 (nems.modules.levelshift.levelshift) initializing level to resp mean 0.207
[nems.initializers INFO] resp has 1 channels
[nems.initializers INFO] Fit: [0 1 2 3]
[nems.initializers INFO] Freeze: []
[nems.initializers INFO] Exclude: [4.]
[nems.initializers INFO] Excluding module 4 (nems.modules.nonlinearity.double_exponential)
[nems.analysis.fit_basic INFO] Data len pre-mask: 49500
[nems.analysis.fit_basic INFO] Data len post-mask: 49500
[nems.modelspec INFO] Freezing fast rec at start=0
[nems.fitters.fitter INFO] options {'ftol': 3.162277660168379e-06, 'maxiter': 1500, 'maxfun': 15000}
[nems.fitters.fitter INFO] Start sigma: [ 0. 0.5 0.3192 0. 0.1 -0.05 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0.
0.2074]
[nems.analysis.cost_functions INFO] Eval #100. E=0.945049
[nems.analysis.cost_functions INFO] Eval #200. E=0.915989
[nems.analysis.cost_functions INFO] Eval #300. E=0.895050
[nems.analysis.cost_functions INFO] Eval #400. E=0.890344
[nems.analysis.cost_functions INFO] Eval #500. E=0.887200
[nems.analysis.cost_functions INFO] Eval #600. E=0.886142
[nems.analysis.cost_functions INFO] Eval #700. E=0.885961
[nems.analysis.cost_functions INFO] Eval #800. E=0.885516
[nems.analysis.cost_functions INFO] Eval #900. E=0.885404
[nems.fitters.fitter INFO] Starting error: 0.980508 -- Final error: 0.885402
[nems.fitters.fitter INFO] Final sigma: [-0.1006 0.109 0.1278 -0.0169 0.3815 0.2093 -0.0939 -0.0681 -0.0717
-0.0123 -0.0209 -0.0318 -0.0432 -0.0083 -0.0112 -0.0095 0.0009 -0.0557
0.1257]
[nems.analysis.fit_basic INFO] Delta error: 0.980508 - 0.885402 = -9.510548e-02
[nems.initializers INFO] Found module 4 (double_exponential) for subset prefit
[nems.initializers INFO] Fit: [4]
[nems.initializers INFO] Freeze: [0 1 2 3]
[nems.initializers INFO] Exclude: []
[nems.initializers INFO] Freezing phi for module 0 (nems.modules.nonlinearity.dlog)
[nems.initializers INFO] Freezing phi for module 1 (nems.modules.weight_channels.gaussian)
[nems.initializers INFO] Freezing phi for module 2 (nems.modules.fir.basic)
[nems.initializers INFO] Freezing phi for module 3 (nems.modules.levelshift.levelshift)
[nems.analysis.fit_basic INFO] Data len pre-mask: 49500
[nems.analysis.fit_basic INFO] Data len post-mask: 49500
[nems.modelspec INFO] Freezing fast rec at start=4
[nems.fitters.fitter INFO] options {'ftol': 3.162277660168379e-06, 'maxiter': 700, 'maxfun': 7000}
[nems.fitters.fitter INFO] Start sigma: [0.6689 0. 1.3294 0.2075]
[nems.analysis.cost_functions INFO] Eval #100. E=0.865425
[nems.fitters.fitter INFO] Starting error: 0.909664 -- Final error: 0.865409
[nems.fitters.fitter INFO] Final sigma: [2.4717 0.0876 0.7382 0.7999]
[nems.analysis.fit_basic INFO] Delta error: 0.909664 - 0.865409 = -4.425523e-02
[nems.xforms INFO] Evaluating: nems.xforms.fit_basic
[nems.xforms INFO] ----------------------------------------------------
[nems.xforms INFO] Fitting: fit 1/1, fold 1/1 (tol=1.00e-06, max_iter=1000)
[nems.analysis.fit_basic INFO] Data len pre-mask: 49500
[nems.analysis.fit_basic INFO] Data len post-mask: 49500
[nems.modelspec INFO] Freezing fast rec at start=0
[nems.fitters.fitter INFO] options {'ftol': 1e-06, 'maxiter': 1000, 'maxfun': 10000}
[nems.fitters.fitter INFO] Start sigma: [-1.0060e-01 1.0900e-01 1.2780e-01 -1.6900e-02 3.8150e-01 2.0930e-01
-9.3900e-02 -6.8100e-02 -7.1700e-02 -1.2300e-02 -2.0900e-02 -3.1800e-02
-4.3200e-02 -8.3000e-03 -1.1200e-02 -9.5000e-03 9.0000e-04 -5.5700e-02
1.2570e-01 2.4717e+00 8.7600e-02 7.3820e-01 7.9990e-01]
[nems.analysis.cost_functions INFO] Eval #100. E=0.864857
[nems.analysis.cost_functions INFO] Eval #200. E=0.862435
[nems.analysis.cost_functions INFO] Eval #300. E=0.861485
[nems.analysis.cost_functions INFO] Eval #400. E=0.860291
[nems.analysis.cost_functions INFO] Eval #500. E=0.858603
[nems.analysis.cost_functions INFO] Eval #600. E=0.857506
[nems.analysis.cost_functions INFO] Eval #700. E=0.854957
[nems.analysis.cost_functions INFO] Eval #800. E=0.856073
[nems.analysis.cost_functions INFO] Eval #900. E=0.853329
[nems.analysis.cost_functions INFO] Eval #1000. E=0.852527
[nems.analysis.cost_functions INFO] Eval #1100. E=0.851789
[nems.analysis.cost_functions INFO] Eval #1200. E=0.851620
[nems.analysis.cost_functions INFO] Eval #1300. E=0.851192
[nems.analysis.cost_functions INFO] Eval #1400. E=0.850913
[nems.analysis.cost_functions INFO] Eval #1500. E=0.850907
[nems.analysis.cost_functions INFO] Eval #1600. E=0.851155
[nems.analysis.cost_functions INFO] Eval #1700. E=0.850678
[nems.analysis.cost_functions INFO] Eval #1800. E=0.850645
[nems.analysis.cost_functions INFO] Eval #1900. E=0.850550
[nems.analysis.cost_functions INFO] Eval #2000. E=0.850305
[nems.analysis.cost_functions INFO] Eval #2100. E=0.850310
[nems.analysis.cost_functions INFO] Eval #2200. E=0.849841
[nems.analysis.cost_functions INFO] Eval #2300. E=0.849612
[nems.analysis.cost_functions INFO] Eval #2400. E=0.849527
[nems.analysis.cost_functions INFO] Eval #2500. E=0.893259
[nems.analysis.cost_functions INFO] Eval #2600. E=0.849483
[nems.analysis.cost_functions INFO] Eval #2700. E=0.849345
[nems.analysis.cost_functions INFO] Eval #2800. E=0.849329
[nems.analysis.cost_functions INFO] Eval #2900. E=0.851132
[nems.analysis.cost_functions INFO] Eval #3000. E=0.849308
[nems.analysis.cost_functions INFO] Eval #3100. E=0.849287
[nems.analysis.cost_functions INFO] Eval #3200. E=0.849280
[nems.fitters.fitter INFO] Starting error: 0.865409 -- Final error: 0.849279
[nems.fitters.fitter INFO] Final sigma: [-1.7353e+00 1.4920e-01 8.4800e-02 -1.0000e-03 2.2820e-01 1.6310e-01
-3.5500e-02 -2.7500e-02 -9.9700e-02 -2.7500e-02 -1.9800e-02 1.8900e-02
-2.2600e-02 -1.4000e-03 2.3000e-03 -2.5500e-02 1.8000e-02 -4.1100e-02
-1.1800e-01 2.4112e+00 1.1850e-01 6.5300e-01 1.0436e+00]
[nems.analysis.fit_basic INFO] Delta error: 0.865409 - 0.849279 = -1.612929e-02
[nems.xforms INFO] Evaluating: nems.xforms.predict
[nems.xforms INFO] Evaluating: nems.analysis.api.standard_correlation
[nems.xforms INFO] Evaluating: nems.xforms.plot_summary
[nems.modelspec INFO] Quickplot: no epoch specified, falling back to "REFERENCE"
[nems.modelspec WARNING] Quickplot: no valid epochs matching REFERENCE. 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
[nems.modelspec INFO] plotting row 6/6
[nems.modelspec INFO] Quickplot: generated fig with title "Cell: TAR010c-18-1, Batch: 271, None #0 dlog-wc.18x1.g-fir.1x15-lvl.1-dexp.1"
bin range: 0-500
Saving Results
[15]:
# get a unique dir to save the modelspec to
destination = Path(results_dir) / str(batch) / xforms.get_meta(ctx)['cellid'] / ms.get_modelspec_longname(ctx['modelspec'])
destination = str(destination)
[16]:
xforms.save_analysis(destination,
recording=ctx['rec'],
modelspec=ctx['modelspec'],
xfspec=xfspec,
figures=ctx['figures'],
log='NO LOG')
[16]:
{'savepath': 'C:\\Users\\Alex\\PycharmProjects\\NEMS\\results\\271\\TAR010c-18-1\\TAR010c.dlog_wc.18x1.g_fir.1x15_lvl.1_dexp.1.fit_basic.2020-05-28T211146/'}
[ ]:
# save summary of results to a database
modelspec = ctx['modelspec']
modelspec.meta['modelpath'] = destination
modelspec.meta['figurefile'] = destination + 'figure.0000.png'
db.update_results_table(modelspec)