11# data structures graphical models
22
3- export FactorGraph
3+ export FactorGraph, jsondata
44
55alphabets = [:spin , :boolean , :integer , :integer_pos , :real , :real_pos ]
66
@@ -14,6 +14,9 @@ type FactorGraph{T <: Real}
1414end
1515FactorGraph {T <: Real} (order:: Int , varible_count:: Int , alphabet:: Symbol , terms:: Dict{Tuple,T} ) = FactorGraph {T} (order, varible_count, alphabet, terms, Nullable {Vector{String}} ())
1616FactorGraph {T <: Real} (matrix:: Array{T,2} ) = convert (FactorGraph{T}, matrix)
17+ FactorGraph {T <: Real} (dict:: Dict{Tuple,T} ) = convert (FactorGraph{T}, dict)
18+ FactorGraph (list:: Array{Any,1} ) = convert (FactorGraph, list)
19+
1720
1821function check_model_data {T <: Real} (order:: Int , varible_count:: Int , alphabet:: Symbol , terms:: Dict{Tuple,T} , variable_names:: Nullable{Vector{String}} )
1922 if ! in (alphabet, alphabets)
@@ -35,11 +38,14 @@ function check_model_data{T <: Real}(order::Int, varible_count::Int, alphabet::S
3538 error (" a term has an index of $(index) but it should be in the range of 1:$(varible_count) " )
3639 return false
3740 end
41+ #=
42+ # TODO see when this should be enforced
3843 if i > 1
3944 if k[i-1] > index
4045 error("the term $(k) does not have ascending indices")
4146 end
4247 end
48+ =#
4349 end
4450 end
4551 return true
@@ -53,12 +59,20 @@ function Base.show(io::IO, gm::FactorGraph)
5359 println (io, " " , get (gm. variable_names))
5460 end
5561
56- println (io, " terms: " )
57- for k in sort (collect (keys (gm. terms)))
62+ println (io, " terms: $( length (gm . terms)) " )
63+ for k in sort (collect (keys (gm. terms)), by = (x) -> ( length (x),x) )
5864 println (" " , k, " => " , gm. terms[k])
5965 end
6066end
6167
68+ function jsondata {T <: Real} (gm:: FactorGraph{T} )
69+ data = []
70+ for k in sort (collect (keys (gm. terms)), by= (x)-> (length (x),x))
71+ push! (data, Dict (" term" => k, " weight" => gm. terms[k]))
72+ end
73+ return data
74+ end
75+
6276Base. start (gm:: FactorGraph ) = start (gm. terms)
6377Base. next (gm:: FactorGraph , state) = next (gm. terms, state)
6478Base. done (gm:: FactorGraph , state) = done (gm. terms, state)
@@ -68,7 +82,6 @@ Base.length(gm::FactorGraph) = length(gm.terms)
6882Base. getindex (gm:: FactorGraph , i) = gm. terms[i]
6983Base. keys (gm:: FactorGraph ) = keys (gm. terms)
7084
71-
7285function diag_keys (gm:: FactorGraph )
7386 dkeys = Tuple[]
7487 for i in 1 : gm. varible_count
@@ -167,6 +180,48 @@ function Base.convert{T <: Real}(::Type{Dict{Tuple,T}}, m::Array{T,2})
167180end
168181
169182
183+ function Base. convert (:: Type{FactorGraph} , list:: Array{Any,1} )
184+ info (" assuming spin alphabet" )
185+ alphabet = :spin
186+
187+ max_variable = 0
188+ max_order = 0
189+ terms = Dict {Tuple,Float64} ()
190+
191+ for item in list
192+ term = item[" term" ]
193+ weight = item[" weight" ]
194+ terms[tuple (term... )] = weight
195+
196+ @assert minimum (term) > 0
197+ max_order = max (max_order, length (term))
198+ max_variable = max (max_variable, maximum (term))
199+ end
200+
201+ info (" dectected $(max_variable) variables with order $(max_order) " )
202+
203+ return FactorGraph (max_order, max_variable, alphabet, terms)
204+ end
205+
206+
207+ Base. convert {T <: Real} (:: Type{FactorGraph} , dict:: Dict{Tuple,T} ) = convert (FactorGraph{T}, dict)
208+ function Base. convert {T <: Real} (:: Type{FactorGraph{T}} , dict:: Dict{Tuple,T} )
209+ info (" assuming spin alphabet" )
210+ alphabet = :spin
211+
212+ max_variable = 0
213+ max_order = 0
214+ for (term,weight) in dict
215+ @assert minimum (term) > 0
216+ max_order = max (max_order, length (term))
217+ max_variable = max (max_variable, maximum (term))
218+ end
219+
220+ info (" dectected $(max_variable) variables with order $(max_order) " )
221+
222+ return FactorGraph (max_order, max_variable, alphabet, dict)
223+ end
224+
170225
171226permutations (items, order:: Int ; asymmetric:: Bool = false ) = sort (permutations ([], items, order, asymmetric))
172227
0 commit comments