Sunday, July 8, 2018

Taking control of fcitx - Dvorak, Chinese (Chewing) and Hangul

輸入法的問題真的很麻煩。像我需要打中英文,可是我英文鍵盤配置是 Dvorak,最近也在用Duolingo學韓文所以需要打韓文,可是fcitx輸入hangul會跟 dvorak 打架

https://github.com/fcitx/fcitx-hangul/issues/6

fcitx 的開發人也不是很想解決這些的問題

而且fcitx其實也很buggy切換輸入法用ctrl-space沒有辦法從 A->B->C->A 而是 A->B->A->B 或是 A->C->A->C …

昨天終於飽受了fcitx的缺陷,就用 bash 寫一個簡單腳本去控制輸入法的切換。fcitx 有 fcitx-remote

概念是這樣:

首先要打開 ~/.config/fcitx/profile

EnabledIMList=fcitx-keyboard-us:True,fcitx-keyboard-us-dvorak:True,chewing:True,hangul:True,pinyin:False,shuangpin:False,fcitx-keyboard-af:False,

<名稱>=True,那就是目前fcitx有啟用的輸入法

有了個輸入法的名稱後就可以用 fcitx-remote 切換

fcitx-remote -s <輸入法名稱>

最後就是寫間單的腳本去操控fcitx-remote

注意:fcitx-remote 沒有指定顯示目前正在使用的輸入法 … 所以要自己想個辦法紀錄

去 fcitx 的設定 和 ubuntu輸入法的設定 取消所以切換熱鍵

再去ubuntu熱鍵設定,設定一些客製化的熱鍵把它門綁到你的腳本。

Wednesday, November 1, 2017

Hershey's 巧克力像嘔吐物??

今天吃了一顆 Hershey's 巧克力糖果,一開始覺得沒怎樣,但吃完後就覺得嘴巴裡還殘留某種微酸的味道。我就去查詢 Hershey's 和 sour aftertaste。

原來 Hershey's 巧克力一直都有帶有微酸像是嘔吐物的評價。

有人猜Hershey's 用的的製造過會產生微量的丁酸(butyric acid)。根據維基百科,丁酸存在於腐臭的黃油、帕馬森乾酪、嘔吐物和腋臭中,雖然對身體沒有什麼危害,但味道是非常的噁心,而且我們的嗅覺和味覺對丁酸很敏感。

https://www.reddit.com/r/explainlikeimfive/comments/3nhoiw/eli5_why_does_american_chocolate_taste_so_bad_in/
Back when I was in Florida in 2012, I couldn't believe how bad the chocolate tasted. And I always thought it may have been in my head.
But recently I've seen a few people saying the exact same thing and I'm wondering why it tastes so bad in comparison to the chocolate we get in Europe?
It's as if it tastes like vomit..

I'm not sure 'vomit's the right word - when I first tasted American chocolate I had to spit it out, I thought it was like eating soap.

https://www.mumsnet.com/Talk/food_and_recipes/1131316-Why-does-Hershey-chocolate-taste-so-vile
When i went to Chicago a few years ago we went to the Hershey store. The store was fab the choc not so.
Dp came home with a bar today , gift from work. The kids all had some and enjoyed it but again for me it had a horrid after taste.

The stuff should not be called chocolate. Hersheys is an abomination (or however you spell it).

Hershey's 'Chocolate' Kisses have an after taste of vomit.

http://www.hexjam.com/uk/food-drink/why-does-hershey-s-chocolate-taste-so-bloody-horrible

 What was a surprise, however, was the discovery that America's favourite chocolate bar tastes rather like one imagines dog treats coated in vomit to taste.

https://www.reddit.com/r/AskReddit/comments/3mbpfv/why_does_hersheys_chocolate_taste_slightly_like/
"Why does Hersheys Chocolate taste slightly like sick?"

Saturday, May 27, 2017

Python 3.6 有哪些新特性?

剛看完了 Brett Cannon 在Pycon演講的錄影 https://youtu.be/c2rEbbGLPQc,題目是What's new in Python 3.6
Python 3.6 是由史以來增加了最多新功能的Python版本,除了Python 3.0 以外。內容飽含:

字串插值語法

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}"  # nested fields
'result:      12.35'

數字可以有下底線

>>> 1_000_000_000_000_000
1000000000000000
>>> 0x_FF_FF_FF_FF
4294967295

各種async擴充

Python 3.5增加了async/await的語法,3.6多增加了幾個該有的功能。

一個『類似檔案路徑』的物件

Python 3.4新增了一個pathlib包裝,目的是提供一個檔案系統路徑的物件Path,這樣各式各樣的檔案系統可以有專屬於它門的Path物件,然後程式碼可以更OO化。時間和日期有專用的物件,為什麼檔案系統的路徑不能有?pathlib.Path當初的優點是他跟Python內建的bytes str劃清界線。可是問題來了,很多已有的程式碼就是把路徑當作字串,包含Python自己提供的os.path模組!所以3.6裡面新增了一個os.PathLike物件,提供__fspath__的方法。這方法會迴轉字串路徑。現在pathlib.Path都繼承了os.PathLike,Python內建的各個模組也都被更新成可以接受有繼承os.PathLike的物件。


除了以上所列出的變化以外,3.6版還有很多很多的改良和新功能,請參考 https://docs.python.org/3/whatsnew/3.6.html

Wednesday, November 2, 2016

Simple numpy and Cython example

In [1]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:50% !important; }</style>"))
In [2]:
%load_ext cython
In [4]:
# Pass compile options to %%cython
# -a means annotate the source code to show sections
# which have Python interaction
In [6]:
%%cython -a
cimport numpy as np


cdef double _dot(double* a, double* b, int n):
    cdef int i
    cdef double sum


    sum = 0
    for i in range(n):
        sum += a[i] * b[i]
    
    return sum


cpdef double cy_dot(np.ndarray a, np.ndarray b):
    cdef int n = a.shape[0]

    cdef double* a_ptr = <double*> a.data
    cdef double* b_ptr = <double*> b.data

    return _dot(a_ptr, b_ptr, n)
Out[6]:
Cython: _cython_magic_3e017d4f9980d56ce613de0b5ec2e832.pyx

Generated by Cython 0.23.4

Yellow lines hint at Python interaction.
Click on a line that starts with a "+" to see the C code that Cython generated for it.

+01: cimport numpy as np
  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;
 02: 
 03: 
+04: cdef double _dot(double* a, double* b, int n):
static double __pyx_f_46_cython_magic_3e017d4f9980d56ce613de0b5ec2e832__dot(double *__pyx_v_a, double *__pyx_v_b, int __pyx_v_n) {
  int __pyx_v_i;
  double __pyx_v_sum;
  double __pyx_r;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("_dot", 0);
/* … */
  /* function exit code */
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}
 05:     cdef int i
 06:     cdef double sum
 07: 
 08: 
+09:     sum = 0
  __pyx_v_sum = 0.0;
+10:     for i in range(n):
  __pyx_t_1 = __pyx_v_n;
  for (__pyx_t_2 = 0; __pyx_t_2 < __pyx_t_1; __pyx_t_2+=1) {
    __pyx_v_i = __pyx_t_2;
+11:         sum += a[i] * b[i]
    __pyx_v_sum = (__pyx_v_sum + ((__pyx_v_a[__pyx_v_i]) * (__pyx_v_b[__pyx_v_i])));
  }
 12: 
+13:     return sum
  __pyx_r = __pyx_v_sum;
  goto __pyx_L0;
 14: 
 15: 
+16: cpdef double cy_dot(np.ndarray a, np.ndarray b):
static PyObject *__pyx_pw_46_cython_magic_3e017d4f9980d56ce613de0b5ec2e832_1cy_dot(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static double __pyx_f_46_cython_magic_3e017d4f9980d56ce613de0b5ec2e832_cy_dot(PyArrayObject *__pyx_v_a, PyArrayObject *__pyx_v_b, CYTHON_UNUSED int __pyx_skip_dispatch) {
  int __pyx_v_n;
  double *__pyx_v_a_ptr;
  double *__pyx_v_b_ptr;
  double __pyx_r;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("cy_dot", 0);
/* … */
  /* function exit code */
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

/* Python wrapper */
static PyObject *__pyx_pw_46_cython_magic_3e017d4f9980d56ce613de0b5ec2e832_1cy_dot(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_pw_46_cython_magic_3e017d4f9980d56ce613de0b5ec2e832_1cy_dot(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyArrayObject *__pyx_v_a = 0;
  PyArrayObject *__pyx_v_b = 0;
  PyObject *__pyx_r = 0;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("cy_dot (wrapper)", 0);
  {
    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_a,&__pyx_n_s_b,0};
    PyObject* values[2] = {0,0};
    if (unlikely(__pyx_kwds)) {
      Py_ssize_t kw_args;
      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);
      switch (pos_args) {
        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
        case  0: break;
        default: goto __pyx_L5_argtuple_error;
      }
      kw_args = PyDict_Size(__pyx_kwds);
      switch (pos_args) {
        case  0:
        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_a)) != 0)) kw_args--;
        else goto __pyx_L5_argtuple_error;
        case  1:
        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_b)) != 0)) kw_args--;
        else {
          __Pyx_RaiseArgtupleInvalid("cy_dot", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
        }
      }
      if (unlikely(kw_args > 0)) {
        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "cy_dot") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
      }
    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {
      goto __pyx_L5_argtuple_error;
    } else {
      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);
      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);
    }
    __pyx_v_a = ((PyArrayObject *)values[0]);
    __pyx_v_b = ((PyArrayObject *)values[1]);
  }
  goto __pyx_L4_argument_unpacking_done;
  __pyx_L5_argtuple_error:;
  __Pyx_RaiseArgtupleInvalid("cy_dot", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L3_error;}
  __pyx_L3_error:;
  __Pyx_AddTraceback("_cython_magic_3e017d4f9980d56ce613de0b5ec2e832.cy_dot", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __Pyx_RefNannyFinishContext();
  return NULL;
  __pyx_L4_argument_unpacking_done:;
  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_a), __pyx_ptype_5numpy_ndarray, 1, "a", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_b), __pyx_ptype_5numpy_ndarray, 1, "b", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __pyx_r = __pyx_pf_46_cython_magic_3e017d4f9980d56ce613de0b5ec2e832_cy_dot(__pyx_self, __pyx_v_a, __pyx_v_b);
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;

  /* function exit code */
  goto __pyx_L0;
  __pyx_L1_error:;
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}

