Category Archives: Coding

How to apply gradient color to matplotlib graphs with colormap?

Matplotlib in python offers some useful tools for plotting with gradient colors. Below is a script that plot a sine wave with gradient color based on its y-value. It shows the use of matplotlib.cm.get_cmap to obtain a color map and the use of matplotlib.colors.Normalize to convert a value to the gradient index used for cmap.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import Normalize


X = np.linspace(0, 10, 101)
Y = 10*np.sin(x)

cmap = cm.get_cmap('Blues', len(y))
norm = Normalize(vmin=np.min(Y),vmax=np.max(Y))

for i in range(len(X)-1):
    plt.plot(X[i:i+2], Y[i:i+2], color=cmap(norm(Y[i])))

plt.legend()
plt.grid()
plt.xlabel("x")
plt.ylabel("y")

A more comprehensive list of colormaps can be found in the matplotlib site: https://matplotlib.org/3.3.2/tutorials/colors/colormaps.html

How to: split PDFs into pages with pyPDF2 (writing your own PDF splitter)

The function below uses pyPDF2 to split pages in a pdf file into separate PDFs by given a path to the master PDF file.

import os
from PyPDF2 import PdfFileReader, PdfFileWriter

def pdf_splitter(path):
    fname = os.path.splitext(os.path.basename(path))[0]
 
    pdf = PdfFileReader(path)
    for page in range(pdf.getNumPages()):
        pdf_writer = PdfFileWriter()
        pdf_writer.addPage(pdf.getPage(page))
 
        output_filename = '{}_page_{}.pdf'.format(
            fname, page+1)
 
        with open(output_filename, 'wb') as out:
            pdf_writer.write(out)
 
        print('Created: {}'.format(output_filename))

How To: Do Conditional AND on Pandas Filters

In pandas, sometimes we may want to have multiple conditional clause on dataframe selection. To do so, use “&” symbol for AND and put the each conditional statement into a curly bracket. Below is a sample code:

import pandas as pd

df = pd.DataFrame({'col_1':[0,1,2,3], 'col_2':[0,0,0,0]})
df.loc[(df['col_1'] > 0) & (df['col_1'] < 3), 'col_2'] = 1

print(df)

#    col_1  col_2
# 0      0      0
# 1      1      1
# 2      2      1
# 3      3      0

How To: Merge PDF Files with Python (PyPDF2)

PyPDF2 is a convenient library in Python to manipulate PDF files. Below is a piece of code that merge PDF files in a folder into one PDF:

import os
from PyPDF2 import PdfFileReader, PdfFileWriter

def merger(output_path, input_paths):
    pdf_writer = PdfFileWriter()
 
    for path in input_paths:
        pdf_reader = PdfFileReader(path, strict=False)

        for page in range(pdf_reader.getNumPages()):
            pdf_writer.addPage(pdf_reader.getPage(page))
            
            if page == 0:
                pdf_writer.addBookmark(os.path.basename(path), pdf_writer.getNumPages()-1, parent=None)
                

folder = r"PATH_TO_FOLDER_CONTAINING_PDFS"
output_path = os.path.join(folder, "out.pdf") # merged PDF
input_paths = []

for root, directory, fn_L in os.walk(folder):
    for fn in fn_L:
        fp = os.path.join(root, fn)
        input_paths.append(fp)
    break

merger(output_path, input_paths)

How to conditionally assign value to Pandas dataframes

import pandas as pd

df = pd.DataFrame({'col_1':[0,1,2,3], 'col_2':[0,0,0,0]})
df.loc[df['col_1'] < 2, 'col_2'] = 1
df.loc[df['col_1'] >= 2, 'col_2'] = -1

print(df)

#    col_1  col_2
# 0      0      1
# 1      1      1
# 2      2     -1
# 3      3     -1

The code above creates a dataframe with two columns and assign values to column 2 based on conditional statement on column 1.

How To Build Your Own Software: Keyboard Chattering / Double Stroking Suppressing Python Script

Previously, I wrote a post regarding keyboard chattering / double tapping software solution (link). This solution requires downloading a software from Softpedia.

