Branch creator: Difference between revisions

From Opasnet
Jump to navigation Jump to search
No edit summary
 
(14 intermediate revisions by the same user not shown)
Line 10: Line 10:


=== Step 1: Create a page for branch ===
=== Step 1: Create a page for branch ===
{{bluebox|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.  
Open two copies of this page to your browser.  
Line 24: Line 26:
=== Step 2: Fill in data about your branch ===
=== Step 2: Fill in data about your branch ===


Fill in the form below, click to create the 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.


<rcode label="Create content for the branch" variables="
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]].
name:branchname|type:hidden|default:'{{PAGENAME}}'|
name:PAGEID|type:hidden|default:'{{PAGEID}}'|
name:output.ovariable|description:Give a short name (no spaces!) to the final outcome variable in the branch model.|type:text|
name:dependencies|description:What are the input variables for the outcome variable? Format c('var1', 'var2')|type:textbox|
name:output.rationale|description:Give free-text rationale about what the outcome variable is and how it is calculated.|type:textbox|
name:intermediate.ovariable|description:Give one input variable name (others are added afterwards)|type:text|
name:t2bdata|description:Give a data table for the input variable|type:table|
name:intermediate.rationale|description:Give free-text rationale about what the intermediate variable is and how it is calculated.|type:textbox
">


library(OpasnetUtils)
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".
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 benefic that reduces consumption of other products and thus footprint."


#<t2b name='Tire inflation parameters' index='Parameter,Unit' obs='Result' unit='-'>
This is a brief description of input. For details, see the linked pages.
#Pressure drop per temperature drop|PSI /10°F|0.5:1:2
;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.
#Fuel economy sensitivity|% /PSI|0.4
;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.
#Miles driven per year|mi /a|10000
;Output variable: The final ovarable in your branch model. Other ovariables are used as inputs for this ovariable.
#Fuel consumption|gl /mi|0.04
;Unit: The unit of measurement that is used for the input data.
#Pressure deficit|PSI|2
;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).
#</t2b>
:* 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: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: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|
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|
name:unit|description:What is(are) the unit(s) of the data?|type:text|default:-|
name:t2bdata|description:Give a data table for the input variable.|type:table|
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|
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.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


text1 <- paste("[[Category:Life cycle assessment]]
library(OpasnetUtils)
[[Category:Handprinter]]
{{method|moderator=",
wiki_username,
"}},
'''",
branchname,
"''' is a '''branch''' of Handprinter. This page is a test case how to manage branches.


== Question ==
if(newpage) {


How to estimate the life-cycle impacts of ",
newpage <- paste("[[Category:Life cycle assessment]]
branchname,
[[Category:Handprinter]]
"?
{{method|moderator=",
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 = ""
)
}


== Answer ==
if(newdata) {
# Take the data out of the input
dat <- as.matrix(t2bdata[-1 , ])
dat <- apply(dat, 1, paste, collapse = "|")
dat <- paste(dat, "
",
sep = "")


<rcode embed=1>
# Find parameter values for t2b tag.
# This is code Op_en",
cols <- as.matrix(t2bdata)[1 , ]
PAGEID,
"/ on page [[",
branchname,
"]]
library(OpasnetUtils)


objects.latest('Op_en",
# Find column(s) with results: has name Result, has colons, or is the last one.
PAGEID,
rescols <- tolower(cols) %in% "result"
"', code_name = '",
if(!any(rescols)) {
output.ovariable,
rescols <- grepl(":", cols)
"')
}
if(!any(rescols)) {
rescols <- 1:length(cols) == length(cols)
}


",
# Indices are to the left from rescols, descriptions are to the right.
output.ovariable,
firstres <- min(grep(TRUE, rescols))
" <- EvalOutput(",
if(firstres > 1) indices <- cols[1:(firstres-1)] else indices <- character()
output.ovariable,
lastres <- max(grep(TRUE, rescols))
")
if(lastres < length(cols)) desc <- cols[(lastres+1):length(cols)] else desc <- character()


oprint(summary(",
resnames <- strsplit(cols[rescols], split = ":")
output.ovariable,
"))


@/rcode>
# 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>
")


== Rationale ==
newdata <- paste(c(
"=== ",
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) {


=== ",
dep <- as.matrix(dependencies)
output.ovariable,
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 = ",
dep,
",
  formula = function(...) {
",
gsub("<", "¤", formula),
"  }
)
objects.store(",
formula.ovariable,
")
cat('Object ',",
formula.ovariable,
"@name, ' stored.')
¤/rcode>
",
formula.rationale,
"
",
sep = ""
)
}


@rcode name='",
finale <- paste(
output.ovariable,
"== See also ==
"' embed=1 label='Initiate ",
* [[Handprinter]]
output.ovariable,
* [[Developing branches]]
" (for developers only)'>
",
# This is code Op_en",
"== Related files ==
PAGEID,
",
"/",
sep = ""
output.ovariable,
)
" on page [[",
branchname,
"]]
library(OpasnetUtils)


",
text1 <- ""
output.ovariable,
if(!is.logical(newpage)) text1 <- paste(text1, newpage, collapse = "")
" <- Ovariable('",
if(!is.logical(newdata)) text1 <- paste(text1, newdata, collapse = "")
output.ovariable,
if(!is.logical(newformula)) text1 <- paste(text1, newformula, collapse = "")
"',
if(!is.logical(newpage)) text1 <- paste(text1, finale, collapse = "")
  dependencies = data.frame(
    Name = c('",
paste(dependencies, collapse = "', '"),
")
  ),
  formula = function(...) {
    ",
output.ovariable,
" <- ",
intermediate.ovariable,
"
    return(",
output.ovariable,
")
  }
)


objects.store(list=ls())
text2 <- strsplit(text1, split = "\n")[[1]]
cat('Objects', ls(), 'stored.\n')
text2 <- data.frame(Pagetemplate = text2)
@/rcode>


",
oprint(text2, include.rownames = FALSE)
output.rationale,
</rcode>


"=== ",
=== Step 3. Finalize the page ===
intermediate.ovariable,
" ===


",
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.
t2bdata,
"@rcode name='",
intermediate.ovariable,
"' embed=1 label='Initiate ",
intermediate.ovariable,
" (for developers only)'>
# This is code Op_en",
PAGEID,
"/",
intermediate.ovariable,
" on page [[",
branchname,
"]]
library(OpasnetUtils)


",
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.
intermediate.ovariable,
" <- Ovariable('",
intermediate.ovariable,
"',
  ddata = 'Op_en",
PAGEID,
"subset = ",
intermediate.ovariable,
")


objects.store(",
== See also ==
intermediate.ovariable,
")
cat('",
intermediate.ovariable,
"stored.\n')",
"@/rcode>
",
intermediate.rationale,
"\n",
"== See also ==


* [[Handprinter]]
* [[Handprinter]]
* [[Developing branches]]
* [[Ovariable]]
",
* [[Modelling in Opasnet]]
"== Related files ==
* [[Test branch]]
",
* [[:en:Life-cycle assessment]]
sep = "")
* [http://www.lcia-recipe.net/project-definition ReCiPe methodology]
* [http://www.impactworldplus.org/en/methodology.php ImpactWorld+]


#text2 <- substr("@", "<", text1) # NEEDED BUT DOES NOT WORK
== Related files ==
text2 <- strsplit(text1, split = "\n")[[1]]
text2 <- data.frame(Pagetemplate = text2)
oprint(text2, include.rownames = FALSE)
</rcode>

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