Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unclosed files (#751, #755, #756, #757) #765

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions supervised/base_automl.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def _check_can_load(self):
def load(self, path):
logger.info("Loading AutoML models ...")
try:
params = json.load(open(os.path.join(path, "params.json")))
with open(os.path.join(path, "params.json")) as file:
params = json.load(file)

self._model_subpaths = params["saved"]
self._mode = params.get("mode", self._mode)
Expand Down Expand Up @@ -215,7 +216,8 @@ def load(self, path):
self._stacked_models += [models_map[stacked_model_name]]

data_info_path = os.path.join(path, "data_info.json")
self._data_info = json.load(open(data_info_path))
with open(data_info_path, "r") as file:
self._data_info = json.load(file)
self.n_features_in_ = self._data_info["n_features"]

if "n_classes" in self._data_info:
Expand Down Expand Up @@ -710,7 +712,8 @@ def load_progress(self):
fname = os.path.join(self._results_path, "progress.json")
if not os.path.exists(fname):
return
state = json.load(open(fname, "r"))
with open(fname, "r") as file:
state = json.load(file)
self._fit_level = state.get("fit_level", self._fit_level)
self._all_params = state.get("all_params", self._all_params)
self._time_ctrl = TimeController.from_json(state.get("time_controller"))
Expand Down Expand Up @@ -1382,15 +1385,15 @@ def select_and_save_best(self, show_warnings=False):

def get_ensemble_models(self, ensemble_name="Ensemble"):
try:
params = json.load(
open(os.path.join(self.results_path, ensemble_name, "ensemble.json"))
)
with open(os.path.join(self.results_path, ensemble_name, "ensemble.json")) as file:
params = json.load(file)
return [m["model"] for m in params["selected_models"]]
except Exception as e:
return []

def models_needed_on_predict(self, required_model_name):
params = json.load(open(os.path.join(self.results_path, "params.json")))
with open(os.path.join(self.results_path, "params.json")) as file:
params = json.load(file)
saved_models = params.get("saved", [])
stacked_models = params.get("stacked", [])

Expand Down
3 changes: 2 additions & 1 deletion supervised/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ def load(results_path, model_subpath, models_map):
model_path = os.path.join(results_path, model_subpath)
logger.info(f"Loading ensemble from {model_path}")

json_desc = json.load(open(os.path.join(model_path, "ensemble.json")))
with open(os.path.join(model_path, "ensemble.json")) as file:
json_desc = json.load(file)

ensemble = Ensemble(json_desc.get("optimize_metric"), json_desc.get("ml_task"))
ensemble._name = json_desc.get("name", ensemble._name)
Expand Down
3 changes: 2 additions & 1 deletion supervised/model_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,8 @@ def load(results_path, model_subpath, lazy_load=True):
model_path = os.path.join(results_path, model_subpath)
logger.info(f"Loading model framework from {model_path}")

json_desc = json.load(open(os.path.join(model_path, "framework.json")))
with open(os.path.join(model_path, "framework.json")) as file:
json_desc = json.load(file)

joblib_version_computer = joblib.__version__
joblib_version_framework = json_desc.get("joblib_version")
Expand Down
3 changes: 2 additions & 1 deletion supervised/preprocessing/goldenfeatures_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def __init__(self, results_path=None, ml_task=None, features_count=None, n_jobs=
self._result_path = os.path.join(results_path, self._result_file)

if os.path.exists(self._result_path):
self.from_json(json.load(open(self._result_path, "r")), results_path)
with open(self._result_path, "r") as file:
self.from_json(json.load(file), results_path)

def fit(self, X, y):
if self._new_features:
Expand Down
3 changes: 2 additions & 1 deletion tests/tests_automl/test_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def test_tune_only_default(self):
# Get number of starting models
n1 = len([x for x in os.listdir(self.automl_dir) if x[0].isdigit()])

progress = json.load(open(os.path.join(self.automl_dir, "progress.json"), "r"))
with open(os.path.join(self.automl_dir, "progress.json"), "r") as file:
progress = json.load(file)
progress["fit_level"] = "default_algorithms"

with open(os.path.join(self.automl_dir, "progress.json"), "w") as fout:
Expand Down
3 changes: 2 additions & 1 deletion tests/tests_automl/test_update_errors_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ def test_custom_init(self):

errors_filename = os.path.join(self.automl_dir, "errors.md")
self.assertTrue(os.path.exists(errors_filename))
self.assertTrue("bad error" in open(errors_filename).read())
with open(errors_filename) as file:
self.assertTrue("bad error" in file.read())
Loading