Esempio n. 1
0
        private void  SaveImages()
        {
            PicesDataBase.ThreadInit();
            savingThreadRunning   = true;
            imagesSaved           = 0;
            imagesFailedToExtract = 0;

            if (images != null)
            {
                SaveGivenImageList();
            }

            else if (group != null)
            {
                SaveImagesByGroup();
            }

            else
            {
                SaveImagesBySipperFiles();
            }

            savingThreadCompleted = true;
            savingThreadRunning   = false;
            PicesDataBase.ThreadEnd();
        } /* SaveImages */
        } /* StartProcessingThread */

        private void  Processing()
        {
            processingThreadRunning = true;
            PicesDataBase.ThreadInit();

            RunLogAddMsg("Retrieving list of SIPPER Files\n");

            dbConn.CancelFlag = false;

            String[] sipperFileNames = null;
            if (String.IsNullOrEmpty(sipperFileName))
            {
                sipperFileNames = dbConn.SipperFileGetList(cruise, station, deployment);
            }
            else
            {
                sipperFileNames    = new String[1];
                sipperFileNames[0] = sipperFileName;
            }

            if (sipperFileNames == null)
            {
                RunLogAddMsg("\n\n  ***ERROR***   Not SIPPETR files names were returned.\n\n");
            }
            else
            {
                ProcessSipperFiles(sipperFileNames);
            }

            PicesDataBase.ThreadEnd();
            processingThreadCompleted = true;
            processingThreadRunning   = false;
        } /* Processing */
Esempio n. 3
0
        } /* LoadImagesForOneSipperFile */

        private void  LoadImagesThread()
        {
            blocker.StartBlock();
            loaderThreadRuning = true;
            PicesDataBase.ThreadInit();
            blocker.EndBlock();

            dbConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(runLog);

            allSipperFiles = (String.IsNullOrEmpty(cruiseName) &&
                              String.IsNullOrEmpty(stationName) &&
                              String.IsNullOrEmpty(deploymentNum) &&
                              String.IsNullOrEmpty(sipperFileName)
                              );

            if (!String.IsNullOrEmpty(sipperFileName))
            {
                sipperFileNames    = new String[1];
                sipperFileNames[0] = sipperFileName;
            }
            else
            {
                sipperFileNames = dbConn.SipperFileGetList(cruiseName, stationName, deploymentNum);
            }

            if (selectedImageGroup != null)
            {
                LoadImagesForSelectedGroup();
            }
            else
            {
                if (sipperFileNames != null)
                {
                    for (curSipperFileIdx = 0; (curSipperFileIdx < sipperFileNames.Length) && (!cancelRequested); curSipperFileIdx++)
                    {
                        LoadImagesForOneSipperFile(sipperFileNames[curSipperFileIdx]);
                    }
                }
            }

            blocker.StartBlock();
            if (!cancelRequested)
            {
                doneLoading = true;
            }
            loaderThreadRuning = false;
            blocker.EndBlock();

            if (completionEvent != null)
            {
                completionEvent(this);
            }

            dbConn.Close();
            dbConn = null;
            GC.Collect();

            PicesDataBase.ThreadEnd();
        } /* LoadImagesTread */
Esempio n. 4
0
        } /* ImageEntry */



        private void  HarvestImages()
        {
            PicesDataBase.ThreadInit();
            harvestingRunning   = true;
            harvestingCompleted = false;

            PicesDataBase threadConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(runLog);

            List <ImageEntry> allImages = null;

            List <ImageEntry>[] imagesByDepth    = null;
            double[]            depthTimeProfile = null;
            double[]            fracPartByDepth  = null;
            List <ImageEntry>   randDistByDepth  = null;

            //if  (!cbOilDeployments.Checked)
            allImages = LoadAllImages(threadConn);
            //else
            //  allImages = LoadOilDeploymentImages();

            if (!cancelHarvesting)
            {
                if (weight)
                {
                    depthTimeProfile = LoadDepthTimeProfile(threadConn);
                    if (!cancelHarvesting)
                    {
                        imagesByDepth   = DeriveImagesByDepth(allImages, depthTimeProfile.Length);
                        fracPartByDepth = DeriveFracPartByDepth(depthTimeProfile, imagesByDepth);

                        randDistByDepth = DeriveRandomDistByDepth(imagesByDepth, fracPartByDepth);
                        harvestedImages = GetDatabaseImageRecords(threadConn, randDistByDepth);
                        UpdateImageGroupTables(threadConn, harvestedImages);

                        RunLogAddMsg("\n" + "Harvesting Completed Successfully." + "\n");
                    }
                }
                else
                {
                    harvestedImages = DeriveRandomDistribution(threadConn, allImages);
                    if (!cancelHarvesting)
                    {
                        UpdateImageGroupTables(threadConn, harvestedImages);
                    }
                }

                if (!cancelHarvesting)
                {
                    harvestingCompleted = true;
                }
            }

            threadConn.Close();
            threadConn = null;
            GC.Collect();

            PicesDataBase.ThreadEnd();
            harvestingRunning = false;
        } /* HarvestImages */
