FLOCK.Preprocessing
index
c:\users\james\gitrepos\gps-ruck\flock\preprocessing.py

functions for pre-processing movement datasets
 
Including interpolation, smoothing and break detection

 
Modules
       
folium
numpy
pandas
matplotlib.pyplot
seaborn
warnings

 
Functions
       
get_centroid(datasets, UTM=True)
Calculate the centroid for each timepoint, or for a window
 
Args:
    datasets (list): list of dataset dfs
    UTM (bool, optional): True if UTM data, False if raw GPS. Defaults to True.
 
Returns:
    cent_list (list): list of centroid location dataframes
get_slices(smoothed_datasets, datasets, UTM=True, plot=False)
Extract time-slices of movement and rest periods from the dataset
A 'Movement' slice is when below 1m/s velocity for 5 minutes or more 
A 'Rest' period is when above 1m/s velocity for 3 minutes or more as 'rest'
 
This is not reccomended. Instead use get_slices_byArea for finding rest periodswith MovingPandas
 
Args:
    smoothed_datasets (list): list of extra smoothed DataFrames for velocity
    datasets (list): list of smoothed DataFrames for slicing
    UTM (bool, optional): True if UTM data, False if GPS data. Defaults to True.
    plot (bool, optional): True if plotting break times. Defaults to False.
 
Returns:
    movement_slices (list): a list of 'movement' period slices as datasets
    rest_slices (list): a list of 'rest' period slices as datasets
get_slices_byArea(interp_datasets, area_diameter=100, area_time=120, plot=False)
Get stop periods where all group memebers are stopped within [area_diameter] meters for at least [area_time] seconds
 
For our sample data (humans walking) we use 100 meters and 120 seconds
(0.833m/s is the reference defined slow walking speed)
 
Using MovingPandas stop detection for each individual 
 
Then finding overlapping stops that include the whole group
 
returns movement periods, rest periods and all stop information (for finding pre/post dynamics)
 
Args:
    interp_datasets (list): list of dataframes with interpolated datasets
    area_diameter (int): The diameter (in meters) for the 'stop area' beign detected. Defaults to 100.
    area_time (int): The amount of time (in seconds) an individual should be in an area_diameter sized place to consider it a stop spot. Defaults to 120.
    plot (bool, optional): True if tha map should be plotted and displayed (for jupyter notebooks). Defaults to False.
 
Returns:
    move_slices (list): list of lists of movement period dataframes for each squad
    rest_slices (list): list of lists of movement period dataframes for each squad
    all_stops (list): list of lists of stop information for each squad for each stop
interpolate_datasets(datasets, threshold=0.99)
Drop outlier values 
Interpolate the missing values in the dataset
 
Args:
    datasets (list of DataFrames): list of pivotted DataFrames
    threshold (float, optional): threshold for dropping ourliers as a percent. The remaining percent of first differences are dropped. Defaults to 0.99.
 
Returns:
    interp_datasets (list): list of dataset dfs with outliers dropped and missing data interpolated
quarter_datasets(datasets, n_sections=4)
Split datasets into [n_sections] numbner of sections while retaining 'whole' as the last 'time period'
 
Args:
    datasets (list): list of DataFrames to be 'quartered'
    n_sections (int, optional): number of sections to create. Defaults to 4.
 
Returns:
    Qs_datasets (dict): dictionary of split datasets, keys being (whole, Q1, Q2, Q3, Q4, ...) and values being lists of dataset dfs for each time period
quarter_datasets_dist(interp_datasets, n_sections=4)
Split datasets into [n_sections] numbner of sections while retaining 'whole' as the last 'time period'
 
Args:
    datasets (list): list of DataFrames to be 'quartered'
    n_sections (int, optional): number of sections to create. Defaults to 4.
 
Returns:
    Qs_datasets (dict): dictionary of split datasets, keys being (whole, Q1, Q2, Q3, Q4, ...) and values being lists of dataset dfs for each time period
smooth_datasets(datasets, window=5)
for extra smoothing before velocity calculation, using a rolling average
 
Args:
    datasets (list): list of smooth DataFrames, to be smoothed further 
    window (int, optional): window for rolling average. Defaults to 5.
 
Returns:
    list: list of extra smooth DataFrames fro velocity calculation
spline_smoothing(datasets, UTM=True, s=100)
Smooths datasets using a spline smoothing method
 
Args:
    datasets (list): list of DataFrames of pivotted and interpolated datasets
    UTM (bool, optional): True if UTM data, False if raw GPS. Defaults to True.
    s (int, optional): Smoothing factor for spline method. Defaults to 100.
 
Returns:
    new_datasets (list): list of smoothed DataFrames