Showing posts with label Jupyter. Show all posts
Showing posts with label Jupyter. Show all posts

Friday, July 24, 2015

Making a Plumbum Autoload Extension for IPython

I recently decided to try my hand at making an auto-load extension for Python and plumbum. I was planning to suggest it as a new feature, then I thought it might be an experimental feature, and now it's just a blog post. But it was an interesting idea and didn't seem to be well documented process on the web. So, here it is.

The plan was to make commands like this:

>>> from plumbum.cmd import echo
>>> echo("This is echoed!")

or

>>> from plumbum import local
>>> echo = local['echo']
>>> echo("This is echoed!")

into this:

>>> echo("This is echoed!")

Thereby making Plumbum even more like Bash for fast scripting.

Uncertainty Extension for IPython

Wouldn't it be nice if we had uncertainty with a nice notation in IPython? The current method would be to use raw Python,

In [1]:
from uncertainties import ufloat
print(ufloat(12.34,.01))
12.340+/-0.010

Let's use the handy infix library to make the notation easier. We'll define |pm| to mean +/-.

Note: this is a very simple library that is less than a page long. Feel free to write the code yourself (as I do later in this notebook).

Wednesday, July 22, 2015

Plumbum Color

I've been working on a color addition to Plumbum for a little while, and I'd like to share the basics of using it with you now. This library was originally built around a special str subclass, but now is built on the new Styles representation and is far more powerful than the first implementation. It safely does nothing if you do not have a color-compatible systems (posix + tty currently), but can be forced if need be. It is included with Plumbum, so you don't have to add a requirement for your scripts that is non-essential (as color often is). It is integrated with plumbum.cli, too. Also, I've managed to accelerate the color selection algorithims about 8x, allowing near game-like speeds. (see the fullcolor.py example).

Tuesday, July 7, 2015

Setting up Blogger for IPython notebooks

I found that setting up Blogger to show IPython HTML was not very well covered for IPython 3.0. To get it to work, I added the following to my template's HTML, right after the head tag:

Simple Overloading in Python

This is intended as an example to demonstrate the use of overloading in object oriented programming. This was written as a Jupyter notebook (aka IPython) in Python 3. To run in Python 2, simply rename the variables that have unicode names, and replace truediv with div.

While there are several nice Python libraries that support uncertainty (for example, the powerful uncertainties package and the related units and uncertainties package pint), they usually use standard error combination rules. For a beginning physics class, often 'maximum error' combination is used. Here, instead of using a standard deviation based error and using combination rules based on uncorrelated statistical distributions, we assume a simple maximum error and simply add errors.

To implement this, let's build a Python class and use overloading to implement algebraic operations.

In [1]:
import unittest
import math

The main rules are listed below.

If $C=A+B$ or $C=A-B$, then $$ \delta C = \delta A + \delta B. \tag{1} $$

If $C=AB$ or $C=A/B$, then $$ \delta C = C_0 \left( \frac{\delta A}{A_0} + \frac{\delta B}{B_0} \right). \tag{2} $$

Following from that, if $C=A^n$ then $$ \delta C = A_0^n \left( n \frac{\delta A}{A_0} \right). \tag{3} $$