"Untitled Post" - Views: 11 · Hits: 11 - Type: Public

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.gridspec as gridspec
import re
import numpy as np
from scipy.interpolate import make_interp_spline, BSpline

data_f = open("resilience-me.posts", "r")

delim = '####POST####'

posts = re.split(delim,data_f.read())

figures = plt.figure(1)

gridspec.GridSpec(4,1)

maxx = 0;

maxy = 0;

interp_degree = 1

labels=["trust", "consensus", "bitlattice"]

d_range = range(0, len(labels))

def post_freq(which):
	global maxy, maxx
	lst = [(lambda x: len(re.findall(re.compile('\\b'+labels[which]+'\\b', re.IGNORECASE), x)))(x) for x in posts]
	maxy = max(maxy,max(lst))
	maxx = max(maxx,len(lst))
	return np.array(lst)

def interpolate(y):
	intplx = np.linspace(xrang.min(),xrang.max(),200)
	spl = make_interp_spline(xrang,y, k=interp_degree)
	return (intplx,spl(intplx))

def trend(freq, label):
	pf = np.polyfit(xrang, freq, 1)
	tr = np.poly1d(pf)
	return plt.plot(xrang,tr(xrang),label=label)

freqs = [post_freq(x) for x in d_range]

yrang = np.arange(0,maxy+1, 1)
xrang = np.arange(1,maxx+1, 1)

axes = plt.subplot2grid((4,1), (0,0), colspan=1, rowspan=1)

interpolated_plots = [plt.plot(*interpolate(freqs[x]), label=labels[x])[0] for x in d_range]
plt.legend(handles=interpolated_plots)

axes.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
axes.xaxis.set_major_locator(ticker.MultipleLocator(base=1.0))
axes.yaxis.set_major_locator(ticker.MaxNLocator(integer=True))
axes.yaxis.set_major_locator(ticker.MultipleLocator(base=1.0))

plt.xlabel("posts")
plt.ylabel("occurences")
plt.yticks(yrang, yrang)
plt.xticks(xrang, xrang)
plt.title('resilience-me frequencies')

axes = plt.subplot2grid((4,1), (1,0), colspan=1, rowspan=1)

trend_plots = [trend(freqs[x], labels[x])[0] for x in d_range]
axes.legend(handles=trend_plots)

axes.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
axes.xaxis.set_major_locator(ticker.MultipleLocator(base=1.0))
axes.yaxis.set_ticks([])
plt.xticks(xrang, xrang)
plt.xlabel("posts")
plt.ylabel("trend strength")
plt.title('resilience-me trends')

axes = plt.subplot2grid((4,1), (2,0), colspan=1, rowspan=2)

data = [sum(freqs[x]) for x in d_range]
pie_labels = [labels[x]+' ('+data[x].astype(str)+')' for x in d_range]
explode = (0.01, 0.15, 0.01)
axes.pie(data, explode=explode, labels=pie_labels, autopct='%1.1f%%', shadow=False, startangle=180, labeldistance=1.2)
axes.axis('equal')
plt.title('words ratio')

figures.set_size_inches(w=8,h=12)
figures.tight_layout()
plt.subplots_adjust(hspace=0.55)
# plt.subplot_tool()
plt.show()