Skip to content

Instantly share code, notes, and snippets.

View kwcooper's full-sized avatar
🧠
User is typing...

Keiland kwcooper

🧠
User is typing...
View GitHub Profile
@kwcooper
kwcooper / spring.py
Created March 5, 2019 06:20
Create a damped oscillation.
import math as m
import kMath as km
import matplotlib.pyplot as plt
A = .96 #initial height
ma = 5.0 #mass
k = 2.1 #spring constant
b = 0.09 #dampening
theta = 0 #phase angle
w = m.sqrt(k / ma)
@kwcooper
kwcooper / convolveMat.py
Last active March 5, 2019 06:21
Convolve a N-D kernel with a NxN matrix, such as an image
import numpy as np
import matplotlib.image as img
import matplotlib.pyplot as plt
image = img.imread('school.png') # Grab the image
def toGreyscale(image):
# convert the RGB to Greyscale
return np.dot(image[...,:3], [0.114, 0.299, 0.587])
@kwcooper
kwcooper / makeMovie.py
Last active April 13, 2019 06:02
Takes images from a directory and builds a movie from them
# make a movie from images
import glob
import imageio
import os
path_name = "img/*.png"
# grab files, sorted by modification time
#filenames = glob.glob("img/*.png")
@kwcooper
kwcooper / datetimeScatterPlot.py
Last active May 16, 2019 05:22
Create a date-time scatter plot from datetime objects
import matplotlib.pyplot as plt
from matplotlib import dates
import datetime
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
# TODO: Turn this bad boy into a function
# Raw data example (Format will vary)
@kwcooper
kwcooper / strpGMailTime.py
Created December 14, 2019 03:47
# Function that can strip the times from a handful of the gmail timestamps I have encountered
def strpMailTime(timestamp):
if len(timestamp) < 27:
# Ex. '5 Nov 2018 12:05:07 -0500' len 25
# '28 Oct 2019 03:33:57 -0400' len 26
dt = datetime.datetime.strptime(timestamp, "%d %b %Y %H:%M:%S %z")
elif len(timestamp) > 29 and len(timestamp) < 32:
# 'Fri, 6 Dec 2019 15:22:49 +0000' len 30
# 'Mon, 09 Dec 2019 14:19:26 +0000' len 31
dt = datetime.datetime.strptime(timestamp, "%a, %d %b %Y %H:%M:%S %z")
elif len(timestamp) > 35 and len(timestamp) < 39:
@kwcooper
kwcooper / plotdatetimes.py
Created December 15, 2019 07:57
new function to plot datetimes. Can pass multiple lists and labels to plot multiple datatypes
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime
import random
import matplotlib.ticker as mticker
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
@kwcooper
kwcooper / scaleToRange.py
Created January 16, 2020 03:12
Ftn that takes a list of numbers and scales it within the bounds of the given arguments.
def scaleInRange(lon, a, b):
r_max, r_min = max(lon), min(lon)
slon = []
for i in lon:
slon.append(((i - r_min) / (r_max - r_min)) * (b - a) + a)
return slon
# Example
lon = range(1,100)
a, b, = 4, 8
@kwcooper
kwcooper / barplot.py
Created April 23, 2020 07:23
Create a simple bar plot so I don't have to keep writing it
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
# Create test data
a, b = np.random.randint(10, 25, 30), np.random.randint(0, 8, 30)
a_mu, b_mu = np.mean(a), np.mean(b)
a_std, b_std = np.std(a), np.std(b)
@kwcooper
kwcooper / groupbylist.py
Created October 1, 2020 05:29
Group one list by another list with itertools and python 3+
from itertools import groupby
l1 = [1,1,1,2,2,2,2,2,1,1,1,0,0,0]
l2 = range(len(l1))
[(k, list(group)) for k, group in groupby(l2, key=lambda _, ig=iter(l1): next(ig))]
# Returns [(1, [0, 1, 2]), (2, [3, 4, 5, 6, 7]), (1, [8, 9, 10]), (0, [11, 12, 13])]
@kwcooper
kwcooper / violinPlot.py
Created December 4, 2020 04:26
create a violin plot with matplotlib
import matplotlib.pyplot as plt
# kwc 201208
# Create synthetic data for each plot
np.random.seed(10)
synData_1 = np.random.normal(100, 10, 200)
synData_2 = np.random.normal(80, 30, 200)
synData_3 = np.random.normal(90, 20, 200)
synData_4 = np.random.normal(70, 25, 200)