private void ResultsForm_Shown(object sender, EventArgs e)
        {
            //1.1 Calculate base arrays (Ratio and Abs) for final statistics calculation
            double[] ZRatioArrayOld =
                CalibrationMethods.CalibrationRatioArrayCalculation(ParentApp.XDataArray, ParentApp.YDataArray, ParentApp.ZDataArray,
                                                                    ParentApp.XCalArray, ParentApp.YCalArray, ParentApp.ZCalTab);
            double[] ZRatioArrayOptm =
                CalibrationMethods.CalibrationRatioArrayCalculation(ParentApp.XDataArray, ParentApp.YDataArray, ParentApp.ZDataArray,
                                                                    ParentApp.XCalArray, ParentApp.YCalArray, ParentApp.ZCalTabOptm);
            double[] ZCalTabArrayOld =
                CalibrationMethods.CalibrationAbsoluteArrayCalculation(ParentApp.XDataArray, ParentApp.YDataArray, ParentApp.ZDataArray,
                                                                       ParentApp.XCalArray, ParentApp.YCalArray, ParentApp.ZCalTab);
            double[] ZCalTabArrayOptm =
                CalibrationMethods.CalibrationAbsoluteArrayCalculation(ParentApp.XDataArray, ParentApp.YDataArray, ParentApp.ZDataArray,
                                                                       ParentApp.XCalArray, ParentApp.YCalArray, ParentApp.ZCalTabOptm);

            //1.2 Calculate Mean and StdDev values (Ratio and Abs) from base arrays
            double ZMeanOld       = Math.Round(VectorStatBasicMethods.Mean(ZRatioArrayOld), Math.Max(3, ParentApp.DataPrecision + 1));
            double ZStdDevOld     = Math.Round(VectorStatBasicMethods.StdDev(ZRatioArrayOld), Math.Max(3, ParentApp.DataPrecision + 1));
            double ZAbsErrMeanOld = Math.Round(VectorStatBasicMethods.ErrorsAvg(ParentApp.ZDataArray, ZCalTabArrayOld),
                                               Math.Max(3, ParentApp.DataPrecision + 1));
            double ZAbsErrStdDOld = Math.Round(VectorStatBasicMethods.ErrorsStdDev(ParentApp.ZDataArray, ZCalTabArrayOld),
                                               Math.Max(3, ParentApp.DataPrecision + 1));

            double ZMeanOptm       = Math.Round(VectorStatBasicMethods.Mean(ZRatioArrayOptm), Math.Max(3, ParentApp.DataPrecision + 1));
            double ZStdDevOptm     = Math.Round(VectorStatBasicMethods.StdDev(ZRatioArrayOptm), Math.Max(3, ParentApp.DataPrecision + 1));
            double ZAbsErrMeanOptm = Math.Round(VectorStatBasicMethods.ErrorsAvg(ParentApp.ZDataArray, ZCalTabArrayOptm),
                                                Math.Max(3, ParentApp.DataPrecision + 1));
            double ZAbsErrStdDOptm = Math.Round(VectorStatBasicMethods.ErrorsStdDev(ParentApp.ZDataArray, ZCalTabArrayOptm),
                                                Math.Max(3, ParentApp.DataPrecision + 1));

            //1.3 Write calculated values on the corresponding labels
            CurrMeanLabel.Text       = ZMeanOld.ToString();
            CurrStdDevLabel.Text     = ZStdDevOld.ToString();
            CurrAbsErrMeanLabel.Text = ZAbsErrMeanOld.ToString();
            CurrAbsErrStdDLabel.Text = ZAbsErrStdDOld.ToString();

            OptmMeanLabel.Text       = ZMeanOptm.ToString();
            OptmStdDevLabel.Text     = ZStdDevOptm.ToString();
            OptmAbsErrMeanLabel.Text = ZAbsErrMeanOptm.ToString();
            OptmAbsErrStdDLabel.Text = ZAbsErrStdDOptm.ToString();

            TableBox.Text  = TransformationMethods.VectorTable2TextTable(ParentApp.ZCalTabOptm);
            XArrayBox.Text = TransformationMethods.VectorLin2TextLin(ParentApp.XCalArray);
            YArrayBox.Text = TransformationMethods.VectorCol2TextCol(ParentApp.YCalArray);

            //2. Open the Results Convergency pattern new window
            ResConvForm Convergency = new ResConvForm(ParentApp.FitType);

            Convergency.Show();
            Convergency.PlotConvergencyCurve(ParentApp.ErrStep,
                                             ParentApp.StdDStep);
        }
        private void CalibrationDataForm_Load(object sender, EventArgs e)
        {
            ZRatioArray = CalibrationMethods.CalibrationRatioArrayCalculation(
                ParentApp.XDataArray, ParentApp.YDataArray, ParentApp.ZDataArray,
                ParentApp.XCalArray, ParentApp.YCalArray, ParentApp.ZCalTab);

            ZCalTabArray = CalibrationMethods.CalibrationAbsoluteArrayCalculation(
                ParentApp.XDataArray, ParentApp.YDataArray, ParentApp.ZDataArray,
                ParentApp.XCalArray, ParentApp.YCalArray, ParentApp.ZCalTab);

            ZRatioMean   = VectorStatBasicMethods.Mean(ZRatioArray);
            ZRatioStdDev = VectorStatBasicMethods.StdDev(ZRatioArray);
            ZAbsErrMean  = VectorStatBasicMethods.ErrorsAvg(ZCalTabArray, ParentApp.ZDataArray);
            ZAbsErrStdD  = VectorStatBasicMethods.ErrorsStdDev(ZCalTabArray, ParentApp.ZDataArray);

            CurrMeanLabel.Text       = ZRatioMean.ToString();
            CurrStdDevLabel.Text     = ZRatioStdDev.ToString();
            CurrAbsErrLabel.Text     = ZAbsErrMean.ToString();
            CurrAbsErrStdDLabel.Text = ZAbsErrStdD.ToString();
        }
