Exemple #1
0
def test_get_c3ds():
    """Test c3ds getter"""
    # should get all
    c3ds = sessionutils.get_c3ds(sessiondir_abs)
    assert len(c3ds) == 22
    c3ds = sessionutils.get_c3ds(sessiondir_abs, trial_type='dynamic')
    assert len(c3ds) == 21
    c3ds = sessionutils.get_c3ds(sessiondir_abs, trial_type='static')
    assert len(c3ds) == 1
    c3ds = sessionutils.get_c3ds(sessiondir_abs, tags=['E1'])
    assert len(c3ds) == 1
    c3ds = sessionutils.get_c3ds(sessiondir_abs, tags=['foo'])
    assert len(c3ds) == 0
Exemple #2
0
def test_average_model_data():
    """Test averaging of model data"""
    c3ds = sessionutils.get_c3ds(sessiondir_abs, trial_type='dynamic')
    data_all, nc = stats.collect_trial_data(c3ds,
                                            collect_types={
                                                'model': True,
                                                'emg': False
                                            })
    data_model = data_all['model']
    avgdata, stddata, ncycles_ok = stats.average_model_data(
        data_model, reject_outliers=None)
    # test whether data was averaged for all vars
    # except CGM2 forefoot (which are not in the c3d data)
    desired_vars = set.union(
        set(models.pig_lowerbody.varnames),
        set(models.pig_lowerbody_kinetics.varnames),
        set(models.musclelen.varnames),
    ) - set(
        [var for var in models.pig_lowerbody.varnames if 'ForeFoot' in var])
    for var in desired_vars:
        assert avgdata[var] is not None and avgdata[var].shape == (101, )
        assert stddata[var] is not None and stddata[var].shape == (101, )
    # test with median stats
    avgdata, stddata, ncycles_ok = stats.average_model_data(
        data_model, reject_outliers=None, use_medians=True)
    for var in desired_vars:
        assert avgdata[var] is not None and avgdata[var].shape == (101, )
        assert stddata[var] is not None and stddata[var].shape == (101, )
    # test outlier rejection
    avgdata, stddata, ncycles_ok_reject = stats.average_model_data(
        data_model, reject_outliers=1e-3)
    for var in desired_vars:
        assert avgdata[var] is not None and avgdata[var].shape == (101, )
        assert stddata[var] is not None and stddata[var].shape == (101, )
    assert any(ncycles_ok_reject[var] < ncycles_ok[var] for var in ncycles_ok)
Exemple #3
0
def test_rm_dead_channels():
    """Test removing dead chs from EMG layout"""
    cfg.emg.autodetect_bads = True    
    c3ds = sessionutils.get_c3ds(sessiondir2_abs, trial_type='dynamic')
    emgs = [trial.Trial(fn).emg for fn in c3ds]
    emg = emgs[0]
    lout = [
        ['LGlut', 'RGlut'],
        ['LHam', 'RHam'],
        ['LRec', 'RRec'],
        ['LVas', 'RVas'],
        ['LTibA', 'RTibA'],
        ['LPer', 'RPer'],
        ['LGas', 'RGas'],
        ['LSol', 'RSol'],
    ]
    lout_ = [
        ['LHam', 'RHam'],
        ['LRec', 'RRec'],
        ['LTibA', 'RTibA'],
        ['LPer', 'RPer'],
        ['LGas', 'RGas'],
        ['LSol', 'RSol'],
    ]
    # assert that Glut and Vas get removed for both single and multiple EMG
    # instances
    assert layouts._rm_dead_channels(emg, lout) == lout_
    assert layouts._rm_dead_channels(emgs, lout) == lout_
    cfg.emg.autodetect_bads = False
Exemple #4
0
def test_collect_trial_data():
    """Test collection of trial data"""
    c3ds = sessionutils.get_c3ds(sessiondir_abs, trial_type='dynamic')
    data_all, cycles_all = stats.collect_trial_data(c3ds)
    data_model = data_all['model']
    collected_vars = set(data_model.keys())
    # test whether data was collected for all vars
    # except CGM2 forefoot (which are not in the c3d data)
    desired_vars = (set.union(
        set(models.pig_lowerbody.varnames),
        set(models.pig_lowerbody_kinetics.varnames),
        set(models.musclelen.varnames),
    ) - set(
        [var for var in models.pig_lowerbody.varnames if 'ForeFoot' in var]))
    assert collected_vars == desired_vars
    # check that the correct number of cycles was collected
    assert len(cycles_all['model']['LHipMomentX']) == 17
    assert data_model['LHipMomentX'].shape[0] == 17
    assert data_model['RAnkleMomentX'].shape[0] == 19
    assert len(cycles_all['model']['RKneeAnglesX']) == 46
    assert data_model['RKneeAnglesX'].shape[0] == 46
    assert data_model['fubar'] is None
    # forceplate cycles only
    data_all, cycles_all = stats.collect_trial_data(c3ds, fp_cycles_only=True)
    data_model = data_all['model']
    assert data_model['RKneeAnglesX'].shape[0] == 19
    assert data_model['RAnkleMomentX'].shape[0] == 19
    assert data_model['RKneeAnglesX'].shape[1] == 101
    assert len(cycles_all['model']['RKneeAnglesX']) == 19
    # EMG data collection
    data_all, cycles_all = stats.collect_trial_data(c3ds,
                                                    collect_types=['emg'],
                                                    analog_len=501)
    assert 'model' not in data_all
    data_emg = data_all['emg']
    emg_chs = [
        'LGas',
        'LGlut',
        'LHam',
        'LPer',
        'LRec',
        'LSol',
        'LTibA',
        'LVas',
        'RGas',
        'RGlut',
        'RHam',
        'RPer',
        'RRec',
        'RSol',
        'RTibA',
        'RVas',
    ]
    assert set(data_emg.keys()) == set(emg_chs)
    assert all(data.shape[0] == 46 for ch, data in data_emg.items()
               if ch[0] == 'R')
    assert all(data.shape[0] == 42 for ch, data in data_emg.items()
               if ch[0] == 'L')
    assert all(data.shape[1] == 501 for data in data_emg.values())
