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 / 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 / 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 / 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 / pprocess_test.py
Created April 20, 2013 01:07
An experiment with pprocess
import pprocess
import time
import numpy as np
# Define a function to parallelize:
def takeuptime(x):
"""A function to waste CPU cycles"""
time.sleep(0.1)
return x[-1]
@thearn
thearn / brew.py
Last active December 16, 2015 10:59
A simple command-line app to help profile homebrew'ed beer. User inputs starting temperature, starting original gravity, and the app will compute and print temperature-adjusted original gravity and max potential ABV. Final (post-fermentation) temperature and specific-gravity can also be input to calculate effective ABV, calories per serving, and…
def adjust(t, og):
t2 = 1.34722124e-4 * t
t3 = 2.04052596e-6 * t ** 2
t4 = 2.32820948e-9 * t ** 3
sgcf = 1.00130346 - t2 + t3 - t4
sg = og * sgcf
adjusted_og = sg
OE = -668.962 + 1262.45 * sg - 776.43 * sg * sg + 182.94 * sg * sg * sg
abv_max = (sg - 1) * 105 * 1.25
@thearn
thearn / fft_series.py
Last active December 16, 2015 11:49
Class for buffering an incoming data series and computing an FFT
import numpy as np
import time
class bandProcess(object):
"""
Component to isolate specific frequency bands
"""
@thearn
thearn / ip.py
Last active December 16, 2015 11:59
A python code to determine a computer's external IP address, which is then written to a text file. Run from a dropbox folder (or something similar), this provides a very simple way to determine your home's dynamic external IP address remotely.
import urllib
import re
import time
import datetime
def get_ip():
group = re.compile(u'(?P<ip>\d+\.\d+\.\d+\.\d+)').search(
urllib.URLopener().open('http://jsonip.com/').read()).groupdict()
return group['ip']
@chackley
chackley / Vaccine-Autism Evidence
Created April 26, 2013 17:05
List of scientific articles regarding the hypothesis that vaccines are related to incidence of autism in children.
Lancet. 2004 Sep 11-17;364(9438):963-9. MMR vaccination and pervasive developmental disorders: a case-control study. PMID 15364187
294 cases and 4469 controls were included. 1010 cases (78.1%) had MMR vaccination recorded before diagnosis, compared with 3671 controls (82.1%) before the age at which their matched case was diagnosed. After adjustment for age at joining the database, the odds ratio for association between MMR and pervasive developmental disorder was 0.86 (95% CI 0.68-1.09). Our findings suggest that MMR vaccination is not associated with an increased risk of pervasive developmental disorders.
Odds ratio below 1 suggests decreased, not increased, risk of autism with vaccine. CI crosses 1 so the effect of vaccine preventing autism is non-significant.
N Engl J Med. 2002 Nov 7;347(19):1477-82. A population-based study of measles, mumps, and rubella vaccination and autism. PMID 12421889
Of the 537,303 children in the cohort (representing 2,129,864 person-years), 440,655 (82.0 percent) had received t
@grantslatton
grantslatton / fizzbuzz.c
Last active August 19, 2022 11:20
FizzBuzz solved using only bit twiddling. It essentially uses two deterministic finite automata for divisibility testing.
#include <stdio.h>
int f0(unsigned int x) { return x? (x&(1<<31)? f1(x<<1) : f0(x<<1)) : 1; }
int f1(unsigned int x) { return x? (x&(1<<31)? f3(x<<1) : f2(x<<1)) : 0; }
int f2(unsigned int x) { return x? (x&(1<<31)? f0(x<<1) : f4(x<<1)) : 0; }
int f3(unsigned int x) { return x? (x&(1<<31)? f2(x<<1) : f1(x<<1)) : 0; }
int f4(unsigned int x) { return x? (x&(1<<31)? f4(x<<1) : f3(x<<1)) : 0; }
int t0(unsigned int x) { return x? (x&(1<<31)? t1(x<<1) : t0(x<<1)) : 1; }
int t1(unsigned int x) { return x? (x&(1<<31)? t0(x<<1) : t2(x<<1)) : 0; }
int t2(unsigned int x) { return x? (x&(1<<31)? t2(x<<1) : t1(x<<1)) : 0; }
@briankung
briankung / The Project Euler Sprint Rules.md
Last active December 14, 2017 02:20
The Project Euler Sprint Rules

The Project Euler Sprint Hack Nights

Project Euler Sprint Hack Nights are beginner friendly events where you can work on your own projects or engage in a friendly competition called the Project Euler Sprint.

The Project Euler Sprint is a friendly competition involving solving Project Euler ([http://projecteuler.net][projecteuler]) problems for points. Project Euler is a series of increasingly difficult computational math problems that must be solved with code (generally speaking - we've had some impressive solutions in pen and paper as well as on an Excel spreadsheet).

Each problem is harder than the last, so each problem is worth its problem number in points. Problem #1 is easy, so it's worth 1 point, while problem #50 is much harder, but worth 50 points. You can form teams of 4 people and solutions can be in any language as long as it's coded there. More detailed rules below.

Sprint Rules