Example #1
0
        /// <summary>
        /// Generates a .csv file containing the factor information displayed in the grid.
        /// The user can edit this file to more efficiently enable or disable factors in bulk.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="args">Event arguments.</param>
        private void OnExportCsv(object sender, EventArgs args)
        {
            try
            {
                var path = explorerPresenter.MainPresenter.AskUserForSaveFileName("*.csv", null);
                if (path != null)
                {
                    // Clone the datatable and add an enabled column.
                    var data = view.List.DataSource.Copy();
                    data.Columns.Add("Enabled?", typeof(bool));
                    foreach (DataRow row in data.Rows)
                    {
                        var simulationName = row[0].ToString();
                        row["Enabled"] = !experiment.DisabledSimNames.Contains(simulationName);
                    }

                    // Convert datatable to csv and write to path.
                    using (var writer = new StreamWriter(path))
                        DataTableUtilities.DataTableToText(data, 0, ",", true, writer);

                    explorerPresenter.MainPresenter.ShowMessage(string.Format("Successfully generated {0}.", path), Simulation.MessageType.Information);
                }
            }
            catch (Exception e)
            {
                explorerPresenter.MainPresenter.ShowError(e);
            }
        }
Example #2
0
        /// <summary>Convert a DataTable to a string.</summary>
        public static string TableToString(DataTable data)
        {
            StringWriter writer = new StringWriter();

            DataTableUtilities.DataTableToText(data, 0, ",", true, writer);
            return(writer.ToString());
        }
