Skip to content

Commit db8d62c

Browse files
d-monnettmigot
andauthored
add csv export option (#95)
* 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]>
1 parent 72d2f25 commit db8d62c

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@ uuid = "ecbce9bc-3e5e-569d-9e29-55181f61f8d0"
33
version = "0.4.3"
44

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

1113
[compat]
14+
CSV = "0.10"
1215
LaTeXStrings = "^1.3"
1316
NaNMath = "0.3, 1"
1417
Requires = "1"
1518
julia = "^1.6"
19+
Tables = "1.11"
1620

1721
[extras]
1822
PGFPlotsX = "8314cec4-20b6-5062-9cdb-752b83310925"

src/BenchmarkProfiles.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import NaNMath
55
using Requires
66
using Printf
77

8-
export performance_ratios, performance_profile, performance_profile_data
8+
export performance_ratios, performance_profile, performance_profile_data, export_performance_profile
99
export data_ratios, data_profile
1010
export bp_backends, PlotsBackend, UnicodePlotsBackend, PGFPlotsXBackend
1111

src/performance_profiles.jl

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55
# D. Orban, 2015, 2016.
66

7+
using CSV, Tables
8+
79
"""Compute performance ratios used to produce a performance profile.
810
911
There is normally no need to call this function directly.
@@ -152,3 +154,57 @@ function performance_profile(
152154
kwargs...,
153155
)
154156
end
157+
158+
"""
159+
export_performance_profile(T, filename; solver_names = [], header, kwargs...)
160+
161+
Export a performance profile plot data as .csv file. Profiles data are padded with `NaN` to ensure .csv consistency.
162+
163+
## Arguments
164+
165+
* `T :: Matrix{Float64}`: each column of `T` defines the performance data for a solver (smaller is better).
166+
Failures on a given problem are represented by a negative value, an infinite value, or `NaN`.
167+
* `filename :: String` : path to the export file.
168+
169+
## Keyword Arguments
170+
171+
* `solver_names :: Vector{S}` : names of the solvers
172+
* `header::Vector{String}`: Contains .csv file column names. Note that `header` value does not change columns order in .csv exported files (see Output).
173+
174+
Other keyword arguments are passed `performance_profile_data`.
175+
176+
Output:
177+
File containing profile data in .csv format. Columns are solver1_x, solver1_y, solver2_x, ...
178+
"""
179+
function export_performance_profile(
180+
T::Matrix{Float64},
181+
filename::String;
182+
solver_names::Vector{S} = String[],
183+
header::Vector{S} = String[],
184+
kwargs...
185+
) where {S <: AbstractString}
186+
nsolvers = size(T)[2]
187+
188+
x_data, y_data, max_ratio = performance_profile_data(T;kwargs...)
189+
max_elem = maximum(length.(x_data))
190+
for i in eachindex(x_data)
191+
append!(x_data[i],[NaN for i=1:max_elem-length(x_data[i])])
192+
append!(y_data[i],[NaN for i=1:max_elem-length(y_data[i])])
193+
end
194+
x_mat = hcat(x_data...)
195+
y_mat = hcat(y_data...)
196+
197+
isempty(solver_names) && (solver_names = ["solver_$i" for i = 1:nsolvers])
198+
199+
if !isempty(header)
200+
header_l = size(T)[2]*2
201+
length(header) == header_l || error("Header should contain $(header_l) elements")
202+
header = vcat([[sname*"_x",sname*"_y"] for sname in solver_names]...)
203+
end
204+
data = Matrix{Float64}(undef,max_elem,nsolvers*2)
205+
for i =0:nsolvers-1
206+
data[:,2*i+1] .= x_mat[:,i+1]
207+
data[:,2*i+2] .= y_mat[:,i+1]
208+
end
209+
CSV.write(filename,Tables.table(data),header=header)
210+
end

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,15 @@ if !Sys.isfreebsd() # GR_jll not available, so Plots won't install
6161
profile = data_profile(PGFPlotsXBackend(), H, T, labels)
6262
@test isa(profile, Plots.Plot)
6363
end
64+
65+
@testset "csv export" begin
66+
T = 10 * rand(25, 3)
67+
filename = "data.csv"
68+
export_performance_profile(T,filename)
69+
@test isfile(filename)
70+
rm(filename)
71+
export_performance_profile(T,filename,header = ["" for _=1:size(T,2)*2])
72+
@test isfile(filename)
73+
rm(filename)
74+
end
6475
end

0 commit comments

Comments
 (0)