示例#1
0
文件: test_utils.py 项目: LBHB/NEMS
def test_split_and_join_escaped():
    s = r'split_me_on\_underscores'
    assert escaped_join(escaped_split(s, '_'), '_') == s

    s = r'split_me_on_\_underscores'
    assert escaped_join(escaped_split(s, '_'), '_') == s

    s = r'split_me_on__\underscores'
    assert escaped_join(escaped_split(s, '_'), '_') == s
示例#2
0
def test_split_and_join():
    s = 'my-escaped_modelspec-name_blah.t5\.5'
    x = escaped_split(s, '_')
    # escaping the . shouldn't matter here, should only escape
    # the active delimiter
    assert len(x) == 3
    blah = x[2]

    # now the . should be ignored during the split, for a length
    # of 2 instead of 3
    assert len(escaped_split(blah, '.')) == 2

    # after the reverse operation, original string shouldl be preserved
    y = escaped_join(x, '_')
    assert y == s
示例#3
0
def init(kw):
    ops = escaped_split(kw, '.')[1:]
    st = False
    tolerance = 10**-5.5
    norm_fir = False
    fit_sig = 'resp'

    for op in ops:
        if op == 'st':
            st = True
        elif op == 'psth':
            fit_sig = 'psth'
        elif op.startswith('t'):
            # Should use \ to escape going forward, but keep d-sub in
            # for backwards compatibility.
            num = op.replace('d', '.').replace('\\', '')
            tolpower = float(num[1:]) * (-1)
            tolerance = 10**tolpower
        elif op == 'L2f':
            norm_fir = True

    if st:
        return [[
            'nems.xforms.fit_state_init', {
                'tolerance': tolerance,
                'fit_sig': fit_sig
            }
        ]]
    else:
        return [[
            'nems.xforms.fit_basic_init', {
                'tolerance': tolerance,
                'norm_fir': norm_fir
            }
        ]]
示例#4
0
def _extract_options(fitkey):
    if fitkey == 'basic' or fitkey == 'iter':
        # empty options (i.e. just use defualts)
        options = []
    else:
        chunks = escaped_split(fitkey, '.')
        options = chunks[1:]
    return options
示例#5
0
文件: test_utils.py 项目: LBHB/NEMS
def test_split_escaped_delimiter():
    splits = ['join', 'on\\', 'me', 'test']
    delims = ['.', '_', '-']

    for d in delims:
        s = d.join(splits)
        split = escaped_split(s, d)
        assert split == ['join', f'on\\{d}me', 'test']
示例#6
0
文件: test_utils.py 项目: LBHB/NEMS
def test_split_normal_delimiter():
    splits = ['join', 'on', 'me', 'test']
    delims = ['.', '_', '-']

    for d in delims:
        s = d.join(splits)
        split = escaped_split(s, d)
        assert split == splits
示例#7
0
def load_model_xform(cellid,
                     batch=271,
                     modelname="ozgf100ch18_wcg18x2_fir15x2_lvl1_dexp1_fit01",
                     eval_model=True,
                     only=None):
    '''
    Load a model that was previously fit via fit_model_xforms

    Parameters
    ----------
    cellid : str
        cellid in celldb database
    batch : int
        batch number in celldb database
    modelname : str
        modelname in celldb database
    eval_model : boolean
        If true, the entire xfspec will be re-evaluated after loading.
    only : int
        Index of single xfspec step to evaluate if eval_model is False.
        For example, only=0 will typically just load the recording.

    Returns
    -------
    xfspec, ctx : nested list, dictionary

    '''

    kws = 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

    d = nd.get_results_file(batch, [modelname], [cellid])
    filepath = d['modelpath'][0]
    # TODO add BAPHY_API support . Not implemented on nems_baphy yet?
    #if get_setting('USE_NEMS_BAPHY_API'):
    #    prefix = '/auto/data/nems_db' # get_setting('NEMS_RESULTS_DIR')
    #    uri = filepath.replace(prefix,
    #                           'http://' + get_setting('NEMS_BAPHY_API_HOST') + ":" + str(get_setting('NEMS_BAPHY_API_PORT')))
    #else:
    #    uri = filepath.replace('/auto/data/nems_db/results', get_setting('NEMS_RESULTS_DIR'))

    # hack: hard-coded assumption that server will use this data root
    uri = filepath.replace('/auto/data/nems_db/results',
                           get_setting('NEMS_RESULTS_DIR'))
    if old:
        raise NotImplementedError("need to use oxf library.")
        xfspec, ctx = oxf.load_analysis(uri, eval_model=eval_model)
    else:
        xfspec, ctx = xforms.load_analysis(uri,
                                           eval_model=eval_model,
                                           only=only)
    return xfspec, ctx
