pysal / momepy

Urban Morphology Measuring Toolkit
https://docs.momepy.org
BSD 3-Clause "New" or "Revised" License
475 stars 58 forks source link

Networkx deprecations #525

Closed virgilxw closed 3 months ago

virgilxw commented 8 months ago

Describe the problem

AttributeError: module 'networkx' has no attribute 'connected_component_subgraphs' occurs when networkx is used because the function is depreciated in networkx 2.3

https://stackoverflow.com/questions/61154740/attributeerror-module-networkx-has-no-attribute-connected-component-subgraph

Steps to reproduce

Momepy implementation

degree = dict(nx.degree(primal)) nx.set_node_attributes(primal, degree, 'degree')

graph = momepy.node_degree(primal, name='degree')

nodes, edges, sw = momepy.nx_to_gdf(graph, points=True, lines=True, spatial_weights=True)

f, ax = plt.subplots(figsize=(10, 10)) nodes.plot(ax=ax, column='degree', cmap='tab20b', markersize=(nodes['degree'] * 100), zorder=2) edges.plot(ax=ax, color='lightgrey', zorder=1) ax.set_axis_off() plt.show()

Versions of your packages

# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
alsa-lib                  1.2.10               hd590300_0    conda-forge
arrow-cpp                 11.0.0          ha770c72_56_cpu    conda-forge
asttokens                 2.4.1              pyhd8ed1ab_0    conda-forge
at-spi2-atk               2.38.0               h0630a04_3    conda-forge
at-spi2-core              2.40.3               h0630a04_0    conda-forge
atk-1.0                   2.38.0               hd4edc92_1    conda-forge
attr                      2.5.1                h166bdaf_1    conda-forge
attrs                     23.1.0          py312h06a4308_0  
aws-c-auth                0.7.8                h538f98c_2    conda-forge
aws-c-cal                 0.6.9                h5d48c4d_2    conda-forge
aws-c-common              0.9.10               hd590300_0    conda-forge
aws-c-compression         0.2.17               h7f92143_7    conda-forge
aws-c-event-stream        0.3.2                h0bcb0bb_8    conda-forge
aws-c-http                0.7.14               hd268abd_3    conda-forge
aws-c-io                  0.13.36              he0cd244_2    conda-forge
aws-c-mqtt                0.10.0               h35285c7_0    conda-forge
aws-c-s3                  0.4.5                h0448019_0    conda-forge
aws-c-sdkutils            0.1.13               h7f92143_0    conda-forge
aws-checksums             0.1.17               h7f92143_6    conda-forge
aws-crt-cpp               0.25.0               h1bbe558_2    conda-forge
aws-sdk-cpp               1.11.210             h0853bfa_5    conda-forge
azure-core-cpp            1.10.3               h91d86a7_0    conda-forge
azure-storage-blobs-cpp   12.10.0              h00ab1b0_0    conda-forge
azure-storage-common-cpp  12.5.0               hb858b4b_2    conda-forge
beautifulsoup4            4.12.2          py312h06a4308_0  
blas                      1.0                    openblas  
blosc                     1.21.5               h0f2a231_0    conda-forge
bokeh                     3.3.0           py312he106c6f_0  
boost-cpp                 1.82.0               hdb19cb5_2  
bottleneck                1.3.5           py312ha883a20_0  
brotli                    1.0.9                he6710b0_2  
brotli-python             1.0.9           py312h6a678d5_7  
bs4                       4.12.2           py38hd3eb1b0_0  
bzip2                     1.0.8                h7b6447c_0  
c-ares                    1.24.0               hd590300_0    conda-forge
ca-certificates           2023.12.12           h06a4308_0  
cairo                     1.18.0               h3faef2a_0    conda-forge
cairomm-1.16              1.16.2               h7e731d7_1    conda-forge
certifi                   2023.11.17      py312h06a4308_0  
cffi                      1.16.0          py312h5eee18b_0  
cfitsio                   4.3.1                hbdc6101_0    conda-forge
charset-normalizer        2.0.4              pyhd3eb1b0_0  
click                     8.1.7           py312h06a4308_0  
click-plugins             1.1.1              pyhd3eb1b0_0  
cligj                     0.7.2              pyhd3eb1b0_0  
cloudpickle               2.2.1           py312h06a4308_0  
comm                      0.1.4              pyhd8ed1ab_0    conda-forge
contourpy                 1.2.0           py312hdb19cb5_0  
cryptography              41.0.7          py312hdda0065_0  
cycler                    0.11.0             pyhd3eb1b0_0  
cytoolz                   0.12.2          py312h5eee18b_0  
dask                      2023.11.0       py312h06a4308_0  
dask-core                 2023.11.0       py312h06a4308_0  
dbus                      1.13.18              hb2f20db_0  
debugpy                   1.8.0           py312h30efb56_1    conda-forge
decorator                 5.1.1              pyhd8ed1ab_0    conda-forge
distributed               2023.11.0       py312h06a4308_0  
epoxy                     1.5.10               h166bdaf_1    conda-forge
exceptiongroup            1.2.0              pyhd8ed1ab_0    conda-forge
executing                 2.0.1              pyhd8ed1ab_0    conda-forge
expat                     2.5.0                h6a678d5_0  
fiona                     1.9.5           py312h66d9856_2    conda-forge
font-ttf-dejavu-sans-mono 2.37                 hd3eb1b0_0  
font-ttf-inconsolata      2.001                hcb22688_0  
font-ttf-source-code-pro  2.030                hd3eb1b0_0  
font-ttf-ubuntu           0.83                 h8b1ccd4_0  
fontconfig                2.14.2               h14ed4e7_0    conda-forge
fonts-anaconda            1                    h8fa9717_0  
fonts-conda-ecosystem     1                    hd3eb1b0_0  
fonttools                 4.25.0             pyhd3eb1b0_0  
freetype                  2.12.1               h4a9f257_0  
freexl                    2.0.0                h743c826_0    conda-forge
fribidi                   1.0.10               h7b6447c_0  
fsspec                    2023.10.0       py312h06a4308_0  
gdal                      3.8.1           py312ha82c271_4    conda-forge
gdk-pixbuf                2.42.10              h829c605_4    conda-forge
geopandas                 0.9.0                      py_1  
geopandas-base            0.9.0                      py_1  
geos                      3.12.1               h59595ed_0    conda-forge
geotiff                   1.7.1               h6b2125f_15    conda-forge
gettext                   0.21.1               h27087fc_0    conda-forge
gflags                    2.2.2                he6710b0_0  
giflib                    5.2.1                h5eee18b_3  
glib                      2.78.3               hfc55251_0    conda-forge
glib-tools                2.78.3               hfc55251_0    conda-forge
glog                      0.6.0                h6f12383_0    conda-forge
gmp                       6.3.0                h59595ed_0    conda-forge
graph-tool                2.58            py312hf63df81_2    conda-forge
graph-tool-base           2.58            py312h0959fd3_2    conda-forge
graphite2                 1.3.14               h295c915_1  
gst-plugins-base          1.22.7               h8e1006c_1    conda-forge
gstreamer                 1.22.7               h98fc4e7_1    conda-forge
gtk3                      3.24.38              h280cfa0_1    conda-forge
harfbuzz                  8.3.0                h3d44ed6_0    conda-forge
hdf4                      4.2.15               h2a13503_7    conda-forge
hdf5                      1.14.3          nompi_h4f84152_100    conda-forge
heapdict                  1.0.1              pyhd3eb1b0_0  
hicolor-icon-theme        0.17                 h06a4308_2  
icu                       73.2                 h59595ed_0    conda-forge
idna                      3.4             py312h06a4308_0  
importlib-metadata        6.0.0           py312h06a4308_0  
importlib_metadata        6.0.0                hd8ed1ab_0    conda-forge
ipykernel                 6.26.0             pyhf8b6a83_0    conda-forge
ipython                   8.18.1             pyh707e725_3    conda-forge
jedi                      0.19.1             pyhd8ed1ab_0    conda-forge
jinja2                    3.1.2           py312h06a4308_0  
joblib                    1.2.0           py312h06a4308_0  
json-c                    0.17                 h7ab15ed_0    conda-forge
jupyter_client            8.6.0              pyhd8ed1ab_0    conda-forge
jupyter_core              5.5.1           py312h7900ff3_0    conda-forge
kealib                    1.5.2                hcd42e92_1    conda-forge
keyutils                  1.6.1                h166bdaf_0    conda-forge
kiwisolver                1.4.4           py312h6a678d5_0  
krb5                      1.21.2               h659d440_0    conda-forge
lame                      3.100                h7b6447c_0  
lcms2                     2.16                 hb7c19ff_0    conda-forge
ld_impl_linux-64          2.38                 h1181459_1  
lerc                      4.0.0                h27087fc_0    conda-forge
libabseil                 20230802.1      cxx17_h59595ed_0    conda-forge
libaec                    1.1.2                h59595ed_1    conda-forge
libarchive                3.7.2                h2aa1ff5_1    conda-forge
libarrow                  11.0.0          h6b1a9bd_56_cpu    conda-forge
libboost                  1.82.0               h109eef0_2  
libboost-python           1.82.0          py312hfb10629_6    conda-forge
libbrotlicommon           1.1.0                hd590300_1    conda-forge
libbrotlidec              1.1.0                hd590300_1    conda-forge
libbrotlienc              1.1.0                hd590300_1    conda-forge
libcap                    2.69                 h0f662aa_0    conda-forge
libclang                  15.0.7          default_hb11cfb5_4    conda-forge
libclang13                15.0.7          default_ha2b6cf4_4    conda-forge
libcrc32c                 1.1.2                h6a678d5_0  
libcups                   2.3.3                h4637d8d_4    conda-forge
libcurl                   8.5.0                hca28451_0    conda-forge
libdeflate                1.19                 hd590300_0    conda-forge
libedit                   3.1.20221030         h5eee18b_0  
libev                     4.33                 h7f8727e_1  
libevent                  2.1.12               hdbd6064_1  
libexpat                  2.5.0                hcb278e6_1    conda-forge
libffi                    3.4.4                h6a678d5_0  
libflac                   1.4.3                h59595ed_0    conda-forge
libgcc-ng                 13.2.0               h807b86a_3    conda-forge
libgcrypt                 1.10.3               hd590300_0    conda-forge
libgdal                   3.8.1                hed8bd54_4    conda-forge
libgfortran-ng            13.2.0               h69a702a_3    conda-forge
libgfortran5              13.2.0               ha4646dd_3    conda-forge
libgirepository           1.78.1               h003a4f0_1    conda-forge
libglib                   2.78.3               h783c2da_0    conda-forge
libgomp                   13.2.0               h807b86a_3    conda-forge
libgoogle-cloud           2.12.0               h5206363_4    conda-forge
libgpg-error              1.47                 h71f35ed_0    conda-forge
libgrpc                   1.59.3               hd6c4280_0    conda-forge
libiconv                  1.17                 hd590300_2    conda-forge
libjpeg-turbo             3.0.0                hd590300_1    conda-forge
libkml                    1.3.0                h096b73e_7  
libllvm15                 15.0.7               hb3ce162_4    conda-forge
libnetcdf                 4.9.2           nompi_h9612171_113    conda-forge
libnghttp2                1.58.0               h47da74e_1    conda-forge
libnl                     3.9.0                hd590300_0    conda-forge
libnsl                    2.0.0                h5eee18b_0  
libnuma                   2.0.16               h0b41bf4_1    conda-forge
libogg                    1.3.5                h27cfd23_1  
libopenblas               0.3.21               h043d6bf_0  
libopus                   1.3.1                h7b6447c_0  
libpng                    1.6.39               h5eee18b_0  
libpq                     16.1                 h33b98f1_7    conda-forge
libprotobuf               4.24.4               hf27288f_0    conda-forge
libpysal                  4.5.1              pyhd3eb1b0_0  
libre2-11                 2023.06.02           h7a70373_0    conda-forge
librsvg                   2.56.3               he3f83f7_1    conda-forge
librttopo                 1.1.0               h8917695_15    conda-forge
libsndfile                1.2.2                hc60ed4a_1    conda-forge
libsodium                 1.0.18               h36c2ea0_1    conda-forge
libspatialindex           1.9.3                h2531618_0  
libspatialite             5.1.0                h7bd4643_4    conda-forge
libsqlite                 3.44.2               h2797004_0    conda-forge
libssh2                   1.11.0               h0841786_0    conda-forge
libstdcxx-ng              13.2.0               h7e041cc_3    conda-forge
libsystemd0               255                  h3516f8a_0    conda-forge
libthrift                 0.19.0               hb90f79a_1    conda-forge
libtiff                   4.6.0                ha9c0a0a_2    conda-forge
libutf8proc               2.8.0                h166bdaf_0    conda-forge
libuuid                   2.38.1               h0b41bf4_0    conda-forge
libvorbis                 1.3.7                h7b6447c_0  
libwebp-base              1.3.2                h5eee18b_0  
libxcb                    1.15                 h7f8727e_0  
libxkbcommon              1.6.0                hd429924_1    conda-forge
libxml2                   2.12.3               h232c23b_0    conda-forge
libzip                    1.10.1               h2629f0a_3    conda-forge
libzlib                   1.2.13               hd590300_5    conda-forge
locket                    1.0.0           py312h06a4308_0  
lz4                       4.3.2           py312h5eee18b_0  
lz4-c                     1.9.4                h6a678d5_0  
lzo                       2.10                 h7b6447c_2  
mapclassify               2.5.0           py312h06a4308_0  
markupsafe                2.1.1           py312h5eee18b_0  
matplotlib                3.8.0           py312h06a4308_0  
matplotlib-base           3.8.0           py312h526ad5a_0  
matplotlib-inline         0.1.6              pyhd8ed1ab_0    conda-forge
minizip                   4.0.3                hf59b114_0  
momepy                    0.6.0              pyhd8ed1ab_2    conda-forge
mpg123                    1.32.3               h59595ed_0    conda-forge
msgpack-python            1.0.3           py312hdb19cb5_0  
munkres                   1.1.4                      py_0  
mysql-common              8.0.33               hf1915f5_6    conda-forge
mysql-libs                8.0.33               hca2cd23_6    conda-forge
ncurses                   6.4                  h6a678d5_0  
nest-asyncio              1.5.8              pyhd8ed1ab_0    conda-forge
networkx                  2.3                        py_0  
nspr                      4.35                 h6a678d5_0  
nss                       3.96                 h1d7d5a4_0    conda-forge
numexpr                   2.8.7           py312he7dcb8a_0  
numpy                     1.26.2          py312h2809609_0  
numpy-base                1.26.2          py312he1a6c75_0  
openjpeg                  2.5.0                h488ebb8_3    conda-forge
openssl                   3.2.0                hd590300_1    conda-forge
orc                       1.9.2                h4b38347_0    conda-forge
packaging                 23.1            py312h06a4308_0  
pandas                    2.1.4           py312h526ad5a_0  
pango                     1.50.14              ha41ecd1_2    conda-forge
parso                     0.8.3              pyhd8ed1ab_0    conda-forge
partd                     1.4.1           py312h06a4308_0  
pcre2                     10.42                hebb0a14_0  
pexpect                   4.8.0              pyh1a96a4e_2    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pillow                    10.1.0          py312hf3581a9_0    conda-forge
pip                       23.3.1          py312h06a4308_0  
pixman                    0.42.2               h59595ed_0    conda-forge
platformdirs              4.1.0              pyhd8ed1ab_0    conda-forge
ply                       3.11            py312h06a4308_1  
poppler                   23.12.0              h590f24d_0    conda-forge
poppler-data              0.4.11               h06a4308_1  
postgresql                16.1                 h7387d8b_7    conda-forge
proj                      9.3.1                h1d62c97_0    conda-forge
prompt-toolkit            3.0.42             pyha770c72_0    conda-forge
psutil                    5.9.0           py312h5eee18b_0  
ptyprocess                0.7.0              pyhd3deb0d_0    conda-forge
pulseaudio-client         16.1                 hb77b528_5    conda-forge
pure_eval                 0.2.2              pyhd8ed1ab_0    conda-forge
pyarrow                   11.0.0          py312hb107042_2  
pycairo                   1.25.1          py312he48a392_0    conda-forge
pycparser                 2.21               pyhd3eb1b0_0  
pygments                  2.17.2             pyhd8ed1ab_0    conda-forge
pygobject                 3.46.0          py312h15a1d3f_1    conda-forge
pyopenssl                 23.2.0          py312h06a4308_0  
pyparsing                 3.0.9           py312h06a4308_0  
pyproj                    3.6.1           py312h38f1c37_5    conda-forge
pyqt                      5.15.10         py312h6a678d5_0  
pyqt5-sip                 12.13.0         py312h5eee18b_0  
pysocks                   1.7.1           py312h06a4308_0  
python                    3.12.0          hab00c5b_0_cpython    conda-forge
python-dateutil           2.8.2              pyhd3eb1b0_0  
python-lmdb               1.4.1           py312h6a678d5_0  
python-tzdata             2023.3             pyhd3eb1b0_0  
python_abi                3.12                    4_cp312    conda-forge
pytz                      2023.3.post1    py312h06a4308_0  
pyyaml                    6.0.1           py312h5eee18b_0  
pyzmq                     25.1.2          py312h886d080_0    conda-forge
qt-main                   5.15.8              h450f30e_18    conda-forge
rdma-core                 49.0                 hd3aeb46_2    conda-forge
re2                       2023.06.02           h2873b5e_0    conda-forge
readline                  8.2                  h5eee18b_0  
requests                  2.31.0          py312h06a4308_0  
rtree                     1.0.1           py312h06a4308_0  
s2n                       1.4.0                h06160fa_0    conda-forge
scikit-learn              1.3.0           py312h526ad5a_2  
scipy                     1.11.4          py312h2809609_0  
setuptools                68.2.2          py312h06a4308_0  
shapely                   2.0.2           py312h9e6bd2c_1    conda-forge
sigcpp-3.0                3.6.0                h59595ed_0    conda-forge
sip                       6.7.12          py312h6a678d5_0  
six                       1.16.0             pyhd3eb1b0_1  
snappy                    1.1.10               h6a678d5_1  
sortedcontainers          2.4.0              pyhd3eb1b0_0  
soupsieve                 2.5             py312h06a4308_0  
sparsehash                2.0.4                hcb278e6_1    conda-forge
sqlite                    3.41.2               h5eee18b_0  
stack_data                0.6.2              pyhd8ed1ab_0    conda-forge
tblib                     1.7.0              pyhd3eb1b0_0  
threadpoolctl             2.2.0              pyh0d69192_0  
tiledb                    2.18.3               hc1131af_1    conda-forge
tk                        8.6.13          noxft_h4845f30_101    conda-forge
toolz                     0.12.0          py312h06a4308_0  
tornado                   6.3.3           py312h5eee18b_0  
tqdm                      4.65.0          py312he106c6f_0  
traitlets                 5.14.0             pyhd8ed1ab_0    conda-forge
typing_extensions         4.9.0              pyha770c72_0    conda-forge
tzcode                    2023c                h0b41bf4_0    conda-forge
tzdata                    2023c                h04d1e81_0  
ucx                       1.15.0               h75e419f_2    conda-forge
uriparser                 0.9.7                h5eee18b_0  
urllib3                   1.26.18         py312h06a4308_0  
wayland                   1.22.0               h8c25dac_1    conda-forge
wcwidth                   0.2.12             pyhd8ed1ab_0    conda-forge
wheel                     0.41.2          py312h06a4308_0  
xcb-util                  0.4.0                hd590300_1    conda-forge
xcb-util-image            0.4.0                h8ee46fc_1    conda-forge
xcb-util-keysyms          0.4.0                h8ee46fc_1    conda-forge
xcb-util-renderutil       0.3.9                hd590300_1    conda-forge
xcb-util-wm               0.4.1                h8ee46fc_1    conda-forge
xerces-c                  3.2.4                h6a678d5_1  
xkeyboard-config          2.40                 hd590300_0    conda-forge
xorg-compositeproto       0.4.2             h7f98852_1001    conda-forge
xorg-damageproto          1.2.1             h7f98852_1002    conda-forge
xorg-fixesproto           5.0               h7f98852_1002    conda-forge
xorg-inputproto           2.3.2             h7f98852_1002    conda-forge
xorg-kbproto              1.0.7             h7f98852_1002    conda-forge
xorg-libice               1.1.1                hd590300_0    conda-forge
xorg-libsm                1.2.4                h7391055_0    conda-forge
xorg-libx11               1.8.7                h8ee46fc_0    conda-forge
xorg-libxau               1.0.11               hd590300_0    conda-forge
xorg-libxcomposite        0.4.6                h0b41bf4_1    conda-forge
xorg-libxcursor           1.2.0                h0b41bf4_1    conda-forge
xorg-libxdamage           1.1.5                h7f98852_1    conda-forge
xorg-libxext              1.3.4                h0b41bf4_2    conda-forge
xorg-libxfixes            5.0.3             h7f98852_1004    conda-forge
xorg-libxi                1.7.10               h7f98852_0    conda-forge
xorg-libxinerama          1.1.5                h27087fc_0    conda-forge
xorg-libxrandr            1.5.2                h7f98852_1    conda-forge
xorg-libxrender           0.9.11               hd590300_0    conda-forge
xorg-libxtst              1.2.3             h7f98852_1002    conda-forge
xorg-randrproto           1.5.0             h7f98852_1001    conda-forge
xorg-recordproto          1.14.2            h7f98852_1002    conda-forge
xorg-renderproto          0.11.1            h7f98852_1002    conda-forge
xorg-util-macros          1.19.0               h27cfd23_2  
xorg-xextproto            7.3.0             h0b41bf4_1003    conda-forge
xorg-xf86vidmodeproto     2.3.1             h7f98852_1002    conda-forge
xorg-xproto               7.0.31            h27cfd23_1007  
xyzservices               2022.9.0        py312h06a4308_1  
xz                        5.4.5                h5eee18b_0  
yaml                      0.2.5                h7b6447c_0  
zeromq                    4.3.5                h59595ed_0    conda-forge
zict                      3.0.0           py312h06a4308_0  
zipp                      3.11.0          py312h06a4308_0  
zlib                      1.2.13               hd590300_5    conda-forge
zstandard                 0.19.0          py312h5eee18b_0  
zstd                      1.5.5                hc292b87_0 

