Skip to content

Commit

Permalink
better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Morris committed Jan 14, 2025
1 parent 0042d1c commit 567e12e
Show file tree
Hide file tree
Showing 26 changed files with 137 additions and 113 deletions.
41 changes: 1 addition & 40 deletions docs/source/tutorials/MUSTANG-2_cluster.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
" units=\"Jy/pixel\", # Units of the input map\n",
")\n",
"\n",
"input_map.data *= 1e1\n",
"input_map.to(units=\"uK_RJ\").plot()"
]
},
Expand Down Expand Up @@ -137,26 +138,6 @@
"tod = sim.run()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tod.metadata"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from maria.spectrum import AtmosphericSpectrum\n",
"\n",
"AtmosphericSpectrum(region=tod.metadata[\"region\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -220,26 +201,6 @@
"output_map = mapper.run()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sim.atmosphere.weather.pwv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"pd.read_csv(\"/Users/tom/maria/src/maria/site/regions.csv\").dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
1 change: 0 additions & 1 deletion maria/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import logging

from . import utils # noqa
from ._version import __version__, __version_tuple__ # noqa
from .instrument import Band, Instrument, all_instruments, get_instrument # noqa
from .map import ProjectedMap # noqa
Expand Down
7 changes: 5 additions & 2 deletions maria/atmosphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

from ..functions import approximate_normalized_matern
from ..spectrum import AtmosphericSpectrum
from ..utils import compute_aligning_transform, human_time
from ..utils import compute_aligning_transform
from ..io import humanize_time
from ..weather import Weather
from .extrusion import ProcessExtrusion, generate_layers

Expand All @@ -23,6 +24,8 @@

SUPPORTED_MODELS_LIST = ["2d", "3d"]

DEFAULT_ATMOSPHERE_KWARGS = {}


class Atmosphere:
def __init__(
Expand Down Expand Up @@ -194,7 +197,7 @@ def initialize(self, sim, timestep: float = 1e-1, max_height=3e3):
triangulation = sp.spatial.Delaunay(tp[..., 1:])

logger.debug(
f"Computed process bounds in {human_time(ttime.monotonic() - process_init_s)}."
f"Computed process bounds in {humanize_time(ttime.monotonic() - process_init_s)}."
)
# proc_s = ttime.monotonic()

Expand Down
1 change: 1 addition & 0 deletions maria/calibration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .calibration import Calibration # noqa
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pandas as pd

from .quantities import parse_units, QUANTITIES
from ..units import parse_units, QUANTITIES
from ..spectrum import AtmosphericSpectrum
from ..constants import k_B, T_CMB
from ..functions.radiometry import (
Expand Down
6 changes: 3 additions & 3 deletions maria/coords/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from scipy.interpolate import interp1d
from copy import deepcopy

from ..io import DEFAULT_TIME_FORMAT
from ..utils import repr_lat_lon, human_time
from ..io import humanize_time, DEFAULT_TIME_FORMAT
from ..utils import repr_lat_lon
from .transforms import (
dx_dy_to_phi_theta,
get_center_phi_theta,
Expand Down Expand Up @@ -119,7 +119,7 @@ def __init__(
self.compute_transforms()
duration_s = ttime.monotonic() - ref_time
logger.debug(
f"Initialized coordinates with shape {self.shape} in {human_time(duration_s)}.",
f"Initialized coordinates with shape {self} in {humanize_time(duration_s)}.",
) # noqa

def compute_transforms(self):
Expand Down
2 changes: 1 addition & 1 deletion maria/instrument/band/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ...functions import planck_spectrum
from ...utils import flatten_config, read_yaml
from ...spectrum import AtmosphericSpectrum
from ...units import Calibration
from ...calibration import Calibration
from ...constants import T_CMB, c, k_B

here, this_filename = os.path.split(__file__)
Expand Down
12 changes: 10 additions & 2 deletions maria/instrument/configs/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
aliases: ["test_1deg"]
description: A simple test array
array:
field_of_view: 1
field_of_view: 0.5
primary_size: 5
bands:
f090:
center: 90
width: 30
efficiency: 0.6
f150:
center: 150
width: 30
width: 40
efficiency: 0.5
f220:
center: 220
width: 50
efficiency: 0.4

10deg:
aliases: ["test_10deg"]
Expand Down
1 change: 1 addition & 0 deletions maria/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .caching import * # noqa
from .logging import * # noqa

DEFAULT_TIME_FORMAT = "YYYY-MM-DD HH:mm:ss.SSS ZZ"
27 changes: 27 additions & 0 deletions maria/io/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import time as ttime
import logging
from ..units import prefixes


def humanize_time(seconds):

for _, prefix in prefixes.iterrows():
if prefix.time:
value = seconds / prefix.factor
if value < 1e3:
break

if value > 100:
value = round(value)
elif value > 10:
value = round(value, 1)
else:
value = round(value, 2)

return f"{value} {prefix.name}s"


def log_duration(ref_time, message, level="debug"):
logger = logging.getLogger("maria")
string = f"{message} in {humanize_time(ttime.monotonic() - ref_time)}."
getattr(logger, level)(string)
3 changes: 2 additions & 1 deletion maria/map/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

from ..instrument import Band
from ..constants import k_B
from ..units import Angle, Calibration, parse_units # noqa
from ..calibration import Calibration
from ..units import parse_units

# from ..plotting import plot_map

Expand Down
3 changes: 2 additions & 1 deletion maria/map/projected.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@


from .base import Map
from ..units import prefixes, QUANTITIES, parse_units
from ..units import QUANTITIES, parse_units
from ..units import prefixes


here, this_filename = os.path.split(__file__)
Expand Down
10 changes: 7 additions & 3 deletions maria/mappers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ..map import ProjectedMap
from ..tod import TOD

from ..utils import human_time
from ..io import humanize_time

# np.seterr(invalid="ignore")

Expand Down Expand Up @@ -93,7 +93,7 @@ def run(self):
band_start_s = ttime.monotonic()
self.map_data[band] = self._run(band)
logger.info(
f"Ran mapper for band {band.name} in {human_time(ttime.monotonic() - band_start_s)}."
f"Ran mapper for band {band.name} in {humanize_time(ttime.monotonic() - band_start_s)}."
)

map_data = np.zeros((len(self.map_data), 1, self.n_y, self.n_x))
Expand Down Expand Up @@ -121,8 +121,12 @@ def run(self):
map_data[i, :] = band_map_numer / band_map_denom
map_weight[i, :] = band_map_denom

map_offsets = np.nansum(map_data * map_weight, axis=(-1, -2)) / map_weight.sum(
axis=(-1, -2)
)

return ProjectedMap(
data=map_data,
data=map_data - map_offsets[..., None, None],
weight=map_weight,
nu=map_freqs,
resolution=self.resolution,
Expand Down
11 changes: 6 additions & 5 deletions maria/plotting/tod.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
from matplotlib.collections import EllipseCollection
from matplotlib.patches import Patch

from maria.instrument.beam import compute_angular_fwhm
from maria.units import Angle, parse_units
from maria.utils.signal import detrend as detrend_signal
from maria.utils.signal import remove_slope
from ..instrument.beam import compute_angular_fwhm
from ..units import Angle, parse_units
from ..utils.signal import detrend as detrend_signal
from ..utils.signal import remove_slope

from ..units import QUANTITIES, prefixes
from ..units import QUANTITIES
from ..units import prefixes


def tod_plot(
Expand Down
14 changes: 7 additions & 7 deletions maria/sim/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ..cmb import CMB, generate_cmb, get_cmb, DEFAULT_CMB_KWARGS
from ..errors import PointingError
from ..instrument import Instrument
from ..io import humanize_time
from ..map import Map
from ..plan import Plan
from ..site import Site
Expand All @@ -23,7 +24,6 @@
from .map import MapMixin
from .noise import NoiseMixin

from ..utils import human_time

here, this_filename = os.path.split(__file__)
logger = logging.getLogger("maria")
Expand Down Expand Up @@ -70,7 +70,7 @@ def __init__(
)

logger.debug(
f"Initialized generic simulation in {human_time(ttime.monotonic() - sim_init_start_s)}."
f"Initialized generic simulation in {humanize_time(ttime.monotonic() - sim_init_start_s)}."
)
base_init_s = ttime.monotonic()

Expand Down Expand Up @@ -119,7 +119,7 @@ def __init__(
self.atmosphere.initialize(self)

logger.debug(
f"Initialized atmosphere simulation in {human_time(ttime.monotonic() - base_init_s)}."
f"Initialized atmosphere simulation in {humanize_time(ttime.monotonic() - base_init_s)}."
)
atmosphere_init_s = ttime.monotonic()

Expand All @@ -137,7 +137,7 @@ def __init__(
raise ValueError(f"Invalid value for cmb: '{cmb}'.")

logger.debug(
f"Initialized CMB simulation in {human_time(ttime.monotonic() - atmosphere_init_s)}."
f"Initialized CMB simulation in {humanize_time(ttime.monotonic() - atmosphere_init_s)}."
)
cmb_init_s = ttime.monotonic()

Expand All @@ -159,20 +159,20 @@ def __init__(
self.map = map.to(units="K_RJ")

logger.debug(
f"Initialized map simulation in {human_time(ttime.monotonic() - cmb_init_s)}."
f"Initialized map simulation in {humanize_time(ttime.monotonic() - cmb_init_s)}."
)
map_init_s = ttime.monotonic()

if noise:
pass

logger.debug(
f"Initialized noise simulation in {human_time(ttime.monotonic() - map_init_s)}."
f"Initialized noise simulation in {humanize_time(ttime.monotonic() - map_init_s)}."
)
# noise_init_s = ttime.monotonic()

logger.debug(
f"Initialized simulation in {human_time(ttime.monotonic() - sim_init_start_s)}."
f"Initialized simulation in {humanize_time(ttime.monotonic() - sim_init_start_s)}."
)

def _run(self):
Expand Down
4 changes: 2 additions & 2 deletions maria/tests/test_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np

from maria import Simulation
from maria.units import Calibration
from maria.calibration import Calibration


def test_brightness_temperature_to_spectral_flux_density_per_pixel():
Expand All @@ -13,7 +13,7 @@ def test_brightness_temperature_to_spectral_flux_density_per_pixel():
)


def test_decalibration():
def test_involution():

sim = Simulation(
instrument="MUSTANG-2",
Expand Down
10 changes: 7 additions & 3 deletions maria/tests/test_cmb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def test_cmb_calibration():

plan = maria.Plan(
scan_pattern="daisy",
scan_options={"radius": 5, "speed": 1}, # in degrees
duration=60, # in seconds
scan_options={"radius": 10, "speed": 1}, # in degrees
duration=120, # in seconds
sample_rate=50, # in Hz
scan_center=(150, 10),
jitter=0,
Expand All @@ -33,6 +33,10 @@ def test_cmb_calibration():

tod = sim.run().to("uK_CMB")

tod.to("uK_CMB").signal.std(axis=1).compute()

# should be around 110 uK
cmb_rms_uK = tod.signal.std(axis=-1).compute()

assert 90 < cmb_rms_uK.mean() < 130 # should be around 110 uK
assert all(cmb_rms_uK > 80)
assert all(cmb_rms_uK < 150)
17 changes: 17 additions & 0 deletions maria/tests/test_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ def test_fetch_fits_map(map_name):
m.plot()


@pytest.mark.parametrize(
"map_name",
["maps/cluster.fits", "maps/big_cluster.fits", "maps/galaxy.fits"],
) # noqa
def test_map_units_conversion(map_name):
map_filename = fetch(map_name)
m = maria.map.load(
filename=map_filename,
nu=90,
resolution=1 / 1024,
center=(150, 10),
units="Jy/pixel",
)

assert np.allclose(m.to("K_RJ").to("Jy/pixel").data, m.data).compute()


@pytest.mark.parametrize("map_name", ["maps/sun.h5"]) # noqa
def test_fetch_hdf_map(map_name):
map_filename = fetch(map_name)
Expand Down
Loading

0 comments on commit 567e12e

Please sign in to comment.