Skip to content

Instantly share code, notes, and snippets.

View rileypeterson's full-sized avatar

rileypeterson

View GitHub Profile
@rileypeterson
rileypeterson / gist:447c1dc405264a09d1e67a7db675b369
Created August 10, 2018 05:45
Google translate applied to pandas Series
from googletrans import Translator
import pandas as pd
translator = Translator()
df = pd.DataFrame({'Spanish':['piso','cama']})
df['English'] = df['Spanish'].apply(translator.translate, src='es', dest='en').apply(getattr, args=('text',))
@rileypeterson
rileypeterson / prog_log.py
Last active August 29, 2018 06:16
Progress Bar for Logging WIP
import logging
import time
import numpy as np
def prog_log(logger, percentage, pwidth=20, pchar="#"):
""" Take a logger object and percentage and output a
progress bar to the log
"""
original_format = logger.handlers[0].formatter
logger.info("Progress:")
@rileypeterson
rileypeterson / new_mysql_wb.md
Created August 23, 2018 18:05
New Mysql workbench window
@rileypeterson
rileypeterson / Expanding Linear Regression
Last active August 29, 2018 01:16
Expanding Linear Regression
# UPDATE: He re-added the question here: https://stackoverflow.com/questions/52048919/how-to-incrementally-add-linear-regression-column-to-pandas-dataframe/52068085#52068085
# Some guy had this weird question on Stack Overflow about cummulatively applying linear regression to a dataframe
# He deleted the question (I don't think this operation is very useful), but I figured out a way to do it here:
# Pretty wacky
from io import StringIO
import pandas as pd
import numpy as np
df = pd.read_table(StringIO(""" a b
@rileypeterson
rileypeterson / gist:a5c80a2a57c4bd74b03a1bac2a049c1c
Created September 7, 2018 15:11
Yield continued fraction coefficients for Napier's constant (e)
def A003417():
n = 0
while True:
if n == 0:
yield 2
if n == 1:
yield 1
else:
if (n+1) % 3 == 0:
yield int(2 * ((n+1) / 3))
# Your init script
#
# Atom will evaluate this file each time a new window is opened. It is run
# after packages are loaded/activated and after the previous editor state
# has been restored.
#
# An example hack to log to the console when each text editor is saved.
#
# atom.workspace.observeTextEditors (editor) ->
# editor.onDidSave ->
@rileypeterson
rileypeterson / decorators.py
Last active February 1, 2019 01:25
Just some musings about decorators
from functools import wraps
STRING1_LIST = ['A', 'B', 'C']
STRING2_LIST = ['D', 'E', 'F']
def type_checker(int1=None, string1=None, string2=None):
""" Decorator function to check if int1 is an int and
that string1 or string2 supplied is actually a valid string
"""
if string1:
s = 'string1'
list_to_find_in = STRING1_LIST
@rileypeterson
rileypeterson / Ordered stacked matplotlib bar chart
Created March 23, 2019 20:36
matplotlib.axes.Axes.bar does not support stacking out of the box. pandas.DataFrame.plot.bar does support stacking, but not ordered stacking. Here is an implementation of ordered stacking.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
a = pd.DataFrame({'a':[0.25, 0.5, 0.15, 0], 'b':[0.15, 0.25, 0.35, 0.15],
'c':[0.50, 0.15, 0.5, 0.35], 'd':[0.35, 0.35, 0.25, 0.5],})
fig, ax = plt.subplots()
x = a.index
indexes = np.argsort(a.values).T
heights = np.sort(a.values).T
order = -1
ax1.set_zorder(ax2.get_zorder()+1) # put ax in front of ax2
ax1.patch.set_visible(False) # hide the 'canvas'
from: http://matplotlib.1069221.n5.nabble.com/Control-twinx-series-zorder-ax2-series-behind-ax1-series-or-place-ax2-on-left-ax1-on-right-td12994.html
@rileypeterson
rileypeterson / second_order_fda.py
Created April 25, 2019 02:44
Return the second order central finite difference of a vector
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 50, 3001)
y = np.sin(3*x)
delta_x = x[1]
def second_order_central_fd(y, delta_x):
"""
Return the second order central finite difference of a vector (i.e. d^2 y/ dx^2).
Zero fills rollover. What it should probably do is a single order FDA
or interpolate the endpoints.