<jupyter_start><jupyter_text>Encoding Neural Activities Contents- [Properties of Neurons](Properties-of-Neurons)- [Spike Trains and Firing Rates](Spike-Trains-and-Firing-Rates)- [Measuring Firing Rate](Measuring-Firing-Rate)- [Tuning Curve](Tuning-Curve)  Properties of Neurons- axons, soma and dendrites- ion channels    - sodium (Na+), potassium (K+), calcium (Ca2+), and chloride (Cl−)- membrane potential    - hyperpolarization    - depolarization- action potential- refractory period- synapse    - an influx of Ca2+ that leads to the release of a neurotransmitter  Spike Trains and Firing Rates neural response functionsee more on [Dirac Function](Dirac-Function).$$\rho (t) = \sum_{i=1}^n \delta (t - t_i),\ 0 \le t_i \le T$$ spike-count rate, rcan be determined from a single trial.$$r = \frac{n}{T} = \frac{1}{T} \int_0^T \rho (\tau) d\tau$$ firing rate, r(t)average from multiple trials (or infinite tirals).$$ r (t) = \frac{1}{\Delta t} \int_t^{t+\Delta t} \langle \rho (\tau) \rangle d\rho$$ spiking probability$$ r (t) \cdot \Delta t$$ average firing rate$$ \langle r \rangle = \frac{\langle n \rangle}{T} = \frac{1}{T} \int_0^T \langle \rho (\tau) \rangle d\tau = \frac{1}{T} \int _0^T r(\tau) d\tau $$<jupyter_code># a spike train of a neuron in SC.
plt.figure(figsize=(18,5))
plt.vlines(spike_train, 0, 1)
plt.xlim((0,50))
plt.ylabel("Spike", fontsize=24)
plt.show()
Y_, X_ = np.histogram(spike_train, range=(0,360), bins=720) # 500ms

X = []
Y = []

for idx in range(np.size(X_)-1):
    X.append(X_[idx])
    X.append(X_[idx+1])
    Y.append(Y_[idx])
    Y.append(Y_[idx])
    
plt.figure(figsize=(18,5))
plt.plot(X, Y)
plt.xlim((0,50))
plt.ylabel("Discrete Fring Rate by Binning Time\n(500ms)", fontsize=16)
plt.show()<jupyter_output><empty_output><jupyter_text>Measuring Firing Rate Linear Filter$$ r_{\mathrm{approx}} (t) = \sum_{i=1}^N w(t-t_i) = \int w(\tau) \rho(t-\tau) d\tau $$w(t) as the kernel function Discrete Kernel$$ w(\tau) = \begin{cases}    \frac{1}{\Delta t} &,\ -\frac{\Delta t}{2} \leq t \leq \frac{\Delta t}{2} \\    0 &,\ otherwise\end{cases}$$ Guassian Kernel$$ w(\tau) = \frac{1}{\sqrt{2 \pi} \sigma_w} \exp(- \frac{\tau^2}{2 \sigma_w^2}) $$ Causal Kernel$$ w(\tau) = [\alpha^2 \tau \exp(- \alpha \tau)]_+ $$<jupyter_code>a = np.array([1,2,3,4,5,6,7,8,9])
a[a>4] = 0
a
def rectangular_kernel(delta):       
    return lambda t: np.ones_like(t[(t>=-delta/2)&(t<=delta/2)]) / delta

def gaussian_kernel(sigma):
    return lambda t: 1/(np.sqrt(2*np.pi)*sigma) * np.exp(-t**2/(2*sigma**2))

def causal_kernel(alpha):
    def causal(t):
        v = alpha**2 * t * np.exp(-alpha*t)
        v[v<0] = 0
        return v
    return causal

def get_linear_filter(train, kernel):
    return lambda t: np.sum(kernel(train - t))


X = np.linspace(0,50,1000)

Y = list(map(get_linear_filter(spike_train, causal_kernel(0.5)), X))
plt.figure(figsize=(18,5))
plt.plot(X, Y)
plt.xlim((0,50))
plt.ylabel("Rate with Square Window", fontsize=14)
plt.show()
X = np.linspace(0,50,1000)

Y = list(map(get_linear_filter(spike_train, rectangular_kernel(0.5)), X))
plt.figure(figsize=(18,5))
plt.plot(X, Y)
plt.xlim((0,50))
plt.ylabel("Rate with Square Window", fontsize=14)
plt.show()

Y = list(map(get_linear_filter(spike_train, gaussian_kernel(0.5)), X))
plt.figure(figsize=(18,5))
plt.plot(X, Y)
plt.xlim((0,50))
plt.ylabel("Rate with Gaussian Window", fontsize=14)
plt.show()

Y = list(map(get_linear_filter(spike_train, causal_kernel(2)), X))
plt.figure(figsize=(18,5))
plt.plot(X, Y)
plt.xlim((0,50))
plt.ylabel("Rate with Causal Window", fontsize=14)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1954595791805094, 'fraction_numerical': 0.03571428571428571, 'mean_word_length': 4.549923195084485, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '130065', 'license': 'MIT', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1425, 'n_words': 336, 'path': 'demo/1 Encoding Neural Activities.ipynb', 'repo_name': 'ZaneMuir/NeuroAnalysis', 'size': '3612'}