예제 #1
0
def _get_figure_filepath(directory, modelspecs, format):
    '''
    Returns a filepath based on a directory, a list of modelspecs,
    and a file format.
    '''
    # TODO: Probably need a smarter way to do figure names since figures
    #       can come from an arbitrary number of modelspecs. Could just
    #       concatenate names for each modelspec? But that would mean some
    #       really long filenames.
    #       For now just uses the long name of the first modelspec until
    #       a better solution is decided on.
    mspec = modelspecs[0]
    mname = ms.get_modelspec_longname(mspec)
    fname = os.path.join(directory, mname) + "." + format
    return fname
예제 #2
0
def fit_pop_model_xforms_baphy(cellid, batch, modelname, saveInDB=False):
    """
    Fits a NEMS population model using baphy data

    DEPRECATED ? Now should work for xhelp.fit_model_xform()

    """

    raise NotImplementedError("Replaced by xhelper function?")
    log.info("Preparing pop model: ({0},{1},{2})".format(
            cellid, batch, modelname))

    # Segment modelname for meta information
    kws = modelname.split("_")
    modelspecname = "-".join(kws[1:-1])

    loadkey = kws[0]
    fitkey = kws[-1]
    if type(cellid) is list:
        disp_cellid="_".join(cellid)
    else:
        disp_cellid=cellid

    meta = {'batch': batch, 'cellid': disp_cellid, 'modelname': modelname,
            'loader': loadkey, 'fitkey': fitkey,
            'modelspecname': modelspecname,
            'username': '******', 'labgroup': 'lbhb', 'public': 1,
            'githash': os.environ.get('CODEHASH', ''),
            'recording': loadkey}

    uri_key = nems.utils.escaped_split(loadkey, '-')[0]
    recording_uri = generate_recording_uri(cellid, batch, uri_key)

    # pass cellid information to xforms so that loader knows which cells
    # to load from recording_uri
    xfspec = xhelp.generate_xforms_spec(recording_uri, modelname, meta,
                                        xforms_kwargs={'cellid': cellid})

    # actually do the fit
    ctx, log_xf = xforms.evaluate(xfspec)

    # save some extra metadata
    modelspec = ctx['modelspec']

    destination = '/auto/data/nems_db/results/{0}/{1}/{2}/'.format(
            batch, disp_cellid, ms.get_modelspec_longname(modelspec))
    modelspec.meta['modelpath'] = destination
    modelspec.meta['figurefile'] = destination+'figure.0000.png'
    modelspec.meta.update(meta)

    # extra thing to save for pop model
    modelspec.meta['cellids'] = ctx['val']['resp'].chans

    # save results
    log.info('Saving modelspec(s) to {0} ...'.format(destination))
    save_data = xforms.save_analysis(destination,
                                     recording=ctx['rec'],
                                     modelspec=modelspec,
                                     xfspec=xfspec,
                                     figures=ctx['figures'],
                                     log=log_xf)
    savepath = save_data['savepath']

    if saveInDB:
        # save in database as well
        nd.update_results_table(modelspec)

    return savepath
예제 #3
0
def fit_model_xforms_baphy(cellid, batch, modelname,
                           autoPlot=True, saveInDB=False):
    """
    DEPRECATED ? Now should work for xhelp.fit_model_xform()

    Fit a single NEMS model using data from baphy/celldb
    eg, 'ozgf100ch18_wc18x1_lvl1_fir15x1_dexp1_fit01'
    generates modelspec with 'wc18x1_lvl1_fir15x1_dexp1'

    based on this function in nems/scripts/fit_model.py
       def fit_model(recording_uri, modelstring, destination):

     xfspec = [
        ['nems.xforms.load_recordings', {'recording_uri_list': recordings}],
        ['nems.xforms.add_average_sig', {'signal_to_average': 'resp',
                                         'new_signalname': 'resp',
                                         'epoch_regex': '^STIM_'}],
        ['nems.xforms.split_by_occurrence_counts', {'epoch_regex': '^STIM_'}],
        ['nems.xforms.init_from_keywords', {'keywordstring': modelspecname}],
        ['nems.xforms.set_random_phi',  {}],
        ['nems.xforms.fit_basic',       {}],
        # ['nems.xforms.add_summary_statistics',    {}],
        ['nems.xforms.plot_summary',    {}],
        # ['nems.xforms.save_recordings', {'recordings': ['est', 'val']}],
        ['nems.xforms.fill_in_default_metadata',    {}],
    ]

    """
    raise NotImplementedError("Replaced by xhelper function?")
    raise DeprecationWarning("Replaced by xhelp.fit_model_xforms")
    log.info('Initializing modelspec(s) for cell/batch %s/%d...',
             cellid, int(batch))

    # Segment modelname for meta information
    kws = nems.utils.escaped_split(modelname, '_')

    old = False
    if (len(kws) > 3) or ((len(kws) == 3) and kws[1].startswith('stategain')
                          and not kws[1].startswith('stategain.')):
        # Check if modelname uses old format.
        log.info("Using old modelname format ... ")
        old = True
        modelspecname = nems.utils.escaped_join(kws[1:-1], '_')
    else:
        modelspecname = nems.utils.escaped_join(kws[1:-1], '-')
    loadkey = kws[0]
    fitkey = kws[-1]

    meta = {'batch': batch, 'cellid': cellid, 'modelname': modelname,
            'loader': loadkey, 'fitkey': fitkey, 'modelspecname': modelspecname,
            'username': '******', 'labgroup': 'lbhb', 'public': 1,
            'githash': os.environ.get('CODEHASH', ''),
            'recording': loadkey}

    if old:
        recording_uri = ogru(cellid, batch, loadkey)
        xfspec = oxfh.generate_loader_xfspec(loadkey, recording_uri)
        xfspec.append(['nems_lbhb.old_xforms.xforms.init_from_keywords',
                       {'keywordstring': modelspecname, 'meta': meta}])
        xfspec.extend(oxfh.generate_fitter_xfspec(fitkey))
        xfspec.append(['nems.analysis.api.standard_correlation', {},
                       ['est', 'val', 'modelspec', 'rec'], ['modelspec']])
        if autoPlot:
            log.info('Generating summary plot ...')
            xfspec.append(['nems.xforms.plot_summary', {}])
    else:
