<jupyter_start><jupyter_text>IMPORT DATA<jupyter_code>ephys_data = loadmat("singleunit.mat")<jupyter_output><empty_output><jupyter_text>BASIC DESCRIPTION OF DATA<jupyter_code>type(ephys_data)
ephys_data.keys()
ephys_data.items()
dat = ephys_data['singleunit']
type(dat), dat.shape
# When we use dat[x][y], x is always 0 (not sure why, maybe 1st dimension?), and y is the neuron. Here, this is neuron 1:
type(dat[0][0]), dat[0][0].shape
print(dat[0][0])
dat[0][0]['NType']
dat[0][0]['recording'].shape<jupyter_output><empty_output><jupyter_text>VISUALIZING SAMPLES OF DATA<jupyter_code># 'recording' has the individual spiking patterns for each of the 33 neurons
# dat[0][0]['recording'] is for neuron 1, shows all data for neuron 1

plt.plot(dat[0][0]['recording'])
# the individual spiking pattern for neuron (zoomed in by going by 100s)

plt.plot(dat[0][0]['recording'][0:-1:100])
# the individual spiking pattern for neuron 1 (first 10,000 timepoints)

plt.plot(dat[0][0]['recording'][0:10000:10])
# 'TTLch' shows the optogenetic stimulation patterns for all 33 neurons
# dat[0][0]['TTLch'] is for neuron 1

plt.plot(dat[0][0]['TTLch'][0:-1:100])
dat[0][0]['TTLch'][17974]
# dat[0][0]['recording'] is for neuron 26

plt.plot(dat[0][25]['recording'][0:-1:100])
# dat[0][25]['TTLch'] is for neuron 26 - can see minimum is 3.5 units, so this will be our threshold

plt.plot(dat[0][25]['TTLch'][0:-1:100])<jupyter_output><empty_output><jupyter_text>DATA ANALYSIS: DETERMINING FIRING RATES<jupyter_code># GOALS OF THE PROJECT
  # - find the avg firing rates for each neuron during stimulation vs. no stim
  # - compare the effects of the optogenetic stimulation on firing rates for
  #   different types of neuron, either PN or DCN 
## create empty lists for the four variables of interest

# avg firing rates for Purkinje Neurons during optogenetic stimulation
PN_firing_stim = []
# avg firing rates for Purkinje Neurons without optogenetic stimulation
PN_firing_no_stim = [] 

# avg firing rates for Deep Cerebellar Nuclei neurons during optogenetic stimulation
DCN_firing_stim = [] 
# avg firing rates for Deep Cerebellar Nuclei neurons without optogenetic stimulation
DCN_firing_no_stim = [] 
# sorting neurons by type
# first 20 are purkinje neurons, last 13 are DCN neurons
for i in range(0, 33):
  print(i + 1, dat[0][i]['NType'])
# Using loops to find when neurons fire, if they are "significant" fires, their averages, etc.

# neurons 1 to 20 are Purkinje Neurons

for i in range(0, 20):

  # create empty lists for times when optogenetic stimulation is on & not on 
  # these lists get reset for each new neuron
  TTLch_stim = []
  TTLch_no_stim = []

  # create empty lists for electrical recording values during & without stimulation
  # these lists get reset for each new neuron
  recording_stim = []
  recording_no_stim = []
    
  # go through all 'TTLch' values for the neuron
  for j in range(len(dat[0][i]['TTLch'])):

    # for all times that the optogenetic stimulation is ON ('TTLch' val > 3.5),
    # store the time index and 'recording' value at that time 
    if dat[0][i]['TTLch'][j] > 3.5:
      TTLch_stim.append(j)
      recording_stim.append(dat[0][i]['recording'][j])

    # for all times that optogenetic stimulation is NOT ON ('TTLch' val < 3.5),
    # store the time index and 'recording' value at that time 
    else:
      TTLch_no_stim.append(j)
      recording_no_stim.append(dat[0][i]['recording'][j])

  # find the average & standard deviation for the stored 'recording' values
  avg_recording_stim = np.mean(recording_stim)
  std_recording_stim = np.std(recording_stim)
  avg_recording_no_stim = np.mean(recording_no_stim)
  std_recording_no_stim = np.std(recording_no_stim)

  # considered neuron firing if the 'recording' val is more than two 
  # standard deviations below average
  count_stim = 0
  count_no_stim = 0

  for k in range(len(recording_stim)): # find spike count for stim  
    if recording_stim[k] < (avg_recording_stim - (2 * std_recording_stim)):
      count_stim += 1
      
  for l in range(len(recording_no_stim)): # find spike count for no stim
    if recording_no_stim[l] < (avg_recording_no_stim - (2 * std_recording_no_stim)):
      count_no_stim += 1
    
  # calculate avg firing for stim & no stim and store in proper list
  avg_firing_stim = count_stim / len(recording_stim)
  PN_firing_stim.append(avg_firing_stim)
  avg_firing_no_stim = count_no_stim / len(recording_no_stim)
  PN_firing_no_stim.append(avg_firing_no_stim)

# Print the data from the previous chunk: should have 20 entries (20 neurons)
print(PN_firing_stim)
print(PN_firing_no_stim)
print(len(PN_firing_no_stim))
print(len(PN_firing_stim))
#len(recording_stim)
#avg_recording_stim
#count_stim
# Same thing for DCN neurons

# neurons 21 to 33 are DCN Neurons

