import numpy as np
from tkinter import filedialog
from tkinter import Tk
import csv
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
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:
= 10
leave_me_alone = 20
remove_me_please 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:
= np.loadtxt('../data/chapter04_datafile.txt')
data
# For a more interactive approach, you can use a file dialog to select a file:
= Tk()
root # we don't want a full GUI, so keep the root window from appearing
root.withdraw() = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
file_path = np.loadtxt(file_path)
data
# Reading data from an Excel file can be done using pandas:
import pandas as pd
# Read data from an Excel file:
= pd.read_excel('../data/chapter04_excel_data.xls')
excel_data = excel_data.to_numpy()
numberdata = excel_data.columns.values
textdata = excel_data.values raw_data
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.
= [] # Initialize an empty list to store the data.
behavioral_data
# Open the file and read line by line.
with open('../data/chapter04_headache_data.txt', 'r') as fid:
= csv.reader(fid, delimiter='\t')
reader
= 0
datarow 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.
= row.index('trial')
trial_column = row.index('choice')
choice_column = row.index('rt')
rt_column = row.index('accuracy')
accuracy_column
# 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])
])
+= 1 # Increment the row counter. datarow
Initializing variables
= 10
num_rows = 35
num_cols
# Initialize with zeros (typical approach in Python)
= np.zeros((num_rows, num_cols))
largematrix
# 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.
= 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
largematrix[
# Decrease dimensions by re-assignment:
= largematrix[:, :, 0] # Remove the last dimension.
largematrix
# You can also decrease the size of arrays by slicing:
print(largematrix.shape)
= largematrix[:, :-5] # Remove the last 5 columns.
largematrix 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):
'../data/my_python_variables.npy', data)
np.save(
# Save as a text file with a specific delimiter:
'../data/data_written_from_python.txt', data, delimiter='\t') np.savetxt(
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:
= csv.writer(fid, delimiter='\t')
writer
# Variable labels
= ['Name', 'trial', 'choice', 'rt', 'accuracy']
variable_labels
# Subject names
= ['billy', 'bob']
subject_names
# Write variable labels
writer.writerow(variable_labels)
# Write data rows
for datarowi, data_row in enumerate(behavioral_data):
# Write subject name and data
% len(subject_names)]] + data_row)
writer.writerow([subject_names[datarowi print(f'Finished writing line {datarowi + 1} of {len(behavioral_data)}')
Finished writing line 1 of 2
Finished writing line 2 of 2