Last active
November 20, 2018 00:34
-
-
Save Tehsurfer/db35df9a79e879509ff905f00e17ed84 to your computer and use it in GitHub Desktop.
A few functions for loading .json and writing it to OpenCOR and general csv
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, json, sys | |
import numpy as np | |
#if we are using utf-8 files, uncomment the next line | |
# sys.setdefaultencoding("UTF-8") #set the encode to utf8 | |
def load_json(filename): | |
with open(filename) as f: | |
data = json.load(f) | |
return data | |
def write_csv(filename, data): | |
f = csv.writer(open(filename, "w", newline='')) | |
datakeys = [] | |
for key in data: | |
datakeys.append(key) | |
f.writerow(datakeys) | |
for i, unused in enumerate(data[next(iter(data))]): | |
row = [] | |
for key in data: | |
row.append(data[key][i]) | |
f.writerow(row) | |
f.close() | |
def write_opencor(filename, data): | |
f = csv.writer(open(filename, "w", newline='')) | |
datakeys = ['environment | time (millisecond)'] | |
for key in data: | |
# note that we assume the keys here are in integers between 1-100. The %02d is to switch numbers such as '2' to '02' | |
datakeys.append( f' electrodes | V{("%02d" % int(key))} (millivolt)') | |
f.writerow(datakeys) | |
size = len(data[next(iter(data))]) | |
# currently time is 1->len(data) | |
time = np.linspace(0,size-1,size) | |
for i, unused in enumerate(data[next(iter(data))]): | |
row = [time[i].tolist()] | |
for key in data: | |
row.append(data[key][i]) | |
f.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment