Ejemplo n.º 1
0
 /// <summary>
 /// adds jobs to the job manager and does the old training offline processing.
 /// </summary>
 /// <returns></returns>
 public bool testGLM()
 {
     JobManager.addFeatureProcess("Glm");
     JobManager.addFeatureProcess("Voxelize");
     Training_OLDProcessing.OfflineProcess();
     return(true);
 }
Ejemplo n.º 2
0
        public void loadRawDataUsing_UDP(IAsyncResult ar)
        {
            if (shouldStop)
            {
                return;
            }

            byte[] data = Preferences.Instance.udp.EndReceive(ar);

            if (data.Length > 0)
            {
                string text = Encoding.ASCII.GetString(data);
                int    loc1 = text.LastIndexOf("\\");
                int    loc2 = text.LastIndexOf(".");
                int    loc3 = text.LastIndexOf("-");
                int    loc4 = text.LastIndexOf(",");

                if (Preferences.Instance.ProblemOriginal.samples == null)
                {
                    Preferences.Instance.ProblemOriginal = new libSVM_ExtendedProblem();
                    GC.Collect();
                    int totalToProcess = Int32.Parse(text.Substring(loc4 + 1, text.Length - (loc4 + 1)));
                    GuiPreferences.Instance.ProgressBarMax = totalToProcess;
                    //loading just the amount of data we need, so we assign accordingly, not this>Program.mainForm.getMaxAvailableTrs()
                    Preferences.Instance.ProblemOriginal.PrepareSamplesLabelsSize(totalToProcess);
                }

                string path = text.Substring(0, loc1 + 1);
                fileName   = text.Substring(loc1 + 1, loc2 - loc1 - 1);
                last_index = Int32.Parse(text.Substring(loc3 + 1, loc2 - loc3 - 1));

                if (last_index <= GuiPreferences.Instance.ToTR)
                {
                    //report progress
                    // DLL GUI Seperation
                    //Program.mainForm.progressBar1.Value = i - GuiPreferences.Instance.FromTR + 1 ;
                    GuiPreferences.Instance.ProgressBarValue = last_index;
                    fileName = GuiPreferences.Instance.WorkDirectory + GuiPreferences.Instance.FileName + last_index.ToString() + ".vdat";
                    loadRawFileName(fileName);
                    Training_OLDProcessing.OfflineProcess();
                    //Preferences.Instance.pipeClient.Send(fileName);
                    GuiPreferences.Instance.setLog("Sending Vector " + last_index.ToString());
                    last_index++;
                }
                else
                {
                    shouldStop = true;
                }


                //updategui();
                // shoot event
                //send the recieved data somewhere
            }

            if (!shouldStop)
            {
                Preferences.Instance.udp.BeginReceive();
            }
        }
Ejemplo n.º 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!");
            }
        }