ラベル Jupyter の投稿を表示しています。 すべての投稿を表示
ラベル Jupyter の投稿を表示しています。 すべての投稿を表示

2023年5月28日日曜日

Jupyterの導入とVS Codeでのノートの利用

Jupyterlabの導入


miniconda3を再導入したubuntuについて、jupyterをインストールして解析環境を作り直すことにした。condaのbase環境に対してインストールした。

$ conda install jupyterlab

モジュールについては、当面必要なものをインストールした。

$ conda install pandas numpy scikit-learn seaborn matplotlib


VS CodeでのJupyter 利用


VS Codeでは、ipynbを編集、実行することができるようになっている。これまではjupyterlabをブラウザで利用していたが、他のスクリプトを含めてVS Codeを使って解析することが多くなってきた。今後は、VS Codeで手元のクライアントからUbuntu機にssh接続して、ipynbを利用することになるだろう。

新しいipynbを作るときは、EXPLOREのNew File..で拡張子を.ipynbにすれば勝手にノートを作ってくれる。pythonのカーネルを右上のところから選択できるようになっていて、condaで導入したbaseのpythonを利用している。


参考

2023年3月3日金曜日

Useful jupyter lab extension: jupyterlab_templates

 I daily use the jupyter lab for data analysis workflow. Recently I updated the jupyter lab and I found that the jupyterlab extension manager can be available. Since then I frequently check the jupyterlab extension to make my jupyter environment more useful and productive. 

Jupyterlab Extension Manager -- T.Y. Blog

 There are some stuffs I always use in the notebook such as description markdown cells and import-modules cells for notebooks. Today I found an extension for jupyterlab that seems useful for setting up templates.

Jupyterlab_template

Support for jupyter notebook templates in jupyterlab

The readme can be available from the github page.



Installation

Installation guide is available in github README.md. Since I use conda for package management, I install jupyterlab_templates using  conda install 



(base) tk$ conda install -c conda-forge jupyterlab_templates

Retrieving notices: ...working... done

Collecting package metadata (current_repodata.json): done

Solving environment: done


## Package Plan ##


  environment location: /Path/to/miniconda3


  added / updated specs:

    - jupyterlab_templates



The following packages will be downloaded:


    package                    |            build

    ---------------------------|-----------------

    jupyterlab_templates-0.3.2 |     pyh1d7be83_0          72 KB  conda-forge

    ------------------------------------------------------------

                                           Total:          72 KB


The following NEW packages will be INSTALLED:


  jupyterlab_templa~ conda-forge/noarch::jupyterlab_templates-0.3.2-pyh1d7be83_0 



Proceed ([y]/n)? y



Then I install jupyterlab_templates on Extension Manager,




Configuration file

Next, adding parameters on  jupyter_notebook_config.py  is necessary to let jupyterlab find the template files in  template_dirs . The jupyter_notebook_config.py is a configration file of jupyterlab. If there is no file on your system yet, it can be generated by


(base) tk$ jupyter lab --generate-config

Writing default config to: /Path/to/home/.jupyter/jupyter_lab_config.py



I edited this file on the terminal to add three lines below,


c.JupyterLabTemplates.template_dirs = ['/Path/to/template/directory']

c.JupyterLabTemplates.include_default = True

c.JupyterLabTemplates.include_core_paths = True

^G Get Help       ^O WriteOut       ^R Read File      ^Y Prev Pg        ^K Cut Text       ^C Cur Pos        

^X Exit           ^J Justify        ^W Where is       ^V Next Pg        ^U UnCut Text     ^T To Spell       


Now the setup of this extension was completed.



Using template from Launcher

I restart the jupyter lab and found the template button was added on Launcher.




Then The preparation of jupyterlab_extension was completed. When clicking the "Template" on Notebook, I can choose the ipynb template on template directory.



Note that the updates of template directories and notebooks are not reflected until the system is once restarted. 



References

2023年3月1日水曜日

Jupyterlab Extension Manager

Extension manager is enable from jupyterlab

 Recently I updated the jupyter lab on my personal computer after a long time since the last execution. 


(base) tk$ conda update conda



Now my version of jupyter lab is 3.5.2. Before that the version was very older one and not a few stuff have been changed on the current version. Especially I found that the Extension manager available in the left of the window. 



Wow! 
I clicked the Enable button and many options available were shown.


It's quite easy to find the extensions from the list and inspect them. Click the blue links and I can open their Github pages. 

Kite is saying farewell


For example I searched Kite autocomplete extension using search box,



I clicked the 'Install' button and the message below was shown,



hmm, OK. 


(base) tk$ pip install jupyter-kite

Collecting jupyterlab-kite

  Downloading jupyterlab-kite-2.0.2.tar.gz (266 kB)




and I got an error message...


Oh, I see...


base) tk$ conda install -c conda-forge nodejs

Collecting package metadata (current_repodata.json): done

Solving environment: done


## Package Plan ##


  environment location: /path/to/my/home/miniconda3


  added / updated specs:

    - nodejs



The following packages will be downloaded:


    package                    |            build

    ---------------------------|-----------------

    libuv-1.44.2               |       hac89ed1_0         441 KB  conda-forge

    nodejs-18.13.0             |       hd0c9b3c_0        10.3 MB  conda-forge

    ------------------------------------------------------------

                                           Total:        10.8 MB


The following NEW packages will be INSTALLED:


  libuv              conda-forge/osx-64::libuv-1.44.2-hac89ed1_0 

  nodejs             conda-forge/osx-64::nodejs-18.13.0-hd0c9b3c_0 




and I retry the installation.


I restart jupyterlab, and I got the message,


The icon of Kite was shown, but I could not use Kite. I clicked the Fix This button,  and found the message below...!

Kite is saying farewell

From 2014 to 2021, Kite was a startup using AI to help developers write code. We have stopped working on Kite, and are no longer supporting the Kite software.

Thank you to everyone who used our product, and thank you to our team members and investors who made this journey possible.



Hmm... is this the reason I could not use Kite on jupyterlab??

I could not fix this problem, but I finished trying to install this extension...


(base) tk$ pip uninstall jupyterlab-kite

Found existing installation: jupyterlab-kite 2.0.2

Uninstalling jupyterlab-kite-2.0.2:



jupyterlab-cell-flash

I tried different jupyterlab extension. 




Looks nice! This extension works well :-)


Jupyterlab documentation:

2020年3月16日月曜日

JupyterLabでダークカラーのテーマが選べる

pythonスクリプトを書くときに、Jupyter notebookではなく、同じくanacondaのナビゲーターにあるJupyterLabを使うようになった。



操作感はJupyter notebookとあまり変わらないが、左側にスライダーがあり今いるディレクトリやラン中のノートなどがわかる。


使っていて気づいたのが、画面のテーマが通常の白地のものに加えて、ダークカラーの物が選べることだ。メニューのSettingsから、JupyterLab Themeで、JupyterLab Darkを選ぶと、黒を基調とする画面に変化する。


こうすると、夜に画面と睨めっこしなければならない場合などにだいぶん目に優しい。ちょっとしたことだが、気づいてよかった。便利なので使っていきたい。


2019年8月22日木曜日

Google ColabでJupyterを使ってみる

IT系のブログや記事を見ていると、開発や機械学習でGoogleのColaboratory (Google Colab)というサービスを利用することについて解説してあるのをよく見る。Colaboratoryは、個人が機械学習の教育や研究をするときに使える無償のツールで、Jupyterノートブックの環境として利用できる。googleのアカウントを持っている人だったら、そこから使うことができる。自分はsafariから使ってみたが、chrome, firefox、safariでは検証済みとのことだ。話題になっているのは、機械学習に必要なGPUなどのリソースを無償で使うことができるためのようだ(ただし、連続使用時間は12時間に制限されているらしい。暗号通貨採掘などに使われないようにするためのようだ)。Jupyterは自分で使う場合、pythonの他にRやBashなどのカーネルを利用するようにできるが、Colabではpython2と3のみ提供されている。将来的には他のカーネルを利用することができるようになるかもしれない。

自分のgoogleアカウントに行き、colabを立ち上げてみる。デザインは少し異なるが、Jupyterノートブックのファイルができるので、helloworldしてみる。




使い方はほとんどローカルのJupyterと同じだった。ファイルを保存するとgoogleドライブに.iynb形式でファイルが保存される。ファイルを利用するために、ドライブの設定で、Colabのところにチェックを入れた。


そうすると、利用するアプリとして、google colaboratoryが出てくるようになる。



 Markdownはテキストボックスという名前で利用できる。左側で入力すると、右側に成形された書式で出力される。


実行すると、以下のように出力される。


colabには、よく使われるライブラリがあらかじめ使えるようになっている。numpyをインポートして、行列の演算を行ってみた。


anacondaを導入しても、あらかじめ主要なライブラリを一緒にインストールすることができるが、colabは自分のパソコンで何もしなくてもだいたい必要なものが利用できるようになっているのは便利だと思った。さらに、機械学習などで利用されると思われるコードがあらかじめスニペットとして用意されているようだ。左側のメニューでコードスニペットを選んで、簡単にスニペットのセルをコピーすることができる。


