diff --git a/navipy/database/__init__.py b/navipy/database/__init__.py index faf8eff5fcfc3d4f503bd67d90b2e01b9340d24f..0a3f5fb1ae3ab758fcecad263665c16754679171 100644 --- a/navipy/database/__init__.py +++ b/navipy/database/__init__.py @@ -316,7 +316,7 @@ class DataBase(): [convention]['q_3'] **where convention can be: quaternion - :type rowid: pd.Series + :type posorient: pd.Series :returns: id :rtype: int """ @@ -605,8 +605,9 @@ class DataBase(): WHERE (rowid={}) """.format(tablename, rowid), self.db) toreturn = toreturn.loc[0, :] - toreturn.name = toreturn.id - toreturn.drop('id') + # if np.isnan(toreturn.frame) + toreturn.name = toreturn.frame_i + # toreturn.drop('id') # toreturn = toreturn.astype(float) posorient = None convention = toreturn.rotconv_id @@ -624,6 +625,7 @@ class DataBase(): posorient[convention]['alpha_0'] = toreturn.loc['q_0'] posorient[convention]['alpha_1'] = toreturn.loc['q_1'] posorient[convention]['alpha_2'] = toreturn.loc['q_2'] + posorient.name = toreturn.name else: tuples = [('location', 'x'), ('location', 'y'), ('location', 'z'), (convention, 'q_0'), @@ -640,6 +642,7 @@ class DataBase(): posorient[convention]['q_1'] = toreturn.loc['q_1'] posorient[convention]['q_2'] = toreturn.loc['q_2'] posorient[convention]['q_3'] = toreturn.loc['q_3'] + posorient.name = toreturn.name return posorient diff --git a/navipy/database/test.py b/navipy/database/test.py index 1dd96048e3a7a835cab7a38f4880299299ad827f..17fbc1cc4a2e1717c7b8a16213f854462d239d9e 100644 --- a/navipy/database/test.py +++ b/navipy/database/test.py @@ -87,112 +87,6 @@ class TestCase(unittest.TestCase): self.mydb.check_data_validity(n) assert self.mydb.check_data_validity(1) - def get_posid_test(self): - """ - this test checks the function get_posid works - correctly. - it checks if correct errors are raised for: - - posorient is missing an entry (no 'x' column) - - posorient contains nan or none values - - posorient is of wrong type (dict instead of pd.series) - """ - conn = sqlite3.connect(self.mydb_filename) - c = conn.cursor() - c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) - rows = c.fetchall()[0] - # convention = rows[1] - - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - - posorient = pd.Series(index=index) - posorient['location']['x'] = rows[6] - posorient['location']['y'] = rows[7] - posorient['location']['z'] = rows[8] - posorient['xyz']['alpha_0'] = rows[3] - posorient['xyz']['alpha_1'] = rows[5] - posorient['xyz']['alpha_2'] = rows[4] - - posid = self.mydb.get_posid(posorient) - assert posid == 1 - - # incorrect case missing column - tuples = [('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - - posorient2 = pd.Series(index=index) - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] - - with self.assertRaises(Exception): - posid = self.mydb.get_posid(posorient2) - - # incorrect case None - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(self.tuples, - names=['position', 'orientation']) - - posorient3 = pd.Series(index=index) - posorient3['location']['x'] = None - posorient3['location']['y'] = rows[7] - posorient3['location']['z'] = rows[8] - posorient3['xyz']['alpha_0'] = rows[3] - posorient3['xyz']['alpha_1'] = rows[5] - posorient3['xyz']['alpha_2'] = rows[4] - with self.assertRaises(ValueError): - posid = self.mydb.get_posid(posorient2) - - # incorrect case nan - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(self.tuples, - names=['position', 'orientation']) - - posorient2 = pd.Series(index=index) - posorient2['location']['x'] = np.nan - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] - with self.assertRaises(ValueError): - posid = self.mydb.get_posid(posorient2) - - # incorrect case no pandas series but dict - posorient2 = {} - posorient2['location']['x'] = rows[6] - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] - with self.assertRaises(TypeError): - self.mydb.get_posid(posorient2) - - # not working case empty - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(self.tuples, - names=['position', 'orientation']) - - posorient2 = pd.Series(index=index) - - with self.assertRaises(Exception): - self.mydb.get_posid(posorient2) - def test_read_posorient(self): """ this test checks the function read_posorient works @@ -202,88 +96,32 @@ class TestCase(unittest.TestCase): - posorient contains nan or none values - posorient is of wrong type (dict instead of pd.series) """ - conn = sqlite3.connect(self.mydb_filename) - c = conn.cursor() - c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) - rows = c.fetchall()[0] - # working case - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - - posorient = pd.Series(index=index) - posorient['location']['x'] = rows[6] - posorient['location']['y'] = rows[7] - posorient['location']['z'] = rows[8] - posorient['xyz']['alpha_0'] = rows[3] - posorient['xyz']['alpha_1'] = rows[5] - posorient['xyz']['alpha_2'] = rows[4] + posorient = self.mydb.read_posorient(rowid=1) posid = self.mydb.read_posorient(posorient=posorient) # print(posid) assert posid['location']['x'] == posorient['location']['x'] # incorrect case missing column - tuples = [('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - - posorient2 = pd.Series(index=index) - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] + posorient2 = posorient.copy() + posorient2 = posorient2.drop('x', level=1) + posorient2['location']['x'] = np.nan with self.assertRaises(ValueError): self.mydb.read_posorient(posorient=posorient2) # incorrect case None - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - - posorient2 = pd.Series(index=index) + posorient2 = posorient.copy() posorient2['location']['x'] = None - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): self.mydb.read_posorient(posorient=posorient2) # incorrect case nan - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - - posorient2 = pd.Series(index=index) + posorient2 = posorient.copy() posorient2['location']['x'] = np.nan - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): self.mydb.read_posorient(posorient=posorient2) # incorrect case no pandas series but dict - posorient2 = {} - posorient2['location'] = {} - posorient2['xyz'] = {} - posorient2['location']['x'] = rows[6] - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] + posorient2 = posorient.to_dict() with self.assertRaises(TypeError): self.mydb.read_posorient(posorient=posorient2) @@ -295,7 +133,6 @@ class TestCase(unittest.TestCase): names=['position', 'orientation']) posorient2 = pd.Series(index=index) - with self.assertRaises(Exception): self.mydb.read_posorient(posorient=posorient2) @@ -399,23 +236,8 @@ class TestCase(unittest.TestCase): - posorient contains nan or none values - posorient is of wrong type (dict instead of pd.series) """ - conn = sqlite3.connect(self.mydb_filename) - c = conn.cursor() - c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) - rows = c.fetchall()[0] - # working case - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - posorient = pd.Series(index=index) - posorient['location']['x'] = rows[6] - posorient['location']['y'] = rows[7] - posorient['location']['z'] = rows[8] - posorient['xyz']['alpha_0'] = rows[3] - posorient['xyz']['alpha_1'] = rows[5] - posorient['xyz']['alpha_2'] = rows[4] + posorient = self.mydb.posorients.iloc[0, :] + posorient.name = 1 image = self.mydb.scene(posorient=posorient) self.assertIsNotNone(image) self.assertFalse(sum(image.shape) == 0) @@ -424,73 +246,30 @@ class TestCase(unittest.TestCase): self.assertTrue(image.shape[3] == 1) # incorrect case missing column - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - - posorient2 = pd.Series(index=index) - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] + posorient2 = posorient.copy() + posorient2.drop(('location', 'x')) with self.assertRaises(Exception): image = self.mydb.scene(posorient=posorient2) # incorrect case None - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - - posorient2 = pd.Series(index=index) + posorient2 = posorient.copy() posorient2['location']['x'] = None - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): image = self.mydb.scene(posorient=posorient2) # incorrect case nan - tuples = [('location', 'x'), ('location', 'y'), - ('location', 'z'), ('xyz', 'alpha_0'), - ('xyz', 'alpha_1'), ('xyz', 'alpha_2')] - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - - posorient2 = pd.Series(index=index) + posorient2 = posorient.copy() posorient2['location']['x'] = np.nan - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): image = self.mydb.scene(posorient=posorient2) # incorrect case no pandas series but dict - posorient2 = {} - posorient2['location'] = {} - posorient2['xyz'] = {} - posorient2['location']['x'] = rows[6] - posorient2['location']['y'] = rows[7] - posorient2['location']['z'] = rows[8] - posorient2['xyz']['alpha_0'] = rows[3] - posorient2['xyz']['alpha_1'] = rows[5] - posorient2['xyz']['alpha_2'] = rows[4] + posorient2 = posorient.to_dict() with self.assertRaises(TypeError): image = self.mydb.scene(posorient=posorient2) # not working case empty - index = pd.MultiIndex.from_tuples(tuples, - names=['position', 'orientation']) - - posorient2 = pd.Series(index=index) + posorient2 = pd.Series(index=posorient.index) with self.assertRaises(Exception): image = self.mydb.scene(posorient=posorient2)