#!/usr/bin/env python3 import random from psweep import psweep as ps def func(pset): return {'result': random.random() * pset['a']} if __name__ == '__main__': params = ps.seq2dicts('a', [1, 2, 3, 4]) df = ps.run(func, params) print(df)
#!/usr/bin/env python3 import random import multiprocessing as mp from psweep import psweep as ps def func(pset): print(mp.current_process().name) return {'result': random.random() * pset['a']} if __name__ == '__main__': params = ps.seq2dicts('a', [1, 2, 3, 4, 5, 6, 7, 8]) df = ps.run(func, params, poolsize=2) print(df)
#!/usr/bin/env python3 import random from itertools import product from psweep import psweep as ps def func(pset): return {'result': random.random() * pset['a']} if __name__ == '__main__': params = ps.loops2params(product(ps.seq2dicts('a', [1,2,3,4]), [{'study': 'a'}])) df = ps.run(func, params) print(df)
return {'result': random.random() * pset['a'] * pset['b']} if __name__ == '__main__': print("first run") const = {'a': 11111, 'b': 55555} params = [] disp_cols = [] values = dict(a=[1, 2, 3], b=[66, 77]) for study, seq in values.items(): # [{'a': 1}, {'a': 2}, {'a': 3}] # [{'b': 66}, {'b': 77}] params_1d = ps.seq2dicts(study, seq) this_params = ps.loops2params(product(params_1d, [{'study': study}])) this_params = [ps.merge_dicts(const, dct) for dct in this_params] params += this_params disp_cols.append(study) disp_cols += ['_run_id'] df = ps.run(func, params, backup_script=__file__, backup_calc_dir=True, verbose=disp_cols) print(df[disp_cols + ['result']])
#!/usr/bin/env python3 import random from itertools import product from psweep import psweep as ps def func(pset): return {'result': random.random() * pset['a'] * pset['b']} if __name__ == '__main__': params = ps.loops2params(product( ps.seq2dicts('a', [1,2,3,4]), ps.seq2dicts('b', [8,9]), )) # First run. df = ps.run(func, params) # Second run. params = ps.loops2params(product( ps.seq2dicts('a', [11,22,33,44]), ps.seq2dicts('b', [88,99]), )) df = ps.run(func, params) print(df)
#!/usr/bin/env python3 import random from itertools import product from psweep import psweep as ps def func(pset): return {'result': random.random() * pset['a'] * pset['b']} if __name__ == '__main__': sel = ['_calc_dir', 'a', 'b', 'result'] # 1st real run: produce some data, vary a, b constant params = ps.loops2params( product(ps.seq2dicts('a', [1, 2, 3, 4]), ps.seq2dicts('b', [100]))) df = ps.run(func, params) print(df[sel]) # simulate run: check if new parameter grid is OK params = ps.loops2params( product(ps.seq2dicts('a', [5, 6]), ps.seq2dicts('b', [88, 99]))) df = ps.run(func, params, simulate=True) print(df[sel]) # looks good, 2nd real run with new params df = ps.run(func, params) print(df[sel])
{'a': 4, 'const': 'const', 'b': 2.0}] """ import timeit, os from itertools import product from subprocess import run from psweep import psweep as ps def func(pset): cmd_tmpl = """ echo "calling func: pset: a={a} b={b} const={const}"; sleep $(echo "0.1 * {a} / {b}" | bc -l) """ cmd = cmd_tmpl.format(**pset) timing = min(timeit.repeat(lambda: run(cmd, shell=True), repeat=3, number=1)) return {'timing': timing} if __name__ == '__main__': results = 'results.json' params = ps.loops2params(product( ps.seq2dicts('a', [1,2,3,4]), ps.seq2dicts('b', [1.0,2.0]), ps.seq2dicts('const', ['const']), )) df = ps.run(func, params)