実際に実行してみたところ。これは手間いらずだ。


googleドライブにアップロードしたファイルを参照するときは、ドライブをマウントする必要がある。やり方は簡単で、左側からドライブにマウントのボタンをクリックすると、必要なコードがコピーされ、それを実行すると以下のようになる。リンクをクリックして自分のドライブがあるアカウントにログインすると、キーが表示されるので、コピーして入力する。

以上の作業を行うと、/content/drive/My\ Drive/の下にgoogleドライブがマウントされる。一度colabのノートブックを閉じて再度開いたときもマウントされたので、一定期間はマウントされるのかもしれない。せっかくなので、ランダムドリフトの簡単なシミュレーションをして結果をプロットするスクリプトをアップロードして、colabで実行してみた。

見えているファイルplot.pyを実行する。pythonファイルを実行するときは、%run file.pyでいけるようだ。初期頻度や世代数、集団サイズ、試行回数を入力すると、以下のように結果がプロットされる。図では、初期頻度が0.5で、30世代100個体の集団を4回シミュレーションしている。


すごいサービスだと思った。何も設定しなくても、googleアカウントがあればすぐにJupyterノートブックを使うことができるし、機械学習などの勉強に必要なリソースが無償で利用できるのだから、使ってみる価値があると思った。大学の研究室でpythonを導入したり、学習したりする場合にも、環境の準備をほとんどすることなくいきなり始めることができるわけなので、導入の敷居はとても低くなる。とりあえず、研究室の研究なり学習でよく使うスクリプトなどをライブラリとしてアップロードした上で使えば、使用PCの条件なども問題にすることなく一斉に演習ができるというのは、かなり便利なのではないかと思う。




2019年5月2日木曜日

Jupyter notebookで、condaで作った環境を使えるようにする

Jupyter notebookを使って解析することで、一つ一つの解析の結果を見ながら次のコマンドを叩くことができる。また、標準出力などの結果を残すことができる。Markdownを使って解析についてのメモを残す事もできるため、解析の時の思考の流れも含めて記録を残すことができるのもとても便利だと思う。一つ一つのセルに分けて実行していくスタイルも、解析の1ステップを塊で捉えることができて分かりやすい。

こういった理由で、解析のときにJupyter notebookを多用するようになった。こちらの情報にもあるようにJupyterでbashやR、または異なるバージョンのpythonなど、複数のカーネルを使う事もできる。これをするとjupyterの中でRの統計検定や作図でデータを見てみることができるのでかなり便利だった。解析で複数のカーネルを使いたいときにも、ファイルを分けずに一つのnotebookにまとめることができる。

ただ、bashを使っているときに問題が起きた。anacondaでいくつか環境を作って、解析用のプログラムを導入していたが、condaの環境をJupyterでコマンドを打って変えようとしても上手く行かずに以下のようなエラーが出てしまう。
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init 

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.
色々調べていくと、condaで作成した仮想環境をJupyter Notebookで使えるようにする方法があったので設定した。
設定の概要は、
[1] condaの新しい環境を作る(既存環境が存在する場合は不要)
conda create -n newenv python=3.7
# この環境に入る
conda activate newenv

# jupyterをインストールする
pip install jupyter

# jupyter_environment_kernelsをインストールする
pip install environment_kernels
[2] Jupyterの設定ファイルを生成する。
jupyter notebook --generate-config

"""
この作業により、ホームディレクトリの下の.jupyterの下に
jupyter_notebook_config.pyというファイルができる。

この作業は一回やれば良い。2回目、3回目の環境の設定の時には必要ない
"""
[3] 設定ファイルに、自動でconda環境を読み込む設定を追記する。
エディタでファイルを開き、以下を追記する。
c.NotebookApp.kernel_spec_manager_class = 'environment_kernels.EnvironmentKernelSpecManager' 
c.EnvironmentKernelSpecManager.env_dirs=['/Users/username/anaconda/envs/']

これで、Jupyterを再起動すれば、カーネルとしてnewenvを選択することができるようになる。カーネルは、Kernel > Change kernelから選択できる。
注意点は、それぞれの環境を使えるようにしたければ、その環境でjupyterとjupyter_environment_kernelが入っていないといけないこと。それぞれの環境をactivateして、上の設定を行う。
こうすることで、解析や依存環境ごとに複数の環境を作っていても、それらをjupyterで使えるようになる。

参考:
https://kazusa-pg.com/jupyter-notebook-use-virtual-env-kernel/
https://qiita.com/yoppe/items/38005f415a5b8b884c7d