Pages

Friday, February 28, 2014

[Python] Cannot write data into multiple array

In this case, I created a 2-D array via numpy, initiated with 0, and update them with float number.
However, without any error message, all of them are 0.0.

It is because I initiated them array with 0 (int), and my data are always between 0 to 1 so that all of them are casted to 0.0 (float)

Make sure initiate your array with the same type you will use.

Below are some codes to generate the 2-D array and update them

multiDimArray.py
import numpy as np nCV = 5 nFeat = 3
# generate a 5-by-3 array initiate with 0.0 result_list = [[0.0 for j in range(0, nFeat)] for i in range(0, 5)] """ Below is what drove me crazy result_list = [[0 for j in range(0, nFeat)] for i in range(0, 5)] """ # convert to array via numpy result_array = np.array(result_list) print 'Before:', result_array # update (Python starts at 0) result[2][2] = 0.8 print 'After: ', result_array """ if you want to use list rather than array afterward, you can use either result_list = result_array.tolist() or result__list = list(result_array) """

Tuesday, February 25, 2014

More pain, more progress, and happiness in the end

經濟、愛情、親情、學業在毫無預警同時施壓的當下,仍在發展初期的新人格突然茁壯了。聯想到某說客對我說「你有著超齡的獨立」,我對曰「哈!我根本沒得選擇」不禁莞爾。

Being suffered by finance, love, parents, and study at the same time.  I will make all the obstacles my strength.

Sunday, February 23, 2014

[Linux] Some tips for screen

In my opinion, screen is like a CLI-version VNC or RDP, which means you can log in your previous window again using only ssh session.

Screen is especially useful if you connect the server via SSH and need to run a time-consuming process.  Just open a window, running the process on that window, then you can leave and log into the same window to continue you work.

In this post, I use 'window' rather than 'screen' to give you a clear image.  But technically, they are 'screen's by definition.

Show the window list
screen -ls

Create new window with given name
screen -S [window-name]

Attach to an unattached, or log in if it is attached, window
screen -r [window-name]

Attach to an attached window (multiple log in the same window)
screen -x [window-name]

Detach (leave but not kill) a window
screen -d [window-name]

Kill a detached window
screen -X -S [session # you want to kill] quit

Leave (and kill) the current window you are on
exit

Check whether you are on any window
echo $STY

if is is 'null', then you are not on any window.  You can also edit your shell to show that in your prompt:
~/.bashrc
# just showing that you are using screen or not if [ -n "$STY" ]; then export PS1="(screen) $PS1"; fi # or showing the window name # if [ -n "$STY" ]; then export PS1="($STY) $PS1"; fi

You can also use hot-keys, which I do not recommend to the beginners.  Check the reference for more detailed information:

References
http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
http://www.bartbania.com/index.php/linux-screen/

Tuesday, February 11, 2014

[Python] Dealing with the 'CSV new-line character seen in unquoted field error'

To load the Windows-imported csv files or any csv files which are generated via Excel without the error message:

_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

Just do as it said: use universal-newline mode, which means ‘U’ in the arguments.

Example:
import csv fin = open(‘test.csv’, ‘rU’) myReader = csv.reader(fin, delimiter = ‘,’)

Thursday, February 6, 2014

[Linux] Check the memory size in human readable format

Just a quick backup.

sudo lshw -businfo -C memory | awk '/System\ Memory$/{print $2}'


Alternative: Show the number of GB (only the number)
free -g | awk '/Mem:/{print $2}'

Saturday, January 18, 2014

[VirtualBox] Make SSH connections among the cluster of guest OS

To build up a distributed system, we need the network to connect the nodes.  However, sometimes I cannot have internet access, so I decided to build a local cluster in my laptop.  The first thing is to create a local network among the cluster.  I will list the minimum steps and briefly explain how it works.


Host OS: OSX 10.7
Guest OS: Ubuntu server 12.04

How to build up in-cluster network?

The spirit:
Create a network interface for in-cluster network

On every guest OS panel
Setting -> Network
Besides the first network card with NAT, add second network card with Bridged Adapter.
This will create a new network interface on your GUEST, say eth2

Edit the network interface configuration
Assume the interface is eth2, and we want to set the ip as 192.168.56.100

Add these
/etc/network/interfaces
auto eth2 #automatically activate this interface iface eth2 inet static #this is a static ip, not dhcp, nor loopback address 192.168.56.100#change the number 100 to whatever you want (less than 255) netmask 255.255.255.0

Restart the network process to read the new configuration.
sudo /etc/init.d/networking restart

You can also manually activate the interface by
sudo ifconfig eth2 inet 192.168.56.100

What to communicate with the host?

The spirit: Create another network interface on host, also create new network interface on guest.

Create new network interface on host
On the VirtualBox main panel
Preference -> Network -> Create a new adapter (e.g. vboxnet0)
This will create a new network interface on your HOST, say vboxnet
type ifconfig to check it
In this case, we have vboxnet (doesn't matter), and 192.168.56.1 as the ip

Create new network interface on guest
Similar to in-cluster network, but we choose 'Host-Only' here.

Conclusion

Use different types of network adapter (interface) for different types of communication.
guest-to-guest: Bridge-Adapter
guest-public (outside of the host): NAT
guest-host: Host-Only Adapter

Now, you supposed to be able to connect the host via 192.168. 56.1; You can also connect to the other node via 192.168.56.100

Friday, January 17, 2014

[Linux] Customize shell prompt


To change the shell prompt, change the variable $PS1 the .bashrc (depends on the shell you use)

\u    : user name
\h    : hostname
\w    : the current absolute path (use /W if you only want to relative path)
\n    : new line

To add color to the prompt

\e[   : start to add color
x;ym  : set the color to x;y
\e[m  : end to add color

another format to add color

For example, green string 'HelloWorld'






.bashrc
# the color code for green is 01;32m PS1='\e[01;32mHelloWorld\e[m'
Another example:
Green user name (spencer)
purple directory (~) in a pair of bracket ([XXXX])
ending up with a dollar sign ($) and leave a space.




.bashrc
PS1='\e[01;32m\u:\e[m\e[11;35m[\w]$\e[m ' # 01;32m = green, 11;35m = purple # use one pair of declaration for one color

Update Jan. 24, 2014:
It seems that using the purple color (11;35m) may result in a bug, choose another color, for example blue (01:34).