Skip to content
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

first attempts at writing the serializers for the new types PhylogeneticModel and GroupBasedPhylogeneticModel #4010

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 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: 2 additions & 0 deletions experimental/AlgebraicStatistics/src/AlgebraicStatistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ include("PhylogeneticModels.jl")
include("PhylogeneticAuxiliary.jl")
include("PhylogeneticParametrization.jl")

include("serialization.jl")

#export models
export cavender_farris_neyman_model
export jukes_cantor_model
Expand Down
2 changes: 1 addition & 1 deletion experimental/AlgebraicStatistics/src/PhylogeneticModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct PhylogeneticModel
graph::Graph{Directed}
n_states::Int
prob_ring::MPolyRing{QQFieldElem}
root_distr::Vector{Any}
root_distr::Vector
trans_matrices::Dict{Edge, MatElem{QQMPolyRingElem}}
end

Expand Down
42 changes: 42 additions & 0 deletions experimental/AlgebraicStatistics/src/serialization.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@register_serialization_type PhylogeneticModel
@register_serialization_type GroupBasedPhylogeneticModel

function save_object(s::SerializerState, pm::PhylogeneticModel)
save_data_dict(s) do
save_typed_object(s, graph(pm), :tree) # already implemented
save_object(s, number_states(pm), :states) # already implemented
save_typed_object(s, probability_ring(pm), :probability_ring) # already implemented
save_object(s, root_distribution(pm), :root_distribution) # needs to be implemented, or we change the entry type to QQFieldElem
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can using save_typed_object here if the type isn't always know, or if your using a vector any because not all entries are the same type you'll need to use a tuple

save_typed_object(s, transition_matrices(pm), :transition_matrices) # needs to be implemented
end
end

function save_object(s::SerializerState, pm::GroupBasedPhylogeneticModel)
save_data_dict(s) do
save_object(s, phylogenetic_model(pm), :phylomodel) # details see above
save_typed_object(s, fourier_ring(pm), :fourier_ring) # already implemented
save_typed_object(s, fourier_parameters(pm), :fourier_params) # needs to be implemented
save_typed_object(s, group_of_model(pm), :group) # already implemented
end
end


function load_object(s::DeserializerState, g::Type{PhylogeneticModel})
graph = load_object(s, Graph{Directed}, :tree)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you would need to use

Suggested change
graph = load_object(s, Graph{Directed}, :tree)
graph = load_typed_object(s, :tree)

n_states = load_object(s, Int64, :states)
prob_ring = load_typed_object(s, :probability_ring)
root_distr = load_object(s, Vector, QQ, :root_distribution)
params = load_type_params(s, Dict{Edge, MatElem{QQMPolyRingElem}})
trans_matrices = load_typed_object(s, :transition_matrices, params)

return PhylogeneticModel(graph, n_states, prob_ring, root_distr, trans_matrices)
end

function load_object(s::DeserializerState, g::Type{GroupBasedPhylogeneticModel})
phylomodel = load_object(s, PhylogeneticModel, :phylomodel)
fourier_ring = load_typed_object(s, :fourier_ring)
fourier_params = load_typed_object(s, :fourier_params)
group = load_typed_object(s, :group)

return GroupBasedPhylogeneticModel(phylomodel, fourier_ring, fourier_params, group)
end
12 changes: 12 additions & 0 deletions src/Serialization/Combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using JSON
###############################################################################
@register_serialization_type Graph{Directed} "Graph{Directed}"
@register_serialization_type Graph{Undirected} "Graph{Undirected}"
@register_serialization_type Edge

function save_object(s::SerializerState, g::Graph{T}) where T <: Union{Directed, Undirected}
smallobject = pm_object(g)
Expand All @@ -19,6 +20,17 @@ function load_object(s::DeserializerState, g::Type{Graph{T}}) where T <: Union{D
return g(smallobj)
end

function save_object(s::SerializerState, e::Edge)
save_data_array(s) do
save_object(s, src(e))
save_object(s, dst(e))
end
end

function load_object(s::DeserializerState, e::Type{Edge})
return Edge(load_object(s, Int, 1), load_object(s, Int, 2))
end

###############################################################################
## Matroid
###############################################################################
Expand Down
16 changes: 10 additions & 6 deletions src/Serialization/containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,24 +413,28 @@ function load_object(s::DeserializerState, ::Type{<:Dict}, params::Dict{Symbol,

if key_type == Int
key = parse(Int, string(k))
elseif haskey(params, :key_params) # type is not Int, String or Symbol
elseif key_type <: Union{Symbol, String}
key = key_type(k)
elseif serialize_with_params(key_type)
load_node(s, i) do _
# 1 is for first entry of tuple which is the key in this case
key = load_object(s, key_type, params[:key_params], 1)
end
else
key = key_type(k)
else
load_node(s,i) do _
key = load_object(s, key_type, 1)
end
end

if value_type != Any
if serialize_with_params(value_type)
if haskey(params, :key_params) # key type is not Int, String or Symbol
if key_type <: Union{Symbol, String}
dict[key] = load_object(s, value_type, params[:value_params], k)
else
load_node(s, i) do _
# 2 is for second entry of tuple which is the value in this case
dict[key] = load_object(s, value_type, params[:value_params], 2)
end
else
dict[key] = load_object(s, value_type, params[:value_params], k)
end
else
if haskey(params, :key_params) # key type is not Int, String or Symbol
Expand Down
Loading