def test_empty_collate(tmpdir):

    # Set up a fresh campaign called "cannon"
    my_campaign = uq.Campaign(name='cannon', work_dir=tmpdir)

    # Define parameter space for the cannonsim app
    params = {
        "angle": {
            "type": "float",
            "min": 0.0,
            "max": 6.28,
            "default": 0.79
        },
        "air_resistance": {
            "type": "float",
            "min": 0.0,
            "max": 1.0,
            "default": 0.2
        },
        "height": {
            "type": "float",
            "min": 0.0,
            "max": 1000.0,
            "default": 1.0
        },
        "time_step": {
            "type": "float",
            "min": 0.0001,
            "max": 1.0,
            "default": 0.01
        },
        "gravity": {
            "type": "float",
            "min": 0.0,
            "max": 1000.0,
            "default": 9.8
        },
        "mass": {
            "type": "float",
            "min": 0.0001,
            "max": 1000.0,
            "default": 1.0
        },
        "velocity": {
            "type": "float",
            "min": 0.0,
            "max": 1000.0,
            "default": 10.0
        }
    }

    # Create an encoder, decoder and collater for the cannonsim app
    encoder = uq.encoders.GenericEncoder(
        template_fname='tests/cannonsim/test_input/cannonsim.template',
        delimiter='#',
        target_filename='in.cannon')
    decoder = uq.decoders.SimpleCSV(
        target_filename='output.csv',
        output_columns=['Dist', 'lastvx', 'lastvy'],
        header=0)
    collater = uq.collate.AggregateSamples(average=False)

    # Add the cannonsim app
    my_campaign.add_app(name="cannonsim",
                        params=params,
                        encoder=encoder,
                        decoder=decoder,
                        collater=collater)

    # Set the active app to be cannonsim (this is redundant when only one app
    # has been added)
    my_campaign.set_app("cannonsim")

    # Set up samplers
    vary = {
        "gravity": cp.Uniform(9.8, 1.0),
        "mass": cp.Uniform(2.0, 10.0),
    }
    sampler = uq.sampling.RandomSampler(vary=vary, max_num=5)

    # Set the campaign to use this sampler
    my_campaign.set_sampler(sampler)

    # Test reloading
    my_campaign.save_state(tmpdir + "test_multisampler.json")
    reloaded_campaign = uq.Campaign(state_file=tmpdir +
                                    "test_multisampler.json",
                                    work_dir=tmpdir)

    # Draw all samples
    my_campaign.draw_samples()

    # Encode
    my_campaign.populate_runs_dir()

    # Do an early collation, before anything has been executed. This means the collation element
    # may attempt to add an empty dataframe to the database (which will cause issues upon subsequent
    # collates due to an empty set of columns (Issue 163).
    my_campaign.collate()

    # Execute
    my_campaign.apply_for_each_run_dir(
        uq.actions.ExecuteLocal(
            "tests/cannonsim/bin/cannonsim in.cannon output.csv"))

    # Attempt to collate() again, now that the runs have been executed. If Issue 163 is not
    # fixed then an error will occur here.
    my_campaign.collate()
    print("data:", my_campaign.get_collation_result())

    pprint(my_campaign._log)
Beispiel #2
0
        "margin_error": 0.25,
    },
    "Ti_boundary": {
        "type": "float",
        "distribution": "Normal",
           "margin_error": 0.25,
      }
}

# For the output: quantities of intersts
output_columns = ["Te", "Ti"]

# Initialize Campaign object
print('>>> Initialize Campaign object')
campaign_name = "uq_ets"
my_campaign = uq.Campaign(name=campaign_name, work_dir=tmp_dir)

# Create new directory for inputs (to be ended with /)
campaign_dir = my_campaign.campaign_dir
common_dir = campaign_dir +"/common/"
os.system("mkdir " + common_dir)

# Copy input CPO files (cf test_ets.f90)
os.system("cp " + cpo_dir + "/*.cpo " + common_dir)

# Copy XML and XSD files
os.system("cp " + xml_dir + "/ets.xml " + common_dir)
os.system("cp " + xml_dir + "/ets.xsd " + common_dir)

# Create the encoder and get the app parameters
print('>>> Create the encoder')
Beispiel #3
0
ets_run = os.path.abspath("../bin/" + SYS + "/ets_run ")

#uncertain_params = ["D1", "D2", "D3", "D4"]#, "Te_boundary", "Ti_boundary"]
uncertain_params = ["Te_boundary", "Ti_boundary"]

# Define parameter space
print('Define parameter space')
params = {k: {"type": "float", "default": "0."} for k in uncertain_params}
params.update({"out_file": {"type": "string", "default": "output.csv"}})

output_filename = params["out_file"]["default"]
output_columns = ["Te", 'Ti']

# Initialize Campaign object
print('Initialize Campaign object')
my_campaign = uq.Campaign(name='uq_ets', work_dir=tmp_dir)

# Copy XML files needed in the ETS wrappers
campaign_dir = my_campaign.campaign_dir
os.system("mkdir " + campaign_dir + "/workflows")
os.system("cp ../../workflows/ets.xml " + campaign_dir + "/workflows")
os.system("cp ../../workflows/ets.xsd " + campaign_dir + "/workflows")

# Copy CPO files in common directory
common_dir = campaign_dir + "/common/"
os.system("mkdir " + common_dir)
os.system("cp " + cpo_dir + "/*.cpo " + common_dir)

# Create an encoder and decoder
encoder = uq.encoders.GenericEncoder(template_fname='inputs/ets.template',
                                     delimiter='#',
Beispiel #4
0
def flee_analyse_PCE(config, ** args):
    '''
    ============================================================================

        fab <remote_machine> flee_analyse_PCE:<conflict_name>

    example:

        fab eagle_vecma flee_analyse_PCE:mali

    ============================================================================
    '''

    load_campaign_files()

    # reload Campaign
    campaign = uq.Campaign(state_file=os.path.join(work_dir_PCESampler,
                                                   "campaign_state.json"),
                           work_dir=work_dir_PCESampler
                           )
    print('========================================================')
    print('Reloaded campaign', campaign._campaign_dir)
    print('========================================================')

    sampler = campaign.get_active_sampler()
    campaign.set_sampler(sampler)

    # fetch only the required folder from remote machine
    with_config(config)

    # fetch results from remote machine
    job_label = campaign._campaign_dir
    job_folder_name = template(env.job_name_template + "_{}".format(job_label))
    print("fetching results from remote machine ...")
    with hide('output', 'running', 'warnings'), settings(warn_only=True):
        fetch_results(regex=job_folder_name)
    print("Done\n")

    # copy only output folder into local campaign_dir :)
    src = os.path.join(env.local_results, job_folder_name, 'RUNS')
    des = os.path.join(work_dir_PCESampler, campaign._campaign_dir, 'SWEEP')

    print("Syncing output_dir ...")
    with hide('output', 'running', 'warnings'), settings(warn_only=True):
        local(
            "rsync -av -m -v \
            --include='/*/' \
            --include='out.csv'  \
            --exclude='*' \
            {}/  {} ".format(src, des)
        )
    print("Done\n")

    campaign.collate()

    # Return dataframe containing all collated results
    collation_result = campaign.get_collation_result()

    collation_result.to_csv(os.path.join(work_dir_PCESampler,
                                         'collation_result.csv'
                                         ),
                            index=False
                            )

    print(collation_result)

    # Post-processing analysis
    analysis = uq.analysis.PCEAnalysis(
        sampler=campaign._active_sampler,
        qoi_cols=output_columns
    )
    campaign.apply_analysis(analysis)
    results = campaign.get_last_analysis()

    # --------------------------------------------------------------------------
    #                   Plotting
    # --------------------------------------------------------------------------
    analysis_log_file = os.path.join(work_dir_PCESampler, 'analysis_log.txt')

    for output_column in output_columns:
        fig = plt.figure()
        ax = fig.add_subplot(111,
                             xlabel="days", ylabel=output_column)
        mean = results["statistical_moments"][output_column]["mean"]
        std = results["statistical_moments"][output_column]["std"]
        ax.plot(mean)
        ax.plot(mean + std, '--r')
        ax.plot(mean - std, '--r')
        #ax.title.set_text('statistical_moments for {}'.format(output_column))

        plt.tight_layout()
        plt.savefig(os.path.join(
            work_dir_PCESampler, 'plot_statistical_moments_{}'.format(output_column)),
            dpi=400)

        # analysis.plot_grid()
        '''
        plot_grid(analysis, ['max_move_speed',
                             'max_walk_speed'],
                  os.path.join(work_dir_PCESampler, 'analysis_plot_grid')
                  )
        '''

        original = sys.stdout
        with open(analysis_log_file, 'w') as file:
            sys.stdout = custom_redirection(sys.stdout, file)
            print(config)
            # print(analysis.samples)
            print('mean Total error = %f' % (mean))
            print('std Total error = %f' % (std))
            print('=================================================')

            print('percentiles:')
            print(results['percentiles'][output_column])
            print('sobols_first:')
            print(results['sobols_first'][output_column])
            print('=================================================')

        sys.stdout = original
Beispiel #5
0
def test_anisotropic_order(tmpdir):

    # Set up a fresh campaign called "sc"
    my_campaign = uq.Campaign(name='sc', work_dir=tmpdir, db_location='sqlite:///')

    # Define parameter space
    params = {
        "Pe": {
            "type": "float",
            "min": 1.0,
            "max": 2000.0,
            "default": 100.0},
        "f": {
            "type": "float",
            "min": 0.0,
            "max": 10.0,
            "default": 1.0},
        "out_file": {
            "type": "string",
            "default": "output.csv"}}

    output_filename = params["out_file"]["default"]
    output_columns = ["u"]

    # Create an encoder, decoder and collation element
    encoder = uq.encoders.GenericEncoder(
        template_fname='tests/sc/sc.template',
        delimiter='$',
        target_filename='ade_in.json')
    decoder = uq.decoders.SimpleCSV(target_filename=output_filename,
                                    output_columns=output_columns,
                                    header=0)
    collater = uq.collate.AggregateSamples()

    # Add the SC app (automatically set as current app)
    my_campaign.add_app(name="sc",
                        params=params,
                        encoder=encoder,
                        decoder=decoder,
                        collater=collater)

    # Create the sampler
    vary = {
        "Pe": cp.Uniform(100.0, 200.0),
        "f": cp.Uniform(0.95, 1.05)
    }

    # different orders for the 2 parameters
    my_sampler = uq.sampling.SCSampler(vary=vary, polynomial_order=[2, 5],
                                       quadrature_rule="G")

    # Associate the sampler with the campaign
    my_campaign.set_sampler(my_sampler)

    # Will draw all (of the finite set of samples)
    my_campaign.draw_samples()
    my_campaign.populate_runs_dir()

    #   Use this instead to run the samples using EasyVVUQ on the localhost
    my_campaign.apply_for_each_run_dir(uq.actions.ExecuteLocal(
        "tests/sc/sc_model.py ade_in.json"))

    my_campaign.collate()

    # Post-processing analysis
    analysis = uq.analysis.SCAnalysis(sampler=my_sampler, qoi_cols=output_columns)

    my_campaign.apply_analysis(analysis)

    #import pickle
    #pickle.dump(analysis, open('analysis.p', 'wb'))

    results = my_campaign.get_last_analysis()

    return results, my_sampler, analysis
Beispiel #6
0
from shutil import rmtree


work_dir = os.path.dirname(os.path.abspath(__file__))
campaign_work_dir = os.path.join(work_dir, "easyvvuq_gp")
# clear the target campaign dir
if os.path.exists(campaign_work_dir):
    rmtree(campaign_work_dir)
os.makedirs(campaign_work_dir)

# Set up a fresh campaign called "coffee_pce"
db_location = "sqlite:///" + campaign_work_dir + "/campaign.db"

my_campaign = uq.Campaign(
    name="coffee_pce",
    db_location=db_location,
    work_dir=campaign_work_dir
)

# Define parameter space
params = {
    "temp_init": {"type": "float", "min": 0.0, "max": 100.0, "default": 95.0},
    "kappa": {"type": "float", "min": 0.0, "max": 0.1, "default": 0.025},
    "t_env": {"type": "float", "min": 0.0, "max": 40.0, "default": 15.0},
    "out_file": {"type": "string", "default": "output.csv"}
}

# Create an encoder, decoder and collater for PCE test app
encoder = uq.encoders.GenericEncoder(
    template_fname="cooling.template",
    delimiter="$",
"""
@author: Federica Gugole

__license__ = "LGPL"
"""

import chaospy as cp
import easyvvuq as uq
import os, subprocess

#home dir of this file
HOME = os.path.abspath(os.path.dirname(__file__))

# Set up a fresh campaign
workdir = '/export/scratch2/home/federica/'
campaign = uq.Campaign(name='virsim_PO_bio_', work_dir=workdir)

# Define parameter space
params = {
    "seed": {
        "type": "integer",  # set to "float" when using MCSampler
        "min": 0,
        "max": 2**31,
        "default": 12345
    },
    "trace_prob_E": {
        "type": "float",
        "min": 0.0,
        "max": 1.0,
        "default": .6
    },
Beispiel #8
0
def flee_analyse_vvp_LoR(config):
    """
    flee_analyse_vvp_LoR will analysis the output of each vvp ensemble series

    usage example:
        fab localhost flee_analyse_vvp_LoR:mali
    """
    update_environment()

    #############################################
    # load flee vvp configuration from yml file #
    #############################################
    flee_VVP_config_file = os.path.join(
        get_plugin_path("FabFlee"),
        "VVP",
        "flee_VVP_config.yml"
    )
    VVP_campaign_config = load_VVP_campaign_config(flee_VVP_config_file)

    polynomial_order_range = range(
        VVP_campaign_config["polynomial_order_range"]["start"],
        VVP_campaign_config["polynomial_order_range"]["end"],
        VVP_campaign_config["polynomial_order_range"]["step"]
    )
    sampler_name = VVP_campaign_config["sampler_name"]

    ###########################################
    # set a default dir to save results sobol #
    ###########################################
    sobol_work_dir = os.path.join(
        get_plugin_path("FabFlee"),
        "VVP",
        "flee_vvp_LoR_{}_{}".format(sampler_name, config),
        "sobol"
    )

    ###################################
    # delete sobol_work_dir is exists #
    ###################################
    if os.path.exists(sobol_work_dir):
        rmtree(sobol_work_dir)
    os.makedirs(sobol_work_dir)

    for polynomial_order in polynomial_order_range:
        campaign_name = "flee_vvp_LoR_{}_po{}_{}_".format(
            sampler_name,
            polynomial_order,
            config
        )

        campaign_work_dir = os.path.join(
            get_plugin_path("FabFlee"),
            "VVP",
            "flee_vvp_LoR_{}_{}".format(sampler_name, config),
            "campaign_po{}".format(polynomial_order)
        )
        load_campaign_files(campaign_work_dir)

        ###################
        # reload Campaign #
        ###################
        db_location = "sqlite:///" + campaign_work_dir + "/campaign.db"
        campaign = uq.Campaign(name=campaign_name, db_location=db_location)
        print("===========================================")
        print("Reloaded campaign {}".format(campaign_name))
        print("===========================================")

        sampler = campaign.get_active_sampler()
        campaign.set_sampler(sampler, update=True)

        ####################################################
        # fetch results from remote machine                #
        # here, we ONLY fetch the required results folders #
        ####################################################
        env.job_desc = "_vvp_LoR_{}_po{}".format(
            sampler_name,
            polynomial_order
        )
        with_config(config)

        job_folder_name = template(env.job_name_template)
        print("fetching results from remote machine ...")
        with hide("output", "running", "warnings"), settings(warn_only=True):
            fetch_results(regex=job_folder_name)
        print("Done\n")

        #####################################################
        # copy ONLY the required output files for analyse,  #
        # i.e., EasyVVUQ.decoders.target_filename           #
        #####################################################
        output_filename = VVP_campaign_config["params"]["out_file"]["default"]
        src = os.path.join(env.local_results, job_folder_name, "RUNS")
        des = campaign.campaign_db.runs_dir()
        print("Syncing output_dir ...")
        # with hide('output', 'running', 'warnings'), settings(warn_only=True):
        local(
            "rsync -pthrz "
            "--include='/*/' "
            "--include='{}' "
            "--exclude='*' "
            "{}/  {} ".format(output_filename, src, des)
        )
        print("Done ...\n")

        #################################
        # Create a decoder #
        #################################
        output_column = VVP_campaign_config["decoder_output_column"]
        decoder = uq.decoders.SimpleCSV(
            target_filename=output_filename,
            output_columns=[output_column]
        )

        #####################
        # execute collate() #
        #####################
        actions = uq.actions.Actions(
            uq.actions.Decode(decoder)
        )
        campaign.replace_actions(campaign_name, actions)
        campaign.execute().collate()
        collation_result = campaign.get_collation_result()

        ##################################################
        # save dataframe containing all collated results #
        ##################################################
        collation_result.to_csv(
            os.path.join(campaign_work_dir, "collation_result.csv"),
            index=False
        )
        collation_result.to_pickle(
            os.path.join(campaign_work_dir, "collation_result.pickle")
        )

        ###################################
        #    Post-processing analysis     #
        ###################################

        if sampler_name == "SCSampler":
            analysis = uq.analysis.SCAnalysis(
                sampler=campaign._active_sampler,
                qoi_cols=[output_column]
            )
        elif sampler_name == "PCESampler":
            analysis = uq.analysis.PCEAnalysis(
                sampler=campaign._active_sampler,
                qoi_cols=[output_column]
            )

        campaign.apply_analysis(analysis)
        results = campaign.get_last_analysis()

        ###################
        #    Plotting     #
        ###################
        fig_desc = "polynomial_order = {}, num_runs = {}, sampler = {}".format(
            polynomial_order,
            campaign.campaign_db.get_num_runs(),
            sampler_name
        )
        props = dict(boxstyle="round", facecolor="wheat", alpha=0.5)

        ########################
        #    Plot raw data     #
        ########################
        output_files = glob.glob(
            os.path.join(campaign_work_dir + "/**/%s" % (output_filename)),
            recursive=True
        )

        fig, ax = plt.subplots()
        ax.set_xlabel("days")
        ax.set_ylabel(output_column)
        fig.suptitle(
            "RAW data : column {}\n".format(output_column),
            fontsize=10, fontweight="bold"
        )
        ax.set_title(
            fig_desc, fontsize=8, loc="center",
            fontweight="bold", bbox=props
        )
        for output_file in output_files:
            total_errors = pd.read_csv(output_file)[
                output_column].values.tolist()
            ax.plot(total_errors)

        plot_file_name = "raw[{}]".format(output_column)
        plt.savefig(os.path.join(campaign_work_dir, plot_file_name),
                    dpi=400)

        ###################################
        #    Plot statistical_moments     #
        ###################################
        fig, ax = plt.subplots()
        ax.set_xlabel("days")
        ax.set_ylabel("velocity {}".format(output_column))
        fig.suptitle(
            "code mean +/- standard deviation\n",
            fontsize=10, fontweight="bold"
        )
        ax.set_title(
            fig_desc, fontsize=8, loc="center",
            fontweight="bold", bbox=props
        )

        mean = results.describe(output_column, "mean")
        std = results.describe(output_column, "std")
        X = range(len(mean))
        ax.plot(X, mean, "b-", label="mean")
        ax.plot(X, mean - std, "--r", label="+1 std-dev")
        ax.plot(X, mean + std, "--r")
        ax.fill_between(X, mean - std, mean + std, color="r", alpha=0.2)
        # plt.tight_layout()
        plt.legend(loc="best")
        plot_file_name = "plot_statistical_moments[{}]".format(output_column)
        plt.savefig(os.path.join(campaign_work_dir, plot_file_name),
                    dpi=400)

        ###################################
        #        Plot sobols_first        #
        ###################################
        fig, ax = plt.subplots()
        ax.set_xlabel("days")
        ax.set_ylabel("Sobol indices")
        fig.suptitle(
            "First order Sobol index [output column = {}]\n".format(
                output_column),
            fontsize=10, fontweight="bold"
        )
        ax.set_title(
            fig_desc, fontsize=8, loc='center',
            fontweight='bold', bbox=props
        )

        sobols_first = results.raw_data["sobols_first"][output_column]
        param_i = 0
        for v in sobols_first:
            y = sobols_first[v].ravel()
            important = False
            if y[-1] != 0:
                ax.plot(y, label=v)
            else:
                print("%s ignored for plotting" % (v))

            param_i = param_i + 1

        plt.legend(loc='best')
        # plt.tight_layout()
        plot_file_name = 'plot_sobols_first[%s]' % (output_column)
        plt.savefig(os.path.join(campaign_work_dir, plot_file_name),
                    dpi=400)

        ###############################################################
        # yml_results contains all campaign info and analysis results #
        # it will be saved in sobols.yml file                         #
        ###############################################################
        S = ruamel.yaml.scalarstring.DoubleQuotedScalarString
        yml_results = ruamel.yaml.comments.CommentedMap()
        yml_results.update({'campaign_info': {}})
        yml_results['campaign_info'].update({
            'name': S(campaign._active_app_name),
            'work_dir': S(campaign.work_dir),
            'num_runs': campaign.campaign_db.get_num_runs(),
            'output_column': S(output_column),
            'polynomial_order': polynomial_order,
            'sampler': S(VVP_campaign_config['sampler_name']),
            'distribution_type': S(VVP_campaign_config['distribution_type']),
            'sparse': S(VVP_campaign_config['sparse']),
            'growth': S(VVP_campaign_config['growth'])
        })
        if sampler_name == 'SCSampler':
            yml_results['campaign_info'].update({
                'quadrature_rule': S(VVP_campaign_config['quadrature_rule']),
                'midpoint_level1': S(VVP_campaign_config['midpoint_level1']),
                'dimension_adaptive': S(VVP_campaign_config
                                        ['dimension_adaptive'])
            })
        elif sampler_name == 'PCESampler':
            yml_results['campaign_info'].update({
                'rule': S(VVP_campaign_config['quadrature_rule']),
            })

        ROUND_NDIGITS = 4
        for param in VVP_campaign_config['selected_vary_parameters']:
            # I used CommentedMap for adding comments
            yml_results[param] = ruamel.yaml.comments.CommentedMap()
            # yml_results.update({param: {}})
            yml_results[param].update({
                "sobols_first_mean":
                    round(float(np.mean(sobols_first[param].ravel())),
                          ROUND_NDIGITS),
                "sobols_first_gmean":
                    round(float(gmean(sobols_first[param].ravel())),
                          ROUND_NDIGITS),
                "sobols_first":
                    np.around(sobols_first[param].ravel(),
                              ROUND_NDIGITS).tolist()
            })
            # add comments to yml
            '''
            yml_results[param].yaml_add_eol_comment(
                "geometric mean, i.e.,  n-th root of (x1 * x2 * … * xn)",
                "sobols_first_gmean")
            yml_results[param].yaml_add_eol_comment(
                "arithmetic mean i.e., (x1 + x2 + … + xn)",
                "sobols_first_mean")
            '''
            yml_results[param].yaml_set_comment_before_after_key(
                "sobols_first_gmean",
                before="geometric mean, i.e., n-th root of (x1 * x2 * … * xn)",
                indent=2)
            yml_results[param].yaml_set_comment_before_after_key(
                "sobols_first_mean",
                before="arithmetic mean i.e., (x1 + x2 + … + xn)/n",
                indent=2)

        yaml = ruamel.yaml.YAML()
        yaml.preserve_quotes = True
        yaml.default_flow_style = None
        # to Prevent long lines getting wrapped in ruamel.yaml
        # we set the yaml.width to a big enough value to prevent line-wrap
        yaml.width = sys.maxsize

        res_file_name = os.path.join(campaign_work_dir, 'sobols.yml')
        print(res_file_name)
        with open(res_file_name, 'w') as outfile:
            yaml.dump(yml_results, outfile)
            '''
            yaml.dump(yml_results, outfile,
                      default_flow_style=None, width=1000)
            '''

        ########################################
        # copy sobols.yml file to sobol folder #
        ########################################
        print("copy sobols.yml file to sobol folder ...")
        # here instead of mkdirs and copy, I used rsync
        local(
            "rsync -pthrz "
            "--include='/*/' "
            "--include='sobols.yml' "
            "--include='*.png' "
            "--exclude='*' "
            "{}  {} ".format(campaign_work_dir, sobol_work_dir)
        )
        print("Done ...\n")

    #####################################################
    # Check the convergence of the SC Sobols indices    #
    # with polynomial refinement                        #
    #####################################################
    ensemble_vvp_LoR(
        results_dirs_PATH=sobol_work_dir,
        load_QoIs_function=load_QoIs_function,
        aggregation_function=plot_convergence,
        plot_file_path=sobol_work_dir
    )
Beispiel #9
0
def init_VVP_campaign(campaign_name, campaign_config,
                      polynomial_order, campaign_work_dir):

    ######################################
    # delete campaign_work_dir is exists #
    ######################################
    if os.path.exists(campaign_work_dir):
        rmtree(campaign_work_dir)
    os.makedirs(campaign_work_dir)

    #####################
    # Create an encoder #
    #####################
    encoder = uq.encoders.GenericEncoder(
        template_fname=os.path.join(get_plugin_path("FabFlee"),
                                    "templates",
                                    campaign_config["encoder_template_fname"]
                                    ),
        delimiter=campaign_config["encoder_delimiter"],
        target_filename=campaign_config["encoder_target_filename"]
    )

    ###########################
    # Set up a fresh campaign #
    ###########################
    db_location = "sqlite:///" + campaign_work_dir + "/campaign.db"

    actions = uq.actions.Actions(
        uq.actions.CreateRunDirectory(root=campaign_work_dir, flatten=True),
        uq.actions.Encode(encoder),
    )

    campaign = uq.Campaign(
        name=campaign_name,
        db_location=db_location,
        work_dir=campaign_work_dir
    )

    ################################
    # Add the flee-vvp-Sampler app #
    ################################
    campaign.add_app(
        name=campaign_name,
        params=campaign_config["params"],
        actions=actions
    )

    ######################
    # parameters to vary #
    ######################
    vary = {}
    for param in campaign_config["selected_vary_parameters"]:
        lower_value = campaign_config[
            "vary_parameters_range"][param]["range"][0]
        upper_value = campaign_config[
            "vary_parameters_range"][param]["range"][1]
        if campaign_config["distribution_type"] == "DiscreteUniform":
            vary.update({param: cp.DiscreteUniform(lower_value, upper_value)})
        elif campaign_config["distribution_type"] == "Uniform":
            vary.update({param: cp.Uniform(lower_value, upper_value)})

    ####################
    # create Sampler #
    ####################
    sampler_name = campaign_config["sampler_name"]
    if sampler_name == "SCSampler":
        sampler = uq.sampling.SCSampler(
            vary=vary,
            polynomial_order=polynomial_order,
            quadrature_rule=campaign_config["quadrature_rule"],
            growth=campaign_config["growth"],
            sparse=campaign_config["sparse"],
            midpoint_level1=campaign_config["midpoint_level1"],
            dimension_adaptive=campaign_config["dimension_adaptive"]
        )
    elif sampler_name == "PCESampler":
        sampler = uq.sampling.PCESampler(
            vary=vary,
            polynomial_order=polynomial_order,
            rule=campaign_config["quadrature_rule"],
            sparse=campaign_config["sparse"],
            growth=campaign_config["growth"]
        )
    # TODO:	add other sampler here

    ###########################################
    # Associate the sampler with the campaign #
    ###########################################
    campaign.set_sampler(sampler)

    #########################################
    # draw all of the finite set of samples #
    #########################################
    campaign.execute().collate()

    #########################################
    # extract generated runs id by campaign #
    #########################################
    runs_dir = []
    for _, run_info in campaign.campaign_db.runs(
            status=uq.constants.Status.NEW
    ):
        runs_dir.append(run_info["run_name"])

    campaign_dir = campaign.campaign_db.campaign_dir()

    ######################################################
    # backup campaign files, i.e, *.db, *.json, *.pickle #
    ######################################################
    backup_campaign_files(campaign.work_dir)

    print("=" * 50)
    print("With user's specified parameters for {}".format(sampler_name))
    print("campaign name : {}".format(campaign_name))
    print("number of generated runs : {}".format(len(runs_dir)))
    print("campaign dir : {}".format(campaign_work_dir))
    print("=" * 50)

    return runs_dir, campaign_dir
Beispiel #10
0
def test_cannonsim(tmpdir):
    # Define parameter space for the cannonsim app
    params = {
        "angle": {
            "type": "float",
            "min": 0.0,
            "max": 6.28,
            "default": 0.79},
        "air_resistance": {
            "type": "float",
            "min": 0.0,
            "max": 1.0,
            "default": 0.2},
        "height": {
            "type": "float",
            "min": 0.0,
            "max": 1000.0,
            "default": 1.0},
        "time_step": {
            "type": "float",
            "min": 0.0001,
            "max": 1.0,
            "default": 0.01},
        "gravity": {
            "type": "float",
            "min": 0.0,
            "max": 1000.0,
            "default": 9.8},
        "mass": {
            "type": "float",
            "min": 0.0001,
            "max": 1000.0,
            "default": 1.0},
        "velocity": {
            "type": "float",
            "min": 0.0,
            "max": 1000.0,
            "default": 10.0}}

    # Create an encoder and decoder for the cannonsim app
    encoder = uq.encoders.GenericEncoder(
        template_fname='tests/cannonsim/test_input/cannonsim.template',
        delimiter='#',
        target_filename='in.cannon')
    output_cols = ['Dist', 'lastvx', 'lastvy']
    decoder = uq.decoders.SimpleCSV(
        target_filename='output.csv', output_columns=output_cols, header=0)
    # Create a collation element for this campaign
    collater = uq.collate.AggregateByVariables(average=False)
    # Make a random sampler
    sweep = {
        "angle": [0.1, 0.2, 0.3],
        "height": [2.0, 10.0],
        "velocity": [10.0, 10.1, 10.2]
    }
    sampler = uq.sampling.BasicSweep(sweep=sweep)

    my_campaign = uq.Campaign(name='aggregate_by_var', work_dir=tmpdir)
    my_campaign.add_app(name="cannon_test",
                        params=params,
                        encoder=encoder,
                        decoder=decoder,
                        collater=collater)
    my_campaign.set_app("cannon_test")
    sampler = uq.sampling.BasicSweep(sweep=sweep)
    my_campaign.set_sampler(sampler)
    my_campaign.draw_samples()
    my_campaign.populate_runs_dir()

    actions = uq.actions.ExecuteLocal("tests/cannonsim/bin/cannonsim in.cannon output.csv")
    my_campaign.apply_for_each_run_dir(actions)
    my_campaign.collate()

    results = my_campaign.get_collation_result()
    assert 'Variable' in results.columns
    assert 'Value' in results.columns
    n_runs = sampler.count
    n_vars = len(output_cols)
    assert len(results) == n_runs * n_vars
Beispiel #11
0
def flee_analyse_vvp_QoI(config):
    """
    flee_analyse_vvp_LoR will analysis the output of each vvp ensemble series

    usage example:
        fab localhost flee_analyse_vvp_QoI:mali
        fab training_hidalgo flee_analyse_vvp_QoI:mali
    """
    update_environment()
    #############################################
    # load flee vvp configuration from yml file #
    #############################################
    flee_VVP_config_file = os.path.join(
        get_plugin_path("FabFlee"),
        "VVP",
        "flee_VVP_config.yml"
    )
    VVP_campaign_config = load_VVP_campaign_config(flee_VVP_config_file)
    polynomial_order = VVP_campaign_config["polynomial_order"]
    sampler_name = VVP_campaign_config["sampler_name"]
    campaign_name = "flee_VVP_{}_{}".format(sampler_name, config)

    ###########################################
    # set a default dir to save results sobol #
    ###########################################
    campaign_work_dir = os.path.join(
        get_plugin_path("FabFlee"),
        "VVP",
        "flee_vvp_QoI_{}_PO{}_{}".format(
            sampler_name, polynomial_order, config
        ),
    )

    ###################
    # reload Campaign #
    ###################
    load_campaign_files(campaign_work_dir)
    db_location = "sqlite:///" + campaign_work_dir + "/campaign.db"
    campaign = uq.Campaign(name=campaign_name, db_location=db_location)
    print("===========================================")
    print("Reloaded campaign {}".format(campaign_name))
    print("===========================================")

    sampler = campaign.get_active_sampler()
    campaign.set_sampler(sampler, update=True)

    ####################################################
    # fetch results from remote machine                #
    # here, we ONLY fetch the required results folders #
    ####################################################
    env.job_desc = "_vvp_QoI_{}_PO{}".format(
        sampler_name,
        polynomial_order
    )
    with_config(config)

    job_folder_name = template(env.job_name_template)
    print("fetching results from remote machine ...")
    with hide("output", "running", "warnings"), settings(warn_only=True):
        fetch_results(regex=job_folder_name)
    print("Done\n")

    #####################################################
    # copy ONLY the required output files for analyse,  #
    # i.e., EasyVVUQ.decoders.target_filename           #
    #####################################################
    src = os.path.join(env.local_results, job_folder_name, "RUNS")
    des = campaign.campaign_db.runs_dir()
    print("Syncing output_dir ...")
    # with hide('output', 'running', 'warnings'), settings(warn_only=True):
    local(
        "rsync -pthrz "
        "--include='/*/' "
        "--include='out_uncertainty.csv' "
        "--include='out.csv' "
        "--exclude='*' "
        "{}/  {} ".format(src, des)
    )
    print("Done ...\n")

    #########################
    # find output csv files #
    #########################
    out_csv_files = glob.glob(des + '/**/out.csv', recursive=True)
    uncertainty_csv_files = glob.glob(des + '/**/out_uncertainty.csv',
                                      recursive=True)

    ###########################################################
    # take the number of refugees number by day per each camp #
    ###########################################################
    pd_out_csv = []
    for csv_file in out_csv_files:
        pd_csv = pd.read_csv(csv_file)
        pd_csv = pd_csv.drop(
            [column_name
             for column_name in pd_csv.columns.values
             if " sim" not in column_name and column_name != "Day"
             ],
            1
        )
        pd_out_csv.append(pd_csv)

    pd_uncertainty_csv = []
    for csv_file in uncertainty_csv_files:
        pd_csv = pd.read_csv(csv_file)
        pd_csv = pd_csv.drop(
            [column_name
             for column_name in pd_csv.columns.values
             if " sim" not in column_name and column_name != "Day"
             ],
            1
        )
        pd_uncertainty_csv.append(pd_csv)

    QoIs = [column_name
            for column_name in list(pd_out_csv[0].columns.values)
            if column_name != "Day"
            ]
    agg_input = {column_name: lambda x: list(x) for column_name in QoIs}

    pd_out_csv = pd.concat(pd_out_csv, axis=0,
                           ignore_index=True
                           ).groupby(['Day']).agg(agg_input)

    pd_uncertainty_csv = pd.concat(pd_uncertainty_csv, axis=0,
                                   ignore_index=True
                                   ).groupby(['Day']).agg(agg_input)

    results = {}
    for QoI in QoIs:
        print("QoI = {}".format(QoI))
        sim_result = pd_out_csv[QoI].tolist()
        uncertainty_result = pd_uncertainty_csv[QoI].tolist()

        res = ensemble_vvp_QoI(sim_result, uncertainty_result, QoI)

        for key, value in res.items():
            if key not in results:
                results.update({key: {}})
            results[key].update(value)

    with open(os.path.join(campaign_work_dir, "results.json"), "w") as f:
        f.write(json.dumps(results))

    for similarity_measure_name, data in results.items():
        # print(data)
        print("=" * 50)
        print("=" * 50)
        print("=" * 50)
        print("similarity_measure_name = {}".format(similarity_measure_name))
        print("locations name : {}".format(data.keys()))

        # find the number of rows and columns for subplots
        size = len(data.keys())
        cols = round(math.sqrt(size))
        rows = cols
        while rows * cols < size:
            rows += 1

        fig, ax_arr = plt.subplots(rows, cols, figsize=(8, 5))
        fig.suptitle(
            'similarity measure : {}'.format(similarity_measure_name),
            fontsize=15
        )
        max_y = -float("inf")
        ax_arr = ax_arr.reshape(-1)
        for i, (location_name, values) in enumerate(data.items()):
            print("location_name = {} max(values) = {}".format(
                location_name, max(values))
            )

            ax_arr[i].plot(values)
            ax_arr[i].set_xlim([1, len(values)])
            ax_arr[i].set_title(
                location_name, fontsize=10, fontweight='bold', loc='center'
            )
        for j in range(i + 1, rows * cols):
            ax_arr[j].axis('off')

        mng = plt.get_current_fig_manager()
        mng.window.showMaximized()

        fig.tight_layout()
        plot_file_name = "vvp_QoI_{}.png".format(similarity_measure_name)
        plt.savefig(os.path.join(campaign_work_dir, plot_file_name),
                    dpi=400)
Beispiel #12
0
    def _campaign(work_dir, campaign_name, app_name, params, encoder, decoder, sampler,
                  collater, actions, stats, vary, num_samples=0, replicas=1, db_type='sql',
                  call_fn=None):
        my_campaign = uq.Campaign(name=campaign_name, work_dir=work_dir, db_type=db_type)
        logging.debug("Serialized encoder: %s", str(encoder.serialize()))
        logging.debug("Serialized decoder: %s", str(decoder.serialize()))
        logging.debug("Serialized collation: %s", str(collater.serialize()))

        # Add the cannonsim app
        my_campaign.add_app(name=app_name,
                            params=params,
                            encoder=encoder,
                            decoder=decoder,
                            collater=collater)
        my_campaign.set_app(app_name)
        logging.debug("Serialized sampler: %s", str(sampler.serialize()))
        # Set the campaign to use this sampler
        my_campaign.set_sampler(sampler)
        # Draw 5 samples
        my_campaign.draw_samples(num_samples=num_samples, replicas=replicas)
        # Print the list of runs now in the campaign db
        logging.debug("List of runs added:")
        logging.debug(pformat(my_campaign.list_runs()))
        logging.debug("---")
        # Encode all runs into a local directory
        logging.debug(pformat(
            f"Encoding all runs to campaign runs dir {my_campaign.get_campaign_runs_dir()}"))
        my_campaign.populate_runs_dir()
        assert(len(my_campaign.get_campaign_runs_dir()) > 0)
        assert(os.path.exists(my_campaign.get_campaign_runs_dir()))
        assert(os.path.isdir(my_campaign.get_campaign_runs_dir()))
        if call_fn is not None:
            my_campaign.call_for_each_run(call_fn)
        # Local execution
        if actions is not None:
            my_campaign.apply_for_each_run_dir(actions)
        # Collate all data into one pandas data frame
        my_campaign.collate()
        logging.debug("data: %s", str(my_campaign.get_collation_result()))
        # Save the state of the campaign
        state_file = work_dir + "{}_state.json".format(app_name)
        my_campaign.save_state(state_file)
        my_campaign = None
        # Load state in new campaign object
        reloaded_campaign = uq.Campaign(state_file=state_file, work_dir=work_dir)
        reloaded_campaign.set_app(app_name)
        # Draw 3 more samples, execute, and collate onto existing dataframe
        logging.debug("Running 3 more samples...")
        reloaded_campaign.draw_samples(num_samples=num_samples, replicas=replicas)
        logging.debug("List of runs added:")
        logging.debug(pformat(reloaded_campaign.list_runs()))
        logging.debug("---")
        reloaded_campaign.populate_runs_dir()
        if call_fn is not None:
            reloaded_campaign.call_for_each_run(call_fn)
        if actions is not None:
            reloaded_campaign.apply_for_each_run_dir(actions)
        logging.debug("Completed runs:")
        logging.debug(pformat(reloaded_campaign.scan_completed()))
        logging.debug("All completed? %s", str(reloaded_campaign.all_complete()))
        reloaded_campaign.collate()
        logging.debug("data:\n %s", str(reloaded_campaign.get_collation_result()))
        logging.debug(reloaded_campaign)
        # Create a BasicStats analysis element and apply it to the campaign
        if stats is not None:
            reloaded_campaign.apply_analysis(stats)
            logging.debug("stats:\n %s", str(reloaded_campaign.get_last_analysis()))
        # Print the campaign log
        logging.debug(pformat(reloaded_campaign._log))
        logging.debug("All completed? %s", str(reloaded_campaign.all_complete()))
Beispiel #13
0
 def __init__(self, campaign_name, work_dir, db_type='sql'):
     self.campaign = uq.Campaign(name=campaign_name, work_dir=work_dir, db_type=db_type)
Beispiel #14
0
import numpy as np
import easyvvuq as uq
import os

import matplotlib.pyplot as plt
plt.close('all')

# author: Wouter Edeling
__license__ = "LGPL"

# home directory of user
home = os.path.expanduser('~')
HOME = os.path.abspath(os.path.dirname(__file__))

# Set up a fresh campaign called "sc"
my_campaign = uq.Campaign(name='sc', work_dir='/tmp')

# Define parameter space
params = {
    "Pe": {
        "type": "float",
        "min": 1.0,
        "max": 2000.0,
        "default": 100.0
    },
    "f": {
        "type": "float",
        "min": 0.0,
        "max": 10.0,
        "default": 1.0
    },
def run_sc_samples(work_dir):
    
    # Set up a fresh campaign called "sc"
    my_campaign = uq.Campaign(name='ocean', work_dir=work_dir)

    # Define parameter space
    params = {
        "decay_time_nu": {
            "type": "float",
            "min": 0.0,
            "max": 1000.0,
            "default": 5.0},
        "decay_time_mu": {
            "type": "float",
            "min": 0.0,
            "max": 1000.0,
            "default": 90.0},
        "out_file": {
            "type": "string",
            "default": "output.csv"}}

    output_filename = params["out_file"]["default"]
    output_columns = ["E_mean", "Z_mean", "E_std", "Z_std"]

    # Create an encoder, decoder and collation element for PCE test app
    encoder = uq.encoders.GenericEncoder(
        template_fname= HOME + '/sc/ocean.template',
        delimiter='$',
        target_filename='ocean_in.json')
    decoder = uq.decoders.SimpleCSV(target_filename=output_filename,
                                    output_columns=output_columns,
                                    header=0)
    collater = uq.collate.AggregateSamples(average=False)

    # Add the SC app (automatically set as current app)
    my_campaign.add_app(name="sc",
                        params=params,
                        encoder=encoder,
                        decoder=decoder,
                        collater=collater)    

    # Create the sampler
    vary = {
        "decay_time_nu": cp.Uniform(1.0, 5.0),
        "decay_time_mu": cp.Uniform(85.0, 95.0)
    }

    my_sampler = uq.sampling.SCSampler(vary=vary, polynomial_order=2)
    # Associate the sampler with the campaign
    my_campaign.set_sampler(my_sampler)
    
    # Will draw all (of the finite set of samples)
    my_campaign.draw_samples()

    my_campaign.populate_runs_dir()
 
    #Run execution using Fabsim 
    fab.run_uq_ensemble(my_campaign.campaign_dir, 'ocean', machine='localhost')
    
    #Save the Campaign
    my_campaign.save_state("campaign_state.json")
Beispiel #16
0
    },
    uparams[1]: {
        "type": "real",
        "default": "180."
    },
    "out_file": {
        "type": "str",
        "default": "output.csv"
    }
}

output_filename = params["out_file"]["default"]
output_columns = ["te", "ti"]

# Initialize Campaign object
my_campaign = uq.Campaign(name='uq_boundaries', work_dir=tmp_dir)

# Copy XML files needed in the ETS, CHEASE and BOHMGB wrappers
campaign_dir = my_campaign.campaign_dir
os.system("mkdir " + campaign_dir + "/workflows")
os.system("cp ../../workflows/ets.xml " + campaign_dir + "/workflows")
os.system("cp ../../workflows/ets.xsd " + campaign_dir + "/workflows")
os.system("cp ../../workflows/chease.xml " + campaign_dir + "/workflows")
os.system("cp ../../workflows/chease.xsd " + campaign_dir + "/workflows")
os.system("cp ../../workflows/bohmgb.xml " + campaign_dir + "/workflows")
os.system("cp ../../workflows/bohmgb.xsd " + campaign_dir + "/workflows")

# Copy CPO files in common directory
common_dir = campaign_dir + "/common/"
os.system("mkdir " + common_dir)
os.system("cp " + cpo_dir + "/*.cpo " + common_dir)
Beispiel #17
0
import chaospy as cp
import easyvvuq as uq
import os, subprocess
import fabsim3_cmd_api as fab

config = 'virsim'
script = 'virsim_FC'
machine = 'eagle_vecma'
workdir = '/export/scratch1/federica/VirsimCampaigns'

#home dir of this file
HOME = os.path.abspath(os.path.dirname(__file__))

# Set up a fresh campaign called "corona"
campaign = uq.Campaign(name='virsim_FC', work_dir=workdir)

# Define parameter space
params = {
    "seed": {
        "type": "float",
        "min": 0,
        "max": 2**31,
        "default": 12345
    },
    "trace_prob_E": {
        "type": "float",
        "min": 0.0,
        "max": 1.0,
        "default": .6
    },
import chaospy as cp
import numpy as np
import easyvvuq as uq
import matplotlib.pyplot as plt
import os
import fabsim3_cmd_api as fab

# author: Wouter Edeling
__license__ = "LGPL"

#home dir of this file
HOME = os.path.abspath(os.path.dirname(__file__))

# Set up a fresh campaign called "sc"
my_campaign = uq.Campaign(name='corona', work_dir='/tmp')

# Define parameter space
params = {
    "incubation": {
        "type": "float",
        "min": 1.0,
        "max": 10.0,
        "default": 5.2
    },
    "r0": {
        "type": "float",
        "min": 1.0,
        "max": 10.0,
        "default": 2.5
    },
    "out_file": {
                                axis=0)
    idx_sort_descend_1st = np.argsort(sobol[:, 0], axis=0)[::-1]
    sobol = sobol[idx_sort_descend_1st, :]
    sobol_idx_bool = sobol_idx_bool[idx_sort_descend_1st]
    sobol_idx = [0 for _ in range(sobol_idx_bool.shape[0])]
    for i_sobol in range(sobol_idx_bool.shape[0]):
        sobol_idx[i_sobol] = np.array(
            [i for i, x in enumerate(sobol_idx_bool[i_sobol, :]) if x])
    var = ((coefficients[1:]**2).sum(axis=0))
    sobol = sobol / var
    return sobol, sobol_idx, sobol_idx_bool


# Read an old campaign
time_start = time.time()
old_campaign = uq.Campaign(state_file="campaign_state.json", work_dir=".")
time_end = time.time()
print('Time for phase 1 = %.3f' % (time_end - time_start))

time_start = time.time()
results_df = old_campaign.get_collation_result()
time_end = time.time()
print('Time for phase 2 = %.3f' % (time_end - time_start))

# Post-processing analysis
time_start = time.time()
analysis = uq.analysis.PCEAnalysis(sampler=old_campaign.get_active_sampler(),
                                   qoi_cols=["te", "ne", "rho", "rho_norm"],
                                   sampling=False)
old_campaign.apply_analysis(analysis)
time_end = time.time()
Beispiel #20
0
    },
    uparams[5]: {
        "type": "real",
        "default": "0.2"
    },
    "out_file": {
        "type": "str",
        "default": "output.csv"
    }
}

output_filename = params["out_file"]["default"]
output_columns = ["te", "ti"]

# Initialize Campaign object
my_campaign = uq.Campaign(name='uq_sources', work_dir=tmp_dir)

# Copy XML files needed in the ETS, CHEASE and BOHMGB codes
campaign_dir = my_campaign.campaign_dir
os.system("mkdir " + campaign_dir + "/workflows")
os.system("cp ../../workflows/ets.xml " + campaign_dir + "/workflows")
os.system("cp ../../workflows/ets.xsd " + campaign_dir + "/workflows")
os.system("cp ../../workflows/chease.xml " + campaign_dir + "/workflows")
os.system("cp ../../workflows/chease.xsd " + campaign_dir + "/workflows")
os.system("cp ../../workflows/bohmgb.xml " + campaign_dir + "/workflows")
os.system("cp ../../workflows/bohmgb.xsd " + campaign_dir + "/workflows")
os.system("cp ../../workflows/source_dummy_new.xml " + campaign_dir +
          "/workflows/source_dummy.xml")
os.system("cp ../../workflows/source_dummy.xsd " + campaign_dir + "/workflows")

# Copy CPO files in common directory
def adaptive_campaign():

    d = 3
    number_of_adaptations = 3
    campaign = uq.Campaign(name='sc', work_dir='/tmp')
    params = {}
    for i in range(d):
        params["x%d" % (i + 1)] = {"type": "float",
                                   "min": 0.0,
                                   "max": 1.0,
                                   "default": 0.5}
    params["out_file"] = {"type": "string", "default": "output.csv"}

    output_filename = params["out_file"]["default"]
    output_columns = ["f"]

    # Create an encoder, decoder and collation element
    encoder = uq.encoders.GenericEncoder(
        template_fname='tests/sc/poly_model_anisotropic.template',
        # template_fname='./sc/poly_model_anisotropic.template',
        delimiter='$',
        target_filename='poly_in.json')
    decoder = uq.decoders.SimpleCSV(target_filename=output_filename,
                                    output_columns=output_columns)
    execute = ExecuteLocal(os.path.abspath("tests/sc/poly_model_anisotropic.py") + " poly_in.json")
    actions = Actions(CreateRunDirectory('/tmp'), Encode(encoder), execute, Decode(decoder))

    # Add the SC app (automatically set as current app)
    campaign.add_app(name="sc",
                     params=params,
                     actions=actions)

    # Create the sampler
    vary = {}
    for i in range(d):
        vary["x%d" % (i + 1)] = cp.Uniform(0, 1)

    sampler = uq.sampling.SCSampler(vary=vary, polynomial_order=1,
                                    quadrature_rule="C",
                                    sparse=True, growth=True,
                                    midpoint_level1=True,
                                    dimension_adaptive=True)
    campaign.set_sampler(sampler)
    campaign.execute().collate()
    data_frame = campaign.get_collation_result()
    analysis = uq.analysis.SCAnalysis(sampler=sampler, qoi_cols=output_columns)

    campaign.apply_analysis(analysis)

    for i in range(number_of_adaptations):
        sampler.look_ahead(analysis.l_norm)

        campaign.execute().collate()
        data_frame = campaign.get_collation_result()
        analysis.adapt_dimension('f', data_frame)

        campaign.apply_analysis(analysis)
    logging.debug(analysis.l_norm)
    logging.debug(sampler.admissible_idx)

    results = campaign.get_last_analysis()

    return sampler, analysis, results
Beispiel #22
0
"""
*****************
* VVUQ ANALYSES *
*****************
"""

config = 'virsim_CT'
script = 'virsim_CT'
machine = 'eagle_vecma'
workdir = '/export/scratch1/federica/VirsimCampaigns'

# home directory of this file    
HOME = os.path.abspath(os.path.dirname(__file__))

# Reload the campaign
campaign = uq.Campaign(state_file = "campaign_state_CT_MC2k_newdistr.json", work_dir = workdir)
print('========================================================')
print('Reloaded campaign', campaign.campaign_dir.split('/')[-1])
print('========================================================')

# get sampler and output columns from my_campaign object
sampler = campaign._active_sampler
# print(type(sampler._samples))
# print(sampler._samples.shape)

output_columns = campaign._active_app_decoder.output_columns

fab.verify(config, campaign.campaign_dir, 
            campaign._active_app_decoder.target_filename, 
            machine=machine, PJ=True)
Beispiel #23
0
def flee_init_PCE(config, simulation_period=-1, mode='parallel', ** args):
    '''
    ============================================================================

        fab <remote_machine> flee_init_PCE:<conflict_name>,simulation_period=<number>

    example:

        fab eagle_vecma flee_init_PCE:mali,simulation_period=100

    ============================================================================
    '''
    # delete work_dir_PCESampler is exists
    if os.path.exists(work_dir_PCESampler):
        rmtree(work_dir_PCESampler)
    os.mkdir(work_dir_PCESampler)

    # Set up a fresh campaign called "flee-PCESampler"
    campaign = uq.Campaign(name='flee-PCESampler',
                           work_dir=work_dir_PCESampler)

    # to make sure we are not overwriting the new simulation on previous ones
    job_label = campaign._campaign_dir

    # Define parameter space for the flee-PCESampler app
    params = json.load(open(os.path.join(get_plugin_path("FabFlee"),
                                         'templates',
                                         'params.json'
                                         )
                            )
                       )

    output_filename = params["out_file"]["default"]

    encoder = uq.encoders.GenericEncoder(
        template_fname=get_plugin_path("FabFlee") +
        '/templates/simsetting.template',
        delimiter='$',
        target_filename='simsetting.csv'
    )

    decoder = uq.decoders.SimpleCSV(target_filename=output_filename,
                                    output_columns=output_columns,
                                    header=0)

    # Create a collation element for this campaign
    collater = uq.collate.AggregateSamples(average=True)

    # Add the flee-PCESampler app
    campaign.add_app(name="flee-PCESampler",
                          params=params,
                          encoder=encoder,
                          decoder=decoder,
                          collater=collater)

    # parameters to vary
    vary = {
        "max_move_speed": cp.Uniform(100, 500),
        "max_walk_speed": cp.Uniform(10, 100),
        #"camp_move_chance": cp.Uniform(0.0, 0.1),
        #"conflict_move_chance": cp.Uniform(0.1, 1.0),
        #"default_move_chance": cp.Uniform(0.1, 1.0),
        #"camp_weight": cp.Uniform(1.0, 10.0),
        #"conflict_weight": cp.Uniform(0.1, 1.0)
    }

    # create PCESampler
    sampler = uq.sampling.PCESampler(vary=vary,
                                     polynomial_order=1
                                     )

    # Associate the sampler with the campaign
    campaign.set_sampler(sampler)

    # Will draw all (of the finite set of samples)
    campaign.draw_samples()

    run_ids = campaign.populate_runs_dir()

    # copy generated run folders to SWEEP directory in config folder
    # 1. clean config SWEEP dir
    # 2. copy all generated runs by easyvvuq to config SWEEP folder
    path_to_config = find_config_file_path(config)
    sweep_dir = path_to_config + "/SWEEP"

    if os.path.exists(sweep_dir):
        rmtree(sweep_dir)
    os.mkdir(sweep_dir)
    print('=' * 20)
    print("Copying easyvvuq runs to %s SWEEP folder ..." % (config))
    with hide('output', 'running', 'warnings'), settings(warn_only=True):
        local(
            "rsync -av -m -v \
            {}/  {} ".format(os.path.join(campaign.work_dir,
                                          campaign.campaign_dir,
                                          'SWEEP'), os.path.join(sweep_dir))
        )
    print("Done")
    print('=' * 20)

    if mode == 'serial':
        flee_script = 'flee'
    else:
        flee_script = 'pflee'

    if simulation_period == -1:
        flee_ensemble(config,
                      script=flee_script,
                      label=job_label,
                      **args)
    else:
        flee_ensemble(config,
                      simulation_period,
                      script=flee_script,
                      label=job_label,
                      **args)

    # save campaign and sampler state
    campaign.save_state(os.path.join(
        work_dir_PCESampler, "campaign_state.json"))

    backup_campaign_files()
import easyvvuq as uq
import chaospy as cp

# 0. Setup some variables describing app to be run
#
#    gauss.py is in current directory and takes one input file
#    and writes to 'output.csv'.
cwd = os.getcwd()
input_filename = 'gauss_in.json'
cmd = f"{cwd}/gauss.py {input_filename}"
out_file = "output.csv"
# Template input to substitute values into for each run
template = f"{cwd}/gauss.template"

# 1. Create campaign
my_campaign = uq.Campaign(name='gauss', work_dir=".")

# 2. Parameter space definition
params = {
    "sigma": {
        "type": "float",
        "min": 0.0,
        "max": 100000.0,
        "default": 0.25
    },
    "mu": {
        "type": "float",
        "min": 0.0,
        "max": 100000.0,
        "default": 1
    },
Beispiel #25
0
from sklearn.neighbors.kde import KernelDensity
from scipy import stats

# author: Wouter Edeling
__license__ = "LGPL"
 
#post processing of UQ samples executed via FabSim. All samples must have been completed
#before this subroutine is executed. Use 'fabsim <machine_name> job_stat' to check their status

#home dir of this file    
HOME = os.path.abspath(os.path.dirname(__file__))
# work_dir = home + "/VECMA/Campaigns/"
work_dir = '/tmp'

#Reload the campaign
my_campaign = uq.Campaign(state_file = 'campaign_state.json', work_dir = work_dir)

print('========================================================')
print('Reloaded campaign', my_campaign.campaign_dir.split('/')[-1])
print('========================================================')

#get sampler and output columns from my_campaign object
my_sampler = my_campaign.get_active_sampler()
output_columns = my_campaign._active_app_decoder.output_columns

#fetch the results from the (remote) host via FabSim3
fab.get_uq_samples(my_campaign.campaign_dir, machine='localhost')

#collate output
my_campaign.collate()
Beispiel #26
0
import chaospy as cp
from pprint import pprint

# 0. Setup some variables describing app to be run
#
#    gauss.py is in current directory and takes one input file
#    and writes to 'output.csv'.
cwd = os.getcwd()
input_filename = 'gauss_in.json'
cmd = f"{cwd}/gauss.py {input_filename}"
out_file = "output.csv"
# Template input to substitute values into for each run
template = f"{cwd}/gauss.template"

# 1. Create campaign
my_campaign = uq.Campaign(name='gauss', work_dir=".")

# 2. Parameter space definition
params = {
    "sigma": {
        "type": "float",
        "min": 0.0,
        "max": 100000.0,
        "default": 0.25
    },
    "mu": {
        "type": "float",
        "min": 0.0,
        "max": 100000.0,
        "default": 1
    },
Beispiel #27
0
from matplotlib.ticker import ScalarFormatter, NullFormatter
plt.rcParams.update({'font.size': 18, 'legend.fontsize': 15})
plt.rcParams['figure.figsize'] = 12,7

"""
*************
* Load data *
*************
"""

# home directory of this file    
HOME = os.path.abspath(os.path.dirname(__file__))

# Reload the FC campaign without biology
workdir = '/export/scratch2/home/federica/'
campaign = uq.Campaign(state_file = "campaign_state_IL_nobio.json", work_dir = workdir)
print('========================================================')
print('Reloaded campaign', campaign.campaign_dir.split('/')[-1])
print('========================================================')

# get sampler and output columns from campaign object
sampler = campaign._active_sampler
#output_columns = campaign._active_app_decoder.output_columns

# collate output
campaign.collate()
# get full dataset of data
data = campaign.get_collation_result()
#print(data.columns)

"""
Beispiel #28
0
def test_pce(tmpdir):

    # Set up a fresh campaign called "pce"
    my_campaign = uq.Campaign(name='pce', work_dir=tmpdir)

    # Define parameter space
    params = {
        "kappa": {
            "type": "real",
            "min": "0.0",
            "max": "0.1",
            "default": "0.025"
        },
        "t_env": {
            "type": "real",
            "min": "0.0",
            "max": "40.0",
            "default": "15.0"
        },
        "out_file": {
            "type": "str",
            "default": "output.csv"
        }
    }

    output_filename = params["out_file"]["default"]
    output_columns = ["te", "ti"]

    # Create an encoder and decoder for PCE test app
    encoder = uq.encoders.GenericEncoder(
        template_fname='tests/cooling/cooling.template',
        delimiter='$',
        target_filename='cooling_in.json')
    decoder = uq.decoders.SimpleCSV(target_filename=output_filename,
                                    output_columns=output_columns,
                                    header=0)

    # Add the PCE app (automatically set as current app)
    my_campaign.add_app(name="pce",
                        params=params,
                        encoder=encoder,
                        decoder=decoder)

    # Create a collation element for this campaign
    collater = uq.collate.AggregateSamples(average=False)
    my_campaign.set_collater(collater)

    # Create the sampler
    vary = {"kappa": cp.Uniform(0.025, 0.075), "t_env": cp.Uniform(15, 25)}

    my_sampler = uq.sampling.PCESampler(vary=vary, polynomial_order=3)

    # Associate the sampler with the campaign
    my_campaign.set_sampler(my_sampler)

    # Will draw all (of the finite set of samples)
    my_campaign.draw_samples()

    my_campaign.populate_runs_dir()
    my_campaign.apply_for_each_run_dir(
        uq.actions.ExecuteLocal(
            "tests/cooling/cooling_model.py cooling_in.json"))

    my_campaign.collate()

    # Update after here

    # Post-processing analysis
    my_analysis = uq.analysis.PCEAnalysis(sampler=my_sampler,
                                          qoi_cols=output_columns)

    my_campaign.apply_analysis(my_analysis)

    results = my_campaign.get_last_analysis()

    # Get Descriptive Statistics
    stats = results['statistical_moments']['te']
    per = results['percentiles']['te']
    sobols = results['sobol_first_order']['te']

    # Test saving and reloading campaign
    #state_file = tmpdir + "pce_state.json"
    #my_campaign.save_state(state_file)
    #new = uq.Campaign(state_file=state_file, work_dir=tmpdir)
    #print(new)

    return stats, per, sobols
Beispiel #29
0
    def encode(self, params={}, target_dir='', fixtures=None):

        simulation_time = 10**params["equilibration2_time_power"]
        params["n_steps_loop"] = int(
            round(simulation_time / params["timestep"], -1))
        params["n_steps"] = 15 * int(
            round(simulation_time / params["timestep"], -1))
        super().encode(params, target_dir, fixtures)


home = os.path.abspath(os.path.dirname(__file__))
#output_columns = ["drug","replica","binding_energy_avg","binding_energy_stdev"]
output_columns = ["binding_energy_avg"]
work_dir = '/hppfs/work/pn72qu/di36yax3/tmp/uq_namd2/campaigns'

campaign = uq.Campaign(state_file="namd_easyvvuq_state.json",
                       work_dir=work_dir)
print('========================================================')
print('Reloaded campaign', campaign.campaign_dir.split('/')[-1])
print('========================================================')
sampler = campaign.get_active_sampler()
sampler.load_state("namd_sampler_state.pickle")
campaign.set_sampler(sampler)

#get results
#fab.get_uq_samples(config, campaign.campaign_dir, sampler._number_of_samples,
#                   machine='eagle_vecma')
campaign.collate()

# Post-processing analysis
analysis = uq.analysis.SCAnalysis(sampler=campaign._active_sampler,
                                  qoi_cols=output_columns)
Beispiel #30
0
def test_premature_run_addition(tmp_path):
    with pytest.raises(Exception):
        our_campaign = uq.Campaign(name='test', work_dir=tmp_path)
        # Requires an active app to be set
        our_campaign.add_runs([])