From d2b14de4622a977a79e82b05afbb67cd5ee6a067 Mon Sep 17 00:00:00 2001 From: dwijenchawra Date: Mon, 10 May 2021 20:09:22 -0700 Subject: [PATCH 1/9] fixed matrix conversion --- src/DiscreteBayesNet/discrete_bayes_net.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/DiscreteBayesNet/discrete_bayes_net.jl b/src/DiscreteBayesNet/discrete_bayes_net.jl index ecefeb3..67b95d8 100644 --- a/src/DiscreteBayesNet/discrete_bayes_net.jl +++ b/src/DiscreteBayesNet/discrete_bayes_net.jl @@ -244,7 +244,8 @@ function statistics(dag::DAG, data::DataFrame) parents = [inneighbors(dag, i) for i in 1:n] ncategories = [Int(infer_number_of_instantiations(data[!,i])) for i in 1 : n] - datamat = convert(Matrix{Int}, data)' +# datamat = convert(Matrix{Int}, data)' + datamat = Matrix{Int}(data) statistics(parents, ncategories, datamat) end @@ -255,8 +256,8 @@ function statistics(bn::DiscreteBayesNet, target::NodeName, data::DataFrame) targetind = bn.name_to_index[target] parents = inneighbors(bn.dag, targetind) ncategories = [Int(infer_number_of_instantiations(data[!,i])) for i in 1 : n] - datamat = convert(Matrix{Int}, data)' - +# datamat = convert(Matrix{Int}, data)' + datamat = Matrix{Int}(data) statistics(targetind, parents, ncategories, datamat) end From ae67c78858c24690b401e951004f746e7b7ab07c Mon Sep 17 00:00:00 2001 From: dwijenchawra Date: Mon, 10 May 2021 20:09:42 -0700 Subject: [PATCH 2/9] added auto parameter --- src/Factors/factors_dataframes.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Factors/factors_dataframes.jl b/src/Factors/factors_dataframes.jl index 630cc7b..53fd2ce 100644 --- a/src/Factors/factors_dataframes.jl +++ b/src/Factors/factors_dataframes.jl @@ -7,7 +7,7 @@ Convert a Factor to a DataFrame """ function Base.convert(::Type{DataFrame}, ϕ::Factor) - df = DataFrames.DataFrame(pattern(ϕ)) + df = DataFrames.DataFrame(pattern(ϕ), :auto) DataFrames.rename!(df, [f => t for (f, t) = zip(names(df), names(ϕ))] ) df[!,:potential] = ϕ.potential[:] From d2e62c0062e631a271ed2a25c8463ac3a244ecb4 Mon Sep 17 00:00:00 2001 From: dwijenchawra Date: Mon, 10 May 2021 20:10:08 -0700 Subject: [PATCH 3/9] fixed dataframe conversion --- src/gibbs.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gibbs.jl b/src/gibbs.jl index 3ef3993..54e5e0f 100644 --- a/src/gibbs.jl +++ b/src/gibbs.jl @@ -241,7 +241,7 @@ function gibbs_sample_main_loop( end - return convert(DataFrame, t), (now() - start_time).value + return DataFrame(t), (now() - start_time).value end """ From 430ffa8989aea3b85d6d6cfd3730f1cd1c4f8645 Mon Sep 17 00:00:00 2001 From: dwijenchawra Date: Mon, 10 May 2021 20:10:32 -0700 Subject: [PATCH 4/9] updated dataframe reqs --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 1d846c4..9928020 100644 --- a/Project.toml +++ b/Project.toml @@ -26,7 +26,7 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" [compat] -DataFrames = "0.21,0.22" +DataFrames = "1.1.0" DataStructures = "0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18" Discretizers = "3.0" Distributions = "0.17,0.18,0.19,0.20,0.21,0.22,0.23,0.24" From 150f28ec4f978682269e3651e7544934c7df0b67 Mon Sep 17 00:00:00 2001 From: dwijenchawra Date: Mon, 10 May 2021 20:11:11 -0700 Subject: [PATCH 5/9] fixed sampling bug --- src/sampling.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sampling.jl b/src/sampling.jl index 5b8d8bd..5483a74 100644 --- a/src/sampling.jl +++ b/src/sampling.jl @@ -169,7 +169,9 @@ function get_weighted_dataframe(bn::BayesNet, nsamples::Integer, evidence::Assig end t[:p] = w / sum(w) - convert(DataFrame, t) + +# convert(DataFrame, t) + t = DataFrame(t) end get_weighted_dataframe(bn::BayesNet, nsamples::Integer, pair::Pair{NodeName}...) = From 5ff9f3d376b20cf2e254479be262d5b09cdcfa50 Mon Sep 17 00:00:00 2001 From: dwijenchawra Date: Mon, 10 May 2021 21:39:41 -0700 Subject: [PATCH 6/9] refactored all instances of convert --- src/DiscreteBayesNet/discrete_bayes_net.jl | 4 ++-- src/DiscreteBayesNet/greedy_hill_climbing.jl | 2 +- src/DiscreteBayesNet/greedy_thick_thinning.jl | 2 +- src/DiscreteBayesNet/scan_greedy_hill_climbing.jl | 2 +- src/DiscreteBayesNet/structure_scoring.jl | 5 +++-- test/test_discrete_bayes_nets.jl | 2 +- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/DiscreteBayesNet/discrete_bayes_net.jl b/src/DiscreteBayesNet/discrete_bayes_net.jl index 67b95d8..0139764 100644 --- a/src/DiscreteBayesNet/discrete_bayes_net.jl +++ b/src/DiscreteBayesNet/discrete_bayes_net.jl @@ -245,7 +245,7 @@ function statistics(dag::DAG, data::DataFrame) parents = [inneighbors(dag, i) for i in 1:n] ncategories = [Int(infer_number_of_instantiations(data[!,i])) for i in 1 : n] # datamat = convert(Matrix{Int}, data)' - datamat = Matrix{Int}(data) + datamat = Matrix{Int}(data)' statistics(parents, ncategories, datamat) end @@ -257,7 +257,7 @@ function statistics(bn::DiscreteBayesNet, target::NodeName, data::DataFrame) parents = inneighbors(bn.dag, targetind) ncategories = [Int(infer_number_of_instantiations(data[!,i])) for i in 1 : n] # datamat = convert(Matrix{Int}, data)' - datamat = Matrix{Int}(data) + datamat = Matrix{Int}(data)' statistics(targetind, parents, ncategories, datamat) end diff --git a/src/DiscreteBayesNet/greedy_hill_climbing.jl b/src/DiscreteBayesNet/greedy_hill_climbing.jl index dd46020..8007ce3 100644 --- a/src/DiscreteBayesNet/greedy_hill_climbing.jl +++ b/src/DiscreteBayesNet/greedy_hill_climbing.jl @@ -65,7 +65,7 @@ function Distributions.fit(::Type{DiscreteBayesNet}, data::DataFrame, params::Gr n = ncol(data) parent_list = map!(i->Int[], Array{Vector{Int}}(undef, n), 1:n) - datamat = convert(Matrix{Int}, data)' + datamat = Matrix{Int}(data)' score_components = bayesian_score_components(parent_list, ncategories, datamat, params.prior, params.cache) while true diff --git a/src/DiscreteBayesNet/greedy_thick_thinning.jl b/src/DiscreteBayesNet/greedy_thick_thinning.jl index bba4ec9..b78ca46 100644 --- a/src/DiscreteBayesNet/greedy_thick_thinning.jl +++ b/src/DiscreteBayesNet/greedy_thick_thinning.jl @@ -20,7 +20,7 @@ function Distributions.fit(::Type{DiscreteBayesNet}, data::DataFrame, params::Gr n = ncol(data) parent_list = map!(i->Int[], Array{Vector{Int}}(n), 1:n) - datamat = convert(Matrix{Int}, data)' + datamat = Matrix{Int}(data)' score_components = bayesian_score_components(parent_list, ncategories, datamat, params.prior, params.cache) while true diff --git a/src/DiscreteBayesNet/scan_greedy_hill_climbing.jl b/src/DiscreteBayesNet/scan_greedy_hill_climbing.jl index 6306bf8..33c7b9a 100755 --- a/src/DiscreteBayesNet/scan_greedy_hill_climbing.jl +++ b/src/DiscreteBayesNet/scan_greedy_hill_climbing.jl @@ -87,7 +87,7 @@ function Distributions.fit(::Type{DiscreteBayesNet}, data::DataFrame, params::Sc n = ncol(data) parent_list = map!(i->Int[], Array{Vector{Int}}(undef, n), 1:n) - datamat = convert(Matrix{Int}, data)' + datamat = Matrix{Int}(data)' score_components = bayesian_score_components(parent_list, ncategories, datamat, params.prior, params.cache) # 0 depth diff --git a/src/DiscreteBayesNet/structure_scoring.jl b/src/DiscreteBayesNet/structure_scoring.jl index 3216ab6..1d88958 100644 --- a/src/DiscreteBayesNet/structure_scoring.jl +++ b/src/DiscreteBayesNet/structure_scoring.jl @@ -111,7 +111,8 @@ function bayesian_score(bn::DiscreteBayesNet, data::DataFrame, prior::DirichletP n = length(bn) parent_list = Array{Vector{Int}}(undef, n) ncategories = Array{Int}(undef, n) - datamat = convert(Matrix{Int}, data)' +# datamat = convert(Matrix{Int}, data)' + datamat = Matrix{Int}(data)' for (i,cpd) in enumerate(bn.cpds) parent_list[i] = inneighbors(bn.dag, i) @@ -168,7 +169,7 @@ function bayesian_score_components(bn::DiscreteBayesNet, data::DataFrame, prior: n = length(bn) parent_list = Array{Vector{Int}}(undef, n) ncategories = Array{Int}(undef, n) - datamat = convert(Matrix{Int}, data)' + datamat = Matrix{Int}(data)' for (i,cpd) in enumerate(bn.cpds) parent_list[i] = inneighbors(bn.dag, i) diff --git a/test/test_discrete_bayes_nets.jl b/test/test_discrete_bayes_nets.jl index d51008b..8ad5ce2 100644 --- a/test/test_discrete_bayes_nets.jl +++ b/test/test_discrete_bayes_nets.jl @@ -30,7 +30,7 @@ let n = length(bn) parent_list = Array{Vector{Int}}(undef, n) bincounts = Array{Int}(undef, n) - datamat = convert(Matrix{Int}, data)' + datamat = Matrix{Int}(data)' for (i,cpd) in enumerate(bn.cpds) parent_list[i] = inneighbors(bn.dag, i) From b70892cd39502b9508a60d2c091e78231a5fcf3b Mon Sep 17 00:00:00 2001 From: dwijenchawra Date: Tue, 11 May 2021 13:58:52 -0700 Subject: [PATCH 7/9] updated Dataframes versions --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 9928020..155acc6 100644 --- a/Project.toml +++ b/Project.toml @@ -26,7 +26,7 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" [compat] -DataFrames = "1.1.0" +DataFrames = "0.22,1.0.0,1.1.0" DataStructures = "0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18" Discretizers = "3.0" Distributions = "0.17,0.18,0.19,0.20,0.21,0.22,0.23,0.24" From c685abbc3de6205c129bda5337f24a25c925e86c Mon Sep 17 00:00:00 2001 From: dwijenchawra Date: Wed, 12 May 2021 10:05:04 -0700 Subject: [PATCH 8/9] removed commented lines --- src/DiscreteBayesNet/discrete_bayes_net.jl | 2 -- src/DiscreteBayesNet/structure_scoring.jl | 1 - src/sampling.jl | 1 - 3 files changed, 4 deletions(-) diff --git a/src/DiscreteBayesNet/discrete_bayes_net.jl b/src/DiscreteBayesNet/discrete_bayes_net.jl index 0139764..b3e32a2 100644 --- a/src/DiscreteBayesNet/discrete_bayes_net.jl +++ b/src/DiscreteBayesNet/discrete_bayes_net.jl @@ -244,7 +244,6 @@ function statistics(dag::DAG, data::DataFrame) parents = [inneighbors(dag, i) for i in 1:n] ncategories = [Int(infer_number_of_instantiations(data[!,i])) for i in 1 : n] -# datamat = convert(Matrix{Int}, data)' datamat = Matrix{Int}(data)' statistics(parents, ncategories, datamat) @@ -256,7 +255,6 @@ function statistics(bn::DiscreteBayesNet, target::NodeName, data::DataFrame) targetind = bn.name_to_index[target] parents = inneighbors(bn.dag, targetind) ncategories = [Int(infer_number_of_instantiations(data[!,i])) for i in 1 : n] -# datamat = convert(Matrix{Int}, data)' datamat = Matrix{Int}(data)' statistics(targetind, parents, ncategories, datamat) end diff --git a/src/DiscreteBayesNet/structure_scoring.jl b/src/DiscreteBayesNet/structure_scoring.jl index 1d88958..62c63fd 100644 --- a/src/DiscreteBayesNet/structure_scoring.jl +++ b/src/DiscreteBayesNet/structure_scoring.jl @@ -111,7 +111,6 @@ function bayesian_score(bn::DiscreteBayesNet, data::DataFrame, prior::DirichletP n = length(bn) parent_list = Array{Vector{Int}}(undef, n) ncategories = Array{Int}(undef, n) -# datamat = convert(Matrix{Int}, data)' datamat = Matrix{Int}(data)' for (i,cpd) in enumerate(bn.cpds) diff --git a/src/sampling.jl b/src/sampling.jl index 5483a74..08331b9 100644 --- a/src/sampling.jl +++ b/src/sampling.jl @@ -170,7 +170,6 @@ function get_weighted_dataframe(bn::BayesNet, nsamples::Integer, evidence::Assig t[:p] = w / sum(w) -# convert(DataFrame, t) t = DataFrame(t) end From dc240d63514e1b192da86d2ef6341a3f5ed32601 Mon Sep 17 00:00:00 2001 From: dwijenchawra Date: Thu, 13 May 2021 08:41:25 -0700 Subject: [PATCH 9/9] changed Dataframes version again --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 155acc6..9021e94 100644 --- a/Project.toml +++ b/Project.toml @@ -26,7 +26,7 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" [compat] -DataFrames = "0.22,1.0.0,1.1.0" +DataFrames = "0.22,1.0,1.1" DataStructures = "0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18" Discretizers = "3.0" Distributions = "0.17,0.18,0.19,0.20,0.21,0.22,0.23,0.24"