Skip to content

Fix minor string key handling bug #38

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

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/gensql/inference/gpm/crosscat.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
;; If the targets and constraints are not equal but the overlapping parts are,
;; just remove the overlapping keys and recur the scores.
(every? (fn [shared-key]
(= (shared-key targets)
(shared-key constraints)))
(= (get targets shared-key)
(get constraints shared-key)))
intersection)
(reduce-kv (fn [logp _ view]
;; Filtering view variables happens naturally.
Expand Down
88 changes: 60 additions & 28 deletions test/gensql/inference/gpm/crosscat_test.cljc
Original file line number Diff line number Diff line change
@@ -1,36 +1,68 @@
(ns gensql.inference.gpm.crosscat-test
(:require [gensql.inference.test-models.crosscat :refer [model]]
[clojure.test :refer [deftest is]]
(:require [gensql.inference.test-models.crosscat :refer [model str-model]]
[clojure.test :refer [deftest is testing]]
[gensql.inference.gpm :as gpm]))

(deftest simulate
(let [sim-no-constraint (gpm/simulate model [:color :height :flip] {})
sim-constraint-not-target (gpm/simulate model [:color] {:height 0.4})
sim-constraints-are-target (gpm/simulate model [:color :height] {:color "blue" :height 0.4})]
;; Simply checking that we generated all columns without error.
(is (= #{:color :height :flip} (set (keys sim-no-constraint))))
(is (= #{:color} (set (keys sim-constraint-not-target))))
(is (= "blue" (:color sim-constraints-are-target)))
(is (= 0.4 (:height sim-constraints-are-target)))))
(testing "keyword"
(let [sim-no-constraint (gpm/simulate model [:color :height :flip] {})
sim-constraint-not-target (gpm/simulate model [:color] {:height 0.4})
sim-constraints-are-target (gpm/simulate model [:color :height] {:color "blue" :height 0.4})]
;; Simply checking that we generated all columns without error.
(is (= #{:color :height :flip} (set (keys sim-no-constraint))))
(is (= #{:color} (set (keys sim-constraint-not-target))))
(is (= "blue" (:color sim-constraints-are-target)))
(is (= 0.4 (:height sim-constraints-are-target)))))

(testing "string"
(let [sim-no-constraint (gpm/simulate str-model ["color" "height" "flip"] {})
sim-constraint-not-target (gpm/simulate str-model ["color"] {"height" 0.4})
sim-constraints-are-target (gpm/simulate str-model ["color" "height"] {"color" "blue" "height" 0.4})]
;; Simply checking that we generated all columns without error.
(is (= #{"color" "height" "flip"} (set (keys sim-no-constraint))))
(is (= #{"color"} (set (keys sim-constraint-not-target))))
(is (= "blue" (get sim-constraints-are-target "color")))
(is (= 0.4 (get sim-constraints-are-target "height"))))))

(deftest logpdf
(let [no-constraints (gpm/logpdf model {:color "red" :height 4.0 :flip true} {})
constraints-match-target (gpm/logpdf model {:color "red" :height 4.0} {:color "red" :height 4.0})
mistmatch (gpm/logpdf model
{:color "red" :height 4.0 :flip true} {:color "blue" :height 4.0 :flip true})
match-subset (gpm/logpdf model
{:color "red" :height 4.0} {:color "red" :flip true})
nonmatch-subset (gpm/logpdf model
{:height 4.0} {:color "red" :flip true})
no-target (gpm/logpdf model
{} {:color "red" :height 4.0 :flip true})
fully-constrained-target (gpm/logpdf model
{:color "red" :height 4.0} {:color "red" :height 4.0 :flip true})]
(is (number? no-constraints))
(is (= 0.0 constraints-match-target))
(is (= ##-Inf mistmatch))
(is (= match-subset nonmatch-subset))
(is (= no-target fully-constrained-target 0.0))))
(testing "keyword"
(let [no-constraints (gpm/logpdf model {:color "red" :height 4.0 :flip true} {})
constraints-match-target (gpm/logpdf model {:color "red" :height 4.0} {:color "red" :height 4.0})
mistmatch (gpm/logpdf model
{:color "red" :height 4.0 :flip true} {:color "blue" :height 4.0 :flip true})
match-subset (gpm/logpdf model
{:color "red" :height 4.0} {:color "red" :flip true})
nonmatch-subset (gpm/logpdf model
{:height 4.0} {:color "red" :flip true})
no-target (gpm/logpdf model
{} {:color "red" :height 4.0 :flip true})
fully-constrained-target (gpm/logpdf model
{:color "red" :height 4.0} {:color "red" :height 4.0 :flip true})]
(is (number? no-constraints))
(is (= 0.0 constraints-match-target))
(is (= ##-Inf mistmatch))
(is (= match-subset nonmatch-subset))
(is (= no-target fully-constrained-target 0.0))))

(testing "string"
(let [no-constraints (gpm/logpdf str-model {"color" "red" "height" 4.0 "flip" true} {})
constraints-match-target (gpm/logpdf str-model {"color" "red" "height" 4.0} {"color" "red" "height" 4.0})
mistmatch (gpm/logpdf str-model
{"color" "red" "height" 4.0 "flip" true} {"color" "blue" "height" 4.0 "flip" true})
match-subset (gpm/logpdf str-model
{"color" "red" "height" 4.0} {"color" "red" "flip" true})
nonmatch-subset (gpm/logpdf str-model
{"height" 4.0} {"color" "red" "flip" true})
no-target (gpm/logpdf str-model
{} {"color" "red" "height" 4.0 "flip" true})
fully-constrained-target (gpm/logpdf str-model
{"color" "red" "height" 4.0} {"color" "red" "height" 4.0 "flip" true})]
(is (number? no-constraints))
(is (= 0.0 constraints-match-target))
(is (= ##-Inf mistmatch))
(is (= match-subset nonmatch-subset))
(is (= no-target fully-constrained-target 0.0)))))

(deftest variables
(is (= #{:color :height :flip} (gpm/variables model))))
(is (= #{:color :height :flip} (gpm/variables model)))
(is (= #{"color" "height" "flip"} (gpm/variables str-model))))
40 changes: 40 additions & 0 deletions test/gensql/inference/test_models/crosscat.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,43 @@
4 :two
5 :two}}}}]
(xcat/construct-xcat-from-latents xcat-spec xcat-latents data {:options options})))


(def str-data
{0 {"color" "red" "height" 6 "flip" true}
1 {"color" "red" "height" 6 "flip" true}
2 {"color" "red" "height" 6 "flip" true}
3 {"color" "red" "height" 4 "flip" false}
4 {"color" "blue" "height" 4 "flip" false}
5 {"color" "green" "height" 4 "flip" false}})

(def str-model
(let [options {"color" ["red" "blue" "green"]}
view-1-name (str (gensym))
view-2-name (str (gensym))

xcat-spec {:views {view-1-name {:hypers {"color" {:alpha 2}
"height" {:m 0 :r 1 :s 2 :nu 3}}}
view-2-name {:hypers {"flip" {:alpha 1 :beta 1}}}}
:types {"color" :categorical
"height" :gaussian
"flip" :bernoulli}}

xcat-latents {:global {:alpha 0.5}
:local {view-1-name {:alpha 1
:counts {"one" 4 "two" 2}
:y {0 "one"
1 "one"
2 "one"
3 "one"
4 "two"
5 "two"}}
view-2-name {:alpha 1
:counts {"one" 3 "two" 3}
:y {0 "one"
1 "one"
2 "one"
3 "two"
4 "two"
5 "two"}}}}]
(xcat/construct-xcat-from-latents xcat-spec xcat-latents str-data {:options options})))
Loading