Jupyter NotebookからPythonでPostgreSQL 13を操作する

まずは堅実に定番RDBから

 NoSQLブームで勉強会界隈では影が薄れた感があるRDBMSですが、当たり前の存在になっただけで健在です。当サイトが使っているCMS『WordPress』にはMySQLが組み込まれていますし、当サイトを置いているAWSでもPostgreSQLがRedShiftやAuroraなど基幹サービスで使われています

 統計モデリングを知らないのにディープラーニングでイキる人が信用出来ないのと同様、RDBMSを知らないのにNoSQLを語るのもイタい人です。私も昨年はElasticsearchなどのNoSQLやApache Igniteなどの分散データベースにハマっていましたが、自分のイタさに気づいたためPostgreSQLを学び直しています。

 NoSQLや分散データベースを業務で使いこなしている方々の講演は勉強会で聞くことが出来ますが、彼らは「RDBMSを経験した上でNoSQLに進んでいる」のです。未経験者が講演を聞いていきなりNoSQLに行くのは『悪手』です。

初心者がNoSQLや分散RDBでやりたいことはPostgreSQLでも出来ている

 NoSQLが主に扱うJSONはPosgreSQLでも『JSON型』として扱えます。IMDBで話題となったインメモリ処理はPostgreSQLでも駆使されています。分散データベースの特徴であるクエリの並列処理はPostgreSQLでもパラレルクエリとして実装されている上、特別の設定無しで自動的に使ってくれます。

 昨年、実際にApage IgniteでPCサーバ5台のクラスタを構築しましたが、数10GB程度のデータではクエリに要する時間がシングルノードのPostgreSQLの方が速かったです。2.5GbEでもLANの遅延で並列処理のメリットが相殺されてしまいます。この規模のデータでクエリを高速化するなら、32コア64スレッドのパソコンでも買えば良いと思います。

 こういう事実は、基本的すぎて勉強会ではなかなか教えてもらえないですね。エンジニアの方にとっては常識なのでしょうが……💦

PythonでPostgreSQLを扱うパッケージ『psycopg2』

 PythonでPostgreSQLを扱う際は『psycopg2』というパッケージを使うのが一般的なようです。視覚化のためのパッケージ『Plotly』と併せてインストールします。

(base) masaru@ASUS-TUF-Gaming:~$ conda install psycopg2
(base) masaru@ASUS-TUF-Gaming:~$ conda install plotly

Jupyter Notebookでの実装

 必要なパッケージの読み込みなどの初期設定を行います。

# -*- coding: utf-8 -*-
import psycopg2
import pandas as pd
import plotly as py
import plotly.graph_objs as go
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()

 psychopg2でデータベース接続を定義します。

def connect():
    con = psycopg2.connect("host=" + "localhost" +
                           " port=" + "5432" +
                           " dbname=" + "google_mobility" +
                           " user=" + "masaru" +
                           " password=" + "xxxxxxxxxxxx")
    return con

 続いて、クエリを定義します。

def select_execute(con, sql):
    with con.cursor() as cur:
        cur.execute(sql)
        rows = cur.fetchall()

    return rows

 定義したクエリを実行します。

con = connect()
sql =  "select * from google_mobility where SUB_REGION_1 = 'Tokyo'"
result = select_execute(con, sql)

 クエリの結果をPandasデータフレームに代入します。

df = pd.DataFrame(result)
df.head()

 とりあえず全部カラム名をつけてあげます。

columns = ["id","country_region_code","country_region","sub_region_1","sub_region_2","metro_area","iso_3166_2_code","census_fips_code","date","retail","grocery","parks","transit","workplaces","residental","place_id"]
df.columns = columns
df.head()

 Plotlyで表示するデータを設定します。

trace1 = go.Scatter(
        x = list(df.date),
        y = list(df.retail),
        mode = 'lines+markers',
        name = 'retail',
        marker = dict(
                color = 'blue'
                )
        )

trace2 = go.Scatter(
        x = list(df.date),
        y = list(df.grocery),
        mode = 'lines+markers',
        name = 'grocery',
        marker = dict(
                color = 'orange'
                )
        )

data = [trace1, trace2]

 続いて、Plotlyの特長であるスライダーを設定します。

layout = dict(
    title='Time series with range slider and selectors',
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label='1m',
                     step='month',
                     stepmode='backward'),
                dict(count=6,
                     label='6m',
                     step='month',
                     stepmode='backward'),
                dict(count=1,
                    label='YTD',
                    step='year',
                    stepmode='todate'),
                dict(count=1,
                    label='1y',
                    step='year',
                    stepmode='backward'),
                dict(step='all')
            ])
        ),
        rangeslider=dict(
            visible = True
        ),
        type='date'
    )
)

 グラフを描画します。

fig = dict(data=data, layout=layout)
iplot(fig)

 スライダーを動かせるグラフの出来上がりです。

Ubuntu 21.04使用開始(1日目)

Ryzen+NVIDIAのノートが欲しい。データベースと機械学習の全部入りラーメンだ

 前々から、図書館などでもガッツリ使い倒せるLinuxノートが欲しいと思っていました。具体的には、PostgreSQLがselect文で4スレッドくらい並列処理してくれて、TensorFlow/KerasでGPU SUGEEEE出来る程度のスペックです。

 Amazonの年末セールで『ASUS ゲーミングノートパソコン TUF Gaming A15 FA506IH (AMD Ryzen5 4600H/8GB・SSD 512GB/GTX 1650/1,920×1,080ドット (フルHD) (144Hz)/15.6インチ/フォートレス・グレイ)』が¥82,800と安かったので衝動買い。メモリは即32GBに増設しました。

「AMDが7nmで作ったワットパフォーマンスが良いRenoirが載ったノートにディープラーニングで使えるGTX 1650が付いて8万ちょい。買うしかない……」

 4600Hは6コア12スレッドのノートPC向けCPUで、AMD Renoirシリーズの廉価版です。『最強』ではありませんが、実売10万円以下のノートでこの性能は魅力的です。SSDは内蔵で別途2TBくらい欲しいですが、金欠なので追い詰まってから考えることにします。

AMD Ryzen 5 4600Hは7nm世代の廉価版モバイルCPU

 なお現在の最新モデルは「Ryzen PRO 5000シリーズ モバイル プロセッサ(5850U/5650U/5450U)」、及び「Ryzen 5000シリーズ モバイル プロセッサ(5800U/5700U/5600U/5500U/5400U/5300U)」で7nm世代のままです。5nmのZen4世代は2022年に登場予定です。

ゲーミングPCへのUbuntu 21.04のインストール

 Ubuntuのインストールが終わると、沢山の有名で楽しそうなアプリをリコメンドされます。「SSDの残りが300GBくらいしかないのに片っ端から入れちゃダメダ」と思いつつも入れられるうちはつい入れてしまいます。

日本語フォルダ名を英語化

 lsなどでパス指定するときにフォルダ名が日本語だとIMEの切り替えや入力が面倒なので、英語に変えてしまいます。

masaru@ASUS-TUF-Gaming:~$ LANG=C xdg-user-dirs-gtk-update

導入直後に入れたアプリ

 初心者でも導入に困らなさそうなものは名前だけ……。

NVIDIA driver metapackage NVIDIA独自のグラフィックドライバ。『ソフトウェアとアップデート』 – 『追加のドライバー』でプロプライエタリ版を指定してインストールする必要がある
Ubuntu Live Patch パッケージの自動更新サービス。実際は手動でapt updateを続けることが多いですが、念のため導入
Shutter 画面キャプチャソフト。不具合のため現在使用できず
Krita 定番の画像ソフト。Shutterが使えなかったので最優先で導入。個人的にも好きなソフト
Google Chrome, Chromium Webブラウザ。SSOの誘惑と逃げたい気持ちでサンドイッチに
dbeaver-ce JDBCの汎用データベースクライアント
Anaconda 言わずと知れたPython機械学習系のフレームワーク。PATHを汚染するので好きではないのですが、楽なので結局入れてしまいます
jupyter-notebook Anacondaに同梱のPython実行環境
Visual Studio Code プログラム開発環境のデファクトスタンダード。Pythonなどのインタプリタを配下に置いてしまうAnacondaとの共存が課題
R Anacondaのパッケージとしてcondaでインストール
RStudio Rの開発環境。PATHの問題を避けるためAnacondaからインストール
traceroute サーバーまでのネットワーク経路を確認するツール
OpenJDK 11 各種データベース用のJava開発環境

NDIVIAドライバの基本設定

 Ubuntu 21.04は、素の状態でNVIDIAの独自ドライバを使ってくれません。チューニングの観点から独自ドライバを入れるのが良さそうです。

masaru@ASUS-TUF-Gaming:~$ lspci | grep -i nvidia
01:00.0 VGA compatible controller: NVIDIA Corporation TU117M (rev ff)
01:00.1 Audio device: NVIDIA Corporation Device 10fa (rev ff)

 続いて、aptのレポジトリに最新のドライバが降ってくるように設定します。

masaru@ASUS-TUF-Gaming:~$ sudo add-apt-repository -y -n ppa:graphics-drivers/ppaPPA publishes dbgsym, you may need to include 'main/debug' component
Repository: 'deb http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu/ hirsute main'
Description:
Fresh drivers from upstream, currently shipping Nvidia.

## Current Status

Current long-lived branch release: `nvidia-430` (430.40)
Dropped support for Fermi series (https://nvidia.custhelp.com/app/answers/detail/a_id/4656)

Old long-lived branch release: `nvidia-390` (390.129)

For GF1xx GPUs use `nvidia-390` (390.129)
For G8x, G9x and GT2xx GPUs use `nvidia-340` (340.107)
For NV4x and G7x GPUs use `nvidia-304` (304.137) End-Of-Life!

Support timeframes for Unix legacy GPU releases:
https://nvidia.custhelp.com/app/answers/detail/a_id/3142

## What we're working on right now:

- Normal driver updates
- Help Wanted: Mesa Updates for Intel/AMD users, ping us if you want to help do this work, we're shorthanded.

## WARNINGS:

This PPA is currently in testing, you should be experienced with packaging before you dive in here:

Volunteers welcome!

### How you can help:

## Install PTS and benchmark your gear:

    sudo apt-get install phoronix-test-suite

Run the benchmark:

    phoronix-test-suite default-benchmark openarena xonotic tesseract gputest unigine-valley

and then say yes when it asks you to submit your results to openbechmarking.org. Then grab a cup of coffee, it takes a bit for the benchmarks to run. Depending on the version of Ubuntu you're using it might preferable for you to grabs PTS from upstream directly: http://www.phoronix-test-suite.com/?k=downloads

## Share your results with the community:

Post a link to your results (or any other feedback to): https://launchpad.net/~graphics-drivers-testers

Remember to rerun and resubmit the benchmarks after driver upgrades, this will allow us to gather a bunch of data on performance that we can share with everybody.

If you run into old documentation referring to other PPAs, you can help us by consolidating references to this PPA.

If someone wants to go ahead and start prototyping on `software-properties-gtk` on what the GUI should look like, please start hacking!

## Help us Help You!

We use the donation funds to get the developers hardware to test and upload these drivers, please consider donating to the "community" slider on the donation page if you're loving this PPA:

http://www.ubuntu.com/download/desktop/contribute
More info: https://launchpad.net/~graphics-drivers/+archive/ubuntu/ppa
Adding repository.
Adding deb entry to /etc/apt/sources.list.d/graphics-drivers-ubuntu-ppa-hirsute.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/graphics-drivers-ubuntu-ppa-hirsute.list
Adding key to /etc/apt/trusted.gpg.d/graphics-drivers-ubuntu-ppa.gpg with fingerprint 2388FF3BE10A76F638F80723FCAE110B1118213C

Linux版のAnacondaはシェルから導入

 Anacondaはパスなど各種設定をOSから乗っ取る『お行儀が悪いフレームワーク』なためか、Ubuntuでもストアアプリには入っていません。シェルスクリプトを落としてbashで実行します。

masaru@ASUS-TUF-Gaming:~/Downloads$ bash ./Anaconda3-2020.11-Linux-x86_64.sh 

Welcome to Anaconda3 2020.11

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
...
masaru@ASUS-TUF-Gaming:~/anaconda3/bin$ ./conda init
no change     /home/masaru/anaconda3/condabin/conda
no change     /home/masaru/anaconda3/bin/conda
no change     /home/masaru/anaconda3/bin/conda-env
no change     /home/masaru/anaconda3/bin/activate
no change     /home/masaru/anaconda3/bin/deactivate
no change     /home/masaru/anaconda3/etc/profile.d/conda.sh
no change     /home/masaru/anaconda3/etc/fish/conf.d/conda.fish
no change     /home/masaru/anaconda3/shell/condabin/Conda.psm1
no change     /home/masaru/anaconda3/shell/condabin/conda-hook.ps1
no change     /home/masaru/anaconda3/lib/python3.8/site-packages/xontrib/conda.xsh
no change     /home/masaru/anaconda3/etc/profile.d/conda.csh
modified      /home/masaru/.bashrc
(base) masaru@ASUS-TUF-Gaming:~$ conda update --all
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/masaru/anaconda3


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    _anaconda_depends-2020.07  |           py38_0           6 KB
...

Anacondaの功罪

 今回インストールしたAnacondaは2020年7月版。主な開発ツールのPATHを乗っ取る仕様であることを考えると半年以上放置されているのは少し疑念を感じます。もちろんconda updateはかけるのですが……。

 最新版をいち早く使いたい方はAnaconda無しのアカウントを作るか、仮想環境で運用することになりそうです。

(base) masaru@ASUS-TUF-Gaming:~$ python --version
Python 3.8.8