Skip to content

Instantly share code, notes, and snippets.

View ArthurDelannoyazerty's full-sized avatar

Arthur Delannoy ArthurDelannoyazerty

View GitHub Profile
@ArthurDelannoyazerty
ArthurDelannoyazerty / oneLineLuminosityAndContrast.py
Last active May 13, 2024 12:43
One line Luminosity and Contrast control in Python
# 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
@ArthurDelannoyazerty
ArthurDelannoyazerty / count_json_items.py
Created March 20, 2024 10:57
Count the number of items in a json file. You can specify a path in the structure of the json file using the 'path_in_json' variable. Useful for large json that cannot fit in memory.
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
@ArthurDelannoyazerty
ArthurDelannoyazerty / count_line_file.py
Created March 20, 2024 11:00
Count the number of line in a file. Useful for large file that cannot fit in memory.
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)
@ArthurDelannoyazerty
ArthurDelannoyazerty / analyze_embedding.ipynb
Last active March 20, 2024 11:34
A jupyter notebook that show stats on an embedding (set of points in higher dimensions). Shows some dimension reduction (tensorboard projector), silhouette score (=best number of clusters)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ArthurDelannoyazerty
ArthurDelannoyazerty / save_show_matplotlib.py
Last active March 27, 2024 10:28
To put in a script. Can save and/or show a plot. Save the plot in a big window.
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)
@ArthurDelannoyazerty
ArthurDelannoyazerty / matplotlib_functions.py
Last active February 26, 2025 16:40
Useful function for matplotlib + cmap + invisible background
# 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)
@ArthurDelannoyazerty
ArthurDelannoyazerty / remove_last_char.py
Last active March 27, 2024 10:26
Remove the last character from a file
with open(filepath, 'rb+') as f:
f.seek(-1, 2)
f.truncate()
@ArthurDelannoyazerty
ArthurDelannoyazerty / json_ijson_read_write.py
Last active April 11, 2024 08:15
Code for json and ijson reading and writing a file
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
@ArthurDelannoyazerty
ArthurDelannoyazerty / merge_dataframe.py
Last active March 27, 2024 10:26
Merge/join dataframe based on a column
df1 = pd.DataFrame() # ['var1', 'var2']
df2 = pd.DataFrame() # ['var3', 'var1']
merged_df = df1.join(df2.set_index('var1'), on='var1')
@ArthurDelannoyazerty
ArthurDelannoyazerty / merge_nested_dict.py
Created March 27, 2024 10:38
Merge nested dict recursively
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