Monday, June 1, 2020

Adding two positive floating point numbers as strings


Interview Question

Q: Assume there are 2 numbers like 2.39 and 0.9 in string format "2.39" and "0.9". Without using any standard string-to-numeric conversion library functions, try to sum the two numbers and represent the output also as a string. Both numbers are assumed to be always positive.

n1 = "12.39"
n2 = "9.999"

(a, b) = n1.split('.')
(c, d) = n2.split('.')

l1 = abs(len(a) - len(c))
if len(a) > len(c):
    c = ''.join(['0'] * l1) + c  # 0000...
else:
    a = ''.join(['0'] * l1) + a

l1 = abs(len(b) - len(d))
if len(b) > len(d):
    d = d + ''.join(['0'] * l1)
else:
    b = b + ''.join(['0'] * l1)

M = '0'+ a + "." + b # Overflow of carry possible - padding with leading zero
N = '0'+ c + "." + d # Overflow of carry possible - padding with leading zero

print("->", M, N)

OUTPUT = []
carry = 0
for i in range(len(M) - 1, -1, -1):
    if M[i] == '.':
        OUTPUT.append('.')
        continue
    else:
        tmp = carry + (ord(M[i]) - 48) + (ord(N[i]) - 48)
        carry = tmp // 10
        OUTPUT.append(str(tmp % 10))

OUTPUT = OUTPUT[::-1]  # reverse the list
if OUTPUT[0] == '0':
    OUTPUT.pop(0)
print(OUTPUT[0])
OUTPUT = ''.join(OUTPUT)
print (OUTPUT)

Saturday, May 12, 2018

Remote Desktop to AWS GPU instances

If you are looking to run Jupyter notebooks from AWS and leverage their EC2 instances which are compatible for GPU computing then you will invariably try to do a remote desktop connection to their Ubuntu instances using RDP or VNC viewer

1. Install VNC viewer on your windows host
2. On the EC2 GPU instance install the following:

sudo apt-get install ubuntu-gnome-desktop tightvncserver xtightvncviewer tightvnc-java  xfonts-75dpi xfonts-100dpi gnome-panel  metacity light-themes ubuntu-settings indicator-applet-complete gnome-session-flashback indicator-applet-appmenu

sudo locale-gen de_DE.UTF-8 && touch ~/.Xresources && vncpasswd

3. Ensure that the contents of your ~/.vnc/xstartup file look like this below:

################### CONTENTS OF ~/.vnc/xstartup  #######################
#!/bin/sh

xrdb $HOME/.Xresources
xsetroot -solid grey
x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
export XKL_XMODMAP_DISABLE=1
export XDG_CURRENT_DESKTOP="GNOME-Flashback:GNOME"
export XDG_MENU_PREFIX="gnome-flashback-"
/etc/X11/Xsession


gnome-session --session=gnome-flashback-metacity --disable-acceleration-check &
gnome-panel &
gnome-settings-daemon &
metacity &
nautilus --force-desktop --no-default-window

Then you are all set!

-----------------------
When you use tightvnc client on Windows ensure that you enter the IP address of your AWS host with the server instance as suffix. Also ensure that the GPU server instance has the tightvncserver instance started as follows:
$ tightvncserver -geometry 1920x1200




Sunday, September 10, 2017



Frustrated with phpmyadmin setup in Ubuntu 16.04?


After spending about 5 hours trying to get phpmyadmin to work with mysql on ubuntu I finally switched gears and started looking at an alternative - mywebsql

This link below explains step by step how to install this lightweight web interface to the mysql and the configuration is much simpler than phpmyadmin. So bye-bye phpmyadmin and hello mywebsql


https://www.linuxhelp.com/how-to-install-mywebsql-on-ubuntu/

Friday, August 25, 2017

OpenCV 3.1 Build commands

OpenCV 3.1

sudo apt-get update -y

sudo apt-get upgrade -y

sudo apt-get install build-essential cmake pkg-config  -y

sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev -y

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y

sudo apt-get install libxvidcore-dev libx264-dev -y

sudo apt-get autoclean

sudo apt-get install libgtk-3-dev -y

sudo apt-get install libatlas-base-dev gfortran -y

