{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":51294,"databundleVersionId":6923401,"sourceType":"competition"},{"sourceId":153209481,"sourceType":"kernelVersion"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"https://www.kaggle.com/code/alexandervc/ribonanza-baselines-medians-onehot-ridge-lb0-229/input","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"https://www.kaggle.com/code/jordanyoung993/sequence-images-cnn/notebook","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.simplefilter('ignore')\n\nimport pandas as pd\npd.set_option('display.max_columns', 30)","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:12:46.534863Z","iopub.execute_input":"2023-12-03T11:12:46.535267Z","iopub.status.idle":"2023-12-03T11:12:46.986119Z","shell.execute_reply.started":"2023-12-03T11:12:46.535226Z","shell.execute_reply":"2023-12-03T11:12:46.984983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nlistdirs = os.listdir('./')\nlistdirs","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:12:46.98833Z","iopub.execute_input":"2023-12-03T11:12:46.988899Z","iopub.status.idle":"2023-12-03T11:12:46.997493Z","shell.execute_reply.started":"2023-12-03T11:12:46.988863Z","shell.execute_reply":"2023-12-03T11:12:46.996503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_del = pd.read_csv('/kaggle/input/stanford-ribonanza-rna-folding/train_data.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:12:46.999002Z","iopub.execute_input":"2023-12-03T11:12:46.999451Z","iopub.status.idle":"2023-12-03T11:14:41.313923Z","shell.execute_reply.started":"2023-12-03T11:12:46.99942Z","shell.execute_reply":"2023-12-03T11:14:41.312833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\n\nif not '2A3_MaP.csv' in listdirs or not 'DMS_MaP.csv' in listdirs:\n    \n    experiment_types_del = ['DMS_MaP', '2A3_MaP']\n    \n    for experiment_type in experiment_types_del:\n        \n        exptype_train_del = train_data_del[train_data_del['experiment_type'] == experiment_type]\n        exptype_train_del.to_csv(f\"./{experiment_type}.csv\", index=False)\n        \n        del exptype_train_del\n        \n    del train_data_del\n    del experiment_types_del\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:14:41.317091Z","iopub.execute_input":"2023-12-03T11:14:41.317607Z","iopub.status.idle":"2023-12-03T11:23:52.366733Z","shell.execute_reply.started":"2023-12-03T11:14:41.31756Z","shell.execute_reply":"2023-12-03T11:23:52.362579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEMP_DIR = '/kaggle/temp'\nos.path.isdir(TEMP_DIR)","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.370764Z","iopub.execute_input":"2023-12-03T11:23:52.371179Z","iopub.status.idle":"2023-12-03T11:23:52.382178Z","shell.execute_reply.started":"2023-12-03T11:23:52.371144Z","shell.execute_reply":"2023-12-03T11:23:52.38072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle')","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.384564Z","iopub.execute_input":"2023-12-03T11:23:52.384962Z","iopub.status.idle":"2023-12-03T11:23:52.395729Z","shell.execute_reply.started":"2023-12-03T11:23:52.384929Z","shell.execute_reply":"2023-12-03T11:23:52.39452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"experiment_types = ['DMS_MaP', '2A3_MaP', 'test']\n\nfor experiment_type in experiment_types:\n    \n    sub_dir = os.path.join(TEMP_DIR, experiment_type)\n        \nos.listdir(TEMP_DIR)","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.39704Z","iopub.execute_input":"2023-12-03T11:23:52.397402Z","iopub.status.idle":"2023-12-03T11:23:52.848568Z","shell.execute_reply.started":"2023-12-03T11:23:52.39737Z","shell.execute_reply":"2023-12-03T11:23:52.845544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"experiment_type = '2A3_MaP'\n# pd.read_csv(f\"./{experiment_type}.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.849668Z","iopub.status.idle":"2023-12-03T11:23:52.850117Z","shell.execute_reply.started":"2023-12-03T11:23:52.849904Z","shell.execute_reply":"2023-12-03T11:23:52.849926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_2A3_MaP_del = pd.read_csv(f\"./{experiment_type}.csv\")\nexp_2A3_MaP = exp_2A3_MaP_del.drop(columns=exp_2A3_MaP_del.filter(like='error').columns, axis=1)\n# exp_2A3_MaP","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.852463Z","iopub.status.idle":"2023-12-03T11:23:52.852895Z","shell.execute_reply.started":"2023-12-03T11:23:52.852695Z","shell.execute_reply":"2023-12-03T11:23:52.852714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del exp_2A3_MaP_del\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.85419Z","iopub.status.idle":"2023-12-03T11:23:52.854878Z","shell.execute_reply.started":"2023-12-03T11:23:52.854659Z","shell.execute_reply":"2023-12-03T11:23:52.854687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_2A3_MaP = exp_2A3_MaP.drop(['reads', 'SN_filter', 'signal_to_noise'], axis=1)\nreactivity = exp_2A3_MaP.filter(like='reactivity').columns\nexp_2A3_MaP[reactivity] = exp_2A3_MaP[reactivity]","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.857228Z","iopub.status.idle":"2023-12-03T11:23:52.858445Z","shell.execute_reply.started":"2023-12-03T11:23:52.858208Z","shell.execute_reply":"2023-12-03T11:23:52.858231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# exp_2A3_MaP","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.860428Z","iopub.status.idle":"2023-12-03T11:23:52.861287Z","shell.execute_reply.started":"2023-12-03T11:23:52.861015Z","shell.execute_reply":"2023-12-03T11:23:52.861048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_2A3_MaP['reactivity'] = exp_2A3_MaP[reactivity] \\\n    .apply(lambda row: row.fillna(row.mean()), axis=1).fillna(0.0).clip(lower=0.0, upper=1.0).values.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.862828Z","iopub.status.idle":"2023-12-03T11:23:52.863697Z","shell.execute_reply.started":"2023-12-03T11:23:52.863436Z","shell.execute_reply":"2023-12-03T11:23:52.863462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_2A3_MaP = exp_2A3_MaP.drop(columns=exp_2A3_MaP.filter(like='reactivity').columns, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.866133Z","iopub.status.idle":"2023-12-03T11:23:52.866555Z","shell.execute_reply.started":"2023-12-03T11:23:52.866354Z","shell.execute_reply":"2023-12-03T11:23:52.866375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_2A3_MaP = exp_2A3_MaP.drop(columns='dataset_name')","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.868233Z","iopub.status.idle":"2023-12-03T11:23:52.869262Z","shell.execute_reply.started":"2023-12-03T11:23:52.868976Z","shell.execute_reply":"2023-12-03T11:23:52.869004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generalize_png_path(seq_id, experiment_type):\n    \n    return os.path.join(TEMP_DIR, experiment_type, f'{seq_id}.png')","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.870535Z","iopub.status.idle":"2023-12-03T11:23:52.871284Z","shell.execute_reply.started":"2023-12-03T11:23:52.870995Z","shell.execute_reply":"2023-12-03T11:23:52.871023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_2A3_MaP['filename'] = exp_2A3_MaP['sequence_id'].apply(generalize_png_path, args=(experiment_type,))","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.873213Z","iopub.status.idle":"2023-12-03T11:23:52.873774Z","shell.execute_reply.started":"2023-12-03T11:23:52.873476Z","shell.execute_reply":"2023-12-03T11:23:52.873503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_2A3_MaP","metadata":{"execution":{"iopub.status.busy":"2023-12-03T11:23:52.875322Z","iopub.status.idle":"2023-12-03T11:23:52.875862Z","shell.execute_reply.started":"2023-12-03T11:23:52.875571Z","shell.execute_reply":"2023-12-03T11:23:52.875596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef generate_images(experiment_data):\n    print(\"Generating Sequence Images\")\n    sequences = experiment_data[[\"sequence\",\"filename\"]]\n    sequences = sequences.values.tolist()\n    \n    for sequence, path in sequences:\n        sequence = pad_sequence(sequence)\n        encoded = encode_sequence(sequence)\n        encoded = np.array(encoded).astype(np.uint8)\n        im = Image.fromarray(encoded)\n        im.save(path)\n        \n        del sequence","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pad_sequence(sequence, max_length=457, padding_value=\"Z\"):\n    # Calculate how many padding values are needed\n    padding_length = max_length - len(sequence)\n    \n    # Create the padded sequence\n    padded_sequence = list(sequence) + [padding_value] * padding_length\n    \n    return padded_sequence\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"exp_2A3_MaP[['sequence', 'filename']]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for sequence, path in exp_2A3_MaP[['sequence', 'filename']].values.tolist():\n    \n    ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if len(os.listdir(TEMP_PATH, experiment_type)) < exp_2A3_MaP.shape[0]:\n    \n    ","metadata":{},"execution_count":null,"outputs":[]}]}