Skip to content

Commit

Permalink
+ coherence
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Kramer committed Nov 5, 2024
1 parent 4df55ef commit b272b70
Show file tree
Hide file tree
Showing 30 changed files with 2,112 additions and 113 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Analyzing_Rhythms_Lab_3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.18"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down
132 changes: 132 additions & 0 deletions Coherence_Lab_Part_1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"title: Coherence Part 1 (Two noise signals)\n",
"project:\n",
" type: website\n",
"format:\n",
" html:\n",
" code-fold: false\n",
" code-tools: true\n",
"jupyter: python 3\n",
"number-sections: false\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load modules we'll need.\n",
"from scipy.io import loadmat\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from scipy.signal import spectrogram"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Make two noise signals"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"N = 1000;\n",
"dt= 0.001;\n",
"T = N*dt;\n",
"x = np.random.randn(N)\n",
"y = np.random.randn(N)\n",
"t = np.arange(0,N)*dt\n",
"\n",
"plt.plot(t,x)\n",
"plt.plot(t,y)\n",
"plt.xlabel('Time [s]');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compute the cross-covariance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cc_xy = \"SOMETHING\" # Compute the covariance.\n",
"lags = np.arange(-N + 1, N) # Create a lag axis,\n",
"plt.plot(lags * dt, cc_xy) # ... and plot the result.\n",
"plt.ylim([-0.1, 1])\n",
"plt.xlabel('Lag [s]')\n",
"plt.ylabel('Cross-covariance');\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compute the coherence"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Xf = np.fft.fft(x - x.mean()) # Compute Fourier transform of x\n",
"Yf = np.fft.fft(y - y.mean()) # Compute Fourier transform of y\n",
"\n",
"# Compute the spectra\n",
"Sxx = ??? # Spectrum of E1 trials\n",
"Syy = ??? # ... and E2 trials\n",
"Sxy = ??? # ... and the cross spectrum\n",
"\n",
"# Compute the coherence.\n",
"cohr = ???\n",
"\n",
"# Define a frequency axis.\n",
"f = np.fft.fftfreq(N, dt)\n",
"\n",
"# Plot the result.\n",
"plt.plot(f, cohr.real)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
163 changes: 163 additions & 0 deletions Coherence_Lab_Part_2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"title: Coherence Part 2 (Two noise signals, again)\n",
"project:\n",
" type: website\n",
"format:\n",
" html:\n",
" code-fold: false\n",
" code-tools: true\n",
"jupyter: python 3\n",
"number-sections: false\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load modules we'll need.\n",
"\n",
"from scipy.io import loadmat\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from scipy.signal import spectrogram"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Make two noise signals, with multiple trials"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"N = 1000;\n",
"K = 100;\n",
"dt= 0.001;\n",
"T = N*dt;\n",
"x = np.random.randn(K,N)\n",
"y = np.random.randn(K,N)\n",
"t = np.arange(0,N)*dt\n",
"\n",
"plt.plot(t,x[0,:])\n",
"plt.plot(t,y[0,:])\n",
"plt.xlabel('Time [s]');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualize the data across all trials"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(x, # ... and show the image,\n",
" extent=[min(t), max(t), K, 1], # ... with meaningful axes,\n",
" aspect='auto') # ... and a nice aspect ratio\n",
"plt.xlabel('Time [s]')\n",
"plt.ylabel('Trial #');\n",
"plt.title('All trials from E1');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compute the cross-covariance, averaged across trials"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cc_xy = \"SOMETHING\" # Compute cc for each trial, \n",
"cc_xy = np.mean(cc_xy,0) # ... average over trials,\n",
"lags = np.arange(-N + 1, N) # ... create a lag axis,\n",
"plt.plot(lags * dt, cc_xy) # ... and plot the result.\n",
"plt.xlabel('Lag [s]')\n",
"plt.ylabel('Trial averaged cross-covariance');\n",
"plt.ylim([-0.1, 1]);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compute the coherence"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Fourier transforms.\n",
"Xf = \"SOMETHING\" # Compute Fourier transform of x for each trial\n",
"Yf = \"SOMETHING\" # Compute Fourier transform of y for each trial\n",
"\n",
"# Auto- and cross-spectra.\n",
"Sxx = \"SOMETHING\" # Spectrum of E1 trials\n",
"Syy = \"SOMETHING\" # ... and E2 trials\n",
"Sxy = \"SOMETHING\" # ... and the cross spectrum\n",
"\n",
"# Trial average.\n",
"Sxx = np.mean(Sxx,0)\n",
"Syy = np.mean(Syy,0)\n",
"Sxy = np.mean(Sxy,0)\n",
"\n",
"# Calculate coherence.\n",
"cohr_squared = \"SOMETHING\"\n",
"\n",
"f = np.fft.fftfreq(N, dt) # Define a frequency axis.\n",
"plt.plot(f, cohr_squared.real) # Plot the coherence.\n",
"plt.ylim([0, 1.1]) # ... with y-axis scaled,\n",
"plt.xlabel('Frequency [Hz]') # ... and with axes labeled.\n",
"plt.ylabel('Coherence')\n",
"plt.title('Trial averaged coherence between two electrodes');"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
2 changes: 1 addition & 1 deletion HH.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.18"
"version": "3.12.4"
}
},
"nbformat": 4,
Expand Down
Binary file added Readings/Fries_TINS_2005.pdf
Binary file not shown.
Binary file added Readings/Traub_J_Neurophysiol_2003.pdf
Binary file not shown.
Binary file added Slides/Coherence_Lecture_1.pdf
Binary file not shown.
Binary file renamed docs/.DS_Store → _extensions/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/Analyzing_Rhythms_Lab_1.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>

<meta charset="utf-8">
<meta name="generator" content="quarto-1.5.56">
<meta name="generator" content="quarto-1.5.57">

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">

Expand Down
2 changes: 1 addition & 1 deletion docs/Analyzing_Rhythms_Lab_2a.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>

<meta charset="utf-8">
<meta name="generator" content="quarto-1.5.56">
<meta name="generator" content="quarto-1.5.57">

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">

Expand Down
2 changes: 1 addition & 1 deletion docs/Analyzing_Rhythms_Lab_2b.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>

<meta charset="utf-8">
<meta name="generator" content="quarto-1.5.56">
<meta name="generator" content="quarto-1.5.57">

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">

Expand Down
2 changes: 1 addition & 1 deletion docs/Analyzing_Rhythms_Lab_3.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>

<meta charset="utf-8">
<meta name="generator" content="quarto-1.5.56">
<meta name="generator" content="quarto-1.5.57">

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">

Expand Down
4 changes: 2 additions & 2 deletions docs/Backpropagation.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>

<meta charset="utf-8">
<meta name="generator" content="quarto-1.5.56">
<meta name="generator" content="quarto-1.5.57">

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">

Expand Down Expand Up @@ -228,7 +228,7 @@
}

// Store cell data
globalThis.qpyodideCellDetails = [{"code":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd","id":1,"options":{"classes":"","out-width":"700px","results":"markup","read-only":"false","label":"","dpi":72,"context":"interactive","message":"true","autorun":"","fig-height":5,"comment":"","output":"true","fig-width":7,"warning":"true","fig-cap":"","out-height":""}},{"code":"df = pd.read_csv(\"https://raw.githubusercontent.com/Mark-Kramer/BU-MA665-MA666/master/Data/backpropagation_example_data.csv\")\n\n# Extract the variables from the loaded data\nin_true = np.array(df.iloc[:,0]) #Get the values associated with the first column of the dataframe\nout_true = np.array(df.iloc[:,1]) #Get the values associated with the second column of the dataframe","id":2,"options":{"classes":"","out-width":"700px","results":"markup","read-only":"false","label":"","dpi":72,"context":"interactive","message":"true","autorun":"","fig-height":5,"comment":"","output":"true","fig-width":7,"warning":"true","fig-cap":"","out-height":""}},{"code":"print(np.transpose([in_true, out_true]))","id":3,"options":{"classes":"","out-width":"700px","results":"markup","read-only":"false","label":"","dpi":72,"context":"interactive","message":"true","autorun":"","fig-height":5,"comment":"","output":"true","fig-width":7,"warning":"true","fig-cap":"","out-height":""}},{"code":"def sigmoid(x):\n return 1/(1+np.exp(-x)) # Define the sigmoid anonymous function.\n\ndef feedforward(w, s0): # Define feedforward solution.\n # ... x1 = activity of first neuron,\n # ... s1 = output of first neuron,\n # ... x2 = activity of second neuron,\n # ... s2 = output of second neuron,\n # ... out = output of neural network.\n return out,s1,s2","id":4,"options":{"classes":"","out-width":"700px","results":"markup","read-only":"false","label":"","dpi":72,"context":"interactive","message":"true","autorun":"","fig-height":5,"comment":"","output":"true","fig-width":7,"warning":"true","fig-cap":"","out-height":""}},{"code":"w = [0.5,0.5] # Choose initial values for the weights.\nalpha = 0.01 # Set the learning constant.\n\nK = np.size(in_true);\nresults = np.zeros([K,3]) # Define a variable to hold the results of each iteration. \n\nfor k in np.arange(K):\n s0 = in_true[k] # Define the input,\n target = out_true[k] # ... and the target output.\n \n #Calculate feedforward solution to get output.\n \n #Update the weights.\n w0 = w[0]; w1 = w[1];\n w[1] = \"SOMETHING\"\n w[0] = \"SOMETHING\"\n \n # Save the results of this step. --------------------------------------\n # Here we save the 3 weights, and the neural network output.\n # results[k,:] = [w[0],w[1], out]\n\n# Plot the NN weights and error during training \n# plt.clf()\n# plt.plot(results[:,1], label='w1')\n# plt.plot(results[:,0], label='w0')\n# plt.plot(results[:,2]-target, label='error')\n# plt.legend() #Include a legend,\n# plt.xlabel('Iteration number'); #... and axis label.\n\n# Print the NN weights\n# print(results[-1,0:2])","id":5,"options":{"classes":"","out-width":"700px","results":"markup","read-only":"false","label":"","dpi":72,"context":"interactive","message":"true","autorun":"","fig-height":5,"comment":"","output":"true","fig-width":7,"warning":"true","fig-cap":"","out-height":""}}];
globalThis.qpyodideCellDetails = [{"options":{"warning":"true","label":"","out-width":"700px","context":"interactive","read-only":"false","classes":"","comment":"","message":"true","output":"true","autorun":"","dpi":72,"out-height":"","fig-width":7,"fig-cap":"","fig-height":5,"results":"markup"},"id":1,"code":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd"},{"options":{"warning":"true","label":"","out-width":"700px","context":"interactive","read-only":"false","classes":"","comment":"","message":"true","output":"true","autorun":"","dpi":72,"out-height":"","fig-width":7,"fig-cap":"","fig-height":5,"results":"markup"},"id":2,"code":"df = pd.read_csv(\"https://raw.githubusercontent.com/Mark-Kramer/BU-MA665-MA666/master/Data/backpropagation_example_data.csv\")\n\n# Extract the variables from the loaded data\nin_true = np.array(df.iloc[:,0]) #Get the values associated with the first column of the dataframe\nout_true = np.array(df.iloc[:,1]) #Get the values associated with the second column of the dataframe"},{"options":{"warning":"true","label":"","out-width":"700px","context":"interactive","read-only":"false","classes":"","comment":"","message":"true","output":"true","autorun":"","dpi":72,"out-height":"","fig-width":7,"fig-cap":"","fig-height":5,"results":"markup"},"id":3,"code":"print(np.transpose([in_true, out_true]))"},{"options":{"warning":"true","label":"","out-width":"700px","context":"interactive","read-only":"false","classes":"","comment":"","message":"true","output":"true","autorun":"","dpi":72,"out-height":"","fig-width":7,"fig-cap":"","fig-height":5,"results":"markup"},"id":4,"code":"def sigmoid(x):\n return 1/(1+np.exp(-x)) # Define the sigmoid anonymous function.\n\ndef feedforward(w, s0): # Define feedforward solution.\n # ... x1 = activity of first neuron,\n # ... s1 = output of first neuron,\n # ... x2 = activity of second neuron,\n # ... s2 = output of second neuron,\n # ... out = output of neural network.\n return out,s1,s2"},{"options":{"warning":"true","label":"","out-width":"700px","context":"interactive","read-only":"false","classes":"","comment":"","message":"true","output":"true","autorun":"","dpi":72,"out-height":"","fig-width":7,"fig-cap":"","fig-height":5,"results":"markup"},"id":5,"code":"w = [0.5,0.5] # Choose initial values for the weights.\nalpha = 0.01 # Set the learning constant.\n\nK = np.size(in_true);\nresults = np.zeros([K,3]) # Define a variable to hold the results of each iteration. \n\nfor k in np.arange(K):\n s0 = in_true[k] # Define the input,\n target = out_true[k] # ... and the target output.\n \n #Calculate feedforward solution to get output.\n \n #Update the weights.\n w0 = w[0]; w1 = w[1];\n w[1] = \"SOMETHING\"\n w[0] = \"SOMETHING\"\n \n # Save the results of this step. --------------------------------------\n # Here we save the 3 weights, and the neural network output.\n # results[k,:] = [w[0],w[1], out]\n\n# Plot the NN weights and error during training \n# plt.clf()\n# plt.plot(results[:,1], label='w1')\n# plt.plot(results[:,0], label='w0')\n# plt.plot(results[:,2]-target, label='error')\n# plt.legend() #Include a legend,\n# plt.xlabel('Iteration number'); #... and axis label.\n\n# Print the NN weights\n# print(results[-1,0:2])"}];


</script>
Expand Down
Loading

0 comments on commit b272b70

Please sign in to comment.