デバグ debug

一方研究室で作成した関数でエラーが出る場合には,それを修正するのに,関数の使い方を修正するだけですむ場合と,関数自体を編集して修正しなくてはならない場合とがある.後者の場合は,編集するためには,コピーする必要があるので,その関数を自分のディレクトリにコピーした上で修正しよう.修正ができたら,元々のファイルを管理している教員に連絡して,教員がその編集したファイルを元々のディレクトリにコピーするか,あるいは編集を元のファイルに取り込む.関数の中身やなにをやっているかが分からなければ教員に聞ける.

デバックの基本は,まずなにが起こっているかを正確に把握することだ.たとえば,変数の型が違うというメッセージが出たら,その型が本来は何であるはずで実際には何なのかを知る必要がある.変数が期待と大きく違うヘンな値になっていたら,どこからヘンな値になってしまったのかを知る必要がある.この実態を知るには,見延は次の4つの方法を使っている.

    1. 標準のデバッガー pdbを使う.pdfの簡単な見延作成の解説がここにある.pdfは関数の中の変数の情報が得ることができる.

    2. ipython -i スクリプト名またはipythonの中でrun スクリプト名でスクリプトを動かして,スクリプトが止まった後にインターラクティブに変数や配列をチェックする.

  1. print文をスクリプトに挿入する.

    1. データを描画して調べる.

研究室外の第三者が作成した関数の中でエラーが生じた場合には,エラーの原因は関数に間違いがあるよりも,関数の使い方を間違ったか関数の使用条件に合わない使い方をしてしまった可能性が高い.後者はたとえばnanを許容していない関数でnanを含んだデータを入力して,途中でエラーになってしまうといった場合だ.そこで,使い方が正しいのか,関数の使用条件を満たしているのかをチェックしよう.万一エラーの原因は関数に間違がある場合には,それを修正することは多くの場合現実的はないだろう.

デバグは,作成したスクリプトの間違いを修正する作業である.間違いには,以下の三通りがある.

    1. 文法エラー.スクリプトの文法が間違っている.通常pythonインタープリターがどういったエラーであるかと何行目でエラーが出たかを示してくれる.修正は容易.

    2. 実行時に検出されるエラー.たとえば,変数の型が合わない,配列のサイズが合わない,関数の引数が合わない,ゼロで割った,関数がnanを許さないのにnanを含んだデータが与えられた,などのエラーは実行時に検出されて,どういったエラーであるかと何行目でエラーが出たかを示してくれる.修正はだいたい容易.

    3. 実行時にも検出されないエラー.スクリプトが正常終了したのに,結果がおかしい.修正の難易度は中から高度.

エラーメッセージが出ているのに対処が分からないときは,教員に正確にエラーメッセージを伝えて,対処を知らないかを教えてもらうとよい.正確に伝えるには,コピペがおすすめ.「エラーが出たんですけれど」と言われてもアドバイスの出しようがない.

Debug is to correct errors in script. There are three categories of errors.

    1. Syntax errors. Script grammar is wrong. It usually indicates what kind of error the python interpreter is and what error occurred on what line. Correction is easy.

    2. Errors detected at runtime. For example, an error such as the type of a variable does not match, the size of an array does not match, the argument of a function does not match, divided by zero, data including nan is given even though the function does not allow nan. It is detected at the time of execution, and it shows what kind of error it is and what line the error has come out. Correction is rather easy.

    3. Errors not detected even at run time. The script ended normally, but the result is wrong. Difficulty of correction is moderate or high.

If you do not know how to deal with an error message, you can ask advice to your mentor. It is important to let them know exact error message by copy & paste of that message. If you just say "I got an error in this script", you mentor cannot give you a good advice.

The basics of debugging is to grasp exactly what is happening. For example, if you get a message saying that the variable type is wrong, you need to know what it is supposed to be and what it really is. To understand what is happening, there are four methods

    1. Use standard debugger pdb. Minobe wrote a simple explanation of pdb here.

    2. Run a python script with "ipython -i script-name" or "run script-name" in an ipython session, and then check variables and arraysinteractively after the script stops.

    3. Put print statements in many places.

    4. Use integrated development environment like PyCharm.

If errors occur in functions that are made outside of our laboratory, it is more likely that you did some mistake for the use of the function or your way of use is outside of what is expected for that function. So, you can check how to use the function rather than debugging it. Even if error comes from the mistake of the developer of that function, it is not practical for us to repair that mistake.

Errors that occurs inside of functions that were made in our laboratory there are two possibilities of cause of errors. The first is how to call it is wrong, and this can be corrected without editing the function itself. The second is an error in the function, and to correct this error one needs to edit the function. In this case, you will copy that function into your directory (so that you can edit), and do debug that function. After you correct the problem, you will tell what is the cause of the error to your mentor. The mentor will copy your edited version of file to the original directory or incorporate your editing to the original function. If you need to know some details about the function, you can ask your mentor.