Using Sacred Module with iPython

Question:

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!

Asked By: RDizzl3

||

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).

Answered By: 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

Answered By: Bohumir Zamecnik

The docs say it nicely (TL;DR: sacred checks this for you and fails in order to warn you)

Warning

By default, Sacred experiments will fail if run in an interactive
environment like a REPL or a Jupyter Notebook. This is an intended
security measure since in these environments reproducibility cannot be
ensured. If needed, this safeguard can be deactivated by passing
interactive=True to the experiment like this:

ex = Experiment('jupyter_ex', interactive=True)
Answered By: serv-inc

Setting interactive=True doesn’t work if you run the notebook as a script through ipython.

$ ipython code.ipynb

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[1], line 1
----> 1 ex = Experiment("image_classification", interactive=True)
      2 ex.observers.append(NeptuneObserver(run=neptune_run))

File ~miniconda3envspy38libsite-packagessacredexperiment.py:119, in Experiment.__init__(self, name, ingredients, interactive, base_dir, additional_host_info, additional_cli_options, save_git_info)
    117     elif name.endswith(".pyc"):
    118         name = name[:-4]
--> 119 super().__init__(
    120     path=name,
    121     ingredients=ingredients,
    122     interactive=interactive,
    123     base_dir=base_dir,
    124     _caller_globals=caller_globals,
    125     save_git_info=save_git_info,
    126 )
    127 self.default_command = None
    128 self.command(print_config, unobserved=True)

File ~miniconda3envspy38libsite-packagessacredingredient.py:75, in Ingredient.__init__(self, path, ingredients, interactive, _caller_globals, base_dir, save_git_info)
     69 self.save_git_info = save_git_info
     70 self.doc = _caller_globals.get("__doc__", "")
     71 (
     72     self.mainfile,
     73     self.sources,
     74     self.dependencies,
---> 75 ) = gather_sources_and_dependencies(
     76     _caller_globals, save_git_info, self.base_dir
     77 )
     78 if self.mainfile is None and not interactive:
     79     raise RuntimeError(
     80         "Defining an experiment in interactive mode! "
     81         "The sourcecode cannot be stored and the "
     82         "experiment won't be reproducible. If you still"
     83         " want to run it pass interactive=True"
     84     )

File ~miniconda3envspy38libsite-packagessacreddependencies.py:725, in gather_sources_and_dependencies(globs, save_git_info, base_dir)
    723 def gather_sources_and_dependencies(globs, save_git_info, base_dir=None):
    724     """Scan the given globals for modules and return them as dependencies."""
--> 725     experiment_path, main = get_main_file(globs, save_git_info)
    727     base_dir = base_dir or experiment_path
    729     gather_sources = source_discovery_strategies[SETTINGS["DISCOVER_SOURCES"]]

File ~miniconda3envspy38libsite-packagessacreddependencies.py:596, in get_main_file(globs, save_git_info)
    594     main = None
    595 else:
--> 596     main = Source.create(globs.get("__file__"), save_git_info)
    461 return Source(main_file, get_digest(main_file), repo, commit, is_dirty)

File ~miniconda3envspy38libsite-packagessacreddependencies.py:382, in get_py_file_if_possible(pyc_name)
    380 if pyc_name.endswith((".py", ".so", ".pyd")):
    381     return pyc_name
--> 382 assert pyc_name.endswith(".pyc")
    383 non_compiled_file = pyc_name[:-1]
    384 if os.path.exists(non_compiled_file):

sacred==0.8.2

Answered By: Siddhant Sadangi