<commit_before># pylint: disable-msg=W0611, W0612, W0511,R0201
"""Tests suite for timeseries.tdates.

:author: <NAME> & <NAME>
:contact: pierregm_at_uga_dot_edu - mattknow_ca_at_hotmail_dot_com
:version: $Id$
"""
__author__ = "<NAME> ($Author$)"
__version__ = '1.0'
__revision__ = "$Revision$"
__date__     = '$Date$'

import types
import datetime

import numpy
import numpy.core.fromnumeric  as fromnumeric
import numpy.core.numeric as numeric
from numpy.testing import NumpyTest, NumpyTestCase
from numpy.testing.utils import build_err_msg

import maskedarray
from maskedarray import masked_array

import maskedarray.testutils
from maskedarray.testutils import assert_equal, assert_array_equal

import timeseries.tdates as tdates
#from timeseries import tdates
#from timeseries.tdates import date_array_fromlist, Date, DateArray, date_array,\
#    mxDFromString, today
from timeseries.tdates import *
from timeseries.tdates import mxDFromString
from timeseries import tcore


class test_creation(NumpyTestCase):
    "Base test class for MaskedArrays."
    
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)
    
    def test_fromstrings(self):
        "Tests creation from list of strings"
        dlist = ['2007-01-%02i' % i for i in range(1,15)]
        # A simple case: daily data
        dates = date_array_fromlist(dlist, 'D')
        assert_equal(dates.freqstr,'D')
        assert(dates.isfull())
        assert(not dates.has_duplicated_dates())
        assert_equal(dates, 732677+numpy.arange(len(dlist)))
        # as simple, but we need to guess the frequency this time
        dates = date_array_fromlist(dlist, 'D')
        assert_equal(dates.freqstr,'D')
        assert(dates.isfull())
        assert(not dates.has_duplicated_dates())
        assert_equal(dates, 732677+numpy.arange(len(dlist)))
        # Still daily data, that we force to month
        dates = date_array_fromlist(dlist, 'M')
        assert_equal(dates.freqstr,'M')
        assert(not dates.isfull())
        assert(dates.has_duplicated_dates())
        assert_equal(dates, [24073]*len(dlist))
        # Now, for monthly data
        dlist = ['2007-%02i' % i for i in range(1,13)]
        dates = date_array_fromlist(dlist, 'M')
        assert_equal(dates.freqstr,'M')
        assert(dates.isfull())
        assert(not dates.has_duplicated_dates())
        assert_equal(dates, 24073 + numpy.arange(12))
        # Monthly data  w/ guessing
        dlist = ['2007-%02i' % i for i in range(1,13)]
        dates = date_array_fromlist(dlist, )
        assert_equal(dates.freqstr,'M')
        assert(dates.isfull())
        assert(not dates.has_duplicated_dates())
        assert_equal(dates, 24073 + numpy.arange(12))
        
    def test_fromstrings_wmissing(self):
        "Tests creation from list of strings w/ missing dates"
        dlist = ['2007-01-%02i' % i for i in (1,2,4,5,7,8,10,11,13)]
        dates = date_array_fromlist(dlist)
        assert_equal(dates.freqstr,'U')
        assert(not dates.isfull())
        assert(not dates.has_duplicated_dates())
        assert_equal(dates.tovalue(),732676+numpy.array([1,2,4,5,7,8,10,11,13]))
        #
        ddates = date_array_fromlist(dlist, 'D')
        assert_equal(ddates.freqstr,'D')
        assert(not ddates.isfull())
        assert(not ddates.has_duplicated_dates())
        #
        mdates = date_array_fromlist(dlist, 'M')
        assert_equal(mdates.freqstr,'M')
        assert(not dates.isfull())
        assert(mdates.has_duplicated_dates())
        #
    
    def test_fromsobjects(self):
        "Tests creation from list of objects."
        dlist = ['2007-01-%02i' % i for i in (1,2,4,5,7,8,10,11,13)]
        dates = date_array_fromlist(dlist)
        dobj = [datetime.datetime.fromordinal(d) for d in dates.toordinal()]
        odates = date_array_fromlist(dobj)
        assert_equal(dates,odates)
        dobj = [mxDFromString(d) for d in dlist]
        odates = date_array_fromlist(dobj)
        assert_equal(dates,odates)

    def test_consistent_value(self):
        "Tests that values don't get mutated when constructing dates from a value"
        freqs = [x[0] for x in tcore.freq_dict.values() if x[0] != 'U']
        print freqs
        for f in freqs:
            today = tdates.thisday(f)
            assert(tdates.Date(freq=f, value=today.value) == today)
            
    def test_shortcuts(self):
        "Tests some creation shortcuts. Because I'm lazy like that."
        # Dates shortcuts
        assert_equal(Date('D','2007-01'), Date('D',string='2007-01'))
        assert_equal(Date('D','2007-01'), Date('D', value=732677))
        assert_equal(Date('D',732677), Date('D', value=732677))
        # DateArray shortcuts
        n = today('M')
        d = date_array(start_date=n, length=3)
        assert_equal(date_array(n,length=3), d)
        assert_equal(date_array(n, n+2), d)

class test_date_properties(NumpyTestCase):
    "Test properties such as year, month, day_of_week, etc...."
    
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)

    def test_properties(self):
    <commit_msg>updated to reflect recent changes to tdates.py<commit_after># pylint: disable-msg=W0611, W0612, W0511,R0201
"""Tests suite for timeseries.tdates.

:author: <NAME> & <NAME>
:contact: pierregm_at_uga_dot_edu - mattknow_ca_at_hotmail_dot_com
:version: $Id$
"""
__author__ = "<NAME> ($Author$)"
__version__ = '1.0'
__revision__ = "$Revision$"
__date__     = '$Date$'

import types
import datetime

import numpy
import numpy.core.fromnumeric  as fromnumeric
import numpy.core.numeric as numeric
from numpy.testing import NumpyTest, NumpyTestCase
from numpy.testing.utils import build_err_msg

import maskedarray
from maskedarray import masked_array

import maskedarray.testutils
from maskedarray.testutils import assert_equal, assert_array_equal

import timeseries.tdates as tdates
from timeseries.tdates import *
from timeseries.parser import DateFromString
from timeseries import tcore


class test_creation(NumpyTestCase):
    "Base test class for MaskedArrays."
    
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)
    
    def test_fromstrings(self):
        "Tests creation from list of strings"
        dlist = ['2007-01-%02i' % i for i in range(1,15)]
        # A simple case: daily data
        dates = date_array_fromlist(dlist, 'D')
        assert_equal(dates.freqstr,'D')
        assert(dates.isfull())
        assert(not dates.has_duplicated_dates())
        assert_equal(dates, 732677+numpy.arange(len(dlist)))
        # as simple, but we need to guess the frequency this time
        dates = date_array_fromlist(dlist, 'D')
        assert_equal(dates.freqstr,'D')
        assert(dates.isfull())
        assert(not dates.has_duplicated_dates())
        assert_equal(dates, 732677+numpy.arange(len(dlist)))
        # Still daily data, that we force to month
        dates = date_array_fromlist(dlist, 'M')
        assert_equal(dates.freqstr,'M')
        assert(not dates.isfull())
        assert(dates.has_duplicated_dates())
        assert_equal(dates, [24073]*len(dlist))
        # Now, for monthly data
        dlist = ['2007-%02i' % i for i in range(1,13)]
        dates = date_array_fromlist(dlist, 'M')
        assert_equal(dates.freqstr,'M')
        assert(dates.isfull())
        assert(not dates.has_duplicated_dates())
        assert_equal(dates, 24073 + numpy.arange(12))
        # Monthly data  w/ guessing
        dlist = ['2007-%02i' % i for i in range(1,13)]
        dates = date_array_fromlist(dlist, )
        assert_equal(dates.freqstr,'M')
        assert(dates.isfull())
        assert(not dates.has_duplicated_dates())
        assert_equal(dates, 24073 + numpy.arange(12))
        
    def test_fromstrings_wmissing(self):
        "Tests creation from list of strings w/ missing dates"
        dlist = ['2007-01-%02i' % i for i in (1,2,4,5,7,8,10,11,13)]
        dates = date_array_fromlist(dlist)
        assert_equal(dates.freqstr,'U')
        assert(not dates.isfull())
        assert(not dates.has_duplicated_dates())
        assert_equal(dates.tovalue(),732676+numpy.array([1,2,4,5,7,8,10,11,13]))
        #
        ddates = date_array_fromlist(dlist, 'D')
        assert_equal(ddates.freqstr,'D')
        assert(not ddates.isfull())
        assert(not ddates.has_duplicated_dates())
        #
        mdates = date_array_fromlist(dlist, 'M')
        assert_equal(mdates.freqstr,'M')
        assert(not dates.isfull())
        assert(mdates.has_duplicated_dates())
        #
    
    def test_fromsobjects(self):
        "Tests creation from list of objects."
        dlist = ['2007-01-%02i' % i for i in (1,2,4,5,7,8,10,11,13)]
        dates = date_array_fromlist(dlist)
        dobj = [datetime.datetime.fromordinal(d) for d in dates.toordinal()]
        odates = date_array_fromlist(dobj)
        assert_equal(dates,odates)
        dobj = [DateFromString(d) for d in dlist]
        odates = date_array_fromlist(dobj)
        assert_equal(dates,odates)

    def test_consistent_value(self):
        "Tests that values don't get mutated when constructing dates from a value"
        freqs = [x[0] for x in tcore.freq_dict.values() if x[0] != 'U']
        print freqs
        for f in freqs:
            today = tdates.thisday(f)
            assert(tdates.Date(freq=f, value=today.value) == today)
            
    def test_shortcuts(self):
        "Tests some creation shortcuts. Because I'm lazy like that."
        # Dates shortcuts
        assert_equal(Date('D','2007-01'), Date('D',string='2007-01'))
        assert_equal(Date('D','2007-01'), Date('D', value=732677))
        assert_equal(Date('D',732677), Date('D', value=732677))
        # DateArray shortcuts
        n = today('M')
        d = date_array(start_date=n, length=3)
        assert_equal(date_array(n,length=3), d)
        assert_equal(date_array(n, n+2), d)

class test_date_properties(NumpyTestCase):
    "Test properties such as year, month, day_of_week, etc...."
    
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)

    def test_properties(self):
    
{'fraction_non_alphanumeric': 0.12056111438100843, 'fraction_numerical': 0.034726309593878756, 'mean_word_length': 3.0960224989955805, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6634784', 'n_tokens_mistral': 3559, 'n_tokens_neox': 3317, 'n_words': 739}