optima-ml 0.3.2a4__tar.gz → 0.3.2a5.dev1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/builtin/evaluation.py +4 -1
  2. optima_ml-0.3.2a5.dev1/OPTIMA/builtin/search_space.py +58 -0
  3. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/core/evaluation.py +6 -126
  4. optima_ml-0.3.2a5.dev1/OPTIMA/core/search_space.py +538 -0
  5. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/core/training.py +0 -1
  6. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/hardware_configs/Dresden_Taurus.py +1 -1
  7. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/keras/model.py +1 -1
  8. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/optima.py +79 -61
  9. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/PKG-INFO +3 -2
  10. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/README.md +1 -1
  11. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/optima_ml.egg-info/PKG-INFO +3 -2
  12. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/optima_ml.egg-info/requires.txt +1 -0
  13. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/setup.py +1 -0
  14. optima-ml-0.3.2a4/OPTIMA/builtin/search_space.py +0 -276
  15. optima-ml-0.3.2a4/OPTIMA/core/search_space.py +0 -188
  16. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/LICENSE +0 -0
  17. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/__init__.py +0 -0
  18. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/__main__.py +0 -0
  19. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/builtin/__init__.py +0 -0
  20. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/builtin/figures_of_merit.py +0 -0
  21. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/builtin/inputs.py +0 -0
  22. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/builtin/model.py +0 -0
  23. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/core/__init__.py +0 -0
  24. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/core/inputs.py +0 -0
  25. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/core/model.py +0 -0
  26. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/core/tools.py +0 -0
  27. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/core/variable_optimization.py +0 -0
  28. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/defaults.py +0 -0
  29. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/hardware_configs/__init__.py +0 -0
  30. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/hardware_configs/common.py +0 -0
  31. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/hardware_configs/helpers.py +0 -0
  32. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/helpers/__init__.py +0 -0
  33. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/helpers/extract_data_from_NTuples.py +0 -0
  34. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/helpers/manage_ray_nodes.py +0 -0
  35. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/keras/__init__.py +0 -0
  36. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/keras/tools.py +0 -0
  37. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/keras/training.py +0 -0
  38. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/lightning/__init__.py +0 -0
  39. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/lightning/inputs.py +0 -0
  40. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/lightning/training.py +0 -0
  41. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/resources/__init__.py +0 -0
  42. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/resources/config_verification.py +0 -0
  43. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/OPTIMA/resources/pbt_with_seed.py +0 -0
  44. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/optima_ml.egg-info/SOURCES.txt +0 -0
  45. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/optima_ml.egg-info/dependency_links.txt +0 -0
  46. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/optima_ml.egg-info/entry_points.txt +0 -0
  47. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/optima_ml.egg-info/top_level.txt +0 -0
  48. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/setup.cfg +0 -0
  49. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/tests/test_builtin.py +0 -0
  50. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/tests/test_core.py +0 -0
  51. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/tests/test_integration.py +0 -0
  52. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/tests/test_integration_sameMachine.py +0 -0
  53. {optima-ml-0.3.2a4 → optima_ml-0.3.2a5.dev1}/tests/test_preprocessing.py +0 -0
