Skip to content

Commit

Permalink
Merge branch '0.2.0-beta1'
Browse files Browse the repository at this point in the history
  • Loading branch information
billyc committed Aug 22, 2023
2 parents 9904e5f + 2e3aebc commit a5baa4f
Show file tree
Hide file tree
Showing 189 changed files with 12,681 additions and 6,921 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ network.xml.gz
analysis_berlin10/
kelheim/
test/
dashboard_test/
.sentinel*
inst/doc
9 changes: 6 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: matsim
Type: Package
Title: R library for interfacing with MATSim agent-based microsimulation model
Version: 0.1.9
Version: 0.2.0
Authors@R: c(
person("Billy", "Charlton", email = "[email protected]", role=c("aut","cre")),
person("Oleksandr", "Soboliev", role="aut"))
Expand All @@ -11,10 +11,10 @@ Description: MATSim support package for R
of the Python matsim-tools package, and thus supports reading standard
MATSim networks and soon, plans and events as well.
.
Developed with support from the Technische Universität Berlin's
Developed with support from the Technische Universität Berlin's
VSP Transport Planning and Transport Telematics department.
.
This project is under ongoing development and submissions are welcome!
This project is under ongoing development and submissions are welcome!
License: GPL (>= 3)
Encoding: UTF-8
Imports:
Expand Down Expand Up @@ -42,5 +42,8 @@ URL: https://github.com/matsim-vsp/matsim-r
BugReports: https://github.com/matsim-vsp/matsim-r/issues
RoxygenNote: 7.2.3
Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0)
Config/testthat/edition: 3
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ docs: .sentinel-make-docs
clean:
> rm -rf docs
> rm -rf man
> rm .sentinel*
.PHONY: clean
45 changes: 40 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(appendDistanceCategory)
export(boxplotScoreDifferences)
export(compareAverageTravelWait)
export(compareBasePolicyOutput)
Expand All @@ -11,7 +12,6 @@ export(deriveODMatrix)
export(filterByRegion)
export(generateXYHexagonYaml)
export(getCrsFromConfig)
export(loadNetwork)
export(mergeCountsAndLinks)
export(plotActivityEndTimes)
export(plotArrivalTimesPerTripPurpose)
Expand All @@ -29,33 +29,68 @@ export(plotTripCountByDepTime)
export(plotTripDistanceByMode)
export(plotTripDistancedByType)
export(plotTripTypesPieChart)
export(plot_arrtime_by_act)
export(plot_compare_count_by_spatialcat_barchart)
export(plot_compare_distcat_by_mainmode_barchart)
export(plot_compare_mainmode_barchart)
export(plot_compare_mainmode_sankey)
export(plot_compare_score_boxplot)
export(plot_compare_travelwaittime_by_mainmode)
export(plot_compare_travelwaittime_by_mainmode_barchart)
export(plot_deptime_by_act)
export(plot_distance_by_mainmode_barchart)
export(plot_distance_by_spatialcat_barchart)
export(plot_distcat_by_mainmode_barchart)
export(plot_mainmode_barchart)
export(plot_mainmode_piechart)
export(plot_map_trips)
export(plot_map_trips_by_spatialcat)
export(plot_spatialtype_by_shape_piechart)
export(plot_travelwaittime_mean_barchart)
export(plot_trips_count_by_deptime_and_mainmode_linechart)
export(prepareComparisonSimwrapperDashboardFromTable)
export(prepareSimwrapperDashboardFromFolder)
export(prepareSimwrapperDashboardFromTable)
export(processDtvEstimationQuality)
export(processLinkStatsDtvDistribution)
export(process_append_distcat)
export(process_append_spatialcat)
export(process_convert_table_to_sf)
export(process_convert_time)
export(process_filter_by_shape)
export(process_get_crs_from_config)
export(process_get_mainmode_distribution)
export(process_get_od_matrix)
export(process_get_travdistance_distribution)
export(process_get_travelwaittime_by_mainmode)
export(process_rename_category)
export(process_rename_mainmodes)
export(readCounts)
export(readLinkStats)
export(readPersonsTable)
export(readTripsTable)
export(read_config)
export(read_network)
export(read_output_persons)
export(read_output_trips)
export(symlog_trans)
export(transformToSf)
import(dplyr)
import(forcats)
import(geomtextpath)
import(ggalluvial)
import(ggplot2)
import(ggplot2, except = last_plot)
import(ggrepel)
import(htmltools)
import(leaflet)
import(lubridate)
import(plotly)
import(purrr)
import(readr)
import(reshape)
import(scales)
import(reshape, except = c(stamp,rename,expand))
import(scales, except = c(discard, col_factor))
import(sf)
import(tidyr)
import(tidyr, except = expand)
import(tidyverse)
import(xml2)
import(yaml)
42 changes: 42 additions & 0 deletions R/config.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' Load MATSIM config file into Memory
#'
#' Loads a MATSim xml config from file or archive,
#' creating a list with parameters as in xml file
#'
#' @param input_path character string, path to matsim output directory or http link to the file.
#' @param n_max integer, maximum number of lines to read within output_trips
#' @return tibble of trips_output
#'
#' @export
read_config <- function(input_path = ".", n_max = Inf) {

result_tibble <- tibble(module = character(),
param = character(),
value = character())

config_xml<-read_xml(input_path)

module_nodeset<-xml_children(config_xml)

result_list <- list()

for(i in 1:length(module_nodeset)){
module_name = xml_attr(module_nodeset[[i]],"name")

param_nodeset = xml_children(module_nodeset[[i]])
for(j in 1:length(param_nodeset)){
param_name<-xml_attr(param_nodeset[[j]],"name")
value_name<-xml_attr(param_nodeset[[j]],"value")
#print(param_name,value_name)

result_list[[module_name]][[param_name]] <- value_name


}

}

return(result_list)


}
65 changes: 32 additions & 33 deletions R/counts.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#'@import tidyverse
#'@import scales
#' @rawNamespace import(scales, except = c(discard, col_factor))
#'@import xml2
#'@import geomtextpath
#'@import readr
Expand All @@ -8,12 +8,12 @@
#'
#'@title Load a MATSim Counts file into memory
#'
#'@description Loads a MATSim Counts XML-File as tibble into memory
#'@description Loads a MATSim Counts XML-file as tibble into memory
#'
#'
#'@param file File to load. Must be an .xml file
#'
#'@return tibble containing with MATSim Link id as "loc_id" as key
#'@return tibble with MATSim link id ("loc_id") as key
#'
#'@export
readCounts <- function(file){
Expand Down Expand Up @@ -51,11 +51,11 @@ readCounts <- function(file){
result
}

#' Load linkstats as tibble into memory
#' Load a MATSim linkstats file into memory
#'
#' Reads Linkstats as .tsv created from LinkStats.class
#' as dataframe into memory.
#' Counts can be provided in any time bins.
#' Loads a linkstats tsv file created from the LinkStats class
#' as a dataframe into memory.
#' Counts can be provided in any time bin.
#' Counts can be provided for any qsim mode. The argument networkModes is used to
#' select and filter the columns.
#'
Expand Down Expand Up @@ -107,30 +107,30 @@ readLinkStats <- function(runId, file, sampleSize = 0.25){
}


#' Load Counts, a limited number of Linkstats and Network links as joined tibble into memory
#' Join counts and linkstats to the network, creating a tibble into memory
#'
#'Function to join counts, network links and several matsim link stats. Data can be aggregated
#'and filtered by time or network mode.
#'Function to join counts, linkstats and network links. Data can be aggregated
#'and filtered by time or mode.
#'
#'
#'@param counts Tibble with counts data
#'
#'@param network Tibble with network nodes and links
#'
#'@param linkStats List with link stats tibbles
#'@param linkStats List with linkstats tibbles
#'
#'@param networkModes a vector with network modes, which are needed for analysis
#'@param networkModes Vector with network modes that will be analyzed, default is "car".
#'
#'@param aggr_to Determinates if data should be aggregated to hour values or DTV, can either be "day" or "hour"
#'@param aggr_to Determines if data should be aggregated into hourly bins or as daily traffic volume, can either be "day" or "hour"
#'
#'@param earliest Lower limit to filter link stats by time, default is 0
#'@param earliest Integer. Lower limit to filter link stats by time, default = 0.
#'
#'@param latest Upper limit to filter link stats by time, default is 86400 (midnight)
#'
#'@return Long-format tibble with MATSim link id as key ("loc_id"), traffic volume from MATSim runs and link type
#'@param latest Integer. Upper limit to filter link stats by time, default = 86400 (midnight).
#'
#'@return Long-format tibble with MATSim link id as key ("loc_id"), traffic volumes from MATSim runs and link type
#'
#'@export

mergeCountsAndLinks <- function(counts, network, linkStats, networkModes = c("car"), aggr_to = c("day", "hour"), earliest = 0, latest = 86400){

if(!is.list(linkStats)){
Expand Down Expand Up @@ -242,7 +242,7 @@ mergeCountsAndLinks <- function(counts, network, linkStats, networkModes = c("ca
join.long
}

#' Categorize DTV and calculate DTV distribution
#' Categorize daily traffic volume (DTV) and calculate DTV for different link types.
#'
#' Takes a tibble from mergeCountsAndLinks. DTV is categorized into bins. Finally
#' data is aggregated to calculate DTV distribution in each link type category,
Expand All @@ -252,13 +252,13 @@ mergeCountsAndLinks <- function(counts, network, linkStats, networkModes = c("ca
#'
#' @param joinedFrame A tibble from mergeCountsAndLinks
#'
#' @param from Lower limit for count bin
#' @param from Integer. Lower limit for count bin, default = 0.
#'
#' @param to Upper limit for count bins
#' @param to Integer. Upper limit for count bins, default = 40000.
#'
#' @param by Size of each count bin
#' @param by Integer. Size of each count bin, default = 5000.
#'
#' @return A long-format tibble which contains share of DTV Categories
#' @return A long-format tibble which contains share of DTV for link types
#'
#' @export
processLinkStatsDtvDistribution <- function(joinedFrame, from = 0, to = 40000, by = 5000){
Expand Down Expand Up @@ -311,17 +311,16 @@ processLinkStatsDtvDistribution <- function(joinedFrame, from = 0, to = 40000, b
#' Categorize DTV deviation and aggregate data
#'
#' Takes a tibble from mergeCountsAndLinks.
#' Deviation between count volumes and Linkstats is calculated
#' (e.g. deviation of 1.2 means 20 percent more DTV in MATSim than in counts) and
#' categorized.
#' Deviation between count volumes and Linkstats is calculated and
#' categorized (i.e. deviation of 1.2 means 20 percent more DTV in MATSim than in counts).
#' If parameter 'aggr' is set to TRUE, data will be aggregated for each run and link type.
#' Can be used to visualize model quality by link type and to compare several runs.
#'
#' Estimation quality is determinated by the 'cut' function, limits for the label
#' 'exact' can be adjusted by tuning the parameter 'll' and 'ul'
#' Estimation quality is determined by the 'cut' function, limits for the label
#' 'exact' can be adjusted by tuning the parameters 'll' (lower limit) and 'ul' (upper limit)
#'
#' @param joinedFrame A tibble from mergeCountsAndLinks
#' @param aggr Boolean, if categorized data should returned aggregated
#' @param aggr Boolean, if categorized data should returned aggregated, default is TRUE.
#' @param ll Formula to calculate lower limit of the quality label 'exact', default = 0.8*x - 200
#' @param ul Formula to calculate lower limit of the quality label 'exact', default = 1.2*x + 200
#'
Expand All @@ -341,8 +340,8 @@ processDtvEstimationQuality <- function(joinedFrame, aggr = TRUE, ll = ~ x *0.8
join.1 <- joinedFrame %>%
mutate(ll = ifelse(ll < 0, 0, ll),
estimation = ifelse(volume < ll, "less",
ifelse(volume > ul, "more",
"exact")),
ifelse(volume > ul, "more",
"exact")),
estimation = factor(estimation, levels = c("less", "exact", "more")))

if(aggr){
Expand All @@ -361,12 +360,12 @@ processDtvEstimationQuality <- function(joinedFrame, aggr = TRUE, ll = ~ x *0.8
#' Creates a Via-Style scatterplot for each run
#'
#' Takes a tibble from mergeCountsAndLinks.
#' A scatterplot with counts on the x axis and matsim dtv on the y axis is created and colored
#' A scatterplot with counts on the x axis and MATSim dtv on the y axis is created and colored
#' by the road type.
#' Lower and upper Limits define the section which is considered as an 'exact' estimation. Limits
#' Lower and upper limits define the section which is considered an 'exact' estimation. Limits
#' are defined by custom formulas.
#'
#' The function calls the matsim-r function processDtvEstimationQuality which is handeling the limits.
#' The function calls the matsim-r function processDtvEstimationQuality which is handling the limits.
#'
#' @param joinedFrame A tibble from mergeCountsAndLinks
#' @param ll Formula to calculate lower limit of the quality label 'exact', default = 0.8*x - 200
Expand Down
Loading

0 comments on commit a5baa4f

Please sign in to comment.