Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preprocessing #10

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
27 changes: 22 additions & 5 deletions Pre-processing/PlotAllChannels.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import numpy as np
import matplotlib.pyplot as plt
from mne.io import concatenate_raws, read_raw_edf
import os


#Load Data
load_path = 'C:/Users/User/Desktop/CSE 481C - Neruo Capstone/Project Repo/Data/TUH/00003010_s003_t000.edf'
filename = '00003010_s003_t000'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's fine for now, but when I integrate with front end I want to pull this filename from the api call instead of hardcoded

fileformat = '.edf'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk if this would be necessary if filename passed through api call

load_path = 'C:/Users/User/Desktop/CSE 481C - Neruo Capstone/Project Repo/Data/TUH/' + filename + fileformat
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general relative paths are preferred, but passing in filename should make this a non-issue. When web-hosting this will be a more interesting problem. We should try to have pre-processing code run on the front end

print(load_path)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove debugging code when we integrate w master

raw = read_raw_edf(load_path, preload=True)

#Inspect the .edf file
print(raw.info)

#Make a directory
directory = filename + '_allChannels'
parent_path = 'C:/Users/User/Desktop/CSE 481C - Neruo Capstone/Project Repo/Data/GeneratedPlots/'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! once again relative paths, maybe something like os.pwd()

path = os.path.join(parent_path, directory)
os.mkdir(path)

#Plotting begins!
#Raw data
raw.plot(duration=100, block=True)
plt.show()
#todo: instead of scrolly image, get a static image
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we get this running on the front end, we might not even need to save anything! Tricky problem though since browser only runs js/css/html

bigPlot = raw.plot(duration=1200, block=True, show_scrollbars = False)
bigPlotName = path + '/' + filename + '_all' + '_RAW'
bigPlot.savefig(bigPlotName)
#plt.show()

#Power spectrum
#raw.plot_psd(tmax=np.inf)
#plt.show()

#Power spectrum
raw.plot_psd(tmax=np.inf)
plt.show()
psd_fig = raw.plot_psd(show=False)
saveNamePower = path + '/' + filename + '_all' + '_psd'
psd_fig.savefig(saveNamePower, dpi = 300)
66 changes: 41 additions & 25 deletions Pre-processing/PlotOneChannel.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
import numpy as np
import matplotlib.pyplot as plt
from mne.io import concatenate_raws, read_raw_edf
import os


#Load Data
load_path = 'C:/Users/User/Desktop/CSE 481C - Neruo Capstone/Project Repo/Data/TUH/00003010_s003_t000.edf'
filename = '00003010_s003_t000'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same note as above, relative paths + filename either passed in, or raw data is

fileformat = '.edf'
load_path = 'C:/Users/User/Desktop/CSE 481C - Neruo Capstone/Project Repo/Data/TUH/' + filename + fileformat
print(load_path)
raw = read_raw_edf(load_path, preload=True)

#Inspect the .edf file
print(raw.info['ch_names'])
ch_name = raw.info['ch_names']
print("Total Channels: ", len(ch_name))

#Select the required Channel
requiredChannel = ch_name[1]
raw.pick(requiredChannel)

#Plotting begins!
data, times = raw[:, :]
fig = plt.subplots(figsize=(10,8))
plt.plot(times, data.T);
plt.xlabel('Seconds')
plt.ylabel('$\mu V$')
plt.title('Channels: 1-5');
plt.legend(raw.ch_names[1:2]);
plt.show()


#Power spectrum
raw.plot_psd(tmax=np.inf)
plt.show()


channel_names = raw.info['ch_names']
len_channels = len(channel_names)
print(channel_names)
print("Total Channels: ", len_channels)

#Make a directory
directory = filename
parent_path = 'C:/Users/User/Desktop/CSE 481C - Neruo Capstone/Project Repo/Data/GeneratedPlots/'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relative path

path = os.path.join(parent_path, directory)
os.mkdir(path)

def makePlots(i):
#Select the required Channel
requiredChannel = channel_names[i]
copy = raw.copy()
copy.pick(requiredChannel)

#Plotting begins!
data, times = copy[:, :]
fig = plt.figure()
plt.plot(times, data.T);
plt.xlabel('Seconds')
plt.ylabel('$\mu V$')
plt.title('Channel: ' + str(requiredChannel));
saveNameRAW = path + '/' + filename + '_' + requiredChannel + '_RAW'
plt.savefig(saveNameRAW, dpi = 300)
plt.close(fig)


#Power spectrum
psd_fig = copy.plot_psd(show=False)
saveNamePower = path + '/' + filename + '_' + requiredChannel + '_psd'
psd_fig.savefig(saveNamePower, dpi = 300)

ran = range(1, len_channels)
for i in ran:
makePlots(i)