chinese-postman icon indicating copy to clipboard operation
chinese-postman copied to clipboard

AttributeError: module 'networkx' has no attribute 'connected_component_subgraphs'

Open u7390 opened this issue 2 years ago • 12 comments

I read https://github.com/rkistner/chinese-postman/issues/21 . Doing pip install --force-reinstall networkx==2.3 on Command Prompt for Windows operating system and Terminal on Linux operating system does not solve the problem when using it as a plugin for QGIS Desktop software.

u7390 avatar Nov 26 '22 20:11 u7390

The following images show what I did with red lines going around the button I clicked. If there is more than 1 line going around, there is number on the left showing the order it was clicked. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

u7390 avatar Nov 26 '22 21:11 u7390

experiencing the same on macOS🥲

MgTheProgrammer avatar Dec 06 '22 14:12 MgTheProgrammer

experiencing the same on macOS🥲

turns out unlike I've read somewhere qgis does install it's own python interpter. I've run pip install --force-reinstall networkx==2.3 on the pip of the qgis python installation and got one step further!

I did encounter another issue though but thankfully it was an easy fix. qgis comes with python3.9 which has moved some function called gcd from a module named fractions to math. I've changed in a file named dag.py from fractions import gcd to from math import gcd and it seems like everything works now.

MgTheProgrammer avatar Dec 06 '22 19:12 MgTheProgrammer

@MgTheProgrammer How did you run the QGIS version of PIP? I tried running in from a Windows command prompt in the directory that pip.exe is installed in under QGIS, but get a fatal python error and no module named encodings.

michael-ts avatar Dec 24 '22 23:12 michael-ts

@MgTheProgrammer I also tried:

import pip
pip.main(['install', '--force-reinstall', 'networkx==2.3'])

But got tons of errors. Next I tried:

pip.main(['install', '--force-reinstall networkx==2.3'])

But this crashes QGIS (I opened, re-ran the import and called it again and it crashes every time)

Another thing I tried was running this from the QGIS python directory:

python -m pip install --force-reinstall networkx==2.3

But this failed with a message pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available following which it failed to download anything due to the missing ssl module.

michael-ts avatar Dec 25 '22 00:12 michael-ts

@michael-ts Sadly I don't quite remember what I did. I imagine though that I ran pip using its directory (on Mac it probably looked like that: /Applications/QGIS.app/Contents/MacOS/bin/pip3 install networkx==2.3).

I opened qgis now as you reminded me about this. And weirdly enough it doesn't work correctly, it gives me a path and concludes that its length is 0 km. I'm pretty sure last time I've played with it, it worked though. So I guess it's more related to something wrong with the input I give from qgis.

MgTheProgrammer avatar Dec 25 '22 08:12 MgTheProgrammer

Btw if what you are looking for is mostly an optimized route and less about specifically using it with qgis. This repo works without much effort from you as a user. It does give a bit less control but it's good enough imo.

MgTheProgrammer avatar Dec 25 '22 08:12 MgTheProgrammer

@MgTheProgrammer Thanks, I'm looking at this and it looks like it might be helpful at least until I can figure out how to get the QGIS plug-in working. I noticed a couple of issues which using QGIS may or may not solve:

  1. I accidentally deleted a segment in the middle of a road I wanted to follow (not sure why; apparently it tries to find the closest segment to the mouse and despite the segment it deleted being some distance away it must have been the closest) and couldn't figure out how to add it back, so all the clicking I had done so far was wasted. It seems to be a LOT of clicking as there are lots of tiny line segments making up a single stretch of road.

  2. There doesn't seem to be any way of specifying that you can take a segment if needed, but don't have to. In this case I had two disconnected networks of roads I wanted to reach in one trip, and it would be nice if it could find them minimal distance way to get between the two. But as soon as I the two networks became disconnected, the one not attached to my starting point was automatically erased.

michael-ts avatar Dec 30 '22 20:12 michael-ts

@MgTheProgrammer Also, it seems to be brute-forcing the solution. I thought there was a polynomial time solution; this isn't the travelling salesman problem...

michael-ts avatar Dec 30 '22 20:12 michael-ts

@MgTheProgrammer It's also not clear to me how to find the actual route... I downloaded the GPX file (was the only option offered). I tried to download it into Google Maps but it claimed (wrongly) that the GPX file was bad. I found a GPX to KML convertor but when I look at the route on the map it shows all the streets reached but I can't tell in what order to visit them. I found an online GPX editor and I can tell the route but only by dragging a slider and this would be a pain to try to manually transcribe into something like turn by turn directions. What method do you use?

michael-ts avatar Dec 31 '22 01:12 michael-ts

This is how I make the plugin work on a newly installed Fedora Workstation that have been updated, and have its graphic driver and related software installed.

After opening , I install the plugin. f1

f2

f3

f4

f5

f6

f7

I somehow see the information about error. f8

f9

I do something to pip. pip install --upgrade pip f10

f11

pip install --force-reinstall networkx==2.3 f12

f13

This is how i run QGIS Desktop. f14

f15

f16

f17

I see there is still error and click there to see more information. f18

f19

I can see above the line mentioning the Python version an error. Above that error I see it says it is in a file called dag.py in line 23. f20

I click on the option to show hidden files and then go to that file so I can edit it. f21

f22

f23

f24

f25

f26

f27

I click on the thing to shows the line number.. f28

f29

f30

I change the writing in line 23 to from math import gcd f31

I click on the thing that looks the letter x on the top right. f32

f33

I run QGIS Desktop again and see that error is gone. f34

f35

I click that so the hidden file is not shown anymore. f36

f37

u7390 avatar Mar 05 '23 03:03 u7390

Following modification to postman.py also fixes the error. nx.connected_component_subgraphs() is deprecated, thus causing the error.

def graph_components(graph):
    # The graph may contain multiple components, but we can only handle one connected component. If the graph contains
    # more than one connected component, we only use the largest one.
    components = list((graph.subgraph(c) for c in nx.connected_components(graph))) #list(nx.connected_component_subgraphs(graph)) <-- this is deprecated
    components.sort(key=lambda c: c.size(), reverse=True)

    return components

jarvena avatar Mar 21 '23 06:03 jarvena