for i in range(20, 33):
  # create empty lists for times when optogenetic stimulation is on & not on 
  # these lists get reset for each new neuron
  TTLch_stim = []
  TTLch_no_stim = []

  # create empty lists for electrical recording values during & without stimulation
  # these lists get reset for each new neuron
  recording_stim = []
  recording_no_stim = []
    
  # go through all 'TTLch' values for the neuron
  for j in range(len(dat[0][i]['TTLch'])):

    # for all times that the optogenetic stimulation is ON ('TTLch' val > 3.5),
    # store the time index and 'recording' value at that time 
    if dat[0][i]['TTLch'][j] > 3.5:
      TTLch_stim.append(j)
      recording_stim.append(dat[0][i]['recording'][j])

    # for all times that optogenetic stimulation is NOT ON ('TTLch' val < 3.5),
    # store the time index and 'recording' value at that time 
    else:
      TTLch_no_stim.append(j)
      recording_no_stim.append(dat[0][i]['recording'][j])

  # find the average & standard deviation for the stored 'recording' values
  avg_recording_stim = np.mean(recording_stim)
  std_recording_stim = np.std(recording_stim)
  avg_recording_no_stim = np.mean(recording_no_stim)
  std_recording_no_stim = np.std(recording_no_stim)

  # considered neuron firing if the 'recording' val is more than two 
  # standard deviations below average
  count_stim = 0
  count_no_stim = 0

  for k in range(len(recording_stim)): # find spike count for stim  
    if recording_stim[k] < (avg_recording_stim - (2 * std_recording_stim)):
      count_stim += 1
      
  for l in range(len(recording_no_stim)): # find spike count for no stim
    if recording_no_stim[l] < (avg_recording_no_stim - (2 * std_recording_no_stim)):
      count_no_stim += 1
    
  # calculate avg firing for stim & no stim and store in proper list
  avg_firing_stim = count_stim / len(recording_stim)
  DCN_firing_stim.append(avg_firing_stim)
  avg_firing_no_stim = count_no_stim / len(recording_no_stim)
  DCN_firing_no_stim.append(avg_firing_no_stim)

# Print the data from the previous chunk: should have 13 entries (13 neurons)
print(DCN_firing_stim)
print(DCN_firing_no_stim)
print(len(DCN_firing_no_stim))
print(len(DCN_firing_stim))<jupyter_output>[0.02891566265060241, 0.033891507069086536, 0.01990069672459358, 0.00850323122786659, 0.0192650089023146, 0.031921300943585626, 0.03358732705033086, 0.022385071279833413, 0.014264992747461612, 0.0020814153624464636, 0.008020854220974533, 0.0036075759094097606, 0.0014004061177741545]
[0.025952777064669818, 0.03605068742403341, 0.02685202608661747, 0.02065024534318203, 0.022433421983233018, 0.02809490270930315, 0.029613904736632612, 0.03015452048900676, 0.02334771049592687, 0.013983871999518343, 0.014693280782576507, 0.014149285763198311, 0.01758649945166611]
13
13<jupyter_text>DISPLAYING THE RESULTS<jupyter_code># independent t-test 
print(stats.ttest_ind(PN_firing_stim, PN_firing_no_stim)) 
print(stats.ttest_ind(DCN_firing_stim, DCN_firing_no_stim))

# group means
print('\nmean firing rate for all purkinje neurons with stim:   ', np.mean(PN_firing_stim))
print('mean firing rate for all purkinje neurons without stim:', np.mean(PN_firing_no_stim))
print('\nmean firing rate for all DCN neurons with stim:   ', np.mean(DCN_firing_stim))
print('mean firing rate for all DCN neurons without stim:', np.mean(DCN_firing_no_stim))
data_to_plot = [PN_firing_stim, PN_firing_no_stim, DCN_firing_stim, DCN_firing_no_stim]
# Create a figure 
fig = plt.figure(1, figsize = (14, 10))
ax = fig.add_subplot(111)

# Create the boxplot & set colors
bp = ax.boxplot(data_to_plot, patch_artist=True,
                boxprops=dict(facecolor = 'darkorange', color = 'k', lw = 1.5),
                capprops=dict(lw = 1.5), whiskerprops=dict(lw = 1.5),
                medianprops=dict(color = 'k', lw = 1.5))

# Add horizontal axis labels
plt.xticks([1, 2, 3, 4], ['Purkinje Neurons with Stimulation', 'Purkinje Neurons without Stimulation', 
                          'DCN Neurons with Stimulation', 'DCN Neurons without Stimulation'])
# Set y-axis limits
plt.ylim(0, 0.065)

# Add title and y-axis labels
ax.set_title('Neuronal Firing Rates with and without Optogenetic Stimulation', fontsize = 16)
ax.set_ylabel('Firing Rate', fontsize = 14)

# Add significance marks for PN stim vs. no stim
plt.plot([1, 1, 2, 2], [0.056, 0.06, 0.06, 0.056], lw = 1.5, c = 'k')
plt.text(1.5, .06, "***", ha = 'center', va = 'bottom', color = 'k', fontsize = 16)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10718897976754198, 'fraction_numerical': 0.07522600086095566, 'mean_word_length': 4.7756370416407705, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '477153', 'license': 'Apache-2.0', 'n_tokens_mistral': 3939, 'n_tokens_neox': 3301, 'n_words': 914, 'path': 'Final_Project_Group_6.ipynb', 'repo_name': 'mikislin/summer20-Intro-python', 'size': '9292'}