Skip to content

Commit

Permalink
add csv export option (#95)
Browse files Browse the repository at this point in the history
* add csv export option

* Update src/performance_profiles.jl

Co-authored-by: tmigot <[email protected]>

* Update src/performance_profiles.jl

Co-authored-by: tmigot <[email protected]>

* Update src/performance_profiles.jl

Co-authored-by: tmigot <[email protected]>

* Update src/performance_profiles.jl

Co-authored-by: tmigot <[email protected]>

* update signature
add header length check

* Update documentation

* add test for coverage

* fix doc

---------

Co-authored-by: tmigot <[email protected]>
  • Loading branch information
d-monnet and tmigot authored Oct 17, 2023
1 parent 72d2f25 commit db8d62c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ uuid = "ecbce9bc-3e5e-569d-9e29-55181f61f8d0"
version = "0.4.3"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
CSV = "0.10"
LaTeXStrings = "^1.3"
NaNMath = "0.3, 1"
Requires = "1"
julia = "^1.6"
Tables = "1.11"

[extras]
PGFPlotsX = "8314cec4-20b6-5062-9cdb-752b83310925"
Expand Down
2 changes: 1 addition & 1 deletion src/BenchmarkProfiles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import NaNMath
using Requires
using Printf

export performance_ratios, performance_profile, performance_profile_data
export performance_ratios, performance_profile, performance_profile_data, export_performance_profile
export data_ratios, data_profile
export bp_backends, PlotsBackend, UnicodePlotsBackend, PGFPlotsXBackend

Expand Down
56 changes: 56 additions & 0 deletions src/performance_profiles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# D. Orban, 2015, 2016.

using CSV, Tables

"""Compute performance ratios used to produce a performance profile.
There is normally no need to call this function directly.
Expand Down Expand Up @@ -152,3 +154,57 @@ function performance_profile(
kwargs...,
)
end

"""
export_performance_profile(T, filename; solver_names = [], header, kwargs...)
Export a performance profile plot data as .csv file. Profiles data are padded with `NaN` to ensure .csv consistency.
## Arguments
* `T :: Matrix{Float64}`: each column of `T` defines the performance data for a solver (smaller is better).
Failures on a given problem are represented by a negative value, an infinite value, or `NaN`.
* `filename :: String` : path to the export file.
## Keyword Arguments
* `solver_names :: Vector{S}` : names of the solvers
* `header::Vector{String}`: Contains .csv file column names. Note that `header` value does not change columns order in .csv exported files (see Output).
Other keyword arguments are passed `performance_profile_data`.
Output:
File containing profile data in .csv format. Columns are solver1_x, solver1_y, solver2_x, ...
"""
function export_performance_profile(
T::Matrix{Float64},
filename::String;
solver_names::Vector{S} = String[],
header::Vector{S} = String[],
kwargs...
) where {S <: AbstractString}
nsolvers = size(T)[2]

x_data, y_data, max_ratio = performance_profile_data(T;kwargs...)
max_elem = maximum(length.(x_data))
for i in eachindex(x_data)
append!(x_data[i],[NaN for i=1:max_elem-length(x_data[i])])
append!(y_data[i],[NaN for i=1:max_elem-length(y_data[i])])
end
x_mat = hcat(x_data...)
y_mat = hcat(y_data...)

isempty(solver_names) && (solver_names = ["solver_$i" for i = 1:nsolvers])

if !isempty(header)
header_l = size(T)[2]*2
length(header) == header_l || error("Header should contain $(header_l) elements")
header = vcat([[sname*"_x",sname*"_y"] for sname in solver_names]...)
end
data = Matrix{Float64}(undef,max_elem,nsolvers*2)
for i =0:nsolvers-1
data[:,2*i+1] .= x_mat[:,i+1]
data[:,2*i+2] .= y_mat[:,i+1]
end
CSV.write(filename,Tables.table(data),header=header)
end
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,15 @@ if !Sys.isfreebsd() # GR_jll not available, so Plots won't install
profile = data_profile(PGFPlotsXBackend(), H, T, labels)
@test isa(profile, Plots.Plot)
end

@testset "csv export" begin
T = 10 * rand(25, 3)
filename = "data.csv"
export_performance_profile(T,filename)
@test isfile(filename)
rm(filename)
export_performance_profile(T,filename,header = ["" for _=1:size(T,2)*2])
@test isfile(filename)
rm(filename)
end
end

0 comments on commit db8d62c

Please sign in to comment.