Category Archives: How to

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.

Study shows face masks can protect you from coronavirus.

Research Finding from 2004 SARS Coronavirus Study 🧐

Despite the government insisting that no evidence has been shown on the effectiveness of face mask for the protection of public health, here is an article from Journal of Epidemiology and Community Health published in 2004 recommending the use of face mask for coronavirus protection.

 

Quotes From the Article:

“The finding of a possible dose-response3 for exposure and infection to SARS CoV lessens the chance of infection through droplet transmission by the general public, especially when some personal protection is afforded. When masks are used along with other hygiene practices, risk of infection, excluding close contact with an infected person, like a family member, can be minimised.”

“For public health protection, use of masks can have some impact on preventing the spread of SARS CoV.”

Why did the government say that?

 

Perhaps the government wants to prioritize the use of face masks for medical care sector, they definitely do not want to deal with the shortage of face masks that may cause health care workers to be under-equipped.  But turning a blind-eye to all the research done in the past and boldly says those advice to the public may not be a reasonable thing to do. Perhaps finding means to expand face mask production is a better solution to get through this challenging covid-19 pandemic.

How to fix orphan files in Google Drive?

Missing Files and Folder on Google Drive!

If you stumble into this post, it means you are also having an orphan files problem with Gdrive.

I had exactly the same issue. When I was trying to remove a big folder from Google Drive to free space, only the top layer of the folder is removed. All the sub-folders inside are not trashed, but appearing in my “Recent” and “Storage” Tab.

Those missing and disappearing files become what are known as orphan files (or invisible ghost files). They are folders / files with an empty path.

Here are the solutions to fix those, you just need to find them and restore / trash them.

Solution 1: Searching “is:unorganized owner:me”

Some of the ghost files can be found by searching “is:unorganized owner:me” on the search bar. This will list out the unorganized files and folder. You can then drag them back to a new location / trash them again to free space.

However, this does not work for files in an orphan folder! See solution 2 to find ghost files in orphan folder.

Solution 2: Finding files in orphan directory

Step 1: List storage by clicking on “XXX GB of XXX GB used” below “Storage” on the left, this will list all of your files

Step 2: Sort the files by size / by names to find 1 orphan file

Step 3: Single Click on the orphan file

Step 4: Open the Detail Tab on the right side, you will see the orphan directory, click onto that to drill into the directory

Step 5: If the orphan directory is also a sub-folder, navigate to the parent folder through the buttons below the search bar

Step 6: Now, you are at the parent orphan directory. Single click on the folder button below the search bar and choose “search within [Folder Name]” from the dropdown menu.

Step 7: Great, now you have a list of all the orphan files in that parent orphan folder. You can then drag them to a new location or proceed to remove them to free space.

Step 8: Repeat to target other parent orphan folder (hope you do not have too many parents folders to deal with!)

How to cancel Microsoft Office 365 subscription and get a refund when the cancellation button is not showing?

If you happened to receive a free Microsoft office 365 subscription (for a limited number of months) or tried the trial version, but forgot to cancel your subscription, Microsoft will renew the subscription for you automatically and charge your credit card.

According to Microsoft, you can request a refund if:

  • You bought an annual subscription to Office 365 within the last 30 days.
  • You have a monthly subscription and cancel within 30 days of your last renewal.

Normally, you can do it online through the website: https://account.microsoft.com/account

You can login and manage your subscription. If the above condition is satisfied, you will see a button to cancel your subscription and get a refund.

However, sometimes the button is not showing up. Perhaps in their system, auto-renewal of the subscription is not detected automatically to be eligible for the refund.

However, no worry, you can contact Microsoft customer service arrange for the refund. I tried contacting Microsoft through their Help Service (https://partner.support.services.microsoft.com/en-us/contactus/) and the process went quite smoothly.

 

How to free up space in windows 10

1. Using the windows Disk Cleanup tool
Windows comes with a disk cleanup tool that lists out most of the temporary files locations. You can remove those files without changing the functionality of your PC. These locations includes:

  • a) Delivery Optimization Files
    Delivery Optimization files are files that were previously downloaded to your computer and can be deleted if currently unused by the Delivery Optimization service.
  • b) Downloads Folder
  • c) Recycle Bin
  • d) Temporary Files
    Apps can store tempporary information in specific folders. These can be cleaned up manually if you app does not do it automatically.
  • e) Thumbnails
    Windows keeps a copy of all of your picture, video, and document thumbnails so they can be displayed quickly when you open a folder. If you delete these thumbnails, they will be automatically created as needed.

 

2. Remove Windows.old folder (up to 40GB)
If you update to windows 10 from a previous windows version, windows would keep a copy of the previous OS in your disk, which is stored in C:\Windows.old folder. This folder can take up to 40GB. As windows 10 is getting stable, this previous windows copy may not be useful anymore and we may choose to remove it to save disk space. This site shows the steps of how to remove windows.old folder.

如何修復Spyder 程式錯誤: “The ordinal 242 could not be located in the dynamic link library mkl_intel_thread.dll”

更新後,spyder 出現以下程式錯誤:
“The ordinal 242 could not be located in the dynamic link library mkl_intel_thread.dll”

錯誤原因:
有其他程序將 MKL 及 Intel OpenMP (libiomp5md.dll) 安裝至 C:\Windows\System32。

暫時性修復::
Enter the following command in anaconda prompt before starting spyder

set CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1
spyder

永久性修復:
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