static PyObject *__pyx_pf_46_cython_magic_3e017d4f9980d56ce613de0b5ec2e832_cy_dot(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_a, PyArrayObject *__pyx_v_b) {
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("cy_dot", 0);
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 = PyFloat_FromDouble(__pyx_f_46_cython_magic_3e017d4f9980d56ce613de0b5ec2e832_cy_dot(__pyx_v_a, __pyx_v_b, 0)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;

  /* function exit code */
  __pyx_L1_error:;
  __Pyx_XDECREF(__pyx_t_1);
  __Pyx_AddTraceback("_cython_magic_3e017d4f9980d56ce613de0b5ec2e832.cy_dot", __pyx_clineno, __pyx_lineno, __pyx_filename);
  __pyx_r = NULL;
  __pyx_L0:;
  __Pyx_XGIVEREF(__pyx_r);
  __Pyx_RefNannyFinishContext();
  return __pyx_r;
}
+17:     cdef int n = a.shape[0]
  __pyx_v_n = (__pyx_v_a->dimensions[0]);
 18: 
+19:     cdef double* a_ptr = <double*> a.data
  __pyx_v_a_ptr = ((double *)__pyx_v_a->data);
+20:     cdef double* b_ptr = <double*> b.data
  __pyx_v_b_ptr = ((double *)__pyx_v_b->data);
 21: 
+22:     return _dot(a_ptr, b_ptr, n)
  __pyx_r = __pyx_f_46_cython_magic_3e017d4f9980d56ce613de0b5ec2e832__dot(__pyx_v_a_ptr, __pyx_v_b_ptr, __pyx_v_n);
  goto __pyx_L0;
In [5]:
import numpy as np
a = np.array([-2., 0., 5.])
b = np.array([1., 1., 1.])
print a.dot(b)
3.0
In [8]:
print cy_dot(a, b)
3.0

Sunday, September 4, 2016

Connecting to your computer from somewhere else

1. The easy way: Tmate.io
2. The harder way, ssh reverse tunneling
https://juntx.wordpress.com/2014/07/28/use-amazon-ec2-and-ssh-reverse-tunneling-to-connect-computers-behind-firewall-or-nat/
http://www.acooke.org/cute/ReverseRem0.html

https://help.ubuntu.com/community/SSH/OpenSSH/PortForwarding
https://thepcspy.com/read/making-ssh-secure/
http://www.howtogeek.com/75007/stupid-geek-tricks-use-your-ssh-config-file-to-create-aliases-for-hosts/

Start remote
From target computer
ssh into ec2
ssh -R 2222:localhost:22 ec2-ip/host-name

ssh into ec2
from ec2 run ssh -p 2222 me@localhost

Wednesday, August 24, 2016

Deep Learning Frameworks The Easy Way (NVIDIA-Docker)

Install drivers and cuda.

Install Docker from the official site (NOT the Ubuntu repos)
sudo apt-get install docker.io DO NOT DO THIS
(What will happen is nvidia-docker will complain during installation that it can't find docker)
see https://github.com/NVIDIA/nvidia-docker/issues/134

Then install nvidia-docker
https://github.com/NVIDIA/nvidia-docker

Finally go to Docker Hub and find a suitable Docker image. e.g. "tensorflow/tensorflow:latest-gpu"

From what I understand these deep learning + gpu docker images are based on Nvidia's official CUDA images at https://hub.docker.com/r/nvidia/cuda

Notes:
Theano + GTX1060 needs the latest version of cuda, v8.0.
The latest binaries for Tensorflow + GTX1060 only work with CUDA v7.5

That means I need two different docker images, one for Tensorflow and one for Theano.

An alternative would be to build Tensorflow from source so that it can use v8.0 but I'm not experienced with that.

Friday, August 19, 2016

The Road to VR

  1. Buy Vive.
  2. Buy computer parts.
  3. Assemble computer.
  4. Install Windows 10.
    1. Flash USB with Windows 10 ISO.
  5. Install motherboard drivers.
    1. Install LAN driver first by copying LAN driver from CD to a flash drive then moving the driver files to the VR PC.
    2. Download and install the rest of the motherboard-related drivers.
      • USB3.0, Audio, Intel Rapid Storage Technology, etc. ... man there's a lot of crap
  6. Install GPU drivers.
  7. Install Steam.
  8. Setup Vive.
  9. FUN!