varwg package¶
Subpackages¶
- varwg.core package
- Submodules
- varwg.core.base module
- varwg.core.core module
- varwg.core.plotting module
PlottingPlotting.fit_seasonal()Plotting.plot_VAR_par()Plotting.plot_all()Plotting.plot_autocorr()Plotting.plot_candidates()Plotting.plot_corr()Plotting.plot_cross_corr()Plotting.plot_daily_cycles()Plotting.plot_daily_fit()Plotting.plot_diff_spaces()Plotting.plot_doy_scatter()Plotting.plot_doy_scatter_residuals()Plotting.plot_episode_hists()Plotting.plot_exceedance_daily()Plotting.plot_exeedance_hourly()Plotting.plot_hourly_fit()Plotting.plot_hyd_year_sums()Plotting.plot_meteogram_daily()Plotting.plot_meteogram_hourly()Plotting.plot_meteogram_trans()Plotting.plot_monthly_hists()Plotting.plot_monthly_hists_hourly()Plotting.plot_psd()Plotting.plot_qq()Plotting.plot_scaling()Plotting.plot_seasonal_corrs()Plotting.plot_spaces()Plotting.plot_windrose()Plotting.ylabels
VGPlottingappend_fa()var_names_greek()
- Module contents
- varwg.meteo package
- Submodules
- varwg.meteo.avrwind module
- varwg.meteo.meteox2y module
- Meteorological Conversions (
meteo.meteox2y)- varwg.meteo.meteox2y.sat_vap_p
- varwg.meteo.meteox2y.rel2vap_p
- varwg.meteo.meteox2y.vap_p2rel
- varwg.meteo.meteox2y.dewpoint
- varwg.meteo.meteox2y.dew2rel
- varwg.meteo.meteox2y.norm_pressure
- varwg.meteo.meteox2y.iziomon
- varwg.meteo.meteox2y.lw2clouds
- varwg.meteo.meteox2y.lw_tennessee
- varwg.meteo.meteox2y.haude
- varwg.meteo.meteox2y.turc
- varwg.meteo.meteox2y.turc_rad
- varwg.meteo.meteox2y.hargreaves
- varwg.meteo.meteox2y.penman_monteith
- varwg.meteo.meteox2y.pot_s_rad
- varwg.meteo.meteox2y.sunshine
- varwg.meteo.meteox2y.blackbody_rad
- varwg.meteo.meteox2y.altitude
- varwg.meteo.meteox2y.spec_hum
- varwg.meteo.meteox2y.psychro2e
- varwg.meteo.meteox2y.slope_sat_p
SPI_ar()SPI_ds()STI_ar()STI_ds()abs_hum2rel()altitude()apparent_temperature()blackbody_rad()brunner_compound()dew2rel()dewpoint()esi()get_tz_offset()hargreaves()haude()humidex()iziomon()lw2clouds()lw_tennessee()max_sunshine_minutes()norm_pressure()penman_monteith()pot_s_rad()pot_s_rad_daily()psychro2e()rel2abs_hum()rel2vap_p()sat_vap_p()slope_sat_p()sonnenscheindauer()spec_hum()sunshine()sunshine_hours()sunshine_pot()sunshine_riseset()temp2lw()turc()turc_rad()vap_p2rel()wet_bulb_stull()
- Meteorological Conversions (
- varwg.meteo.windrose module
- Module contents
- varwg.time_series_analysis package
- Submodules
- varwg.time_series_analysis.distributions module
- varwg.time_series_analysis.models module
- varwg.time_series_analysis.models.VAR_LS
- varwg.time_series_analysis.models.VAR_LS_sim
- varwg.time_series_analysis.models.VAREX_LS
- varwg.time_series_analysis.models.VAREX_LS_sim
- varwg.time_series_analysis.models.VAR_order_selection
- varwg.time_series_analysis.models.VAR_residuals
- varwg.time_series_analysis.models.VAREX_residuals
- varwg.time_series_analysis.models.VAR_LS_predict
- varwg.time_series_analysis.models.AIC
- varwg.time_series_analysis.models.FPE
- varwg.time_series_analysis.models.HQ
- varwg.time_series_analysis.models.SC
AIC()B2A()FPE()HQ()MGARCH_ML()MGARCH_residuals()MGARCH_sim()SC()SVAR_LS()SVAR_LS_fill()SVAR_LS_sim()SVAR_residuals()VAREX_LS()VAREX_LS_sim()VAREX_residuals()VARMA_LS()VARMA_LS_prelim()VARMA_LS_sim()VARMA_order_selection()VARMA_residuals()VAR_LS()VAR_LS_asy()VAR_LS_extro()VAR_LS_predict()VAR_LS_sim()VAR_LS_sim_asy()VAR_YW()VAR_YW_sim()VAR_mean()VAR_onestep_predictions()VAR_order_selection()VAR_residuals()matrix_exp_sym()matrix_log_spd()mgarch_param_factoryscale_z()sqrtm()symmetrize()unvec()unvech()vec()vech()
- varwg.time_series_analysis.optimize module
- varwg.time_series_analysis.phase_randomization module
- varwg.time_series_analysis.seasonal module
- varwg.time_series_analysis.seasonal_kde module
SeasonalHourlyKDESeasonalHourlyKDE.data_boundsSeasonalHourlyKDE.density_gridSeasonalHourlyKDE.density_per_doy()SeasonalHourlyKDE.fit()SeasonalHourlyKDE.lower_bound()SeasonalHourlyKDE.pdf()SeasonalHourlyKDE.scotts_rule()SeasonalHourlyKDE.solutionSeasonalHourlyKDE.sum_log_density()SeasonalHourlyKDE.upper_bound()
SeasonalKDESeasonalKDE.cdf()SeasonalKDE.cdf_interp_per_daySeasonalKDE.chi2_test()SeasonalKDE.data_boundsSeasonalKDE.density_gridSeasonalKDE.density_per_doy()SeasonalKDE.doy_maskSeasonalKDE.doy_mask_dictSeasonalKDE.fit()SeasonalKDE.kernel_widthsSeasonalKDE.lower_bound()SeasonalKDE.median()SeasonalKDE.pdf()SeasonalKDE.ppf()SeasonalKDE.ppf_interp_per_daySeasonalKDE.quantile_gridSeasonalKDE.scatter_cdf()SeasonalKDE.scatter_pdf()SeasonalKDE.solutionSeasonalKDE.sum_log_density()SeasonalKDE.upper_bound()SeasonalKDE.x_grid
array_gen()doy_hour_fft()fft2par()
- Module contents
Submodules¶
varwg.config_template module¶
Central configuration file for all things VG. This dummy-file contains necessary variables used as configurations and suggestions for their values in the form of comments
Adjust this file for your needs and rename it to “config.py”.
varwg.ctimes module¶
- varwg.ctimes.datetime2doy(dt)¶
Extracts the day of year as a float from the given datetimes.
- varwg.ctimes.doy_distance(doy0, doys)¶
varwg.ecdf module¶
varwg.helpers module¶
Just some functions I find myself writing or searching for again and again…
- class varwg.helpers.ADict(dict=None, /, **kwargs)[source]¶
Bases:
UserDictMethods
clear()get(k[,d])items()keys()pop(k[,d])If key is not found, d is returned if given, otherwise KeyError is raised.
popitem()as a 2-tuple; but raise KeyError if D is empty.
setdefault(k[,d])update([E, ]**F)If E present and has a .keys() method, does: for k in E.keys(): D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
values()copy
fromkeys
- varwg.helpers.cache(*names, **name_values)[source]¶
Use as a decorator, to supply *names attributes that can be used as a cache. The attributes are set to their default/None during compile time. The wrapped function also has a ‘clear_cache’-method to delete those variables.
- Parameters:
- *namesstr
- varwg.helpers.chdir(dirname)[source]¶
Temporarily change the working directory with a with-statement.
- varwg.helpers.chi2_test(x, y, k=None, n_parameters=0)[source]¶
Chi-square test for inequality. H0: x and y were sampled from the same distribution.
- Parameters:
- kint
Number of classes (bins)
- Returns:
- p_valuefloat
- varwg.helpers.clear_def_cache(function, cache_names=None, cache_name_values=None)[source]¶
I often use a simplified function cache in the form of ‘function.attribute = value’. This function helps cleaning it up, i.e. setting them to None.
- Parameters:
- functionobject with settable attributes
- cache_namessequence of str or None, optional
if None, function should have an attribute called _cache_names with names of attributes that are cached.
- varwg.helpers.csv2dict(filename, *args, **kwds)[source]¶
Returns a dictionary containing the columns of a csv-file. The keys are taken from the first row of the file.
- varwg.helpers.csv2list(filename, startfrom=None, delimiter=None, column_ids=None, conversions=None, comment='#')[source]¶
Returns a list of each column of a csv-file.
- varwg.helpers.fourier_approx(data, order=4, size=None, how='longest')[source]¶
Approximate data with a Fourier transform, using the order number of frequencies with the highest amplitudes.
- Parameters:
- data1-dim ndarray
- orderint, optional
Number of frequencies to account for.
- sizeint, optional
Desired length of the output. If None, it will be the same as data.
- how“longest” or “strongest”, optional
- varwg.helpers.gaps(data)[source]¶
Return indices referring to start and end points of gaps (marked by nans in the given array ‘data’
- Parameters:
- data1dim ndarray, dtype float or bool
- >>> import numpy as np
- >>> a = np.arange(20.)
- >>> a[-1] = np.nan
- >>> gaps(a)
- array([[19, 19]])
- >>> a = np.arange(20.)
- >>> a[[0, 3, 4, 5, 12, 13, -1]] = np.nan
- >>> gaps(a)
- array([[ 0, 0],
[ 3, 5], [12, 13], [19, 19]])
- >>> a = np.arange(20.)
- >>> a[[0, 1, 3, 4, 5, 12, -2, -1]] = np.nan
- >>> gaps(a)
- array([[ 0, 1],
[ 3, 5], [12, 12], [18, 19]])
- varwg.helpers.hist(values, n_bins, dist=None, pdf=None, kde=False, fig=None, ax=None, discrete=False, figsize=None, legend=True, *args, **kwds)[source]¶
Plots a histogram and therotical or empirical densities.
- varwg.helpers.interp_nonfin(values, times=None, max_interp=None, pad_periodic=False, mask=None)[source]¶
Remove nans from values by linear interpolation.
- Parameters:
- values2d array
- times1d array, optional
- max_interpint
Maximum number of subsequent nans to interpolate over.
Examples
>>> import numpy as np >>> a = np.array([0., np.nan, 1., np.nan, np.nan, 4.]) >>> interp_nan(a) array([ 0. , 0.5, 1. , 2. , 3. , 4. ]) >>> interp_nan(a, max_interp=1) array([ 0. , 0.5, 1. , nan, nan, 4. ]) >>> a = np.arange(6, dtype=float).reshape((2, 3)) >>> a[0, 1] = np.nan >>> interp_nan(a) array([[ 0., 1., 2.], [ 3., 4., 5.]])
- varwg.helpers.kde_gauss(dataset, evaluation_points=None, kernel_width=None, maxopt=500, return_width=False, verbose=False)[source]¶
- Parameters:
- dataset(T,) ndarray
input data as array of length T
- evaluation points(N,) ndarray, optional if return_width=True
N points as array (e.g. xx=np.linspace(-3,3,100)
- kernel_widthfloat, optional
kernel_width (eg 0.3) if set, no MLM-routine to infer optimal kernel width
- maxoptint, optional
size of sample to optimize kernel_width, affects runtime strongly. depends on memorysize. max 1000 with 2GB mem
- return_widthboolean, optional
Return the optimized kernel width and nothing else.
- verboseboolean, optional
Print information (also from the optimizer).
Examples
dataset = np.array(np.random.normal(size=1e3)) xx = np.linspace(-3,3, 1e3) plt.plot(xx,kde_gauss(dataset,xx))
- varwg.helpers.kendalls_tau(x, y)[source]¶
Kendall’s Rank correlation coefficient. See Hartung p.599f
Examples
>>> A = [8, 6, 5, 3.5, 1, 2, 3.5, 7] >>> B = [6, 7.5, 4, 1, 2, 3, 5, 7.5] >>> kendalls_tau(A, B) 0.5714285714285714
- varwg.helpers.list_transpose(list_)[source]¶
Transposes a “2-dim” nested list.
Examples
>>> list_transpose([[1, 2, 3], [4, 5, 6]]) [[1, 4], [2, 5], [3, 6]]
- varwg.helpers.pickle_cache(filepath_template='%s.pkl', clear_cache=False, warn=True)[source]¶
Use this as a function decorator to cache the result of a function as a pickle-file. The filename is determined by the arguments in the function. If the filename turns out to be too long, a hash of it is used.
- varwg.helpers.recursive_diff(name, obj1, obj2, *, ignore_types=None, plot=False, verbose=False, diff=None)[source]¶
Show differences between two objects.
For debugging purposes mostly. Recurses into sequences and instances of the current module.
- Parameters:
- namestr or None
Name of the current object. If None, will be determined by str(obj1).
- obj1object
First object to compare.
- obj2object
Second object to compare.
- ignore_typessequence or None, optional
Ignore differences for objects of these types. callables are ignored by default.
- plotbool, optional
Plot 1- and 2-D np.ndarrays if they are different.
- verbosebool, optional
Be more talkative.
Examples
tba
- varwg.helpers.rel_ranks(values, method='average')[source]¶
Returns ranks of values in the range [0,1].
- varwg.helpers.round_to_float(values, precision)[source]¶
Round to nearest precision.
>>> round_to_float([8, 12], 5.) array([ 10., 10.])
- varwg.helpers.scale_yticks(event)[source]¶
Automagically make room for yticklabels. Use it like this:
fig = gcf() fig.canvas.mpl_connect(‘draw_event’, scale_yticks)
Stolen from the matplotlib-howto. Slightly changed, so it is possible to separate the function from the calling code. http://matplotlib.sourceforge.net/faq/howto_faq.html #automatically-make-room-for-tick-labels
- varwg.helpers.splom(data, variable_names=None, f_kwds=None, h_kwds=None, s_kwds=None, opacity=0.1, highlight_mask=None, ticklabels=True, figsize=None, hists=True, facecolor=(0, 0.5, 0.5), edgecolor=(1, 1, 1), f_opacity=None, e_opacity=None, highlight_color='red')[source]¶
Scatter-plot matrix with interactive capabilities.
- Parameters:
- data(K,T) ndarray
K variables, T timesteps
- variable_namessequence of strings, optional
Used to label the subplots.
- f_kwdsdictionary, optional
Keyword arguments that are passed to plt.subplots.
- h_kwdsdictionary, optional
Keyword arguments that are passed to the histogramm calls.
- s_kwdsdictionary, optional
Keyword arguments that are passed to the scatter calls.
- opacityfloat, optional
Opacity used for edgecolor parameter of scatter call.
- highlight_mask(T,) boolean ndarray, optional
Mask of timesteps that will be highlighted in the scatter plots.
- ticklabelsboolean, optional
Set to False to supress displaying x- and yticklabels.
- figsizeNone or tuple of width, height, optional
Size of the figure.
- histsboolean, optional
Plot histograms on the main diagonal.
- varwg.helpers.square_subplots(n_variables, *args, **kwds)[source]¶
Similar to plt.subplots, but shares x-axes column-wise and y-axes row - wise. Main diagonal subplots only share x-axes.
- varwg.helpers.sumup(values, width=24, times_=None, drop_extra=True, mean=False, middle_time=True, sum_to_nan=False, acceptable_nans=6, max_interp=3)[source]¶
Sum up width number of values along the rows. If there are surplus entries, they are dropped as if they were hot (Snoop Dog et al).
Examples
>>> import numpy as np >>> a = np.arange(10.).reshape((2, 5)) >>> a array([[ 0., 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.]]) >>> sumup(a, 2) array([[ 1., 5.], [ 11., 15.]]) >>> sumup(a, 2, drop_extra=False) array([[ 1., 5., 8.], [ 11., 15., 18.]]) >>> sumup(a.ravel(), 5) array([ 10., 35.]) >>> a[0, 0] = np.nan >>> sumup(a, 2, mean=True) array([[ 1. , 2.5], [ 5.5, 7.5]])
varwg.smoothing module¶
1dim smoothing and moving moments (smoothing)¶
1-dimensional smoothing Simplified & modified http://www.scipy.org/Cookbook/SignalSmooth |
|
Moving variance. |
|
Moving standard deviation. |
|
Moving skew. |
|
Moving median. |
|
Moving minimum. |
|
Moving maximum. |
|
Moving percentile. |
|
Moving autocorrelation. |
|
Moving correlation. |
|
Moving crosscorrelation. |
- varwg.smoothing.autocorr(data, lag=1, window_len=100)[source]¶
Moving autocorrelation. Returned array has the same length as data. This is achieved by assuming a special kind of periodicity: end of data is prepended to the beginning. You might be better off ignoring the first window_len elements.
- Parameters:
- data1dim ndarray
- lagint, optional
- window_lenint, optional
window length, need i say more?
- varwg.smoothing.corr(data1, data2, window_len=100)[source]¶
Moving correlation. Returned array has the same length as data. This is achieved by assuming a special kind of periodicity: end of data is prepended to the beginning. You might be better off ignoring the first window_len elements.
- Parameters:
- data11dim ndarray
- data21dim ndarray
This array will be shifted back by ‘lag’ steps
- window_lenint, optional
window length, need i say more?
- varwg.smoothing.crosscorr(data1, data2, lag=1, window_len=100)[source]¶
Moving crosscorrelation. Returned array has the same length as data. This is achieved by assuming a special kind of periodicity: end of data is prepended to the beginning. You might be better off ignoring the first window_len elements.
- Parameters:
- data11dim ndarray
- data21dim ndarray
This array will be shifted back by ‘lag’ steps
- lagint, optional
0 works and gives correlations instead of crosscorrelations.
- window_lenint, optional
window length, need i say more?
- varwg.smoothing.max(data, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]¶
Moving maximum.
- Parameters:
- data1d ndarray
- window_lenint, optional
Length of the moving window.
- periodicboolean, optional
Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.
- l_valuefloat, optional
Will be appended at the beginning. Might help to better estimate the first window_len elements.
- r_valuefloat, optional
Will be appended at the end. Might help to better estimate the last window_len elements.
- looboolean, optional
leave one out Estimates the percentile for data[t] without data[t].
- no_future: boolean, optional
only use past values for smoothing
- varwg.smoothing.maxdiff(data, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]¶
Moving maximum difference.
- Parameters:
- data1d ndarray
- window_lenint, optional
Length of the moving window.
- periodicboolean, optional
Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.
- l_valuefloat, optional
Will be appended at the beginning. Might help to better estimate the first window_len elements.
- r_valuefloat, optional
Will be appended at the end. Might help to better estimate the last window_len elements.
- looboolean, optional
leave one out Estimates the percentile for data[t] without data[t].
- no_future: boolean, optional
only use past values for smoothing
- varwg.smoothing.median(data, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]¶
Moving median.
- Parameters:
- data1d ndarray
- window_lenint, optional
Length of the moving window.
- periodicboolean, optional
Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.
- l_valuefloat, optional
Will be appended at the beginning. Might help to better estimate the first window_len elements.
- r_valuefloat, optional
Will be appended at the end. Might help to better estimate the last window_len elements.
- looboolean, optional
leave one out Estimates the percentile for data[t] without data[t].
- no_future: boolean, optional
only use past values for smoothing
- varwg.smoothing.min(data, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]¶
Moving minimum.
- Parameters:
- data1d ndarray
- window_lenint, optional
Length of the moving window.
- periodicboolean, optional
Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.
- l_valuefloat, optional
Will be appended at the beginning. Might help to better estimate the first window_len elements.
- r_valuefloat, optional
Will be appended at the end. Might help to better estimate the last window_len elements.
- looboolean, optional
leave one out Estimates the percentile for data[t] without data[t].
- no_future: boolean, optional
only use past values for smoothing
- varwg.smoothing.mindiff(data, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]¶
Moving minimum difference.
- Parameters:
- data1d ndarray
- window_lenint, optional
Length of the moving window.
- periodicboolean, optional
Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.
- l_valuefloat, optional
Will be appended at the beginning. Might help to better estimate the first window_len elements.
- r_valuefloat, optional
Will be appended at the end. Might help to better estimate the last window_len elements.
- looboolean, optional
leave one out Estimates the percentile for data[t] without data[t].
- no_future: boolean, optional
only use past values for smoothing
- varwg.smoothing.percentile(data, perc, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False)[source]¶
Moving percentile.
- Parameters:
- data1d ndarray
- window_lenint, optional
Length of the moving window.
- periodicboolean, optional
Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.
- l_valuefloat, optional
Will be appended at the beginning. Might help to better estimate the first window_len elements.
- r_valuefloat, optional
Will be appended at the end. Might help to better estimate the last window_len elements.
- looboolean, optional
leave one out Estimates the percentile for data[t] without data[t].
- no_future: boolean, optional
only use past values for smoothing
- varwg.smoothing.skew(data, window_len=10, window_function=<function hanning>, periodic=False, l_value=None, r_value=None, loo=False, no_future=False)[source]¶
Moving skew.
- Parameters:
- data1d ndarray
- window_lenint, optional
Length of the moving window.
- periodicboolean, optional
Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.
- l_valuefloat, optional
Will be appended at the beginning. Might help to better estimate the first window_len elements.
- r_valuefloat, optional
Will be appended at the end. Might help to better estimate the last window_len elements.
- looboolean, optional
leave one out Estimates the percentile for data[t] without data[t].
- no_future: boolean, optional
only use past values for smoothing
- ddofint, optional
Means Delta Degrees of Freedom. The divisor used in calculations is
N - ddof, whereNrepresents the number of elements. Default: 2 (passed on via **kwds)
- varwg.smoothing.smooth(data, window_len=10, window_function=<function hanning>, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]¶
1-dimensional smoothing Simplified & modified http://www.scipy.org/Cookbook/SignalSmooth
- Parameters:
- data1d ndarray
- window_lenint, optional
Length of the moving window.
- periodicboolean, optional
Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.
- l_valuefloat, optional
Will be appended at the beginning. Might help to better estimate the first window_len elements.
- r_valuefloat, optional
Will be appended at the end. Might help to better estimate the last window_len elements.
- looboolean, optional
leave one out Estimates the percentile for data[t] without data[t].
- no_future: boolean, optional
only use past values for smoothing
- ddofint, optional
Means Delta Degrees of Freedom. The divisor used in calculations is
N - ddof, whereNrepresents the number of elements. Default: 0 (passed on via **kwds)
- varwg.smoothing.std(data, window_len=10, window_function=<function hanning>, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]¶
Moving standard deviation.
- Parameters:
- data1d ndarray
- window_lenint, optional
Length of the moving window.
- periodicboolean, optional
Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.
- l_valuefloat, optional
Will be appended at the beginning. Might help to better estimate the first window_len elements.
- r_valuefloat, optional
Will be appended at the end. Might help to better estimate the last window_len elements.
- looboolean, optional
leave one out Estimates the percentile for data[t] without data[t].
- no_future: boolean, optional
only use past values for smoothing
- ddofint, optional
Means Delta Degrees of Freedom. The divisor used in calculations is
N - ddof, whereNrepresents the number of elements. Default: 1 (passed on via **kwds)
- varwg.smoothing.variance(data, window_len=10, window_function=<function hanning>, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]¶
Moving variance.
- Parameters:
- data1d ndarray
- window_lenint, optional
Length of the moving window.
- periodicboolean, optional
Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.
- l_valuefloat, optional
Will be appended at the beginning. Might help to better estimate the first window_len elements.
- r_valuefloat, optional
Will be appended at the end. Might help to better estimate the last window_len elements.
- looboolean, optional
leave one out Estimates the percentile for data[t] without data[t].
- no_future: boolean, optional
only use past values for smoothing
- ddofint, optional
Means Delta Degrees of Freedom. The divisor used in calculations is
N - ddof, whereNrepresents the number of elements. Default: 1 (passed on via **kwds)
varwg.times module¶
Time helpers and conversions (times)¶
Helper functions to convert arrays containing time information.
All the functions with a 2 in the name provide the advertised conversions
for scalar as well as for array input.
Converts a CWR-timesting into a datetime object. |
|
Converts a CWR-timestring into a human-readable timestring. |
|
Converts a CWR-timestring to a unix timestamp. |
|
Extracts the day of year as a float from the given datetimes. |
|
Converts a datetime object into an ordinal. |
|
Converts a datetime object into a human-readable string. |
|
Converts a dateime object into a unix-timestamp. |
|
Constructs a datetime object from given day of year. |
|
Converts an ISO formated time string to a datetime object. |
|
Converts an ISO formated time string to a unix time stamp. |
|
Converts an ordinal to a datetime object. |
|
Converts a human readable time string tinto a datetime object. |
|
Converts a human readable time string to an ordinal. |
|
Converts a time-string of the given d_format (default: "dd.mm.yyyy HH:MM:SS") into a unix-timestamp. |
|
Converts a unix-timestamp to a CWR-timestring. |
|
Convert a unix time stamp to a datetime object. |
|
Converts a unix time stamp to an ordinal. |
|
Converts a unix-timestamp into a time-string of the given d_format (default: "dd.mm.yyyy HH:MM:SS"). |
|
Returns the "time_part" of a timestamp or datetimes. |
|
Groups "values" as a nested list according to the "sub_format_str" of the "timestamps". |
|
Calculates the array index for a certain time in an equidistant time-series given the reference time (where the index would be 0) and the time discretization. |
|
Calculates the ISOstring timestamp for a certain index in an equidistant time-series given the reference time (where the index would be 0) and the time discretization. |
- varwg.times.build_diff_timestring(diff)[source]¶
Split time difference given in seconds into a string representation of days, hours, minutes and seconds.
- varwg.times.cwr2datetime(cwr_string)[source]¶
Converts a CWR-timesting into a datetime object. >>> cwr2datetime(“2040001.00”) datetime.datetime(2040, 1, 1, 0, 0) >>> cwr2datetime(‘1980001’) datetime.datetime(1980, 1, 1, 0, 0)
- varwg.times.cwr2str(cwr_string, d_format='%d.%m.%Y %H:%M:%S')[source]¶
Converts a CWR-timestring into a human-readable timestring.
>>> cwr2str("2040001.00") '01.01.2040 00:00:00'
- varwg.times.cwr2unix(cwr_string)[source]¶
Converts a CWR-timestring to a unix timestamp.
>>> cwr2unix("2040001.00") 2208988800.0
- varwg.times.daily_ranges(dtimes, data)[source]¶
Daily max-min.
- Parameters:
- dtimessequence of datetime objects
For the time being it is assumed that time steps are equally spaced!
- data1d array
- Returns:
- ranges1d array
length is number of days in dtimes.
- varwg.times.datetime2cwr(dt)[source]¶
Converts a datetime object into a CWR/Julian timestamp.
>>> import datetime >>> datetime2cwr(datetime.datetime(2040, 1, 1, 12, 30, 30, 0)) 2040001.5211805555
- varwg.times.datetime2doy(dt)[source]¶
Extracts the day of year as a float from the given datetimes.
>>> import datetime >>> dt = datetime.datetime(2040, 1, 1, 12, 30, 30, 500000) >>> datetime2doy(dt) 1.5211863425925927
- varwg.times.datetime2hour(dt)[source]¶
Extracts the hour of a day as a float from the given datetimes.
>>> import datetime >>> dt = datetime.datetime(2040, 1, 1, 12, 30) >>> datetime2hour(dt) 12.5
- varwg.times.datetime2ordinal(dt)[source]¶
Converts a datetime object into an ordinal.
>>> import datetime >>> dt = datetime.datetime(2040, 1, 1, 0, 0, 0, 500000) >>> datetime2ordinal(dt) 744730 >>> datetime2ordinal(datetime.datetime(1, 1, 1, 0, 0)) 1
- varwg.times.datetime2str(dt, d_format='%d.%m.%Y %H:%M:%S')[source]¶
Converts a datetime object into a human-readable string.
>>> import datetime >>> datetime2str(datetime.datetime(2040, 1, 1, 0, 0, 0, 500000)) '01.01.2040 00:00:00'
- varwg.times.datetime2unix(dt)[source]¶
Converts a dateime object into a unix-timestamp.
>>> import datetime >>> dt = datetime.datetime(2040, 1, 1, 0, 0, 0, 500000) >>> datetime2unix(dt) 2208988800.5 >>> datetime2unix(datetime.datetime(1970, 1, 1, 0, 0)) 0.0
- varwg.times.doy2datetime(doy, year=2000)[source]¶
Constructs a datetime object from given day of year.
- Parameters:
- yearint, optional
The year of the day of the year.
Examples
>>> import datetime, numpy >>> doy2datetime(1.5211863425925927, 2040) datetime.datetime(2040, 1, 1, 12, 30, 30, 500000) >>> doy2datetime(numpy.arange(1,3)) array([datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 2, 0, 0)], dtype=object)
- varwg.times.doy_distance(doy1, doy2)[source]¶
Examples
>>> import numpy as np >>> doy_distance(0, np.array([364, 0, 1])) array([ 1., 0., 1.])
- varwg.times.dy2datetime(dy)[source]¶
Converts a DYRESM float time (a.k.a. julian date) into a datetime object.
>>> dy2datetime(2451544.5) datetime.datetime(2000, 1, 1, 0, 0) >>> dy2datetime(2456462.4375) datetime.datetime(2013, 6, 18, 22, 30)
- varwg.times.expand_timeseries(timestamps, repeats=4, values=None)[source]¶
Interpolates timestamps and repeats according values. Assumes constant length of time-step.
Examples
>>> import numpy as np >>> timestamps = str2unix(["01.01.1980 00:00:00", "02.01.1980 00:00:00"]) >>> values = np.array([1, 2]) >>> timestamps, values = expand_timeseries(timestamps, 2, values) >>> print (unix2str(timestamps), values) ['01.01.1980 00:00:00' '01.01.1980 12:00:00' '02.01.1980 00:00:00' '02.01.1980 12:00:00'] [1 1 2 2]
- varwg.times.feb29_mask(dtimes)[source]¶
Returns a mask indicating the location of the additional day in the leap years.
Should help you to get rid off those buggers.
- Parameters:
- dtimesndarray
- Returns:
- maskboolean ndarray
- varwg.times.index2timestamp(idx, dt, refts, **kwargs)[source]¶
Calculates the ISOstring timestamp for a certain index in an equidistant time-series given the reference time (where the index would be 0) and the time discretization. If any of the input parameters contains timezone information, all others also need to contain timezone information.
- Parameters:
- idxstr or datetime-object
The timestamp to determine the index for If it is a string, it will be converted to datetime using the function _datetimefromisoformat Formatting keywords may be passed to this function
- dtstr or timedelta object
The discretization of the time series (the amount of time that elapsed between indices) If used as a string, it needs to be given in the format “keyword1=value1,keyword2=value2”. Keywords must be understood by the timedelta constructor (like days, hours, minutes, seconds) and the values may only be integers.
- reftsstr or datetime-object
The timestamp to determine the index for If it is a string, it will be converted to datetime using the function _datetimefromisoformat Formatting keywords may be passed to this function
- Returns:
- ISO-timestampstring
The ISO-timestamp of a discrete time series array of the given parameters in this format: ‘%Y-%m-%dT%H:%M:%S’
Examples
>>> index2timestamp(25637, 'seconds=10', '2010-09-25T00:00:10') '2010-09-27T23:13:00' >>> index2timestamp(365, 'days=1', '2010-09-25T00:00:10') '2011-09-25T00:00:10'
- varwg.times.ordinal2datetime(ord_)[source]¶
Converts an ordinal to a datetime object.
>>> ordinal2datetime(744730) datetime.datetime(2040, 1, 1, 0, 0)
- varwg.times.periodic_distance(x1, x2, period)[source]¶
Examples
>>> periodic_distance(0, 23, 24) array(1.0)
- varwg.times.regularize(values, dtimes, nan=False, main_diff=None)[source]¶
Regularize an irregular time series by linear interpolation. The time interval is guessed from the most frequent interval in times.
>>> dtimes = datetime(2000, 1, 1) + np.arange(4) * timedelta(hours=1) >>> dtimes[1] += timedelta(minutes=30) >>> values = np.arange(4.) >>> values[1] = 1.5 >>> values array([ 0. , 1.5, 2. , 3. ]) >>> dtimes array([datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 1, 30), datetime.datetime(2000, 1, 1, 2, 0), datetime.datetime(2000, 1, 1, 3, 0)], dtype=object) >>> regularize(values, dtimes) (array([ 0., 1., 2., 3.]), array([datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 1, 0), datetime.datetime(2000, 1, 1, 2, 0), datetime.datetime(2000, 1, 1, 3, 0)], dtype=object))
- varwg.times.str2datetime(str_, d_format='%d.%m.%Y %H:%M:%S')[source]¶
Converts a human readable time string tinto a datetime object.
>>> str2datetime("01.01.2040 00:00:00") datetime.datetime(2040, 1, 1, 0, 0)
- varwg.times.str2ordinal(str_, d_format='%d.%m.%Y %H:%M:%S')[source]¶
Converts a human readable time string to an ordinal.
>>> str2ordinal("01.01.2040 00:00:00") 744730 >>> str2ordinal("01.01.0001 00:00:00") 1
- varwg.times.str2unix(str_, d_format='%d.%m.%Y %H:%M:%S')[source]¶
Converts a time-string of the given d_format (default: “dd.mm.yyyy HH:MM:SS”) into a unix-timestamp.
>>> str2unix("01.01.2040 00:00:00") 2208988800.0 >>> str2unix("01.01.1970 00:00:00") 0.0
- varwg.times.time_part(timestamps_or_datetimes, sub_format_str)[source]¶
Returns the “time_part” of a timestamp or datetimes. The time_part has to be convertible into an integer. This is useful to group values according to months, weeks and so on.
- varwg.times.time_part_sort(timestamps, values, sub_format_str)[source]¶
Groups “values” as a nested list according to the “sub_format_str” of the “timestamps”.
>>> dtimes = datetime(2000, 1, 1) + np.arange(4) * timedelta(days=16) >>> values = np.arange(4) >>> time_part_sort(dtimes, values, "%m") ([1, 2], [array([0, 1]), array([2, 3])])
- varwg.times.timestamp2index(ts, dt, refts, **kwargs)[source]¶
Calculates the array index for a certain time in an equidistant time-series given the reference time (where the index would be 0) and the time discretization. If any of the input parameters contains timezone information, all others also need to contain timezone information.
- Parameters:
- tsstr or datetime-object
The timestamp to determine the index for If it is a string, it will be converted to datetime using the function _datetimefromisoformat Formatting keywords may be passed to this function
- dtstr or timedelta object
The discretization of the time series (the amount of time that elapsed between indices) If used as a string, it needs to be given in the format “keyword1=value1,keyword2=value2”. Keywords must be understood by the timedelta constructor (like days, hours, minutes, seconds) and the values may only be integers.
- reftsstr or datetime-object
The timestamp to determine the index for If it is a string, it will be converted to datetime using the function _datetimefromisoformat Formatting keywords may be passed to this function
- Returns:
- indexinteger
The index of a discrete time series array of the given parameters
Examples
>>> timestr1, timestr2 = '2008-06-01T00:00:00', '2007-01-01T00:00:00' >>> timestamp2index(timestr1, 'minutes=5', timestr2) 148896 >>> timestamp2index(timestr1, 'hours=1,minutes=5',timestr2) 11453 >>> timestamp2index(timestr1, timedelta(hours=1, minutes=5), timestr2) 11453
- varwg.times.timestamps2slice(startts=None, endts=None, dt=None, refts=None, step=None)[source]¶
Ask Thomas.
>>> timestamps2slice('2010-03-01T23:54:00', '2010-03-07T23:52:00', timedelta(days=1)) slice(0, 5, None) >>> timestamps2slice('2010-03-01T23:54:00', '2010-03-07T23:52:00', timedelta(days=1), '2010-03-04T00:00:00') slice(-2, 3, None) >>> timestamps2slice('2010-03-01T23:54:00', '2010-03-07T23:52:00', timedelta(days=1), '2010-01-04T00:00:00') slice(56, 61, None)
- varwg.times.unix2cwr(timestamp)[source]¶
Converts a unix-timestamp to a CWR-timestring.
>>> unix2cwr(2208988800.0) 2040001.0
- varwg.times.unix2datetime(timestamp)[source]¶
Convert a unix time stamp to a datetime object.
>>> import datetime >>> unix2datetime(2208988800.5) datetime.datetime(2040, 1, 1, 0, 0, 0, 500000) >>> unix2datetime(0) datetime.datetime(1970, 1, 1, 0, 0)
- varwg.times.unix2ordinal(timestamp)[source]¶
Converts a unix time stamp to an ordinal.
>>> unix2ordinal(2208988800.0) 744730
- varwg.times.unix2str(timestamp, d_format='%d.%m.%Y %H:%M:%S')[source]¶
Converts a unix-timestamp into a time-string of the given d_format (default: “dd.mm.yyyy HH:MM:SS”).
>>> unix2str(2208988800.5) '01.01.2040 00:00:00' >>> unix2str(0) '01.01.1970 00:00:00'
- varwg.times.xls2datetime(xldate, datemode=0)[source]¶
Here’s the bare-knuckle no-seat-belts use-at-own-risk version posted by John Machin in http://stackoverflow.com datemode: 0 for 1900-based, 1 for 1904-based