使用 Python 运行实验 - 快速而干净。
项目描述
Pyexperiment 有助于使用最少的样板代码开发小型、可重复的实验。考虑以下示例,实现一个简单的程序来存储数字并计算它们的总和:
from pyexperiment import experiment, state, conf, log
conf['pyexperiment.save_state'] = True
conf['pyexperiment.load_state'] = True
conf['message'] = "The stored numbers are: "
def store(number):
"""Store a number"""
if 'numbers' not in state:
log.debug("Initialize state['numbers'] to empty list")
state['numbers'] = []
log.debug("Store number: %s", number)
state['numbers'].append(float(number))
def show():
"""Show the stored numbers and compute their sum"""
if not 'numbers' in state:
print('No numbers stored yet')
return
print(conf['message'] + str(state['numbers']))
with log.timed("sum"):
total = sum(state['numbers'])
print("The total is: " + str(total))
if __name__ == '__main__':
experiment.main(default=show,
commands=[store, show])
Pyexperiment 可以很容易地从命令行试验这段代码:
$ ./numbers store 42
$ ./numbers store 3.14
$ ./numbers
The stored numbers are: [42.0, 3.14]
The total is: 45.14
$ ./numbers -o message "Numbers: "
Numbers: [42.0, 3.14]
The total is: 45.14
$ ./numbers -v
[DEBUG ] [0.069s] Start: './numbers -v'
[DEBUG ] [0.069s] Time: '2015-08-14 14:23:00.027378'
[INFO ] [0.075s] Loading state from file 'experiment_state.h5f'
The stored numbers are: [42.0, 3.14]
[DEBUG ] [0.076s] sum took 0.000025s
The total is: 45.14
[DEBUG ] [0.078s] No need to save unchanged state
[DEBUG ] [0.078s] End: './numbers -v show'
[DEBUG ] [0.078s] Time: '2015-08-14 14:23:00.037124'
[DEBUG ] [0.078s] Took: 0.010s
$ ./numbers -h
usage: numbers [-h] [-c CONFIG] [-o key value] [-i]
[--verbosity {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [-v]
[-j PROCESSES]
[{store,show,help,test,show_tests,show_config,save_config,show_state,show_commands}]
[argument [argument ...]]
Thanks for using numbers.
positional arguments:
{store,show,help,test,show_tests,show_config,save_config,show_state,show_commands}
choose a command to run, running show by default
argument argument to the command
optional arguments:
-h, --help show this help message and exit
-c CONFIG, --config CONFIG
specify a configuration file
-o key value, --option key value
override a configuration option
-i, --interactive drop to interactive prompt after COMMAND
--verbosity {DEBUG,INFO,WARNING,ERROR,CRITICAL}
choose the console logger's verbosity
-v shortcut for --verbosity DEBUG
-j PROCESSES, --processes PROCESSES
set number of parallel processes used
available commands:
store: Store a number
show (default): Show the stored numbers and compute their sum
help: Shows help for a specified command
test: Run tests for the experiment
show_tests: Show available tests for the experiment
show_config: Print the configuration
save_config: Save a configuration file to a filename
show_state: Shows the contents of the state loaded by the configuration or from the file specified as an argument
show_commands: Print the available commands
动机
不乏用于命令行界面、日志记录、配置文件管理、持久状态、并行性或绘图的优秀 Python 库。但是,在为快速实验编写小脚本时,配置这些组件通常需要花费太多精力,并且最终会一遍又一遍地重写相同的设置代码。
Pyexperiment 通过提供一种简单的方法来快速开始简短的实验来解决这个问题。导入 pyexperiment 将为您提供:
一个基本的命令行界面,允许从命令提示符调用任意函数(并传递参数),提供从函数的文档字符串和 zsh/bash 自动完成(基于标准库的 argparse 和 argcomplete)派生的帮助文本。
一个简单的配置管理,提供默认值的简单方法(基于优秀的 configobj 库)。
一个线程安全的记录器,具有可配置的日志级别、带有统计信息的计时实用程序和轮换日志文件(基于标准库的日志模块)。
具有平台独立、可配置(可选旋转)状态文件的持久状态,与许多其他程序兼容(基于 h5py)。
并行执行复制。
一个合理的绘图设置(基于 matplotlib,以及可选的 seaborn),具有可配置的默认值和异步绘图。
许多其他可能派上用场的点点滴滴……
作为设计原则,pyexperiment 的组件无需任何进一步配置即可使用。不可避免地,在此设置中做出的选择是固执己见的,可能符合也可能不符合您的个人品味。随意在 问题页面上开始讨论。
安装
安装 pyexperiment 的最简单方法是从 pypi,只需调用pip install --user pyexperiment(或者,在 virtualenv 中使用pip install pyexperiment ,或在系统范围安装之前添加sudo)。
pyexperiment 包有一些外部依赖项(如您在requirements.txt中所见):
六
配置对象
麻木的
h5py
matplotlib
锁定文件
工具
IPython(可选,允许将 IPython 与 –interactive 命令一起使用)
argcomplete(可选,添加 activate_autocompletion 命令)
seaborn(可选,添加更多绘图选项)
如果您从 pypi 安装(h5py 依赖项),您可能需要先安装 libhdf5,例如,通过运行sudo apt-get install libhdf5-dev。您可能还会发现您需要先安装 cython,例如,通过运行sudo apt-get install Cython或pip install Cython。
可重复的实验
为了让您的实验可重复并避免依赖性问题,自动化设置您的开发环境是一个好主意,例如,使用 Vagrant 盒子,或者 - 在许多情况下甚至更好 - Docker 映像。要开始使用 Vagrant 或 Docker 进行 pyexperiment,您可以在此处使用 Vagrantfile 和设置脚本 ,或在此处使用 Dockerfile 和 设置脚本。
执照
pyexperiment 包在 MIT 许可证下获得许可(请参阅 LICENSE)。