public async Task FitSingleProfile(FittingProfile profile) { var d_data = WideScan.GetRestrictedData(profile.RangeBegin, profile.RangeEnd).Differentiate(_differentialWindow); var fitting_results = new FittingProfile.FittingResult(); // キーはサイクル数. EqualIntervalData target_data = new EqualIntervalData(); // 1.まず,フィッティングの計算を行う. { #region 固定参照スペクトルを取得する。(一時的にコメントアウト中) /* * List<decimal> fixed_data = new List<decimal>(); * if (FixedSpectra.Count > 0) * { * var v_data = await FixedSpectra.ForEachAsync( * async sp => await sp.GetShiftedDataAsync(d_data.Parameter, 3), 10); * * for (int j = 0; j < v_data.First().Count; j++) * { * fixed_data.Add(v_data.Sum(one => one[j])); * } * } */ #endregion /// フィッティング対象となるデータ。すなわち、もとのデータからFixされた分を差し引いたデータ。 //var target_data = fixed_data.Count > 0 ? data.Substract(fixed_data) : data; // なんだけど、とりあえずはFixedを考慮しない。 target_data = d_data.Data; //fitting_tasks.Add(i, profile.FitOneCycle(i, target_data[i], d_data.Parameter)); fitting_results = profile.FitOneCycle(-1, target_data, d_data.Parameter); } // 2.その後に,チャート出力を行う? Gnuplot charts = await Output(d_data.Parameter, profile, target_data, fitting_results); // pltファイルも出力してみる。 using (var writer = new StreamWriter(GetCsvFileName(profile.Name, -1) + ".plt")) { await charts.OutputPltFileAsync(writer); } // チャートを描画する。 await charts.Draw(); }
// (0.1.0)メソッド名をFitからOutputに変更.というか,これどこにおけばいいのかな? private async Task <Gnuplot> Output( ScanParameter originalParameter, FittingProfile profile, EqualIntervalData target_data, FittingProfile.FittingResult result) { // フィッティングした結果をチャートにする? // ★とりあえずFixedなデータは表示しない。 bool output_convolution = result.Standards.Count > 1; // それには、csvを出力する必要がある。 //string fitted_csv_path = Path.Combine(FittingCondition.OutputDestination, $"{FittingCondition.Name}_{layer}.csv"); using (var csv_writer = new StreamWriter(GetCsvFileName(profile.Name))) { await OutputFittedCsv(csv_writer, originalParameter, target_data, result, output_convolution).ConfigureAwait(false); } // チャート出力の準備? return(ConfigureChart(result, profile, output_convolution)); }
// (0.1.0)メソッド名をFitからOutputに変更.というか,これどこにおけばいいのかな? #region *CSVを出力して,グラフ描画の準備を行う(Output) /// <summary> /// フィッティングした結果から,チャートの出力を設定します. /// </summary> /// <param name="cycle"></param> /// <param name="originalParameter"></param> /// <param name="profile"></param> /// <param name="target_data"></param> /// <param name="result"></param> /// <returns></returns> private async Task <Gnuplot> Output( int cycle, ScanParameter originalParameter, FittingProfile profile, EqualIntervalData target_data, FittingProfile.FittingResult result) { // フィッティングした結果をチャートにする? // ★とりあえずFixedなデータは表示しない。 Trace.WriteLine($"Let's start outputting! cycle{cycle} {DateTime.Now} [{Thread.CurrentThread.ManagedThreadId}]"); bool output_convolution = result.Standards.Count > 1; // それには、csvを出力する必要がある。 using (var csv_writer = new StreamWriter(GetCsvFileName(cycle, profile.Name))) { await OutputFittedCsv(csv_writer, originalParameter, target_data, result, output_convolution); } Trace.WriteLine($"CSV output Completed! cycle{cycle} {DateTime.Now} [{Thread.CurrentThread.ManagedThreadId}]"); // チャート出力の準備? return(ConfigureChart(result, profile, output_convolution, cycle)); #region こうすると,1024バイトほど書き込んだところで落ちる. // どう違うのかはよくわかっていない. /* * Gnuplot chartConfiguration; * // chartConfigurationを構成して返す処理とcsv出力処理が入り交じっているので注意.(こういう書き方しか思いつかなかった.) * using (var csv_writer = new StreamWriter(GetCsvFileName(cycle, profile.Name))) * { * //var outputCsvTask = OutputFittedCsv(csv_writer, originalParameter, target_data, result, output_convolution); * //chartConfiguration = ConfigureChart(cycle, result, profile, output_convolution); * //await outputCsvTask; * await OutputFittedCsv(csv_writer, originalParameter, target_data, result, output_convolution); * } * return chartConfiguration; */ #endregion }
/// <summary> /// 具体的なチャートの設定を行います. /// </summary> /// <param name="cycle"></param> /// <param name="result"></param> /// <param name="profile"></param> /// <param name="outputConvolution"></param> /// <returns></returns> public Gnuplot ConfigureChart(FittingProfile.FittingResult result, FittingProfile profile, bool outputConvolution, int cycle = -1) { string chart_ext = string.Empty; switch (FittingCondition.ChartFormat) { case ChartFormat.Png: chart_ext = ".png"; break; case ChartFormat.Svg: chart_ext = ".svg"; break; } var chart_destination = Path.Combine(FittingCondition.OutputDestination, $"{profile.Name}{(cycle >= 0 ? string.Format("_{0}", cycle) : string.Empty)}{chart_ext}"); #region チャート設定 var gnuplot = new Gnuplot { Format = FittingCondition.ChartFormat, Width = 800, Height = 600, FontScale = 1.6, Destination = chart_destination, XTitle = "Kinetic Energy / eV", YTitle = "dN(E)/dE", Title = (cycle >= 0 ? $"Cycle {cycle} , " : string.Empty) + $"Shift {result.Shift} eV" }; var source_csv = GetCsvFileName(profile.Name, cycle); gnuplot.DataSeries.Add(new LineChartSeries { SourceFile = source_csv, XColumn = 1, YColumn = 2, Title = "data", Style = new LineChartSeriesStyle(LineChartStyle.Lines) { Style = new LinePointStyle { LineColor = "#FF0000", LineWidth = 3, } } }); var reference_names = profile.ReferenceSpectra.Select(r => r.Name).ToList(); for (int j = 0; j < reference_names.Count; j++) { gnuplot.DataSeries.Add(new LineChartSeries { SourceFile = source_csv, XColumn = 1, YColumn = j + 3, Title = $"{result.Gains[j].ToString("f3")} * {reference_names[j]}", Style = new LineChartSeriesStyle(LineChartStyle.Lines) { Style = new LinePointStyle { LineColorIndex = j, LineWidth = 2, } } }); } if (outputConvolution) { gnuplot.DataSeries.Add(new LineChartSeries { SourceFile = source_csv, XColumn = 1, YColumn = reference_names.Count + 3, Title = "Convolution", Style = new LineChartSeriesStyle(LineChartStyle.Lines) { Style = new LinePointStyle { LineColor = "#0000FF", LineWidth = 3, } } }); } #endregion gnuplot.PreConfigureAxis(); return(gnuplot); }
private async Task OutputFittedCsv(StreamWriter writer, ScanParameter originalParameter, EqualIntervalData targetData, FittingProfile.FittingResult result, bool outputConvolution) { for (int k = 0; k < originalParameter.PointsCount; k++) { List <string> cols = new List <string>(); // 0列: エネルギー値 cols.Add((originalParameter.Start + k * originalParameter.Step + result.Shift).ToString("f2")); // 1列: 測定データ cols.Add(targetData[k].ToString("f3")); // 2~N列: 各成分(標準データ×ゲイン) decimal conv = 0; for (int j = 0; j < result.Standards.Count; j++) { var intensity = result.GetGainedStandard(j, k); conv += intensity; cols.Add(intensity.ToString("f3")); } // 最終列: 成分の総和 if (outputConvolution) { cols.Add(conv.ToString("f3")); } await writer.WriteLineAsync(string.Join(",", cols)); } }