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)
class PyannoApplication(HasTraits): database = Instance(PyannoDatabase) main_window = Instance(ModelDataView) database_window = Instance(DatabaseView) database_ui = Instance(UI) logging_level = Int(logging.INFO) pyanno_pathname = Str db_window_open = Property(Bool) def _get_db_window_open(self): return (self.database_ui is not None and self.database_ui.control is not None) def open(self): self._start_logging() self._open_pyanno_database() self._open_main_window() def close(self): self.database.close() logger.info('Closing pyAnno -- Goodbye!') def _start_logging(self): logging.basicConfig(level=self.logging_level) logger.info('Starting pyAnno') def _create_pyanno_directory(self): """Create a pyanno directort in the user's home if it is missing.""" home_dir = os.getenv('HOME') or os.getenv('HOMEPATH') logger.debug('Found home directory at ' + str(home_dir)) self.pyanno_pathname = os.path.join(home_dir, PYANNO_PATH_NAME) try: logger.debug('Creating pyAnno directory at ' + self.pyanno_pathname) os.makedirs(self.pyanno_pathname) except OSError as e: logger.debug('pyAnno directory already existing') if e.errno != errno.EEXIST: raise 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) def _open_main_window(self): self.main_window = ModelDataView(application=self) model = ModelBt.create_initial_state(5, 8) self.main_window.set_model(model=model) self.main_window.configure_traits() def open_database_window(self): if self.db_window_open: # windows exists, raise self.database_ui.control.Raise() else: # window was closed or not existent logger.debug('Open database window') database_window = DatabaseView(database=self.database, application=self) database_ui = database_window.edit_traits(kind='live') self.database_window = database_window self.database_ui = database_ui def close_database_window(self): # wx specific self.database_ui.control.Close() def update_window_from_database_record(self, record): """Update main window from pyanno database record. """ self.main_window.set_from_database_record(record) def add_current_state_to_database(self): mdv = self.main_window # file name may contain unicode characters data_id = mdv.annotations_view.annotations_container.name if data_id is '': data_id = 'anonymous_annotations' elif type(data_id) is unicode: u_data_id = unicodedata.normalize('NFKD', data_id) data_id = u_data_id.encode('ascii','ignore') try: self.database.store_result( data_id, mdv.annotations_view.annotations_container, mdv.model, mdv.log_likelihood ) except PyannoValueError as e: logger.info(e) errmsg = e.args[0] error('Error: ' + errmsg) if self.db_window_open: self.database_window.db_updated = True 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
class PyannoApplication(HasTraits): database = Instance(PyannoDatabase) main_window = Instance(ModelDataView) database_window = Instance(DatabaseView) database_ui = Instance(UI) logging_level = Int(logging.INFO) pyanno_pathname = Str db_window_open = Property(Bool) def _get_db_window_open(self): return (self.database_ui is not None and self.database_ui.control is not None) def open(self): self._start_logging() self._open_pyanno_database() self._open_main_window() def close(self): self.database.close() logger.info('Closing pyAnno -- Goodbye!') def _start_logging(self): logging.basicConfig(level=self.logging_level) logger.info('Starting pyAnno') def _create_pyanno_directory(self): """Create a pyanno directort in the user's home if it is missing.""" home_dir = os.getenv('HOME') or os.getenv('HOMEPATH') logger.debug('Found home directory at ' + str(home_dir)) self.pyanno_pathname = os.path.join(home_dir, PYANNO_PATH_NAME) try: logger.debug('Creating pyAnno directory at ' + self.pyanno_pathname) os.makedirs(self.pyanno_pathname) except OSError as e: logger.debug('pyAnno directory already existing') if e.errno != errno.EEXIST: raise 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) def _open_main_window(self): self.main_window = ModelDataView(application=self) model = ModelBt.create_initial_state(5, 8) self.main_window.set_model(model=model) self.main_window.configure_traits() def open_database_window(self): if self.db_window_open: # windows exists, raise self.database_ui.control.Raise() else: # window was closed or not existent logger.debug('Open database window') database_window = DatabaseView(database=self.database, application=self) database_ui = database_window.edit_traits(kind='live') self.database_window = database_window self.database_ui = database_ui def close_database_window(self): # wx specific self.database_ui.control.Close() def update_window_from_database_record(self, record): """Update main window from pyanno database record. """ self.main_window.set_from_database_record(record) def add_current_state_to_database(self): mdv = self.main_window # file name may contain unicode characters data_id = mdv.annotations_view.annotations_container.name if data_id is '': data_id = 'anonymous_annotations' elif type(data_id) is unicode: u_data_id = unicodedata.normalize('NFKD', data_id) data_id = u_data_id.encode('ascii', 'ignore') try: self.database.store_result( data_id, mdv.annotations_view.annotations_container, mdv.model, mdv.log_likelihood) except PyannoValueError as e: logger.info(e) errmsg = e.args[0] error('Error: ' + errmsg) if self.db_window_open: self.database_window.db_updated = True 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