Example #3
0
        /// <summary>Create a text report from tables in this data store.</summary>
        /// <param name="dataStore">The data store.</param>
        /// <param name="fileName">Name of the file.</param>
        private static void WriteAllTables(DataStore dataStore, string fileName)
        {
            // Write out each table for this simulation.
            foreach (string tableName in dataStore.TableNames)
            {
                if (tableName != "Messages" && tableName != "InitialConditions")
                {
                    DataTable firstRowOfTable = dataStore.RunQuery("SELECT * FROM " + tableName + " LIMIT 1");
                    if (firstRowOfTable != null)
                    {
                        string fieldNamesString = "";
                        for (int i = 1; i < firstRowOfTable.Columns.Count; i++)
                        {
                            if (i > 1)
                            {
                                fieldNamesString += ", ";
                            }
                            fieldNamesString += "[" + firstRowOfTable.Columns[i].ColumnName + "]";
                        }

                        string sql = String.Format("SELECT Name, {0} FROM Simulations, {1} " +
                                                   "WHERE Simulations.ID = {1}.SimulationID " +
                                                   "ORDER BY Name",
                                                   fieldNamesString, tableName);
                        DataTable data = dataStore.RunQuery(sql);
                        if (data != null && data.Rows.Count > 0)
                        {
                            StreamWriter report = new StreamWriter(Path.ChangeExtension(fileName, "." + tableName + ".csv"));
                            report.Write(DataTableUtilities.DataTableToText(data, 0, ",", true));
                            report.Close();
                        }
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Adds an older yield prophet job to the APSIM cloud.
        /// </summary>
        /// <param name="yieldProphet">The job specification.</param>
        public void AddYP(string yieldProphetXML, DataTable weatherData, DataTable soilProbeData)
        {
            // Parse the PaddockXML.
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(yieldProphetXML);

            // Set the report type to indicate to the run machine to do a current growth stage.
            //XmlHelper.SetValue(Doc.DocumentElement, "ReportType", "Current growth stage");

            // Get the paddock node.
            XmlNode PaddockNode = XmlUtilities.FindByType(doc.DocumentElement, "Paddock");

            if (PaddockNode == null)
            {
                throw new Exception("Cannot find a <paddock> node in the PaddockXML");
            }

            // Get a temporary working folder.
            string workingDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            Directory.CreateDirectory(workingDirectory);

            // Save the xml to file so that we can attach it to the email.
            string ypFileName = Path.Combine(workingDirectory, "YieldProphet.xml");

            doc.Save(ypFileName);

            // Write the weather file.
            string       weatherFileName = Path.Combine(workingDirectory, "observed.csv");
            StreamWriter writer          = new StreamWriter(weatherFileName);

            DataTableUtilities.DataTableToText(weatherData, 0, ",", true, writer);
            writer.Close();

            // Write the soil probe file.
            string soilProbeFileName = Path.Combine(workingDirectory, "soilprobe.csv");

            writer = new StreamWriter(soilProbeFileName);
            DataTableUtilities.DataTableToText(soilProbeData, 0, ",", true, writer);
            writer.Close();

            // Send email to run machine.
            MailMessage mail = new MailMessage();

            mail.Subject = "YieldProphet run";
            mail.To.Add(new MailAddress("*****@*****.**"));
            mail.From       = new System.Net.Mail.MailAddress("*****@*****.**");
            mail.IsBodyHtml = false;
            mail.Attachments.Add(new Attachment(ypFileName));
            mail.Attachments.Add(new Attachment(weatherFileName));
            mail.Attachments.Add(new Attachment(soilProbeFileName));
            SmtpClient smtp = new SmtpClient("smtp-relay.csiro.au");

            smtp.Send(mail);
            mail.Dispose();

            // Clean up our directory.
            Directory.Delete(workingDirectory, true);
        }
Example #5
0
        /// <summary>Main run method for performing our post simulation calculations</summary>
        public void Run()
        {
            // Note - we seem to be assuming that the predicted data table is called Report.
            // If the predicted table has not been modified during the most recent simulations run, don't do anything.
            if (dataStore?.Writer != null && !dataStore.Writer.TablesModified.Contains("Report"))
            {
                return;
            }

            string    sql           = "SELECT * FROM [Report]";
            DataTable predictedData = dataStore.Reader.GetDataUsingSql(sql);

            if (predictedData != null)
            {
                IndexedDataTable predictedDataIndexed = new IndexedDataTable(predictedData, null);

                string outputNames = StringUtilities.Build(Outputs, ",", "\"", "\"");
                string inputNames  = StringUtilities.Build(Inputs, ",", "\"", "\"");
                string anovaVariableValuesFileName = GetTempFileName("anovaVariableValues", ".csv");

                // Write variables file
                using (var writer = new StreamWriter(anovaVariableValuesFileName))
                    DataTableUtilities.DataTableToText(predictedDataIndexed.ToTable(), 0, ",", true, writer, excelFriendly: true);

                string script = string.Format(
                    "inputs <- c({0})" + Environment.NewLine +
                    "inputs <- inputs[inputs != \"\"]" + Environment.NewLine +
                    "outputs <- c({1})" + Environment.NewLine +
                    "outputs <- outputs[outputs != \"\"]" + Environment.NewLine +
                    "factorial_data <- read.csv(\"{2}\")" + Environment.NewLine +
                    "indices <- data.frame(matrix(ncol = 4, nrow = 0))" + Environment.NewLine +
                    "colnames(indices) <- c(\"Input\", \"Output\", \"FirstOrder\", \"TotalOrder\")" + Environment.NewLine +
                    "for (output in outputs){{" + Environment.NewLine +
                    "  data <- factorial_data[, names(factorial_data) %in% inputs | names(factorial_data) == output]" + Environment.NewLine +
                    "  data[, names(data) %in% inputs] <- lapply(data[, names(data) %in% inputs], factor)" + Environment.NewLine +
                    "  output_mean <- mean(data[[output]])" + Environment.NewLine +
                    "  TSS <- sum((data[[output]] - output_mean)^2)" + Environment.NewLine +
                    "  anova_model <- aov(data[[output]] ~ (.)^1000, data = data[, names(data) %in% inputs])" + Environment.NewLine +
                    "  SSi <- summary(anova_model)[[1]][2]" + Environment.NewLine +
                    "  variance_contributions <- SSi / TSS" + Environment.NewLine +
                    "  parameter_names <- trimws(rownames(SSi), which = \"both\")" + Environment.NewLine +
                    "  all_results <- data.frame(parameter_names, variance_contributions, row.names = NULL)" + Environment.NewLine +
                    "  names(all_results) <- list(\"input\", \"% of variance\")  " + Environment.NewLine +
                    "  for (input in inputs){{" + Environment.NewLine +
                    "    first <- all_results[all_results$input == input, colnames(all_results) == \"% of variance\"]" + Environment.NewLine +
                    "    total <- sum(all_results[grepl(input, all_results$input), colnames(all_results) == \"% of variance\"])" + Environment.NewLine +
                    "    result <- data.frame(Input=c(input), Output=c(output), FirstOrder=c(first), TotalOrder=c(total))" + Environment.NewLine +
                    "    indices <- rbind(indices, result)" + Environment.NewLine +
                    "  }}" + Environment.NewLine +
                    "}}" + Environment.NewLine +
                    "write.table(indices, sep=\",\", row.names=FALSE)" + Environment.NewLine
                    ,
                    inputNames, outputNames, anovaVariableValuesFileName.Replace("\\", "/"));

                DataTable results = RunR(script);
                results.TableName = Name + "Statistics";
                dataStore.Writer.WriteTable(results);
            }
        }
Example #6
0
 /// <summary>Create a text report from tables in this data store.</summary>
 /// <param name="storage">The data store.</param>
 /// <param name="fileName">Name of the file.</param>
 public static void WriteAllTables(IStorageReader storage, string fileName)
 {
     // Write out each table for this simulation.
     foreach (string tableName in storage.TableNames)
     {
         DataTable data = storage.GetData(tableName);
         if (data != null && data.Rows.Count > 0)
         {
             StreamWriter report = new StreamWriter(Path.ChangeExtension(fileName, "." + tableName + ".csv"));
             DataTableUtilities.DataTableToText(data, 0, ",", true, report);
             report.Close();
         }
     }
 }
Example #7
0
 /// <summary>
 /// Write the specified table to the TextWriter.
 /// </summary>
 /// <param name="writer">The writer to write to</param>
 /// <param name="table">The table to write</param>
 /// <param name="html">Indicates whether html format should be produced</param>
 /// <param name="includeHeadings">Include headings in the html table produced?</param>
 /// <param name="className">The class name of the generated html table</param>
 private static void WriteTable(TextWriter writer, DataTable table, bool html, bool includeHeadings, string className)
 {
     if (html)
     {
         bool   showHeadings = className != "PropertyTable";
         string line         = DataTableUtilities.DataTableToText(table, 0, "  ", showHeadings);
         line = line.Replace("\r\n", "<br/>");
         writer.WriteLine(line);
     }
     else
     {
         bool   showHeadings = className != "PropertyTable";
         string line         = DataTableUtilities.DataTableToText(table, 0, "  ", showHeadings);
         writer.WriteLine(line);
     }
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            Response.AppendHeader("Content-Disposition", "attachment; filename=Registrations.csv");
            Response.Buffer = false;

            Response.ContentType = "text/plain";

            SqlDataSource1.ConnectionString = System.IO.File.ReadAllText(@"D:\Websites\dbConnect.txt") + ";Database=APSIM.Registration";
            System.Data.DataView dv     = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
            DataTable            Data   = dv.ToTable();
            StringWriter         writer = new StringWriter();

            DataTableUtilities.DataTableToText(Data, 1, ",", true, writer, excelFriendly: true);

            Response.Write(writer.ToString());
            Response.Flush();                 // send our content to the client browser.
            Response.SuppressContent = true;  // stops .net from writing it's stuff.
        }
Example #9
0
        /// <summary>
        /// Calculate factors that we need to run. Put combinations into allCombinations
        /// </summary>
        private void CalculateFactors()
        {
            if (allCombinations.Count == 0)
            {
                if (ParameterValues == null || ParameterValues.Rows.Count == 0)
                {
                    ParameterValues = RunRToGetParameterValues();
                }
                if (ParameterValues == null || ParameterValues.Rows.Count == 0)
                {
                    throw new Exception("The morris function in R returned null");
                }

                if (ParameterValues.Rows.Count != NumPaths * (Parameters.Count + 1))
                {
                    StringBuilder msg = new StringBuilder();
                    msg.AppendLine("Morris error: Number of parameter values from R is not equal to num paths * (N + 1).");
                    msg.AppendLine($"Number of parameters from R = {ParameterValues.Rows.Count}");
                    msg.AppendLine($"NumPaths={NumPaths}");
                    msg.AppendLine($"Parameters.Count={Parameters.Count}");
                    msg.AppendLine($"ParameterValues as returned from R:");
                    using (StringWriter writer = new StringWriter(msg))
                        DataTableUtilities.DataTableToText(ParameterValues, 0, ",", true, writer);
                    Console.WriteLine(msg.ToString());
                }

                int simulationNumber = 1;
                foreach (DataRow parameterRow in ParameterValues.Rows)
                {
                    var factors = new List <CompositeFactor>();
                    foreach (Parameter param in Parameters)
                    {
                        object          value = Convert.ToDouble(parameterRow[param.Name], CultureInfo.InvariantCulture);
                        CompositeFactor f     = new CompositeFactor(param.Name, param.Path, value);
                        factors.Add(f);
                    }

                    string newSimulationName = Name + "Simulation" + simulationNumber;
                    allCombinations.Add(factors);
                    simulationNumber++;
                }
            }
        }
Example #10
0
        /// <summary>
        /// Write the specified table to the TextWriter.
        /// </summary>
        /// <param name="writer">The writer to write to</param>
        /// <param name="table">The table to write</param>
        /// <param name="outtype">Indicates the format to be produced</param>
        /// <param name="className">The class name of the generated html table</param>
        /// <param name="document">Document object if using MigraDoc to generate output, null otherwise </param>
        private static void WriteTable(TextWriter writer, DataTable table, OutputType outtype, string className, Document document)
        {
            bool showHeadings = className != "PropertyTable";

            if (outtype == OutputType.html)
            {
                if (showHeadings)
                {
                    writer.WriteLine("<table class='headered'>");
                    writer.Write("<tr>");
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        writer.Write("<th");
                        if (i == 0)
                        {
                            writer.Write(" class='col1'");
                        }
                        writer.Write(">" + table.Columns[i].ColumnName + "</th>");
                    }
                }
                else
                {
                    writer.WriteLine("<table>");
                }

                foreach (DataRow row in table.Rows)
                {
                    bool titleRow = Convert.IsDBNull(row[0]);
                    if (titleRow)
                    {
                        writer.Write("<tr class='total'>");
                    }
                    else
                    {
                        writer.Write("<tr>");
                    }

                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        string st;
                        if (titleRow && i == 0)
                        {
                            st = "Total";
                        }
                        else
                        {
                            st = row[i].ToString();
                        }

                        writer.Write("<td");
                        if (i == 0)
                        {
                            writer.Write(" class='col1'");
                        }
                        writer.Write(">");
                        writer.Write(st);
                        writer.Write("</td>");
                    }
                    writer.WriteLine("</tr>");
                }
                writer.WriteLine("</table><br/>");
            }
            else if (outtype == OutputType.rtf)
            {
                MigraDoc.DocumentObjectModel.Tables.Table tabl = new MigraDoc.DocumentObjectModel.Tables.Table();
                tabl.Borders.Width = 0.75;

                foreach (DataColumn col in table.Columns)
                {
                    Column column = tabl.AddColumn(Unit.FromCentimeter(18.0 / table.Columns.Count));
                }

                if (showHeadings)
                {
                    MigraDoc.DocumentObjectModel.Tables.Row row = tabl.AddRow();
                    row.Shading.Color  = Colors.PaleGoldenrod;
                    tabl.Shading.Color = new Color(245, 245, 255);
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        Cell      cell      = row.Cells[i];
                        Paragraph paragraph = cell.AddParagraph();
                        if (i == 0)
                        {
                            paragraph.Format.Alignment = ParagraphAlignment.Left;
                        }
                        else
                        {
                            paragraph.Format.Alignment = ParagraphAlignment.Right;
                        }
                        paragraph.AddText(table.Columns[i].ColumnName);
                    }
                }

                foreach (DataRow row in table.Rows)
                {
                    bool   titleRow = Convert.IsDBNull(row[0]);
                    string st;
                    MigraDoc.DocumentObjectModel.Tables.Row newRow = tabl.AddRow();

                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        if (titleRow && i == 0)
                        {
                            st = "Total";
                            newRow.Format.Font.Color = Colors.DarkOrange;
                            newRow.Format.Font.Bold  = true;
                        }
                        else
                        {
                            st = row[i].ToString();
                        }

                        Cell      cell      = newRow.Cells[i];
                        Paragraph paragraph = cell.AddParagraph();
                        if (!showHeadings)
                        {
                            cell.Borders.Style         = BorderStyle.None;
                            paragraph.Format.Alignment = ParagraphAlignment.Left;
                        }
                        else if (i == 0)
                        {
                            paragraph.Format.Alignment = ParagraphAlignment.Left;
                        }
                        else
                        {
                            paragraph.Format.Alignment = ParagraphAlignment.Right;
                        }

                        if (showHeadings && i == 0)
                        {
                            paragraph.AddFormattedText(st, TextFormat.Bold);
                        }
                        else
                        {
                            paragraph.AddText(st);
                        }
                    }
                }

                document.LastSection.Add(tabl);
                document.LastSection.AddParagraph(); // Just to give a bit of spacing
            }
            else
            {
                DataTableUtilities.DataTableToText(table, 0, "  ", showHeadings, writer);
            }
        }
