Windows下安裝Python + igraph (用於複雜網絡分析)

最近在研究一個關於複雜網絡的課題,由於所下載的文件都是gml格式,針對gml文件格式進行作出有點困難,在網上查資料發現igraph可以很好地進行處理。這裏就把資料轉載過來供大家參考。

來自兩篇博客,原文網址如下:

http://www.cnblogs.com/luweiseu/archive/2011/09/25/2190210.html

http://blog.sciencenet.cn/blog-830776-651722.html


庫功能:
Python interface to the igraph high performance graph library, primarily aimed at complex network research and analysis.

1.安裝
下載地址

>>> import igraph.test
>>> igraph.test.test()


 

測試安裝成功

2.Graph plotting in igraph on Windows

 

http://hal.elte.hu/~nepusz/development/igraph/tutorial/install.html
先安裝pycairo

 

然後還有幾個相關的dll,

http://alex.matan.ca/2009/11/21/cairo-integration-with-wxpython-installation-of-python-wxpython-pycairo-and-cairo-2-d-graphics-library-on-windows-xp/

分別下載:libcairo-2.dll、libpng13.dll、zlib1.dll;然後複製到環境變量path中包含的路徑中
3. launch Python again and check if it worked:

>>> from igraph import *
>>> g = Graph.Famous("petersen")
>>> plot(g)



直接讀取GML文件代碼:

>>> g = Graph.Read_GML("karate.gml")
>>> plot(g)

關於庫文件的安裝可以參考以下內容:


一、iGraph的安裝

      igraph安裝比較簡單,直接在http://igraph.sourceforge.net/download.html下載python安裝包即可。

 後來我開始畫圖,發現報錯,報錯如下:

  raise TypeError("plotting not available") 

  TypeError: plotting not available

在網絡上搜索,找到如下解決方法,主要原因是igraph依賴的一個包沒有裝。安裝方法如下

原文地址 http://chuanwang66.iteye.com/blog/1704942

Graph plotting in igraph on Windows:cairo和dll被安裝到C:\Python25\Lib\site-packages\cairo中

Graph plotting in igraph is implemented using a third-party package called Cairo . If you want to create publication-quality plots in igraph on Windows, you must also install Cairo and its Python bindings. The Cairo project does not provide pre-compiled binaries for Windows, but other projects depending on Cairo do, so the preferred way to install Cairo on Windows along with its Python bindings is as follows:

  1. Get the latest PyCairo for Windows installer fromhttp://ftp.gnome.org/pub/gnome/binaries/win32/pycairo/1.8 . Make sure you grab the one that matches your Python version. At the time of writing, the above folder contained installers for Python 2.6 only. You may also try and go one level up, then down then 1.4 subfolder – these are older versions, but they work with Python 2.5 and Python 2.6 as well.
  2. Install PyCairo using the installer .(我用的是pycairo-1.4.12-2.win32-py2.5.exe) The installer extracts the necessary files into Lib\site-packages\cairo within the folder where Python is installed. Unfortunately there are some extra DLLs which are required to make Cairo work, so we have to get these as well.
  3. Head to http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/ and get the binary versions of Cairo (cairo_1.8.10-3_win32.zip at the time of writing), Fontconfig (fontconfig_2.8.0-2_win32.zip ), Freetype (freetype_2.4.4-1_win32.zip ), Expat (expat_2.0.1-1_win32.zip ), libpng (libpng_1.4.3-1_win32.zip ) and zlib (zlib_1.2.5-2_win32.zip ). Version numbers may vary, so be adaptive! Each ZIP file will contain a bin subfolder with a DLL file in it. Put the following DLLs in Lib\site-packages\cairowithin your Python installation:
    • freetype6.dll (from freetype_2.4.4-1_win32.zip )
    • libcairo-2.dll (from cairo_1.8.10-3_win32.zip )
    • libexpat-1.dll (from expat_2.0.1-1_win32.zip )
    • libfontconfig-1.dll (from fontconfig_2.8.0-2_win32.zip )
    • libpng14-14.dll (from libpng_1.4.3-1_win32.zip )
    • zlib1.dll (from zlib_1.2.5-2_win32.zip ).

Having done that, you can launch Python again and check if it worked:

>>> from igraph import *  
>>> g = Graph.Famous("petersen")  
>>> plot(g) 

注意:在運行時,我遇到了如下錯誤,最後發現時沒有將cairo的dll加入環境變量path

 

下面是解決這個問題時參考的文檔:

