Skip to content

Commit

Permalink
Allow TestSuite.write() to specify relations
Browse files Browse the repository at this point in the history
Addresses #150
  • Loading branch information
goodmami committed Oct 3, 2018
1 parent dfdac5d commit c0693eb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 47 deletions.
42 changes: 25 additions & 17 deletions delphin/itsdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,8 @@ def select(self, arg, cols=None, mode='list'):
cols = [f.name for f in self.relations[table]]
return select_rows(cols, self[table], mode=mode)

def write(self, tables=None, path=None, append=False, gzip=None):
def write(self, tables=None, path=None, relations=None,
append=False, gzip=None):
"""
Write the testsuite to disk.
Expand All @@ -666,6 +667,8 @@ def write(self, tables=None, path=None, append=False, gzip=None):
all tables will be written
path: the destination directory; if `None` use the path
assigned to the TestSuite
relations: a :class:`Relations` object or path to a
relations file to be used when writing the tables
append: if `True`, append to rather than overwrite tables
gzip: compress non-empty tables with gzip
Examples:
Expand All @@ -684,27 +687,32 @@ def write(self, tables=None, path=None, append=False, gzip=None):
pass
elif isinstance(tables, Sequence):
tables = dict((table, self[table]) for table in tables)
if relations is None:
relations = self.relations
elif isinstance(relations, stringtypes):
relations = Relations.from_file(relations)

# prepare destination
if not os.path.exists(path):
os.makedirs(path)
# raise error if path != self._path?
if not os.path.isfile(os.path.join(path, _relations_filename)):
with open(os.path.join(path, _relations_filename), 'w') as fh:
print(str(self.relations), file=fh)

for tablename, data in tables.items():
# reload table from disk if it is invalidated
if data is None:
data = self[tablename]
_write_table(
path,
tablename,
data,
self.relations[tablename],
gzip=gzip,
encoding=self.encoding
)
with open(os.path.join(path, _relations_filename), 'w') as fh:
print(str(relations), file=fh)

for tablename, relation in relations.items():
if tablename in tables:
data = tables[tablename]
# reload table from disk if it is invalidated
if data is None:
data = self[tablename]
_write_table(
path,
tablename,
data,
relation,
gzip=gzip,
encoding=self.encoding
)

def process(self, cpu, selector=None, source=None, fieldmapper=None):
"""
Expand Down
82 changes: 52 additions & 30 deletions tests/itsdb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,41 @@
from delphin.interfaces.base import Processor, ParseResponse
from delphin import itsdb

_simple_relations = '''
item:
_simple_relations = '''item:
i-id :integer :key
i-input :string
run:
run-id :integer :key # unique test run identifier
run-id :integer :key # unique test run identifier
parse:
parse-id :integer :key # unique parse identifier
run-id :integer :key # test run for this parse
i-id :integer :key # item parsed
parse-id :integer :key # unique parse identifier
run-id :integer :key # test run for this parse
i-id :integer :key # item parsed
result:
parse-id :integer :key # parse for this result
result-id :integer # result identifier
mrs :string # MRS for this reading
parse-id :integer :key # parse for this result
result-id :integer # result identifier
mrs :string # MRS for this reading
'''

_alt_relations = '''item:
i-id :integer :key
i-input :string
i-date :date
parse:
parse-id :integer :key # unique parse identifier
run-id :integer :key # test run for this parse
i-id :integer :key # item parsed
result:
parse-id :integer :key # parse for this result
result-id :integer # result identifier
mrs :string # MRS for this reading
'''


@pytest.fixture
def parser_cpu():
class DummyParser(Processor):
Expand Down Expand Up @@ -95,34 +111,32 @@ def process_item(self, datum, keys=None):
return DummyParser()

@pytest.fixture
def empty_profile():
d = tempfile.mkdtemp()
print(_simple_relations, file=open(os.path.join(d, 'relations'), 'w'))
return d
def empty_profile(tmpdir):
ts = tmpdir.mkdir('empty')
ts.join('relations').write(_simple_relations)
return str(ts)

@pytest.fixture
def single_item_skeleton():
d = tempfile.mkdtemp()
print(_simple_relations, file=open(os.path.join(d, 'relations'), 'w'))
print('0@The dog barks.', file=open(os.path.join(d, 'item'), 'w'))
return d
def single_item_skeleton(tmpdir):
ts = tmpdir.mkdir('skeleton')
ts.join('relations').write(_simple_relations)
ts.join('item').write('0@The dog barks.')
return str(ts)

@pytest.fixture
def single_item_profile():
d = tempfile.mkdtemp()
print(_simple_relations, file=open(os.path.join(d, 'relations'), 'w'))
print('0@The dog barks.', file=open(os.path.join(d, 'item'), 'w'))
print('0', file=open(os.path.join(d, 'run'), 'w'))
print('0@0@0', file=open(os.path.join(d, 'parse'), 'w'))
print(
def single_item_profile(tmpdir):
ts = tmpdir.mkdir('single')
ts.join('relations').write(_simple_relations)
ts.join('item').write('0@The dog barks.')
ts.join('run').write('0')
ts.join('parse').write('0@0@0')
ts.join('result').write(
'0@0@[ LTOP: h0 INDEX: e2 RELS: < '
'[ _the_q<0:3> LBL: h4 ARG0: x3 RSTR: h5 BODY: h6 ] '
'[ _dog_n_1<4:7> LBL: h7 ARG0: x3 ] '
'[ _bark_v_1<8:14> LBL: h1 ARG0: e2 ARG1: x3 ] > '
'HCONS: < h0 qeq h1 h5 qeq h7 > ]',
file=open(os.path.join(d, 'result'), 'w')
)
return d
'HCONS: < h0 qeq h1 h5 qeq h7 > ]')
return str(ts)

def test_Field():
f = itsdb.Field('x', ':y', True, False, 'a comment')
Expand Down Expand Up @@ -262,7 +276,7 @@ def test_reload(self, single_item_profile):
t.reload()
assert t['item'][0]['i-input'] == 'The dog barks.'

def test_write(self, single_item_profile):
def test_write(self, single_item_profile, tmpdir):
t = itsdb.TestSuite(single_item_profile)
assert t['item'][0]['i-input'] == 'The dog barks.'
t['item'][0]['i-input'] = 'The dog sleeps.'
Expand All @@ -283,6 +297,14 @@ def test_write(self, single_item_profile):
t.write({'item': [record]})
t.reload()
assert t['item'][0]['i-input'] == 'The cat meows.'
d = tmpdir.mkdir('alt')
altrels = itsdb.Relations.from_string(_alt_relations)
t.write(path=str(d), relations=altrels)
assert d.join('relations').read() == _alt_relations
assert sorted(x.basename for x in d.listdir()) == [
'item', 'parse', 'relations', 'result']
ts = itsdb.TestSuite(str(d))
assert 'i-date' in ts['item'].fields

def test_process(self, parser_cpu, single_item_skeleton):
ts = itsdb.TestSuite(single_item_skeleton)
Expand Down

0 comments on commit c0693eb

Please sign in to comment.