Example #11
0
        /// <summary>
        /// Get a list of parameter values that we are to run. Call R to do this.
        /// </summary>
        private void RunRPostSimulation(DataTable predictedValues, out DataTable eeDataRaw, out DataTable statsDataRaw)
        {
            string morrisParametersFileName = GetTempFileName("parameters", ".csv");
            string apsimVariableFileName    = GetTempFileName("apsimvariable", ".csv");
            string rFileName     = GetTempFileName("morrisscript", ".r");
            string eeFileName    = GetTempFileName("ee", ".csv");
            string statsFileName = GetTempFileName("stats", ".csv");

            // write predicted values file
            using (StreamWriter writer = new StreamWriter(apsimVariableFileName))
                DataTableUtilities.DataTableToText(predictedValues, 0, ",", true, writer);

            // Write parameters
            using (StreamWriter writer = new StreamWriter(morrisParametersFileName))
                DataTableUtilities.DataTableToText(ParameterValues, 0, ",", true, writer);

            string paramNames  = StringUtilities.Build(Parameters.Select(p => p.Name), ",", "\"", "\"");
            string lowerBounds = StringUtilities.Build(Parameters.Select(p => p.LowerBound), ",");
            string upperBounds = StringUtilities.Build(Parameters.Select(p => p.UpperBound), ",");
            string script      = GetMorrisRScript();

            script += string.Format
                          ("apsimMorris$X <- read.csv(\"{0}\")" + Environment.NewLine +
                          "values = read.csv(\"{1}\", check.names = F)" + Environment.NewLine +
                          "allEE <- data.frame()" + Environment.NewLine +
                          "allStats <- data.frame()" + Environment.NewLine +
                          "for (columnName in colnames(values))" + Environment.NewLine +
                          "{{" + Environment.NewLine +
                          " apsimMorris$y <- values[[columnName]]" + Environment.NewLine +
                          " tell(apsimMorris)" + Environment.NewLine +

                          " ee <- data.frame(apsimMorris$ee)" + Environment.NewLine +
                          " ee$variable <- columnName" + Environment.NewLine +
                          " ee$path <- c(1:{2})" + Environment.NewLine +
                          " allEE <- rbind(allEE, ee)" + Environment.NewLine +

                          " mu <- apply(apsimMorris$ee, 2, mean)" + Environment.NewLine +
                          " mustar <- apply(apsimMorris$ee, 2, function(x) mean(abs(x)))" + Environment.NewLine +
                          " sigma <- apply(apsimMorris$ee, 2, sd)" + Environment.NewLine +
                          " stats <- data.frame(mu, mustar, sigma)" + Environment.NewLine +
                          " stats$param <- params" + Environment.NewLine +
                          " stats$variable <- columnName" + Environment.NewLine +
                          " allStats <- rbind(allStats, stats)" + Environment.NewLine +

                          "}}" + Environment.NewLine +
                          "write.csv(allEE,\"{3}\", row.names=FALSE)" + Environment.NewLine +
                          "write.csv(allStats, \"{4}\", row.names=FALSE)" + Environment.NewLine,
                          morrisParametersFileName.Replace("\\", "/"),
                          apsimVariableFileName.Replace("\\", "/"),
                          NumPaths,
                          eeFileName.Replace("\\", "/"),
                          statsFileName.Replace("\\", "/"));
            File.WriteAllText(rFileName, script);

            // Run R
            R r = new R();

            r.RunToTable(rFileName);

            eeDataRaw    = ApsimTextFile.ToTable(eeFileName);
            statsDataRaw = ApsimTextFile.ToTable(statsFileName);
        }
