This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The worst things I ever had to write. | |
| # Origin multi-line from : https://www.geeksforgeeks.org/changing-the-contrast-and-brightness-of-an-image-using-python-opencv/ | |
| import cv2 as cv | |
| def transform(image, contrast=[0-255](default=128), luminosity=[0-255](default=255)): | |
| return (cv.addWeighted(((cv.addWeighted(image, ((255 if (int((brightness - 0) * (255 - (-255)) / (510 - 0) + (-255)))>0 else 255+(int((brightness - 0) * (255 - (-255)) / (510 - 0) + (-255)))) - ((int((brightness - 0) * (255 - (-255)) / (510 - 0) + (-255))) if (int((brightness - 0) * (255 - (-255)) / (510 - 0) + (-255)))>0 else 0)) / 255, image, 0, (int((brightness - 0) * (255 - (-255)) / (510 - 0) + (-255))) if (int((brightness - 0) * (255 - (-255)) / (510 - 0) + (-255)))>0 else 0) if (int((brightness - 0) * (255 - (-255)) / (510 - 0) + (-255))) != 0 else image)), float(131 * ((int((contrast - 0) * (127 - (-127)) / (254 - 0) + (-127))) + 127)) / (127 * (131 - (int((contrast - 0) * (127 - (-127)) / (254 - 0) + (-127))))), ((cv.addWeighted(i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from ijson import items | |
| from tqdm import tqdm | |
| def count_json_items(filepath:str, path_in_json:str) -> int: | |
| with open(filepath, 'r') as file: | |
| parser = items(file, path_in_json) | |
| i=0 | |
| for _ in tqdm(parser, desc="Counting number of items", leave=False): | |
| i=i+1 | |
| return i |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from itertools import takewhile, repeat | |
| def rawincount(filepath:str) -> int: | |
| f = open(filepath, 'rb') | |
| bufgen = takewhile(lambda x: x, (f.raw.read(1024*1024) for _ in repeat(None))) | |
| return sum(buf.count(b'\n') for buf in bufgen) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import matplotlib.pyplot as plt | |
| SHOW = False | |
| SAVE = True | |
| def display_save_plt(title:str): | |
| if SAVE: | |
| plt.gcf().set_size_inches(25, 13) | |
| plt.savefig("data/stats/" + title + ".png", dpi=200) | |
| if SHOW: | |
| plt.gcf().set_size_inches(20, 11) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # meta | |
| %matplotlib widget # interactive matplotlib for jupyter notebook | |
| matplotlib.use('agg') # change backend ('agg' is a static backend => only for saving the image, plt.show() will not work) | |
| # Size & Position | |
| plt.gcf().set_size_inches(25, 13) # set the size of the chart | |
| plt.get_current_fig_manager().window.setGeometry(50,100,640, 545) # configure position & size of the screen (posX, posY, dX, dY) | |
| # Chart organisation | |
| plt.subplots_adjust(bottom=0.25) # add space at the bottom of the chart for axis title(s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with open(filepath, 'rb+') as f: | |
| f.seek(-1, 2) | |
| f.truncate() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import ijson | |
| # ijson READ | |
| with open(filepath) as f: | |
| parser = ijson.items(f, "item") # or : "key.items" | |
| for item in parser: | |
| print(item) | |
| # json READ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| df1 = pd.DataFrame() # ['var1', 'var2'] | |
| df2 = pd.DataFrame() # ['var3', 'var1'] | |
| merged_df = df1.join(df2.set_index('var1'), on='var1') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def merge(a:dict, b:dict, path=[]): | |
| for key in b: | |
| if key in a: | |
| if isinstance(a[key], dict) and isinstance(b[key], dict): | |
| merge(a[key], b[key], path + [str(key)]) | |
| elif a[key] != b[key]: | |
| raise Exception('Conflict at ' + '.'.join(path + [str(key)])) | |
| else: | |
| a[key] = b[key] | |
| return a |
OlderNewer