Pages

Sunday, September 14, 2014

Hold on and waiting for the clouds with silver lining

It is not homesick, I'm just sick of the assignments.  Anyway, just hold up, everything will become great in the end, and every cloud has silver lining, I just need to work harder and smarter to see them.

沒想到我也會有生平無大志,只求六十分的一天啊。

現在放棄的話,就要回到神豬當首都市長的小島了喔^.<

想到這裡我都醒了,繼續撐著,總有一天會看到鑲銀邊的雲朵的吧!


Tuesday, July 29, 2014

[轉錄] 我要我們在一起

很有感觸的文章,全文出處
http://www.cheers.com.tw/article/article.action?id=5023891&page=1

我要我們在一起

我要我們在一起
圖片來源:王竹君
在「工作」衝撞「愛情」的交互作用中,什麼會引爆最強烈的化學反應?那就是「外派」。
陳敏慧與吳俊賢這對夫妻,兩個人都是外商圈中極優秀的經理人。面對這個全球化潮流下不可擋的工作趨勢,他們的因應之道跌破很多人的眼鏡──不僅是「全家出征」,「工作到哪裡,家就在哪裡」,而且一般人想當然爾「夫唱婦隨」的模式,到了他們身上完全相反,變成了「婦唱夫隨」。
2000年,當時在台灣擔任L'OREAL化妝品香水事業部副總經理的陳敏慧,決定接下外派到L'OREAL巴黎總公司的任務。
而一句法文也不會的吳俊賢,放下台灣花旗銀行消費金融部貸款業務處副總裁的職務,帶著2個分別還在念幼稚園、小學1年級的孩子,跟著太太去旅行,當起全職的「家庭主夫」。
吳俊賢還記得,同事知道他向公司提出留職停薪的理由後,第一個反應都是「哇!」:「本來他們都不知道我太太是誰,一下子全部都知道了。」
爬到再高,頭上都有一個老闆
事實上,要放下前途、暫停腳步,談何容易?更何況是對一個事業有成的男性?吳俊賢並不是一開始就能夠這麼灑脫。
加入L'OREAL集團前,陳敏慧曾有過幾次外派機會。當時吳俊賢一心想在工作上衝刺,所以給太太的建議是「妳自己去」,他自己與小孩則留在台灣繼續奮鬥。
但是陳敏慧拒絕了。如果家庭必須兩地分離,她寧願放棄升遷。從此,「不管在哪裡,全家在一起」,逐漸成為彼此的共識。
也許是因為對妻子始終懷抱一份歉疚,2000年初,當陳敏慧在年度績效考核上,主動寫下想去巴黎歷練的意願時,這一次吳俊賢表達了支持。
然而有兩件事,還是完全出乎他們的意料之外:第一,沒想到3個月後,公司的外派指令真的就下來了;第二,吳俊賢本來也想向花旗申請外派到歐洲,但當時就是沒有理想的工作可以配合,他不得不以留職停薪的方式,暫時中斷生涯。
面對四周關切的眼光,吳俊賢說,上司、朋友都好應對,最大的壓力來自於父母。他們根本沒辦法想像兒子帶小孩、不工作的狀態,更擔心他失業。
可是吳俊賢對工作的想法倒發生了轉變。剛出社會時,他對成功的詮釋,也是「能爬到多高的位子」。隨年紀漸長,吳俊賢卻慢慢體會到:「再高,每個人頭上還是有一個老闆。」為了事業犧牲家庭,談不上真正的成功。
於是,2000年8月7日,這一家人浩浩蕩蕩出發,踏上異鄉的旅程。
怎麼苦,當中都帶著甜
他們在巴黎展開全然不同的生活:媽媽照常上下班,但是變成爸爸清早負責買早餐,接送小孩。
「當家庭主夫也很忙,」吳俊賢笑著說。白天小孩上學後,他學習法文,或騎著腳踏車四處探索城市風光,等下午4點小孩回家,他得一直忙到他們上床睡覺,才有時間溫習自己的法文功課。
因為遠離故鄉,「家」的感覺反而分外強烈。本來在台灣很少一起吃晚飯的雙薪家庭,互相扶持,克服各種生活調適與文化挫折,變得更親密。「再怎麼苦的時候,當中都帶著甜,」陳敏慧回憶。
儘管如此,吳俊賢誠實說,還是有徬徨的時候。
有時候他一個人坐在公園裡,看見一群老太太在餵鴨子,心底會浮起一種奇特的感受:「怎麼我也是其中之一?」想到自己的下一步,「那種掙扎是一直存在的,」吳俊賢描述。
在巴黎的點點滴滴,後來能夠成為全家人最幸福、珍貴的記憶,陳敏慧跟吳俊賢都同意,其中不乏幸運的成份。
譬如2002年,L'OREAL集團因為買下植村秀品牌,希望陳敏慧執掌管理,所以她比原定計畫的3年時間,更早調回台灣,吳俊賢也順利回到原來的工作崗位。
又譬如,假使外派地點不是巴黎,少了強大的文化魅力,也許兩人就會遲疑。
但他們真正令人欣羨的部份,也許不是好運,而是那份互相「成全」的心意。
吳俊賢與陳敏慧是小學同學,大學畢業也一起到國外留學。「她拿獎學金,我是自費去,」吳俊賢說。他從來都知道,工作與成就對太太多麼重要,即使有一天她的光芒可能超越自己。
還有面對風險的態度。
「既然是共同的決定,決定了就往前走,」吳俊賢認為。不管結果是否如同預期。
回國後,吳俊賢與陳敏慧,連同2個小孩,將這段過程寫成一本小書《那2年,我們全家在巴黎》。有了第一次的經驗,陳敏慧說,說不定3年後,她又會再度考慮外派。
但是這一次,她會換成配合先生。「這次我要跟你一起去,」她轉頭對吳俊賢笑著承諾。

