Example #1
0
    def persist(self, path: Text) -> None:
        """Persists the policy to a storage."""

        if self.model is None:
            logger.debug("Method `persist(...)` was called "
                         "without a trained model present. "
                         "Nothing to persist then!")
            return

        model_path = Path(path)
        tf_model_file = model_path / f"{SAVE_MODEL_FILE_NAME}.tf_model"

        io_utils.create_directory_for_file(tf_model_file)

        self.featurizer.persist(path)

        self.model.save(str(tf_model_file))

        io_utils.json_pickle(
            model_path / f"{SAVE_MODEL_FILE_NAME}.priority.pkl", self.priority)
        io_utils.pickle_dump(model_path / f"{SAVE_MODEL_FILE_NAME}.meta.pkl",
                             self.config)
        io_utils.json_pickle(
            model_path / f"{SAVE_MODEL_FILE_NAME}.data_example.pkl",
            self.data_example)
        io_utils.json_pickle(
            model_path / f"{SAVE_MODEL_FILE_NAME}.label_data.pkl",
            self._label_data)
Example #2
0
    def persist(self, file_name: Text, model_dir: Text) -> Dict[Text, Any]:
        """Persist this model into the passed directory.

        Return the metadata necessary to load the model again.
        """

        if self.model is None:
            return {"file": None}

        model_dir = Path(model_dir)
        tf_model_file = model_dir / f"{file_name}.tf_model"

        io_utils.create_directory_for_file(tf_model_file)

        self.model.save(str(tf_model_file))

        io_utils.pickle_dump(model_dir / f"{file_name}.data_example.pkl",
                             self.data_example)
        io_utils.pickle_dump(model_dir / f"{file_name}.label_data.pkl",
                             self._label_data)
        io_utils.json_pickle(
            model_dir / f"{file_name}.index_label_id_mapping.pkl",
            self.index_label_id_mapping,
        )
        io_utils.json_pickle(
            model_dir / f"{file_name}.index_tag_id_mapping.pkl",
            self.index_tag_id_mapping,
        )

        return {"file": file_name}
    def save_tests(tests_string: Text,
                   filename: Optional[Text] = None) -> List[Text]:
        """Saves `test_string` to a file `filename`.

        Args:
            tests_string: Test that needs to be saved.
            filename: Path to a test file.
        """

        if not filename:
            filename = utils.get_project_directory() / DEFAULT_FILENAME

        existing_tests = get_tests_from_file(filename)
        new_tests = [
            test for test in _split_tests(tests_string)
            if test not in existing_tests
        ]

        if new_tests:
            all_tests = [f"## {test}\n" for test in existing_tests + new_tests]

            io_utils.create_directory_for_file(filename)
            io_utils.write_text_file("\n".join(all_tests), filename)

        return [f"## {test}" for test in new_tests]
    def persist(self, path: Text) -> None:
        """Persist the tracker featurizer to the given path.

        Args:
            path: The path to persist the tracker featurizer to.
        """
        featurizer_file = os.path.join(path, "featurizer.json")
        io_utils.create_directory_for_file(featurizer_file)

        # noinspection PyTypeChecker
        io_utils.write_text_file(str(jsonpickle.encode(self)), featurizer_file)
Example #5
0
def test_create_directory_for_file(tmp_path: Path):
    file = str(tmp_path / "dir" / "test.txt")

    io_utils.create_directory_for_file(str(file))
    assert not os.path.exists(file)
    assert os.path.exists(os.path.dirname(file))