diff --git a/DataAnalysis/Analysis.cs b/DataAnalysis/Analysis.cs new file mode 100644 index 0000000..6402f0d --- /dev/null +++ b/DataAnalysis/Analysis.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAnalysis { + + class Analysis { + List _data = new List(); + const double ignoreTime = 2000.0D; + + public void Analyze() { + var imglist = new List() { 1, 2, 3 }; + foreach (var imgid in imglist) { + var logall = _data.Where(s => s.image == imgid).OrderBy(s => s.etime); + var startTime = logall.Min(s => s.etime); + var endTime = logall.Max(s => s.etime); + //Console.WriteLine($"image {imgid} {startTime} - {endTime}ms"); + //var grance = logall.Take(10); + //foreach (var d in grance) { + // Console.WriteLine($"{d.etime}, {d.image}, ({d.x}, {d.y})"); + //} + var target = logall.Where(s => s.etime > startTime + ignoreTime); + var mx = target.Average(s => s.x); + var my = target.Average(s => s.y); + var ssumx = 0D; + var ssumy = 0D; + foreach (var d in target) { + ssumx += (d.x - mx) * (d.x - mx); + ssumy += (d.y - my) * (d.y - my); + Console.WriteLine($"{d.etime},{d.image},{d.x - mx},{d.y - my}"); + } + var sdx = Math.Sqrt(ssumx / (double)target.Count()); + var sdy = Math.Sqrt(ssumy / (double)target.Count()); + //Console.WriteLine($"SD x={sdx}, y={sdy}"); + Console.WriteLine(",,,"); + } + } + + public void ReadData(string filename) { + + var csv = new CsvFile(); + csv.Open(filename, new List()); + + foreach (var d in csv.Data) { + if (d.Length != 4) { + Console.WriteLine("CSVデータが4列になっていません"); + return; + } + var logdata = new LogData(); + if (!double.TryParse(d[0], out logdata.etime)) { + Console.WriteLine("CSVデータの1列目が実数になっていません"); + return; + } + if (!int.TryParse(d[1], out logdata.image)) { + Console.WriteLine("CSVデータの2列目が整数になっていません"); + return; + } + if (!double.TryParse(d[2], out logdata.x)) { + Console.WriteLine("CSVデータの3列目が実数になっていません"); + return; + } + if (!double.TryParse(d[3], out logdata.y)) { + Console.WriteLine("CSVデータの4列目が実数になっていません"); + return; + } + _data.Add(logdata); + } + //Console.WriteLine($"{_data.Count}件のデータを読み込みました"); + } + } +} diff --git a/DataAnalysis/CsvFile.cs b/DataAnalysis/CsvFile.cs new file mode 100644 index 0000000..49d5cc6 --- /dev/null +++ b/DataAnalysis/CsvFile.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using System.Diagnostics; +using Microsoft.VisualBasic.FileIO; + +namespace DataAnalysis { + + class CsvFile { + public string[] Headers { get; private set; } + public List Data { get; private set; } + public int Cols { get { return Headers.Length; } } + public int Rows { get { return Data.Count; } } + public bool IsOpen { get { return Headers != null; } } + public string ErrorMsg { get; private set; } + Dictionary _HeaderIndex; + + /// + /// ファイルを開く + /// + /// ファイル名 + /// 必須列名 + public void Open(string filename, IEnumerable requiredFields) { + var contents = ReadContents(filename); + Headers = contents.First(); + if (!requiredFields.All(s => Headers.Contains(s))) { + Headers = null; + Data = null; + throw new Exception("CSVファイルに必要な列がありません"); + } + Data = contents.Skip(1).ToList(); + + // インデックス辞書作成 + _HeaderIndex = Headers.Select((s, idx) => new { str = s, idx = idx }) + .ToDictionary(s => s.str, s => s.idx); + } + + /// + /// ヘッダの列番号(インデックス)を返す + /// + /// 列番号(0開始) + /// + public int HeaderIndex(string header) { + return _HeaderIndex[header]; + } + + /// + /// CSVファイルを読み込む + /// + /// CSVファイル名 + /// デリミタ + /// エンコード + /// + public static IEnumerable ReadContents( + string path, string separator = ",", Encoding encoding = null) { + + using (Stream stream = + new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) + + using (TextFieldParser parser = + new TextFieldParser(stream, encoding ?? Encoding.GetEncoding("Shift_JIS"), true, false)) { + + parser.TextFieldType = FieldType.Delimited; + parser.Delimiters = new[] { separator }; + parser.HasFieldsEnclosedInQuotes = true; + parser.TrimWhiteSpace = true; + while (parser.EndOfData == false) { + string[] fields = parser.ReadFields(); + yield return fields; + } + } + } + } +} diff --git a/DataAnalysis/DataAnalysis.csproj b/DataAnalysis/DataAnalysis.csproj index e115f61..e0d4fdf 100644 --- a/DataAnalysis/DataAnalysis.csproj +++ b/DataAnalysis/DataAnalysis.csproj @@ -33,6 +33,7 @@ 4 + @@ -43,6 +44,9 @@ + + + diff --git a/DataAnalysis/LogData.cs b/DataAnalysis/LogData.cs new file mode 100644 index 0000000..8ebc383 --- /dev/null +++ b/DataAnalysis/LogData.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAnalysis { + class LogData { + public double etime; + public int image; + public double x; + public double y; + } +} diff --git a/DataAnalysis/Program.cs b/DataAnalysis/Program.cs index b676db7..e0863f7 100644 --- a/DataAnalysis/Program.cs +++ b/DataAnalysis/Program.cs @@ -4,18 +4,17 @@ using System.Text; using System.Threading.Tasks; using System.IO; +using System.Diagnostics; namespace DataAnalysis { + class Program { static void Main(string[] args) { - StreamReader sr = new StreamReader(@"D:\usr\prog\PrismSoftware\log\1_oguchi\log20191220_131538.csv"); - while (!sr.EndOfStream) { - var line = sr.ReadLine(); - var values = line.Split(','); - - } - - System.Console.ReadKey(); + var filename = @"D:\usr\DL\log\1_oguchi\log20191220_131538.csv"; + var analysis = new Analysis(); + analysis.ReadData(filename); + analysis.Analyze(); + //System.Console.ReadKey(); }