1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Watson Studioの環境のライブラリをカスタマイズする

Last updated at Posted at 2022-07-19

Watson Studioはチームメンバーと同じライブラリの環境を使えることがメリットですが、デフォルトのライブラリでは不足したり、バージョンを上げたいことがあります。

そのため、環境をカスタマイズする機能があります。

  • テスト環境
    • CP4D 4.0.9

新規環境定義

プロジェクトの「環境」タブで「新規環境定義+」をクリックします。

image.png

名前をつけ、ベースとなる環境として「IBM Runtime 22.1 on Python 3.9」を選びました。
なお、vCPUやRAMの量も指定が可能です。

image.png

デフォルトで導入されているパッケージは「ソフトウェア構成の詳細」に書かれています。
カスタマイズする場合には「作成」のリンクをクリックします。
image.png

condaとpipでパッケージを追加、修正可能です。
重要なことは#に書かれているように「dependencies:」のコメントを外すことです。
また、condaの場合は半角空白2つのインデントと「-」ハイフン、
pipの場合は半角4つと「-」ハイフン
の指定が必要なことです。

以下の例では、ライブラリのカスタマイズを行っています。

  • condaでpython=3.9.12にアップデート
  • condaでpandas-profiling=2.9を導入
  • pipでsklearn-pandas==2.2.0を導入

image.png

# Modify the following content to add a software customization to an environment.
# To remove an existing customization, delete the entire content and click Apply.
# The customizations must follow the format of a conda environment yml file.
# 次のコンテンツを変更して、ソフトウェアのカスタマイズを環境に追加します。
# 既存のカスタマイズを削除するには、コンテンツ全体を削除して[適用]をクリックします。
# カスタマイズは、conda環境のymlファイルの形式に従う必要があります。

# Add conda channels below defaults, indented by two spaces and a hyphen.
# 2つのスペースとハイフンでインデントされたデフォルトの下にcondaチャネルを追加します。
channels:
  - defaults

# To add packages through conda or pip, remove the # on the following line.
# condaまたはpipを介してパッケージを追加するには、次の行の#を削除します。
dependencies:

# Add conda packages here, indented by two spaces and a hyphen.
# Remove the # on the following line and replace sample package name with your package name:
# ここに、2つのスペースとハイフンでインデントされたcondaパッケージを追加します。
# 次の行の#を削除し、サンプルパッケージ名をパッケージ名に置き換えます。
#  - a_conda_package=1.0
  - python=3.9.12
  - pandas-profiling=2.9


# Add pip packages here, indented by four spaces and a hyphen.
# Remove the # on the following lines and replace sample package name with your package name.
# ここに、4つのスペースとハイフンでインデントされたpipパッケージを追加します。
# 次の行の#を削除し、サンプルパッケージ名をパッケージ名に置き換えます。
  - pip:
#    - a_pip_package==1.0
    - sklearn-pandas==2.2.0

カスタマイズ環境でnotebookを作成

「資産」タブで「プロジェクトに追加+」をクリックし、「Notebook」を選びます。
image.png

「ランタイムの選択」で先ほど作った「python3.9custom」を選び、Notebookを作成します。
image.png

インスタンス化時にカスタマイズを適用します。
image.png

カスタマイズの確認

カスタマイズのログは/var/ibm/logs/customization-YYYYMMD_HHMMSS.logに書き出されます。(時間はGMTです)

image.png

/var/ibm/logs/customization-20220719_014957.log
Customizations to apply in Python-3.9:
--------------------------------------------------------------
# Modify the following content to add a software customization to an environment.
# To remove an existing customization, delete the entire content and click Apply.
# The customizations must follow the format of a conda environment yml file.
# 次のコンテンツを変更して、ソフトウェアのカスタマイズを環境に追加します。
# 既存のカスタマイズを削除するには、コンテンツ全体を削除して[適用]をクリックします。
# カスタマイズは、conda環境のymlファイルの形式に従う必要があります。

# Add conda channels below defaults, indented by two spaces and a hyphen.
# 2つのスペースとハイフンでインデントされたデフォルトの下にcondaチャネルを追加します。
channels:
  - defaults

# To add packages through conda or pip, remove the # on the following line.
# condaまたはpipを介してパッケージを追加するには、次の行の#を削除します。
dependencies:

# Add conda packages here, indented by two spaces and a hyphen.
# Remove the # on the following line and replace sample package name with your package name:
# ここに、2つのスペースとハイフンでインデントされたcondaパッケージを追加します。
# 次の行の#を削除し、サンプルパッケージ名をパッケージ名に置き換えます。
#  - a_conda_package=1.0
  - python=3.9.12
  - pandas-profiling=2.9


# Add pip packages here, indented by four spaces and a hyphen.
# Remove the # on the following lines and replace sample package name with your package name.
# ここに、4つのスペースとハイフンでインデントされたpipパッケージを追加します。
# 次の行の#を削除し、サンプルパッケージ名をパッケージ名に置き換えます。
  - pip:
#    - a_pip_package==1.0
    - sklearn-pandas==2.2.0
--------------------------------------------------------------

+ conda env update -f /var/ibm/custom/conda-env-update.yml
+ local cmd=env
+ case "$cmd" in
+ __conda_exe env update -f /var/ibm/custom/conda-env-update.yml
+ __add_sys_prefix_to_path
+ '[' -n '' ']'
++ dirname /opt/conda/bin/conda
+ SYSP=/opt/conda/bin
++ dirname /opt/conda/bin
+ SYSP=/opt/conda
+ '[' -n '' ']'
+ PATH=/opt/conda/bin:/opt/conda/envs/Python-3.9/bin:/opt/conda/condabin:/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/ibm/dsdriver/bin
+ export PATH
+ /opt/conda/bin/conda env update -f /var/ibm/custom/conda-env-update.yml
Warning: you have pip-installed dependencies in your environment file, but you do not list pip itself as one of your conda dependencies.  Conda may not use the correct pip to install your packages, and they may end up in the wrong place.  Please add an explicit pip dependency.  I'm adding one for you, but still nagging you.
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... done

Downloading and Extracting Packages
pip-22.1.2           | 2.5 MB    | ########## | 100% 
tangled-up-in-unicod | 689 KB    | ########## | 100% 
pandas-profiling-2.9 | 168 KB    | ########## | 100% 
imagehash-4.2.1      | 285 KB    | ########## | 100% 
visions-0.5.0        | 45 KB     | ########## | 100% 
ibm-wsrt-py39t1-keep | 2 KB      | ########## | 100% 
ibm-wsrt-py39t1-main | 2 KB      | ########## | 100% 
confuse-1.5.0        | 23 KB     | ########## | 100% 
python-3.9.12        | 19.1 MB   | ########## | 100% 
certifi-2022.6.15    | 153 KB    | ########## | 100% 
phik-0.11.2          | 538 KB    | ########## | 100% 
missingno-0.4.2      | 15 KB     | ########## | 100% 
openssl-1.1.1q       | 2.5 MB    | ########## | 100% 
ca-certificates-2022 | 124 KB    | ########## | 100% 
sqlite-3.38.2        | 1.0 MB    | ########## | 100% 
htmlmin-0.1.12       | 24 KB     | ########## | 100% 
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
Installing pip dependencies: ...working... Ran pip subprocess with arguments:
['/opt/conda/envs/Python-3.9/bin/python', '-m', 'pip', 'install', '-U', '-r', '/var/ibm/custom/condaenv.89b_y40_.requirements.txt']
Pip subprocess output:
Collecting sklearn-pandas==2.2.0
  Downloading sklearn_pandas-2.2.0-py2.py3-none-any.whl (10 kB)
Requirement already satisfied: pandas>=1.1.4 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from sklearn-pandas==2.2.0->-r /var/ibm/custom/condaenv.89b_y40_.requirements.txt (line 1)) (1.3.4)
Requirement already satisfied: scipy>=1.5.1 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from sklearn-pandas==2.2.0->-r /var/ibm/custom/condaenv.89b_y40_.requirements.txt (line 1)) (1.7.3)
Requirement already satisfied: scikit-learn>=0.23.0 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from sklearn-pandas==2.2.0->-r /var/ibm/custom/condaenv.89b_y40_.requirements.txt (line 1)) (1.0.2)
Requirement already satisfied: numpy>=1.18.1 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from sklearn-pandas==2.2.0->-r /var/ibm/custom/condaenv.89b_y40_.requirements.txt (line 1)) (1.20.3)
Requirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from pandas>=1.1.4->sklearn-pandas==2.2.0->-r /var/ibm/custom/condaenv.89b_y40_.requirements.txt (line 1)) (2.8.2)
Requirement already satisfied: pytz>=2017.3 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from pandas>=1.1.4->sklearn-pandas==2.2.0->-r /var/ibm/custom/condaenv.89b_y40_.requirements.txt (line 1)) (2021.3)
Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from scikit-learn>=0.23.0->sklearn-pandas==2.2.0->-r /var/ibm/custom/condaenv.89b_y40_.requirements.txt (line 1)) (2.2.0)
Requirement already satisfied: joblib>=0.11 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from scikit-learn>=0.23.0->sklearn-pandas==2.2.0->-r /var/ibm/custom/condaenv.89b_y40_.requirements.txt (line 1)) (0.17.0)
Requirement already satisfied: six>=1.5 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from python-dateutil>=2.7.3->pandas>=1.1.4->sklearn-pandas==2.2.0->-r /var/ibm/custom/condaenv.89b_y40_.requirements.txt (line 1)) (1.15.0)
Installing collected packages: sklearn-pandas
Successfully installed sklearn-pandas-2.2.0

done
#
# To activate this environment, use
#
#     $ conda activate /opt/conda/envs/Python-3.9
#
# To deactivate an active environment, use
#
#     $ conda deactivate


The customization log file is:
/var/ibm/logs/customization-20220719_014957.log

conda listで確認します。指定したバージョンになっています。

!conda list|grep python
biopython                 1.78             py39h7f8727e_0  
ipython                   7.29.0           py39hb070fc8_0  
ipython_genutils          0.2.0              pyhd3eb1b0_1  
mysql-connector-python    8.0.28                   pypi_0    pypi
python                    3.9.12               h12debd9_0  
python-clang              10.0.1          default_h1154ca9_0    open-ce
python-dateutil           2.8.2              pyhd3eb1b0_0  
python-flatbuffers        2.0                pyhd3eb1b0_0  
python-graphviz           0.19                     pypi_0    pypi
!conda list|grep pandas-profiling
pandas-profiling          2.9.0              pyhd3eb1b0_0  

pip listで確認します。

!pip list|grep sklearn-pandas
sklearn-pandas                2.2.0

起動済み環境のカスタマイズの反映

一度環境ランタイムを起動した後に、さらにカスタマイズをした場合にはすでに起動した環境ランタイムを停止する必要があります。
「環境」タブから「アクティブな環境ランタイム」から起動した環境ランタイムを選び、「停止」で止めます。

image.png

参考

Watson Studioの環境をカスタマイズする - Qiita

CP4DのWatsonStudioでメモリやCPUを増やしてJupyterNotebookを起動する - Qiita

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?