ningshuxia
2025-04-09 845ce9e76fab972e3d19672c1f1fbb3a6a05666f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using IStation.Model;
 
namespace IStation.Test
{
    public class TotalFlowDiffHelper
    {
        /// <summary>
        /// 输出偏差数据
        /// </summary>
        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<int, long>
            {
                { 1, 462958406303813 },
                { 2, 462958422204485 }
            };
 
 
            var ptFilterList = new List<PointViewModel>();
            foreach (var station in stationDict)
            {
                var stationIndex = station.Key;
                var stationId = station.Value;
                var packets = bll.Get(monitorDataSourcesId, stationId);
                var ptList = new List<PointViewModel>();
 
                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<RecordViewModel>();
            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<DataCellViewModel>() {
                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<DataCellViewModel>.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();
        }
 
 
 
 
 
 
      
    }
 
 
}