ぴろの狂人日記

2014年から頑張ってブログを継続しようと思います。「継続と習慣」を今年の目標にしたので、頑張って更新を続けようと思います。おいおいはレビューや数学や認知科学などについて記事を書いていければと思っています

Macでpython3の環境構築13---OpenCV3のインストール1---

画像処理とかもやってみたいと思いOpenCVをインストールしようと思っておりました。

しかし、なかなかうまくいかず格闘。その結果なんとかインストールできたので、どうやってうまくできたのか、自分用のメモや勉強の意味も含め書いておきたいと思います。
結論から言えば、うまくいかなかったのはPATHが通っていなかったからなんですけどね。

まず、次のようにします。

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ brew install opencv3 --with-python3&nbsp
#「brew install opencv3 --with-python3」と打ってreturnを押す。


そうすると次のようになります。

==> Installing opencv3 from homebrew/homebrew-science
Error: You must `brew link openexr numpy' before homebrew/science/opencv3 can be installed


Errorがでてきます。
直訳とはなりますが、「homebrew/science/opencv3がインストールされる前に、"brew link openxr numpy"をしなければなりません。」という意味。

まあでは、「実際どうすればいいの?」というのはまた別の話ですが。とりあえず先へ進みます。

その次はこうします。正確な意味などはまだわかりませんが、(特に--with-python3の部分)とりあえず保留にし先へ進みます。

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ brew install numpy --with-python3    # 「brew install numpy --with-python3」と打ってreturnを押す。
 


そうするとこうなります。

Warning: homebrew/python/numpy-1.9.3 already installed, it's just not linked


今度はWarning。「homebrew/python/numpy-1.9.3は既にインストールされているけども、ただしリンクはされていません。」とのこと。

ではリンクさせましょう。ということで、先ほどのErrorにも表示されていましたが、どうやらlinkをするためにはbrew link openxr numpyをすればいいらしいので、素直にErrorの指示に従ってみます。

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ brew link openexr numpy                                                                                                                        # 「brew link openxr numpy」と打ってreturnを押す。


そうすると、こうなります。

Linking /usr/local/Cellar/openexr/2.2.0...
Error: Could not symlink share/aclocal/openexr.m4
/usr/local/share/aclocal is not writable.


ここでもまたError。symlinkできません。「/usr/local/share/aclocal には書き込みできません。」とのこと。
これは、現在のコマンドの実行者では、/usr/local/share/aclocalに書き込みできません、/usr/local/share/aclocalの権限を変更する必要があるようです。

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ sudo chown -R hiroshi   /usr/local/share/aclocal
#  「sudo chown -R hiroshi」と打ってreturnを押す。
#今自分の環境では「hiroshi」というユーザーでログインしてます。-Rのあとは、今ログインしているユーザーの名前を入れる。
# sudo chown -R hiroshi  というのは、その右に入力したファイルの権限をhiroshiに変更するという意味。
 


sudoをつけて実行しているので、パスワードを求められます。ここには自分のパソコンのパスワードを入れます。
今までみたいに、ずらずらと何か処理が始まるわけではありません。
また次のコマンドの入力待ちになるので、続けてこうします。

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ brew link openexr
# 「brew link openxr」と打ってreturnを押す。
#   とりあえずopenxrに対してだけbrew linkを行っておきます。


そうすると、こうなる。

Linking /usr/local/Cellar/openexr/2.2.0... 103 symlinks created

ということでこれは問題なく処理できました。

その上でさらに、

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ brew link openexr numpy

とします。
そうすると、

Warning: Already linked: /usr/local/Cellar/openexr/2.2.0        #この行は、openxrはすでにリンクされていますよ、という意味。上記の処理で、もう既にうまくいっているからね。
To relink: brew unlink openexr && brew link openexr     
Linking /usr/local/Cellar/numpy/1.9.3...        # numpyに対する処理。結局エラー。
Error: Could not symlink bin/f2py
Target /usr/local/bin/f2py
already exists. You may want to remove it:
  rm '/usr/local/bin/f2py'

To force the link and overwrite all conflicting files:
  brew link --overwrite numpy

To list all files that would be deleted:
  brew link --overwrite --dry-run numpy

となります。
openxrとnumpyそれぞれに対して処理が走ります。
openxrの方は、「もう既にリンクされています」と。確かに先ほど処理が無事できておりましたので、そうなりますね。
numpyの方はError

というかここまで、コマンドの打ち方が一貫していないですが、ぶっちゃけ、処理していた当時も現在も、完全に意味がわかっているわけではないので、ご勘弁ください。

でこのあとも、よく意味はわかってないので、詳しいコメントはしませんが、そのあと行った処理も記載しておきたいと思います。

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ rm '/usr/local/bin/f2py'       # rm はその右のファイルを削除する。
hiroshi-no-MacBook-Air:Pro-tr hiroshi$ brew link --overwrite numpy
Linking /usr/local/Cellar/numpy/1.9.3... 392 symlinks created
hiroshi-no-MacBook-Air:Pro-tr hiroshi$ brew link --overwrite --dry-run numpy
Warning: Already linked: /usr/local/Cellar/numpy/1.9.3
To relink: brew unlink numpy && brew link numpy
hiroshi-no-MacBook-Air:Pro-tr hiroshi$ brew link numpy
Warning: Already linked: /usr/local/Cellar/numpy/1.9.3
To relink: brew unlink numpy && brew link numpy


途中の部分の処理が一部理解できていない部分もありますが、
「Linking /usr/local/Cellar/numpy/1.9.3... 392 symlinks created」とあったり、「Warning: Already linked: /usr/local/Cellar/numpy/1.9.3」と出ているので、どうやらnumpyのリンクもできたようです。

これで、当初冒頭ででていた「brew install opencv3 --with-python3」の実行に対するErrorError: You must `brew link openexr numpy' before homebrew/science/opencv3 can be installed」に対する対策が一応できたはずなので、改めて「brew install opencv3 --with-python3」を実行してみます。

hiroshi-no-MacBook-Air:Pro-tr hiroshi$ brew install opencv3 --with-python3


そうすると今度は、こういう処理が始まります。

