public void RocCurveCreator_CalculateAUC_NoErrorThrown() { //Arrange var creator = new RocCurveCreator(); var xVals = "0 0,5 1"; var yVals = "0 0,5 1"; //Act var auc = creator.CalculateAUC(xVals, yVals); //Assert Assert.AreEqual(auc.ToString(), "0,5"); }
public ActionResult UploadResultsFile(UploadResultViewModel model) { var regex = new Regex("^([0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9])$"); if (model.RocCalc == "yes" && !regex.IsMatch(model.RocClassNumber)) { ModelState.AddModelError("CustomErrorRoc", "Incorrect class number."); return(View(model)); } if (model.file == null) { ModelState.AddModelError("CustomError", "Please, attach test results file."); return(View(model)); } else { using (var context = new Entities()) { var run = context.TestRuns.Find(model.RunNumber); run.RocClassNumber = model.RocClassNumber; var results = new TestResults() { AlgorithmId = context.Algorithms .Where(x => x.Name == model.AlgorithmName).ToArray()[0].Id, TestRunId = model.RunNumber }; var test = context.TestSets.Where(x => x.Name == model.TestName).ToArray()[0]; try { var analyzer = new TestResultsAnalyzer(); if (model.RocCalc == "no") { using (MemoryStream ms = new MemoryStream()) { model.file.InputStream.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); var statistics = analyzer.CalcErrorPercentage (test.ExpectedResults, ms, Convert.ToInt32(model.RepeatNumber)); results.CorrectRate = statistics.ToString("0.000"); context.TestResults.Add(results); context.SaveChanges(); run.Status = "Results saved"; context.SaveChanges(); return(RedirectToAction("RunResults", new { resultsId = results.Id })); } } else if (model.RocCalc == "yes") { using (MemoryStream ms = new MemoryStream()) { model.file.InputStream.CopyTo(ms); ms.Seek(0, SeekOrigin.Begin); var rocCreator = new RocCurveCreator(); var sbSensivity = new StringBuilder(); var sbSpecifity = new StringBuilder(); var statistics = rocCreator.GenerateRocCurveCoordinates(test.ExpectedResults, ms, model.RocClassNumber); for (var i = 0; i < statistics.rocCoordinatesSensivity.Count(); i++) { sbSensivity.Append(statistics.rocCoordinatesSensivity[i].ToString("0.000") + " "); sbSpecifity.Append(statistics.rocCoordinatesSpecifity[i].ToString("0.000") + " "); } var AUCrate = rocCreator.CalculateAUC(sbSpecifity.ToString(), sbSensivity.ToString()); results.RocCoordinatesSensiv = sbSensivity.ToString(); results.RocCoordinatesSpecif = sbSpecifity.ToString(); results.FN = statistics.FalseNegativeNumber.Average().ToString("0.000"); results.FP = statistics.FalsePositiveNumber.Average().ToString("0.000"); results.TN = statistics.TrueNegativeNumber.Average().ToString("0.000"); results.TP = statistics.TruePositiveNumber.Average().ToString("0.000"); results.CorrectRate = statistics.ErrorRate.ToString("0.000"); results.OtherInfo = AUCrate.ToString("0.000"); context.TestResults.Add(results); context.SaveChanges(); run.Status = "Results saved"; context.SaveChanges(); return(RedirectToAction("RunResultsRoc", new { resultsId = results.Id })); } } } catch (Exception) { ModelState.AddModelError("FileFormatException", "Error occured in results processing. Please check file format."); return(View(model)); } } } ModelState.AddModelError("FileFormatException", "Server error."); return(View(model)); }