{"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":91249,"databundleVersionId":11294684,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Check Location of BFM in Slice Image \n\nhttps://www.kaggle.com/code/stpeteishii/blended-image-animation","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport glob\nimport pandas as pd\nimport random\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\n!mkdir train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:10.319816Z","iopub.execute_input":"2025-03-14T02:23:10.320214Z","iopub.status.idle":"2025-03-14T02:23:10.455347Z","shell.execute_reply.started":"2025-03-14T02:23:10.320181Z","shell.execute_reply":"2025-03-14T02:23:10.453711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/byu-locating-bacterial-flagellar-motors-2025/train_labels.csv')\nprint(df.columns.tolist())\ndf=df[df['Number of motors']!=0]\ndisplay(df)\nunique_names = df['tomo_id'].unique().tolist()\nos.makedirs(\"frames\", exist_ok=True)\nnames=random.sample(unique_names,1)\nprint(names)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:10.457222Z","iopub.execute_input":"2025-03-14T02:23:10.457584Z","iopub.status.idle":"2025-03-14T02:23:10.490325Z","shell.execute_reply.started":"2025-03-14T02:23:10.457552Z","shell.execute_reply":"2025-03-14T02:23:10.489307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for name in names:\n    print(name)\n    folder_path =\"/kaggle/input/byu-locating-bacterial-flagellar-motors-2025/train/\"+name+\"/*.jpg\"\n    image_files = sorted(glob.glob(folder_path))\n    images = [\n        Image.open(img).resize((Image.open(img).width // 2, Image.open(img).height // 2), Image.LANCZOS)\n        for img in image_files\n    ]\n    images2 = []\n    for i in range(0, len(images), 5):\n        blended = Image.blend(images[i], images[i+1], alpha=0.20)\n        for j in range(i+2, i+5):\n            blended = Image.blend(blended, images[j], alpha=0.20)\n        images2.append(blended)\n    images2[0].save('./train/'+name+\".gif\", save_all=True, append_images=images2[1:], duration=200, loop=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:10.492028Z","iopub.execute_input":"2025-03-14T02:23:10.492355Z","iopub.status.idle":"2025-03-14T02:23:19.585769Z","shell.execute_reply.started":"2025-03-14T02:23:10.492328Z","shell.execute_reply":"2025-03-14T02:23:19.584726Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"    gpaths=[]\n    for dirname, _, filenames in os.walk('./train'):\n        for filename in filenames:\n            gpaths+=[(os.path.join(dirname, filename))]\n    print(gpaths)","metadata":{"execution":{"iopub.status.busy":"2025-03-14T02:23:19.587117Z","iopub.execute_input":"2025-03-14T02:23:19.587494Z","iopub.status.idle":"2025-03-14T02:23:19.594330Z","shell.execute_reply.started":"2025-03-14T02:23:19.587425Z","shell.execute_reply":"2025-03-14T02:23:19.593064Z"}}},{"cell_type":"code","source":"df=df[df['tomo_id'] == name]\ndisplay(df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:19.595581Z","iopub.execute_input":"2025-03-14T02:23:19.595905Z","iopub.status.idle":"2025-03-14T02:23:19.618372Z","shell.execute_reply.started":"2025-03-14T02:23:19.595877Z","shell.execute_reply":"2025-03-14T02:23:19.616989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_2d_name(name):\n    fig, ax = plt.subplots(figsize=(6,6))  # 2D plot\n    dfi = df[df['tomo_id'] == name]\n    display(dfi)\n    #path = paths[z]\n    #image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n    h, w = dfi.iloc[0,6],dfi.iloc[0,7] \n    #ax.imshow(image, cmap='gray', extent=[0, w, 0, h], origin='upper')\n    ax.scatter(dfi['Motor axis 2'], dfi['Motor axis 1'], color='red', alpha=0.5)\n    ax.set_xlabel('X')\n    ax.set_ylabel('Y')\n    ax.set_xlim(0,w)\n    ax.set_ylim(h,0)\n    #filename = f\"frames/{name}_2d.png\"\n    #plt.savefig(filename)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:19.619595Z","iopub.execute_input":"2025-03-14T02:23:19.619989Z","iopub.status.idle":"2025-03-14T02:23:19.634629Z","shell.execute_reply.started":"2025-03-14T02:23:19.619948Z","shell.execute_reply":"2025-03-14T02:23:19.633498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_2d_name_i(name,i):\n    fig, ax = plt.subplots(figsize=(6,6))  # 2D plot\n    dfi = df[df['tomo_id'] == name]\n    display(dfi)\n    path =f\"/kaggle/input/byu-locating-bacterial-flagellar-motors-2025/train/{name}/slice_{i:04d}.jpg\"\n    print(path)\n    image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n    h, w = dfi.iloc[0,6],dfi.iloc[0,7] \n    ax.imshow(image, cmap='gray', extent=[0, w, 0, h], origin='upper')\n    ax.scatter(dfi['Motor axis 2'], h-dfi['Motor axis 1'], color='red', alpha=0.2)\n    ax.set_xlabel('X')\n    ax.set_ylabel('Y')\n    ax.set_xlim(0,w)\n    ax.set_ylim(0,h)\n    #filename = f\"frames/{name}_2d.png\"\n    #plt.savefig(filename)\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:19.635616Z","iopub.execute_input":"2025-03-14T02:23:19.636021Z","iopub.status.idle":"2025-03-14T02:23:19.653588Z","shell.execute_reply.started":"2025-03-14T02:23:19.635992Z","shell.execute_reply":"2025-03-14T02:23:19.652452Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#name=unique_names[32]\nname=names[0]\nprint(name)\njpaths=[]\nfor dirname, _, filenames in os.walk(f'/kaggle/input/byu-locating-bacterial-flagellar-motors-2025/train/{name}'):\n    for filename in filenames:\n        jpaths+=[(os.path.join(dirname, filename))]\njpaths.sort()\n#display(df)\n#zs=sorted(df['Motor axis 0'].astype(int).unique().tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:19.655837Z","iopub.execute_input":"2025-03-14T02:23:19.656177Z","iopub.status.idle":"2025-03-14T02:23:19.762933Z","shell.execute_reply.started":"2025-03-14T02:23:19.656123Z","shell.execute_reply":"2025-03-14T02:23:19.761826Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from IPython.display import Image\ni=int(df.iloc[0,2]) #Motor axis 0\npath=jpaths[i]\nprint(path)\nImage(open(path,'rb').read())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:19.764234Z","iopub.execute_input":"2025-03-14T02:23:19.764526Z","iopub.status.idle":"2025-03-14T02:23:19.779805Z","shell.execute_reply.started":"2025-03-14T02:23:19.764499Z","shell.execute_reply":"2025-03-14T02:23:19.778538Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_2d_name(name)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:19.781177Z","iopub.execute_input":"2025-03-14T02:23:19.781698Z","iopub.status.idle":"2025-03-14T02:23:19.945506Z","shell.execute_reply.started":"2025-03-14T02:23:19.781662Z","shell.execute_reply":"2025-03-14T02:23:19.944544Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(name,i)\nshow_2d_name_i(name,i)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:19.946623Z","iopub.execute_input":"2025-03-14T02:23:19.946956Z","iopub.status.idle":"2025-03-14T02:23:20.306829Z","shell.execute_reply.started":"2025-03-14T02:23:19.946924Z","shell.execute_reply":"2025-03-14T02:23:20.305624Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from IPython.display import Image\npath=f'./train/{name}.gif'\nImage(open(path,'rb').read())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:20.308000Z","iopub.execute_input":"2025-03-14T02:23:20.308377Z","iopub.status.idle":"2025-03-14T02:23:20.747792Z","shell.execute_reply.started":"2025-03-14T02:23:20.308342Z","shell.execute_reply":"2025-03-14T02:23:20.745851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#!rm -rf frames","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-14T02:23:20.749825Z","iopub.execute_input":"2025-03-14T02:23:20.750456Z","iopub.status.idle":"2025-03-14T02:23:20.755973Z","shell.execute_reply.started":"2025-03-14T02:23:20.750377Z","shell.execute_reply":"2025-03-14T02:23:20.753896Z"}},"outputs":[],"execution_count":null}]}