test.py aktualisiert

This commit is contained in:
mbergmann 2024-10-22 07:21:37 +00:00
parent 7e02490a89
commit 54d10789b4

View File

@ -1,101 +1,131 @@
import glob import glob
import pathlib import pathlib
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import rlxnix as rlx import rlxnix as rlx
from IPython import embed from IPython import embed
from scipy.signal import welch from scipy.signal import welch
def binary_spikes(spike_times, duration, dt): def binary_spikes(spike_times, duration, dt):
""" """
Converts the spike times to a binary representations Converts the spike times to a binary representations
Parameters Parameters
---------- ----------
spike_times : np.array spike_times : np.array
The spike times. The spike times.
duration : float duration : float
The trial duration: The trial duration:
dt : float dt : float
The temporal resolution. The temporal resolution.
Returns Returns
------- -------
binary : np.array binary : np.array
The binary representation of the spike train. The binary representation of the spike train.
""" """
binary = np.zeros(int(np.round(duration / dt))) #create the binary array with the same length as potential binary = np.zeros(int(np.round(duration / dt))) #create the binary array with the same length as potential
spike_indices = np.asarray(np.round(spike_times / dt), dtype = int) # get the indices spike_indices = np.asarray(np.round(spike_times / dt), dtype = int) # get the indices
binary[spike_indices] = 1 # put the indices into binary binary[spike_indices] = 1 # put the indices into binary
return binary return binary
def firing_rate(binary_spikes, dt = 0.000025, box_width = 0.01): def firing_rate(binary_spikes, dt = 0.000025, box_width = 0.01):
box = np.ones(int(box_width // dt)) box = np.ones(int(box_width // dt))
box /= np.sum(box) * dt # normalisierung des box kernels to an integral of one box /= np.sum(box) * dt # normalisierung des box kernels to an integral of one
rate = np.convolve(binary_spikes, box, mode = 'same') rate = np.convolve(binary_spikes, box, mode = 'same')
return rate return rate
def power_spectrum(rate, dt): def power_spectrum(rate, dt):
freq, power = welch(rate, fs = 1/dt, nperseg = 2**16, noverlap = 2**15) freq, power = welch(rate, fs = 1/dt, nperseg = 2**16, noverlap = 2**15)
return freq, power return freq, power
#find example data def extract_stim_data(stimulus):
datafolder = "../data" '''
extracts all necessary metadata for each stimulus
example_file = datafolder + "/" + "2024-10-16-ad-invivo-1.nix"
Parameters
#load dataset ----------
dataset = rlx.Dataset(example_file) stimulus : Stimulus object or rlxnix.base.repro module
# find all sams The stimulus from which the data is needed.
sams = dataset.repro_runs('SAM')
sam = sams[2] # our example sam Returns
potential,time = sam.trace_data("V-1") #membrane potential -------
spike_times, _ = sam.trace_data('Spikes-1') #spike times amplitude : float
df = sam.metadata['RePro-Info']['settings']['deltaf'][0][0] #find df in metadata The relative signal amplitude in percent.
amp = sam.metadata['RePro-Info']['settings']['contrast'][0][0] * 100 #find amplitude in metadata df : float
Distance of the stimulus to the current EODf.
#figure for a quick plot eodf : float
fig = plt.figure(figsize = (5, 2.5)) Current EODf.
ax = fig.add_subplot() stim_freq : float
ax.plot(time[time < 0.1], potential[time < 0.1]) # plot the membrane potential in 0.1s The total stimulus frequency (EODF+df).
ax.scatter(spike_times[spike_times < 0.1],
np.ones_like(spike_times[spike_times < 0.1]) * np.max(potential)) #plot teh spike times on top '''
plt.show() # extract metadata
plt.close() # the stim.name adjusts the first key as it changes with every stimulus
# get all the stimuli amplitude = stim.metadata[stim.name]['Contrast'][0][0]
stims = sam.stimuli df = stim.metadata[stim.name]['DeltaF'][0][0]
# empty list for the spike times eodf = stim.metadata[stim.name]['EODf'][0][0]
spikes = [] stim_freq = stim.metadata[stim.name]['Frequency'][0][0]
#spikes2 = np.array(range(len(stims))) return amplitude, df, eodf, stim_freq
# loop over the stimuli
for stim in stims:
# get the spike times #find example data
spike, _ = stim.trace_data('Spikes-1') datafolder = "../data"
# append the first 100ms to spikes
spikes.append(spike[spike < 0.1]) example_file = datafolder + "/" + "2024-10-16-ad-invivo-1.nix"
# get stimulus duration
duration = stim.duration #load dataset
ti = stim.trace_info("V-1") dataset = rlx.Dataset(example_file)
dt = ti.sampling_interval # get the stimulus interval # find all sams
bin_spikes = binary_spikes(spike, duration, dt) #binarize the spike_times sams = dataset.repro_runs('SAM')
print(len(bin_spikes)) sam = sams[2] # our example sam
pot,tim= stim.trace_data("V-1") #membrane potential potential,time = sam.trace_data("V-1") #membrane potential
rate = firing_rate(bin_spikes, dt = dt) spike_times, _ = sam.trace_data('Spikes-1') #spike times
print(np.mean(rate)) df = sam.metadata['RePro-Info']['settings']['deltaf'][0][0] #find df in metadata
fig, [ax1, ax2] = plt.subplots(1, 2,layout = 'constrained') amp = sam.metadata['RePro-Info']['settings']['contrast'][0][0] * 100 #find amplitude in metadata
ax1.plot(tim,rate)
ax1.set_ylim(0,600) #figure for a quick plot
ax1.set_xlim(0, 0.04) fig = plt.figure(figsize = (5, 2.5))
freq, power = power_spectrum(rate, dt) ax = fig.add_subplot()
ax2.plot(freq,power) ax.plot(time[time < 0.1], potential[time < 0.1]) # plot the membrane potential in 0.1s
ax2.set_xlim(0,1000) ax.scatter(spike_times[spike_times < 0.1],
np.ones_like(spike_times[spike_times < 0.1]) * np.max(potential)) #plot teh spike times on top
# make an eventplot plt.show()
fig = plt.figure(figsize = (5, 3), layout = 'constrained') plt.close()
ax = fig.add_subplot() # get all the stimuli
ax.eventplot(spikes, linelength = 0.8) stims = sam.stimuli
ax.set_xlabel('time [ms]') # empty list for the spike times
ax.set_ylabel('loop no.') spikes = []
#spikes2 = np.array(range(len(stims)))
# loop over the stimuli
for stim in stims:
# get the spike times
spike, _ = stim.trace_data('Spikes-1')
# append the first 100ms to spikes
spikes.append(spike[spike < 0.1])
# get stimulus duration
duration = stim.duration
ti = stim.trace_info("V-1")
dt = ti.sampling_interval # get the stimulus interval
bin_spikes = binary_spikes(spike, duration, dt) #binarize the spike_times
print(len(bin_spikes))
pot,tim= stim.trace_data("V-1") #membrane potential
rate = firing_rate(bin_spikes, dt = dt)
print(np.mean(rate))
fig, [ax1, ax2] = plt.subplots(1, 2,layout = 'constrained')
ax1.plot(tim,rate)
ax1.set_ylim(0,600)
ax1.set_xlim(0, 0.04)
freq, power = power_spectrum(rate, dt)
ax2.plot(freq,power)
ax2.set_xlim(0,1000)
# make an eventplot
fig = plt.figure(figsize = (5, 3), layout = 'constrained')
ax = fig.add_subplot()
ax.eventplot(spikes, linelength = 0.8)
ax.set_xlabel('time [ms]')
ax.set_ylabel('loop no.')
plt.show() plt.show()