# Buggy code
def predict(self, infile_name, outfile_name):
    """Make predictions on the given data, and output predicted scores to a file.

    Args:
        infile_name (str): Input file name, format is same as train/val/test file.
        outfile_name (str): Output file name, each line is the predict score.

    Returns:
        obj: An instance of self.
    """
    load_sess = self.sess
    with tf.gfile.GFile(outfile_name, "w") as wt:
        for batch_data_input in self.iterator.load_data_from_file(infile_name):
            step_pred = self.infer(load_sess, batch_data_input)
            step_pred = np.reshape(step_pred, -1)
            wt.write("\n".join(map(str, step_pred)))
            # line break after each batch.
            wt.write("\n")
    return self


# Traceback (PapermillExecutionError)
=========================================================================== test session starts ===========================================================================
platform linux -- Python 3.6.7, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: /home/miguel/repos/Recommenders, inifile:
collected 1 item

tests/smoke/test_notebooks_gpu.py F                                                                                                                                 [100%]

================================================================================ FAILURES =================================================================================
_____________________________________________________________________________ test_dkn_smoke ______________________________________________________________________________

notebooks = {'als_deep_dive': '/home/miguel/repos/Recommenders/examples/02_model_collaborative_filtering/als_deep_dive.ipynb', 'al...eep_dive': '/home/miguel/repos/Recommenders/examples/02_model_collaborative_filtering/cornac_bpr_deep_dive.ipynb', ...}

@pytest.mark.smoke
@pytest.mark.gpu
def test_dkn_smoke(notebooks):
notebook_path = notebooks["dkn_quickstart"]
pm.execute_notebook(
notebook_path,
OUTPUT_NOTEBOOK,
kernel_name=KERNEL_NAME,
parameters=dict(epoch=1),
)

tests/smoke/test_notebooks_gpu.py:118:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../anaconda/envs/reco_gpu/lib/python3.6/site-packages/papermill/execute.py:78: in execute_notebook
raise_for_execution_errors(nb, output_path)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

nb = {'cells': [{'cell_type': 'code', 'metadata': {'inputHidden': True, 'hide_input': True}, 'execution_count': None, 'sour...end_time': '2020-07-02T08:05:40.865564', 'duration': 9.904528, 'exception': True}}, 'nbformat': 4, 'nbformat_minor': 2}
output_path = 'output.ipynb'

def raise_for_execution_errors(nb, output_path):
error = None
for cell in nb.cells:
if cell.get("outputs") is None:
continue

for output in cell.outputs:
if output.output_type == "error":
error = PapermillExecutionError(
exec_count=cell.execution_count,
source=cell.source,
ename=output.ename,
evalue=output.evalue,
traceback=output.traceback,
)
break

