Skip to content

Commit 0f448b5

Browse files
authored
Merge pull request #835 from MilesCranmer/use-logging
use standard library logging
2 parents b02c62a + e98c86d commit 0f448b5

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

pysr/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import logging
12
import os
23

4+
pysr_logger = logging.getLogger("pysr")
5+
pysr_logger.setLevel(logging.INFO)
6+
handler = logging.StreamHandler()
7+
handler.setLevel(logging.INFO)
8+
pysr_logger.addHandler(handler)
9+
310
if os.environ.get("PYSR_USE_BEARTYPE", "0") == "1":
411
from beartype.claw import beartype_this_package
512

pysr/feature_selection.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Functions for doing feature selection during preprocessing."""
22

3+
import logging
34
from typing import cast
45

56
import numpy as np
@@ -8,6 +9,8 @@
89

910
from .utils import ArrayLike
1011

12+
pysr_logger = logging.getLogger(__name__)
13+
1114

1215
def run_feature_selection(
1316
X: ndarray,
@@ -44,7 +47,7 @@ def _handle_feature_selection(
4447
):
4548
if select_k_features is not None:
4649
selection = run_feature_selection(X, y, select_k_features)
47-
print(f"Using features {[variable_names[i] for i in selection]}")
50+
pysr_logger.info(f"Using features {[variable_names[i] for i in selection]}")
4851
X = X[:, selection]
4952
else:
5053
selection = None

pysr/sr.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Define the PySRRegressor scikit-learn interface."""
22

33
import copy
4+
import logging
45
import os
56
import pickle as pkl
67
import re
@@ -67,6 +68,8 @@
6768

6869
ALREADY_RAN = False
6970

71+
pysr_logger = logging.getLogger(__name__)
72+
7073

7174
def _process_constraints(
7275
binary_operators: list[str],
@@ -1107,7 +1110,7 @@ def from_file(
11071110

11081111
pkl_filename = Path(run_directory) / "checkpoint.pkl"
11091112
if pkl_filename.exists():
1110-
print(f"Attempting to load model from {pkl_filename}...")
1113+
pysr_logger.info(f"Attempting to load model from {pkl_filename}...")
11111114
assert binary_operators is None
11121115
assert unary_operators is None
11131116
assert n_features_in is None
@@ -1129,7 +1132,7 @@ def from_file(
11291132

11301133
return model
11311134
else:
1132-
print(
1135+
pysr_logger.info(
11331136
f"Checkpoint file {pkl_filename} does not exist. "
11341137
"Attempting to recreate model from scratch..."
11351138
)
@@ -1232,12 +1235,16 @@ def __getstate__(self) -> dict[str, Any]:
12321235
)
12331236
state_keys_containing_lambdas = ["extra_sympy_mappings", "extra_torch_mappings"]
12341237
for state_key in state_keys_containing_lambdas:
1235-
if state[state_key] is not None and show_pickle_warning:
1236-
warnings.warn(
1237-
f"`{state_key}` cannot be pickled and will be removed from the "
1238-
"serialized instance. When loading the model, please redefine "
1239-
f"`{state_key}` at runtime."
1240-
)
1238+
warn_msg = (
1239+
f"`{state_key}` cannot be pickled and will be removed from the "
1240+
"serialized instance. When loading the model, please redefine "
1241+
f"`{state_key}` at runtime."
1242+
)
1243+
if state[state_key] is not None:
1244+
if show_pickle_warning:
1245+
warnings.warn(warn_msg)
1246+
else:
1247+
pysr_logger.debug(warn_msg)
12411248
state_keys_to_clear = state_keys_containing_lambdas
12421249
state_keys_to_clear.append("logger_")
12431250
pickled_state = {
@@ -1280,7 +1287,7 @@ def _checkpoint(self):
12801287
try:
12811288
pkl.dump(self, f)
12821289
except Exception as e:
1283-
print(f"Error checkpointing model: {e}")
1290+
pysr_logger.debug(f"Error checkpointing model: {e}")
12841291
self.show_pickle_warnings_ = True
12851292

12861293
def get_pkl_filename(self) -> Path:
@@ -1752,7 +1759,7 @@ def _pre_transform_training_data(
17521759
self.selection_mask_ = selection_mask
17531760
self.feature_names_in_ = _check_feature_names_in(self, variable_names)
17541761
self.display_feature_names_in_ = self.feature_names_in_
1755-
print(f"Using features {self.feature_names_in_}")
1762+
pysr_logger.info(f"Using features {self.feature_names_in_}")
17561763

17571764
# Denoising transformation
17581765
if self.denoise:
@@ -1824,7 +1831,7 @@ def _run(
18241831

18251832
# Start julia backend processes
18261833
if not ALREADY_RAN and runtime_params.update_verbosity != 0:
1827-
print("Compiling Julia backend...")
1834+
pysr_logger.info("Compiling Julia backend...")
18281835

18291836
parallelism, numprocs = _map_parallelism_params(
18301837
self.parallelism, self.procs, getattr(self, "multithreading", None)

0 commit comments

Comments
 (0)