When it comes to downloading software that records your keyboard strokes, caution must be taken, to make sure the source is free from malware. Or, why not build your own script to fix the keyboard chattering.

It is actually quite simple to write such script (similar to keylogger) in python with the keyboard library. A simple algorithm to prevent double tapping is to write a callback function for each key press event. Usually the double taps occur within a very short interval, and we can block the key for ~20ms to filter out the second strokes

Script:

import keyboard
import time
import os
import threading

class myThread (threading.Thread):
    def __init__(self, key, keyboard):
        threading.Thread.__init__(self)
        self.key = key
        self.keyboard = keyboard
    def run(self):
        keyboard = self.keyboard
        key = self.key
        hook = keyboard.block_key(key)
        time.sleep(0.02) # block key for 0.02 after pressing (adjustable)
        keyboard.unhook(hook) # unblock key after 0.02 sec

def callback(e):
    key = e.name
    if not key in ["ctrl", "shift", "alt"]: # Don't block function keys
        if e.event_type == "up":
            thread = myThread(key, keyboard)
            thread.run()

if __name__ == "__main__":
    keyboard.hook(callback)

The advantage of writing your own script is the flexibility. Depending on how your keyboard chatter, you can adjust the parameters to make it work. In the script below, I also added an exception list, as I don’t want to block the function keys (ctrl, shift, alt) and double strokes really do not have effects on these keys. Please let me know if you find the script useful.

How To Fix: Keyboard doublestrokes / multistrokes / double tapping / key chattering (Software Solution)

Issue: Keyboard doublestrokes / multistrokes / double tapping / key chattering

Fix 1: Install “Free Keyboard Chattering Fix Software” from Softpedia:
https://www.softpedia.com/get/System/System-Miscellaneous/Keyboard-Chattering-Fix.shtml

Is it safe?
Softpedia is a popular website for software downloads. If you see “100% Free” or “100% Safe” on the top right side of their website, it means the software has been tested by the Softpedia team to ensure it does not contain any form of malware, including but not limited to: spyware, viruses, trojans and backdoors.

Fix 2: Build your own script to filter out the double strokes
See my other post (url).

How to reset ubuntu password in virtual machine

1. Enter the Recovery Mode
– To do this, hold SHIFT key during boot-up
– Select *Advanced options for Ubuntu in the GNU GRUB Menu
– Select (recovery mode)

2. In the recovery mode, choose root – Drop to root shell prompt

3. Remount system with write access, type the following command:

mount -o remount,rw /

4. Reset password, type the following command:

passwd USERNAME

Where USERNAME is your account’s id. If you forgot your username, you can type “ls /home” to list out all usernames.

Source: https://wiki.ubuntu.com/RecoveryMode

How to normalize vectors in numpy

import numpy as np

# Normalizing a vector in Numpy
v = np.array([1, 2, 3]).T
v_normalized = v / np.linalg.norm(v)

# Normalizing multiple vectors stored in Matrix in Numpy
M = np.array([[1, 2, 3], [2, 3, 4], [5, 6, 7]]).T 
M_normalized = M / np.linalg.norm(M, axis=0)

Numpy offers some easy way to normalize vectors into unit vectors. The codes above use numpy.linalg.norm method to compute the L2 norm of the vector.

Source: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.norm.html

Related post:
How to normalize vectors in Matlab

How To Fix: “The ordinal 242 could not be located in the dynamic link library mkl_intel_thread.dll”

One day after updating spyder in anaconda, I got the following error message when opening Spyder:
“The ordinal 242 could not be located in the dynamic link library mkl_intel_thread.dll”

Cause:
Another software installed MKL or Intel OpenMP (libiomp5md.dll) files into the C:\Windows\System32 folder (which is a bad practice) and the libiomp5md.dll installed is not compatible with numpy.

Temporary Fix:
Enter the following command in anaconda prompt before starting spyder

set CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1
spyder

Permanent Fix:
Remove libiomp5md.dll in C:\Windows\System32 or rename it to libiomp5md.dll.bak.

Source: https://conda.io/projects/conda/en/latest/user-guide/troubleshooting.html#numpy-mkl-library-load-failed