RunModel

RunModel is responsible for running the model in parallel for all selected sets of parameters. It runs Parallel in Parallel. RunModel organizes the results in a Data object.

API Reference

class uncertainpy.core.RunModel(model, parameters, features=None, logger_level=u'info', CPUs=u'max')[source]

Calculate model and feature results for a series of different model parameters, and store them in a Data object.

Parameters:
  • model ({None, Model or Model subclass instance, model function}, optional) – Model to perform uncertainty quantification on. For requirements see Model.run. Default is None.

  • parameters ({dict {name: parameter_object}, dict of {name: value or Chaospy distribution}, …], list of Parameter instances, list [[name, value or Chaospy distribution], …], list [[name, value, Chaospy distribution or callable that returns a Chaospy distribution],…],}) – List or dictionary of the parameters that should be created. On the form parameters =

    • {name_1: parameter_object_1, name: parameter_object_2, ...}
    • {name_1:  value_1 or Chaospy distribution, name_2:  value_2 or Chaospy distribution, ...}
    • [parameter_object_1, parameter_object_2, ...],
    • [[name_1, value_1 or Chaospy distribution], ...].
    • [[name_1, value_1, Chaospy distribution or callable that returns a Chaospy distribution], ...]
  • features ({None, Features or Features subclass instance, list of feature functions}, optional) – Features to calculate from the model result. If None, no features are calculated. If list of feature functions, all will be calculated. Default is None.

  • logger_level ({“info”, “debug”, “warning”, “error”, “critical”, None}, optional) – Set the threshold for the logging level. Logging messages less severe than this level is ignored. If None, no logging to file is performed. Default logger level is “info”.

  • CPUs ({int, None, “max”}, optional) – The number of CPUs to use when calculating the model and features. If None, no multiprocessing is used. If “max”, the maximum number of CPUs on the computer (multiprocess.cpu_count()) is used. Default is “max”.

Variables:
  • model (uncertainpy.Model or subclass of uncertainpy.Model) – The model to perform uncertainty quantification on.
  • parameters (uncertainpy.Parameters) – The uncertain parameters.
  • features (uncertainpy.Features or subclass of uncertainpy.Features) – The features of the model to perform uncertainty quantification on.
  • CPUs (int) – The number of CPUs used when calculating the model and features.
apply_interpolation(results, feature)[source]

Perform interpolation of one model/feature using the interpolation objects created by Parallel.

