Esempio n. 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();
 }
Esempio n. 2
0
        public static void WekaTrainingPipelineForMultiRuns()
        {
            //todo check max index in file,
            //todo check if needs to remove 204801 from it so it doesnt effect the class.

            Instances finalData = Training_MultiRunProcessing.ConcatenationPipeLine("TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scaledCS.libsvm.arff",
                                                                                    "TrainSet_" + (GuiPreferences.Instance.NudClassifyUsingTR + 1).ToString() + "th_vectors_scaledCS.libsvm.arff");

            WekaTrainingMethods.TrainSMO(finalData);
        }
Esempio n. 3
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!");
            }
        }
Esempio n. 4
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!");
            }
        }
Esempio n. 5
0
        private static void SVMWrapperTrainingPipeline()
        {
            Preferences.Instance.modelLoaded = false;

            TrainingTesting_SharedVariables.training = new LibSVM_TrainTest();
            TrainingTesting_SharedVariables.training.setSvmType();

            GuiPreferences.Instance.setLog("NOTE: we shouldnt balance labels count. it should be balanced in the protocol");
            GuiPreferences.Instance.setLog("Default Training MultiClass on all data");

            TrainingTesting_SharedVariables._trainingProblem = TrainingTesting_SharedVariables._trialProblem;

            // show some statistics regarding the labels
            Training_MultiRunProcessing.labelStats(TrainingTesting_SharedVariables._trainingProblem);

            //train
            GuiPreferences.Instance.setLog("Training on Data");
            TrainingTesting_SharedVariables.training.TrainOnAllData(TrainingTesting_SharedVariables._trainingProblem);
            GuiPreferences.Instance.setLog("Finished Training.");


            //test on self should get 100%
            TrainingTesting_SharedVariables.training.TestOnAllData(TrainingTesting_SharedVariables._trainingProblem);
            GuiPreferences.Instance.setLog("SVM Model Tested on Training Data, check that you get 100%.");

            TrainingTesting_SharedVariables.training.saveModel(GuiPreferences.Instance.WorkDirectory + "TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scaledCS_filteredIG.libsvm.model");
            GuiPreferences.Instance.setLog("SVM Model saved.");

            TrainingTesting_SharedVariables.training.loadModel(GuiPreferences.Instance.WorkDirectory + "TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scaledCS_filteredIG.libsvm.model");
            GuiPreferences.Instance.setLog("SVM Model loaded.");

            //test loaded model - DOES IT REALY USE THE LOADED MODEL? THIS IS UNVERIFIED, DONT RELY UNTIL TESTED.
            TrainingTesting_SharedVariables.training.TestOnAllData(TrainingTesting_SharedVariables._trainingProblem);
            GuiPreferences.Instance.setLog("SVM Model Tested on data (sanity check for loaded model).");

            //display top IG on dicom view
            if (Preferences.Instance.attsel == null)
            {
                GuiPreferences.Instance.setLog("there are no ranked IG attributes or selected attr, continuing but please fix this possible bug.");
            }
            GuiPreferences.Instance.setLog("Dicom Viewer Displaying..");
            string dicomDir = GuiPreferences.Instance.WorkDirectory;

            dicomDir = dicomDir.Substring(0, dicomDir.Length - 4) + @"master\";
            string[] files     = System.IO.Directory.GetFiles(dicomDir, "*.dcm");
            string   firstFile = files[0].Substring(files[0].LastIndexOf(@"\") + 1);


            bool thresholdOrVoxelAmount;

            if (GuiPreferences.Instance.IgSelectionType == IGType.Threshold)
            {
                thresholdOrVoxelAmount = true;
            }
            else
            {
                thresholdOrVoxelAmount = false;
            }
            Form plotForm = new DicomImageViewer.MainForm(dicomDir + firstFile, firstFile,
                                                          Preferences.Instance.attsel.rankedAttributes(),
                                                          Convert.ToDouble(GuiPreferences.Instance.NudIGThreshold),
                                                          Convert.ToInt32(GuiPreferences.Instance.NudIGVoxelAmount),
                                                          thresholdOrVoxelAmount);// _trainTopIGFeatures);

            plotForm.StartPosition = FormStartPosition.CenterParent;
            plotForm.ShowDialog();
            plotForm.Close();
            GuiPreferences.Instance.setLog("Dicom Viewer Closed.");
        }
Esempio n. 6
0
        /// <summary>
        /// when train button pushed, we add jobs, pre, process, post them.
        /// </summary>
        public static void Train()
        {
            GC.Collect();
            if (TrainingTesting_SharedVariables._trialProblem != null)
            {
                if (TrainingTesting_SharedVariables._trialProblem.samples != null)
                {
                    //temprarily the weka pipeline is the first thing we want to do, as it doesnt need all the C# wrapper functions and configuration
                    if (GuiPreferences.Instance.TrainType == TrainingType.Weka)
                    {
                        if (GuiPreferences.Instance.CbSVMChecked)
                        {
                            GuiPreferences.Instance.setLog("SVM C# Wrapper Training..");
                            SVMWrapperTrainingPipeline();
                        }
                        else if (GuiPreferences.Instance.CbSMOChecked)
                        {
                            GuiPreferences.Instance.setLog("SMO Training..");
                            WekaTrainingPipeline(TrainingTesting_SharedVariables._trialWekaData);
                        }

                        return;
                    }

                    GuiPreferences.Instance.setLog("TODO: if any models were loaded we ignore then as they would be replaced by training again.\n" +
                                                   "actually this should only be for rfe/grid/90-10, as cfv doesnt provide a model.");
                    Preferences.Instance.modelLoaded = false;

                    TrainingTesting_SharedVariables.training = new LibSVM_TrainTest();
                    TrainingTesting_SharedVariables.training.setSvmType();

                    GuiPreferences.Instance.setLog("NOTE: we shouldnt balance labels count. it should be balanced in the protocol");

                    //if not multiclass, keep only the two binary classes
                    if (!GuiPreferences.Instance.CbMultiClassChecked)
                    {
                        TrainingTesting_SharedVariables._trainingProblem = Training_MultiRunProcessing.getBinaryClases(TrainingTesting_SharedVariables._trialProblem);
                        GuiPreferences.Instance.setLog("Using only Binary Classes");
                    }
                    else
                    {
                        TrainingTesting_SharedVariables._trainingProblem = TrainingTesting_SharedVariables._trialProblem;
                    }

                    // show some statistics regarding the labels
                    Training_MultiRunProcessing.labelStats(TrainingTesting_SharedVariables._trainingProblem);

                    if (GuiPreferences.Instance.TrainType == TrainingType.TrainTestSplit)
                    {
                        GuiPreferences.Instance.setLog("Starting Training, splitting folds 90%/10%.");
                        libSVM_ExtendedProblem train_problem = new libSVM_ExtendedProblem();
                        libSVM_ExtendedProblem test_problem  = new libSVM_ExtendedProblem();
                        int[] test_indices = ChronologicalSplitTrainTest(TrainingTesting_SharedVariables._trainingProblem, ref train_problem, ref test_problem);
                        TrainingTesting_SharedVariables.training.TrainTestSplit(train_problem, test_problem, test_indices);
                        GuiPreferences.Instance.setLog("Finished Training.");
                    }
                    else if (GuiPreferences.Instance.TrainType == TrainingType.CrossValidation)
                    {
                        GuiPreferences.Instance.setLog("Starting Cross Validation, using " + GuiPreferences.Instance.NudCVFolds.ToString() + " folds.");
                        TrainingTesting_SharedVariables.training.TrainFolds((int)GuiPreferences.Instance.NudCVFolds, TrainingTesting_SharedVariables._trainingProblem, LibSVM_TrainTest.trainingType.cfv);
                        GuiPreferences.Instance.setLog("Finished Cross Validation.");
                    }
                    else if (GuiPreferences.Instance.TrainType == TrainingType.GridSearch)
                    {
                        GuiPreferences.Instance.setLog("Starting Grid Search, using " + GuiPreferences.Instance.NudGridFolds.ToString() + " folds.");
                        TrainingTesting_SharedVariables.training.TrainFolds((int)GuiPreferences.Instance.NudGridFolds, TrainingTesting_SharedVariables._trainingProblem, LibSVM_TrainTest.trainingType.bestTotal);
                        GuiPreferences.Instance.setLog("Finished Grid Search.");
                    }
                    else if (GuiPreferences.Instance.TrainType == TrainingType.RFE)
                    {
                        GuiPreferences.Instance.setLog("Not Implemented yet.");
                    }

                    ///copy model into main model variable
                    if (GuiPreferences.Instance.TrainType != TrainingType.CrossValidation)
                    {
                        //training.
                        Preferences.Instance.svmModel = (libSVM)Preferences.Instance.svmWrapper;
                        GuiPreferences.Instance.setLog("Model saved into memory");
                    }
                }
                else
                {
                    GuiPreferences.Instance.setLog("Training Failed: Original(unprocessed) or Final Problem(processed) Samples empty!");
                }
            }
            else
            {
                GuiPreferences.Instance.setLog("Training Failed: Original(unprocessed) or Final Problem(processed) null!");
            }
        }
Esempio n. 7
0
        /// <summary>
        /// WAS A BIG BUTTON: quickloads a range of commands to test the weka pipeline
        /// loads protocol and data, processes for SMO, trick, IG, etc.. has java/python intergration
        /// 1. Trick: QuickLoad, Export to Libsvm, separate to TRs files in libsvm,  convert TR-3 and TR-4 to arff, use TR4 + IG to get 1000 features, filter TR-3 based on features from TR-4, save result to libsvm format, train using LibSvm (grid?), save model, test on training data - must get 100%, display 1000 on viewport
        /// 2. No Trick: QuickLoad, Export to Libsvm, separate to TRs files in libsvm,  convert TR-3 to arff, filter TR-3 based on 1000 top IG, save result to libsvm format, train using LibSvm (grid?), save model, test on training data - must get 100%, display 1000 on viewport
        /// </summary>
        /// <param name="from"></param>
        /// <returns></returns>
        public bool QuickProcessWekaPipeline(int from)
        {
            // --- from this point the loading data phaze begins --- //

            // tirosh null movement

            /*
             * GuiPreferences.Instance.WorkDirectory = @"H:\My_Dropbox\VERE\MRI_data\Tirosh\20120508.Rapid+NullClass.day2\4\rtp\";
             * GuiPreferences.Instance.FileName = "tirosh-";
             * GuiPreferences.Instance.FileType = OriBrainLearnerCore.dataType.rawValue;
             * GuiPreferences.Instance.ProtocolFile = @"H:\My_Dropbox\VERE\MRI_data\Tirosh\20120705.NullClass1_zbaseline.prt";
             */

            // magali classification

            /*GuiPreferences.Instance.WorkDirectory = @"H:\My_Dropbox\VERE\Experiment1\Kozin_Magali\20121231.movement.3.imagery.1\18-classification.movement\rtp\";
             * GuiPreferences.Instance.FileName = "tirosh-";
             * GuiPreferences.Instance.FileType = OriBrainLearnerCore.dataType.rawValue;
             * GuiPreferences.Instance.ProtocolFile = @"H:\My_Dropbox\VERE\MRI_data\Tirosh\20113110.short.5th.exp.hands.legs.zscore.thought_LRF.prt";
             * */

            /// moshe sherf classification, 4 aggregated to test on 1.
            //GuiPreferences.Instance.WorkDirectory = @"H:\My_Dropbox\VERE\Experiment1\Sherf_Moshe\20121010.movement.1\1234-5\";
            //GuiPreferences.Instance.ProtocolFile = @"H:\My_Dropbox\VERE\Experiment1\Sherf_Moshe\20121010.movement.1\1234-5\20113110.short.5th.exp.hands.legs.zscore.thought_LRF.prt";

            string[] directoryList =
            {
                @"H:\My_Dropbox\VERE\Experiment1\Sherf_Moshe\20121010.movement.1\05_classification\rtp\",
                @"H:\My_Dropbox\VERE\Experiment1\Sherf_Moshe\20121010.movement.1\07_classification\rtp\",
                @"H:\My_Dropbox\VERE\Experiment1\Sherf_Moshe\20121010.movement.1\09_classification\rtp\",
                @"H:\My_Dropbox\VERE\Experiment1\Sherf_Moshe\20121010.movement.1\11_classification\rtp\"
            };
            GuiPreferences.Instance.ProtocolFile = @"H:\My_Dropbox\VERE\MRI_data\Tirosh\20113110.short.5th.exp.hands.legs.zscore.thought_LRF.prt";
            //GuiPreferences.Instance.WorkDirectory = @"H:\My_Dropbox\VERE\Experiment1\Sherf_Moshe\20121010.movement.1\15_classification\rtp\";

            GuiPreferences.Instance.FileName = "tirosh-";
            GuiPreferences.Instance.FileType = OriBrainLearnerCore.DataType.rawValue;



            //read prot file
            Preferences.Instance.prot = new ProtocolManager();

            double[][] topIGFeatures = {};
            foreach (string directory in directoryList)
            {
                GuiPreferences.Instance.WorkDirectory = directory;
                //delete all files that are going to be created, in order to prevent anomaly vectors.
                string[] deleteFiles =
                {
                    "TrainSet.libsvm",
                    "TrainSet_3th_vectors.libsvm",
                    "TrainSet_3th_vectors_scale_paramCS.libsvm",
                    "TrainSet_3th_vectors_scaledCS.libsvm",
                    "TrainSet_3th_vectors_scaledCS.libsvm.arff",
                    "TrainSet_3th_vectors_scaledCS_filteredIG.arff",
                    "TrainSet_3th_vectors_scaledCS_filteredIG.model",
                    "TrainSet_3th_vectors_scaledCS_filteredIG_indices.xml",
                    "TrainSet_4th_vectors.libsvm",
                    "TrainSet_4th_vectors_scale_paramCS.libsvm",
                    "TrainSet_4th_vectors_scaledCS.libsvm",
                    "TrainSet_4th_vectors_scaledCS.libsvm.arff"
                };

                foreach (string fileName in deleteFiles)
                {
                    FileDirectoryOperations.DeleteFile(GuiPreferences.Instance.WorkDirectory + fileName);
                }

                //get all files in the path with this extention
                GuiManager.getFilePaths("*.vdat");

                //update certain info
                GuiManager.updateFilePaths();

                //assigned after we know what to assign from the protocol
                //PublicMethods.setClassesLabels();
                GuiPreferences.Instance.CmbClass1Selected = 1; //left
                GuiPreferences.Instance.CmbClass2Selected = 2; //right

                //NEED TO ADD A VARIABLE FOR EVERY OPTION IN THE GUI. RAW VALUES. UNPROCESSED. MULTI CLASS. CROSS VALD, GRID, FOLDS, ETC...
                //and for every button a function!


                //for the training set
                GuiPreferences.Instance.FromTR = from; // 264;

                //for the test set
                //GuiPreferences.Instance.FromTR = 46;

                //GuiPreferences.Instance.ToTR = 100;// 264;


                //finally load
                TrainingTesting_SharedVariables.binary.loadRawData();

                topIGFeatures = new double[][] {};
                Instances data;
                //files are loaded,thresholded,vectorized,normalized. false means that IG and training are not done here.
                if (!Training_MultiRunProcessing.ProcessSingleRunOffline(ref topIGFeatures, Preferences.Instance.ProblemOriginal))
                {
                    GuiPreferences.Instance.setLog("Samples are empty");
                }
                //++grab findl vectors and concat them
                // grab min max values for saving the median.
            }

            //create a dir that holds the final DS in C:\
            GuiPreferences.Instance.WorkDirectory = @"C:\FinalData_" + DateTime.Now.ToLongTimeString().Replace(':', '-');
            GuiPreferences.Instance.setLog(@"Creating Final Directory in: " + GuiPreferences.Instance.WorkDirectory);
            FileDirectoryOperations.CreateDirectory(GuiPreferences.Instance.WorkDirectory);
            GuiPreferences.Instance.WorkDirectory += @"\";



            //concatenate libsvm normalized and vectorized files
            FileStream fileStream;
            FileStream outputFileStream = new FileStream(GuiPreferences.Instance.WorkDirectory + "TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scaledCS.libsvm", FileMode.CreateNew, FileAccess.Write);

            foreach (string directory in directoryList)
            {
                fileStream = new FileStream(directory + "TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scaledCS.libsvm", FileMode.Open, FileAccess.Read);
                Training_MultiRunProcessing.CopyStream(outputFileStream, fileStream);
                fileStream.Close();
            }
            outputFileStream.Close();

            //save concatenated tr3 to a file
            if (WekaCommonFileOperation.ConvertLIBSVM2ARFF(GuiPreferences.Instance.WorkDirectory + "TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scaledCS.libsvm", 204800))
            {
                GuiPreferences.Instance.setLog("Converted to ARFF: TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scaledCS.arff");
            }



            double[][] feature_max = new double[directoryList.Length][];
            double[][] feature_min = new double[directoryList.Length][];
            int        i           = 0;
            int        max_index   = -1;

            foreach (string directory in directoryList)
            {
                TrainingTesting_SharedVariables._svmscaleTraining.getConfigFileMinMaxValues(
                    directory + "TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scale_paramCS.libsvm",
                    ref feature_max[i], ref feature_min[i], ref max_index);
                i++;
            }


            //calculate Mean + save new min/max param to C:\
            double[] finalFeature_max = new double[feature_max[0].Length];
            double[] finalFeature_min = new double[feature_max[0].Length];

            //create a list with enough values for the runs, in order to calculate the median
            var values_max = new List <double>(feature_max.Length);
            var values_min = new List <double>(feature_max.Length);

            for (int k = 0; k < feature_max.Length; k++)
            {
                //init zeros
                values_max.Add(0);
                values_min.Add(0);
            }

            for (int j = 0; j < feature_max[0].Length; j++)
            {
                for (int k = 0; k < feature_max.Length; k++)
                {
                    values_max[k] = feature_max[k][j];
                    values_min[k] = feature_min[k][j];
                }
                //finalFeature_max[j] = GetMedian(values_max);
                //finalFeature_min[j] = GetMedian(values_min);
                finalFeature_max[j] = values_max.Max();
                finalFeature_min[j] = values_min.Min();
            }

            TrainingTesting_SharedVariables._svmscaleTraining.saveConfigMinMax_CSharp(GuiPreferences.Instance.WorkDirectory + "TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scale_paramCS.libsvm", finalFeature_min, finalFeature_max, 204801, 0.0f, 1.0f);

            //todo check max index in file,
            //todo check if needs to remove 204801 from it so it doesnt effect the class.

            double[][] FinaltopIGFeatures = { };
            Instances  finalData          = Training_MultiRunProcessing.ConcatenationPipeLine("TrainSet_" + GuiPreferences.Instance.NudClassifyUsingTR.ToString() + "th_vectors_scaledCS.libsvm.arff", "TrainSet_4th_vectors_scaledCS.libsvm.arff");

            WekaTrainingMethods.TrainSMO(finalData);
            //save median param file

            //display top IG on dicom view
            string dicomDir = directoryList[0];

            dicomDir = dicomDir.Substring(0, dicomDir.Length - 4) + @"master\";
            string[] files     = System.IO.Directory.GetFiles(dicomDir, "*.dcm");
            string   firstFile = files[0].Substring(files[0].LastIndexOf(@"\") + 1);


            bool thresholdOrVoxelAmount;

            if (GuiPreferences.Instance.IgSelectionType == IGType.Threshold)
            {
                thresholdOrVoxelAmount = true;
            }
            else
            {
                thresholdOrVoxelAmount = false;
            }

            //NOTE final top may be empty, please remember that the IG are not at preferences.instance.attsel.selectedattributes or rankedattributes.
            Form plotForm = new DicomImageViewer.MainForm(dicomDir + firstFile, firstFile, FinaltopIGFeatures,
                                                          Convert.ToDouble(GuiPreferences.Instance.NudIGThreshold),
                                                          Convert.ToInt32(GuiPreferences.Instance.NudIGVoxelAmount),
                                                          thresholdOrVoxelAmount,
                                                          GuiPreferences.Instance.WorkDirectory + "brain");

            plotForm.StartPosition = FormStartPosition.CenterParent;
            plotForm.ShowDialog();
            plotForm.Close();

            return(true);
        }