private void btn_CalculateResults_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog() { Description = "Select folder to load test subjects from" }; if (fbd.ShowDialog() == DialogResult.OK) { FolderBrowserDialog sfd = new FolderBrowserDialog() { Description = "Select folder to load excel sheets from" }; if (sfd.ShowDialog() == DialogResult.OK) { Excel.Application exc = new Excel.Application() { Visible = false }; List<string> singles = new List<string>() { "EEG", "FACE", "GSR", "HR" }; List<string> fusion = new List<string>() { "Stacking", "Voting" }; Dictionary<SAMDataPoint.FeelingModel, int> cellOffsets = new Dictionary<SAMDataPoint.FeelingModel, int>() { [SAMDataPoint.FeelingModel.Arousal2High] = 11, [SAMDataPoint.FeelingModel.Arousal2Low] = 24, [SAMDataPoint.FeelingModel.Arousal3] = 40, [SAMDataPoint.FeelingModel.Valence2High] = 53, [SAMDataPoint.FeelingModel.Valence2Low] = 66, [SAMDataPoint.FeelingModel.Valence3] = 82 }; //test subject[SAMModel[machineType]] //items: C, Gamma, Kernel var configs = new Dictionary<string, Dictionary<SAMDataPoint.FeelingModel, Dictionary<string, Tuple<double, double, int>>>>(); foreach (var configBook in singles.Concat(fusion)) { Log.LogMessage($"Reading {configBook}..."); Excel.Workbook dataBook = exc.Workbooks.Open(sfd.SelectedPath + $"/{configBook}.xlsx", ExcelHandler.missingValue, false, ExcelHandler.missingValue, ExcelHandler.missingValue, ExcelHandler.missingValue, true, ExcelHandler.missingValue, ExcelHandler.missingValue, true, ExcelHandler.missingValue, ExcelHandler.missingValue, ExcelHandler.missingValue); foreach (Excel.Worksheet sheet in dataBook.Sheets) { if (sheet.Name == "Overview" || sheet.Name == "First" || sheet.Name == "Last") continue; if (!configs.ContainsKey(sheet.Name)) { configs.Add(sheet.Name, new Dictionary<SAMDataPoint.FeelingModel, Dictionary<string, Tuple<double, double, int>>>()); } if (configs[sheet.Name].Count == 0) { foreach (SAMDataPoint.FeelingModel feel in Enum.GetValues(typeof(SAMDataPoint.FeelingModel))) { configs[sheet.Name].Add(feel, new Dictionary<string, Tuple<double, double, int>>()); singles.ForEach(x => configs[sheet.Name][feel].Add(x, null)); fusion.ForEach(x => configs[sheet.Name][feel].Add(x, null)); if (feel.ToString().Contains("Valence")) { configs[sheet.Name][feel].Remove("GSR"); } } } foreach (SAMDataPoint.FeelingModel feel in Enum.GetValues(typeof(SAMDataPoint.FeelingModel))) { if (configBook == "GSR" && feel.ToString().Contains("Valence")) continue; Excel.Range c = (Excel.Range)sheet.Cells[cellOffsets[feel], 3]; Excel.Range gamma = (Excel.Range)sheet.Cells[cellOffsets[feel] + 1, 3]; Excel.Range kernel = (Excel.Range)sheet.Cells[cellOffsets[feel] + 2, 3]; if (c.Value != null && gamma.Value != null && kernel.Value != null) { configs[sheet.Name][feel][configBook] = Tuple.Create(c.Value, gamma.Value, (int)kernel.Value); } } } dataBook.Close(); } List<string> skippedList = new List<string>(); foreach (var subject in configs) { foreach (var feel in subject.Value) { foreach (var machine in feel.Value) { if (machine.Value == null) { skippedList.Add(subject.Key); } } } } foreach (var skipped in skippedList) { configs.Remove(skipped); Log.LogMessage($"Skipped {skipped}"); } var resultsList = new Dictionary<string, Dictionary<string, List<int>>>(); foreach (string feel in Enum.GetValues(typeof(SAMDataPoint.FeelingModel))) { resultsList.Add(feel, new Dictionary<string, List<int>>()); resultsList.Add("SAM", new Dictionary<string, List<int>>()); foreach (var item in singles.Concat(fusion)) { resultsList[feel].Add(item, new List<int>()); } } foreach (var subject in configs) { if (LoadData(fbd.SelectedPath + $"/{subject.Key}", _fd)) { foreach (var feel in subject.Value) { resultsList["SAM"][feel.Key.ToString()].AddRange(samData.dataPoints.Select(x => x.ToAVCoordinate(feel.Key))); foreach (var machine in feel.Value) { Log.LogMessage($"Calculating {subject.Key} / {feel} / {machine}"); var param = new SVMParameter() { C = machine.Value.Item1, Gamma = machine.Value.Item2, Kernel = (SVMKernelType)machine.Value.Item3 }; if (singles.Contains(machine.Key)) { var classifier = new StdClassifier(new SVMConfiguration(param, FeatureCreator.GetFeatures(machine.Key, feel.Key)), samData); var results = classifier.OldCrossValidate(feel.Key, 1); foreach (var res in results[0].guesses) { resultsList[feel.Key.ToString()][machine.Key].Add(res); } } else { //var classifier = new MetaClassifier(machine.Key, param, samData) } } } } else { Log.LogMessage($"Skipped {subject.Key} due to bad data"); skippedList.Add(subject.Key); } } Excel.Workbook resultBook = exc.Workbooks.Add(ExcelHandler.missingValue); Excel.Worksheet metaSheet = resultBook.Sheets.Add(); metaSheet.Name = "Meta"; metaSheet.Cells[1, 1] = "Skipped " + skippedList.Count; for (int i = 0; i < skippedList.Count; i++) { metaSheet.Cells[i + 2, 1] = skippedList[i]; } foreach (var feel in Enum.GetNames(typeof(SAMDataPoint.FeelingModel))) { Excel.Worksheet feelSheet = resultBook.Sheets.Add(); feelSheet.Name = feel; } exc.Quit(); } } }
List<StdClassifier> ConfigurationsToStds(List<SVMConfiguration> confs) { return confs.Select((x) => { StdClassifier cls = new StdClassifier(x, samData); return cls; }).ToList(); }
Thread CreateMachineThread(string name, List<SVMParameter> pars, List<Feature> feats, SAMDataPoint.FeelingModel feels, Action<int, int> UpdateCallback, bool useControlSAM) { return new Thread(() => { StdClassifier mac = new StdClassifier(name, pars, feats, samData); mac.UpdateCallback = UpdateCallback; var res = mac.OldCrossValidate(feels, 1, useControlSAM); SaveBestResult(res, mac.Name + "_" + feels); }); }
private void btn_RunAll_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { chk_useControlValues.Enabled = false; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Log.LogMessage("Starting Stopwatch"); List<SAMDataPoint.FeelingModel> feelings = new List<SAMDataPoint.FeelingModel>() { SAMDataPoint.FeelingModel.Arousal2High, SAMDataPoint.FeelingModel.Arousal2Low, SAMDataPoint.FeelingModel.Arousal3, SAMDataPoint.FeelingModel.Valence2Low, SAMDataPoint.FeelingModel.Valence2High, SAMDataPoint.FeelingModel.Valence3 }; eh = new ExcelHandler(fbd.SelectedPath); if (!eh.BooksOpen) { Log.LogMessage("Cannot open or write to books"); return; } var dataFolders = Directory.GetDirectories(fbd.SelectedPath); List<SVMParameter> parameters = GenerateSVMParameters(); //Debug param /* List<SVMParameter> parameters = new List<SVMParameter> { new SVMParameter() }; parameters[0].C = 32; parameters[0].Gamma = 0.25; parameters[0].Kernel = SVMKernelType.SIGMOID;*/ int curDat = 1; int maxDat = dataFolders.Length; foreach (var item in dataFolders) { if (item.Split('\\').Last() == "Stats") { Log.LogMessage("Stats folder skipping"); continue; } DataProgressHandler DPH = new DataProgressHandler(item); if (DPH.AllDone) { Log.LogMessage("Already did " + item + ", skipping.."); curDat++; continue; } if (!LoadData(item, _fd)) { Log.LogMessage(item.Split('-').Last() + " is not classifiable"); continue; } string personName = item.Split('\\').Last(); eh.AddPersonToBooks(personName); foreach (var feel in feelings) { statusLabel.Text = "STANDARD: " + curDat + "/" + maxDat + " -> " + feel + " -> " + item.Split('\\').Last(); gsrProg = 0; gsrTot = 1; hrProg = 0; hrTot = 1; eegProg = 0; eegTot = 1; faceProg = 0; faceTot = 1; bool gsrWrite = false; bool hrWrite = false; bool eegWrite = false; bool faceWrite = false; if (feel != SAMDataPoint.FeelingModel.Valence2High && feel != SAMDataPoint.FeelingModel.Valence2Low && feel != SAMDataPoint.FeelingModel.Valence3 && !skipGSR && !DPH.done["GSR" + Enum.GetName(typeof(SAMDataPoint.FeelingModel), feel)] && shouldRun["GSR.dat"]) { gsrThread = CreateMachineThread("GSR", parameters, FeatureCreator.GSRArousalOptimizationFeatures, feel, (cur, max) => { gsrProg = cur; gsrTot = max; }, chk_useControlValues.Checked); gsrThread.Priority = threadPrio; gsrThread.Start(); } else { Log.LogMessage("GSR skipping"); } if (!DPH.done["HR" + Enum.GetName(typeof(SAMDataPoint.FeelingModel), feel)] && !skipHR && shouldRun["HR.dat"]) { hrThread = CreateMachineThread("HR", parameters, (feel == SAMDataPoint.FeelingModel.Valence2High || feel == SAMDataPoint.FeelingModel.Valence2Low || feel == SAMDataPoint.FeelingModel.Valence3) ? FeatureCreator.HRValenceOptimizationFeatures : FeatureCreator.HRArousalOptimizationFeatures, feel, (cur, max) => { hrProg = cur; hrTot = max; }, chk_useControlValues.Checked); hrThread.Priority = threadPrio; hrThread.Start(); } else { Log.LogMessage("HR skipping"); } if (!DPH.done["EEG" + Enum.GetName(typeof(SAMDataPoint.FeelingModel), feel)] && !skipEEG && shouldRun["EEG.dat"]) { eegThread = CreateMachineThread("EEG", parameters, (feel == SAMDataPoint.FeelingModel.Valence2High || feel == SAMDataPoint.FeelingModel.Valence2Low || feel == SAMDataPoint.FeelingModel.Valence3) ? FeatureCreator.EEGValenceOptimizationFeatures : FeatureCreator.EEGArousalOptimizationFeatures, feel, (cur, max) => { eegProg = cur; eegTot = max; }, chk_useControlValues.Checked); eegThread.Priority = threadPrio; eegThread.Start(); } else { Log.LogMessage("EEG skipping"); } if (!DPH.done["Face" + Enum.GetName(typeof(SAMDataPoint.FeelingModel), feel)] && !skipFace && shouldRun["KINECT.dat"]) { faceThread = CreateMachineThread("FACE", parameters, (feel == SAMDataPoint.FeelingModel.Valence2High || feel == SAMDataPoint.FeelingModel.Valence2Low || feel == SAMDataPoint.FeelingModel.Valence3) ? FeatureCreator.FACEValenceOptimizationFeatures : FeatureCreator.FACEArousalOptimizationFeatures, feel, (cur, max) => { faceProg = cur; faceTot = max; }, chk_useControlValues.Checked); faceThread.Priority = threadPrio; faceThread.Start(); } else { Log.LogMessage("Face skipping"); } List<SVMConfiguration> confs = new List<SVMConfiguration>(); SVMConfiguration gsrConf; SVMConfiguration eegConf; SVMConfiguration hrConf; SVMConfiguration faceConf; while ((gsrThread != null && gsrThread.IsAlive) || (hrThread != null && hrThread.IsAlive) || (eegThread != null && eegThread.IsAlive) || (faceThread != null && faceThread.IsAlive)) { Thread.Sleep(1000); eegBar.Value = (int)(((double)eegProg / eegTot) * 100); gsrBar.Value = (int)(((double)gsrProg / gsrTot) * 100); faceBar.Value = (int)(((double)faceProg / faceTot) * 100); hrBar.Value = (int)(((double)hrProg / hrTot) * 100); Application.DoEvents(); if (gsrThread != null && !gsrThread.IsAlive && !gsrWrite) { gsrWrite = true; gsrConf = svmConfs.OfType<SVMConfiguration>().First((x) => x.Name.StartsWith("GSR") && x.Name.Contains(feel.ToString())); confs.Add(gsrConf); var gsrMac = new StdClassifier(gsrConf, samData); var gsrRes = gsrMac.OldCrossValidate(feel, 1); Log.LogMessage("Best result for person " + curDat + " GSR " + feel + " is " + gsrRes[0].GetAccuracy()); eh.AddDataToPerson(personName, ExcelHandler.Book.GSR, gsrRes.First(), feel); DPH.done["GSR" + Enum.GetName(typeof(SAMDataPoint.FeelingModel), feel)] = true; DPH.SaveProgress(); gsrThread = null; } if (hrThread != null && !hrThread.IsAlive && !hrWrite) { hrWrite = true; hrConf = svmConfs.OfType<SVMConfiguration>().First((x) => x.Name.StartsWith("HR") && x.Name.Contains(feel.ToString())); confs.Add(hrConf); var hrMac = new StdClassifier(hrConf, samData); var hrRes = hrMac.OldCrossValidate(feel, 1); Log.LogMessage("Best result for person " + curDat + " HR " + feel + " is " + hrRes[0].GetAccuracy()); eh.AddDataToPerson(personName, ExcelHandler.Book.HR, hrRes.First(), feel); DPH.done["HR" + Enum.GetName(typeof(SAMDataPoint.FeelingModel), feel)] = true; DPH.SaveProgress(); hrThread = null; } if (eegThread != null && !eegThread.IsAlive && !eegWrite) { eegWrite = true; eegConf = svmConfs.OfType<SVMConfiguration>().First((x) => x.Name.StartsWith("EEG") && x.Name.Contains(feel.ToString())); confs.Add(eegConf); var eegMac = new StdClassifier(eegConf, samData); var eegRes = eegMac.OldCrossValidate(feel, 1); Log.LogMessage("Best result for person " + curDat + " EEG " + feel + " is " + eegRes[0].GetAccuracy()); eh.AddDataToPerson(personName, ExcelHandler.Book.EEG, eegRes.First(), feel); DPH.done["EEG" + Enum.GetName(typeof(SAMDataPoint.FeelingModel), feel)] = true; DPH.SaveProgress(); eegThread = null; } if (faceThread != null && !faceThread.IsAlive && !faceWrite) { faceWrite = true; faceConf = svmConfs.OfType<SVMConfiguration>().First((x) => x.Name.StartsWith("FACE") && x.Name.Contains(feel.ToString())); confs.Add(faceConf); var faceMac = new StdClassifier(faceConf, samData); var faceRes = faceMac.OldCrossValidate(feel, 1); Log.LogMessage("Best result for person " + curDat + " Face " + feel + " is " + faceRes[0].GetAccuracy()); eh.AddDataToPerson(personName, ExcelHandler.Book.FACE, faceRes.First(), feel); DPH.done["Face" + Enum.GetName(typeof(SAMDataPoint.FeelingModel), feel)] = true; DPH.SaveProgress(); faceThread = null; } } Log.LogMessage("Done with single machine searching."); foreach (var cnf in confs) { Log.LogMessage("Saving " + cnf.Name + "..."); } //Write normal results eh.Save(); Log.LogMessage("Total time: " + stopwatch.Elapsed + " Current person: " + curDat + " and model " + feel.ToString()); } curDat++; eh.Save(); Log.LogMessage("DonnoDK"); } eh.CloseBooks(); Log.LogMessage("Closing books and saving"); Log.LogMessage("Done in: " + stopwatch.Elapsed); } }