3.2. sciexp2.expdef.experiments
Source: sciexp2/expdef/experiments.py
Automate generation of parametrized launchers.
Functions
Helper to use |
|
Parameter value to add given parameter permutations to every experiment. |
|
Wrap function to get experiment as first argument during translation. |
|
Similar to |
Classes
Define and generate experiment sets. |
|
Proxy to a subset of elements in a |
3.2.3. from_find_files
- from_find_files(template, **kwargs)
Helper to use
sciexp2.common.utils.find_fileswithfrom_function.Will provide all variable names that are tags in the template. If argument path is provided, will also provide that variable name (with the full path to the found file).
- Parameters:
- templatestr
Template of file paths to find.
- kwargs
Arguments to
sciexp2.common.utils.find_files.
Examples
After creating some example files:
>>> import tempfile >>> tmp = tempfile.TemporaryDirectory() >>> _ = open(os.path.join(tmp.name, "0.txt"), "w") >>> _ = open(os.path.join(tmp.name, "1.txt"), "w")
Now we can use the variables on the file template (
a) and its full path (PATH) as new variables on the experiments (varandpath):>>> e = Experiments() >>> f = from_find_files(os.path.join(tmp.name, "{{a}}.txt"), path="PATH") >>> e.params(var=f.param("a"), path=f.param("PATH")) >>> e Experiments([{'path': '/tmp/.../0.txt', 'var': 0}, {'path': '/tmp/.../1.txt', 'var': 1}])
3.2.4. from_function
- from_function(fun, keys)
Parameter value to add given parameter permutations to every experiment.
For every experiment, it will permute it with the values returned by fun. Note that fun is expected to return entries with multiple variable names (specified by keys), so you must call
paramon the result offrom_functionto select the specific variable name.- Parameters:
- funcallable
Function to call on every experiment, receives the experiment at hand and must return a list of dict.
- keyslist of str
Variable names in the elements returned by fun.
See also
Examples
The result can use any of the values available on each experiment; in this case
bis two and three times the value ofa(note how from_func and other values can be mixed):>>> def func(exp): ... return [{'b': exp["a"]*2}, {'b': exp["a"]*3}] >>> e = Experiments() >>> e.params(a=range(2), b=from_function(func, ["b"]).param("b")) >>> e Experiments([{'a': 0, 'b': 0}, {'a': 1, 'b': 2}, {'a': 1, 'b': 3}])
3.2.5. get_executor_workers
- get_executor_workers(parallel)
3.2.6. with_exp
- with_exp(func)
Wrap function to get experiment as first argument during translation.
Examples
>>> e = Experiments() >>> e.params(a=range(2), b=range(2), ... c=with_exp(lambda exp: exp['a']+exp['b'])) >>> e.translate("{{c}}") ['0', '1', '2']
3.2.7. with_exp_tpl
- with_exp_tpl(func, template)
Similar to
with_exp, but passes translated template as second argument.This is a convenience shortcut for using
text.translateinwith_exp.Examples
In its basic form, we can combine translated templates with other experiment values:
>>> e = Experiments() >>> def fun(exp, tpl): ... return tpl + " == " + str(exp['a']+exp['b']) >>> e.params(a=range(2), b=range(2), ... f=with_exp_tpl(fun, "{{a}}+{{b}}")) >>> e.translate("{{f}}") ['0+0 == 0', '0+1 == 1', '1+0 == 1', '1+1 == 2']
But this comes in handy when we want to pull the contents of per-experiment files into new experiment values. Let’s start by creating some files:
>>> import tempfile >>> tmp_dir = tempfile.TemporaryDirectory() >>> with open(os.path.join(tmp_dir.name, "file-0"), "w") as f: ... f.write("000") 3 >>> with open(os.path.join(tmp_dir.name, "file-1"), "w") as f: ... f.write("111") 3
And now we can use them to populate experiment values:
>>> def get_file(exp, path): ... return open(path, "r").read() >>> v = e.view("b == 0") >>> v.params(f=with_exp_tpl(get_file, os.path.join(tmp_dir.name, "file-1"))) >>> v.translate("{{f}}") ['111'] >>> v.params(f=with_exp_tpl(get_file, os.path.join(tmp_dir.name, "file-{{a}}"))) >>> v.translate("{{f}}") ['000', '111']