Skip to content

Commit

Permalink
Fix load_schema bugs and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasColthurst committed Sep 24, 2024
1 parent 34865c8 commit 1d90b5a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
86 changes: 86 additions & 0 deletions cxx/assets/animals.unary.with_comments.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Schema for animal attributes expressed as unary relations.
black ~ bernoulli(animal)
white ~ bernoulli(animal)
blue ~ bernoulli(animal)
brown ~ bernoulli(animal)
gray ~ bernoulli(animal)
orange ~ bernoulli(animal)
red ~ bernoulli(animal)
yellow ~ bernoulli(animal)
patches ~ bernoulli(animal)
spots ~ bernoulli(animal)
stripes ~ bernoulli(animal)
furry ~ bernoulli(animal)
hairless ~ bernoulli(animal)
toughskin ~ bernoulli(animal)
big ~ bernoulli(animal) # TODO(thomaswc): Add definitions of ambiguous properties
small ~ bernoulli(animal)
bulbous ~ bernoulli(animal)
lean ~ bernoulli(animal)
flippers ~ bernoulli(animal)
hands ~ bernoulli(animal)
hooves ~ bernoulli(animal)
pads ~ bernoulli(animal)
paws ~ bernoulli(animal)
longleg ~ bernoulli(animal)
longneck ~ bernoulli(animal)
tail ~ bernoulli(animal)
chewteeth ~ bernoulli(animal)
meatteeth ~ bernoulli(animal)
buckteeth ~ bernoulli(animal)
strainteeth ~ bernoulli(animal)
horns ~ bernoulli(animal)
claws ~ bernoulli(animal)
tusks ~ bernoulli(animal)
smelly ~ bernoulli(animal)
flys ~ bernoulli(animal)
hops ~ bernoulli(animal)
swims ~ bernoulli(animal)
tunnels ~ bernoulli(animal)
walks ~ bernoulli(animal)
fast ~ bernoulli(animal)
slow ~ bernoulli(animal)
strong ~ bernoulli(animal)
weak ~ bernoulli(animal)
muscle ~ bernoulli(animal)
bipedal ~ bernoulli(animal)
quadrapedal ~ bernoulli(animal)
active ~ bernoulli(animal)
inactive ~ bernoulli(animal)
nocturnal ~ bernoulli(animal)
hibernate ~ bernoulli(animal)
agility ~ bernoulli(animal)
fish ~ bernoulli(animal) # Is this the verb or the noun?
meat ~ bernoulli(animal)
plankton ~ bernoulli(animal)
vegetation ~ bernoulli(animal)
insects ~ bernoulli(animal)
forager ~ bernoulli(animal)
grazer ~ bernoulli(animal)
hunter ~ bernoulli(animal)
scavenger ~ bernoulli(animal)
skimmer ~ bernoulli(animal)
stalker ~ bernoulli(animal)
newworld ~ bernoulli(animal)
oldworld ~ bernoulli(animal)
arctic ~ bernoulli(animal)
coastal ~ bernoulli(animal)
desert ~ bernoulli(animal)
bush ~ bernoulli(animal)
plains ~ bernoulli(animal)
forest ~ bernoulli(animal)
fields ~ bernoulli(animal)
jungle ~ bernoulli(animal)
mountains ~ bernoulli(animal)
ocean ~ bernoulli(animal)
ground ~ bernoulli(animal)
water ~ bernoulli(animal)
tree ~ bernoulli(animal)
cave ~ bernoulli(animal)
fierce ~ bernoulli(animal)
timid ~ bernoulli(animal)
smart ~ bernoulli(animal)
group ~ bernoulli(animal)
solitary ~ bernoulli(animal)
nestspot ~ bernoulli(animal)
domestic ~ bernoulli(animal)
4 changes: 2 additions & 2 deletions py/hirm_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def load_schema(path):
relations = {}
comment_line_re = re.compile(r'\s*#.*')
# TODO(thomaswc): Handle distribution parameters in brackets
line_re = re.compile(r'\s*(\w+)\s*~\s*(\w+)\s*\(\s*(.+)\s*\)\s*(#.*)?')
line_re = re.compile(r'\s*(\w+)\s*~\s*(\w+)\s*\(\s*([^\)]+)\s*\)\s*(#.*)?')
with open(path, 'r') as f:
for line in f:
if comment_line_re.match(line):
next
continue
m = line_re.match(line)
if not m:
print(f"Could not parse schema line\n{line}\n")
Expand Down
10 changes: 10 additions & 0 deletions py/hirm_io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ def test_load_schema(self):
self.assertEqual(relations["black"].distribution, "bernoulli")
self.assertEqual(relations["black"].domains, ["animal"])

def test_load_schema_with_comments(self):
relations = hirm_io.load_schema("../cxx/assets/animals.unary.with_comments.schema")
self.assertEqual(len(relations), 85)
self.assertEqual(relations["black"].name, "black")
self.assertEqual(relations["black"].distribution, "bernoulli")
self.assertEqual(relations["black"].domains, ["animal"])
self.assertEqual(relations["big"].name, "big")
self.assertEqual(relations["big"].distribution, "bernoulli")
self.assertEqual(relations["big"].domains, ["animal"])

def test_load_observations(self):
observations = hirm_io.load_observations("../cxx/assets/animals.unary.obs")
self.assertEqual(len(observations), 4250)
Expand Down

0 comments on commit 1d90b5a

Please sign in to comment.