Tuesday, July 15, 2014

感恩

感恩喔感恩喔!我已經比我以前所預想的還要幸福,還要進步很多了。放輕鬆一點,幼稚一點吧!事情本來就不是想得夠清楚,做得多努力就保證一定會有更好的結局的阿!傻傻的誤打誤撞說不定可以看到些其他的東西。

Saturday, July 12, 2014

手段

其實我還是會有失望的時候,然後我就會想:我是不是真的太好相處太好說話,面對自己人的時候總是太好欺負太好凹,使得讓人誤解我是有所圖而委曲求全、死纏爛打,而不是掏心掏肺、毫無保留地將對方的事當成自己的事甚至比自己更重要?是不是真的必須要有些手段,才不會常常被人當做犯賤沒個性?才不會吃力不討好,反而弄得別人認為是我太任性,而我也因為配合得很辛苦卻又太心疼對方而時時壓下自尊與情緒?

我想,這可能也是我愛恨分明的原因吧!除了坦蕩面對自己不怕誤解以外,我其實把從親友們身上得到的委屈和無力感都吸收儲存了下來,如果能短暫地就經由自己的性格去消解的話就好;如果不能,那就找機會轉嫁到我可以合理生氣的敵人身上,算他倒楣,而且干我屁事,我打你幹嘛怕你會痛?你哪位阿?

還...還是我本來就是個超難相處又沒個性的死爛人呢? 這應該也是很有可能的事情。

嗯...改天再想辦法調整吧!

Wednesday, July 9, 2014

[Android] Get access to external SD card

Have you ever used Environment.getExternalStorageDirectory()?  During the past, I had used it to get access to my SD card and it worked great!  However, some Android smartphones nowadays (2014) have a large internal storage and another external SD card slot.  When I use Environment.getExternalStorageDirectory() on these phones, it sometimes returns me the internal one, which is not what I want.

Below is the official document for Environment.getExternalStorageDirectory()
Return the primary external storage directory.

Hmmm...It says it will return external storage, great!  But, it actually returns the PRIMARY external storage, which is sometimes the embedded/internal storage

So, how can I get the path of my SD card without hardcoding the path into my codes?  Below is a naïve way:
String SD_PATH; if(Environment.isExternalStorageRemovable()){ SD_PATH = Environment.getExternalStorageDirectory().toString(); } else{ SD_PATH = System.getenv("SECONDARY_STORAGE"); } String primaryStoragePath = Environment.getExternalStorageDirectory().toString();

As you can see, the concept is quite so simple: If your primary storage is removable (not embedded), then it is a SD card for real.  Otherwise, we will search your second storage assuming there is no other storage available.


Tuesday, July 1, 2014

不留遺憾

終於和她見了面,很自私的說出了這十多年來從沒對她,或其他人說的話(或說:傾倒壓力在這無辜的人身上)。過程中,即使我極力保持冷靜並收斂情緒,但在某些時候,還是讓我真正的感受到了什麼叫做「語帶哽咽」。原來那竟是一種沒辦法控制的生理反應:情緒波動感覺起來是平穩的,但雙唇一啟卻總有股強烈的壓力由胸腔一路擠迫到喉頭;於是,為了不將此壓力化作情緒並藉由雙眼與鼻腔發洩(即一般典型的泣訴),選擇了以話不成句的方式,在每一字之間強壓那在喉之重,命早已紊亂不堪的言語儘速逃脫。

談話結束後,我沒有太大的情緒變化,也沒有很強烈的如釋重負的感覺;一切都這麼的自然,這麼的如我預期;畢竟我們從沒有發展過感情,何來失戀?只不過是老朋友間普通的談話、告別罷了;身體上的疲累應該只是交通的緣故,不礙事。第二天依舊起了個大早,自己準備了早餐、打掃了家裡、幫寵物洗個澡,然後開始打開電腦作事,一直到了下午約一點我才恍然發現我竟什麼事都沒有做、連打混摸魚都沒有,就只是茫然的看著程式碼、看累了就趴著休息,就這樣虛度到了下午;胡亂吃了點午餐後,不只沒收拾(要知道,我開伙可是先收完才開動的人),連下午預定要去的約也就算了。哈!原來躺在床上聽著雨聲「耍廢」也蠻舒服的嘛!

再隔天,回到工作崗位了。良好的工作環境與談話,工作效率似乎有恢復了一點,很快的找到了預計在週末就要解決的問題,至於無心治本應該只是出至於惰性與不必要吧!只是,依然心神不寧,腦袋一直對著「虛無」空轉。是的!我一直在想著東西,而那東西我唯一的形容方式就是「什麼都沒有」,我只好隨著靈魂的意思任它行動並趁早下班以獨處並休息。果不其然,在一陣的放空發呆以後,我癱軟在床上沈沈睡去,在進入夢鄉前的腦中最後訊息我記得是

「那你現在應該沒有失眠了吧!」
「嗯阿!太忙了,時間跟體力都不夠用了。」

在做了場美夢醒來之後,夢中依人已然不是故人,而我在夢中卻笑得更燦爛。這時我才恍然大悟兩件事。第一:原來那陣虛無是因為心理的寄託被掏空了,過往那可笑至極的執念讓我在徬徨之際至少有個寄託,而當我真正解脫了以後,那些力氣突然撲了個空;這個概念大概類似於武俠小說裡面那種「如果他沒了心中報仇的對象,那他生存的動力還剩下些什麼?」吧!原來,我不只是身體遲鈍,連情感上的傷也很遲鈍地到第三天才發現,哈!我想,我只是需要時間,讓心理重新找到真正的依靠,或是讓情感再鈍化一些。第二:我似乎在保護機制的啟動下,將這股執念開始轉移了,使得感情突然放重了。這不是個好現象,這是種有如白血病般,為了防禦外在侵略大量增生卻反而自傷的症狀;我不想要在目前就放得這麼重,我想再放輕鬆一點、再放淡一些,不要超過七分,一切自然、正常、隨緣就好。

不過呢!可以解開這十多年來那無形的結,其實是個很大的解脫,這樣我才能勇敢而無畏地面對現在、或是以後會遇到的人,應該比較不會像過去一樣每次都那麼難看了吧(遮臉)。另外,和她見面以後記錄這過程也是有目的的,如果我連對她都說不出這些私密的話,那我要如何對其他重要的人敞開心胸呢?更甚者,我想試試看言語不帶太多保留,赤裸裸的顯示自己會有什麼事情發生,在這應對的過程中,我想我會收獲不少。

