Skip to content

Commit

Permalink
stuck on regression in polyhedral
Browse files Browse the repository at this point in the history
  • Loading branch information
antonydellavecchia committed Jan 20, 2025
1 parent af036e9 commit 215e946
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 41 deletions.
17 changes: 13 additions & 4 deletions src/Serialization/PolyhedralGeometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,25 @@ end
function load_object(s::DeserializerState, T::Type{<:PolyhedralObject}, dict::Dict)
polymake_dict = load_object(s, Dict{String, Any}, dict)
bigobject = _dict_to_bigobject(polymake_dict)
field = load_object(s, dict["_coeff"][1], dict["_coeff"][2], :_coeff)

if Base.issingletontype(dict["_coeff"][1])
field = dict["_coeff"][1]()
else
field = load_object(s, dict["_coeff"][1], dict["_coeff"][2], :_coeff)
end
return T{elem_type(field)}(bigobject, field)
end

function load_object(s::DeserializerState, T::Type{<:PolyhedralObject{S}},
dict::Dict) where S <: FieldElem
polymake_dict = load_typed_object(s)
bigobject = _dict_to_bigobject(polymake_dict)
polymake_dict = load_object(s, Dict{String, Any}, dict)
bigobject = _dict_to_bigobject(polymake_dict)
field = load_object(s, dict["_coeff"][1], dict["_coeff"][2], :_coeff)
if Base.issingletontype(dict["_coeff"][1])
field = dict["_coeff"][1]()
else
field = load_object(s, dict["_coeff"][1], dict["_coeff"][2], :_coeff)
end

return T(bigobject, field)
end

Expand Down
84 changes: 63 additions & 21 deletions src/Serialization/containers.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
################################################################################
# Saving and loading vectors

@register_serialization_type Vector uses_params

const MatVecType{T} = Union{Matrix{T}, Vector{T}, SRow{T}}
const ContainerTypes = Union{MatVecType, Set, Dict, Tuple, NamedTuple}

function type_params(obj::S) where {T, S <:MatVecType{T}}
if isempty(obj)
return S, nothing
end

params = type_params.(obj)
params_all_equal = all(map(x -> isequal(first(params), x), params))
@req params_all_equal "Not all params of Vector or Matrix entries are the same, consider using a Tuple for serialization"
return S, params[1]
end

function has_empty_entries(obj::T) where T
return false
end

function has_empty_entries(obj::T) where T <: ContainerTypes
isempty(obj) && return true
any(has_empty_entries, obj) && return true
return false
end

function type_params(obj::S) where {T <: ContainerTypes, S <:MatVecType{T}}
isempty(obj) && return S, nothing

# empty entries can inherit params from the rest of the collection
params = type_params.(filter(!has_empty_entries, obj))
params_all_equal = all(map(x -> isequal(first(params), x), params))
@req params_all_equal "Not all params of Vector or Matrix entries are the same, consider using a Tuple for serialization"
return S, params[1]
end

################################################################################
# loads to handle

function load_object(s::DeserializerState, T::Type{Vector{S}},
key::Union{Symbol, Int}) where S
load_node(s, key) do _
load_object(s, T)
end
end

################################################################################
# Saving and loading vectors
@register_serialization_type Vector

function save_type_params(s::SerializerState, T::Type{Vector{S}}, ::Nothing) where S
save_data_dict(s) do
save_object(s, encode_type(T), :name)
Expand All @@ -35,6 +67,8 @@ function save_type_params(s::SerializerState, T::Type, params::Vector)
end

function load_type_params(s::DeserializerState, T::Type{<: Union{MatVecType, Set}})
!haskey(s, :params) && return T, nothing