Example #12
0
        /// <summary>
        /// Calculate factors that we need to run. Put combinations into allCombinations
        /// </summary>
        private void CalculateFactors()
        {
            if (allCombinations.Count == 0)
            {
                if (ParameterValues == null || ParameterValues.Rows.Count == 0)
                {
                    ParameterValues = RunRToGetParameterValues();
                }
                if (ParameterValues == null || ParameterValues.Rows.Count == 0)
                {
                    throw new Exception("The morris function in R returned null");
                }

                int n = NumPaths * (Parameters.Count + 1);
                // Sometimes R will return an incorrect number of parameter values, usually
                // this happens when jump is too high. In this situation, we retry up to 10 times.
                for (int numTries = 1; numTries < 10 && ParameterValues.Rows.Count != n; numTries++)
                {
                    StringBuilder msg = new StringBuilder();
                    msg.AppendLine("Morris error: Number of parameter values from R is not equal to num paths * (N + 1).");
                    msg.AppendLine($"Number of parameters from R = {ParameterValues.Rows.Count}");
                    msg.AppendLine($"NumPaths={NumPaths}");
                    msg.AppendLine($"Parameters.Count={Parameters.Count}");
                    msg.AppendLine($"Trying again...");
                    Console.WriteLine(msg.ToString());

                    ParameterValues = RunRToGetParameterValues();
                }

                if (ParameterValues.Rows.Count != n)
                {
                    // We've tried and failed 10 times to generate the right number of parameter values.
                    // Time to give up and throw a fatal.
                    StringBuilder msg = new StringBuilder();
                    msg.AppendLine("Morris error: Number of parameter values from R is not equal to num paths * (N + 1).");
                    msg.AppendLine($"Number of parameters from R = {ParameterValues.Rows.Count}");
                    msg.AppendLine($"NumPaths={NumPaths}");
                    msg.AppendLine($"Parameters.Count={Parameters.Count}");
                    msg.AppendLine($"ParameterValues as returned from R:");
                    using (StringWriter writer = new StringWriter(msg))
                        DataTableUtilities.DataTableToText(ParameterValues, 0, ",", true, writer);
                    throw new Exception(msg.ToString());
                }

                int simulationNumber = 1;
                foreach (DataRow parameterRow in ParameterValues.Rows)
                {
                    var factors = new List <CompositeFactor>();
                    foreach (Parameter param in Parameters)
                    {
                        object          value = Convert.ToDouble(parameterRow[param.Name], CultureInfo.InvariantCulture);
                        CompositeFactor f     = new CompositeFactor(param.Name, param.Path, value);
                        factors.Add(f);
                    }

                    string newSimulationName = Name + "Simulation" + simulationNumber;
                    allCombinations.Add(factors);
                    simulationNumber++;
                }
            }
        }
