public double[] WindowStat(int i, int j)
        {
            int index_p = 0;
            int new_r = 0, new_c = 0;
            var stat = new double[7];
            var hsv  = ToDouble(i, j);

            stat[0] = hsv[0];
            stat[1] = hsv[1];
            stat[2] = hsv[2];

            double[][] window = new double[3][];

            for (int c = 0; c < 3; c++)
            {
                window[c] = new double[9];
            }

            for (int r = i - 1; r <= i + 1; r++)
            {
                for (int c = j - 1; c <= j + 1; c++)
                {
                    new_r = r;
                    new_c = c;

                    if (r < 0)
                    {
                        new_r = 0;
                    }
                    if (r == _nrow)
                    {
                        new_r = _nrow - 1;
                    }
                    if (c < 0)
                    {
                        new_c = 0;
                    }
                    if (c == _ncol)
                    {
                        new_c = _ncol - 1;
                    }

                    var pp = ToDouble(new_r, new_c);
                    window[0][index_p] = pp[0];
                    window[1][index_p] = pp[1];
                    window[2][index_p] = pp[2];
                    index_p++;
                }
            }

            stat[0] = window[0][5];
            stat[1] = window[1][5];
            stat[2] = window[2][5];

            stat[3] = MyStatisticsMath.StandardDeviation(window[0]);
            stat[4] = MyStatisticsMath.StandardDeviation(window[1]);
            stat[5] = MyStatisticsMath.StandardDeviation(window[2]);
            stat[6] = window[1].Average();
            return(stat);
        }
        public void Scale(float min, float max, int step, int sdNumber)
        {
            var   vv     = Metric.GetVector(0, step.ToString(), ":");
            var   stat   = MyStatisticsMath.SimpleStatistics(vv);
            float vmax   = (float)(stat.Average + sdNumber * stat.StandardDeviation);
            float vmin   = (float)(stat.Average - sdNumber * stat.StandardDeviation);
            float vdelta = vmax - vmin;
            float ndelta = max - min;
            float value  = 0;

            for (int i = 0; i < vv.Length; i++)
            {
                value = Metric[0, step, i];
                if (value < vmin)
                {
                    Metric[0, step, i] = min;
                }
                else if (value > vmax)
                {
                    Metric[0, step, i] = max;
                }
                else
                {
                    Metric[0, step, i] = min + (value - vmin) / vdelta * ndelta;
                }
            }
        }
        public StatisticsInfo GetValueStatistics(ObservationSeries series)
        {
            string sql = string.Format("select DateTimeUTC, DataValue from DataValues where SiteID={0} and VariableID={1} order by DateTimeUTC",
                                       series.SiteID, series.VariableID);
            var dt    = ODMDB.QueryDataTable(sql).AsEnumerable();
            var dates = from dr in dt select dr.Field <DateTime>("DateTimeUTC");

            var values = from dr in dt select dr.Field <double>("DataValue");

            var filtered = from vv in values where vv != 0 select vv;
            var info     = MyStatisticsMath.SimpleStatistics(filtered.ToArray());

            return(info);
        }
        public void Scale(int step, float min, float max)
        {
            var vv   = Metric.GetVector(0, step.ToString(), ":");
            var stat = MyStatisticsMath.SimpleStatistics(vv);

            float vmax   = (float)stat.Max;
            float vmin   = (float)stat.Min;
            float vdelta = vmax - vmin;
            float ndelta = max - min;

            for (int i = 0; i < vv.Length; i++)
            {
                Metric[0, step, i] = min + (Metric[0, step, i] - vmin) / vdelta * ndelta;
            }
        }
        private void menu_trend_Click(object sender, EventArgs e)
        {
            var series = SelectSeries(_selectedMenuItem);

            if (series != null)
            {
                int      npt = series.Points.Count;
                double   rs, slope, yint;
                double[] xx = new double[npt];
                double[] yy = new double[npt];

                for (int i = 0; i < npt; i++)
                {
                    xx[i] = i + 1;
                    yy[i] = series.Points[i].YValues[0];
                }

                MyStatisticsMath.LinearRegression(xx, yy, 0, npt, out rs, out yint, out slope);
                string trend_name = "Trend of" + series.Name;
                lvStatistics.Items[4].SubItems[3].Text = string.Format("y = {0}x + {1}", slope.ToString("0.00"), yint.ToString("0.00"));
                double[] yy_trend = new double[2];
                if (series.XValueType == ChartValueType.Date || series.XValueType == ChartValueType.DateTime)
                {
                    DateTime[] date_trend = new DateTime[2];
                    double[]   xx_trend   = new double[2];
                    xx_trend[0]   = 1;
                    xx_trend[1]   = npt;
                    date_trend[0] = DateTime.FromOADate(series.Points[0].XValue);
                    date_trend[1] = DateTime.FromOADate(series.Points[npt - 1].XValue);
                    yy_trend[0]   = slope * xx_trend[0] + yint;
                    yy_trend[1]   = slope * xx_trend[1] + yint;
                    Plot <double>(date_trend, yy_trend, trend_name, SeriesChartType.Line);
                }
                else
                {
                    double[] xx_trend = new double[2];
                    xx_trend[0] = 1;
                    xx_trend[1] = npt;
                    yy_trend[0] = slope * xx_trend[0] + yint;
                    yy_trend[1] = slope * xx_trend[1] + yint;
                    Plot <double>(xx_trend, yy_trend, trend_name, SeriesChartType.Line);
                }
            }
        }
