def test_getDailyVoltDataByLevel(self) -> None: """tests the function that gets hourly data of entity metric """ appDbConnStr = self.jsonConf['appDbConnStr'] mRepo = MetricsDataRepo(appDbConnStr) startDt = dt.datetime(2020, 1, 1) endDt = dt.datetime(2020, 1, 10) samples = mRepo.getDailyVoltDataByLevel( 765, "%Time >420 or 800", startDt, endDt) self.assertFalse(len(samples) == 0)
def fetchSection1_7_2Context(appDbConnStr: str, startDt: dt.datetime, endDt: dt.datetime) -> ISection_1_7_2: mRepo = MetricsDataRepo(appDbConnStr) # get voltage data for this month maxVoltData = mRepo.getDailyVoltDataByLevel(400, "Max", startDt, endDt) maxVoltDf = pd.DataFrame(maxVoltData) maxVoltDf["data_val"] = pd.to_numeric( maxVoltDf["data_val"], errors='coerce') maxVoltSeries = maxVoltDf.groupby("entity_name").apply(getMax) maxVoltSeries = maxVoltSeries.round() maxVoltSeries = maxVoltSeries.rename("max_vol") minVoltData = mRepo.getDailyVoltDataByLevel(400, "Min", startDt, endDt) minVoltDf = pd.DataFrame(minVoltData) minVoltDf["data_val"] = pd.to_numeric( minVoltDf["data_val"], errors='coerce') minVoltSeries = minVoltDf.groupby("entity_name").apply(getMin) minVoltSeries = minVoltSeries.round() minVoltSeries = minVoltSeries.rename("min_vol") lessVoltPercData = mRepo.getDailyVoltDataByLevel( 400, "%Time <380 or 728", startDt, endDt) lessVoltPercDf = pd.DataFrame(lessVoltPercData) lessVoltPercDf["data_val"] = pd.to_numeric( lessVoltPercDf["data_val"], errors='coerce') lessVoltPercSeries = lessVoltPercDf.groupby("entity_name").apply(getMean) lessVoltPercSeries = lessVoltPercSeries.round(2) lessVoltPercSeries = lessVoltPercSeries.rename("less_perc") bandVoltPercData = mRepo.getDailyVoltDataByLevel( 400, "%Time within IEGC Band", startDt, endDt) bandVoltPercDf = pd.DataFrame(bandVoltPercData) bandVoltPercDf["data_val"] = pd.to_numeric( bandVoltPercDf["data_val"], errors='coerce') bandVoltPercSeries = bandVoltPercDf.groupby("entity_name").apply(getMean) bandVoltPercSeries = bandVoltPercSeries.round(2) bandVoltPercSeries = bandVoltPercSeries.rename("band_perc") moreVoltPercData = mRepo.getDailyVoltDataByLevel( 400, "%Time >420 or 800", startDt, endDt) moreVoltPercDf = pd.DataFrame(moreVoltPercData) moreVoltPercDf["data_val"] = pd.to_numeric( moreVoltPercDf["data_val"], errors='coerce') moreVoltPercSeries = moreVoltPercDf.groupby("entity_name").apply(getMean) moreVoltPercSeries = moreVoltPercSeries.round(2) moreVoltPercSeries = moreVoltPercSeries.rename("more_perc") numMonthHrs = (endDt - startDt).total_seconds()/(60*60) secDf = pd.concat([maxVoltSeries, minVoltSeries, lessVoltPercSeries, bandVoltPercSeries, moreVoltPercSeries], axis=1) secDf['less_hrs'] = secDf['less_perc']*(numMonthHrs*0.01) secDf['more_hrs'] = secDf['more_perc']*(numMonthHrs*0.01) secDf['out_hrs'] = secDf['less_hrs'] + secDf['more_hrs'] secDf['vdi'] = secDf['out_hrs']*(1/numMonthHrs) secDf['less_hrs'] = secDf['less_hrs'].apply(hrsToDurationStr) secDf['more_hrs'] = secDf['more_hrs'].apply(hrsToDurationStr) secDf['out_hrs'] = secDf['out_hrs'].apply(hrsToDurationStr) secDf['vdi'] = secDf['vdi'].apply(hrsToDurationStr) secDf.reset_index(inplace=True) secDf['entity_name'] = secDf['entity_name'].apply(strip400) secDf.rename(columns={"entity_name": "station"}, inplace=True) secDataRows = secDf.to_dict('records') sectionData: ISection_1_7_2 = { 'voltVdiProfile400': secDataRows } return sectionData
def fetchSection1_1_voltContext(appDbConnStr: str, startDt: dt.datetime, endDt: dt.datetime) -> ISection_1_1_volt: monthDtObj = dt.datetime(startDt.year, startDt.month, 1) month_name = dt.datetime.strftime(startDt, "%b %y") mRepo = MetricsDataRepo(appDbConnStr) # get high voltage violation data for 765 kV voltData765 = mRepo.getDailyVoltDataByLevel( 765, "%Time >420 or 800", startDt, endDt) # convert to dataframe voltData765Df = pd.DataFrame(voltData765) # take only required columns voltData765Df = voltData765Df[["entity_name", "data_val"]] # convert column to numeric voltData765Df['data_val'] = pd.to_numeric( voltData765Df['data_val'], errors='coerce') # get mean for each substation voltData765Df = voltData765Df.groupby("entity_name").mean() voltData765Df.reset_index(inplace=True) # check if there is violation more than 0.1 is765MoreThan10Perc = voltData765Df[voltData765Df["data_val"] > 10].shape[0] > 0 msg765 = "" if not is765MoreThan10Perc: msgStations = voltData765Df[voltData765Df["data_val"] > 0]["entity_name"].values msgStations = [x.replace(' - 765KV', '').capitalize() for x in msgStations] msg765 = "All 765 kV nodes of WR were within the IEGC limit." if len(msgStations) > 0: msg765 = "All 765 kV nodes of WR were within the IEGC limit except few instances at {0}.".format( ','.join(msgStations)) else: msgStations = voltData765Df[voltData765Df["data_val"] > 10]["entity_name"].values msgStations = [x.replace(' - 765KV', '').capitalize() for x in msgStations] highViolSubstation = voltData765Df.loc[voltData765Df['data_val'].idxmax( )] msg765 = "High Voltage (greater than 800 kV) at 765 kV substations were observed at {0}. Highest of {1}{2} of time voltage remained above 780 kV at {3} in the month of {4}.".format( ', '.join(msgStations), round(highViolSubstation["data_val"], 2), "%", highViolSubstation["entity_name"].replace(' - 765KV', '').capitalize(), month_name) # get high voltage violation data for 400 kV voltData400 = mRepo.getDailyVoltDataByLevel( 400, "%Time >420 or 800", startDt, endDt) # convert to dataframe voltData400Df = pd.DataFrame(voltData400) # take only required columns voltData400Df = voltData400Df[["entity_name", "data_val"]] # convert column to numeric voltData400Df['data_val'] = pd.to_numeric( voltData400Df['data_val'], errors='coerce') # get mean for each substation voltData400Df = voltData400Df.groupby("entity_name").mean() voltData400Df.reset_index(inplace=True) # check if there is violation more than 0.1 is400MoreThan10Perc = voltData400Df[voltData400Df["data_val"] > 10].shape[0] > 0 msg400 = "" if not is400MoreThan10Perc: msgStations = voltData400Df[voltData400Df["data_val"] > 0]["entity_name"].values msgStations = [x.replace(' - 400KV', '').capitalize() for x in msgStations] msg400 = "All 400 kV nodes of WR were within the IEGC limit." if len(msgStations) > 0: msg400 = "All 400 kV nodes of WR were within the IEGC limit except few instances at {0}.".format( ','.join(msgStations)) else: msgStations = voltData400Df[voltData400Df["data_val"] > 10]["entity_name"].values msgStations = [x.replace(' - 400KV', '').capitalize() for x in msgStations] highViolSubstation = voltData400Df.loc[voltData400Df['data_val'].idxmax( )] msg400 = "High Voltage (greater than 420 kV) at 400 kV substations were observed at {0}. Highest of {1}{2} of time voltage remained above 420 kV at {3} in the month of {4}.".format( ', '.join(msgStations), round(highViolSubstation["data_val"], 2), "%", highViolSubstation["entity_name"].replace(' - 400KV', '').capitalize(), month_name) msg = " ".join([msg765, msg400]) secData: ISection_1_1_volt = { "msg_1_1_volt": msg } return secData
def fetchSection1_7Context(appDbConnStr: str, startDt: dt.datetime, endDt: dt.datetime) -> ISection_1_7: metricsInfo = getVoltMetrics() mRepo = MetricsDataRepo(appDbConnStr) allMetris = {} allMetris['400'] = {} allMetris['765'] = {} allStations = [] for metricIndex in range(len(metricsInfo)): metricInfo = metricsInfo[metricIndex] voltLevel = metricInfo['voltageLevel'] operation = metricInfo['operation'] metricName = metricInfo['metric_name'] if operation != 'compute ': allEntityMetricData = mRepo.getDailyVoltDataByLevel( voltLevel, metricName, startDt, endDt) allEntityMetricDf = pd.DataFrame(allEntityMetricData) allEntityMetricDf = allEntityMetricDf.groupby("entity_name") allStations = allEntityMetricDf.groups.keys() combinedObj = {} for eachStation in allStations: combinedObj[eachStation] = [] for eachRecord in allEntityMetricData: if math.isnan(float(eachRecord['data_val'])): eachRecord['data_val'] = 0 combinedObj[eachRecord['entity_name']].append( float(eachRecord['data_val'])) stndWiseOperationData = {} for eachStation in allStations: stnData = combinedObj[eachStation] val = 0 if operation == 'max': val = max(stnData) elif operation == 'sum': val = sum(stnData) / ((endDt - startDt).days + 1) # val = sum(stnData) elif operation == 'min': val = min(stnData) stndWiseOperationData[eachStation] = val allMetris[str(voltLevel)][metricName] = stndWiseOperationData else: refColumnName = metricInfo['evalColumn'] multiplyFactor = metricInfo['multiply'] allMetris[metricName] = {} stndWiseOperationData = {} for eachStation in allStations: val = allMetris[str(voltLevel)][refColumnName][eachStation] if multiplyFactor == 'monthHrs': difference = endDt - startDt days = difference.days + 1 factor = days * 24 stndWiseOperationData[eachStation] = int(val) * factor elif multiplyFactor == 'weekHrs': factor = 24 * 7 stndWiseOperationData[eachStation] = int(val) / factor allMetris[str(voltLevel)][metricName] = stndWiseOperationData print(allMetris) df400 = pd.DataFrame(allMetris['400']) df765 = pd.DataFrame(allMetris['765']) df = df765.append(df400, ignore_index=True) allMetricRecords400KV: List[IVoltageRecord] = [] allMetricRecords765V: List[IVoltageRecord] = [] allMetricRecords400KV = builtIRecord(df400) allMetricRecords765V = builtIRecord(df765) secData: ISection_1_7 = { 'voltageLevel400KV': allMetricRecords400KV, 'voltageLevel765KV': allMetricRecords765V } return secData
def fetchSection1_7_3Context(appDbConnStr: str, startDt: dt.datetime, endDt: dt.datetime) -> ISection_1_7_3: fullMonthName = dt.datetime.strftime(startDt, "%b' %Y") mRepo = MetricsDataRepo(appDbConnStr) metrics = ["Max", "Min"] lvls = [400, 765] numPltsPerPage = 6 numPages = 0 for m in metrics: for l in lvls: voltData = mRepo.getDailyVoltDataByLevel(l, m, startDt, endDt) voltDf = pd.DataFrame(voltData) voltDf["data_val"] = pd.to_numeric(voltDf["data_val"], errors='coerce') voltDf = voltDf.pivot(index="data_time", columns="entity_name", values="data_val") if l == 400: voltDf.columns = [strip400(x) for x in voltDf.columns] elif l == 765: voltDf.columns = [strip765(x) for x in voltDf.columns] lowThresh = 300 if l == 400 else 650 voltDf[voltDf < lowThresh] = np.nan numStations = voltDf.shape[1] pageStartStnInd = 0 while pageStartStnInd < numStations: pageEndStnInd = pageStartStnInd + numPltsPerPage if pageEndStnInd >= numStations: pageEndStnInd = numStations - 1 # create a plotting area and get the figure, axes handle in return fig, ax = plt.subplots(figsize=(7.5, 4.5)) pltTitle = "{0}. Voltage Profile during the month of {1} - {2} kV S/S".format( m, fullMonthName, l) # set plot title ax.set_title(pltTitle) # set x and y labels ax.set_xlabel('Date') ax.set_ylabel('kV') #c0c0c0 ax.set_facecolor("#c0c0c0") # enable y axis grid lines ax.yaxis.grid(True) for stnIter in range(pageStartStnInd, pageEndStnInd + 1): stnName = voltDf.columns[stnIter] la, = ax.plot(voltDf.index.values, voltDf[stnName].values, linestyle='solid', marker='.') la.set_label(stnName) # set x axis locator as day of month ax.set_xlim((startDt, endDt), auto=True) ax.xaxis.set_major_locator(mdates.DayLocator()) # set x axis formatter as month name ax.xaxis.set_major_formatter(mdates.DateFormatter('%d')) # enable legends ax.legend(bbox_to_anchor=(0.0, -0.3, 1, 0), loc='lower center', ncol=numPltsPerPage, mode="expand", borderaxespad=0.) fig.subplots_adjust(bottom=0.2, left=0.07, right=0.99) fig.savefig('assets/section_1_7_3_{0}.png'.format(numPages)) numPages += 1 pageStartStnInd = pageEndStnInd + 1 sectionData: ISection_1_7_3 = {'num_plts_sec_1_7_3': numPages} return sectionData