Sovereign 8s.is mirror of standardgalactic/Handwriting_codec
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-12-03 23:52:00 +08:00
bits_per_spike-Fig4.py Add files via upload 2025-02-07 23:00:18 +08:00
dtw_recognition-Fig5.py Add files via upload 2025-02-07 23:00:18 +08:00
encoding model-Fig4d-S7.py Update and rename encoding model-Fig4d-S6.py to encoding model-Fig4d-S7.py 2025-12-03 23:52:00 +08:00
full model-Fig4F-S8.py Update and rename full model-Fig4F-S7.py to full model-Fig4F-S8.py 2025-12-03 23:51:39 +08:00
handwriting_decoding-Fig5.py Add files via upload 2025-12-03 23:47:17 +08:00
LICENSE Initial commit 2025-02-07 22:48:27 +08:00
README.md Update README.md 2025-12-03 23:45:17 +08:00
requirements.txt Add files via upload 2025-07-09 15:27:50 +08:00

Neural encoding and decoding models for handwriting

Overview

This repository contains the core codes for the paper Wang et al., 2025 "Cortical representation of multidimensional handwriting movement and implications for neuroprostheses".

Installation

Set up a new environment with Python==3.9 and using the provided requirements.txt as follows:

pip install -r requirements.txt

This will install the key Python packages to run this code

Usage

  • encoding model(fig4d,figS7).py: This module uses multidimensional handwriting data to predict single-neuron firing rates during imagined handwriting. We systematically test different combinations of multidimensional handwriting data as inputs:
dims = ['Vxy','Vxyz','Vxy_grip','Vxy_pres','Vxy_emg']

For both strokes and pen lifts, we fit a Linear-Nonlinear Poisson (LNP) model separately with 10-fold cross-validation strategy:

theta_lnp = fit_lnp(X_train, y_train, num_epochs, input_dim) # Fit LNP model
predict = predict_fr_lnp(X_test,input_dim, theta_lnp) # Test LNP model
  • full model-(fig4f,figS8).py: This module evaluates the uniqueness of each handwriting dimension in neural encoding by removing corresponding dimension from the full model. Firstly, train a full model using all dimensions of handwriting data:
theta_lnp = fit_lnp(X_train, y_train, num_epochs) # Fit the full model

Then, remove certain dimension of handwriting data as the inputs to encode single-neuron firing rates:

theta_lnp_delV = theta_lnp[:, 3:] # example:remove model weights for velocity
predict_delV = torch.exp(torch.sum(X_test_delV * theta_lnp_delV[0, :-1], dim=1) + theta_lnp_delV[0, -1]) # example:predict reults after removing handwriting velocity from the input
  • bits_per_spike.py: This module is used to assess the encoding performance in bits/spike as follows:
mi = metrics.mutual_info_score(y_true, predict_disc) # mutual information
bps_file[neuron,seg] = mi/len(predict_disc)/np.mean(y_true) # bits per spike
  • handwriting_decoding(fig5).py: This module decodes each subject's multidimensional handwriting data (velocity, grip force and pressure) from neural signals using LSTM network.
single_model = LSTM(input_dim=bined_spk_cv.shape[1], hidden_dim=32, num_layers=1, output_dim=5).to('cuda')
  • dtw_recognition(fig5).py: This module recognizes the decoded results as Chinese characters in the character library using the dynamic time warping (DTW) algorithm.
dist_vel2d,paths_vel2d = dtw_match(prediction_vel2d, [arr[[0, 1], :] for arr in interp_library]) # example: using decoded velocity of each character to recognize.
d, path = fastdtw(vel.T, database[i].T, dist=euclidean) # employ the fastdtw algorithm to compute the warping path between decoded results and all character templates in the character library.
match_target_vel2d = np.argmin(dist_vel2d)  # character with the minimum distance is the recognition result.