def main():
    try:
        if sys.argv[1].endswith('.svg'):
            matplotlib.use('SVG')
    except IndexError:
        pass

    font = FontProperties()
    font.set_size('medium')
    font.set_weight('semibold')

    with open('data/article_metadata.pkl') as data_file:
        data = pkl.load(data_file)

    data = data['datasets']

    citations = defaultdict(lambda: [])

    reuse_rates = {}
    with open('data/reuse_estimates') as data_file:
        for line in data_file:
            if line.startswith('#'):
                repo = line.split('[')[0][1:].strip()
                l = '[' + line.split('[')[1]
                l = eval(l)
                # average wos/gs reuse rates
                rate = (l[0][2] + l[1][2]) / 2
                reuse_rates[repo] = rate

    for (k, v) in data.items():
        repo = v['repo']
        years = v['years']
        years = [y - min(years) for y in years]
        citations[repo].append(years)

    vals = defaultdict(lambda: {})
    for repo in citations:
        v = vals[repo]
        for i in range(5 + 1):
            v[i] = []
            for y in citations[repo]:
                val = len([x for x in y if x <= i]) * reuse_rates[repo]
                v[i].append(val)

    fig = plt.figure(figsize=(12, 8))
    subs = []

    for (n, (repo, v)) in enumerate(sorted(vals.items())):
        sub = plt.subplot(2, 4, n + 1)
        subs.append(sub)

        plt.ylim(0, 25)
        plt.text(0.5,
                 0.9,
                 repo,
                 fontproperties=font,
                 horizontalalignment='center',
                 verticalalignment='center',
                 transform=sub.transAxes)

        k = sorted(v.keys())
        # show the median and 95%, 50% CIs
        for p in [2.5, 25, 50, 75, 97.5]:
            y = [np.percentile(v[i], p) for i in k]
            plt.plot(k, y, 'b-' if p == 50 else 'b--')

    for sub in subs:
        sub.minorticks_off()
        layout.cross_spines(ax=sub)

    fig.text(0.5, 0.04, 'Years since publication', ha='center', va='center')
    fig.text(0.06,
             0.5,
             'Instances of reuse',
             ha='center',
             va='center',
             rotation='vertical')

    try:
        figname = sys.argv[1]
        plt.savefig(figname, dpi=200)
    except IndexError:
        plt.show()
Example #2
0
def main():
    try:
        if sys.argv[1].endswith('.svg'):
            matplotlib.use('SVG')
    except IndexError: pass
    
    font = FontProperties()
    font.set_size('medium')
    font.set_weight('semibold')
    
    with open('data/article_metadata.pkl') as data_file:
        data = pkl.load(data_file)
    
    data = data['datasets']
    
    citations = defaultdict(lambda: [])
    
    reuse_rates = {}
    with open('data/reuse_estimates') as data_file:
        for line in data_file:
            if line.startswith('#'):
                repo = line.split('[')[0][1:].strip()
                l = '[' + line.split('[')[1]
                l = eval(l)
                # average wos/gs reuse rates
                rate = (l[0][2] + l[1][2])/2
                reuse_rates[repo] = rate

    for (k,v) in data.items():
        repo = v['repo']
        years = v['years']
        years = [y - min(years) for y in years]
        citations[repo].append(years)
    
    vals = defaultdict(lambda: {})
    for repo in citations:
        v = vals[repo]
        for i in range(5+1):
            v[i] = []
            for y in citations[repo]:
                val = len([x for x in y if x <= i]) * reuse_rates[repo]
                v[i].append(val)
    
    fig = plt.figure(figsize=(12,8))
    subs = []
    
    for (n, (repo, v)) in enumerate(sorted(vals.items())):
        sub = plt.subplot(2,4,n+1)
        subs.append(sub)
        
        plt.ylim(0,25)
        plt.text(0.5, 0.9, repo, fontproperties=font,
                 horizontalalignment='center',
                 verticalalignment='center',
                 transform=sub.transAxes)
        
        k = sorted(v.keys())
        # show the median and 95%, 50% CIs
        for p in [2.5, 25, 50, 75, 97.5]:
            y = [np.percentile(v[i], p) for i in k]
            plt.plot(k, y, 'b-' if p==50 else 'b--')
    
    for sub in subs:
        sub.minorticks_off()
        layout.cross_spines(ax=sub)
    
    fig.text(0.5, 0.04, 'Years since publication', ha='center', va='center')
    fig.text(0.06, 0.5, 'Instances of reuse', ha='center', va='center', rotation='vertical')
    
    try:
        figname = sys.argv[1]
        plt.savefig(figname, dpi=200)
    except IndexError:
        plt.show()
