|
5 | 5 | import textwrap |
6 | 6 | from functools import lru_cache |
7 | 7 | from importlib.metadata import EntryPoint, entry_points |
| 8 | +from importlib.util import find_spec |
8 | 9 | from typing import Any, Callable, Dict, Iterable, List, Literal, Optional, Union |
9 | 10 |
|
10 | 11 | from semantic_version import SimpleSpec, Version |
@@ -459,6 +460,43 @@ def log_once(msg: str, log_type: Literal["warning", "error", "info", "debug"]) - |
459 | 460 | }.get(log_type, log.info)(msg) |
460 | 461 |
|
461 | 462 |
|
| 463 | +def set_optimiser(name: str) -> None: |
| 464 | + """ |
| 465 | + Set optimiser for fitting interface. |
| 466 | +
|
| 467 | + Alternatively, optimiser can be set through terminal via |
| 468 | +
|
| 469 | + .. code:: bash |
| 470 | +
|
| 471 | + >>> export SPEY_OPTIMISER=<name> |
| 472 | +
|
| 473 | + spey will automatically track ``SPEY_OPTIMISER`` settings. |
| 474 | +
|
| 475 | + .. versionadded:: 0.2.6 |
| 476 | +
|
| 477 | + Args: |
| 478 | + name (``str``): name of the optimiser, ``scipy`` or ``minuit``. |
| 479 | + """ |
| 480 | + log.debug( |
| 481 | + "Currently optimiser is set to: `%s`", os.environ.get("SPEY_OPTIMISER", "scipy") |
| 482 | + ) |
| 483 | + if name in ["minuit", "iminuit"]: |
| 484 | + if find_spec("iminuit") is not None: |
| 485 | + os.environ["SPEY_OPTIMISER"] = "minuit" |
| 486 | + log.debug("Optimiser set to minuit.") |
| 487 | + else: |
| 488 | + log.error("iminuit package is not available.") |
| 489 | + elif name == "scipy": |
| 490 | + os.environ["SPEY_OPTIMISER"] = "scipy" |
| 491 | + log.debug("Optimiser set to scipy.") |
| 492 | + else: |
| 493 | + log.error( |
| 494 | + "Unknown optimiser: %s. The optimiser is set to %s", |
| 495 | + name, |
| 496 | + os.environ.get("SPEY_OPTIMISER", "scipy"), |
| 497 | + ) |
| 498 | + |
| 499 | + |
462 | 500 | if int(os.environ.get("SPEY_LOGLEVEL", -1)) >= 0: |
463 | 501 | set_log_level(int(os.environ.get("SPEY_LOGLEVEL"))) |
464 | 502 |
|
|
0 commit comments