Example #13
0
        /// <summary>Main run method for performing our calculations and storing data.</summary>
        public void Run()
        {
            if (dataStore?.Writer != null && !dataStore.Writer.TablesModified.Contains("Report"))
            {
                return;
            }

            DataTable predictedData = dataStore.Reader.GetData("Report", filter: "SimulationName LIKE '" + Name + "%'", orderBy: "SimulationID");

            if (predictedData != null)
            {
                IndexedDataTable variableValues = new IndexedDataTable(null);

                // Determine how many years we have per simulation
                DataView view = new DataView(predictedData);
                view.RowFilter = "SimulationName='" + Name + "Simulation1'";
                var Years = DataTableUtilities.GetColumnAsIntegers(view, "Clock.Today.Year");

                // Create a results table.
                IndexedDataTable results;
                if (Years.Count() > 1)
                {
                    results = new IndexedDataTable(new string[] { "Year" });
                }
                else
                {
                    results = new IndexedDataTable(null);
                }


                // Loop through all years and perform analysis on each.
                List <string> errorsFromR = new List <string>();
                foreach (double year in Years)
                {
                    view.RowFilter = "Clock.Today.Year=" + year;

                    foreach (DataColumn predictedColumn in predictedData.Columns)
                    {
                        if (predictedColumn.DataType == typeof(double))
                        {
                            var values = DataTableUtilities.GetColumnAsDoubles(view, predictedColumn.ColumnName);
                            if (values.Distinct().Count() > 1)
                            {
                                variableValues.SetValues(predictedColumn.ColumnName, values);
                            }
                        }
                    }

                    string paramNames                  = StringUtilities.Build(Parameters.Select(p => p.Name), ",", "\"", "\"");
                    string sobolx1FileName             = GetTempFileName("sobolx1", ".csv");
                    string sobolx2FileName             = GetTempFileName("sobolx2", ".csv");
                    string sobolVariableValuesFileName = GetTempFileName("sobolvariableValues", ".csv");

                    // Write variables file
                    using (var writer = new StreamWriter(sobolVariableValuesFileName))
                        DataTableUtilities.DataTableToText(variableValues.ToTable(), 0, ",", true, writer, excelFriendly: false, decimalFormatString: "F6");

                    // Write X1
                    using (var writer = new StreamWriter(sobolx1FileName))
                        DataTableUtilities.DataTableToText(X1, 0, ",", true, writer, excelFriendly: false, decimalFormatString: "F6");

                    // Write X2
                    using (var writer = new StreamWriter(sobolx2FileName))
                        DataTableUtilities.DataTableToText(X2, 0, ",", true, writer, excelFriendly: false, decimalFormatString: "F6");

                    string script = string.Format(
                        $".libPaths(c('{R.PackagesDirectory}', .libPaths()))" + Environment.NewLine +
                        $"library('boot', lib.loc = '{R.PackagesDirectory}')" + Environment.NewLine +
                        $"library('sensitivity', lib.loc = '{R.PackagesDirectory}')" + Environment.NewLine +
                        "params <- c({0})" + Environment.NewLine +
                        "n <- {1}" + Environment.NewLine +
                        "nparams <- {2}" + Environment.NewLine +
                        "X1 <- read.csv(\"{3}\")" + Environment.NewLine +
                        "X2 <- read.csv(\"{4}\")" + Environment.NewLine +
                        "sa <- sobolSalt(model = NULL, X1, X2, scheme=\"A\", nboot = 100)" + Environment.NewLine +
                        "variableValues = read.csv(\"{5}\")" + Environment.NewLine +
                        "for (columnName in colnames(variableValues))" + Environment.NewLine +
                        "{{" + Environment.NewLine +
                        "  sa$y <- variableValues[[columnName]]" + Environment.NewLine +
                        "  tell(sa)" + Environment.NewLine +
                        "  sa$S$Parameter <- params" + Environment.NewLine +
                        "  sa$T$Parameter <- params" + Environment.NewLine +
                        "  sa$S$ColumnName <- columnName" + Environment.NewLine +
                        "  sa$T$ColumnName <- columnName" + Environment.NewLine +
                        "  sa$S$Indices <- \"FirstOrder\"" + Environment.NewLine +
                        "  sa$T$Indices <- \"Total\"" + Environment.NewLine +
                        "  if (!exists(\"allData\"))" + Environment.NewLine +
                        "    allData <- rbind(sa$S, sa$T)" + Environment.NewLine +
                        "  else" + Environment.NewLine +
                        "    allData <- rbind(allData, sa$S, sa$T)" + Environment.NewLine +
                        "}}" + Environment.NewLine +
                        "write.table(allData, sep=\",\", row.names=FALSE)" + Environment.NewLine
                        ,
                        paramNames, NumPaths, Parameters.Count,
                        sobolx1FileName.Replace("\\", "/"),
                        sobolx1FileName.Replace("\\", "/"),
                        sobolVariableValuesFileName.Replace("\\", "/"));

                    DataTable resultsForYear = null;
                    try
                    {
                        resultsForYear = RunR(script);

                        // Put output from R into results table.
                        if (Years.Count() > 1)
                        {
                            results.SetIndex(new object[] { year.ToString() });
                        }

                        foreach (DataColumn col in resultsForYear.Columns)
                        {
                            if (col.DataType == typeof(string))
                            {
                                results.SetValues(col.ColumnName, DataTableUtilities.GetColumnAsStrings(resultsForYear, col.ColumnName));
                            }
                            else
                            {
                                results.SetValues(col.ColumnName, DataTableUtilities.GetColumnAsDoubles(resultsForYear, col.ColumnName));
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        string msg = err.Message;

                        if (Years.Count() > 1)
                        {
                            msg = "Year " + year + ": " + msg;
                        }
                        errorsFromR.Add(msg);
                    }
                }
                var resultsRawTable = results.ToTable();
                resultsRawTable.TableName = Name + "Statistics";
                dataStore.Writer.WriteTable(resultsRawTable);

                if (errorsFromR.Count > 0)
                {
                    string msg = StringUtilities.BuildString(errorsFromR.ToArray(), Environment.NewLine);
                    throw new Exception(msg);
                }
            }
        }
Example #14
0
        /// <summary>
        /// Write the specified table to the TextWriter.
        /// </summary>
        /// <param name="writer">The writer to write to</param>
        /// <param name="table">The table to write</param>
        /// <param name="html">Indicates whether html format should be produced</param>
        /// <param name="includeHeadings">Include headings in the html table produced?</param>
        /// <param name="className">The class name of the generated html table</param>
        private static void WriteTable(TextWriter writer, DataTable table, bool html, bool includeHeadings, string className)
        {
            if (html)
            {
                if (className == string.Empty)
                {
                    writer.WriteLine("<table>");
                }
                else
                {
                    writer.WriteLine("<table class=\"" + className + "\">");
                }

                if (includeHeadings)
                {
                    writer.Write("<tr>");
                    foreach (DataColumn col in table.Columns)
                    {
                        writer.Write("<th>");
                        writer.Write(col.ColumnName);
                        writer.Write("</th>");
                    }

                    writer.WriteLine("</tr>");
                }

                foreach (DataRow row in table.Rows)
                {
                    bool titleRow = Convert.IsDBNull(row[0]);

                    if (titleRow)
                    {
                        writer.Write("<tr class=\"TitleRow\">");
                    }
                    else
                    {
                        writer.Write("<tr>");
                    }

                    foreach (DataColumn col in table.Columns)
                    {
                        string st = row[col].ToString();
                        if (titleRow && col.Ordinal == 0)
                        {
                            st = "Total";
                        }

                        if (className == "MessageTable")
                        {
                            if (col.Ordinal == 0)
                            {
                                writer.WriteLine("<td class=\"col1\">");
                            }
                            else
                            {
                                writer.WriteLine("<td class=\"col2\">");
                            }
                        }
                        else
                        {
                            writer.Write("<td>");
                        }

                        if (st.Contains("\n"))
                        {
                            st = st.Replace("\n", "<br/>");
                        }

                        if (st.Contains("WARNING:"))
                        {
                            st = "<p class=\"Warning\">" + st + "</p>";
                        }
                        else if (st.Contains("ERROR:"))
                        {
                            st = "<p class=\"Error\">" + st + "</p>";
                        }

                        // For property table, bold the value field.
                        if (className == "PropertyTable" && col.Ordinal == 1)
                        {
                            writer.Write("<b>" + st + "</b>");
                        }
                        else
                        {
                            writer.Write(st);
                        }

                        writer.Write("</td>");
                    }

                    writer.WriteLine("</tr>");
                }

                writer.WriteLine("</table>");
            }
            else
            {
                bool showHeadings = className != "PropertyTable";
                writer.WriteLine(DataTableUtilities.DataTableToText(table, 0, "  ", showHeadings));
            }
        }
Example #15
0
        /// <summary>Concatenates the specified output files into one file.</summary>
        /// <param name="outFiles">The out files.</param>
        private static void ConcatenateOutputFiles(string[] outFiles, string fileName, string outputFileType)
        {
            if (outFiles.Length > 0)
            {
                // Assume they are all structured the same i.e. same headings and units.
                // Read in data from all files.
                DataTable allData = null;
                foreach (string outputFileName in outFiles)
                {
                    DataTable     data   = null;
                    ApsimTextFile reader = new ApsimTextFile();
                    try
                    {
                        reader.Open(outputFileName);

                        List <string> constantsToAdd = new List <string>();
                        constantsToAdd.Add("Title");
                        data = reader.ToTable(constantsToAdd);
                    }
                    finally
                    {
                        reader.Close();
                    }

                    if (data != null && data.Columns.Count > 0 && data.Rows.Count > 0)
                    {
                        if (allData == null)
                        {
                            allData = data;
                        }
                        else
                        {
                            DataTableUtilities.CopyRows(data, allData);
                        }
                    }
                }

                if (allData != null)
                {
                    // Move the title column to be first.
                    allData.Columns["Title"].SetOrdinal(0);

                    // Strip off the outputFileType (e.g. Yearly) from the titles.
                    foreach (DataRow row in allData.Rows)
                    {
                        row["Title"] = row["Title"].ToString().Replace(outputFileType, "");
                    }

                    // Write data.
                    string workingFolder        = Path.GetDirectoryName(outFiles[0]);
                    string singleOutputFileName = Path.Combine(workingFolder, fileName);
                    using (StreamWriter outWriter = new StreamWriter(singleOutputFileName))
                        DataTableUtilities.DataTableToText(allData, 0, ",  ", true, outWriter);
                }

                // Delete the .out files.
                foreach (string outputFileName in outFiles)
                {
                    File.Delete(outputFileName);
                }
            }
        }
Example #16
0
        /// <summary>
        /// Write the specified table to the TextWriter.
        /// </summary>
        /// <param name="writer">The writer to write to</param>
        /// <param name="table">The table to write</param>
        /// <param name="outtype">Indicates the format to be produced</param>
        /// <param name="className">The class name of the generated html table</param>
        private static void WriteTable(TextWriter writer, DataTable table, OutputType outtype, string className)
        {
            bool showHeadings = className != "PropertyTable";

            if (outtype == OutputType.html)
            {
                if (showHeadings)
                {
                    writer.WriteLine("<table class='headered'>");
                    writer.Write("<tr>");
                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        writer.Write("<th");
                        if (i == 0)
                        {
                            writer.Write(" class='col1'");
                        }
                        writer.Write(">" + table.Columns[i].ColumnName + "</th>");
                    }
                    writer.WriteLine();
                }
                else
                {
                    writer.WriteLine("<table>");
                }

                foreach (DataRow row in table.Rows)
                {
                    bool titleRow = Convert.IsDBNull(row[0]);
                    if (titleRow)
                    {
                        writer.Write("<tr class='total'>");
                    }
                    else
                    {
                        writer.Write("<tr>");
                    }

                    for (int i = 0; i < table.Columns.Count; i++)
                    {
                        string st;
                        if (titleRow && i == 0)
                        {
                            st = "Total";
                        }
                        else
                        {
                            st = row[i].ToString();
                        }

                        writer.Write("<td");
                        if (i == 0)
                        {
                            writer.Write(" class='col1'");
                        }
                        writer.Write(">");
                        writer.Write(st);
                        writer.Write("</td>");
                    }
                    writer.WriteLine("</tr>");
                }
                writer.WriteLine("</table><br/>");
            }
            else if (outtype == OutputType.Markdown)
            {
                writer.WriteLine(DataTableUtilities.ToMarkdown(table, true));
                writer.WriteLine();
            }
            else
            {
                DataTableUtilities.DataTableToText(table, 0, "  ", showHeadings, writer);
            }
        }