Exemple #6
0
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            int    var_indexA = 0;
            var    matA       = Get3DMat(InputDataCube, ref var_indexA);
            double prg        = 0;
            int    count      = 1;

            if (matA != null)
            {
                int nstep = matA.Size[1];
                int ncell = matA.Size[2];

                var mat_out = new DataCube <float>(1, 1, ncell);
                mat_out.Name      = OutputDataCube;
                mat_out.Variables = new string[] { "Slope" };

                for (int c = 0; c < ncell; c++)
                {
                    var    vec = matA.GetVector(var_indexA, ":", c.ToString());
                    var    dou_vec = MatrixOperation.ToDouble(vec);
                    var    steps = new double[nstep];
                    double rs, slope, yint;
                    for (int t = 1; t < nstep; t++)
                    {
                        steps[t] = t + 1;
                    }
                    MyStatisticsMath.LinearRegression(steps, dou_vec, 0, nstep, out rs, out yint, out slope);
                    mat_out[0, 0, c] = (float)slope;
                    prg = (c + 1) * 100.0 / ncell;
                    if (prg > count)
                    {
                        cancelProgressHandler.Progress("Package_Tool", (int)prg, "Caculating Cell: " + (c + 1));
                        count++;
                    }
                }
                Workspace.Add(mat_out);
                return(true);
            }
            else
            {
                return(false);
            }
        }
 private void btnStat_Click(object sender, EventArgs e)
 {
     if (chart1.Series.Count != 2)
     {
         _MessageService.ShowWarning(this, "Two series are required!");
     }
     else
     {
         var xx = (from pt in chart1.Series[0].Points select pt.YValues[0]).ToArray();
         var yy = (from pt in chart1.Series[1].Points select pt.YValues[0]).ToArray();
         if (xx.Length != yy.Length)
         {
             return;
         }
         lvStatistics.Items[0].SubItems[3].Text = MyStatisticsMath.Mse(xx, yy).ToString("F3");
         lvStatistics.Items[1].SubItems[3].Text = MyStatisticsMath.RMse(xx, yy).ToString("F3");
         lvStatistics.Items[2].SubItems[3].Text = MyStatisticsMath.Correlation(xx, yy).ToString("F3");
         lvStatistics.Items[3].SubItems[3].Text = MyStatisticsMath.NashStucliffeR(xx, yy).ToString("F3");
         // lvStatistics.Items[4].SubItems[3].Text = MyStatisticsMath.GetSimilarityScore(xx, yy).ToString("F3");
     }
 }