==> Installing opencv3 from homebrew/homebrew-science
==> Installing dependencies for homebrew/science/opencv3: cmake, pkg-config, homebrew/python/numpy
==> Installing homebrew/science/opencv3 dependency: cmake
==> Downloading https://homebrew.bintray.com/bottles/cmake-3.3.1.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring cmake-3.3.1.yosemite.bottle.1.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
/usr/local/share/emacs/site-lisp/

Add the following to your init file to have packages installed by Homebrew added to your load-path:
(let ((default-directory "/usr/local/share/emacs/site-lisp/"))
  (normal-top-level-add-subdirs-to-load-path))
==> Summary
🍺  /usr/local/Cellar/cmake/3.3.1: 1901 files, 33M
==> Installing homebrew/science/opencv3 dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.28.yosemite.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.28.yosemite.bottle.2.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.28: 10 files, 612K
==> Installing homebrew/science/opencv3 dependency: homebrew/python/numpy
==> Using Homebrew-provided fortran compiler.
This may be changed by setting the FC environment variable.
==> Downloading https://pypi.python.org/packages/source/n/numpy/numpy-1.9.3.tar.gz
######################################################################## 100.0%
==> Downloading https://pypi.python.org/packages/source/n/nose/nose-1.3.4.tar.gz
######################################################################## 100.0%
==> python -c import setuptools... --no-user-cfg install --prefix=/usr/local/Cellar/numpy/1.9.3/libexec/nose --sin
==> python setup.py build --fcompiler=gnu95 install --prefix=/usr/local/Cellar/numpy/1.9.3
==> Downloading https://pypi.python.org/packages/source/n/nose/nose-1.3.4.tar.gz
Already downloaded: /Library/Caches/Homebrew/numpy--nose-1.3.4.tar.gz
==> python3 -c import setuptools... --no-user-cfg install --prefix=/usr/local/Cellar/numpy/1.9.3/libexec/nose --si
==> python3 setup.py build --fcompiler=gnu95 install --prefix=/usr/local/Cellar/numpy/1.9.3
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink lib/python3.4/site-packages/numpy/__config__.py
Target /usr/local/lib/python3.4/site-packages/numpy/__config__.py
already exists. You may want to remove it:
  rm '/usr/local/lib/python3.4/site-packages/numpy/__config__.py'

To force the link and overwrite all conflicting files:
  brew link --overwrite numpy

To list all files that would be deleted:
  brew link --overwrite --dry-run numpy

