{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Add Blg_Attributes\n", "\n", "第一次处理的时候没有按之前的储存方法读 blg 的细节,这里改一下 h5 里面的 dataset。\n", "\n", "最开始存的是 metadata,现在要加一个名为 \"Blg_Attributes\" 的 dataset。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "文件: blg_F10_30m_IM6_st0.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([30.]), 'stories': tensor([10]), 'struct_type': tensor([0])}\n", "文件: blg_F10_30m_IM6_st1.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([30.]), 'stories': tensor([10]), 'struct_type': tensor([1])}\n", "文件: blg_F10_30m_IM7_st0.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([30.]), 'stories': tensor([10]), 'struct_type': tensor([0])}\n", "文件: blg_F10_30m_IM7_st1.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([30.]), 'stories': tensor([10]), 'struct_type': tensor([1])}\n", "文件: blg_F10_30m_IM8_st0.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([30.]), 'stories': tensor([10]), 'struct_type': tensor([0])}\n", "文件: blg_F10_30m_IM8_st1.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([30.]), 'stories': tensor([10]), 'struct_type': tensor([1])}\n", "文件: blg_F11_33m_IM6_st1.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([33.]), 'stories': tensor([11]), 'struct_type': tensor([1])}\n", "文件: blg_F11_33m_IM7_st1.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([33.]), 'stories': tensor([11]), 'struct_type': tensor([1])}\n", "文件: blg_F11_33m_IM8_st1.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([33.]), 'stories': tensor([11]), 'struct_type': tensor([1])}\n", "文件: blg_F12_36m_IM6_st1.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([36.]), 'stories': tensor([12]), 'struct_type': tensor([1])}\n", "文件: blg_F12_36m_IM7_st1.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([36.]), 'stories': tensor([12]), 'struct_type': tensor([1])}\n", "文件: blg_F12_36m_IM8_st1.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([36.]), 'stories': tensor([12]), 'struct_type': tensor([1])}\n", "文件: blg_F13_39m_IM6_st1.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([39.]), 'stories': tensor([13]), 'struct_type': tensor([1])}\n", "文件: blg_F13_39m_IM7_st1.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([39.]), 'stories': tensor([13]), 'struct_type': tensor([1])}\n", "文件: blg_F13_39m_IM8_st1.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([39.]), 'stories': tensor([13]), 'struct_type': tensor([1])}\n", "文件: blg_F14_42m_IM6_st1.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([42.]), 'stories': tensor([14]), 'struct_type': tensor([1])}\n", "文件: blg_F14_42m_IM7_st1.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([42.]), 'stories': tensor([14]), 'struct_type': tensor([1])}\n", "文件: blg_F14_42m_IM8_st1.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([42.]), 'stories': tensor([14]), 'struct_type': tensor([1])}\n", "文件: blg_F15_45m_IM6_st1.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([45.]), 'stories': tensor([15]), 'struct_type': tensor([1])}\n", "文件: blg_F15_45m_IM7_st1.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([45.]), 'stories': tensor([15]), 'struct_type': tensor([1])}\n", "文件: blg_F15_45m_IM8_st1.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([45.]), 'stories': tensor([15]), 'struct_type': tensor([1])}\n", "文件: blg_F16_48m_IM6_st1.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([48.]), 'stories': tensor([16]), 'struct_type': tensor([1])}\n", "文件: blg_F16_48m_IM7_st1.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([48.]), 'stories': tensor([16]), 'struct_type': tensor([1])}\n", "文件: blg_F16_48m_IM8_st1.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([48.]), 'stories': tensor([16]), 'struct_type': tensor([1])}\n", "文件: blg_F17_51m_IM6_st1.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([51.]), 'stories': tensor([17]), 'struct_type': tensor([1])}\n", "文件: blg_F17_51m_IM7_st1.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([51.]), 'stories': tensor([17]), 'struct_type': tensor([1])}\n", "文件: blg_F17_51m_IM8_st1.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([51.]), 'stories': tensor([17]), 'struct_type': tensor([1])}\n", "文件: blg_F1_3m_IM6_st0.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([3.]), 'stories': tensor([1]), 'struct_type': tensor([0])}\n", "文件: blg_F1_3m_IM7_st0.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([3.]), 'stories': tensor([1]), 'struct_type': tensor([0])}\n", "文件: blg_F1_3m_IM8_st0.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([3.]), 'stories': tensor([1]), 'struct_type': tensor([0])}\n", "文件: blg_F2_6m_IM6_st0.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([6.]), 'stories': tensor([2]), 'struct_type': tensor([0])}\n", "文件: blg_F2_6m_IM7_st0.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([6.]), 'stories': tensor([2]), 'struct_type': tensor([0])}\n", "文件: blg_F2_6m_IM8_st0.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([6.]), 'stories': tensor([2]), 'struct_type': tensor([0])}\n", "文件: blg_F3_9m_IM6_st0.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([9.]), 'stories': tensor([3]), 'struct_type': tensor([0])}\n", "文件: blg_F3_9m_IM7_st0.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([9.]), 'stories': tensor([3]), 'struct_type': tensor([0])}\n", "文件: blg_F3_9m_IM8_st0.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([9.]), 'stories': tensor([3]), 'struct_type': tensor([0])}\n", "文件: blg_F4_12m_IM6_st0.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([12.]), 'stories': tensor([4]), 'struct_type': tensor([0])}\n", "文件: blg_F4_12m_IM7_st0.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([12.]), 'stories': tensor([4]), 'struct_type': tensor([0])}\n", "文件: blg_F4_12m_IM8_st0.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([12.]), 'stories': tensor([4]), 'struct_type': tensor([0])}\n", "文件: blg_F5_15m_IM6_st0.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([15.]), 'stories': tensor([5]), 'struct_type': tensor([0])}\n", "文件: blg_F5_15m_IM7_st0.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([15.]), 'stories': tensor([5]), 'struct_type': tensor([0])}\n", "文件: blg_F5_15m_IM8_st0.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([15.]), 'stories': tensor([5]), 'struct_type': tensor([0])}\n", "文件: blg_F6_18m_IM6_st0.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([18.]), 'stories': tensor([6]), 'struct_type': tensor([0])}\n", "文件: blg_F6_18m_IM7_st0.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([18.]), 'stories': tensor([6]), 'struct_type': tensor([0])}\n", "文件: blg_F6_18m_IM8_st0.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([18.]), 'stories': tensor([6]), 'struct_type': tensor([0])}\n", "文件: blg_F7_21m_IM6_st0.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([21.]), 'stories': tensor([7]), 'struct_type': tensor([0])}\n", "文件: blg_F7_21m_IM7_st0.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([21.]), 'stories': tensor([7]), 'struct_type': tensor([0])}\n", "文件: blg_F7_21m_IM8_st0.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([21.]), 'stories': tensor([7]), 'struct_type': tensor([0])}\n", "文件: blg_F8_24m_IM6_st0.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([24.]), 'stories': tensor([8]), 'struct_type': tensor([0])}\n", "文件: blg_F8_24m_IM6_st1.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([24.]), 'stories': tensor([8]), 'struct_type': tensor([1])}\n", "文件: blg_F8_24m_IM7_st0.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([24.]), 'stories': tensor([8]), 'struct_type': tensor([0])}\n", "文件: blg_F8_24m_IM7_st1.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([24.]), 'stories': tensor([8]), 'struct_type': tensor([1])}\n", "文件: blg_F8_24m_IM8_st0.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([24.]), 'stories': tensor([8]), 'struct_type': tensor([0])}\n", "文件: blg_F8_24m_IM8_st1.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([24.]), 'stories': tensor([8]), 'struct_type': tensor([1])}\n", "文件: blg_F9_27m_IM6_st0.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([27.]), 'stories': tensor([9]), 'struct_type': tensor([0])}\n", "文件: blg_F9_27m_IM6_st1.h5, Blg_Attributes: {'IM': tensor([0]), 'height': tensor([27.]), 'stories': tensor([9]), 'struct_type': tensor([1])}\n", "文件: blg_F9_27m_IM7_st0.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([27.]), 'stories': tensor([9]), 'struct_type': tensor([0])}\n", "文件: blg_F9_27m_IM7_st1.h5, Blg_Attributes: {'IM': tensor([1]), 'height': tensor([27.]), 'stories': tensor([9]), 'struct_type': tensor([1])}\n", "文件: blg_F9_27m_IM8_st0.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([27.]), 'stories': tensor([9]), 'struct_type': tensor([0])}\n", "文件: blg_F9_27m_IM8_st1.h5, Blg_Attributes: {'IM': tensor([2]), 'height': tensor([27.]), 'stories': tensor([9]), 'struct_type': tensor([1])}\n" ] } ], "source": [ "import os\n", "import h5py\n", "import numpy as np\n", "import torch\n", "\n", "def create_blg_attributes(directory):\n", " for filename in os.listdir(directory):\n", " if filename.endswith('.h5'):\n", " filepath = os.path.join(directory, filename)\n", " try:\n", " with h5py.File(filepath, 'a') as h5file: # 使用 'a' 模式以便写入\n", " # 检查文件中是否存在 'metadata' 数据集\n", " if 'Metadata' in h5file:\n", " metadata = h5file['Metadata'][:]\n", "\n", " # 将 IM 的值从 6, 7, 8 映射到 0, 1, 2\n", " im_mapping = {6: 0, 7: 1, 8: 2}\n", " im_value = im_mapping.get(metadata[2], -1) # 如果未找到映射则返回 -1\n", "\n", " # 创建 Blg_Attributes 数据集\n", " blg_attributes = {\n", " 'stories': np.array([metadata[0]], dtype=np.int64),\n", " 'height': np.array([metadata[1]], dtype=np.float32),\n", " 'IM': np.array([im_value], dtype=np.int64), # 将 IM 改为 int64 类型\n", " 'struct_type': np.array([metadata[3]], dtype=np.int64)\n", " }\n", "\n", " # 检查 Blg_Attributes 组是否存在,如果存在则删除\n", " if 'Blg_Attributes' in h5file:\n", " del h5file['Blg_Attributes']\n", "\n", " # 创建 Blg_Attributes 数据集组\n", " blg_group = h5file.create_group('Blg_Attributes')\n", "\n", " # 将字典中的数据集写入 Blg_Attributes 组\n", " for key, value in blg_attributes.items():\n", " blg_group.create_dataset(key, data=value)\n", " else:\n", " print(f\"文件 {filename} 中没有 'Metadata' 数据集\")\n", " except Exception as e:\n", " print(f\"读取文件 {filename} 时出错: {e}\")\n", "\n", "# 指定包含 HDF5 文件的目录路径\n", "directory_path = os.getcwd()\n", "\n", "# 创建 Blg_Attributes 数据集\n", "create_blg_attributes(directory_path)\n", "\n", "# 读取并打印 Blg_Attributes 数据集的值\n", "for filename in os.listdir(directory_path):\n", " if filename.endswith('.h5'):\n", " filepath = os.path.join(directory_path, filename)\n", " try:\n", " with h5py.File(filepath, 'r') as h5file:\n", " if 'Blg_Attributes' in h5file:\n", " blg_attributes = {}\n", " for key, dataset in h5file['Blg_Attributes'].items():\n", " data = dataset[:]\n", " if dataset.dtype.kind in ['i', 'u']:\n", " tensor = torch.from_numpy(data).to(torch.int64)\n", " elif dataset.dtype.kind == 'f':\n", " tensor = torch.from_numpy(data).to(torch.float32)\n", " else:\n", " raise TypeError(f'Unknown data type: {data.dtype}')\n", " blg_attributes[key] = tensor\n", " print(f\"文件: {filename}, Blg_Attributes: {blg_attributes}\")\n", " except Exception as e:\n", " print(f\"读取文件 {filename} 时出错: {e}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 检查" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "ml", "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.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }