示例#1
0
 //was implemented in a button, not used anymore.
 private void ClearProblemSVMJob(object sender, EventArgs e)
 {
     Training_MultiRunProcessing.clearProblem();
     Training_MultiRunProcessing.clearSVM();
     JobManager.clearJob();
     GC.Collect();
 }
示例#2
0
        /// <summary>
        /// Process is the most updated code toward all the classification options, but should be used when doing offline classification as it expects the param file to be located in the test dir
        /// in the online version it should look for the Param file
        /// </summary>
        public static void OfflineProcess()
        {
            TrainingTesting_SharedVariables._trialProblem = null;
            if (Preferences.Instance.ProblemOriginal != null)
            {
                if (Preferences.Instance.ProblemOriginal.samples != null)
                {
                    //we only clear all memory in offline analysis, and once in the first realtime processing vector - which is done externally in testdll
                    if (!Preferences.Instance.RealTimeTestingSingleVector)
                    {
                        GuiPreferences.Instance.setLog("Starting Processing.");
                        Training_MultiRunProcessing.clearProblem();
                        Training_MultiRunProcessing.clearSVM();
                        JobManager.clearJob();
                        GC.Collect();
                    }

                    //if unprocessed is UNchecked then process all jobs.
                    if (!GuiPreferences.Instance.UnprocessedChecked)
                    {
                        //we only need to add jobs at the beginning and do it once.
                        if ((!Preferences.Instance.RealTimeTestingSingleVector) || (Preferences.Instance.RealTimeTestingSingleVector && Preferences.Instance.ProblemOriginal.samples[1] == null))
                        {
                            JobManager.addFeatureJobs();
                            JobManager.calculatePreviousKValues();
                        }

                        //pre for all jobs, future support only
                        JobManager.preProcessFeatureJobs();
                        //here the pre/proc/post is done for every job.
                        JobManager.processFeatureJobs();
                        //post for all jobs, future support only
                        JobManager.postProcessFeatureJobs();


                        //TODO: do this each time we get out of processing, or do this just when "Testing" is activated". seems harmless to do it all the time.
                        TrainingTesting_SharedVariables._trialProblem = Preferences.Instance.ProblemFinal;

                        //we only need to assign to the final problem when we are in offline mode
                        //in real time we only need to send the classification result and sample weight.
                        if (!Preferences.Instance.RealTimeTestingSingleVector)
                        {
                            GuiPreferences.Instance.setLog("Processing Finished!");
                        }
                        else
                        {
                            if (Preferences.Instance.currentUDPVector >= Preferences.Instance.maxKvectorsToWaitBeforeClassification)
                            {
                                Preferences.Instance.currentClassifiedVector += 1;
                                //allow to predict only if sample is not labeled as baseline - this happens in the beginning of a experiment session
                                if (Preferences.Instance.ProblemFinal.labels[Preferences.Instance.currentClassifiedVector - 1] != 1.0)
                                {
                                    //allow to predict
                                    double pred = 1.0;
                                    GuiPreferences.Instance.setLog("Single Vector Processing Finished..");
                                    pred = Preferences.Instance.svmWrapper.GetAccuracyFromTestSingleSample(Preferences.Instance.ProblemFinal, Preferences.Instance.svmModel);
                                    RealTimeProcessing.transmitPrediction(pred);
                                }
                            }
                        }
                    }
                    else //process without PROCESSING JOBS, the original problem
                    {
                        //real-time testing
                        if (Preferences.Instance.RealTimeTestingSingleVector)
                        {
                            // we load the configuration file for min/max for all features - ONCE.
                            if (!TrainingTesting_SharedVariables._svmscaleTraining.ConfigFileLoaded)
                            {
                                //load normalization configuration from training stage.
                                string commandLine = "-l 0 " +
                                                     "-r " + GuiPreferences.Instance.WorkDirectory +
                                                     "TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scale_paramCS.libsvm " +
                                                     GuiPreferences.Instance.WorkDirectory + "nofile";

                                TrainingTesting_SharedVariables._svmscaleTraining.initSingleVectorConfig(commandLine.Split(' '), 80 * 80 * 32);
                            }

                            if (GuiPreferences.Instance.CbSMOChecked)
                            {
                                RealTimeProcessing.ExecuteRealtimeTest(TrainingTesting_SharedVariables.smo);

                                Preferences.Instance.currentClassifiedVector += 1;

                                //test on self should get 100%
                                //testingEval = new weka.classifiers.Evaluation(data);
                                //testingEval.evaluateModel(smo, data);
                                //printWekaResults(testingEval.toSummaryString("\nResults\n======\n", false));
                                //GuiPreferences.Instance.setLog("RealTime SMO Model Testing on current vector");
                                //pred = (double)testingEval.predictions().elementAt(0);
                                //transmitPrediction(pred);

                                //GuiPreferences.Instance.setLog("Single Vector Processing Finished..");
                            }
                            else if (GuiPreferences.Instance.CbSVMChecked) //weka + SVM, pipeline unfinished.
                            {
                                //if (Preferences.Instance.currentUDPVector >= Preferences.Instance.maxKvectorsToWaitBeforeClassification)
                                {
                                    Preferences.Instance.currentClassifiedVector += 1;
                                    //allow to predict only if sample is not labeled as baseline - this happens in the beginning of a experiment session
                                    //if (Preferences.Instance.ProblemFinal.labels[Preferences.Instance.currentClassifiedVector - 1] != 1.0)
                                    {
                                        //allow to predict
                                        double pred = 1.0;
                                        pred = Preferences.Instance.svmWrapper.GetAccuracyFromTestSingleSample(Preferences.Instance.ProblemFinal, Preferences.Instance.svmModel);
                                        RealTimeProcessing.transmitPrediction(pred);
                                        GuiPreferences.Instance.setLog("Single Vector Processing Finished..");
                                    }
                                }
                            }
                        }
                        else
                        {
                            TrainingTesting_SharedVariables._trialProblem = Preferences.Instance.ProblemOriginal;

                            //process the data, save files, normalize, filter, pick upto 1000, save etc..
                            TrainingTesting_SharedVariables._trialWekaData = WekaPipeline_Unprocessed(TrainingTesting_SharedVariables._trialProblem);
                            if (TrainingTesting_SharedVariables._trialWekaData != null)
                            {
                                GuiPreferences.Instance.setLog(
                                    "Weka ProcessingFinished! (in UN-Processing code condition)");
                            }
                            else
                            {
                                GuiPreferences.Instance.setLog(
                                    "Processing Failed: Weka Processing (in UN-Processing code condition)");
                            }

                            if (GuiPreferences.Instance.CbSVMChecked)
                            {
                                Preferences.Instance.ProblemOriginal = new libSVM_ExtendedProblem();
                                //here we need to load the filtered DS
                                //Preferences.Instance.svmWrapper.

                                //GuiPreferences.Instance.WorkDirectory + "TrainSet_3th_vectors_scaledCS_filteredIG.libsvm";
                            }
                        }
                    }
                }
                else
                {
                    GuiPreferences.Instance.setLog("Processing Failed: samples empty!");
                }
            }
            else
            {
                GuiPreferences.Instance.setLog("Processing Failed: Problem null!");
            }
        }
