Productivity Tools: sacred python package for code experimenting

@TOC

Intro

Sacred is a python package created for experiment configuration,. As a radar algorithm engineer my job inevitably data processing experiments, analysis and most importantly, the reproduction of such experiments. Thus a tool for good experiment tool would be very helpful.

Quick Start

Sacred can be installed with pip or directly install from github. Currrent version is 0.8.2 (0.8.1 for pypi). Import Experiment class, then instantiate a few decorators form the experiment class. The @ex.automain decorator determines which function is the main experiment to run (This is kinda like pytest with all functions starting with test_ are executed in turn, but here only one main is allowed and previous defined will be flushed). The @ex.config decorator broadcasts all local variables declared in the config functions as visible for all main experiments. Each function setting configuration parameters must be decorated with @ex.config

from sacred import Experiment
from radar import detect
ex = Experiment('radar_detect')

@ex.config
def detect_config():
	threshold = 10

@ ex.automain
def detect_experiment(threshold):
	detect(threshold)

In this example, the function detect_experiment is executed with the parameter threshold form the detect_config parameter when executing this file. Unlike the pytest, the sacred considers a scenario where the configurations are much more volatile. Suppose we have a parameter “threshold” which is set to 10 in the config function. It can be altered by running the experiment file as:

python experiment.py with threshold=12

Alternatively, re-execution with update parameter can also be put into a script to avoid too much interaction. A run object is created and returned by the Experiment.run() method to hold the information about the execution for later consumption.

ex_run = ex.run(config_updates={"threshold": 14})

Decorator is a special function that takes a function as parameter and returns a function. It can be used at the declaration of a function with syntax “@” for easy and readable modification of the function. Typically the input function parameter is executed within the returned wrapper function. To avoid conflict, copy the attributes of the input function into the output function is recommended

functools.wraps(func)

Apart form @ex.main/automain, the decorator @ex.capture also loads missing parameters form global parameters broadcast-ed by Experiment. The broadcast-ed parameters flushed function level default values, and is flushed by explicit input upon calling.

Some Notices

  • By default sacred does not allow interactive environment. To bypass this set interactive=True when initializing Experiment object
  • ex.config only broadcast jsonable parameters.
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页