Skip to content

Commit

Permalink
Merge pull request #8 from Stepania/TestingSystem
Browse files Browse the repository at this point in the history
Testing system
  • Loading branch information
HamishBrownPFR authored Sep 26, 2023
2 parents 1af819c + bf7663b commit a7ae7a8
Show file tree
Hide file tree
Showing 782 changed files with 276,185 additions and 1,148 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,4 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/
.ipynb_checkpoints/
2,276 changes: 1,138 additions & 1,138 deletions NutrientBalance.ipynb

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions modelCsharp/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[*.cs]

# IDE0005: Using directive is unnecessary.
dotnet_diagnostic.IDE0005.severity = none
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_prefer_braces = true:silent
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
csharp_indent_labels = one_less_than_current

[*.{cs,vb}]
#### Naming styles ####

# Naming rules

dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

# Symbol specifications

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =

# Naming styles

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_object_initializer = true:suggestion
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
indent_size = 4
end_of_line = crlf
dotnet_style_collection_initializer = true:suggestion
7 changes: 6 additions & 1 deletion modelCsharp/SVSModel.Excel/ExcelInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using SVSModel.Configuration;
using ExcelDna.Integration;
using System.Diagnostics;
using System.Linq;
using SVSModel.Models;

namespace SVSModel.Excel
{
Expand Down Expand Up @@ -46,6 +49,7 @@ public static class MyFunctions //: IMyFunctions
Dictionary<DateTime, double> _testResults = Functions.dictMaker(testResults, "Value");
Dictionary<DateTime, double> _nApplied = Functions.dictMaker(nApplied, "Amount");
var _config = new Config(Functions.dictMaker(config));

return Simulation.SimulateField(_tt, _rain, _pet, _testResults, _nApplied, _config);
}
else
Expand Down Expand Up @@ -81,7 +85,8 @@ public static class MyFunctions //: IMyFunctions
[ExcelFunction(Description = "Gets crop coefficient table")]
public static object[,] GetCropCoefficients()
{
return Functions.packDataFrame(SVSModel.Crop.LoadCropCoefficients());
return Functions.packDataFrame(Crop.LoadCropCoefficients());
}

}
}
6 changes: 4 additions & 2 deletions modelCsharp/SVSModel.Excel/SVSModel.Excel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
<TargetFramework>net48</TargetFramework>
</PropertyGroup>

<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ExcelDna.AddIn" Version="1.7.0-rc1" />
<PackageReference Include="GenericParser" Version="1.1.6" />
<PackageReference Include="Microsoft.Data.Analysis" Version="0.20.0" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions modelCsharp/SVSModel.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SVSModel", "SVSModel\SVSModel.csproj", "{255D77C7-5338-42BC-A136-8801657D51BB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SVSModel.Excel", "SVSModel.Excel\SVSModel.Excel.csproj", "{30EFA8DB-CE6D-4B36-9EBC-0A0B2CD7A444}"
ProjectSection(ProjectDependencies) = postProject
{255D77C7-5338-42BC-A136-8801657D51BB} = {255D77C7-5338-42BC-A136-8801657D51BB}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsSVS-test", "WinFormsSVS-test\WinFormsSVS-test.csproj", "{7DD24577-0C84-42E1-B10C-551BB92A718A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestModel", "TestModel\TestModel\TestModel.csproj", "{B98654E6-CEB9-4418-ACC8-DFC06055081B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +28,14 @@ Global
{30EFA8DB-CE6D-4B36-9EBC-0A0B2CD7A444}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30EFA8DB-CE6D-4B36-9EBC-0A0B2CD7A444}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30EFA8DB-CE6D-4B36-9EBC-0A0B2CD7A444}.Release|Any CPU.Build.0 = Release|Any CPU
{7DD24577-0C84-42E1-B10C-551BB92A718A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7DD24577-0C84-42E1-B10C-551BB92A718A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7DD24577-0C84-42E1-B10C-551BB92A718A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7DD24577-0C84-42E1-B10C-551BB92A718A}.Release|Any CPU.Build.0 = Release|Any CPU
{B98654E6-CEB9-4418-ACC8-DFC06055081B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B98654E6-CEB9-4418-ACC8-DFC06055081B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B98654E6-CEB9-4418-ACC8-DFC06055081B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B98654E6-CEB9-4418-ACC8-DFC06055081B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 2 additions & 3 deletions modelCsharp/SVSModel/ModelInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public interface IModelInterface
/// <param name="config">Model config object, all parameters are required</param>
/// <returns>A list of <see cref="DailyNBalance"/> objects</returns>
List<DailyNBalance> GetDailyNBalance(string weatherStation, Dictionary<DateTime, double> testResults, Dictionary<DateTime, double> nApplied, Config config);

/// <summary>
/// Gets the crop data from the data file
/// </summary>
Expand Down Expand Up @@ -108,7 +107,7 @@ private static IEnumerable<WeatherStationData> GetMetData(string weatherStation)
}
}

private static MetDataDictionaries BuildMetDataDictionaries(DateTime startDate, DateTime endDate, string weatherStation)
public static MetDataDictionaries BuildMetDataDictionaries(DateTime startDate, DateTime endDate, string weatherStation)
{
var metData = GetMetData(weatherStation).ToList();

Expand All @@ -128,7 +127,7 @@ private static MetDataDictionaries BuildMetDataDictionaries(DateTime startDate,

currDate = currDate.AddDays(1);
}

return new MetDataDictionaries { MeanT = meanT, Rain = rain, MeanPET = meanPET };
}

Expand Down
4 changes: 0 additions & 4 deletions modelCsharp/SVSModel/TestConfig.csv

This file was deleted.

200 changes: 200 additions & 0 deletions modelCsharp/TestModel/TestModel/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
using System.Linq;
using System.Diagnostics;
using SVSModel.Configuration;
using SVSModel;
using System.ComponentModel;
using SVSModel.Models;
using System.Text.Json;
using Microsoft.Data.Analysis;
using System.Xml.Linq;
using System.Reflection;
using System.Data;
using System;
using static System.Net.Mime.MediaTypeNames;
using System.Text.Json.Serialization;
//using ServiceStack;
//using ServiceStack.Text;
using System.Collections.Generic;
//using Nancy.Routing.Constraints;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting;

