Refactor the DataFrame output

development
Max Kannenberg 2022-06-03 12:26:58 +02:00
parent 99c873a4e7
commit b05b9dbd0b
4 changed files with 33 additions and 29 deletions

View File

@ -19,6 +19,6 @@ settings = Settings("test/data/settings/driving_course.yaml")
for p in 1:length(paths) for p in 1:length(paths)
for t in 1:length(trains) for t in 1:length(trains)
driving_course = trainrun(trains[t], paths[p], settings) 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
end end

View File

@ -5,7 +5,6 @@ using TrainRuns
train = Train("test/data/trains/freight.yaml") train = Train("test/data/trains/freight.yaml")
path = Path("test/data/paths/const.yaml") path = Path("test/data/paths/const.yaml")
runtime_dataFrame = trainrun(train, path) runtime = trainrun(train, path)[end, :t]
runtime = runtime_dataFrame[!, 1][2]
println("The train needs $runtime seconds for the running path.") println("The train needs $runtime seconds for the running path.")

View File

@ -45,24 +45,30 @@ end
function createDataFrame(output_vector::Vector{Dict}, outputDetail) function createDataFrame(output_vector::Vector{Dict}, outputDetail)
if outputDetail == :running_time if outputDetail == :running_time
# create DataFrame with running time information # create a DataFrame with running time information
dataFrame = DataFrame(column1=["t (in s)", output_vector[end][:t]]) dataFrame = DataFrame(t=[output_vector[end][:t]])
else # :points_of_interest or :driving_course 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] columnSymbols = [:label, :behavior, :s, :v, :t, :a, :F_T, :F_R, :R_path, :R_traction, :R_wagons]
allColumns = Array{Any,1}[] allColumns = []
for column in 1:length(header) for column in 1:length(columnSymbols)
currentColumn = Any[] if typeof(output_vector[1][columnSymbols[column]]) == String
push!(currentColumn, header[column]) currentStringColumn::Vector{String} = []
for point in output_vector for point in output_vector
push!(currentColumn, point[columnSymbols[column]]) 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 end
push!(allColumns, currentColumn)
end # for end # for
# combine the columns in a data frame # 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 end
return dataFrame return dataFrame

View File

@ -14,24 +14,24 @@ settings = Dict()
@testset "load data" begin @testset "load data" begin
println("testing load train data") println("testing load train data")
push!(trains, :freight => @time Train("test/data/trains/freight.yaml")) push!(trains, :freight => @time Train("data/trains/freight.yaml"))
push!(trains, :local => @time Train("test/data/trains/local.yaml")) push!(trains, :local => @time Train("data/trains/local.yaml"))
push!(trains, :longdistance => @time Train("test/data/trains/longdistance.yaml")) push!(trains, :longdistance => @time Train("data/trains/longdistance.yaml"))
println("testing load path data") println("testing load path data")
push!(paths, :const => @time Path("test/data/paths/const.yaml")) push!(paths, :const => @time Path("data/paths/const.yaml"))
push!(paths, :slope => @time Path("test/data/paths/slope.yaml")) push!(paths, :slope => @time Path("data/paths/slope.yaml"))
push!(paths, :speed => @time Path("test/data/paths/speed.yaml")) push!(paths, :speed => @time Path("data/paths/speed.yaml"))
push!(paths, :realworld => @time Path("test/data/paths/realworld.yaml")) push!(paths, :realworld => @time Path("data/paths/realworld.yaml"))
println("testing load settings data") println("testing load settings data")
push!(settings, "default" => @time Settings()) push!(settings, "default" => @time Settings())
push!(settings, "poi" => @time Settings("test/data/settings/points_of_interest.yaml")) push!(settings, "poi" => @time Settings("data/settings/points_of_interest.yaml"))
push!(settings, "drivingcourse" => @time Settings("test/data/settings/driving_course.yaml")) push!(settings, "drivingcourse" => @time Settings("data/settings/driving_course.yaml"))
push!(settings, "strip" => @time Settings("test/data/settings/strip.yaml")) push!(settings, "strip" => @time Settings("data/settings/strip.yaml"))
push!(settings, "time" => @time Settings("test/data/settings/time.yaml")) push!(settings, "time" => @time Settings("data/settings/time.yaml"))
push!(settings, "timestrip" => @time Settings("test/data/settings/time_strip.yaml")) push!(settings, "timestrip" => @time Settings("data/settings/time_strip.yaml"))
push!(settings, "velocity" => @time Settings("test/data/settings/velocity.yaml")) push!(settings, "velocity" => @time Settings("data/settings/velocity.yaml"))
@test typeof(first(paths)[2]) == Path @test typeof(first(paths)[2]) == Path
@test typeof(first(settings)[2]) == Settings @test typeof(first(settings)[2]) == Settings
@ -74,8 +74,7 @@ anticipated = Dict(
for test in tests for test in tests
test_name = String(test[1][1]) * "_" * String(test[2][1]) test_name = String(test[1][1]) * "_" * String(test[2][1])
println("testing $test_name") println("testing $test_name")
@time result_dataFrame = trainrun(test[1][2], test[2][2]) @time result = trainrun(test[1][2], test[2][2])[end, :t]
result = result_dataFrame[!, 1][2]
expected = anticipated[:default][Symbol(test_name)] expected = anticipated[:default][Symbol(test_name)]
# compare result to test data set # compare result to test data set
@test isapprox(result, expected, rtol=0.01) @test isapprox(result, expected, rtol=0.01)