#        uri_key = nems.utils.escaped_split(loadkey, '-')[0]
#        recording_uri = generate_recording_uri(cellid, batch, uri_key)
        log.info("DONE? Moved handling of registry_args to xforms_init_context")
        recording_uri = None

        # registry_args = {'cellid': cellid, 'batch': int(batch)}
        registry_args = {}
        xforms_init_context = {'cellid': cellid, 'batch': int(batch)}

        xfspec = xhelp.generate_xforms_spec(recording_uri, modelname, meta,
                                            xforms_kwargs=registry_args,
                                            xforms_init_context=xforms_init_context)
        log.info(xfspec)

    # actually do the loading, preprocessing, fit
    ctx, log_xf = xforms.evaluate(xfspec)

    # save some extra metadata
    modelspec = ctx['modelspec']

    # this code may not be necessary any more.
    destination = '/auto/data/nems_db/results/{0}/{1}/{2}/'.format(
            batch, cellid, ms.get_modelspec_longname(modelspec))
    modelspec.meta['modelpath'] = destination
    modelspec.meta['figurefile'] = destination+'figure.0000.png'
    modelspec.meta.update(meta)

    # save results
    log.info('Saving modelspec(s) to {0} ...'.format(destination))
    save_data = xforms.save_analysis(destination,
                                     recording=ctx['rec'],
                                     modelspec=modelspec,
                                     xfspec=xfspec,
                                     figures=ctx['figures'],
                                     log=log_xf)
    savepath = save_data['savepath']

    # save in database as well
    if saveInDB:
        # TODO : db results finalized?
        nd.update_results_table(modelspec)

    return savepath
예제 #4
0
    ctx['modelspecs'][0][0]['meta']['r_test']
    
    ctxF=ctx.copy()

# Close the log, remove the handler, and add the 'log' string to context
log.info('Done (re-)evaluating xforms.')
ch.close()
rootlogger.removeFilter(ch)

log_xf = log_stream.getvalue()

# save some extra metadata
modelspec = ctx['modelspec']
#
destination = '/auto/data/nems_db/results/{0}/{1}/{2}/'.format(
        batch, cellid, ms.get_modelspec_longname(modelspec))
modelspec['meta']['modelpath'] = destination
modelspec['meta']['figurefile'] = destination+'figure.0000.png'

# save results
if saveToFile:
    log.info('Saving modelspec(s) to {0} ...'.format(destination))
    xforms.save_analysis(destination,
                      recording=ctx['rec'],
                      modelspec=modelspec,
                      xfspec=xfspec,
                      figures=ctx['figures'],
                      log=log_xf)

