Wednesday, August 30, 2017

Numpy, how to divide the row by its sum efficiently

Method #1: use None (or np.newaxis) to add an extra dimension so that broadcasting will behave:
>>> e
array([[ 0.,  1.],
       [ 2.,  4.],
       [ 1.,  5.]])
>>> e/e.sum(axis=1)[:,None]
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])
Method #2: go transpose-happy:
>>> (e.T/e.sum(axis=1)).T
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])
(You can drop the axis= part for conciseness, if you want.)
Method #3: (promoted from Jaime's comment). (only works Numpy 1.7+)
Use the keepdims argument on sum to preserve the dimension:
>>> e/e.sum(axis=1, keepdims=True)
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])

Tuesday, August 15, 2017

The Gap between two displays under MATE desktop UBUNTU


I found a related question on Ask Ubuntu which sort of did the trick for me. Instructions for the whole process, including creating the "gap" between monitors (works at least on Ubuntu 14.04):
  • Find out the current total screen size (assuming there's currently no virtual gap between monitors):
    $ xrandr | grep Screen Screen 0: minimum 320 x 200, current2048 x 1280, maximum 32767 x 32767
  • Add the desired gap size in pixels to the width (first value displayed after current – in my case, 2048). I chose a gap of 136 pixels, so I'd have a new screen width of 2184.
  • Set the new screen size:
    xrandr --fb 2184x1280
  • Find out the display name of your right monitor:
    $ xrandr | grep ^[^\(]*right HDMI3connected 1024x1280+1024+0 right (normal left inverted right x axis y axis) 376mm x 301mm
  • Virtually move the right monitor further to the right to create the gap. Add your gap size to the third number displayed by the above command (highlighted in bold). In my example, this is 1024+136=1160:
    xrandr --output HDMI3 --pos 1160x0
Now, make Compiz handle the whole area as one single screen
  • Start CompizConfig Settings Manager (installable with sudo apt-get install compizconfig-settings-manager)
  • Go to "General Options" plugin (e.g. type this in search box)
  • Select tab "Display Settings"
  • Uncheck "Detect Outputs"
  • If this doesn't already do the trick, right click on each line listed under "Outputs", select "Edit" and delete their content.
When maximizing a window, it will now stretch both monitors. Fullscreen will also stretch both monitors.
Clearly, this isn't ideal, as most of the time it is desirable that maximized windows fill a single monitor. This means, the "Detect Outputs" Compiz option will have to be checked and unchecked depending on what behavior is currently desired.
A note to user with non-English locale: If you can't find the CompizConfig plugin and setting because you don't know how their names were translated, start CompizConfig like so:

Friday, June 23, 2017

Several Tricks

1. Print .JSON file nicely by using python:
  
python -m json.tool my_json.json
 
2. Read how many words in a line:
 
echo "abc,def,ghi"  | tr ',' ' ' | wc