Exemple #8
0
        public My3DMat <float> StandardDeviation(My3DMat <float> source)
        {
            var mat    = new My3DMat <float>(source.Size[0], source.Size[1], source.Size[2]);
            int nrow   = source.Size[1];
            int ncol   = source.Size[2];
            int ncol_1 = ncol - 1;
            int nrow_1 = nrow - 1;

            var mat_source = source.Value[0];
            var vec5       = new float[5];

            // the first and the last columns
            for (int r = 1; r < nrow - 1; r++)
            {
                vec5[0]            = mat_source[r - 1][0];
                vec5[1]            = mat_source[r - 1][1];
                vec5[2]            = mat_source[r][1];
                vec5[3]            = mat_source[r + 1][1];
                vec5[4]            = mat_source[r + 1][0];
                mat.Value[0][r][0] = MyStatisticsMath.StandardDeviation(vec5);

                vec5[0] = mat_source[r - 1][ncol_1];
                vec5[1] = mat_source[r - 1][ncol_1 - 1];
                vec5[2] = mat_source[r][ncol_1 - 1];
                vec5[3] = mat_source[r + 1][ncol_1 - 1];
                vec5[4] = mat_source[r + 1][ncol_1];
                mat.Value[0][r][ncol_1] = MyStatisticsMath.StandardDeviation(vec5);
            }
            // the first and the last rows
            for (int c = 1; c < ncol - 1; c++)
            {
                vec5[0]            = mat_source[0][c - 1];
                vec5[1]            = mat_source[1][c - 1];
                vec5[2]            = mat_source[1][c];
                vec5[3]            = mat_source[1][c + 1];
                vec5[4]            = mat_source[0][c + 1];
                mat.Value[0][0][c] = MyStatisticsMath.StandardDeviation(vec5);

                vec5[0] = mat_source[nrow_1][c - 1];
                vec5[1] = mat_source[nrow_1 - 1][c - 1];
                vec5[2] = mat_source[nrow_1 - 1][c];
                vec5[3] = mat_source[nrow_1 - 1][c + 1];
                vec5[4] = mat_source[nrow_1][c + 1];

                mat.Value[0][nrow_1][c] = MyStatisticsMath.StandardDeviation(vec5);
            }

            float[] vec8 = new float[8];
            for (int r = 1; r < nrow - 1; r++)
            {
                for (int c = 1; c < ncol - 1; c++)
                {
                    vec8[0] = mat_source[r - 1][c - 1];
                    vec8[1] = mat_source[r - 1][c];
                    vec8[2] = mat_source[r - 1][c + 1];
                    vec8[3] = mat_source[r][c - 1];
                    vec8[4] = mat_source[r][c + 1];
                    vec8[5] = mat_source[r + 1][c - 1];
                    vec8[6] = mat_source[r + 1][c];
                    vec8[7] = mat_source[r + 1][c + 1];

                    mat.Value[0][r][c] = MyStatisticsMath.StandardDeviation(vec8);
                }
            }

            return(mat);
        }
