Branch creator: Difference between revisions

From Opasnet
Jump to navigation Jump to search
(user interface split into newpage, newdata, and newformula)
 
(10 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 form below, click to create content for the page, and copy-paste that onto the page you created in step 1. Save the page.
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 Opanet]].
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]].


{{attack|# |Code works except that oprint puts and extra space between "< rcode" and that breaks the code created.|--[[User:Jouni|Jouni]] ([[User talk:Jouni|talk]]) 18:15, 5 March 2016 (UTC)}}
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".


<rcode name="fill" label="Create content for the branch" recall_inputs=1 variables="
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="
name:newpage|description:Do you want to create content for a new branch page?|type:selection|options:FALSE;No;TRUE;Yes|default:FALSE|
name:newpage|description:Do you want to create content for a new branch page?|type:selection|options:FALSE;No;TRUE;Yes|default:FALSE|
name:newdata|description:Do you want to create content for a new ovariable with data?|type:selection|options:FALSE;No;TRUE;Yes|default:FALSE|
name:newdata|description:Do you want to create content for a new ovariable with data?|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: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:output.ovariable0|description:Give a short name (no spaces!) for the ovariable that is the final outcome of the branch|type:text|
name:data.ovariable|description:Give a short name (no spaces!) for an ovariable with data|type:text|
name:intermediate.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|type:table|
name:unit|description:What is(are) the unit(s) of the data?|type:text|default:-|
name:intermediate.rationale|description:Give free-text rationale about what the intermediate variable is and how it is calculated.|type:textbox|
name:t2bdata|description:Give a data table for the input variable.|type:table|
name:output.ovariable|description:Give a short name (no spaces!) to an ovariable with a formula.|type:text|
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? Format c('var1', 'var2')|type:default|
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:output.rationale|description:Give free-text rationale about what the outcome variable is and how it is calculated.|type:textbox
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)
t2bdata <- ""
#wiki_username <- "Jouni"
#branchname <- "Garden tower branch"
#PAGEID <- "1234"
#output.ovariable <- "gardentower"
#intermediate.ovariable <- "vegeproduction"
#t2bdata <- "tobefilled"
#dependencies <- c("vegeproduction", "wasteproduction", "waterconsumption")
#output.rationale <- "Garden tower composts kitchen waste and turns it into soil."
#intermediate.rationale <- "vegeproduction is the amount of vegetables produced in the garden tower. It is a critical benefit that reduces consumption of other products and thus footprint."
#<t2b name='Tire inflation parameters' index='Parameter,Unit' obs='Result' unit='-'>
#Pressure drop per temperature drop|PSI /10°F|0.5:1:2
#Fuel economy sensitivity|% /PSI|0.4
#Miles driven per year|mi /a|10000
#Fuel consumption|gl /mi|0.04
#Pressure deficit|PSI|2
#</t2b>


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) {
"'''{{PAGENAME}}''' is a '''branch''' of Handprinter. This page is a test case how to manage branches.
# Take the data out of the input
dat <- as.matrix(t2bdata[-1 , ])
dat <- apply(dat, 1, paste, collapse = "|")
dat <- paste(dat, "
",
sep = "")


== Question ==
# Find parameter values for t2b tag.
cols <- as.matrix(t2bdata)[1 , ]


How to estimate the life-cycle impacts of '''{{PAGENAME}}'''?
# 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)
}


== Answer ==
# 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()


@rcode embed=1 variables='
resnames <- strsplit(cols[rescols], split = ":")
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 = '",
# If only one rescol
output.ovariable0,
## 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(
output.ovariable0,
"=== ",
" <- EvalOutput(",
data.ovariable,
output.ovariable0,
" ===
")
",
 
