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 load_data(filename, stack=True, collapse_unsuccesful=True, drop_outbound=None, drop_return=None): if drop_outbound is None: drop_outbound = [] if drop_return is None: drop_return = [] df = Experiment.load(filename).dataframe.dropna() for column, type_ in [ ('height', 'int'), ('hip_height', 'float'), ('knee_height', 'float'), ('lowest_height_not_afforded', 'int'), ]: df[column] = df[column].astype(type_) df.loc[df.index.get_level_values('participant').isin(drop_outbound), 'outbound'] = None df.loc[df.index.get_level_values('participant').isin(drop_return), 'return'] = None df['relative_height'] = df['height'] - df['lowest_height_not_afforded'] df['scaled_height'] = df['height'] / df['lowest_height_not_afforded'] if collapse_unsuccesful: for phase in _phases: df.loc[df[phase] == 'unsuccessful', phase] = 'over' if stack: df = pd.concat((df, df), keys=_phases, names=['phase']) df = df.reset_index().set_index(['participant', 'trial', 'phase']).sort() df['action'] = [row[phase] for (p, t, phase), row in df.iterrows()] for phase in _phases: del df[phase] df = df.dropna() return df
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
def test_cli(): exp = make_blocked_exp() exp.set_context_manager('participant', participant_context) exp.set_context_manager('block', block_context) exp.filename = 'test.pkl' exp.save() call_cli('exp run test.pkl --next participant') exp = Experiment.load('test.pkl') for row in exp.dataframe.iterrows(): if row[0][0] == 1: yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp --demo run test.pkl participant 2 block 1') for row in exp.dataframe.iterrows(): if row[0][0] == 1: yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp run test.pkl participant 2 block 1') exp = Experiment.load('test.pkl') for row in exp.dataframe.iterrows(): if row[0][0] == 1 or (row[0][0] == 2 and row[0][1] == 1): yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp resume test.pkl participant') exp = Experiment.load('test.pkl') for row in exp.dataframe.iterrows(): if row[0][0] <= 2: yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp run test.pkl --next trial') exp = Experiment.load('test.pkl') for row in exp.dataframe.iterrows(): if row[0][0] <= 2 or row[0] == (3, 1, 1): yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp resume test.pkl participant 3 block 1 --demo') exp = Experiment.load('test.pkl') for row in exp.dataframe.iterrows(): if row[0][0] <= 2 or row[0] == (3, 1, 1): yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp --debug resume test.pkl participant 3 block 1') exp = Experiment.load('test.pkl') for row in exp.dataframe.iterrows(): if row[0][0] <= 2 or row[0][:2] == (3, 1): yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp run test.pkl participant 3 block 3 --from 3') exp = Experiment.load('test.pkl') for row in exp.dataframe.iterrows(): if row[0][0] <= 2 or row[0][:2] == (3, 1) or (row[0][:2] == (3, 3) and row[0][-1] >= 3): yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp run test.pkl participant 3 --from 2,4') exp = Experiment.load('test.pkl') for row in exp.dataframe.iterrows(): if (row[0][0] <= 2 or row[0][:2] == (3, 1) or (row[0][:2] == (3, 2) and row[0][-1] >= 4) or (row[0][:2] == (3, 3) and row[0][2] > 2)): yield check_trial, row else: assert isnan(row[1]['result']) for file in glob('test.pkl*'): os.remove(file)
def test_exp_repr(): make_deterministic_exp() e = Experiment.load('test.pkl') assert e == eval(repr(e)) for file in glob('test.pkl*'): os.remove(file)
def test_cli(): exp = make_blocked_exp() exp.add_callback('participant', participant_context, is_context=True) exp.add_callback('block', block_context, is_context=True) exp.filename = 'test.yaml' exp.save() call_cli('exp run test.yaml --next participant') exp = Experiment.load('test.yaml') for row in exp.dataframe.iterrows(): if row[0][0] == 1: yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp --demo run test.yaml participant 2 block 1') for row in exp.dataframe.iterrows(): if row[0][0] == 1: yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp run test.yaml participant 2 block 1') exp = Experiment.load('test.yaml') for row in exp.dataframe.iterrows(): if row[0][0] == 1 or (row[0][0] == 2 and row[0][1] == 1): yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp resume test.yaml participant') exp = Experiment.load('test.yaml') for row in exp.dataframe.iterrows(): if row[0][0] <= 2: yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp run test.yaml --next trial') exp = Experiment.load('test.yaml') for row in exp.dataframe.iterrows(): if row[0][0] <= 2 or row[0] == (3, 1, 1): yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp resume test.yaml participant 3 block 1 --demo') exp = Experiment.load('test.yaml') for row in exp.dataframe.iterrows(): if row[0][0] <= 2 or row[0] == (3, 1, 1): yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp --debug resume test.yaml participant 3 block 1') exp = Experiment.load('test.yaml') for row in exp.dataframe.iterrows(): if row[0][0] <= 2 or row[0][:2] == (3, 1): yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp run test.yaml participant 3 block 3 --from 3') exp = Experiment.load('test.yaml') for row in exp.dataframe.iterrows(): if row[0][0] <= 2 or row[0][:2] == (3, 1) or (row[0][:2] == (3, 3) and row[0][-1] >= 3): yield check_trial, row else: assert isnan(row[1]['result']) call_cli('exp run test.yaml participant 3 --from 2,4') exp = Experiment.load('test.yaml') for row in exp.dataframe.iterrows(): if (row[0][0] <= 2 or row[0][:2] == (3, 1) or (row[0][:2] == (3, 2) and row[0][-1] >= 4) or (row[0][:2] == (3, 3) and row[0][2] > 2)): yield check_trial, row else: assert isnan(row[1]['result']) for file in glob('test.yaml*'): os.remove(file)
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'])