# save in database as well
if saveInDB:
예제 #5
0
def fit_model_xforms_baphy(cellid,
                           batch,
                           modelname,
                           autoPlot=True,
                           saveInDB=False):
    """
    Fits a single NEMS model using data from baphy/celldb
    eg, 'ozgf100ch18_wc18x1_lvl1_fir15x1_dexp1_fit01'
    generates modelspec with 'wc18x1_lvl1_fir15x1_dexp1'

    based on this function in nems/scripts/fit_model.py
       def fit_model(recording_uri, modelstring, destination):

     xfspec = [
        ['nems.xforms.load_recordings', {'recording_uri_list': recordings}],
        ['nems.xforms.add_average_sig', {'signal_to_average': 'resp',
                                         'new_signalname': 'resp',
                                         'epoch_regex': '^STIM_'}],
        ['nems.xforms.split_by_occurrence_counts', {'epoch_regex': '^STIM_'}],
        ['nems.xforms.init_from_keywords', {'keywordstring': modelspecname}],
        ['nems.xforms.set_random_phi',  {}],
        ['nems.xforms.fit_basic',       {}],
        # ['nems.xforms.add_summary_statistics',    {}],
        ['nems.xforms.plot_summary',    {}],
        # ['nems.xforms.save_recordings', {'recordings': ['est', 'val']}],
        ['nems.xforms.fill_in_default_metadata',    {}],
    ]
"""

    log.info('Initializing modelspec(s) for cell/batch {0}/{1}...'.format(
        cellid, batch))

    # parse modelname
    kws = modelname.split("_")
    loader = kws[0]
    modelspecname = "_".join(kws[1:-1])
    fitter = kws[-1]

    # generate xfspec, which defines sequence of events to load data,
    # generate modelspec, fit data, plot results and save
    xfspec = generate_loader_xfspec(cellid, batch, loader)

    xfspec.append(
        ['nems.xforms.init_from_keywords', {
            'keywordstring': modelspecname
        }])

    # parse the fit spec: Use gradient descent on whole data set(Fast)
    if fitter == "fit01":
        # prefit strf
        log.info("Prefitting STRF without other modules...")
        xfspec.append(['nems.xforms.fit_basic_init', {}])
        xfspec.append(['nems.xforms.fit_basic', {}])
    elif fitter == "fitjk01":
        # prefit strf
        log.info("Prefitting STRF without NL then JK...")
        xfspec.append(['nems.xforms.fit_basic_init', {}])
        xfspec.append(['nems.xforms.split_for_jackknife', {'njacks': 10}])
        xfspec.append(['nems.xforms.fit_basic', {}])
    elif fitter == "fit02":
        # no pre-fit
        log.info("Performing full fit...")
        xfspec.append(['nems.xforms.fit_basic', {}])
    else:
        raise ValueError('unknown fitter string')

    xfspec.append(['nems.xforms.add_summary_statistics', {}])

    if autoPlot:
        # GENERATE PLOTS
        log.info('Generating summary plot...')
        xfspec.append(['nems.xforms.plot_summary', {}])

    # actually do the fit
    ctx, log_xf = xforms.evaluate(xfspec)

    # save some extra metadata
    modelspecs = ctx['modelspecs']

    if 'CODEHASH' in os.environ.keys():
        githash = os.environ['CODEHASH']
    else:
        githash = ""
    meta = {
        'batch': batch,
        'cellid': cellid,
        'modelname': modelname,
        'loader': loader,
        'fitter': fitter,
        'modelspecname': modelspecname,
        'username': '******',
        'labgroup': 'lbhb',
        'public': 1,
        'githash': githash,
        'recording': loader
    }
    if not 'meta' in modelspecs[0][0].keys():
        modelspecs[0][0]['meta'] = meta
    else:
        modelspecs[0][0]['meta'].update(meta)
    destination = '/auto/data/tmp/modelspecs/{0}/{1}/{2}/'.format(
        batch, cellid, ms.get_modelspec_longname(modelspecs[0]))
    modelspecs[0][0]['meta']['modelpath'] = destination
    modelspecs[0][0]['meta']['figurefile'] = destination + 'figure.0000.png'

    # save results

    xforms.save_analysis(destination,
                         recording=ctx['rec'],
                         modelspecs=modelspecs,
                         xfspec=xfspec,
                         figures=ctx['figures'],
                         log=log_xf)
    log.info('Saved modelspec(s) to {0} ...'.format(destination))

    # save in database as well
    if saveInDB:
        # TODO : db results
        nd.update_results_table(modelspecs[0])

    return ctx
예제 #6
0
# shorthand for:
# ctx = {}
# for xfa in xfspec:
#     ctx = xforms.evaluate_step(xfa, ctx)

if browse_results:
    import nems.gui.editors as gui
    ex = gui.browse_xform_fit(ctx, xfspec)

if save_results:
    # ----------------------------------------------------------------------------
    # SAVE YOUR RESULTS

    # save results to file
    destination = os.path.join(results_dir, str(batch), xforms.get_meta(ctx)['cellid'],
                               ms.get_modelspec_longname(ctx['modelspec']))
    log.info('Saving modelspec(s) to {0} ...'.format(destination))
    xforms.save_analysis(destination,
                         recording=ctx['rec'],
                         modelspec=ctx['modelspec'],
                         xfspec=xfspec,
                         figures=ctx['figures'],
                         log=log_xf)

    # save summary of results to a database
    log.info('Saving metadata to db  ...')
    modelspec = ctx['modelspec']
    modelspec.meta['modelpath'] = destination
    modelspec.meta['figurefile'] = destination + 'figure.0000.png'
    nd.update_results_table(modelspec)