Example #3
0
# sinusoidal lines with colors from default color cycle
L = 2 * np.pi
x = np.linspace(0, L)
ncolors = len(plt.rcParams['axes.color_cycle'])
shift = np.linspace(0, L, ncolors, endpoint=False)
for s in shift:
    ax2.plot(x, np.sin(x + s), '-')
ax2.margins(0)

# bar graphs
x = np.arange(5)
y1, y2 = np.random.randint(1, 25, size=(2, 5))
width = 0.25
ax3.bar(x, y1, width)
ax3.bar(x + width, y2, width, color=plt.rcParams['axes.color_cycle'][2])
ax3.set_xticks(x + width)
ax3.set_xticklabels(['a', 'b', 'c', 'd', 'e'])

# circles with colors from default color cycle
for i, color in enumerate(plt.rcParams['axes.color_cycle']):
    xy = np.random.normal(size=2)
    ax4.add_patch(plt.Circle(xy, radius=0.3, color=color))
ax4.axis('equal')
ax4.margins(0)

# Remove ticks on top and right sides of plot
for ax in axes.ravel():
    layout.cross_spines(ax=ax)

plt.show()
Example #4
0
def PlotData(DataDF):
	'''
	facecolor decided the frame color around the plot

	'''
	fig = plt.figure()                                   #Plot figure 1
	ax = fig.add_subplot(111)
	'''
	Preparing list and variables to plot
	'''
	Index = []
	TotalTime =[]
	playCount =[]
	ArtistName = []
	for i in xrange(len(DataDF['Total Time'])):
		if DataDF['Total Time'].iloc[i] > 150:
			Index.append(i)
			TotalTime.append(DataDF['Total Time'].iloc[i])
			playCount.append(DataDF['Play Count'].iloc[i])
			ArtistName.append(DataDF['Artist Name'].iloc[i])
	
	'''
	Scatter plot is for values which have run time greater a given Threshold
	This Threshold is for the Total Time of the Song played is choosed arbitariraly 
	for now. This has to be passed as  a parameter which needs to be added

	Most of the commands are self explanatory still will try and elaborate on each
	
	'''
	'''
	Scatter plot and Line plot on the same graph
	'''
	ax.scatter(Index,TotalTime,s =playCount)
	ax.plot(DataDF['Total Time'],label = 'Frequency') 						#label shows the Legend on the graph

	'''
	Setting the labels for the X-axis. Not plotting all the values, Clutter the x axis
	'''
	ax.set_xticks(Index)
	ax.set_xticklabels(ArtistName,rotation = 65)
	plt.legend(loc = 'best')												#Decides the position of the legend automagically
	
	'''
	Some more triviality regarding how the plot should look like
	'''
	# ax.set_ylim(ymin = -10)
	# ax.set_xlim(xmin = -5,xmax = len(DataDF['Total Time'])+10)
	'''
	Setting the Background color of the plot
	'''
	# ax.set_axis_bgcolor(dark2_colors[7])

	'''
	Set ylabel and xlabel
	'''

	ax.set_ylabel('Amount of time(min)')
	ax.set_xlabel('Artist')
	# ax.axis('off')
	# ax.yaxis.set_visible(False)

	'''
	Decided to show only the axis which were necesaary that is left and bottom 
	rest can R.I.P. 

	Same happens with the ticks! parasites f***a!! die!! :D
	'''
	# ax.spines['right'].set_visible(False)
	# ax.spines['top'].set_visible(False)
	# ax.xaxis.set_ticks_position('bottom')
	# ax.yaxis.set_ticks_position('left')

	layout.cross_spines(ax = ax)

	'''
	Finally save the image!! Phew
	Too much work just for nothing! :-/
	Well now I know the kind of music my ears love the most :DataDF
	Long live FloyD 
	'''
	# extent = ax.get_window_extent().transformed(plt.gcf().dpi_scale_trans.inverted())
	plt.savefig('Itunes Frequency Analysis.png')