Possible conflicting files are:
/usr/local/lib/python3.4/site-packages/numpy/__config__.py
/usr/local/lib/python3.4/site-packages/numpy/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/_import_tools.py
/usr/local/lib/python3.4/site-packages/numpy/add_newdocs.py
/usr/local/lib/python3.4/site-packages/numpy/compat/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/compat/_inspect.py
/usr/local/lib/python3.4/site-packages/numpy/compat/py3k.py
/usr/local/lib/python3.4/site-packages/numpy/compat/setup.py
/usr/local/lib/python3.4/site-packages/numpy/core/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/core/_dotblas.so
/usr/local/lib/python3.4/site-packages/numpy/core/_dummy.so
/usr/local/lib/python3.4/site-packages/numpy/core/_internal.py
/usr/local/lib/python3.4/site-packages/numpy/core/_methods.py
/usr/local/lib/python3.4/site-packages/numpy/core/arrayprint.py
/usr/local/lib/python3.4/site-packages/numpy/core/cversions.py
/usr/local/lib/python3.4/site-packages/numpy/core/defchararray.py
/usr/local/lib/python3.4/site-packages/numpy/core/fromnumeric.py
/usr/local/lib/python3.4/site-packages/numpy/core/function_base.py
/usr/local/lib/python3.4/site-packages/numpy/core/generate_numpy_api.py
/usr/local/lib/python3.4/site-packages/numpy/core/getlimits.py
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/__multiarray_api.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/__ufunc_api.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/_neighborhood_iterator_imp.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/_numpyconfig.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/arrayobject.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/arrayscalars.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/halffloat.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/multiarray_api.txt
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/ndarrayobject.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/ndarraytypes.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/noprefix.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/npy_3kcompat.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/npy_common.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/npy_cpu.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/npy_endian.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/npy_interrupt.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/npy_math.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/npy_no_deprecated_api.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/npy_os.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/numpyconfig.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/old_defines.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/oldnumeric.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/ufunc_api.txt
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/ufuncobject.h
/usr/local/lib/python3.4/site-packages/numpy/core/include/numpy/utils.h
/usr/local/lib/python3.4/site-packages/numpy/core/info.py
/usr/local/lib/python3.4/site-packages/numpy/core/lib/libnpymath.a
/usr/local/lib/python3.4/site-packages/numpy/core/lib/npy-pkg-config/mlib.ini
/usr/local/lib/python3.4/site-packages/numpy/core/lib/npy-pkg-config/npymath.ini
/usr/local/lib/python3.4/site-packages/numpy/core/machar.py
/usr/local/lib/python3.4/site-packages/numpy/core/memmap.py
/usr/local/lib/python3.4/site-packages/numpy/core/multiarray.so
/usr/local/lib/python3.4/site-packages/numpy/core/multiarray_tests.so
/usr/local/lib/python3.4/site-packages/numpy/core/numeric.py
/usr/local/lib/python3.4/site-packages/numpy/core/numerictypes.py
/usr/local/lib/python3.4/site-packages/numpy/core/operand_flag_tests.so
/usr/local/lib/python3.4/site-packages/numpy/core/records.py
/usr/local/lib/python3.4/site-packages/numpy/core/scalarmath.so
/usr/local/lib/python3.4/site-packages/numpy/core/setup.py
/usr/local/lib/python3.4/site-packages/numpy/core/setup_common.py
/usr/local/lib/python3.4/site-packages/numpy/core/shape_base.py
/usr/local/lib/python3.4/site-packages/numpy/core/struct_ufunc_test.so
/usr/local/lib/python3.4/site-packages/numpy/core/test_rational.so
/usr/local/lib/python3.4/site-packages/numpy/core/tests/data/astype_copy.pkl
/usr/local/lib/python3.4/site-packages/numpy/core/tests/data/recarray_from_file.fits
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_abc.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_api.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_arrayprint.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_blasdot.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_datetime.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_defchararray.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_deprecations.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_dtype.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_einsum.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_errstate.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_function_base.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_getlimits.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_half.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_indexerrors.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_indexing.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_item_selection.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_machar.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_memmap.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_multiarray.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_multiarray_assignment.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_nditer.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_numeric.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_numerictypes.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_print.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_records.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_regression.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_scalarinherit.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_scalarmath.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_scalarprint.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_shape_base.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_ufunc.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_umath.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_umath_complex.py
/usr/local/lib/python3.4/site-packages/numpy/core/tests/test_unicode.py
/usr/local/lib/python3.4/site-packages/numpy/core/umath.so
/usr/local/lib/python3.4/site-packages/numpy/core/umath_tests.so
/usr/local/lib/python3.4/site-packages/numpy/ctypeslib.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/__config__.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/__version__.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/ccompiler.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/autodist.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/bdist_rpm.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/build.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/build_clib.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/build_ext.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/build_py.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/build_scripts.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/build_src.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/config.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/config_compiler.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/develop.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/egg_info.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/install.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/install_clib.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/install_data.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/install_headers.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/command/sdist.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/compat.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/conv_template.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/core.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/cpuinfo.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/environment.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/exec_command.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/extension.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/absoft.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/compaq.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/g95.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/gnu.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/hpux.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/ibm.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/intel.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/lahey.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/mips.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/nag.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/none.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/pathf95.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/pg.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/sun.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/fcompiler/vast.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/from_template.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/info.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/intelccompiler.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/lib2def.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/line_endings.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/log.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/mingw/gfortran_vs2003_hack.c
/usr/local/lib/python3.4/site-packages/numpy/distutils/mingw32ccompiler.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/misc_util.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/npy_pkg_config.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/numpy_distribution.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/pathccompiler.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/setup.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/system_info.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/f2py_ext/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/f2py_ext/setup.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/f2py_ext/src/fib1.f
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/f2py_ext/src/fib2.pyf
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/f2py_ext/tests/test_fib2.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/f2py_f90_ext/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/f2py_f90_ext/include/body.f90
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/f2py_f90_ext/setup.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/f2py_f90_ext/src/foo_free.f90
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/f2py_f90_ext/tests/test_foo.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/gen_ext/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/gen_ext/setup.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/gen_ext/tests/test_fib3.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/pyrex_ext/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/pyrex_ext/primes.pyx
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/pyrex_ext/setup.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/pyrex_ext/tests/test_primes.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/setup.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/swig_ext/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/swig_ext/setup.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/swig_ext/src/example.c
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/swig_ext/src/example.i
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/swig_ext/src/zoo.cc
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/swig_ext/src/zoo.h
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/swig_ext/src/zoo.i
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/swig_ext/tests/test_example.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/swig_ext/tests/test_example2.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/test_exec_command.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/test_fcompiler_gnu.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/test_fcompiler_intel.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/test_misc_util.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/tests/test_npy_pkg_config.py
/usr/local/lib/python3.4/site-packages/numpy/distutils/unixccompiler.py
/usr/local/lib/python3.4/site-packages/numpy/doc/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/doc/basics.py
/usr/local/lib/python3.4/site-packages/numpy/doc/broadcasting.py
/usr/local/lib/python3.4/site-packages/numpy/doc/byteswapping.py
/usr/local/lib/python3.4/site-packages/numpy/doc/constants.py
/usr/local/lib/python3.4/site-packages/numpy/doc/creation.py
/usr/local/lib/python3.4/site-packages/numpy/doc/glossary.py
/usr/local/lib/python3.4/site-packages/numpy/doc/howtofind.py
/usr/local/lib/python3.4/site-packages/numpy/doc/indexing.py
/usr/local/lib/python3.4/site-packages/numpy/doc/internals.py
/usr/local/lib/python3.4/site-packages/numpy/doc/io.py
/usr/local/lib/python3.4/site-packages/numpy/doc/jargon.py
/usr/local/lib/python3.4/site-packages/numpy/doc/methods_vs_functions.py
/usr/local/lib/python3.4/site-packages/numpy/doc/misc.py
/usr/local/lib/python3.4/site-packages/numpy/doc/performance.py
/usr/local/lib/python3.4/site-packages/numpy/doc/structured_arrays.py
/usr/local/lib/python3.4/site-packages/numpy/doc/subclassing.py
/usr/local/lib/python3.4/site-packages/numpy/doc/ufuncs.py
/usr/local/lib/python3.4/site-packages/numpy/dual.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/__version__.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/auxfuncs.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/capi_maps.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/cb_rules.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/cfuncs.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/common_rules.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/crackfortran.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/diagnose.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/f2py2e.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/f2py_testing.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/f90mod_rules.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/func2subr.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/info.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/rules.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/setup.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/src/fortranobject.c
/usr/local/lib/python3.4/site-packages/numpy/f2py/src/fortranobject.h
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/assumed_shape/.f2py_f2cmap
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/assumed_shape/foo_free.f90
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/assumed_shape/foo_mod.f90
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/assumed_shape/foo_use.f90
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/assumed_shape/precision.f90
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/kind/foo.f90
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/mixed/foo.f
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/mixed/foo_fixed.f90
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/mixed/foo_free.f90
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/regression/inout.f90
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/src/size/foo.f90
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_array_from_pyobj.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_assumed_shape.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_callback.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_kind.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_mixed.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_regression.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_return_character.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_return_complex.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_return_integer.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_return_logical.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_return_real.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/test_size.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/tests/util.py
/usr/local/lib/python3.4/site-packages/numpy/f2py/use_rules.py
/usr/local/lib/python3.4/site-packages/numpy/fft/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/fft/fftpack.py
/usr/local/lib/python3.4/site-packages/numpy/fft/fftpack_lite.so
/usr/local/lib/python3.4/site-packages/numpy/fft/helper.py
/usr/local/lib/python3.4/site-packages/numpy/fft/info.py
/usr/local/lib/python3.4/site-packages/numpy/fft/setup.py
/usr/local/lib/python3.4/site-packages/numpy/fft/tests/test_fftpack.py
/usr/local/lib/python3.4/site-packages/numpy/fft/tests/test_helper.py
/usr/local/lib/python3.4/site-packages/numpy/lib/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/lib/_compiled_base.so
/usr/local/lib/python3.4/site-packages/numpy/lib/_datasource.py
/usr/local/lib/python3.4/site-packages/numpy/lib/_iotools.py
/usr/local/lib/python3.4/site-packages/numpy/lib/_version.py
/usr/local/lib/python3.4/site-packages/numpy/lib/arraypad.py
/usr/local/lib/python3.4/site-packages/numpy/lib/arraysetops.py
/usr/local/lib/python3.4/site-packages/numpy/lib/arrayterator.py
/usr/local/lib/python3.4/site-packages/numpy/lib/financial.py
/usr/local/lib/python3.4/site-packages/numpy/lib/format.py
/usr/local/lib/python3.4/site-packages/numpy/lib/function_base.py
/usr/local/lib/python3.4/site-packages/numpy/lib/index_tricks.py
/usr/local/lib/python3.4/site-packages/numpy/lib/info.py
/usr/local/lib/python3.4/site-packages/numpy/lib/nanfunctions.py
/usr/local/lib/python3.4/site-packages/numpy/lib/npyio.py
/usr/local/lib/python3.4/site-packages/numpy/lib/polynomial.py
/usr/local/lib/python3.4/site-packages/numpy/lib/recfunctions.py
/usr/local/lib/python3.4/site-packages/numpy/lib/scimath.py
/usr/local/lib/python3.4/site-packages/numpy/lib/setup.py
/usr/local/lib/python3.4/site-packages/numpy/lib/shape_base.py
/usr/local/lib/python3.4/site-packages/numpy/lib/stride_tricks.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/data/python3.npy
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/data/win64python2.npy
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test__datasource.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test__iotools.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test__version.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_arraypad.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_arraysetops.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_arrayterator.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_financial.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_format.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_function_base.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_index_tricks.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_io.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_nanfunctions.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_polynomial.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_recfunctions.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_regression.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_shape_base.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_stride_tricks.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_twodim_base.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_type_check.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_ufunclike.py
/usr/local/lib/python3.4/site-packages/numpy/lib/tests/test_utils.py
/usr/local/lib/python3.4/site-packages/numpy/lib/twodim_base.py
/usr/local/lib/python3.4/site-packages/numpy/lib/type_check.py
/usr/local/lib/python3.4/site-packages/numpy/lib/ufunclike.py
/usr/local/lib/python3.4/site-packages/numpy/lib/user_array.py
/usr/local/lib/python3.4/site-packages/numpy/lib/utils.py
/usr/local/lib/python3.4/site-packages/numpy/linalg/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/linalg/_umath_linalg.so
/usr/local/lib/python3.4/site-packages/numpy/linalg/info.py
/usr/local/lib/python3.4/site-packages/numpy/linalg/lapack_lite.so
/usr/local/lib/python3.4/site-packages/numpy/linalg/linalg.py
/usr/local/lib/python3.4/site-packages/numpy/linalg/setup.py
/usr/local/lib/python3.4/site-packages/numpy/linalg/tests/test_build.py
/usr/local/lib/python3.4/site-packages/numpy/linalg/tests/test_deprecations.py
/usr/local/lib/python3.4/site-packages/numpy/linalg/tests/test_linalg.py
/usr/local/lib/python3.4/site-packages/numpy/linalg/tests/test_regression.py
/usr/local/lib/python3.4/site-packages/numpy/ma/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/ma/bench.py
/usr/local/lib/python3.4/site-packages/numpy/ma/core.py
/usr/local/lib/python3.4/site-packages/numpy/ma/extras.py
/usr/local/lib/python3.4/site-packages/numpy/ma/mrecords.py
/usr/local/lib/python3.4/site-packages/numpy/ma/setup.py
/usr/local/lib/python3.4/site-packages/numpy/ma/tests/test_core.py
/usr/local/lib/python3.4/site-packages/numpy/ma/tests/test_extras.py
/usr/local/lib/python3.4/site-packages/numpy/ma/tests/test_mrecords.py
/usr/local/lib/python3.4/site-packages/numpy/ma/tests/test_old_ma.py
/usr/local/lib/python3.4/site-packages/numpy/ma/tests/test_regression.py
/usr/local/lib/python3.4/site-packages/numpy/ma/tests/test_subclassing.py
/usr/local/lib/python3.4/site-packages/numpy/ma/testutils.py
/usr/local/lib/python3.4/site-packages/numpy/ma/timer_comparison.py
/usr/local/lib/python3.4/site-packages/numpy/ma/version.py
/usr/local/lib/python3.4/site-packages/numpy/matlib.py
/usr/local/lib/python3.4/site-packages/numpy/matrixlib/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/matrixlib/defmatrix.py
/usr/local/lib/python3.4/site-packages/numpy/matrixlib/setup.py
/usr/local/lib/python3.4/site-packages/numpy/matrixlib/tests/test_defmatrix.py
/usr/local/lib/python3.4/site-packages/numpy/matrixlib/tests/test_multiarray.py
/usr/local/lib/python3.4/site-packages/numpy/matrixlib/tests/test_numeric.py
/usr/local/lib/python3.4/site-packages/numpy/matrixlib/tests/test_regression.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/_polybase.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/chebyshev.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/hermite.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/hermite_e.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/laguerre.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/legendre.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/polynomial.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/polytemplate.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/polyutils.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/setup.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/tests/test_chebyshev.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/tests/test_classes.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/tests/test_hermite.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/tests/test_hermite_e.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/tests/test_laguerre.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/tests/test_legendre.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/tests/test_polynomial.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/tests/test_polyutils.py
/usr/local/lib/python3.4/site-packages/numpy/polynomial/tests/test_printing.py
/usr/local/lib/python3.4/site-packages/numpy/random/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/random/info.py
/usr/local/lib/python3.4/site-packages/numpy/random/mtrand.so
/usr/local/lib/python3.4/site-packages/numpy/random/randomkit.h
/usr/local/lib/python3.4/site-packages/numpy/random/setup.py
/usr/local/lib/python3.4/site-packages/numpy/random/tests/test_random.py
/usr/local/lib/python3.4/site-packages/numpy/random/tests/test_regression.py
/usr/local/lib/python3.4/site-packages/numpy/setup.py
/usr/local/lib/python3.4/site-packages/numpy/testing/__init__.py
/usr/local/lib/python3.4/site-packages/numpy/testing/decorators.py
/usr/local/lib/python3.4/site-packages/numpy/testing/noseclasses.py
/usr/local/lib/python3.4/site-packages/numpy/testing/nosetester.py
/usr/local/lib/python3.4/site-packages/numpy/testing/print_coercion_tables.py
/usr/local/lib/python3.4/site-packages/numpy/testing/setup.py
/usr/local/lib/python3.4/site-packages/numpy/testing/tests/test_decorators.py
/usr/local/lib/python3.4/site-packages/numpy/testing/tests/test_doctesting.py
/usr/local/lib/python3.4/site-packages/numpy/testing/tests/test_utils.py
/usr/local/lib/python3.4/site-packages/numpy/testing/utils.py
/usr/local/lib/python3.4/site-packages/numpy/tests/test_ctypeslib.py
/usr/local/lib/python3.4/site-packages/numpy/tests/test_matlib.py
/usr/local/lib/python3.4/site-packages/numpy/version.py
==> Caveats
Python modules have been installed and Homebrew's site-packages is not
in your Python sys.path, so you will not be able to import the modules
this formula installed. If you plan to develop with these modules,
please run:
  mkdir -p /Users/hiroshi/Library/Python/2.7/lib/python/site-packages
  echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/hiroshi/Library/Python/2.7/lib/python/site-packages/homebrew.pth
