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:
ConfigMap is a special config that takes input files.
The method
applyis dynamically assigned. When using points, theapplywill bemap_point, while using regular binning, theapplywill bemap_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.
- build_regbin(data)[source]
Cache the map to jnp.array if bins_type is regbin.
coordinate_typemay 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
- class appletree.Constant(name: str, type: Union[type, tuple, list, str] = '<OMITTED>', default: Any = '<OMITTED>', help: str = '')[source]
Bases:
ConfigConstant is a special config which takes only certain value.
- 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”.