def main_analyzer(datafilepath: str, foldername: str='./testplots', header: int=None, time_format: str='%m/%d/%y %I:%M:%S %p CST', unit_name: str='kW'): """ This function reads the data and put plots in the specified directory. Inputs: ========== datafilepath: string path to the data file folder_path: str directory where the diagrams are saved header: int, list of ints, default None Row (0-indexed) to use for the column labels of the parsed DataFrame. If a list of integers is passed those row positions will be combined into a MultiIndex. Default None time_format: string format of string in time. Default '%m/%d/%y %I:%M:%S %p CST' Please check https://docs.python.org/3.5/library/datetime.html#strftime-and-strptime-behavior for details unit_name: string unit of the data. Default 'kW' """ pddf = read_data(datafilepath, header) dfhour_profile_plot(pddf, foldername, col_name='CLG', y_label=''.join([ 'Instantaneous building cooling load [', unit_name, ']' ]), showfliers=True, diagram_types=['png']) histogram_plot(pddf, foldername, col_name='CLG', xlabel_name='Building Cooling Load During Operating Hours', add_xlabel=''.join([' [', unit_name, ']']), diagram_types=['png'])
return result # testing functions if __name__ == '__main__': from os.path import basename from os import remove from data_read import read_data from pandas import read_csv, read_excel, Timestamp, ExcelFile # check to estimate step function correctly when the required time # interval is larger than the time interval between the data points FILENAME = '../dat/time_of_change.csv' TEST_DFS = read_data(FILENAME, header=0) NEW_DFS = convert_df(TEST_DFS, datetime(2017, 1, 1, 8, 0), interval=60 * 30, ini_val=3) assert NEW_DFS['time_of_change'].loc[datetime(2017, 1, 1, 10, 0), 'Item 3'] == 0 assert NEW_DFS['time_of_change'].loc[datetime(2017, 1, 1, 10, 30), 'Item 3'] == 0 assert NEW_DFS['time_of_change'].loc[datetime(2017, 1, 1, 12, 30), 'Item 3'] == 1 assert NEW_DFS['time_of_change'].loc[NEW_DFS['time_of_change'].index[-1], 'Item 3'] == 0 # check for assuming nan values for data before the first valid value FILENAME = '../dat/time_of_change.csv'
''.join([ folder_path, '/', load_type, '-load-profile-', col_name, '-', '%04i' % yr, '-', '%02i' % mn ]), diagram_types) # test functions if __name__ == '__main__': from pathlib import Path import shutil from data_read import read_data # testing the dfhour_profile_plot. Can it plot? PDDF = read_data('../dat/load.csv', header=None) dfhour_profile_plot(PDDF, '../testplots', col_name='CLG', y_label='Instantaneous building cooling load [kW]', showfliers=True, diagram_types=['png']) assert Path('../testplots/wkdy-load-profile-CLG-2015-01.png').exists() assert Path('../testplots/wkdy-load-profile-CLG-2016-01.png').exists() assert not Path('../testplots/wkdy-load-profile-CLG-2014-01.png').exists() print('All functions in', os.path.basename(__file__), 'are ok') print('Please delete plots in ../testplots/ upon completing inspection')
def Analyzer(self, evt): """ Function to initiate the main analysis. """ # check all required inputs # check the existence of the folder if not isfile(self.dfpath.GetValue()): wx.MessageBox( u'Cannot open the data file!', u'Error', wx.OK | wx.ICON_INFORMATION ) return # check the existence of the saving path if not Path(dirname(self.newdfpath.GetValue())).exists(): box = wx.MessageDialog( self, u'Saving directory does not exist!', u'Error', wx.OK | wx.ICON_INFORMATION ) box.Fit() box.ShowModal() return # check file type ext = get_ext(self.newdfpath.GetValue()) if not (ext == 'csv' or ext == 'xls' or ext == 'xlsx'): box = wx.MessageDialog( self, u'Output file type not supported!', u'Error', wx.OK | wx.ICON_INFORMATION ) box.Fit() box.ShowModal() return # check the time start_time = datetime( int(self.start_yr.GetValue()), int(self.start_mon.GetValue()), int(self.start_day.GetValue()), int(self.start_hr.GetValue()), int(self.start_min.GetValue()) ) end_time = datetime( int(self.end_yr.GetValue()), int(self.end_mon.GetValue()), int(self.end_day.GetValue()), int(self.end_hr.GetValue()), int(self.end_min.GetValue()) ) if not self.no_endtime.GetValue() and start_time > end_time: wx.MessageBox( u'Starting time later than ending time!', u'Error', wx.OK | wx.ICON_INFORMATION ) return # Run the analyzer # output any error to a message box if needed try: header_exist = self.header.GetValue() datadfs = read_data( self.dfpath.GetValue(), header=(self.header_no.GetValue() if header_exist else None), time_format=self.timestring.GetValue(), sheetnames=( [] if self.loadallsheets.GetValue() else ( None if get_ext(self.dfpath.GetValue()) == 'csv' else [self.sheetname.GetValue()] ) ), dateautodetect=self.autotimeinputformat.GetValue() ) # return error if load all sheet option is selected for csv file # output if get_ext(self.newdfpath.GetValue()) == 'csv' and \ self.loadallsheets.GetValue() and \ len(datadfs) > 1: wx.MessageBox( u'\n'.join([ u'Cannot output multiple worksheets to a csv file!', u'Please output it as a xls or xlsx file!' ]), u'Error', wx.OK | wx.ICON_INFORMATION ) return # show warning for columns that contain no valid data for sheet_name in datadfs: datadf = datadfs[sheet_name] for col in datadf.columns: if all([ isinstance(x, str) or isnan(x) for x in datadf.loc[:, col] ]): dlg = MessageDlg(''.join([ 'Column ', col, ' in ', sheet_name, ' does not contain any valid values.', ' Closing in 2s......' ]), u'Warning') wx.CallLater(2000, dlg.Destroy) dlg.ShowModal() convert_df( datadfs, (None if self.use_starttime.GetValue() else start_time), (None if self.no_endtime.GetValue() else end_time), interval=int(self.time_int.GetValue())*60, step=(True if self.func_choice.GetSelection() == 0 else False), ini_val=self.early_pts.GetSelection()+1, output_file=self.newdfpath.GetValue(), sep=self.output_sep.GetValue(), output_timestring=self.outputtimestring.GetValue(), outputtimevalue=self.numtimeoutput.GetValue() ) except BaseException: # box = wx.MessageDialog( # self, format_exc(), u'Error', wx.OK | wx.ICON_INFORMATION # ) chgdep = ErrorReportingDialog(None) chgdep.ShowModal() chgdep.Destroy() return # function to be called upon finishing processing wx.CallLater(0, self.ShowMessage) evt.Skip()