def test_get_running_df(running_data, timestamps): expected = pd.DataFrame({ 'speed': { 0.0: 4.0677840296488785, 0.01670847: 4.468231641421186, 0.03336808: 4.869192250359061, 0.05002418: 4.47027713320348, 0.06672007: 4.070849018882336 }, 'dx': { 0.0: 0.0, 0.01670847: 0.8444478, 0.03336808: 0.7076058, 0.05002418: 1.4225141, 0.06672007: 1.5040479 }, 'v_sig': { 0.0: 3.460190074169077, 0.01670847: 3.4692217108095065, 0.03336808: 3.4808338150614873, 0.05002418: 3.5014775559538975, 0.06672007: 3.5259919982636347 }, 'v_in': { 0.0: 4.996858536847867, 0.01670847: 4.99298783543054, 0.03336808: 4.995568303042091, 0.05002418: 4.996858536847867, 0.06672007: 5.00201947207097 } }) expected.index.name = "timestamps" pd.testing.assert_frame_equal(expected, get_running_df(running_data, timestamps))
def test_get_running_df_one_fewer_timestamp_check_warning( running_data, timestamps, lowpass): with pytest.warns( UserWarning, match="Time array is 1 value shorter than encoder array.*"): # Call with one fewer timestamp, check for a warning _ = get_running_df(data=running_data, time=timestamps[:-1], lowpass=lowpass)
def get_running_data_df(self) -> pd.DataFrame: """Get running speed data. :returns: pd.DataFrame -- dataframe containing various signals used to compute running speed. """ stimulus_timestamps = self.get_stimulus_timestamps() data = self._behavior_stimulus_file() return get_running_df(data, stimulus_timestamps)
def test_get_running_df_one_fewer_timestamp_check_truncation( running_data, timestamps, lowpass): # Call with one fewer timestamp output = get_running_df(data=running_data, time=timestamps[:-1], lowpass=lowpass) # Check that the output is actually trimmed, and the values are the same assert len(output) == len(timestamps) - 1 np.testing.assert_equal( output["v_sig"], running_data["items"]["behavior"]["encoders"][0]["vsig"][:-1]) np.testing.assert_equal( output["v_in"], running_data["items"]["behavior"]["encoders"][0]["vin"][:-1])
def test_get_running_df(running_data, timestamps, lowpass): actual = get_running_df(running_data, timestamps, lowpass=lowpass) np.testing.assert_array_equal(actual.index, timestamps) assert sorted(list(actual)) == ["dx", "speed", "v_in", "v_sig"] # Should bring raw data through np.testing.assert_array_equal( actual["v_sig"].values, running_data["items"]["behavior"]["encoders"][0]["vsig"]) np.testing.assert_array_equal( actual["v_in"].values, running_data["items"]["behavior"]["encoders"][0]["vin"]) np.testing.assert_array_equal( actual["dx"].values, running_data["items"]["behavior"]["encoders"][0]["dx"]) if lowpass: assert np.count_nonzero(np.isnan(actual["speed"])) == 0
def get_running_acquisition_df(self, lowpass=True, zscore_threshold=10.0) -> pd.DataFrame: """Get running speed acquisition data from a behavior pickle file. NOTE: Rebases timestamps with the self.get_stimulus_timestamps() method which varies between the BehaviorDataTransformer and the BehaviorOphysDataTransformer. Parameters ---------- lowpass: bool (default=True) Whether to apply a 10Hz low-pass filter to the running speed data. zscore_threshold: float The threshold to use for removing outlier running speeds which might be noise and not true signal Returns ------- pd.DataFrame Dataframe with an index of timestamps and the following columns: "speed": computed running speed "dx": angular change, computed during data collection "v_sig": voltage signal from the encoder "v_in": the theoretical maximum voltage that the encoder will reach prior to "wrapping". This should theoretically be 5V (after crossing 5V goes to 0V, or vice versa). In practice the encoder does not always reach this value before wrapping, which can cause transient spikes in speed at the voltage "wraps". """ stimulus_timestamps = self.get_stimulus_timestamps() data = self._behavior_stimulus_file() return get_running_df(data, stimulus_timestamps, lowpass=lowpass, zscore_threshold=zscore_threshold)
def get_running_data_df(self): stimulus_timestamps = self.get_stimulus_timestamps() behavior_stimulus_file = self.get_behavior_stimulus_file() data = pd.read_pickle(behavior_stimulus_file) return get_running_df(data, stimulus_timestamps)