Skip to content

Commit de0efba

Browse files
committed
use data from geodatasets in examples
1 parent 52e063f commit de0efba

File tree

4 files changed

+58722
-5511
lines changed

4 files changed

+58722
-5511
lines changed

README.md

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ PySGN can be installed using pip:
1515
pip install pysgn
1616
```
1717

18-
If you plan to run the Getting Started notebook `docs/getting_started.ipynb` (or build the documentation) locally, install the optional `docs` extras to get the other dependencies such as Jupyter and Sphinx:
18+
If you plan to run the code snippets below or the Getting Started notebook `docs/getting_started.ipynb` locally, install the optional `docs` extras to get the other dependencies such as geodatasets, Jupyter and Sphinx:
1919

2020
```bash
2121
pip install "pysgn[docs]"
@@ -33,12 +33,21 @@ pip install -e ".[docs]"
3333

3434
Here's a simple example of how to use the `geo_erdos_renyi_network` function to create a geospatial Erdős-Rényi network. It generates a network where each pair of nodes is connected with probability `p`, which depends on the spatial distance between the nodes. The parameter `a` controls the rate of decay of the connection probability with distance.
3535

36+
All PySGN functions expect the input GeoDataFrame to contain a single geometry type (Points or Polygons).
37+
3638
```python
39+
import geodatasets
3740
import geopandas as gpd
3841
from pysgn import geo_erdos_renyi_network
3942

40-
# Load your geospatial data into a GeoDataFrame
41-
gdf = gpd.read_file('path/to/your/geospatial_data.shp')
43+
# Load the sample grocery-store points from geodatasets
44+
# and explode the GeoDataFrame into single points (one point per row).
45+
gdf = (
46+
gpd.read_file(geodatasets.get_path("geoda.groceries"))
47+
.explode(index_parts=False)
48+
.reset_index(drop=True)
49+
.to_crs("EPSG:26971")
50+
)
4251

4352
# Create a geospatial Erdős-Rényi network
4453
graph = geo_erdos_renyi_network(gdf, a=3)
@@ -53,11 +62,16 @@ print(f"Number of edges: {graph.number_of_edges()}")
5362
Similarly you can use the `geo_watts_strogatz_network` function to create a geospatial Watts-Strogatz network. It first creates a network where each node is connected to its `k` nearest neighbors. Then, it rewires each edge with probability `p`. If an edge is chosen to be rewired, it is replaced with a new edge to a random node, where the probability of connecting to this new node is inversely proportional to the spatial distance.
5463

5564
```python
65+
import geodatasets
5666
import geopandas as gpd
5767
from pysgn import geo_watts_strogatz_network
5868

59-
# Load your geospatial data into a GeoDataFrame
60-
gdf = gpd.read_file('path/to/your/geospatial_data.shp')
69+
gdf = (
70+
gpd.read_file(geodatasets.get_path("geoda.groceries"))
71+
.explode(index_parts=False)
72+
.reset_index(drop=True)
73+
.to_crs("EPSG:26971")
74+
)
6175

