Skip to content

Instantly share code, notes, and snippets.

View brianckeegan's full-sized avatar

Brian Keegan brianckeegan

View GitHub Profile
@brianckeegan
brianckeegan / tikz_triad_census.tex
Created March 28, 2019 17:04
Making a table in LaTeX using TikZ of a triad census for directed graphs
\begin{figure*}[!tb]
\centering
\footnotesize
\begin{tabular}{cccc}
% First row of triad census
\begin{tikzpicture}
[user/.style={circle,draw=black,fill=black!60,thick,text=white,inner sep=0pt,minimum size=.5cm},
->,-stealth,line width = 2, black,
font=\boldmath]
@brianckeegan
brianckeegan / baseball_reference_scraping.py
Created April 3, 2017 04:50
Debugging baseball-reference standings scrape
from bs4 import BeautifulSoup
import requests
# Get the data
raw = requests.get('http://www.baseball-reference.com/leagues/MLB/2016-standings.shtml').text
# Parse it
soup = BeautifulSoup(raw,'html.parser')
# Find the table
@brianckeegan
brianckeegan / gist:b62b041dbfa58944ab87
Created April 10, 2015 00:21
OpenName verification
Verifying that +bkeegan is my openname (Bitcoin username). https://onename.com/bkeegan
@brianckeegan
brianckeegan / top_wikipedia_articles_by_editors.py
Last active June 19, 2021 13:31
Top Wikipedia stories in 2014
import numpy as np
import pandas as pd
from bs4 import BeautifulSoup, element
import urllib2, re
# Read the HTML from the webpage on Wikipedia stats and convert to soup
soup = BeautifulSoup(urllib2.urlopen('http://stats.wikimedia.org/EN/TablesWikipediaEN.htm').read())
# Look for all the paragraphs with 2014
_p = soup.findAll('b',text=re.compile('2014'))
@brianckeegan
brianckeegan / barchart_labels.py
Last active August 29, 2015 14:00
Put labels inside barcharts.
# adapted from http://matplotlib.org/examples/api/barchart_demo.html
def autolabel(rects):
max_height = max([rect.get_height() for rect in rects if hasattr(rect,'get_height') and not np.isnan(rect.get_height())])
for rect in rects:
if hasattr(rect,'get_height'):
height = rect.get_height()
if not np.isnan(height):
ax.text(rect.get_x()+rect.get_width()/2., height-.1*max_height, '%d'%int(height),
ha='center', va='bottom',color='w')
@brianckeegan
brianckeegan / diverse_df.py
Last active October 4, 2021 10:22
Generates a random pandas DataFrame containing categories, timestamps, integers, and random floats. Useful for debugging or answering StackOverflow questions.
import pandas as pd
import numpy as np
import datetime
dft = pd.DataFrame({'A' : ['spam', 'eggs', 'spam', 'eggs'] * 6,
'B' : ['alpha', 'beta', 'gamma'] * 8,
'C' : [np.random.choice(pd.date_range(datetime.datetime(2013,1,1),datetime.datetime(2013,1,3))) for i in range(24)],
'D' : np.random.randn(24),
'E' : np.random.random_integers(0,4,24)})
@brianckeegan
brianckeegan / backbone_extractor.py
Last active August 2, 2023 19:26
Given a networkx graph containing weighted edges and a threshold parameter alpha, this code will return another networkx graph with the "backbone" of the graph containing a subset of weighted edges that fall above the threshold following the method in Serrano et al. 2008.
# Serrano, Boguna, Vespigani backbone extractor
# from http://www.pnas.org/content/106/16/6483.abstract
# Thanks to Michael Conover and Qian Zhang at Indiana with help on earlier versions
# Thanks to Clay Davis for pointing out an error
import networkx as nx
import numpy as np
def extract_backbone(g, weight='weight', alpha=.05):
backbone_graph = nx.Graph()