最後,我必須誠實的表示,我不是為了誰而要與她作個結束,我最主要的目的就只是為了不讓自己後悔罷了,其他的都在其次,以此為證,以防我日後腦袋錯亂隨便唬爛。

我失戀了,但我也復原了,我現在想好好的、輕鬆的交朋友。

Thursday, June 26, 2014

不必二選一

多了寄託,多了方向,長年來的空缺開始注入活水,即便那只是道娟娟細流,即使這一切極大可能只是虛幻;即便我知道那其實是這一路上所遇過的人的幫助與體諒,以及我每次都敗得非常難看,但卻總是屢敗屢戰所開闢出來的渠道,心中仍是感謝。好一陣子沒有這麼輕鬆的感覺了,雖然日後我可能沒辦法從這字裡行間確切體會出我現在的心靈狀態;但我認為,我能藉由記錄強化我當下的感受,就活在當下吧!


這種感覺是眉目俱笑、是怡然自得、是悠然自得不患得患失(好啦!偶而還是會有),是開心卻不是狂喜、是掛念卻不是踮念、有份量卻如清風一般不留痕跡,渴望坦承也贊成神祕。我真的很享受這種感覺,如果你們發現了什麼,能不能當做不知道,給我點空間呢?

其實在我第一次意會到的時候,我是很排斥的;我很怕會因為這樣而分心,甚至產生牽掛使得我未來的路出了差錯,我不想要有任何變數發生,我放不開。只是,我突然發覺到這種想法真的很幼稚,誰說念書和交朋友一定是會互相排斥的呢?就算會,現在說這也太找了吧!我在擔心什麼阿?這種想太多的心態才是真的會讓我兩者皆空停滯不前的毒瘤,我應該要放輕鬆、讓一切順其自然發展就好,不管會有怎樣的結局,不要讓自己後悔就好。


只願接下來的日子裡,一切隨緣、正常、自在。

大道不移,兩不捨。

Tuesday, June 24, 2014

[Android] Error inflating class fragment


We have a project that will contain several fragments in one activity, we compile and deploy it to a device without any error message.  However, runtime exception occurs when we launch the App, even though we just declare a fragment and not use it yet.



At the first glance, I would consider that it is caused by the fragment class.  But it is actually caused by the XML syntax while declaring the fragment component in the layout file of my main activity.  See the rectangular area

android.view.InflateException: Binary XML file line #25: Error inflating class fragment

What happened?




It is due to the line 27 in the editor, which uses capital characters (even though it is exactly the same with the real java file we are going to use).  All I need to do it change them with all lower characters (it is legal to use capital ones in the rear).  That is to say, we revise it as

android:name="com.example.soundclassifier2.MusicFragment"

And everything works now!

[Android] Missing AppTheme (styles)

Although I use git to include most of the resources and libraries for deploying my Android project, sometimes I get stuck when importing my own project among different machines, even in the same OS.

There is a typical one:
in your res/values/styles.xml

error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.




It is because I did not include a library appcompat_vXXX correctly.  The fastest way to include it, though locally (you have to do it once on every machine), is load an suitable appcompat_vXXX project on your machine.


Right click on your project -> Properties -> Android -> (scroll down) Library



Then you will see an item X ../appcompat_vXXX in the window.  All we have to do is click the 'Add...' button to select one of them (in my case: appcompat_v7_4), and click 'Apply' and 'OK'.




If there no any appcompat_XXX available, then you have to download them via Android SDK Manager and download 'Android Support Library' first.


You can see more details in
http://developer.android.com/tools/support-library/index.html

Thursday, May 8, 2014

[Android] Run C/C++ codes on Android–Android NDK

Before Start

When we develop Android projects, Java codes can handle most of the cases.  However, we may need to run C/C++ codes under some circumstances:


  1. Some of your methods have performance issues.
  2. Re-use some open-sourced codes written in C/C++.
  3. The methods written in C/C++ handle CPU-intensive tasks such as signal process, machine learning algorithms, etc.


Again, running C/C++ codes on Android does NOT guarantee performance improvement, it somethings make your application slower.  Only transform the methods that are really CPU-intensive.


What is Android NDK 


