<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <center> <h1>Python in the Lab</h1> </center>
Topics
Python 
Control Flow
Data Structures
Modules and Packages
Object-oriented programming
Iterators, Generators
Decorators
Magic Methods
Context Manager
All the other cool stuff
Science
Plotting
Numerical Calculations
Reading/Writing Data (Txt, CSV, Binary, HDF, ...)
Fitting
FFT
Symbolic Math
Plotly
Pymeasure
Tools
IPython and Jupyter
Spyder IDE
pip/PyPI and conda
Git and Github
virtualenv
Be Pythonic
Zen of Python
PEP 8
Docstrings and Spinx
Ducktyping
Design Patterns
Functional programming
Unicode and UTF-8
Speed
Numpy
Cython
Python
<a href='http
Step1:  Documentation
Quickstart
Video Tutorial with Notebooks
Step2:  <a href="http
Step3:  <i>"NumPy is an extension to the Python programming language, adding <b>support for large, multi-dimensional arrays and matrices</b>, along with a large library of <b>high-level mathematical functions</b> to operate on these arrays.
Using NumPy in Python gives <b>functionality comparable to MATLAB</b> ..."</i>, Wikipedia
* Documentation
* Quickstart
* Video Tutorial with Slides
Step4:  <a href="http
Step5:  <i>matplotlib is a <b>plotting library</b> for the Python programming language and its numerical mathematics extension NumPy.
There is also a procedural "pylab" interface ..., designed to closely resemble that of MATLAB.</i>
Documentation
Step6:  <a href='http
Step7:  <i>"SciPy contains modules for <b>optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers</b> and other tasks common in science and engineering."</i>, Wikipedia
Documentation
Book
Step8:  "SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible." SymPy
<a href='http
Step9:  <a href='http
Step10:  <i>"HDF5 is a <b>data model, library, and file format</b> for storing and managing data. It supports an <b>unlimited variety of datatypes</b>, and is designed for <b>flexible and efficient I/O and for high volume and complex data</b>. HDF5 is <b>portable and is extensible</b>, allowing applications to evolve in their use of HDF5. The HDF5 Technology suite includes tools and applications for managing, manipulating, viewing, and analyzing data in the HDF5 format."</i>, HDF Group
Documentation
Quickstart
Video Introduction
Step11:  <a href='http
Step12:  <i>"Cython is a compiled language that generates <b>CPython extension modules</b>. These extension modules can then be loaded and used by regular Python code using the import statement.
It works by producing a standard Python module. However, the behavior differs from standard Python in that the module code, originally written in Python, is translated into C. While <b>the resulting code is fast</b>, ..."
</i>, Wikipedia
Documentation
Video Tutorial with Slides
Step13:  Examples
Typical imports
Step14:  Simple Plot
Step15:  Interactive
Step16:  Subplots
Step17:  Fitting
<ASSISTANT_TASK:>
Python Code:
import IPython
IPython.__version__
Explanation: <center> <h1>Python in the Lab</h1> </center>
Topics
Python 
Control Flow
Data Structures
Modules and Packages
Object-oriented programming
Iterators, Generators
Decorators
Magic Methods
Context Manager
All the other cool stuff
Science
Plotting
Numerical Calculations
Reading/Writing Data (Txt, CSV, Binary, HDF, ...)
Fitting
FFT
Symbolic Math
Plotly
Pymeasure
Tools
IPython and Jupyter
Spyder IDE
pip/PyPI and conda
Git and Github
virtualenv
Be Pythonic
Zen of Python
PEP 8
Docstrings and Spinx
Ducktyping
Design Patterns
Functional programming
Unicode and UTF-8
Speed
Numpy
Cython
Python
<a href='http://www.python.org'>
    <img src="images/python_logo.png", align='left'>
</a>
Documentation
<b>Online Course: Python 3 Tutorial (English and German)</b>
Online Book with some Videos: Automate the Boring Stuff with Python.
Online Book and interactive Tutorial: How to Think Like a Computer Scientist: Learning with Python 3
PyCon: Videos of 2015, 2014, 2013, ...
SyiPy: Videos of 2015,  2014, 2013, ...
EuroPython: Videos of 2015, 2014, ...
Scientific Python Distributions
All distributions provide the same functionality.
<a href="https://winpython.github.io/">
    <img src="images/winpython_logo.png", align="left">
</a>
portable
Do not download the Qt5 version at the moment.
<a href="https://www.continuum.io/">
    <img src="images/anaconda_logo.png", align="left">
</a>
platform independent (Window, Linux, Mac)
<a href='https://www.enthought.com/products/canopy/'>
    <img src='images/canopy_logo.png', align='left'>
</a>
platform independent (Window, Linux, Mac)
Wissenschaftliche Pakete
<a href='http://ipython.org/'> 
    <img src="images/ipython_logo.png", align="left"*>
</a>
End of explanation
from IPython.display import YouTubeVideo
YouTubeVideo("05fA_DXgW-Y")
Explanation: Documentation
Quickstart
Video Tutorial with Notebooks: IPython & Jupyter in depth: high productivity interactive and parallel python, PyCon 2015
Book: IPython Interactive Computing and Visualization Cookbook, Packt Publishing (2014)
End of explanation
import numpy
numpy.__version__
Explanation: <a href="http://numpy.org/">
    <img src="images/numpy_logo.jpg", align="left">
</a>
End of explanation
from IPython.display import YouTubeVideo
YouTubeVideo("1zmV8lZsHF4")
Explanation: <i>"NumPy is an extension to the Python programming language, adding <b>support for large, multi-dimensional arrays and matrices</b>, along with a large library of <b>high-level mathematical functions</b> to operate on these arrays.
Using NumPy in Python gives <b>functionality comparable to MATLAB</b> ..."</i>, Wikipedia
* Documentation
* Quickstart
* Video Tutorial with Slides: Introduction to NumPy, SciPy 2015
* Book: Numpy Beginner's Guide - Third Edition, Packt Publishing (2015)
End of explanation
import matplotlib
matplotlib.__version__
Explanation: <a href="http://matplotlib.org/">
    <img src="images/matplotlib_logo.png", align="left">
</a>
End of explanation
from IPython.display import YouTubeVideo
YouTubeVideo("MKucn8NtVeI")
Explanation: <i>matplotlib is a <b>plotting library</b> for the Python programming language and its numerical mathematics extension NumPy.
There is also a procedural "pylab" interface ..., designed to closely resemble that of MATLAB.</i>
Documentation: Guides, FAQ, Resources, API, ...
Gallery: Unterschiedlichste Plots mit Code Beispielen
Quickstart
Video Tutorial with Notebooks: Anatomy of matplotlib, SciPy (2015)
Buch: Mastering matplotlib, Packt Publishing (2015)
End of explanation
import scipy
scipy.__version__
Explanation: <a href='http://www.scipy.org'> 
    <img src="images/scipy_logo.png", align="left">
</a>
End of explanation
from IPython.display import YouTubeVideo
YouTubeVideo('0CFFTJUZ2dc')
Explanation: <i>"SciPy contains modules for <b>optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers</b> and other tasks common in science and engineering."</i>, Wikipedia
Documentation
Book: Learning SciPy for Numerical and Scientific Computing Second Edition, Packt Publishing (2015)
<a href='http://pandas.pydata.org/'>
    <img src='images/pandas_logo.png', align="left">
 </a>
<i>"Pandas is a software library written for the Python programming language for <b>data manipulation and analysis</b>. In particular, it offers <b>data structures and operations for manipulating numerical tables and time series.</b>"</i>, Wikpedia
Documentation
Quickstart
Video Tutorial with Notebooks: Analyzing and Manipulating Data with Pandas, SciPy (2015
)* Book: Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython, O'Reilly and Associates (2012)
End of explanation
from IPython.display import YouTubeVideo
YouTubeVideo('Lgp442bibDM')
Explanation: "SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible." SymPy
<a href='http://www.sympy.org'>
    <img src='images/sympy_logo.png', align='left'>
</a>
Documentation
Quickstart
Video with Slides: SymPy, SciPy (2014)
End of explanation
import h5py
h5py.__version__
Explanation: <a href='http://www.h5py.org'>
    <img src='images/hdf_logo.jpg', align='left'>