subtype, params = load_node(s, :params) do _
U = decode_type(s)
subtype, params = load_type_params(s, U)
Expand Down Expand Up @@ -82,6 +116,20 @@ function load_object(s::DeserializerState, ::Type{Vector{T}}, params::S) where {
end
end

function load_object(s::DeserializerState, T::Type{Vector{U}}, ::Nothing) where U
entries = load_array_node(s) do _
load_object(s, U)
end
return T(entries)
end

function load_object(s::DeserializerState, ::Type{Vector{U}}, ::Nothing) where U
entries = load_array_node(s) do _
load_object(s, U, nothing)
end
return Vector{U}(entries)
end

################################################################################
# Saving and loading matrices
@register_serialization_type Matrix
Expand Down Expand Up @@ -307,7 +355,12 @@ function load_object(s::DeserializerState,
params::Dict{S, V}) where {S <: Union{Symbol, String, Int}, U, V}
dict = T()
for k in keys(params)
dict[k] = load_object(s, params[k]..., Symbol(k))
# has no data, hence no key was generated on the data side
if Base.issingletontype(params[k][1])
dict[k] = params[k][1]()
else
dict[k] = load_object(s, params[k]..., Symbol(k))
end
end
return dict
end
Expand Down Expand Up @@ -359,27 +412,20 @@ function load_object(s::DeserializerState, S::Type{<:Set{T}}, params::Ring) wher
elems = load_array_node(s) do _
load_object(s, T, params)
end
return Set(elems)
return S(elems)
end

function load_object(s::DeserializerState, S::Type{<:Set{T}}, ::Nothing) where T
elems = load_array_node(s) do _
load_object(s, T, nothing)
load_object(s, T)
end
return Set(elems)
end

function load_object(s::DeserializerState, S::Type{<:Set{T}}) where T <: Union{String, Symbol, Int}
elems = load_array_node(s) do _
load_object(s, T, nothing)
end
return Set(elems)
return S(elems)
end

################################################################################
# Sparse rows

@register_serialization_type SRow uses_params
@register_serialization_type SRow

function save_object(s::SerializerState, obj::SRow)
save_data_array(s) do
Expand All @@ -395,11 +441,7 @@ function load_object(s::DeserializerState, ::Type{<:SRow}, params::Ring)
values = entry_type[]
load_array_node(s) do _
push!(pos, load_object(s, Int, 1))
if serialize_with_params(entry_type)
push!(values, load_object(s, entry_type, params, 2))
else
push!(values, load_object(s, entry_type, 2))
end
push!(values, load_object(s, entry_type, params, 2))
end
return sparse_row(params, pos, values)
end
12 changes: 0 additions & 12 deletions src/Serialization/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,25 +313,13 @@ function load_object(s::DeserializerState, T::Type, key::Union{Symbol, Int})
end
end

function load_object(s::DeserializerState, T::Type{Vector{S}},
key::Union{Symbol, Int}) where S
load_node(s, key) do _
load_object(s, T)
end
end

function load_object(s::DeserializerState, T::Type, params::S,
key::Union{Symbol, Int}) where S
load_node(s, key) do _
load_object(s, T, params)
end
end

function load_object(s::DeserializerState, T::Type{Vector{U}}, ::Nothing) where U
!isempty(s.obj) && error("Attempting loading non empty vector without parent")
return U[]
end

################################################################################
# serializing attributes
function save_attrs(s::SerializerState, obj::T) where T
Expand Down
8 changes: 4 additions & 4 deletions test/Serialization/PolyhedralGeometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ using Oscar: _integer_variables
f_vector(d_hedron)
lattice_points(d_hedron)

dict_ps = Dict{String, Any}(
dict_ps = Dict{String, Polyhedron}(
"unprecise" => polyhedron(
Polymake.common.convert_to{Float64}(Oscar.pm_object(d_hedron))
),
Expand All @@ -86,7 +86,7 @@ using Oscar: _integer_variables
end
end

@test_skip @testset "PolyhedralComplex" begin
@testset "PolyhedralComplex" begin
IM = incidence_matrix([[1,2,3],[1,3,4]])
vr = [0 0; 1 0; 1 1; 0 1]
PC = polyhedral_complex(IM, vr)
Expand All @@ -105,7 +105,7 @@ using Oscar: _integer_variables
end
end

@test_skip @testset "PolyhedralFan" begin
@testset "PolyhedralFan" begin
nfsquare = normal_fan(cube(2))
test_save_load_roundtrip(path, nfsquare) do loaded
@test n_rays(nfsquare) == n_rays(loaded)
Expand Down Expand Up @@ -154,7 +154,7 @@ using Oscar: _integer_variables
end
end

@test_skip @testset "SubdivisionOfPoints" begin
@testset "SubdivisionOfPoints" begin
moaepts = [4 0 0; 0 4 0; 0 0 4; 2 1 1; 1 2 1; 1 1 2]
moaeimnonreg0 = incidence_matrix([[4,5,6],[1,4,2],[2,4,5],[2,3,5],[3,5,6],[1,3,6],[1,4,6]])
MOAE = subdivision_of_points(moaepts, moaeimnonreg0)
Expand Down

0 comments on commit 215e946

Please sign in to comment.