==> Summary
🍺  /usr/local/Cellar/numpy/1.9.3: 1379 files, 28M, built in 3.7 minutes
==> Installing homebrew/science/opencv3
==> Using Homebrew-provided fortran compiler.
This may be changed by setting the FC environment variable.
==> Downloading https://github.com/Itseez/opencv/archive/3.0.0.tar.gz
==> Downloading from https://codeload.github.com/Itseez/opencv/tar.gz/3.0.0
######################################################################## 100.0%
==> Downloading https://downloads.sourceforge.net/project/opencvlibrary/3rdparty/ippicv/ippicv_macosx_20141027.tgz
==> Downloading from http://netassist.dl.sourceforge.net/project/opencvlibrary/3rdparty/ippicv/ippicv_macosx_20141
######################################################################## 100.0%
==> cmake .. -DCMAKE_C_FLAGS_RELEASE= -DCMAKE_CXX_FLAGS_RELEASE= -DCMAKE_INSTALL_PREFIX=/usr/local/Cellar/opencv3/
==> make
==> make install
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

opencv3 and opencv install many of the same files.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/opencv3/lib
    CPPFLAGS: -I/usr/local/opt/opencv3/include

If you need Python to find bindings for this keg-only formula, run:
  echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth
  mkdir -p /Users/hiroshi/Library/Python/2.7/lib/python/site-packages
  echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/hiroshi/Library/Python/2.7/lib/python/site-packages/homebrew.pth