Exemple #5
0
def test_trials_extract_values():
    """Test curve value extraction from trials"""
    c3ds = sessionutils.get_c3ds(sessiondir_abs, trial_type='dynamic')
    extr_vals = stats._trials_extract_values(
        c3ds, from_models=[models.pig_lowerbody])
    # forefoot vars are not present in our test data
    forefoot = models._list_with_context(
        ['ForeFootAnglesX', 'ForeFootAnglesY', 'ForeFootAnglesZ'])
    assert set(extr_vals.keys()) == (set(models.pig_lowerbody.varnames) -
                                     set(forefoot))
Exemple #6
0
def test_avgtrial():
    """Test the AvgTrial class"""
    # create from trials
    c3ds = sessionutils.get_c3ds(sessiondir_abs, trial_type='dynamic')
    atrial = stats.AvgTrial.from_trials(
        c3ds,
        sessionpath=sessiondir_abs,
        reject_outliers=1e-3,
    )
    assert atrial.sessionpath == sessiondir_abs
    assert atrial.trialname
    assert atrial.t.shape == (101, )
    assert len(atrial.cycles) == atrial.ncycles == 2
    assert atrial.cycles[0].trial == atrial
    with pytest.raises(ValueError):
        atrial.set_norm_cycle(None)
    adata, t = atrial.get_model_data('RKneeAnglesX')
    assert adata.shape == (101, )
    cycs = atrial.get_cycles('all')
    assert len(cycs) == 2
    cycs = atrial.get_cycles('forceplate')
    assert len(cycs) == 2
    # create from already averaged data
    data_all, nc = stats.collect_trial_data(c3ds,
                                            collect_types={
                                                'model': True,
                                                'emg': True
                                            })
    data_model = data_all['model']
    avgdata_model, stddata_model, ncycles_ok = stats.average_model_data(
        data_model, reject_outliers=None)
    data_emg = data_all['emg']
    avgdata_emg, stddata_emg, ncycles_ok = stats.average_analog_data(
        data_emg, reject_outliers=None)
    atrial = stats.AvgTrial(
        avgdata_model=avgdata_model,
        stddata_model=stddata_model,
        avgdata_emg=avgdata_emg,
        stddata_emg=stddata_emg,
        sessionpath=sessiondir_abs,
        nfiles=len(c3ds),
    )
    assert atrial.sessionpath == sessiondir_abs
    assert atrial.trialname
    assert atrial.t.shape == (101, )
    assert len(atrial.cycles) == atrial.ncycles == 2
    assert atrial.cycles[0].trial == atrial
    with pytest.raises(ValueError):
        atrial.set_norm_cycle(None)
    adata, t = atrial.get_model_data('RKneeAnglesX')
    assert adata.shape == (101, )
    cycs = atrial.get_cycles('all')
    assert len(cycs) == 2
    cycs = atrial.get_cycles('forceplate')
    assert len(cycs) == 2
Exemple #7
0
def test_plot_trials():
    """Test individual trial plotter"""
    c3ds = sessionutils.get_c3ds(sessiondir_abs)
    trials = [trial.Trial(fn) for fn in c3ds]
    for backend in ['plotly', 'matplotlib']:
        # XXX: we don't test the figs for the time being
        fig = plots.plot_trials(trials, backend=backend)
        # fig = plots.plot_trials(trials, backend='plotly')
        tr = trials[0]
        # test different cycle args
        fig = plots.plot_trials(tr, cycles='all', backend=backend)
        fig = plots.plot_trials(tr, cycles='forceplate', backend=backend)
        fig = plots.plot_trials(tr, cycles='unnormalized', backend=backend)
        with pytest.raises(ValueError):
            fig = plots.plot_trials(tr, cycles='foo', backend=backend)
        fig = plots.plot_trials(tr, cycles={'emg': 'all'}, backend=backend)
        fig = plots.plot_trials(tr, cycles={'emg': 0}, backend=backend)
