ろむめも

気になったこととか、調べたことをゆるくまとめます。主にプログラミング関連の話題が多いです。

Python sqlite3のjournal Modeを変更する方法

以下で変更できるようです。
まずコネクトして、

        conn = sqlite3.connect(_name, isolation_level=None)

PRGMAを使ってjournal_mode=***に所望のmodeを設定すればOKです。
(今回はMemoryしました。)

        conn.execute("PRAGMA journal_mode=MEMORY")

ちなみにconnect時のisolation_levelは自動コミット有効/無効(=None/の切り替えとの事です。

MacでCaffe(DeepLeaningフレームワーク)の立ちあげ方

経緯

我が家のMac環境にDeepLearning用フレームワークであるcaffeをインストールしようとした時の話。

公式では、Caffeの特徴を以下のようにうたっています。
アーキテクチャが良い(Cpu/Gpuモードの切り替えが簡単
・コードの拡張性が高い(多くの開発者が参加しているから
・処理が早い(シングルNVIDIA K40 GPU上で1日当たり6千万枚?の画像を処理できる
・コミュニティがある

今回、私がCaffeを選んだ理由は日本語ネイティブの辛い所ではありますが、日本語のオライリー本がある所でした。
O'Reilly Japan - Caffeをはじめよう

やった事

まず、公式のgithubレポジトリからpullリクエストを行い、caffeのコードをローカル環境に持ってきました。
BVLC/caffe · GitHub
実際には私の環境ではgithub desktopを使ってpullしました。

次に、これをビルドして使えるようにする必要があります。
手順はまずは公式サイトのインストール項を参照しました。
Caffe | Installation

GPUモードもあるようですが、CUDAの設定もやらないといけなくなるので色々面倒だと思い、CPUのみのモードでやります。
CPUのみのモードだとmakefile.configの中身の編集が必要とのことでした。

CPU-only Caffe: for cold-brewed CPU-only Caffe uncomment the CPU_ONLY := 1 flag in Makefile.config to configure and build Caffe without CUDA. This is helpful for cloud or cluster deployment.

本日時点では8行目ぐらい?にある設定の頭に「#」があるのでそれを削除してuncommentしました。

これでいけるかなと思いまして、

cd caffe
make all

でビルド
(caffeの場所はpullした場所によるので各自の環境でmake実行する必要があります)

PROTOC src/caffe/proto/caffe.proto
make: protoc: No such file or directory
make: *** [.build_release/src/caffe/proto/caffe.pb.cc] Error 1

ダメでした。
読むとgoogle protocolが無いのでダメですとのこと。
今回、homebrew環境からgoogle protocolをインストールしました。
その手順はこちらの記事を参照ください。
Mac環境にHomebrew経由でGoogle Protocolをインストールする - ろむめも


で、protocを仕入れて再度make all

🍺  /usr/local/Cellar/protobuf/2.6.1: 118 files, 7.1M

PROTOC src/caffe/proto/caffe.proto
CXX .build_release/src/caffe/proto/caffe.pb.cc
CXX src/caffe/blob.cpp
In file included from src/caffe/blob.cpp:4:
In file included from ./include/caffe/blob.hpp:8:
./include/caffe/common.hpp:4:10: fatal error: 'boost/shared_ptr.hpp' file not found
#include <boost/shared_ptr.hpp>
         ^
1 error generated.
make: *** [.build_release/src/caffe/blob.o] Error 1

boostが無いって言ってる。

こんな感じで無いものをhomebrewで
brew install **
しながら環境構築していきました。
私の環境でインストールが必要だったものは以下です。

brew install snappy leveldb gflags glog szip lmdb
brew install boost
brew install home-brew/science/opencv
brew install hdf5
brew install openblas

brewの不足品は以下のサイトを参考にしながらinstallしました。
CaffeをOS X 10.10 にインストールした // ichyo.jp
CaffeをCPU ModeでMac OS X 10.10に入れる手順 - Qiita
ただ、私の環境では上記サイトにあるようなboostバージョンによるエラーは出ませんでした。

また、余談ですがbrewしている過程で、以下のメッセージが出ました。
GitHubを特定の時間内で使いまくるとこうなるんでしょうか。
個人用tokenを作ってそれ使って下さいって言ってますが、今回は無視しましたが問題なくインストールできました。

Error: GitHub API rate limit exceeded for IPアドレス (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)
Try again in 4 minutes 45 seconds, or create a personal access token:
  https://github.com/settings/tokens/new?scopes=&description=Homebrew
and then set the token as: HOMEBREW_GITHUB_API_TOKEN

また、opencvのインストール時に以下のメッセージが出ましたが、これも無視しました。

Warning: homebrew/science/opencv dependency gcc was built with a different C++ standard
library (libstdc++ from clang). This may cause problems at runtime.
==> 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/note/.local/lib/python2.7/site-packages
  echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/note/.local/lib/python2.7/site-packages/homebrew.pth

とにかく早く環境立ちあげたかったので、あとで問題あれば対処という方向付けにしました。

必要なパッケージのインストールが完了したので無事

make all

でcaffeのビルドができました。

caffeはビルド後のテストを行うことができるようです。
これにクリアすればビルドが問題なくできた事が確認できます。
早速、

make test
make runtest

とすると、

[----------] Global test environment tear-down
[==========] 927 tests from 140 test cases ran. (57201 ms total)
[  PASSED  ] 927 tests.

で問題なくインストールできている事が確認できました。

Mac環境にHomebrew経由でパッケージをインストールする

経緯

mac環境にGoogle ProtocolをHomebrewでインストールしようとした時の話です。
後で別の記事に書きますが、もともとはCaffeをインストールしたかったのですが、
その過程で、後述の通りグーグルプロトコルのインストールを行いました。
他のパッケージもこれと同じようにインストールできます

やった事

久しぶりにHomebrewを立ち上げたのでまずは環境の検証から。

brew doctor

すると

Warning: Your Homebrew is outdated.
You haven't updated for at least 24 hours, this is a long time in brewland!
To update Homebrew, run `brew update`.

バージョンが古いということなので

brew update

でアップデート実行。

ところが、

error: unable to unlink old '.gitignore' (Permission denied)
error: unable to create file .travis.yml (Permission denied)
error: unable to create file .yardopts (Permission denied)
error: unable to unlink old 'CODEOFCONDUCT.md' (Permission denied)
error: unable to unlink old 'CONTRIBUTING.md' (Permission denied)
error: unable to unlink old 'LICENSE.txt' (Permission denied)
error: unable to unlink old 'README.md' (Permission denied)
error: unable to unlink old 'SUPPORTERS.md' (Permission denied)
Error: Failure while executing: git pull -q origin refs/heads/master:refs/remotes/origin/master

と表示されアップデートできませんでした。

homebrewのupdateが失敗した時 - まめ畑
の記事を発見し、書かれているように実行

cd `brew --prefix`
git remote add origin https://github.com/mxcl/homebrew.git
fatal: remote origin already exists.
git fetch origin
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 10 (delta 4), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.
From https://github.com/Homebrew/homebrew
   ed46a0a..1e6d9b9  master     -> origin/master
git reset --hard origin/master
warning: unable to unlink Library/ENV/pkgconfig/10.9: Operation not permitted
error: unable to unlink old '.gitignore' (Permission denied)
error: unable to create file .travis.yml (Permission denied)
error: unable to create file .yardopts (Permission denied)
error: unable to unlink old 'CODEOFCONDUCT.md' (Permission denied)
error: unable to unlink old 'CONTRIBUTING.md' (Permission denied)
error: unable to unlink old 'LICENSE.txt' (Permission denied)
error: unable to unlink old 'README.md' (Permission denied)
error: unable to unlink old 'SUPPORTERS.md' (Permission denied)
fatal: Could not reset index file to revision 'origin/master'.
brew update
Error: The /usr/local directory is not writable.
Even if this directory was writable when you installed Homebrew, other
software may change permissions on this directory. Some versions of the
"InstantOn" component of Airfoil or running Cocktail cleanup/optimizations
are known to do this.

You should probably change the ownership and permissions of /usr/local
back to your user account.
  sudo chown -R $(whoami):admin /usr/local

これでもダメのようでしたが、よく読むとpermissionの問題を指摘されていました。

そこで、ログインしているアカウントにアクセス件を与えて、

sudo chown -R $(whoami):admin /user/local
chown: /user/local: No such file or directory
sudo chown -R $(whoami):admin /usr/local

これでホームブリューのアップデートが

brew update
Updated Homebrew from 3dcf8b5d to b961ec13.

成功しました。

最後に

brew install protobuf

Google ProtocolBufferのインストールが完了しました

試しに実行

protoc
Missing input file.

ちゃんとprotocはいますね。

以上です。

python pandasでjson出力する時に文字コードが上手くいかない場合

環境

現象

df = DataFrame(***)
ret = df.to_json()
print(ret)

で、日本語を含む場合に、よくある「\u65e5\u672c\u8a9e」こういうのが出る

対策

df = DataFrame(***)
ret = df.to_json(force_ascii=True)
print(ret)

とすると日本語が正しく表示される。

ちなみに

df = DataFrame(***)
ret = df.to_json(orient="records")
print(ret)

じゃないと同じデータがあるとか言われてエラーが出る。
ググったら出力の形式というか、データ構造を指定するオプションのようでした。

pycharm Tips

pycharmにこっぴどく怒られてるからまとめ

①タブは使わない。半角スペース4つ
②lowercaseを使え(abc_def)
③augmented assignmentを使え(+=/-=)

algorithm - Remove redundant parentheses from an arithmetic expression - Stack Overflow

python - pycharm convert tabs to spaces automatically - Stack Overflow