==> Summary
🍺  /usr/local/Cellar/opencv3/3.0.0: 247 files, 104M, built in 17.0 minutes


で、一応処理が終わりました。
実際、無事インストールできているかどうか確認するためにはターミナルからpythonを起動してcv2というモジュールがインストールできるかどうか確認すればいいようです。
しかし

hiroshi-no-MacBook-Air:~ hiroshi$ python3
Python 3.4.3 (default, Aug 11 2015, 08:57:25) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named 'cv2'

Errorがでてしまう。

ここから試行錯誤が始まりますが、長くなったので続きは次回で。
(結論から言えば PATHと通すだけで改善したんですけどね。。。)

Macでpython3の環境構築12---NetworkXの使い方3---

NetworkXには、ある種類のグラフを自動的に作る機能も備わっております。

道(path)

path_graph — NetworkX 1.10 documentation


道(path)とは、頂点を順番につないだグラフです。
nx.path_graph(n)でn頂点のpathが生成されます。ラベルは0〜n-1までが自動的につき、0〜n-1を順番につないだグラフになります。

例えば、4頂点のpathは次のようなコードになります。
import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.path_graph(4)        #4頂点のpath
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=15,width=2.0)
    
    pyplot.show()

if __name__ == '__main__':
    main()


これを実行すると

となります。

もし空のグラフを先に作成し、pathを追加するのであればこうなります。


import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
    
    g.add_path([1,2,3,4])          #4頂点のpath
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=15,width=2.0)
    
    pyplot.show()

if __name__ == '__main__':
    main()

 

星(star graph)

 
星とは一つの頂点にその他のノードがつながっているグラフです。
nx.star_graph(n)でn頂点の星が生成できます。ラベルは中心が0になりn-1まで自動的につきます。
 
例えば4頂点の星は次のようなコードになります。
import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.star_graph(4)    # 4頂点の星
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=15,width=2.0)
    
    pyplot.show()

if __name__ == '__main__':
    main()

 

これを実行すると、

となります。

もし先に空のグラフを作成し、星を追加するのであればこうなります。

import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
    
    g.add_star([1,2,3,4])   # 4頂点の星。この場合は引数は頂点のリスト(=自分で指定できる)
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=15,width=2.0)
    
    pyplot.show()

if __name__ == '__main__':
    main()


閉路グラフ(cycle graph)

 
閉路グラフとは、文字通り一つの閉路から成るグラフです。(閉路グラフ - Wikipedia)
nx.cycle_graph(n)でn頂点の閉グラフが生成できます。ラベルは0〜n-1でつきます。
 
例えば4頂点の閉路グラフは次のようなコードになります。
import networkx as nx

from matplotlib import pyplot

def main() :
    
    g =nx.cycle_graph(4)        #  4頂点の閉グラフ
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=15,width=2.0)
    
    pyplot.show()

if __name__ == '__main__':
    main()

 

これを実行すると、
となります。

もし先に空のグラフを作成し、閉グラフを追加するのであればこうなります。
import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
    
    g.add_cycle([1,2,3,4])      #  4頂点の閉グラフ。この場合は、引数は頂点のリスト(=自分で指定できる)
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=15,width=2.0)
    
    pyplot.show()

if __name__ == '__main__':
    main()


完全グラフ(complete graph)

complete_graph — NetworkX 1.10 documentation


