Skip to content

Commit

Permalink
Merge pull request #182 from OHI-Science/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Melsteroni authored Jun 30, 2016
2 parents fbf866d + 92839a7 commit 410cebb
Show file tree
Hide file tree
Showing 99 changed files with 661 additions and 43,270 deletions.
34 changes: 10 additions & 24 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,38 @@ LazyData: TRUE
Depends:
R (>= 3.1.0)
Imports:
RColorBrewer,
RJSONIO,
dplyr (>= 0.3),
ggplot2 (>= 1.0.0),
ggvis (>= 0.4),
httr,
markdown (>= 0.6.3),
plyr (>= 1.8.0),
rCharts,
reshape2,
shiny (>= 0.10.0),
stringr,
knitr (>= 1.5)
git2r,
rgdal,
sp,
methods,
tidyr,
RColorBrewer,
htmlwidgets,
plotly
Suggests:
testthat
Collate:
'CalculateAll.R'
'CalculateGoalIndex.R'
'CalculatePressuresAll.R'
'CalculatePressuresComponent.R'
'CalculatePressuresMatrix.R'
'CalculatePressuresScore.R'
'CalculateResilienceAll.R'
'CalculateResilienceComponent.R'
'CalculateResilienceScore.R'
'CalculateStatusComponent.R'
'CalculateSubgoal.R'
'CheckLayers.R'
'Conf.R'
'Layers.R'
'PlotFlower.R'
'ScoreScaling.R'
'SelectLayersData.R'
'SpatialSchemes.R'
'TransformSpatialScheme.R'
'aster.R'
'compare_scores_df.R'
'gapfill_georegions.R'
'get_scenarios.R'
'launch_app.R'
'launch_cmp.R'
'mapvalues.R'
'name_to_rgn.R'
'read_git_csv.R'
'shp_to_geojson.R'
'trace_git_csv_value.R'
'write_shortcuts.R'
'zzz.R'
'mapvalues.R'
'change_plot.R'
RoxygenNote: 5.0.1
29 changes: 5 additions & 24 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,15 @@
export(CalculateAll)
export(CalculateGoalIndex)
export(CalculatePressuresAll)
export(CalculatePressuresComponent)
export(CalculatePressuresMatrix)
export(CalculatePressuresScore)
export(CalculateResilienceAll)
export(CalculateResilienceComponent)
export(CalculateResilienceMatrix)
export(CalculateResilienceScore)
export(CalculateStatusComponent)
export(CalculateSubgoal)
export(CheckLayers)
export(Conf)
export(Layers)
export(PlotFlower)
export(SelectLayersData)
export(TransformSpatialScheme)
export(aster)
export(change_plot)
export(compare_scores_df)
export(gapfill_georegions)
export(get_scenarios)
export(launch_app)
export(launch_cmp)
export(mapvalues)
export(name_to_rgn)
export(read_git_csv)
Expand All @@ -32,23 +20,16 @@ export(score.max)
export(score.rescale)
export(shp_to_geojson)
export(trace_git_csv_value)
export(write_shortcuts)
exportClasses(Conf)
exportClasses(Layers)
exportClasses(SpatialSchemes)
import(RColorBrewer)
import(RJSONIO)
import(dplyr)
import(ggplot2)
import(ggvis)
import(httr)
import(markdown)
import(git2r)
import(htmlwidgets)
import(plotly)
import(plyr)
import(rCharts)
import(reshape2)
import(shiny)
import(stringr)
import(yaml)
import(tidyr)
importFrom(dplyr,arrange)
importFrom(dplyr,desc)
importFrom(dplyr,failwith)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# ohicore 0.1


## Major changes


* Added datasets

