5

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!

serv-inc
  • 35,772
  • 9
  • 166
  • 188
RDizzl3
  • 318
  • 3
  • 13
  • 2
    Try putting this code into a file `hello_world.py`, as in the example on the page you link – donkopotamus Mar 30 '16 at 00:30
  • @donkopotamus - yes that worked - so something like this cannot be run from an IPython notebook? This is where I was running it. – RDizzl3 Mar 30 '16 at 00:32
  • 1
    Looks like I was able to get this running through IPython/Jupyter - I had to write the code in the .py as @donkopotamus recommended the used the `%run` cell magic command. If you would like to move your comment to answer I will gladly accept. – RDizzl3 Mar 30 '16 at 00:37
  • 1
    use the %run from iPython. It loads the module into the interpreter. – mugabits Mar 30 '16 at 00:39
  • 2
    For you information, the developer of sacred is considering modifying the package so that it can be run from ipython. Users will just have to be aware that they will lose part of the reproducibility of their experiment because you cannot be sure that the code in the notebook was run in the order of the file saved along the experiment. Stay tuned in github page for sacred. – wotter Apr 11 '16 at 18:18

4 Answers4

3

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

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
3

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

Bohumir Zamecnik
  • 2,450
  • 28
  • 23
2

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)
Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • Note that this feature and the warning did not exist when the question was asked; see [here](https://github.com/IDSIA/sacred/issues/100#issuecomment-204565520). – Junuxx Dec 09 '19 at 21:24
0

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 ~\miniconda3\envs\py38\lib\site-packages\sacred\experiment.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 ~\miniconda3\envs\py38\lib\site-packages\sacred\ingredient.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 ~\miniconda3\envs\py38\lib\site-packages\sacred\dependencies.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 ~\miniconda3\envs\py38\lib\site-packages\sacred\dependencies.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 ~\miniconda3\envs\py38\lib\site-packages\sacred\dependencies.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

Siddhant Sadangi
  • 125
  • 4
  • 11