完全グラフとは、任意の二つのノードの間にエッジがあるグラフです。
nx.complete_graph(n)でn頂点の完全グラフが生成できます。ラベルは0〜n-1でつきます。

例えば4頂点の完全グラフは次のようなコードになります。
import networkx as nx

from matplotlib import pyplot

def main() :
    g =nx.complete_graph(4)      # 4頂点の完全グラフ
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=15,width=2.0)
    
    pyplot.show()

if __name__ == '__main__':
    main()


これを実行すると、

となります。

n=5の場合は


n=6の場合は


となっていきます。

g.add_complete([リスト])という方法はできないようです。

それ以外にもあるクラスのグラフを自動的に作れるような機能がいろいろ備わっているようですが、勉強不足でそもそもなんのことをどんなグラフのことを意味しているのかわからないものがたくさん。
ランダムグラフとかいろいろあるっぽいです。

詳細は下の参考ページで確認ください。

参考にしたページや文献・書籍

Graph generators — NetworkX 1.10 documentation

MacでC言語の環境を構築2

しばらくずっとpythonの話題が続いておりましたが、今回はC/C++の話題で。

ずいぶん前に、

piroshhh.hatenablog.com

で、コンパイラなどをインストールしたはいいものの、実際にコードが動くかどうかは確かめておりませんでした。

実際にコンパイルする作業をしてみます。

C言語の実行

例によって、テストにつかうのは次のコードです。
 
#include<stdio.h>
int main(){
printf("hello world\n") ;
return 0 ;
}

これを、「hello.c」というファイル名で保存します。

そして、ターミナルにてカレントディレクトリをhello.cがあるフォルダと同じフォルダに移動させます。
その上で、

$ gcc hello.c      #「hello.c」のところは、作成したファイル名と同じ名前を入力。


そうすると、同じフォルダ内に、「a.out」という新しいファイルができます。

続いて、ターミナルで、

$ ./a.out

と打つと、

$ ./a.out
hello world

 

C++の実行

こちらも例によってテストに使うのは次のコードです。
#include<iostream>
using namespace std;

int main(){
  cout << "Hello World"<<endl;
  return 0 ;
}


今度は拡張子を.cppにして保存します。
ここでは、「hello.cpp」というファイル名で保存します。

またカレントディレクトリに移動します。

今度はターミナルにて次のように打ちます。

$ c++ hello.cpp      #C++の場合はgccではなくc++


C言語のときと同様に「a.out」というファイルができます。
また同様に、

$ ./a.out

とうてば、

$ ./a.out
Hello world

となります。

参考にしたページや文献・書籍

www.sirochro.com


piroshhh.hatenablog.com

Macでpython3の環境構築11---NetworkXの使い方2---

前回

piroshhh.hatenablog.com

で、なんとかNetworkXでラベルを表示することができたので、今回はNetworkXの基本的な使い方を公式Documentを参考にしながら掘り下げてみようと思います。

グラフの作成(ノードとエッジの作成)

参考:Creating a graph — NetworkX 1.10 documentation

ノードの作成

簡単な処理から順番に行きます。ノードを追加するには以下のようなコードになります。
import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()       # まずは、空のグラフオブジェクトを用意します。
        
    g.add_nodes_from([2,3,4,5])      #  用意したグラフオブジェクトにノード(だけ)を追加します。引数はノードのリストです。
    
    nx.draw_networkx(g,pos=nx.spring_layout(g))     # グラフの描画に必要

    pyplot.show()
if __name__ == '__main__':
    main()


これを実行すると、次のような画面が出てきます。(配置は自動でされます)


エッジの追加

さらにコードを書き足します。

import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
             
    g.add_nodes_from([2,3,4,5])
    
    g.add_edge(2,3)      # 2と3をつなぐエッジを追加する。
    
    nx.draw_networkx(g,pos=nx.spring_layout(g))
    
    pyplot.show()

if __name__ == '__main__':
    main()


これを実行すると、以下のようになり2と3をつなぐエッジが追加されます。

エッジは次のような書き方でも追加できるようです。


import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
               
    g.add_nodes_from([2,3,4,5])
    
    g.add_edge(2,3)
    
    e=(4,5)        
    
    g.add_edge(*e)      # こういう書き方でもエッジは追加できる。
    
    nx.draw_networkx(g,pos=nx.spring_layout(g))
    
    pyplot.show()

if __name__ == '__main__':
    main()


これを実行すると、確かに4と5をつなぐエッジが追加されています。

 

オプションの指定

挿入している画像のサイズなどを調整したいので、このあたりでオプションの指定をしおきます。

参考:draw_networkx — NetworkX 1.10 documentation

ノードのサイズ

node_sizeで指定します。デフォルトは300だそうです。

import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
     
    g.add_nodes_from([2,3,4,5])
    
    g.add_edge(2,3)
    
    e=(4,5)
    
    g.add_edge(*e)
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500)        # node_sizeで指定します。デフォルトは300だそうです。
    
    pyplot.show()

if __name__ == '__main__':
    main()


これを実行すると、こうなります。


(ラベルの)フォントサイズ

font_sizeで指定します。デフォルトは12らしいです。

import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
      
    g.add_nodes_from([2,3,4,5])
    
    g.add_edge(2,3)
    
    e=(4,5)
    
    g.add_edge(*e)
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=30)    # font_sizeで指定します。デフォルトは12らしいです。
    
    pyplot.show()

if __name__ == '__main__':
    main()


これを実行すると、こうなります。

 

ノードの色

node_colorで指定します。デフォルトは今まで見てきたように赤です。

import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
                
    g.add_nodes_from([2,3,4,5])
    
    g.add_edge(2,3)
    
    e=(4,5)
    
    g.add_edge(*e)
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=30,node_color="b")     # node_colorで指定します。デフォルトは赤です。
    
    pyplot.show()

if __name__ == '__main__':
    main()


これを実行すると、こうなります。

