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

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

Ubuntu Installer for Windows

Ubuntu 12.04 (the latest version at the time of writing this) has an installer for windows which can be downloaded from here. It allows you to download the installer as a windows executable (.exe) and perform the installation like installing many other windows applications. Thanks, Ubuntu.

Mar 31, 2012

Using LaTeX in Windows 7

Despite being a supporter and believer of software freedom, often times I've to compromise and use proprietary software. Since many of the people I have to collaborate and communicate with are completely running on proprietary or mixed platform like me, its a good idea to have a list of open source and proprietary software combinations of choice.

My favorite combination of software tools for using LaTeX in Windos 7 is MikTex, LEd. MikTex is the core LaTeX engine, can be downloaded for free from the miktex project's website. MikTex comes with it's own editor, but I prefer LEd since it allows arranging source files into projects. LEd can be downloaded from here.

For the MikTeX+LEd combination, MikTeX must be downloaded and installed before LEd since it uses the MikTex engine for compiling documents.

Mar 27, 2012

Matlab Cell Array

Cell arrays is a tabular data structure in Matlab, useful for storing heterogeneous objects whereas arrays can hold only homogeneous objects. Elements in cell array can have different dimensions as well. Elements of cell array, called cells, can contain
  • numeric arrays
  • strings
  • structures
  • cell arrays
Following example code creates and populates a cell array :

A = {['1st element'] [2 3] [8 9 7; 1 2 3] ['a' 'b']};


Index is written within curly braces e.g. A{1} to access the elements of a cell array. To recursively display the contents of a cell array, celldisp function can be used. For example, celldisp(A) produces the following result


A{1} =

1st element


A{2} =

2 3



A{3} =

8 9 7
1 2 3



A{4} =

ab



The contents of a cell array can be visualized with cellplot function, cellplot(A) would produce the following visualization.



This entry on mathworks blog can be consulted for farther information.

Mar 17, 2012

Useful keyboard shortcuts for Sciplore Mindmapping

This post will be expanded / edited as I find more and more useful keyboard shortcuts

Graphical link : Crtl + L
Select two nodes in the order (source, destination)


Move a node : Crtl + Arrow
Moves the node one higher/lower along the tree or shifts it in the ordered list of nodes in same level

New sibling node after selected one : Enter

New sibling node before selected one : Shift + Enter

New child node : Insert

Feb 13, 2012

Sciplore MindMapping

Sciplore MindMapping is an excellent software tool for researchers. Besides the ability to create mind maps of your brainstorming sessions, it allows linking nodes of the mind map with PDF files in your computer, import book-marks and references stored in BibTex format. Recommended for researchers in all scientific disciplines.

What started as a research project by the PhD students Bela Gipp and Jöran Beel at the University of Magdeburg, Germany in 2008 is a full fledged and still growing software tool and platform assisting research in many fields. Here is a little introductory video from their website.

Dec 23, 2011

Sea Turtles on Malaysian Bank Notes

From the website of Central Bank of Malaysia
As ambassadors of the rich and colourful marine life found in our tropical waters, two of the most well-known species of sea turtles endemic to Malaysian waters are on the new RM20 banknote - the Hawksbill Turtle (Eretmochelys imbricata) and Leatherback Turtle (Dermochelys coriacea).

Jul 26, 2011

Algorithm for Handedness Detection of Fiddler Crabs

Members of the genus Uca are generally known as Fiddler crabs for the overgrown claw of the males. The following algorithm detects a male fiddler crabs handedness from a photograph. The algorithm was implemented using Matlab R2009a.


Jul 23, 2011

JTP Turtle Talk (4)

Part 1
Part 2
Part 3


Meet Jo, a 5 year old green sea turtle that we take care of. Jo was born in our hatchery in 2006. When we went to release her in the sea with her other brothers and sisters, she was found running in circles on the sand because of a weak arm. That's when we realized that something is wrong with her and a close inspection also revealed that she has no eyes. She was born this way probably due to some kind of genetic or developmental anomaly from mistreatment of the egg while transfering from the original nest. A turtle without eyes will not survive in the ocean or on land. This is just natures way of making sure that only the fittest individuals survive. So by keeping her in our sanctuary we are not tampering with the natural course here.