Exemplo n.º 1
0
        /// <summary>Main run method for performing our calculations and storing data.</summary>
        public void Run()
        {
            // If the target table has not been modified during the simulation run, don't do anything.
            if (dataStore?.Writer != null && !dataStore.Writer.TablesModified.Contains(TableName))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(TableName))
            {
                throw new Exception(string.Format("Error in probability model {0}: TableName is null", Name));
            }
            else if (!dataStore.Reader.TableNames.Contains(TableName))
            {
                throw new Exception(string.Format("Error in probability model {0}: table '{1}' does not exist in the database.", Name, TableName));
            }
            DataTable simulationData = dataStore.Reader.GetData(TableName, fieldNames: dataStore.Reader.ColumnNames(TableName));

            if (simulationData != null)
            {
                IndexedDataTable simData         = new IndexedDataTable(simulationData, new string[] { FieldToSplitOn });
                IndexedDataTable probabilityData = new IndexedDataTable(new string[] { FieldToSplitOn });

                foreach (var group in simData.Groups())
                {
                    object keyValue = group.IndexValues[0];

                    // Add in our key column
                    probabilityData.SetIndex(new object[] { keyValue });
                    probabilityData.Set <object>(FieldToSplitOn, keyValue);

                    // Add in all other numeric columns.
                    bool haveWrittenProbabilityColumn = false;

                    foreach (DataColumn column in simulationData.Columns)
                    {
                        if (column.DataType == typeof(double))
                        {
                            var values = group.Get <double>(column.ColumnName).ToList();
                            values.Sort();

                            if (!haveWrittenProbabilityColumn)
                            {
                                // Add in the probability column
                                double[] probabilityValues = MathUtilities.ProbabilityDistribution(values.Count, this.Exceedence);
                                probabilityData.SetValues("Probability", probabilityValues);
                                haveWrittenProbabilityColumn = true;
                            }

                            probabilityData.SetValues(column.ColumnName, values);
                        }
                    }
                }

                // Write the stats data to the DataStore
                DataTable t = probabilityData.ToTable();
                t.TableName = this.Name;
                dataStore.Writer.WriteTable(t);
            }
        }
Exemplo n.º 2
0
        public void TestIterateThroughGroups()
        {
            IndexedDataTable indexedTable = new IndexedDataTable(new string[] { "Year", "Name" });

            indexedTable.SetIndex(new object[] { 2000, "Name1" });
            indexedTable.SetValues("A", new double[] { 1, 2, 3, 4 }); // vector
            indexedTable.Set("B", 1234);                              // scalar

            indexedTable.SetIndex(new object[] { 2001, "Name2" });
            indexedTable.SetValues("A", new double[] { 5, 6, 7, 8 }); // vector
            indexedTable.Set("B", 5678);                              // scalar

            int i = 1;

            foreach (var group in indexedTable.Groups())
            {
                var a = group.Get <double>("A");
                if (i == 1)
                {
                    Assert.AreEqual(a, new double[] { 1, 2, 3, 4 });
                }
                else
                {
                    Assert.AreEqual(a, new double[] { 5, 6, 7, 8 });
                }
                i++;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// The main run method called to fill tables in the specified DataStore.
        /// </summary>
        /// <param name="dataStore">The DataStore to work with</param>
        public void Run(IStorageReader dataStore)
        {
            dataStore.DeleteDataInTable(this.Name);

            DataTable simulationData = dataStore.GetData(TableName, fieldNames: dataStore.GetTableColumns(TableName));

            if (simulationData != null)
            {
                IndexedDataTable simData         = new IndexedDataTable(simulationData, new string[] { FieldToSplitOn });
                IndexedDataTable probabilityData = new IndexedDataTable(new string[] { FieldToSplitOn });

                foreach (var group in simData.Groups())
                {
                    object keyValue = group.IndexValues[0];

                    // Add in our key column
                    probabilityData.SetIndex(new object[] { keyValue });
                    probabilityData.Set <object>(FieldToSplitOn, keyValue);

                    // Add in all other numeric columns.
                    bool haveWrittenProbabilityColumn = false;

                    foreach (DataColumn column in simulationData.Columns)
                    {
                        if (column.DataType == typeof(double))
                        {
                            var values = group.Get <double>(column.ColumnName).ToList();
                            values.Sort();

                            if (!haveWrittenProbabilityColumn)
                            {
                                // Add in the probability column
                                double[] probabilityValues = MathUtilities.ProbabilityDistribution(values.Count, this.Exceedence);
                                probabilityData.SetValues("Probability", probabilityValues);
                                haveWrittenProbabilityColumn = true;
                            }

                            probabilityData.SetValues(column.ColumnName, values);
                        }
                    }
                }

                // Write the stats data to the DataStore
                DataTable t = probabilityData.ToTable();
                t.TableName = this.Name;
                dataStore.WriteTable(t);
            }
        }