Android NDK stands for Android Native Development Kit.  As Java run the codes in the JVM, no matter what the hardware is; however, it does matter to C/C++ codes, which need to be compiled in different ways (implicitly) according to the hardware architecture before we can run it, that is why we call it "native".

How it work


  1. Install the NDK to compile the C/C++ code to native libraries
    https://developer.android.com/tools/sdk/ndk/index.html
  2. Declare an "interface" as a bridge from C/C++ functions to Java methods
  3. Declare the Java methods in your application, then use it like (most) normal Java methods


Case study

Instead of using a "HelloWorld" for demonstration, I would like to trace a open-source project for your reference, and that is how I learned by the way.

Libsvm-androidjni -- LIBSVM for android jni environment
https://github.com/cnbuff410/Libsvm-androidjni

Libsvm is one of the most powerful tools for SVM and widely used in many classification problems in the world, and it is definitely CPU-intensive.  To make this tutorial simple, I just introduce the svm-train function here in a bottom up approach (target the C/C++ codes first, then the interface and how to call the Java methods and so on).

Original source code
/jni/src/svm/svm-train.cpp
/jni/src/svm/svm-train.h

The entry in the svm-train.cpp file is

int svmtrain(int argc, char **argv)
that is the function we want to link to Java

Interface C/C++ code
/jni/src/train.cpp
/jni/src/train.h

int train(const char *trainingFile, int kernelType, int cost, float gamma, int isProb, const char *modelFile)

This function receive some parameters from another interface C/C++ code, and call the svmtrain(int argc, char **argv) in the original source code to do the real work.

Interface C/C++ (2)
/jni/jni/info_kunli_androidlibsvmexample_AndroiLibsvmExampleActivity.cpp
This is an unusual C/C++ that really link the native codes (C/C++) to you Android (Java) code

static jint trainClassifier(JNIEnv *env, jobject obj, jstring trainingFileS, jint kernelType, jint cost, jfloat gamma, jint isProb, jstring modelFileS) { jboolean isCopy; const char *trainingFile = env->GetStringUTFChars(trainingFileS, &isCopy); const char *modelFile = env->GetStringUTFChars(modelFileS, &isCopy); int v = train(trainingFile, kernelType, cost, gamma, isProb, modelFile); env->ReleaseStringUTFChars(trainingFileS, trainingFile); env->ReleaseStringUTFChars(modelFileS, modelFile); return v; }

This is the native function that will be compiled and imported to the Android Java codes.  It receives the parameters from your other Java codes, and pass them to int train() in the interface C/C++ function mentioned above.

Three rules observed here

  1. Besides the real parameters, please add JNIEnv *env and jobject obj as the first two parameters
  2. Add j in front of type identifier; use Array instead of [].  For instance, int becomes jint, int[] becomes jintArray
  3. Use objectArray for any multi-dimensional array.  For instance both int[][] and float[][] will become objectArray
  4. Convert the strings before and after use them.

static JNINativeMethod sMethods[] = { /* name, signature, funcPtr */ {"trainClassifierNative", "(Ljava/lang/String;IIFILjava/lang/String;)I", (void*)trainClassifier}, };
Declare the real type of parameters in the native function.  That is, the real type that are accepted in the compiled native method in your Java codes depend on this statement.

Put the parameters in the () pair, followed by the return value type

