utils module
Collection of different functions and tools for reading data from different tasks and tools for data analysis @Author: Mehrdad Kashefi
- class utils.Analysis_tools(fs)
Class for tools often required for data analysis
- align_continuous(trial_info, D, condition_columns, align_column, channel_column, t_range, return_mean=True)
Aligns continuous data on a specific event
- Parameters:
trial_info (df) – Data frame of the trial information (Start time, end time, etc.)
D (df) – Data frame of the continuous data (Hand pos, Hand vel, etc.)
condition_columns (list) – Columns to group the data
align_column (str) – Column to align the data on
channel_column (str) – Which data column is selected
t_range (list) – Time range to align the data
return_mean (bool) – Whether to return the mean of trials of a condition, default is True
- Returns:
data_aligned (np.array) – Aligned data (condition x time x channel)
conds (np.array) – Unique conditions
aligned_trials_type (np.array) – Type of the trials
- align_fr(trial_info, units, condition_columns, align_column, unit, t_range, return_mean=True)
Aligns units firing rate on a specific event
- Parameters:
trial_info (df) – Data frame of the trial information (Start time, end time, etc.)
units (df) – Data frame of the units (Spike times, KS label, etc.)
condition_columns (list) – Columns to group the data
align_column (str) – Column to align the data on
unit (list) – Which unit to get the spike times
t_range (list) – Time range to align the data
return_mean (bool) – Whether to return the mean of trials of a condition, default is True
- Returns:
data_aligned (np.array) – Aligned data (condition x time x unit)
- align_stem(trial_info, units, condition_columns, align_column, unit, t_range)
Aligns units firing rate on a specific event
- Parameters:
trial_info (df) – Data frame of the trial information (Start time, end time, etc.)
units (df) – Data frame of the units (Spike times, KS label, etc.)
condition_columns (list) – Columns to group the data
align_column (str) – Column to align the data on
unit (list) – Which unit to get the spike times
t_range (list) – Time range to align the data
- Returns:
data_aligned (np.array) – Aligned data (condition x time x unit)
- get_fr(which_unit, units, t, **kwargs)
Get firing rate for a specific unit, during a specific time
- Parameters:
which_unit (int) – Which unit to get the spike times
units (df) – Data frame of the units (Spike times, KS label, etc.)
t (list) – Time range to get the spike times
gauss_width (int) – Window size for the Gaussian window, default is 50
dt (float) – Step size, default is 1/1000
t_pad (float) – How many seconds to pad the data from before and after the trial, default is 0.5
plot (bool) – Plot the single trial, default is False
return_padded (bool) – Whether to include padded data in return, default is False
causal (bool) – Whether to use causal convolution, default is True
- Returns:
FR (np.array) – Continuous firing rate for the specific unit, in the specific time range
- get_stem(units, which_unit, t, **kwargs)
Get spike times for a specific unit, in a specific time range
- Parameters:
units (df) – Data frame of the units (Spike times, KS label, etc.)
which_unit (int) – Which unit to get the spike times
t (list) – Time range to get the spike times
df (float) – Step size, default is 1/1000
t_pad (float) – How many seconds to pad the data from before and after the trial, default is 0.5
plot (bool) – Plot the single trial, default is False
return_padded (bool) – Whether to include padded data in return, default is False
- Returns:
stem (np.array) – Spike times for the specific unit, in the specific time range
- utils.collapse_cond_time(data)
Collapse the condition and time dimensions of the data. :param data: numpy array of shape (n_cond, n_time, n_chan)
- Returns:
numpy array of shape (n_time * n_cond, n_chan)
- utils.cosine_similarity(D1, D2)
Calculate the cosine similarity between two matrices. :param D1: First matrix :type D1: np.array :param D2: Second matrix :type D2: np.array
- Returns:
float – Cosine similarity between the two matrices
- utils.dist_to_G(dist)
Transforms a squared Euclidean matrix into a second moment matrix :param dist: 2d array :type dist: ndarray
- utils.get_G(data, normalize=False)
Get the second moment matrix from the data :param data: 2D array of shape (K, N) where K is the number of conditions and N is the number of samples :type data: np.array :param normalize: If True, normalize the second moment matrix by its trace :type normalize: bool
- Returns:
np.array – Second moment matrix of shape (K, K)
- utils.get_condition_mean(D, indicator)
Get the mean of the conditions
- Parameters:
D (np.array) – Data matrix
indicator (np.array) – Indicator for the conditions,
- utils.get_indicator_matrix(data)
Get the indicator matrix for the data
- Parameters:
data (np.array) – One dimensional data contianing the conditions
- Returns:
indicator_matrix (np.array) – Indicator matrix
- utils.read_mc_maze(data_path)
Read the data from the NHP maze task
- Parameters:
data_path (str) – Path to the data set
- Returns:
D (df) – Data frame of the continuous data (Hand pos, Hand vel, etc.)
trial_info (df) – Data frame of the trial information (Start time, end time, etc.)
units (df) – Data frame of the units (Spike times, KS label, etc.)
- utils.read_mc_rtt(data_path)
Read the data from the NHP random target reaching task
- Parameters:
data_path (str) – Path to the data set
- Returns:
D (df) – Data frame of the continuous data (Hand pos, Hand vel, etc.)
trial_info (df) – Data frame of the trial information (Start time, end time, etc.)
units (df) – Data frame of the units (Spike times, KS label, etc.)
- utils.read_nhp_sequence(data_path, **kwargs)
Read the data from the NHP sequence task
- Parameters:
data_path (str) – Path to the data set
- Returns:
D (df) – Data frame of the continuous data (Hand pos, Hand vel, etc.)
trial_info (df) – Data frame of the trial information (Start time, end time, etc.)
units (df) – Data frame of the units (Spike times, KS label, etc.)
- utils.read_point2point(data_path, **kwargs)
Read the data from point2point task
- Parameters:
data_path (str) – Path to the data set
- Returns:
D (df) – Data frame of the continuous data (Hand pos, Hand vel, etc.)
trial_info (df) – Data frame of the trial information (Start time, end time, etc.)
units (df) – Data frame of the units (Spike times, KS label, etc.)
- utils.resample(Data, timestamps)
Resamples using a linear interpolation
- Parameters:
Data (np.array) – Data to be resampled
timestamps (np.array) – Timestamps for the data
- Returns:
resampled_data (np.array)