3

As my notebook gets longer, I want to extract some code out, so the notebook would be easier to read.

For example, this is a cell/function that I want to extract from the notebook

def R_square_of(MSE, kde_result):
    # R square measure:
    # https://en.wikipedia.org/wiki/Coefficient_of_determination
    y_mean = np.mean(kde_result)
    SS_tot = np.power(kde_result - y_mean,2)
    SS_tot_avg = np.average(SS_tot)

    SS_res_avg = MSE
    R_square = 1 - SS_res_avg/SS_tot_avg

    return R_square

How can I do it effectively?

My thoughts:

  1. It's pretty easy to create a my_helper.py and put the code above there, then from my_helper import *
  2. The problem is that, I may use other package in the method (in this case, np, i.e. numpy), then I need to re-import numpy in my_helper.py. Can it re-use the environment created in ipython notebook, hence no need for re-importing?
  3. If I change the code in my_helper.py, I need to restart the kernel to load the change(NameError: global name 'numpy' is not defined), this makes it difficult to change code in that file.
Community
  • 1
  • 1
ZK Zhao
  • 19,885
  • 47
  • 132
  • 206

1 Answers1

4

Instead of importing your other file, you could instead run it with the %run magic command:

In [1]: %run -i my_helper.py

-i: run the file in IPython’s namespace instead of an empty one. This is useful if you are experimenting with code written in a text editor which depends on variables defined interactively.


I'd still take the opportunity to recommend writing the file as a proper python module and importing it. This way you actually develop a codebase usable outside of the notebook environment. You could write tests for it or publish it somewhere.

Kos
  • 70,399
  • 25
  • 169
  • 233
  • Yes, I think maybe a module is better, but I need to restart the kernel to commit the change. Is it possible to get rid of that? – ZK Zhao Jan 30 '16 at 11:30
  • 1
    The builtin function [reload](https://docs.python.org/2/library/functions.html#reload) may get you some way forward. – Kos Jan 30 '16 at 11:59
  • Find this, maybe helpful, http://stackoverflow.com/questions/5364050/reloading-submodules-in-ipython – ZK Zhao Jan 30 '16 at 12:11
  • 1
    After playing for a while, I think `module` + `autoreload` is the answer. – ZK Zhao Jan 30 '16 at 13:41