Sep 14, 2013

Difference Between Update and Upgrade in Ubuntu

Update :
Updates the list of available packages and their versions, but it does not install or upgrade any packages.
apt-get update


Upgrade :
Installs newer versions of the packages based on the updated list of available packages.
apt-get upgrade

After updating the lists, the package manager would know about available updates for the software already installed in a computer. This is why running update prior to upgrade is recommended.

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.

Jul 1, 2013

Creating GIF animation in Ubuntu

From the manual of 'covert'
The convert program is a member of the ImageMagick(1) suite of tools. Use it to convert between image formats as well as resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more.
The above quoted paragraph is an understatement of the abilities of convert. Besides many amazing acts, Convert is capable of creating of animated GIF images. To do so, type the following in terminal:
$ convert -delay 100 -loop 0 image*.png animation.gif
For more on ImageMagick, I recommend this blog post.

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))

Nov 30, 2012

Fixing broken merge list in Ubuntu

Sometime broken merge list poses a barricade on the path of further updates. To fix this problem, the old (flawed) merge list has to be removed first, then a new one has to be created. In Ubuntu 12.04, the following worked for me.
sudo rm /var/lib/apt/lists/* -vf sudo apt-get update

Nov 7, 2012

Opening a Terminal Anywhere in Ubuntu

Being able to open a terminal from any location on the file system tree (even when you're logged in on a remote server) is a very useful feature. Installing nautilus-open-terminal provides this option in the context menu. To install,
$ sudo apt-get install nautilus-open-terminal
Natuilus file system needs to be reset after installation. To do so,
$ nautilus -q
Right click to bring up the context menu and be happy to see the 'Opne in terminal' option :)