<commit_before>#!/usr/bin/env python

"""
:author: <NAME> <<EMAIL>>

:copyright:
    Copyright 2012 <NAME>

    Licensed under the Apache License, Version 2.0 (the "License"); you
    may not use this file except in compliance with the License.  You
    may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    implied.  See the License for the specific language governing
    permissions and limitations under the License.
"""

from __future__ import division

import sys
import matplotlib.pyplot as plt
from numpy import NaN, arange, zeros
from pandas import *


DATAPATH = './data.tsv'


def convert_time_string(timestring):
    if timestring == 'NA':
        rval = NaN
    else:
        hour, minute = map(int, timestring.split(':'))
        rval = hour + minute/60
    return rval


def read_data(filename='weight.tsv'):
    return read_table(filename, header=1, index_col=0, parse_dates=True,
            converters={'Wake': convert_time_string})


def show_data(start=0, floatstyle='.', nofloatstyle='k,', floaters=True,
        path=DATAPATH):
    plt.close('all')
    data = read_data(path)

    wakeup_goal = 7
    window = 20 # days
    fig, axs = plt.subplots(nrows=3, sharex=True)

    # Wakeup-time plot
    wake_delta = data.Wake - wakeup_goal
    if floaters:
        axs[2].errorbar(data.index[start:], data.Wake[start:],
                [wake_delta[start:], zeros(len(data.index))[start:]],
                ecolor='g', capsize=0, fmt=floatstyle)
        axs[2].axhline(7, color='r')
    else:
        data.Wake[start:].plot(ax=axs[2], style=nofloatstyle)
        axs[2].axhline(8, color='r')
        axs[2].axhline(5, color='r')
    axs[2].set_ylim((0,24))
    axs[2].set_yticks(arange(1,24,3))
    axs[2].set_yticklabels(map(lambda t: '%02d'%(t,), arange(1,24,3)))
    axs[2].set_ylabel('Wake Time')
    axs[2].grid(axis='both')

    # Weight plot
    weight_avg = ewma(data.Weight, window)
    weight_delta = data.Weight - weight_avg
    if floaters:
        axs[0].errorbar(data.index[start:], data.Weight[start:],
                [weight_delta[start:], zeros(len(data.index))[start:]],
                ecolor='g', capsize=0, fmt=floatstyle)
    else:
        data.Weight[start:].plot(ax=axs[0], style=nofloatstyle)
    weight_avg[start:].plot(ax=axs[0])
    axs[0].set_ylabel('Weight')

    # Body fat plot
    bf_avg = ewma(data.BF, window)
    bf_delta = data.BF - bf_avg
    if floaters:
        axs[1].errorbar(data.index[start:], data.BF[start:],
                [bf_delta[start:], zeros(len(data.index))[start:]],
                ecolor='g', capsize=0, fmt=floatstyle)
    else:
        data.BF[start:].plot(ax=axs[1], style=nofloatstyle)
    bf_avg[start:].plot(ax=axs[1])
    axs[1].set_ylabel('% Body Fat')

    axs[0].set_title('Life Data')
    fig.text(0.5, 0.05, 'wt: %.1f    bf: %.1f'%(weight_avg[-1], bf_avg[-1]),
            horizontalalignment='center', verticalalignment='bottom')
    #print "Weight est: %4.1f lbs"%(weight_avg[-1],)
    #print "    BF est: %4.1f%%"%(bf_avg[-1],)

    for ax in axs:
        ax.tick_params(axis='y', labelleft='on', labelright='on')

    plt.show()


def summarize(start=0):
    data = read_data()
    return data[start:].describe()


# Simple command-line interface
if __name__ == '__main__':
    if len(sys.argv) > 1:
        start = int(sys.argv[1])
    else:
        start = 0

    if start is 0:
        floatstyle = None
    else:
        floatstyle = '.'

    show_data(start, floatstyle=floatstyle)<commit_msg>Add docstrings.<commit_after>#!/usr/bin/env python

"""
:author: <NAME> <<EMAIL>>

:copyright:
    Copyright 2012 <NAME>

    Licensed under the Apache License, Version 2.0 (the "License"); you
    may not use this file except in compliance with the License.  You
    may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    implied.  See the License for the specific language governing
    permissions and limitations under the License.
"""

from __future__ import division

import sys
import matplotlib.pyplot as plt
from numpy import NaN, arange, zeros
from pandas import *


DATAPATH = './data.tsv'


def convert_time_string(timestring):
    """Parse a time value from the data file."""
    if timestring == 'NA':
        rval = NaN
    else:
        hour, minute = map(int, timestring.split(':'))
        rval = hour + minute/60
    return rval


def read_data(path=DATAPATH):
    """Parse the data."""
    return read_table(path, header=1, index_col=0, parse_dates=True,
            converters={'Wake': convert_time_string})


def show_data(start=0, path=DATAPATH, floaters=True, floatstyle='.',
        nofloatstyle='k,'):
    """Plot the data.

    `start`   : Integer offset from beginning of data (in days) to allow
                plotting only recent data.  If negative, works as an offset
                from end of data.
    `path`    : Path to the tab-separated value file containing the data.
    `floaters`: Boolean indicating whether you want 'floaters and sinkers'
                plotted like in the original Hacker's Diet online.
    `floatstyle`: If floaters are used, pass this style parameter to
                matplotlib.
    `nofloatstyle`: If floaters are not used, pass this style parameter to
                matplotlib.
    """
    wakeup_goal = 7  # reference line in Wakeup-time plot
    window = 20 # ndays in exponentially-weighted moving average
    fig, axs = plt.subplots(nrows=3, sharex=True)

    data = read_data(path)

    # Wakeup-time plot
    wake_delta = data.Wake - wakeup_goal
    if floaters:
        axs[2].errorbar(data.index[start:], data.Wake[start:],
                [wake_delta[start:], zeros(len(data.index))[start:]],
                ecolor='g', capsize=0, fmt=floatstyle)
        axs[2].axhline(7, color='r')
    else:
        data.Wake[start:].plot(ax=axs[2], style=nofloatstyle)
        axs[2].axhline(8, color='r')
        axs[2].axhline(5, color='r')
    axs[2].set_ylim((0,24))
    axs[2].set_yticks(arange(1,24,3))
    axs[2].set_yticklabels(map(lambda t: '%02d'%(t,), arange(1,24,3)))
    axs[2].set_ylabel('Wake Time')
    axs[2].grid(axis='both')

    # Weight plot
    weight_avg = ewma(data.Weight, window)
    weight_delta = data.Weight - weight_avg
    if floaters:
        axs[0].errorbar(data.index[start:], data.Weight[start:],
                [weight_delta[start:], zeros(len(data.index))[start:]],
                ecolor='g', capsize=0, fmt=floatstyle)
    else:
        data.Weight[start:].plot(ax=axs[0], style=nofloatstyle)
    weight_avg[start:].plot(ax=axs[0])
    axs[0].set_ylabel('Weight')

    # Body fat plot
    bf_avg = ewma(data.BF, window)
    bf_delta = data.BF - bf_avg
    if floaters:
        axs[1].errorbar(data.index[start:], data.BF[start:],
                [bf_delta[start:], zeros(len(data.index))[start:]],
                ecolor='g', capsize=0, fmt=floatstyle)
    else:
        data.BF[start:].plot(ax=axs[1], style=nofloatstyle)
    bf_avg[start:].plot(ax=axs[1])
    axs[1].set_ylabel('% Body Fat')

    axs[0].set_title('Life Data')
    fig.text(0.5, 0.05, 'wt: %.1f    bf: %.1f'%(weight_avg[-1], bf_avg[-1]),
            horizontalalignment='center', verticalalignment='bottom')
    #print "Weight est: %4.1f lbs"%(weight_avg[-1],)
    #print "    BF est: %4.1f%%"%(bf_avg[-1],)

    for ax in axs:
        ax.tick_params(axis='y', labelleft='on', labelright='on')

    plt.show()


def summarize(start=0):
    """Return the pandas summary of the data."""
    data = read_data()
    return data[start:].describe()


# Simple command-line interface.  Interprets a single command-line argument as
# an offset for the range of data to be plotted.  If plotting all the data it
# doesn't use floaters (it looks too cluttered).  Else, it does.
if __name__ == '__main__':
    if len(sys.argv) > 1:
        start = int(sys.argv[1])
    else:
        start = 0

    if start is 0:
        floatstyle = None
    else:
        floatstyle = '.'

    show_data(start, floatstyle=floatstyle)
{'fraction_non_alphanumeric': 0.14019133104995865, 'fraction_numerical': 0.017715837959135467, 'mean_word_length': 2.945945945945946, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2297018', 'n_tokens_mistral': 2996, 'n_tokens_neox': 2864, 'n_words': 714}