{ "cells": [ { "cell_type": "markdown", "id": "needed-watch", "metadata": {}, "source": [ "# EasyVVUQ in R\n", "\n", "**Louise Kimpton**, Vytautas Jancauskas" ] }, { "cell_type": "markdown", "id": "activated-devices", "metadata": {}, "source": [ "In this tutorial we will use the ```reticulate``` package in R to interface with EasyVVUQ. This tutorial is similar to the basic tutorial in EasyVVUQ. The idea here is that you would use R to generate a sampling plan for EasyVVUQ to manage and execute and then retrieve the simulation results back into R. To this end we don't use any of the EasyVVUQ sampling and analysis tools." ] }, { "cell_type": "markdown", "id": "focused-distance", "metadata": {}, "source": [ "We will use the [reticulate](https://rstudio.github.io/reticulate/) package which allows one to interact with a running Python instance and exchange data frames between R and pandas." ] }, { "cell_type": "code", "execution_count": 1, "id": "powered-adoption", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Updating HTML index of packages in '.Library'\n", "\n", "Making 'packages.html' ...\n", " done\n", "\n" ] } ], "source": [ "install.packages(\"reticulate\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "dirty-vintage", "metadata": {}, "outputs": [], "source": [ "library(\"reticulate\")" ] }, { "cell_type": "markdown", "id": "chubby-delta", "metadata": {}, "source": [ "We then import the easyvvuq packages we will need in this tutorial." ] }, { "cell_type": "code", "execution_count": 3, "id": "concerned-ratio", "metadata": {}, "outputs": [], "source": [ "uq <- import('easyvvuq')\n", "actions <- import('easyvvuq.actions')\n", "sampling <- import('easyvvuq.sampling')" ] }, { "cell_type": "markdown", "id": "exciting-collect", "metadata": {}, "source": [ "We define input parameters as we would normally." ] }, { "cell_type": "code", "execution_count": 4, "id": "crucial-shooting", "metadata": {}, "outputs": [], "source": [ "params <- dict(\n", " 'F' = dict('type' = 'float', 'default' = 1.0),\n", " 'L' = dict('type' = 'float', 'default' = 1.5),\n", " 'a' = dict('type' = 'float', 'min' = 0.7, 'max' = 1.2, 'default' = 1.0),\n", " 'D' = dict('type' = 'float', 'min' = 0.75, 'max' = 0.85, 'default' = 0.8),\n", " 'd' = dict('type' = 'float', 'default' = 0.1),\n", " 'E' = dict('type' = 'float', 'default' = 200000),\n", " 'outfile' = dict('type' = 'string', 'default' = 'output.json')\n", ")" ] }, { "cell_type": "markdown", "id": "floating-headset", "metadata": {}, "source": [ "Creating the campaign is again the same." ] }, { "cell_type": "code", "execution_count": 5, "id": "following-publisher", "metadata": {}, "outputs": [], "source": [ "encoder <- uq$encoders$GenericEncoder(template_fname='beam.template', delimiter='$', target_filename='input.json')\n", "decoder <- uq$decoders$JSONDecoder(target_filename='output.json', output_columns=list('g1')) \n", "execute <- actions$ExecuteLocal(paste(getwd(), '/beam input.json', sep=\"\"))\n", "actions <- actions$Actions(actions$CreateRunDirectory('/tmp'), \n", " actions$Encode(encoder), execute, actions$Decode(decoder))" ] }, { "cell_type": "code", "execution_count": 6, "id": "central-colon", "metadata": {}, "outputs": [], "source": [ "campaign <- uq$Campaign(name='beam', params=params, actions=actions)" ] }, { "cell_type": "markdown", "id": "individual-knight", "metadata": {}, "source": [ "We specify a data frame with the points we will use to evaluate the model on. In this case three points will be evaluated. This data frame represents a sampling plan as required to use whatever analysis method you want." ] }, { "cell_type": "code", "execution_count": 7, "id": "nonprofit-edward", "metadata": {}, "outputs": [], "source": [ "df <- data.frame(\n", " 'F' = c(1.145, 1.029, 0.961),\n", " 'L' = c(1.495, 1.508, 1.501),\n", " 'a' = c(0.782, 0.899, 1.141),\n", " 'D' = c(0.795, 0.802, 0.816)\n", ")" ] }, { "cell_type": "markdown", "id": "demonstrated-charter", "metadata": {}, "source": [ "This is then used as an argument to ```DataFrameSampler```." ] }, { "cell_type": "code", "execution_count": 8, "id": "neural-roberts", "metadata": {}, "outputs": [], "source": [ "campaign$set_sampler(sampling$DataFrameSampler(df=df))" ] }, { "cell_type": "code", "execution_count": 9, "id": "graphic-table", "metadata": {}, "outputs": [], "source": [ "campaign$execute()$collate()" ] }, { "cell_type": "markdown", "id": "affected-century", "metadata": {}, "source": [ "Finally we can retrieve the results as usual." ] }, { "cell_type": "code", "execution_count": 10, "id": "executive-maria", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
A data.frame: 3 × 10
run_id 0iteration 0F 0L 0a 0D 0d 0E 0outfile 0g1 0
<dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><chr><dbl>
101.1451.4950.7820.7950.12e+05output.json-1.012152e-05
201.0291.5080.8990.8020.12e+05output.json-8.395071e-06
300.9611.5011.1410.8160.12e+05output.json-4.137187e-06
\n" ], "text/latex": [ "A data.frame: 3 × 10\n", "\\begin{tabular}{llllllllll}\n", " run\\_id 0 & iteration 0 & F 0 & L 0 & a 0 & D 0 & d 0 & E 0 & outfile 0 & g1 0\\\\\n", " & & & & & & & & & \\\\\n", "\\hline\n", "\t 1 & 0 & 1.145 & 1.495 & 0.782 & 0.795 & 0.1 & 2e+05 & output.json & -1.012152e-05\\\\\n", "\t 2 & 0 & 1.029 & 1.508 & 0.899 & 0.802 & 0.1 & 2e+05 & output.json & -8.395071e-06\\\\\n", "\t 3 & 0 & 0.961 & 1.501 & 1.141 & 0.816 & 0.1 & 2e+05 & output.json & -4.137187e-06\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "A data.frame: 3 × 10\n", "\n", "| run_id 0 <dbl> | iteration 0 <dbl> | F 0 <dbl> | L 0 <dbl> | a 0 <dbl> | D 0 <dbl> | d 0 <dbl> | E 0 <dbl> | outfile 0 <chr> | g1 0 <dbl> |\n", "|---|---|---|---|---|---|---|---|---|---|\n", "| 1 | 0 | 1.145 | 1.495 | 0.782 | 0.795 | 0.1 | 2e+05 | output.json | -1.012152e-05 |\n", "| 2 | 0 | 1.029 | 1.508 | 0.899 | 0.802 | 0.1 | 2e+05 | output.json | -8.395071e-06 |\n", "| 3 | 0 | 0.961 | 1.501 | 1.141 | 0.816 | 0.1 | 2e+05 | output.json | -4.137187e-06 |\n", "\n" ], "text/plain": [ " run_id 0 iteration 0 F 0 L 0 a 0 D 0\n", "1 1 0 1.145 1.495 0.782 0.795 \n", "2 2 0 1.029 1.508 0.899 0.802 \n", "3 3 0 0.961 1.501 1.141 0.816 \n", " d 0 E 0 outfile 0 g1 0 \n", "1 0.1 2e+05 output.json -1.012152e-05\n", "2 0.1 2e+05 output.json -8.395071e-06\n", "3 0.1 2e+05 output.json -4.137187e-06" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "campaign$get_collation_result()" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.0.2" } }, "nbformat": 4, "nbformat_minor": 5 }