2

so I've been poking around with sacred a little bit and it seems great. unfortunately I did not find any multiple files use-cases examples like I am trying to implement.

so i have this file called configuration.py, it is intended to contain different variables which will (using sacred) be plugged in to the rest of the code (laying in different files):

from sacred import Experiment
ex = Experiment('Analysis')

@ex.config
def configure_analysis_default():
    """Initializes default  """
    generic_name = "C:\\basic_config.cfg" # configuration filename
    message = "This is my generic name: %s!" % generic_name
    print(message)

@ex.automain #automain function needs to be at the end of the file. Otherwise everything below it is not defined yet
#  when the experiment is run.
def my_main(message):
    print(message)

This by itself works great. sacred is working as expected. However, when I'm trying to introduce a second file named Analysis.py:

import configuration
from sacred import Experiment
ex = Experiment('Analysis')

@ex.capture
def what_is_love(generic_name):
    message = " I don't know"
    print(message)
    print(generic_name)

@ex.automain
def my_main1():
    what_is_love()

running Analysis.py yields:

Error:

TypeError: what_is_love is missing value(s) for ['generic_name']

I expected that the 'import configuration' statement to include the configuration.py file, thus importing everything that was configured in there including configure_analysis_default() alongside its decorator @ex.config and then inject it to what_is_love(generic_name). What am I doing wrong? how can i fix this?

Appreciate it!

sachin dubey
  • 755
  • 9
  • 28
orialz
  • 75
  • 1
  • 9

2 Answers2

3

It looks like we're supposed to use Ingredients for this kind of thing.

http://sacred.readthedocs.io/en/latest/ingredients.html

But I haven't quite figured that out yet.

I ran into a circular import problem in my setup, so I am using a separate file exp.py that only states

from sacred import Experiment
ex = Experiment("default")

In each of the files in the package I do

from exp import ex

and the decorators and config variable passing seems to work. I can change the name of the experiment on the command line with --name:

$> python main.py --name newname

rodin
  • 131
  • 7
  • IMHO ingredients are some kind of 'functions' whenever you might be using an atomic operation relatively often across multiple projects instead of copy-pasting code... Take a look here: http://sacred.readthedocs.io/en/latest/ingredients.html – orialz Feb 13 '18 at 16:06
  • This is a perfect way of doing this! – London guy May 01 '18 at 14:08
2

So, pretty dumb, but I'll post it here in favour of whoever will have similar issue...

My issue is that I have created a different instance of experiment. I needed simply to import my experiment from the configuration file.

replacing this:

import configuration
from sacred import Experiment
ex = Experiment('Analysis')

with this:

import configuration
ex = configuration.ex
orialz
  • 75
  • 1
  • 9