Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cumulative ranges #96

Merged
merged 26 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cccdf84
Move range construction to misc module
clhunsen Feb 19, 2018
da8f851
Add possibility to get raw ranges
clhunsen Feb 19, 2018
9dd91f7
Update 'construct.ranges' to work more reliably on midnight datetimes
clhunsen Feb 19, 2018
8b14cc3
Implement function to construct overlapping ranges
clhunsen Feb 19, 2018
fc11886
Implement function to construct cumulative ranges
clhunsen Feb 20, 2018
f08d671
Implement function to aggregate ranges
clhunsen Feb 20, 2018
d96ed2f
Refactor 'split.data.by.networks'
clhunsen Feb 20, 2018
d9bcb80
Implement function to construct consecutive ranges
clhunsen Feb 21, 2018
7223a4f
Add utility function to generate date sequences
clhunsen Feb 21, 2018
335c2b5
Refactor 'construct.overlapping.ranges' to use 'generate.date.sequence'
clhunsen Feb 21, 2018
cbc1f3c
Fix range construction to properly include end date
clhunsen Feb 21, 2018
d421807
Refactor 'split.get.bins.time.based' to use 'generate.date.sequence'
clhunsen Feb 21, 2018
889af54
Add functions to split data and networks by ranges
clhunsen Feb 21, 2018
73da31b
Use 'split.data.time.based.by.ranges' in 'split.data.by.networks'
clhunsen Feb 21, 2018
23117c6
Tidy 'tests/test-split.R'
clhunsen Feb 21, 2018
843f8c7
Update splitting tests
clhunsen Feb 21, 2018
99e368b
Fix bug with end date when constructing overlapping ranges
clhunsen Feb 21, 2018
0ec4690
Check overlap for overlapping ranges
clhunsen Feb 21, 2018
db0cb14
Add test for consecutive and overlapping ranges
clhunsen Feb 21, 2018
41e8d56
Fix bug with project end when aggregating ranges
clhunsen Feb 21, 2018
87829a7
Add tests for cumulative and aggregated ranges
clhunsen Feb 21, 2018
dee0abb
Fix test for 'split.data.by.networks'
clhunsen Feb 22, 2018
afc96f4
Document exclicit bins in functions 'split.*.time.based' more precisely
clhunsen Feb 22, 2018
d97c889
Fix 'generate.date.sequence' to handle strings correctly
clhunsen Feb 22, 2018
28b4122
Add tests for date handling
clhunsen Feb 22, 2018
22124ee
Minor fixes from review in PR #96
clhunsen Feb 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions util-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,118 @@ construct.cumulative.ranges = function(start, end, time.period, raw = FALSE) {
return(ranges)
}

#' Aggregate a given list/vector of ranges to specific levels, configurable through the
#' the parameter \code{aggregation.level} (see below for more details).
#'
#' Using different aggregation levels given by the parameter \code{aggregation.level},
#' it is possible to configure the exact treatment of range bounds and, thus, the
#' re-arrangement of the given list of ranges. The various aggregation levels work
#' as follows:
#' - \code{"range"}: The ranges will be kept exactly as given.
#' - \code{"cumulative"}: The ranges will be re-arranged in a cumulative manner.
#' - \code{"all.ranges"}: The ranges will be re-arranged to exactly to the time range
#' specified by the start of the first range and end of the last
#' range. All ranges will be exactly the same.
#' - \code{"project.cumulative"}: The same re-arrangement as for \code{"cumulative"}, but
#' all ranges will start at \code{project.start} and *not* at the
#' beginning of the first range.
#' - \code{"project.all.ranges"}: The same re-arrangement as for \code{"all.ranges"}, but
#' all ranges will start at \code{project.start} and *not* at
#' the beginning of the first range. All ranges will be exactly the same.
#' - \code{"complete"}: The same re-arrangement as for \code{"all.ranges"}, but all ranges
#' will start at \code{project.start} and end at \code{project.end}. All
#' ranges will be exactly the same.
#'
#' Note: You may want to use the function \code{ProjectData$get.data.timestamps} with this
#' function here, to pass proper values for \code{project.start} and \code{project.end}.
#'
#' @param ranges the list or vector of ranges to aggregate
#' @param project.start the project start time as string or POSIXct object
#' @param project.end the project end time as string or POSIXct object
#' @param aggregation.level One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. S7ee above for more details.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S7ee ;)

#' @param raw whether to return pairs of POSIXct objects or strings rather than
#' formatted strings [default: FALSE]
#'
#' @return the constructed ranges, either formatted or raw; the raw ranges are a named list,
#' for which the ranges from \code{ranges} are the names
aggregate.ranges = function(ranges, project.start, project.end,
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
raw = FALSE) {

## get the chosen aggregation level
aggregation.level = match.arg(aggregation.level)

## get the timestamp data from the project data (needed for some aggr. levels)
project.start = get.date.from.string(project.start)
project.end = get.date.from.string(project.end)

## loop over all ranges and split the data for each range accordingly:
list.of.range.bounds = lapply(ranges, get.range.bounds)
ranges.raw = lapply(ranges, function(range) {
## 1) get the range bounds to work with
start.end = get.range.bounds(range)

## 2) adjust the range bounds for the respective aggregation levels
## (if nothing else is stated below, the respective range bounds stay unchanged)
switch(aggregation.level,

range = {
## use the exact range bounds
},
cumulative = {
## the start is always at the first network's start bound
start.end[1] = list.of.range.bounds[[1]][1]
},
all.ranges = {
## the start is always at the first network's start bound
start.end[1] =list.of.range.bounds[[1]][1]
## the end is always at the last network's ending bound
start.end[2] = list.of.range.bounds[[length(ranges)]][2]
},
project.cumulative = {
## the start is always at the project data's start
start.end[1] = project.start
},
project.all.ranges = {
## the start is always at the project data's start
start.end[1] = project.start
## the end is always at the last network's ending bound
start.end[2] = list.of.range.bounds[[length(ranges)]][2]
},
complete = {
## the start is always at the project data's start
start.end[1] = project.start
## the start is always at the project data's ending
start.end[2] = project.end
}
)

return(start.end)
})

## construct actual range strings (without names)
ranges.new = sapply(ranges.raw, construct.ranges, sliding.window = FALSE, raw = FALSE)
ranges.new = unname(ranges.new)

## if raw is enabled, we need to attach proper names
if (raw) {
## add formatted original(!) ranges as names
if (is.list(ranges)) {
names(ranges.raw) = names(ranges)
} else {
names(ranges.raw) = ranges
}
## set as return value
ranges.new = ranges.raw
}

return(ranges.new)
}

#' Calculate the bounds of a range from its name.
#'
#' @param range The range name
Expand Down
3 changes: 1 addition & 2 deletions util-split.R
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,7 @@ split.data.activity.based = function(project.data, activity.type = c("commits",
#'
#' @param list.of.networks The network list
#' @param project.data The entire project data
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' @param aggregation.level One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See above for more details.
#'
Expand Down