Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
N
navipy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Olivier Bertrand
navipy
Commits
86d05f04
Commit
86d05f04
authored
7 years ago
by
Luise Odenthal
Browse files
Options
Downloads
Patches
Plain Diff
flake8 test database init
parent
5ca49f90
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
navipy/database/__init__.py
+41
-32
41 additions, 32 deletions
navipy/database/__init__.py
navipy/database/__init__.py~
+158
-26
158 additions, 26 deletions
navipy/database/__init__.py~
with
199 additions
and
58 deletions
navipy/database/__init__.py
+
41
−
32
View file @
86d05f04
...
...
@@ -91,7 +91,7 @@ def adapt_array(arr):
"""
http://stackoverflow.com/a/31312102/190597 (SoulNibbler)
"""
if
arr
ay
is
None
:
if
arr
is
None
:
raise
ValueError
(
'
array must not be None
'
)
out
=
io
.
BytesIO
()
np
.
save
(
out
,
arr
)
...
...
@@ -123,11 +123,13 @@ It creates three sql table on initialisation.
"""
Initialisation of the database
"""
if
not
isinstance
(
filename
,
str
):
raise
TypeError
(
'
filename should be a string
'
)
if
(
not
isinstance
(
channels
,
list
)
or
not
isinstance
(
channels
,
np
.
array
)):
if
(
not
isinstance
(
channels
,
list
)
or
not
isinstance
(
channels
,
np
.
array
)):
raise
TypeError
(
'
nb_channel should be a list or np array
'
)
"""
for c in channels:
if not c in [
'
R
'
,
'
G
'
,
'
B
'
,
'
D
'
]:
raise ValueError(
'
channels must be either R,G,B or D (Distance)
'
)
"""
raise ValueError(
'
channels
must be either R,G,B or D (Distance)
'
)
"""
self
.
filename
=
filename
self
.
channels
=
channels
self
.
normalisation_columns
=
list
()
...
...
@@ -159,8 +161,8 @@ It creates three sql table on initialisation.
self
.
db_cursor
=
self
.
db
.
cursor
()
for
tablename
,
_
in
self
.
tablecolumns
.
items
():
if
not
self
.
table_exist
(
tablename
):
raise
Exception
(
'
{} does not contain a table
named {}
'
.
format
(
filename
,
tablename
))
raise
Exception
(
'
{} does not contain a table
\
named {}
'
.
format
(
filename
,
tablename
))
elif
self
.
create
:
# Create database
self
.
db
=
sqlite3
.
connect
(
...
...
@@ -196,7 +198,7 @@ It creates three sql table on initialisation.
return
bool
(
self
.
db_cursor
.
fetchone
())
def
check_data_validity
(
self
,
rowid
):
if
not
isinstance
(
rowid
,
int
):
if
not
isinstance
(
rowid
,
int
):
raise
TypeError
(
'
rowid must be an integer
'
)
if
rowid
<=
0
:
raise
ValueError
(
'
rowid must be greater zero
'
)
...
...
@@ -226,8 +228,8 @@ It creates three sql table on initialisation.
return
valid
def
get_posid
(
self
,
posorient
):
if
no
isinstance
(
posorient
,
pd
.
Series
):
(
'
posorient should be a pandas Series
'
)
if
no
t
isinstance
(
posorient
,
pd
.
Series
):
raise
TypeError
(
'
posorient should be a pandas Series
'
)
if
posorient
is
not
None
:
if
not
isinstance
(
posorient
,
pd
.
Series
):
raise
TypeError
(
'
posorient should be a pandas Series
'
)
...
...
@@ -313,11 +315,13 @@ class DataBaseLoad(DataBase):
"""
Initialise the DataBaseLoader
"""
if
not
isinstance
(
filename
,
str
):
raise
TypeError
(
'
filename should be a string
'
)
if
(
not
isinstance
(
channels
,
list
)
or
not
isinstance
(
channels
,
np
.
array
)):
if
(
not
isinstance
(
channels
,
list
)
or
not
isinstance
(
channels
,
np
.
array
)):
raise
TypeError
(
'
nb_channel should be a list or np array
'
)
for
c
in
channels
:
if
not
c
in
[
'
R
'
,
'
G
'
,
'
B
'
,
'
D
'
]:
raise
ValueError
(
'
channels must be either R,G,B or D (Distance)
'
)
if
c
not
in
[
'
R
'
,
'
G
'
,
'
B
'
,
'
D
'
]:
raise
ValueError
(
'
channels must be either
\
R,G,B or D (Distance)
'
)
DataBase
.
__init__
(
self
,
filename
,
channels
=
channels
)
@property
...
...
@@ -356,7 +360,7 @@ database
return
posorient
def
read_posorient
(
self
,
posorient
=
None
,
rowid
=
None
):
if
no
isinstance
(
posorient
,
pd
.
Series
):
if
no
t
isinstance
(
posorient
,
pd
.
Series
):
(
'
posorient should be a pandas Series
'
)
if
posorient
is
not
None
:
if
not
isinstance
(
posorient
,
pd
.
Series
):
...
...
@@ -375,15 +379,15 @@ database
raise
ValueError
(
'
missing index alpha_1
'
)
if
'
alpha_2
'
not
in
posorient
.
index
:
raise
ValueError
(
'
missing index alpha_2
'
)
if
not
~
np
.
any
(
pd
.
isnull
(
posorient
)):
if
np
.
any
(
pd
.
isnull
(
posorient
)):
raise
ValueError
(
'
posorient must not contain nan
'
)
if
not
isinstance
(
rowid
,
int
):
if
not
isinstance
(
rowid
,
int
):
raise
TypeError
(
'
rowid must be an integer
'
)
if
rowid
<=
0
:
raise
ValueError
(
'
rowid must be greater zero
'
)
if
rowid
is
np
.
nan
:
raise
ValueError
(
'
rowid must not be nan
'
)
if
(
posorient
is
None
)
and
(
rowid
is
None
):
if
(
posorient
is
None
)
and
(
rowid
is
None
):
Exception
(
'
posorient and rowid can not be both None
'
)
if
posorient
is
not
None
:
rowid
=
self
.
get_posid
(
posorient
)
...
...
@@ -402,7 +406,7 @@ database
return
toreturn
def
read_image
(
self
,
posorient
=
None
,
rowid
=
None
):
if
no
isinstance
(
posorient
,
pd
.
Series
):
if
no
t
isinstance
(
posorient
,
pd
.
Series
):
(
'
posorient should be a pandas Series
'
)
if
posorient
is
not
None
:
if
not
isinstance
(
posorient
,
pd
.
Series
):
...
...
@@ -421,9 +425,9 @@ database
raise
ValueError
(
'
missing index alpha_1
'
)
if
'
alpha_2
'
not
in
posorient
.
index
:
raise
ValueError
(
'
missing index alpha_2
'
)
if
not
~
np
.
any
(
pd
.
isnull
(
posorient
)):
if
np
.
any
(
pd
.
isnull
(
posorient
)):
raise
ValueError
(
'
posorient must not contain nan
'
)
if
not
isinstance
(
rowid
,
int
):
if
not
isinstance
(
rowid
,
int
):
raise
TypeError
(
'
rowid must be an integer
'
)
if
rowid
<=
0
:
raise
ValueError
(
'
rowid must be greater zero
'
)
...
...
@@ -463,7 +467,7 @@ database
FROM {}
WHERE (rowid={})
"""
.
format
(
tablename
,
rowid
),
self
.
db
)
if
cmaxminrange
.
shape
[
0
]
!=
1
:
if
cmaxminrange
.
shape
[
0
]
!=
1
:
raise
Exception
(
'
Error while reading normalisation factors
'
)
cmaxminrange
=
cmaxminrange
.
iloc
[
0
,
:]
cmaxminrange
.
name
=
cmaxminrange
.
id
...
...
@@ -475,19 +479,23 @@ database
if
len
(
image
.
shape
)
!=
3
:
raise
Exception
(
'
image should be 3D array
'
)
if
image
.
shape
[
2
]
!=
len
(
self
.
channels
):
raise
Exception
(
'
image does not have the required number of channels {}
'
.
format
(
raise
Exception
(
'
image does not have the required
\
number of channels {}
'
.
format
(
len
(
self
.
channels
)))
if
not
isinstance
(
cmaxminrange
,
pd
.
Series
):
raise
Exception
(
'
cmaxminrange should be a pandas Series
'
)
if
cmaxminrange
.
empty
:
raise
Exception
(
'
cmaxminrange must not be empty
'
)
raise
Exception
(
'
cmaxminrange must not be empty
'
)
for
chan_n
in
self
.
channels
:
if
str
(
chan_n
)
+
'
_max
'
not
in
posorient
.
index
:
raise
ValueError
(
'
cminmax range is missing index
'
+
str
(
chan_n
)
+
'
_max
'
)
if
str
(
chan_n
)
+
'
_min
'
not
in
posorient
.
index
:
raise
ValueError
(
'
cminmax range is missing index
'
+
str
(
chan_n
)
+
'
_min
'
)
if
str
(
chan_n
)
+
'
_range
'
not
in
posorient
.
index
:
raise
ValueError
(
'
cminmax range is missing index
'
+
str
(
chan_n
)
+
'
_range
'
)
if
str
(
chan_n
)
+
'
_max
'
not
in
cmaxminrange
.
index
:
raise
ValueError
(
'
cminmax range is missing index
'
+
str
(
chan_n
)
+
'
_max
'
)
if
str
(
chan_n
)
+
'
_min
'
not
in
cmaxminrange
.
index
:
raise
ValueError
(
'
cminmax range is missing index
'
+
str
(
chan_n
)
+
'
_min
'
)
if
str
(
chan_n
)
+
'
_range
'
not
in
cmaxminrange
.
index
:
raise
ValueError
(
'
cminmax range is missing index
'
+
str
(
chan_n
)
+
'
_range
'
)
if
any
(
np
.
isnan
(
cmaxminrange
.
loc
[
str
(
chan_n
)
+
'
_max
'
])):
raise
ValueError
(
'
cmaxminrange contains nans
'
)
if
any
(
np
.
isnan
(
cmaxminrange
.
loc
[
str
(
chan_n
)
+
'
_min
'
])):
...
...
@@ -575,15 +583,16 @@ class DataBaseSave(DataBase):
self
.
db
.
commit
()
def
normalise_image
(
self
,
image
,
dtype
=
np
.
uint8
):
if
not
isinstance
(
image
,
np
.
ndarray
):
if
not
isinstance
(
image
,
np
.
ndarray
):
raise
TypeError
(
'
image must be np.array
'
)
if
any
(
np
.
isnan
(
image
)):
raise
ValueError
(
'
image must not contain nan values
'
)
if
image
.
shape
[
0
]
<=
0
or
image
.
shape
[
1
]
<=
0
:
if
image
.
shape
[
0
]
<=
0
or
image
.
shape
[
1
]
<=
0
:
raise
Exception
(
'
image dimensions incorrect
'
)
if
image
.
shape
[
2
]
!=
len
(
self
.
channels
):
raise
Exception
(
'
image channels number differs from given channel number
'
)
if
not
tools
.
is_numeric_array
(
scene
):
if
image
.
shape
[
2
]
!=
len
(
self
.
channels
):
raise
Exception
(
'
image channels number differs from
\
given channel number
'
)
if
not
tools
.
is_numeric_array
(
image
):
raise
TypeError
(
'
scene is of non numeric type
'
)
normed_im
=
np
.
zeros
(
image
.
shape
,
dtype
=
dtype
)
maxval_nim
=
np
.
iinfo
(
normed_im
.
dtype
).
max
...
...
This diff is collapsed.
Click to expand it.
navipy/database/__init__.py~
+
158
−
26
View file @
86d05f04
...
...
@@ -84,12 +84,15 @@ import pandas as pd
import sqlite3
import io
import warnings
import navipy.processing.tools as tools
def adapt_array(arr):
"""
http://stackoverflow.com/a/31312102/190597 (SoulNibbler)
"""
if array is None:
raise ValueError('array must not be None')
out = io.BytesIO()
np.save(out, arr)
out.seek(0)
...
...
@@ -97,6 +100,8 @@ def adapt_array(arr):
def convert_array(text):
if text is None:
raise ValueError('text must not be None')
out = io.BytesIO(text)
out.seek(0)
return np.load(out)
...
...
@@ -118,8 +123,13 @@ It creates three sql table on initialisation.
"""Initialisation of the database """
if not isinstance(filename, str):
raise TypeError('filename should be a string')
if not isinstance(channels, list):
raise TypeError('nb_channel should be an integer')
if (not isinstance(channels, list) or
not isinstance(channels, np.array)):
raise TypeError('nb_channel should be a list or np array')
"""for c in channels:
if not c in ['R','G','B','D']:
raise ValueError('channels
must be either R,G,B or D (Distance)')"""
self.filename = filename
self.channels = channels
self.normalisation_columns = list()
...
...
@@ -150,9 +160,9 @@ It creates three sql table on initialisation.
timeout=10)
self.db_cursor = self.db.cursor()
for tablename, _ in self.tablecolumns.items():
asser
t self.table_exist(tablename)
,\
'{} does not contain a table
named {}'.format(
filename, tablename)
if no
t self.table_exist(tablename)
:
raise Exception(
'{} does not contain a table
\
named {}'.format(
filename, tablename)
)
elif self.create:
# Create database
self.db = sqlite3.connect(
...
...
@@ -177,7 +187,8 @@ It creates three sql table on initialisation.
self.viewing_directions[..., 1] = ma
def table_exist(self, tablename):
assert isinstance(tablename, str), 'tablename should be a string'
if not isinstance(tablename, str):
raise TypeError('tablename should be a string')
self.db_cursor.execute(
"""
SELECT count(*)
...
...
@@ -187,6 +198,12 @@ It creates three sql table on initialisation.
return bool(self.db_cursor.fetchone())
def check_data_validity(self, rowid):
if not isinstance(rowid, int):
raise TypeError('rowid must be an integer')
if rowid <= 0:
raise ValueError('rowid must be greater zero')
if rowid is np.nan:
raise ValueError('rowid must not be nan')
self.db_cursor.execute(
"""
SELECT count(*)
...
...
@@ -211,8 +228,27 @@ It creates three sql table on initialisation.
return valid
def get_posid(self, posorient):
assert isinstance(posorient, pd.Series),\
'posorient should be a pandas Series'
if not isinstance(posorient, pd.Series):
raise TypeError('posorient should be a pandas Series')
if posorient is not None:
if not isinstance(posorient, pd.Series):
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')
if not ~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<=?"""
...
...
@@ -277,6 +313,15 @@ class DataBaseLoad(DataBase):
def __init__(self, filename, channels=['R', 'G', 'B', 'D']):
"""Initialise the DataBaseLoader"""
if not isinstance(filename, str):
raise TypeError('filename should be a string')
if (not isinstance(channels, list) or
not isinstance(channels, np.array)):
raise TypeError('nb_channel should be a list or np array')
for c in channels:
if c not in ['R', 'G', 'B', 'D']:
raise ValueError('channels must be either\
R,G,B or D (Distance)')
DataBase.__init__(self, filename, channels=channels)
@property
...
...
@@ -315,8 +360,35 @@ database
return posorient
def read_posorient(self, posorient=None, rowid=None):
assert (posorient is None) or (rowid is None),\
'posorient and rowid can not be both None'
if not isinstance(posorient, pd.Series):
('posorient should be a pandas Series')
if posorient is not None:
if not isinstance(posorient, pd.Series):
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')
if np.any(pd.isnull(posorient)):
raise ValueError('posorient must not contain nan')
if not isinstance(rowid, int):
raise TypeError('rowid must be an integer')
if rowid <= 0:
raise ValueError('rowid must be greater zero')
if rowid is np.nan:
raise ValueError('rowid must not be nan')
if (posorient is None) and (rowid is None):
Exception('posorient and rowid can not be both None')
if posorient is not None:
rowid = self.get_posid(posorient)
# Read images
...
...
@@ -334,6 +406,37 @@ database
return toreturn
def read_image(self, posorient=None, rowid=None):
if not isinstance(posorient, pd.Series):
('posorient should be a pandas Series')
if posorient is not None:
if not isinstance(posorient, pd.Series):
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')
if np.any(pd.isnull(posorient)):
raise ValueError('posorient must not contain nan')
if not isinstance(rowid, int):
raise TypeError('rowid must be an integer')
if rowid <= 0:
raise ValueError('rowid must be greater zero')
if rowid is np.nan:
raise ValueError('rowid must not be nan')
if (posorient is None) and (rowid is None):
raise Exception('posorient and rowid can not be both None')
if posorient is not None:
rowid = self.get_posid(posorient)
"""Read an image at a given position-orientation or given id of row in the \
database.
...
...
@@ -343,8 +446,8 @@ database
:returns: an image
:rtype: numpy.ndarray
"""
assert
(posorient is None)
or
(rowid is None)
,\
'posorient and rowid can not be both None'
if
(posorient is None)
and
(rowid is None)
:
Exception(
'posorient and rowid can not be both None'
)
if posorient is not None:
rowid = self.get_posid(posorient)
# Read images
...
...
@@ -364,8 +467,8 @@ database
FROM {}
WHERE (rowid={})
""".format(tablename, rowid), self.db)
assert
cmaxminrange.shape[0]
=
= 1
,\
'Error while reading normalisation factors'
if
cmaxminrange.shape[0]
!
= 1
:
raise Exception(
'Error while reading normalisation factors'
)
cmaxminrange = cmaxminrange.iloc[0, :]
cmaxminrange.name = cmaxminrange.id
cmaxminrange.drop('id')
...
...
@@ -373,13 +476,32 @@ database
return self.denormalise_image(image, cmaxminrange)
def denormalise_image(self, image, cmaxminrange):
assert len(image.shape) == 3,\
'image should be 3D array'
assert image.shape[2] == len(self.channels),\
'image does not have the required number of channels {}'.format(
len(self.channels))
assert isinstance(cmaxminrange, pd.Series),\
'cmaxminrange should be a pandas Series'
if len(image.shape) != 3:
raise Exception('image should be 3D array')
if image.shape[2] != len(self.channels):
raise Exception('image does not have the required\
number of channels {}'.format(
len(self.channels)))
if not isinstance(cmaxminrange, pd.Series):
raise Exception('cmaxminrange should be a pandas Series')
if cmaxminrange.empty:
raise Exception('cmaxminrange must not be empty')
for chan_n in self.channels:
if str(chan_n) + '_max' not in cmaxminrange.index:
raise ValueError('cminmax range is missing index '
+ str(chan_n) + '_max')
if str(chan_n) + '_min' not in cmaxminrange.index:
raise ValueError('cminmax range is missing index '
+ str(chan_n) + '_min')
if str(chan_n) + '_range' not in cmaxminrange.index:
raise ValueError('cminmax range is missing index '
+ str(chan_n) + '_range')
if any(np.isnan(cmaxminrange.loc[str(chan_n) + '_max'])):
raise ValueError('cmaxminrange contains nans')
if any(np.isnan(cmaxminrange.loc[str(chan_n) + '_min'])):
raise ValueError('cmaxminrange contains nans')
if any(np.isnan(cmaxminrange.loc[str(chan_n) + '_range'])):
raise ValueError('cmaxminrange contains nans')
denormed_im = np.zeros(image.shape, dtype=np.float)
maxval_nim = np.iinfo(image.dtype).max
#
...
...
@@ -408,7 +530,6 @@ class DataBaseSave(DataBase):
DataBase.__init__(self, filename, channels=channels)
self.arr_dtype = arr_dtype
def create(self):
"""use to decide weather to alter the database or not
return True because we will need
...
...
@@ -433,10 +554,10 @@ class DataBaseSave(DataBase):
self.insert_replace(tablename, params)
def insert_replace(self, tablename, params):
asser
t isinstance(tablename, str)
,\
'table are named by string'
asser
t isinstance(params, dict)
,\
'params should be dictionary columns:val'
if no
t isinstance(tablename, str)
:
raise ValueError(
'table are named by string'
)
if no
t isinstance(params, dict)
:
raise ValueError(
'params should be dictionary columns:val'
)
params_list = list()
columns_str = ''
for key, val in params.items():
...
...
@@ -462,6 +583,17 @@ class DataBaseSave(DataBase):
self.db.commit()
def normalise_image(self, image, dtype=np.uint8):
if not isinstance(image, np.ndarray):
raise TypeError('image must be np.array')
if any(np.isnan(image)):
raise ValueError('image must not contain nan values')
if image.shape[0] <= 0 or image.shape[1] <= 0:
raise Exception('image dimensions incorrect')
if image.shape[2] != len(self.channels):
raise Exception('image channels number differs from\
given channel number')
if not tools.is_numeric_array(image):
raise TypeError('scene is of non numeric type')
normed_im = np.zeros(image.shape, dtype=dtype)
maxval_nim = np.iinfo(normed_im.dtype).max
#
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment