report + format (html, tab)

This commit is contained in:
gauthiier 2016-09-11 14:24:59 +02:00
parent 9a2badf32a
commit 30116a6745
3 changed files with 140 additions and 24 deletions

View File

@ -1,5 +1,5 @@
import query
import logging, html
import logging, html, numpy
from tabulate import tabulate
class Html:
@ -14,7 +14,9 @@ class Html:
self.query = q
def threads_ranking(self, rank=5):
def threads_ranking(self, rank=5, resolution=None):
#if resolution is None:
data = self.query.threads_ranking(rank=rank)
@ -36,7 +38,8 @@ class Html:
return str(t)
def from_dataframe(self, data_frame, table_name=None, name_map={}):
@staticmethod
def from_dataframe(data_frame, table_name=None, name_map={}, url_map={}):
header = []
header.append(data_frame.index.name)
@ -57,6 +60,17 @@ class Html:
else:
t = h.table()
# url map
url_hash = {}
url_skip = []
url_keys = url_map.keys()
for u in url_keys:
if u in header and url_map[u] in header:
url_indx = header.index(url_map[u])
url_hash[header.index(u)] = url_indx
url_skip.append(url_indx)
header.pop(url_indx)
#header
r = t.tr
n = 0
@ -64,12 +78,27 @@ class Html:
r.td(str(j), klass=css_header[n])
n += 1
#elements
for k, row in data_frame.iterrows():
r = t.tr
r.td(str(k), klass=css_element[0])
n = 1
for l in row:
if n in url_skip:
continue
if type(l) == numpy.float64:
l = '{0:.4f}'.format(l)
if n in url_hash.keys():
url = row[url_hash[n] - 1]
print '---->' + l
print '<<<<<>' + url
r.td('', klass=css_element[n]).text(str(h.a(str(l), href=url)), escape=False)
else:
r.td(str(l), klass=css_element[n])
n += 1
@ -77,17 +106,8 @@ class Html:
class Tab:
query = None
def __init__(self, q=None):
if not isinstance(q, query.Query):
logging.error("HtmlFormat constructor Error: query must be of type nettime.query.Query")
raise Exception()
self.query = q
def from_dataframe(self, data_frame, name_map={}):
@staticmethod
def from_dataframe(data_frame, name_map={}):
header = []
header.append(data_frame.index.name)
@ -96,6 +116,6 @@ class Tab:
h = name_map[h]
header.append(h)
return tabulate(data_frame, headers=header)
return tabulate(data_frame, headers=header, floatfmt=".4f")

View File

@ -12,6 +12,10 @@ import query
def bar_plot_series(series, title, color='blueviolet'):
return series.plot(kind = 'bar', title=title, color=color, alpha=0.8, stacked=True)
def save(plot, name):
fig = plot.get_figure()
fig.savefig(name)
class Plot:
query = None

View File