@@ -578,7 +578,10 @@ def evaluate(
578
578
  # for each contribution to the histogram, we need to provide the type of contribution (step or scatter), the
579
579
  # index of the reference to use for the ratio subplot, and bin content itself..
580
580
  bin_contents_with_type = zip(["step"] * num_classes, [None] * num_classes, bin_contents)
581
- colors = sns.color_palette()
581
+ if (num_classes <= 10 and not explicit_testing_dataset) or num_classes <= 5:
582
+ colors = sns.color_palette()
583
+ else:
584
+ colors = sns.color_palette("husl", num_classes if not explicit_testing_dataset else 2 * num_classes)
582
585
  colors_errors = [None] * (num_classes - 1) + ["0.4"] # only the uppermost contribution should have error bars
583
586
  if class_labels is not None:
584
587
  legend_labels = class_labels if not binary_classification else class_labels[::-1]
@@ -0,0 +1,58 @@
1
+ # -*- coding: utf-8 -*-
2
+ """A module that provides functions to handle the search space for the hyperparameter optimization for the build-in multilayer perceptrons."""
3
+ from OPTIMA.core.model import model_config_type
4
+
5
+
6
+ def get_hp_defaults() -> tuple[model_config_type, model_config_type]:
7
+ """Provides default values for all hyperparameters needed by the built-in ``build_model`` and ``compile_model``-functions.
8
+
9
+ This function is specific to the built-in ``build_model`` and ``compile_model``-functions for classification using
10
+ multilayer perceptrons. If they are not overwritten, hyperparameters that are omitted from the search space will be
11
+ set to their default values. When defining an own ``build_model`` or ``compile_model``-function, this functionality
12
+ is disabled and the corresponding default values will NOT be added to the search space, thus all necessary
13
+ hyperparameter are expected to be present in the search space.
14
+
15
+ Returns
16
+ -------
17
+ tuple[model_config_type, model_config_type]
18
+ Dictionaries with the names of all hyperparameters as keys and the corresponding default values as values. The
19
+ first return value contains the hyperparameters of the built-in ``build_model``-function, the second return value
20
+ the hyperparameters of the built-in ``compile_model``-function.
21
+ """
22
+ hyperparameter_defaults_build = {
23
+ "num_layers": 3,
24
+ "units": 32,
25
+ "activation": "swish",
26
+ "kernel_initializer": "auto",
27
+ "bias_initializer": "auto",
28
+ "l1_lambda": 0.0,
29
+ "l2_lambda": 0.0,
30
+ "dropout": 0.1,
31
+ "batch_size": 64,
32
+ }
33
+ hyperparameter_defaults_compile = {
34
+ "learning_rate": 0.001,
35
+ "Adam_beta_1": 0.9,
36
+ "one_minus_Adam_beta_2": 0.001,
37
+ "Adam_epsilon": 1e-7,
38
+ "loss_function": "BinaryCrossentropy",
39
+ }
40
+ return hyperparameter_defaults_build, hyperparameter_defaults_compile
41
+
42
+
43
+ def get_hps_to_mutate() -> tuple[list[str], list[str]]:
44
+ """Provides a list of built-in hyperparameters that allow mutation.
45
+
46
+ This function is specific to the built-in ``build_model`` and ``compile_model``-functions. If either of the two
47
+ functions are overwritten, the corresponding hyperparameters are not assumed to be mutatable anymore.
48
+
49
+ Returns
50
+ -------
51
+ tuple[list[str], list[str]]
52
+ Two lists of mutatable, built-in hyperparameters. The first return value contains the mutatable hyperparameters
53
+ of the built-in ``build_model``-function, the second return value the hyperparameters of the built-in
54
+ ``compile_model``-function.
55
+ """
56
+ mutatable_hps_build = ["l1_lambda", "l2_lambda", "dropout", "batch_size"]
57
+ mutatable_hps_compile = ["learning_rate", "Adam_beta_1", "one_minus_Adam_beta_2", "Adam_epsilon"]
58
+ return mutatable_hps_build, mutatable_hps_compile
@@ -30,7 +30,7 @@ import OPTIMA.core.model
30
30
  import OPTIMA.builtin.evaluation
31
31
  import OPTIMA.builtin.inputs
32
32
  import OPTIMA.builtin.search_space
33
- from OPTIMA.core.search_space import tune_search_space_type, run_config_search_space_entry_type
33
+ from OPTIMA.core.search_space import run_config_search_space_entry_type
34
34
 
35
35
 
36
36
  def evaluate_experiment(
@@ -39,8 +39,7 @@ def evaluate_experiment(
39
39
  run_config: ModuleType,
40
40
  optimize_name: str,
41
41
  optimize_op: Union[Literal["max"], Literal["min"]],
42
- search_space: tune_search_space_type,
43
- run_config_search_space: dict[str, run_config_search_space_entry_type],
42
+ search_space: dict[str, run_config_search_space_entry_type],
44
43
  results_dir: str,
45
44
  inputs_split: list[ray.ObjectRef],
46
45
  targets_split: list[ray.ObjectRef],
@@ -127,9 +126,7 @@ def evaluate_experiment(
127
126
  Name of the target metric.
128
127
  optimize_op : Union[Literal["max"], Literal["min"]]
129
128
  Specifies if the target metric is to be maximized or minimized. Can be either ``'max'`` or ``'min'``.
130
- search_space : tune_search_space_type
131
- The tune search space provided to the Tuner.
132
- run_config_search_space : dict[str, run_config_search_space_entry_type]
129
+ search_space : dict[str, run_config_search_space_entry_type]
133
130
  The search space as defined in the run-config.
134
131
  results_dir : str
135
132
  Path to the directory where the results are to be saved.
@@ -350,12 +347,6 @@ def evaluate_experiment(
350
347
  all_model_configs, trial_ids = pickle.load(file)
351
348
 
352
349
  # start with results from the best metric values
353
- # get the hp limit and rounding function
354
- if hasattr(run_config, "limit_and_round_hyperparameters"):
355
- limit_and_round_hps = run_config.limit_and_round_hyperparameters
356
- else:
357
- limit_and_round_hps = OPTIMA.builtin.search_space.limit_and_round_hyperparameters
358
-
359
350
  dirs_to_evaluate = (
360
351
  []
361
352
  ) # will contain the paths to the directories containing the models to evaluate (best model from optimization + crossvalidation models)
@@ -375,7 +366,7 @@ def evaluate_experiment(
375
366
 
376
367
  # round the config where necessary (in the same way as during the optimization), and add the hyperparameters to
377
368
  # the dataframe containing the configs of the best trials
378
- model_config = limit_and_round_hps(model_configs_to_evaluate[-1])
369
+ model_config = model_configs_to_evaluate[-1]
379
370
  for hp in model_configs_df.index:
380
371
  model_configs_df.loc[hp, metric] = model_config[hp]
381
372
 
@@ -401,7 +392,7 @@ def evaluate_experiment(
401
392
  ] = trial_id # best_trail is full path to the optimization folder while trails in trial_list as only the names
402
393
  break
403
394
  model_configs_to_evaluate.append(model_config_to_evaluate)
404
- model_config = limit_and_round_hps(model_configs_to_evaluate[-1])
395
+ model_config = model_configs_to_evaluate[-1]
405
396
  for hp in model_configs_df.index:
406
397
  model_configs_df.loc[hp, f"{metric} fit"] = model_config[hp]
407
398
  target_folder = os.path.join(results_dir, metric if len(best_trials_fit.index) > 1 else "", "best_fit")
@@ -571,10 +562,7 @@ def evaluate_experiment(
571
562
  optimization_str += "input variables: {}\n\n".format(", ".join(input_handler.get_vars()))
572
563
  optimization_str += "search space:\n"
573
564
  for hp in search_space.keys():
574
- if hp in run_config_search_space.keys():
575
- optimization_str += f"\t{hp}: {run_config_search_space[hp]}\n"
576
- else:
577
- optimization_str += f"\t{hp}: {search_space[hp]}\n"
565
+ optimization_str += f"\t{hp}: {search_space[hp]}\n"
578
566
 
579
567
  # write results to file
580
568
  if write_results:
@@ -738,114 +726,6 @@ def evaluate_experiment(
738
726
  )
739
727
 
740
728
 
741
- def evaluate_search_algorithm_test_step(
742
- analysis: tune.ExperimentAnalysis,
743
- optimize_name: str,
744
- optimize_op: Union[Literal["max"], Literal["min"]],
745
- search_space: tune_search_space_type,
746
- run_config_search_space: dict[str, run_config_search_space_entry_type],
747
- results_dir: str,
748
- inputs_split: list[np.ndarray],
749
- targets_split: list[np.ndarray],
750
- weights_split: list[np.ndarray],
751
- input_handler: OPTIMA.builtin.inputs.InputHandler,
752
- preprocessor: Any,
753
- custom_metrics: Optional[list[tuple[str, Callable]]] = None,
754
- composite_metrics: Optional[list[tuple[str, tuple[str, str], Callable]]] = None,
755
- overtraining_conditions: Optional[list] = None,
756
- write_results: bool = True,
757
- return_results_str: bool = False,
758
- ) -> None:
759
- """(unmaintained!) Performs the evaluation of one of an optimization step when running with option ``--test_search_algorithm``.
760
-
761
- This function performs the same evaluations done in ``evaluate_experiment`` to determine the best trial with the
762
- two evaluation methods but skips the crossvalidation and evaluation of the individual models.
763
-
764
- Parameters
765
- ----------
766
- analysis : tune.ExperimentAnalysis
767
- The ``tune.ExperimentAnalysis``-object extracted from the ``tune.ResultsGrid`` returned by the ``Tuner``.
768
- optimize_name : str
769
- Name of the target metric.
770
- optimize_op : Union[Literal["max"], Literal["min"]]
771
- Specifies if the target metric is to be maximized or minimized. Can be either ``'max'`` or ``'min'``.
772
- search_space : tune_search_space_type
773
- The tune search space provided to the Tuner.
774
- run_config_search_space : dict[str, run_config_search_space_entry_type]
775
- The search space as defined in the run-config.
776
- results_dir : str
777
- Path to the directory where the results are to be saved.
778
- inputs_split : list[np.ndarray]
779
- List containing the numpy array of input features for the training, validation and (if used) testing sets.
780
- targets_split : list[np.ndarray]
781
- List containing the numpy array of target labels for the training, validation and (if used) testing sets.
782
- weights_split : list[np.ndarray]
783
- List containing the numpy array of event weights for the training, validation and (if used) testing sets.
784
- input_handler : OPTIMA.builtin.inputs.InputHandler
785
- Instance of the ``preprocessing.InputHandler``-class
786
- preprocessor : Any
787
- The fitted scaler used to preprocess the input features that were used for training during the optimization.
788
- custom_metrics : Optional[list[tuple[str, Callable]]]
789
- A list of `custom metrics` as defined in the run-config. (Default value = None)
790
- composite_metrics : Optional[list[tuple[str, tuple[str, str], Callable]]]
791
- A list of `composite metrics` as defined in the run-config. (Default value = None)
792
- overtraining_conditions : Optional[list]
793
- A list of `overtraining conditions` as defined in the run-config. (Default value = None)
794
- write_results : bool
795
- If ``True``, the results are written to `results.txt` in ``results_dir``. (Default value = True)
796
- return_results_str : bool
797
- If ``True``, the results string that is printed to console is also returned. (Default value = False)
798
- """
799
- if overtraining_conditions is None:
800
- overtraining_conditions = []
801
- if composite_metrics is None:
802
- composite_metrics = []
803
- if custom_metrics is None:
804
- custom_metrics = []
805
-
806
- # save analysis to disk for later manual evaluation
807
- with open(os.path.join(results_dir, "analysis.pickle"), "wb") as file:
808
- pickle.dump(analysis, file)
809
-
810
- # build a list containing the names of all metrics, grouped together like [[train_loss, val_loss], [train_accuracy, val_accuracy], ...]
811
- metric_names = []
812
- optimize_name_included = False
813
- for metric, _ in custom_metrics:
814
- group = ("train_" + metric, "val_" + metric)
815
- metric_names.append(group)
816
- if optimize_name in group:
817
- optimize_name_included = True
818
- for metric, _, _ in composite_metrics:
819
- metric_names.append(metric)
820
- if metric == optimize_name:
821
- optimize_name_included = True
822
- if not optimize_name_included:
823
- metric_names = [optimize_name] + metric_names
824
-
825
- # get the results dataframes and remove all NaN and inf values
826
- dfs_dirty = analysis.fetch_trial_dataframes()
827
- dfs = clean_analysis_results(dfs_dirty, metric_names)
828
-
829
- # now also save the dataframes
830
- with open(os.path.join(results_dir, "dfs.pickle"), "wb") as file:
831
- pickle.dump(dfs, file)
832
-
833
- # go through dataframes and explicitly check if overtraining conditions are fulfilled, and add results (True/False)
834
- # as new column "overtrained"
835
- dfs_overtraining_checked = check_overtraining(dfs, overtraining_conditions)
836
-
837
- # produce two sets of plots showing the overall progress of the experiment, one set containing all trials as datapoints,
838
- # and one showing the evolution of the "best" trial; both as a function of time
839
- draw_total_progress(
840
- dfs_overtraining_checked,
841
- optimize_name,
842
- optimize_op,
843
- metric_names,
844
- figs_dir=results_dir,
845
- reject_overtrained=True,
846
- )
847
-
848
-
849
729
  def scientific_rounding(value, err, notation="separate"):
850
730
  """Helper function to perform scientific rounding based on the provided uncertainty.
851
731