def main(args=None):
    # The console script created by setuptools takes the cwd off the path.
    sys.path.insert(0, os.getcwd())

    scheme = Schema({'--debug': bool,
                     '--delim': str,
                     '--demo': bool,
                     '--help': bool,
                     '--float': Or(None, str),
                     '--from': Or(None, Use(lambda x: list(map(int, x.split(','))))),
                     '--nan': Or(None, str),
                     '--next': Or(None, str),
                     '--no-index-label': bool,
                     '--not-finished': bool,
                     '-o': Or(None, str),
                     '--skip': Or(None, Use(lambda x: x.split(','))),
                     '--skip-parents': bool,
                     '--version': bool,
                     '<data-file>': Or(None, str),
                     '<exp-file>': Or(lambda x: x is None, os.path.exists, error='Invalid <exp-file>'),
                     '<level>': [str],
                     '<n>': [And(Use(int), lambda n: n > 0)],
                     'export': bool,
                     'resume': bool,
                     'run': bool,
                     })

    options = scheme.validate(docopt(__doc__, argv=args, version=__version__))

    if options['--debug']:
        logging.basicConfig(level=logging.DEBUG)

    if options['run'] or options['resume']:
        exp = Experiment.load(options['<exp-file>'])
        kwargs = {'demo': options['--demo'],
                  'parent_callbacks': not options['--skip-parents'],
                  'resume': options['resume'],
                  'session_options': options['-o'],
                  'from_section': options['--from'],
                  }

        if options['--next']:
            kwargs.update(section_obj=exp.find_first_not_run(
                options['--next'], by_started=not options['--not-finished']))

        elif options['resume'] and not options['<n>']:
            kwargs.update(section_obj=exp.find_first_partially_run(options['<level>'][0]))

        else:
            kwargs.update(zip(options['<level>'], options['<n>']))

        run_experiment_section(exp, **kwargs)

    elif options['export']:
        export_experiment_data(options['<exp-file>'], options['<data-file>'],
                               float_format=options['--float'],
                               skip_columns=options['--skip'],
                               index_label=False if options['--no-index-label'] else None,
                               na_rep=options['--nan'],
                               sep=options['--delim'])
def test_exception():
    exp = make_blocked_exp()
    exp.set_run_callback(bad_trial)
    exp.save()
    exp.save('test.pkl')
    with pytest.raises(QuitSession):
        run_experiment_section('test.pkl', participant=1)

    exp = Experiment.load('test.pkl')
    assert exp.subsection(participant=1, block=1, trial=1).has_started
    assert not exp.subsection(participant=1, block=1, trial=1).has_finished
    os.remove('test.pkl')

    e = QuitSession('message')
    assert e.__str__() == 'message'
    with pytest.raises(QuitSession):
        raise e
def test_exception():
    exp = make_blocked_exp()
    exp.add_callback('trial', bad_trial)
    exp.save()
    exp.save('test.yaml')
    with pytest.raises(QuitSession):
        run_experiment_section('test.yaml', participant=1)

    exp = Experiment.load('test.yaml')
    assert exp.subsection(participant=1, block=1, trial=1).has_started
    assert not exp.subsection(participant=1, block=1, trial=1).has_finished
    os.remove('test.yaml')

    e = QuitSession('message')
    assert e.__str__() == 'message'
    with pytest.raises(QuitSession):
        raise e
Exemple #4
0
def main(args=None):
    # The console script created by setuptools takes the cwd off the path.
    sys.path.insert(0, os.getcwd())

    scheme = Schema({
        '--debug':
        bool,
        '--delim':
        str,
        '--demo':
        bool,
        '--help':
        bool,
        '--float':
        Or(None, str),
        '--from':
        Or(None, Use(lambda x: list(map(int, x.split(','))))),
        '--nan':
        Or(None, str),
        '--next':
        Or(None, str),
        '--no-index-label':
        bool,
        '--not-finished':
        bool,
        '-o':
        Or(None, str),
        '--skip':
        Or(None, Use(lambda x: x.split(','))),
        '--skip-parents':
        bool,
        '--version':
        bool,
        '<data-file>':
        Or(None, str),
        '<exp-file>':
        Or(lambda x: x is None, os.path.exists, error='Invalid <exp-file>'),
        '<level>': [str],
        '<n>': [And(Use(int), lambda n: n > 0)],
        'export':
        bool,
        'resume':
        bool,
        'run':
        bool,
    })

    options = scheme.validate(docopt(__doc__, argv=args, version=__version__))

    if options['--debug']:
        logging.basicConfig(level=logging.DEBUG)

    if options['run'] or options['resume']:
        exp = Experiment.load(options['<exp-file>'])
        kwargs = {
            'demo': options['--demo'],
            'parent_callbacks': not options['--skip-parents'],
            'resume': options['resume'],
            'session_options': options['-o'],
            'from_section': options['--from'],
        }

        if options['--next']:
            kwargs.update(section_obj=exp.find_first_not_run(
                options['--next'], by_started=not options['--not-finished']))

        elif options['resume'] and not options['<n>']:
            kwargs.update(section_obj=exp.find_first_partially_run(
                options['<level>'][0]))

        else:
            kwargs.update(zip(options['<level>'], options['<n>']))

        run_experiment_section(exp, **kwargs)

    elif options['export']:
        export_experiment_data(
            options['<exp-file>'],
            options['<data-file>'],
            float_format=options['--float'],
            skip_columns=options['--skip'],
            index_label=False if options['--no-index-label'] else None,
            na_rep=options['--nan'],
            sep=options['--delim'])
Exemple #5
0
    if opts['--debug']:
        logging.basicConfig(level=logging.DEBUG)
        logging.debug('DEMO MODE = {}.'.format(opts['--demo']))

    if opts['new']:
        settings = {'participant': dict(n=int(opts['--participants'])),
                    'block': dict(sort='random', ivs={'perception': ['haptic', 'visual']}),
                    'trial': dict(n=int(opts['--trials']), sort='random', ivs={'angle': list(range(1, 8))})}
        levels = ('participant', 'block', 'trial')
        RampExperiment(settings, levels=levels, experiment_file=opts['<experiment_file>'])

    elif opts['run']:
        experiment_file = opts['<experiment_file>']
        by_started = not opts['--finished']

        section_dict = {level: int(n) for level, n in zip(opts['<level>'], opts['<n>'])}
        experiment = RampExperiment.load_experiment(experiment_file)

        if section_dict:
            # If section not specified, could be overwriting data
            experiment.save(experiment_file + datetime.now().strftime('.%m-%d-%H-%M-backup'))
            section = experiment.find_section(**section_dict)
        else:
            section = experiment.find_first_not_run('participant', by_started=by_started)

        exp.run_experiment_section(experiment, demo=opts['--demo'], section=section)
        experiment.save(experiment_file)

    elif opts['export']:
        exp.export_experiment_data(opts['<experiment_file>'], opts['<data_file>'])