except:
    filename = None
import matplotlib
if filename and filename.endswith('.svg'):
    matplotlib.use('SVG')
from mpltools import layout
from mpltools import style
style.use('ggplot')
import matplotlib.pyplot as plt
import numpy as np

xlim = 20
log = False

with open('data/%s_group_richness.pkl' % dataset, 'rb') as pickle_file:
    data = pkl.load(pickle_file)

for route, values in data.items():
    xs, ys = [v[0] for v in values], [v[1] for v in values]
    plt.plot(xs, ys)

plt.xlim(0, xlim)
plt.ylabel('group richness')
plt.xlabel('clade grouping percentile')
if log: plt.yscale('log')

layout.cross_spines(ax=plt.gca())

if filename: plt.savefig(filename)
else:
    plt.show()
Example #6
0
def graphs():
    brand = request.args.get('brand')
    if not brand:
        return 'Missing brand'

    calc_method = request.args.get('calc_method')
    if not calc_method:
        return 'Missing calculation method (events, costs,etc.)'

    file_path = ''

    if calc_method == 'events':
        file_path = '/events_input.json'
    elif calc_method == 'costs':
        file_path = '/costs_input.json'
    elif calc_method == 'reaches':
        file_path = '/reaches_input.json'
    else:
        return 'Invalid calculation method'

    this_folder = os.getcwd()

    with open(this_folder+file_path) as json_file:
        data = json.load(json_file)

    headers = dict()
    headers['Authorization'] = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NjU2Mzc1NjksImV4cCI6MTU5NzE3MzU2OSwiaXNzIjoiTUtURyIsInVzZXJfaWQiOiI1ZDFiOGEwZjNlZjAyOTRmNGZkNjUyM2YiLCJlbWFpbCI6ImNodW4ud3VAbWt0Zy5jb20iLCJyb2xlX2lkIjoiNWFlMGVkZjBiZDg3MmU4NzNlNjk4YjAyIiwic2NvcGVzIjp7InBob3RvcG9ydGFsIjp7ImFjY2VzcyI6IkFkbWluIn19fQ.VOg35B3BmvLc14Q7m9lYXt9UIOefz5oHeVrMkQ-T3Ww'
    headers['Content-Type'] = 'application/json'

    # endpoint = 'http://127.0.0.1:5000/solver/solve'
    endpoint = 'https://martini.mktg.run/solver/solve'

    if brand.lower() not in data:
        return 'Cannot find this brand'

    resp = requests.post(endpoint, headers=headers, json=data[brand.lower()])

    result = resp.json()['summary']

    calc_combs = {'brand-channel':'Brand & Channel',
                  'channel-region': 'Channel & Region',
                  'channel-month': 'Channel & Month',
                  'program': 'Program'
                  }

    solve = ['total_reach', 'total_cost', 'total_event']

    graph_url = []
    title = []
    for calc in calc_combs:
        print(calc)
        output = pd.DataFrame(result[calc])

        if 'month' in list(output):
            output['month'] = output['month'].apply(lambda x: calendar.month_name[x])

        if calc == 'brand-channel':
            labels = output['channel'].unique()

            fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharex='col')
            x = 0
            for s in solve:
                total = s + '\n' + f'{int(round(sum(output[s]))):,}'
                make_pie(output[s], total, ax[x])
                x += 1

            ax[0].legend(loc=2, labels=labels)

        elif len(calc.split('-')) < 2:
            labels = output[calc].unique()

            labels = ['\n'.join(wrap(l, 15)) for l in labels]

            if len(labels) >= 5:
                fig, ax = plt.subplots(3, 1, figsize=(9, 9), sharex='col')
            else:
                fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharex='col')

            ind = np.arange(len(labels))
            width = .5

            x = 0

            for s in solve:
                make_bar(ind, [output[s]], width, s, labels, ax[x])
                x += 1

        else:
            index = calc.split('-')
            legend_index = index[0]
            merge_index = index[0]

            for i in index:
                if len(output[i].unique()) <= 2:
                    legend = output[i].unique()
                    legend_index = i
                else:
                    labels = output[i].unique()
                    merge_index = i

            if len(labels) >= 5:
                fig, ax = plt.subplots(3, 1, figsize=(9, 9), sharex='col')
            else:
                fig, ax = plt.subplots(1, 3, figsize=(9, 3), sharex='col')

            ind = np.arange(len(labels))
            width = .5

            x = 0
            for s in solve:
                # on premise
                on = output[output[legend_index] == legend[0]]
                off = output[output[legend_index] != legend[0]]

                onoff = pd.merge(on, off, on=[merge_index], how='outer')
                onoff.fillna(0, inplace=True)

                make_bar(ind, [onoff[s + '_x'], onoff[s + '_y']], width, s, labels, ax[x])
                x += 1

            ax[0].legend(loc=2, labels=legend)

        # fig.suptitle(calc, fontsize=16)
        # Remove ticks on top and right sides of plot
        for a in ax.ravel():
            layout.cross_spines(ax=a)

        plt.tight_layout()

        img = io.BytesIO()

        plt.savefig(img, format='png')
        img.seek(0)
        graph_url.append('data:image/png;base64,{}'.format(base64.b64encode(img.getvalue()).decode()))
        title.append(calc_combs[calc])
        plt.close()

    return render_template('graphs.html',
                           title1=title[0],
                           title2=title[1],
                           title3=title[2],
                           title4=title[3],
                           graph1=graph_url[0],
                           graph2=graph_url[1],
                           graph3=graph_url[2],
                           graph4=graph_url[3]
                           )
