Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Npgsql;
- using NpgsqlTypes;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Windows.Forms.DataVisualization.Charting;
- namespace DaraAnalisator
- {
- public partial class RegressionAnalysis : Form
- {
- private double a;
- private double b;
- private string element;
- private string sensorName;
- public RegressionAnalysis()
- {
- InitializeComponent();
- InitChart(chart);
- }
- private void buttonStart_Click(object sender, EventArgs e)
- {
- if ( comboBoxSensor.SelectedItem == null)
- {
- MessageBox.Show("Выберите сенсор!");
- return;
- }
- richBoxResult.Text = "";
- if ( radioButtonLine.Checked )
- {
- LinearRegr();
- }
- else if ( radioButtonPoly.Checked )
- {
- PolynomialRegr();
- }
- }
- private void LinearRegr()
- {
- Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
- Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
- const string connectionString =
- "Host=localhost;Database=turboshaft;Username=ts_engineer;Password=engineer_pass";
- using (var connection = new NpgsqlConnection(connectionString))
- {
- Sensor sensor = new Sensor(connection, element, sensorName + (comboBoxSensor.SelectedIndex + 1));
- NpgsqlDateTime now = NpgsqlDateTime.Now;
- IRegression linearRegression = new LinearRegression(sensor);
- double[] linParameters = linearRegression.CalculateParameters();
- double p = 0.8;
- Tuple<double, double> linInterval = linearRegression.ConfidenceInterval(p);
- String query = "select "+ sensorName + (comboBoxSensor.SelectedIndex + 1) + " from " + element + ";";
- var array = DataModule.Instance.ExecuteDoubleSelect(query);
- var intervals = linearRegression.ConfidenceInterval(p);
- a = linParameters[1];
- b = linParameters[0];
- BuildChart(chart, array, intervals);
- richBoxResult.AppendText(NpgsqlDateTime.Now.ToString() + "\n\n");
- richBoxResult.AppendText("y = " + linParameters[1] + " * x + " + linParameters[0] + "\n\n");
- richBoxResult.AppendText("R^2 = " + linearRegression.GetRSquared() + "\n\n");
- richBoxResult.AppendText(linearRegression.Predict(now).ToString() + "\n\n");
- richBoxResult.AppendText(p + ": " + linInterval.Item1 + " - " + linInterval.Item2 + Environment.NewLine + "\n\n");
- }
- }
- private void PolynomialRegr()
- {
- Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
- Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
- const string connectionString =
- "Host=localhost;Database=turboshaft;Username=ts_engineer;Password=engineer_pass";
- using (var connection = new NpgsqlConnection(connectionString))
- {
- Sensor sensor = new Sensor(connection, element, sensorName + (comboBoxSensor.SelectedIndex + 1));
- NpgsqlDateTime now = NpgsqlDateTime.Now;
- IRegression linearRegression = new PolynomialRegression(sensor,
- Convert.ToInt32(numericUpDownOrder.Value));
- double[] linParameters = linearRegression.CalculateParameters();
- double p = 0.8;
- Tuple<double, double> linInterval = linearRegression.ConfidenceInterval(p);
- String query = "select " + sensorName + (comboBoxSensor.SelectedIndex + 1) + " from " + element + ";";
- var array = DataModule.Instance.ExecuteDoubleSelect(query);
- var intervals = linearRegression.ConfidenceInterval(p);
- a = linParameters[1];
- b = linParameters[0];
- BuildChart(chart, array, intervals);
- richBoxResult.AppendText(NpgsqlDateTime.Now.ToString() + "\n\n");
- richBoxResult.AppendText("y = " + linParameters[1] + " * x + " + linParameters[0] + "\n\n");
- richBoxResult.AppendText("R^2 = " + linearRegression.GetRSquared() + "\n\n");
- richBoxResult.AppendText(linearRegression.Predict(now).ToString() + "\n\n");
- richBoxResult.AppendText(p + ": " + linInterval.Item1 + " - " + linInterval.Item2 + Environment.NewLine + "\n\n");
- }
- }
- private void BuildChart( Chart chart, List<double> samples, Tuple<double, double> interval)
- {
- chart.Series[0].Points.Clear();
- chart.Series[1].Points.Clear();
- chart.Series[2].Points.Clear();
- for (int x = 0; x < samples.Count; x++)
- {
- chart.Series[0].Points.AddXY(x, samples[x]);
- }
- for (int x = 0; x < samples.Count; x++)
- {
- chart.Series[1].Points.AddXY(x, interval.Item1);
- }
- for (int x = 0; x < samples.Count; x++)
- {
- chart.Series[1].Points.AddXY(x, interval.Item2);
- }
- for (int x = 0; x < samples.Count; x++)
- {
- double y = (a * x + b);
- chart.Series[2].Points.AddXY(x, y);
- }
- }
- private void InitChart(Chart chart)
- {
- chart.Series[0].ChartType = SeriesChartType.Spline;
- chart.Series[1].ChartType = SeriesChartType.Spline;
- chart.Series[2].ChartType = SeriesChartType.Spline;
- chart.Series[0].LegendText = "График";
- chart.Series[1].LegendText = "Доверительные интервалы";
- chart.Series[2].LegendText = "Аппроксимация";
- var area = chart.ChartAreas[chart.Series[0].ChartArea];
- area.AxisX.Minimum = 0;
- area.CursorX.AutoScroll = true;
- area.AxisX.ScaleView.Zoomable = true;
- area.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
- int position = 0;
- int block_size = 200;
- int size = block_size;
- area.AxisX.ScaleView.Zoom(position, size);
- area.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
- area.AxisX.ScaleView.SmallScrollSize = block_size;
- var area1 = chart.ChartAreas[chart.Series[1].ChartArea];
- area1.AxisX.Minimum = 0;
- area1.CursorX.AutoScroll = true;
- area1.AxisX.ScaleView.Zoomable = true;
- area1.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
- area1.AxisX.ScaleView.Zoom(position, size);
- area1.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
- area1.AxisX.ScaleView.SmallScrollSize = block_size;
- var area2 = chart.ChartAreas[chart.Series[0].ChartArea];
- area2.AxisX.Minimum = 0;
- area2.CursorX.AutoScroll = true;
- area2.AxisX.ScaleView.Zoomable = true;
- area2.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number;
- area2.AxisX.ScaleView.Zoom(position, size);
- area2.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
- area2.AxisX.ScaleView.SmallScrollSize = block_size;
- }
- private void radioButtonsElement_CheckedChanged(object sender, EventArgs e)
- {
- int to = 0;
- comboBoxSensor.Items.Clear();
- if (radioButtonCompressor.Checked)
- {
- to = 6;
- sensorName = "pressure_sensor_";
- element = "compressor";
- }
- else if (radioButtonСombustion.Checked)
- {
- to = 12;
- sensorName = "temperature_sensor_";
- element = "combustion_chamber";
- }
- else if (radioButtonTurbine.Checked)
- {
- to = 6;
- sensorName = "speed_sensor_";
- element = "turbine";
- }
- for (var i = 1; i <= to; i++)
- {
- comboBoxSensor.Items.Add("Sensor " + i);
- }
- comboBoxSensor.SelectedIndex = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement