Fix createCategory. Add functionProbability.

This commit is contained in:
John J. Camilleri
2021-09-21 23:34:03 +02:00
parent b4b8572af3
commit 4af807c982
4 changed files with 58 additions and 18 deletions

View File

@@ -1,8 +1,9 @@
import pytest
from pgf import *
import math
ty = readType("(N -> N) -> P (s z)")
prob = 3.142
prob = math.pi
@pytest.fixture(scope="module")
def gr1():
@@ -12,7 +13,7 @@ def gr1():
def gr2(gr1):
t = gr1.newTransaction()
t.createFunction("foo", ty, 0, prob),
# t.createCategory("Q", [(BIND_TYPE_EXPLICIT, "x", ty)], prob)
t.createCategory("Q", [(BIND_TYPE_EXPLICIT, "x", ty)], prob)
assert t.commit()
return gr1
@@ -20,7 +21,7 @@ def gr2(gr1):
def gr3(gr1):
with gr1.newTransaction("bar_branch") as t:
t.createFunction("bar", ty, 0, prob),
# t.createCategory("R", [(BIND_TYPE_EXPLICIT, "x", ty)], prob)
t.createCategory("R", [(BIND_TYPE_EXPLICIT, "x", ty)], prob)
return gr1
# gr1
@@ -35,26 +36,29 @@ def test_original_function_type(gr1):
with pytest.raises(KeyError):
gr1.functionType("foo")
# def test_original_function_prob(gr1):
# with pytest.raises(KeyError):
# gr1.functionProbability("foo")
def test_original_function_prob(gr1):
# with pytest.raises(KeyError):
# gr1.functionProbability("foo")
assert gr1.functionProbability("foo") == float('inf')
# gr2
def test_extended_functions(gr2):
assert gr2.functions == ["c", "foo", "ind", "s", "z"]
# def test_extended_categories(gr2):
# assert gr2.categories == ["Float","Int","N","P","Q","S","String"]
#
# def test_extended_category_context(gr2):
# assert gr2.categoryContext("Q") == [(BIND_TYPE_EXPLICIT, "x", ty)]
def test_extended_categories(gr2):
assert gr2.categories == ["Float","Int","N","P","Q","S","String"]
def test_extended_category_context(gr2):
assert gr2.categoryContext("Q") == [(BIND_TYPE_EXPLICIT, "x", ty)]
def test_extended_function_type(gr2):
assert gr2.functionType("foo") == ty
# def test_extended_function_prob(gr2):
# assert gr2.functionProbability("foo") == prob
def test_extended_function_prob(gr2):
# TODO: can't we get higher precision?
# assert gr2.functionProbability("foo") == prob
assert math.isclose(gr2.functionProbability("foo"), prob, rel_tol=1e-06)
# gr3