Parameters:
  • results (list) – A list where each element is a result dictionary for each set of model evaluations. An example:

    result = {self.model.name: {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                                "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
              "feature1d": {"values": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                            "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
              "feature0d": {"values": 1,
                            "time": np.nan},
              "feature2d": {"values": array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                             [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]),
                            "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
              "feature_adaptive": {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                                   "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                                   "interpolation": scipy interpolation object},
              "feature_invalid": {"values": np.nan,
                                  "time": np.nan}}
    
    results = [result 1, result 2, ..., result N]
    
  • feature (str) – Name of a feature or the model.

Returns:

  • time (array_like) – The time array with the greatest number of time steps.
  • interpolated_results (list) – A list containing all interpolated model/features results. Interpolated at the points of the time results with the greatest number of time steps.

Notes

Chooses the time array with the highest number of time points and use this time array to interpolate the model/feature results in each of those points. If an interpolation is None, gives numpy.nan instead.

create_model_parameters(nodes, uncertain_parameters)[source]

Combine nodes (values) with the uncertain parameter names to create a list of dictionaries corresponding to the model values for each model evaluation.

Parameters:
  • nodes (array) – A series of different set of parameters. The model and each feature is evaluated for each set of parameters in the series.
  • uncertain_parameters (list) – A list of names of the uncertain parameters.
Returns:

model_parameters – A list where each element is a dictionary with the model parameters for a single evaluation. An example:

model_parameter = {"parameter 1": value 1, "parameter 2": value 2, ...}
model_parameters = [model_parameter 1, model_parameter 2, ...]

Return type:

list

evaluate_nodes(nodes, uncertain_parameters)[source]

Evaluate the the model and calculate the features for the nodes (values) for the uncertain parameters.

Parameters:
  • nodes (array) – The values for the uncertain parameters to evaluate the model and features for.
  • uncertain_parameters (list) – A list of the names of all uncertain parameters.
Returns:

results – A list where each element is a result dictionary for each set of model evaluations. An example:

result = {self.model.name: {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                            "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature1d": {"values": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                        "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature0d": {"values": 1,
                        "time": np.nan},
          "feature2d": {"values": array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]),
                        "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature_adaptive": {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                               "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                               "interpolation": scipy interpolation object},
          "feature_invalid": {"values": np.nan,
                              "time": np.nan}}

results = [result 1, result 2, ..., result N]

Return type:

list

Raises:

ImportError – If xvfbwrapper is not installed.

features

Features to calculate from the model result.

Parameters:new_features ({None, Features or Features subclass instance, list of feature functions}) – Features to calculate from the model result. If None, no features are calculated. If list of feature functions, all will be calculated.
Returns:features – Features to calculate from the model result. If None, no features are calculated.
Return type:{None, Features object}
is_regular(results, feature)[source]

Test if feature in results is regular or not, meaning it has a varying number of values for each evaluation. Ignores results that contains numpy.nan.

Parameters:
  • results (list) – A list where each element is a result dictionary for each set of model evaluations. An example:

    result = {self.model.name: {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                                "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
              "feature1d": {"values": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                            "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
              "feature0d": {"values": 1,
                            "time": np.nan},
              "feature2d": {"values": array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                             [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]),
                            "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
              "feature_adaptive": {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                                   "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                                   "interpolation": scipy interpolation object},
              "feature_invalid": {"values": np.nan,
                                  "time": np.nan}}
    
    results = [result 1, result 2, ..., result N]
    
  • feature (str) – Name of a feature or the model.

Returns:

True if the feature is regular or False if the feature is irregular.

Return type:

bool

model

Model to perform uncertainty quantification on. For requirements see Model.run.

Parameters:new_model ({None, Model or Model subclass instance, model function}) – Model to perform uncertainty quantification on.
Returns:model – Model to perform uncertainty quantification on.
Return type:Model or Model subclass instance
parameters

Model parameters.

Parameters:new_parameters ({None, Parameters instance, list of Parameter instances, list [[name, value, distribution], …]}) – Either None, a Parameters instance or a list of the parameters that should be created. The two lists are similar to the arguments sent to Parameters. Default is None.
Returns:parameters – Parameters of the model. If None, no parameters have been set.
Return type:{None, Parameters}
regularize_nan_results(results)[source]

Regularize arrays with that only contain numpy.nan values.

Make each result for each feature have the same the same shape, if they only contain numpy.nan values.

Parameters:

results (list) – A list where each element is a result dictionary for each set of model evaluations. An example:

result = {self.model.name: {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                            "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature1d": {"values": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                        "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature0d": {"values": 1,
                        "time": np.nan},
          "feature2d": {"values": array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                         [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]),
                        "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature_adaptive": {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                               "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                               "interpolation": scipy interpolation object},
          "feature_invalid": {"values": np.nan,
                              "time": np.nan}}

results = [result 1, result 2, ..., result N]
Returns:

results – A list with where the only nan results have been regularized. On the form:

result = {self.model.name: {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                            "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature1d": {"values": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                        "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature0d": {"values": 1,
                        "time": np.nan},
          "feature2d": {"values": array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                         [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]),
                        "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature_adaptive": {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                               "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                               "interpolation": scipy interpolation object},
          "feature_invalid": {"values": np.nan,
                              "time": np.nan}}

results = [result 1, result 2, ..., result N]

Return type:

list

results_to_data(results)[source]

Store results in a Data object.

Stores the time and (interpolated) results for the model and each feature in a Data object. Performs the interpolation calculated in Parallel, if the result is irregular.

Parameters:

results (list) – A list where each element is a result dictionary for each set of model evaluations. An example:

result = {self.model.name: {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                            "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature1d": {"values": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                        "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature0d": {"values": 1,
                        "time": np.nan},
          "feature2d": {"values": array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                                         [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]),
                        "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])},
          "feature_adaptive": {"values": array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
                               "time": array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
                               "interpolation": scipy interpolation object},
          "feature_invalid": {"values": np.nan,
                              "time": np.nan}}

results = [result 1, result 2, ..., result N]
Returns:

data – A Data object with time and (interpolated) results for the model and each feature.

Return type:

Data object

Notes

Sets the following in data, if applicable: 1. data["model/features"].evaluations, which contains all values 2. data["model/features"].time 3. data["model/features"].labels 4. data.model_name

run(nodes, uncertain_parameters)[source]

Evaluate the the model and calculate the features for the nodes (values) for the uncertain parameters. The results are interpolated as necessary.

Parameters:
  • nodes (array) – A series of different set of parameters. The model and each feature is evaluated for each set of parameters in the series.
  • uncertain_parameters (list) – A list of names of the uncertain parameters.
Returns:

data – A Data object with time and (interpolated) results for the model and each feature.

Return type:

Data object