File size: 7,772 Bytes
508cc13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e02edab2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import h5py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8647ba47",
   "metadata": {},
   "outputs": [],
   "source": [
    "h5_file = \"/home/user/data/PHMbenchdata/PHM-Vibench/metadata_5_29.h5\"\n",
    "h5 = h5py.File(h5_file, 'r')\n",
    "# analyze_hdf5_dataset(h5_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ecebef1",
   "metadata": {},
   "source": [
    "## single fix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c1ab2d25",
   "metadata": {},
   "outputs": [],
   "source": [
    "h5_file = \"/home/user/data/PHMbenchdata/PHM-Vibench/metadata_5_29.h5\"\n",
    "h5 = h5py.File(h5_file, 'r')\n",
    "h6_file = \"/home/user/data/PHMbenchdata/PHM-Vibench/metadata_6_1.h5\"\n",
    "h6 = h5py.File(h6_file, 'r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5919b4ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<HDF5 dataset \"47064\": shape (512000, 6, 1), type \"<f8\">"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id = '47064'\n",
    "# h5[id],\n",
    "h6[id]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bbc401df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(49653, 49875)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(h5), len(h6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "29a1068e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/49653 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 49653/49653 [03:32<00:00, 234.17it/s] \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据转换结果:\n",
      "      dataset_id  original_shape    new_shape original_dtype new_dtype  \\\n",
      "0              1  (243938, 2, 1)  (243938, 2)        float64   float64   \n",
      "1             10  (486224, 2, 1)  (486224, 2)        float64   float64   \n",
      "2            100  (484483, 2, 1)  (484483, 2)        float64   float64   \n",
      "3           1000   (32768, 2, 1)   (32768, 2)        float64   float64   \n",
      "4          10000    (2560, 2, 1)    (2560, 2)        float64   float64   \n",
      "...          ...             ...          ...            ...       ...   \n",
      "49648       9995    (2560, 2, 1)    (2560, 2)        float64   float64   \n",
      "49649       9996    (2560, 2, 1)    (2560, 2)        float64   float64   \n",
      "49650       9997    (2560, 2, 1)    (2560, 2)        float64   float64   \n",
      "49651       9998    (2560, 2, 1)    (2560, 2)        float64   float64   \n",
      "49652       9999    (2560, 2, 1)    (2560, 2)        float64   float64   \n",
      "\n",
      "      conversion_type   status  \n",
      "0            3D to 2D  success  \n",
      "1            3D to 2D  success  \n",
      "2            3D to 2D  success  \n",
      "3            3D to 2D  success  \n",
      "4            3D to 2D  success  \n",
      "...               ...      ...  \n",
      "49648        3D to 2D  success  \n",
      "49649        3D to 2D  success  \n",
      "49650        3D to 2D  success  \n",
      "49651        3D to 2D  success  \n",
      "49652        3D to 2D  success  \n",
      "\n",
      "[49653 rows x 7 columns]\n",
      "\n",
      "转换完成!新文件已保存为: /home/user/data/PHMbenchdata/PHM-Vibench/metadata_6_1.h5\n",
      "转换结果已保存到: conversion_summary.csv\n"
     ]
    }
   ],
   "source": [
    "# 统计h5文件并进行转换\n",
    "conversion_results = []\n",
    "output_file = \"/home/user/data/PHMbenchdata/PHM-Vibench/metadata_6_1.h5\"\n",
    "import tqdm\n",
    "import numpy as np\n",
    "# 创建新的h5文件\n",
    "with h5py.File(output_file, 'w') as new_h5:\n",
    "    for key in tqdm.tqdm(h5.keys()):\n",
    "        try:\n",
    "            original_data = h5[key][()]\n",
    "            original_shape = original_data.shape\n",
    "            original_dtype = original_data.dtype\n",
    "            \n",
    "            # 转换数据类型为float\n",
    "            converted_data = original_data.astype(np.float64)\n",
    "            \n",
    "            # 如果是3维,转换为2维 (L, C)\n",
    "            if len(original_shape) == 3:\n",
    "                # 假设3维数据格式为 (samples, length, channels) 或类似\n",
    "                # 将其重塑为 (length, channels) 或 (L, C)\n",
    "                if original_shape[-1] == 1:\n",
    "                    # 如果第一维是1,去掉它\n",
    "                    # new_shape = original_shape[1:]\n",
    "                    converted_data = converted_data.reshape(original_shape[0], original_shape[1])  # 保持第一维不变,展平其他维度\n",
    "                # else:\n",
    "                #     # 将3维转为2维:合并前两维或根据具体需求调整\n",
    "                    new_shape = (original_shape[0], original_shape[1])\n",
    "                #     converted_data = converted_data.reshape(new_shape)\n",
    "            else:\n",
    "                new_shape = converted_data.shape\n",
    "            \n",
    "            # 保存到新文件\n",
    "            new_h5.create_dataset(key, data=converted_data)\n",
    "            \n",
    "            # 记录转换结果\n",
    "            conversion_results.append({\n",
    "                'dataset_id': key,\n",
    "                'original_shape': original_shape,\n",
    "                'new_shape': new_shape,\n",
    "                'original_dtype': str(original_dtype),\n",
    "                'new_dtype': str(converted_data.dtype),\n",
    "                'conversion_type': '3D to 2D' if len(original_shape) == 3 else 'dtype only',\n",
    "                'status': 'success'\n",
    "            })\n",
    "            \n",
    "        except Exception as e:\n",
    "            conversion_results.append({\n",
    "                'dataset_id': key,\n",
    "                'original_shape': 'error',\n",
    "                'new_shape': 'error',\n",
    "                'original_dtype': 'error',\n",
    "                'new_dtype': 'error',\n",
    "                'conversion_type': 'error',\n",
    "                'status': f'failed: {str(e)}'\n",
    "            })\n",
    "\n",
    "# 创建转换结果表格\n",
    "conversion_df = pd.DataFrame(conversion_results)\n",
    "print(\"数据转换结果:\")\n",
    "print(conversion_df)\n",
    "\n",
    "# 保存转换结果到CSV\n",
    "conversion_df.to_csv(\"conversion_summary.csv\", index=False)\n",
    "print(f\"\\n转换完成!新文件已保存为: {output_file}\")\n",
    "print(\"转换结果已保存到: conversion_summary.csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "LQ_signal",
   "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.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}