Beispiel #1
0
        private void SaveButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.CheckPathExists = true;
            sfd.Filter          = "txt file (*.txt)|*.txt|html (*.html)|*.html";
            sfd.Title           = "Save Confusion Matrix";
            sfd.DefaultExt      = "txt";
            sfd.AddExtension    = true;
            sfd.FileName        = OSservices.GetRootName(trainingModel.ConfigFileName);
            DialogResult dr = sfd.ShowDialog(this);

            if (dr == DialogResult.OK)
            {
                String       extension = OSservices.GetFileExtension(sfd.FileName).ToUpper();
                StreamWriter sr        = null;
                try  { sr = new StreamWriter(sfd.FileName); } catch (Exception) { sr = null; }
                if ((extension == @"HTML") || (extension == @"HTM"))
                {
                    SaveAsHTML(sr);
                }
                else
                {
                    SaveAsText(sr);
                }

                sr.Close();
                sr.Dispose();
                sr = null;
            }
        }
        } /* ImportAssignments */

        private void  ImportAssignmentsImportDir(String dirName)
        {
            String[] fileNames = null;
            try  { fileNames = Directory.GetFiles(dirName); }
            catch (Exception e)
            {
                RunLogAddMsg("\n" + "Error retrieving file info for Directory[" + dirName + "]");
                RunLogAddMsg("Exception[" + e.ToString() + "]" + "\n");
                fileNames = null;
            }

            if (fileNames != null)
            {
                int numThisDir = 0;
                foreach (String fn in fileNames)
                {
                    String ext = OSservices.GetFileExtension(fn).ToLower();
                    if (ext == "bmp")
                    {
                        importedFileNames.Add(OSservices.GetRootName(fn));
                        numThisDir++;
                    }
                }

                RunLogAddMsg("Dir[" + dirName + "]  FilesFound[" + numThisDir.ToString("###,##0") + "]");
            }

            if (!cancelBackGround)
            {
                String[] directories = null;
                try { directories = Directory.GetDirectories(dirName); }
                catch (Exception) { directories = null; }

                if (directories != null)
                {
                    foreach (String subDir in directories)
                    {
                        if ((subDir == ".") || (subDir == ".."))
                        {
                            continue;
                        }
                        ImportAssignmentsImportDir(subDir);
                        if (cancelBackGround)
                        {
                            break;
                        }
                    }
                }
            }
        } /* ImportAssignmentsImportDir */
        } /* ProcessDirectory */

        private void  ProcessOneImage(String fullImageFileName)
        {
            String             rootName = OSservices.GetRootName(fullImageFileName);
            PicesDataBaseImage i        = dbConn.ImageLoad(rootName);

            if (i != null)
            {
                runStatsImagesInDataBase++;
                return;
            }


            String sipperFileName = null;
            uint   scanLine       = 0;
            uint   scanCol        = 0;

            PicesFeatureVector.ParseImageFileName(fullImageFileName, ref sipperFileName, ref scanLine, ref scanCol);

            if (String.IsNullOrEmpty(sipperFileName) || (scanLine < 1))
            {
                // We are not going to be able to locate this image in the Database.
                runStatsImagesNotInDataBase++;
                reconcilingRunLog.Writeln(fullImageFileName + "\t" + "Name format will not let me derive needed info.");
                return;
            }

            String sqlStr = "call  ImagesLocateClosestImage(" + "\"" + sipperFileName + "\"" + ", " + scanLine.ToString() + ", " + scanCol.ToString() + ")";

            String[][] results = dbConn.QueryStatement(sqlStr, null);
            if ((results == null) || (results.Length < 1))
            {
                runStatsImagesNotInDataBase++;
                reconcilingRunLog.Writeln(fullImageFileName + "\t" + "Could not find an appropriate image in the Database");

                RemoveImage(fullImageFileName);
                return;
            }

            String correctImageFileName = results[0][1];
            String newFullName          = OSservices.AddSlash(OSservices.GetPathPartOfFile(fullImageFileName)) +
                                          correctImageFileName + "." +
                                          OSservices.GetFileExtension(fullImageFileName);

            OSservices.RenameFile(fullImageFileName, newFullName);
            runStatsImagesFixed++;
        } /* ProcessOneImage */
        } /* ImportImage */

        private String[]  FilterListForImageFiles(String[]  fileNames)
        {
            List <String> results = new List <string> ();

            foreach (String s in  fileNames)
            {
                String ext = OSservices.GetFileExtension(s);
                if (ext.Equals("bmp", StringComparison.InvariantCultureIgnoreCase) ||
                    ext.Equals("jpg", StringComparison.InvariantCultureIgnoreCase)
                    )
                {
                    results.Add(s);
                }
            }
            if (results.Count < 1)
            {
                return(null);
            }
            else
            {
                return(results.ToArray());
            }
        } /* FilterListFirImageFiles */
        } /* WriteForGnuplotToStream */

        private void  SaveForGnuplot(Object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter          = "Comma delimited data to disk|*.txt";
            sfd.Title           = "Save Abundance info for gnuplot to disk.";
            sfd.DefaultExt      = "txt";
            sfd.AddExtension    = true;
            sfd.OverwritePrompt = true;

            String fn = cruise + "-" + station;

            if (!String.IsNullOrEmpty(deployment))
            {
                fn = fn + "-" + deployment;
            }
            fn = fn + "_AbundanceByDeployment";
            if (classToPlot != null)
            {
                fn += ("_" + classToPlot.Name);
            }
            fn          += ".txt";
            sfd.FileName = fn;

            //sfd.CheckFileExists = true;
            DialogResult dr = sfd.ShowDialog(this);

            if (dr == DialogResult.OK)
            {
                fn = sfd.FileName;

                System.IO.StreamWriter sw = null;
                try  { sw = new System.IO.StreamWriter(fn, false, System.Text.Encoding.ASCII); }
                catch (Exception) { sw = null; }

                if (sw == null)
                {
                    MessageBox.Show(this, "Could not open file[" + fn + "]", "Save for gnuplot", MessageBoxButtons.OK);
                    return;
                }

                WriteForGnuplotToStream(sw, bucketsDisplayed, true, false);

                sw.Close();
                sw = null;

                String ext = OSservices.GetFileExtension(fn);
                String ofn = OSservices.RemoveExtension(fn) + "_nVd." + ext;

                try  { sw = new System.IO.StreamWriter(ofn, false, System.Text.Encoding.ASCII); }
                catch (Exception) { sw = null; }

                if (sw == null)
                {
                    MessageBox.Show(this, "Could not open file[" + ofn + "]", "Save for gnuplot", MessageBoxButtons.OK);
                    return;
                }

                WriteForGnuplotToStream(sw, bucketsDisplayed, true, true);

                sw.Close();
                sw = null;

                MessageBox.Show(this, "Abundance Data for gnuplot written to \"" + fn + "\".", "", MessageBoxButtons.OK);
            }
        } /* SaveForGnuplot */
        } /* ImportImage */

        private void  ImportValidatedClassAssignmentsDir(PicesDataBase threadConn,
                                                         String dirName
                                                         )
        {
            RunLogAddMsg("Dir[" + dirName + "]" + "\n");

            String[] fileNames = null;
            try  { fileNames = Directory.GetFiles(dirName); }
            catch (Exception e)
            {
                RunLogAddMsg("\n" + "Error retrieving file info for Directory[" + dirName + "]" + "\n");
                RunLogAddMsg("Exception[" + e.ToString() + "]" + "\n\n");
                fileNames = null;
            }

            if (fileNames != null)
            {
                String     className = PicesClass.GetClassNameFromDirName(dirName);
                PicesClass mlClass   = null;

                int numThisDir       = 0;
                int numFailedThisDir = 0;
                foreach (String fn in fileNames)
                {
                    String ext = OSservices.GetFileExtension(fn).ToLower();
                    if ((ext == "bmp") || (ext == "jpg"))
                    {
                        String rn = OSservices.GetRootName(fn);
                        if (mlClass == null)
                        {
                            mlClass = GetClassFromName(threadConn, className);
                        }

                        if (importImages)
                        {
                            numThisDir++;
                            ImportImage(threadConn, fn);
                        }
                        else
                        {
                            //rn = "TRAIN_" + rn;
                            threadConn.ImagesUpdateValidatedAndPredictClass(rn, mlClass, 1.0f);
                            if (threadConn.Successful())
                            {
                                numThisDir++;
                            }
                            else
                            {
                                numFailedThisDir++;
                                RunLogAddMsg("Dir[" + dirName + "]  RootName[" + rn + "]  Failed" + "\n");
                                RunLogAddMsg(threadConn.LastErrorDesc() + "\n");
                            }
                        }

                        if ((numThisDir % 100) == 0)
                        {
                            RunLogAddMsg("Dir[" + dirName + "]  Files Updated[" + numThisDir.ToString("###,##0") + "]" + "\n");
                        }
                    }
                }

                RunLogAddMsg("Dir[" + dirName + "]  Files Updated[" + numThisDir.ToString("###,##0") + "]" + "\n");
                totalImagesUpdated += numThisDir;
            }

            if (!cancelImporting)
            {
                String[] directories = null;
                try { directories = Directory.GetDirectories(dirName); }
                catch (Exception) { directories = null; }

                if (directories != null)
                {
                    foreach (String subDir in directories)
                    {
                        if ((subDir == ".") || (subDir == ".."))
                        {
                            continue;
                        }
                        ImportValidatedClassAssignmentsDir(threadConn, subDir);
                        if (cancelImporting)
                        {
                            break;
                        }
                    }
                }
            }
        } /* ImportValidatedClassAssignmentsDir */
        } /* FilterListFirImageFiles */

        private void  ImportImagesFromDirectoryStructure(PicesDataBase threadConn,
                                                         String dirName
                                                         )
        {
            runLog.Writeln("Dir[" + dirName + "]" + "\n");

            String[] fileNames = null;
            try  { fileNames = Directory.GetFiles(dirName); }
            catch (Exception e)
            {
                runLog.Writeln("\n" + "Error retrieving file info for Directory[" + dirName + "]" + "\n");
                runLog.Writeln("Exception[" + e.ToString() + "]" + "\n\n");
                fileNames = null;
            }

            if (fileNames != null)
            {
                fileNames = FilterListForImageFiles(fileNames);
            }

            if (fileNames != null)
            {
                String sipperFileName = cruiseName + stationName + deploymentNum + "-" + OSservices.GetRootName(dirName);

                sipperFile               = new PicesSipperFile(sipperFileName);
                sipperFile.CruiseName    = cruiseName;
                sipperFile.StationName   = stationName;
                sipperFile.DeploymentNum = deploymentNum;
                threadConn.SipperFileInsert(sipperFile);

                int numThisDir       = 0;
                int numFailedThisDir = 0;
                foreach (String fn in fileNames)
                {
                    String ext = OSservices.GetFileExtension(fn).ToLower();
                    if ((ext == "bmp") || (ext == "jpg"))
                    {
                        numThisDir++;
                        bool successful = false;
                        ImportImage(threadConn, sipperFileName, fn, ref successful);
                        if (!successful)
                        {
                            ++numFailedThisDir;
                        }

                        if ((numThisDir % 100) == 0)
                        {
                            runLog.Writeln("Dir[" + dirName + "]  Files Updated[" + numThisDir.ToString("###,##0") + "]" + "\n");
                        }
                    }
                    if (cancelImporting)
                    {
                        break;
                    }
                }

                runLog.Writeln("Dir[" + dirName + "]  Files Updated[" + numThisDir.ToString("###,##0") + "]" + "\n");
                totalImagesUpdated += numThisDir;
            }

            if (!cancelImporting)
            {
                String[] directories = null;
                try { directories = Directory.GetDirectories(dirName); }
                catch (Exception) { directories = null; }

                if (directories != null)
                {
                    foreach (String subDir in directories)
                    {
                        if ((subDir == ".") || (subDir == ".."))
                        {
                            continue;
                        }
                        ImportImagesFromDirectoryStructure(threadConn, subDir);
                        if (cancelImporting)
                        {
                            break;
                        }
                    }
                }
            }
        } /* ImportImagesFromDirectoryStructure */