def test_change_epoch(): date = np.datetime64('2000-01-01') # use private method to clear the epoch and allow it to be set... mdates._reset_epoch_test_example() mdates.get_epoch() # Set default. with pytest.raises(RuntimeError): # this should fail here because there is a sentinel on the epoch # if the epoch has been used then it cannot be set. mdates.set_epoch('0000-01-01') mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01') dt = (date - np.datetime64('1970-01-01')).astype('datetime64[D]') dt = dt.astype('int') np.testing.assert_equal(mdates.date2num(date), float(dt)) mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') np.testing.assert_equal(mdates.date2num(date), 730120.0) mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T01:00:00') np.testing.assert_allclose(mdates.date2num(date), dt - 1. / 24.) mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T00:00:00') np.testing.assert_allclose( mdates.date2num(np.datetime64('1970-01-01T12:00:00')), 0.5)
def test_epoch2num(): mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') assert mdates.epoch2num(86400) == 719164.0 assert mdates.num2epoch(719165.0) == 86400 * 2 # set back to the default mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T00:00:00') assert mdates.epoch2num(86400) == 1.0 assert mdates.num2epoch(2.0) == 86400 * 2
def test_epoch2num(): with _api.suppress_matplotlib_deprecation_warning(): mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') assert mdates.epoch2num(86400) == 719164.0 assert mdates.num2epoch(719165.0) == 86400 * 2 # set back to the default mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T00:00:00') assert mdates.epoch2num(86400) == 1.0 assert mdates.num2epoch(2.0) == 86400 * 2
def test_julian2num(): mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') # 2440587.5 is julian date for 1970-01-01T00:00:00 # https://en.wikipedia.org/wiki/Julian_day assert mdates.julian2num(2440588.5) == 719164.0 assert mdates.num2julian(719165.0) == 2440589.5 # set back to the default mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T00:00:00') assert mdates.julian2num(2440588.5) == 1.0 assert mdates.num2julian(2.0) == 2440589.5
def test_axhline(): # make sure that axhline doesn't set the xlimits... fig, ax = plt.subplots() ax.axhline(1.5) ax.plot([np.datetime64('2016-01-01'), np.datetime64('2016-01-02')], [1, 2]) np.testing.assert_allclose(ax.get_xlim(), [ mdates.date2num(np.datetime64('2016-01-01')), mdates.date2num(np.datetime64('2016-01-02')) ]) mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') fig, ax = plt.subplots() ax.axhline(1.5) ax.plot([np.datetime64('2016-01-01'), np.datetime64('2016-01-02')], [1, 2]) np.testing.assert_allclose(ax.get_xlim(), [ mdates.date2num(np.datetime64('2016-01-01')), mdates.date2num(np.datetime64('2016-01-02')) ]) mdates._reset_epoch_test_example()
def test_date_empty(): # make sure we do the right thing when told to plot dates even # if no date data has been presented, cf # http://sourceforge.net/tracker/?func=detail&aid=2850075&group_id=80706&atid=560720 fig, ax = plt.subplots() ax.xaxis_date() fig.draw_without_rendering() np.testing.assert_allclose(ax.get_xlim(), [ mdates.date2num(np.datetime64('2000-01-01')), mdates.date2num(np.datetime64('2010-01-01')) ]) mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') fig, ax = plt.subplots() ax.xaxis_date() fig.draw_without_rendering() np.testing.assert_allclose(ax.get_xlim(), [ mdates.date2num(np.datetime64('2000-01-01')), mdates.date2num(np.datetime64('2010-01-01')) ]) mdates._reset_epoch_test_example()
def wrapper(): mdates._reset_epoch_test_example() mdates.set_epoch('2000-01-01') thefunc() mdates._reset_epoch_test_example()
import itertools import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates import warnings import sklearn.metrics as metrics import config as cfg import datetime as dt import math import pandas as pd import matplotlib.colors as colors COLORS = colors.CSS4_COLORS mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31T00:00:00') # old epoch (pre MPL 3.3) warnings.simplefilter("ignore") NR_COLUMNS: int = 3 HEIGHT: int = 4 def choose_grid(nr): if nr < NR_COLUMNS: return 1, nr else: return (nr // NR_COLUMNS, NR_COLUMNS) if nr % NR_COLUMNS == 0 else (nr // NR_COLUMNS + 1, NR_COLUMNS)
def _reset_epoch_for_tutorial(): """ Users (and downstream libraries) should not use the private method of resetting the epoch. """ mdates._reset_epoch_test_example()