File size: 14,326 Bytes
d48acef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
{
 "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
}