Skip to content

Commit

Permalink
Merge pull request #377 from NREL-Sienna/gks/add_function_data_printing
Browse files Browse the repository at this point in the history
Add `FunctionData` Printing
  • Loading branch information
GabrielKS authored Jun 14, 2024
2 parents 29940e9 + 5c19680 commit c6f8c35
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/function_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ function transform_array_for_hdf(
return transform_array_for_hdf(transfd_data)
end

function Base.show(io::IO, ::MIME"text/plain", fd::LinearFunctionData)
get(io, :compact, false)::Bool || print(io, "$(typeof(fd)) representing function ")
print(io, "f(x) = $(fd.proportional_term) x + $(fd.constant_term)")
end

"""
Structure to represent the underlying data of quadratic functions. Principally used for the
representation of cost functions
Expand Down Expand Up @@ -94,6 +99,14 @@ function _validate_piecewise_x(x_coords::Vector)
end
end

function Base.show(io::IO, ::MIME"text/plain", fd::QuadraticFunctionData)
get(io, :compact, false)::Bool || print(io, "$(typeof(fd)) representing function ")
print(
io,
"f(x) = $(fd.quadratic_term) x^2 + $(fd.proportional_term) x + $(fd.constant_term)",
)
end

"""
Structure to represent piecewise linear data as a series of points: two points define one
segment, three points define two segments, etc. The curve starts at the first point given,
Expand Down Expand Up @@ -191,6 +204,18 @@ function transform_array_for_hdf(
return transform_array_for_hdf(transfd_data)
end

function Base.show(io::IO, ::MIME"text/plain", fd::PiecewiseLinearData)
if get(io, :compact, false)::Bool
print(io, "piecewise linear ")
else
print(io, "$(typeof(fd)) representing piecewise linear function ")
end
print(io, "y = f(x) connecting points:")
for point in fd.points
print(io, "\n $point")
end
end

"""
Structure to represent a step function as a series of endpoint x-coordinates and segment
y-coordinates: two x-coordinates and one y-coordinate defines a single segment, three
Expand Down Expand Up @@ -274,6 +299,15 @@ function transform_array_for_hdf(
return transform_array_for_hdf(transfd_data)
end

function Base.show(io::IO, ::MIME"text/plain", fd::PiecewiseStepData)
get(io, :compact, false)::Bool ||
print(io, "$(typeof(fd)) representing step (piecewise constant) function ")
print(io, "f(x) =")
for (y, x1, x2) in zip(fd.y_coords, fd.x_coords[1:(end - 1)], fd.x_coords[2:end])
print(io, "\n $y for x in [$x1, $x2)")
end
end

"""
Calculates the x-length of each segment of a piecewise curve.
"""
Expand Down
32 changes: 31 additions & 1 deletion test/test_function_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ end
end
end

@testset "Test equality with NaN" begin
@testset "Test FunctionData equality with NaN" begin
examples_1 = get_more_test_function_data()
examples_2 = get_more_test_function_data()

Expand All @@ -179,3 +179,33 @@ end
@test hash(examples_1[(my_type, false)]) != hash(examples_2[(my_type, true)])
end
end

@testset "Test FunctionData printing" begin
repr_answers = [
"InfrastructureSystems.LinearFunctionData(5.0, 1.0)",
"InfrastructureSystems.QuadraticFunctionData(2.0, 3.0, 4.0)",
"InfrastructureSystems.PiecewiseLinearData(@NamedTuple{x::Float64, y::Float64}[(x = 1.0, y = 1.0), (x = 3.0, y = 5.0), (x = 5.0, y = 10.0)])",
"InfrastructureSystems.PiecewiseStepData([1.0, 3.0, 5.0], [2.0, 2.5])",
]
plain_answers = [
"InfrastructureSystems.LinearFunctionData representing function f(x) = 5.0 x + 1.0",
"InfrastructureSystems.QuadraticFunctionData representing function f(x) = 2.0 x^2 + 3.0 x + 4.0",
"InfrastructureSystems.PiecewiseLinearData representing piecewise linear function y = f(x) connecting points:\n (x = 1.0, y = 1.0)\n (x = 3.0, y = 5.0)\n (x = 5.0, y = 10.0)",
"InfrastructureSystems.PiecewiseStepData representing step (piecewise constant) function f(x) =\n 2.0 for x in [1.0, 3.0)\n 2.5 for x in [3.0, 5.0)",
]
compact_plain_answers = [
"f(x) = 5.0 x + 1.0",
"f(x) = 2.0 x^2 + 3.0 x + 4.0",
"piecewise linear y = f(x) connecting points:\n (x = 1.0, y = 1.0)\n (x = 3.0, y = 5.0)\n (x = 5.0, y = 10.0)",
"f(x) =\n 2.0 for x in [1.0, 3.0)\n 2.5 for x in [3.0, 5.0)",
]

for (fd, repr_ans, plain_ans, compact_plain_ans) in
zip(get_test_function_data(), repr_answers, plain_answers, compact_plain_answers)
@test sprint(show, fd) == repr(fd) == repr_ans
@test sprint(show, "text/plain", fd) ==
sprint(show, "text/plain", fd; context = :compact => false) == plain_ans
@test sprint(show, "text/plain", fd; context = :compact => true) ==
compact_plain_ans
end
end
2 changes: 1 addition & 1 deletion test/test_printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)
io = IOBuffer()
for mime in ("text/plain", "text/html")
show(io, "text/plain", sys)
show(io, mime, sys)
text = String(take!(io))
@test occursin("TestComponent", text)
@test occursin("time_series_type", text)
Expand Down

0 comments on commit c6f8c35

Please sign in to comment.