Several mappings here
[type in Java]:[identifier here]
int: I
float: F
double: D
String: Ljava/lang/String; (do not forget the semicolon)
float[][]: [[F
int[][]: [[I

For example:
float HelloWord(int i, String s1, String s2, int[][] arrayI) becomes
"(ILjava/lang/String;Ljava/lang/String;[[I)F"

Register this interface and native methods
int register_Signal(JNIEnv *env)
Change the second parameter in static int jniRegisterNativeMethods() with this file's title (without the extension).  For example:
int register_library(JNIEnv *env) { return jniRegisterNativeMethods(env, "info/kunli/AndroidLibsvmExampleActivity", sMethods, sizeof(sMethods) / sizeof(sMethods[0]));
}

Again, you must replace "info/kunli/AndroidLibsvmExampleActivity" to the file name where you register the library.

Link the interface java codes with Java Virtual Machine 
onload.cpp


Makefile of the NDK
jni/src/Android.mk

LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_C_INCLUDES += $(JNI_H_INCLUDE) LOCAL_SRC_FILES := \ jni/onload.cpp \ jni/info_kunli_androidlibsvmexample_AndroidLibsvmExampleActivity.cpp \ src/train.cpp \ src/svm/svm-train.cpp \ src/svm/svm.cpp LOCAL_NDK_VERSION := 4 LOCAL_SDK_VERSION := 10 LOCAL_MODULE := libsignal LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY)

Call NDK to compile the cpp files into a native library (*.so)
libsignal is the name of compiled native library here

jni/src/Application.mk
Set up the hardware architecture

Call NDK to compile the library
ndk-build clean
ndk-build


Overview of the native library building process (top-down)
NDK configuration
Android.mk
Application.mk

Linking JVM to native library
onload.cpp

Set up the interface between Java and native codes
info_kunli_androidlibsvmexample_AndroiLibsvmExampleActivity.cpp

Call the native function as Java methods in the Android project
AndroidLibsvmExampleActivity.java


Note:
If you are familiar with libsvm, you probably found that the lasted version when I am writing this post (May/2014) does not fully support the original LIBSVM well.  That is why I want (have) to learn how the NDK work so that I can expand it via the interface codes (not the original C/C++ codes).

Saturday, May 3, 2014

Less is more

"Less is more", it is a well known saying, which I have been eager to understand the meaning.  Recently, I found that this is a concept with multiple aspects, and I figured out one of them.

I am always a hard-working person, and I have been suffered from enormous stuffs I am working on.  In the past, I had spent more and more time on working even than holidays; however, spending more time on working does not always work.  In my personal experience, allocating more time for working somehow does not work!  Why? It turns out that I would assuming I still have much time to finish the works, resulting the decrease of efficiency.

Fortunately, I have found such a hazard.  The solution is: Do not expect extra time (e.g. weekend and night) and have some recreational activities on holidays to strike a balance between work and life.  With a tight schedule,  I have to finish the tasks within the weekdays and the normal working hours; therefore, both my throughput and happiness increase due to the fact that I have better working efficiency and relax.

"Less is more" means that spending less time on working brings more throughput and pleasure.

Wednesday, April 30, 2014

重回軌道了

總算總算阿~我各項要做的事終於又回到穩定發展的軌道上了。

專業上,開始能夠複習程式開發方面的技能;網球方面開始能放鬆的打球,而不是緊繃的只練某些固定的弱點項目,即使各項技能仍有很大的進步空間;作息也開始慢慢回復到早睡早起的模式了,我記得這幾乎是一年多以前的事情了,哈哈!

最重要的我想就是人際方面了!現在的我,可以在依然真誠待人的心境下,而能保有自我的空間,一步一步在實現「邀請對方進入我的世界,而不是一味的付出」,這樣的相處模式有什麼好處呢?一言難盡。但我想,最大的好處就是不會再患得患失,心境可以常保平靜卻又不致於對世間的感情覺得冷漠,我喜歡這種感覺。

終於又感到在成長了呢! <3

Friday, April 11, 2014

放鬆

我好像知道什麼叫做醉成屍體的感覺了。


我的身體跟脾氣的僵硬度基本上我已經放棄去挽救了(sign),不過今天倒是在一陣小沮喪和放鬆下(這啥詭異的順序?),整個肩膀像融化般的化在床上了;身體的疲累是還好,我甚至還是有意識的(所以才能記下這感覺),但卻隱約感受到,原來我不只是騙過身體的疲累,似乎心理上的壓力其實有一部分也被蓋下了,而我以為那是我小時候才會作的事。


幹!我真的越來越不像人類了,從「我身體叫我休息」繼續突變到「我的心理叫我休息,雖然我不是很想」。



好吧!以理智來思考的話,這種現象其實是可以解釋的,第一個可能是我把各種敏感度不知不覺中越調越低了,以致外界引發的負面影響無論大小我都只能感覺到「還好」;第二個就是我只是個很帥的北七,三不五時不知所云而已。

對了,因為我沒當過真的屍體,今天也沒喝酒,這是比喻而已,筆記一下以防以後忘記。

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