### Install Necessary Libraries Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/hyperparameter_tuning_tutorial_with_fabsim.ipynb.txt Installs EasyVVUQ, TensorFlow, and TensorFlow Datasets. Ensure these are installed before proceeding with the tutorial. ```python # !pip install easyvvuq !pip install tensorflow !pip install tensorflow_datasets ``` -------------------------------- ### Basic Setup for Dimension Adaptive Analysis Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/dimension_adaptive_tutorial.ipynb.txt Initializes the EasyVVUQ environment and defines a simple function for analysis. This is a starting point for setting up dimension-adaptive workflows. ```python import easyvvuq as uq from easyvvuq.tools import Tool import numpy as np def my_function(x): return np.sum(x**2) # Create a campaign object my_campaign = uq.Campaign(name='my_campaign', workdir='campaign_workdir') # Create a tool object my_tool = Tool(name='my_tool', executable='python', command='-c "import numpy as np; print(np.sum(np.array({input_params})**2))"') # Add the tool to the campaign my_campaign.add_tool(my_tool) ``` -------------------------------- ### Basic Dimension Adaptive Setup Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/dimension_adaptive_tutorial.ipynb.txt This snippet shows the fundamental setup for dimension-adaptive processing. Ensure necessary libraries are imported. ```python import numpy as np import matplotlib.pyplot as plt from easyvvuq.dimension_adaptive import DimensionAdaptive from easyvvuq.dimension_adaptive.utils import get_random_data ``` -------------------------------- ### Install Dependencies Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/hyperparameter_tuning_tutorial.ipynb.txt Uncomment and run these lines to install EasyVVUQ, TensorFlow, and TensorFlow Datasets if they are not already installed. ```python # !pip install easyvvuq # !pip install tensorflow # !pip install tensorflow_datasets ``` -------------------------------- ### Install and Configure FabSim3 Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/hyperparameter_tuning_tutorial_with_fabsim.ipynb.txt Installs FabSim3 and its dependencies. This step is crucial for setting up the simulation environment. ```bash git clone https://github.com/djgagne/fabsim3.git cd fabsim3 python3 -m pip install -r requirements.txt cp fabsim3/config-examples/example.yaml config.yaml ``` -------------------------------- ### Install EasyVVUQ Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/mega_tutorial.html Installs the EasyVVUQ library using pip. Restart the runtime if the installation fails. ```shell #!pip install git+https://github.com/UCL-CCS/EasyVVUQ ``` -------------------------------- ### Install EasyVVUQ from GitHub Source: https://easyvvuq.readthedocs.io/en/latest/_sources/installation.rst.txt Install the most current development version of EasyVVUQ directly from its GitHub repository. This method involves cloning the repository and running an installation script. ```bash git clone https://github.com/UCL-CCS/EasyVVUQ.git cd EasyVVUQ bash install_EasyVVUQ.sh ``` -------------------------------- ### ExecuteKubernetes Start Method Source: https://easyvvuq.readthedocs.io/en/latest/_modules/easyvvuq/actions/execute_kubernetes.html Starts the Kubernetes action by creating the Pod and associated ConfigMaps and Volumes. It waits for the Pod to complete and then finalizes the action. Ensure input and output filenames are correctly set. ```python def start(self, previous=None): """Will create the Kubernetes pod and hence start the action. Parameters ---------- previous: dict Data from previous Action. Returns ------- dict Data from previous Action appended with data from this Action. """ target_dir = previous['rundir'] if self.input_file_names is None: self.input_file_names = [previous['encoder_filename']] if self.output_file_name is None: self.output_file_name = previous['decoder_filename'] file_names = [(os.path.join(target_dir, input_file_name), str(uuid.uuid4())) for input_file_name in self.input_file_names] self.config_names = file_names dep = copy.deepcopy(self.body) dep['metadata']['name'] = str(uuid.uuid4()) self.create_config_maps(self.config_names) self.create_volumes(self.config_names, dep) self.core_v1.create_namespaced_pod(body=dep, namespace="default") self._started = True self.result = previous while not self.finished(): time.wait(5) self.finalise() return previous ``` -------------------------------- ### Basic Plotting Example Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial_qcgpj.ipynb.txt Demonstrates a simple plotting function. Ensure Matplotlib is installed. ```python import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.xlabel("X-axis") plt.ylabel("Y-axis") plt.title("Simple Sine Wave Plot") plt.show() ``` -------------------------------- ### Install EasyVVUQ Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/dimension_adaptive_tutorial.ipynb.txt Installs the EasyVVUQ library and its dependencies. It's recommended to install future and fipy if they are not already present. ```bash !pip install EasyVVUQ ``` ```bash #!pip install future ``` ```bash #!pip install fipy ``` -------------------------------- ### Install EasyVVUQ from GitHub Source: https://easyvvuq.readthedocs.io/en/latest/installation.html Install the most current version of EasyVVUQ directly from its GitHub repository. This method involves cloning the repository and running an installation script. ```bash git clone https://github.com/UCL-CCS/EasyVVUQ.git cd EasyVVUQ bash install_EasyVVUQ.sh ``` -------------------------------- ### Install EasyVVUQ and Dependencies Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/easyvvuq_fusion_tutorial.html Install the necessary libraries for EasyVVUQ, future, and fipy. ```python #!pip install EasyVVUQ #!pip install future #!pip install fipy ``` -------------------------------- ### Sensitivity Analysis Setup Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial.ipynb.txt Configures the campaign for sensitivity analysis by defining input distributions. ```python from easyvvuq.config import Config config = Config.from_file("config.json") config.set_input_distributions({'param1': 'uniform(0, 1)'}) print(config.inputs) ``` -------------------------------- ### Install EasyVVUQ and Dependencies Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/dimension_adaptive_tutorial.html This code installs the EasyVVUQ library and its dependencies using pip. It also includes commented-out installations for 'future' and 'fipy'. ```bash !pip install EasyVVUQ #!pip install future #!pip install fipy ``` -------------------------------- ### Kubernetes Execution Status Example Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/kubernetes_tutorial.html An example of the output from `execution.progress()`, showing the current state of the Kubernetes simulation jobs. ```json {"ready": 28, "active": 8, "finished": 0, "failed": 0} ``` -------------------------------- ### Uncertainty Quantification Setup Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial.ipynb.txt Configures the campaign for uncertainty quantification by specifying methods and parameters. ```python config.set_uq_method('monte_carlo', n_samples=1000) print(config.uq) ``` -------------------------------- ### ExecuteSLURM Start Method Source: https://easyvvuq.readthedocs.io/en/latest/_modules/easyvvuq/actions/execute_slurm.html Starts the SLURM job by generating the script, submitting it using 'sbatch', and monitoring its status using 'squeue' until completion. The job ID is extracted from the 'sbatch' output. ```python def start(self, previous=None): """Start the SLURM job. Parameters ---------- previous: dict A dictionary containing information provided by previously executed actions. """ target_dir = previous['rundir'] script_name = os.path.join(target_dir, os.path.basename(self.script_name)) script, = self.template.replace(self.variable, target_dir), with open(script_name, 'w') as fd: fd.write(script) result = subprocess.run( ['sbatch', script_name], cwd=target_dir, check=True, capture_output=True) stdout = result.stdout.decode('utf-8') self.job_id = re.findall(r'\d+', stdout)[0] while True: result = subprocess.run( ['squeue', '-j', self.job_id], cwd=target_dir, check=True, capture_output=True) stdout = result.stdout.decode('utf-8') if self.job_id not in stdout: break time.sleep(random.randint(1, 600)) return previous ``` -------------------------------- ### Install reticulate Package in R Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_in_r.ipynb.txt Installs the 'reticulate' package, which is necessary for interfacing R with Python. This is a one-time setup step. ```r install.packages("reticulate") ``` -------------------------------- ### Initialize and Use Library Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial_qcgpj.ipynb.txt Demonstrates basic initialization and usage of the library. Ensure the library is correctly installed and imported before use. ```python import numpy as np import pandas as pd from pandas import DataFrame from typing import List, Dict, Any # Example usage print("Hello, World!") ``` -------------------------------- ### Get Library Version Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial.ipynb.txt Retrieves the currently installed version of the library. ```python from easyvvuq import __version__ print(f"EasyVVUQ version: {__version__}") ``` -------------------------------- ### Basic Setup and Initialization Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/dimension_adaptive_tutorial.ipynb.txt Initializes the EasyVVUQ environment and sets up basic parameters for adaptive sampling. Ensure necessary libraries are imported. ```python import easyvvuq as uq from easyvvuq.tools import adaptive_sampling import numpy as np ``` -------------------------------- ### FabSim Execution Output Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/hyperparameter_tuning_tutorial_with_fabsim.html Example output from FabSim indicating the start of the ensemble run. ```text Executing fabsim localhost run_uq_ensemble:grid_search,campaign_dir=/tmp/grid_test9hb35tv6,script=grid_search,skip=0,PJ=False ``` -------------------------------- ### Prepare Input File for BEAM Application Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial_qcgpj.ipynb.txt This example demonstrates how to set up the input file for the BEAM application. It first changes the directory and then creates an 'input.json' file with specific parameters. ```bash !pwd !echo "{\"outfile\": \"output.json\", \"F\": 1.0, \"L\": 1.5, \"a\": 1.0, \"D\": 0.8, \"d\": 0.1, \"E\": 200000}" > input.json ``` -------------------------------- ### Setting up the MCMC sampler Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_mcmc.ipynb.txt Initialize an MCMC sampler, such as `emcee`, and configure it with the model and prior information. This step prepares the sampler for the simulation. ```python sampler = mcmc.MCMC(model=simple_model, nwalkers=50, nsteps=1000, nburn=100) ``` -------------------------------- ### Set up Campaign Working Directory Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/correlated/coffe_cup-pce.html Prepares the working directory for the EasyVVUQ campaign. Clears the directory if it already exists and then creates it. ```python work_dir = os.getcwd() campaign_work_dir = os.path.join(work_dir, "easyvvuq_pce") # clear the target campaign dir if os.path.exists(campaign_work_dir): rmtree(campaign_work_dir) os.makedirs(campaign_work_dir) ``` -------------------------------- ### Basic Vector Configuration Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/vector_qoi_tutorial.ipynb.txt A minimal Vector configuration file. This serves as a starting point for more complex setups. ```toml data_dir = "/var/lib/vector" [api] enabled = true address = "127.0.0.1:8080" [sources.my_source] type = "file" include = ["/var/log/**/*.log"] [transforms.my_transform] type = "remap" inputs = ["my_source"] source = ''' .message = .message + " transformed" ''' [sinks.my_sink] type = "console" inputs = ["my_transform"] encoding = "json" ``` -------------------------------- ### Basic Plotting with Matplotlib Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/dimension_adaptive_tutorial.ipynb.txt A fundamental example of creating a plot using Matplotlib. This is often a starting point for data visualization tasks. ```python import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x) plt.figure(figsize=(8, 6)) plt.plot(x, y, label='sin(x)') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Simple Sine Wave Plot') plt.legend() plt.grid(True) plt.show() ``` -------------------------------- ### Set up MCMC sampler Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_mcmc.ipynb.txt Initialize the MCMC sampler with the model, priors, and likelihood function. This example uses the 'emcee' sampler. ```python import emcee import numpy as np # Assuming 'simple_model', 'priors', and 'likelihood' are defined as above def log_prob(params): prior_log_prob = sum(p.logpdf(params[name]) for name, p in priors.items()) if not np.isfinite(prior_log_prob): return -np.inf return prior_log_prob + likelihood(params, observed_data, simple_model) initial_state = [p.rvs() for p in priors.values()] n_walkers = 50 n_dim = len(initial_state) sampler = emcee.EnsembleSampler(n_walkers, n_dim, log_prob) ``` -------------------------------- ### Monte Carlo Simulation Setup Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_MC_tutorial.ipynb.txt Sets up a Monte Carlo simulation using EasyVVUQ. It defines the input parameters, their distributions, and the number of samples for the simulation. ```python import easyvvuq as uq import numpy as np import math def ishigami(x, y): return math.sin(x[0]) + a * math.sin(x[1])**2 + b * x[1] * x[0] a = 7.0 b = 0.1 inputs = [ { "name": "x", "type": "uniform", "grid": np.linspace(-math.pi, math.pi, 100) }, { "name": "y", "type": "uniform", "grid": np.linspace(-math.pi, math.pi, 100) } ] coll = uq.collocation.CollocationEncoder(input_json=inputs, encoder='first_order') # Create a Campaign object my_campaign = uq.Campaign(name='ishigami_mc', work_dir='workdir') # Create a Simulation object my_campaign.add_simulations( sims=[uq.Sim(name='mc_run', app_name='python', desc='A basic Monte Carlo run', params={'x': 'x', 'y': 'y'}, cmd_line='python {model_file} {x} {y}')], design=uq.Design(encoder=coll, n_samples=1000) ) # Set the model file my_campaign.set_model(model_file='model.py') # Add the ishigami function to the model file with open('model.py', 'w') as f: f.write('import math\n') f.write('a = 7.0\n') f.write('b = 0.1\n') f.write('def ishigami(x, y):\n') f.write(' return math.sin(x) + a * math.sin(y)**2 + b * y * x\n') f.write('import sys\n') f.write('if __name__ == "__main__":\n') f.write(' x_val = float(sys.argv[1])\n') f.write(' y_val = float(sys.argv[2])\n') f.write(' print(ishigami(x_val, y_val))\n') ``` -------------------------------- ### Install TensorFlow and Dependencies Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/hyperparameter_tuning_tutorial_with_fabsim.ipynb.txt Installs TensorFlow and its required packages. Uncomment the lines to run the installation. ```python # !pip install tensorflow ``` -------------------------------- ### Define Campaign for Ishigami SC Tutorial Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_SC_tutorial.ipynb.txt Sets up the campaign directory and configuration for the Ishigami SC tutorial. This involves creating necessary files and directories for the analysis. ```python import easyvvuq as uq from easyvvuq.actions import Action, ActionCollection import os # Define campaign directory campaign_dir = "campaign_ishigami_sc" # Create campaign object my_campaign = uq.Campaign( name=campaign_dir, work_dir=campaign_dir, # Define the actions to be performed actions=ActionCollection([ Action(name="create_campaign", path="./create_campaign.py", args={}) ]) ) # Create the campaign directory structure my_campaign.create_campaign() # Print campaign directory print(f"Campaign directory created: {os.path.abspath(campaign_dir)}") ``` -------------------------------- ### Setting up the Adaptive Sampler Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/dimension_adaptive_tutorial.ipynb.txt Configures the adaptive sampler with specific strategies and parameters. This example uses a Sobol sampler with adaptive refinement. ```python sampler = uq.Sampling.sobol_adaptive(num_vars=10, max_adaptive_level=3, num_samples=1000) campaign.add_sampling(sampler) # Define the model to be run # In a real scenario, this would be a callable function or a script def my_model(x): # Example: a simple function that sums the inputs return sum(x.values()) # Add the model to the campaign campaign.add_model(model=uq.models.FunctionModel(my_model), params=params) ``` -------------------------------- ### EasyVVUQ Installation Check Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/dimension_adaptive_tutorial.ipynb.txt Verifies the installation of EasyVVUQ and its dependencies. This snippet shows the output of a successful installation check. ```python Requirement already satisfied: EasyVVUQ in /home/wouter/.local/lib/python3.9/site-packages (1.2.3.dev74+g31b69069.d20241205) Requirement already satisfied: numpy<2 in /home/wouter/anaconda3/lib/python3.9/site-packages (from EasyVVUQ) (1.26.4) Requirement already satisfied: pandas in /home/wouter/anaconda3/lib/python3.9/site-packages (from EasyVVUQ) (2.2.2) Requirement already satisfied: scipy in /home/wouter/anaconda3/lib/python3.9/site-packages (from EasyVVUQ) (1.12.0) Requirement already satisfied: wheel in /home/wouter/anaconda3/lib/python3.9/site-packages/setuptools/_vendor (from EasyVVUQ) (0.43.0) Requirement already satisfied: chaospy==4.3.2 in /home/wouter/.local/lib/python3.9/site-packages/chaospy-4.3.2-py3.9.egg (from EasyVVUQ) (4.3.2) ``` -------------------------------- ### Install EasyVVUQ using pip Source: https://easyvvuq.readthedocs.io/en/latest/_sources/installation.rst.txt Use this command to install the latest stable version of EasyVVUQ from PyPI. Ensure you have Python 3.8+ installed. ```bash pip install easyvvuq ``` -------------------------------- ### Set up EasyVVUQ Campaign Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_MC_tutorial.ipynb.txt Initializes an EasyVVUQ campaign for Monte Carlo analysis. This involves specifying the model, sampling method, and output file path. The campaign is then run. ```python from easyvvuq import Campaign, Decorators from easyvvuq.decoders import BasicDecoder # Define the campaign directory campaign_dir = "campaign_ishigami_mc" # Create a campaign object my_campaign = Campaign(campaign_dir=campaign_dir, work_dir=campaign_dir) # Define the decoder for the model output my_campaign.add_decoder(BasicDecoder(encoder=None, decoder=None)) # Add the Ishigami function as the model my_campaign.add_model(model=ishigami, name="ishigami_model") # Set up the Monte Carlo sampling strategy # We will use 10000 samples for the Monte Carlo simulation my_campaign.set_sampling_type(name="MC", n_samples=10000) # Set the input parameters and their distributions # The Ishigami function takes 3 inputs, assumed to be uniformly distributed between -pi and pi my_campaign.set_input_params(x1=EasyVVUQ.Normal(0, 1), x2=EasyVVUQ.Normal(0, 1), x3=EasyVVUQ.Normal(0, 1)) # Run the campaign my_campaign.run() # Wait for the campaign to finish my_campaign.wait_for_runs() ``` -------------------------------- ### Install and Import FiPy Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/easyvvuq_fusion_SC_dask_tutorial.html Checks if the 'fipy' library is installed and installs it if necessary. FiPy is a finite volume PDE solver used in the fusion model. ```python # we need fipy -- install if not already available try: import fipy except ModuleNotFoundError: ! pip install future ! pip install fipy import fipy ``` -------------------------------- ### Load Configuration Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial.ipynb.txt Shows how to load a configuration file. This is useful for setting up complex experiments. ```python from easyvvuq.config import Config config = Config.from_file("config.json") print(config) ``` -------------------------------- ### Basic Setup for Dimension Adaptive Sampling Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/dimension_adaptive_tutorial.ipynb.txt Initializes the EasyVVUQ environment and sets up a basic configuration for dimension adaptive sampling. Ensure necessary libraries are imported. ```python import easyvvuq as uq from easyvvuq.tools import Decoder, Encoder import numpy as np # Define the campaign directory campaign_dir = "campaign_adaptive_sampling" # Create a campaign object campaign = uq.Campaign(name=campaign_dir, work_dir=campaign_dir) # Define the decoder and encoder decoder = Decoder() encoder = Encoder() # Define the model model = uq.Model(name="my_model", encoder=encoder, decoder=decoder) campaign.add_model(model) # Define the sampler (e.g., adaptive sampling) sampler = uq.adaptive.AdaptiveSampler(campaign, model, "sobol") # Set up the campaign campaign.set_sampler(sampler) campaign.setup() ``` -------------------------------- ### Setting up a Simple Workflow Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial.ipynb.txt Configure a basic workflow for your campaign. This includes defining the actions to be performed, such as running simulations or analyzing results. ```python workflow = uq.Workflow(campaign, params) workflow.add_action(uq.actions.actions.CreateRunDir()) ``` -------------------------------- ### Set up Sobol sampler Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_MC_tutorial.ipynb.txt Initializes a Sobol sampler for uncertainty quantification. This sampler is suitable for global sensitivity analysis and requires defining the input variables and their distributions. ```python from easyvvuq.samplers import Sobol import numpy as np # Define the input variables and their distributions input_vars = { 'x0': np.linspace(-np.pi, np.pi, 100), 'x1': np.linspace(-np.pi, np.pi, 100), 'x2': np.linspace(-np.pi, np.pi, 100), 'x3': np.linspace(-np.pi, np.pi, 100) } sampler = Sobol(input_vars=input_vars) ``` -------------------------------- ### Start and Monitor Execution Statuses Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/mega_tutorial.html Starts the execution of submitted jobs and prints the progress. ```python statuses.start() print(statuses.progress()) statuses.actions ``` -------------------------------- ### Initialize Campaign and Sampler Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial_qcgpj.ipynb.txt Initializes a new campaign with specified parameters and actions, and sets up a SCSampler for the campaign. This is often a prerequisite for executing campaign actions. ```python campaign = uq.Campaign(name='beam', params=params, actions=actions) campaign.set_sampler(uq.sampling.SCSampler(vary=vary, polynomial_order=1)) ``` -------------------------------- ### Setting up Monte Carlo Sampler and Executing Campaign Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_MC_tutorial.ipynb.txt Configures a Monte Carlo sampler using the Sobol sequence for uncertainty quantification. Executes the campaign sequentially and collates results with a progress bar. ```python # Latin HyperCube sampler = uq.sampling.MCSampler(vary=define_vary(), n_mc_samples=n_mc_samples, rule='sobol') # QMC method with Sobol sequence #sampler = uq.sampling.QMCSampler(vary=define_vary(), n_mc_samples=n_mc_samples) my_campaign.set_sampler(sampler) # Will draw all (of the finite set of samples) my_campaign.draw_samples() print('Number of samples = %s' % my_campaign.get_active_sampler().count) time_end = time.time() times[2] = time_end-time_start print('Time for phase 2 = %.3f' % (times[2])) time_start = time.time() # Run the cases my_campaign.execute(sequential=True).collate(progress_bar=True) time_end = time.time() times[3] = time_end-time_start print('Time for phase 3 = %.3f' % (times[3])) time_start = time.time() # Get the results results_df = my_campaign.get_collation_result() time_end = time.time() times[4] = time_end-time_start print('Time for phase 4 = %.3f' % (times[4])) time_start = time.time() # Post-processing analysis results = my_campaign.analyse(qoi_cols=["Ishigami"]) time_end = time.time() times[5] = time_end-time_start print('Time for phase 5 = %.3f' % (times[5])) time_start = time.time() # Save the results #pickle.dump(results, open('Ishigami_results.pickle','bw')) time_end = time.time() times[6] = time_end-time_start print('Time for phase 6 = %.3f' % (times[6])) # times[0] = time_end - time_start_whole return results_df, results, times, n_mc_samples, my_campaign.get_active_sampler().count ``` -------------------------------- ### Basic Plotting Example Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial.ipynb.txt A simple example of plotting simulation outputs against a single input parameter. ```python import matplotlib.pyplot as plt results.plot(x='input_param', y='output_value', title='Input vs Output') plt.show() ``` -------------------------------- ### Set up EasyVVUQ Campaign Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_MC_tutorial.ipynb.txt Initializes an EasyVVUQ campaign for uncertainty quantification. This involves defining the model, parameters, and the type of analysis (e.g., Monte Carlo). ```python import easyvvuq as uq import numpy as np # Define the Ishigami function def ishigami(x): # Ishigami function # x is a list of 3 values [x, y, z] return (np.sin(x[0]) + 0.7 * np.sin(x[1])**2 + 0.1 * x[2]**4 * np.sin(x[0])) # Create a campaign object my_campaign = uq.Campaign(name='ishigami_mc', work_dir='work') # Define the model my_model = uq.Model(name='ishigami', model_type='python_function', input_func=ishigami) # Add the model to the campaign my_campaign.add_model(my_model) # Define the parameters for the Ishigami function # The Ishigami function has 3 parameters: x, y, z # Each parameter is defined with a name, distribution, and bounds params = [{"name": "x", "distribution": "uniform", "lower": -np.pi, "upper": np.pi}, {"name": "y", "distribution": "uniform", "lower": -np.pi, "upper": np.pi}, {"name": "z", "distribution": "uniform", "lower": -np.pi, "upper": np.pi}] # Set the parameters for the campaign my_campaign.set_campaign_params(params) # Create the runs for the campaign # This will generate the input files for the Monte Carlo simulation my_campaign.create_runs(runs_dir='runs', n_samples=1000) ``` -------------------------------- ### Example Output of Drawn Samples Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/correlated/coffe_cup-fd.html This is an example of the output generated after drawing samples, showing temperature and kappa values. ```text temperature,kappa 20.000000,0.050000 20.225000,0.051500 19.775000,0.048500 20.360000,0.050000 19.640000,0.050000 ``` -------------------------------- ### Install EasyVVUQ Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial_qcgpj.ipynb.txt Installs the EasyVVUQ library and its dependencies, including QCG-PilotJob, using pip. Ensure you have a compatible Python environment. ```python import easyvvuq as uq import chaospy import numpy as np from easyvvuq.runners import QCGPilotJobRunner from easyvvuq.decoders import JPetDecoder # Create a campaign object campaign = uq.Campaign(name='qcg_pilotjob_tutorial', workdir='campaign_workdir') # Define the parameters for the campaign params = { 'input_file': uq.Variable(value='input.txt'), 'param1': uq.Variable(value=np.linspace(0, 1, 10)), 'param2': uq.Variable(value=np.linspace(0, 1, 10)) } # Create a "simulation" action simulation = uq.actions.Action(name='simulation', params=params, encoder=uq.encoders.GenericEncoder(input_template='input.txt', delimiter='='), decoder=JPetDecoder()) campaign.add_action(simulation) campaign.populate_runs() # Define the QCG-PilotJob runner runner = QCGPilotJobRunner(pilot_job_script='pilot_job.sh', pilot_job_walltime='00:10:00', pilot_job_nodes=1, pilot_job_cores_per_node=2, pilot_job_queue='debug') campaign.set_runner(runner) campaign.run() campaign.collate() campaign.truncate() ``` -------------------------------- ### Set up EasyVVUQ workflow Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_SC_tutorial.ipynb.txt Initializes the EasyVVUQ workflow by defining the model, input parameters, and sampling strategy. This sets the stage for running the sensitivity analysis. ```python from easyvvuq.actions import Action, ActionCollection from easyvvuq.core import Input, Output, Campaign from easyvvuq.decoders import BasicDecoder from easyvvuq.samplers import SaltelliSampler # Define the model action model_action = Action(name='ishigami_model', function=ishigami) # Define the input parameters for the Ishigami function input_params = [ {'name': 'x0', 'type': 'float', 'distribution': 'uniform', 'lower': -np.pi, 'upper': np.pi}, {'name': 'x1', 'type': 'float', 'distribution': 'uniform', 'lower': -np.pi, 'upper': np.pi}, {'name': 'x2', 'type': 'float', 'distribution': 'uniform', 'lower': -np.pi, 'upper': np.pi}, {'name': 'x3', 'type': 'float', 'distribution': 'uniform', 'lower': -np.pi, 'upper': np.pi} ] # Define the output of the model output_params = ['y'] # Create an Input object input_obj = Input(input_params=input_params, output_params=output_params) # Define the sampler sampler = SaltelliSampler(num_levels=4) # Create an ActionCollection actions = ActionCollection(model_action=model_action, decoder=BasicDecoder()) # Create a Campaign object campaign = Campaign(name='ishigami_campaign', actions=actions, input_obj=input_obj, sampler=sampler) ``` -------------------------------- ### Ishigami parameter file example Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_tutorial.ipynb.txt This is an example of a parameter file used to define the input parameters for the Ishigami function in EasyVVUQ. ```text names = ['x1', 'x2', 'x3'] # Bounds for the parameters # x1: [-pi, pi] # x2: [-pi, pi] # x3: [-pi, pi] # Ishigami function parameters # a = 7, b = 0.1 # Example bounds (adjust as needed) bounds = [ [-3.141592653589793, 3.141592653589793], [-3.141592653589793, 3.141592653589793], [-3.141592653589793, 3.141592653589793] ] ``` -------------------------------- ### Initialize and Configure Campaign Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/easyvvuq_fusion_tutorial.html Initialize a new EasyVVUQ campaign named 'fusion_pce.' and add the fusion application with its defined parameters and actions. ```python # Set up a fresh campaign called "fusion_pce." my_campaign = uq.Campaign(name='fusion_pce.') # Add the app (automatically set as current app) my_campaign.add_app(name="fusion", params=params, actions=actions) ``` -------------------------------- ### Actions Class Start Method Source: https://easyvvuq.readthedocs.io/en/latest/_modules/easyvvuq/actions/execute_local.html Iterates through the list of actions, applying a wrapper function to each. It ensures each action has a 'start' method before proceeding. ```python def start(self, previous=None): for action in self.actions: if not hasattr(action, 'start'): raise RuntimeError('action in the actions list does not provide a start method') previous = copy.copy(previous) run_id = previous['run_id'] for action in self.actions: previous = self.wrapper(action, previous) self.result = previous assert (self.result['run_id'] == run_id) return previous ``` -------------------------------- ### Set up Sobol sampler Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_MC_tutorial.ipynb.txt Initializes a Sobol sampler for uncertainty quantification. This sampler is used to estimate Sobol indices, which quantify the contribution of each input variable to the output variance. ```python from easyvvuq.samplers import Sobol sampler = Sobol(model, n_samples=1000, num_levels=4) ``` -------------------------------- ### ActionPool Start Method Source: https://easyvvuq.readthedocs.io/en/latest/_modules/easyvvuq/actions/action_statuses.html Starts the execution of actions using a provided or default ThreadPoolExecutor. Returns the ActionPool instance for progress tracking. ```python def start(self, pool=None): if pool is None: pool = ThreadPoolExecutor() self.pool = pool for previous in self.inits: previous = copy.copy(previous) if self.sequential: result = self.actions.start(previous) self.results.append(result) else: future = self.pool.submit(self.actions.start, previous) self.futures.append(future) return self ``` -------------------------------- ### Example Output of Best Hyperparameters Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/hyperparameter_tuning_tutorial.html This is an example of the output format when printing the best hyperparameters and test accuracy. It shows the achieved accuracy percentage. ```text Best hyperparameters with 97.36% test accuracy: ``` -------------------------------- ### Initialize a Model Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial.ipynb.txt Demonstrates how to initialize a model with default parameters. Ensure you have the necessary imports. ```python from easyvvuq.modules.model import Model model = Model() print(model) ``` -------------------------------- ### Example Simulation Output Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/basic_tutorial.html An example of the JSON output from the beam simulation, showing the vertical displacement ('g1') and displaced angles ('g2', 'g3'). ```json {"g1": -6.909453505549654e-06, "g2": -1.3818907011099308e-05, "g3": 1.7273633763874136e-05} ``` -------------------------------- ### Set up the campaign for EasyVVUQ Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_SC_tutorial.ipynb.txt This code configures the campaign for EasyVVUQ, specifying the model, input parameters, and output variables. It prepares the environment for running sensitivity analysis. ```python import easyvvuq as uq import numpy as np # Define the Ishigami function def ishigami(x): return np.sin(x[0]) + 7.0 * np.sin(x[1])**2 + 0.1 * x[2]**4 * np.sin(x[0]) # Define the campaign module = uq.Module(name='ishigami', file='campaign.py', file_content=f'def ishigami(x):\n return np.sin(x[0]) + 7.0 * np.sin(x[1])**2 + 0.1 * x[2]**4 * np.sin(x[0])') # Define the input parameters for the Ishigami function params = [{"name": "x0", "type": "float", "distribution": "uniform", "lower": -np.pi, "upper": np.pi}, {"name": "x1", "type": "float", "distribution": "uniform", "lower": -np.pi, "upper": np.pi}, {"name": "x2", "type": "float", "distribution": "uniform", "lower": -np.pi, "upper": np.pi}] # Define the output variable output_vars = ["y"] # Create the campaign object campaña = uq.Campaign(name='ishigami_campaign', model=module, input_params=params, output_vars=output_vars) ``` -------------------------------- ### Example of Dimension Adaptive Class Usage Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/dimension_adaptive_tutorial.ipynb.txt Demonstrates a complete workflow using the DimensionAdaptive class, from loading to saving results. This is a comprehensive example for practical application. ```python from easyvvuq.dimension_adaptive import DimensionAdaptive data = DimensionAdaptive.load_data("path/to/your/data.csv") transformed_data = data.transform(method="pca", n_components=10) analysis_results = transformed_data.analyze(model="linear_regression") DimensionAdaptive.save_results(analysis_results, "path/to/save/results.json") ``` -------------------------------- ### Initialize and configure the MCMC sampler Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/mcmc_tutorial.html Sets up the MCMC sampler with initial values, proposal distribution, likelihood function, and an estimator for the mean. A ReplicaSampler is also configured. ```python mcmc_sampler = uq.sampling.MCMCSampler( vary_init, proposal, 'Values', n_chains=5, likelihood=get_likelihood(observed), estimator=lambda x: np.mean(x)) sampler = uq.sampling.ReplicaSampler(mcmc_sampler, replicas=20) campaign.set_sampler(sampler) ``` -------------------------------- ### Vector Quantization Example Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/vector_qoi_tutorial.ipynb.txt A complete example demonstrating the VQ process: generating a codebook, encoding data, and then decoding it back. This illustrates the lossy compression nature of VQ. ```python # Example Usage: # Generate some sample data np.random.seed(0) data = np.random.rand(100, 2) # 100 data points, 2 dimensions # Define number of codewords num_codewords = 10 # Generate codebook codebook = generate_codebook(data, num_codewords) # Encode data encoded_data = encode_data(data, codebook) # Decode data decoded_data = decode_data(encoded_data, codebook) # Calculate reconstruction error (optional) reconstruction_error = np.mean(np.sum((data - decoded_data)**2, axis=1)) print(f"Reconstruction Error: {reconstruction_error}") ``` -------------------------------- ### Task Template Structure Example Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/basic_tutorial_qcgpj.html This is an example of a task template structure, showing the keys for execution details and resource allocation. Keys like 'name', 'stdout', and 'stderr' are mandatory. ```json { 'name': '${name}', 'execution': { 'exec': '${exec}', 'args': ${args}, 'stdout': '${stdout}', 'stderr': '${stderr}', 'venv': '${venv}', 'model': '${model}', 'model_opts': ${model_opts} }, 'resources': { 'numCores': { 'exact': ${numCores} }, 'numNodes': { 'exact': ${numNodes} } } } ``` -------------------------------- ### Set up MCMC Sampler Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_mcmc.ipynb.txt Initialize an MCMC sampler with the defined prior and likelihood. ```python sampler = easyuvq.MCMC(prior=prior, likelihood=likelihood) ``` -------------------------------- ### Get First Order Derivative-Based Sensitivity Indices Source: https://easyvvuq.readthedocs.io/en/latest/_modules/easyvvuq/analysis/results.html Retrieve first-order derivative-based sensitivity indices. Can be called with no arguments to get all indices, or with specific quantity of interest (qoi) and/or input parameters. ```python >>> results.derivatives_first() {'f': {'x1': array([0.610242]), 'x2': array([0.26096511])}} ``` ```python >>> results.derivatives_first('f') {'x1': array([0.610242]), 'x2': array([0.26096511])} ``` ```python >>> results.derivatives_first('f', 'x1') array([0.610242]) ``` ```python >>> results_vectors.derivatives_first(('g', 2)) {'x1': array([0.5]), 'x2': array([0.5])} ``` -------------------------------- ### Setup Campaign Actions Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/jinja_tutorial.html Configures the necessary actions for the campaign, including encoding inputs, executing the simulation locally, and decoding the JSON output. ```python decoder = uq.decoders.JSONDecoder(target_filename='output.json', output_columns=['g1']) execute = ExecuteLocal('{}/beam input.json'.format(os.getcwd())) actions = Actions(CreateRunDirectory('/tmp', flatten=True), Encode(encoder), execute, Decode(decoder)) ``` -------------------------------- ### Campaign.get_campaign_runs_dir() Source: https://easyvvuq.readthedocs.io/en/latest/api.html Gets the directory for campaign runs. ```APIDOC ## Campaign.get_campaign_runs_dir() ### Description Gets the directory for campaign runs. ### Method `Campaign.get_campaign_runs_dir()` ``` -------------------------------- ### run Source: https://easyvvuq.readthedocs.io/en/latest/_modules/easyvvuq/db/base.html Get the information for a specified run. ```APIDOC ## run ### Description Get the information for a specified run. ### Parameters #### Path Parameters - **run_name** (str) - Required - Name of run to filter for. - **campaign** (int or None) - Optional - Campaign id to filter for. - **sampler** (int or None) - Optional - Sample id to filter for. ### Returns - **dict** - Containing run information (run_name, params, status, sample, campaign, app) ``` -------------------------------- ### Set up UQ workflow with EasyVVUQ Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_SC_tutorial.ipynb.txt This Python code initializes a UQ workflow using EasyVVUQ, defining the model, parameters, and the campaign for sensitivity analysis. It requires the 'easyVVUQ' library. ```python from easyVVUQ.actions import SensitivityAnalysis from easyVVUQ.actions.sensitivity import SobolAnalysis from easyVVUQ.actions.model import Model from easyVVUQ.actions.parameter import ParameterDistribution # Define the model model = Model(ishigami_sobol) # Define the parameter space params = dict() params['x0'] = ParameterDistribution(np.linspace(-np.pi, np.pi, 100)) params['x1'] = ParameterDistribution(np.linspace(-np.pi, np.pi, 100)) params['x2'] = ParameterDistribution(np.linspace(-np.pi, np.pi, 100)) # Create a sensitivity analysis campaign sa = SensitivityAnalysis(model, params, SobolAnalysis) sa.campaign.get_full_design() ``` -------------------------------- ### get_campaign_runs_dir Source: https://easyvvuq.readthedocs.io/en/latest/_autodoc/easyvvuq.html Gets the directory where campaign runs are stored. ```APIDOC ## get_campaign_runs_dir() ### Description Get the runs directory from the CampaignDB. ### Returns Path in which the runs information will be written. ### Return type str ``` -------------------------------- ### Initialize Campaign and DB Info Source: https://easyvvuq.readthedocs.io/en/latest/_modules/easyvvuq/db/sql.html Initializes a new campaign in the database, checking for version compatibility. Adds campaign and DB info tables. ```python is_db_empty = (self.session.query(CampaignTable).first() is None) version_check = self.session.query( CampaignTable).filter(CampaignTable.easyvvuq_version != info.easyvvuq_version).all() if (not is_db_empty) and (len(version_check) != 0): raise RuntimeError('Database contains campaign created with an incompatible' + ' version of EasyVVUQ!') self._next_run = 1 self.session.add(CampaignTable(**info.to_dict(flatten=True))) self.session.add(DBInfoTable(next_run=self._next_run)) self.session.commit() ``` -------------------------------- ### Get Adaptation Errors Source: https://easyvvuq.readthedocs.io/en/latest/_modules/easyvvuq/analysis/sc_analysis.html Returns the 'adaptation_errors' attribute of the object. ```python def get_adaptation_errors(self): """Returns self.adaptation_errors """ return self.adaptation_errors ``` -------------------------------- ### Basic Usage of Dimension Adaptive Methods Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/dimension_adaptive_tutorial.ipynb.txt Illustrates the fundamental setup for employing dimension adaptive strategies. Ensure necessary imports are present before execution. ```python import easyvvuq as uq from easyvvuq.actions import DimensionAdaptiveAction # Setup campaign campaign = uq.Campaign(name='dimension_adaptive_tutorial') # Define actions action = DimensionAdaptiveAction(campaign, 'my_action') action.add_actions(uq.actions.Action(campaign, 'my_action_1')) action.add_actions(uq.actions.Action(campaign, 'my_action_2')) # Add actions to campaign campaign.add_action(action) # Run campaign campaign.run() # Collect results campaign.collect() ``` -------------------------------- ### Set up Monte Carlo simulation Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/easyvvuq_Ishigami_MC_tutorial.ipynb.txt Initializes the EasyVVUQ sampler for Monte Carlo simulation. This involves specifying the model, parameters, and the number of samples. ```python from easyvvuq import Campaign, Decorators, Output,வுகளை from easyvvuq.decoders import CSVDecoder import numpy as np # Define the Ishigami function def ishigami(x): return np.sin(x[0]) + 7.0 * np.sin(x[1])**2 + 0.1 * x[2]**4 * np.sin(x[0]) # Define the campaign campaign = Campaign(name='ishigami_mc', work_dir='work') # Define the decoder campaign.add_decoder(CSVDecoder(header=False, columns=['f'] )) # Define the model campaign.add_model(model_name='ishigami', model_executable=ishigami, input_template=None, output_filename='output.csv') # Define the parameters params = [ {'name': 'a', 'type': 'float', 'distribution': 'uniform', 'lower': -np.pi, 'upper': np.pi}, {'name': 'b', 'type': 'float', 'distribution': 'uniform', 'lower': -np.pi, 'upper': np.pi}, {'name': 'c', 'type': 'float', 'distribution': 'uniform', 'lower': -np.pi, 'upper': np.pi} ] # Add parameters to the campaign campaign.add_params(params) # Set up the sampler sampler = campaign.get_sampler(method='uniform', n_samples=1000) sampler.set_sampling_workflow() sampler.draw_samples() ``` -------------------------------- ### Plot Results Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial.ipynb.txt Shows how to plot the results. Requires matplotlib to be installed. ```python import matplotlib.pyplot as plt results.plot(x='input_param', y='output_value') plt.show() ``` -------------------------------- ### Create and Configure an EasyVVUQ Campaign Source: https://easyvvuq.readthedocs.io/en/latest/tutorials/mega_tutorial.html Initializes an EasyVVUQ campaign with execution actions. This sets up the simulation environment and defines how runs will be executed. ```python from easyvvuq import Campaign, Actions, ExecuteLocal, Encode, Decode import os execute = ExecuteLocal('{}/epidemic/epidemic.py epidemic_in.json'.format(os.getcwd())) actions = Actions(CreateRunDirectory('/tmp'), Encode(encoder), execute, Decode(decoder)) campaign = Campaign(name='epidemic_basic', params=params, actions=actions) print(campaign) ``` -------------------------------- ### Get All Results Source: https://easyvvuq.readthedocs.io/en/latest/_sources/tutorials/basic_tutorial.ipynb.txt Retrieves all collected results from the campaign after it has finished running. ```python all_results = campaign.get_all_results() print(all_results) ```