Skip to content

Commit cc807d5

Browse files
authored
Merge pull request #98 from radionets-project/logging
Logging
2 parents 4fd0cd6 + 30eb047 commit cc807d5

File tree

14 files changed

+130
-40
lines changed

14 files changed

+130
-40
lines changed

docs/api-reference/utils/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Submodules
2222

2323
config
2424
data
25+
logging
2526

2627

2728
Reference/API
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.. _utils_logging:
2+
3+
***************************************
4+
Logging (:mod:`pyvisgen.utils.logging`)
5+
***************************************
6+
7+
.. currentmodule:: pyvisgen.utils.logging
8+
9+
Logging submodule of :mod:`pyvisgen.utils`.
10+
11+
12+
Reference/API
13+
=============
14+
15+
.. automodapi:: pyvisgen.utils.logging
16+
:inherited-members:

docs/changes/98.feature.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- Added a new `setup_logger` function in {mod}`pyvisgen.utils`
2+
- Added logging throughout the codebase
3+
- Replaced bare prints with `LOGGER.info` or `LOGGER.warning` for better status messages
4+
- Added exception logging before raising an exception, providing better tracebacks and error context

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
"pandas": ("https://pandas.pydata.org/pandas-docs/stable", None),
186186
"pytest": ("https://docs.pytest.org/en/stable", None),
187187
"python": ("https://docs.python.org/3", None),
188+
"rich": ("https://rich.readthedocs.io/en/stable", None),
188189
"scipy": ("https://docs.scipy.org/doc/scipy", None),
189190
"torch": ("https://pytorch.org/docs/stable/", None),
190191
}

src/pyvisgen/gridding/gridder.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from numpy.exceptions import AxisError
55

66
from pyvisgen.gridding.alt_gridder import ms2dirty_python_fast
7+
from pyvisgen.utils.logging import setup_logger
8+
9+
LOGGER = setup_logger()
710

811

912
def ducc0_gridding(uv_data, freq_data):

src/pyvisgen/gridding/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
from pyvisgen.gridding import grid_data
1010
from pyvisgen.utils.config import read_data_set_conf
1111
from pyvisgen.utils.data import load_bundles, open_bundles
12+
from pyvisgen.utils.logging import setup_logger
1213

1314
os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE"
15+
LOGGER = setup_logger()
1416

1517

1618
def create_gridded_data_set(config):

src/pyvisgen/layouts/layouts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import torch
77
from astropy.coordinates import EarthLocation
88

9+
from pyvisgen.utils.logging import setup_logger
10+
11+
LOGGER = setup_logger()
12+
913

1014
@dataclass
1115
class Stations:

src/pyvisgen/simulation/data_set.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from astropy import units as un
77
from astropy.time import Time
88
from joblib import Parallel, delayed
9-
from rich import print
9+
from rich.pretty import pretty_repr
1010
from tqdm.auto import tqdm
1111

1212
import pyvisgen.fits.writer as writer
@@ -22,9 +22,11 @@
2222
from pyvisgen.simulation.visibility import vis_loop
2323
from pyvisgen.utils.config import read_data_set_conf
2424
from pyvisgen.utils.data import load_bundles, open_bundles
25+
from pyvisgen.utils.logging import setup_logger
2526

2627
__all__ = ["SimulateDataSet"]
2728

29+
LOGGER = setup_logger()
2830

2931
DATEFMT = "%d-%m-%Y %H:%M:%S"
3032

@@ -111,7 +113,8 @@ def from_config(
111113
else:
112114
raise ValueError("Expected config to be one of str, Path or dict!")
113115

114-
print("Simulation Config:\n", cls.conf)
116+
LOGGER.info("Simulation Config:")
117+
LOGGER.info(pretty_repr(cls.conf))
115118

116119
cls.device = cls.conf["device"]
117120

@@ -125,7 +128,7 @@ def from_config(
125128

126129
cls.data_paths = load_bundles(cls.conf["in_path"])
127130

128-
if not cls.num_images:
131+
if cls.num_images is None:
129132
data_bundles = tqdm(
130133
range(len(cls.data_paths)),
131134
position=0,
@@ -211,7 +214,7 @@ def _run(self) -> None:
211214
truth_fft = convert_real_imag(truth_fft, sky_sim=True)
212215

213216
if sim_data.shape[1] != 2:
214-
raise ValueError("Expected sim_data axis 1 to be 2!")
217+
raise ValueError("Expected sim_data axis at index 1 to be 2!")
215218

216219
out = self.out_path / Path(
217220
f"samp_{self.conf['file_prefix']}_" + str(i) + ".h5"
@@ -234,7 +237,7 @@ def _run(self) -> None:
234237
f"samp_{self.conf['file_prefix']}_<id>.fits"
235238
)
236239

237-
print(
240+
LOGGER.info(
238241
f"Successfully simulated and saved {samp_opts_idx} images to '{path_msg}'!"
239242
)
240243

src/pyvisgen/simulation/observation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
from pyvisgen.layouts import layouts
1313
from pyvisgen.simulation.array import Array
14+
from pyvisgen.utils.logging import setup_logger
1415

1516
torch.set_default_dtype(torch.float64)
17+
LOGGER = setup_logger()
1618

1719
__all__ = ["Baselines", "ValidBaselineSubset", "Observation"]
1820

@@ -968,4 +970,5 @@ def calc_direction_cosines(
968970
"Expected u, v, and w to have the same shapes "
969971
f"but got {u.shape}, {v.shape}, and {w.shape}."
970972
)
973+
971974
return u, v, w

src/pyvisgen/simulation/visibility.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from tqdm.auto import tqdm
77

88
import pyvisgen.simulation.scan as scan
9+
from pyvisgen.utils.logging import setup_logger
910

1011
torch.set_default_dtype(torch.float64)
12+
LOGGER = setup_logger()
1113

1214
__all__ = [
1315
"Visibilities",
@@ -500,8 +502,10 @@ def vis_loop(
500502
elif mode == "dense":
501503
if obs.device == torch.device("cpu"):
502504
raise ValueError("Only available for GPU calculations!")
503-
obs.calc_dense_baselines()
504-
bas = obs.dense_baselines_gpu
505+
506+
# We cannot test this at the moment
507+
obs.calc_dense_baselines() # pragma: no cover
508+
bas = obs.dense_baselines_gpu # pragma: nocover
505509
else:
506510
raise ValueError("Unsupported mode!")
507511

0 commit comments

Comments
 (0)