文檔代碼  收藏代碼
    Hi, I have just installed igraph and I am having a problem with plotting.   
      
    I have followed the instructions on the documentation site   
    http://www.cs.rhul.ac.uk/home/tamas/development/igraph/tutorial/install.html#graph-plotting-in-igraph-on-windows  
      
    which includes installing pycairo, and putting the required dll files into   
    Python2.6\Lib\site-packages\cairo  
      
    igraph itself has installed properly, as running   
    >>> import igraph.test  
    >>> igraph.test.test()  
    gives no errors, and all tests work.   
      
    However, when I try to run   
    from igraph import *  
    g = Graph.Famous("petersen")  
    summary(g)  
    plot(g)  
      
    I get the following error:  
      
    Traceback (most recent call last):  
      File "C:\Documents and Settings\fulford\Desktop\netstuff\test1.py", line 4,   
    in <module>  
        plot(g)  
      File "C:\Python26\lib\site-packages\igraph\drawing.py", line 762, in plot  
        result = Plot(target, bbox)  
      File "C:\Python26\lib\site-packages\igraph\drawing.py", line 222, in __init__  
        self._surface_was_created=not isinstance(target, cairo.Surface)  
      File "C:\Python26\lib\site-packages\igraph\drawing.py", line 51, in   
    __getattr__  
        raise TypeError, "plotting not available"  
    TypeError: plotting not available  
      
    I also tried the following:  
    import cairo  
      
    but got the error:   
      
    Traceback (most recent call last):  
      File "<pyshell#2>", line 1, in <module>  
        import cairo  
      File "C:\Python26\lib\site-packages\cairo\__init__.py", line 1, in <module>  
        from _cairo import *  
    ImportError: DLL load failed: The specified module could not be found.  
      
    I am fairly new to python, so am wondering if there is something else I needed   
    to do, like add something to my path variable so that the dll files are found?   
      
    Any help or suggestions anyone can give me would be much appreciated, sicve I   
    would very much like to use this software. .   
    Thanks Glenn.  
      
    PS platform details:   
      
    Platform: Windows XP  
    Python version 2.6 (32 bitr) (enthought python distribution 6.2)  
      
      
    Dr Glenn Fulford  +61 7 313 85196 QUT Mathematical Sciences,   
    GPO Box 2343 Brisbane, Qld AUSTRALIA 4001.     Cricos no. 00213J  
    email: address@hidden;   
    -------------------------------------------------------------------------------------  
    Dear Glenn,  
      
    It seems like the Python bindings of Cairo are not installed properly on  
    your machine -- igraph simply reports this when it says "plotting not  
    available".  
      
    I checked a working installation on Windows XP (32-bit) and the  
    following files should be present in  
    c:\python26\lib\site-packages\cairo:  
      
    __init__.py  
    _cairo.pyd  
    libcairo-2.dll  
    libexpat-1.dll  
    libfontconfig-1.dll  
    libpng14-14.dll  
    zlib1.dll  
      
    The error message you see means that Python finds __init__.py in the  
    cairo subfolder successfully, and it tries to import _cairo.pyd from  
    there. (.pyd files are like ordinary .dll files, Python just uses a  
    different extension to distinguish them from .dlls). Unfortunately the  
    import can fail for at least two reasons:  
      
    1) _cairo.pyd itself is missing  
    2) _cairo.pyd is there, but one of its dependencies is missing.  
      
    Unfortunately, the error message is the same in both cases. So, I'd do  
    the following:  
      
    1. Check whether all the files I mentioned above are in the  
    site-packages\cairo folder. If not, install them.  
      
    2. If they are all there, download Dependency Walker from  
    www.dependencywalker.com and open _cairo.pyd with it. Dependency Walker  
    should recursively traverse the dependency tree of _cairo.pyd and report  
    any other DLLs that you are missing.  
      
    --   
    Tamas  
    -------------------------------------------------------------------------------------  
    Dear Tamas,   
    thanks very much for your suggestions. I beleive I eventiually tracked down the   
    problem.   
      
    I found the following web page very useful.   
    http://alex.matan.ca/install-cairo-wxpyton-pycairo-python-windows  
      
    First thing it told me was to append c:\Python26\Lib\site-packages\cairo to my   
    path, which I hadn't done, but this didn't fix the problem.   
      
    I then reinstalled the dll files, this time from  the wxpythion site. Thiat   
    seemed to fix the problem. So not surre exactly which dll it was, but I can   
    compare themn if anyone is interested.   
      
    Glenn  

二、iGraph使用

 

     參考這裏 http://www.cs.rhul.ac.uk/home/tamas/development/igraph/tutorial/tutorial.html




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章