Encog.Neural.Data.Buffer.EncogEGBFile.Open C# (CSharp) Method

Open() public method

Open an existing EGB file.
public Open ( ) : void
return void
        public void Open()
        {
            try
            {
                this.stream = new FileStream(this.file, FileMode.Open, FileAccess.Read);
                this.binaryReader = new BinaryReader(this.stream);
                this.binaryWriter = null;

                bool isEncogFile = true;

                isEncogFile = isEncogFile ? this.binaryReader.ReadByte() == 'E' : false;
                isEncogFile = isEncogFile ? this.binaryReader.ReadByte() == 'N' : false;
                isEncogFile = isEncogFile ? this.binaryReader.ReadByte() == 'C' : false;
                isEncogFile = isEncogFile ? this.binaryReader.ReadByte() == 'O' : false;
                isEncogFile = isEncogFile ? this.binaryReader.ReadByte() == 'G' : false;
                isEncogFile = isEncogFile ? this.binaryReader.ReadByte() == '-' : false;

                if (!isEncogFile)
                {
                    throw new BufferedDataError(
                            "File is not a valid Encog binary file:"
                                    + this.file);
                }

                char v1 = (char)this.binaryReader.ReadByte();
                char v2 = (char)this.binaryReader.ReadByte();
                String versionStr = "" + v1 + v2;

                try
                {
                    int version = int.Parse(versionStr);
                    if (version > 0)
                    {
                        throw new BufferedDataError(
                                "File is from a newer version of Encog than is currently in use.");
                    }
                }
                catch (Exception)
                {
                    throw new BufferedDataError("File has invalid version number.");
                }

                this.inputCount = (int)this.binaryReader.ReadDouble();
                this.idealCount = (int)this.binaryReader.ReadDouble();

                this.recordCount = this.inputCount + this.idealCount;
                this.recordSize = this.recordCount * EncogEGBFile.DOUBLE_SIZE;
                this.numberOfRecords = (int)((this.stream.Length - EncogEGBFile.HEADER_SIZE) / this.recordSize);

            }
            catch (IOException ex)
            {
                throw new BufferedDataError(ex);
            }

        }

Usage Example

示例#1
0
        /// <summary>
        /// Convert an Encog binary file to an external form, such as CSV.
        /// </summary>
        /// <param name="binaryFile">THe binary file to use.</param>
        public void Binary2External(String binaryFile)
        {
            Status.Report(0, 0, "Exporting binary file: " + binaryFile);

            EncogEGBFile egb = new EncogEGBFile(binaryFile);

            egb.Open();

            this.codec.PrepareWrite(egb.NumberOfRecords, egb.InputCount,
                                    egb.IdealCount);

            int inputCount = egb.InputCount;
            int idealCount = egb.IdealCount;

            double[] input = new double[inputCount];
            double[] ideal = new double[idealCount];

            int currentRecord = 0;
            int lastUpdate    = 0;

            // now load the data
            for (int i = 0; i < egb.NumberOfRecords; i++)
            {
                for (int j = 0; j < inputCount; j++)
                {
                    input[j] = egb.Read();
                }

                for (int j = 0; j < idealCount; j++)
                {
                    ideal[j] = egb.Read();
                }

                this.codec.Write(input, ideal);

                currentRecord++;
                lastUpdate++;
                if (lastUpdate >= 10000)
                {
                    lastUpdate = 0;
                    this.Status.Report(egb.NumberOfRecords, currentRecord,
                                       "Exporting...");
                }
            }

            egb.Close();
            this.codec.Close();
            Status.Report(0, 0, "Done exporting binary file: "
                          + binaryFile);
        }
All Usage Examples Of Encog.Neural.Data.Buffer.EncogEGBFile::Open