Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Model/gwmalgorithmmetavariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bool GwmAlgorithmMetaVariable::validate(QString &error) const

if (weightType == Weight::WeightType::BandwidthWeight)
{
if (weightBandwidthSize == 0)
if (!weightBandwidthAutoselect && weightBandwidthSize == 0)
{
error = QTranslator::tr("Bandwidth size is too small.");
return false;
Expand Down
1 change: 1 addition & 0 deletions src/Model/gwmalgorithmmetavariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct GwmAlgorithmMetaVariable
gwm::Weight::WeightType weightType = gwm::Weight::BandwidthWeight;
double weightBandwidthSize = DBL_MAX;
bool weightBandwidthAdaptive = true;
bool weightBandwidthAutoselect = false;
gwm::BandwidthWeight::KernelFunctionType weightBandwidthKernel = gwm::BandwidthWeight::KernelFunctionType::Gaussian;
// Distance
gwm::Distance::DistanceType distanceType = gwm::Distance::DistanceType::CRSDistance;
Expand Down
11 changes: 10 additions & 1 deletion src/Model/gwmlayergwaverageitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ GwmLayerGWAverageItem::GwmLayerGWAverageItem(GwmLayerItem* parentItem, QgsVector
auto taskMeta = taskThread->meta();
mDataPointsSize = taskMeta.layer->featureCount();
mVariables = taskMeta.variables;
mBandwidth = new gwm::BandwidthWeight(taskMeta.weightBandwidthSize, taskMeta.weightBandwidthAdaptive, gwm::BandwidthWeight::KernelFunctionType(taskMeta.weightBandwidthKernel));
// Use final bandwidth from taskThread (may be updated by autoselect), fallback to meta
gwm::BandwidthWeight finalBw = taskThread->finalBandwidth();
if (finalBw.bandwidth() != 0.0 || finalBw.adaptive())
{
mBandwidth = new gwm::BandwidthWeight(finalBw);
}
else
{
mBandwidth = new gwm::BandwidthWeight(taskMeta.weightBandwidthSize, taskMeta.weightBandwidthAdaptive, gwm::BandwidthWeight::KernelFunctionType(taskMeta.weightBandwidthKernel));
}
mQuantile = taskThread->quantile();
mResultList = taskThread->resultlist();

Expand Down
6 changes: 6 additions & 0 deletions src/PropertyPanelTabs/gwmpropertygwaveragetab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ void GwmPropertyGWAverageTab::updateUI()
QString bwSizeString = QString("%1 (number of nearest neighbours)").arg(int(weight.bandwidth()));
ui->lblBandwidthSize->setText(bwSizeString);
}
else
{
// for fixed bandwidth show distance value
QString bwSizeString = QString("%1 (distance)").arg(weight.bandwidth(), 0, 'f', 3);
ui->lblBandwidthSize->setText(bwSizeString);
}
ui->lblNumberDataPoints->setText(QString("%1").arg(mLayerItem->dataPointsSize()));
if (true)
{
Expand Down
31 changes: 24 additions & 7 deletions src/TaskThread/gwmbasicgwralgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,22 @@ void GwmBasicGWRAlgorithm::run()
if (mIsAutoselectBandwidth)
{
emit message(QString(tr("Automatically selecting bandwidth ...")));
qDebug() << "ParallelType in run before =" << mParallelType;
qDebug() << "OmpThreadNum in run before =" << mOmpThreadNum;
mGWRCore->setParallelType(mParallelType);
mGWRCore->setOmpThreadNum(mOmpThreadNum);
qDebug() << "core parallelType =" << mGWRCore->parallelType();
qDebug() << "core parallelAbility =" << mGWRCore->parallelAbility();

mGWRCore->setTelegram(std::make_unique<GwmTaskThreadTelegram>(this));
QElapsedTimer timer;
timer.start();

mBetas = mGWRCore->fit();

qint64 elapsed = timer.elapsed();
qDebug() << "fit() time =" << elapsed << "ms";

gwm::BandwidthWeight* bw = mGWRCore->spatialWeight().weight<gwm::BandwidthWeight>();
if (bw && !checkCanceled())
{
Expand All @@ -155,7 +166,7 @@ void GwmBasicGWRAlgorithm::run()
QVariant data = QVariant::fromValue(qlist);
emit plot(data, &GwmBandwidthSizeSelector::PlotBandwidthResult);
}
std::cout << "mBetas = \n" << mBetas << std::endl;
// std::cout << "mBetas = \n" << mBetas << std::endl;
}
else
{
Expand Down Expand Up @@ -826,6 +837,7 @@ double GwmBasicGWRAlgorithm::bandwidthSizeCriterionAICSerial(GwmBandwidthWeight*
double GwmBasicGWRAlgorithm::bandwidthSizeCriterionAICOmp(GwmBandwidthWeight *bandwidthWeight)
{
int nDp = mDataPoints.n_rows, nVar = mIndepVars.size() + 1;
const int selectorStep = static_cast<int>(mBandwidthSizeSelector.bandwidthCriterion().size());
mat betas(nVar, nDp, fill::zeros);
mat shat_all(2, mOmpThreadNum, fill::zeros);
bool flag = true;
Expand Down Expand Up @@ -854,8 +866,10 @@ double GwmBasicGWRAlgorithm::bandwidthSizeCriterionAICOmp(GwmBandwidthWeight *ba
{
flag = false;
}
if(mBandwidthSizeSelector.counter<10)
emit tick(mBandwidthSizeSelector.counter*10 + current * 10 / nDp, 100);
// if(mBandwidthSizeSelector.counter<10)
// emit tick(mBandwidthSizeSelector.counter*10 + current * 10 / nDp, 100);
if (selectorStep < 10)
emit tick(selectorStep * 10 + current * 10 / nDp, 100);
current++;
}
}
Expand Down Expand Up @@ -976,6 +990,7 @@ double GwmBasicGWRAlgorithm::bandwidthSizeCriterionCVSerial(GwmBandwidthWeight *
double GwmBasicGWRAlgorithm::bandwidthSizeCriterionCVOmp(GwmBandwidthWeight *bandwidthWeight)
{
int nDp = mDataPoints.n_rows;
const int selectorStep = static_cast<int>(mBandwidthSizeSelector.bandwidthCriterion().size());
vec shat(2, fill::zeros);
vec cv_all(mOmpThreadNum, fill::zeros);
bool flag = true;
Expand Down Expand Up @@ -1006,8 +1021,10 @@ double GwmBasicGWRAlgorithm::bandwidthSizeCriterionCVOmp(GwmBandwidthWeight *ban
{
flag = false;
}
if(mBandwidthSizeSelector.counter<10)
emit tick(mBandwidthSizeSelector.counter*10 + current * 10 / nDp, 100);
// if(mBandwidthSizeSelector.counter<10)
// emit tick(mBandwidthSizeSelector.counter*10 + current * 10 / nDp, 100);
if (selectorStep < 10)
emit tick(selectorStep * 10 + current * 10 / nDp, 100);
current++;
}
}
Expand Down Expand Up @@ -1530,6 +1547,6 @@ void GwmBasicGWRAlgorithm::setBandwidthSelectionCriterionType(const gwm::GWRBasi

void GwmBasicGWRAlgorithm::setParallelType(const gwm::ParallelType &type)
{
mParallelType = type;
mGWRCore->setParallelType(type);
// mParallelType = type;
// mGWRCore->setParallelType(type);
}
54 changes: 32 additions & 22 deletions src/TaskThread/gwmgeneralizedgwralgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ mat GwmGeneralizedGWRAlgorithm::regressionPoissonOmp(const mat &x, const vec &y)
mat S(isStoreS ? nDp : 1, nDp, fill::zeros);
int current = 0;
if(mHasHatMatrix && !checkCanceled()){
mat shat = mat(2,mOmpThreadNum,fill::zeros);
mat shat = mat(2,mOmpThreadNum,fill::zeros);
#pragma omp parallel for num_threads(mOmpThreadNum)
for(int i = 0; i < nDp; i++){
mat ci,s_ri;
Expand Down Expand Up @@ -818,7 +818,7 @@ double GwmGeneralizedGWRAlgorithm::bandwidthSizeGGWRCriterionCVSerial(GwmBandwid
.arg(res);
emit message(msg);
return res;
}
}
else return DBL_MAX;

}
Expand All @@ -829,6 +829,7 @@ double GwmGeneralizedGWRAlgorithm::bandwidthSizeGGWRCriterionCVOmp(GwmBandwidthW
vec cv = vec(n);
mat wt = mat(n,n);
int current1 = 0, current2 = 0;
const int selectorStep = static_cast<int>(mBandwidthSizeSelector.bandwidthCriterion().size());
#pragma omp parallel for num_threads(mOmpThreadNum)
for (int i = 0; i < n; i++)
{
Expand All @@ -838,9 +839,11 @@ double GwmGeneralizedGWRAlgorithm::bandwidthSizeGGWRCriterionCVOmp(GwmBandwidthW
vec w = bandwidthWeight->weight(d);
w.row(i) = 0;
wt.col(i) = w;
if(mBandwidthSizeSelector.counter<10)
emit tick(mBandwidthSizeSelector.counter*10 + current1 * 5 / n, 100);
current1++;
// if(mBandwidthSizeSelector.counter<10)
// emit tick(mBandwidthSizeSelector.counter*10 + current2 * 5 / n + 5, 100);
if (selectorStep < 10)
emit tick(selectorStep * 10 + current2 * 5 / n + 5, 100);
current2++;
}
}
if (!checkCanceled()) (this->*mCalWtFunction)(mX,mY,wt);
Expand All @@ -857,8 +860,10 @@ double GwmGeneralizedGWRAlgorithm::bandwidthSizeGGWRCriterionCVOmp(GwmBandwidthW
else{
cv.row(i) = mY.row(i) - exp(yhatnoi)/(1+exp(yhatnoi));
}
if(mBandwidthSizeSelector.counter<10)
emit tick(mBandwidthSizeSelector.counter*10 + current2 * 5 / n + 5, 100);
// if(mBandwidthSizeSelector.counter<10)
// emit tick(mBandwidthSizeSelector.counter*10 + current2 * 5 / n + 5, 100);
if (selectorStep < 10)
emit tick(selectorStep * 10 + current2 * 5 / n + 5, 100);
current2++;
}
}
Expand All @@ -883,7 +888,7 @@ double GwmGeneralizedGWRAlgorithm::bandwidthSizeGGWRCriterionAICSerial(GwmBandwi
int n = mDataPoints.n_rows;
vec cv = vec(n);
mat S = mat(n,n);
mat wt = mat(n,n);
mat wt = mat(n,n);
for (int i = 0; i < n && !checkCanceled(); i++)
{
vec d = mSpatialWeight.distance()->distance(i);
Expand Down Expand Up @@ -935,6 +940,7 @@ double GwmGeneralizedGWRAlgorithm::bandwidthSizeGGWRCriterionAICOmp(GwmBandwidth
mat S = mat(n,n);
mat wt = mat(n,n);
int current1 = 0, current2 = 0;
const int selectorStep = static_cast<int>(mBandwidthSizeSelector.bandwidthCriterion().size());
#pragma omp parallel for num_threads(mOmpThreadNum)
for (int i = 0; i < n; i++)
{
Expand All @@ -943,8 +949,10 @@ double GwmGeneralizedGWRAlgorithm::bandwidthSizeGGWRCriterionAICOmp(GwmBandwidth
vec d = mSpatialWeight.distance()->distance(i);
vec w = bandwidthWeight->weight(d);
wt.col(i) = w;
if(mBandwidthSizeSelector.counter<10)
emit tick(mBandwidthSizeSelector.counter*10 + current1 * 5 / n, 100);
// if(mBandwidthSizeSelector.counter<10)
// emit tick(mBandwidthSizeSelector.counter*10 + current1 * 5 / n, 100);
if (selectorStep < 10)
emit tick(selectorStep * 10 + current1 * 5 / n, 100);
current1++;
}
}
Expand All @@ -959,8 +967,10 @@ double GwmGeneralizedGWRAlgorithm::bandwidthSizeGGWRCriterionAICOmp(GwmBandwidth
mat Ci = CiMat(mX,wi);
S.row(i) = mX.row(i) * Ci;
trS(thread) += S(i,i);
if(mBandwidthSizeSelector.counter<10)
emit tick(mBandwidthSizeSelector.counter*10 + current2 * 5 / n + 5, 100);
// if(mBandwidthSizeSelector.counter<10)
// emit tick(mBandwidthSizeSelector.counter*10 + current2 * 5 / n + 5, 100);
if (selectorStep < 10)
emit tick(selectorStep * 10 + current2 * 5 / n + 5, 100);
current2++;
}
}
Expand Down Expand Up @@ -1460,11 +1470,11 @@ void GwmGeneralizedGWRAlgorithm::fTest(FTestParameters params)
f3.append(f3i);
continue;
}

double g1 = diagB(0);
double g2 = diagB(1);
double numdf = g1 * g1 / g2;

// 检查计算结果的有效性
if (g1 <= 0 || g2 <= 0 || numdf <= 0 || !isfinite(numdf))
{
Expand All @@ -1476,7 +1486,7 @@ void GwmGeneralizedGWRAlgorithm::fTest(FTestParameters params)
f3.append(f3i);
continue;
}

GwmFTestResult f3i;
f3i.s = (vk2(i) / g1) / sigma2delta1;
f3i.df1 = numdf;
Expand Down Expand Up @@ -1516,20 +1526,20 @@ vec GwmGeneralizedGWRAlgorithm::calcDiagBSerial(int i)
arma::uword nDp = mX.n_rows, nVar = mX.n_cols;
vec diagB(nDp, fill::zeros), c(nDp, fill::zeros);
mat wspan(1, nVar, fill::ones);

// 第一遍循环:计算 c(所有数据点的系数矩阵第 i 列的平均值)
for (arma::uword j = 0; j < nDp && !checkCanceled(); j++)
{
vec wj = mWtMat2.col(j);
vec weights = wj % mWt2;

// 检查权重有效性
if (sum(weights) < 1e-10 || any(weights < 0) || !weights.is_finite())
{
emit error("Invalid weights in calcDiagB (first loop).");
return { DBL_MAX, DBL_MAX };
}

mat xtw = trans(mX % (weights * wspan));
try {
// 使用 inv_sympd 替代 pinv,与 BasicGWR 保持一致
Expand All @@ -1540,20 +1550,20 @@ vec GwmGeneralizedGWRAlgorithm::calcDiagBSerial(int i)
return { DBL_MAX, DBL_MAX };
}
}

// 第二遍循环:计算 diagB
for (arma::uword k = 0; k < nDp && !checkCanceled(); k++)
{
vec wk = mWtMat2.col(k);
vec weights = wk % mWt2;

// 检查权重有效性
if (sum(weights) < 1e-10 || any(weights < 0) || !weights.is_finite())
{
emit error("Invalid weights in calcDiagB (second loop).");
return { DBL_MAX, DBL_MAX };
}

mat xtw = trans(mX % (weights * wspan));
try {
// 使用 inv_sympd 替代 pinv,与 BasicGWR 保持一致
Expand All @@ -1565,7 +1575,7 @@ vec GwmGeneralizedGWRAlgorithm::calcDiagBSerial(int i)
return { DBL_MAX, DBL_MAX };
}
}

diagB = 1.0 / nDp * diagB;
return { sum(diagB), sum(diagB % diagB) };
}
Expand Down
39 changes: 38 additions & 1 deletion src/TaskThread/gwmgwaveragetaskthread.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "gwmgwaveragetaskthread.h"
#include "gwmgwaveragetaskthread.h"
#include <exception>
#include <gwmodel.h>
#include "SpatialWeight/gwmcrsdistance.h"
Expand Down Expand Up @@ -45,6 +45,11 @@ GwmGWAverageTaskThread::GwmGWAverageTaskThread(const GwmAlgorithmMetaVariable& m
}
SpatialWeight spatialWeight(&weight, distance);
mAlgorithm.setSpatialWeight(spatialWeight);
mAlgorithm.setAutoselectBandwidth(meta.weightBandwidthAutoselect);
qDebug() << "GWAverageTaskThread configured: autoselectBandwidth=" << mAlgorithm.isAutoselectBandwidth()
<< ", initial bandwidth=" << weight.bandwidth()
<< ", adaptive=" << weight.adaptive()
<< ", kernel=" << (int)weight.kernel();
// Parallel
mAlgorithm.setParallelType(meta.parallelType);
switch (meta.parallelType)
Expand All @@ -60,6 +65,14 @@ GwmGWAverageTaskThread::GwmGWAverageTaskThread(const GwmAlgorithmMetaVariable& m
delete distance;
}

gwm::BandwidthWeight GwmGWAverageTaskThread::finalBandwidth() const
{
auto bw = mAlgorithm.spatialWeight().weight<gwm::BandwidthWeight>();
if (bw)
return *bw;
return gwm::BandwidthWeight();
}

void GwmGWAverageTaskThread::run()
{
emit tick(0, 0);
Expand All @@ -75,7 +88,31 @@ void GwmGWAverageTaskThread::run()
try
{
mAlgorithm.setTelegram(make_unique<GwmTaskThreadTelegram>(this));
qDebug() << "core parallelType =" << mAlgorithm.parallelType();
qDebug() << "core parallelAbility =" << mAlgorithm.parallelAbility();
qDebug() << "core autoselect bandwidth =" << mAlgorithm.isAutoselectBandwidth();
if (mAlgorithm.isAutoselectBandwidth())
{
auto bw = mAlgorithm.spatialWeight().weight<gwm::BandwidthWeight>();
if (bw)
{
qDebug() << "core bandwidth before run =" << bw->bandwidth()
<< ", adaptive =" << bw->adaptive()
<< ", kernel =" << (int)bw->kernel();
}
}
mAlgorithm.run();
qDebug() << "runfinished";
if (mAlgorithm.isAutoselectBandwidth())
{
auto bw = mAlgorithm.spatialWeight().weight<gwm::BandwidthWeight>();
if (bw)
{
qDebug() << "core bandwidth after run =" << bw->bandwidth()
<< ", adaptive =" << bw->adaptive()
<< ", kernel =" << (int)bw->kernel();
}
}
if(!checkCanceled())
{
mResultList.push_back(qMakePair(QString("LM"), localmean()));
Expand Down
5 changes: 4 additions & 1 deletion src/TaskThread/gwmgwaveragetaskthread.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef GWMGWAVERAGETASKTHREAD_H
#ifndef GWMGWAVERAGETASKTHREAD_H
#define GWMGWAVERAGETASKTHREAD_H

#include <QObject>
Expand Down Expand Up @@ -74,6 +74,9 @@ class GwmGWAverageTaskThread : public GwmSpatialMonoscaleAlgorithm, public IGwmM
QList<GwmVariable> mVariables;
CreateResultLayerData mResultList;

public:
gwm::BandwidthWeight finalBandwidth() const;

public:
static int treeChildCount;

Expand Down
2 changes: 2 additions & 0 deletions src/TaskThread/gwmgwcorrelationtaskthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ void GwmGWCorrelationTaskThread::run()
mGWCorrCore->setBandwidthSelectionApproach(bandwidthSelTypes);
mGWCorrCore->setParallelType(static_cast<gwm::ParallelType>(mParallelType));
mGWCorrCore->setOmpThreadNum(mOmpThreadNum);
qDebug() << "core parallelType =" << mGWCorrCore->parallelType();
qDebug() << "core parallelAbility =" << mGWCorrCore->parallelAbility();

// std::vector<gwm::SpatialWeight> a = mGWCorrCore->spatialWeights();
// gwm::BandwidthWeight bwa = a[0].weight<gwm::BandwidthWeight>();
Expand Down
Loading