def test_align_index(dobj, removal): index = pd.date_range(start='now', freq='5min', periods=20, name='timestamp') data = dobj(index=index, dtype=float) data = data.drop(index[removal]) out = utils.align_index(data, pd.Timedelta('5min')) assert_index_equal(out.index, index)
def test_align_index_new_length(): index = pd.date_range(start='now', freq='5min', periods=20, name='timestamp') data = pd.Series(index=index, dtype=float) out = utils.align_index(data, pd.Timedelta('1min')) nindex = pd.date_range(start=index[0], end=index[-1], freq='1min', name='timestamp') assert_index_equal(out.index, nindex)
def generate_observation_figure(observation, data, limit=pd.Timedelta('3d')): """ Creates a bokeh figure from API responses for an observation Parameters ---------- observation : datamodel.Observation The Observation that is being plotted data : pandas.DataFrame The observation data to be plotted with datetime index and ('value', 'quality_flag') columns limit : pandas.Timedelta or None The time limit from the last datapoint to plot. If None, all data is plotted. Returns ------- None When the data is empty script, div : str When return_components = True, return the <script> and <div> components for the Bokeh plot. bokeh components from gridplot When return_components = False """ logger.info('Starting observation forecast generation...') if len(data.index) == 0: return None data = plot_utils.align_index(data, observation.interval_length, limit) quality_flag = data.pop('quality_flag').dropna().astype(int) bool_flags = quality_mapping.convert_mask_into_dataframe(quality_flag) active_flags = quality_mapping.convert_flag_frame_to_strings(bool_flags) active_flags.name = 'active_flags' flags = bool_flags.mask(~bool_flags).reindex(data.index) # add missing flags['MISSING'] = pd.Series(1.0, index=data.index)[pd.isna(data['value'])] # need to fill as line needs more than a single point to show up if observation.interval_label == 'ending': flags.bfill(axis=0, limit=1, inplace=True) else: # for interval beginning and instantaneous flags.ffill(axis=0, limit=1, inplace=True) cds = ColumnDataSource(pd.concat([data, flags, active_flags], axis=1)) figs = [ make_basic_timeseries(cds, observation.name, observation.variable, observation.interval_label, PLOT_WIDTH) ] figs.extend(make_quality_bars(cds, PLOT_WIDTH, figs[0].x_range)) layout = _make_layout(figs) logger.info('Figure generated succesfully') return layout
def test_align_index_limit(): index = pd.date_range(start='now', freq='5min', periods=20, name='timestamp') data = pd.Series(index=index) out = utils.align_index(data, pd.Timedelta('5min'), limit=pd.Timedelta('60min')) nindex = pd.date_range(start=index[-13], end=index[-1], freq='5min', name='timestamp') assert_index_equal(out.index, nindex)
def generate_forecast_figure(forecast, data, limit=None): """ Creates a bokeh timeseries figure for forcast data Parameters ---------- forecast : datamodel.Forecast The Forecast that is being plotted data : pandas.Series The forecast data with a datetime index to be plotted limit : pandas.Timedelta or None The time limit from the last datapoint to plot. If None, all data is plotted. Returns ------- None When the data is empty script, div : str When return_components = True, return the <script> and <div> components for the Bokeh plot. bokeh components from gridplot When return_components = False """ logger.info('Starting forecast figure generation...') if len(data.index) == 0: return None data = plot_utils.align_index(data, forecast.interval_length, limit) cds = ColumnDataSource(data.reset_index()) fig = make_basic_timeseries(cds, forecast.name, forecast.variable, forecast.interval_label, PLOT_WIDTH) layout = _make_layout([fig]) logger.info('Figure generated succesfully') return layout