namespace TestModel
{
public class Test
{
private static void runPythonScript()
{
////just checking
string progToRun = @"C:\Users\1989s\source\repos\SVS\modelCsharp\TestModel\testGraph\testGraph\testGraph.py";
Process proc = new Process();
proc.StartInfo.FileName = "python.exe";
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Arguments =progToRun;
proc.Start();
StreamReader sReader = proc.StandardOutput;
proc.WaitForExit();
Console.ReadLine();

}
public static void RunTests(Dictionary<string, object> _configDict)

{

//DataFrame testConfigs = Crop.LoadCoefficients("SVSModel.Data.TestConfig.csv");

string resourceName = "TestModel.TestConfig.csv";
var assembly = Assembly.GetExecutingAssembly();
Stream csv = assembly.GetManifestResourceStream(resourceName);
DataFrame allTests = DataFrame.LoadCsv(csv);

List<string> Tests = new List<string>();

foreach (DataFrameRow row in allTests.Rows)
{
Tests.Add(row[0].ToString());
}

foreach (string test in Tests)
{
int testRow = getTestRow(test, allTests);

SVSModel.Configuration.Config _config = SetConfigFromDataFrame(test, allTests);

Dictionary<DateTime, double> testResults = new Dictionary<DateTime, double>();
Dictionary<DateTime, double> nApplied = new Dictionary<DateTime, double>();

string weatherStation = allTests["WeatherStation"][testRow].ToString();

MetDataDictionaries metData = ModelInterface.BuildMetDataDictionaries(_config.Prior.EstablishDate, _config.Following.HarvestDate.AddDays(1), weatherStation);

object[,] output = Simulation.SimulateField(metData.MeanT, metData.Rain, metData.MeanPET, testResults, nApplied, _config);

DataFrameColumn[] columns = new DataFrameColumn[13];
List<string> OutPutHeaders = new List<string>();
for (int i = 0; i< output.GetLength(1); i +=1)
{
OutPutHeaders.Add(output[0, i].ToString());
if (i == 0)
{
columns[i] = new PrimitiveDataFrameColumn<DateTime>(output[0, i].ToString());
}
else
{
columns[i] = new PrimitiveDataFrameColumn<double>(output[0, i].ToString());
}
}

var newDataframe= new DataFrame(columns);

for (int r = 1; r < output.GetLength(0); r += 1)
{
List<KeyValuePair<string, object>> nextRow = new List<KeyValuePair<string, object>>();
for (int c = 0; c < output.GetLength(1); c += 1)
{
nextRow.Add(new KeyValuePair<string, object>(OutPutHeaders[c], output[r, c]));
}
newDataframe.Append(nextRow, true);
}

DataFrame.SaveCsv(newDataframe, @"C:\Users\1989s\source\repos\svs\modelCsharp\TestModel\testGraph\OutputFiles\" + test + ".csv");

}
runPythonScript();

}

public static SVSModel.Configuration.Config SetConfigFromDataFrame(string test, DataFrame allTests)
{
int testRow = getTestRow(test, allTests);


List<string> coeffs = new List<string> { "InitialN",
"SoilOrder",
"SampleDepth",
"BulkDensity",
"PMNtype",
"PMN",
"Trigger",
"Efficiency",
"Splits",
"AWC",
"PrePlantRain",
"InCropRain",
"Irrigation",
"PriorCropNameFull",
"PriorSaleableYield",
"PriorFieldLoss",
"PriorDressingLoss",
"PriorMoistureContent",
"PriorEstablishDate",
"PriorEstablishStage",
"PriorHarvestDate",
"PriorHarvestStage",
"PriorResidueRemoval",
"PriorResidueIncorporation",
"CurrentCropNameFull",
"CurrentSaleableYield",
"CurrentFieldLoss",
"CurrentDressingLoss",
"CurrentMoistureContent",
"CurrentEstablishDate",
"CurrentEstablishStage",
"CurrentHarvestDate",
"CurrentHarvestStage",
"CurrentResidueRemoval",
"CurrentResidueIncorporation",
"FollowingCropNameFull",
"FollowingSaleableYield",
"FollowingFieldLoss",
"FollowingDressingLoss",
"FollowingMoistureContent",
"FollowingEstablishDate",
"FollowingEstablishStage",
"FollowingHarvestDate",
"FollowingHarvestStage",
"FollowingResidueRemoval",
"FollowingResidueIncorporation"
};

Dictionary<string, object> testConfigDict = new Dictionary<string, object>();
foreach (string c in coeffs)
{
testConfigDict.Add(c, allTests[c][testRow]);
}

List<string> datesNames = new List<string>(){ "PriorEstablishDate", "PriorHarvestDate", "CurrentEstablishDate", "CurrentHarvestDate", "FollowingEstablishDate", "FollowingHarvestDate" };

foreach (string dN in datesNames)
{
float year = (float)allTests[dN.Replace("Date", "") + "Year"][testRow];
float month = (float)allTests[dN.Replace("Date", "") + "Month"][testRow];
float day = (float)allTests[dN.Replace("Date", "") + "Day"][testRow];

testConfigDict[dN] = new DateTime((int)year, (int)month, (int)day);
}

SVSModel.Configuration.Config ret = new SVSModel.Configuration.Config(testConfigDict);

return ret;
}

private static int getTestRow(string test, DataFrame allTests)
{
int testRow = 0;
bool testNotFound = true;
while (testNotFound)
{
if (allTests[testRow, 0].ToString() == test)
testNotFound = false;
else
testRow += 1;
}
return testRow;
}

}
}

Loading

0 comments on commit a7ae7a8

Please sign in to comment.