* georegion_labels
* georegions
* sovregion_labels
* sovregions
* rgn_synonyms
* test
94 changes: 46 additions & 48 deletions R/CalculateAll.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#' @import reshape2
#' @import plyr
#' @import dplyr
#' @import tidyr
#' @import git2r
#' @importFrom dplyr arrange
#' @importFrom dplyr desc
#' @importFrom dplyr failwith
Expand All @@ -69,7 +71,7 @@
#' @importFrom plyr rename
#'
#' @export
CalculateAll = function(conf, layers, debug=F){
CalculateAll = function(conf, layers){

## Remove global scores
if (exists('scores', envir=.GlobalEnv)) rm(scores, envir=.GlobalEnv)
Expand All @@ -79,11 +81,11 @@ CalculateAll = function(conf, layers, debug=F){
cat('Running Setup()...\n')
conf$functions$Setup()
}
## Pre-Index functions: Status and Trend, by goal

## Access Pre-Index functions: Status and Trend, by goal
goals_X = conf$goals %>%
filter(!is.na(preindex_function)) %>%
arrange(order_calculate)
dplyr::filter(!is.na(preindex_function)) %>%
dplyr::arrange(order_calculate)

## Setup scores variable; many rbinds to follow
scores = data.frame(
Expand All @@ -93,7 +95,7 @@ CalculateAll = function(conf, layers, debug=F){
score = numeric())

## Calculate Status and Trend, all goals
for (i in 1:nrow(goals_X)){ # i=3
for (i in 1:nrow(goals_X)){ # i=5
g = goals_X$goal[i]
cat(sprintf('Calculating Status and Trend for each region for %s...\n', g))

Expand All @@ -106,26 +108,23 @@ CalculateAll = function(conf, layers, debug=F){
}

## Calculate Pressures, all goals
cat(sprintf('Calculating Pressures for each region...\n'))
scores_P = CalculatePressuresAll(layers, conf, gamma=conf$config$pressures_gamma, debug)
scores_P = CalculatePressuresAll(layers, conf)
scores = rbind(scores, scores_P)

## Calculate Resilience, all goals
cat(sprintf('Calculating Resilience for each region...\n'))
cat(sprintf('Note: each goal in resilience_matrix.csv must have at least one resilience field\n'))
scores = rbind(scores, CalculateResilienceAll(layers, conf, debug))
scores = rbind(scores, CalculateResilienceAll(layers, conf))
scores = data.frame(scores)

## Calculate Goal Score and Likely Future, all goals
goals_G = as.character(unique(subset(scores, dimension=='status', goal, drop=T)))
for (g in goals_G){ # g = 'FIS'
cat(sprintf('Calculating Goal Score and Likely Future for each region for %s...\n', g))

## spread the scores by dimension
v = scores %>%
filter(goal == g) %>%
spread(dimension, score)
dplyr::filter(goal == g) %>%
tidyr::spread(dimension, score)

## message if missing dimension, assign NA
for (col in c('status','trend','pressures','resilience')){
if (!col %in% names(v)){
Expand All @@ -150,11 +149,11 @@ CalculateAll = function(conf, layers, debug=F){
## Gather to scores format: goal, dimension, region_id, score
scores_G = x %>%
dplyr::select(region_id = id,
future = xF,
score) %>%
gather(dimension, score, -region_id) %>%
mutate(goal = g) %>%
select(goal, dimension, region_id, score)
future = xF,
score) %>%
tidyr::gather(dimension, score, -region_id) %>%
dplyr::mutate(goal = g) %>%
dplyr::select(goal, dimension, region_id, score)

## bind to other scores
scores = rbind(scores, scores_G)
Expand All @@ -163,7 +162,7 @@ CalculateAll = function(conf, layers, debug=F){
## Post-Index functions: supragoals
goals_Y = subset(conf$goals, !is.na(postindex_function))
supragoals = subset(conf$goals, is.na(parent), goal, drop=T); supragoals

for (i in 1:nrow(goals_Y)){ # i = 1

cat(sprintf('Calculating post-Index function for each region for %s...\n', goals_Y$goal[i]))
Expand All @@ -177,42 +176,42 @@ CalculateAll = function(conf, layers, debug=F){
cat(sprintf('Calculating Index score for each region for supragoals using goal weights...\n'))

# calculate weighted-mean Index scores from goal scores and rbind to 'scores' variable
scores =
rbind(scores,
scores =
rbind(scores,
scores %>%

# filter only supragoal scores, merge with supragoal weightings
filter(dimension=='score', goal %in% supragoals) %>%
dplyr::filter(dimension=='score', goal %in% supragoals) %>%
merge(conf$goals %>%
select(goal, weight)) %>%

# calculate the weighted mean of supragoals, add goal and dimension column
group_by(region_id) %>%
dplyr::group_by(region_id) %>%
dplyr::summarise(score = weighted.mean(score, weight, na.rm=T)) %>%
mutate(goal = 'Index',
dplyr::mutate(goal = 'Index',
dimension = 'score') %>%
data.frame())


## Calculate Region Likely Future State Scores using goal weights
cat(sprintf('Calculating Likely Future State for each region for supragoals using goal weights...\n'))

# calculate weighted-mean Likely Future State scores and rbind to 'scores' variable
scores =
rbind(scores,
scores =
rbind(scores,
scores %>%

# filter only supragoal scores, merge with supragoal weightings
filter(dimension=='future', goal %in% supragoals) %>%
dplyr::filter(dimension=='future', goal %in% supragoals) %>%
merge(conf$goals %>%
select(goal, weight)) %>%

# calculate the weighted mean of supragoals, add goal and dimension column
group_by(region_id) %>%
dplyr::group_by(region_id) %>%
dplyr::summarise(score = weighted.mean(score, weight, na.rm=T)) %>%
mutate(goal = 'Index',
dplyr::mutate(goal = 'Index',
dimension = 'future') %>%
data.frame())
data.frame())

## Post-process scores, but pre-global calculation: for global assessment only
if ('PreGlobalScores' %in% ls(conf$functions)){
Expand All @@ -222,25 +221,24 @@ CalculateAll = function(conf, layers, debug=F){

## Assessment Areas (sometimes known as 'global', region_id-0) scores by area weighting
cat(sprintf('Calculating scores for ASSESSMENT AREA (region_id=0) by area weighting...\n'))
## Calculate area-weighted Assessment Area scores and rbind to all scores

## Calculate area-weighted Assessment Area scores and rbind to all scores
scores = rbind(
scores,
scores %>%

# filter only score, status, future dimensions, merge to the area (km2) of each region
filter(dimension %in% c('score','status','future')) %>%
dplyr::filter(dimension %in% c('score','status','future')) %>%
merge(SelectLayersData(layers, layers=conf$config$layer_region_areas, narrow=T) %>%
dplyr::select(region_id = id_num,
area = val_num)) %>%

# calculate weighted mean by area
group_by(goal, dimension) %>%
summarise(score = weighted.mean(score, area, na.rm=T),
dplyr::group_by(goal, dimension) %>%
dplyr::summarise(score = weighted.mean(score, area, na.rm=T),
region_id = 0) %>%
ungroup())


ungroup())

## post-process
if ('FinalizeScores' %in% ls(conf$functions)){
cat(sprintf('Calculating FinalizeScores function...\n'))
Expand Down
Loading

0 comments on commit 410cebb

Please sign in to comment.