Skip to content

Commit

Permalink
Merge pull request #104 from Ziqi-Li/master
Browse files Browse the repository at this point in the history
Allowing strictly global (inf) bandwidth for gwr/mgwr
  • Loading branch information
Ziqi-Li committed Nov 8, 2021
2 parents 905d187 + 3a83ec0 commit 5172d80
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 75 deletions.
17 changes: 17 additions & 0 deletions .ci/37.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: test
channels:
- conda-forge
dependencies:
- python=3.7
- libpysal
- numpy>=1.3
- pandas>=1
- scikit-learn>=0.22
- scipy>=1.0
- spglm
- spreg
# testing
- codecov
- coverage
- pytest
- pytest-cov
17 changes: 17 additions & 0 deletions .ci/38.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: test
channels:
- conda-forge
dependencies:
- python=3.8
- libpysal
- numpy>=1.3
- pandas>=1
- scikit-learn>=0.22
- scipy>=1.0
- spglm
- spreg
# testing
- codecov
- coverage
- pytest
- pytest-cov
18 changes: 18 additions & 0 deletions .ci/39-DEV.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: test
channels:
- conda-forge
dependencies:
- python=3.9
- libpysal
- shapely
- numpy>=1.3
- pandas>=1
- scikit-learn>=0.22
- scipy>=1.0
- spglm
- spreg
# testing
- codecov
- coverage
- pytest
- pytest-cov
17 changes: 17 additions & 0 deletions .ci/39.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: test
channels:
- conda-forge
dependencies:
- python=3.9
- libpysal
- numpy>=1.3
- pandas>=1
- scikit-learn>=0.22
- scipy>=1.0
- spglm
- spreg
# testing
- codecov
- coverage
- pytest
- pytest-cov
61 changes: 61 additions & 0 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
on:
push:
branches:
- '*'
pull_request:
branches:
- '*'
schedule:
- cron: '59 23 * * *'

jobs:
unittests:
name: ${{ matrix.os }}, ${{ matrix.environment-file }}
runs-on: ${{ matrix.os }}
timeout-minutes: 30
strategy:
matrix:
os: ['macos-latest', 'ubuntu-latest', 'windows-latest']
environment-file: [.ci/37.yaml, .ci/38.yaml, .ci/39.yaml]
include:
- environment-file: .ci/39-DEV.yaml
os: ubuntu-latest

steps:
- name: checkout repo
uses: actions/checkout@v2

- name: setup micromamba
uses: mamba-org/provision-with-micromamba@main
with:
environment-file: ${{ matrix.environment-file }}
micromamba-version: 'latest'
- name: install spglm via pip
shell: bash -l {0}
run: pip install spglm

- name: install spreg via pip
shell: bash -l {0}
run: pip install spreg

- name: install bleeding edge libpysal (only Ubuntu / Python 3.9)
shell: bash -l {0}
run: pip install git+https://github.com/pysal/libpysal.git@master
if: matrix.os == 'ubuntu-latest' && contains(matrix.environment-file, 'DEV')

- name: run tests - bash
shell: bash -l {0}
run: pytest -v mgwr --cov=mgwr --cov-report=xml
if: matrix.os != 'windows-latest'

- name: run tests - powershell
shell: powershell
run: pytest -v mgwr --cov=mgwr --cov-report=xml
if: matrix.os == 'windows-latest'

- name: codecov
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
name: spot-codecov
67 changes: 0 additions & 67 deletions .travis.yml

This file was deleted.

4 changes: 4 additions & 0 deletions mgwr/gwr.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ def __init__(self, coords, y, X, bw, family=Gaussian(), offset=None,
self.hat_matrix = hat_matrix

def _build_wi(self, i, bw):

if bw == np.inf:
wi = np.ones((self.n))
return wi

try:
wi = Kernel(i, self.coords, bw, fixed=self.fixed,
Expand Down
29 changes: 23 additions & 6 deletions mgwr/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import numpy as np
from copy import deepcopy


def golden_section(a, c, delta, function, tol, max_iter, int_score=False,
def golden_section(a, c, delta, function, tol, max_iter, bw_max, int_score=False,
verbose=False):
"""
Golden section search routine
Expand Down Expand Up @@ -43,14 +42,19 @@ def golden_section(a, c, delta, function, tol, max_iter, int_score=False,
output : list of tuples
searching history
"""
b = a + delta * np.abs(c - a)
d = c - delta * np.abs(c - a)
score = 0.0
if c == np.inf:
b = a + delta * np.abs(n - a)
d = n - delta * np.abs(n - a)
else:
b = a + delta * np.abs(c - a)
d = c - delta * np.abs(c - a)

opt_score = np.inf
diff = 1.0e9
iters = 0
output = []
dict = {}
while np.abs(diff) > tol and iters < max_iter:
while np.abs(diff) > tol and iters < max_iter and a != np.inf:
iters += 1
if int_score:
b = np.round(b)
Expand Down Expand Up @@ -96,6 +100,19 @@ def golden_section(a, c, delta, function, tol, max_iter, int_score=False,

diff = score_b - score_d
score = opt_score


if a == np.inf or bw_max == np.inf:
score_ols = function(np.inf)
output.append((np.inf, score_ols))

if score_ols <= opt_score:
opt_score = score_ols
opt_val = np.inf

if verbose:
print("Bandwidth: ", np.inf, ", score: ",
"{0:.2f}".format(score_ols[0]))

return opt_val, opt_score, output

Expand Down
4 changes: 2 additions & 2 deletions mgwr/sel_bw.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def _bw(self):
self.constant)
delta = 0.38197 #1 - (np.sqrt(5.0)-1.0)/2.0
self.bw = golden_section(a, c, delta, gwr_func, self.tol,
self.max_iter, self.int_score,
self.max_iter, self.bw_max, self.int_score,
self.verbose)
elif self.search_method == 'interval':
self.bw = equal_interval(self.bw_min, self.bw_max, self.interval,
Expand Down Expand Up @@ -432,7 +432,7 @@ def _init_section(self, X_glob, X_loc, coords, constant):

if self.bw_min is not None:
a = self.bw_min
if self.bw_max is not None:
if self.bw_max is not None and self.bw_max is not np.inf:
c = self.bw_max

return a, c

0 comments on commit 5172d80

Please sign in to comment.