Esempio n. 5
0
        } /* PerformUpdate */

        private void  UpdateProcess()
        {
            PicesDataBase.ThreadInit();
            updateRunning = true;

            PicesRunLog   updRunLog = new PicesRunLog();
            PicesDataBase updDbConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(updRunLog);

            sipperFilesTotal   = 0;
            sipperFilesUpdated = 0;

            if (addingNewClass || deletingExistingClass || (mergeClass != null))
            {
                sipperFiles = updDbConn.SipperFileLoad("", "", "");
                if (sipperFiles == null)
                {
                    RunLogAddMsg("Could not retrieve list of SIPPER files update failed." + "\n");
                    updateRunning = false;
                    return;
                }
                sipperFilesTotal = sipperFiles.Count;
            }

            if (addingNewClass)
            {
                UpdateProcessAdd(updDbConn);
            }

            else if (deletingExistingClass)
            {
                UpdateProcessDelete(updDbConn);
            }

            else if (mergeClass != null)
            {
                UpdateProcessMerge(updDbConn);
            }

            else if (nameChangeHasOccured)
            {
                UpdateProcessNameChange(updDbConn);
            }

            else
            {
                UpdateDataFields(updDbConn);
            }

            updDbConn.Close();
            updDbConn = null;
            GC.Collect();

            PicesDataBase.ThreadEnd();
            updateRunning = false;
        } /* UpdateProcess */
Esempio n. 6
0
        private void  BackGroundThread()
        {
            PicesDataBase.ThreadInit();
            backGroundThreadRunning  = true;
            imagesLoaded             = 0;
            imagesFilteredOut        = 0;
            imagesMissingFeatureData = 0;

            if (group != null)
            {
                featureData = SaveFeatureDataByGroup();
            }
            else
            {
                featureData = SaveFeatureDataBySipperFiles();
            }

            if (!cancelRequested)
            {
                backGroundStatus = "Saving Feature File";
                String pathName = OSservices.GetPathPartOfFile(featureFileName);
                if (!String.IsNullOrEmpty(pathName))
                {
                    OSservices.CreateDirectoryPath(pathName);
                }

                if (IncludeSampleImages.Checked)
                {
                    SaveSampleImages(featureData);
                }

                driver = new PicesFeatureFileIO(fileFormat);
                if (driver != null)
                {
                    if (NormalizeData.Checked)
                    {
                        driver.SaveFeatureFileNormalized(featureFileName, featureData, runLog);
                    }
                    else
                    {
                        driver.SaveFeatureFile(featureFileName, featureData, runLog);
                    }
                }
            }

            PicesDataBase.ThreadEnd();
            backgroundThreadCompleted = true;
            backGroundThreadRunning   = false;
        } /* BackGroundThread */
