﻿data = """Sage 4.6.2: February 28, 2011
84.8
Sage 4.6.1: January 13, 2011
84.4
Sage 4.6: October 31, 2010
84.3
Sage 4.5.3: September 07, 2010
83.8
Sage 4.5.2: August 07, 2010
83.8
Sage 4.5.1: July 07, 2010
82.7
Sage 4.4.4: July 07, 2010
82.7
Sage 4.4.3: June 04, 2010
82.1
Sage 4.4.2: May 19, 2010
82.1
Sage 4.4.1: May 02, 2010
81.7
Sage 4.4: April 25, 2010
81.7
Sage 4.3.5: March 28, 2010
81.5
Sage 4.3.4: March 19, 2010
81.5
Sage 4.3.3: February 21st, 2010
81.6
Sage 4.3.2: February 06th, 2010
81.5
Sage 4.3.1: January 20th, 2010
81.3
Sage 4.3: December 24th, 2009
79.5
Sage 4.2: October 24th, 2009
79.3
Sage 4.1.2: October 14th, 2009
78.6
Sage 4.1.1: August 14th, 2009
77.8
Sage 4.1: July 09th, 2009
77.5
Sage 4.0.2: June 18th, 2009
77.2
Sage-4.0.1: June 06th, 2009
76.9
Sage-4.0: May 29th, 2009
71.1
Sage-3.4.2: May 05, 2009
64.3
SAGE-3.2.3: January 5th, 2009
64.2
SAGE-3.2.2: December 30th, 2008
63.3
SAGE-3.2.1: December 1st, 2008
63.0
SAGE-3.2: November 20th, 2008
62.3
SAGE-3.1.4: October 20th, 2008
60.5
SAGE-3.1.3: October 14th, 2008
60.5
SAGE-3.1.2: September 19th, 2008
56.5
Sage 3.0.2: May 24th, 2008
51.8
SAGE-2.11: March 30, 2008
47.6
SAGE-2.10.4: March 17, 2008
44.5
SAGE-2.10.3: March 11, 2008
40.1
SAGE-2.10.2: February 22, 2008
38.3
SAGE-2.10.1: February 2, 2008
35.4
SAGE-2.10: January 18th, 2008
35.2
SAGE-2.9.3: January 6th, 2008
35.2
SAGE-2.9.1.1: December 25th, 2007
35.3
SAGE 2.8.15: 2007-12-04
34.8
SAGE 2.8.14: 2007-11-25
34.4
"""

from dateutil.parser import parse

###############
# class Version
###############

class Version:
    r"""

    EXAMPLES::

        sage: Version('SAGE-2.9.1.1: December 25th, 2007', '35.3')
        SAGE-2.9.1.1
        2007-12-25
        35.3

    """
    def __init__(self, version_date, coverage):
        r"""
        """
        self._version, date = version_date.split(':')
        try:
            self._date = parse(date)
        except ValueError:
            print "Error reading : ", version, coverage
            raise
        self._coverage = coverage
    def __repr__(self):
        return "%s\n%s\n%s" % (self._version, self._date.date(), self._coverage)
    def coverage(self):
        r"""
        EXAMPLES::

            sage: info = Version('SAGE-2.9.1.1: December 25th, 2007', '35.3')
            sage: info.year()
            2007.9166666666667
            sage: info.coverage()
            35.299999999999997
        """
        return float(self._coverage)

    def year(self):
        r"""
        EXAMPLES::

            sage: info = Version('SAGE-2.9.1.1: December 25th, 2007', '35.3')
            sage: info.year()
            2007.9166666666667
        """
        return self._date.year + (self._date.month-1) / 12.0

################################
# The evolution points
################################

LINES = data.splitlines()
N = len(LINES) / 2
L = []
for i in range(1, 2*N+1, 2):
    info = Version(LINES[-i-1], LINES[-i])
    L.append(info)

PLOT = points([(i.year(), i.coverage()) for i in L])

#############################
# Three selected point in red
#############################

start = (L[0].year(), L[0].coverage())
PLOT += point(start, size=20, color='red')
PLOT += text( "\n%s" % L[0], start, color='green', vertical_alignment='top')

x0, y0 = left = (L[20].year(), L[20].coverage())
PLOT += point(left, size=20, color='red')
PLOT += text( "%s\n" % L[20], left, color='green', vertical_alignment='bottom')

x1, y1 = right = (L[-1].year(), L[-1].coverage())
PLOT += point(right, size=20, color='red')
PLOT += text( "%s\n" % L[-1], right, color='green', vertical_alignment='bottom')

######################
# Extrapolation points
######################

y = (x - x0) * (y1 - y0) / (x1 - x0) + y0

def extrapol(coverage):
    r"""
    EXAMPLES::

        sage: extrapol(95)
        2013.2352320675102
        sage: extrapol(100)
        2014.2900843881853
    """
    y = coverage
    x = (y - y0) * (x1 - x0) / (y1 - y0) + x0
    return x

from sage.all import numerical_approx as n

extrapol_90 = (n(extrapol(90), digits=6), 90)
extrapol_95 = (n(extrapol(95), digits=6), 95)
extrapol_100 = (n(extrapol(100), digits=6), 100)
PLOT += point(extrapol_90, size=20, color='red')
PLOT += point(extrapol_95, size=20, color='red')
PLOT += point(extrapol_100, size=20, color='red')
PLOT += text( "March 2012\n%s\n" % extrapol_90[1], extrapol_90,
        color='green', vertical_alignment='bottom')
PLOT += text( "March 2013\n%s\n" % extrapol_95[1], extrapol_95, color='green', vertical_alignment='bottom')
PLOT += text( "April 2014\n%s\n" % extrapol_100[1], extrapol_100,
        color='green', vertical_alignment='bottom')

####################
# Extrapolation line
####################

x = var('x')
y = (x - x0) * (y1 - y0) / (x1 - x0) + y0
#PLOT += line([left, right], color='red')
#PLOT += plot(y, (2009, 2015), color='red')
PLOT += line([(2009, y(x=2009)),(2011.1, y(x=2011.1))], color='red')
PLOT += line([(2011.1, y(x=2011.1)),(2015, y(x=2015))], linestyle=':', color='red')
slope = n(diff(y, x), digits=3)

#######
# Slope
#######

PLOT += text("slope = %s\ni.e. we gain %s a year" % (slope,slope), (2013, 85), color='red')

#######################
# Title and axes labels
#######################

PLOT.axes_labels(["Year", 'Percentage (%)'])
# PLOT += text('Evolution of the Overall Doctest Coverage of Sage', (2011, 120))

# The following is broken because the x-axis doesn't start at zero.
# Now fixed by #10889
#PLOT += text('Evolution of the Overall Doctest Coverage of Sage', (0.5, 1.0), axis_coords=True)
PLOT += plot(Graphics(), title='Evolution of the Overall Doctest Coverage of Sage')
####################################
# Cut the y-axis at 100 (tentatives)
####################################

# With the following, we lose the title and some extrapolation points
# PLOT.ymax(100)
# PLOT.ymin(0)

# Doesn't work neither
# PLOT.set_axes_range(ymin=0, ymax=100)

# Manually
PLOT.ymax(120)
PLOT.ymin(0)

