{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 数据聚合:「输入药物 SMILES,输出基因表达 + 细胞形态」的多模态虚拟细胞模型,配对不齐,训练集不均衡。\n", "\n", "## 策略有3种:\n", "\n", "### 1 是按 SMILES 聚合,对每个 SMILES 在每个模态上 取平均或中位数(或更复杂的 embedding 聚合)。\n", "\n", "### 2 是多实例学习(Multiple Instance Learning, MIL):思路:把一个 SMILES 的所有 replicate 当作一个 bag,模型学习 bag-level 对齐。\n", "\n", "### 3 是不对齐 replicate,随机采样:思路:训练时,每个 batch 从两个模态中随机采样该 SMILES 的一个 replicate,逐步逼近两个分布。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import os\n", "import h5py\n", "\n", "def load_from_HDF(fname):\n", " \"\"\"Load data from a HDF5 file to a dictionary.\"\"\"\n", " data = dict()\n", " with h5py.File(fname, 'r') as f:\n", " for key in f:\n", " data[key] = np.asarray(f[key])\n", " if isinstance(data[key][0], np.bytes_):\n", " data[key] = data[key].astype(str)\n", " return data\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "CP_path = './Processed_Paired_CP_KeepRep.h5'\n", "GE_path = './Processed_Paired_GE_KeepRep.h5'\n", "\n", "CP_CSV = load_from_HDF(CP_path)\n", "GE_CSV = load_from_HDF(GE_path)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "((15012, 602), (3451, 977))" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "CP_CSV['target'].shape, GE_CSV['target'].shape" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'canonical_smiles': array(['CCCOc1cc(N)ccc1C(=O)OCCN(CC)CC',\n", " 'C=C(C)[C@@H]1CCC(C)=C[C@H]1c1c(OC)cc(C)cc1OC',\n", " 'COc1cc(O)cc(/C=C/c2ccccc2)c1', ...,\n", " 'CC(C)[C@H]1c2ccc(F)cc2CC[C@@]1(CCN(C)CCCc1nc2ccccc2[nH]1)OC(=O)C1CC1',\n", " 'CCCCC/C=C\\\\C/C=C\\\\C/C=C\\\\CCCCCCC(=O)NCCO',\n", " 'COc1cc(OC)c(C(=O)c2ccc(O)cc2)c(OC)c1'], dtype=' 0 or inf_count > 0:\n", " print(f\"警告: {name} 包含 {nan_count} NaN 和 {inf_count} Inf\")\n", " data_array = np.nan_to_num(data_array, nan=0.0, posinf=1.0, neginf=-1.0)\n", " \n", " # 数据标准化\n", " mean = data_array.mean()\n", " std = data_array.std()\n", " \n", " print(f\"{name}: 均值={mean:.4f}, 标准差={std:.4f}, 形状={data_array.shape}\")\n", " \n", " return data_array\n", " \n", " final_dataset['control_CP'] = clean_and_validate_data(dataset_dict['control_CP'], 'control_CP')\n", " final_dataset['target_CP'] = clean_and_validate_data(dataset_dict['target_CP'], 'target_CP')\n", " final_dataset['control_GE'] = clean_and_validate_data(dataset_dict['control_GE'], 'control_GE')\n", " final_dataset['target_GE'] = clean_and_validate_data(dataset_dict['target_GE'], 'target_GE')\n", " \n", " # 保存为HDF5\n", " with h5py.File(out_path, 'w') as f:\n", " for key, value in final_dataset.items():\n", " f.create_dataset(key, data=value)\n", " \n", " print(f\"✅ 稳健配对数据集已保存到: {out_path}\")\n", " return final_dataset\n", "\n", "\n", "out_path = './Paired_CP_GE_Dataset.h5'\n", "paired_dataset = create_robust_paired_dataset(CP_CSV, GE_CSV, out_path)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_path = './Paired_CP_GE_Dataset.h5'\n", "\n", "agg_data = load_from_HDF(out_path)\n", "agg_data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "boom", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.10" } }, "nbformat": 4, "nbformat_minor": 2 }