Whitespace

This commit is contained in:
2019-04-11 12:17:23 +01:00
parent 94b7fcb0e6
commit 7752b73bc6
2 changed files with 158 additions and 158 deletions

View File

@@ -40,7 +40,7 @@ buildExpr <- function(pressStatus) {
}
expr<-substr(expr, 1, nchar(expr)-2)
expr<-paste0(expr, ')')
return(expr)
}
@@ -55,14 +55,14 @@ parseScenario <- function(press, prefix = 'p') {
cat('Duplicated pressure node names found')
print(pressNodes[duplicated(pressNames)])
}
return(list(
timeSeq=press,
nodes=data.frame(name = pressNames,
code=paste0(prefix, seq(1:length(pressNames))),
growth = coefs[,'growth'],
confidence=coefs[,'confidence'],
layer=coefs[,'layer'],
timeSeq=press,
nodes=data.frame(name = pressNames,
code=paste0(prefix, seq(1:length(pressNames))),
growth = coefs[,'growth'],
confidence=coefs[,'confidence'],
layer=coefs[,'layer'],
stringsAsFactors = FALSE),
edges=data.frame(input=NULL, output=NULL, impact=NULL)
))
@@ -75,7 +75,7 @@ getInitial <- function(string, letter) {
split <- function(cell) {
params <- unlist(strsplit(cell, ','))
values <- rep(0, length(states))
for (n in 1:length(params)) {
kvp <- unlist(strsplit(params[n], '='))
ref <- match(getInitial(trimws(kvp[1])), getInitial(states))
@@ -106,11 +106,11 @@ buildGraph <- function(model, desc) {
#model contains the following
# node table, edge table
#descriptor (desc) contains:
#inputCode - the top layer of the model
#outputCodes - all subsequent layers to be included in the model
inputNodes <- model$nodes$code[which(startsWith(model$nodes$code, desc$inputCode))]
inputText <- paste0("[", inputNodes, "]", collapse ="")
@@ -128,19 +128,19 @@ buildGraph <- function(model, desc) {
edges <- paste0(edges, paste0("[", outNodes[idx], "|", substr(inputsStr, start=1, stop=(nchar(inputsStr)-1)), "]"))
#Make the coefficient of the distribution
coefVal <- setNames(c(model$nodes$growth[nodeRef], model$edges$values[rows]),
coefVal <- setNames(c(model$nodes$growth[nodeRef], model$edges$values[rows]),
c("(Intercept)", model$edges$input[rows])
)
#str(coefVal)
outDist[[idx]] <- list(coef = coefVal,
outDist[[idx]] <- list(coef = coefVal,
sd = model$nodes$confidence[nodeRef])
}
print('about to build network')
print(paste0(inputText, edges))
net <- model2network(paste0(inputText, edges), debug=TRUE)
print('network build successful')
inDist <- vector(mode="list", length=length(inputNodes))
@@ -153,19 +153,19 @@ buildGraph <- function(model, desc) {
allDists = as.list(setNames(c(inDist, outDist), c(inputNodes, outNodes)))
cfit = custom.fit(net, allDists)
cat('about to calculate sample distributions')
print(outNodes)
sampleDists <- cpdist(cfit, nodes = outNodes, evidence = TRUE, n = 10000, method = "lw")
summDists <- summary(sampleDists)
#stdDev <- sd(sampleDists)
print('sample distribution build successful')
model$edges$input <- model$nodes$name[match(model$edges$input, model$nodes$code)]
model$edges$output <- model$nodes$name[match(model$edges$output, model$nodes$code)]
return(
list(
nodes = model$nodes,
@@ -180,10 +180,10 @@ buildGraph <- function(model, desc) {
getValidNodes <- function(mapping, prevOutputs, prefix) {
#Find row id for input nodes, internal and published
inputNodes <- mapping[2:nrow(mapping),1]
#check that all input nodes are in the previous table
inputNodes <- delNA(mapping[mapping[,"Node.Type"] == 'input', "Nodes"])
if (length(inputNodes)>0) {
@@ -192,27 +192,27 @@ getValidNodes <- function(mapping, prevOutputs, prefix) {
print(inputNodes[!inputNodes %in% prevOutputs$name])
}
} else print('Invalid sheet - table must have at least one input row containing names from previous table')
#Check the row headings concur with previous names
validInputs <- delNA(inputNodes[which(unique(inputNodes) %in% prevOutputs$name)])
if (length(validInputs)==0) print('Invalid sheet - table must have at least one input row containing names from previous table')
inputInts <- delNA(inputNodes[mapping$Node.Type!='link'])
if (sum(duplicated(inputInts))>0) {
cat('Duplicated input node names found')
print(inputNodes[duplicated(inputNodes)])
}
outNodes <- delNA(colnames(mapping)[FIRST_NODE_COL:ncol(mapping)])
if (sum(duplicated(outNodes))>0) {
cat('Duplicated output node names found')
print(outNodes[duplicated(outNodes)])
}
#check that all internal nodes are in the columns
intNodes <- delNA(mapping[mapping[,"Node.Type"] == 'internal', "Nodes"])
if (length(intNodes)>0) {
@@ -221,17 +221,17 @@ getValidNodes <- function(mapping, prevOutputs, prefix) {
print(intNodes[!intNodes %in% outNodes])
}
}
coefs <- matrix(data=NA, nrow=length(outNodes), ncol=3, dimnames=list(NULL, c('growth', 'confidence', 'layer')))
for (idx in 1:length(outNodes)) {
col <- match(outNodes[idx], colnames(mapping))
coefs[idx,] <- as.numeric(split(mapping[1, col]))[match(c('growth', 'confidence', 'layer'), states)]
}
print(coefs)
return(data.frame(
code=c(prevOutputs$code, paste0(prefix, seq(1:length(outNodes)))),
code=c(prevOutputs$code, paste0(prefix, seq(1:length(outNodes)))),
name=c(prevOutputs$name, outNodes),
growth=c(prevOutputs$growth, coefs[,"growth"]),
confidence=c(prevOutputs$confidence, coefs[,"confidence"]),
@@ -248,17 +248,17 @@ getValidEdges <- function(mapping, nodeDF, prevEdge=NULL, prefix) {
str(nodeDF)
edgeCols <- c('inputNode', 'outputNode', 'impact')
edgeM <- matrix(data=NA, nrow=0, ncol=length(edgeCols), dimnames=list(NULL, edgeCols))
#to start let just get the statements and print them out....
for (col in FIRST_NODE_COL:ncol(mapping)) {
count=0
for (row in 2:nrow(mapping)) {
if (!is.na(mapping[row, col])) {
edgeM <- rbind(edgeM,
c(getCode(mapping[row, 1], nodeDF),
getCode(colnames(mapping)[col], nodeDF),
edgeM <- rbind(edgeM,
c(getCode(mapping[row, 1], nodeDF),
getCode(colnames(mapping)[col], nodeDF),
split(mapping[row,col])[match('impact', states)]
)
)
@@ -285,10 +285,10 @@ getValidEdges <- function(mapping, nodeDF, prevEdge=NULL, prefix) {
}
parseMapping <- function(mapping, prevOutputs, prefix) {
mapping <- mapping[,-1]
mapping[,1] <- cleanTitles(mapping[,1])
nodeDF <- getValidNodes(mapping, prevOutputs$nodes, prefix)
edgeDF <- getValidEdges(mapping, nodeDF, prevEdge=prevOutputs$edges, prefix)
@@ -302,32 +302,32 @@ parseMapping <- function(mapping, prevOutputs, prefix) {
parseSheet <- function(fName) {
#get sheet names
print(paste('starting sheet load', fName))
if (file.exists(fName)) {
names <- openxlsx::getSheetNames(fName)
if (length(names)>0) {
sheets <- sort(delNA(match(names, mappings)))
cat('starting sheet parse')
print(sheets)
if (sum(sheets==refs)==length(refs)) {
#read all mapping tables
scenario <- parseScenario(readXL(fName,mappings[1], startRow=1), prefix='p')
p_ba <- parseMapping(readXL(fName,mappings[2], startRow=1), scenario, prefix='ba')
p_op <- parseMapping(readXL(fName,mappings[3], startRow=1), p_ba, prefix='op')
p_es <- parseMapping(readXL(fName,mappings[4], startRow=1), p_op, prefix='es')
#print('building graphs')
#p_baNet <- buildGraph(p_ba, desc=list(inputCode='p', outputCodes='ba'))
#p_opNet <- buildGraph(p_op, desc=list(inputCode='p', outputCodes=c('ba', 'op')))
#p_esNet <- buildGraph(p_es, desc=list(inputCode='p', outputCodes=c('ba', 'op', 'es')))
print('sheet load completed')
return(
#list(