using IStation.Model;
namespace IStation.Test
{
public class TotalFlowDiffHelper
{
///
/// 输出偏差数据
///
public static void Test()
{
var bll = new BLL.StationSignalRecordPacket();
var projectId = 661070185922629;
IStation.SettingsD.Project.ID = projectId;
var monitorDataSourcesId = 606203941007429;
var dateStart = new DateTime(2024, 1, 1);
var dateEnd = new DateTime(2025, 1, 1);
var stationDict = new Dictionary
{
{ 1, 462958406303813 },
{ 2, 462958422204485 }
};
var ptFilterList = new List();
foreach (var station in stationDict)
{
var stationIndex = station.Key;
var stationId = station.Value;
var packets = bll.Get(monitorDataSourcesId, stationId);
var ptList = new List();
var records = packets.SelectMany(x => x.StationSignalRecords).ToList();
foreach (var x in records)
{
if (x.TotalPressure > 0 && x.TotalFlow > 0)
{
//if (stationIndex == 2 && Math.Abs(x.DiffFlow) > 0)
if (stationIndex == 2 && x.DiffFlow > 0)
{
continue;
}
ptList.Add(new PointViewModel(x.Time, x.TotalFlow, x.TotalPressure, x.DiffFlow, stationIndex));
}
}
//var newPtList = DynamicThresholdProcessorHelper.Filter(ptList);
var newPtList = ptList;
ptFilterList.AddRange(newPtList);
}
var recordList = new List();
var timeGroup = ptFilterList.GroupBy(x => x.Time);
foreach (var group in timeGroup)
{
if (group.Count() < 2)
continue;
var record = new RecordViewModel
{
Time = group.Key
};
foreach (var item in group)
{
if (item.Index == 1)
{
record.Flow1 = item.X;
record.Pressure1 = item.Y;
}
else
{
record.Flow2 = item.X;
record.Pressure2 = item.Y;
record.FlowDiff2 = item.Diff;
}
}
recordList.Add(record);
}
var fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "csv");
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(fullPath);
}
{
var r_list = recordList.Select(x => new PointViewModel(x.Time, Math.Round(x.Pressure2, 1), x.FlowDiff2, 0, 2)).ToList();
var group = r_list.GroupBy(x => x.X);
string filePath = Path.Combine(fullPath, "atest.csv");
using StreamWriter writer = new StreamWriter(filePath, false, System.Text.Encoding.UTF8);
string filePathLow = Path.Combine(fullPath, "alow.csv");
using StreamWriter writerLow = new StreamWriter(filePathLow, false, System.Text.Encoding.UTF8);
writer.WriteLine($"Pressure2,FlowDiffUpper,FlowDiffAverage,FlowDiffLower");
writerLow.WriteLine($"Pressure2,FlowDiffLower");
foreach (var item in group)
{
var fList = DynamicThresholdProcessorHelper.Filter(item.ToList());
var x = item.Key;
var yUpper = item.Max(x => x.Y);
var yLower = item.Min(x => x.Y);
var yAverage = item.Average(x => x.Y);
if (fList != null && fList.Any())
{
yUpper = fList.Max(x => x.Y);
yLower = fList.Min(x => x.Y);
yAverage = fList.Average(x => x.Y);
}
writer.WriteLine($"{x},{yUpper},{yAverage},{yLower}");
writerLow.WriteLine($"{x},{yLower}");
}
}
{
string filePath = Path.Combine(fullPath, "all.csv");
using StreamWriter writer = new StreamWriter(filePath, false, System.Text.Encoding.UTF8);
writer.WriteLine($"Time,Flow1,Pressure1,Flow2,Pressure2,FlowDiff2");
foreach (var record in recordList)
{
writer.WriteLine($"{record.Time},{record.Flow1},{record.Pressure1},{record.Flow2},{record.Pressure2},{record.FlowDiff2}");
}
}
return;
var dataCellList = new List() {
new() { Name = "Flow1", ValueList = recordList.Select(x => x.Flow1).ToArray() },
new() { Name = "Pressure1", ValueList = recordList.Select(x => x.Pressure1).ToArray() },
new() { Name = "Flow2", ValueList = recordList.Select(x => x.Flow2).ToArray() },
new() { Name = "Pressure2", ValueList = recordList.Select(x => x.Pressure2).ToArray() },
new() { Name = "FlowDiff2", ValueList = recordList.Select(x => x.FlowDiff2).ToArray() } };
var combineList = PermutationAndCombination.GetCombination(dataCellList.ToArray(), 2);
Console.WriteLine("输出文件");
foreach (var combine in combineList)
{
var x = combine[0];
var y = combine[1];
string filePath = Path.Combine(fullPath, $"{x.Name}-{y.Name}.csv");
using StreamWriter writer = new StreamWriter(filePath, false, System.Text.Encoding.UTF8);
var count = x.ValueList.Length;
writer.WriteLine($"{x.Name},{y.Name}");
for (int i = 0; i < count; i++)
{
var xv = x.ValueList[i];
var yv = y.ValueList[i];
writer.WriteLine($"{xv},{yv}");
}
}
Console.WriteLine("计算皮尔逊积差相关系数");
foreach (var combine in combineList)
{
var x = combine[0];
var y = combine[1];
var (r, pValue) = CorrelationHelper.PearsonTest(x.ValueList, y.ValueList);
Console.WriteLine($"{x.Name}-{y.Name}: r = {r:F3}");
}
Console.WriteLine("计算斯皮尔曼等级相关系数");
foreach (var combine in combineList)
{
var x = combine[0];
var y = combine[1];
var (rho, pValue) = CorrelationHelper.SpearmanTest(x.ValueList, y.ValueList);
Console.WriteLine($"{x.Name}-{y.Name}: ρ = {rho:F3}");
}
foreach (var combine in combineList)
{
var x = combine[0];
var y = combine[1];
string filePath = Path.Combine(fullPath, $"{x.Name}-{y.Name}.csv");
using StreamWriter writer = new StreamWriter(filePath, false, System.Text.Encoding.UTF8);
var count = x.ValueList.Length;
writer.WriteLine($"{x.Name},{y.Name}");
for (int i = 0; i < count; i++)
{
var xv = x.ValueList[i];
var yv = y.ValueList[i];
writer.WriteLine($"{xv},{yv}");
}
}
//foreach (var combine in combineList)
//{
// var x = combine[0];
// var y = combine[1];
// var name = $"{x.Name}-{y.Name}";
// if (name.Equals("Flow2-Pressure2") || name.Equals("Pressure1-Pressure2") || name.Equals("Flow1-Pressure2"))
// {
// NonlinearRegressionExample2.Test(x, y);
// }
//}
Console.WriteLine("ok");
Console.ReadKey();
}
}
}