Skip to content

Commit fae19a6

Browse files
authored
Merge pull request #51 from jGaboardi/lint_notebooks
Lint notebooks & add notebooks dir to pre-commit
2 parents e1f4373 + b68f3cf commit fae19a6

21 files changed

+464
-1444
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
files: "spint\/"
1+
files: "spint\/|notebooks\/"
22
repos:
33
- repo: https://github.com/astral-sh/ruff-pre-commit
44
rev: "v0.15.2"

notebooks/4d_distance.ipynb

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
"outputs": [],
1313
"source": [
1414
"import numpy as np\n",
15-
"from scipy.spatial import distance\n",
1615
"import scipy.spatial as spatial\n",
16+
"from pysal.weights import Distance as Distance\n",
1717
"from pysal.weights import W\n",
1818
"from pysal.weights.util import isKDTree\n",
19-
"from pysal.weights import Distance as Distance"
19+
"from scipy.spatial import KDTree, distance"
2020
]
2121
},
2222
{
@@ -66,6 +66,7 @@
6666
" distance.euclidean(np.array((30, 46, 23, 80)), np.array((30, 46, 23, 80)))\n",
6767
"\n",
6868
"\n",
69+
"\"\"\"\n",
6970
"def distpython():\n",
7071
" dist(np.array((67, 46, 92, 67)), np.array((44, 97, 25, 50)))\n",
7172
" dist(np.array((67, 46, 92, 67)), np.array((84, 37, 66, 53)))\n",
@@ -83,11 +84,11 @@
8384
" dist(np.array((30, 46, 23, 80)), np.array((44, 97, 25, 50)))\n",
8485
" dist(np.array((30, 46, 23, 80)), np.array((30, 46, 23, 80)))\n",
8586
"\n",
86-
"\n",
8787
"def scpkdtree():\n",
88-
" data = zip(x.ravel(), y.ravel(), w.ravel(), z.ravel())\n",
88+
" data = zip(x.ravel(), y.ravel(), w.ravel(), z.ravel(), strict=True)\n",
8989
" tree = spatial.KDTree(data)\n",
90-
" W = pysal.weights.DistanceBand(tree, threshold=9999, alpha=-1.5, binary=False)"
90+
" W = pysal.weights.DistanceBand(tree, threshold=9999, alpha=-1.5, binary=False)\n",
91+
"\"\"\""
9192
]
9293
},
9394
{
@@ -122,9 +123,9 @@
122123
"w = np.random.randint(1, 1000, 3000)\n",
123124
"z = np.random.randint(1, 1000, 3000)\n",
124125
"\n",
125-
"data = zip(x.ravel(), y.ravel(), w.ravel(), z.ravel())\n",
126+
"data = zip(x.ravel(), y.ravel(), w.ravel(), z.ravel(), strict=True)\n",
126127
"tree = spatial.KDTree(data)\n",
127-
"W = pysal.weights.DistanceBand(tree, threshold=9999, alpha=-1.5, binary=False)"
128+
"# W = pysal.weights.DistanceBand(tree, threshold=9999, alpha=-1.5, binary=False)"
128129
]
129130
},
130131
{
@@ -193,8 +194,8 @@
193194
" data = data.astype(float)\n",
194195
" self.data = data\n",
195196
" self.kd = KDTree(self.data)\n",
196-
" except:\n",
197-
" raise ValueError(\"Could not make array from data\")\n",
197+
" except Exception:\n",
198+
" raise ValueError(\"Could not make array from data\") from None\n",
198199
"\n",
199200
" self.p = p\n",
200201
" self.threshold = threshold\n",
@@ -214,14 +215,11 @@
214215
" self.dmat = np.array(distance_matrix(self.data, self.data))\n",
215216
"\n",
216217
" def _distance_to_W(self, ids=None):\n",
217-
" if ids:\n",
218-
" ids = np.array(ids)\n",
219-
" else:\n",
220-
" ids = np.arange(self.dmat.shape[0])\n",
221-
" neighbors = dict([(i, []) for i in ids])\n",
222-
" weights = dict([(i, []) for i in ids])\n",
218+
" ids = np.array(ids) if ids else np.arange(self.dmat.shape[0])\n",
219+
" neighbors = {i: [] for i in ids}\n",
220+
" weights = {i: [] for i in ids}\n",
223221
" if self.binary:\n",
224-
" for key, weight in self.dmat.items():\n",
222+
" for key, _ in self.dmat.items():\n",
225223
" i, j = key\n",
226224
" if i != j:\n",
227225
" if j not in neighbors[i]:\n",
@@ -232,7 +230,7 @@
232230
" neighbors[j].append(i)\n",
233231
"\n",
234232
" else:\n",
235-
" weighted = np.array(map(lambda x: pow(x, -1.5), self.dmat))\n",
233+
" weighted = np.array(map(lambda x: pow(x, -1.5), self.dmat)) # noqa: C417 Unnecessary `map()`\n",
236234
" print(weighted.shape)\n",
237235
" rows, cols = self.dmat.shape\n",
238236
" for i in range(rows):\n",
@@ -264,7 +262,7 @@
264262
"w = np.random.randint(1, 1000, 500)\n",
265263
"z = np.random.randint(1, 1000, 500)\n",
266264
"\n",
267-
"data = zip(x.ravel(), y.ravel(), w.ravel(), z.ravel())\n",
265+
"data = zip(x.ravel(), y.ravel(), w.ravel(), z.ravel(), strict=True)\n",
268266
"tree = spatial.KDTree(data)"
269267
]
270268
},
@@ -426,13 +424,6 @@
426424
"source": [
427425
"np.allclose(W_new.full()[0], W_old.full()[0])"
428426
]
429-
},
430-
{
431-
"cell_type": "code",
432-
"execution_count": null,
433-
"metadata": {},
434-
"outputs": [],
435-
"source": []
436427
}
437428
],
438429
"metadata": {

notebooks/Example_NYCBikes_AllFeatures.ipynb

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,20 @@
1111
},
1212
"outputs": [],
1313
"source": [
14-
"from pysal.contrib.spint.gravity import (\n",
15-
" BaseGravity,\n",
16-
" Gravity,\n",
17-
" Production,\n",
18-
" Attraction,\n",
19-
" Doubly,\n",
20-
")\n",
21-
"from pysal.contrib.spint.dispersion import phi_disp\n",
22-
"from pysal.contrib.spint.vec_SA import VecMoran\n",
23-
"import pysal as ps\n",
24-
"import pandas as pd\n",
2514
"import geopandas as gp\n",
26-
"import numpy as np\n",
27-
"import seaborn as sb\n",
2815
"import matplotlib.pylab as plt\n",
16+
"import numpy as np\n",
17+
"import pandas as pd\n",
18+
"import pysal as ps\n",
2919
"\n",
30-
"%pylab inline\n",
31-
"from descartes import PolygonPatch\n",
32-
"import matplotlib as mpl\n",
33-
"from mpl_toolkits.basemap import Basemap\n",
34-
"import pyproj as pj\n",
35-
"from shapely.geometry import Polygon, Point"
20+
"from spint.dispersion import phi_disp\n",
21+
"from spint.gravity import (\n",
22+
" Attraction,\n",
23+
" Doubly,\n",
24+
" Gravity,\n",
25+
" Production,\n",
26+
")\n",
27+
"from spint.vec_SA import VecMoran"
3628
]
3729
},
3830
{
@@ -87,7 +79,8 @@
8779
" ]\n",
8880
")\n",
8981
"\n",
90-
"# Destination variables: square footage of buildings, housing units, total station capacity\n",
82+
"# Destination variables:\n",
83+
"# square footage of buildings, housing units, total station capacity\n",
9184
"d_vars = np.hstack(\n",
9285
" [\n",
9386
" bikes[\"d_sq_foot\"].values.reshape((-1, 1)),\n",
@@ -209,9 +202,11 @@
209202
},
210203
"outputs": [],
211204
"source": [
212-
"# Next, we can test the models for violations of the equidispersion assumption of Poisson models\n",
205+
"# Next, we can test the models for violations of the\n",
206+
"# equidispersion assumption of Poisson models\n",
213207
"\n",
214-
"# test the hypotehsis of equidispersion (var[mu] = mu) against that of QuasiPoisson (var[mu] = phi * mu)\n",
208+
"# test the hypotehsis of equidispersion\n",
209+
"# (var[mu] = mu) against that of QuasiPoisson (var[mu] = phi * mu)\n",
215210
"# Results = [phi, tvalue, pvalue]\n",
216211
"print(phi_disp(grav))\n",
217212
"print(phi_disp(prod))\n",
@@ -235,7 +230,8 @@
235230
},
236231
"outputs": [],
237232
"source": [
238-
"# As a result we can compare our standard errors and tvalues for a Poisson model to a QuasiPoisson\n",
233+
"# As a result we can compare our standard errors\n",
234+
"# and tvalues for a Poisson model to a QuasiPoisson\n",
239235
"\n",
240236
"print(\"Production-constrained Poisson model standard errors and tvalues\")\n",
241237
"print(prod.params[-4:])\n",
@@ -343,7 +339,8 @@
343339
},
344340
"outputs": [],
345341
"source": [
346-
"# Plot local \"cost\" values: darker blue is stronger distance decay; grey is no data\n",
342+
"# Plot local \"cost\" values:\n",
343+
"# darker blue is stronger distance decay; grey is no data\n",
347344
"\n",
348345
"fig = plt.figure(figsize=(12, 12))\n",
349346
"ax = fig.add_subplot(111)\n",
@@ -366,7 +363,8 @@
366363
},
367364
"outputs": [],
368365
"source": [
369-
"# Plot local estimates for destination capacity: darker red is larger effect; grey is no data\n",
366+
"# Plot local estimates for destination capacity:\n",
367+
"# darker red is larger effect; grey is no data\n",
370368
"\n",
371369
"fig = plt.figure(figsize=(12, 12))\n",
372370
"ax = fig.add_subplot(111)\n",
@@ -390,7 +388,8 @@
390388
},
391389
"outputs": [],
392390
"source": [
393-
"# Plot local estimates for # of housing units: darker red is larger effect; grey is no data\n",
391+
"# Plot local estimates for # of housing units:\n",
392+
"# darker red is larger effect; grey is no data\n",
394393
"\n",
395394
"fig = plt.figure(figsize=(12, 12))\n",
396395
"ax = fig.add_subplot(111)\n",
@@ -414,7 +413,8 @@
414413
},
415414
"outputs": [],
416415
"source": [
417-
"# Plot local estimates for destination building sq footage: darker red is larger effect; grey is no data\n",
416+
"# Plot local estimates for destination building sq footage:\n",
417+
"# darker red is larger effect; grey is no data\n",
418418
"\n",
419419
"fig = plt.figure(figsize=(12, 12))\n",
420420
"ax = fig.add_subplot(111)\n",
@@ -459,7 +459,8 @@
459459
},
460460
"outputs": [],
461461
"source": [
462-
"# Prep OD data as vectors and then compute origin or destination focused distance-based weights\n",
462+
"# Prep OD data as vectors and then compute\n",
463+
"# origin or destination focused distance-based weights\n",
463464
"\n",
464465
"ids = bikes[\"index\"].reshape((-1, 1))\n",
465466
"origin_x = bikes[\"SX\"].reshape((-1, 1))\n",

notebooks/NYC_Bike_Example.ipynb

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,11 @@
1111
},
1212
"outputs": [],
1313
"source": [
14-
"import os\n",
15-
"\n",
16-
"os.chdir(\"../\")\n",
17-
"from gravity import Gravity, Production, Attraction, Doubly\n",
18-
"\n",
19-
"os.chdir(\"/Users/toshan/dev/pysal/pysal/contrib/spint\")\n",
20-
"import entropy as grav\n",
21-
"import numpy as np\n",
22-
"import scipy.stats as stats\n",
23-
"import pandas as pd\n",
24-
"import seaborn as sns\n",
2514
"import geopandas as gp\n",
26-
"\n",
27-
"os.chdir(\"/Users/toshan/Dropbox/Data/NYC_BIKES\")\n",
2815
"import matplotlib.pylab as plt\n",
29-
"\n",
30-
"%pylab inline\n",
31-
"from descartes import PolygonPatch\n",
32-
"import matplotlib as mpl\n",
33-
"from mpl_toolkits.basemap import Basemap\n",
34-
"import pyproj as pj\n",
35-
"from shapely.geometry import Polygon, Point"
16+
"import numpy as np\n",
17+
"import pandas as pd\n",
18+
"from gravity import Doubly, Gravity, Production"
3619
]
3720
},
3821
{
@@ -89,9 +72,6 @@
8972
},
9073
"outputs": [],
9174
"source": [
92-
"os.chdir(\"/Users/toshan/dev/pysal/pysal/contrib/spint\")\n",
93-
"from gravity import Gravity, Production, Attraction, Doubly\n",
94-
"\n",
9575
"model = Gravity(flows, o_vars, d_vars, cost, \"exp\")\n",
9676
"print(model.params)\n",
9777
"print(model.deviance)"

0 commit comments

Comments
 (0)