Skip to content

Instantly share code, notes, and snippets.

View thearn's full-sized avatar

Tristan Hearn thearn

  • NASA Glenn Research Center
  • Cleveland, OH
View GitHub Profile
@thearn
thearn / svd_approximate.py
Last active January 8, 2024 20:25
Function to generate an SVD low-rank approximation of a matrix, using numpy.linalg.svd. Can be used as a form of compression, or to reduce the condition number of a matrix.
import numpy as np
def low_rank_approx(SVD=None, A=None, r=1):
"""
Computes an r-rank approximation of a matrix
given the component u, s, and v of it's SVD
Requires: numpy
"""
@thearn
thearn / fft_convolution.py
Last active November 18, 2023 09:47
1D and 2D FFT-based convolution functions in Python, using numpy.fft
from numpy.fft import fft, ifft, fft2, ifft2, fftshift
import numpy as np
def fft_convolve2d(x,y):
""" 2D convolution, using FFT"""
fr = fft2(x)
fr2 = fft2(np.flipud(np.fliplr(y)))
m,n = fr.shape
cc = np.real(ifft2(fr*fr2))
cc = np.roll(cc, -m/2+1,axis=0)
@thearn
thearn / opencv-python-ipcam.py
Last active January 12, 2023 17:01
python-opencv ip camera example
import base64
import time
import urllib2
import cv2
import numpy as np
"""
Examples of objects for image frame aquisition from both IP and
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import random
import pickle
import matplotlib.dates as mdates
from matplotlib.ticker import LinearLocator
import numpy as np
import openmdao.api as om
class SIRvec(om.ExplicitComponent):
"""Basic epidemiological infection model
S (suceptible), I (infected), R (recovered).
"""
def initialize(self):
self.options.declare('num_nodes', types=int)
@thearn
thearn / vector.py
Created April 21, 2020 15:45
vectorized comp
import numpy as np
import openmdao.api as om
class SIRvec(om.ExplicitComponent):
"""Basic epidemiological infection model
S (suceptible), I (infected), R (recovered).
"""
def initialize(self):
self.options.declare('num_nodes', types=int)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@thearn
thearn / gzip_pickle.py
Created April 20, 2013 00:57
Functions for saving and loading python objects using pickle with gzip compression.
import pickle
import gzip
def save(object, filename, protocol = 0):
"""Saves a compressed object to disk
"""
file = gzip.GzipFile(filename, 'wb')
file.write(pickle.dumps(object, protocol))
file.close()
@thearn
thearn / capture.py
Created June 15, 2013 21:31
Screen capture using python PIL
import time
import Image
import ImageGrab
while True:
time.sleep(0.5)
t = str(time.time()).replace('.','-')
tt = time.time()
img=ImageGrab.grab()
img = img.resize((800,600))
4 21 894
419 794 987
424 797 125
651 305 558
655 631 963
2 628 436
736 50 363
657 707 408
252 705 98
532 173 878