示例#8
0
def _parse_kw_string(kw_string, registry):
    xfspec = []
    for kw in escaped_split(kw_string, '-'):
        try:
            xfspec.extend(registry[kw])
        except KeyError:
            log.error("No keyword found for: '%s', raising KeyError.", kw)
            raise

    return xfspec
示例#9
0
def _parse_kw_string(kw_string, registry):
    xfspec = []
    for kw in escaped_split(kw_string, '-'):
        try:
            xfspec.extend(registry[kw])
        except KeyError:
            log.info("No keyword found for: %s , skipping ...", kw)
            pass

    return xfspec
示例#10
0
def _parse_options(fitkey, **default_options):

    chunks = escaped_split(fitkey, '.')
    ops = chunks[1:]

    # set defaults
    options = {}
    options['max_iter'] = default_options.get('max_iter', 30000)
    options['tolerance'] = default_options.get('tolerance', 1e-5)
    options['fitter'] = default_options.get('fitter', 'scipy_minimize')
    options['choose_best'] = default_options.get('choose_best', False)
    options['rand_count'] = default_options.get('rand_count', 1)

    for op in ops:
        if op.startswith('mi'):
            pattern = re.compile(r'^mi(\d{1,})')
            options['max_iter'] = int(re.match(pattern, op).group(1))

        elif op.startswith('FL'):
            if ':' in op:
                # ex: FL0:5  would be freeze_layers = [0,1,2,3,4]
                lower, upper = [int(i) for i in op[2:].split(':')]
                options['freeze_layers'] = list(range(lower, upper))
            else:
                # ex: FL2x6x9  would be freeze_layers = [2, 6, 9]
                options['freeze_layers'] = [int(i) for i in op[2:].split('x')]
        elif op.startswith('t'):
            # Should use \ to escape going forward, but keep d-sub in
            # for backwards compatibility.
            num = op.replace('d', '.').replace('\\', '')
            tolpower = float(num[1:]) * (-1)
            options['tolerance'] = 10**tolpower
        elif op == 'cd':
            options['fitter'] = 'coordinate_descent'
        elif op == 'b':
            options['choose_best'] = True
        elif op.startswith('rb'):
            if len(op) == 2:
                options['rand_count'] = 10
            else:
                options['rand_count'] = int(op[2:])
            options['choose_best'] = True

    return options
示例#11
0
def best(fitkey):
    '''
    Collapse modelspecs to singleton list with only the "best" modelspec.
    '''
    options = escaped_split(fitkey, '.')[1:]
    metakey = 'r_test'
    comparison = 'greatest'

    for op in options:
        if op == '<':
            comparison = 'least'
        elif op == '>':
            comparison = 'greatest'
        else:
            # Assume it's the name of a metakey, and remove any escapes
            metakey = op.replace('\\', '')

    return [[
        'nems.xforms.only_best_modelspec', {
            'metakey': metakey,
            'comparison': comparison
        }
    ]]
示例#12
0
def sort(fitkey):
    '''
    Sorts modelspecs by specified meta entry in either descending or
    ascending order.
    '''
    ops = escaped_split(fitkey, '.')[1:]
    metakey = 'r_test'
    order = 'descending'

    for op in ops:
        if op in ['a', 'asc', 'ascending']:
            order = op
        elif op in ['d', 'desc', 'descending']:
            order = op
        else:
            # Assume it's the name of a metakey, and remove any escapes
            metakey = op.replace('\\', '')

    return [[
        'nems.xforms.sort_modelspecs', {
            'metakey': metakey,
            'order': order
        }
    ]]
