Skip to content

✨ Bump version, add src/convert.jl #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "RelationalDatasets"
uuid = "6b8bdb16-f930-11eb-22b5-7ba1dabdf7a8"
license = "Apache 2.0"
authors = ["Alexander L. Hayes <[email protected]>"]
version = "0.1.0"
version = "0.1.1"

[deps]
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
Expand Down
3 changes: 3 additions & 0 deletions src/RelationalDatasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ module RelationalDatasets
include("request.jl")
export load

include("convert.jl")
export from_vector

end # module
80 changes: 80 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright © 2021 Alexander L. Hayes
# Apache 2.0 License

"""Convert (array-based) propositional datasets to a relational/ILP
representation.
"""

include("types.jl")

"""
from_vector()

Convert a classification dataset to an ILP representation.
"""
function from_vector(X::Matrix{Int64}, y::Vector{Int64}, names::Union{Vector{String}, Nothing} = nothing)

# Make sure the X and y are valid.
@assert size(y)[1] == size(X)[2]

if names == nothing
names = ["v$(i)" for i in 1:size(X)[1] + 1]
end

# Make sure the names vector makes sense.
@assert length(names) - 1 == size(X)[1]

pos, neg, facts = String[], String[], String[]

for (i, row) in enumerate(y)
if Bool(row)
push!(pos, "$(last(names))(id$(i)).")
else
push!(neg, "$(last(names))(id$(i)).")
end
end

for (i, col) in enumerate(eachrow(X))
var = names[i]
facts = vcat(facts, ["$(var)(id$(j),$(row))." for (j, row) in enumerate(col)])
end

modes = ["$(name)(+id,#var$(name))." for name in names[1:end-1]]
push!(modes, "$(last(names))(+id).")

return RelationalDataset((pos, neg, facts)), modes
end

"""
from_vector()

Convert a regression dataset to an ILP representation.
"""
function from_vector(X::Matrix{Int64}, y::Vector{Float64}, names::Union{Vector{String}, Nothing} = nothing)

# Make sure the X and y are valid.
@assert size(y)[1] == size(X)[2]

if names == nothing
names = ["v$(i)" for i in 1:size(X)[1] + 1]
end

# Make sure the names vector makes sense.
@assert length(names) - 1 == size(X)[1]

pos, neg, facts = String[], String[], String[]

for (i, row) in enumerate(y)
push!(pos, "regressionExample($(last(names))(id$(i)),$(row)).")
end

for (i, col) in enumerate(eachrow(X))
var = names[i]
facts = vcat(facts, ["$(var)(id$(j),$(row))." for (j, row) in enumerate(col)])
end

modes = ["$(name)(+id,#var$(name))." for name in names[1:end-1]]
push!(modes, "$(last(names))(+id).")

return RelationalDataset((pos, neg, facts)), modes
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ using RelationalDatasets
include("test_base.jl")
include("test_types.jl")
include("test_request.jl")
include("test_convert.jl")

end # module
125 changes: 125 additions & 0 deletions test/test_convert.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Copyright © 2021 Alexander L. Hayes
# Apache 2.0 License

module TestConvert

using Test
using RelationalDatasets

@testset "Test converting propositional datasets" begin

@test RelationalDatasets.from_vector(
[[0, 1, 1] [1, 0, 2] [2, 2, 0]],
[0, 0, 1],
) == (RelationalDatasets.RelationalDataset((
pos=["v4(id3)."],
neg=["v4(id1).", "v4(id2)."],
facts=[
"v1(id1,0).",
"v1(id2,1).",
"v1(id3,2).",
"v2(id1,1).",
"v2(id2,0).",
"v2(id3,2).",
"v3(id1,1).",
"v3(id2,2).",
"v3(id3,0).",
])),
[
"v1(+id,#varv1).",
"v2(+id,#varv2).",
"v3(+id,#varv3).",
"v4(+id).",
]
)

@test RelationalDatasets.from_vector(
[[0, 1, 1] [1, 0, 2] [2, 2, 0]],
[0.1, 0.2, 0.3],
) == (RelationalDatasets.RelationalDataset((
pos=[
"regressionExample(v4(id1),0.1).",
"regressionExample(v4(id2),0.2).",
"regressionExample(v4(id3),0.3).",
],
neg=[],
facts=[
"v1(id1,0).",
"v1(id2,1).",
"v1(id3,2).",
"v2(id1,1).",
"v2(id2,0).",
"v2(id3,2).",
"v3(id1,1).",
"v3(id2,2).",
"v3(id3,0).",
])),
[
"v1(+id,#varv1).",
"v2(+id,#varv2).",
"v3(+id,#varv3).",
"v4(+id).",
]
)

@test RelationalDatasets.from_vector(
[[1, 1] [0, 1]],
[0, 1],
["a", "b", "c"],
) == (RelationalDatasets.RelationalDataset((
pos=["c(id2)."],
neg=["c(id1)."],
facts=[
"a(id1,1).",
"a(id2,0).",
"b(id1,1).",
"b(id2,1).",
])),
[
"a(+id,#vara).",
"b(+id,#varb).",
"c(+id).",
]
)

@test RelationalDatasets.from_vector(
[[1, 1] [0, 1]],
[0.5, 1.0],
["a", "b", "c"],
) == (RelationalDatasets.RelationalDataset((
pos=["regressionExample(c(id1),0.5).", "regressionExample(c(id2),1.0)."],
neg=[],
facts=[
"a(id1,1).",
"a(id2,0).",
"b(id1,1).",
"b(id2,1).",
])),
[
"a(+id,#vara).",
"b(+id,#varb).",
"c(+id).",
]
)
end

@testset "Misaligned Classification and Regression" begin
@test_throws AssertionError RelationalDatasets.from_vector([[1] [2]], [1])
@test_throws AssertionError RelationalDatasets.from_vector([[1] [2]], [0.5])
end

@testset "Misaligned Custom Variable Names" begin
@test_throws AssertionError RelationalDatasets.from_vector(
[[1] [2]],
[1],
["a", "b", "c", "d"],
)

@test_throws AssertionError RelationalDatasets.from_vector(
[[1] [2]],
[0.5],
["a", "b", "c", "d"]
)
end

end # module