Chapter 4c

Chapter 4c

Analyzing Neural Time Series Data

Python code for Chapter 4 script C – converted from original Matlab by AE Studio (and ChatGPT)
Original Matlab code by Mike X Cohen

This code accompanies the book, titled “Analyzing Neural Time Series Data” (MIT Press).
Using the code without following the book may lead to confusion, incorrect data analyses, and misinterpretations of results.
Mike X Cohen and AE Studio assume no responsibility for inappropriate or incorrect use of this code.

Import necessary libraries

import numpy as np
from tkinter import filedialog
from tkinter import Tk
import csv

Clearing variables

Clearing variables in Python are almost always done by simply overwriting them, but they can also be deleted.

# Here's an example of deleting a variable:
leave_me_alone = 10
remove_me_please = 20
del remove_me_please

# To see the list of variables currently in the namespace, you can use:


# Trying to print a deleted variable will raise an error in Python.
# Uncommenting the following line will raise a NameError:
# print(remove_me_please)
Variable         Type      Data/Info
------------------------------------
Tk               type      <class 'tkinter.Tk'>
csv              module    <module 'csv' from '/User<...>2/lib/python3.10/csv.py'>
filedialog       module    <module 'tkinter.filedial<...>0/tkinter/filedialog.py'>
leave_me_alone   int       10
np               module    <module 'numpy' from '/Us<...>kages/numpy/__init__.py'>

Basic importing text data

# In Python, you can load numeric data from a text file using numpy.

# Loading data from a text file:
data = np.loadtxt('../data/chapter04_datafile.txt')

# For a more interactive approach, you can use a file dialog to select a file:
root = Tk()
root.withdraw()  # we don't want a full GUI, so keep the root window from appearing
file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
data = np.loadtxt(file_path)

# Reading data from an Excel file can be done using pandas:
import pandas as pd

# Read data from an Excel file:
excel_data = pd.read_excel('../data/chapter04_excel_data.xls')
numberdata = excel_data.to_numpy()
textdata = excel_data.columns.values
raw_data = excel_data.values
2023-12-06 13:04:11.321 python[29248:42197023] +[CATransaction synchronize] called within transaction

Advanced importing text data

# In Python, you can use the csv module for more complex data import scenarios.

behavioral_data = []  # Initialize an empty list to store the data.

# Open the file and read line by line.
with open('../data/chapter04_headache_data.txt', 'r') as fid:
    reader = csv.reader(fid, delimiter='\t')
    
    datarow = 0
    for row in reader:
        if 'trial' not in [element.lower() for element in row]:
            continue  # Skip to the next iteration if 'trial' is not in the row.
        
        # Find the column indices for the relevant data.
        trial_column = row.index('trial')
        choice_column = row.index('choice')
        rt_column = row.index('rt')
        accuracy_column = row.index('accuracy')
        
        # Extract and convert the data, then append to the behavioral_data list.
        behavioral_data.append([
            float(row[trial_column + 1]),
            float(row[choice_column + 1]),
            float(row[rt_column + 1]),
            float(row[accuracy_column + 1])
        ])
        
        datarow += 1  # Increment the row counter.

Initializing variables

num_rows = 10
num_cols = 35

# Initialize with zeros (typical approach in Python)
largematrix = np.zeros((num_rows, num_cols))

# Example of processing in a nested loop
for rowi in range(num_rows):
    for coli in range(num_cols):
        # Processing here...
        pass

# Note that in Python, you can dynamically resize numpy arrays, but it's not efficient.
largematrix = np.pad(largematrix, ((0, 1), (0, 0)), mode='constant', constant_values=10) # Add a new row.
largematrix = np.reshape(largematrix, (largematrix.shape[0], largematrix.shape[1], 1)) # Add a dimension.
largematrix = np.pad(largematrix, ((0, 0), (0, 0), (0, 2)), mode='constant', constant_values=0)
largematrix[0, num_cols // 2, 2] = 100

# Decrease dimensions by re-assignment:
largematrix = largematrix[:, :, 0]  # Remove the last dimension.

# You can also decrease the size of arrays by slicing:
print(largematrix.shape)
largematrix = largematrix[:, :-5]  # Remove the last 5 columns.
print(largematrix.shape)

# Again, changing matrix sizes and dimensions should be avoided when
# possible, and done carefully when necessary.
(11, 35)
(11, 30)

Basic saving data

# Save as a .npy file (Python's binary format, readable with numpy):
np.save('../data/my_python_variables.npy', data)

# Save as a text file with a specific delimiter:
np.savetxt('../data/data_written_from_python.txt', data, delimiter='\t')

Advanced saving data

# Writing data to a text file in a format that can be imported into SPSS or Excel.
with open('../data/data_output_SPSS_format.txt', 'w', newline='') as fid:
    writer = csv.writer(fid, delimiter='\t')
    
    # Variable labels
    variable_labels = ['Name', 'trial', 'choice', 'rt', 'accuracy']
    
    # Subject names
    subject_names = ['billy', 'bob']
    
    # Write variable labels
    writer.writerow(variable_labels)
    
    # Write data rows
    for datarowi, data_row in enumerate(behavioral_data):
        # Write subject name and data
        writer.writerow([subject_names[datarowi % len(subject_names)]] + data_row)
        print(f'Finished writing line {datarowi + 1} of {len(behavioral_data)}')
Finished writing line 1 of 2
Finished writing line 2 of 2