sudo apt-get install python2.7-dev -y

sudo apt-get install tree unzip -y

sudo apt-get install python-pip -y

sudo -H pip install pip -U

sudo apt-get remove python3 --purge -y

sudo -H pip install numpy -U

sudo  apt-get install --upgrade ipython

sudo -H pip install jupyter -U



##############################################################
cd ~
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.1.0.zip
rm -rf ~/opencv-3.1.0
unzip opencv.zip
rm -rf ~/opencv_contrib-3.1.0
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
unzip opencv_contrib.zip


#########################################################
cd ~/opencv-3.1.0/
rm -rf build
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
    -D PYTHON_EXECUTABLE=/usr/bin/python \
    -D BUILD_EXAMPLES=OFF  ..


###############################################################

make -j4

sudo make install

sudo ldconfig

sudo ln -sf /home/ubuntu/opencv-3.1.0/build/lib/cv2.so  /usr/local/lib/python2.7/site-packages/cv2.so


Monday, July 31, 2017

Python and OpenCV 3.1


First you need Jupyter and AWS ubuntu 16.04
https://www.rosehosting.com/blog/how-to-install-jupyter-on-an-ubuntu-16-04-vps/

All the powerful feature descriptors are 3rd party extensions which need special build of OpenCV

Prior to installing OpenCV, resolve some dependencies like Tesseract, PyVTK etc.,
https://medium.com/@lucas63/installing-tesseract-3-04-in-ubuntu-14-04-1dae8b748a32
https://lucacerone.net/2017/install-tesseract-3-0-5-in-ubuntu-16-04/

Then proceed with OpenCV build
http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/

You will run into a snag with the OpenCV build. Here is how to resolve it:
https://github.com/opencv/opencv/issues/6016

locate the file:modules/python/common.cmake and append 2 lines of code 
find_package(HDF5)

include_directories(${HDF5_INCLUDE_DIRS})


The way to access the features has changed from OpenCV 2.4 days to OpenCV 3.1
Reference: http://www.pyimagesearch.com/2015/07/16/where-did-sift-and-surf-go-in-opencv-3/#comment-431206


Some great descriptors are DCT, GLCM, FLANN, LUCID, ORB, BRIEF, SURF and SIFT, Gabor

https://www.researchgate.net/post/I_want_to_extract_Haralick_texture_features_in_openCV2
https://stackoverflow.com/questions/19556538/how-to-find-glcm-of-an-image-in-opencv-or-numpy
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_orb/py_orb.html
http://docs.opencv.org/3.1.0/d5/d51/group__features2d__main.html
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html
http://answers.opencv.org/question/63517/how-to-successfully-implement-a-gabor-filtering/
http://docs.opencv.org/3.0-beta/modules/imgproc/doc/filtering.html
https://gist.github.com/odebeir/5237529

Finding the best features which are rotation invariant, scale invariant, resolution invariant, robust against blurring etc., is a challenge.


Of course going with a deep-learning way of classification would imply that there is no need to explicitly do feature extraction

http://www.wolfib.com/Image-Recognition-Intro-Part-1/
https://research.googleblog.com/2016/03/train-your-own-image-classifier-with.html
https://github.com/rdcolema/tensorflow-image-classification/blob/master/cnn.ipynb

Finally, remember to run Jupyter from this location so that the cv2 version is set to 3.1.0

~/.virtualenvs/cv/lib/python2.7/site-packages$


Tuesday, July 25, 2017

Setting up R Server Connect

R Server Connect is a great piece of software!
It allows data scientists to publish their static or interactive dashboards which might be RMarkdown or Shiny applications

But one is bound to run into installation problems like I did.

Please ensure that the following dependencies are met.

Rstudio IDE:
install.packages('rsconnect')


install.packages("devtools", dependencies=TRUE)

Linux Shell:
sudo apt-get install r-cran-rserve
sudo apt-get install openssl
sudo apt-get install libssl-dev
sudo apt-get install libcurl4-gnutls-dev
sudo apt-get install libxml2-dev


Tuesday, June 13, 2017

Python Threads (lifesaver!)

https://pymotw.com/2/threading/