Skip to content

Instantly share code, notes, and snippets.

View ArthurDelannoyazerty's full-sized avatar

Arthur Delannoy ArthurDelannoyazerty

View GitHub Profile
@ArthurDelannoyazerty
ArthurDelannoyazerty / plot_altitude_gpx.py
Last active June 27, 2024 14:29
Plot the altitude over the distance from GPX files
import gpxpy
import matplotlib.pyplot as plt
import os
import re
from itertools import accumulate
size = 1.2
gpx_folderpath = "gpx/"
@ArthurDelannoyazerty
ArthurDelannoyazerty / sort_filepath_alphabetically.py
Last active June 27, 2024 07:36
Code that gives a sorted file list
files = [f for f in os.listdir(gpx_folderpath) if os.path.isfile(os.path.join(gpx_folderpath, f))]
def sort_key(file):
num = re.search(r'(\d+)', file).group(1)
num = num.replace('-', ' ')
num = int(num) if num.isdigit() else 0
return (num, file)
files.sort(key=sort_key)
@ArthurDelannoyazerty
ArthurDelannoyazerty / pytorch_index_slice_with_backpropagation.py
Created June 5, 2024 15:00
A small code that slice a tensor and can pass the backpropagation.
N = 4
x = torch.randn(N, 3)
out = torch.Tensor([1,0,1,0]).float().requires_grad_(True)
res1 = x * out[:, None]
idx = res1.nonzero()[:, 0].unique()
res2 = res1[idx]
# perform your operation here
res = res2.mean()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ArthurDelannoyazerty
ArthurDelannoyazerty / pytorch_computation_graph_break.md
Last active June 13, 2024 13:20
Example of code that break the pytorch computation graph.

Things to do to break the computation graph

By using the output of the following code in the flow of the model.

torch.tensor(existing_tensor)
existing_tensor.item()
existing_tensor.detach()
@ArthurDelannoyazerty
ArthurDelannoyazerty / matplotlib_auto_image_size.py
Created May 28, 2024 09:22
Python functions that return the dpi of the main screen, and the x/y size of the image in inch in order to resize the plot automatically.
import ctypes
import tkinter
def get_dpi():
ctypes.windll.shcore.SetProcessDpiAwareness(1)
root = tkinter.Tk()
dpi = ctypes.windll.user32.GetDpiForWindow(root.winfo_id())
root.destroy()
return dpi
@ArthurDelannoyazerty
ArthurDelannoyazerty / rotation_animation_matplotlib_3d.py
Last active May 23, 2024 07:49
A function that create a gif/mp4 of a rotating 3d matplotlib plot.
def rotanimate(ax, output_filename:str, angles:list=None, elevation:list=None, width:float=8, height:float=8, filename_prefix:str='tmprot_', nb_image:int=120, fps:int=30):
if angles==None: angles = np.linspace(0, 360, nb_image+1)
if elevation==None: elevation = np.linspace(20, 20, nb_image+1)
ax.figure.set_size_inches(width,height)
files = []
for i, angle in enumerate(angles):
ax.view_init(elev = elevation[i], azim=angle)
fname = '%s%03d.jpeg'%(filename_prefix,i)
@ArthurDelannoyazerty
ArthurDelannoyazerty / torch_dataset.py
Created May 17, 2024 09:52
Example of a torch dataset that load sparse matrix.
class CustomImageDatasetNumpy(Dataset):
def __init__(self, dir_path:str):
self.dir_path = dir_path
self.filenames = os.listdir(dir_path)
def __len__(self):
return len(self.filenames)
def __getitem__(self, idx):
np_path = self.dir_path + "/" + self.filenames[idx]
@ArthurDelannoyazerty
ArthurDelannoyazerty / split_train_test_torch.py
Created May 17, 2024 09:48
Torch split train test dataset.
def create_data_loader(dirpath:str, test_split:float=0.1, shuffle_dataset:bool=True):
"""Create a dataset and split+shuffle it."""
dataset = CustomImageDatasetNumpy(dirpath)
dataset_size = len(dataset)
indices = list(range(dataset_size))
split = int(np.floor(test_split * dataset_size))
if shuffle_dataset: np.random.shuffle(indices)
train_indices, val_indices = indices[split:], indices[:split]
@ArthurDelannoyazerty
ArthurDelannoyazerty / delete_content_folder.py
Created May 17, 2024 08:21
Recursively delete the content of a folder
def delete_content_folder(folder:str):
for filename in tqdm(os.listdir(folder), desc="Erasing Temporary Data", leave=False):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))