-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSplineFitting.cs
103 lines (88 loc) · 3.71 KB
/
SplineFitting.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO;
namespace AutomatedLIBS {
public partial class SplineFitting : Form {
private List<Spectrum> mSpectra;
public List<RawSpectrum> mSplines = new List<RawSpectrum>();
public bool mApplyResult = false;
public SplineFitting(List<Spectrum> spectra) {
InitializeComponent();
mSpectra = spectra;
nudShowSpectrumNumber.Minimum = 1;
nudShowSpectrumNumber.Maximum = spectra.Count;
lblSpectrumCount.Text = spectra.Count.ToString();
foreach(var spectrum in spectra) {
mSplines.Add(spectrum.GetFittingSpline((int)nudSubdivisionsCount.Value));
}
UpdateChart();
}
void UpdateChart() {
chrMainChart.Series.Clear();
int number = (int)nudShowSpectrumNumber.Value - 1;
PlotSpectrum(mSpectra[number], number);
PlotSpline(mSplines[number], number);
}
void PlotSpectrum(Spectrum spec, int n) {
var name = "Spectrum " + n;
var series = chrMainChart.Series.Add(name);
series.ChartType = SeriesChartType.FastLine;
for (int i = 0; i < spec.mAbsoluteIntensities.Value.Count(); ++i) {
series.Points.AddXY(Math.Round(spec.mWaveLengths.Value[i], 2), spec.mAbsoluteIntensities.Value[i]);
}
}
void PlotSpline(RawSpectrum spec, int n) {
var name = "Spline " + n;
var series = chrMainChart.Series.Add(name);
series.ChartType = SeriesChartType.FastLine;
for (int i = 0; i < spec.mAbsoluteIntensities.Count; ++i) {
series.Points.AddXY(Math.Round(spec.mWavelengths[i], 2), spec.mAbsoluteIntensities[i]);
}
}
private void SplineFitting_Load(object sender, EventArgs e) {
}
private void nudShowSpectrumNumber_ValueChanged(object sender, EventArgs e) {
UpdateChart();
}
private void nudSubdivisionsCount_ValueChanged(object sender, EventArgs e) {
for (int i = 0; i < mSpectra.Count; ++i) {
mSplines[i] = mSpectra[i].GetFittingSpline((int)nudSubdivisionsCount.Value);
}
UpdateChart();
}
private void SplineFitting_Resize(object sender, EventArgs e) {
int px = 20;
int py = 50;
if (ClientSize.Width > px && ClientSize.Height > py) {
chrMainChart.Size = new Size(ClientSize.Width - px, ClientSize.Height - py);
}
}
private void btnApply_Click(object sender, EventArgs e) {
mApplyResult = true;
Close();
}
private void btnSaveSplines_Click(object sender, EventArgs e) {
FolderBrowserDialog dlg = new FolderBrowserDialog();
if(dlg.ShowDialog() == DialogResult.OK) {
var basePath = dlg.SelectedPath;
if (basePath != "") {
for (int i = 0; i < mSplines.Count; ++i) {
StreamWriter s = new StreamWriter(basePath + "/" + "spline" + i + ".txt");
for(int j = 0; j < mSplines[i].mAbsoluteIntensities.Count; ++j) {
s.WriteLine(mSplines[i].mWavelengths[j] + "\t" + mSplines[i].mAbsoluteIntensities[j]);
}
s.Close();
}
}
}
}
}
}