</a>
End of explanation
from IPython.display import YouTubeVideo
YouTubeVideo('nddj5OA8LJo')
Explanation: <i>"HDF5 is a <b>data model, library, and file format</b> for storing and managing data. It supports an <b>unlimited variety of datatypes</b>, and is designed for <b>flexible and efficient I/O and for high volume and complex data</b>. HDF5 is <b>portable and is extensible</b>, allowing applications to evolve in their use of HDF5. The HDF5 Technology suite includes tools and applications for managing, manipulating, viewing, and analyzing data in the HDF5 format."</i>, HDF Group
Documentation
Quickstart
Video Introduction: HDF5 is Eating the World, SyiPy (2015)
Book: Python and HDF5, O'Reilly Media (2013)
End of explanation
import Cython
Cython.__version__
Explanation: <a href='http://cython.org/'>
    <img src='images/cython_logo.jpg', align='left'>
</a>
End of explanation
from IPython.display import YouTubeVideo
YouTubeVideo('gMvkiQ-gOW8')
Explanation: <i>"Cython is a compiled language that generates <b>CPython extension modules</b>. These extension modules can then be loaded and used by regular Python code using the import statement.
It works by producing a standard Python module. However, the behavior differs from standard Python in that the module code, originally written in Python, is translated into C. While <b>the resulting code is fast</b>, ..."
</i>, Wikipedia
Documentation
Video Tutorial with Slides: Cython: Blend the Best of Python and C++, SciPy 2015 
Book  Cython, A Guide for Python Programmers, O'Reilly Media (2015)
End of explanation
# Import necassary packages
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# Set up matpltolib for nice plotting in the notebook
%matplotlib notebook
plt.style.use('seaborn-notebook')
Explanation: Examples
Typical imports
End of explanation
x = np.linspace(0, 4 * np.pi, 200)
rad = x / np.pi
plt.figure(figsize=(12, 3))
line, = plt.plot(rad, 2 * np.sin(x))
plt.ylim(-5, 5)
plt.grid()
plt.tight_layout()
Explanation: Simple Plot
End of explanation
from ipywidgets import interact
@interact(a=(1, 4), f=(0.1, 2, 0.1), phi=(0, 2, 0.1))
def update(a=2, f = 1, phi=0):
    line.set_ydata(a * np.sin((x + phi * np.pi) / f))
Explanation: Interactive
End of explanation
x = np.linspace(0, 4 * np.pi, 200)
rad = x / np.pi
#Create some noise
noise = 0.75 * np.random.randn(x.size)
# Define differnt harmonic functions
y0 = 1.0 * np.sin(x + 0) + noise
y1 = 1.5 * np.sin(x + np.pi / 2) + noise
y2 = 2.5 * np.sin(x + np.pi) + noise
# Plot everything
fig, axs = plt.subplots(3 , 1, figsize=(12, 6))
axs[0].plot(rad, y0, 'b.')
axs[0].set_xticks([])
axs[1].plot(rad, y1, 'g.')
axs[1].set_xticks([])
axs[2].plot(rad, y2, 'k.')
axs[2].set_xlabel('x / 2$\pi$')
for ax in axs:
    ax.set_ylim(-5.5, 5.5)
plt.tight_layout(h_pad=0)
Explanation: Subplots
End of explanation
# Define the fit function
def sin(x, a, phi):
    return a * np.sin(x + phi)
# Find the fit parameters
(a0, phi0), *err = curve_fit(sin, x, y0)
(a1, phi1), *err = curve_fit(sin, x, y1)
(a2, phi2), *err = curve_fit(sin, x, y2)
# Plot fits into subplots
axs[0].plot(rad, sin(x, a0, phi0), 'r--', lw=3, label='${:.2f} \cdot Sin(x + {:.2f}\pi$)'.format(a0, phi0 / np.pi))
axs[1].plot(rad, sin(x, a1, phi1), 'r--', lw=3, label='${:.2f} \cdot Sin(x + {:.2f}\pi$)'.format(a1, phi1 / np.pi))
axs[2].plot(rad, sin(x, a2, phi2), 'r--', lw=3, label='${:.2f} \cdot Sin(x + {:.2f}\pi$)'.format(a2, phi2 / np.pi))
for ax in axs:
    ax.legend(loc=4)
Explanation: Fitting
End of explanation
<END_TASK>