Exemple #9
0
        public My3DMat <double> StandardDeviation(My3DMat <double> source)
        {
            int nvar   = source.Size[0];
            int nrow   = source.Size[1];
            int ncol   = source.Size[2];
            var mat    = new My3DMat <double>(nvar, nrow, ncol);
            int ncol_1 = ncol - 1;
            int nrow_1 = nrow - 1;

            for (int v = 0; v < nvar; v++)
            {
                var mat_source = source.Value[v];
                var vec5       = new double[5];
                // the first and the last columns
                for (int r = 1; r < nrow - 1; r++)
                {
                    vec5[0]            = mat_source[r - 1][0];
                    vec5[1]            = mat_source[r - 1][1];
                    vec5[2]            = mat_source[r][1];
                    vec5[3]            = mat_source[r + 1][1];
                    vec5[4]            = mat_source[r + 1][0];
                    mat.Value[v][r][0] = MyStatisticsMath.StandardDeviation(vec5);

                    vec5[0] = mat_source[r - 1][ncol_1];
                    vec5[1] = mat_source[r - 1][ncol_1 - 1];
                    vec5[2] = mat_source[r][ncol_1 - 1];
                    vec5[3] = mat_source[r + 1][ncol_1 - 1];
                    vec5[4] = mat_source[r + 1][ncol_1];
                    mat.Value[v][r][ncol_1] = MyStatisticsMath.StandardDeviation(vec5);
                }
                // the first and the last rows
                for (int c = 1; c < ncol - 1; c++)
                {
                    vec5[0]            = mat_source[0][c - 1];
                    vec5[1]            = mat_source[1][c - 1];
                    vec5[2]            = mat_source[1][c];
                    vec5[3]            = mat_source[1][c + 1];
                    vec5[4]            = mat_source[0][c + 1];
                    mat.Value[v][0][c] = MyStatisticsMath.StandardDeviation(vec5);

                    vec5[0] = mat_source[nrow_1][c - 1];
                    vec5[1] = mat_source[nrow_1 - 1][c - 1];
                    vec5[2] = mat_source[nrow_1 - 1][c];
                    vec5[3] = mat_source[nrow_1 - 1][c + 1];
                    vec5[4] = mat_source[nrow_1][c + 1];

                    mat.Value[v][nrow_1][c] = MyStatisticsMath.StandardDeviation(vec5);
                }

                double[] vec8 = new double[8];
                for (int r = 1; r < nrow - 1; r++)
                {
                    for (int c = 1; c < ncol - 1; c++)
                    {
                        vec8[0] = mat_source[r - 1][c - 1];
                        vec8[1] = mat_source[r - 1][c];
                        vec8[2] = mat_source[r - 1][c + 1];
                        vec8[3] = mat_source[r][c - 1];
                        vec8[4] = mat_source[r][c + 1];
                        vec8[5] = mat_source[r + 1][c - 1];
                        vec8[6] = mat_source[r + 1][c];
                        vec8[7] = mat_source[r + 1][c + 1];

                        mat.Value[v][r][c] = MyStatisticsMath.StandardDeviation(vec8);
                    }
                }
            }
            return(mat);
        }
