def load(self, path: Path, details_path: Path = None): name = FilenameHandler.get_filename(self.__class__.__name__, "pt") file_path = path / name if file_path.is_file(): self.model = torch.load(str(file_path)) self.model.eval() else: raise FileNotFoundError( f"{self.__class__.__name__} model could not be loaded from {file_path}. " f"Check if the path to the {name} file is properly set.") if details_path is None: params_path = path / FilenameHandler.get_filename( self.__class__.__name__, "yaml") else: params_path = details_path if params_path.is_file(): with params_path.open("r") as file: desc = yaml.safe_load(file) if "label" in desc: setattr(self, "label", Label(**desc["label"])) for param in ["feature_names", "classes"]: if param in desc: setattr(self, param, desc[param])
def store(self, path: Path, feature_names=None, details_path=None): content = self._convert_object_to_dict() PathBuilder.build(path) file_path = path / FilenameHandler.get_filename( self.__class__.__name__, "pickle") with file_path.open("wb") as file: pickle.dump(content, file) if details_path is None: params_path = path / FilenameHandler.get_filename( self.__class__.__name__, "yaml") else: params_path = details_path with params_path.open("w") as file: desc = { self.label.name: { **content, "feature_names": feature_names, "classes": list(self.class_mapping.values()) } } if self.label is not None: desc["label"] = vars(self.label) yaml.dump(desc, file)
def store(self, path, feature_names=None, details_path: Path = None): PathBuilder.build(path) name = FilenameHandler.get_filename(self.__class__.__name__, "pt") torch.save(self.model, str(path / name)) if details_path is None: params_path = path / FilenameHandler.get_filename( self.__class__.__name__, "yaml") else: params_path = details_path with params_path.open("w") as file: desc = { **(self.get_params()), "feature_names": feature_names, "classes": self.get_classes() } yaml.dump(desc, file)
def load(self, path: Path): name = FilenameHandler.get_filename(self.__class__.__name__, "pt") file_path = path / name if file_path.is_file(): self.model = torch.load(str(file_path)) self.model.eval() else: raise FileNotFoundError(f"{self.__class__.__name__} model could not be loaded from {file_path}. " f"Check if the path to the {name} file is properly set.")
def run(simulation_state: SimulationState = None): path = simulation_state.result_path / FilenameHandler.get_dataset_name( SignalImplanter.__name__) if path.is_file(): dataset = PickleImport.import_dataset({"path": path}, SignalImplanter.DATASET_NAME) else: dataset = SignalImplanter._implant_signals_in_dataset( simulation_state) return dataset
def _encode_by_model(self, dataset, params: EncoderParams, vectors): examples = CacheHandler.memo_by_params(self._prepare_caching_params(dataset, params, vectors, Word2VecEncoder.DESCRIPTION_REPERTOIRES), lambda: self._encode_examples(dataset, vectors, params)) if params.encode_labels: labels = CacheHandler.memo_by_params(self._prepare_caching_params(dataset, params, vectors, Word2VecEncoder.DESCRIPTION_LABELS), lambda: self._encode_labels(dataset, params)) else: labels = None scaler_filename = params.result_path / FilenameHandler.get_filename("standard_scaling", "pkl") scaled_examples = FeatureScaler.standard_scale(scaler_filename, examples) encoded_dataset = self._build_encoded_dataset(dataset, scaled_examples, labels, params) return encoded_dataset
def _vectorize_encoded(self, examples: list, params: EncoderParams): if self.vectorizer_path is None: self.vectorizer_path = params.result_path / FilenameHandler.get_filename(DictVectorizer.__name__, "pickle") if params.learn_model: vectorizer = DictVectorizer(sparse=True, dtype=float) vectorized_examples = vectorizer.fit_transform(examples) PathBuilder.build(params.result_path) with self.vectorizer_path.open('wb') as file: pickle.dump(vectorizer, file) else: with self.vectorizer_path.open('rb') as file: vectorizer = pickle.load(file) vectorized_examples = vectorizer.transform(examples) return vectorized_examples, vectorizer.get_feature_names()
def load(self, path: Path): keys = list(vars(self).keys()) file_path = path / FilenameHandler.get_filename( self.__class__.__name__, "pickle") if file_path.is_file(): with file_path.open("rb") as file: content = pickle.load(file) assert all( key in keys for key in content.keys()), f"ProbabilisticBinaryClassifier: error while loading from {file_path}: " \ f"object attributes from file and from the class do not match.\n" \ f"Attributes from file: {list(content.keys())}\n" \ f"Attributes for object of class ProbabilisticBinaryClassifier: {keys}" for key in content: setattr(self, key, content[key]) else: raise FileNotFoundError( f"{self.__class__.__name__} model could not be loaded from {file_path}. " f"Check if the path to the {file_path.name} file is properly set." )
def test_get_filename(self): name = FilenameHandler.get_filename("RandomForestClassifier", "json") self.assertEqual("random_forest_classifier.json", name)
def test_model_name(self): name = FilenameHandler.get_model_name("Word2VecEncoder") self.assertEqual("word2_vec_encoder_model.pickle", name)
def test_get_dataset_name(self): name = FilenameHandler.get_dataset_name("KmerFrequencyEncoder") self.assertEqual("encoded_dataset.iml_dataset", name)
def check_if_exists(self, path): file_path = path / FilenameHandler.get_filename( self.__class__.__name__, "pt") return file_path.is_file()
def _get_model_filename(self): return FilenameHandler.get_filename(self.__class__.__name__, "")