@ -36,7 +36,7 @@ class Report:
mat['nbr-single-messages'] = mat['nbr-messages'] - mat['nbr-replies'] - mat['nbr-threads']
# avg. rep per message
mat['avg-rep-per-msg'] = mat['nbr-replies'] / mat['nbr-messages']
mat['avg--per-msg'] = mat['nbr-threads'] / mat['nbr-messages']
# avg. rep per thread
mat['avg-rep-per-thrd'] = mat['nbr-replies'] / mat['nbr-threads']
@ -55,36 +55,128 @@ class Report:
self.matrix_msgs_threads()
nettime.plot.bar_plot_series(self.matrix['nbr-messages'].to_frame(label), title=title, color=color)
return nettime.plot.bar_plot_series(self.matrix['nbr-messages'].to_frame(label), title=title, color=color)
def plot_nbr_threads(self, title='Nbr. Threads', label='threads', color='crimson'):
self.matrix_msgs_threads()
nettime.plot.bar_plot_series(self.matrix['nbr-threads'].to_frame(label), title=title, color=color)
return nettime.plot.bar_plot_series(self.matrix['nbr-threads'].to_frame(label), title=title, color=color)
def plot_nbr_replies(self, title='Nbr. Replies in Threads', label='replies', color='dimgray'):
self.matrix_msgs_threads()
nettime.plot.bar_plot_series(self.matrix['nbr-replies'].to_frame(label), title=title, color=color)
return nettime.plot.bar_plot_series(self.matrix['nbr-replies'].to_frame(label), title=title, color=color)
def plot_avg_rep_p_msg(self, title='Avg. Replies per Messages', label='replies-per-messasges', color='limegreen'):
def plot_avg_rep_p_msg(self, title='Avg. Thread per Message', label='replies-per-messasges', color='limegreen'):
self.matrix_msgs_threads()
nettime.plot.bar_plot_series(self.matrix['avg-rep-per-msg'].to_frame(label), title=title, color=color)
return nettime.plot.bar_plot_series(self.matrix['avg--per-msg'].to_frame(label), title=title, color=color)
def plot_avg_rep_p_thrd(self, title='Avg. Replies per Thread', label='replies-per-thread', color='blueviolet'):
self.matrix_msgs_threads()
nettime.plot.bar_plot_series(self.matrix['avg-rep-per-thrd'].to_frame(label), title=title, color=color)
return nettime.plot.bar_plot_series(self.matrix['avg-rep-per-thrd'].to_frame(label), title=title, color=color)
def plot_msgs_replies(self, title='Nbr. Messages segments (individual messages vs thread replies)'):
self.matrix_msgs_threads()
nettime.plot.bar_plot_series(self.matrix[['nbr-single-messages', 'nbr-threads', 'nbr-replies']], color=['mediumblue', 'red', 'dimgray'], title=title)
return nettime.plot.bar_plot_series(self.matrix[['nbr-single-messages', 'nbr-threads', 'nbr-replies']], color=['mediumblue', 'red', 'dimgray'], title=title)
'''
text (tabular)
'''
def tab_msgs_threads_replies(self):
self.matrix_msgs_threads()
return nettime.format.Tab.from_dataframe(self.matrix[['nbr-messages', 'nbr-threads', 'nbr-replies']],
name_map={'nbr-messages': 'messages', 'nbr-threads': 'threads', 'nbr-replies': 'replies in threads'})
def tab_avg_rep_msg_thrd(self):
self.matrix_msgs_threads()
return nettime.format.Tab.from_dataframe(self.matrix[['avg--per-msg', 'avg-rep-per-thrd']],
name_map={'avg--per-msg': 'avg. thread per message', 'avg-rep-per-thrd': 'avg. replies per thread'})
def tab_activity_from_ranking(self, rank=5):
d = self.query.activity_from_ranking(rank=rank)
return nettime.format.Tab.from_dataframe(d, name_map={'nbr-messages': 'messages'})
def tab_content_length_from_ranking(self, rank=5):
d = self.query.activity_from_ranking(rank=rank)
return nettime.format.Tab.from_dataframe(d, name_map={'nbr-bytes': 'bytes'})
def tab_threads_ranking(self, rank=5):
d = self.query.threads_ranking(rank=rank)
return nettime.format.Tab.from_dataframe(d, name_map={'nbr-references': 'nbr. replies'})
def tab_threads_ranking_year(self, rank=5, resolution='y'):
d = self.query.threads_ranking(rank=rank, resolution=resolution)
years = sorted(d)
nl = '\n'
s = ""
for i in years:
s += 'year: ' + i + nl
s += nettime.format.Tab.from_dataframe(d[i], name_map={'nbr-references': 'nbr. replies'}) + nl
return s + nl
'''
html
'''
def html_msgs_threads_replies(self):
self.matrix_msgs_threads()
return nettime.format.Html.from_dataframe(self.matrix[['nbr-messages', 'nbr-threads', 'nbr-replies']],
name_map={'nbr-messages': 'messages', 'nbr-threads': 'threads', 'nbr-replies': 'replies in threads'})
def html_avg_rep_msg_thrd(self):
self.matrix_msgs_threads()
return nettime.format.Html.from_dataframe(self.matrix[['avg--per-msg', 'avg-rep-per-thrd']],
name_map={'avg--per-msg': 'avg. thread per message', 'avg-rep-per-thrd': 'avg. replies per thread'})
def html_activity_from_ranking(self, rank=5):
html = nettime.format.Html(self.query)
return html.threads_ranking(rank=rank)
def html_content_length_from_ranking(self, rank=5):
d = self.query.activity_from_ranking(rank=rank)
return nettime.format.Html.from_dataframe(d, name_map={'nbr-bytes': 'bytes'})
def html_threads_ranking(self, rank=5):
d = self.query.threads_ranking(rank=rank)
return nettime.format.Html.from_dataframe(d, name_map={'nbr-references': 'nbr. replies'}, url_map={'subject': 'url'})
def html_threads_ranking_year(self, rank=5, resolution='y'):
d = self.query.threads_ranking(rank=rank, resolution=resolution)
years = sorted(d)
nl = '\n'
s = ""
for i in years:
s += '<div class="year_t">' + i + '</div>' + nl
s += nettime.format.Html.from_dataframe(d[i], name_map={'nbr-references': 'nbr. replies'}, url_map={'subject': 'url'}) + nl
return s + nl