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 / exr_to_np.py
Created September 19, 2019 20:26
Convert EXR files to numpy float32 matrices
# -*- coding:utf-8 -*-
"""
Place this script on a directory and convert the .exr files it contains
into np.float32 matrices
"""
import os
import OpenEXR
@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 / py3_color_logger.py
Last active October 15, 2023 01:06
A well-suported and pre-configured color logger for Python 3
#!/usr/bin python
# -*- coding:utf-8 -*-
"""
This module features convenient color logger (wrapping the built-in ``loging.``
class), plus some helper functionality.
"""
@andres-fr
andres-fr / flask__server__client.py
Last active June 25, 2023 18:41
Flask client/server to exchange compressed numpy arrays:
################################################################################
### server.py
################################################################################
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Dummy server, interacts with the client receiving, altering and returning
@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