示例#13
0
文件: test_utils.py 项目: LBHB/NEMS
def test_split_and_join_normal():
    s = 'split_me_on_underscores'
    assert escaped_join(escaped_split(s, '_'), '_') == s
示例#14
0
def generate_xforms_spec(recording_uri,
                         modelname,
                         meta={},
                         xforms_kwargs={},
                         kw_kwargs={},
                         autoPred=True,
                         autoStats=True,
                         autoPlot=True):
    """
    TODO: Update this doc

    OUTDATED
    Fits a single NEMS model
    eg, 'ozgf100ch18_wc18x1_lvl1_fir15x1_dexp1_fit01'
    generates modelspec with 'wc18x1_lvl1_fir1x15_dexp1'

    based on fit_model function in nems/scripts/fit_model.py

    example xfspec:
     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 recording/model {0}/{1}...'.format(
        recording_uri, modelname))

    # parse modelname and assemble xfspecs for loader and fitter

    # TODO: naming scheme change: pre_modules, modules, post_modules?
    #       or something along those lines... since they aren't really
    #       just loaders and fitters
    load_keywords, model_keywords, fit_keywords = escaped_split(modelname, '_')

    xforms_lib = KeywordRegistry(recording_uri=recording_uri, **xforms_kwargs)
    xforms_lib.register_modules(
        [default_loaders, default_fitters, default_initializers])
    xforms_lib.register_plugins(get_setting('XFORMS_PLUGINS'))

    keyword_lib = KeywordRegistry(**kw_kwargs)
    keyword_lib.register_module(default_keywords)
    keyword_lib.register_plugins(get_setting('KEYWORD_PLUGINS'))

    # Generate the xfspec, which defines the sequence of events
    # to run through (like a packaged-up script)
    xfspec = []

    # 1) Load the data
    xfspec.extend(_parse_kw_string(load_keywords, xforms_lib))

    # 2) generate a modelspec
    xfspec.append([
        'nems.xforms.init_from_keywords', {
            'keywordstring': model_keywords,
            'meta': meta,
            'registry': keyword_lib
        }
    ])

    # 3) fit the data
    xfspec.extend(_parse_kw_string(fit_keywords, xforms_lib))

    # TODO: need to make this smarter about how to handle the ordering
    #       of pred/stats when only stats is overridden.
    #       For now just have to manually include pred if you want to
    #       do your own stats or plot xform (like using stats.pm)

    # 4) generate a prediction (optional)
    if autoPred:
        if not _xform_exists(xfspec, 'nems.xforms.predict'):
            xfspec.append(['nems.xforms.predict', {}])

    # 5) add some performance statistics (optional)
    if autoStats:
        if not _xform_exists(xfspec, 'nems.xforms.add_summary_statistics'):
            xfspec.append(['nems.xforms.add_summary_statistics', {}])

    # 6) generate plots (optional)
    if autoPlot:
        if not _xform_exists(xfspec, 'nems.xforms.plot_summary'):
            log.info('Adding summary plot to xfspec...')
            xfspec.append(['nems.xforms.plot_summary', {}])

    return xfspec
示例#15
0
def init(kw):
    '''
    Initialize modelspecs in an attempt to avoid getting stuck in
    local minima.
    Written/optimized to work for (dlog)-wc-(stp)-fir-(dexp) architectures
    optional modules in (parens)

    Parameter
    ---------
    kw : string
        A string of the form: init.option1.option2...

    Options
    -------
    tN : Set tolerance to 10**-N, where N is any positive integer.
    st : Remove state replication/merging before initializing.
    psth : Initialize by fitting to 'psth' intead of 'resp' (default)
    nlN : Initialize nonlinearity with version N
        For dexp, options are {1,2} (default is 2),
            pre 11/29/18 models were fit with v1
            1: amp = np.nanstd(resp) * 3
               kappa = np.log(2 / (np.max(pred) - np.min(pred) + 1))
            2:
               amp = resp[pred>np.percentile(pred,90)].mean()
               kappa = np.log(2 / (np.std(pred)*3))
        For other nonlinearities, mode is not specified yet
    L2f : normalize fir (default false)
    .rN : initialize with N random phis drawn from priors (via init.rand_phi),
          default N=10
    .rbN : initialize with N random phis drawn from priors (via init.rand_phi),
           and pick best mse_fit, default N=10
    .iN : include module N. ie, fit phi for this module. if not specified,
          defaults to 0:len(modelspec). can be repeated for multiple
          modules
    .inegN : include module len(modelspec)-N
    .iiN : include modules 0:(N+1) -- including N! Note that .ii behavior
           differs from .ff and .xx
    .iinegN : include modules len(modelspec)-N:len(modelspec)
    .fN : freeze module N. ie, keep module in model but keep phi fixed
    .fnegN : freeze module len(modelspec)-N
    .ffN : freeze modules N:len(modelspec). Note that .ii behavior
           differs from .ff and .xx
    .ffnegN : freeze modules len(modelspec)-N:len(modelspec)
    .xN : exclude module N. ie, remove from model, assume that it won't break!
    .xnegN : exclude module len(modelspec)-N
    .xxN : exclude modules N:len(modelspec). Note that .ii behavior
           differs from .ff and .xx
    .xxnegN : exclude modules len(modelspec)-N:len(modelspec)
    .n

    TODO: Optimize more, make testbed to check how well future changes apply
    to disparate datasets.

    '''

    ops = escaped_split(kw, '.')[1:]
    st = False
    tf = False

    max_iter = 2000
    tolerance = 10**-5.5
    norm_fir = False
    fit_sig = 'resp'
    nl_kw = {}

    rand_count = 0
    keep_best = False

    sel_options = {'include_idx': [], 'exclude_idx': [], 'freeze_idx': []}
    metric_options = {'metric': 'nmse', 'alpha': 0}

    # TODO add support?
    initialize_nl = False

    # TF- specific parameters
    # TODO: integrate with others, share processing with regular basic/tf
    # eg, early_stopping_tolerance =?= tolerance
    # loss_type =?= metric
    use_modelspec_init = False
    fitter = 'Adam'
    loss_type = 'squared_error'
    early_stopping_steps = 5
    early_stopping_tolerance = 1e-5
    learning_rate = 0.01
    distr = 'norm'
    keep_n = 1
    generate_predictions_for_each_initalization_condition = False

    for op in ops:
        if op == 'st':
            st = True
        elif op.startswith('tf'):
            tf = True
            max_iter = 10000
        elif op.startswith('it'):
            if len(op[2:]) == 0:
                max_iter = None
            else:
                max_iter = int(op[2:])
        elif op == 'psth':
            fit_sig = 'psth'
        elif op.startswith('nl'):
            nl_kw = {'nl_mode': int(op[2:])}
        elif op.startswith('t'):
            # Should use \ to escape going forward, but keep d-sub in
            # for backwards compatibility.
            num = op.replace('d', '.').replace('\\', '')
            tolpower = float(num[1:]) * (-1)
            tolerance = 10**tolpower
        elif op.startswith('pLV'):
            # pupil dep. cost function
            metric = 'pup_dep_LV'
            alpha = float(op[3:].replace(',', '.'))
            metric_options.update({'metric': metric, 'alpha': alpha})
        elif op == 'L2f':
            norm_fir = True
        elif op.startswith('rbp'):
            keep_best = True
            generate_predictions_for_each_initalization_condition = True
            if len(op) == 3:
                rand_count = 10
            else:
                rand_count = int(op[3:])
        elif op.startswith('rb'):
            if len(op) == 2:
                rand_count = 10
            else:
                rand_count = int(op[2:])
            keep_best = True
            keep_n = 1
        elif op.startswith('r'):
            if len(op) == 1:
                rand_count = 10
            else:
                rand_count = int(op[1:])
        elif op.startswith('b'):
            keep_best = True
            if len(op) == 1:
                keep_n = 1
            else:
                keep_n = int(op[1:])
        elif op.startswith('iineg'):
            sel_options['include_through'] = -int(op[5:])
        elif op.startswith('ineg'):
            sel_options['include_idx'].append(-int(op[4:]))
        elif op.startswith('ii'):
            sel_options['include_through'] = int(op[2:])
        elif op.startswith('i'):
            if not tf:
                sel_options['include_idx'].append(int(op[1:]))
            else:
                if len(op[1:]) == 0:
                    max_iter = None
                else:
                    max_iter = int(op[1:])

        elif op.startswith('ffneg'):
            sel_options['freeze_after'] = -int(op[5:])
        elif op.startswith('fneg'):
            sel_options['freeze_idx'].append(-int(op[4:]))
        elif op.startswith('ff'):
            sel_options['freeze_after'] = int(op[2:])
        elif op.startswith('f'):
            sel_options['freeze_idx'].append(int(op[1:]))
        elif op.startswith('xxneg'):
            sel_options['exclude_after'] = -int(op[5:])
        elif op.startswith('xneg'):
            sel_options['exclude_idx'].append(-int(op[4:]))
        elif op.startswith('xx'):
            sel_options['exclude_after'] = int(op[2:])
        elif op.startswith('x'):
            sel_options['exclude_idx'].append(int(op[1:]))

            # begin TF-specific options
        elif op == 'n':
            use_modelspec_init = True
        elif op.startswith('lr'):
            learning_rate = op[2:]
            if 'e' in learning_rate:
                base, exponent = learning_rate.split('e')
                learning_rate = int(base) * 10**-int(exponent)
            else:
                learning_rate = int(learning_rate)
        elif op[:1] == 'f':
            fitter = op[1:]
            if fitter in ['adam', 'a']:
                fitter = 'Adam'
            elif fitter == 'gd':
                fitter = 'GradientDescent'
        elif op[:1] == 'l':
            loss_type = op[1:]
            if loss_type == 'se':
                loss_type = 'squared_error'
            if loss_type == 'p':
                loss_type = 'poisson'
            if loss_type == 'nmse':
                loss_type = 'nmse'
            if loss_type == 'nmses':
                loss_type = 'nmse_shrinkage'
        elif op.startswith('et'):
            early_stopping_tolerance = 1 * 10**-int(op[2:])
        elif op[:1] == 'e':
            early_stopping_steps = int(op[1:])
        elif op[:1] == 'd':
            distr = op[1:]
            if distr == 'gu':
                distr = 'glorot_uniform'
            elif distr == 'heu':
                distr = 'he_uniform'

    bsel = False
    for key in list(sel_options.keys()):
        value = sel_options[key]
        if (type(value) is list) and (len(value) > 0):
            bsel = True
        elif (type(value) is int):
            bsel = True
        else:
            del sel_options[key]

    xfspec = []
    if rand_count > 0:
        xfspec.append(
            ['nems.initializers.rand_phi', {
                'rand_count': rand_count
            }])

    sel_options.update({
        'tolerance': tolerance,
        'norm_fir': norm_fir,
        'nl_kw': nl_kw
    })
    sel_options.update({
        'max_iter': max_iter,
        'use_modelspec_init': use_modelspec_init,
        'optimizer': fitter,
        'cost_function': loss_type,
        'early_stopping_steps': early_stopping_steps,
        'early_stopping_tolerance': early_stopping_tolerance,
        'learning_rate': learning_rate,
        'distr': distr
    })
    if tf:
        sel_options['fit_function'] = 'nems.tf.cnnlink.fit_tf_init'
        sel_options['use_modelspec_init'] = use_modelspec_init
    elif st:
        sel_options['fit_function'] = 'nems.xforms.fit_state_init'
        sel_options['fit_sig'] = fit_sig
    elif bsel:
        sel_options['fit_function'] = 'nems.xforms.fit_basic_subset'
    else:
        sel_options['fit_function'] = 'nems.xforms.fit_basic_init'

    # save cost function for use by fitter (default is nmse)
    sel_options.update(metric_options)

    xfspec.append(['nems.xforms.fit_wrapper', sel_options])

    if generate_predictions_for_each_initalization_condition:
        xfspec.append([
            'nems.analysis.test_prediction.predict_and_summarize_for_all_modelspec',
            {}
        ])
    if keep_best:
        xfspec.append([
            'nems.analysis.test_prediction.pick_best_phi', {
                'criterion': 'mse_fit',
                'keep_n': keep_n
            }
        ])

    return xfspec
示例#16
0
def fit_model_xform(cellid,
                    batch,
                    modelname,
                    autoPlot=True,
                    saveInDB=False,
                    returnModel=False,
                    recording_uri=None,
                    initial_context=None):
    """
    Fit a single NEMS model using data stored in database. First generates an xforms
    script based on modelname parameter and then evaluates it.
    :param cellid: cellid and batch specific dataset in database
    :param batch:
    :param modelname: string specifying model architecture, preprocessing
    and fit method
    :param autoPlot: generate summary plot when complete
    :param saveInDB: save results to Results table
    :param returnModel: boolean (default False). If False, return savepath
       if True return xfspec, ctx tuple
    :param recording_uri
    :return: savepath = path to saved results or (xfspec, ctx) tuple
    """
    startime = time.time()
    log.info('Initializing modelspec(s) for cell/batch %s/%d...', cellid,
             int(batch))

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

    modelspecname = 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 type(cellid) is list:
        meta['siteid'] = cellid[0][:7]

    # registry_args = {'cellid': cellid, 'batch': int(batch)}
    registry_args = {}
    xforms_init_context = {'cellid': cellid, 'batch': int(batch)}
    if initial_context is not None:
        xforms_init_context.update(initial_context)

    log.info("TODO: simplify generate_xforms_spec parameters")
    xfspec = generate_xforms_spec(recording_uri=recording_uri,
                                  modelname=modelname,
                                  meta=meta,
                                  xforms_kwargs=registry_args,
                                  xforms_init_context=xforms_init_context,
                                  autoPlot=autoPlot)
    log.debug(xfspec)

    # actually do the loading, preprocessing, fit
    if initial_context is None:
        initial_context = {}
    ctx, log_xf = xforms.evaluate(xfspec)  #, context=initial_context)

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

    if type(cellid) is list:
        cell_name = cellid[0].split("-")[0]
    else:
        cell_name = cellid

    if 'modelpath' not in modelspec.meta:
        prefix = get_setting('NEMS_RESULTS_DIR')
        destination = os.path.join(prefix, str(batch), cell_name,
                                   modelspec.get_longname())

        log.info(f'Setting modelpath to "{destination}"')
        modelspec.meta['modelpath'] = destination
        modelspec.meta['figurefile'] = os.path.join(destination,
                                                    'figure.0000.png')
    else:
        destination = modelspec.meta['modelpath']

    # figure out URI for location to save results (either file or http, depending on USE_NEMS_BAPHY_API)
    if get_setting('USE_NEMS_BAPHY_API'):
        prefix = 'http://' + get_setting('NEMS_BAPHY_API_HOST') + ":" + str(get_setting('NEMS_BAPHY_API_PORT')) + \
                 '/results'
        save_loc = str(
            batch) + '/' + cell_name + '/' + modelspec.get_longname()
        save_destination = prefix + '/' + save_loc
        # set the modelspec meta save locations to be the filesystem and not baphy
        modelspec.meta['modelpath'] = get_setting(
            'NEMS_RESULTS_DIR') + '/' + save_loc
        modelspec.meta['figurefile'] = modelspec.meta[
            'modelpath'] + '/' + 'figure.0000.png'
    else:
        save_destination = destination

    modelspec.meta['runtime'] = int(time.time() - startime)
    modelspec.meta.update(meta)

    if returnModel:
        # return fit, skip save!
        return xfspec, ctx

    # save results
    log.info('Saving modelspec(s) to {0} ...'.format(save_destination))
    if 'figures' in ctx.keys():
        figs = ctx['figures']
    else:
        figs = []
    save_data = xforms.save_analysis(save_destination,
                                     recording=ctx.get('rec'),
                                     modelspec=modelspec,
                                     xfspec=xfspec,
                                     figures=figs,
                                     log=log_xf,
                                     update_meta=False)

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

    return save_data['savepath']
示例#17
0
文件: lnp_fit.py 项目: nadoss/nems_db
    # Set initial values and do a rough "pre-fit"
    # Initialize fir coeffs to L2-norm of random values
    "-init.lnp.t8"  #.L2f"
    # Do the full fits
    #"-lnp.t5"
    #"-nestspec"
)

# Equivalent to:
#modelname = "ozgf.fs200.ch18-ld-splitep_dlog.f-wc.18x2.g-fir.2x15-lvl.1-dexp.1_init.t3-lnp.t5"

log.info('Initializing modelspec(s) for cell/batch %s/%d...', cellid,
         int(batch))

# Segment modelname for meta information
kws = escaped_split(modelname, '_')
modelspecname = "-".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', ''),
示例#18
0
def generate_xforms_spec(recording_uri=None,
                         modelname=None,
                         meta={},
                         xforms_kwargs={},
                         kw_kwargs={},
                         autoPred=True,
                         autoStats=True,
                         autoPlot=True):
    """
    Generate an xforms spec based on a modelname, which can then be evaluated
    in order to process and fit a model.

    Parameter
    ---------
    recording_uri : str
        Location to load recording from, e.g. a filepath or URL.
    modelname : str
        NEMS-formatted modelname, e.g. 'ld-sev_wc.18x2-fir.2x15-dexp.1_basic'
        The modelname will be parsed into a series of xforms functions using
        xforms and keyword registries.
    meta : dict
        Additional keyword arguments for nems.initializers.init_from_keywords
    xforms_kwargs : dict
        Additional keyword arguments for the xforms registry
    kw_kwargs : dict
        Additional keyword arguments for the keyword registry
    autoPred : boolean
        If true, will automatically append nems.xforms.predict to the xfspec
        if it is not already present.
    autoStats : boolean
        If true, will automatically append nems.xforms.add_summary_statistics
        to the xfspec if it is not already present.
    autoPlot : boolean
        If true, will automatically append nems.xforms.plot_summary to the
        xfspec if it is not already present.

    Returns
    -------
    xfspec : list of 2- or 4- tuples

    """

    log.info('Initializing modelspec(s) for recording/model {0}/{1}...'.format(
        recording_uri, modelname))

    # parse modelname and assemble xfspecs for loader and fitter

    # TODO: naming scheme change: pre_modules, modules, post_modules?
    #       or something along those lines... since they aren't really
    #       just loaders and fitters
    load_keywords, model_keywords, fit_keywords = escaped_split(modelname, '_')
    if recording_uri is not None:
        xforms_lib = KeywordRegistry(recording_uri=recording_uri,
                                     **xforms_kwargs)
    else:
        xforms_lib = KeywordRegistry(**xforms_kwargs)

    xforms_lib.register_modules(
        [default_loaders, default_fitters, default_initializers])
    xforms_lib.register_plugins(get_setting('XFORMS_PLUGINS'))

    keyword_lib = KeywordRegistry(**kw_kwargs)
    keyword_lib.register_module(default_keywords)
    keyword_lib.register_plugins(get_setting('KEYWORD_PLUGINS'))

    # Generate the xfspec, which defines the sequence of events
    # to run through (like a packaged-up script)
    xfspec = []

    # 1) Load the data
    xfspec.extend(_parse_kw_string(load_keywords, xforms_lib))

    # 2) generate a modelspec
    xfspec.append([
        'nems.xforms.init_from_keywords', {
            'keywordstring': model_keywords,
            'meta': meta,
            'registry': keyword_lib
        }
    ])

    # 3) fit the data
    xfspec.extend(_parse_kw_string(fit_keywords, xforms_lib))

    # TODO: need to make this smarter about how to handle the ordering
    #       of pred/stats when only stats is overridden.
    #       For now just have to manually include pred if you want to
    #       do your own stats or plot xform (like using stats.pm)

    # 4) generate a prediction (optional)
    if autoPred:
        if not _xform_exists(xfspec, 'nems.xforms.predict'):
            xfspec.append(['nems.xforms.predict', {}])

    # 5) add some performance statistics (optional)
    if autoStats:
        if not _xform_exists(xfspec, 'nems.xforms.add_summary_statistics'):
            xfspec.append(['nems.xforms.add_summary_statistics', {}])

    # 6) generate plots (optional)
    if autoPlot:
        if not _xform_exists(xfspec, 'nems.xforms.plot_summary'):
            # log.info('Adding summary plot to xfspec...')
            xfspec.append(['nems.xforms.plot_summary', {}])

    return xfspec
示例#19
0
# xfspec = xhelp.generate_xforms_spec(recording_uri, modelname, meta)
"""
{'stim': 0, 'chancount': 0, 'pupil': 1, 'rasterfs': 20, 'rawid': None, 'cellid': 'BRT026c-15-1', 'pupil_median': 0, 'pertrial': 0, 'pupil_deblink': 1, 'stimfmt': 'parm', 'runclass': None, 'includeprestim': 1, 'batch': 307}
{'stimfmt': 'parm', 'chancount': 0, 'pupil': 1, 'rasterfs': 20, 'rawid': None, 'cellid': 'BRT026c-15-1', 'pupil_median': 0, 'pertrial': 0, 'pupil_deblink': 1, 'stim': 0, 'runclass': None, 'includeprestim': 1, 'batch': 307}
"""
#log.info('Initializing modelspec(s) for recording/model {0}/{1}...'
#         .format(recording_uri, modelname))
xforms_kwargs = {}
xforms_init_context = {'cellid': cellid, 'batch': int(batch)}
recording_uri = None
kw_kwargs ={}

# equivalent of xform_helper.generate_xforms_spec():

# parse modelname and assemble xfspecs for loader and fitter
load_keywords, model_keywords, fit_keywords = escaped_split(modelname, '_')
if recording_uri is not None:
    xforms_lib = KeywordRegistry(recording_uri=recording_uri, **xforms_kwargs)
else:
    xforms_lib = KeywordRegistry(**xforms_kwargs)

xforms_lib.register_modules([default_loaders, default_fitters,
                             default_initializers])
xforms_lib.register_plugins(get_setting('XFORMS_PLUGINS'))

keyword_lib = KeywordRegistry()
keyword_lib.register_module(default_keywords)
keyword_lib.register_plugins(get_setting('KEYWORD_PLUGINS'))

# Generate the xfspec, which defines the sequence of events
# to run through (like a packaged-up script)
示例#20
0
#lr = 0.005, mi=1000, eval_interval = 75 --- fit=0.689, test=0.691
#lr = 0.01, mi=2500, eval_interval = 75 --- fit=0.686, test=0.684
modelname = "ozgf.fs100.ch18-ld-norm-sev_dlog-wc.18x16-fir.4x18x4-relu.4-wc.4x1_tf.n.rb10"


autoPlot = True
saveInDB = False
save_data = False
browse_results = False

log.info('Initializing modelspec(s) for cell/batch %s/%d...',
         cellid, int(batch))

# Segment modelname for meta information, specifically the parts between _s
kws = escaped_split(modelname, '_')

modelspecname = escaped_join(kws[1:-1], '-')
loadkey = kws[0]                                             #name of loader, samplying freq,channels
fitkey = kws[-1]                                             #what the fitter will be (in my case now NEMS or tf

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 type(cellid) is list:
    meta['siteid'] = cellid[0][:7]

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