Skip to content

Instantly share code, notes, and snippets.

View andres-fr's full-sized avatar

Andres Fernandez andres-fr

View GitHub Profile
@andres-fr
andres-fr / distributed_hdf5.py
Last active January 21, 2024 05:48
Static class to manage distributed (virtual) HDF5 databases
#!/usr/bin python
# -*- coding:utf-8 -*-
"""
"""
import os
import h5py
@andres-fr
andres-fr / plt_font_manager.py
Last active March 26, 2023 01:12
Setting arbitrary fonts in matplotlib
import matplotlib.pyplot as plt
import matplotlib.font_manager as plt_fm
class PltFontManager:
"""
Sometimes matplotlib finds the system font paths, but setting them can
still be challenging due to using the wrong name or matplotlib complaining
about missing elements.
This manager static class is intended to aid with that, by facilitating
@andres-fr
andres-fr / subset_sampler_pytorch.py
Last active March 14, 2023 23:14
PyTorch dataset sampler that retrieves a subset of the indices, with the possibility of deterministic and balanced behaviour.
from collections import defaultdict
import random
class SubsetSampler(torch.utils.data.Sampler):
"""
Like ``torch.utils.data.SubsetRandomsampler``, but without the random,
and with the possibility of balanced sampling. Samples a subset of the
given dataset from a given list of indices, without replacement.
Usage example::
@andres-fr
andres-fr / cose_noisy_parallel.py
Last active February 5, 2023 01:49
Parallelized implementation of L1 CoSe with error tolerance
#!/usr/bin/env python
# -*-coding:utf-8-*-
"""
This module contains 2 functions:
* ``cose_recovery``: Implements the L1 compressed sensing program, allowing
for L2 error tolerance in the constraint.
* ``cose_recovery_mpp``: Parallelized version of ``cose_recovery`` that runs
@andres-fr
andres-fr / gsnr_backpack.py
Last active November 18, 2022 16:05
Different ways of computing GSNR with PyTorch+BackPACK
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
The quantities computed in this gist are wrong. While a correction is
pending, see the following discussion for more details. The TLDR is that
they must be divided by batch_size^2, or use the alternative Cockpit
@andres-fr
andres-fr / cose.py
Last active November 11, 2022 01:09
Compressed Sensing, minimal example
#!/usr/bin/env python
# -*-coding:utf-8-*-
"""
This module contains a minimal example to illustrate one of the main
implications of the compressed sensing paradigm: That a perfect recovery of a
sparse signal is possible with a number of measurements that depends on the
signal sparsity, and not on the signal length.
@andres-fr
andres-fr / bash_default_named_params.sh
Last active June 17, 2022 23:07
Example of a bash function with named parameters+defaults
interactive_gpu()
{
# needed by getopt: https://stackoverflow.com/a/5048356
local OPTIND
# default parameter values
seconds=300
partition="large"
# parse flags to optionally override param defaults
@andres-fr
andres-fr / bfs.py
Created June 12, 2022 19:30
Pixel-wise breadth-first search that not only traverses pixels but also returns the corresponding tree. Includes convenience functions.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Most existing breadth-first search implementations provide a solution to
*traverse* a tree, but they don't *return* that specific tree. This module
provides a tree datastructure and BFS implementation to obtain the BFS tree.
"""
@andres-fr
andres-fr / wav_to_mel.py
Created May 13, 2022 13:04
Flexible and efficient conversion from mono WAV to log-mel spectrogram
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
"""
import librosa
import numpy as np
@andres-fr
andres-fr / jax_inv_sampling.py
Created February 23, 2022 00:28
Inverse regular sampling from a black-box function using JAX+SGD with momentum
import jax.numpy as jnp
from jax import jit, value_and_grad
from jax.config import config
config.update("jax_debug_nans", True)
class RegularInv1dSampler:
"""
This regular inverse sampler deals with the following problem: given a
smooth function ``y=f(x)`` for ``x`` scalar and ``y`` n-dimensional,
retrieve ``N`` monotonically increasing values of ``x``, such that the