Project: s41467-025-56162-9 | Function: Voxel_based_encoder_r2 (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Implements R²-score based neural encoding model evaluation function. Uses ridge regression to fit language vectors to neural data mapping, calculates R² score to evaluate model prediction performance. Takes vector representations, transcripts and neural data as input, outputs R² score results.

**KEY TERMS:** {
  "vectors": "word embedding representations",
  "neural_data": "fMRI BOLD signals",
  "R2": "coefficient of determination",
  "ridge": "ridge regression",
  "alpha": "regularization parameter"
}

**CONTEXT CODE:** 
import extract_embeddings
import ridge
from sklearn.decomposition import PCA
from scipy.stats import pearsonr

import numpy as np
'''import pickle
with open('schaefer_atlas_400.pkl', 'rb') as f:
    atlas = pickle.load(f)
'''

def cos_sim(a,b):
    from numpy.linalg import norm
    return np.dot(a,b)/(norm(a)*norm(b))


def Voxel_based_encoder(vectors,transcript,neural_data ,warmup=False, shuffle=False,bootstrap=True ,folds=5, alpha=0.0):
    # MASKED: Implements core neural encoding analysis function that uses language model vectors to predict fMRI neural signals. Employs cross-validation and ridge regression with bootstrap sampling support, calculating Pearson correlation coefficient to evaluate prediction performance.
    raise NotImplementedError("This function is masked for testing purposes.")


def Voxel_based_encoder_r2(vectors,transcript,neural_data, alpha=0.0):
    # MASKED: Implements R²-score based neural encoding model evaluation function. Uses ridge regression to fit language vectors to neural data mapping, calculates R² score to evaluate model prediction performance.
    raise NotImplementedError("This function is masked for testing purposes.")

def pca_embed(v,n):
    p=PCA(n)
    return p.fit_transform(v)


def data_from_img(img,mask):
    inds=[]
    signals=[]
    for i in range(mask.shape[0]):
        for j in range(mask.shape[1]):
            for k in range(mask.shape[2]):
                if mask[i,j,k]==1:
                    inds.append([i,j,k])
                    signals.append(img[i,j,k,:])

    neural_data=np.array(signals)
    return {"neural_data":neural_data,"coord":inds}

def data_to_img(data,coord, shape, affine=None):
    import nibabel
    img=np.zeros(shape=shape)
    for i in range(len(coord)):
        img[coord[i][0],coord[i][1],coord[i][2]]=data[i]
    img_=nibabel.Nifti1Image(img,affine=affine)

    return img_




if __name__ == "__main__":
    # Main execution code - only runs when script is executed directly
    print("Neural_Encoding module can be executed as main script, but main execution is disabled during testing.")


Complete the function implementation replacing "raise NotImplementedError".