Your operating system

No response

Additional context

No response

martinfleis commented 8 months ago

Thanks for the report. Though we don't use connected_component_subgraphs in our code. Can you post the full traceback?

virgilxw commented 8 months ago

AttributeError Traceback (most recent call last) Cell In[12], line 8 4 nx.set_node_attributes(primal, degree, 'degree') 6 graph = momepy.node_degree(primal, name='degree') ----> 8 nodes, edges, sw = momepy.nx_to_gdf(graph, points=True, lines=True, 9 spatial_weights=True) 11 f, ax = plt.subplots(figsize=(10, 10)) 12 nodes.plot(ax=ax, column='degree', cmap='tab20b', markersize=(nodes['degree'] * 100), zorder=2)

File ~/miniforge3/envs/processor/lib/python3.12/site-packages/momepy/utils.py:438, in nx_to_gdf(net, points, lines, spatial_weights, nodeID) 435 for nid, n in enumerate(net): 436 net.nodes[n][nodeID] = nid --> 438 return _primal_to_gdf( 439 net, 440 points=points, 441 lines=lines, 442 spatial_weights=spatial_weights, 443 node_id=nodeID, 444 )

File ~/miniforge3/envs/processor/lib/python3.12/site-packages/momepy/utils.py:319, in _primal_to_gdf(net, points, lines, spatial_weights, node_id) 316 gdf_nodes = _points_to_gdf(net) 318 if spatial_weights is True: --> 319 weights = libpysal.weights.W.from_networkx(net) ... --> 352 sparse_matrix = nx.to_scipy_sparse_matrix(graph) 353 w = WSP(sparse_matrix).to_W() 354 return w

AttributeError: module 'networkx' has no attribute 'to_scipy_sparse_matrix' Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

martinfleis commented 8 months ago

It is truncated. Can you open the full version in VS Code and copy that please? Also, this shows a different error than reported above?

martinfleis commented 8 months ago

I think that the issue comes from the outdated libpysal in your environment. The version 4.5.1 is more than 2 years old. I suggest updating libpysal (and ideally also momepy) to their latest versions.

martinfleis commented 3 months ago

Closing due to missing info. Can reopen if it pops up again in relevant versions.