Register maps and constants to plugins

Sometimes, a plugin could depend on some constants and maps, for example the energy range in an energy sampler, or a curve that gives the energy spectrum. In appletree, we recommend to use appletree.takes_config to systematically manage them.

appletree.takes_config(*configs)[source]

Decorator for plugin classes, to specify which configs it takes.

Parameters

configs – Config instances of configs this plugin takes.

appletree.takes_config takes Config as arguments, which will be discussed short later, and returns a decorator for the plugin. For example,

@takes_config(
    config0,
    config1,
    ...
)
class TestPlugin(appletree.plugin):
    ...

Currently, appletree supports two kinds of configs, appletree.Constant and appletree.Map. Both inherit from appletree.Config

class appletree.Map(method='IDW', **kwargs)[source]

Bases: Config

Map is a special config that takes input files.

The method apply is dynamically assigned. When using points, the apply will be map_point, while using regular binning, the apply will be map_regbin. When using log-binning, we will first convert the positions to log space.

_get_regbin_interpolator(ndim)[source]

Return the regular-binning interpolator for the given dimensionality.

_validate_log_coords(*coord_arrays)[source]

Raise if any log-scaled coordinate is non-positive.

build(llh_name: Optional[str] = None)[source]

Cache the map to jnp.array.

build_point(data)[source]

Cache the map to jnp.array if bins_type is point.

build_regbin(data)[source]

Cache the map to jnp.array if bins_type is regbin.

coordinate_type may be a single string ("regbin" or "log_regbin") applied to every axis, or a list of such strings with one entry per axis for mixed linear/log grids (e.g. ["regbin", "log_regbin"]).

property lineage
map_point(pos)[source]
map_regbin(pos)[source]
pdf_to_cdf(x, pdf)[source]

Convert pdf map to cdf map.

preprocess(pos)[source]

Apply log10 to axes marked as log in _log_mask.

class appletree.Constant(name: str, type: Union[type, tuple, list, str] = '<OMITTED>', default: Any = '<OMITTED>', help: str = '')[source]

Bases: Config

Constant is a special config which takes only certain value.

build(llh_name: Optional[str] = None)[source]

Set value of Constant.

property lineage
value = None

Here is an example of using Constant

@takes_config(
    Constant(
        name='a',
        type=float,
        default=137.,
        help='A meaningless scaler.',
    ),
)
class Scale(Plugin):
    depends_on = ['x']
    provides = ['y']

    @partial(jit, static_argnums=(0, ))
    def simulate(self, key, parameters, x):
        y = self.a.value * x
        return key, y

and an example of using Map

@takes_config(
    Map(
        name='b',
        default='test_file.json',
        help='A meaningless shift.',
    ),
)
class Shift(Plugin):
    depends_on = ['y']
    provides = ['z']

    @partial(jit, static_argnums=(0, ))
    def simulate(self, key, parameters, y):
        shift = appletree.interpolation.curve_interpolator(
            y,
            self.b.coordinate_system,
            self.s2_bias.map,
        )
        z = y + shift
        return key, z

As mentioned in instruct, the instruct file for Context can overwrite the default value of plugins’ config. For example,

{
    "configs": {
        "a": 137.036,
        "b": "alt_test_file.json",
    },
    ...
}

which changes the value of a in Scale plugin to 137.036 and json file of b in Shift Plugin to “alt_test_file.json”.