Esempio n. 7
0
        } /* AddCruiseToSeries */

        /// <summary>
        /// This next method will be ran as a separate thread; it is responsible for collecting all the data needed to generate the plot.
        /// </summary>
        private void  BuildPlotData()
        {
            if (buildPlotDataRunning)
            {
                return;
            }

            PicesDataBase.ThreadInit();

            threadConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(runLog);

            buildPlotDataRunning = true;

            timeInterval = (int)TimeInterval.Value;

            series = new List <DataSeriesToPlot> ();
            foreach (PlotRequest pr in plotRequests)
            {
                if (cancelRequested)
                {
                    break;
                }
                AddDeploymentToSeries(pr.deployment, pr.color);
            }

            if (plotCruise && (!cancelRequested))
            {
                AddCruiseToSeries();
            }

            threadConn.Close();
            threadConn = null;
            GC.Collect();
            PicesDataBase.ThreadEnd();

            if (cancelRequested)
            {
                statusMsgs.AddMsg("Plotting of data CANCELLED!!!");
            }
            else
            {
                statusMsgs.AddMsg("Building of plot data completed !!!");
            }

            buildPlotDataRunning = false;
            buildPlotDataDone    = true;
        } /* BuildPlotData */
        } /* AddDeploymentToSeries */

        /// <summary>
        /// This method will be ran as a separate thread; it is responsible for collecting all the data needed to generate the plot.
        /// </summary>
        private void  BuildPlotData()
        {
            if (buildPlotDataRunning)
            {
                return;
            }

            PicesDataBase.ThreadInit();

            threadConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(runLog);

            buildPlotDataRunning = true;

            depthIncrement = (int)DepthIncr.Value;
            classToPlot    = PicesClassList.GetUniqueClass(ClassToPlot.Text, "");

            series = new List <DataSeriesToPlot> ();

            foreach (PlotRequest pr in plotRequests)
            {
                if (cancelRequested)
                {
                    break;
                }
                AddDeploymentToSeries(pr.deployment);
            }

            threadConn.Close();
            threadConn = null;
            GC.Collect();
            PicesDataBase.ThreadEnd();

            if (cancelRequested)
            {
                statusMsgs.AddMsg("Plotting of data CANCELLED!!!");
            }
            else
            {
                statusMsgs.AddMsg("Building of plot data completed !!!");
            }

            buildPlotDataRunning = false;
            buildPlotDataDone    = true;
        } /* BuildPlotData */
Esempio n. 9
0
        private void  LoadTrainingModel()
        {
            trainingModelRunning = true;
            PicesDataBase.ThreadInit();

            if (trainingModel != null)
            {
                trainingModel = null;
                GC.Collect();
            }

            trainingModel            = new TrainingModel2(runLog, trainingModelName);
            trainingModelLogFileName = trainingModel.RunLogFileName;
            trainingModel.AttachMsgQueueToRunLog(logMessages);

            if (forceRebuild)
            {
                trainingModel.LoadTrainigLibrary(true); // true = Force Rebuild of Model
            }
            else
            {
                trainingModel.LoadExistingModelOtherwiseBuild(null);
            }

            trainingModel.AttachMsgQueueToRunLog(null);

            if (trainingModel.Valid)
            {
                validModel = true;
            }
            else
            {
                List <String> errMsgs = trainingModel.ErrorMsgs;
                if (errMsgs != null)
                {
                    logMessages.AddMsgs(errMsgs);
                }
                validModel    = false;
                trainingModel = null;
            }

            PicesDataBase.ThreadEnd();
            trainingModelRunning = false;
        } /* LoadTrainingModel */
Esempio n. 10
0
        } /* GetNextFeatureVector*/

        private void  ThreadProcess()
        {
            threadRunning = true;
            PicesDataBase.ThreadInit();


            if ((classifier == null) && (!String.IsNullOrEmpty(classifierName)))
            {
                PicesRunLog log = new PicesRunLog();
                classifier = new TrainingModel2(log, classifierName);
                classifier.LoadExistingModelOtherwiseBuild(null);
                classifierWeOwnIt = true;
            }

            while (!cancelFlag)
            {
                PicesFeatureVector fv = GetNextFeatureVector();
                if (fv != null)
                {
                    ProcessOneFeatureVector(fv);
                }
                else
                {
                    if (threadTerminate)
                    {
                        break;
                    }
                    Thread.Sleep(10);
                }
            }

            if (dbConn != null)
            {
                dbConn.Close();
                dbConn = null;
                GC.Collect();
            }

            PicesDataBase.ThreadEnd();

            threadRunning = false;
            threadDone    = true;
        } /* ThreadProcess */
