Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Sep 9, 2013

Python for Scientific Computing


The persuasion of a masters degree has for the last two years has exposed me to scientific computing on a regular basis. In light of that experience, scientific computing demands a distinct way of information representation, interpretation and processing. While general purpose programming languages and platforms are capable of getting the job done, a specialized scientific computing platform allows the scientist to be the scientist more often than being the programmer.

There are several scientific computing platforms.  MATLAB is perhaps the most widely known proprietary option. There are a few open source alternatives too, R is one example. My favorite used to be MATLAB. That changed about two years ago when I started working in Python.

In my experience the following list of Python packages and modules comprise of a satisfying scientific computing environment. The libraries are listed bellow:

Core Python Packages/Modules: 

sys

For passing command line arguments to your python script

time

For timestamping or for benchmarking

string

Read and write text in different formats including parsing

math

A very handy library for, obviously, mathematical operations. This library has several built in statistical functions too.

random

Allows creating of randomized samples from various probability distribution functions.

Third Party Packages/Modules

 

SciPy

A Python based ecosystem of open-source software for mathematics, science, and engineering.

NumPy

The fundamental package for scientific computing with Python. Linear algebra, N dimensional array etc. Member of the SciPy ecosystem.

Matplotlib

A library for producing publication quality 2D charts in Python. Can be integrated with GUI libraries like PyQt.

PyQt

Python bindings for a cross platform open source GUI toolkit called Qt. It comes with a standard set of widgets as well as the option of making custom ones. Meets the need for smart clean GUI for Scientific computing. PyQt has a good chance of becoming one of the leading GUI library for scientific and advanced computing. This video shows an impressive GUI for particle system in Maya.



PyQt GUI for Maya from Tim Withers on Vimeo.

Sep 4, 2013

Saving images of plots made in Matplotlib

Matplotlib, a versatile python library for 2D plotting, allows saving images in several bitmap formats as well as vector formats like SVG.To save an image, one has to specify output format first. Then simply calling a save image function gets the job done. The following example saves an image in SVG format, then in PNG.
from matplotlib import pyplot as plt #code for drawing the plot goes here plt.savefig("output.svg") plt.savefig("output.png")
As you can see, the library figures out the format of output from the extension of the output file name.

Jul 28, 2013

String representation of Python objects

It is often necessary to provide string representation of the instances of different classes. Application of this can be found in debugging, dumping in a file, communication across the network, etc. Several functions in the object class of Python can be overriden to output appropriate string representations. I'm listing a few bellow:

__str__()
returns an informal string representation of an object. Does not necessarily has to be a valid python expression.

__repr__()
this function can be overriden to output a valid Python string. This is called by the builtin repr() function which, returns the python representation of an object. Since this is often used in debugging, it is suggested to be information rich.

Jun 19, 2013

Static member in Python

Python has different syntax from most common OOP languages like C++ or Java. When declaring variables, the name is not followed by a type identifier or scope modifier. For creating a static member of a class, simply write it inside the class outside of the scope of member functions. To declare a variable belonging to instance, simply add self. prefix.
class Some_class(Name_of_super_class): static_field = 0.0 __init__() : self.variable_belonging_to_instance = 1.0 print("object of Some_class instantiated") @staticmethod def static_method() : print("static method called") static_return = 1.0 return static_return

May 30, 2013

Satisfying GUI needs in Python

There are a few GUI libraries that can satisfy the GUI need in Python. I have tried a couple of them so far.

First one I tried was Tk, an open source, cross-platform GUI toolkit. Tk was originally developed as an extension of the Tcl scripting language. But modern Tk supports many languages, including Python. Using Tk was simple and straight forward. But the rapid learning curve of Tk quickly led me to a dead end which made me decide against using it in my current project. The low level graphics programming in Tk is done on Canvas. I needed to draw semitransparent shapes on the canvas. But using RGBA color is not supported in Tk. So I decided to move to another GUI library.

The second one I tried was Qt. Several licensing exists for Qt at the moment and its history is complicated. It can be safely said, Without getting into details, that there is a GNU GPL 3.0 licensed version of Qt is available. For using with Python, PyQt4 can be downloaded and installed.
sudo apt-get install python-qt4
Working with Qt also turns out to be pretty straight forward and simple. This framework also supports building of custom widgets and use of RGBA color is also supported. That's all I needed for the time being. I'm a happy coder now.

Dec 2, 2012

Calling C functions from Python

Python is capable of calling functions from a C library. Say, we have the following C code which needs to be used in a python script. I'm using Ubuntu 12.04 with python 2.7.
int add(int a, int b) { return (a+b) ; }
First the C code has to be compiled as a shared library.
$ gcc -fpic -c c_code.c $ gcc -shared -o c_code.so c_code.o
Then the shared library has to be copied to /lib/x86_64-linux-gnu/
$ sudo cp c_code.so /lib/x86_64-linux-gnu/
The following python script imports the shared library and invokes functions from it.
from ctypes import cdll,c_int lib = "c_code.so" dll = cdll.LoadLibrary(lib) add = (lambda x,y: dll.add(c_int(x), c_int(y))) print(add(1,1)) print(add(2,3))

Oct 13, 2012

Matplotlib


Quoting from the Matplotlib website,

matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. matplotlib can be used in python scripts, the python and ipython shell (ala MATLAB®* or Mathematica®†), web application servers, and six graphical user interface toolkits.
It allows generation of publication ready plots and charts from a python script. Just started using this in one of my current projects and finding it satisfactory so far. The matplotlib figures can also be exported to a number of bitmap formats and vector formats like PDF. A definite advantage compared to MATLAB.

I'm using Ubuntu these days, to install Matplotlib in your Ubuntu box:

1. Ubuntu software centre,
or
2. Install it using apt
$ sudo apt-get install python-matplotlib

Oct 3, 2012

Python in Ubuntu

My favorite development environment for Python in Ubuntu is Eclipse. The following steps describe how to get an up and running development environment on your Ubuntu box.

1. Python

Python usually comes with the bundle but just in case:
$ sudo apt-get install python

2. Eclipse

Eclipse is available from the Ubuntu Software Center, I usually install it from there that. Another option is to install it manually, which I never tried myself.
$ sudo apt-get install eclipse sun-java6-jdk $ sudo udate-java-alternatives -s java-6-sun

3. Pydev

  1. Run Eclipse
  2. Help -> Install New Software
  3. Type in "Work with": http://pydev.org/updates, name should be Python
  4. Select Pydev from the list of available options, then click Next
  5. Select the Python module that needs to be installed, click Next
  6. Let the downloading finish, then restart Eclipse