def view_data(self): try: self.ind_pr_selected = int( self.Table_Project_list.selectedItems()[0].text()) except: msgbox.Message_popup("Warning", "No project Selected", "Please select one Project") else: project_selected = self.Pr_list[self.ind_pr_selected] if project_selected.date_ini != "ND": time_db = project_selected.get_time_db_global( "overview", project_selected.date_ini, project_selected.date_end) ui_viewdata = gui_viewdata.Ui_MainWindow( time_db, project_selected.get_dbnames()) ui_viewdata.setupUi() ui_viewdata.show() while ui_viewdata.finish_window == False: QtCore.QCoreApplication.processEvents() time.sleep(0.05) else: msgbox.Message_popup( "Warning", "Project has no date", "The Project has no uploaded data to get the timespan")
def delete_experiment( self ): #must to display a message to make sure the user wants to delete the selected experiment try: exp_selected = self.treeWidget.selectedIndexes()[-1].data() except: msgbox.Message_popup("Error", "Error", "Please select an Experiment") else: if len(exp_selected.split("/")) < 2 or len( exp_selected.split("/")) > 3: msgbox.Message_popup("Error", "Error", "Please select an Experiment row") else: season_selected = int(exp_selected.split("/")[0]) exp_selected = int(exp_selected.split("/")[1]) yesorno = msgbox.Message_popup( "YesorNo", "Delete Experiment", "Are you sure you want to delete the selected experiment? Note: All data uploaded to this entry will be deleted (not the files)" ) if yesorno.response == "Yes": del self.Pr_list[self.ind_pr_selected].seasons[ season_selected].experiments[exp_selected] self.populate_tree() item = self.treeWidget.topLevelItem(season_selected) self.treeWidget.expandItem(item)
def open_experiment(self): #exp_selected1=self.treeWidget.selectedItems() try: exp_selected = self.treeWidget.selectedIndexes()[-1].data() except: msgbox.Message_popup("Error", "Error", "Please select an Experiment row") else: if len(exp_selected.split("/")) < 2 or len( exp_selected.split("/")) > 3: msgbox.Message_popup("Error", "Error", "Please select an Experiment row") else: season_selected = int(exp_selected.split("/")[0]) exp_selected = int(exp_selected.split("/")[1]) print("opening the open_experiment window") ui_openexperiment = gui_openexperiment.Ui_MainWindow( self.Pr_list, [self.ind_pr_selected, season_selected, exp_selected]) ui_openexperiment.setupUi() ui_openexperiment.show() #print("window openned") while ui_openexperiment.finish_window == False: QtCore.QCoreApplication.processEvents() time.sleep(0.05) self.populate_tree()
def create_experiment(self): print("creating experiment") text_boxes=[self.text_name,self.text_DateStart,self.text_TimeStart,self.text_DateEnd,self.text_TimeEnd,self.text_fuel,self.text_bed,self.text_comments] for tx in text_boxes: if tx.toPlainText()=="": tx.setText(tx.placeholderText()) #time.sleep(5) if self.text_TimeStart.toPlainText()=="" or self.text_TimeEnd.toPlainText()=="": #print("No text was written") msgbox.Message_popup("Error","No Text","No time was given in one of the respective boxes") elif self.text_DateStart.toPlainText()=="" or self.text_DateEnd.toPlainText()=="": msgbox.Message_popup("Error","No Text","No date was given in one of the respective boxes") elif self.text_name.toPlainText()=="" or self.text_bed.toPlainText()=="" or self.text_fuel.toPlainText()=="" or self.text_comments.toPlainText()=="": msgbox.Message_popup("Error","No Text","One text box is empty, please make sure all the boxes have their respective info") else: d_ini=self.text_DateStart.toPlainText()+" "+self.text_TimeStart.toPlainText() d_end=self.text_DateEnd.toPlainText()+" "+self.text_TimeEnd.toPlainText() if len(self.text_TimeStart.toPlainText().split(":"))==2: d_ini+=":00" elif len(self.text_TimeEnd.toPlainText().split(":"))==2: d_ini+=":00" try: d_ini=datetime.strptime(d_ini,"%Y-%m-%d %H:%M:%S") d_end=datetime.strptime(d_end,"%Y-%m-%d %H:%M:%S") except: msgbox.Message_popup("Error","No Text","the date or time has not the right format. Please check: Date: YYYY-MM-DD, time:HH:MM:SS") else: d_ini=d_ini.strftime("%Y-%m-%d %H:%M:%S") d_end=d_end.strftime("%Y-%m-%d %H:%M:%S") self.index_season_selected=self.comboBox_seasons.currentIndex() self.exp_attributes=(self.text_name.toPlainText(),d_ini,d_end, self.text_fuel.toPlainText(),self.text_bed.toPlainText(),self.text_comments.toPlainText()) #add more and order desired print("the text was read") self.cancel_window()
def link_data(self): self.label_status.setText("Status: Linking Data...") if len(self.list_types.selectedIndexes())==0 or len(self.list_types.selectedIndexes())>1: msgbox.Message_popup("Warning","No selection","Please select one data collection from the list") else: # self.label_status.setText("Status: Adding New Data...") collect_data=self.list_types.selectedIndexes()[0].data() if collect_data=="AUTOMATIC": collect_data=self.exp_selected.get_dbnames() else: collect_data=[collect_data] ui_linkdatapoint=gui_linkdatapoint.Ui_MainWindow(collect_data,[self.point_selected.date_ini,self.point_selected.date_end]) ui_linkdatapoint.setupUi() ui_linkdatapoint.show() while ui_linkdatapoint.finish_window==False: QtCore.QCoreApplication.processEvents() time.sleep(0.05) # print(ui_linkdatapoint.linkdata_attrib) if len(ui_linkdatapoint.linkdata_attrib)>0: c_data=ui_linkdatapoint.linkdata_attrib[0] d_ini=ui_linkdatapoint.linkdata_attrib[1] d_end=ui_linkdatapoint.linkdata_attrib[2] delay=ui_linkdatapoint.linkdata_attrib[3] self.point_selected.link_point_data(c_data,"SCADA",d_ini,d_end,delay,self.exp_selected.data_experiment) self.populate_linkeddatatable() msgbox.Message_popup("Info","Data Linked","Data of the experiment was succesfully linked to this point") self.label_status.setText("Status: Ready!")
def load_allprojects(self): # print("{} {}".format(KCbckend.Project.filepath,KCbckend.Project.filename)) try: self.Pr_list = [] # self.Pr_list=backup_projects.load_pr(KCbckend.Project.filepath,KCbckend.Project.filename) with open( "{}\{}.pkl".format(KCbckend.Project.filepath, KCbckend.Project.filename), "rb") as f: self.Pr_list = pickle.load(f) except: msgbox.Message_popup("Error", "Load File error", "Something Ocurred when loading the file") yesorno = msgbox.Message_popup( "YesorNo", "Load Test Projects", "Do you want to create a test project list?") if yesorno.response == "Yes": self.Pr_list = create_test() msgbox.Message_popup("Info", "Load Test List", "Test List Succesfully Created") else: self.Pr_list = [] else: msgbox.Message_popup("Info", "Load Project", "Projects succesfully loaded")
def modify_attrib(self): self.label_status.setText("Status: Modifying Attributes...") d_ini_list=[d_info[0]-d_info[2] for d_type in self.exp_selected.get_dbnames() for d_info in self.point_selected.data_added[d_type]] d_end_list=[d_info[1]-d_info[2] for d_type in self.exp_selected.get_dbnames() for d_info in self.point_selected.data_added[d_type] ] # print(d_ini_list) # print(d_end_list) if self.text_name.isEnabled()==True: d_ini=self.text_DateStart.toPlainText()+" "+self.text_TimeStart.toPlainText() d_end=self.text_DateEnd.toPlainText()+" "+self.text_TimeEnd.toPlainText() if len(self.text_TimeStart.toPlainText().split(":"))==2: d_ini+=":00" if len(self.text_TimeEnd.toPlainText().split(":"))==2: d_end+=":00" try: d_ini=datetime.strptime(d_ini,"%Y-%m-%d %H:%M:%S") d_end=datetime.strptime(d_end,"%Y-%m-%d %H:%M:%S") if d_ini>d_end: raise Exception("overtime","overtime") except Exception as exc: if exc.args[0]=="overtime": msgbox.Message_popup("Error","Dates Error","The Date Start is later than Date End") else: msgbox.Message_popup("Error","Dates Format Error","the date or time has not the right format. Please check: Date: YYYY-MM-DD, time:HH:MM:SS") else: if self.table_DataLinked.rowCount()>0: #if there is any data already loaded to the experiment check that the new dates include within the dates of the data linked d_ini_list=[d_info[0]-d_info[2] for d_type in self.exp_selected.get_dbnames() for d_info in self.point_selected.data_added[d_type]] d_end_list=[d_info[1]-d_info[2] for d_type in self.exp_selected.get_dbnames() for d_info in self.point_selected.data_added[d_type] ] # print("d_ini {},min_d_ini_list {}".format(d_ini,min(d_ini_list))) # print("d_end {},max_d_end_list {}".format(d_end,max(d_end_list))) if d_ini<=min(d_ini_list) and d_end>=max(d_end_list): self.point_selected.date_ini=d_ini.strftime("%Y-%m-%d %H:%M:%S") self.point_selected.date_end=d_end.strftime("%Y-%m-%d %H:%M:%S") else: msgbox.Message_popup("Error","New Dates Error","The span of the new timeframe does not cover the times of the data linked. Please Check, or delete from the table the data that is out of the interval. The times will be reset to their original value") self.text_DateStart.setText(self.point_selected.date_ini.split(" ")[0]) self.text_TimeStart.setText(self.point_selected.date_ini.split(" ")[1]) self.text_DateEnd.setText(self.point_selected.date_end.split(" ")[0]) self.text_TimeEnd.setText(self.point_selected.date_end.split(" ")[1]) else: self.point_selected.date_ini=d_ini.strftime("%Y-%m-%d %H:%M:%S") self.point_selected.date_end=d_end.strftime("%Y-%m-%d %H:%M:%S") self.point_selected.exp_name=self.text_name.toPlainText() self.point_selected.exp_comments=self.text_comments.toPlainText() self.populate_attributes() msgbox.Message_popup("Info","Modify Attributes","Valid attributes were succesfully updated") self.label_status.setText("Status: Ready!") else: for t_box in self.text_boxes: t_box.setEnabled(True)
def delete_project (self): #ind_pr_selected=int(self.Table_Project_list.selectedItems()[0].text()) try: ind_pr_selected=int(self.Table_Project_list.selectedItems()[0].text()) except: msgbox.Message_popup("Error","Error","Please select an Project") else: if ind_pr_selected >-1: # print(f"this is the selected: {ind_pr_selected}") yesorno=msgbox.Message_popup("YesorNo","Delete Project", "Are you sure you want to delete the selected Project? Note: All data uploaded to this entry will be deleted (not the files)") if yesorno.response=="Yes": del self.Pr_list[ind_pr_selected] self.populate_projecttable()
def modify_attrib(self): self.label_status.setText("Status: Modifying Attributes...") if self.text_name.isEnabled()==True: d_ini=self.text_DateStart.toPlainText()+" "+self.text_TimeStart.toPlainText() d_end=self.text_DateEnd.toPlainText()+" "+self.text_TimeEnd.toPlainText() if len(self.text_TimeStart.toPlainText().split(":"))==2: d_ini+=":00" elif len(self.text_TimeEnd.toPlainText().split(":"))==2: d_ini+=":00" try: d_ini=datetime.strptime(d_ini,"%Y-%m-%d %H:%M:%S") d_end=datetime.strptime(d_end,"%Y-%m-%d %H:%M:%S") if d_ini>d_end: raise Exception except: if d_ini>d_end: msgbox.Message_popup("Error","Dates Error","The Date Start is later than Date End") else: msgbox.Message_popup("Error","Dates Format Error","the date or time has not the right format. Please check: Date: YYYY-MM-DD, time:HH:MM:SS") else: if self.tableWidget_db.rowCount()>0: #if there is any data already loaded to the experiment check that the new dates intersects with the data loaded d_ini_list=[datetime.strptime(d_info[1],"%Y-%m-%d %H:%M:%S") for d_type in self.exp_selected.get_dbnames() for d_info in self.data_experiment_info[d_type]] d_end_list=[datetime.strptime(d_info[2],"%Y-%m-%d %H:%M:%S") for d_type in self.exp_selected.get_dbnames() for d_info in self.data_experiment_info[d_type] ] if max(min(d_ini_list),d_ini)<min(max(d_end_list),d_end): self.exp_selected.date_ini=d_ini.strftime("%Y-%m-%d %H:%M:%S") self.exp_selected.date_end=d_end.strftime("%Y-%m-%d %H:%M:%S") else: msgbox.Message_popup("Error","New Dates Error","The new timeframe assigned does not intersect with one of the data loaded for this experiment. Please Check") self.text_DateStart.setText(self.exp_selected.date_ini.split(" ")[0]) self.text_TimeStart.setText(self.exp_selected.date_ini.split(" ")[1]) self.text_DateEnd.setText(self.exp_selected.date_end.split(" ")[0]) self.text_TimeEnd.setText(self.exp_selected.date_end.split(" ")[1]) else: self.exp_selected.date_ini=d_ini.strftime("%Y-%m-%d %H:%M:%S") self.exp_selected.date_end=d_end.strftime("%Y-%m-%d %H:%M:%S") self.exp_selected.exp_name=self.text_name.toPlainText() self.exp_selected.fuel_type=self.text_fuel.toPlainText() self.exp_selected.bed_type=self.text_bed.toPlainText() self.exp_selected.exp_comments=self.text_comments.toPlainText() self.populate_attributes() msgbox.Message_popup("Info","Modify Attributes","Valid attributes have been succesfully updated") self.label_status.setText("Status: Ready...") else: text_boxes=[self.text_name,self.text_DateStart,self.text_TimeStart,self.text_DateEnd,self.text_TimeEnd,self.text_fuel,self.text_bed,self.text_comments] for t_box in text_boxes: t_box.setEnabled(True)
def create_point(self): for i,t_box in enumerate(self.text_boxes): if t_box.toPlainText()=="": t_box.setText(self.default_attributes[i]) d_ini=self.text_DateStart.toPlainText()+" "+self.text_TimeStart.toPlainText() d_end=self.text_DateEnd.toPlainText()+" "+self.text_TimeEnd.toPlainText() # print("1{},{}".format(d_ini,d_end)) if len(self.text_TimeStart.toPlainText().split(":"))==2: d_ini+=":00" if len(self.text_TimeEnd.toPlainText().split(":"))==2: d_end+=":00" try: d_ini=datetime.strptime(d_ini,"%Y-%m-%d %H:%M:%S") d_end=datetime.strptime(d_end,"%Y-%m-%d %H:%M:%S") if d_ini>=d_end or d_ini<datetime.strptime(self.exp_selected.date_ini,"%Y-%m-%d %H:%M:%S") or d_end>datetime.strptime(self.exp_selected.date_end,"%Y-%m-%d %H:%M:%S") : raise Exception("overtime","overtime") except Exception as exc: if exc.args[0]=="overtime": if d_ini>=d_end: msgbox.Message_popup("Error","Error Date","Start date is later than End Date. Check that!") elif d_ini<datetime.strptime(self.exp_selected.date_ini,"%Y-%m-%d %H:%M:%S"): msgbox.Message_popup("Error","Error Date","Start date is earlier than Experiment's Start Date. Check that!") elif d_end>datetime.strptime(self.exp_selected.date_end,"%Y-%m-%d %H:%M:%S"): msgbox.Message_popup("Error","Error Date","End date is later than Experiment's End Date. Check that!") else: msgbox.Message_popup("Error","Error Date","There is an error with the dates. Check them!") else: msgbox.Message_popup("Error","Error Date","the date or the time or the delay has not the right format. Please check: Date: YYYY-MM-DD, time: HH:MM:SS") else: d_ini=d_ini.strftime("%Y-%m-%d %H:%M:%S") d_end=d_end.strftime("%Y-%m-%d %H:%M:%S") print("4{},{}".format(d_ini,d_end)) self.exp_selected.add_Point(self.text_name.toPlainText(),d_ini,d_end,self.text_comments.toPlainText()) #creates the point in the selected experiment self.point_selected=self.exp_selected.points[-1] for item in self.modifypoint_items: item.setEnabled(True) time.sleep(0.1) self.populate_attributes() #self.Button_ModifyInfoPoint.setText("MODIFY INFO") self.Button_CreatePoint.setEnabled(False) #self.populate_linkeddatatable() self.label_status.setText("Status: Point Created!") print("Point Created")
def new_season(self): ui_newseason = gui_newseason.Ui_MainWindow() ui_newseason.setupUi() ui_newseason.show() while ui_newseason.finish_window == False: QtCore.QCoreApplication.processEvents() time.sleep(0.05) if ui_newseason.season_attributes != "": # print("N. seasons of project selected:{}".format(len(self.project_selected.seasons))) (season_name, season_description) = ui_newseason.season_attributes self.project_selected.add_Season(season_name, season_description) # print("new season created. N. seasons of project selected:{}".format(len(self.project_selected.seasons))) self.comboBox_seasons.addItem("") self.comboBox_seasons.setItemText( len(self.project_selected.seasons) - 1, self.project_selected.seasons[-1].season_name) self.comboBox_seasons.setCurrentIndex( len(self.project_selected.seasons) - 1) self.write_default_attrib(len(self.project_selected.seasons) - 1) else: msgbox.Message_popup( "Error", "Season Error", "Season cannot be created because neither the season name nor its description was given, please check!" )
def delete_data(self): try: ind_data_selected = int( self.table_DataLinked.selectedItems()[0].text()) # print(ind_data_selected) except: msgbox.Message_popup("Error", "Error", "Please select an entry from table") else: # print(f"this is the selected: {ind_data_selected}") yesorno = msgbox.Message_popup( "YesorNo", "Unlink Data", "Are you sure you want to unlink the selected data Note: All data uploaded to this entry will be deleted (not the files)" ) if yesorno.response == "Yes": d_type = self.table_info[ind_data_selected][1] d_type_ind = self.table_info[ind_data_selected][-1] del self.point_selected.data_added[d_type][d_type_ind] self.populate_linkeddatatable()
def new_experiment(self): ind_season_selected = self.treeWidget.selectedIndexes()[-1].data( ).split("/")[0] n_seasons0 = len(self.project_selected.seasons ) #total number fo seasons of the selected project print(f"season_selected:{ind_season_selected}") default_attributes = "" ui_newexperiment = gui_newexperiment.Ui_MainWindow( self.project_selected, default_attributes, ind_season_selected) ui_newexperiment.setupUi() ui_newexperiment.show() while ui_newexperiment.finish_window == False: QtCore.QCoreApplication.processEvents() time.sleep(0.05) print("add experiment window closed") ind_season_selected = ui_newexperiment.index_season_selected #int(ind_season_selected) #print(ind_season_selected) n_exp0 = len( self.project_selected.seasons[ind_season_selected].experiments) # print(f"{n_seasons0}, season:{ind_season_selected}, exp:{n_exp0}") if ui_newexperiment.exp_attributes != "": (exp_name, d_ini, d_end, fuel_type, bed_type, exp_comments) = ui_newexperiment.exp_attributes # print("creating the experiment object. attributes:",ui_newexperiment.exp_attributes) self.project_selected.seasons[ind_season_selected].add_Experiment( exp_name, d_ini, d_end, fuel_type, bed_type, exp_comments) if len(self.project_selected.seasons) > n_seasons0: item = QtWidgets.QTreeWidgetItem(self.treeWidget) if len(self.project_selected.seasons[ind_season_selected]. experiments) > n_exp0: item_child = QtWidgets.QTreeWidgetItem( self.treeWidget.topLevelItem(ind_season_selected)) self.populate_tree() print("tree populated") else: msgbox.Message_popup( "Error", "Error", "The Experiment attributes were not read. Please check and add again the info" )
def new_season(self): ui_newseason=gui_newseason.Ui_MainWindow() ui_newseason.setupUi() ui_newseason.show() while ui_newseason.finish_window==False: QtCore.QCoreApplication.processEvents() time.sleep(0.05) if ui_newseason.season_attributes!="": # print("N. seasons of project selected:{}".format(len(self.project_selected.seasons))) (season_name,season_description)=ui_newseason.season_attributes self.project_selected.add_Season(season_name,season_description) # print("new season created. N. seasons of project selected:{}".format(len(self.project_selected.seasons))) item=QtWidgets.QTreeWidgetItem(self.treeWidget) #creates the high level item in the tree #self.treeWidget.addTopLevelItem(item) self.populate_tree() # print("tree populated!") else: msgbox.Message_popup("Error","Season Error","Season cannot be created because neither the season name nor its description was given, please check!")
def open_project(self): try: self.ind_pr_selected=int(self.Table_Project_list.selectedItems()[0].text()) #print(f"this is the selected: {p_selected}") except: msgbox.Message_popup("Error","No project","Please select a Project") else: ui_openproject=gui_openproject.Ui_MainWindow(self.Pr_list,self.ind_pr_selected) ui_openproject.setupUi() #introduces the selected project into the ui_open project to open the respective project ui_openproject.show() while ui_openproject.finish_window==False:# and ui_openproject.isVisible()==True: QtCore.QCoreApplication.processEvents() time.sleep(0.05) if ui_openproject.finish_window==True: print("window OpenProject is closed") self.populate_projecttable()
def link_point_data(self, collect_data, time_type, date_ini, date_end, delay, db_experiment): #,name_timecolumn): #delete data_type #collect_data (list) = list with the type of data to be introduced SCADA,GC1,Inferno,SPA #time_type (str)= SCADA or GC #date_ini (str) = initial date of the point #date_end (str) = end date of the point #delay (str) = delay in minutes respect the SCADA time (SCADA -> delay=0 minutes) #db_experiment = dictionary with all the data added to the experiment given by the attribute data_experiment of the Experiment class #this method defines the attributes of the point created and add the data to the time_db date_ini = datetime.strptime(date_ini, "%Y-%m-%d %H:%M:%S") date_end = datetime.strptime(date_end, "%Y-%m-%d %H:%M:%S") # delay=float(delay) # self.delay=delay date_i = date_ini date_e = date_end #self.point_route=point_route#it is a string with the indexes of Project/Season/experiment/point delay_db = {k: [0, 0, 0] for k in ["SCADA", "GC1", "INFERNO", "SPA"] } #all delays are set as zero # if time_type=="GC":#if the time is the time of the GC file then the SCADA must to be read at time-delay from scada file. (however the point date will be always the scada time) # delay_db["SCADA"]=-delay #it is negative because the SCADA is the real time # date_i=date_i-delay #the point date will be always the scada time # date_e=date_e-delay #the point date will be always the scada time # elif time_type=="SCADA":#if the time is the one of the SCADA file then the GC and inferno must to be read at time+delay # delay_db["GC1"]=delay_db["INFERNO"]=delay_db["SPA"]=delay # date_i=date_i # date_e=date_e #always will be SCADA if len(collect_data) > 1: for k in collect_data: if k == "SCADA": delay_db[k] = [0, 0, 0] else: delay_db[k] = [int(i) for i in delay.split(":")] else: k = collect_data[0] if k == "SCADA": delay_db[k] = [0, 0, 0] else: delay_db[k] = [int(i) for i in delay.split(":")] # date_i=date_i #these variables are maybe not necesary because all will be respect of SCADA. however to avoid further debugging these two extra variables will be left as they are # date_e=date_e #minutes0=date_ini.minute self.time_db_pnt = { fd: [] for fd in Point.time_db_fields } #initializing the directory time_db for the evaluated point (this later will be added to the global time_db directory) #time_dp_pnt will be transformed into a pandas dataframe #key="DATE" => creates a list of rounded time (floor minute) from date_ini to date_end #date_i=date_i while date_i <= date_e: t_rounded = datetime( date_i.year, date_i.month, date_i.day, date_i.hour, date_i.minute, 00 ) #time floor-rounded to the minute (this is in order to create the point time slot list) self.time_db_pnt["DATE"].append(t_rounded) self.time_db_pnt["POINT_ROUTE"].append(self.point_route) date_i += timedelta(minutes=1) # if collect_data=="AUTOMATIC": #the time taken by the point is the SCADA time #collect data from all databases Nentries = {k: 0 for k in Experiment.db_names} for k in collect_data: print(k) #if k in self.data_point.keys(): #the data from the database k has already added to the point # continue if len(db_experiment[k]) == 0: #pop up a message saying that the database k is missing msgbox.Message_popup( "Error", "Missed Database", f"the database {k} is missing, please add it to the experiment" ) continue if k in [ "SCADA", "GC1", "INFERNO" ]: #this must to be taken from a function (to generalize for the case when data_type!=automatic) d_t0, d_t1 = None, None Nentries[k] = 0 #search which of the list elements added to the db_experiment[k] has the time interval it is being looking for for db in db_experiment[ k]: #db_experiment[k] is a dictionary with a list per each key (when more that one SCADA or GC has been added to the experiment) d_t0 = db[Experiment.name_timecolumn[ k]] >= date_ini + timedelta(hours=delay_db[k][0], minutes=delay_db[k][1], seconds=delay_db[k][2]) d_t1 = db[Experiment.name_timecolumn[ k]] <= date_end + timedelta(hours=delay_db[k][0], minutes=delay_db[k][1], seconds=delay_db[k][2]) if all(d_t0 == False) and all( d_t1 == False ): #means that the timeslot defined is not on the evaluated data entry of the experiment (an experiment can have several GC´s or SCADA´s) msgbox.Message_popup( "Warning", "time error", f"the times defined are not within the database {k}, please add the data within the timeframe or check the time intervals defined" ) continue #extracts the row dataframes from the different databases to create self.time_db_pnt[database] for t_i in self.time_db_pnt["DATE"]: t0 = db[Experiment.name_timecolumn[ k]] >= t_i + timedelta(hours=delay_db[k][0], minutes=delay_db[k][1], seconds=delay_db[k][2]) t1 = db[ Experiment.name_timecolumn[k]] <= t_i + timedelta( hours=delay_db[k][0], minutes=delay_db[k][1], seconds=delay_db[k][2]) + timedelta( seconds=59.999) self.time_db_pnt[k].append( db[t0 & t1] ) #in this way each entry in the list will correspond with a time in the row of the pnadas dataframe if len( self.time_db_pnt[k][-1] ) > 0: #each element of the list corresponds with one minute (if there is data of the data_type k at that minute then Nentries+=1) Nentries[k] += 1 elif k == "SPA": Nentries[k] = 0 #number of entries of the database k for t_i in self.time_db_pnt[ "DATE"]: #goes for all the dates that are stored in time_db_pnt (each date will be at each row of the pandas dataframe) t0 = t_i + timedelta(hours=delay_db[k][0], minutes=delay_db[k][1], seconds=delay_db[k][2]) t1 = t_i + timedelta( hours=delay_db[k][0], minutes=delay_db[k][1], seconds=delay_db[k][2]) + timedelta(seconds=59.999) #self.data_point[k]={} fv = 0 #number of entries found G_PX0 = "G_None" #temp=time_db_pnt[k] SPA_samples = [] for db in db_experiment[ k]: #considering that several SPA files were added in that experiment #When two SPA's are in series both share the same time, therefore two dataframes must to be in the db_experiment["SPA"] dictionary for a specific key (since the key is the time) for t_spa, v_list in db.items(): time_SPA = datetime.strptime( t_spa, "%Y-%m-%d %H:%M:%S" ) #gets the time from the keys of the SPA data (check method get_data_fromfile) if t0 <= time_SPA <= t1: SPA_samples.append( [[t_spa, v[0], v[1], v[2]] for v in v_list] ) #collect the different F/C matrices for the evaluated time #each SPA file has its data grouped into certain times, then once the time is found it must to jump to the other SPA file added break # maybe not <- the loop must continue because it must allow the case when 2 SPA syringes are used (both are marked at the same hour) #at the end, all the list fields should be joined together into just one list (not a list of lists as it is right know (one list for each SPA file)) #msgbox.Message_popup("Info","SPA Added",f"It has been added {len(SPA_samples)} samples at time t_i={t_i} of the SPA taken at t_spa={t_spa}") self.time_db_pnt[k].append(SPA_samples) if len(SPA_samples) > 0: Nentries[k] += 1 #else: #as the times t_spa are sorted within the db_experiment["SPA"] it must to be found the time t_spa within the interval [t0,t1] #print(f"there is no data within the interval {str(t0)} and {str(t1)}\n in any of the {len(db_experiment[k])} databases added in the experiment.\n Please check the SPA dates") # msgbox.Message_popup("Error","time error", # f"there is no data within the interval {str(t0)} and {str(t1)}\n in any of the {len(db_experiment[k])} databases added in the experiment.\n Please check the SPA dates") #continue #else: # tk.message.showinfo("No SPA Added","No data of SPA was added at time t_i=. Please check the SPA dates") #Add the log of the database added to show it in the table of the databases added in the Add_point window #data_added[k]=[database date_ini,database date_end,delay,Nentries] # delaystr=[str(elem) if len(str(elem))>1 else "0"+str(elem) for elem in delay_db[k]] # delaystr=":".join(delaystr) #turns the list of delay_db[k] into a single string witht he forma HH:MM:SS index0_time_db_global = len( Point.time_db_global_overview.index ) #initial index where this data will be saved in the time_db_global (the whole data is extracted based on this index0 and the Nentries of the SCADA) self.data_added[k].append([ date_ini + timedelta(hours=delay_db[k][0], minutes=delay_db[k][1], seconds=delay_db[k][2]), date_end + timedelta(hours=delay_db[k][0], minutes=delay_db[k][1], seconds=delay_db[k][2]), timedelta(hours=delay_db[k][0], minutes=delay_db[k][1], seconds=delay_db[k][2]), Nentries[k], index0_time_db_global ]) for k, v in self.time_db_pnt.items(): if len(v) == 0: self.time_db_pnt[k] = ["" for i in self.time_db_pnt["DATE"]] #transform the time_db_pnt in a dictionary where the keys correspond with the date time_dp_pnt_dict = {} for k_i, k_time in enumerate( self.time_db_pnt["DATE"] ): #ktime is already a datetime object (great!) time_dp_pnt_dict[k_time] = [ v1[k_i] for k1, v1 in self.time_db_pnt.items() ] #if k1!="DATE" #create the dataframe with only yes or no (for instance: if SCADA exists then it shows 1 if not 0, for SPA in future can be written 2 in the case of serial SPAs (or maybe not snce all the SPA is read at once)) self.time_db_pnt_overview = {} for k, v in self.time_db_pnt.items(): self.time_db_pnt_overview[k] = v if k in Experiment.db_names: for v1_i, v1 in enumerate(v): #go for the different elements: a = 0 if len(v1) > 0: a = 1 self.time_db_pnt_overview[k][v1_i] = a self.time_db_pnt_overview = pd.DataFrame.from_dict( self.time_db_pnt_overview) self.time_db_pnt = time_dp_pnt_dict self.update_db_global(self)
def get_data_fromfile( self, data_type, filename ): #, dates=None): #it should be added a list "dates" for the cases where there are several measurement sets (e.g. GC) if data_type == "SCADA": ExcelTable = pd.read_excel(filename, sheet_name="_DATA") name_timecolumn = Experiment.name_timecolumn[data_type] ExcelTable[name_timecolumn] = pd.to_datetime( ExcelTable[name_timecolumn]) d_t0 = ExcelTable[ name_timecolumn] >= self.date_ini ## d_t0=ExcelTable["Acquisition Date & Time"].dt.strftime("%H:%M")>t0 (when t0 is in HH:MM format) d_t1 = ExcelTable[name_timecolumn] <= self.date_end Table_timeinterval = ExcelTable[d_t0 & d_t1] # if len(Table_timeinterval.index)==0: #check if the dataframe has any values on it (all depends on the dates provided) # Table_timeinterval="" return Table_timeinterval elif data_type == "GC1" or data_type == "INFERNO": #this must to be checkd because it could exist several measurements sets within the experiment time ExcelTable = pd.read_excel(filename) name_timecolumn = Experiment.name_timecolumn[data_type] ExcelTable[name_timecolumn] = pd.to_datetime( ExcelTable[name_timecolumn]) d_t0 = ExcelTable[ name_timecolumn] >= self.date_ini #dates[0] #It must to be read the whole file (this is why it is important tha the experiment dates be wider enough) d_t1 = ExcelTable[name_timecolumn] <= self.date_end #dates[1] Table_timeinterval = ExcelTable[d_t0 & d_t1] # if len(Table_timeinterval.index)==0: # Table_timeinterval="" return Table_timeinterval elif data_type == "SPA": #this must to be checked because it could exist several measurements sets within the experiment time #app=QtWidgets.QApplication(sys.argv) spa_win = guiSPA.Ui_MainWindow() spa_win.setupUi() spa_win.read_file(filename) spa_win.MainWindow.show( ) #displays a window where the different sheets are assigned to the different times when the sample was collected while spa_win.finish_window == False: QtCore.QCoreApplication.processEvents() time.sleep(0.01) try: sheets_dates = spa_win.sh_dates except: msgbox.Message_popup("Error", "Error reading SPA table", "The sheets were not read from SPA file") Table_timeinterval = {} return Table_timeinterval #sheets_dates is a dictionary where the key is the time => dict["YYY-MM-DD HH:MM:SS"]. the keys must be sorted by the time #the value is a list. list[0]=GPX, list[1]=FR/CR, list[2]=sheet_name of R. (R is the repetition of the GC, X is the spa sample number, P is the point) else: #read the excel tables from the respective files print("done with the SPA file!") Table_timeinterval = {} for tm, v_list in sheets_dates.items(): #print("V[2]={}".format(v1[2])) Table_timeinterval[tm] = [[ v[0], v[1], pd.read_excel(filename, sheet_name=v[2]) ] for v in v_list] print("SPA directory created") # #sys.exit(app.exec_()) return Table_timeinterval
def add_data(self, data_type, filename, delay, comments=""): #data_type (str) = type of data to be introduced SCADA,GC1,Inferno,SPA #filename (str)= this is the route of the file where the information will be extracted out (user defined through a explorer window) #delay(str) = delay time respect of the SCADA time (format: HH:MM:SS) #The data can be read from a predefined excel sheet format #the different data should be stored in a dictionary (with the data_type string as the key) #each experiment can have several time intervals (to check) data_addedtolist = False if data_type == "SCADA": self.data_experiment[data_type].append( self.get_data_fromfile(data_type, filename) ) #it must to search all the files which are within the dates given (maybe not) elif data_type == "GC1" or data_type == "INFERNO": #The time interval of the GC corresponds with the whole time registered in the file self.data_experiment[data_type].append( self.get_data_fromfile(data_type, filename)) #maybe this one elif data_type == "SPA": self.data_experiment[data_type].append( self.get_data_fromfile(data_type, filename)) if data_type in ["SCADA", "GC1", "INFERNO"]: if len(self.data_experiment[data_type][-1].index ) > 0: #check if the last dataframe added has data on it data_addedtolist = True elif data_type in ["SPA"]: if len(self.data_experiment[data_type][-1].items() ) > 0: #check if the last dataframe added has data on it data_addedtolist = True if data_addedtolist: #check if the last dataframe added has data on it d_min, d_max = self.get_dates_db( data_type, -1 ) #-1 because always must to take the last db loaded for a given data_type if len(self.data_experiment_info[data_type]) == 0: self.data_experiment_info[data_type].append([ data_type + "_" + str(len(self.data_experiment[data_type]) - 1), d_min, d_max, delay, comments ]) else: d_min_list = [ datetime.strptime(d_info[1], "%Y-%m-%d %H:%M:%S") for d_info in self.data_experiment_info[data_type] ] d_max_list = [ datetime.strptime(d_info[2], "%Y-%m-%d %H:%M:%S") for d_info in self.data_experiment_info[data_type] ] if datetime.strptime(d_min, "%Y-%m-%d %H:%M:%S") >= min( d_min_list) or datetime.strptime( d_max, "%Y-%m-%d %H:%M:%S") <= max(d_max_list): yesorno = msgbox.Message_popup( "YesorNo", "Time intervals overlapped", "The time interval of the new data is overlapping with one of the databases already uploaded. keep it anyway?" ) if yesorno.response == "Yes": self.data_experiment_info[data_type].append([ data_type + "_" + str(len(self.data_experiment[data_type]) - 1), d_min, d_max, delay, comments ]) else: del self.data_experiment[data_type][-1] msgbox.Message_popup( "Warning", "Data overlapped", "Data times overlapped. Check the file and upload it again" ) else: del self.data_experiment[data_type][ -1] #deletes the last element because the times dont interesect msgbox.Message_popup( "Error", "Time interval error", "The time interval of the experiment does not intersect with the time interval of the data in the file selected. Please check the times" )
def add_data(self): #needs to be created a list to pick up the database type if len(self.list_types.selectedIndexes())==0 or len(self.list_types.selectedIndexes())>1: msgbox.Message_popup("Warning","No Data","Please select one data type from the list") else: self.label_status.setText("Status: Adding New Data...") data_type=self.list_types.selectedIndexes()[0].data() N_dbloaded_0=len(self.exp_selected.data_experiment_info[data_type]) #Number of databases of the data_type already loaded print(N_dbloaded_0) if data_type=="SCADA": ui_addscada=gui_addscada.Ui_MainWindow(len(self.exp_selected.data_experiment[data_type])) ui_addscada.setupUi() ui_addscada.show() while ui_addscada.finish_window==False: QtCore.QCoreApplication.processEvents() time.sleep(0.05) if ui_addscada.datafile_info!="" and ui_addscada.text_filepath!="": try: self.exp_selected.add_data(data_type,ui_addscada.datafile_info[0],ui_addscada.datafile_info[1],ui_addscada.datafile_info[2]) N_dbloaded=len(self.exp_selected.data_experiment[data_type]) print(N_dbloaded) if N_dbloaded>N_dbloaded_0: d_ini_file=datetime.strptime(self.exp_selected.data_experiment_info[data_type][N_dbloaded-1][1], "%Y-%m-%d %H:%M:%S") d_end_file=datetime.strptime(self.exp_selected.data_experiment_info[data_type][N_dbloaded-1][2], "%Y-%m-%d %H:%M:%S") d_ini_exp=datetime.strptime(self.exp_selected.date_ini, "%Y-%m-%d %H:%M:%S") d_end_exp=datetime.strptime(self.exp_selected.date_end, "%Y-%m-%d %H:%M:%S") if max(d_ini_file,d_ini_exp)<min(d_end_file,d_end_exp): #checks if the timesets of the file and the experiment intersect r=self.tableWidget_db.rowCount() self.tableWidget_db.insertRow(r) for c in range(self.tableWidget_db.columnCount()): r=self.tableWidget_db.rowCount() item=QtWidgets.QTableWidgetItem() self.tableWidget_db.setItem(r-1, c, item) item=self.tableWidget_db.item(r-1,c) item.setText(self.exp_selected.data_experiment_info[data_type][N_dbloaded-1][c]) msgbox.Message_popup("Info","Data Added", "Data succesfully added") else: del self.exp_selected.data_experiment[data_type][N_dbloaded-1] #deletes the last database added for the data_type evaluated (SCADA,GC1,etc..) del self.exp_selected.data_experiment_info[data_type][N_dbloaded-1] #deletes the last database added for the data_type evaluated (SCADA,GC1,etc..) msgbox.Message_popup("Error","Dates error", "The timeframe of the selected file does not interesect with the one of the experiment, please check the times and upload again the file") except: msgbox.Message_popup("Error","Data Error", "An error ocurred while getting the data from the file. Please check the dates and/or the file and upload it again") elif data_type=="GC1" or data_type=="INFERNO": ui_addgc=gui_addgc.Ui_MainWindow(len(self.exp_selected.data_experiment[data_type])) ui_addgc.setupUi() ui_addgc.show() while ui_addgc.finish_window==False: QtCore.QCoreApplication.processEvents() time.sleep(0.05) if ui_addgc.datafile_info!="" and ui_addgc.text_filepath!="": try: self.exp_selected.add_data(data_type,ui_addgc.datafile_info[0],ui_addgc.datafile_info[1],ui_addgc.datafile_info[2]) N_dbloaded=len(self.exp_selected.data_experiment[data_type]) if N_dbloaded>N_dbloaded_0: d_ini_file=datetime.strptime(self.exp_selected.data_experiment_info[data_type][N_dbloaded-1][1], "%Y-%m-%d %H:%M:%S") d_end_file=datetime.strptime(self.exp_selected.data_experiment_info[data_type][N_dbloaded-1][2], "%Y-%m-%d %H:%M:%S") d_ini_exp=datetime.strptime(self.exp_selected.date_ini, "%Y-%m-%d %H:%M:%S") d_end_exp=datetime.strptime(self.exp_selected.date_end, "%Y-%m-%d %H:%M:%S") if max(d_ini_file,d_ini_exp)<min(d_end_file,d_end_exp): #checks if the timesets of the file and the experiment intersect r=self.tableWidget_db.rowCount() self.tableWidget_db.insertRow(r) for c in range(self.tableWidget_db.columnCount()): r=self.tableWidget_db.rowCount() item=QtWidgets.QTableWidgetItem() self.tableWidget_db.setItem(r-1, c, item) item=self.tableWidget_db.item(r-1,c) item.setText(self.exp_selected.data_experiment_info[data_type][N_dbloaded-1][c]) msgbox.Message_popup("Info","Data Added", "Data succesfully added") else: del self.exp_selected.data_experiment[data_type][N_dbloaded-1] #deletes the last database added for the data_type evaluated (SCADA,GC1,etc..) del self.exp_selected.data_experiment_info[data_type][N_dbloaded-1] #deletes the last database added for the data_type evaluated (SCADA,GC1,etc..) msgbox.Message_popup("Error","Dates error", "The timeframe of the selected file does not interesect with the one of the experiment, please check the times and upload again the file") except: msgbox.Message_popup("Error","Data Error", "An error ocurred while getting the data from the file. Please check the dates and/or the file and upload it again") elif data_type=="SPA": ui_addspa=gui_addspa.Ui_MainWindow(len(self.exp_selected.data_experiment[data_type])) ui_addspa.setupUi() ui_addspa.show() while ui_addspa.finish_window==False: QtCore.QCoreApplication.processEvents() time.sleep(0.05) if ui_addspa.datafile_info!="" and ui_addspa.text_filepath!="": try: self.exp_selected.add_data(data_type,ui_addspa.datafile_info[0],ui_addspa.datafile_info[1],ui_addspa.datafile_info[2]) N_dbloaded=len(self.exp_selected.data_experiment[data_type]) if N_dbloaded>N_dbloaded_0: d_ini_file=datetime.strptime(self.exp_selected.data_experiment_info[data_type][N_dbloaded-1][1], "%Y-%m-%d %H:%M:%S") d_end_file=datetime.strptime(self.exp_selected.data_experiment_info[data_type][N_dbloaded-1][2], "%Y-%m-%d %H:%M:%S") d_ini_exp=datetime.strptime(self.exp_selected.date_ini, "%Y-%m-%d %H:%M:%S") d_end_exp=datetime.strptime(self.exp_selected.date_end, "%Y-%m-%d %H:%M:%S") if max(d_ini_file,d_ini_exp)<min(d_end_file,d_end_exp): #checks if the timesets of the file and the experiment intersect r=self.tableWidget_db.rowCount() self.tableWidget_db.insertRow(r) for c in range(self.tableWidget_db.columnCount()): r=self.tableWidget_db.rowCount() item=QtWidgets.QTableWidgetItem() self.tableWidget_db.setItem(r-1, c, item) item=self.tableWidget_db.item(r-1,c) item.setText(self.exp_selected.data_experiment_info[data_type][N_dbloaded-1][c]) msgbox.Message_popup("Info","Data Added", "Data succesfully added") else: del self.exp_selected.data_experiment[data_type][N_dbloaded-1] #deletes the last database added for the data_type evaluated (SCADA,GC1,etc..) del self.exp_selected.data_experiment_info[data_type][N_dbloaded-1] #deletes the last database added for the data_type evaluated (SCADA,GC1,etc..) msgbox.Message_popup("Error","Dates error", "The timeframe of the selected file does not intersect with the one of the experiment, please check the times and upload again the file") except: msgbox.Message_popup("Error","Data Error", "An error ocurred while getting the data from the file. Please check the dates and/or the file and upload it again") self.label_dataloaded.setText("Data Loaded: {}".format(self.tableWidget_db.rowCount())) self.label_status.setText("Status: Ready!")