Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Activity-Reference-Scripts/generate_csv.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
220 lines (188 sloc)
8.96 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import os | |
import matplotlib.pyplot as plt | |
import tkinter as tk | |
class CEvent_Categorical: | |
def __init__(self, label, onset, offset): | |
self.label = label | |
self.onset = onset | |
self.offset = offset | |
def generate_csv(input_file): | |
""" Takes an input CSV file generated by datavyu and generates a formatted CSV file | |
Args: | |
input_file::str | |
Path to CSV file generated by datavyu | |
Returns: | |
None | |
""" | |
output_rows = [] | |
output_filename = input_file.split(".")[0] + "_FORMATTED.csv" | |
# Open a datavyu CSV file for reading | |
with open(input_file, "r") as csv_file: | |
reader = csv.reader(csv_file, delimiter=",") | |
labels = next(reader) | |
names = [] | |
index = 0 | |
while(labels[index] != ""): | |
names.append(labels[index].split(".")[0]) | |
# Increment by 4 in order to get to next name (there are 4 columns per name) | |
index += 4 | |
for row in reader: | |
# Set index to 1 because we're looking at the 'onset time' field for each name | |
index = 1 | |
while(index < len(row)): | |
if(row[index] != ""): | |
# Divide by 1000 to get data in seconds | |
# Divide index for 'names' list by 4 to get appropriate name for data | |
output_row = [float(row[index])/1000, float(row[index+1])/1000, names[index/4]] | |
output_rows.append(output_row) | |
index += 4 | |
# Sort the rows of the CSV file by 'onset time' field | |
output_rows = sorted(output_rows, key=lambda row: row[1]) | |
# Open a CSV file to write formatted data | |
with open(output_filename, "wb") as output_csv_file: | |
writer = csv.writer(output_csv_file, delimiter=",") | |
writer.writerows(output_rows) | |
def generate_csvs(directory): | |
""" Takes a directory of subjects and generates a formatted CSV file for each datavyu CSV file | |
Args: | |
directory::str | |
Path to directory of subject directories | |
Returns: | |
None | |
""" | |
for subject in os.listdir(directory): | |
try: | |
path = "{0}\\{1}\\Data\\Activity Coding\\{2}_EXP.csv".format(directory, subject, subject.upper()) | |
if(os.path.exists(path)): | |
generate_csv(path) | |
except IOError as e: | |
print("Error opening CSV file: " + str(e)) | |
def generate_plot(subject, formatted_file, title, timepoints=None, objects=None, fixed=False, activity=True): | |
""" Generates plot for specified subject, C_Event file, time range, and objects | |
Args: | |
subject::str | |
Subject whose data is being examined | |
formatted_file::str | |
Path to C_Event file in CSV format for given subject | |
timepoints::tuple | |
Tuple of float values where timepoints[0] is the start point and timepoints[1] is the end point | |
objects::list | |
List of strings of objects to include in plot. If None is given, all objects are included | |
Returns: | |
None | |
""" | |
labels = set() | |
label_indices = dict() | |
c_events = [] | |
colors = ["#ff0000", "#ffd700", "#40e0d0", "#0000ff", "#00ff00", "#ffc0cb", "#468499", "#800080", | |
"#808080", "#000000", "#ffc3a0", "#ff7f50", "#bada55", "#afeeee", "#cbbeb5", "#53454C", | |
"#01B9FE", "#1C6406", "#98B686", "#B6C050", "#C87F35", "#2B106A", "#D330BC"] | |
if(fixed): | |
fixed_labels = ["BB", "BERT", "BK_CLR", "BK_LS", "CAR", "COW_B", "COW_W", "DOG", | |
"DOLL", "EGG", "FOOT", "HAND", "HAT", "OTH", "PENG", "PIG_L", "PIG_S", "RING", | |
"RTL", "SHEEP_L", "SHEEP_S", "TRUCK", "MULTI"] | |
labels = set(fixed_labels) | |
for i, label in enumerate(fixed_labels): | |
label_indices[label] = i | |
with open(formatted_file, "r") as csvfile: | |
reader = csv.reader(csvfile, delimiter=",") | |
for row in reader: | |
if((not objects) or (objects and row[2] in objects)): | |
c_event = CEvent_Categorical(row[2], float(row[0]), float(row[1])) | |
c_events.append(c_event) | |
if(not fixed): | |
labels.add(c_event.label) | |
if(not c_event.label in label_indices): | |
label_indices[c_event.label] = len(labels)-1 | |
fig = plt.figure() | |
fig.set_size_inches(18.5, 5.5, forward=True) | |
ax = fig.add_subplot(111) | |
label_positions = [0.1*i for i in label_indices.values()] | |
plt.yticks(label_positions, labels) | |
plt.title(title) | |
plt.xlabel("Time (s)") | |
if(timepoints): | |
plt.xlim(timepoints[0], timepoints[1]) | |
for point in c_events: | |
ax.barh(0.1*label_indices[point.label], point.offset-point.onset, 0.05, | |
left=point.onset, align="center", color=colors[label_indices[point.label]]) | |
plt.show() | |
def plot(subject, start_time=None, end_time=None, objects_list="", fixed=False, variable="Activity"): | |
""" Wrapper function for generate_plot(). Parses input and calls generate_plot() | |
Args: | |
subject::str | |
Subject whose data is being examined | |
start_time::str | |
String that represents start time for plot. Cast to float | |
end_time::str | |
String that represents end time for plot. Cast to float | |
objects_list::str | |
String of comma-separated objects. If only whitespace is given, all objects are included | |
Returns: | |
None | |
""" | |
# Testing for whitespace-only data in objects_list | |
objects_list = objects_list.rstrip() | |
if(objects_list == ""): | |
objects = None | |
else: | |
objects = objects_list.split(",") | |
if(variable == "Activity"): | |
path = "Q:\\PSY-LAB\\Suanda\\Rollins\\9mos\\{0}\\Data\\Activity Coding\\{1}_EXP_FORMATTED.csv".format(subject, subject.upper()) | |
title = "Activity Coding - " + subject | |
elif(variable == "Reference"): | |
path = "Q:\\PSY-LAB\\Suanda\\Rollins\\9mos\\{0}\\Data\\Reference Coding\\{1}_cevent_ref.csv".format(subject, subject.upper()) | |
title = "Reference Coding - " + subject | |
else: | |
return False | |
# Try to specify timepoints using start_time and end_time, exits "try" block if either start_time or end_time weren't specified/aren't proper numbers | |
try: | |
generate_plot(subject, | |
path, | |
title, | |
(float(start_time), float(end_time)), | |
objects, | |
fixed=fixed) | |
except ValueError: | |
generate_plot(subject, | |
path, | |
title, | |
None, | |
objects, | |
fixed=fixed) | |
def main(): | |
window = tk.Tk() | |
window.title("Plotting") | |
window.geometry("350x250") | |
tk.Label(window, text="Subject ID").grid(row=0) | |
subject = tk.Entry(window, width=20) | |
subject.grid(row=0, column=1) | |
tk.Label(window, text="Start Time").grid(row=1) | |
start_time = tk.Entry(window, width=20) | |
start_time.grid(row=1, column=1) | |
tk.Label(window, text="End Time").grid(row=2) | |
end_time = tk.Entry(window, width=20) | |
end_time.grid(row=2, column=1) | |
tk.Label(window, text="Objects (comma-separated)").grid(row=3) | |
objects = tk.Text(window, height=3, width=20) | |
objects.grid(row=3, column=1) | |
choices = {"Auto", "Fixed"} | |
var = tk.StringVar(window) | |
var.set("Auto") | |
tk.Label(window, text="Label Ordering").grid(row=4) | |
dropdown = tk.OptionMenu(window, var, *choices) | |
dropdown.grid(row=4, column=1) | |
types = {"Activity", "Reference"} | |
types_var = tk.StringVar(window) | |
types_var.set("Activity") | |
tk.Label(window, text="Variable").grid(row=5) | |
types_dropdown = tk.OptionMenu(window, types_var, *types) | |
types_dropdown.grid(row=5, column=1) | |
padding = tk.Frame(window, height=25) | |
padding.grid(row=6) | |
button = tk.Button(window, text="Plot", command=lambda: plot(subject.get(), start_time.get(), end_time.get(), objects.get("1.0", tk.END), (var.get() == "Fixed"), types_var.get())) | |
button.grid(row=7, column=1) | |
window.mainloop() | |
if(__name__ == "__main__"): | |
main() |