Model Iterator¶
Classes for the iteration of models. The parameter space page contains classes related to parameter search algorithms.
API¶
Classes:
ModelInnerIterator([config, par_generator, ...])Class to run all models in a single iteration.
ModelIterator([config, model_kwargs, ...])Iterator for models
SplitModelIterator(**kwargs)Class to run all models in a single iteration.
- class model_iterator.ModelInnerIterator(config=None, par_generator=None, do_dummy_run=False, dummy_chi2_function=None)¶
Class to run all models in a single iteration.
Uses
pathos.multiprocessing.Poolto execute the models
- Parameters:
- configa
dyn.config_reader.Configurationobject- par_generatora
dyn.parameter_space.ParameterGeneratorchild object- do_dummy_runBool
whether this is a dummy run - if so, dummy_chi2_function is executed instead of the model (for testing!)
- dummy_chi2_functionfunction
a function of model parameters to be executed instead of the real model
Methods:
assign_model_directories([rows_orblib, rows_ml])Assigns model directories in all_models.table.
create_and_run_model(data_input)Main method to create and run a model
is_new_orblib(row_idx)Checks whether the orbit library characterized by the parameters in row number row_idx exists in earlier rows of self.all_models.table.
run_iteration([split_orblib_weights])Run one iteration step
write_output_to_all_models_table(rows_to_do, ...)
- assign_model_directories(rows_orblib=[], rows_ml=[])¶
Assigns model directories in all_models.table.
Models indexed by rows_orblib: The model directories follow the pattern orblib_xxx_yyy/mlzz.zz/ where xxx is the iteration number, yyy a consecutive number of that iteration’s orbit library, and zz.zz is the value of the models’ ml parameter in the format given in its sformat attribute.
Models indexed by rows_ml: These models re-use an existing orbit library. Hence, their directory strings re-use an existing orblib_xxx_yyy part and get augmented with the appropriate /mlzz.zz/.
- Parameters:
- rows_orbliblist, optional
Indices of models with new orbit libraries. The default is None.
- rows_mllist, optional
Indices of models with existing orbit libraries. The default is None.
- Returns:
- None.
- Raises:
- ValueError
If the orbit library of a model in rows_ml cannot be found in all_models.table.
- create_and_run_model(data_input)¶
Main method to create and run a model
- Parameters:
- data_inputtuple of length 2 or 5
len(input)==2: (i, row) where i is the index of a model in this iteration, and row is the row index of the all_models table for this model. Both the orblib and the weights will be computed, but no external chi2. len(input)==5: (i, row, get_orblib, get_weights, get_chi2_ext) where i and row are as above, get_orblib==True if the orblib needs to be computed get_weights==True if the weights need to be computed get_chi2_ext==True if the external chi2 needs to be computed and added to all DYNAMITE chi2 values (incompatible with get_orblib==True or get_weights==True)
- Returns:
- tuple
all the output for this model, bundled up in a tuple
- is_new_orblib(row_idx)¶
Checks whether the orbit library characterized by the parameters in row number row_idx exists in earlier rows of self.all_models.table.
- Parameters:
- row_idxint
Row index of the model entry to be checked.
- Returns:
- is_newbool
True if no earlier row contains the orbit library, False otherwise.
- run_iteration(split_orblib_weights=False)¶
Run one iteration step
Executes one iteration step: run all models in self.all_models.table for which all_done == False. The model runs (1) build the orbit library and (2) execute weight_solver. The models are run in parallel threads as defined in the ncpus parameter in the configuration file. In case multiple models comprise the same (new) orbit library it is ensured that it is calculated only once, avoiding conflicting threads and saving resources. If a method that calculates an external chi2 exists, the resulting chi2 value will be added to the DYNAMITE chi2 values.
- Parameters:
- split_orblib_weightsbool, optional
If True, first the orbit libraries are calculated in self.ncpus parallel pools, then the weights in self.ncpus_weights parallel pools, and finally the external chi2 values in self.ncpus_ext parallel pools. The default is False.
- Returns:
- dict
ParameterGenerator.status.
- write_output_to_all_models_table(rows_to_do, output)¶
- Parameters:
- rows_to_dolist of ints
which rows of all models table to fill with output
- outputlist
output returned by Pool.map
- Returns:
- Fills output into the all_models table
- class model_iterator.ModelIterator(config=None, model_kwargs={}, do_dummy_run=None, dummy_chi2_function=None, plots=True)¶
Iterator for models
Creating this
ModelIteratorobject will (i) generate parameters sets, (ii) run models for those parameters, (iii) check stopping criteria, and iterate this procedure till a stopping criterion is met. This is implemented by creating aModelInnerIteratorobject whoserun_iterationmethod is called a number of times.
- Parameters:
- configa
dyn.config_reader.Configurationobject- model_kwargsdict
other kewyord argument required for this model
- do_dummy_runBool
whether this is a dummy run - if so, dummy_chi2_funciton is executed instead of the model (for testing!)
- dummy_chi2_functionfunction
a function of model parameters to be executed instead of the real model
- plotsbool
whether or not to make plots
Methods:
get_missing_chi2_ext(row)Recalculate chi2_ext for the model in a row of the all_models table
get_missing_weights(row)Recalculate weights for the model in a row of the all_models table
Returns the latest iteration's plots as figure objects.
Recalculates weights and - if applicable - chi2_ext for failed models
- get_missing_chi2_ext(row)¶
Recalculate chi2_ext for the model in a row of the all_models table
- Returns:
- tuple (float, float, float, float, time object)
The model’s chi2, kinchi2, kinmapchi2, chi2_ext_added, time_modified
- get_missing_weights(row)¶
Recalculate weights for the model in a row of the all_models table
- Returns:
- tuple (float, float, float, float, time object)
The model’s chi2, kinchi2, kinmapchi2, chi2_ext_added, time_modified
- get_plots()¶
Returns the latest iteration’s plots as figure objects.
- Returns:
- tuple of matplotlib.pyplot.figure:
matplotlib.pyplot.figure: chi2 vs. model id plot matplotlib.pyplot.figure: chisquare plot (matplotlib.pyplot.figure, str): kinematic maps of best model so far, kinematics name
- reattempt_failed_weights()¶
Recalculates weights and - if applicable - chi2_ext for failed models
- Returns:
- Updates the all_models table
- class model_iterator.SplitModelIterator(**kwargs)¶
Class to run all models in a single iteration.
First calculates the orbit libraries and then runs weight solving. Uses
pathos.multiprocessing.Poolto execute the models. Orbit integration uses a pool of ncpus parallel processes, weight solving uses ncpus_weights from the Configuration object.
- Parameters:
- configa
dyn.config_reader.Configurationobject- par_generatora
dyn.parameter_space.ParameterGeneratorchild object- do_dummy_runBool
whether this is a dummy run - if so, dummy_chi2_funciton is executed instead of the model (for testing!)
- dummy_chi2_functionfunction
a function of model parameters to be executed instead of the real model
Methods:
Execute one iteration step
- run_iteration()¶
Execute one iteration step
Calls the parameter generator and (a) calculates all new orbit libraries and consecutively (b) does the weight solving. (a) and (b) are run in their respective parallel pools as defined by the ncpus and ncpus_weights parameters, respectively.
- Returns:
- dict
ParameterGenerator.status.
Inheritance Diagram¶
