private void PopulateWthNormalParams() { List <WthNormalParams> newNormalParamsList = new List <WthNormalParams>(); List <WthNormalParams> modelsWithNotTwelveReadings = new List <WthNormalParams>(); List <WthNormalParams> modelsWithReadingOrWeatherIssues = new List <WthNormalParams>(); List <WthNormalParams> modelsWithOtherIssues = new List <WthNormalParams>(); Log.Information("Starting PopulateWthNormalParams(). Searching WNRdngData01 stored procedure... "); List <WNRdngData> allWNRdngData = _weatherRepository.GetAllReadingsFromStoredProcedure(); var wNRdngDataGroups = allWNRdngData.GroupBy(s => new { s.AccID, s.UtilID, s.UnitID }); //List<WNRdngData> heyy = wNRdngDataGroups.Contains(s => new { }) int updateCount = 0; int insertCount = 0; int failCount = 0; foreach (var wNRdngGroup in wNRdngDataGroups) { List <WNRdngData> wNRdngList = wNRdngGroup.OrderBy(s => s.MoID).ToList(); WNRdngData lastRead = wNRdngList.LastOrDefault(); WthNormalParams normalParams = new WthNormalParams() { AccID = lastRead.AccID, UtilID = lastRead.UtilID, UnitID = lastRead.UnitID, WstID = lastRead.WstID, ZipW = lastRead.Zip, EndDate = lastRead.DateEnd, EMoID = lastRead.EMoID, MoCt = lastRead.MoCt }; if (wNRdngList.Count < 6) { Log.Error("There are only {0} readings for AccID/UtilID/UnitID: {1}/{2}/{3}. No model found.", wNRdngList.Count, normalParams.AccID, normalParams.UtilID, normalParams.UnitID); continue; } if (wNRdngList.Count % 12 != 0) { modelsWithNotTwelveReadings.Add(normalParams); Log.Warning($"wNRdngList is not a multiple of 12 .. {wNRdngList.Count} Readings.\"MoCt\": {normalParams.MoCt}, still proceeding..."); } bool normalParamsExists = _weatherRepository.GetWthNormalParamsExists(normalParams); try { List <BalancePointPair> allBalancePointStatsFromYear = CalculateOneYearOfDegreeDaysForAllBalancePoints(wNRdngList); //int daysInYear = allBalancePointStatsFromYear.FirstOrDefault().DaysInYear; //normalParams.DaysInYear = daysInYear; bool success = false; if (allBalancePointStatsFromYear.Count == 0) { Log.Error($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID} >> " + $"Why are there no BalancePointStatsFromYear?"); if (normalParamsExists) { success = UpdateOrInsertWthNormalParams(normalParams); if (success) { newNormalParamsList.Add(normalParams); updateCount++; } else { failCount++; } } else { success = UpdateOrInsertWthNormalParams(normalParams); if (success) { newNormalParamsList.Add(normalParams); insertCount++; } else { failCount++; } } continue; } AccordResult accord = CalculateLinearRegression(allBalancePointStatsFromYear, normalParams); //if (accord.R2Accord < 0.7500) //{ // success = UpdateOrInsertWthNormalParams(normalParams); // if (success && normalParamsExists) // { // newNormalParamsList.Add(normalParams); // updateCount++; // } // else if (success && !normalParamsExists) // { // newNormalParamsList.Add(normalParams); // insertCount++; // } // else // { // failCount++; // } // continue; //} normalParams.B1 = decimal.Round(Convert.ToDecimal(accord.Intercept), 9, MidpointRounding.AwayFromZero); if (accord.IsSimpleSingleRegression == true && accord.HeatingBP > 0) { normalParams.B2 = decimal.Round(Convert.ToDecimal(accord.B2), 9, MidpointRounding.AwayFromZero); normalParams.B3 = accord.HeatingBP; } else if (accord.IsSimpleSingleRegression == true && accord.CoolingBP > 0) { normalParams.B4 = decimal.Round(Convert.ToDecimal(accord.B4), 9, MidpointRounding.AwayFromZero); normalParams.B5 = accord.CoolingBP; } else if (accord.IsMultipleLinearRegression == true) { normalParams.B2 = decimal.Round(Convert.ToDecimal(accord.B2), 9, MidpointRounding.AwayFromZero); normalParams.B3 = accord.HeatingBP; normalParams.B4 = decimal.Round(Convert.ToDecimal(accord.B4), 9, MidpointRounding.AwayFromZero); normalParams.B5 = accord.CoolingBP; } if (!double.IsNaN(accord.R2Accord) && !Double.IsInfinity(accord.R2Accord)) { normalParams.R2 = decimal.Round(Convert.ToDecimal(accord.R2Accord), 9, MidpointRounding.AwayFromZero); } if (accord.FTestFailed) { Log.Warning($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID}: " + $"Best Regression Model's F-Test failed and model was not rejected. R2 = {normalParams.R2}"); } success = UpdateOrInsertWthNormalParams(normalParams); if (success && normalParamsExists) { newNormalParamsList.Add(normalParams); updateCount++; } else if (success && !normalParamsExists) { newNormalParamsList.Add(normalParams); insertCount++; } else if (!success) { modelsWithOtherIssues.Add(normalParams); failCount++; } } catch (BadWNRdngDataException bdEx) { failCount++; modelsWithReadingOrWeatherIssues.Add(normalParams); Log.Error(bdEx.Message + " " + bdEx.StackTrace); } catch (Exception e) { failCount++; modelsWithOtherIssues.Add(normalParams); Log.Error($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID} >> {e.Message} {e.StackTrace}"); } } foreach (WthNormalParams normalParams in modelsWithNotTwelveReadings) { Log.Warning($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID} >> " + $"Model was inserted but did not have (a multiple of) 12 readings from SP. "); } //foreach(WthNormalParams normalParams in modelsWithReadingOrWeatherIssues) //{ // Log.Warning($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID} >> No model found. " + // $"Readings had Bad/Null data from SP."); //} foreach (WthNormalParams normalParams in modelsWithOtherIssues) { Log.Error($"AccID/UtilID/UnitID: {normalParams.AccID}/{normalParams.UtilID}/{normalParams.UnitID} >> No model found. See daily log for more detail."); } if (failCount == 0) { Log.Information($"Finished PopulateWthNormalParams(). Rows Updated: {updateCount}. Rows Inserted: {insertCount}. Failures: {failCount}"); } else { Log.Warning($"Finished PopulateWthNormalParams() with failures. Rows Updated: {updateCount}. Rows Inserted: {insertCount}. Failures: {failCount}"); } //UpdateWthExpUsage(newNormalParamsList); }
private void PopulateWthNormalParams() { List <WNRdngData> allWNRdngData = _weatherRepository.GetAllReadingsFromStoredProcedure(); var wNRdngDataGroups = allWNRdngData.GroupBy(s => new { s.AccID, s.UtilID, s.UnitID }); List <WthNormalParams> newNormalParamsList = new List <WthNormalParams>(); int updateCount = 0; int insertCount = 0; foreach (var wNRdngGroup in wNRdngDataGroups) { List <WNRdngData> wNRdngList = wNRdngGroup.OrderBy(s => s.MoID).ToList(); WNRdngData lastRead = wNRdngList.LastOrDefault(); WthNormalParams normalParams = new WthNormalParams() { AccID = lastRead.AccID, UtilID = lastRead.UtilID, UnitID = lastRead.UnitID, WstID = lastRead.WstID, ZipW = lastRead.Zip, EndDate = lastRead.DateEnd, EMoID = lastRead.EMoID, MoCt = lastRead.MoCt }; bool normalParamsExists = _weatherRepository.GetWthNormalParamsExists(normalParams); try { List <BalancePointPair> allBalancePointStatsFromYear = CalculateOneYearOfDegreeDaysForAllBalancePoints(wNRdngList); int daysInYear = allBalancePointStatsFromYear.FirstOrDefault().DaysInYear; normalParams.DaysInYear = daysInYear; if (allBalancePointStatsFromYear.Count == 0) { if (normalParamsExists) { _weatherRepository.UpdateWthNormalParams(normalParams); newNormalParamsList.Add(normalParams); updateCount++; Log.Debug($"Updated WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } else { _weatherRepository.InsertWthNormalParams(normalParams); newNormalParamsList.Add(normalParams); insertCount++; Log.Debug($"Inserted WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } continue; } AccordResult accord = CalculateLinearRegression(allBalancePointStatsFromYear, normalParams); if (accord.FTestFailed) { Log.Information("F Test failed... " + normalParams.AccID + " " + normalParams.UtilID + " " + normalParams.UnitID); } normalParams.B1 = decimal.Round(Convert.ToDecimal(accord.Intercept), 9, MidpointRounding.AwayFromZero); if (accord.IsSimpleSingleRegression == true && accord.HeatingBP > 0) { normalParams.B2 = decimal.Round(Convert.ToDecimal(accord.B2), 9, MidpointRounding.AwayFromZero); normalParams.B3 = accord.HeatingBP; } else if (accord.IsSimpleSingleRegression == true && accord.CoolingBP > 0) { normalParams.B4 = decimal.Round(Convert.ToDecimal(accord.B4), 9, MidpointRounding.AwayFromZero); normalParams.B5 = accord.CoolingBP; } else if (accord.IsMultipleLinearRegression == true) { normalParams.B2 = decimal.Round(Convert.ToDecimal(accord.B2), 9, MidpointRounding.AwayFromZero); normalParams.B3 = accord.HeatingBP; normalParams.B4 = decimal.Round(Convert.ToDecimal(accord.B4), 9, MidpointRounding.AwayFromZero); normalParams.B5 = accord.CoolingBP; } if (!Double.IsNaN(accord.R2Accord) && !Double.IsInfinity(accord.R2Accord)) { normalParams.R2 = decimal.Round(Convert.ToDecimal(accord.R2Accord), 9, MidpointRounding.AwayFromZero); } if (normalParamsExists) { _weatherRepository.UpdateWthNormalParams(normalParams); newNormalParamsList.Add(normalParams); updateCount++; Log.Debug($"Updated WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } else { _weatherRepository.InsertWthNormalParams(normalParams); newNormalParamsList.Add(normalParams); insertCount++; Log.Debug($"Inserted WthNormalParams >> AccID: {normalParams.AccID}. UtilID: {normalParams.UtilID} UnitID: {normalParams.UnitID} " + $"B1: {normalParams.B1} B2: {normalParams.B2} B3: {normalParams.B3} B4: {normalParams.B4} B5: {normalParams.B5} R2: {normalParams.R2}."); } } catch (Exception e) { Log.Error(normalParams.AccID + " " + normalParams.UtilID + " " + normalParams.UnitID + " " + e.Message + " " + e.StackTrace); } } Log.Information($"PopulateWthNormalParams Finished. Rows Updated: {updateCount}. Rows Inserted: {insertCount}."); UpdateWthExpUsage(newNormalParamsList); }
private void PopulateWthNormalParams() { List <WNRdngData> allWNRdngData = _weatherRepository.GetAllReadingsFromStoredProcedure(); var wNRdngDataGroups = allWNRdngData.GroupBy(s => new { s.AccID, s.UtilID, s.UnitID }); foreach (var wNRdngGroup in wNRdngDataGroups) { List <WNRdngData> wNRdngList = wNRdngGroup.OrderBy(s => s.MoID).ToList(); WNRdngData lastRead = wNRdngList.LastOrDefault(); NormalParamsAccord nParamsAccord = new NormalParamsAccord() { AccID = lastRead.AccID, UtilID = lastRead.UtilID, UnitID = lastRead.UnitID, WstID = lastRead.WstID, ZipW = lastRead.Zip, EndDate = lastRead.DateEnd, EMoID = lastRead.EMoID, MoCt = lastRead.MoCt }; try { List <BPPairAccord> allBalancePointStatsFromYear = CalculateOneYearOfDegreeDaysForAllBalancePoints(wNRdngList); int daysInYear = allBalancePointStatsFromYear.FirstOrDefault().DaysInYear; nParamsAccord.DaysInYear = daysInYear; if (allBalancePointStatsFromYear.Count == 0) { //_weatherRepository.InsertWthNormalParams(normalParamsKey, Accord: true); _weatherRepository.InsertWthNormalParamsFinal(nParamsAccord); continue; } AccordResultNew accord = CalculateLinearRegression(allBalancePointStatsFromYear, nParamsAccord); if (accord.FTestFailed) { Console.WriteLine("F Test failed... " + nParamsAccord.AccID + " " + nParamsAccord.UtilID + " " + nParamsAccord.UnitID); } nParamsAccord.B1 = decimal.Round(Convert.ToDecimal(accord.Intercept), 9, MidpointRounding.AwayFromZero); if (accord.IsSimpleSingleRegression == true && accord.HeatingBP > 0) { nParamsAccord.B2 = decimal.Round(Convert.ToDecimal(accord.B2), 9, MidpointRounding.AwayFromZero); nParamsAccord.B3 = accord.HeatingBP; } else if (accord.IsSimpleSingleRegression == true && accord.CoolingBP > 0) { nParamsAccord.B4 = decimal.Round(Convert.ToDecimal(accord.B4), 9, MidpointRounding.AwayFromZero); nParamsAccord.B5 = accord.CoolingBP; } else if (accord.IsMultipleLinearRegression == true) { nParamsAccord.B2 = decimal.Round(Convert.ToDecimal(accord.B2), 9, MidpointRounding.AwayFromZero); nParamsAccord.B3 = accord.HeatingBP; nParamsAccord.B4 = decimal.Round(Convert.ToDecimal(accord.B4), 9, MidpointRounding.AwayFromZero); nParamsAccord.B5 = accord.CoolingBP; } if (!Double.IsNaN(accord.R2Accord) && !Double.IsInfinity(accord.R2Accord)) { nParamsAccord.R2 = decimal.Round(Convert.ToDecimal(accord.R2Accord), 9, MidpointRounding.AwayFromZero); } //nParams.YearOfReadsDateStart = accord.bpPair.YearOfReadsDateStart; //nParams.YearOfReadsDateEnd = accord.bpPair.YearOfReadsDateEnd; //nParams.Readings = accord.bpPair.ReadingsInNormalYear; //nParams.Days = accord.bpPair.DaysInNormalYear; //nParams.WthZipCode = accord.bpPair.WthZipCode; //_weatherRepository.InsertWthNormalParams(nParams, Accord: true); _weatherRepository.InsertWthNormalParamsFinal(nParamsAccord); } catch (Exception e) { Console.WriteLine(nParamsAccord.AccID + " " + nParamsAccord.UtilID + " " + nParamsAccord.UnitID); Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } } Console.WriteLine("PopulateWthNormalParams Finished."); }