示例#3
0
        /// <summary>
        /// Process is the most updated code toward all the classification options, currently only used for a single vector online classificaiton, the difference from the offline is the ability to load the param file from the model directory
        /// </summary>
        public static void RealTimeProcess()
        {
            TrainingTesting_SharedVariables._trialProblem = null;
            if (Preferences.Instance.ProblemOriginal != null)
            {
                if (Preferences.Instance.ProblemOriginal.samples != null)
                {
                    //we only clear all memory in offline analysis, and once in the first realtime processing vector - which is done externally in testdll
                    if (!Preferences.Instance.RealTimeTestingSingleVector)
                    {
                        GuiPreferences.Instance.setLog("Starting Processing.");
                        Training_MultiRunProcessing.clearProblem();
                        Training_MultiRunProcessing.clearSVM();
                        JobManager.clearJob();
                        GC.Collect();
                    }

                    //if unprocessed is UNchecked then process all jobs.
                    if (!GuiPreferences.Instance.UnprocessedChecked)
                    {
                        //we only need to add jobs at the beginning and do it once.
                        if ((!Preferences.Instance.RealTimeTestingSingleVector) || (Preferences.Instance.RealTimeTestingSingleVector && Preferences.Instance.ProblemOriginal.samples[1] == null))
                        {
                            JobManager.addFeatureJobs();
                            JobManager.calculatePreviousKValues();
                        }

                        //pre for all jobs, future support only
                        JobManager.preProcessFeatureJobs();
                        //here the pre/proc/post is done for every job.
                        JobManager.processFeatureJobs();
                        //post for all jobs, future support only
                        JobManager.postProcessFeatureJobs();


                        //TODO: do this each time we get out of processing, or do this just when "Testing" is activated". seems harmless to do it all the time.
                        TrainingTesting_SharedVariables._trialProblem = Preferences.Instance.ProblemFinal;

                        //we only need to assign to the final problem when we are in offline mode
                        //in real time we only need to send the classification result and sample weight.
                        if (!Preferences.Instance.RealTimeTestingSingleVector)
                        {
                            GuiPreferences.Instance.setLog("Processing Finished!");
                        }
                        else
                        {
                            if (Preferences.Instance.currentUDPVector >= Preferences.Instance.maxKvectorsToWaitBeforeClassification)
                            {
                                Preferences.Instance.currentClassifiedVector += 1;
                                //allow to predict only if sample is not labeled as baseline - this happens in the beginning of a experiment session
                                if (Preferences.Instance.ProblemFinal.labels[Preferences.Instance.currentClassifiedVector - 1] != 1.0)
                                {
                                    //allow to predict
                                    double pred = 1.0;
                                    GuiPreferences.Instance.setLog("Single Vector Processing Finished..");
                                    pred = Preferences.Instance.svmWrapper.GetAccuracyFromTestSingleSample(Preferences.Instance.ProblemFinal, Preferences.Instance.svmModel);
                                    transmitPrediction(pred);
                                }
                            }
                        }
                    }
                    else //process without PROCESSING JOBS, the original problem
                    {
                        //real-time testing
                        if (Preferences.Instance.RealTimeTestingSingleVector)
                        {
                            //NOTE: minmax loading moved to form1.cs before pipe async creation.

                            if (GuiPreferences.Instance.CbSMOChecked)
                            {
                                if (!Preferences.Instance.corruptedVector)
                                {
                                    ExecuteRealtimeTest(TrainingTesting_SharedVariables.smo);
                                }
                                else
                                {
                                    ExecuteRealtimeTestForCorrupted(TrainingTesting_SharedVariables.smo);
                                }

                                Preferences.Instance.currentClassifiedVector += 1;


                                //create Median (and more) after the baseline event has finished.
                                if (Preferences.Instance.currentClassifiedVector == Preferences.Instance.events.eventList[0].var2) //==10 for the first baseline
                                {
                                    Preferences.Instance.TestingBaselineStatistics.createAllStatistics();
                                    Preferences.Instance.TestingBaselineStatistics.saveCSV();
                                }

                                //create and save all the statistics the entire run has finished. must
                                //NOTE it must be a complete run, no errors!
                                if (Preferences.Instance.currentClassifiedVector >= Preferences.Instance.events.EventListLastTr) //==10 for the first baseline
                                {
                                    Sound.PlayTestingFinished();

                                    //save full csv must happen before createallstatistics as we remove 2 baseline cells in there.
                                    //saved per feature RAW value at the 4th tr
                                    Preferences.Instance.MatrixStatistics.saveFullMatrixCSV(4, "RAW_");

                                    Preferences.Instance.MatrixStatistics.createAllStatistics();
                                    Preferences.Instance.MatrixStatistics.saveCSV();

                                    //saved per feature normalized value at the 4th tr
                                    Preferences.Instance.MatrixStatisticsNormalized.saveFullMatrixCSV(4, "NORM_");

                                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                    /// MOVE ME out of here: save true min and true max vs simulated min and simulated max (true minmax can only be seen at the end of the test run)
                                    string filename = GuiPreferences.Instance.WorkDirectory + "MinMaxSimulatedMinSimulatedMax_" +
                                                      DateTime.Now.ToString("g").Replace(":", "_").Replace("/", "").Replace(" ", "_") + ".csv";
                                    List <string> l = new List <string>();

                                    l.Add("min,max,simulatedMin,simulatedMax");
                                    string line = "";



                                    for (int k = 0; k < TrainingTesting_SharedVariables._trainTopIGFeatures.Length; k++)
                                    {
                                        int    infoGainFeature = TrainingTesting_SharedVariables._trainTopIGFeatures[k] + 1;
                                        double simulatedMin    =
                                            Preferences.Instance.TestingBaselineStatistics.getMedianWhenIGIndicesSaved(infoGainFeature) - Preferences.Instance.medianRange.feature_min[infoGainFeature];
                                        double simulatedMax =
                                            Preferences.Instance.TestingBaselineStatistics.getMedianWhenIGIndicesSaved(infoGainFeature) + Preferences.Instance.medianRange.feature_max[infoGainFeature];
                                        //global minmax
                                        //line = Preferences.Instance.MatrixStatistics.min[k].ToString() + "," +
                                        //       Preferences.Instance.MatrixStatistics.max[k].ToString() + ","

                                        //tr4 or the one used minmax.
                                        line = Preferences.Instance.MinMax[0][Convert.ToInt32(GuiPreferences.Instance.NudClassifyUsingTR)].feature_min[infoGainFeature] + "," +
                                               Preferences.Instance.MinMax[0][Convert.ToInt32(GuiPreferences.Instance.NudClassifyUsingTR)].feature_max[infoGainFeature] + "," +
                                               simulatedMin.ToString() + "," + simulatedMax.ToString();
                                        l.Add(line);
                                    }
                                    GuiPreferences.Instance.setLog("Saving " + filename);
                                    File.WriteAllLines(filename, l);

                                    //global mean and after ig mean
                                    filename = GuiPreferences.Instance.WorkDirectory + "MeanBeforeIGMeanAfterIG" +
                                               DateTime.Now.ToString("g").Replace(":", "_").Replace("/", "").Replace(" ", "_") + ".csv";
                                    GuiPreferences.Instance.setLog("Saving " + filename);
                                    File.WriteAllLines(filename, Preferences.Instance.BrainMean);
                                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                    try
                                    {
                                        StatisticsAccuracy.generateStats();
                                    }
                                    catch (Exception e)
                                    {
                                        GuiPreferences.Instance.setLog(e.Message);
                                    }
                                    Logging.saveLog("Testing");
                                }

                                //test on self should get 100%
                                //testingEval = new weka.classifiers.Evaluation(data);
                                //testingEval.evaluateModel(smo, data);
                                //printWekaResults(testingEval.toSummaryString("\nResults\n======\n", false));
                                //GuiPreferences.Instance.setLog("RealTime SMO Model Testing on current vector");
                                //pred = (double)testingEval.predictions().elementAt(0);
                                //transmitPrediction(pred);

                                //GuiPreferences.Instance.setLog("Single Vector Processing Finished..");
                            }
                            else if (GuiPreferences.Instance.CbSVMChecked) //weka + SVM, pipeline unfinished.
                            {
                                //if (Preferences.Instance.currentUDPVector >= Preferences.Instance.maxKvectorsToWaitBeforeClassification)
                                {
                                    Preferences.Instance.currentClassifiedVector += 1;
                                    //allow to predict only if sample is not labeled as baseline - this happens in the beginning of a experiment session
                                    //if (Preferences.Instance.ProblemFinal.labels[Preferences.Instance.currentClassifiedVector - 1] != 1.0)
                                    {
                                        //allow to predict
                                        double pred = 1.0;
                                        pred = Preferences.Instance.svmWrapper.GetAccuracyFromTestSingleSample(Preferences.Instance.ProblemFinal, Preferences.Instance.svmModel);
                                        transmitPrediction(pred);
                                        GuiPreferences.Instance.setLog("Single Vector Processing Finished..");
                                    }
                                }
                            }
                        }
                        else
                        {
                            TrainingTesting_SharedVariables._trialProblem = Preferences.Instance.ProblemOriginal;

                            //process the data, save files, normalize, filter, pick upto 1000, save etc..
                            TrainingTesting_SharedVariables._trialWekaData = Training_OLDProcessing.WekaPipeline_Unprocessed(TrainingTesting_SharedVariables._trialProblem);
                            if (TrainingTesting_SharedVariables._trialWekaData != null)
                            {
                                GuiPreferences.Instance.setLog(
                                    "Weka ProcessingFinished! (in UN-Processing code condition)");
                            }
                            else
                            {
                                GuiPreferences.Instance.setLog(
                                    "Processing Failed: Weka Processing (in UN-Processing code condition)");
                            }

                            if (GuiPreferences.Instance.CbSVMChecked)
                            {
                                Preferences.Instance.ProblemOriginal = new libSVM_ExtendedProblem();
                                //here we need to load the filtered DS
                                //Preferences.Instance.svmWrapper.

                                //GuiPreferences.Instance.WorkDirectory + "TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scaledCS_filteredIG.libsvm";
                            }
                        }
                    }
                }
                else
                {
                    GuiPreferences.Instance.setLog("Processing Failed: samples empty!");
                }
            }
            else
            {
                GuiPreferences.Instance.setLog("Processing Failed: Problem null!");
            }
        }