Skip to content

Instantly share code, notes, and snippets.

@rossant
rossant / _template.ott
Last active March 25, 2024 12:03
Convert from Markdown to OpenOffice for Linux Magazine
@rossant
rossant / vulkan_sync_warning.c
Last active January 25, 2024 20:45
Vulkan minimal example that generates a SYNC-HAZARD-WRITE-AFTER-READ validation error
// Requires gcc, the Vulkan SDK, and libglfw3-dev
// On Linux, compile with:
// gcc -Wall -o fail fail.c -Ibuild/_deps/glfw-src/include/ -lglfw -lvulkan
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include <vulkan/vulkan.h>
#include <GLFW/glfw3.h>
@rossant
rossant / glwidget.py
Created November 26, 2018 17:14
Minimal modern PyQt5, OpenGL, QOpenGLWindow working example
#!/usr/bin/env python3
"""
Code from http://www.labri.fr/perso/nrougier/python-opengl/#the-hard-way
"""
import ctypes
import logging
@rossant
rossant / raytracing.py
Last active December 24, 2023 12:50
Very simple ray tracing engine in (almost) pure Python. Depends on NumPy and Matplotlib. Diffuse and specular lighting, simple shadows, reflections, no refraction. Purely sequential algorithm, slow execution.
"""
MIT License
Copyright (c) 2017 Cyrille Rossant
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@rossant
rossant / parallel_write.py
Created August 30, 2019 15:04
Write a NumPy array in parallel from multiple CPUs/processes, using shared memory. No copy/serialization involved. Pure Python/NumPy.
"""Write a NumPy array in parallel from multiple CPUs/processes, using shared memory."""
from contextlib import closing
import multiprocessing as mp
import os
import numpy as np
def _init(shared_arr_):
@rossant
rossant / benchmark.ipynb
Last active July 12, 2023 09:34
Quick HDF5 benchmark
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rossant
rossant / latex_to_image.py
Created February 12, 2018 13:46
Convert a LaTeX equation into EPS and PNG with Python
import os
import os.path as op
from pathlib import Path
import shutil
import subprocess
import tempfile
from IPython.lib.latextools import genelatex
@rossant
rossant / hdf5.md
Created October 9, 2015 08:49
Problems with HDF5

Problems with HDF5

  • Corruption: not sure why, sometimes files get corrupted during a session and users lose all their work, either automatic or manual, which may correspond to days of computer time or, worse, human time. Corruption is more likely to happen because libhdf5 is a very complex piece of software, and a crash or sudden kill is likely to corrupt the file completely. This would be much rarer with flat binary or text files, at least you'd be able to recover part of the data.

  • Not possible to delete arrays, but that might be fixed in the future (not today though...).

  • Various bugs with strings on Windows and h5py: users may need to downgrade h5py in order to use their files, otherwise a nasty segfault occurs. Not a good sign...

  • There is a single implementation of HDF5 in the world, so we depend critically on it. It is almost impossible to contribute on such a complex piece of code since it is really low-level (in C). There are bugs and performance issues with it and there is nothing we can

@rossant
rossant / lru_cache.py
Created January 6, 2020 14:02
Custom LRU cache implementation that gives access to the underlying cache dictionary.
def lru_cache(maxsize=0):
"""Custom LRU cache implementation that gives access to the underlying cache dictionary. [better to used functools one if you don't need this]"""
def wrap(f):
cache = {}
last_used = []
def wrapped(*args):
if args in cache:
# HIT.
# Update the last_used list.
@rossant
rossant / onelight.py
Last active September 24, 2019 09:10
ONE light API proposal
from onelib import one
"""
Different ONE backends are available, HTTP, figshare, etc.
One has to implement the following functions to create a new ONE backend:
* list_all_files(): return a list of relative file paths for ALL available files.
* search(...): return a list of dset_ids (by default, a dset_id is a relative file path).
The default implementation calls list_all_files(), and performs the search directly on that list.