Exemple #8
0
def get_static_files(subject, newer_than=None, rootdir=None):
    """ Get trial files according to given subject and trial type
    (e.g. 'normal') and file extension """

    rootdir = rootdir or params['rootdir']
    logger.debug('finding static trial files for %s' % subject)
    # try to auto find data dirs under subject dir
    subjdir = op.join(rootdir, subject)
    if not op.isdir(subjdir):
        logger.warning('Subject directory not found: %s' % subjdir)
        return list()

    datadirs = [
        file for file in os.listdir(subjdir)
        if op.isdir(op.join(subjdir, file))
    ]

    logger.debug('subject data dirs: %s' % datadirs)

    for datadir in datadirs:

        logger.debug('trying data dir %s/%s' % (subject, datadir))
        sessiondir = op.join(subjdir, datadir)

        files = sessionutils.get_c3ds(sessiondir,
                                      trial_type='static',
                                      check_if_exists=True)

        if not files:
            logger.debug('%s is probably not a CP data dir' % datadir)
            continue

        if newer_than is not None:
            sessiondate = sessionutils.get_session_date(sessiondir)
            logger.info('session %s timestamp %s' % (datadir, sessiondate))
            if sessiondate < newer_than:
                logger.info('session %s too old' % datadir)
                continue

        logger.debug('subject %s, datadir %s, static trials: found %d files' %
                     (subject, sessiondir, len(files)))
        return files

    # we did not hit return
    logger.info('no acceptable static trials found for %s' % subject)
    return list()
Exemple #9
0
def test_collect_trial_data():
    """Test collection of trial data"""
    c3ds = sessionutils.get_c3ds(sessiondir_abs, trial_type='dynamic')
    data_all, nc = stats.collect_trial_data(c3ds)
    data_model = data_all['model']
    collected_vars = set(data_model.keys())
    # test whether data was collected for all vars
    # except CGM2 forefoot (which are not in the c3d data)
    desired_vars = set.union(
        set(models.pig_lowerbody.varnames),
        set(models.pig_lowerbody_kinetics.varnames),
        set(models.musclelen.varnames),
    ) - set(
        [var for var in models.pig_lowerbody.varnames if 'ForeFoot' in var])
    assert collected_vars == desired_vars
    # check that correct number of cycles was collected
    assert nc == {'R_fp': 19, 'R': 54, 'L': 53, 'L_fp': 17}
    assert data_model['RKneeAnglesX'].shape[0] == nc['R']
    assert data_model['RAnkleMomentX'].shape[0] == nc['R_fp']
    assert data_model['RKneeAnglesX'].shape[1] == 101
    assert data_model['fubar'] is None
    # forceplate cycles only
    data_all, nc = stats.collect_trial_data(c3ds, fp_cycles_only=True)
    data_model = data_all['model']
    assert nc == {'R_fp': 19, 'R': 19, 'L': 17, 'L_fp': 17}
    assert data_model['RKneeAnglesX'].shape[0] == nc['R']
    assert data_model['RAnkleMomentX'].shape[0] == nc['R_fp']
    assert data_model['RKneeAnglesX'].shape[1] == 101
    # EMG data collection
    data_all, nc = stats.collect_trial_data(c3ds,
                                            collect_types={
                                                'model': False,
                                                'emg': True
                                            },
                                            analog_len=501)
    assert 'model' not in data_all
    data_emg = data_all['emg']
    assert set(data_emg.keys()) == set(cfg.emg.channel_labels.keys())
    assert all(data.shape[0] == nc['L'] for ch, data in data_emg.items()
               if ch[0] == 'L')
    assert all(data.shape[0] == nc['R'] for ch, data in data_emg.items()
               if ch[0] == 'R')
    assert all(data.shape[1] == 501 for data in data_emg.values())
Exemple #10
0
def test_convert_videos():
    c3d_file = sessionutils.get_c3ds(
        sessiondir_abs, tags=None, trial_type='dynamic', check_if_exists=False
    )[0]
    original_vids = videos.get_trial_videos(c3d_file, vid_ext='.avi', overlay=False)
    assert len(original_vids) == 3
    target_vids = videos.get_trial_videos(c3d_file, vid_ext='.ogv', overlay=False)
    # remove target videos if they exist
    for vidfile in target_vids:
        vidfile.unlink()
    # check should not find target videos any more
    assert not videos.convert_videos(original_vids, check_only=True)
    # start conversion process
    procs = videos.convert_videos(original_vids)
    assert procs
    completed = False
    # wait in a sleep loop until processes have finished
    while not completed:
        n_complete = len([p for p in procs if p.poll() is not None])
        completed = n_complete == len(procs)
        time.sleep(0.1)
    # now conversion target videos should exist
    assert videos.convert_videos(original_vids, check_only=True)