From b05b9dbd0bbbb91d1dade7c062f7a5ba8c09ecff Mon Sep 17 00:00:00 2001 From: Max Kannenberg <95709892+MaxKannenberg@users.noreply.github.com> Date: Fri, 3 Jun 2022 12:26:58 +0200 Subject: [PATCH] Refactor the DataFrame output --- docs/examples/ExtendedWorkingExample.jl | 2 +- docs/examples/MinimalWorkingExample.jl | 3 +-- src/output.jl | 28 ++++++++++++++---------- test/runtests.jl | 29 ++++++++++++------------- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/docs/examples/ExtendedWorkingExample.jl b/docs/examples/ExtendedWorkingExample.jl index 97be3cb..9824695 100644 --- a/docs/examples/ExtendedWorkingExample.jl +++ b/docs/examples/ExtendedWorkingExample.jl @@ -19,6 +19,6 @@ settings = Settings("test/data/settings/driving_course.yaml") for p in 1:length(paths) for t in 1:length(trains) driving_course = trainrun(trains[t], paths[p], settings) - CSV.write("docs/examples/drivingCourse_path"*string(p)*"_train"*string(t)*".csv", driving_course, header=false) + CSV.write("docs/examples/drivingCourse_path"*string(p)*"_train"*string(t)*".csv", driving_course) end end diff --git a/docs/examples/MinimalWorkingExample.jl b/docs/examples/MinimalWorkingExample.jl index ba4577d..0c7b449 100644 --- a/docs/examples/MinimalWorkingExample.jl +++ b/docs/examples/MinimalWorkingExample.jl @@ -5,7 +5,6 @@ using TrainRuns train = Train("test/data/trains/freight.yaml") path = Path("test/data/paths/const.yaml") -runtime_dataFrame = trainrun(train, path) -runtime = runtime_dataFrame[!, 1][2] +runtime = trainrun(train, path)[end, :t] println("The train needs $runtime seconds for the running path.") diff --git a/src/output.jl b/src/output.jl index b838ac7..dca58ea 100644 --- a/src/output.jl +++ b/src/output.jl @@ -45,24 +45,30 @@ end function createDataFrame(output_vector::Vector{Dict}, outputDetail) if outputDetail == :running_time - # create DataFrame with running time information - dataFrame = DataFrame(column1=["t (in s)", output_vector[end][:t]]) + # create a DataFrame with running time information + dataFrame = DataFrame(t=[output_vector[end][:t]]) else # :points_of_interest or :driving_course - header = ["label", "driving mode", "s (in m)", "v (in m/s)", "t (in s)", "a (in m/s^2)", "F_T (in N)", "F_R (in N)", "R_path (in N)", "R_traction (in N)", "R_wagons (in N)"] columnSymbols = [:label, :behavior, :s, :v, :t, :a, :F_T, :F_R, :R_path, :R_traction, :R_wagons] - allColumns = Array{Any,1}[] - for column in 1:length(header) - currentColumn = Any[] - push!(currentColumn, header[column]) - for point in output_vector - push!(currentColumn, point[columnSymbols[column]]) + allColumns = [] + for column in 1:length(columnSymbols) + if typeof(output_vector[1][columnSymbols[column]]) == String + currentStringColumn::Vector{String} = [] + for point in output_vector + push!(currentStringColumn, point[columnSymbols[column]]) + end + push!(allColumns, currentStringColumn) + elseif typeof(output_vector[1][columnSymbols[column]]) <: Real + currentRealColumn::Vector{Real} = [] + for point in output_vector + push!(currentRealColumn, point[columnSymbols[column]]) + end + push!(allColumns, currentRealColumn) end - push!(allColumns, currentColumn) end # for # combine the columns in a data frame - dataFrame = DataFrame(c1=allColumns[1], c2=allColumns[2],c3=allColumns[3], c4=allColumns[4], c5=allColumns[5], c6=allColumns[6], c7=allColumns[7], c8=allColumns[8], c9=allColumns[9], c10=allColumns[10], c11=allColumns[11]) + dataFrame = DataFrame(label=allColumns[1], driving_mode=allColumns[2], s=allColumns[3], v=allColumns[4], t=allColumns[5], a=allColumns[6], F_T=allColumns[7], F_R=allColumns[8], R_path=allColumns[9], R_traction=allColumns[10], R_wagons=allColumns[11]) end return dataFrame diff --git a/test/runtests.jl b/test/runtests.jl index 3019c23..7e8de06 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -14,24 +14,24 @@ settings = Dict() @testset "load data" begin println("testing load train data") - push!(trains, :freight => @time Train("test/data/trains/freight.yaml")) - push!(trains, :local => @time Train("test/data/trains/local.yaml")) - push!(trains, :longdistance => @time Train("test/data/trains/longdistance.yaml")) + push!(trains, :freight => @time Train("data/trains/freight.yaml")) + push!(trains, :local => @time Train("data/trains/local.yaml")) + push!(trains, :longdistance => @time Train("data/trains/longdistance.yaml")) println("testing load path data") - push!(paths, :const => @time Path("test/data/paths/const.yaml")) - push!(paths, :slope => @time Path("test/data/paths/slope.yaml")) - push!(paths, :speed => @time Path("test/data/paths/speed.yaml")) - push!(paths, :realworld => @time Path("test/data/paths/realworld.yaml")) + push!(paths, :const => @time Path("data/paths/const.yaml")) + push!(paths, :slope => @time Path("data/paths/slope.yaml")) + push!(paths, :speed => @time Path("data/paths/speed.yaml")) + push!(paths, :realworld => @time Path("data/paths/realworld.yaml")) println("testing load settings data") push!(settings, "default" => @time Settings()) - push!(settings, "poi" => @time Settings("test/data/settings/points_of_interest.yaml")) - push!(settings, "drivingcourse" => @time Settings("test/data/settings/driving_course.yaml")) - push!(settings, "strip" => @time Settings("test/data/settings/strip.yaml")) - push!(settings, "time" => @time Settings("test/data/settings/time.yaml")) - push!(settings, "timestrip" => @time Settings("test/data/settings/time_strip.yaml")) - push!(settings, "velocity" => @time Settings("test/data/settings/velocity.yaml")) + push!(settings, "poi" => @time Settings("data/settings/points_of_interest.yaml")) + push!(settings, "drivingcourse" => @time Settings("data/settings/driving_course.yaml")) + push!(settings, "strip" => @time Settings("data/settings/strip.yaml")) + push!(settings, "time" => @time Settings("data/settings/time.yaml")) + push!(settings, "timestrip" => @time Settings("data/settings/time_strip.yaml")) + push!(settings, "velocity" => @time Settings("data/settings/velocity.yaml")) @test typeof(first(paths)[2]) == Path @test typeof(first(settings)[2]) == Settings @@ -74,8 +74,7 @@ anticipated = Dict( for test in tests test_name = String(test[1][1]) * "_" * String(test[2][1]) println("testing $test_name") - @time result_dataFrame = trainrun(test[1][2], test[2][2]) - result = result_dataFrame[!, 1][2] + @time result = trainrun(test[1][2], test[2][2])[end, :t] expected = anticipated[:default][Symbol(test_name)] # compare result to test data set @test isapprox(result, expected, rtol=0.01)