Example #7
0
"""
==============
Crossed spines
==============

By default, matplotlib surrounds plot axes with borders. ``cross_spines` uses
the axes ``spines`` (the names ``axes`` and ``axis`` were already taken)
attribute to eliminate the top and right spines from the plot.

"""
import numpy as np
import matplotlib.pyplot as plt

from mpltools import layout


figsize = layout.figaspect(aspect_ratio=0.5)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=figsize)

x, y = np.random.normal(size=(2, 20))

layout.cross_spines(ax=ax1)
ax1.plot(x, y, "ro")

layout.cross_spines(zero_cross=True, ax=ax2)
ax2.plot(x, y, "ro")

plt.show()
                 transform=sub.transAxes)
        plt.text(0.5, 0.7, 'mean=%s' % round(np.mean(data+zeroes), 1),
                 horizontalalignment='center',
                 verticalalignment='center',
                 transform=sub.transAxes)
        
        if data:
            plt.hist(data, bins=bins, weights=weights, color='blue')
        if zeroes:
            plt.hist(zeroes, bins=bins, weights=zero_weights, color='red')
        
        plt.xscale('symlog', basex=2)
        sub.set_xticks([x*2 if x > 0 else 1 for x in bins])
        sub.set_xticklabels([int(x) for x in bins],rotation=45, rotation_mode="anchor", ha="right")
        sub.set_xlim(0,bins[-1])
    
    
    fig.text(0.5, 0.04, '%s count' % plot_type, ha='center', va='center')
    fig.text(0.06, 0.5, '% of datasets', ha='center', va='center', rotation='vertical')
    
    for sub in subs:
        sub.minorticks_off()
        layout.cross_spines(ax=sub)
    
    
    try:
        figname = sys.argv[2]
        plt.savefig(figname, dpi=200)
    except IndexError:
        plt.show()
    y1.append(c-b)
    y2.append(c-a)
    y3.append(c-y1[-1]-y2[-1])
def smooth(x, y):
    ys = []
    for xi, yi in zip(x, y):
        weights = [1 if xi == xi2 else (smooth_radius*xi**0.5) - abs(xi2 - xi) if abs(xi2-xi) < smooth_radius*xi**0.5 else 0 for xi2 in x]
        total_weight = sum(weights)
        ys.append(sum([y[n] * weights[n] / total_weight for n in range(len(x))]))
    return ys


plt.xlim(0, xlim)
plt.ylim(0, round(max(y1+y2+y3) + 0.05, 1))

a = plt.plot(xs, smooth(xs, y2), label='var', linewidth=2)
plt.scatter(xs, y2, marker='+', s=10, color=a[0].get_markeredgecolor())
a = plt.plot(xs, smooth(xs, y1), label='mean', linewidth=2)
plt.scatter(xs, y1, marker='+', s=10, color=a[0].get_markeredgecolor())
a = plt.plot(xs, smooth(xs, y3), label='shared', linewidth=2)
plt.scatter(xs, np.maximum(y3, 0), marker='+', s=10, color=a[0].get_markeredgecolor())

plt.ylabel('unique rsquared')
plt.xlabel('clade grouping percentile')
plt.legend(loc='upper left')
if log: plt.xscale('log')

layout.cross_spines(ax=plt.gca())

if filename: plt.savefig(filename)
else: plt.show()
Example #10
0
        if data:
            plt.hist(data, bins=bins, weights=weights, color='blue')
        if zeroes:
            plt.hist(zeroes, bins=bins, weights=zero_weights, color='red')

        plt.xscale('symlog', basex=2)
        sub.set_xticks([x * 2 if x > 0 else 1 for x in bins])
        sub.set_xticklabels([int(x) for x in bins],
                            rotation=45,
                            rotation_mode="anchor",
                            ha="right")
        sub.set_xlim(0, bins[-1])

    fig.text(0.5, 0.04, '%s count' % plot_type, ha='center', va='center')
    fig.text(0.06,
             0.5,
             '% of datasets',
             ha='center',
             va='center',
             rotation='vertical')

    for sub in subs:
        sub.minorticks_off()
        layout.cross_spines(ax=sub)

    try:
        figname = sys.argv[2]
        plt.savefig(figname, dpi=200)
    except IndexError:
        plt.show()
"""
==============
Crossed spines
==============

By default, matplotlib surrounds plot axes with borders. ``cross_spines` uses
the axes ``spines`` (the names ``axes`` and ``axis`` were already taken)
attribute to eliminate the top and right spines from the plot.

"""
import numpy as np
import matplotlib.pyplot as plt

from mpltools import layout

figsize = layout.figaspect(aspect_ratio=0.5)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=figsize)

x, y = np.random.normal(size=(2, 20))

layout.cross_spines(ax=ax1)
ax1.plot(x, y, 'ro')

layout.cross_spines(zero_cross=True, ax=ax2)
ax2.plot(x, y, 'ro')

plt.show()
Example #12
0
# sinusoidal lines with colors from default color cycle
L = 2*np.pi
x = np.linspace(0, L)
ncolors = len(plt.rcParams['axes.color_cycle'])
shift = np.linspace(0, L, ncolors, endpoint=False)
for s in shift:
    ax2.plot(x, np.sin(x + s), '-')
ax2.margins(0)

# bar graphs
x = np.arange(5)
y1, y2 = np.random.randint(1, 25, size=(2, 5))
width = 0.25
ax3.bar(x, y1, width)
ax3.bar(x+width, y2, width, color=plt.rcParams['axes.color_cycle'][2])
ax3.set_xticks(x+width)
ax3.set_xticklabels(['a', 'b', 'c', 'd', 'e'])

# circles with colors from default color cycle
for i, color in enumerate(plt.rcParams['axes.color_cycle']):
    xy = np.random.normal(size=2)
    ax4.add_patch(plt.Circle(xy, radius=0.3, color=color))
ax4.axis('equal')
ax4.margins(0)

# Remove ticks on top and right sides of plot
for ax in axes.ravel():
    layout.cross_spines(ax=ax)

plt.show()