示例#1
0
        /// <summary>
        /// Get a param as a double.
        /// </summary>
        ///
        /// <param name="name">The name of the double.</param>
        /// <param name="required">True if this value is required.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The value.</returns>
        public double GetDouble(String name, bool required, double defaultValue)
        {
            String str = GetString(name, required, null);

            if (str == null)
            {
                return(defaultValue);
            }

            try
            {
                return(_format.Parse(str));
            }
            catch (FormatException)
            {
                throw new EncogError("Property " + name
                                     + " has an invalid value of " + str
                                     + ", should be valid floating point.");
            }
        }
示例#2
0
        /// <summary>
        ///     Perform a pass one analysis of this field.
        /// </summary>
        /// <param name="str">The current value.</param>
        public void Analyze1(String v)
        {
            bool   accountedFor = false;
            string str          = v.Trim();

            if (str.Trim().Length == 0 || str.Equals("?"))
            {
                Complete = false;
                return;
            }

            _instances++;

            if (Real)
            {
                try
                {
                    double d = _fmt.Parse(str);
                    Max          = Math.Max(d, Max);
                    Min          = Math.Min(d, Min);
                    _total      += d;
                    accountedFor = true;
                }
                catch (FormatException)
                {
                    Real = false;
                    if (!Integer)
                    {
                        Max = 0;
                        Min = 0;
                        StandardDeviation = 0;
                    }
                }
            }

            if (Integer)
            {
                try
                {
                    int i = Int32.Parse(str);
                    Max = Math.Max(i, Max);
                    Min = Math.Min(i, Min);
                    if (!accountedFor)
                    {
                        _total += i;
                    }
                }
                catch (FormatException)
                {
                    Integer = false;
                    if (!Real)
                    {
                        Max = 0;
                        Min = 0;
                        StandardDeviation = 0;
                    }
                }
            }

            if (Class)
            {
                AnalystClassItem item;

                // is this a new class?
                if (!_classMap.ContainsKey(str))
                {
                    item           = new AnalystClassItem(str, str, 1);
                    _classMap[str] = item;

                    // do we have too many different classes?
                    int max = _script.Properties.GetPropertyInt(
                        ScriptProperties.SetupConfigMaxClassCount);
                    if (_classMap.Count > max)
                    {
                        Class = false;
                    }
                }
                else
                {
                    item = _classMap[str];
                    item.IncreaseCount();
                }
            }
        }
        /// <summary>
        ///     Analyze the input file.
        /// </summary>
        /// <param name="input">The input file.</param>
        /// <param name="headers">True, if there are headers.</param>
        /// <param name="format">The format of the CSV data.</param>
        public virtual void Analyze(FileInfo input, bool headers,
                                    CSVFormat format)
        {
            ResetStatus();
            InputFilename      = input;
            ExpectInputHeaders = headers;
            Format             = format;
            _columnMapping.Clear();
            _columns.Clear();

            // first count the rows
            TextReader reader = null;

            try
            {
                int recordCount = 0;
                reader = new StreamReader(InputFilename.OpenRead());
                while (reader.ReadLine() != null)
                {
                    UpdateStatus(true);
                    recordCount++;
                }

                if (headers)
                {
                    recordCount--;
                }
                RecordCount = recordCount;
            }
            catch (IOException ex)
            {
                throw new QuantError(ex);
            }
            finally
            {
                ReportDone(true);
                if (reader != null)
                {
                    try
                    {
                        reader.Close();
                    }
                    catch (IOException e)
                    {
                        throw new QuantError(e);
                    }
                }
                InputFilename      = input;
                ExpectInputHeaders = headers;
                Format             = format;
            }

            // now analyze columns
            ReadCSV csv = null;

            try
            {
                csv = new ReadCSV(input.ToString(), headers, format);
                if (!csv.Next())
                {
                    throw new QuantError("File is empty");
                }

                for (int i = 0; i < csv.ColumnCount; i++)
                {
                    String name;

                    if (headers)
                    {
                        name = AttemptResolveName(csv.ColumnNames[i]);
                    }
                    else
                    {
                        name = "Column-" + (i + 1);
                    }

                    // determine if it should be an input/output field

                    String str = csv.Get(i);

                    bool io = false;

                    try
                    {
                        Format.Parse(str);
                        io = true;
                    }
                    catch (FormatException ex)
                    {
                        EncogLogging.Log(ex);
                    }

                    AddColumn(new FileData(name, i, io, io));
                }
            }
            finally
            {
                if (csv != null)
                {
                    csv.Close();
                }
                Analyzed = true;
            }
        }