using Azure; using Bogus; using HTEX.Lib.ETL.Lesson; using HTEX.Test.Controllers; using HTEX.Test.Service; using MathNet.Numerics; using MathNet.Numerics.LinearAlgebra; using System; using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using TEAMModelOS.SDK; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Helper.Common.FileHelper; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK.Models.Cosmos.Student; using static Microsoft.Azure.Amqp.Serialization.SerializableType; using static System.Runtime.InteropServices.JavaScript.JSType; namespace HTEX.Test { public class Program { public static async Task Main(string[] args) { //Test2(); await MockDataController.MockData(); //var builder = WebApplication.CreateBuilder(args); //// Add services to the container. //builder.Services.AddControllers(); //builder.Services.AddAzureStorage(builder.Configuration.GetValue("Azure:Storage:ConnectionString")); //builder.Services.AddAzureRedis(builder.Configuration.GetValue("Azure:Redis:ConnectionString")); //builder.Services.AddAzureCosmos(builder.Configuration.GetValue("Azure:Cosmos:ConnectionString")); //builder.Services.AddAzureServiceBus(builder.Configuration.GetValue("Azure:ServiceBus:ConnectionString")); //builder.Services.AddAzureSignalR(builder.Configuration.GetValue("Azure:SignalR:ConnectionString")); //builder.Services.AddSnowflakeId(Convert.ToInt64(builder.Configuration.GetValue("Option:LocationNum")), 1); //builder.Services.AddHttpClient(); //var app = builder.Build(); //// Configure the HTTP request pipeline. //app.UseHttpsRedirection(); //app.UseAuthorization(); //app.MapControllers(); //app.Run(); } static double CalculateScore(int n, double k = 1) { if (n == 0) { return 0; } else { double score = 60 + (40 / (1 + Math.Exp(-k * (n - 1)))); return Math.Max(80, Math.Min(score, 100)); } } public static string Test2() { string? pathAnalysis = $"F:\\lesson-local\\analysis"; var filesAnalysis = FileHelper.ListAllFiles(pathAnalysis); List lessonDataAnalysisMonths = new List(); LessonDataAnalysisCluster lessonDataAnalysisCluster = new LessonDataAnalysisCluster(); foreach (var file in filesAnalysis) { //读取每月的数据 if (file.EndsWith("-m-analysis.json")) { string jsons = System.IO.File.ReadAllText(file); LessonDataAnalysisMonth lessonDataAnalysisMonth = jsons.ToObject(); lessonDataAnalysisMonths.Add(lessonDataAnalysisMonth); if (lessonDataAnalysisMonth.task.IsNotEmpty()) { lessonDataAnalysisCluster.task.AddRange(lessonDataAnalysisMonth.task); } if (lessonDataAnalysisMonth.irs.IsNotEmpty()) { lessonDataAnalysisCluster.irs.AddRange(lessonDataAnalysisMonth.irs); } if (lessonDataAnalysisMonth.interactNormal.IsNotEmpty()) { lessonDataAnalysisCluster.interactNormal.AddRange(lessonDataAnalysisMonth.interactNormal); } if (lessonDataAnalysisMonth.pscore.IsNotEmpty()) { lessonDataAnalysisCluster.pscore.AddRange(lessonDataAnalysisMonth.pscore); } if (lessonDataAnalysisMonth.gscore.IsNotEmpty()) { lessonDataAnalysisCluster.gscore.AddRange(lessonDataAnalysisMonth.gscore); } if (lessonDataAnalysisMonth.tscore.IsNotEmpty()) { lessonDataAnalysisCluster.tscore.AddRange(lessonDataAnalysisMonth.tscore); } if (lessonDataAnalysisMonth.stuCowork.IsNotEmpty()) { lessonDataAnalysisCluster.stuCowork.AddRange(lessonDataAnalysisMonth.stuCowork); } if (lessonDataAnalysisMonth.groupCowork.IsNotEmpty()) { lessonDataAnalysisCluster.groupCowork.AddRange(lessonDataAnalysisMonth.groupCowork); } } } //标准差偏差N倍,视为异常数据 double thresholdMultiplier =2; lessonDataAnalysisCluster.task= LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.task.OrderBy(x => x), thresholdMultiplier); lessonDataAnalysisCluster.pscore= LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.pscore.OrderBy(x => x), thresholdMultiplier); lessonDataAnalysisCluster.gscore= LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.gscore.OrderBy(x => x), thresholdMultiplier); lessonDataAnalysisCluster.tscore= LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.tscore.OrderBy(x => x), thresholdMultiplier); lessonDataAnalysisCluster.irs = LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.irs.OrderBy(x => x), thresholdMultiplier); lessonDataAnalysisCluster.interactNormal=LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.interactNormal.OrderBy(x => x), thresholdMultiplier); lessonDataAnalysisCluster.stuCowork=LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.stuCowork.OrderBy(x => x), thresholdMultiplier); lessonDataAnalysisCluster.groupCowork=LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.groupCowork.OrderBy(x => x), thresholdMultiplier); //超过60 80的 var all = lessonDataAnalysisCluster.interactNormal.Select(x => x).OrderBy(x => x).ToArray(); double n = all.Max()+1; var clusterInteract = HTEX.Lib.ETL. KMeansService.KMeansOptimized(all,3); //foreach (var item in clusterInteract.OrderBy(x => x.Key)) //{ // lessonDataAnalysisCluster.clustersInteract.Add(new KeyValuePair>(item.Value.Average(), item.Value)); // Console.WriteLine($"dp:{item.Key} ,avg: {item.Value.Average()}, count: {item.Value.Count}, min:{item.Value.Min()}, max:{item.Value.Max()},weight:{item.Value.Count*1.0/d.Count()}"); //} //IEnumerable all = lessonDataAnalysisCluster.clustersInteract.SelectMany(x => x.Value); int pass = 0; for (var i = 1; i=60) { pass = i; break; } } var low = all.Where(x => x=80) { good = i; break; } } var medium = all.Where(x => x>=pass &&x x>=good).Average(); lessonDataAnalysisCluster.interactGood=good; lessonDataAnalysisCluster.interactPass=pass; lessonDataAnalysisCluster.interactHigh=high; lessonDataAnalysisCluster.interactMedium=medium; lessonDataAnalysisCluster.interactLow=low; List>> levelInteract = new List>>() { new KeyValuePair>(low, all.Where(x => x>(medium, all.Where(x => x>=pass &&x>(high, all.Where(x => x>=good).ToList()) }; lessonDataAnalysisCluster.levelInteract= levelInteract; double ss = 0; for (var i = 1; i> curr = new KeyValuePair>(); var s = lessonDataAnalysisCluster.levelInteract.FindAll(x => x.Value.Min()<=i && x.Value.Max()>=i).MinBy(x=>x.Key); var p = LessonETLService.GetPersent(all, i); var l= i x.Key)) //{ // clustersDataInteract.Add(s); //} //lessonDataAnalysisCluster.clustersInteract= clustersDataInteract; System.IO.File.WriteAllText(Path.Combine(pathAnalysis, "analysis.json"), lessonDataAnalysisCluster.ToJsonString()); return ""; } public static string Test() { // 创建一个随机数生成器 Random random = new Random(); // 示例数据集 var data = Enumerable.Range(1, 25000).Select(i => Vector.Build.Dense(i % 10, i % 10)).ToArray(); return "Hello World!"; } static bool[] MarkAnomalies(List array) { if (array.Count == 0) return new bool[0]; double average = array.Average(); double variance = array.Average(x => Math.Pow(x - average, 2)); double standardDeviation = Math.Sqrt(variance); // 定义异常值的阈值,这里使用2倍标准差 double threshold =3* standardDeviation; bool[] anomalies = new bool[array.Count]; for (int i = 0; i < array.Count; i++) { double deviation = Math.Abs(array[i] - average); anomalies[i] = deviation > threshold; } return anomalies; } /// /// 计算当前元素在集合中超过了多少百分比的值 /// /// /// /// public static double GetPersent(IEnumerable nums, int curr) { int count = 0; foreach (var op in nums.OrderBy(x => x)) { if (op < curr) { count++; } else if (op == curr) { count++; } else { break; } } return count *1.0/ nums.Count() * 100; } public class KeyCount { public int count { get; set;} public int key { get; set;} } class WeightedItem { public int Value { get; set; } public double Weight { get; set; } } } }