Esempio n. 11
0
        } /* ToCmdLine */

        private void  ImportValidatedClassAssignments()
        {
            importingRunning   = true;
            importingCompleted = false;

            PicesDataBase.ThreadInit();

            PicesRunLog   runLog     = new PicesRunLog();
            PicesDataBase threadConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(runLog);

            extractionLogEntryId = threadConn.LogEntriesProcessStart("IV",
                                                                     "ImportValidatedClass",
                                                                     ExecutableDateTimeStamp(),
                                                                     ToCmdLine(),
                                                                     OSservices.GetRootName(sipperFileName)
                                                                     );
            totalImagesUpdated = 0;

            ImportValidatedClassAssignmentsDir(threadConn, sourceDirectory);
            if (cancelImporting)
            {
                RunLogAddMsg("\n" + "Importing Assignments has been Canceled." + "\n\n");
            }
            else
            {
                RunLogAddMsg("\n\n" + "Total Images[" + totalImagesUpdated.ToString("###,##0") + "\n\n");
            }

            threadConn.Close();
            threadConn = null;
            GC.Collect();

            PicesDataBase.ThreadEnd();

            importingRunning   = false;
            importingCompleted = true;
        } /* ImportValidatedClassAssignments */
        } /* ToCmdLine */

        private void  ImportImagesIntoDeploymentProcess()
        {
            importingRunning   = true;
            importingCompleted = false;

            PicesDataBase.ThreadInit();

            PicesRunLog   runLog     = new PicesRunLog();
            PicesDataBase threadConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(runLog);

            extractionLogEntryId = threadConn.LogEntriesProcessStart("II",
                                                                     "ImportImagesIntoDeployment",
                                                                     ExecutableDateTimeStamp(),
                                                                     ToCmdLine(),
                                                                     ""
                                                                     );
            totalImagesUpdated = 0;

            ImportImagesFromDirectoryStructure(threadConn, sourceDirectory);
            if (cancelImporting)
            {
                runLog.Writeln("\n" + "Importing Images has been Canceled." + "\n\n");
            }
            else
            {
                runLog.Writeln("\n\n" + "Total Images[" + totalImagesUpdated.ToString("###,##0") + "\n\n");
            }

            threadConn.Close();
            threadConn = null;
            GC.Collect();

            PicesDataBase.ThreadEnd();

            importingRunning   = false;
            importingCompleted = true;
        } /* ImportImagesIntoDeploymentProcess */
Esempio n. 13
0
        private void  BackGroundThread()
        {
            PicesDataBase.ThreadInit();
            backGroundThreadRunning = true;
            imagesLoaded            = 0;

            if (group != null)
            {
                planktonData = RetrievePlanktonDataByGroup();
            }
            else
            {
                planktonData = RetrievePlanktonDataBySipperFiles();
            }

            if (!cancelRequested)
            {
                backGroundStatus = "Saving Data File";
                String pathName = OSservices.GetPathPartOfFile(dataFileName);
                if (!String.IsNullOrEmpty(pathName))
                {
                    OSservices.CreateDirectoryPath(pathName);
                }

                if (IncludeSampleImages.Checked)
                {
                    SaveSampleImages(planktonData);
                }

                SavePlanktonDataFile(planktonData);
            }

            PicesDataBase.ThreadEnd();
            backgroundThreadCompleted = true;
            backGroundThreadRunning   = false;
        } /* BackGroundThread */
Esempio n. 14
0
        } /* SizeDistributionForAClass */

        /// <summary>
        /// This method will be ran as a separate thread; it is responsible for collecting all the data needed to generate the plot.
        /// </summary>
        private void  BuildPlotData()
        {
            if (buildPlotDataRunning)
            {
                return;
            }

            PicesDataBase.ThreadInit();

            threadConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(runLog);

            buildPlotDataRunning = true;

            classToPlot = PicesClassList.GetUniqueClass(ClassToPlot.Text, "");

            sbyte ch = (sbyte)statistic;

            downCast = null;
            upCast   = null;

            goalie.StartBlock();

            SizeDistributionForAClass(classToPlot,
                                      IncludeSubClasses.Checked,
                                      ref downCast,
                                      ref upCast
                                      );

            if (cast == "Down")
            {
                bucketsDisplayed = downCast;
            }

            else if (cast == "Up")
            {
                bucketsDisplayed = upCast;
            }

            else
            {
                bucketsDisplayed = new PicesImageSizeDistribution(downCast);
                bucketsDisplayed.AddIn(upCast, runLog);
            }

            integratedDensityDistribution = bucketsDisplayed.IntegratedDensityDistribution();

            threadConn.Close();
            threadConn = null;
            GC.Collect();
            PicesDataBase.ThreadEnd();

            goalie.EndBlock();

            if (cancelRequested)
            {
                statusMsgs.AddMsg("Plotting of data CANCELLED!!!");
            }
            else
            {
                statusMsgs.AddMsg("Building of plot data completed !!!");
            }

            buildPlotDataRunning = false;
            buildPlotDataDone    = true;
        } /* BuildPlotData */
