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 / find_files_recursive_folder.py
Last active March 20, 2024 10:01
Find files with the corresponding extension in the selected folder and its subdirectories. Return the list of files paths
def find_files_recursive_folder(folder_path:str, file_extension:str) -> list:
list_filepath = list()
for dirpath, _, filenames in os.walk(folder_path):
for filename in [f for f in filenames if f.endswith(file_extension)]:
filepath = dirpath.replace("\\","/") + "/" + filename
list_filepath.append(filepath)
return list_filepath
@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 / print_graph_summary.py
Created March 20, 2024 11:02
Print stats of a networkX graph.
from networkx import Graph
def print_graph_summary(graph:Graph):
nb_node = graph.number_of_nodes()
nb_edge = graph.number_of_edges()
print("Number of nodes : ", nb_node, "Number of edges : ", nb_edge)
return nb_node, nb_edge
@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 / iterate_folder.py
Last active March 22, 2024 14:39
Iterate the content of a folder.
for filename in os.listdir(directory):
f = directory + "/" + filename
if os.path.isfile(f):
print(f)
@ArthurDelannoyazerty
ArthurDelannoyazerty / matplotlib_functions.py
Last active May 23, 2024 12:16
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()