Neps space
This module provides functionality for resolving NePS spaces, including sampling from domains, resolving pipelines, and handling various resolvable objects.
NepsCompatConverter
#
A class to convert between NePS configurations and NEPS-compatible configurations. It provides methods to convert a SamplingResolutionContext to a NEPS-compatible config and to convert a NEPS-compatible config back to a SamplingResolutionContext.
from_neps_config
classmethod
#
Convert a NEPS-compatible config to a SamplingResolutionContext.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
A mapping of NEPS-compatible configuration keys to their values. |
| RETURNS | DESCRIPTION |
|---|---|
_FromNepsConfigResult
|
A _FromNepsConfigResult containing predefined samplings, environment values, and extra kwargs. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the config is not a valid NEPS-compatible config. |
Source code in neps/space/neps_spaces/neps_space.py
to_neps_config
classmethod
#
to_neps_config(
resolution_context: SamplingResolutionContext,
) -> Mapping[str, Any]
Convert a SamplingResolutionContext to a NEPS-compatible config.
| PARAMETER | DESCRIPTION |
|---|---|
resolution_context
|
The SamplingResolutionContext to convert. |
| RETURNS | DESCRIPTION |
|---|---|
Mapping[str, Any]
|
A mapping of NEPS-compatible configuration keys to their values. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the resolution_context is not a SamplingResolutionContext. |
Source code in neps/space/neps_spaces/neps_space.py
SamplingResolutionContext
#
SamplingResolutionContext(
*,
resolution_root: Resolvable,
domain_sampler: DomainSampler,
environment_values: Mapping[str, Any]
)
A context for resolving samplings in a NePS space. It manages the resolution root, domain sampler, environment values, and keeps track of samplings made and resolved objects.
| PARAMETER | DESCRIPTION |
|---|---|
resolution_root
|
The root of the resolution, which should be a Resolvable object.
TYPE:
|
domain_sampler
|
The DomainSampler to use for sampling from Domain objects.
TYPE:
|
environment_values
|
A mapping of environment values that are fixed and not related to samplings. These values can be used in the resolution process. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the resolution_root is not a Resolvable, or if the domain_sampler is not a DomainSampler, or if the environment_values is not a Mapping. |
| PARAMETER | DESCRIPTION |
|---|---|
resolution_root
|
The root of the resolution, which should be a Resolvable object.
TYPE:
|
domain_sampler
|
The DomainSampler to use for sampling from Domain objects.
TYPE:
|
environment_values
|
A mapping of environment values that are fixed and not related to samplings. These values can be used in the resolution process. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the resolution_root is not a Resolvable, or if the domain_sampler is not a DomainSampler, or if the environment_values is not a Mapping. |
Source code in neps/space/neps_spaces/neps_space.py
environment_values
property
#
resolution_root
property
#
resolution_root: Resolvable
Get the root of the resolution.
| RETURNS | DESCRIPTION |
|---|---|
Resolvable
|
The root of the resolution, which should be a Resolvable object. |
samplings_made
property
#
add_resolved
#
Add a resolved object to the context.
| PARAMETER | DESCRIPTION |
|---|---|
original
|
The original object that was resolved.
TYPE:
|
resolved
|
The resolved value of the original object.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the original object was already resolved or if it is a Resample. |
Source code in neps/space/neps_spaces/neps_space.py
get_resolved
#
Get the resolved value for the given object.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The object for which to get the resolved value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The resolved value of the object. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the object was not already resolved in the context. |
Source code in neps/space/neps_spaces/neps_space.py
get_value_from_environment
#
Get a value from the environment variables.
| PARAMETER | DESCRIPTION |
|---|---|
var_name
|
The name of the environment variable to get the value from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The value of the environment variable. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the environment variable is not found in the context. |
Source code in neps/space/neps_spaces/neps_space.py
resolving
#
Context manager for resolving an object in the current resolution context.
| PARAMETER | DESCRIPTION |
|---|---|
_obj
|
The object being resolved, can be any type.
TYPE:
|
name
|
The name of the object being resolved, used for debugging.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the name is not a valid string. |
Source code in neps/space/neps_spaces/neps_space.py
sample_from
#
Sample a value from the given domain object.
| PARAMETER | DESCRIPTION |
|---|---|
domain_obj
|
The domain object from which to sample a value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The sampled value from the domain object. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the domain object was already resolved or if the path has already been sampled from. |
Source code in neps/space/neps_spaces/neps_space.py
was_already_resolved
#
SamplingResolver
#
A class responsible for resolving samplings in a NePS space. It uses a SamplingResolutionContext to manage the resolution process, and a DomainSampler to sample values from Domain objects.
__call__
#
__call__(
obj: Resolvable,
domain_sampler: DomainSampler,
environment_values: Mapping[str, Any],
) -> tuple[Resolvable, SamplingResolutionContext]
Resolve the given object in the context of the provided domain sampler and environment values.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The Resolvable object to resolve.
TYPE:
|
domain_sampler
|
The DomainSampler to use for sampling from Domain objects.
TYPE:
|
environment_values
|
A mapping of environment values that are fixed and not related to samplings. |
| RETURNS | DESCRIPTION |
|---|---|
tuple[Resolvable, SamplingResolutionContext]
|
A tuple containing the resolved object and the SamplingResolutionContext. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the object is not a Resolvable, or if the domain_sampler is not a DomainSampler, or if the environment_values is not a Mapping. |
Source code in neps/space/neps_spaces/neps_space.py
adjust_evaluation_pipeline_for_neps_space
#
adjust_evaluation_pipeline_for_neps_space(
evaluation_pipeline: Callable[
..., EvaluatePipelineReturn
],
pipeline_space: P,
operation_converter: Callable[
[Operation], Any
] = convert_operation_to_callable,
) -> Callable
Adjust the evaluation pipeline to work with a NePS space. This function wraps the evaluation pipeline to sample from the NePS space and convert the sampled pipeline to a format compatible with the evaluation pipeline.
| PARAMETER | DESCRIPTION |
|---|---|
evaluation_pipeline
|
The evaluation pipeline to adjust.
TYPE:
|
pipeline_space
|
The NePS pipeline space to sample from.
TYPE:
|
operation_converter
|
A callable to convert Operation objects to a format compatible with the evaluation pipeline.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable
|
A wrapped evaluation pipeline that samples from the NePS space. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the evaluation_pipeline is not callable or if the pipeline_space is not a Pipeline object. |
Source code in neps/space/neps_spaces/neps_space.py
check_neps_space_compatibility
#
check_neps_space_compatibility(
optimizer_to_check: (
OptimizerChoice
| Mapping[str, Any]
| tuple[OptimizerChoice, Mapping[str, Any]]
| Callable[
Concatenate[SearchSpace, ...], AskFunction
]
| Callable[
Concatenate[PipelineSpace, ...], AskFunction
]
| Callable[
Concatenate[SearchSpace | PipelineSpace, ...],
AskFunction,
]
| CustomOptimizer
| Literal["auto"]
) = "auto",
) -> Literal["neps", "classic", "both"]
Check if the given optimizer is compatible with a NePS space. This function checks if the optimizer is a NePS-specific algorithm, a classic algorithm, or a combination of both.
| PARAMETER | DESCRIPTION |
|---|---|
optimizer_to_check
|
The optimizer to check for compatibility. It can be a NePS-specific algorithm, a classic algorithm, or a combination of both.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Literal['neps', 'classic', 'both']
|
A string indicating the compatibility: - "neps" if the optimizer is a NePS-specific algorithm, - "classic" if the optimizer is a classic algorithm, - "both" if the optimizer is a combination of both. |
Source code in neps/space/neps_spaces/neps_space.py
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 | |
construct_sampling_path
#
Construct a sampling path for a domain object.
The sampling path uniquely identifies a sampled value in the resolution context. It consists of the hierarchical path through the pipeline space and a domain identifier that includes type and range information.
| PARAMETER | DESCRIPTION |
|---|---|
path_parts
|
The hierarchical path parts (e.g., ["Resolvable", "integer1"]). |
domain_obj
|
The domain object for which to construct the path.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
A string representing the full sampling path in the format:
" |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If path_parts is empty or domain_obj is not a Domain. |
Source code in neps/space/neps_spaces/neps_space.py
convert_classic_to_neps_search_space
#
convert_classic_to_neps_search_space(
space: SearchSpace,
) -> PipelineSpace
Convert a classic SearchSpace to a NePS PipelineSpace if possible. This function converts a classic SearchSpace to a NePS PipelineSpace.
| PARAMETER | DESCRIPTION |
|---|---|
space
|
The classic SearchSpace to convert.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PipelineSpace
|
A NePS PipelineSpace. |
Source code in neps/space/neps_spaces/neps_space.py
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 | |
convert_neps_to_classic_search_space
#
convert_neps_to_classic_search_space(
space: PipelineSpace,
) -> SearchSpace | None
Convert a NePS space to a classic SearchSpace if possible. This function checks if the NePS space can be converted to a classic SearchSpace by ensuring that it does not contain any complex types like Operation or Resample, and that all choices of Categorical parameters are of basic types (int, str, float). If the checks pass, it converts the NePS space to a classic SearchSpace.
| PARAMETER | DESCRIPTION |
|---|---|
space
|
The NePS space to convert, which should be a Pipeline object.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SearchSpace | None
|
A classic SearchSpace if the conversion is possible, otherwise None. |
Source code in neps/space/neps_spaces/neps_space.py
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 | |
convert_operation_to_callable
#
Convert an Operation to a callable that can be executed.
| PARAMETER | DESCRIPTION |
|---|---|
operation
|
The Operation to convert.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable
|
A callable that represents the operation. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the operation is not a valid Operation object. |
Source code in neps/space/neps_spaces/neps_space.py
resolve
#
resolve(
pipeline: P,
domain_sampler: DomainSampler | None = None,
environment_values: Mapping[str, Any] | None = None,
) -> tuple[P, SamplingResolutionContext]
Resolve a NePS pipeline with the given domain sampler and environment values.
| PARAMETER | DESCRIPTION |
|---|---|
pipeline
|
The pipeline to resolve, which should be a Pipeline object.
TYPE:
|
domain_sampler
|
The DomainSampler to use for sampling from Domain objects. If None, a RandomSampler with no predefined values will be used.
TYPE:
|
environment_values
|
A mapping of environment variable names to their values. If None, an empty mapping will be used. |
| RETURNS | DESCRIPTION |
|---|---|
tuple[P, SamplingResolutionContext]
|
A tuple containing the resolved pipeline and the SamplingResolutionContext. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the pipeline is not a Pipeline object or if the domain_sampler is not a DomainSampler or if the environment_values is not a Mapping. |