Esempio n. 15
0
        } /* StartTheHarvestingProcedure */

        private void  ImportAssignments()
        {
            PicesDataBase.ThreadInit();
            backGroundRunning   = true;
            backGroundCompleted = false;

            PicesDataBase threadConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(runLog);

            RunLogAddMsg("Source Directory [" + sourceDirectory + "]");
            RunLogAddMsg("Group Name       [" + groupName + "]");
            RunLogAddMsg("Start Date/Time  [" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss") + "]");
            RunLogAddMsg("Active DataBase  [" + threadConn.Server.Description + "]");

            importedFileNames = new List <String> ();

            group = threadConn.ImageGroupLoad(groupName);
            if (group != null)
            {
                // Group already exists;  will delete it.
                RunLogAddMsg("\n" + "Group[" + groupName + "] already exists.   Will remove and replace with new one." + "\n");

                threadConn.ImageGroupDelete(group.ImageGroupId);
            }

            group = new PicesDataBaseImageGroup(-1, groupName, groupDesc, 0);
            threadConn.ImageGroupInsert(group);

            ImportAssignmentsImportDir(sourceDirectory);
            if (cancelBackGround)
            {
                RunLogAddMsg("\n" + "Importing Assignments has been Canceled." + "\n");
            }
            else
            {
                RunLogAddMsg("\n\n" + "Total Images[" + importedFileNames.Count.ToString("###,##0") + "\n");

                RunLogAddMsg("Writing to DataBase Now" + "\n");

                imageFileErrorList = new List <string>();

                int numInserted = 0;

                for (int x = 0; (x < importedFileNames.Count) && (!cancelBackGround); x++)
                {
                    PicesDataBaseImageGroupEntry ige = new PicesDataBaseImageGroupEntry();
                    ige.ImageGroupId  = group.ImageGroupId;
                    ige.ImageFileName = importedFileNames[x];
                    threadConn.ImageGroupEntriesInsert(ige);
                    if (!threadConn.Valid())
                    {
                        imageFileErrorList.Add(importedFileNames[x]);
                        RunLogAddMsg("Image[" + importedFileNames[x] + "]  ***ERROR***");
                    }
                    else
                    {
                        numInserted++;
                    }

                    if ((x % 100) == 0)
                    {
                        RunLogAddMsg("Images Assigned[" + numInserted + "]  Failures[" + imageFileErrorList.Count + "]");
                    }
                }
            }

            threadConn.Close();
            threadConn = null;
            GC.Collect();
            PicesDataBase.ThreadEnd();

            backGroundRunning   = false;
            backGroundCompleted = true;
        } /* ImportAssignments */
        } /* DialogTimer_Tick */

        private void  CreateFeatureDataFileThread()
        {
            savingThreadRunning = true;

            PicesDataBase.ThreadInit();

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Started");

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Loading feature data for Model[" + modelName + "]");
            config = new PicesTrainingConfigManaged(modelName, runLog);
            if (!config.Valid())
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Model[" + modelName + "]  is invalid.");
                savingThreadFailed  = true;
                savingThreadDone    = true;
                savingThreadRunning = false;
                return;
            }


            bool changesMade            = false;
            PicesFeatureVectorList data = config.LoadFeatureDataFromTrainingLibraries(changesMade);

            if (data == null)
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Model[" + modelName + "]  Count not load Feature Data.");
                savingThreadFailed  = true;
                savingThreadDone    = true;
                savingThreadRunning = false;
                return;
            }

            if (savingThreadCancelReq)
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Process Canceled.");
                savingThreadDone    = true;
                savingThreadRunning = false;
                return;
            }

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Feature Data Loaded.");

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Checking that Depth Data Exists.");
            MakeSureDepthFieldIsIncluded(data);

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Splitting into separate files.");

            if (!savingThreadCancelReq)
            {
                data.Save(destinationFileName, FileFormat.Text, runLog);
            }

            AddMsgToDialogMsgQueue("\n\n");
            if (savingThreadCancelReq)
            {
                AddMsgToDialogMsgQueue("Process    *** CANCELED ***.");
            }
            else
            {
                AddMsgToDialogMsgQueue("Process Completed.");
            }

            PicesDataBase.ThreadEnd();

            savingThreadDone    = true;
            savingThreadRunning = false;
        } /* CreateFeatureDataFileThread */