示例#3
0
        } //Calculates resulting Z calibrated value for each provided X, Y data point

        static public double[,] CalibrationTabOptimizationWiPB(double MeanTar, int PrecisionTar,
                                                               int WeightBox, int FineTuneIterBox, int FineTuneSubIterBox, bool FitType,
                                                               double[] DataX, double[] DataY, double[] DataZ,
                                                               double[] XBkpt, double[] YBkpt, double[,] ZTab,
                                                               ref ProgressBoxInvokation ProgressBox, ref double[] ErrStep, ref double[] StdDStep) // WiPB = With Progress Box
        {
            double X_A, X_B, Y_A, Y_B, ZMeanInterval;

            double[,] ZWorkTab = new double[1 + ZTab.GetUpperBound(0), 1 + ZTab.GetUpperBound(1)];
            double[] ZRatio = new double[DataZ.Length];
            byte[,] ZTabStatus = new byte[1 + ZTab.GetUpperBound(0), 1 + ZTab.GetUpperBound(1)];


            //0. Populate the iteration converging error vector (first position) to be plotted afterwards
            if (FitType == false)
            {
                ErrStep[ErrStep.GetLowerBound(0)] = VectorStatBasicMethods.ErrorsAvg(DataZ,
                                                                                     CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZTab));
                StdDStep[ErrStep.GetLowerBound(0)] = VectorStatBasicMethods.ErrorsStdDev(DataZ,
                                                                                         CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZTab));
            }
            else
            {
                ErrStep[ErrStep.GetLowerBound(0)] = VectorStatBasicMethods.Mean(
                    CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZTab));
                StdDStep[ErrStep.GetLowerBound(0)] = VectorStatBasicMethods.StdDev(
                    CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZTab));
            }

            //1. Sweep each table point and get average value from delivered data
            for (int i = 0; i < 1 + ZWorkTab.GetUpperBound(0); i++)     //i sweeps Y
            {
                for (int j = 0; j < 1 + ZWorkTab.GetUpperBound(1); j++) //j sweeps X
                {
                    //1.1 Select interval around table current point - X Values
                    if (j == 0)
                    {
                        X_A = XBkpt[j] - ((XBkpt[j + 1] - XBkpt[j]) / 2);
                        X_B = (XBkpt[j + 1] + XBkpt[j]) / 2;
                    }
                    else if (j == ZWorkTab.GetUpperBound(1))
                    {
                        X_A = (XBkpt[j] + XBkpt[j - 1]) / 2;
                        X_B = XBkpt[j] + ((XBkpt[j] - XBkpt[j - 1]) / 2);
                    }
                    else
                    {
                        X_A = (XBkpt[j] + XBkpt[j - 1]) / 2;
                        X_B = (XBkpt[j + 1] + XBkpt[j]) / 2;
                    }

                    //1.2 Select interval around table current point - Y Values
                    if (i == 0)
                    {
                        Y_A = YBkpt[i] - ((YBkpt[i + 1] - YBkpt[i]) / 2);
                        Y_B = (YBkpt[i + 1] + YBkpt[i]) / 2;
                    }
                    else if (i == ZWorkTab.GetUpperBound(0))
                    {
                        Y_A = (YBkpt[i] + YBkpt[i - 1]) / 2;
                        Y_B = YBkpt[i] + ((YBkpt[i] - YBkpt[i - 1]) / 2);
                    }
                    else
                    {
                        Y_A = (YBkpt[i] + YBkpt[i - 1]) / 2;
                        Y_B = (YBkpt[i + 1] + YBkpt[i]) / 2;
                    }

                    //1.3 Collect data corresponding to the targeted interval
                    int m_A = 0, m_B = 0, m_C = 0, m_D = 0;
                    //1.3.1 Num. of positions needed for each array
                    for (int k = 0; k < DataX.Length; k++)
                    {
                        m_A += (DataX[k] >= X_A && DataX[k] < XBkpt[j] && DataY[k] >= Y_A && DataY[k] < YBkpt[i]) ? 1 : 0;
                        m_B += (DataX[k] >= XBkpt[j] && DataX[k] < X_B && DataY[k] >= Y_A && DataY[k] < YBkpt[i]) ? 1 : 0;
                        m_C += (DataX[k] >= X_A && DataX[k] < XBkpt[j] && DataY[k] >= YBkpt[i] && DataY[k] < Y_B) ? 1 : 0;
                        m_D += (DataX[k] >= XBkpt[j] && DataX[k] < X_B && DataY[k] >= YBkpt[i] && DataY[k] < Y_B) ? 1 : 0;
                    }

                    double[] DataZSel_A = new double[m_A];
                    double[] DataZWei_A = new double[m_A];

                    double[] DataZSel_B = new double[m_B];
                    double[] DataZWei_B = new double[m_B];

                    double[] DataZSel_C = new double[m_C];
                    double[] DataZWei_C = new double[m_C];

                    double[] DataZSel_D = new double[m_D];
                    double[] DataZWei_D = new double[m_D];

                    m_A = 0;
                    m_B = 0;
                    m_C = 0;
                    m_D = 0;

                    //1.3.2 - Z values and weight calculation for each interval
                    for (int k = 0; k < DataX.Length; k++)
                    {
                        if (DataX[k] >= X_A && DataX[k] < XBkpt[j] && DataY[k] >= Y_A && DataY[k] < YBkpt[i])
                        {
                            DataZSel_A[m_A] = DataZ[k];
                            DataZWei_A[m_A] = TabManagementMethods.PointsDist2D((DataX[k] - X_A) / (XBkpt[j] - X_A),
                                                                                (DataY[k] - Y_A) / (YBkpt[i] - Y_A),
                                                                                0, 0);
                            m_A++;
                        }
                        else if (DataX[k] >= XBkpt[j] && DataX[k] < X_B && DataY[k] >= Y_A && DataY[k] < YBkpt[i])
                        {
                            DataZSel_B[m_B] = DataZ[k];
                            DataZWei_B[m_B] = TabManagementMethods.PointsDist2D((DataX[k] - X_B) / (XBkpt[j] - X_B),
                                                                                (DataY[k] - Y_A) / (YBkpt[i] - Y_A),
                                                                                0, 0);
                            m_B++;
                        }
                        else if (DataX[k] >= X_A && DataX[k] < XBkpt[j] && DataY[k] >= YBkpt[i] && DataY[k] < Y_B)
                        {
                            DataZSel_C[m_C] = DataZ[k];
                            DataZWei_C[m_C] = TabManagementMethods.PointsDist2D((DataX[k] - X_A) / (XBkpt[j] - X_A),
                                                                                (DataY[k] - Y_B) / (YBkpt[i] - Y_B),
                                                                                0, 0);
                            m_C++;
                        }
                        else if (DataX[k] >= XBkpt[j] && DataX[k] < X_B && DataY[k] >= YBkpt[i] && DataY[k] < Y_B)
                        {
                            DataZSel_D[m_D] = DataZ[k];
                            DataZWei_D[m_D] = TabManagementMethods.PointsDist2D((DataX[k] - X_B) / (XBkpt[j] - X_B),
                                                                                (DataY[k] - Y_B) / (YBkpt[i] - Y_B),
                                                                                0, 0);
                            m_D++;
                        }
                    }

                    //1.4 Calculate ZMean, within selected interval
                    byte b_A = 1, b_B = 1, b_C = 1, b_D = 1;

                    b_A = DataZSel_A.Length == 0 ? (byte)0 : (byte)1;
                    b_B = DataZSel_B.Length == 0 ? (byte)0 : (byte)1;
                    b_C = DataZSel_C.Length == 0 ? (byte)0 : (byte)1;
                    b_D = DataZSel_D.Length == 0 ? (byte)0 : (byte)1;

                    ZMeanInterval = (b_A * VectorStatBasicMethods.MeanWeighted(DataZSel_A, DataZWei_A, WeightBox) +
                                     b_B * VectorStatBasicMethods.MeanWeighted(DataZSel_B, DataZWei_B, WeightBox) +
                                     b_C * VectorStatBasicMethods.MeanWeighted(DataZSel_C, DataZWei_C, WeightBox) +
                                     b_D * VectorStatBasicMethods.MeanWeighted(DataZSel_D, DataZWei_D, WeightBox)) / (b_A + b_B + b_C + b_D);

                    //1.5 Input mean optimized value (based on data) in ZWorkTab
                    ZWorkTab[i, j] = ZMeanInterval;

                    //1.6 Track/Flag the table values which didn't have associated data to be calculated
                    ZTabStatus[i, j] = (b_A + b_B + b_C + b_D == 0) ? (byte)0 : (byte)1;
                    ZWorkTab[i, j]   = (b_A + b_B + b_C + b_D == 0) ? Math.Pow(10, -1 * (PrecisionTar + 1)) : ZWorkTab[i, j];
                }
            }

            //2. Sweep empty table points (due to lack of data) and estimate value by neighbors
            for (int i = 0; i < 1 + ZWorkTab.GetUpperBound(0); i++)     //i sweeps Y
            {
                for (int j = 0; j < 1 + ZWorkTab.GetUpperBound(1); j++) //j sweeps X
                {
                    ZWorkTab[i, j] += (ZWorkTab[i, j] == 0) ? Math.Pow(10, -1 * (PrecisionTar + 1)) : 0;

                    if (ZTabStatus[i, j] == 0)// Empty position gotten
                    {
                        double Z_Ya = 0, Z_Yb = 0, Z_Xa = 0, Z_Xb = 0;
                        byte   b_Ya = 0, b_Yb = 0, b_Xa = 0, b_Xb = 0;

                        Z_Ya = (i == 0) ? 0 : ZWorkTab[i - 1, j];
                        Z_Yb = (i == ZWorkTab.GetUpperBound(0)) ? 0 : ZWorkTab[i + 1, j];
                        Z_Xa = (j == 0) ? 0 : ZWorkTab[i, j - 1];
                        Z_Xb = (j == ZWorkTab.GetUpperBound(1)) ? 0 : ZWorkTab[i, j + 1];

                        b_Ya = (byte)((i == 0) ? 0 : ZTabStatus[i - 1, j]);
                        b_Yb = (byte)((i == ZWorkTab.GetUpperBound(0)) ? 0 : ZTabStatus[i + 1, j]);
                        b_Xa = (byte)((j == 0) ? 0 : ZTabStatus[i, j - 1]);
                        b_Xb = (byte)((j == ZWorkTab.GetUpperBound(1)) ? 0 : ZTabStatus[i, j + 1]);


                        ZWorkTab[i, j] = (b_Ya + b_Yb + b_Xa + b_Xb) != 0 ?
                                         (b_Ya * Z_Ya + b_Yb * Z_Yb + b_Xa * Z_Xa + b_Xb * Z_Xb) / (b_Ya + b_Yb + b_Xa + b_Xb) :
                                         Math.Pow(10, -1 * (PrecisionTar + 1));

                        ZWorkTab[i, j] += Math.Pow(10, -1 * (PrecisionTar + 1));
                    }
                }
            }

            //3. Sweep each table point and adjust to dimish Z Ratio average deviation
            double MFactor = 1.25;

            double[] ZRatioArray = CalibrationMethods.CalibrationRatioArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab);
            double   ZErr        = VectorStatBasicMethods.ErrorsStdDev(DataZ,
                                                                       CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab)), ZErrOld;

            if (ProgressBox != null)
            {
                ProgressBox.ProgressBoxStart();                      //If delivered, ProgressBox is desired, thus start ProgressBox
            }
            if (ProgressBox != null)
            {
                ProgressBox.ProgressBoxUpdate(0);                      //If delivered, ProgressBox is desired, thus set ProgressBox=0
            }
            for (int P2A = 0; P2A < FineTuneIterBox; P2A++)
            {
                for (int i = 0; i < 1 + ZWorkTab.GetUpperBound(0); i++)     //i sweeps Y
                {
                    for (int j = 0; j < 1 + ZWorkTab.GetUpperBound(1); j++) //j sweeps X
                    {
                        int LoopSkip = 0;

                        if (FitType == false) //Absolute Error optimization
                        {
                            ZErr = VectorStatBasicMethods.ErrorsStdDev(DataZ,
                                                                       CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
                        }
                        else //Relative Error optimization
                        {
                            ZErr = VectorStatBasicMethods.StdDev(
                                CalibrationMethods.CalibrationRatioArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
                        }

                        for (int P2B = 0; P2B < FineTuneSubIterBox; P2B++)
                        {
                            // Increment or Decrement a Table value by a specific percent
                            ZWorkTab[i, j] *= MFactor;

                            // Evaluate the results of the value increment/decrement in terms of summed squared errors
                            ZErrOld = ZErr;

                            if (FitType == false) //Absolute Error optimization
                            {
                                ZErr = VectorStatBasicMethods.ErrorsStdDev(DataZ,
                                                                           CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
                            }
                            else //Relative Error optimization
                            {
                                ZErr = VectorStatBasicMethods.StdDev(
                                    CalibrationMethods.CalibrationRatioArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
                            }

                            // If the result didn't optimize the StdDev, undo operation and change direction and retrieve old error
                            MFactor         = (ZErr >= ZErrOld) ? 1 / MFactor : MFactor;
                            ZWorkTab[i, j] *= (ZErr >= ZErrOld) ? MFactor : 1;
                            ZErr            = (ZErr >= ZErrOld) ? ZErrOld : ZErr;

                            // When a second direction change is required (optm reached), break loop and goto next Table position
                            LoopSkip  = (P2B == 1 && LoopSkip == 0) ? 1 : LoopSkip;
                            LoopSkip += (ZErr >= ZErrOld) ? 1 : 0;
                            if (LoopSkip == 2)
                            {
                                break;
                            }
                        }

                        if (ProgressBox != null)
                        {
                            ProgressBox.ProgressBoxUpdate(100 * P2A / FineTuneIterBox);                      //If delivered, ProgressBox is desired, thus set ProgressBox update
                        }
                    }
                }

                MFactor = MFactor < 1 ? 1 / MFactor : MFactor;
                MFactor = ((MFactor - 1) / 2) + 1;

                //Populate the iteration converging error vector to be plotted afterwards
                if (FitType == false)
                {
                    ErrStep[P2A + 1] = VectorStatBasicMethods.ErrorsAvg(DataZ,
                                                                        CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
                    StdDStep[P2A + 1] = VectorStatBasicMethods.ErrorsStdDev(DataZ,
                                                                            CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
                }
                else
                {
                    ErrStep[P2A + 1] = VectorStatBasicMethods.Mean(
                        CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
                    StdDStep[P2A + 1] = VectorStatBasicMethods.StdDev(
                        CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
                }
            }

            if (ProgressBox != null)
            {
                ProgressBox.ProgressBoxFinish();                      //If delivered, ProgressBox is desired, thus finish ProgressBox
            }
            //4 Adjust Calibration Table values - MeanTar, Decimal Places, Round Values
            double ZMean = VectorStatBasicMethods.Mean(ZRatioArray);

            for (int i = 0; i < 1 + ZWorkTab.GetUpperBound(0); i++)     //i sweeps Y
            {
                for (int j = 0; j < 1 + ZWorkTab.GetUpperBound(1); j++) //j sweeps X
                {
                    if (FitType == false)
                    {
                        ZWorkTab[i, j] += ErrStep[ErrStep.GetUpperBound(0) - 1];

                        ZWorkTab[i, j] *= MeanTar;
                    }
                    else
                    {
                        ZWorkTab[i, j] *= MeanTar * VectorStatBasicMethods.Mean(
                            CalibrationMethods.CalibrationRatioArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));;
                    }

                    ZWorkTab[i, j] = Math.Round(ZWorkTab[i, j], PrecisionTar);
                }
            }

            //5. Populate the iteration converging error vector (last position) to be plotted afterwards
            if (FitType == false)
            {
                ErrStep[ErrStep.GetUpperBound(0)] = VectorStatBasicMethods.ErrorsAvg(DataZ,
                                                                                     CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
                StdDStep[ErrStep.GetUpperBound(0)] = VectorStatBasicMethods.ErrorsStdDev(DataZ,
                                                                                         CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
            }
            else
            {
                ErrStep[ErrStep.GetUpperBound(0)] = VectorStatBasicMethods.Mean(
                    CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
                StdDStep[ErrStep.GetUpperBound(0)] = VectorStatBasicMethods.StdDev(
                    CalibrationMethods.CalibrationAbsoluteArrayCalculation(DataX, DataY, DataZ, XBkpt, YBkpt, ZWorkTab));
            }

            return(ZWorkTab);
        }