色の指定については(他のオプションもかもしれませんが)、Matplotlibの機能を使って実現されています。
なので、指定方法はMatplotlibに則ります。
よく使う色であれば、青であれば"b"、赤は"r"、緑は"g"、と記号で指定できます。
その他、hexstring(16進指定、#FFFFFFみたいな)だったり、gray shade(灰色の濃淡指定)で0〜1の間の値で指定できたりもします。
より詳しいことは、

colors — Matplotlib 1.5.1 documentation

(Matplotlibのドキュメントページ)やHTMLの色の指定方法などを調べてみましょう。

エッジの太さ

widthで指定します。デフォルトは1.0です。

import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
        
    g.add_nodes_from([2,3,4,5])
    
    g.add_edge(2,3)
    
    e=(4,5)
    
    g.add_edge(*e)
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=30,node_color="b",width=5.0)     # widthで指定します。デフォルトは1.0です。
    
    pyplot.show()

if __name__ == '__main__':
    main()


これを実行すると、こうなります。

 

エッジの種類

ノードとノードをつなぐ線の種類はstyleで指定します。デフォルトはsolidですが、その他dashedやdotted、dashdotなどが指定できるようです。
次のコードはdashedを指定してみました。

import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
    
    g.add_nodes_from([2,3,4,5])
    
    g.add_edge(2,3)
    
    e=(4,5)
    
    g.add_edge(*e)
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=30,node_color="b",width=5.0,style="dashed")    # styleで指定します。デフォルトはsolidです。
    
    pyplot.show()

if __name__ == '__main__':
    main()


実行結果はこちら。

 

透かし

ノードの透かします。これはalphaで指定します。デフォルトでは当然透かしは入っていないので、それは1.0となっています。

import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
    
    g.add_nodes_from([2,3,4,5])
    
    g.add_edge(2,3)
    
    e=(4,5)
    
    g.add_edge(*e)
    
    nx.draw_networkx(g,pos=nx.spring_layout(g),node_size=500,font_size=30,node_color="b",width=5.0,style="dashed",alpha=0.5)    # alphaで指定します。デフォルトは1.0で透かしは入っていないですが、ここでは0.5と指定しています。
    
    pyplot.show()

if __name__ == '__main__':
    main()


実行結果はこちら。


とりあえず、よく使いそうなところだけ取りあげましたが、NetworkXのドキュメントを見ると他にもいろいろ指定できるようですが、 値として何を指定できるのかがわからないものもある。。。

とりあえずはここまでにしておいて、おいおい分かり次第、紹介します。

Macでpython3の環境構築10---NetworkXの使い方1---

以前の記事

piroshhh.hatenablog.com

piroshhh.hatenablog.com

piroshhh.hatenablog.com

 

にて、Matplotlibをインストールしたものの、Networkxのプログラムを実行しても、ノードにラベルがつかない、といろいろ悩んでおりました。

 
で、解決策の一つがわかったので、自分のための忘備録もかねて書いておきたいと思います。

サンプルプログラムとして次のコードを使います。

プログラムの各行の意味などは、また別の機会に書きますが、最後の方の描画部分がポイントです。
import networkx as nx

from matplotlib import pyplot

def main() :
    g=nx.Graph()
              
    g.add_nodes_from([2,3,4,5])
    
    g.add_edge(2,3)
    
    e=(4,5)
    
    g.add_edge(*e)
    
    g.add_path([1,3,4,2])   #  ノードのリストを渡すと、ノードを順番通りにつなぐ道のりがグラフについかされる。

    nx.draw_networkx(g,pos=nx.spring_layout(g))        # この行が重要。nx.draw()ではない。posを指定しないとエラーになる。
    pyplot.show()
    


if __name__ == '__main__':
    main()


これまでは、nx.draw(g)などとしていましたがnx.draw_networkx(g,pos=nx.spring_layout(g)) とすれば改善するようです。

実際の実行結果がこちら。


となり、確かにノードにラベルがついております。


で話はそれますが、

if __name__ == '__main__':
    main()

の部分についてよく理解していなかったので調べてみました。

調べていたらとてもわかりやすい説明があったので、そちらのリンクを紹介しておきたいと思います。

azuuun-memorandum.hatenablog.com

 

Macでpython3の環境構築9---Scipyのインストール---

今回はScipyのインストールをします。

参考書として手元に置いてある『パーフェクトpython』では、gfortranとswig(とそれに必要なPCRE)というものを先にインストールしているようなのですが、ネット上ではswigをインストールしているものはあまり見かけませんでした。

swigとはなにか調べてみると、pythonの中でC/C++を呼び出すためのライブラリのようです。

特にswigは必ずしも必要ではないようでしたので、とりあえず、Scipyだけインストールすることにしました。

gfortranのインストール

ということで、まずはgfortranのインストールをします。
次のように打ちます。


hiroshi-no-MacBook-Air:~ hiroshi$ brew install gfortran      #「brew install gfortran」と打ってreturnを押す。

するとこんなメッセージがでました。


GNU Fortran is now provided as part of GCC, and can be installed with:
  brew install gcc

gfortranはgccの一部として提供されています。brew install gccとすればインストールできます、と表示されているので素直に指示に従ってみます。


hiroshi-no-MacBook-Air:~ hiroshi$ brew install gcc      #  「brew install gcc」と打ってreturnを押す。

そうすると、処理が始まります。


==> Installing dependencies for gcc: gmp, mpfr, libmpc, isl
==> Installing gcc dependency: gmp
==> Downloading https://homebrew.bintray.com/bottles/gmp-6.0.0a.yosemite.bottle.
######################################################################## 100.0%
==> Pouring gmp-6.0.0a.yosemite.bottle.tar.gz
🍺  /usr/local/Cellar/gmp/6.0.0a: 15 files, 3.2M
==> Installing gcc dependency: mpfr
==> Downloading https://homebrew.bintray.com/bottles/mpfr-3.1.3.yosemite.bottle.
######################################################################## 100.0%
==> Pouring mpfr-3.1.3.yosemite.bottle.tar.gz
🍺  /usr/local/Cellar/mpfr/3.1.3: 24 files, 3.6M
==> Installing gcc dependency: libmpc
==> Downloading https://homebrew.bintray.com/bottles/libmpc-1.0.3.yosemite.bottl
######################################################################## 100.0%
==> Pouring libmpc-1.0.3.yosemite.bottle.tar.gz
🍺  /usr/local/Cellar/libmpc/1.0.3: 10 files, 380K
==> Installing gcc dependency: isl
==> Downloading https://homebrew.bintray.com/bottles/isl-0.14.1.yosemite.bottle.
######################################################################## 100.0%
==> Pouring isl-0.14.1.yosemite.bottle.3.tar.gz
🍺  /usr/local/Cellar/isl/0.14.1: 68 files, 3.2M
==> Installing gcc
==> Downloading https://homebrew.bintray.com/bottles/gcc-5.2.0.yosemite.bottle.1
######################################################################## 100.0%
==> Pouring gcc-5.2.0.yosemite.bottle.1.tar.gz
==> Caveats
GCC has been built with multilib support. Notably, OpenMP may not work:
  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60670
If you need OpenMP support you may want to
  brew reinstall gcc --without-multilib
==> Summary
🍺  /usr/local/Cellar/gcc/5.2.0: 1353 files, 248M

で、確認のためもう一度同じコマンドを打ってみます。


hiroshi-no-MacBook-Air:~ hiroshi$ brew install gcc      #「brew install gcc」と打ってreturnを押すと、、、
Warning: gcc-5.2.0 already installed       #  すでにインストールされてますよ、と出る。

gfortranのインストールはこれで完了。

Scipyのインストール

gfortranのインストールさえしてしまえば、あとはほとんどやることはありません。

「pip3 install scipy」とするだけです。


hiroshi-no-MacBook-Air:~ hiroshi$ pip3 install scipy      #  「pip3 install scipy」と打ってreturnを押すと下の行からの処理が始まる。
You are using pip version 7.1.0, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting scipy
  Downloading scipy-0.16.0-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (19.7MB)
    100% |████████████████████████████████| 19.7MB 19kB/s
Installing collected packages: scipy
Successfully installed scipy-0.16.0

「Successfully installed scipy-0.16.0」と出ているので無事インストール終了です。

でもさ。少し疑問。
gccってXcodeをインストールしたときに一緒にインストールしなかったっけ?それとは違うのか?。
わかる人いたらご教示ください。

csvファイルの扱い1---スプレッドシートやExcelとの絡みにて---

csvファイルについて調べたことを書いてみようと思います。

もともとの目標は、pythonなどのプログラミング言語csvファイルを扱う方法について調べようと思っていたのですが、もう少し基本的なことまで遡ってcsvファイルの扱い方について調べてみました。

csvファイルとは

そもそもなんなのか、という部分については検索すれば丁寧な解説がたくさんでているので、そちらへ譲ります。
要するに、windowsでいえばメモ帳、Macでいえばテキストエディタとかに書かれていて、カンマで区切られて書かれている
こういうファイルです。
 
これを、例えばExcelとか、もともとは表形式で作成したデータを、テキストファイル形式にして、他のアプリケーションとやりとりをしやすくします。
 
表形式のデータだと、ExcelExcelの間のやりとりなら、まだいいとしても、他のソフトとやりとりをしようと思うと、ファイルの形式が合わず、うまくいきません。
 
データそのもの(つまりExcelの場合でいえば、各セルに入力されているデータ)以外にも、体裁を整えるための余分なデータがくっついてしまっているので。
 
で、データの基本的な部分だけ残して作ったものが、csvファイルです。

 

スプレッドシートcsvファイルの作成

Excelをあまり使ったことがないのでわかりませんが、おそらくExcelでも基本的には同じだと思います。
 
ここでは、Googleドライブのスプレッドシートからcsvファイルを作成します。
 
サンプル用のデータとして、次のようなデータを用意します。
 
 
データを作成したら、画面左上の「ファイル」をクリックし、「形式を指定してダウンロード」→「カンマ区切りの値(.csv、現在のシート)」を選んでクリックします。
 
 
クリックをすると、すぐにダウンロードされます。
 
普段ダウンロードされたデータが入る場所をみると、「.csv」のファイルができています。
今はMacを使っているので、「ダウンロード」というフォルダの中に入りました。
 
そのままcsvのデータを開くと、Excelとかデフォルトで指定されているソフトで開かれてしまうと思うので、どのソフトで開くかを指定します。
 
Macの場合であれば、
 

 

ここをクリックすると、


「このアプリケーションを開く」にマウスを合わせ、

テキストエディタ」をクリックします。

そうすると、テキストエディタ

このように書かれたデータが表示されます。これがcsvファイルです。

csvファイル→スプレッドシート

具体的な操作手順に関してはいろいろあると思いますが、今回は次のような方法でいきます。

新規にスプレッドシートを開き、画面左上のメニューから「インポート」を選びます。


新しく出てくる画面で、アップロードを選び、画面の指示通り、該当のファイルを直接ドラッグするか、パソコンのから該当のファイルを選んで指定します。そうすると、左下の「選択」のボタンが押せるようになるので、クリックします。


そうすると次のような画面がでます。


ここで操作を選びます。
インポート操作のところは、

 

    • 新しいシートを挿入する。
      →現在開いているスプレッドシートで、新しいシートを追加し新しく追加したシートにcsvファイルを読み込み。

 

  • スプレッドシートを置き換えする。
    →現在のシートにそのままcsvファイルを流し込む。(それ以下の三つと混同しやすい表現ですが。。。)
それ以下三つの選択肢はやや説明が面倒なのでしません。
 
区切り文字の方は、文字通り「データの区切りの記号はなににしているか」です。
ここは「自動的に検出する」か、「カンマ」にします。csvですのでね。
 
「タブ」もあるのは、世の中にはcsv(Camma Separeted Value)に対して、「タブ」で区切られてるtsv(Tab Separated Value)なるものもあるからです。
 
それで読み込みをすると、こうなって無事読み込めます。
 
 
 
ちなみにcsvファイルを読み込むのに、区切り文字に「タブ」を選択すると、次のようになります。
 
これは、区切り文字は「タブ」と指定しているので、「タブ」がデータの区切りになっているわけです。
つまり、タブが現れるまでは一塊のデータです。
一方、csvと同じように改行コードがくれば、行が変わります。
csvファイルをtsvとして読み込むと、タブが来る前に改行コードが来ているので、一行目が、丸々ひとつのデータをして扱われ、改行コードにて次の行に読み込まれているわけです。
 
次回は、csvpythonやCで扱う方法について書いてみようと思います。