out,
oprint(summary(",
"¤rcode name='",
output.ovariable0,
data.ovariable,
"))
"' embed=1 label='Initiate ",
 
data.ovariable,
@/rcode>
" (for developers only)' variables='
 
name:PAGEID|type:hidden|default:{{PAGEID}}
== Rationale ==
'>
 
# This is code Op_enXXXX/",
",
data.ovariable,
sep = "")
" 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) {


newformula <- paste(
dep <- as.matrix(dependencies)
"=== ",
dep <- apply(dep, 2, FUN = function(x) return(paste(c(x[1], "= c('", paste(x[-1], collapse = "', '"), "')"), collapse = "")))
output.ovariable,
dep <- paste(dep, collapse = ",\n\t")
" ===
dep <- paste(c("data.frame(", dep, ")"), collapse = "\n\t")
 
@rcode name='",
newformula <- paste(
output.ovariable,
"=== ",
"' embed=1 label='Initiate ",
formula.ovariable,
output.ovariable,
" ===
" (for developers only)' variables='
¤rcode name='",
name:PAGEID|type:hidden|default:`{{PAGEID}}`
formula.ovariable,
'>
"' embed=1 label='Initiate ",
# This is code Op_enXXXX/",
formula.ovariable,
output.ovariable,
" (for developers only)'>
" on page [[",
# This is code Op_enXXXX/",
branchname,
formula.ovariable,
"]]
" on page [[",
library(OpasnetUtils)
branchname,
 
"]]
",
library(OpasnetUtils)
output.ovariable,
",
" <- Ovariable('",
formula.ovariable,
output.ovariable,
" ¤- Ovariable('",
"',
formula.ovariable,
  dependencies = data.frame(
"',
    Name = c('",
  dependencies = ",
paste(dependencies, collapse = "', '"),
dep,
")
",
  ),
  formula = function(...) {
  formula = function(...) {
",
    ",
gsub("<", "¤", formula),
formula,
"  }
" }
)
)
objects.store(",
 
formula.ovariable,
rm(wiki_username)
")
objects.store(list=ls())
cat('Object ',",
cat('Objects', ls(), 'stored.\n')
formula.ovariable,
@/rcode>
"@name, ' stored.')
 
¤/rcode>
",
",
output.rationale,
formula.rationale,
"
"
 
",
",
sep = ""
sep = "")
)
}
 
if(newdata) {
newdata <- paste(
"=== ",
intermediate.ovariable,
" ===
 
",
t2bdata,
"@rcode name='",
intermediate.ovariable,
"' embed=1 label='Initiate ",
intermediate.ovariable,
" (for developers only)'>
# This is code Op_enXXXX/",
intermediate.ovariable,
" on page [[",
branchname,
"]]
library(OpasnetUtils)
 
",
intermediate.ovariable,
" <- Ovariable('",
intermediate.ovariable,
"',
  ddata = 'Op_en{{PAGEID}}',
  subset = ",
intermediate.ovariable,
")
 
objects.store(",
intermediate.ovariable,
")
cat('",
intermediate.ovariable,
"stored.@@')
",
"@/rcode>
",
 
intermediate.rationale,
"
",
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 <- gsub("@@", "\n", text2)
text2 <- gsub("@", "<", text2)
text2 <- data.frame(Pagetemplate = text2)
text2 <- data.frame(Pagetemplate = text2)


oprint(text2, include.rownames = FALSE)
oprint(text2, include.rownames = FALSE)
</rcode>
</rcode>
=== 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]]
* [http://www.lcia-recipe.net/project-definition ReCiPe methodology]
* [http://www.impactworldplus.org/en/methodology.php ImpactWorld+]
== Related files ==

Latest revision as of 17:14, 20 March 2016



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.

Do you want to create content for a new branch page?:

Do you want to create content for a new ovariable with data?:

Do you want to create content for a new ovariable with a formula?:

What was the branch name again?:

Content for a new branch page

What again is the name of the ovariable that is the final outcome of the branch?:

Content for a new ovariable with data

Give a short name (no spaces!) for an ovariable with data:

What is(are) the unit(s) of the data?:

Give a data table for the input variable.:

Cancel

Give free-text rationale about what the data ovariable is and where the data comes from.:

Content for a new ovariable with formula

Give a short name (no spaces!) to an ovariable with a formula.:

What are the input (parent or upstream) ovariables for this ovariable?:

Cancel

What is the formula that defines the calculations with the parents?:

Give free-text rationale about what the outcome variable is and how it is calculated.:

+ Show code

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

Related files