def test_persistence(self): with closing(PyannoDatabase(self.tmp_filename)) as db: db.store_result(self.data_id1, self.anno_container1, self.model1, self.value1) with closing(PyannoDatabase(self.tmp_filename)) as db: results = db.retrieve_id(self.data_id1) np.testing.assert_equal(results[0].anno_container.annotations, self.anno_container1.annotations)
def _create_debug_database(self): """Create and populate a test database in a temporary file. """ from tempfile import mktemp from pyanno.modelA import ModelA from pyanno.modelB import ModelB from pyanno.annotations import AnnotationsContainer # database filename tmp_filename = mktemp(prefix='tmp_pyanno_db_') db = PyannoDatabase(tmp_filename) def _create_new_entry(model, annotations, id): value = model.log_likelihood(annotations) ac = AnnotationsContainer.from_array(annotations, name=id) db.store_result(id, ac, model, value) # populate database model = ModelA.create_initial_state(5) annotations = model.generate_annotations(100) _create_new_entry(model, annotations, 'test_id') modelb = ModelB.create_initial_state(5, 8) _create_new_entry(modelb, annotations, 'test_id') annotations = model.generate_annotations(100) _create_new_entry(modelb, annotations, 'test_id2') self.database = db
def main(): """ Entry point for standalone testing/debugging. """ from tempfile import mktemp from contextlib import closing # database filename tmp_filename = mktemp(prefix='tmp_pyanno_db_') with closing(PyannoDatabase(tmp_filename)) as db: # populate database model = ModelA.create_initial_state(5) annotations = model.generate_annotations(100) _create_new_entry(model, annotations, 'test_id', db) modelb = ModelB.create_initial_state(5, 8) _create_new_entry(modelb, annotations, 'test_id', db) annotations = model.generate_annotations(100) _create_new_entry(modelb, annotations, 'test_id2', db) # create view database_view = DatabaseView(database=db) database_view.edit_traits(view='traits_view') return model, database_view
def test_storage(self): with closing(PyannoDatabase(self.tmp_filename)) as db: db.store_result(self.data_id1, self.anno_container1, self.model1, self.value1) self.assertEqual(len(db.database), 1) results = db.retrieve_id(self.data_id1) self.assert_(isinstance(results, list)) self.assertEqual(len(results), 1) self.assert_(isinstance(results[0], PyannoResult)) np.testing.assert_equal(results[0].anno_container.annotations, self.anno_container1.annotations) self.assertEqual(results[0].model.nclasses, self.model1.nclasses) self.assertEqual(results[0].value, self.value1) self.assert_(isinstance(results[0].model, self.model1.__class__)) # store new entry for different annotations db.store_result(self.data_id2, self.anno_container2, self.model2, self.value2) self.assertEqual(len(db.database), 2) # add new result for same annotations db.store_result(self.data_id1, self.anno_container1, self.model2, self.value2) results = db.retrieve_id(self.data_id1) self.assertEqual(len(results), 2) self.assertEqual(results[1].value, self.value2)
def test_conflict(self): # behavior: one cannot store two different annotations with same id with closing(PyannoDatabase(self.tmp_filename)) as db: db.store_result(self.data_id1, self.anno_container1, self.model1, self.value1) with self.assertRaises(PyannoValueError): db.store_result(self.data_id1, self.anno_container2, self.model1, self.value1)
def test_database_create_and_close(self): with closing(PyannoDatabase(self.tmp_filename)) as db: self.assertEqual(len(db.database), 0) self.assertEqual(db.db_filename, self.tmp_filename) self.assertFalse(db.closed) self.assertTrue(db.closed) # make sure database is closed with self.assertRaisesRegexp(ValueError, 'closed shelf'): db.database['a'] = 2
def test_tmp_dataname(self): # behavior: the method get_available_id should return a new ID of the # form <name_N>, that is not yet present in the database with closing(PyannoDatabase(self.tmp_filename)) as db: id1 = db.get_available_id() self.assertEqual(id1, '<name_0>') self.assert_(not db.database.has_key(id1)) db.store_result(id1, self.anno_container1, self.model1, self.value1) id2 = db.get_available_id() self.assertNotEqual(id1, id2) self.assert_(not db.database.has_key(id2))
def test_remove(self): # behavior: removing one item makes the item disappear, # the rest of the database is intact # deletion should be indexed by data id and index in the list of # entries with the same if with closing(PyannoDatabase(self.tmp_filename)) as db: db.store_result(self.data_id1, self.anno_container1, self.model1, self.value1) db.store_result(self.data_id1, self.anno_container1, self.model2, self.value2) db.store_result(self.data_id2, self.anno_container2, self.model2, self.value2) db.remove(self.data_id1, 1) self.assertEqual(len(db.database), 2) self.assertEqual(len(db.database[self.data_id1]), 1) self.assertEqual(len(db.database[self.data_id2]), 1) self.assertTrue( isinstance(db.database[self.data_id1][0].model, self.model1.__class__))
def _open_pyanno_database(self): # database filename self._create_pyanno_directory() db_filename = os.path.join(self.pyanno_pathname, DATABASE_FILENAME) self.database = PyannoDatabase(db_filename)