Skip to content
Snippets Groups Projects
Commit f0b982cb authored by Stefano Di Frischia's avatar Stefano Di Frischia
Browse files

L2SS-528: bugs and typos fixing

parent 0f5dd404
No related branches found
No related tags found
1 merge request!193Resolve L2SS-528 "Timescaledb defaults"
...@@ -11,7 +11,7 @@ from sqlalchemy import create_engine, and_ ...@@ -11,7 +11,7 @@ from sqlalchemy import create_engine, and_
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.exc import NoResultFound
import importlib import importlib
import numpy as np import numpy
logger = logging.getLogger() logger = logging.getLogger()
...@@ -40,7 +40,7 @@ def parse_device_name(device_name:str, tango_host:str = 'databaseds:10000'): ...@@ -40,7 +40,7 @@ def parse_device_name(device_name:str, tango_host:str = 'databaseds:10000'):
elif (chunk_num==6 and device_name.split('/')[0]=='tango:'): elif (chunk_num==6 and device_name.split('/')[0]=='tango:'):
return device_name return device_name
else: else:
raise Exception(f'{device_name} is a wrong device name') raise ValueError(f'{device_name} is a wrong device name')
class Archiver(): class Archiver():
...@@ -67,7 +67,7 @@ class Archiver(): ...@@ -67,7 +67,7 @@ class Archiver():
try: try:
self.apply_selector() self.apply_selector()
except Exception as e: except Exception as e:
raise Exception("Error in selecting configuration! Archiving framework will not be updated!") from e raise Exception("Error in selecting configuration. Archiving framework will not be updated.") from e
def get_db_config(self, device_name:str): def get_db_config(self, device_name:str):
""" """
...@@ -320,7 +320,7 @@ class Archiver(): ...@@ -320,7 +320,7 @@ class Archiver():
if len(attributes)==2 and (attributes[0].endswith(attributes[1]+'w') or attributes[1].endswith(attributes[0]+'w')): if len(attributes)==2 and (attributes[0].endswith(attributes[1]+'w') or attributes[1].endswith(attributes[0]+'w')):
return True return True
else: else:
raise Exception(f"Multiple Attributes Matched! {attributes}") raise Exception(f"Multiple Attributes Matched: {attributes}")
elif len(attributes)==1: elif len(attributes)==1:
return True return True
else: else:
...@@ -461,7 +461,7 @@ class Selector(): ...@@ -461,7 +461,7 @@ class Selector():
data = json.load(f) data = json.load(f)
f.close() f.close()
except FileNotFoundError as e: except FileNotFoundError as e:
raise Exception("JSON configuration file not found!") from e raise
return data return data
class Retriever(): class Retriever():
...@@ -501,7 +501,7 @@ class Retriever(): ...@@ -501,7 +501,7 @@ class Retriever():
libname = 'postgresql+psycopg2' libname = 'postgresql+psycopg2'
dbms = 'postgres' dbms = 'postgres'
else: else:
raise Exception(f"Invalid hostname! {host}") raise ValueError(f"Invalid hostname: {host}")
engine = create_engine(libname+'://'+user+':'+pw+'@'+host+':'+port+'/'+dbname) engine = create_engine(libname+'://'+user+':'+pw+'@'+host+':'+port+'/'+dbname)
Session = sessionmaker(bind=engine) Session = sessionmaker(bind=engine)
return Session(),dbms return Session(),dbms
...@@ -529,7 +529,7 @@ class Retriever(): ...@@ -529,7 +529,7 @@ class Retriever():
Takes as input the fully-qualified name of a device and returns a list of its archived attributes Takes as input the fully-qualified name of a device and returns a list of its archived attributes
""" """
try: try:
[domain, family, member] = device_fqname.split('/') domain, family, member = device_fqname.split('/')
except: except:
raise AttributeFormatException(f"Could not parse device name {device_fqname}. Please provide FQDN, e.g. STAT/Device/1") raise AttributeFormatException(f"Could not parse device name {device_fqname}. Please provide FQDN, e.g. STAT/Device/1")
attrs = self.session.query(self.ab.Attribute).filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, \ attrs = self.session.query(self.ab.Attribute).filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, \
...@@ -542,7 +542,7 @@ class Retriever(): ...@@ -542,7 +542,7 @@ class Retriever():
Takes as input the fully-qualified name of an attribute and returns its id. Takes as input the fully-qualified name of an attribute and returns its id.
""" """
try: try:
[domain, family, member, name] = attribute_fqname.split('/') domain, family, member, name = attribute_fqname.split('/')
except: except:
raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute")
try: try:
...@@ -550,7 +550,7 @@ class Retriever(): ...@@ -550,7 +550,7 @@ class Retriever():
self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one() self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one()
return result[0] return result[0]
except TypeError as e: except TypeError as e:
raise Exception("Attribute not found!") from e raise Exception(f"Attribute {attribute_fqname} not found!") from e
except NoResultFound as e: except NoResultFound as e:
raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e
...@@ -561,7 +561,7 @@ class Retriever(): ...@@ -561,7 +561,7 @@ class Retriever():
as DB table name suffix in which values are stored. as DB table name suffix in which values are stored.
""" """
try: try:
[domain, family, member, name] = attribute_fqname.split('/') domain, family, member, name = attribute_fqname.split('/')
except: except:
raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute")
try: try:
...@@ -573,7 +573,7 @@ class Retriever(): ...@@ -573,7 +573,7 @@ class Retriever():
filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one() filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one()
return result[0] return result[0]
except TypeError as e: except TypeError as e:
raise Exception("Attribute not found!") from e raise Exception(f"Attribute not {attribute_fqname} found!") from e
except NoResultFound as e: except NoResultFound as e:
raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e
...@@ -584,7 +584,7 @@ class Retriever(): ...@@ -584,7 +584,7 @@ class Retriever():
* Works only for POSTGRESQL * * Works only for POSTGRESQL *
""" """
try: try:
[domain, family, member, name] = attribute_fqname.split('/') domain, family, member, name = attribute_fqname.split('/')
except: except:
raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute")
try: try:
...@@ -620,7 +620,7 @@ class Retriever(): ...@@ -620,7 +620,7 @@ class Retriever():
attr_datatype = self.get_attribute_datatype(attribute_fqname) attr_datatype = self.get_attribute_datatype(attribute_fqname)
# Retrieves the class that maps the DB table given the tablename # Retrieves the class that maps the DB table given the tablename
if self.dbms=='mysql': if self.dbms=='mysql':
tablename = 'att_'+str(attr_datatype) tablename = f"att_{attr_datatype}"
elif self.dbms=='postgres': elif self.dbms=='postgres':
tablename = self.get_attribute_tablename(attribute_fqname) tablename = self.get_attribute_tablename(attribute_fqname)
base_class = self.ab.get_class_by_tablename(tablename) base_class = self.ab.get_class_by_tablename(tablename)
...@@ -636,7 +636,7 @@ class Retriever(): ...@@ -636,7 +636,7 @@ class Retriever():
filter(and_(self.ab.Attribute.att_conf_id == attr_id,base_class.data_time >= time_delta_db, \ filter(and_(self.ab.Attribute.att_conf_id == attr_id,base_class.data_time >= time_delta_db, \
base_class.data_time <= time_now_db)).order_by(base_class.data_time).all() base_class.data_time <= time_now_db)).order_by(base_class.data_time).all()
except AttributeError as e: except AttributeError as e:
raise Exception(f"Empty result! Attribute {attribute_fqname} not found") from e raise Exception(f"Empty result: Attribute {attribute_fqname} not found") from e
return result return result
def get_attribute_value_by_interval(self,attribute_fqname: str, start_time: datetime, stop_time: datetime): def get_attribute_value_by_interval(self,attribute_fqname: str, start_time: datetime, stop_time: datetime):
...@@ -647,10 +647,9 @@ class Retriever(): ...@@ -647,10 +647,9 @@ class Retriever():
""" """
attr_id = self.get_attribute_id(attribute_fqname) attr_id = self.get_attribute_id(attribute_fqname)
attr_datatype = self.get_attribute_datatype(attribute_fqname) attr_datatype = self.get_attribute_datatype(attribute_fqname)
attr_table_name = 'att_'+str(attr_datatype)
# Retrieves the class that maps the DB table given the tablename # Retrieves the class that maps the DB table given the tablename
if self.dbms=='mysql': if self.dbms=='mysql':
tablename = 'att_'+str(attr_datatype) tablename = f"att_{attr_datatype}"
elif self.dbms=='postgres': elif self.dbms=='postgres':
tablename = self.get_attribute_tablename(attribute_fqname) tablename = self.get_attribute_tablename(attribute_fqname)
base_class = self.ab.get_class_by_tablename(tablename) base_class = self.ab.get_class_by_tablename(tablename)
...@@ -660,7 +659,7 @@ class Retriever(): ...@@ -660,7 +659,7 @@ class Retriever():
filter(and_(self.ab.Attribute.att_conf_id == attr_id,base_class.data_time >= str(start_time), \ filter(and_(self.ab.Attribute.att_conf_id == attr_id,base_class.data_time >= str(start_time), \
base_class.data_time <= str(stop_time))).order_by(base_class.data_time).all() base_class.data_time <= str(stop_time))).order_by(base_class.data_time).all()
except AttributeError as e: except AttributeError as e:
raise Exception(f"Empty result! Attribute {attribute_fqname} not found") from e raise Exception(f"Empty result: Attribute {attribute_fqname} not found") from e
return result return result
def get_masked_fpga_temp(self,start_time: datetime, stop_time: datetime,temp_attr_name:str='stat/sdp/1/fpga_temp_r', def get_masked_fpga_temp(self,start_time: datetime, stop_time: datetime,temp_attr_name:str='stat/sdp/1/fpga_temp_r',
...@@ -690,5 +689,5 @@ class Retriever(): ...@@ -690,5 +689,5 @@ class Retriever():
temp_array_values = self.ab.get_values_from_record(temp_data) temp_array_values = self.ab.get_values_from_record(temp_data)
# Multiply the matrix # Multiply the matrix
#masked_values = np.multiply(temp_array_values,mask_array_values) #masked_values = np.multiply(temp_array_values,mask_array_values)
masked_values = np.ma.masked_array(temp_array_values,mask=np.invert(mask_array_values.astype(bool))) masked_values = numpy.ma.masked_array(temp_array_values,mask=numpy.invert(mask_array_values.astype(bool)))
return masked_values, mask_values, temp_values return masked_values, mask_values, temp_values
...@@ -7,7 +7,7 @@ from sqlalchemy.dialects.mysql import DOUBLE,TIMESTAMP,BLOB, FLOAT, BIGINT ...@@ -7,7 +7,7 @@ from sqlalchemy.dialects.mysql import DOUBLE,TIMESTAMP,BLOB, FLOAT, BIGINT
from sqlalchemy.sql.expression import table from sqlalchemy.sql.expression import table
from typing import List from typing import List
from itertools import groupby from itertools import groupby
import numpy as np import numpy
#Declarative system used to define classes mapped to relational DB tables #Declarative system used to define classes mapped to relational DB tables
Base = declarative_base() Base = declarative_base()
...@@ -905,14 +905,14 @@ def build_array_from_record(rows: List[Array], dim_x: int): ...@@ -905,14 +905,14 @@ def build_array_from_record(rows: List[Array], dim_x: int):
""" """
Converts Array database items in Python lists Converts Array database items in Python lists
""" """
matrix = np.array([]) matrix = numpy.array([])
for i in range(0,dim_x): for i in range(0,dim_x):
x = np.array([item for item in rows if item.idx==i]) #group records by array index x = numpy.array([item for item in rows if item.idx==i]) #group records by array index
if i==0: if i==0:
matrix = np.append(matrix,x) #append first row matrix = numpy.append(matrix,x) #append first row
else: else:
matrix = np.vstack([matrix,x]) #stack vertically matrix = numpy.vstack([matrix,x]) #stack vertically
result = np.transpose(matrix) #transpose -> each row is a distinct array of value result = numpy.transpose(matrix) #transpose -> each row is a distinct array of value
list_result = result.tolist() list_result = result.tolist()
return list_result return list_result
...@@ -920,8 +920,8 @@ def get_values_from_record(data_matrix: List[Array]): ...@@ -920,8 +920,8 @@ def get_values_from_record(data_matrix: List[Array]):
""" """
Returns a matrix of values from a matrix of Array records Returns a matrix of values from a matrix of Array records
""" """
array_matrix = np.matrix(data_matrix) array_matrix = numpy.matrix(data_matrix)
value_matrix = np.empty(array_matrix.shape) value_matrix = numpy.empty(array_matrix.shape)
for index in range(array_matrix.size): # for each object element for index in range(array_matrix.size): # for each object element
value_matrix.itemset(index,array_matrix.item(index).value_r) # extract the value from object and put in the matrix value_matrix.itemset(index,array_matrix.item(index).value_r) # extract the value from object and put in the matrix
return value_matrix return value_matrix
......
...@@ -9,7 +9,7 @@ from sqlalchemy import Column, Integer, String ...@@ -9,7 +9,7 @@ from sqlalchemy import Column, Integer, String
from sqlalchemy.sql.expression import table from sqlalchemy.sql.expression import table
from typing import List from typing import List
from itertools import groupby from itertools import groupby
import numpy as np import numpy
#Declarative system used to define classes mapped to relational DB tables #Declarative system used to define classes mapped to relational DB tables
Base = declarative_base() Base = declarative_base()
...@@ -450,14 +450,14 @@ def build_array_from_record(rows: List[Array], dim_x: int): ...@@ -450,14 +450,14 @@ def build_array_from_record(rows: List[Array], dim_x: int):
""" """
Converts Array database items in Python lists Converts Array database items in Python lists
""" """
matrix = np.array([]) matrix = numpy.array([])
for i in range(0,dim_x): for i in range(0,dim_x):
x = np.array([item for item in rows if item.idx==i]) #group records by array index x = numpy.array([item for item in rows if item.idx==i]) #group records by array index
if i==0: if i==0:
matrix = np.append(matrix,x) #append first row matrix = numpy.append(matrix,x) #append first row
else: else:
matrix = np.vstack([matrix,x]) #stack vertically matrix = numpy.vstack([matrix,x]) #stack vertically
result = np.transpose(matrix) #transpose -> each row is a distinct array of value result = numpy.transpose(matrix) #transpose -> each row is a distinct array of value
list_result = result.tolist() list_result = result.tolist()
return list_result return list_result
...@@ -465,8 +465,8 @@ def get_values_from_record(data_matrix: List[Array]): ...@@ -465,8 +465,8 @@ def get_values_from_record(data_matrix: List[Array]):
""" """
Returns a matrix of values from a matrix of Array records Returns a matrix of values from a matrix of Array records
""" """
array_matrix = np.matrix(data_matrix) array_matrix = numpy.matrix(data_matrix)
value_matrix = np.empty(array_matrix.shape) value_matrix = numpy.empty(array_matrix.shape)
for index in range(array_matrix.size): # for each object element for index in range(array_matrix.size): # for each object element
value_matrix.itemset(index,array_matrix.item(index).value_r) # extract the value from object and put in the matrix value_matrix.itemset(index,array_matrix.item(index).value_r) # extract the value from object and put in the matrix
return value_matrix return value_matrix
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment