Advertisement
Guest User

RosbagToPandas Reindexing

a guest
Sep 19th, 2014
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. from __future__ import division
  4. import numpy as np
  5. import pandas as pd       # pip install pandas
  6. import rosbag_pandas      # pip install rosbag_pandas
  7.  
  8. # Fast Introduction to pandas:
  9. # http://www.gregreda.com/2013/10/26/intro-to-pandas-data-structures/
  10. #
  11. # tl;dr: pandas extends numpy array with named columns and a time index
  12.  
  13.  
  14. # Example usage:
  15. # dataframe = rosbag_pandas.bag_to_dataframe('2014-08-20-17-37-47.bag')
  16. # ridf=reindex_df(dataframe, dataframe.uC_time__data)
  17. # print ridf
  18. # plt.plot(ridf.index, ridf.sensors_imu_data_raw__angular_velocity_z)
  19.  
  20. def datetime_index_to_sec(dti):
  21.     '''Converts pandas DatetimeIndex into seconds.
  22.    This may only work for DatetimeIndex from rosbag_pandas. Untested on other.'''
  23.     return dti.map(lambda x:x.value/1e9)
  24.  
  25. def notnull_values(series):
  26.     '''Returns all not null values in the panda Series'''
  27.     return series[series.notnull()].values
  28.  
  29. def notnull_keys(series):
  30.     '''Returns all not null keys in the panda Series'''
  31.     return series[series.notnull()].keys()
  32.  
  33. def notnull_keys_as_sec(series):
  34.     '''Returns all not null keys in the panda Series as seconds'''
  35.     return datetime_index_to_sec(notnull_keys(series))
  36.  
  37. def get_closest_values(desired_timestamps, timestamps, data):
  38.     '''Returns the values in data whose timestamps are closest to the desired_timestamps.
  39.    Assumes sorted desired_timestamps and uniform dt.'''
  40.     assert len(desired_timestamps.shape)==1
  41.     assert len(timestamps.shape)==1
  42.     assert len(data.shape)==1
  43.     assert timestamps.shape[0] == data.shape[0]
  44.    
  45.     n = timestamps.shape[0]
  46.     dt = np.diff(timestamps).mean()
  47.     start = timestamps[0]
  48.  
  49.     values = np.empty_like(desired_timestamps)
  50.     idx = (desired_timestamps-start)/dt
  51.     idx = np.round(idx)
  52.     idx[idx<0]=0
  53.     idx[idx>n-1]=n-1
  54.     idx=idx.astype('int')
  55.     values = data[idx]
  56.  
  57.     return values
  58.  
  59. def get_closest_values_from_df(reference_series, series):
  60.     '''Returns the values in series whose timestamps are closest to the timestamps in the reference_series.
  61.    Assumes uniform dt in reference_series.'''
  62.     return get_closest_values(
  63.         notnull_keys_sec(reference_series),
  64.         notnull_keys_sec(series),
  65.         notnull_values(series))
  66.  
  67. def reindex_dataframe(dataframe, reference_series, idx_start_from_zero=True):
  68.     '''Reindex the pandas dataframe to only contain the timestamps from reference_series.
  69.    The closest values (time-wise) for each column are used.
  70.    idx_start_from_zero specifies whether the reindexed timestamps shall start from zero.
  71.    Only columns with float64 values are include in the reindexed dataframe.
  72.    '''
  73.     if type(reference_series) is str:
  74.         reference_series = dataframe[reference_series]
  75.    
  76.     idx = notnull_keys_sec(reference_series)
  77.     n = idx.shape[0]
  78.     data = dict()
  79.     for col in dataframe.columns:
  80.         col = df.columns[i]
  81.         data[col] = np.empty(shape=(n))
  82.         series = df[col]
  83.         series = series[series.notnull()]
  84.         values = series.values
  85.         keys = datetime_index_to_sec(series.keys())
  86.         values = get_closest_values(idx,keys,values)
  87.         data[col] = values
  88.  
  89.     if idx_start_from_zero:
  90.         idx -= idx[0]
  91.    
  92.     reindexed = pd.DataFrame(data,idx)
  93.    
  94.     return reindexed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement