Skip to content

Preprocessing #10

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/seizure-classification.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Pre-processing/environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: mne
channels:
- conda-forge
dependencies:
- python>=3.8
- pip
- numpy
- scipy
- matplotlib
- numba
- pandas
- xlrd
- scikit-learn
- h5py
- pillow
- statsmodels
- jupyter
- joblib
- psutil
- numexpr
- imageio
- tqdm
- spyder-kernels>=1.10.0
- imageio-ffmpeg>=0.4.1
- vtk>=9.0.1
- pyvista>=0.30
- pyvistaqt>=0.4
- qdarkstyle
- darkdetect
- mayavi
- PySurfer
- dipy
- nibabel
- nilearn
- python-picard
- pyqt!=5.15.3
- mne
- mffpy>=0.5.7
- ipywidgets
- pip:
- ipyvtklink
27 changes: 27 additions & 0 deletions Pre-processing/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from mne.io import concatenate_raws, read_raw_edf
from tfr_raw_psd import spectral_power, plot_tfr_and_raw_bin_width_all_channel, load

# ------------------------------------------------------

if __name__ == '__main__':
load_path, filename = load()
raw = read_raw_edf(load_path, preload=True)

#Bandpass filter 0.1Hz - 50Hz
raw = raw.filter(0.1, 50)

# Resample to 100 Hz
raw = raw.resample(100, npad='auto')

print(raw.info)
binNumber = 10

#Plot TFR and RAW data for the corresponding time for all channels
plot_tfr_and_raw_bin_width_all_channel(raw = raw, start=100, stop=350, filename=filename, binNumber=binNumber)

#Plot Spectral Power Density Plot of all channels
spectral_power(raw=raw, filename=filename)




114 changes: 114 additions & 0 deletions Pre-processing/tfr_raw_psd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import numpy as np
import matplotlib.pyplot as plt
import mne
import os

# -----------------------------------------------------

def spectral_power(raw, filename):
channel_names = raw.info['ch_names']

# Make a directory path
directory = filename + '_tfr_raw_spd'
# parent_path = 'C:/Users/User/Desktop/CSE 481C - Neruo Capstone/Project Repo/Data/GeneratedPlots/'
parent_path = os.getcwd() + '/saved_plots/'
path = os.path.join(parent_path, directory)

# If directory does not exist then make it
if (os.path.exists(path) == False):
os.mkdir(path)

for i in range(len(channel_names)):

# Select the required channel
requiredChannel = channel_names[i]

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


# -----------------------------------------------------

def plot_tfr_and_raw_bin_width_all_channel(raw, start, stop, filename, binNumber):
# Channel Info
channel_names = raw.info['ch_names']

# Setting reference
raw.set_eeg_reference(ref_channels=['EEG CZ-LE'])
rawLength = int(len(raw) / raw.info['sfreq'])
print(rawLength)

# Construct events
events = mne.make_fixed_length_events(raw, duration=rawLength - 1)

# Construct Epochs
epochs = mne.Epochs(raw, events, preload=True, tmin=0, tmax=rawLength - 1, baseline=None)

# Plotting TFRs
frequencies = np.arange(1, 50, 1)
power = mne.time_frequency.tfr_morlet(epochs, n_cycles=2, return_itc=False, freqs=frequencies, decim=3) # Run this only once

#debug
print(filename)
print(binNumber)

# Make a directory path
directory = filename + '_tfr_raw_spd'
# parent_path = 'C:/Users/User/Desktop/CSE 481C - Neruo Capstone/Project Repo/Data/GeneratedPlots/'
parent_path = os.getcwd() + '/saved_plots/'
path = os.path.join(parent_path, directory)

#If directory does not exist then make it
if(os.path.exists(path) == False):
os.mkdir(path)

for i in range(len(channel_names)):

# Select the required channel
requiredChannel = channel_names[i]

#Plot RAW
copy = raw.copy()
copy.pick(requiredChannel)
copy.crop(tmin=start, tmax=stop, include_tmax=True)

# Plotting begins!
data, times = copy[:, :]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(times, data.T, linewidth=0.5);
ax.set_xticklabels([])


plt.xlabel('Seconds')
plt.ylabel('$\mu V$')
plt.title('Channel: ' + str(requiredChannel));
positions = np.arange(0, start-stop, 1)



saveNameRAW = path + '/' + filename + '_' + 'bn' + str(binNumber) + '_' + str(requiredChannel) + '_raw'
plt.savefig(saveNameRAW, dpi=300)
plt.close(fig)

# Plot TFR
tfr_fig = power.plot(requiredChannel, tmin=start, tmax=stop, vmin=-0.000000005, vmax=0.000000005, show=False)
saveNamePower = path + '/' + filename + '_' + 'bn' + str(binNumber) + '_' + str(requiredChannel) + '_tfr'
tfr_fig.savefig(saveNamePower, dpi=300)


# -----------------------------------------------------

def load():
# Load Data
filename = '00000258_s001_t000'
fileformat = '.edf'
load_path = 'C:/Users/User/Desktop/CSE 481C - Neruo Capstone/Project Repo/Data/TUSZ/v1.5.2/edf/dev/02_tcp_le/002/00000258/s001_2003_07_16/' \
+ filename + fileformat
return load_path, filename

# -----------------------------------------------------
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"postcss": "^8.2.9",
"postcss": "^8.2.12",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
"react-scripts": "^4.0.3",
"web-vitals": "^1.0.1",
"yarn": "^1.22.10"
},
"scripts": {
"build:css": "postcss src/styles/tailwind.css -o src/styles/app.css",
"watch:css": "postcss src/styles/tailwind.css -o src/styles/app.css --watch",
"react-scripts:start": "sleep 5 && react-scripts start",
"react-scripts:start": "timeout 5 && react-scripts start",
Copy link
Collaborator

Choose a reason for hiding this comment

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

windows specific changes should be handled dynamically and not hardcoded. We should think about this carefully before merging to master.

Copy link
Owner

Choose a reason for hiding this comment

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

agreed.

"start": "run-p watch:css react-scripts:start",
"start-api": "cd api && venv/bin/flask run --no-debugger",
"build": "run-s build:css react-scripts:build",
Expand Down
15 changes: 7 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"resolved" "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.13.13.tgz"
"version" "7.13.13"
dependencies:
"@babel/compat-data" "^7.13.12"
"@babel/compat-data" "^7.13.15"
Copy link
Collaborator

Choose a reason for hiding this comment

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

same with these changes. We should probably have an OS-specific switch that determines the correct versions here.

"@babel/helper-validator-option" "^7.12.17"
"browserslist" "^4.14.5"
"semver" "^6.3.0"
Expand Down Expand Up @@ -237,8 +237,8 @@
"resolved" "https://registry.npmjs.org/@babel/helper-hoist-variables/-/helper-hoist-variables-7.13.0.tgz"
"version" "7.13.0"
dependencies:
"@babel/traverse" "^7.13.0"
"@babel/types" "^7.13.0"
"@babel/traverse" "^7.13.15"
"@babel/types" "^7.13.16"

"@babel/helper-member-expression-to-functions@^7.13.0", "@babel/helper-member-expression-to-functions@^7.13.12":
"integrity" "sha512-48ql1CLL59aKbU94Y88Xgb2VFy7a95ykGRbJJaaVv+LX5U8wFpLfiGXJJGUozsmA1oEh/o5Bp60Voq7ACyA/Sw=="
Expand Down Expand Up @@ -836,7 +836,7 @@
"resolved" "https://registry.npmjs.org/@babel/plugin-transform-block-scoping/-/plugin-transform-block-scoping-7.12.13.tgz"
"version" "7.12.13"
dependencies:
"@babel/helper-plugin-utils" "^7.12.13"
"@babel/helper-plugin-utils" "^7.13.0"

"@babel/plugin-transform-classes@^7.12.1", "@babel/plugin-transform-classes@^7.13.0":
"integrity" "sha512-9BtHCPUARyVH1oXGcSJD3YpsqRLROJx5ZNP6tN5vnk17N0SVf9WCtf8Nuh1CFmgByKKAIMstitKduoCmsaDK5g=="
Expand Down Expand Up @@ -1443,7 +1443,7 @@
"version" "7.13.15"
dependencies:
"@babel/code-frame" "^7.12.13"
"@babel/generator" "^7.13.9"
"@babel/generator" "^7.13.16"
"@babel/helper-function-name" "^7.12.13"
"@babel/helper-split-export-declaration" "^7.12.13"
"@babel/parser" "^7.13.15"
Expand Down Expand Up @@ -2092,7 +2092,6 @@
"resolved" "https://registry.npmjs.org/@types/glob/-/glob-7.1.1.tgz"
"version" "7.1.1"
dependencies:
"@types/events" "*"
"@types/minimatch" "*"
"@types/node" "*"

Expand Down Expand Up @@ -2306,8 +2305,8 @@
"resolved" "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-4.21.0.tgz"
"version" "4.21.0"
dependencies:
"@typescript-eslint/types" "4.21.0"
"@typescript-eslint/visitor-keys" "4.21.0"
"@typescript-eslint/types" "4.22.0"
"@typescript-eslint/visitor-keys" "4.22.0"

"@typescript-eslint/[email protected]":
"integrity" "sha512-+3+FCUJIahE9q0lDi1WleYzjCwJs5hIsbugIgnbB+dSCYUxl8L6PwmsyOPFZde2hc1DlTo/xnkOgiTLSyAbHiQ=="
Expand Down