public void SetConfiguration(string config_path)
        {
            _config = DataPointConfiguration.LoadFromFile(config_path);

            _columnMapping = new Dictionary <string, DataColumn>();

            foreach (var column in _config._types)
            {
                //TODO get rid of this switch... push it into the data column as a static factory or such
                DataColumn dc = null;
                switch (column.Value)
                {
                case DataColumn.DataValueTypes.NUMBER:
                    dc = new DoubleDataColumn();
                    break;

                case DataColumn.DataValueTypes.CATEGORY:
                    dc = new CategoryDataColumn();
                    break;

                case DataColumn.DataValueTypes.ID:
                    break;

                case DataColumn.DataValueTypes.CLASS:
                    _hasClasses = true;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                if (dc != null)
                {
                    dc._header = column.Key;
                    _columns.Add(dc);
                    _columnMapping.Add(dc._header, dc);
                }
            }

            //at this point, the columns exists, go ahead and load the codebook values
            //TODO really need to generalize this file name and take an input

            using (StreamReader sr = new StreamReader("codebook.txt"))
            {
                while (!sr.EndOfStream)
                {
                    //TODO move all of this code to the Codebook where it belongs
                    var line  = sr.ReadLine();
                    var parts = line.Split('|');

                    string header = parts[0];
                    string data   = parts[1];

                    var col = _columnMapping[header] as CategoryDataColumn;

                    col._codebook.PopulateFromString(data);
                }
            }
        }
        public static DataPointConfiguration CreateDefaultFromFile(string path)
        {
            using (StreamReader sr = new StreamReader(File.OpenRead(path)))
            {
                string headers = sr.ReadLine();

                return(DataPointConfiguration.CreateDefaultFromHeaders(headers));
            }
        }
        public static DataPointConfiguration CreateDefaultFromHeaders(string headers)
        {
            var config = new DataPointConfiguration();
            var parts  = headers.Split(',');

            foreach (var header in parts)
            {
                config._types.Add(header, DataColumn.DataValueTypes.CATEGORY);
            }
            return(config);
        }
示例#4
0
        void Btn_configDefaultClick(object sender, EventArgs e)
        {
            //create default file based on data file
            string data_path = txt_dataFile.Text;

            //HACK this is just to allow copy/paste straight from Explorer
            data_path = data_path.Replace("\"", string.Empty);

            config = DataPointConfiguration.CreateDefaultFromFile(data_path);
            config.SaveToFile(Path.GetDirectoryName(data_path) + @"\" + Path.GetFileNameWithoutExtension(data_path) + "_config.txt");
        }
        public static DataPointConfiguration LoadFromFile(string path)
        {
            DataPointConfiguration config = new DataPointConfiguration();

            using (StreamReader sr = new StreamReader(File.OpenRead(path)))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();

                    var parts = line.Split('|');

                    config._types.Add(parts[0], (DataColumn.DataValueTypes)Enum.Parse(typeof(DataColumn.DataValueTypes), parts[1]));
                }
            }

            return(config);
        }
示例#6
0
        public static DataPoint FromString(string[] raw_data,
                                           Dictionary <int, string> header_mapping,
                                           Dictionary <int, DataColumn> colMapping,
                                           DataPointConfiguration configs)
        {
            DataPoint dp = new DataPoint();

            //need to create a data point and deal with the types
            dp._data = new List <DataValue>();

            for (int i = 0; i < raw_data.Length; i++)
            {
                var value = raw_data[i];

                var dv = new DataValue();

                var        header = header_mapping[i];
                DataColumn column = colMapping[i];
                var        config = configs._types[header];

                //TODO abstract this code into multiple classes
                switch (config)
                {
                case DataColumn.DataValueTypes.NUMBER:
                    if (value == string.Empty || !double.TryParse(value, out dv._value))
                    {
                        dv._isMissing            = true;
                        column._hasMissingValues = true;
                        dv._value = double.NaN;
                    }

                    dp._data.Add(dv);

                    break;

                case DataColumn.DataValueTypes.CATEGORY:
                    if (value == string.Empty)
                    {
                        dv._isMissing            = true;
                        column._hasMissingValues = true;
                        dv._value = -2;
                    }
                    else
                    {
                        var dataColumn = column as CategoryDataColumn;
                        dv._value = dataColumn._codebook.GetOrAddValue(value);
                    }

                    dp._data.Add(dv);

                    break;

                case DataColumn.DataValueTypes.ID:
                    dp._id = value;

                    break;

                case DataColumn.DataValueTypes.CLASS:

                    //TODO fix this with the actual codebook for teh column
                    dv._value          = double.Parse(value);
                    dp._classification = dv;

                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                if (column != null)
                {
                    column._values.Add(dv);
                }
            }

            return(dp);
        }