I am working with networkx library for graph optimization problems. However, when I try running the example on their documentation it says in my PyCharm IDE after executing the example:
Traceback (most recent call last):
File "/home/PycharmProjects/testing_things.py", line 1, in <module>
import community
ImportError: No module named community
Does anyone knows how to get rid of this error? I am using Python 2.7
It would seem that your python installation doesn't have community installed.
You can install it by running:
pip install python-louvain
Cheers!
you can use :
conda install python-louvain
Use pip to install Python-Louvain:
pip install python_louvain
then in your script import the module directly using:
from community import community_louvain
In your code, use the function in the following way:
partition = community_louvain.best_partition(G)
Here's the sample community detection on the famous karate club graph based on Louvain Community Detection Algorithm:
# Replace this with your networkx graph loading depending on your format!
r = nx.karate_club_graph()
#first compute the best partition
partition = community.best_partition(r)
#drawing
size = float(len(set(partition.values())))
pos = nx.spring_layout(r)
count = 0
for com in set(partition.values()) :
count = count + 1.
list_nodes = [nodes for nodes in partition.keys()
if partition[nodes] == com]
nx.draw_networkx_nodes(r, pos, list_nodes, node_size = 20,
node_color = str(count / size))
nx.draw_networkx_edges(r, pos, alpha=0.5)
plt.show()
Related
I am working through the Qiskit tutorial textbook, and in Section 1.4 ('Single Qubit Gates'), I can't seem to plot vectors on the Bloch Sphere.
I am using Google Colab and am importing as:
!pip install qiskit
!pip install qiskit[visualization]
from qiskit import QuantumCircuit, assemble, Aer
from math import pi, sqrt
from qiskit.visualization import plot_bloch_multivector, plot_histogram
sim = Aer.get_backend('aer_simulator')
and then the following code is taken directly from the textbook:
qc = QuantumCircuit(1)
qc.x(0)
qc.save_statevector()
qobj = assemble(qc)
state = sim.run(qobj).result().get_statevector()
plot_bloch_multivector(state)
Yet doing this gives the error: " 'Arrow3D' object has no attribute '_path2d' ". Any help would be greatly appreciated.
Edit: Adding a line plt.show() no longer brings up an error message, but still no image shows.
i had this same issue and upgrading matplotlib (to 3.5.1) fixed it for me
I'm having the same problem. One workaround that I found is to use the Kaleidoscope package link. It's a visualization package developed by someone working at IBM. I actually like this as it uses Plotly for the figures.
import kaleidoscope.qiskit
from kaleidoscope import bloch_sphere
Then you can just type
bloch_sphere(state)
I am trying to use open3d to create an "alphahull" around a set of 3d points using TriangleMesh. However I get a TypeError.
import open3d as o3d
import numpy as np
xx =np.asarray([[10,21,18], [31,20,25], [36,20,24], [33,19,24], [22,25,13], [25,19,24], [22,26,10],[29,19,24]])
cloud = o3d.geometry.PointCloud()
cloud.points = o3d.utility.Vector3dVector(xx)
mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_alpha_shape(pcd=cloud, alpha=10.0)
output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: create_from_point_cloud_alpha_shape(): incompatible function arguments. The following argument types are supported:
1. (pcd: open3d.open3d.geometry.PointCloud, alpha: float, tetra_mesh: open3d::geometry::TetraMesh, pt_map: List[int]) -> open3d.open3d.geometry.TriangleMesh
The error says the object I am passing the function is the wrong type. But when I check the type I get this:
>>print(type(cloud))
<class 'open3d.open3d.geometry.PointCloud'>
Please can someone help me with this error?
Note: A comment on this post Python open3D no attribute 'create_coordinate_frame' suggested it might be a problem with the installation and that a solution was to compile the library from source. So I compiled the library from source. After this ran
make install-pip-package. Though I am not sure it completed correctly because I couldn't import open3d in python yet; see output of installation: https://pastebin.com/sS2TZfTL
(I wasn't sure if that command was supposed to complete the installation, or if you were still required to run pip? After I ran python3 -m pip install Open3d I could import the library in python.)
Bug in bindings of current release (v0.9.0):
https://github.com/intel-isl/Open3D/issues/1621
Fix for master:
https://github.com/intel-isl/Open3D/pull/1624
Workaround:
tetra_mesh, pt_map = o3d.geometry.TetraMesh.create_from_point_cloud(pcd)
mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_alpha_shape(pcd, alpha, tetra_mesh, pt_map)
I'm following a tutorial, and I'm using the last python2 (homebrew) with PyCharm (with project interpreter configured) - But I'm stuck in this part:
from py2neo import Graph, Node
graph = Graph()
nicole = Node("Person", name="Nicole")
graph.create(nicole)
graph.delete(nicole)
nicole = graph.merge_one("Person", "name", "Nicole")
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Graph' object has no attribute 'merge_one'
I already checked the documentation and it seems that I'm doing everything ok . I tried to uninstall and install last version of py2neo but with no success. How I solve this problem?
Expected behavior: Running that command from the python2 console: If that Person exists don't duplicate it but change its values, if don't exist create it.
I fastly ended up using the version 4 as opposed to 2. So using Graph.merge, solved the problem:
jonh = Node("Person", name="Jonh", age = 21)
graph.create(jonh)
ana = Node("Person", name="Ana", age = 44)
graph.create(ana)
michael = Node("Person", name="Ana", age = 33)
graph.merge(michael, "Person", "name") # So the age of Ana will change to 33, as expected.
For using the commands related to my question, the version 2 must be installed, for eg. directly from py2neo repo:
pip install https://github.com/technige/py2neo/archive/release/2.0.7.zip
From inspecting the source code, I think the function you're looking for is Graph.match_one. There is also a function Graph.merge, but that doesn't take Node as an argument.
I am starting to use this interface now, I have some experience with Python but nothing extensive. I am calculating the transitivity and community structure of a small graph:
import networkx as nx
G = nx.read_edgelist(data, delimiter='-', nodetype=str)
nx.transitivity(G)
#find modularity
part = best_partition(G)
modularity(part, G)
I get the transitivity just fine, however - there is the following error with calculating modularity.
NameError: name 'best_partition' is not defined
I just followed the documentation provided by the networkx site, is there something I am doing wrong?
As far as I can tell best_partition isn't part of networkx. It looks like you want to use https://sites.google.com/site/findcommunities/ which you can install from https://bitbucket.org/taynaud/python-louvain/src
Once you've installed community try this code:
import networkx as nx
import community
import matplotlib.pyplot as plt
G = nx.random_graphs.powerlaw_cluster_graph(300, 1, .4)
nx.transitivity(G)
#find modularity
part = community.best_partition(G)
mod = community.modularity(part,G)
#plot, color nodes using community structure
values = [part.get(node) for node in G.nodes()]
nx.draw_spring(G, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=False)
plt.show()
edit: How I installed the community detection library
ryan#palms ~/D/taynaud-python-louvain-147f09737714> pwd
/home/ryan/Downloads/taynaud-python-louvain-147f09737714
ryan#palms ~/D/taynaud-python-louvain-147f09737714> sudo python3 setup.py install
I just met the same error NameError: name 'best_partition' is not defined when using this example code.
This error occurs because I named my python file as networkx.py, then when we execute this program
import networkx as nx
This program may import the networkx we defined instead of the library. In the program, best_partition is not defined. So this error occur.
Having same name with library is not appropriate. Maybe you should check this!
I have installed nodebox 2 for windows on my machine and have verified that all the examples are running as it is.
Now i want to use graph library Graph for the same.
I went and copied it as it is in my site-packages folder and then ran the examples it had given along it in IDLE.
I recieved an error of ximport . So then i added in the code as from nodebox.graphics import *
Now i get the following error
Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\graph\graph_example2.py", line 39, in <module>
g.draw(highlight=path, weighted=True, directed=True)
File "C:\Python26\lib\site-packages\graph\__init__.py", line 453, in draw
self.update()
File "C:\Python26\lib\site-packages\graph\__init__.py", line 416, in update
self.x = _ctx.WIDTH - max.x*self.d - min_.x*self.d
AttributeError: 'NoneType' object has no attribute 'WIDTH'
Is there any way I can run this library from outside of nodebox in windows ?
thanks...
I am pasting the code for which i get the error below...
from nodebox.graphics import *
try:
graph = ximport("graph")
except ImportError:
graph = ximport("__init__")
reload(graph)
size(600, 600)
# A graph object.
g = graph.create(iterations=500, distance=1.0)
# Add nodes with a random id,
# connected to other random nodes.
for i in range(50):
node1 = g.add_node(random(500))
if random() > 0.5:
for i in range(choice((2, 3))):
node2 = choice(g.nodes)
g.add_edge(node1.id, node2.id, weight=random())
# We leave out any orphaned nodes.
g.prune()
# Colorize nodes.
# Nodes with higher importance are blue.
g.styles.apply()
# Update the graph layout until it's done.
g.solve()
# Show the shortest path between two random nodes.
path = []
id1 = choice(g.keys())
id2 = choice(g.keys())
path = g.shortest_path(id1, id2)
# Draw the graph and display the shortest path.
g.draw(highlight=path, weighted=True, directed=True)
The Nodebox Graph docs mention that it supports Nodebox 1.9.5.6, which is a Nodebox 1 (Mac only) version number. To my knowledge the Graph library has not yet been ported to Nodebox 2, so can only currently run on the Mac.
One option is a project called Nodebox OpenGL which implements the Nodebox API and includes its own graph library, with an example of using it under examples\08-physics\07-graph. The Nodebox 1 Graph library is not yet compatible, but it includes its own graph class nodebox.graphics.physics.Graph.
To use it, you'll need to download:
Nodebox for OpenGL
pyglet cross platform multimedia library
Extract these and install, or just place the nodebox and pyglet packages somewhere on your Python path (site-packages). When you run 07-graph.py you should see this: