Skip to content

Instantly share code, notes, and snippets.

View cpatdowling's full-sized avatar

Chase Dowling cpatdowling

View GitHub Profile
@cpatdowling
cpatdowling / blockface_geo_distance_example.py
Created February 15, 2022 01:15
This is an example of using sklearn's haversine distance to compute pairwise distances between lat-long pairs. This is then used to find locations or in this instance blockfaces at incremental distances away from each known location
### compute pairwise distnances
from sklearn.metrics.pairwise import haversine_distances
import numpy as np
#here we make up a bunch of random coordinates
lats = np.random.uniform(45,47,size=(100,))
longs = np.random.uniform(-120,-100,size=(100,))
coords = list(zip(lats, longs))
@cpatdowling
cpatdowling / linear_transition_kernel.py
Last active April 27, 2021 00:50
A linear transition kernel akin to continuous voting for a multi-agent MDP
import numpy as np
def return_linear_pdf(n):
#n is the number of dimensions/players in the joint action
#this describes a n-dimensional linear surface defined from [0,1] that integrates to 1
P = lambda x: (2.0/float(n))*np.sum(x)
return(P)
def return_linear_cdf(n):
#cummulative distribution function for linear PDF
@cpatdowling
cpatdowling / date_to_feature.py
Last active May 26, 2022 20:34
This (inefficient) function accepts a python datetime and shows how to transform repeating features (day of the year, weekday, hour) on the numberline into continuous features, (ie days 1 and 365 are topologically close to one another at the top of a circle)
from datetime import datetime as dt
import calendar
import numpy as np
def num_to_circle(z):
#this functions accepts a number between 0 and 1 and projects it onto x, y coordinates on the unit circle
x = np.cos(2*np.pi*z)
y = np.sin(2*np.pi*z)
return((x, y))
@cpatdowling
cpatdowling / dynamic_function_generation_for_tkinter_buttons.py
Last active October 21, 2020 21:03
An abuse of python globals() method to dynamically generate sequentially named functions; this was used to automatically populate buttons in a passive, same-thread GUI using tkinter
#credit to StackOverflow user yann-vernier for means of abuse of globals()
#https://stackoverflow.com/questions/49631178/using-for-loop-to-define-multiple-functions-python
import time
import tkinter as tk
from types import FunctionType
from copy import copy
#The GUI will print filenames and some filename feature when button presses are passively detected
@cpatdowling
cpatdowling / tkinter_while_loop_switch.py
Last active October 20, 2020 18:49
Basic structure for a switch condition triggered by a GUI button, designed to change variable values within a while loop performing some set of instructions without the need for a dedicated tkinter window thread at the cost of strict concurrency of action
import tkinter as tk
flag = False # Global flag
idx = 0 # loop index
def switch():
"""Enable scanning by setting the global flag to True."""
global flag
flag = True
@cpatdowling
cpatdowling / jupyter_header.txt
Last active November 6, 2020 20:00
Personal header for Jupyter Notebooks:
#cells will fill entire width of the browser
from IPython.display import display, HTML
display(HTML(data="""
<style>
div#notebook-container { width: 95%; }
div#menubar-container { width: 65%; }
div#maintoolbar-container { width: 99%; }
</style>
"""))
@cpatdowling
cpatdowling / help_for_sean.py
Last active October 14, 2018 01:12
Demonstrating a global reference variable in action for a friend
import copy
import random
global market_ref
market_ref = {'item_1': 100.0,
'item_2': 200.0,
'item_3': 100.0}
def market_shift():
#updates the global market baseline
@cpatdowling
cpatdowling / matlab_to_np_txt_array.py
Created May 17, 2017 22:50
Read a Matlab text array into NumPy array
def read_matlab_float(strin):
strin = strin.strip()
tokens = strin.split("e")
sign_ = tokens[1][0]
if sign_ == "-":
sign = -1.0
else:
sign = 1.0
p = float(tokens[1][1:].strip())
mant = float(tokens[0])
@cpatdowling
cpatdowling / send_notif_email.py
Last active June 23, 2017 23:30
Python 2.7+ class for sending failure notifications via gmail to oneself; can be nice emails too, I'm not your boss.
#Requires google application password generation if using 2-factor authentication
#https://support.google.com/accounts/answer/185833?hl=en
#Update credential storage method in __init__ method
#Example usage:
"""
from send_notif_email import *
try:
@cpatdowling
cpatdowling / mapoverlay.py
Created April 5, 2017 20:16
A native python 2.7+ class that projects WSG84 latitude/longitude coordinates to pixel positions in a Web Mercator map image with known coordination.
#Example usage
"""
upleft = [47.6197793,-122.3592749] Upper left coordinate of map image
bttmright = [47.607274, -122.334786] Lower right coordinate of map image
imgsize = [1135,864] Pixel size of map image
mp = MapOverlay(upleft, bttmright, imgsize)
#then given a list of latitude/longitude pairs , we can get their relative positions on the image of the map, note
#that image pixel positions start with (0,0) at the top left of the image, so plotting will require inverting the y-axis