From 2e800342e7e6f5a5d3ff74679b362bbdcec224fb Mon Sep 17 00:00:00 2001 From: Nianshuo Wen <1565408369@qq.com> Date: Mon, 23 Mar 2026 17:42:55 +0800 Subject: [PATCH 1/3] fix gwcorr index error --- src/gwmodelpp/GWCorrelation.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/gwmodelpp/GWCorrelation.cpp b/src/gwmodelpp/GWCorrelation.cpp index d3dcb0bf..c00ad522 100644 --- a/src/gwmodelpp/GWCorrelation.cpp +++ b/src/gwmodelpp/GWCorrelation.cpp @@ -143,8 +143,10 @@ void GWCorrelation::GWCorrelationSerial() mLocalMean.row(i) = trans(Wi) * mXY; mat centerized = mXY.each_row() - mLocalMean.row(i); mLVar.row(i) = Wi.t() * (centerized % centerized); - uword coly = col / nVar; - uword colx = (col + nVar) % nVar; + //uword coly = col / nVar; + //uword colx = (col + nVar) % nVar; + uword colx = col / nRsp; + uword coly = col % nRsp; //correlation double covjk = covwt(mY.col(coly), mX.col(colx), Wi); double sumW2 = sum(Wi % Wi); @@ -242,8 +244,10 @@ void GWCorrelation::GWCorrelationOmp() mLocalMean.row(i) = trans(Wi) * mXY; mat centerized = mXY.each_row() - mLocalMean.row(i); mLVar.row(i) = Wi.t() * (centerized % centerized); - uword coly = col / nVar; - uword colx = (col + nVar) % nVar; + //uword coly = col / nVar; + //uword colx = (col + nVar) % nVar; + uword colx = col / nRsp; + uword coly = col % nRsp; double covjk = covwt(mY.col(coly), mX.col(colx), Wi); double sumW2 = sum(Wi % Wi); double covjj = mLVar(i, colx) / (1.0 - sumW2); From b42c87f11ff945c59bcc1eb11ebcaa6b51dfb91e Mon Sep 17 00:00:00 2001 From: Nianshuo Wen <1565408369@qq.com> Date: Sun, 29 Mar 2026 00:20:47 +0800 Subject: [PATCH 2/3] fix index error, change nVarX into nVarY --- src/gwmodelpp/GWCorrelation.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/gwmodelpp/GWCorrelation.cpp b/src/gwmodelpp/GWCorrelation.cpp index c00ad522..f91e2613 100644 --- a/src/gwmodelpp/GWCorrelation.cpp +++ b/src/gwmodelpp/GWCorrelation.cpp @@ -129,10 +129,10 @@ void GWCorrelation::GWCorrelationSerial() rankX.each_col([&](vec &x) { x = rank(x); }); mat rankY = mY; rankY.each_col([&](vec &y) { y = rank(y); }); - uword nRp = mCoords.n_rows, nVar = mX.n_cols, nRsp=mY.n_cols; - uword nCol = nVar * nRsp; + uword nRp = mCoords.n_rows, nVarX = mX.n_cols, nVarY=mY.n_cols; + uword nVar = nVarX * nVarY; mat mXY = join_rows(mX,mY); - for (uword col = 0; col < nCol; col++) + for (uword col = 0; col < nVar; col++) { for (uword i = 0; i < nRp; i++) { @@ -143,15 +143,13 @@ void GWCorrelation::GWCorrelationSerial() mLocalMean.row(i) = trans(Wi) * mXY; mat centerized = mXY.each_row() - mLocalMean.row(i); mLVar.row(i) = Wi.t() * (centerized % centerized); - //uword coly = col / nVar; - //uword colx = (col + nVar) % nVar; - uword colx = col / nRsp; - uword coly = col % nRsp; + uword coly = col / nVarY; + uword colx = (col + nVarY) % nVarY; //correlation double covjk = covwt(mY.col(coly), mX.col(colx), Wi); double sumW2 = sum(Wi % Wi); double covjj = mLVar(i, colx) / (1.0 - sumW2); - double covkk = mLVar(i, coly+nVar) / (1.0 - sumW2); + double covkk = mLVar(i, coly+nVarX) / (1.0 - sumW2); mCovmat(i, col) = covjk; mCorrmat(i, col) = covjk / sqrt(covjj * covkk); mSCorrmat(i, col) = corwt(rankY.col(coly), rankX.col(colx), Wi); @@ -229,11 +227,11 @@ void GWCorrelation::GWCorrelationOmp() rankX.each_col([&](vec &x) { x = rank(x); }); mat rankY = mY; rankY.each_col([&](vec &y) { y = rank(y); }); - uword nRp = mCoords.n_rows, nVar = mX.n_cols, nRsp=mY.n_cols; - uword nCol = nVar * nRsp; + uword nRp = mCoords.n_rows, nVarX = mX.n_cols, nVarY=mY.n_cols; + uword nVar = nVarX * nVarY; mat mXY = join_rows(mX,mY); #pragma omp parallel for num_threads(mOmpThreadNum) - for (uword col = 0; col < nCol; col++) + for (uword col = 0; col < nVar; col++) { for (uword i = 0; i < nRp; i++) { @@ -244,14 +242,12 @@ void GWCorrelation::GWCorrelationOmp() mLocalMean.row(i) = trans(Wi) * mXY; mat centerized = mXY.each_row() - mLocalMean.row(i); mLVar.row(i) = Wi.t() * (centerized % centerized); - //uword coly = col / nVar; - //uword colx = (col + nVar) % nVar; - uword colx = col / nRsp; - uword coly = col % nRsp; + uword coly = col / nVarY; + uword colx = (col + nVarY) % nVarY; double covjk = covwt(mY.col(coly), mX.col(colx), Wi); double sumW2 = sum(Wi % Wi); double covjj = mLVar(i, colx) / (1.0 - sumW2); - double covkk = mLVar(i, coly+nVar) / (1.0 - sumW2); + double covkk = mLVar(i, coly+nVarX) / (1.0 - sumW2); mCovmat(i, col) = covjk; mCorrmat(i, col) = covjk / sqrt(covjj * covkk); mSCorrmat(i, col) = corwt(rankY.col(coly), rankX.col(colx), Wi); From 026a780298cf680061ecd420b6591d105c90e7f3 Mon Sep 17 00:00:00 2001 From: Nianshuo Wen <1565408369@qq.com> Date: Tue, 31 Mar 2026 22:38:17 +0800 Subject: [PATCH 3/3] fix index error; fix aic input error, gwmodelS run successfully --- include/gwmodelpp/GWCorrelation.h | 6 +++--- src/gwmodelpp/GWCorrelation.cpp | 21 +++++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/include/gwmodelpp/GWCorrelation.h b/include/gwmodelpp/GWCorrelation.h index a8e42957..fb0d5df5 100644 --- a/include/gwmodelpp/GWCorrelation.h +++ b/include/gwmodelpp/GWCorrelation.h @@ -443,7 +443,7 @@ class GWCorrelation : public SpatialMultiscaleAlgorithm, public IMultivariateAna /** * @brief \~english set variables \~chinese 设置变量x。 * - * @param x \~english variables for GWAverage \~chinese 进行GWAverage的变量,如果只有一列,只能进行GWAverage。 + * @param x \~english variables for GWAverage \~chinese 进行GWCorrelation的变量。 */ void setVariables2(const arma::mat& x) override { mX = x; } @@ -451,9 +451,9 @@ class GWCorrelation : public SpatialMultiscaleAlgorithm, public IMultivariateAna const arma::mat& variables1() const override { return mY; } /** - * @brief \~english set variables \~chinese 设置变量x。 + * @brief \~english set variables \~chinese 设置变量y。 * - * @param x \~english variables for GWAverage \~chinese 进行GWAverage的变量,如果只有一列,只能进行GWAverage。 + * @param y \~english variables for GWAverage \~chinese 进行GWCorrelation的变量。 */ void setVariables1(const arma::mat& y) override { mY = y; } diff --git a/src/gwmodelpp/GWCorrelation.cpp b/src/gwmodelpp/GWCorrelation.cpp index f91e2613..0bce1513 100644 --- a/src/gwmodelpp/GWCorrelation.cpp +++ b/src/gwmodelpp/GWCorrelation.cpp @@ -101,7 +101,7 @@ void GWCorrelation::run() mBandwidthSizeCriterion = bandwidthSizeCriterionVar(mBandwidthSelectionApproach[i]); mBandwidthSelectionCurrentIndex = i; mXi = mX.col(i/nRsp); - mYi = mY.col((i+nRsp)%nRsp); + mYi = mY.col(i%nRsp); GWM_LOG_INFO(string(GWM_LOG_TAG_GWCORR_INITIAL_BW) + to_string(i)); BandwidthWeight* bw0 = bandwidth(i); BandwidthSelector selector; @@ -112,8 +112,13 @@ void GWCorrelation::run() if(bw) { mSpatialWeights[i].setWeight(bw); + GWM_LOG_INFO(string(GWM_LOG_TAG_GWCORR_INITIAL_BW) + to_string(i) + "," + to_string(bw->bandwidth())); } - GWM_LOG_INFO(string(GWM_LOG_TAG_GWCORR_INITIAL_BW) + to_string(i) + "," + to_string(bw->bandwidth())); + else + { + GWM_LOG_INFO(string(GWM_LOG_TAG_GWCORR_INITIAL_BW) + to_string(i) + ",optimize-failed"); + } + //GWM_LOG_INFO(string(GWM_LOG_TAG_GWCORR_INITIAL_BW) + to_string(i) + "," + to_string(bw->bandwidth())); } GWM_LOG_STOP_RETURN(mStatus, void()); } @@ -143,8 +148,8 @@ void GWCorrelation::GWCorrelationSerial() mLocalMean.row(i) = trans(Wi) * mXY; mat centerized = mXY.each_row() - mLocalMean.row(i); mLVar.row(i) = Wi.t() * (centerized % centerized); - uword coly = col / nVarY; - uword colx = (col + nVarY) % nVarY; + uword colx = col / nVarY; + uword coly = col % nVarY; //correlation double covjk = covwt(mY.col(coly), mX.col(colx), Wi); double sumW2 = sum(Wi % Wi); @@ -242,8 +247,8 @@ void GWCorrelation::GWCorrelationOmp() mLocalMean.row(i) = trans(Wi) * mXY; mat centerized = mXY.each_row() - mLocalMean.row(i); mLVar.row(i) = Wi.t() * (centerized % centerized); - uword coly = col / nVarY; - uword colx = (col + nVarY) % nVarY; + uword colx = col / nVarY; + uword coly = col % nVarY; double covjk = covwt(mY.col(coly), mX.col(colx), Wi); double sumW2 = sum(Wi % Wi); double covjj = mLVar(i, colx) / (1.0 - sumW2); @@ -284,7 +289,7 @@ double GWCorrelation::bandwidthSizeCriterionAICSerial(BandwidthWeight *bandwidth return DBL_MAX; } } - double value = GWRBase::AICc(mX, mY, betas.t(), shat); + double value = GWRBase::AICc(mXi, mYi, betas.t(), shat); if (mStatus == Status::Success && isfinite(value)) { GWM_LOG_PROGRESS_PERCENT(exp(- abs(mBandwidthLastCriterion - value))); @@ -374,7 +379,7 @@ double GWCorrelation::bandwidthSizeCriterionAICOmp(BandwidthWeight *bandwidthWei if (mStatus == Status::Success && flag) { vec shat = sum(shat_all, 1); - double value = GWRBase::AICc(mX, mY, betas.t(), shat); + double value = GWRBase::AICc(mXi, mYi, betas.t(), shat); if (isfinite(value)) { GWM_LOG_PROGRESS_PERCENT(exp(-abs(mBandwidthLastCriterion - value)));