Esempio n. 17
0
        } /* StartTheBackGroundProcedure */

        private void  BackGroundProcess()
        {
            backGroundRunning = true;

            PicesDataBase.ThreadInit();


            OSservices.CreateDirectoryPath(reportFileDir);

            DateTime n           = DateTime.Now;
            String   logFileName = OSservices.AddSlash(PicesSipperVariables.TempDirectory()) + "InstrumentDataByDeployment_Log_" +
                                   n.Year.ToString("0000") + "-" + n.Month.ToString("00") + "-" + n.Day.ToString("00") + "_" +
                                   n.Hour.ToString("00") + "-" + n.Minute.ToString("00") + "-" + n.Hour.ToString("00") +
                                   ".txt";

            backGroundLog = new PicesRunLog(logFileName);
            PicesDataBase threadConn = PicesDataBase.GetGlobalDatabaseManagerNewInstance(backGroundLog);

            RunLogAddMsg("Cruise           [" + cruiseName + "]" + "\n");
            RunLogAddMsg("Station          [" + stationName + "]" + "\n");
            RunLogAddMsg("Deployment       [" + deploymentNum + "]" + "\n");
            RunLogAddMsg("Start Date/Time  [" + DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss") + "]" + "\n");
            RunLogAddMsg("Active DataBase  [" + threadConn.Server.Description + "]" + "\n");

            PicesSipperDeploymentList deployments = null;

            if (!String.IsNullOrEmpty(deploymentNum))
            {
                PicesSipperDeployment d = threadConn.SipperDeploymentLoad(cruiseName, stationName, deploymentNum);
                if (d != null)
                {
                    deployments = new PicesSipperDeploymentList();
                    deployments.Add(d);
                }
            }
            else
            {
                deployments = threadConn.SipperDeploymentLoad(cruiseName, stationName);
            }

            if (deployments == null)
            {
                RunLogAddMsg("\n\nNo Deployments for Specified Criteria!\n\n");
                return;
            }

            int reportNum = 0;

            foreach (PicesSipperDeployment d in deployments)
            {
                if (cancelBackGround)
                {
                    break;
                }

                reportNum++;

                RunLogAddMsg("Generating Report " + reportNum.ToString() + " of " + deployments.Count.ToString());

                SummarizeInstrumentDataForADeployment(threadConn, d.CruiseName, d.StationName, d.DeploymentNum);
            }

            threadConn.Close();
            threadConn = null;
            GC.Collect();

            PicesDataBase.ThreadEnd();

            backGroundRunning = false;
        } /* BackGroundProcess */
        } /* DialogTimer_Tick */

        private void  CreateTuningFeatureFiles()
        {
            tuningThreadRunning = true;

            PicesDataBase.ThreadInit();


            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Started");

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Creating Destination Directory");
            String destinationDirectory = OSservices.AddSlash(DestinationDirectory.Text) + OSservices.GetRootName(modelName);

            OSservices.CreateDirectoryPath(destinationDirectory);

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Loading feature data for Model[" + modelName + "]");
            config = new PicesTrainingConfigManaged(modelName, runLog);
            if (!config.Valid())
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Model[" + modelName + "]  is invalid.");
                tuningThreadFailed  = true;
                tuningThreadDone    = true;
                tuningThreadRunning = false;
                return;
            }


            bool changesMade            = false;
            PicesFeatureVectorList data = config.LoadFeatureDataFromTrainingLibraries(changesMade);

            if (data == null)
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Model[" + modelName + "]  Could not load Feature Data.");
                tuningThreadFailed  = true;
                tuningThreadDone    = true;
                tuningThreadRunning = false;
                return;
            }

            if (tuningThreadCancelReq)
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Process Canceled.");
                tuningThreadDone    = true;
                tuningThreadRunning = false;
                return;
            }

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Feature Data Loaded.");

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Checking that Depth Data Exists.");
            MakeSureDepthFieldIsIncluded(data);

            AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Splitting into separate files.");

            PicesFeatureVectorList trainData      = new PicesFeatureVectorList();
            PicesFeatureVectorList testData       = new PicesFeatureVectorList();
            PicesFeatureVectorList validationData = new PicesFeatureVectorList();

            PicesClassList classes = data.ExtractListOfClasses();

            if (classes == null)
            {
                AddMsgToDialogMsgQueue("\n\n");
                AddMsgToDialogMsgQueue("Could not extract list of classes from data.");
                tuningThreadFailed  = true;
                tuningThreadDone    = true;
                tuningThreadRunning = false;
                return;
            }


            foreach (PicesClass c in classes)
            {
                int trainCount      = 0;
                int testCount       = 0;
                int validationCount = 0;

                PicesFeatureVectorList examplesForClass = data.ExtractExamplesForAGivenClass(c);
                int n = examplesForClass.Count;

                AddMsgToDialogMsgQueue(DateTime.Now.ToShortTimeString() + " Processing Class[" + c.Name + "]  Count[" + n + "]");

                trainCount = (int)((float)n * trainingDataPercentage / 100.0f + 0.5f);
                if (trainingDataMaxImagesPerClass > 0)
                {
                    if (trainCount > trainingDataMaxImagesPerClass)
                    {
                        trainCount = trainingDataMaxImagesPerClass;
                    }
                }

                if (validationDataPercentage == 0.0f)
                {
                    testCount = n - trainCount;
                }
                else
                {
                    testCount = (int)((float)n * testDataPercentage / 100.0f + 0.5f);
                }

                validationCount = n - (trainCount + testCount);

                examplesForClass.RandomizeOrder();

                int validationStart = trainCount + testCount;
                int testStart       = trainCount;

                for (int idx = 0; idx < n; idx++)
                {
                    PicesFeatureVector fv = examplesForClass[idx];
                    if (idx >= validationStart)
                    {
                        validationData.Add(fv);
                    }

                    else if (idx >= testStart)
                    {
                        testData.Add(fv);
                    }

                    else
                    {
                        trainData.Add(fv);
                    }
                }

                if (tuningThreadCancelReq)
                {
                    AddMsgToDialogMsgQueue("\n\n");
                    AddMsgToDialogMsgQueue("Process Canceled.");
                    break;
                }
            }

            if (!tuningThreadCancelReq)
            {
                AddMsgToDialogMsgQueue("\n\n" + DateTime.Now.ToShortTimeString() + " Stratifying Data");

                PicesFeatureVectorList trainDataStratified      = trainData.StratifyAmoungstClasses(numOfFolds);
                PicesFeatureVectorList testDataStratified       = testData.StratifyAmoungstClasses(numOfFolds);
                PicesFeatureVectorList validationDataStratified = validationData.StratifyAmoungstClasses(numOfFolds);

                String rootName = OSservices.AddSlash(destinationDirectory) + OSservices.GetRootName(modelName);


                if ((trainDataStratified.Count > 0) && (!tuningThreadCancelReq))
                {
                    trainDataFileName = rootName + "_Train.data";
                    AddMsgToDialogMsgQueue("Saving Training Data[" + trainDataFileName + "]");
                    trainDataStratified.Save(trainDataFileName, "", runLog);
                }

                if ((testDataStratified.Count > 0) && (!tuningThreadCancelReq))
                {
                    testDataFileName = rootName + "_Test.data";
                    AddMsgToDialogMsgQueue("Saving Test Data[" + testDataFileName + "]");
                    testDataStratified.Save(testDataFileName, "", runLog);
                }

                if ((validationDataStratified.Count > 0) && (!tuningThreadCancelReq))
                {
                    validationDataFileName = rootName + "_Validation.data";
                    AddMsgToDialogMsgQueue("Saving Validation Data[" + validationDataFileName + "]");
                    validationDataStratified.Save(validationDataFileName, "", runLog);
                }
            }

            AddMsgToDialogMsgQueue("\n\n");
            if (tuningThreadCancelReq)
            {
                AddMsgToDialogMsgQueue("Process    *** CANCELED ***.");
            }
            else
            {
                AddMsgToDialogMsgQueue("Process Completed.");
            }

            PicesDataBase.ThreadEnd();

            tuningThreadDone    = true;
            tuningThreadRunning = false;
        } /* CreateTuningFeatureFiles */