Skip to content

Commit

Permalink
update CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ntessore committed Jan 8, 2024
1 parent b0acf2e commit da55064
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 46 deletions.
32 changes: 9 additions & 23 deletions heracles/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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=("=",),
Expand Down Expand Up @@ -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()
}


Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
27 changes: 4 additions & 23 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"],),),
]


Expand Down

0 comments on commit da55064

Please sign in to comment.