if error:
# Write notebook back out with the Error Message at the top of the Notebook.
error_msg = ERROR_MESSAGE_TEMPLATE % str(error.exec_count)
error_msg_cell = nbformat.v4.new_code_cell(
source="%%html\n" + error_msg,
outputs=[
nbformat.v4.new_output(output_type="display_data", data={"text/html": error_msg})
],
metadata={"inputHidden": True, "hide_input": True},
)
nb.cells = [error_msg_cell] + nb.cells
write_ipynb(nb, output_path)
raise error
E           papermill.exceptions.PapermillExecutionError:
E           ---------------------------------------------------------------------------
E           Exception encountered at "In [7]":
E           ---------------------------------------------------------------------------
E           NotFoundError                             Traceback (most recent call last)
E           <ipython-input-7-607bbc4e411e> in <module>
E           ----> 1 model = DKN(hparams, input_creator)
E
E           ~/repos/Recommenders/reco_utils/recommender/deeprec/models/dkn.py in __init__(self, hparams, iterator_creator)
E                73                     self.context_embedding.assign(c_embedding_transformed)
E                74
E           ---> 75         super().__init__(hparams, iterator_creator, graph=self.graph)
E                76
E                77     def _init_embedding(self, file_path):
E
E           ~/repos/Recommenders/reco_utils/recommender/deeprec/models/base_model.py in __init__(self, hparams, iterator_creator, graph, seed)
E                30
E                31         self.graph = graph if graph is not None else tf.Graph()
E           ---> 32         self.iterator = iterator_creator(hparams, self.graph)
E                33         self.train_num_ngs = (
E                34             hparams.train_num_ngs if "train_num_ngs" in hparams else None
E
E           ~/repos/Recommenders/reco_utils/recommender/deeprec/io/dkn_iterator.py in __init__(self, hparams, graph, col_spliter, ID_spliter)
E                67         with tf.gfile.GFile(hparams.news_feature_file, "r") as rd:
E                68             while True:
E           ---> 69                 line = rd.readline()
E                70                 if not line:
E                71                     break
E
E           ~/anaconda/envs/reco_gpu/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py in readline(self)
E               181   def readline(self):
E               182     r"""Reads the next line from the file. Leaves the '\n' at the end."""
E           --> 183     self._preread_check()
E               184     return self._prepare_value(self._read_buf.ReadLineAsString())
E               185
E
E           ~/anaconda/envs/reco_gpu/lib/python3.6/site-packages/tensorflow/python/lib/io/file_io.py in _preread_check(self)
E                83       with errors.raise_exception_on_not_ok_status() as status:
E                84         self._read_buf = pywrap_tensorflow.CreateBufferedInputStream(
E           ---> 85             compat.as_bytes(self.__name), 1024 * 512, status)
E                86
E                87   def _prewrite_check(self):
E
E           ~/anaconda/envs/reco_gpu/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
E               526             None, None,
E               527             compat.as_text(c_api.TF_Message(self.status.status)),
E           --> 528             c_api.TF_GetCode(self.status.status))
E               529     # Delete the underlying status object from memory otherwise it stays alive
E               530     # as there is a reference to status from this from the traceback due to
E
E           NotFoundError: ../../tests/resources/deeprec/dkn/doc_feature.txt; No such file or directory

../../anaconda/envs/reco_gpu/lib/python3.6/site-packages/papermill/execute.py:198: PapermillExecutionError
-------------------------------------------------------------------------- Captured stderr call ---------------------------------------------------------------------------
62%|██████▏   | 13/21 [00:08<00:06,  1.23it/s]
============================================================================ warnings summary =============================================================================
/home/miguel/anaconda/envs/reco_gpu/lib/python3.6/site-packages/nbconvert/exporters/exporter_locator.py:28
/home/miguel/anaconda/envs/reco_gpu/lib/python3.6/site-packages/nbconvert/exporters/exporter_locator.py:28: DeprecationWarning: `nbconvert.exporters.exporter_locator` is deprecated in favor of `nbconvert.exporters.base` since nbconvert 5.0.
DeprecationWarning)

/home/miguel/anaconda/envs/reco_gpu/lib/python3.6/site-packages/tornado/web.py:1747
/home/miguel/anaconda/envs/reco_gpu/lib/python3.6/site-packages/tornado/web.py:1747: DeprecationWarning: @asynchronous is deprecated, use coroutines instead
DeprecationWarning)

tests/smoke/test_notebooks_gpu.py::test_dkn_smoke
/home/miguel/anaconda/envs/reco_gpu/lib/python3.6/site-packages/jupyter_client/session.py:371: DeprecationWarning: Session._key_changed is deprecated in traitlets 4.1: use @observe and @unobserve instead.
def _key_changed(self):

-- Docs: https://docs.pytest.org/en/latest/warnings.html
================================================================== 1 failed, 3 warnings in 11.56 seconds ==================================================================

# Fixed code
def predict(self, infile_name, outfile_name):
    """Make predictions on the given data, and output predicted scores to a file.

    Args:
        infile_name (str): Input file name, format is same as train/val/test file.
        outfile_name (str): Output file name, each line is the predict score.

    Returns:
        obj: An instance of self.
    """
    load_sess = self.sess
    with tf.gfile.GFile(outfile_name, "w") as wt:
        for batch_data_input, _, _ in self.iterator.load_data_from_file(infile_name):
            step_pred = self.infer(load_sess, batch_data_input)
            step_pred = np.reshape(step_pred, -1)
            wt.write("\n".join(map(str, step_pred)))
            # line break after each batch.
            wt.write("\n")
    return self