6276
# Create a geospatial Watts-Strogatz network
6377
graph = geo_watts_strogatz_network(
@@ -77,12 +91,17 @@ print(f"Number of edges: {graph.number_of_edges()}")
7791
You can also use the `geo_barabasi_albert_network` function to create a geospatial Barabási-Albert network. It creates a network using geospatial preferential attachment, where the probability of connecting to existing nodes depends on both their degrees and the spatial distances.
7892

7993
```python
94+
import geodatasets
8095
import geopandas as gpd
8196
from pysgn import geo_barabasi_albert_network
8297
from pysgn.ordering import density_order
8398

84-
# Load your geospatial data into a GeoDataFrame
85-
gdf = gpd.read_file('path/to/your/geospatial_data.shp')
99+
gdf = (
100+
gpd.read_file(geodatasets.get_path("geoda.groceries"))
101+
.explode(index_parts=False)
102+
.reset_index(drop=True)
103+
.to_crs("EPSG:26971")
104+
)
86105

87106
# Create a geospatial Barabási-Albert network
88107
graph = geo_barabasi_albert_network(

docs/getting_started.ipynb

Lines changed: 58668 additions & 5497 deletions
Large diffs are not rendered by default.

docs/index.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ PySGN can be installed using pip:
1515
pip install pysgn
1616
```
1717

18-
If you plan to run the Getting Started notebook `docs/getting_started.ipynb` (or build the documentation) locally, install the optional `docs` extras to get the other dependencies such as Jupyter and Sphinx:
18+
If you plan to run the code snippets below or the Getting Started notebook `docs/getting_started.ipynb` locally, install the optional `docs` extras to get the other dependencies such as geodatasets, Jupyter and Sphinx:
1919

2020
```bash
2121
pip install "pysgn[docs]"
@@ -33,12 +33,21 @@ pip install -e ".[docs]"
3333

3434
Here's a simple example of how to use the `geo_erdos_renyi_network` function to create a geospatial Erdős-Rényi network. It generates a network where each pair of nodes is connected with probability `p`, which depends on the spatial distance between the nodes. The parameter `a` controls the rate of decay of the connection probability with distance.
3535

36+
All PySGN functions expect the input GeoDataFrame to contain a single geometry type (Points or Polygons).
37+
3638
```python
39+
import geodatasets
3740
import geopandas as gpd
3841
from pysgn import geo_erdos_renyi_network
3942

40-
# Load your geospatial data into a GeoDataFrame
41-
gdf = gpd.read_file('path/to/your/geospatial_data.shp')
43+
# Load the sample grocery-store points from geodatasets
44+
# and explode the GeoDataFrame into single points (one point per row).
45+
gdf = (
46+
gpd.read_file(geodatasets.get_path("geoda.groceries"))
47+
.explode(index_parts=False)
48+
.reset_index(drop=True)
49+
.to_crs("EPSG:26971")
50+
)
4251

4352
# Create a geospatial Erdős-Rényi network
4453
graph = geo_erdos_renyi_network(gdf, a=3)
@@ -53,11 +62,16 @@ print(f"Number of edges: {graph.number_of_edges()}")
5362
Similarly you can use the `geo_watts_strogatz_network` function to create a geospatial Watts-Strogatz network. It first creates a network where each node is connected to its `k` nearest neighbors. Then, it rewires each edge with probability `p`. If an edge is chosen to be rewired, it is replaced with a new edge to a random node, where the probability of connecting to this new node is inversely proportional to the spatial distance.
5463

5564
```python
65+
import geodatasets
5666
import geopandas as gpd
5767
from pysgn import geo_watts_strogatz_network
5868

59-
# Load your geospatial data into a GeoDataFrame
60-
gdf = gpd.read_file('path/to/your/geospatial_data.shp')
69+
gdf = (
70+
gpd.read_file(geodatasets.get_path("geoda.groceries"))
71+
.explode(index_parts=False)
72+
.reset_index(drop=True)
73+
.to_crs("EPSG:26971")
74+
)
6175

6276
# Create a geospatial Watts-Strogatz network
6377
graph = geo_watts_strogatz_network(
@@ -77,12 +91,17 @@ print(f"Number of edges: {graph.number_of_edges()}")
7791
You can also use the `geo_barabasi_albert_network` function to create a geospatial Barabási-Albert network. It creates a network using geospatial preferential attachment, where the probability of connecting to existing nodes depends on both their degrees and the spatial distances.
7892

7993
```python
94+
import geodatasets
8095
import geopandas as gpd
8196
from pysgn import geo_barabasi_albert_network
8297
from pysgn.ordering import density_order
8398

84-
# Load your geospatial data into a GeoDataFrame
85-
gdf = gpd.read_file('path/to/your/geospatial_data.shp')
99+
gdf = (
100+
gpd.read_file(geodatasets.get_path("geoda.groceries"))
101+
.explode(index_parts=False)
102+
.reset_index(drop=True)
103+
.to_crs("EPSG:26971")
104+
)
86105

87106
# Create a geospatial Barabási-Albert network
88107
graph = geo_barabasi_albert_network(
@@ -117,6 +136,7 @@ Don't forget to check out the [Contributors guide](https://github.com/wang-boyu/
117136
PySGN is released under the MIT License.
118137

119138

139+
120140
```{toctree}
121141
---
122142
maxdepth: 2

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ docs = [
5151
"jupyterlab-lsp",
5252
"python-lsp-server",
5353
"isort",
54+
"geodatasets",
5455
]
5556

5657
[project.urls]

0 commit comments

Comments
 (0)