diff --git a/tangostationcontrol/tangostationcontrol/toolkit/archiver.py b/tangostationcontrol/tangostationcontrol/toolkit/archiver.py index 0de191499b9700c725885b645faa1a339c4d3bc6..3dc170b52e180a5ffc1d5354dc34169cad920f51 100644 --- a/tangostationcontrol/tangostationcontrol/toolkit/archiver.py +++ b/tangostationcontrol/tangostationcontrol/toolkit/archiver.py @@ -11,7 +11,7 @@ from sqlalchemy import create_engine, and_ from sqlalchemy.orm import sessionmaker from sqlalchemy.orm.exc import NoResultFound import importlib -import numpy as np +import numpy logger = logging.getLogger() @@ -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:'): return device_name else: - raise Exception(f'{device_name} is a wrong device name') + raise ValueError(f'{device_name} is a wrong device name') class Archiver(): @@ -67,7 +67,7 @@ class Archiver(): try: self.apply_selector() 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): """ @@ -320,7 +320,7 @@ class Archiver(): if len(attributes)==2 and (attributes[0].endswith(attributes[1]+'w') or attributes[1].endswith(attributes[0]+'w')): return True else: - raise Exception(f"Multiple Attributes Matched! {attributes}") + raise Exception(f"Multiple Attributes Matched: {attributes}") elif len(attributes)==1: return True else: @@ -461,7 +461,7 @@ class Selector(): data = json.load(f) f.close() except FileNotFoundError as e: - raise Exception("JSON configuration file not found!") from e + raise return data class Retriever(): @@ -501,7 +501,7 @@ class Retriever(): libname = 'postgresql+psycopg2' dbms = 'postgres' else: - raise Exception(f"Invalid hostname! {host}") + raise ValueError(f"Invalid hostname: {host}") engine = create_engine(libname+'://'+user+':'+pw+'@'+host+':'+port+'/'+dbname) Session = sessionmaker(bind=engine) return Session(),dbms @@ -529,7 +529,7 @@ class Retriever(): Takes as input the fully-qualified name of a device and returns a list of its archived attributes """ try: - [domain, family, member] = device_fqname.split('/') + domain, family, member = device_fqname.split('/') except: 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, \ @@ -542,7 +542,7 @@ class Retriever(): Takes as input the fully-qualified name of an attribute and returns its id. """ try: - [domain, family, member, name] = attribute_fqname.split('/') + domain, family, member, name = attribute_fqname.split('/') except: raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") try: @@ -550,7 +550,7 @@ class Retriever(): self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one() return result[0] 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: raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e @@ -561,7 +561,7 @@ class Retriever(): as DB table name suffix in which values are stored. """ try: - [domain, family, member, name] = attribute_fqname.split('/') + domain, family, member, name = attribute_fqname.split('/') except: raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") try: @@ -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() return result[0] 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: raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e @@ -584,7 +584,7 @@ class Retriever(): * Works only for POSTGRESQL * """ try: - [domain, family, member, name] = attribute_fqname.split('/') + domain, family, member, name = attribute_fqname.split('/') except: raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") try: @@ -620,7 +620,7 @@ class Retriever(): attr_datatype = self.get_attribute_datatype(attribute_fqname) # Retrieves the class that maps the DB table given the tablename if self.dbms=='mysql': - tablename = 'att_'+str(attr_datatype) + tablename = f"att_{attr_datatype}" elif self.dbms=='postgres': tablename = self.get_attribute_tablename(attribute_fqname) base_class = self.ab.get_class_by_tablename(tablename) @@ -636,7 +636,7 @@ class Retriever(): 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() 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 def get_attribute_value_by_interval(self,attribute_fqname: str, start_time: datetime, stop_time: datetime): @@ -647,10 +647,9 @@ class Retriever(): """ attr_id = self.get_attribute_id(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 if self.dbms=='mysql': - tablename = 'att_'+str(attr_datatype) + tablename = f"att_{attr_datatype}" elif self.dbms=='postgres': tablename = self.get_attribute_tablename(attribute_fqname) base_class = self.ab.get_class_by_tablename(tablename) @@ -660,7 +659,7 @@ class Retriever(): 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() 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 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(): temp_array_values = self.ab.get_values_from_record(temp_data) # Multiply the matrix #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 diff --git a/tangostationcontrol/tangostationcontrol/toolkit/archiver_base_mysql.py b/tangostationcontrol/tangostationcontrol/toolkit/archiver_base_mysql.py index 0e85ad5ec377536d2e50f4323e60ca9ddb9d76e1..4224b2349e7554d51a0a918fc22f70a01c022cdf 100644 --- a/tangostationcontrol/tangostationcontrol/toolkit/archiver_base_mysql.py +++ b/tangostationcontrol/tangostationcontrol/toolkit/archiver_base_mysql.py @@ -7,7 +7,7 @@ from sqlalchemy.dialects.mysql import DOUBLE,TIMESTAMP,BLOB, FLOAT, BIGINT from sqlalchemy.sql.expression import table from typing import List from itertools import groupby -import numpy as np +import numpy #Declarative system used to define classes mapped to relational DB tables Base = declarative_base() @@ -905,14 +905,14 @@ def build_array_from_record(rows: List[Array], dim_x: int): """ Converts Array database items in Python lists """ - matrix = np.array([]) + matrix = numpy.array([]) 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: - matrix = np.append(matrix,x) #append first row + matrix = numpy.append(matrix,x) #append first row else: - matrix = np.vstack([matrix,x]) #stack vertically - result = np.transpose(matrix) #transpose -> each row is a distinct array of value + matrix = numpy.vstack([matrix,x]) #stack vertically + result = numpy.transpose(matrix) #transpose -> each row is a distinct array of value list_result = result.tolist() return list_result @@ -920,8 +920,8 @@ def get_values_from_record(data_matrix: List[Array]): """ Returns a matrix of values from a matrix of Array records """ - array_matrix = np.matrix(data_matrix) - value_matrix = np.empty(array_matrix.shape) + array_matrix = numpy.matrix(data_matrix) + value_matrix = numpy.empty(array_matrix.shape) 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 return value_matrix diff --git a/tangostationcontrol/tangostationcontrol/toolkit/archiver_base_ts.py b/tangostationcontrol/tangostationcontrol/toolkit/archiver_base_ts.py index e86473df231e1e39604c154ea4883392a3866a08..f73ca2abc9a27497bfa3c90603b9a29663d9be2b 100644 --- a/tangostationcontrol/tangostationcontrol/toolkit/archiver_base_ts.py +++ b/tangostationcontrol/tangostationcontrol/toolkit/archiver_base_ts.py @@ -9,7 +9,7 @@ from sqlalchemy import Column, Integer, String from sqlalchemy.sql.expression import table from typing import List from itertools import groupby -import numpy as np +import numpy #Declarative system used to define classes mapped to relational DB tables Base = declarative_base() @@ -450,14 +450,14 @@ def build_array_from_record(rows: List[Array], dim_x: int): """ Converts Array database items in Python lists """ - matrix = np.array([]) + matrix = numpy.array([]) 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: - matrix = np.append(matrix,x) #append first row + matrix = numpy.append(matrix,x) #append first row else: - matrix = np.vstack([matrix,x]) #stack vertically - result = np.transpose(matrix) #transpose -> each row is a distinct array of value + matrix = numpy.vstack([matrix,x]) #stack vertically + result = numpy.transpose(matrix) #transpose -> each row is a distinct array of value list_result = result.tolist() return list_result @@ -465,8 +465,8 @@ def get_values_from_record(data_matrix: List[Array]): """ Returns a matrix of values from a matrix of Array records """ - array_matrix = np.matrix(data_matrix) - value_matrix = np.empty(array_matrix.shape) + array_matrix = numpy.matrix(data_matrix) + value_matrix = numpy.empty(array_matrix.shape) 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 return value_matrix