From da55064ce5d5caedfa8f613681d15392b988c00b Mon Sep 17 00:00:00 2001 From: Nicolas Tessore Date: Mon, 8 Jan 2024 13:40:38 +0000 Subject: [PATCH] update CLI --- heracles/cli.py | 32 +++++++++----------------------- tests/test_cli.py | 27 ++++----------------------- 2 files changed, 13 insertions(+), 46 deletions(-) diff --git a/heracles/cli.py b/heracles/cli.py index d55a1cc..2e259da 100644 --- a/heracles/cli.py +++ b/heracles/cli.py @@ -40,9 +40,6 @@ "weights": "heracles.fields:Weights", } -# global path to healpy's data files -HEALPIX_DATAPATH: str | None = None - def getlist(value: str) -> list[str]: """Convert to list.""" @@ -102,7 +99,9 @@ class ConfigParser(configparser.ConfigParser): def __init__(self) -> None: # fully specify parent class super().__init__( - defaults=None, + defaults={ + "kernel": "healpix", + }, dict_type=dict, allow_no_value=False, delimiters=("=",), @@ -189,20 +188,13 @@ def fields_from_config(config): } -def mapper_from_config(config, section): - """Construct a mapper instance from config.""" - - from .maps import Healpix - - nside = config.getint(section, "nside") - return Healpix(nside, datapath=HEALPIX_DATAPATH) - - def mappers_from_config(config): """Construct all mapper instances from config.""" + from .maps import mapper_from_dict + sections = config.subsections("fields") return { - name: mapper_from_config(config, section) for name, section in sections.items() + name: mapper_from_dict(config[section]) for name, section in sections.items() } @@ -497,21 +489,16 @@ def alms( """ - global HEALPIX_DATAPATH - from .io import AlmFits - from .maps import transform_maps + from .maps import Healpix, transform_maps # load the config file, this contains alms setting and maps definition logger.info("reading configuration from %s", files) config = loader(files) # set the HEALPix datapath - # FIXME: make this part of a configurable mapper interface - HEALPIX_DATAPATH = healpix_datapath - - # load the mappers to perform the transformation of each field - mappers = mappers_from_config(config) + if healpix_datapath is not None: + Healpix.DATAPATH = healpix_datapath # load the individual lmax values for each field into a dictionary lmax = lmax_from_config(config) @@ -532,7 +519,6 @@ def alms( for maps in itermaps: logger.info("transforming %d maps", len(maps)) transform_maps( - mappers, maps, lmax=lmax, progress=progress, diff --git a/tests/test_cli.py b/tests/test_cli.py index f3e5f87..0a3d1de 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -168,26 +168,7 @@ def test_fields_from_config(mock): ] -@patch("heracles.cli.HEALPIX_DATAPATH") -@patch("heracles.maps.Healpix") -def test_mapper_from_config(mock, mock_datapath): - from heracles.cli import ConfigParser, mapper_from_config - - config = ConfigParser() - config.read_dict( - { - "a": { - "nside": "1", - }, - }, - ) - - a = mapper_from_config(config, "a") - mock.assert_called_once_with(1, datapath=mock_datapath) - assert mock.return_value is a - - -@patch("heracles.cli.mapper_from_config") +@patch("heracles.maps.mapper_from_dict") def test_mappers_from_config(mock): from heracles.cli import ConfigParser, mappers_from_config @@ -208,9 +189,9 @@ def test_mappers_from_config(mock): "c": mock.return_value, } assert mock.call_args_list == [ - ((config, "fields:a"),), - ((config, "fields:b"),), - ((config, "fields:c"),), + ((config["fields:a"],),), + ((config["fields:b"],),), + ((config["fields:c"],),), ]