Branch creator: Difference between revisions
(temporary step 3 added) |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 26: | Line 26: | ||
=== Step 2: Fill in data about your branch === | === Step 2: Fill in data about your branch === | ||
In step 2, you can create a new branch page or add more ovariables to an existing page. Fill in the | In step 2, you can create a new branch page or add more ovariables to an existing page. Fill in the forms below, click to create content for the page, and copy-paste that onto the page you created in step 1. You can create the main content of an entire branch page, a new ovariable that takes data as input, and a new ovariable that takes a formula as input. | ||
Each branch is a model that consists of sub-models called ovariables. If you | Each branch is a model that consists of sub-models called ovariables. If you don´t know what they are, you should first read pages [[Ovariable]] and [[Modelling in Opasnet]]. | ||
If you need more ovariables for your branch model, you can simply rerun this code. Just remember to copy-paste the previous content first, as it will be replaced by the new ovariables. Each ovariable has its own sub-heading under the heading "Rationale". | If you need more ovariables for your branch model, you can simply rerun this code. Just remember to copy-paste the previous content first, as it will be replaced by the new ovariables. Each ovariable has its own sub-heading under the heading "Rationale". | ||
This is a brief description of input. For details, see the linked pages. | |||
;Branch name: Name of the page where the content and code goes. This name may contain several words and spaces, and it is recommended that it has the word 'branch' in it. | |||
;Names of ovariables: Name of a new ovariable must be a single word with no spaces. It may contain numbers and '_' and '.'. Preferably it is short but easily understandable. | |||
;Output variable: The final ovarable in your branch model. Other ovariables are used as inputs for this ovariable. | |||
;Unit: The unit of measurement that is used for the input data. | |||
;Data table: Data used by the data ovariable. It is a copy-pasted table where the first row contains the names of the columns and all other rows are the data. There must be one or more result columns, i.e. columns that contain the actual result of the ovariable; all other columns either define the scope of the result (index columns) or describe the content of the row (description columns). | |||
:* There are three ways to define the result column(s): (i) the name of the column is "Result", (ii) the result column names have two parts separated by colon (see details below), (iii) if result column has not been defined, the last column is used as the result column. | |||
:* All columns left from the result column(s) are treated as index columns. All columns right from the result column(s) are treated as description columns. | |||
:* There can be several result columns so that each column is about a specified location, e.g. a year. Then the result column names must be divided into two parts by a colon so that the index is the first part and location is the second, e.g. Year:2010, Year:2011, Year:2013 and so on. For details, see [[Table2Base]] and [[Table2Base2]]. | |||
;Rationale for data and formula ovariables: Rationale is any free-text description of the ovariable to make it more understandable and reasoned. | |||
; Dependencies of ovariable: It is a copy-pasted table that must contain column 'Name' and may contain columns 'Ident' or 'Key'. Name lists the names of all ovariables that are needed to calculate the result of this ovariable. Ident tells the name of the codes that were used to save the ovariables and it is of format 'Op_en1234/codename' where the first part is the page identifier and the second part is the name of the code. For details, see [[R-tools]]. | |||
; Formula of ovariable: It is an [[R]] function (a particular kind of software code) that uses the dependencies and produces an output for this ovariable. You need to know something about R and [[ovariable]]s to be able to use this effectively. In brief, the code starts where all input variables have already been loaded and calculated, and ends with 'return(out)' where out is the output for this ovariable. | |||
<rcode name="fill" embed=1 label="Create content for the branch" recall_inputs=1 variables=" | <rcode name="fill" embed=1 label="Create content for the branch" recall_inputs=1 variables=" | ||
Line 37: | Line 50: | ||
name:newformula|description:Do you want to create content for a new ovariable with a formula?|type:selection|options:FALSE;No;TRUE;Yes|default:FALSE| | name:newformula|description:Do you want to create content for a new ovariable with a formula?|type:selection|options:FALSE;No;TRUE;Yes|default:FALSE| | ||
name:branchname|description:What was the branch name again?|type:text| | name:branchname|description:What was the branch name again?|type:text| | ||
name:output.ovariable|description:What again is the name of the ovariable that is the final outcome of the branch?|type:text| | |||
category:Content for a new branch page|category_conditions:newpage;TRUE| | category:Content for a new branch page|category_conditions:newpage;TRUE| | ||
name: | name:data.ovariable|description:Give a short name (no spaces!) for an ovariable with data|type:text| | ||
category:Content for a new ovariable with data|category_conditions:newdata;TRUE| | category:Content for a new ovariable with data|category_conditions:newdata;TRUE| | ||
name:t2bdata|description:Give a data table for the input variable | name:unit|description:What is(are) the unit(s) of the data?|type:text|default:-| | ||
name: | name:t2bdata|description:Give a data table for the input variable.|type:table| | ||
name: | name:data.rationale|description:Give free-text rationale about what the data ovariable is and where the data comes from.|type:textbox| | ||
name:formula.ovariable|description:Give a short name (no spaces!) to an ovariable with a formula.|type:text| | |||
category:Content for a new ovariable with formula|category_conditions:newformula;TRUE| | category:Content for a new ovariable with formula|category_conditions:newformula;TRUE| | ||
name:dependencies|description:What are the input (parent or upstream) ovariables for this ovariable? | name:dependencies|description:What are the input (parent or upstream) ovariables for this ovariable?|type:table| | ||
name:formula|description:What is the formula that defines the calculations with the parents?|type:textbox| | name:formula|description:What is the formula that defines the calculations with the parents?|type:textbox| | ||
name: | name:formula.rationale|description:Give free-text rationale about what the outcome variable is and how it is calculated.|type:textbox | ||
"> | "> | ||
# This is code Op_en7457/fill on page [[Branch creator]] | |||
# Parameters: | |||
## newpage: TRUE if a new page is being created | |||
## newdata: TRUE if a new data-based (from t2b) ovariable is being created | |||
## newformula: TRUE if a new formula-based ovariable is being created | |||
## branchname: name of the branch | |||
## output.ovariable: name of the ovariable that calculates the final output of this branch. The name is used in the Answer code. | |||
## data.ovariable: name of an ovariable that takes data as input | |||
## unit: unit(s) of the data | |||
## t2bdata: table of data to be converted in the data slot of the variable | |||
## data.rationale: textbox of description for data.ovariable | |||
## formula.ovariable: name of an ovariable based on formula and dependencies | |||
## dependencies: character vector of the names of dependencies | |||
## formula: the body of the formula (should contain the function 'return' in the end) | |||
## formula.rationale: textbox of description for formula.ovariable | |||
library(OpasnetUtils) | library(OpasnetUtils) | ||
if(newpage) { | if(newpage) { | ||
newpage <- paste("[[Category:Life cycle assessment]] | newpage <- paste("[[Category:Life cycle assessment]] | ||
[[Category:Handprinter]] | [[Category:Handprinter]] | ||
{{method|moderator=", | {{method|moderator=", | ||
wiki_username, | wiki_username, | ||
"}} | "}} | ||
", | |||
"'''{{PAGENAME}}''' is a '''branch''' of Handprinter. This page is a test case how to manage branches. | |||
== Question == | |||
How to estimate the life-cycle impacts of '''{{PAGENAME}}'''? | |||
== Answer == | |||
¤rcode embed=1 variables=' | |||
name:PAGEID|type:hidden|default:{{PAGEID}} | |||
'> | |||
# This is code Op_enXXXX/ on page [[", | |||
branchname, | |||
"]] | |||
library(OpasnetUtils) | |||
objects.latest(paste('Op_en', PAGEID, sep = ''), code_name = '", | |||
output.ovariable, | |||
"') | |||
", | |||
output.ovariable, | |||
" ¤- EvalOutput(", | |||
output.ovariable, | |||
") | |||
oprint(summary(", | |||
output.ovariable, | |||
")) | |||
¤/rcode> | |||
== Rationale == | |||
", | |||
sep = "" | |||
) | |||
} | |||
if(newdata) { | |||
# Take the data out of the input | |||
dat <- as.matrix(t2bdata[-1 , ]) | |||
dat <- apply(dat, 1, paste, collapse = "|") | |||
dat <- paste(dat, " | |||
", | |||
sep = "") | |||
" | |||
# Find parameter values for t2b tag. | |||
cols <- as.matrix(t2bdata)[1 , ] | |||
# Find column(s) with results: has name Result, has colons, or is the last one. | |||
rescols <- tolower(cols) %in% "result" | |||
" <- | if(!any(rescols)) { | ||
rescols <- grepl(":", cols) | |||
} | |||
if(!any(rescols)) { | |||
rescols <- 1:length(cols) == length(cols) | |||
} | |||
# Indices are to the left from rescols, descriptions are to the right. | |||
firstres <- min(grep(TRUE, rescols)) | |||
if(firstres > 1) indices <- cols[1:(firstres-1)] else indices <- character() | |||
lastres <- max(grep(TRUE, rescols)) | |||
if(lastres < length(cols)) desc <- cols[(lastres+1):length(cols)] else desc <- character() | |||
resnames <- strsplit(cols[rescols], split = ":") | |||
== | # If only one rescol | ||
## If first is Splittable | |||
### If first is Result then no index, last is obs else add first as index, last is locations | |||
## If nonsplittable then no index, last is obs | |||
# If more than one rescol, then first is always splittable | |||
## Add first as index, last is locations | |||
if("result" == tolower(resnames[[1]][1]) | length(resnames[[1]]) == 1) { | |||
res <- paste(c("obs='", resnames[[1]][length(resnames[[1]])], "'"), collapse = "") | |||
} else { | |||
indices <- c(indices, resnames[[1]][1]) | |||
resnames <- sapply(resnames, FUN = function(x) {return(x[length(x)])}) | |||
res <- paste(c(" locations='", paste(resnames, collapse = ","), "'"), collapse = "") | |||
} | |||
if(length(indices) > 0) { | |||
indices <- paste(c("index='", paste(indices, collapse = ","), "'"), collapse = "") | |||
} | |||
if(length(desc) > 0) { | |||
desc <- paste(c("desc='", paste(desc, collapse = ","), "'"), collapse = "") | |||
} | |||
out <- paste(c( | |||
"¤t2b", | |||
paste(c("name='", data.ovariable, "'"), collapse = ""), | |||
indices, | |||
res, | |||
desc, | |||
paste(c("unit='", unit, "'"), collapse = ""), | |||
">"), | |||
collapse = " " | |||
) | |||
out <- c(out, " | |||
", | |||
dat, "¤/t2b> | |||
") | |||
", | newdata <- paste(c( | ||
sep = "") | "=== ", | ||
data.ovariable, | |||
" === | |||
", | |||
out, | |||
"¤rcode name='", | |||
data.ovariable, | |||
"' embed=1 label='Initiate ", | |||
data.ovariable, | |||
" (for developers only)' variables=' | |||
name:PAGEID|type:hidden|default:{{PAGEID}} | |||
'> | |||
# This is code Op_enXXXX/", | |||
data.ovariable, | |||
" on page [[", | |||
branchname, | |||
"]] | |||
library(OpasnetUtils) | |||
", | |||
data.ovariable, | |||
" ¤- Ovariable('", | |||
data.ovariable, | |||
"', | |||
ddata = paste('Op_en', PAGEID, sep = ''), | |||
subset = '", | |||
data.ovariable, | |||
"') | |||
objects.store(", | |||
data.ovariable, | |||
") | |||
cat('Object',", | |||
data.ovariable, | |||
"@name, 'stored.') | |||
", | |||
"¤/rcode> | |||
", | |||
data.rationale, | |||
" | |||
"), | |||
collapse = "" | |||
) | |||
} | } | ||
if(newformula) { | if(newformula) { | ||
dep <- as.matrix(dependencies) | |||
dep <- apply(dep, 2, FUN = function(x) return(paste(c(x[1], "= c('", paste(x[-1], collapse = "', '"), "')"), collapse = ""))) | |||
dep <- paste(dep, collapse = ",\n\t") | |||
dep <- paste(c("data.frame(", dep, ")"), collapse = "\n\t") | |||
newformula <- paste( | |||
"=== ", | |||
formula.ovariable, | |||
" === | |||
¤rcode name='", | |||
formula.ovariable, | |||
"' embed=1 label='Initiate ", | |||
formula.ovariable, | |||
" (for developers only)'> | |||
# This is code Op_enXXXX/", | |||
formula.ovariable, | |||
" on page [[", | |||
branchname, | |||
"]] | |||
library(OpasnetUtils) | |||
", | |||
" | formula.ovariable, | ||
" ¤- Ovariable('", | |||
formula.ovariable, | |||
"', | |||
dependencies = ", | |||
paste( | dep, | ||
") | ", | ||
formula = function(...) { | |||
", | |||
gsub("<", "¤", formula), | |||
" } | |||
" | ) | ||
) | objects.store(", | ||
formula.ovariable, | |||
") | |||
cat('Object ',", | |||
formula.ovariable, | |||
"@name, ' stored.') | |||
¤/rcode> | |||
", | ", | ||
formula.rationale, | |||
" | " | ||
", | |||
", | sep = "" | ||
) | |||
"=== ", | |||
" === | |||
"' embed=1 label='Initiate ", | |||
" (for developers only)'> | |||
# This is code Op_enXXXX/", | |||
" on page [[", | |||
branchname, | |||
"]] | |||
library(OpasnetUtils) | |||
", | |||
" | |||
"', | |||
") | |||
objects.store(", | |||
") | |||
cat('", | |||
"stored. | |||
", | |||
" | |||
", | |||
sep = "") | |||
} | } | ||
finale <- paste( | finale <- paste( | ||
"== See also == | "== See also == | ||
* [[Handprinter]] | |||
* [[Handprinter]] | * [[Developing branches]] | ||
* [[Developing branches]] | ", | ||
", | "== Related files == | ||
"== Related files == | ", | ||
", | sep = "" | ||
sep = "") | ) | ||
text1 <- "" | text1 <- "" | ||
Line 230: | Line 291: | ||
text2 <- strsplit(text1, split = "\n")[[1]] | text2 <- strsplit(text1, split = "\n")[[1]] | ||
text2 <- data.frame(Pagetemplate = text2) | text2 <- data.frame(Pagetemplate = text2) | ||
Line 239: | Line 298: | ||
=== Step 3. Finalize the page === | === Step 3. Finalize the page === | ||
In step 2, page content is | In step 2, page content is compiled. You can rerun the step 2 code as many times as you need to define all the ovariables. Remember to copy-paste the outputs each time to the branch page. | ||
In the end, you have to adjust the page content, because for security reasons some character combinations are not allowed in code output. In the text produced, there is a character "¤" where there should be "<". So, you should find and replace "¤" with "<" (use the button at the top-right corner of the edit window). Then you can save your page. | |||
== See also == | == See also == | ||
Line 246: | Line 307: | ||
* [[Ovariable]] | * [[Ovariable]] | ||
* [[Modelling in Opasnet]] | * [[Modelling in Opasnet]] | ||
* [[Test branch]] | |||
* [[:en:Life-cycle assessment]] | |||
* [http://www.lcia-recipe.net/project-definition ReCiPe methodology] | |||
* [http://www.impactworldplus.org/en/methodology.php ImpactWorld+] | |||
== Related files == | == Related files == |
Latest revision as of 17:14, 20 March 2016
Moderator:Jouni (see all) |
|
Upload data
|
Question
How to create a branch for handprinter?
Answer
Step 1: Create a page for branch
If the branch that you are working with already has a page, open the page for editing and go to step 2.
Open two copies of this page to your browser.
Give a name to your branch and click to create a new page for it.
On the other page, continue from step 2.
<createbox> break=yes width=50 buttonlabel=Create branch align=left </createbox>
Step 2: Fill in data about your branch
In step 2, you can create a new branch page or add more ovariables to an existing page. Fill in the forms below, click to create content for the page, and copy-paste that onto the page you created in step 1. You can create the main content of an entire branch page, a new ovariable that takes data as input, and a new ovariable that takes a formula as input.
Each branch is a model that consists of sub-models called ovariables. If you don´t know what they are, you should first read pages Ovariable and Modelling in Opasnet.
If you need more ovariables for your branch model, you can simply rerun this code. Just remember to copy-paste the previous content first, as it will be replaced by the new ovariables. Each ovariable has its own sub-heading under the heading "Rationale".
This is a brief description of input. For details, see the linked pages.
- Branch name
- Name of the page where the content and code goes. This name may contain several words and spaces, and it is recommended that it has the word 'branch' in it.
- Names of ovariables
- Name of a new ovariable must be a single word with no spaces. It may contain numbers and '_' and '.'. Preferably it is short but easily understandable.
- Output variable
- The final ovarable in your branch model. Other ovariables are used as inputs for this ovariable.
- Unit
- The unit of measurement that is used for the input data.
- Data table
- Data used by the data ovariable. It is a copy-pasted table where the first row contains the names of the columns and all other rows are the data. There must be one or more result columns, i.e. columns that contain the actual result of the ovariable; all other columns either define the scope of the result (index columns) or describe the content of the row (description columns).
- There are three ways to define the result column(s): (i) the name of the column is "Result", (ii) the result column names have two parts separated by colon (see details below), (iii) if result column has not been defined, the last column is used as the result column.
- All columns left from the result column(s) are treated as index columns. All columns right from the result column(s) are treated as description columns.
- There can be several result columns so that each column is about a specified location, e.g. a year. Then the result column names must be divided into two parts by a colon so that the index is the first part and location is the second, e.g. Year:2010, Year:2011, Year:2013 and so on. For details, see Table2Base and Table2Base2.
- Rationale for data and formula ovariables
- Rationale is any free-text description of the ovariable to make it more understandable and reasoned.
- Dependencies of ovariable
- It is a copy-pasted table that must contain column 'Name' and may contain columns 'Ident' or 'Key'. Name lists the names of all ovariables that are needed to calculate the result of this ovariable. Ident tells the name of the codes that were used to save the ovariables and it is of format 'Op_en1234/codename' where the first part is the page identifier and the second part is the name of the code. For details, see R-tools.
- Formula of ovariable
- It is an R function (a particular kind of software code) that uses the dependencies and produces an output for this ovariable. You need to know something about R and ovariables to be able to use this effectively. In brief, the code starts where all input variables have already been loaded and calculated, and ends with 'return(out)' where out is the output for this ovariable.
Step 3. Finalize the page
In step 2, page content is compiled. You can rerun the step 2 code as many times as you need to define all the ovariables. Remember to copy-paste the outputs each time to the branch page.
In the end, you have to adjust the page content, because for security reasons some character combinations are not allowed in code output. In the text produced, there is a character "¤" where there should be "<". So, you should find and replace "¤" with "<" (use the button at the top-right corner of the edit window). Then you can save your page.
See also
- Handprinter
- Ovariable
- Modelling in Opasnet
- Test branch
- en:Life-cycle assessment
- ReCiPe methodology
- ImpactWorld+