{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":106809,"databundleVersionId":13056355,"sourceType":"competition"}],"dockerImageVersionId":31154,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!ls /kaggle/input","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2025-10-29T17:30:41.543218Z","iopub.execute_input":"2025-10-29T17:30:41.543449Z","iopub.status.idle":"2025-10-29T17:30:41.669352Z","shell.execute_reply.started":"2025-10-29T17:30:41.543429Z","shell.execute_reply":"2025-10-29T17:30:41.668568Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **INPUT**","metadata":{}},{"cell_type":"code","source":"import h5py\nimport numpy as np\n\ndef load_h5py_file(file_path):\n    data = {\n        'neural_features': [],\n        'n_time_steps': [],\n        'seq_class_ids': [],\n        'seq_len': [],\n        'transcriptions': [],\n        'sentence_label': [],\n        'session': [],\n        'block_num': [],\n        'trial_num': [],\n    }\n    with h5py.File(file_path, 'r') as f:\n        keys = list(f.keys())\n        for key in keys:\n            g = f[key]\n            neural_features = g['input_features'][:]\n            n_time_steps = g.attrs.get('n_time_steps')\n            seq_class_ids = g.get('seq_class_ids')\n            seq_class_ids = seq_class_ids[:] if seq_class_ids is not None else None\n            seq_len = g.attrs.get('seq_len')\n            transcription = g.get('transcription')\n            transcription = transcription[:] if transcription is not None else None\n            sentence_label = g.attrs.get('sentence_label')\n            session = g.attrs.get('session')\n            block_num = g.attrs.get('block_num')\n            trial_num = g.attrs.get('trial_num')\n\n            data['neural_features'].append(neural_features)\n            data['n_time_steps'].append(n_time_steps)\n            data['seq_class_ids'].append(seq_class_ids)\n            data['seq_len'].append(seq_len)\n            data['transcriptions'].append(transcription)\n            data['sentence_label'].append(sentence_label)\n            data['session'].append(session)\n            data['block_num'].append(block_num)\n            data['trial_num'].append(trial_num)\n    return data\n\n\nif __name__ == \"__main__\":\n    file_path = \"/kaggle/input/brain-to-text-25/t15_copyTask_neuralData/hdf5_data_final/t15.2023.08.13/data_val.hdf5\"\n    \n    data = load_h5py_file(file_path)\n\n    print(\"\\nLoaded file summary:\")\n    print(f\"number of instances in a day: {len(data['neural_features'])}\")\n    print(f\"Neural features shape (first trial): {data['neural_features'][0].shape}\")\n    print(f\"First transcription: {data['transcriptions'][0]}\")\n    print(f\"Sentence label: {data['sentence_label'][0]}\")\n    print(f\"len of one transcription vector: {len(data['transcriptions'][0])}\")\n","metadata":{"trusted":true,"jupyter":{"outputs_hidden":true},"execution":{"iopub.status.busy":"2025-10-29T18:07:49.336745Z","iopub.execute_input":"2025-10-29T18:07:49.336995Z","iopub.status.idle":"2025-10-29T18:07:50.97892Z","shell.execute_reply.started":"2025-10-29T18:07:49.336975Z","shell.execute_reply":"2025-10-29T18:07:50.978195Z"},"collapsed":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!git clone https://github.com/Neuroprosthetics-Lab/nejm-brain-to-text.git","metadata":{"trusted":true,"jupyter":{"outputs_hidden":true},"execution":{"iopub.status.busy":"2025-10-29T17:30:47.0307Z","iopub.execute_input":"2025-10-29T17:30:47.031219Z","iopub.status.idle":"2025-10-29T17:30:51.219362Z","shell.execute_reply.started":"2025-10-29T17:30:47.031199Z","shell.execute_reply":"2025-10-29T17:30:51.218361Z"},"collapsed":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%cd /kaggle/working/nejm-brain-to-text\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:30:51.220597Z","iopub.execute_input":"2025-10-29T17:30:51.220935Z","iopub.status.idle":"2025-10-29T17:30:51.230994Z","shell.execute_reply.started":"2025-10-29T17:30:51.220904Z","shell.execute_reply":"2025-10-29T17:30:51.230304Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -q condacolab\nimport condacolab\ncondacolab.install()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:30:51.231912Z","iopub.execute_input":"2025-10-29T17:30:51.232167Z","iopub.status.idle":"2025-10-29T17:31:06.793413Z","shell.execute_reply.started":"2025-10-29T17:30:51.232143Z","shell.execute_reply":"2025-10-29T17:31:06.79278Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!conda create -n b2txt25 python=3.10 -y\n!conda run -n b2txt25 pip install numpy pandas torch torchvision torchaudio tqdm editdistance redis omegaconf h5py","metadata":{"trusted":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"execution":{"iopub.status.busy":"2025-10-29T17:31:06.794313Z","iopub.execute_input":"2025-10-29T17:31:06.794598Z","iopub.status.idle":"2025-10-29T17:34:48.930085Z","shell.execute_reply.started":"2025-10-29T17:31:06.794574Z","shell.execute_reply":"2025-10-29T17:34:48.929318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install \\\n    redis==5.2.1 \\\n    jupyter==1.1.1 \\\n    numpy==2.1.2 \\\n    pandas==2.3.0 \\\n    matplotlib==3.10.1 \\\n    scipy==1.15.2 \\\n    scikit-learn==1.6.1 \\\n    tqdm==4.67.1 \\\n    g2p_en==2.1.0 \\\n    h5py==3.13.0 \\\n    omegaconf==2.3.0 \\\n    editdistance==0.8.1 \\\n    huggingface-hub==0.33.1 \\\n    transformers==4.53.0 \\\n    tokenizers==0.21.2 \\\n    accelerate==1.8.1 \\\n    bitsandbytes==0.46.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:34:48.931322Z","iopub.execute_input":"2025-10-29T17:34:48.93159Z","iopub.status.idle":"2025-10-29T17:38:12.879527Z","shell.execute_reply.started":"2025-10-29T17:34:48.931565Z","shell.execute_reply":"2025-10-29T17:38:12.878733Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%cd /kaggle/working\n!wget http://download.redis.io/redis-stable.tar.gz -q\n!tar xvzf redis-stable.tar.gz > /dev/null\n%cd redis-stable\n!make > /dev/null\n!./src/redis-server --daemonize yes --port 6379\n!./src/redis-cli ping\n%cd /kaggle/working/nejm-brain-to-text","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:38:12.880694Z","iopub.execute_input":"2025-10-29T17:38:12.881268Z","iopub.status.idle":"2025-10-29T17:40:48.71333Z","shell.execute_reply.started":"2025-10-29T17:38:12.881243Z","shell.execute_reply":"2025-10-29T17:40:48.712479Z"},"jupyter":{"outputs_hidden":true},"collapsed":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Evaluation steps follow**","metadata":{}},{"cell_type":"code","source":"!./redis-stable/src/redis-cli ping\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:41:06.957469Z","iopub.execute_input":"2025-10-29T17:41:06.958091Z","iopub.status.idle":"2025-10-29T17:41:07.080284Z","shell.execute_reply.started":"2025-10-29T17:41:06.958058Z","shell.execute_reply":"2025-10-29T17:41:07.079277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%cd /kaggle/working/nejm-brain-to-text\n!python language_model/language-model-standalone.py \\\n  --lm_path language_model/pretrained_language_models/openwebtext_1gram_lm_sil \\\n  --do_opt \\\n  --nbest 100 \\\n  --acoustic_scale 0.325 \\\n  --blank_penalty 90 \\\n  --alpha 0.55 \\\n  --redis_ip localhost \\\n  --gpu_number 0\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:41:09.09705Z","iopub.execute_input":"2025-10-29T17:41:09.097651Z","iopub.status.idle":"2025-10-29T17:41:11.565103Z","shell.execute_reply.started":"2025-10-29T17:41:09.097622Z","shell.execute_reply":"2025-10-29T17:41:11.564368Z"},"jupyter":{"outputs_hidden":true},"collapsed":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls /kaggle/working/nejm-brain-to-text/language_model | grep lm_decoder\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:41:14.857774Z","iopub.execute_input":"2025-10-29T17:41:14.858066Z","iopub.status.idle":"2025-10-29T17:41:14.987626Z","shell.execute_reply.started":"2025-10-29T17:41:14.858041Z","shell.execute_reply":"2025-10-29T17:41:14.98652Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!cp /kaggle/input/brain-to-text-25/t15_pretrained_rnn_baseline/t15_pretrained_rnn_baseline/checkpoint/args.yaml \\\n    /kaggle/working/nejm-brain-to-text/args_eval.yaml\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:43:02.024631Z","iopub.execute_input":"2025-10-29T17:43:02.025416Z","iopub.status.idle":"2025-10-29T17:43:02.164733Z","shell.execute_reply.started":"2025-10-29T17:43:02.025375Z","shell.execute_reply":"2025-10-29T17:43:02.163627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cat /kaggle/working/nejm-brain-to-text/args_eval.yaml","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:43:05.357081Z","iopub.execute_input":"2025-10-29T17:43:05.357918Z","iopub.status.idle":"2025-10-29T17:43:05.478507Z","shell.execute_reply.started":"2025-10-29T17:43:05.35789Z","shell.execute_reply":"2025-10-29T17:43:05.477555Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import yaml\n\nyaml_path = \"/kaggle/working/nejm-brain-to-text/args_eval.yaml\"\n\nwith open(yaml_path) as f:\n    cfg = yaml.safe_load(f)\n\n# point to Kaggle data and checkpoint\ncfg[\"dataset\"][\"dataset_dir\"] = \"/kaggle/input/brain-to-text-25/t15_copyTask_neuralData/hdf5_data_final\"\ncfg[\"checkpoint_dir\"] = \"/kaggle/input/brain-to-text-25/t15_pretrained_rnn_baseline/t15_pretrained_rnn_baseline/checkpoint\"\ncfg[\"init_from_checkpoint\"] = True\ncfg[\"init_checkpoint_path\"] = \"/kaggle/input/brain-to-text-25/t15_pretrained_rnn_baseline/t15_pretrained_rnn_baseline/checkpoint/best_checkpoint\"\ncfg[\"mode\"] = \"eval\"  # ensure eval mode\n\nwith open(yaml_path, \"w\") as f:\n    yaml.safe_dump(cfg, f)\n\nprint(\"✅ args_eval.yaml updated successfully!\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:43:10.095783Z","iopub.execute_input":"2025-10-29T17:43:10.09638Z","iopub.status.idle":"2025-10-29T17:43:10.134674Z","shell.execute_reply.started":"2025-10-29T17:43:10.096331Z","shell.execute_reply":"2025-10-29T17:43:10.133963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# === Step 1: Go to working directory ===\n%cd /kaggle/working/nejm-brain-to-text","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:43:16.717165Z","iopub.execute_input":"2025-10-29T17:43:16.717505Z","iopub.status.idle":"2025-10-29T17:43:16.722483Z","shell.execute_reply.started":"2025-10-29T17:43:16.717476Z","shell.execute_reply":"2025-10-29T17:43:16.721805Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%cd /kaggle/working/nejm-brain-to-text/model_training\n!cp evaluate_model.py evaluate_model_backup.py\nfrom pathlib import Path\n\npath = Path(\"/kaggle/working/nejm-brain-to-text/model_training/evaluate_model.py\")\ncode = path.read_text()\nprint(code[:800])  # just to confirm we’re seeing the start of the file\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:50:24.817061Z","iopub.execute_input":"2025-10-29T17:50:24.81743Z","iopub.status.idle":"2025-10-29T17:50:24.942203Z","shell.execute_reply.started":"2025-10-29T17:50:24.817402Z","shell.execute_reply":"2025-10-29T17:50:24.941365Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fixed_code = code.replace(\n    'b2txt_csv_df = pd.read_csv(args.csv_path)',\n    'import os\\nif os.path.exists(args.csv_path):\\n    b2txt_csv_df = pd.read_csv(args.csv_path)\\nelse:\\n    b2txt_csv_df = pd.DataFrame(columns=[\"ground_truth\", \"prediction\"])'\n)\n\npath.write_text(fixed_code)\nprint(\"✅ evaluate_model.py fixed and saved!\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:50:38.084876Z","iopub.execute_input":"2025-10-29T17:50:38.085551Z","iopub.status.idle":"2025-10-29T17:50:38.091551Z","shell.execute_reply.started":"2025-10-29T17:50:38.085521Z","shell.execute_reply":"2025-10-29T17:50:38.090491Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!grep -n \"b2txt_csv_df\" /kaggle/working/nejm-brain-to-text/model_training/evaluate_model.py\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:50:53.456735Z","iopub.execute_input":"2025-10-29T17:50:53.45733Z","iopub.status.idle":"2025-10-29T17:50:53.577388Z","shell.execute_reply.started":"2025-10-29T17:50:53.457303Z","shell.execute_reply":"2025-10-29T17:50:53.576576Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python /kaggle/working/nejm-brain-to-text/model_training/evaluate_model.py \\\n    --model_path /kaggle/input/brain-to-text-25/t15_pretrained_rnn_baseline/t15_pretrained_rnn_baseline \\\n    --data_dir /kaggle/input/brain-to-text-25/t15_copyTask_neuralData/hdf5_data_final \\\n    --eval_type val \\\n    --gpu_number 0\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-29T17:54:25.294121Z","iopub.execute_input":"2025-10-29T17:54:25.29456Z","iopub.status.idle":"2025-10-29T18:07:49.335379Z","shell.execute_reply.started":"2025-10-29T17:54:25.294532Z","shell.execute_reply":"2025-10-29T18:07:49.334603Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}