diff --git a/navipy/database/__init__.py b/navipy/database/__init__.py index 0a08881fb959bb0d8661b4624ca5fcb30e32939c..4d8ff1adcde7b386996aa73fbe7aee4217755ff7 100644 --- a/navipy/database/__init__.py +++ b/navipy/database/__init__.py @@ -39,12 +39,23 @@ sqlite3.register_converter("array", convert_array) class DataBase(): """DataBase is the parent class of DataBaseLoad and DataBaseSave. -It creates three sql table on initialisation. + It creates three sql table on initialisation. """ __float_tolerance = 1e-14 def __init__(self, filename, channels=['R', 'G', 'B', 'D']): - """Initialisation of the database """ + """Initialisation of the database + the first database is the image database to store the images + the second database is the position_orientation database to + store the position and orientations of the corresponding image + the third database is the normalisation database that stores the + ranges of the images. + :param filename: filename of the database to be loaded, stored + created + channels: channels for the images (Red,Green,Blue,Distance) + :type filename: string + channels: list + """ if not isinstance(filename, str): raise TypeError('filename should be a string') if not isinstance(channels, list): @@ -73,9 +84,11 @@ It creates three sql table on initialisation. self.tablecolumns['position_orientation']['x'] = 'real' self.tablecolumns['position_orientation']['y'] = 'real' self.tablecolumns['position_orientation']['z'] = 'real' - self.tablecolumns['position_orientation']['alpha_0'] = 'real' - self.tablecolumns['position_orientation']['alpha_1'] = 'real' - self.tablecolumns['position_orientation']['alpha_2'] = 'real' + self.tablecolumns['position_orientation']['q_0'] = 'real' + self.tablecolumns['position_orientation']['q_1'] = 'real' + self.tablecolumns['position_orientation']['q_2'] = 'real' + self.tablecolumns['position_orientation']['q_3'] = 'real' + self.tablecolumns['position_orientation']['rotconv_id'] = 'string' self.tablecolumns['image'] = dict() self.tablecolumns['image']['data'] = 'array' # self.tablecolumns['viewing_directions'] = dict() @@ -120,6 +133,13 @@ It creates three sql table on initialisation. self.viewing_directions[..., 1] = ma def table_exist(self, tablename): + """ + checks wether a table with name tablename exists in the database + :param tablename: name of the table + :type tablename: string + :returns: validity + :rtype: boolean + """ if not isinstance(tablename, str): raise TypeError('tablename should be a string') self.db_cursor.execute( @@ -131,6 +151,15 @@ It creates three sql table on initialisation. return bool(self.db_cursor.fetchone()) def check_data_validity(self, rowid): + """ + checks wether all three tables in the database + (images,position_orientation, normalisation) contain + an entry with the given id. + :param rowid: id to be checked + :type rowid: int + :returns: validity + :rtype: boolean + """ if not isinstance(rowid, int): raise TypeError('rowid must be an integer') if rowid <= 0: @@ -161,6 +190,34 @@ It creates three sql table on initialisation. return valid def get_posid(self, posorient): + """ + returns the id of a given position and orientation + in the database + :param posorient: position and orientation + is a 1x6 vector containing: + *in case of euler angeles the index should be + ['location']['x'] + ['location']['y'] + ['location']['z'] + [convention][alpha_0] + [convention][alpha_1] + [convention][alpha_2] + **where convention can be: + rxyz, rxzy, ryxz, ryzx, rzyx, rzxy + *in case of quaternions the index should be + ['location']['x'] + ['location']['y'] + ['location']['z'] + [convention]['q_0'] + [convention]['q_1'] + [convention]['q_2'] + [convention]['q_3'] + **where convention can be: + quaternion + :type rowid: pd.Series + :returns: id + :rtype: int + """ if not isinstance(posorient, pd.Series): raise TypeError('posorient should be a pandas Series') if posorient is not None: @@ -168,39 +225,104 @@ It creates three sql table on initialisation. raise TypeError('posorient should be a pandas Series') if posorient.empty: raise Exception('position must not be empty') - if 'x' not in posorient.index: - raise ValueError('missing index x') - if 'y' not in posorient.index: - raise ValueError('missing index y') - if 'z' not in posorient.index: - raise ValueError('missing index z') - if 'alpha_0' not in posorient.index: - raise ValueError('missing index alpha_0') - if 'alpha_1' not in posorient.index: - raise ValueError('missing index alpha_1') - if 'alpha_2' not in posorient.index: - raise ValueError('missing index alpha_2') + found_convention = False + index = posorient.index + convention = index.get_level_values(0)[-1] + if convention == 'rxyz': + found_convention = True + elif convention == 'ryzx': + found_convention = True + elif convention == 'rxzy': + found_convention = True + elif convention == 'ryxz': + found_convention = True + elif convention == 'rzxy': + found_convention = True + elif convention == 'rzyx': + found_convention = True + elif convention == 'quaternion': + found_convention = True + if not found_convention: + raise ValueError("your convention is not supported") + if convention != 'quaternion': + if 'x' not in posorient.index.get_level_values(1): + raise ValueError('missing index x') + if 'y' not in posorient.index.get_level_values(1): + raise ValueError('missing index y') + if 'z' not in posorient.index.get_level_values(1): + raise ValueError('missing index z') + if 'alpha_0' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_0') + if 'alpha_1' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_1') + if 'alpha_2' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_2') + elif convention == 'quaternion': + if 'x' not in posorient.index.get_level_values(1): + raise ValueError('missing index x') + if 'y' not in posorient.index.get_level_values(1): + raise ValueError('missing index y') + if 'z' not in posorient.index.get_level_values(1): + raise ValueError('missing index z') + if 'q_0' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_0') + if 'q_1' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_1') + if 'q_2' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_2') + if 'q_3' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_2') if np.any(pd.isnull(posorient)): raise ValueError('posorient must not contain nan') - where = """x>=? and x<=?""" - where += """and y>=? and y<=?""" - where += """and z>=? and z<=?""" - where += """and alpha_0>=? and alpha_0<=?""" - where += """and alpha_1>=? and alpha_1<=?""" - where += """and alpha_2>=? and alpha_2<=?""" - params = ( - posorient['x'] - self.__float_tolerance, - posorient['x'] + self.__float_tolerance, - posorient['y'] - self.__float_tolerance, - posorient['y'] + self.__float_tolerance, - posorient['z'] - self.__float_tolerance, - posorient['z'] + self.__float_tolerance, - posorient['alpha_0'] - self.__float_tolerance, - posorient['alpha_0'] + self.__float_tolerance, - posorient['alpha_1'] - self.__float_tolerance, - posorient['alpha_1'] + self.__float_tolerance, - posorient['alpha_2'] - self.__float_tolerance, - posorient['alpha_2'] + self.__float_tolerance) + + where = "" + if convention != 'quaternion': + where += """x>=? and x<=?""" + where += """and y>=? and y<=?""" + where += """and z>=? and z<=?""" + where += """and q_0>=? and q_0<=?""" + where += """and q_1>=? and q_1<=?""" + where += """and q_2>=? and q_2<=?""" + where += """and rotconv_id =?""" + params = ( + posorient['location']['x'] - self.__float_tolerance, + posorient['location']['x'] + self.__float_tolerance, + posorient['location']['y'] - self.__float_tolerance, + posorient['location']['y'] + self.__float_tolerance, + posorient['location']['z'] - self.__float_tolerance, + posorient['location']['z'] + self.__float_tolerance, + posorient[convention]['alpha_0'] - self.__float_tolerance, + posorient[convention]['alpha_0'] + self.__float_tolerance, + posorient[convention]['alpha_1'] - self.__float_tolerance, + posorient[convention]['alpha_1'] + self.__float_tolerance, + posorient[convention]['alpha_2'] - self.__float_tolerance, + posorient[convention]['alpha_2'] + self.__float_tolerance, + convention) + else: + where += """x>=? and x<=?""" + where += """and y>=? and y<=?""" + where += """and z>=? and z<=?""" + where += """and q_0>=? and q_0<=?""" + where += """and q_1>=? and q_1<=?""" + where += """and q_2>=? and q_2<=?""" + where += """and q_3>=? and q_3<=?""" + where += """and rotconv_id =?""" + params = ( + posorient['location']['x'] - self.__float_tolerance, + posorient['location']['x'] + self.__float_tolerance, + posorient['location']['y'] - self.__float_tolerance, + posorient['location']['y'] + self.__float_tolerance, + posorient['location']['z'] - self.__float_tolerance, + posorient['location']['z'] + self.__float_tolerance, + posorient[convention]['q_0'] - self.__float_tolerance, + posorient[convention]['q_0'] + self.__float_tolerance, + posorient[convention]['q_1'] - self.__float_tolerance, + posorient[convention]['q_1'] + self.__float_tolerance, + posorient[convention]['q_2'] - self.__float_tolerance, + posorient[convention]['q_2'] + self.__float_tolerance, + posorient[convention]['q_3'] - self.__float_tolerance, + posorient[convention]['q_3'] + self.__float_tolerance, + convention) self.db_cursor.execute( """ SELECT count(*) @@ -215,19 +337,39 @@ It creates three sql table on initialisation. WHERE {}; """.format(where), params) return self.db_cursor.fetchone()[0] + elif self.create & (convention != 'quaternion'): + self.db_cursor.execute( + """ + INSERT + INTO position_orientation(x,y,z,q_0,q_1,q_2,q_3,rotconv_id) + VALUES (?,?,?,?,?,?,?,?) + """, ( + posorient['location']['x'], + posorient['location']['y'], + posorient['location']['z'], + posorient[convention]['alpha_0'], + posorient[convention]['alpha_1'], + posorient[convention]['alpha_2'], + np.nan, + convention)) + rowid = self.db_cursor.lastrowid + self.db.commit() + return rowid elif self.create: self.db_cursor.execute( """ INSERT - INTO position_orientation(x,y,z,alpha_0,alpha_1,alpha_2) - VALUES (?,?,?,?,?,?) + INTO position_orientation(x,y,z,q_0,q_1,q_2,rotconv_id) + VALUES (?,?,?,?,?,?,?,?) """, ( - posorient['x'], - posorient['y'], - posorient['z'], - posorient['alpha_0'], - posorient['alpha_1'], - posorient['alpha_2'])) + posorient['location']['x'], + posorient['location']['y'], + posorient['location']['z'], + posorient[convention]['q_0'], + posorient[convention]['q_1'], + posorient[convention]['q_2'], + posorient[convention]['q_3'], + convention)) rowid = self.db_cursor.lastrowid self.db.commit() return rowid @@ -284,7 +426,9 @@ class DataBaseLoad(DataBase): @property def posorients(self): """Return the position orientations of all points in the \ -database + database + :returns: all position orientations + :rtype: list of pd.Series """ posorient = pd.read_sql_query( "select * from position_orientation;", self.db) @@ -293,8 +437,10 @@ database @property def normalisations(self): - """Return the position orientations of all points in the \ -database + """Returns the normalised scenes of all points in the \ + database + :returns: all position orientations + :rtype: list of pd.Series """ posorient = pd.read_sql_query( "select * from normalisation;", self.db) @@ -307,20 +453,55 @@ database raise TypeError('posorient should be a pandas Series') if posorient.empty: raise Exception('position must not be empty') - if 'x' not in posorient.index: - raise ValueError('missing index x') - if 'y' not in posorient.index: - raise ValueError('missing index y') - if 'z' not in posorient.index: - raise ValueError('missing index z') - if 'alpha_0' not in posorient.index: - raise ValueError('missing index alpha_0') - if 'alpha_1' not in posorient.index: - raise ValueError('missing index alpha_1') - if 'alpha_2' not in posorient.index: - raise ValueError('missing index alpha_2') + found_convention = False + index = posorient.index + convention = index.get_level_values(0)[-1] + if convention == 'rxyz': + found_convention = True + elif convention == 'ryzx': + found_convention = True + elif convention == 'rxzy': + found_convention = True + elif convention == 'ryxz': + found_convention = True + elif convention == 'rzxy': + found_convention = True + elif convention == 'rzyx': + found_convention = True + elif convention == 'quaternion': + found_convention = True + if not found_convention: + raise ValueError("your convention is not supported") + if convention != 'quaternion': + if 'x' not in posorient.index.get_level_values(1): + raise ValueError('missing index x') + if 'y' not in posorient.index.get_level_values(1): + raise ValueError('missing index y') + if 'z' not in posorient.index.get_level_values(1): + raise ValueError('missing index z') + if 'alpha_0' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_0') + if 'alpha_1' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_1') + if 'alpha_2' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_2') + elif convention == 'quaternion': + if 'x' not in posorient.index.get_level_values(1): + raise ValueError('missing index x') + if 'y' not in posorient.index.get_level_values(1): + raise ValueError('missing index y') + if 'z' not in posorient.index.get_level_values(1): + raise ValueError('missing index z') + if 'q_0' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_0') + if 'q_1' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_1') + if 'q_2' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_2') + if 'q_3' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_2') if np.any(pd.isnull(posorient)): - raise ValueError('posorient must not contain nan') + raise ValueError('posorient must not contain nan') if rowid is not None: if not isinstance(rowid, int): raise TypeError('rowid must be an integer') @@ -343,15 +524,65 @@ database toreturn = toreturn.loc[0, :] toreturn.name = toreturn.id toreturn.drop('id') - toreturn = toreturn.astype(float) - return toreturn + convention = toreturn.loc['rotconv_id'] + # toreturn = toreturn.astype(float) + posorient = None + if not convention == 'quaternion': + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', 'alpha_2')] + index = pd.MultiIndex.from_tuples(tuples, + names=['position', + 'orientation']) + posorient = pd.Series(index=index) + posorient['location']['x'] = toreturn.loc['x'] + posorient['location']['y'] = toreturn.loc['y'] + posorient['location']['z'] = toreturn.loc['z'] + posorient[convention]['alpha_0'] = toreturn.loc['q_0'] + posorient[convention]['alpha_1'] = toreturn.loc['q_1'] + posorient[convention]['alpha_2'] = toreturn.loc['q_2'] + else: + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), (convention, 'q_0'), + (convention, 'q_1'), (convention, 'q_2'), + (convention, 'q_3')] + index = pd.MultiIndex.from_tuples(tuples, + names=['position', + 'orientation']) + posorient = pd.Series(index=index) + posorient['location']['x'] = toreturn.loc['x'] + posorient['location']['y'] = toreturn.loc['y'] + posorient['location']['z'] = toreturn.loc['z'] + posorient[convention]['q_0'] = toreturn.loc['q_0'] + posorient[convention]['q_1'] = toreturn.loc['q_1'] + posorient[convention]['q_2'] = toreturn.loc['q_2'] + posorient[convention]['q_3'] = toreturn.loc['q_3'] + + return posorient def scene(self, posorient=None, rowid=None): """Read an image at a given position-orientation or given id of row in the \ database. - - :param posorient: a pandas Series with index \ - ['x','y','z','alpha_0','alpha_1','alpha_2'] + :param posorient: is a 1x6 vector containing: + *in case of euler angeles the index should be + ['location']['x'] + ['location']['y'] + ['location']['z'] + [convention][alpha_0] + [convention][alpha_1] + [convention][alpha_2] + **where convention can be: + rxyz, rxzy, ryxz, ryzx, rzyx, rzxy + *in case of quaternions the index should be + ['location']['x'] + ['location']['y'] + ['location']['z'] + [convention]['q_0'] + [convention]['q_1'] + [convention]['q_2'] + [convention]['q_3'] + **where convention can be: + quaternion :param rowid: an integer :returns: an image :rtype: numpy.ndarray @@ -362,18 +593,53 @@ database raise TypeError('posorient should be a pandas Series') if posorient.empty: raise Exception('position must not be empty') - if 'x' not in posorient.index: - raise ValueError('missing index x') - if 'y' not in posorient.index: - raise ValueError('missing index y') - if 'z' not in posorient.index: - raise ValueError('missing index z') - if 'alpha_0' not in posorient.index: - raise ValueError('missing index alpha_0') - if 'alpha_1' not in posorient.index: - raise ValueError('missing index alpha_1') - if 'alpha_2' not in posorient.index: - raise ValueError('missing index alpha_2') + found_convention = False + index = posorient.index + convention = index.get_level_values(0)[-1] + if convention == 'rxyz': + found_convention = True + elif convention == 'ryzx': + found_convention = True + elif convention == 'rxzy': + found_convention = True + elif convention == 'ryxz': + found_convention = True + elif convention == 'rzxy': + found_convention = True + elif convention == 'rzyx': + found_convention = True + elif convention == 'quaternion': + found_convention = True + if not found_convention: + raise ValueError("your convention is not supported") + if convention != 'quaternion': + if 'x' not in posorient.index.get_level_values(1): + raise ValueError('missing index x') + if 'y' not in posorient.index.get_level_values(1): + raise ValueError('missing index y') + if 'z' not in posorient.index.get_level_values(1): + raise ValueError('missing index z') + if 'alpha_0' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_0') + if 'alpha_1' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_1') + if 'alpha_2' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_2') + elif convention == 'quaternion': + if 'x' not in posorient.index.get_level_values(1): + raise ValueError('missing index x') + if 'y' not in posorient.index.get_level_values(1): + raise ValueError('missing index y') + if 'z' not in posorient.index.get_level_values(1): + raise ValueError('missing index z') + if 'q_0' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_0') + if 'q_1' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_1') + if 'q_2' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_2') + if 'q_3' not in posorient.index.get_level_values(1): + raise ValueError('missing index alpha_2') if np.any(pd.isnull(posorient)): raise ValueError('posorient must not contain nan') if rowid is not None: @@ -420,6 +686,14 @@ database return toreturn def denormalise_image(self, image, cmaxminrange): + """denomalises an image + :param image: the image to be denormalised + :param cmaxminrange: new range of the denormalised image + :type image: np.ndarray + :type cmaxminrange: pd.Series + :returns: denormalised image + :rtype: numpy.ndarray + """ if not isinstance(image, np.ndarray): raise TypeError('image must be np.array') if len(image.shape) != 3: @@ -484,6 +758,33 @@ class DataBaseSave(DataBase): return True def write_image(self, posorient, image): + """stores an image in the database. Automatically + calculates the cminmax range from the image and + channels. + :param posorient: is a 1x6 vector containing: + *in case of euler angeles the index should be + ['location']['x'] + ['location']['y'] + ['location']['z'] + [convention][alpha_0] + [convention][alpha_1] + [convention][alpha_2] + **where convention can be: + rxyz, rxzy, ryxz, ryzx, rzyx, rzxy + *in case of quaternions the index should be + ['location']['x'] + ['location']['y'] + ['location']['z'] + [convention]['q_0'] + [convention]['q_1'] + [convention]['q_2'] + [convention]['q_3'] + **where convention can be: + quaternion + :param image: image to be stored + :type image: np.ndarray + :type posorient: pd.Series + """ normed_im, cmaxminrange = self.normalise_image(image, self.arr_dtype) rowid = self.get_posid(posorient) # Write image @@ -530,6 +831,13 @@ class DataBaseSave(DataBase): self.db.commit() def normalise_image(self, image, dtype=np.uint8): + """normalises an image to a range between 0 and 1. + :param image: image to be normalised + :param dtype: type of the image (default: np.uint8) + :type image: np.ndarray + :returns: normalised image + :rtype: np.ndarray + """ if not isinstance(image, np.ndarray): raise TypeError('image must be np.array') if np.any(np.isnan(image)): diff --git a/navipy/database/test.py b/navipy/database/test.py index c073d3a15dab13462e9d525416a8084a460ad3f2..372faea96de60441c9a94152817af4c9bd1b67f4 100644 --- a/navipy/database/test.py +++ b/navipy/database/test.py @@ -12,7 +12,7 @@ from navipy.database import DataBaseLoad, DataBaseSave, DataBase class TestCase(unittest.TestCase): def setUp(self): self.mydb_filename = pkg_resources.resource_filename( - 'navipy', 'resources/database.db') + 'navipy', 'resources/database2.db') self.mydb = database.DataBaseLoad(self.mydb_filename) def test_DataBase_init_(self): @@ -103,67 +103,95 @@ class TestCase(unittest.TestCase): c = conn.cursor() c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) rows = c.fetchall()[0] - # working case - posorient = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient.x = rows[5] - posorient.y = rows[6] - posorient.z = rows[1] - posorient.alpha_0 = rows[3] - posorient.alpha_1 = rows[2] - posorient.alpha_2 = rows[4] + # convention = rows[1] + + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', '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['rxyz']['alpha_0'] = rows[3] + posorient['rxyz']['alpha_1'] = rows[5] + posorient['rxyz']['alpha_2'] = rows[4] + posid = self.mydb.get_posid(posorient) assert posid == 1 # incorrect case missing column - posorient2 = pd.Series(index=['y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient2.y = posorient.y - posorient2.z = posorient.z - posorient2.alpha_0 = posorient.alpha_0 - posorient2.alpha_1 = posorient.alpha_1 - posorient2.alpha_2 = posorient.alpha_2 + tuples = [('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', '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['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] + with self.assertRaises(Exception): posid = self.mydb.get_posid(posorient2) # incorrect case None - posorient2 = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient2.x = None - posorient2.y = posorient.y - posorient2.z = posorient.z - posorient2.alpha_0 = posorient.alpha_0 - posorient2.alpha_1 = posorient.alpha_1 - posorient2.alpha_2 = posorient.alpha_2 + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', '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['rxyz']['alpha_0'] = rows[3] + posorient3['rxyz']['alpha_1'] = rows[5] + posorient3['rxyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): posid = self.mydb.get_posid(posorient2) # incorrect case nan - posorient2 = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient2.x = np.nan - posorient2.y = posorient.y - posorient2.z = posorient.z - posorient2.alpha_0 = posorient.alpha_0 - posorient2.alpha_1 = posorient.alpha_1 - posorient2.alpha_2 = posorient.alpha_2 + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', '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['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): posid = self.mydb.get_posid(posorient2) # incorrect case no pandas series but dict posorient2 = {} - posorient2['x'] = posorient.x - posorient2['y'] = posorient.y - posorient2['z'] = posorient.z - posorient2['alpha_0'] = posorient.alpha_0 - posorient2['alpha_1'] = posorient.alpha_1 - posorient2['alpha_2'] = posorient.alpha_2 + posorient2['location']['x'] = rows[6] + posorient2['location']['y'] = rows[7] + posorient2['location']['z'] = rows[8] + posorient2['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] with self.assertRaises(TypeError): self.mydb.get_posid(posorient2) # not working case empty - posorient2 = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', '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) @@ -204,66 +232,94 @@ class TestCase(unittest.TestCase): c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) rows = c.fetchall()[0] # working case - posorient = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient.x = rows[5] - posorient.y = rows[6] - posorient.z = rows[1] - posorient.alpha_0 = rows[3] - posorient.alpha_1 = rows[2] - posorient.alpha_2 = rows[4] + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', '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['rxyz']['alpha_0'] = rows[3] + posorient['rxyz']['alpha_1'] = rows[5] + posorient['rxyz']['alpha_2'] = rows[4] posid = self.mydb.read_posorient(posorient=posorient) - assert posid.x == posorient.x + # print(posid) + assert posid['location']['x'] == posorient['location']['x'] # incorrect case missing column - posorient2 = pd.Series(index=['y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient2.y = posorient.y - posorient2.z = posorient.z - posorient2.alpha_0 = posorient.alpha_0 - posorient2.alpha_1 = posorient.alpha_1 - posorient2.alpha_2 = posorient.alpha_2 + tuples = [('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', '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['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): self.mydb.read_posorient(posorient=posorient2) # incorrect case None - posorient2 = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient2.x = None - posorient2.y = posorient.y - posorient2.z = posorient.z - posorient2.alpha_0 = posorient.alpha_0 - posorient2.alpha_1 = posorient.alpha_1 - posorient2.alpha_2 = posorient.alpha_2 + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', 'alpha_2')] + index = pd.MultiIndex.from_tuples(tuples, + names=['position', 'orientation']) + + posorient2 = pd.Series(index=index) + posorient2['location']['x'] = None + posorient2['location']['y'] = rows[7] + posorient2['location']['z'] = rows[8] + posorient2['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): self.mydb.read_posorient(posorient=posorient2) # incorrect case nan - posorient2 = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient2.x = np.nan - posorient2.y = posorient.y - posorient2.z = posorient.z - posorient2.alpha_0 = posorient.alpha_0 - posorient2.alpha_1 = posorient.alpha_1 - posorient2.alpha_2 = posorient.alpha_2 + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', 'alpha_2')] + index = pd.MultiIndex.from_tuples(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['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): self.mydb.read_posorient(posorient=posorient2) # incorrect case no pandas series but dict posorient2 = {} - posorient2['x'] = posorient.x - posorient2['y'] = posorient.y - posorient2['z'] = posorient.z - posorient2['alpha_0'] = posorient.alpha_0 - posorient2['alpha_1'] = posorient.alpha_1 - posorient2['alpha_2'] = posorient.alpha_2 + posorient2['location'] = {} + posorient2['rxyz'] = {} + posorient2['location']['x'] = rows[6] + posorient2['location']['y'] = rows[7] + posorient2['location']['z'] = rows[8] + posorient2['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] with self.assertRaises(TypeError): self.mydb.read_posorient(posorient=posorient2) # not working case empty - posorient2 = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', 'alpha_2')] + index = pd.MultiIndex.from_tuples(tuples, + names=['position', 'orientation']) + + posorient2 = pd.Series(index=index) with self.assertRaises(Exception): self.mydb.read_posorient(posorient=posorient2) @@ -283,15 +339,19 @@ class TestCase(unittest.TestCase): c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) rows = c.fetchall()[0] # working case - posorient = pd.Series(index=['id', 'x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient.id = rows[0] - posorient.x = rows[5] - posorient.y = rows[6] - posorient.z = rows[1] - posorient.alpha_0 = rows[3] - posorient.alpha_1 = rows[2] - posorient.alpha_2 = rows[4] + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', '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['rxyz']['alpha_0'] = rows[3] + posorient['rxyz']['alpha_1'] = rows[5] + posorient['rxyz']['alpha_2'] = rows[4] for rowid in [0, -2]: with self.assertRaises(ValueError): # print("rowid",rowid) @@ -307,13 +367,15 @@ class TestCase(unittest.TestCase): for rowid in [1]: posoriend2 = self.mydb.read_posorient(rowid=rowid) - assert posoriend2.id == posorient.id - assert posoriend2.x == posorient.x - assert posoriend2.y == posorient.y - assert posoriend2.z == posorient.z - assert posoriend2.alpha_0 == posorient.alpha_0 - assert posoriend2.alpha_1 == posorient.alpha_1 - assert posoriend2.alpha_2 == posorient.alpha_2 + assert posoriend2['location']['x'] == posorient['location']['x'] + assert posoriend2['location']['y'] == posorient['location']['y'] + assert posoriend2['location']['z'] == posorient['location']['z'] + assert (posoriend2['rxyz']['alpha_0'] == + posorient['rxyz']['alpha_0']) + assert (posoriend2['rxyz']['alpha_1'] == + posorient['rxyz']['alpha_1']) + assert (posoriend2['rxyz']['alpha_2'] == + posorient['rxyz']['alpha_2']) def test_scene_id(self): """ @@ -367,14 +429,18 @@ class TestCase(unittest.TestCase): c.execute(""" SELECT * FROM position_orientation WHERE (rowid=1) """) rows = c.fetchall()[0] # working case - posorient = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient.x = rows[5] - posorient.y = rows[6] - posorient.z = rows[1] - posorient.alpha_0 = rows[3] - posorient.alpha_1 = rows[2] - posorient.alpha_2 = rows[4] + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', '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['rxyz']['alpha_0'] = rows[3] + posorient['rxyz']['alpha_1'] = rows[5] + posorient['rxyz']['alpha_2'] = rows[4] image = self.mydb.scene(posorient=posorient) self.assertIsNotNone(image) self.assertFalse(sum(image.shape) == 0) @@ -383,54 +449,73 @@ class TestCase(unittest.TestCase): self.assertTrue(image.shape[3] == 1) # incorrect case missing column - posorient2 = pd.Series(index=['y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient2.y = posorient.y - posorient2.z = posorient.z - posorient2.alpha_0 = posorient.alpha_0 - posorient2.alpha_1 = posorient.alpha_1 - posorient2.alpha_2 = posorient.alpha_2 + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', '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['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] with self.assertRaises(Exception): image = self.mydb.scene(posorient=posorient2) # incorrect case None - posorient2 = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient2.x = None - posorient2.y = posorient.y - posorient2.z = posorient.z - posorient2.alpha_0 = posorient.alpha_0 - posorient2.alpha_1 = posorient.alpha_1 - posorient2.alpha_2 = posorient.alpha_2 + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', 'alpha_2')] + index = pd.MultiIndex.from_tuples(tuples, + names=['position', 'orientation']) + + posorient2 = pd.Series(index=index) + posorient2['location']['x'] = None + posorient2['location']['y'] = rows[7] + posorient2['location']['z'] = rows[8] + posorient2['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): image = self.mydb.scene(posorient=posorient2) # incorrect case nan - posorient2 = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) - posorient2.x = np.nan - posorient2.y = posorient.y - posorient2.z = posorient.z - posorient2.alpha_0 = posorient.alpha_0 - posorient2.alpha_1 = posorient.alpha_1 - posorient2.alpha_2 = posorient.alpha_2 + tuples = [('location', 'x'), ('location', 'y'), + ('location', 'z'), ('rxyz', 'alpha_0'), + ('rxyz', 'alpha_1'), ('rxyz', 'alpha_2')] + index = pd.MultiIndex.from_tuples(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['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] with self.assertRaises(ValueError): image = self.mydb.scene(posorient=posorient2) # incorrect case no pandas series but dict posorient2 = {} - posorient2['x'] = posorient.x - posorient2['y'] = posorient.y - posorient2['z'] = posorient.z - posorient2['alpha_0'] = posorient.alpha_0 - posorient2['alpha_1'] = posorient.alpha_1 - posorient2['alpha_2'] = posorient.alpha_2 + posorient2['location'] = {} + posorient2['rxyz'] = {} + posorient2['location']['x'] = rows[6] + posorient2['location']['y'] = rows[7] + posorient2['location']['z'] = rows[8] + posorient2['rxyz']['alpha_0'] = rows[3] + posorient2['rxyz']['alpha_1'] = rows[5] + posorient2['rxyz']['alpha_2'] = rows[4] with self.assertRaises(TypeError): image = self.mydb.scene(posorient=posorient2) # not working case empty - posorient2 = pd.Series(index=['x', 'y', 'z', - 'alpha_0', 'alpha_1', 'alpha_2']) + index = pd.MultiIndex.from_tuples(tuples, + names=['position', 'orientation']) + + posorient2 = pd.Series(index=index) with self.assertRaises(Exception): image = self.mydb.scene(posorient=posorient2) diff --git a/setup.py b/setup.py index d445eafb86a023c37d2e043b3f6af03c130f1e48..c6b371505520a9900012577901d19bbad2a6c156 100644 --- a/setup.py +++ b/setup.py @@ -45,6 +45,7 @@ setup_dict = {'name': 'navipy', 'flake8', 'tox'], 'package_data': {'navipy': ['resources/database.db', + 'resources/database2.db', 'resources/*.blend']}, 'include_package_data': True, 'entry_points': {