Exemple #10
0
        private void PrePro(Dictionary<int, ReachFeatureCollection> fealist, out string msg)
        {
            double rs = 0, slope = 0, yint = 0;
            var dt = _out_sfr_layer.DataTable;
            var prj = MyAppManager.Instance.CompositionContainer.GetExportedValue<IProjectService>();
            msg = "";
            for (int i = 0; i < _out_sfr_layer.Features.Count; i++)
            {
                try
                {
                    var dr = dt.Rows[i];
                    var geo = _out_sfr_layer.GetFeature(i).Geometry;
                    if (geo.Length <= _dem_layer.CellHeight)
                    {
                        continue;
                    }
                    var npt = geo.Coordinates.Count();
                    int segid = int.Parse(dr[SegmentField].ToString()) + 1;
                    double[] dis = new double[npt];
                    double[] ac_dis = new double[npt];
                    double[] elvs = new double[npt];
                    double elev_av = 0;
                    var pt0 = geo.Coordinates[0];
                    var cell = _dem_layer.ProjToCell(pt0.X, pt0.Y);
                    double ad = 0;
                    dis[0] = 0;
                    elvs[0] = _dem_layer.Value[cell.Row, cell.Column];
                    for (int j = 0; j < npt; j++)
                    {
                       cell = _ad_layer.ProjToCell(geo.Coordinates[j].X, geo.Coordinates[j].Y);
                       ad += _ad_layer.Value[cell.Row, cell.Column];
                    }
                    ad = ad / npt;
                    for (int j = 1; j < npt; j++)
                    {
                        cell = _dem_layer.ProjToCell(geo.Coordinates[j].X, geo.Coordinates[j].Y);
                        elvs[j] = _dem_layer.Value[cell.Row, cell.Column];
                        dis[j] = SpatialDistance.DistanceBetween(geo.Coordinates[j - 1], geo.Coordinates[j]);
                    }
                    for (int j = 0; j < npt; j++)
                    {
                        ac_dis[j] = dis.Take(j + 1).Sum();
                    }

                    MyStatisticsMath.LinearRegression(ac_dis, elvs, 0, elvs.Length, out rs, out yint, out slope);

                    if (slope < 0)
                    {
                        slope = -slope;
                    }
                    else if (slope == 0)
                    {
                        slope = _minum_slope;
                    }

                    for (int j = 0; j < npt; j++)
                    {
                        elvs[j] = yint + slope * ac_dis[j];
                    }
                    elev_av = elvs.Average();

                    if (slope < _minum_slope)
                        slope = _minum_slope;
                    if (slope > _maximum_slope)
                        slope = _maximum_slope;

                    var rch = new ReachFeature()
                    {
                        Row = dr,
                        Elevation = elev_av,
                        Slope = slope
                    };

                    if (fealist[segid].Reaches.ContainsKey(ad))
                    {
                        ad += i * 0.001;
                    }
                    fealist[segid].Reaches.Add(ad, rch);
                    fealist[segid].OutSegmentID = int.Parse(dr["DSLINKNO"].ToString());
                    dr["Length"] = geo.Length;
                }
                catch (Exception ex)
                {
                    msg += ex.Message + "\n";
                }
            }
        }
        public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler)
        {
            FileStream   fs = new FileStream(_OutputFileName, FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);

            var var_index = 0;
            var mat       = Get3DMat(Source, ref var_index);
            int progress  = 0;
            int nsteps    = mat.Size[1];
            var grid      = ProjectService.Project.Model.Grid as RegularGrid;

            if (grid != null)
            {
                float[][] lonlat = null;
                if (CoornidateSystem == CS.GCS)
                {
                    lonlat = grid.GetLonLatAxis();
                }
                else if (CoornidateSystem == CS.PCS)
                {
                    lonlat = grid.GetPCSAxis();
                }
                var nrow  = grid.RowCount;
                var ncol  = grid.ColumnCount;
                var times = new float[nsteps];
                if (mat.DateTimes != null)
                {
                    for (int t = 0; t < nsteps; t++)
                    {
                        times[t] = (float)mat.DateTimes[t].ToOADate();
                    }
                }
                else
                {
                    for (int t = 0; t < nsteps; t++)
                    {
                        times[t] = (float)DateTime.Now.AddDays(t).ToOADate();
                    }
                }

                bw.Write(nrow);
                bw.Write(ncol);
                bw.Write(nsteps);

                for (int i = 0; i < ncol; i++)
                {
                    bw.Write(lonlat[0][i]);
                }
                for (int i = 0; i < nrow; i++)
                {
                    bw.Write(lonlat[1][i]);
                }

                for (int t = 0; t < nsteps; t++)
                {
                    bw.Write(times[t]);
                    var vec = mat[var_index, t.ToString(), ":"];
                    bw.Write(vec.Max());
                    bw.Write(vec.Min());
                    bw.Write(vec.Average());
                    bw.Write(MyStatisticsMath.StandardDeviation(vec));
                    var mat_step = grid.ToMatrix <float>(vec, 0);
                    for (int r = 0; r < nrow; r++)
                    {
                        for (int c = 0; c < ncol; c++)
                        {
                            bw.Write(mat_step[r][c]);
                        }
                    }
                    progress = t * 100 / nsteps;
                    cancelProgressHandler.Progress("Package_Tool", progress, "Processing step:" + t);
                }

                fs.Close();
                bw.Close();
                return(true);
            }
            else
            {
                return(false);
            }
        }