def __init__(self,
                 exp_prefix,
                 est_params,
                 sim_params,
                 observations,
                 keys_of_interest,
                 n_mc_samples=10**7,
                 n_x_cond=5,
                 n_seeds=5,
                 use_gpu=True,
                 tail_measures=True):

        assert est_params and exp_prefix and sim_params and keys_of_interest
        assert observations.all()

        # every simulator configuration will be run multiple times with different randomness seeds
        sim_params = _add_seeds_to_sim_params(n_seeds, sim_params)

        self.observations = observations
        self.n_mc_samples = n_mc_samples
        self.n_x_cond = n_x_cond
        self.keys_of_interest = keys_of_interest
        self.exp_prefix = exp_prefix
        self.use_gpu = use_gpu
        self.tail_measures = tail_measures

        logger.configure(log_directory=config.DATA_DIR,
                         prefix=exp_prefix,
                         color='green')
        ''' ---------- Either load or generate the configs ----------'''
        config_pkl_path = os.path.join(logger.log_directory, logger.prefix,
                                       EXP_CONFIG_FILE)

        if os.path.isfile(config_pkl_path):
            logger.log("{:<70s} {:<30s}".format(
                "Loading experiment previous configs from file: ",
                config_pkl_path))
            self.configs = logger.load_pkl(EXP_CONFIG_FILE)
        else:
            logger.log("{:<70s} {:<30s}".format(
                "Generating and storing experiment configs under: ",
                config_pkl_path))
            self.configs = self._generate_configuration_variants(
                est_params, sim_params)
            logger.dump_pkl(data=self.configs, path=EXP_CONFIG_FILE)
        ''' ---------- Either load already existing results or start a new result collection ---------- '''
        results_pkl_path = os.path.join(logger.log_directory, logger.prefix,
                                        RESULTS_FILE)
        if os.path.isfile(results_pkl_path):
            logger.log_line("{:<70s} {:<30s}".format("Continue with: ",
                                                     results_pkl_path))
            self.gof_single_res_collection = dict(
                logger.load_pkl_log(RESULTS_FILE))

        else:  # start from scratch
            self.gof_single_res_collection = {}

        self.gof_results = GoodnessOfFitResults(self.gof_single_res_collection)
예제 #2
0
def test_load_pkl_log(setup):
    import numpy
    d1 = numpy.random.randn(20, 10)
    logger.log_pkl(d1, 'test_file.pkl')
    sleep(1.0)
    d2 = numpy.random.randn(20, 10)
    logger.log_pkl(d2, 'test_file.pkl')
    sleep(1.0)

    data = logger.load_pkl_log('test_file.pkl')
    assert len(data) == 2, "data should contain two arrays"
    assert numpy.array_equal(data[0], d1), "first should be the same as d1"
    assert numpy.array_equal(data[1], d2), "first should be the same as d2"
예제 #3
0
def test_log_data(setup):
    import numpy
    d1 = numpy.random.randn(20, 10)
    logger.log_pkl(d1, 'test_file.pkl')
    sleep(1.0)
    d2 = numpy.random.randn(20, 10)
    logger.dump_pkl(d2, 'test_file.pkl')
    sleep(1.0)

    data = logger.load_pkl_log('test_file.pkl')
    assert len(
        data
    ) == 1, "data should contain only one array because we overwrote it."
    assert numpy.array_equal(data[0], d2), "first should be the same as d2"
예제 #4
0
def get_density_plots(estimators_list,
                      simulators_dict,
                      path_to_results,
                      exp_prefix="question1_noise_reg_x",
                      task_ids=None):
    """
  This function allows to compare plots from estimators and simulators (i.e. fitted and true densities). Two modes are currently available:
  1) by specifying estimators and simulator, the function picks one result pair randomly that matches the given simulator/estimator
  selection
  2) by specifying the task_ids as list, it is possible to pick specific plots to compare

  Args:
    estimators: a list containing strings of estimators to be evaluated, e.g. ['KernelMixtureNetwork', 'MixtureDensityNetwork']
    simulators: a dict containing specifications of a simulator under which the estimators shall be compared, e.g.
      {'heteroscedastic': True, 'random_seed': 20, 'std': 1, 'simulator': 'EconDensity'}
    path_to_results: absolute path to where the dumped model files are stored
    exp_prefix: specifies the task question

  Returns:
    A list of figures for fitted and true densities.
  """

    if task_ids is not None:
        assert type(task_ids) == list
        assert len(task_ids) == len(estimators_list)

    RESULTS_FILE = 'results.pkl'
    logger.configure(path_to_results, exp_prefix)

    results_from_pkl_file = dict(logger.load_pkl_log(RESULTS_FILE))
    gof_result = GoodnessOfFitResults(
        single_results_dict=results_from_pkl_file)
    results_df = gof_result.generate_results_dataframe(
        base_experiment.KEYS_OF_INTEREST)
    """ load model's estimators """
    if task_ids is None:

        models_of_interest = {
            k: v
            for k, v in gof_result.single_results_dict.items()
            if v.probabilistic_model_params == simulators_dict and v.ndim_x +
            v.ndim_y == 2
        }

        models = [
            ConfigRunner.load_dumped_estimator(
                take_of_type(1, estimator_str, models_of_interest))
            for estimator_str in estimators_list
        ]
    else:
        models = [
            ConfigRunner.load_dumped_estimators(gof_result, task_id=task_ids)
        ]
    """ load model's simulators """
    # todo: implement when simulator dumps exist

    figs = []

    for model in models:
        graph = model.estimator.sess.graph
        sess = tf.Session(graph=graph)

        with sess:
            sess.run(tf.global_variables_initializer())
            model.estimator.sess = sess
            """ fitted density figures"""
            plt.suptitle(model.estimator.name)
            fig_fitted = model.estimator.plot3d()
            figs.append(fig_fitted)
            """ true density figures """
            # todo: use newly dumped simulators

            sess.close()

    return figs
            'y_noise_std': [0.1],
        },
    }

    simulators_params = {'LinearStudentT': {'ndim_x': [10]}}

    observations = 100 * np.logspace(2, 6, num=8, base=2.0, dtype=np.int32)

    return estimator_params, simulators_params, observations


if __name__ == '__main__':
    estimator_params, simulators_params, observations = question4()
    load = base_experiment.launch_experiment(estimator_params,
                                             simulators_params,
                                             observations,
                                             EXP_PREFIX,
                                             n_mc_samples=N_MC_SAMPLES,
                                             tail_measures=False)

    if load:
        logger.configure(config.DATA_DIR, EXP_PREFIX)

        results_from_pkl_file = dict(logger.load_pkl_log(RESULTS_FILE))
        gof_result = GoodnessOfFitResults(
            single_results_dict=results_from_pkl_file)
        results_df = gof_result.generate_results_dataframe(
            base_experiment.KEYS_OF_INTEREST)

        gof_result = ConfigRunner.load_dumped_estimators(gof_result)
예제 #6
0
def test_load_module(setup, test_module):
    result, = logger.load_pkl_log(f"modules/{0:04d}_Test.pkl")
    import numpy as np
    assert (result['var_1'] == np.ones(
        [100, 2])).all(), "should be the same as test data"