Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Sacred Module with iPython

I am trying to set up sacred for Python and I am going through the tutorial. I was able to set up sacred using pip install sacred with no issues. I am having trouble running the basic code:

from sacred import Experiment

ex = Experiment("hello_world")

Running this code returns the a ValueError:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-66f549cfb192> in <module>()
      1 from sacred import Experiment
      2 
----> 3 ex = Experiment("hello_world")

/Users/ryandevera/anaconda/lib/python2.7/site-packages/sacred/experiment.pyc in __init__(self, name, ingredients)
     42         super(Experiment, self).__init__(path=name,
     43                                          ingredients=ingredients,
---> 44                                          _caller_globals=caller_globals)
     45         self.default_command = ""
     46         self.command(print_config, unobserved=True)

/Users/ryandevera/anaconda/lib/python2.7/site-packages/sacred/ingredient.pyc in __init__(self, path, ingredients, _caller_globals)
     48         self.doc = _caller_globals.get('__doc__', "")
     49         self.sources, self.dependencies = \
---> 50             gather_sources_and_dependencies(_caller_globals)
     51 
     52     # =========================== Decorators ==================================

/Users/ryandevera/anaconda/lib/python2.7/site-packages/sacred/dependencies.pyc in gather_sources_and_dependencies(globs)
    204 def gather_sources_and_dependencies(globs):
    205     dependencies = set()
--> 206     main = Source.create(globs.get('__file__'))
    207     sources = {main}
    208     experiment_path = os.path.dirname(main.filename)

/Users/ryandevera/anaconda/lib/python2.7/site-packages/sacred/dependencies.pyc in create(filename)
     61         if not filename or not os.path.exists(filename):
     62             raise ValueError('invalid filename or file not found "{}"'
---> 63                              .format(filename))
     64 
     65         mainfile = get_py_file_if_possible(os.path.abspath(filename))

ValueError: invalid filename or file not found "None"

I am not sure why this error is returning. The documentation does not say anything about setting up an Experiment file prior to running the code. Any help would be greatly appreciated!

like image 531
RDizzl3 Avatar asked Feb 08 '23 09:02

RDizzl3


2 Answers

The traceback given indicates that the constructor for Experiment searches its namespace to find the file in which its defined.

Thus, to make the example work, place the example code into a file and run that file directly.

If you are using ipython, then you could always try using the %%python command, which will effectively capture the code you give it into a file before running it (in a separate python process).

like image 142
donkopotamus Avatar answered Feb 12 '23 12:02

donkopotamus


According to the docs, if you're in IPython/Jupyter, you can allow the Experiment to run in a non-reproducible interactive environment:

ex = Experiment('jupyter_ex', interactive=True)

https://sacred.readthedocs.io/en/latest/experiment.html#run-the-experiment

like image 37
Bohumir Zamecnik Avatar answered Feb 12 '23 11:02

Bohumir Zamecnik