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 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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
41 changes: 41 additions & 0 deletions experimental/AlgebraicStatistics/src/serialization.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@register_serialization_type PhylogeneticModel
@register_serialization_type GroupBasedPhylogeneticModel

function save_object(s::SerializerState, pm::PhylogeneticModel)
save_data_dict(s) do
save_object(s, graph(pm), :tree) # already implemented
save_object(s, number_states(pm), :states) # already implemented
save_object(s, probability_ring(pm), :probability_ring) # already implemented
mariusneu marked this conversation as resolved.
Show resolved Hide resolved
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_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_object(s, fourier_ring(pm), :fourier_ring) # already implemented
mariusneu marked this conversation as resolved.
Show resolved Hide resolved
save_object(s, fourier_parameters(pm), :fourier_params) # needs to be implemented
save_object(s,group_of_model(pm), :group) # already implemented
end
end


function load_object(s::DeserializerState, ::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_object(s, QQMPolyRing, :probability_ring)
mariusneu marked this conversation as resolved.
Show resolved Hide resolved
root_distr = load_object(s, Vector{Any}, :root_distribution)
trans_matrices = load_object(s, Dict{Edge, MatElem{QQMPolyRingElem}}, :trans_matrices)

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

function load_object(s::DeserializerState, ::Type{<: GroupBasedPhylogeneticModel})
phylomodel = load_object(s, PhylogeneticModel, :phylomodel)
fourier_ring = load_object(s, QQMPolyRing, :fourier_ring)
mariusneu marked this conversation as resolved.
Show resolved Hide resolved
fourier_params = load_object(s, Dict{Edge, Vector{QQMPolyRingElem}} , :fourier_params)
group = load_object(s, Vector{FinGenAbGroupElem}, :group)

return GroupBasedPhylogeneticModel(phylomodel, fourier_ring, fourier_params, group)
end
14 changes: 14 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 "Edge"

function save_object(s::SerializerState, g::Graph{T}) where T <: Union{Directed, Undirected}
smallobject = pm_object(g)
Expand All @@ -19,6 +20,19 @@ 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_dict(s) do
Copy link
Collaborator

Choose a reason for hiding this comment

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

this can be done using save_data_array instead, it'll make file sizes smaller

save_object(s, src(e), :source)
save_object(s, dst(e), :target)
end
end

function load_object(s::DeserializerState, e::Type{Edge})
source = load_object(s, Int64, :source)
target = load_object(s, Int64, :target)
return Edge(source, target)
end

###############################################################################
## Matroid
###############################################################################
Expand Down
Loading