03. January 2021
Python snippets
Python snippets that are too long for me to remember.
These scripts are classified into different usage scenario. Some of them are easier to understand while not so efficient.
Read all certain type of files from a directory
1
2images = [os.path.join(dir, name) for name in os.listdir(dir) if
3 os.path.isfile(os.path.join(dir, name)) & name.endswith('.jpg')]
4
Write list to file
1with open("outfile", "w") as outfile:
2 outfile.write("\n".join(itemlist))
f-string
1pi = 3.141592653589793238462643383279
2print(f'The first 6 decimals of pi are {pi:.6f}.')
3
4from random import randint
5for i in range(5):
6 print(f'My money is {randint(0, 150):>3}$')
7
8print(f'Just found {10000000000:,}$ in my pocket')
pandas
about manipulating DataFrame, do not grow dataframe, use list.append in loop instead
https://stackoverflow.com/questions/13784192/creating-an-empty-pandas-dataframe-then-filling-it
Read and save dictionary with json format
1import json
2f = open('file.json', 'r')
3raw = json.load(f)
4for k, v in raw.items():
5 # do something
6
7# this can generate beautiful json output
8with open('saving_file.json', 'w') as fp:
9 json_dumps_str = json.dumps(saving_dictionary, indent=4)
10 print(json_dumps_str, file=fp)
Taking argument from command line
1
2#argsparse
3import argparse
4parser = argparse.ArgumentParser()
5parser.add_argument('-x', '--xml', help='blast xml input', nargs='+')
6parser.add_argument('-a', '--aminoacid', help='genome_aa_file_name', nargs='+')
7
8args = parser.parse_args()
9xml_files=args.xml
10genome_aa_file_name = args.aminoacid
11
conda
virtual environment configuration for jupyter lab
1
2#Assuming your conda-env is named cenv, it is as simple as :
3
4conda activate cenv
5conda install ipykernel
6ipython kernel install --user --name=<any_name_for_kernel>
7conda deactivate
Image processing
1from PIL import ImageEnhance, Image
2resize_factor = 0.1
3image = Image.open(image_path)
4width, height = image.size
5print(image.size)
6height = int(height*resize_factor)
7width = int(width*resize_factor)
8image = image.resize((width, height))
9
10# rotate image
11if width > height:
12angle = 270
13print("rotate")
14image = image.rotate(angle, expand=True)
15width, height = image.size
16print(image.size)
17# enhancement
18image = ImageEnhance.Contrast(image).enhance(0.6)
19image = ImageEnhance.Brightness(image).enhance(1.7)
20# image.show()
21# pixel manipulation
22image_data = image.load()
23for loop1 in range(width):
24 for loop2 in range(height):
25 r,g,b = image_data[loop1,loop2]
26 # print(r)
27 if b>g or b>r:
28 image_data[loop1,loop2] = 0,0,0
29 elif r-b>200:
30 image_data[loop1,loop2] = 0,0,0
31new_file_name = file_name.loc[image_name].genotype + '_' + image_name
32new_file_name = new_file_name.replace(' ','_')
33new_file_name = new_file_name.replace('JPG','jpg')
34new_file_name = shelter_num + new_file_name
35image.save(new_file_name)
Compose image sequences into video
1import cv2
2img_array = []
3ww = 640
4hh = 480
5color = (0,0,0)
6
7for img_path in img_path_list:
8 img = cv2.imread(img_path)
9 height, width, layers = img.shape
10 x = int((ww-width)/2)
11 y = int((hh-height)/2)
12 result = np.full((hh,ww,layers), color, dtype=np.uint8)
13 result[y:y+height, x:x+width] = img
14 img_array.append(result)
15
16# write a video composed of images
17size = (ww,hh)
18#fourcc mp4v or avc1 for mac, windows and linux need different fourcc
19video = cv2.VideoWriter('dirt_image_sequence.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 6, size)
20
21for i in range(len(img_array)):
22 video.write(img_array[i])
23video.release()