#!/usr/bin/env python # coding: utf-8 # # Scientific modules and IPython # Nikolay Koldunov # # # koldunovn@gmail.com # This is part of [**Python for Geosciences**](https://github.com/koldunovn/python_for_geosciences) notes. # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pylab as plt # ## Core scientific packages # When people say that they do their scientific computations in Python it's only half true. Python is a construction set, similar to MITgcm or other models. Without packages it's only a core, that although very powerful, does not seems to be able to do much by itself. # # There is a set of packages, that almost every scientist would need: # # # We are going to talk about all exept Sympy # ## Installation # Installation instructions can be found in the README.md file of this repository. Better to use [rendered version from GitHub](https://github.com/koldunovn/python_for_geosciences/blob/master/README.md). # # ## IPython # In order to be productive you need comfortable environment, and this is what IPython provides. It was started as enhanced python interactive shell, but with time become architecture for interactive computing. # ## Jupyter notebook # Since the 0.12 release, IPython provides a new rich text web interface - IPython notebook. Here you can combine: # #### Code execution # In[3]: print('I love Python') # #### Text (Markdown) # IPython [website](http://ipython.org/). # # List: # # * [Python on Codeacademy](http://www.codecademy.com/tracks/python) # * [Google's Python Class](https://developers.google.com/edu/python/) # # Code: # # print('hello world') # # #### $\LaTeX$ equations # $$\int_0^\infty e^{-x^2} dx=\frac{\sqrt{\pi}}{2}$$ # $$ # F(x,y)=0 ~~\mbox{and}~~ # \left| \begin{array}{ccc} # F''_{xx} & F''_{xy} & F'_x \\ # F''_{yx} & F''_{yy} & F'_y \\ # F'_x & F'_y & 0 # \end{array}\right| = 0 # $$ # #### Plots # In[4]: x = [1,2,3,4,5] plt.plot(x); # #### Rich media # In[5]: from IPython.display import YouTubeVideo YouTubeVideo('F4rFuIb1Ie4') # * [IPython website](http://ipython.org/) # * [Notebook gallery](https://github.com/ipython/ipython/wiki/A-gallery-of-interesting-IPython-Notebooks) # ## Run notebook # In order to start Jupyter notebook you have to type: # # jupyter notebook # # ### You can download and run this lectures: # Web version can be accesed from the [github repository](https://github.com/koldunovn/python_for_geosciences). # ## Main IPython features # ### Getting help # You can use question mark in order to get help. To execute cell you have to press *Shift+Enter* # In[36]: get_ipython().show_usage() # Question mark after a function will open pager with documentation. Double question mark will show you source code of the function. # In[38]: get_ipython().run_line_magic('pinfo2', 'plt.plot') # Press SHIFT+TAB after opening bracket in order to get help for the function (list of arguments, doc string). # In[ ]: sum() # ### Accessing the underlying operating system # You can access system functions by typing exclamation mark. # In[6]: get_ipython().system('pwd') # If you already have some netCDF file in the directory and *ncdump* is installed, you can for example look at its header. # In[7]: get_ipython().system('ncdump -h test_netcdf.nc') # ## Magic functions # The magic function system provides a series of functions which allow you to # control the behavior of IPython itself, plus a lot of system-type # features. # Let's create some set of numbers using [range](http://docs.python.org/2/library/functions.html#range) command: # In[9]: list(range(10)) # And find out how long does it take to run it with *%timeit* magic function: # In[10]: get_ipython().run_line_magic('timeit', 'list(range(10))') # Print all interactive variables (similar to Matlab function): # In[11]: get_ipython().run_line_magic('whos', '') # ### Cell-oriented magic # Receive as argument both the current line where they are declared and the whole body of the cell. # In[12]: get_ipython().run_cell_magic('timeit', '', 'range(10)\nrange(100)\n') # Thre are several cell-oriented magic functions that allow you to run code in other languages: # In[13]: get_ipython().run_cell_magic('bash', '', '\necho "My shell is:" $SHELL\n') # In[14]: get_ipython().run_cell_magic('perl', '', '\n$variable = 1;\nprint "The variable has the value of $variable\\n";\n') # You can write content of the cell to a file with *%%writefile* (or *%%file* for ipython < 1.0): # In[15]: get_ipython().run_cell_magic('writefile', 'hello.py', "#if you use ipython < 1.0, use %%file comand\n#%%file \na = 'hello world!'\nprint(a)\n") # And then run it: # In[16]: get_ipython().run_line_magic('run', 'hello.py') # The *%run* magic will run your python script and load all variables into your interactive namespace for further use. # In[17]: get_ipython().run_line_magic('whos', '') # In order to get information about all magic functions type: # In[18]: get_ipython().run_line_magic('magic', '') # ### Links: # [The cell magics in IPython](http://nbviewer.ipython.org/urls/raw.github.com/ipython/ipython/1.x/examples/notebooks/Cell%20Magics.ipynb)