Skip to content

Instantly share code, notes, and snippets.

View jakelevi1996's full-sized avatar

Jake Levi jakelevi1996

View GitHub Profile
@jakelevi1996
jakelevi1996 / Recursively flatten an irregularly nested list in Python.md
Created July 6, 2020 13:22
Recursively flatten an irregularly nested list in Python

Recursively flatten an irregularly nested list in Python

As described in my answer to a question on Stack Overflow, here is some code to recursively flatten an irregularly nested list in Python using generator functions:

def flat_gen(nd_list):
    if type(nd_list) is list:
        for i in nd_list:
            yield from flat_gen(i)
 else:
@jakelevi1996
jakelevi1996 / List of dictionaries to dictionary of lists.md
Last active July 2, 2020 15:35
Python: converting a list of dictionaries to a dictionary of lists

Python: converting a list of dictionaries to a dictionary of lists

Shown below is a method for converting a list of dictionaries to a dictionary of lists in Python, when the original list of dictionaries share some (but not all) of the same keys:

# Create dict list
d1 = {x: x + 2 for x in range(0, 20, 2)}
d2 = {x: x * 4 for x in range(0, 20, 3)}
d3 = {x: x ** 2 for x in range(0, 20, 5)}
dict_list = [d1, d2, d3]
@jakelevi1996
jakelevi1996 / .Single script example of using TensorRT and TensorFlow.md
Last active August 6, 2020 11:50
Single script example of using TensorRT and TensorFlow

Single script example of using TensorRT and TensorFlow

This Gist demonstrates a self-contained, single-script example of how to define a simple Keras CNN model, train it on MNIST, convert to TensorRT format, and then perform GPU inference on the Jetson Nano. The script is shown below, followed by the console output.

TODO: fix errors; maybe ask question on Stack Overflow and raise issue on Nvidia website

NB, according to this answer on Stack Overflow, the error messages can be removed by limiting the GPU memory to 2GB (after importing Tensorflow but before importing anything else) as follows:

import tensorflow as tf
@jakelevi1996
jakelevi1996 / . Audio in Python using sounddevice and soundfile.md
Last active June 20, 2020 02:22
Audio in Python using sounddevice and soundfile

Audio in Python using sounddevice and soundfile

sounddevice is a Python module for playing and recording sounds using physical audio devices connected to a computer. The documentation is available here. It can be installed with the command python -m pip install sounddevice (use python3 instead of python on Linux).

soundfile is a Python module for reading and writing audio files. The documentation is available here. It can be installed with the command python -m pip install soundfile (use python3 instead of python on Linux).

This Gist demonstrates simple usage of the sounddevice and soundfile modules. Two simple triads (one major and one minor) are created using pure tones (sinusoids). They are played through the speakers using sounddevice and then saved as WAV files using soundfile (currently, MP3 files are not supported by soundfile because they are not supported by the cross-plat

@jakelevi1996
jakelevi1996 / Download datasets using TensorFlow Datasets.md
Last active June 18, 2020 16:00
Download datasets using TensorFlow Datasets

Download datasets using TensorFlow Datasets

The TensorFlow Datasets module provides easy access to many useful machine learning data sets. It can be installed from pip using the following command:

pip install tensorflow-datasets

There are many datasets available from this module in various categories, including audio, image classification, object detection, text, translation, and more. The full list of data sets and categories is available here. One useful example is the imagenette data-set, a small version of imagenet with 10 classes, 9,469 training examples and 3,925 validation examples, available in 3 different resolutions, requiring 100 MB, 330 MB, and 1.5 GB respectively. Below is an example script which downloads imagenette if it isn't available already, and displays images from it one by one using matplotlib:

@jakelevi1996
jakelevi1996 / .timeplot - a Python module for plotting time complexity.md
Last active September 26, 2021 17:13
timeplot: a Python module for plotting time complexity

timeplot: a Python module for plotting time complexity

Here is the source code (and some usage examples and their resulting images, shown below) for timeplot: a Python function/module which accepts a dictionary of string:function pairs, and plots the time complexity of each function over a specified range of input sizes (the key-strings are used as legend entries). Each function must accept an integer size, and return a floating-point value for the time-taken for the function's main operation (not including set-up code). Usage examples and images shown below include comparing different matrix operations, comparing strategies for modifying an iterable while iterating over it, and comparing the efficiency of operations on python lists vs dicts. The images are shown first, followed by the modules that generated them using timeplot.

@jakelevi1996
jakelevi1996 / Custom exceptions in Python.md
Last active June 25, 2021 11:54
Custom exceptions in Python

Custom exceptions in Python

The simplest way to define a custom error in Python is as follows:

class CustomError(Exception): pass

This exception can be raised in any of the following ways:

@jakelevi1996
jakelevi1996 / Read-only attributes in Python.md
Created May 29, 2020 22:49
Read-only attributes in Python

Read-only attributes in Python

The Python built-in function property can be used as a decorator to create read-only attributes of a class, as described in the Python documentation for built-in functions. To make a property read-only, simply do not define a setter method for that property, in which case an AttributeError will be raised when trying to assign a value to that property, as demonstrated below:

class C:
    def __init__(self, x):
        self._x = x
    
 @property

Timing in Python

Python provides a timeit module for timing statements and function calls. Below are some examples of using timeit, along with some results to demonstrate the efficiency of various different operations in Python. Using this approach can be useful before implementing a new feature when there are a few different options for how how to implement that feature, in order to gauge which approach will be the most efficient.

Different types of function calls

When using timeit, there are various different options for choosing how to time a function, EG using default arguments, wrapping the function in a lambda expression, or putting the statement in a string, which supposedly reduces the overhead. This example demonstrates the consequences for execution time of those different choices, as well as using the repeat function from the timeit module, which calls the function repeatedly, and returns a list of the results; as stated in [the d

@jakelevi1996
jakelevi1996 / Householder transformations and the QR decomposition.md
Last active May 3, 2020 15:37
Householder transformations and the QR decomposition

Householder transformations and the QR decomposition

This Gist demonstrates some code for generating Householder transformations, and using it to perform QR decompositions, including pytest unit tests. This is prototype code, and could be made more efficient in various ways; for example, when performing the QR decomposition, calculating v (the Householder vector) could be made a lot more efficient by using the fact that y is always a one-hot vector, and the memory efficiency can be improved by normalising v such that the first non-zero element is unity, and storing the rest of the vector in the corresponding column of R, as described in chapter 5 of Matrix Computations by Golub and Van Loan (4th edition).

householder.py

import numpy as np

class Householder: