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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "see: http://millionsongdataset.com/pages/getting-dataset/#subset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget http://labrosa.ee.columbia.edu/~dpwe/tmp/millionsongsubset.tar.gz\n",
    "# !tar -xvzf millionsongsubset.tar.gz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install pandas h5py pyarrow fastparquet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import h5py\n",
    "import pandas as pd\n",
    "from tqdm.auto import tqdm\n",
    "\n",
    "import unibox as ub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a7418816c46f4f5b95a8c7e307b6e569",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Listing local files: 0files [00:00, ?files/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(ub.ls(\"../data/MillionSongSubset\", [\".h5\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 10000/10000 [00:39<00:00, 250.36it/s]\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import hdf5_getters\n",
    "import h5py\n",
    "from tqdm import tqdm\n",
    "from concurrent.futures import ProcessPoolExecutor\n",
    "\n",
    "# Define dataset path\n",
    "dataset_path = \"/lv0/yada/dataproc5/data/MillionSongSubset\"\n",
    "\n",
    "# Function to extract all available fields from an HDF5 file\n",
    "def extract_song_data(file_path):\n",
    "    \"\"\"Extracts all available fields from an HDF5 song file using hdf5_getters.\"\"\"\n",
    "    song_data = {}\n",
    "\n",
    "    try:\n",
    "        with hdf5_getters.open_h5_file_read(file_path) as h5:\n",
    "            # Get all getter functions from hdf5_getters\n",
    "            getters = [func for func in dir(hdf5_getters) if func.startswith(\"get_\")]\n",
    "\n",
    "            for getter in getters:\n",
    "                try:\n",
    "                    # Dynamically call each getter function\n",
    "                    value = getattr(hdf5_getters, getter)(h5)\n",
    "\n",
    "                    # Optimize conversions\n",
    "                    if isinstance(value, np.ndarray):\n",
    "                        value = value.tolist()\n",
    "                    elif isinstance(value, bytes):\n",
    "                        value = value.decode()\n",
    "\n",
    "                    # Store in dictionary with a cleaned-up key name\n",
    "                    song_data[getter[4:]] = value\n",
    "\n",
    "                except Exception:\n",
    "                    continue  # Skip errors but don't slow down\n",
    "\n",
    "    except Exception as e:\n",
    "        print(f\"Error processing {file_path}: {e}\")\n",
    "    \n",
    "    return song_data\n",
    "\n",
    "# Function to process multiple files in parallel\n",
    "def process_files_in_parallel(h5_files, num_workers=8):\n",
    "    \"\"\"Processes multiple .h5 files in parallel.\"\"\"\n",
    "    all_songs = []\n",
    "\n",
    "    with ProcessPoolExecutor(max_workers=num_workers) as executor:\n",
    "        for song_data in tqdm(executor.map(extract_song_data, h5_files), total=len(h5_files)):\n",
    "            if song_data:\n",
    "                all_songs.append(song_data)\n",
    "    \n",
    "    return all_songs\n",
    "\n",
    "# Find all .h5 files\n",
    "h5_files = [os.path.join(root, file) for root, _, files in os.walk(dataset_path) for file in files if file.endswith(\".h5\")]\n",
    "\n",
    "# Process files in parallel\n",
    "all_songs = process_files_in_parallel(h5_files, num_workers=24)\n",
    "\n",
    "# Convert to Pandas DataFrame\n",
    "df = pd.DataFrame(all_songs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10000, 55)\n",
      "Index(['analysis_sample_rate', 'artist_7digitalid', 'artist_familiarity',\n",
      "       'artist_hotttnesss', 'artist_id', 'artist_latitude', 'artist_location',\n",
      "       'artist_longitude', 'artist_mbid', 'artist_mbtags',\n",
      "       'artist_mbtags_count', 'artist_name', 'artist_playmeid', 'artist_terms',\n",
      "       'artist_terms_freq', 'artist_terms_weight', 'audio_md5',\n",
      "       'bars_confidence', 'bars_start', 'beats_confidence', 'beats_start',\n",
      "       'danceability', 'duration', 'end_of_fade_in', 'energy', 'key',\n",
      "       'key_confidence', 'loudness', 'mode', 'mode_confidence', 'num_songs',\n",
      "       'release', 'release_7digitalid', 'sections_confidence',\n",
      "       'sections_start', 'segments_confidence', 'segments_loudness_max',\n",
      "       'segments_loudness_max_time', 'segments_loudness_start',\n",
      "       'segments_pitches', 'segments_start', 'segments_timbre',\n",
      "       'similar_artists', 'song_hotttnesss', 'song_id', 'start_of_fade_out',\n",
      "       'tatums_confidence', 'tatums_start', 'tempo', 'time_signature',\n",
      "       'time_signature_confidence', 'title', 'track_7digitalid', 'track_id',\n",
      "       'year'],\n",
      "      dtype='object')\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>analysis_sample_rate</th>\n",
       "      <th>artist_7digitalid</th>\n",
       "      <th>artist_familiarity</th>\n",
       "      <th>artist_hotttnesss</th>\n",
       "      <th>artist_id</th>\n",
       "      <th>artist_latitude</th>\n",
       "      <th>artist_location</th>\n",
       "      <th>artist_longitude</th>\n",
       "      <th>artist_mbid</th>\n",
       "      <th>artist_mbtags</th>\n",
       "      <th>...</th>\n",
       "      <th>start_of_fade_out</th>\n",
       "      <th>tatums_confidence</th>\n",
       "      <th>tatums_start</th>\n",
       "      <th>tempo</th>\n",
       "      <th>time_signature</th>\n",
       "      <th>time_signature_confidence</th>\n",
       "      <th>title</th>\n",
       "      <th>track_7digitalid</th>\n",
       "      <th>track_id</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>22050</td>\n",
       "      <td>174717</td>\n",
       "      <td>0.450743</td>\n",
       "      <td>0.331215</td>\n",
       "      <td>AR1DGSO1187FB59B15</td>\n",
       "      <td>NaN</td>\n",
       "      <td></td>\n",
       "      <td>NaN</td>\n",
       "      <td>fe4e71a9-ddb9-47b5-9e2e-ec53862a91c6</td>\n",
       "      <td>[]</td>\n",
       "      <td>...</td>\n",
       "      <td>266.879</td>\n",
       "      <td>[0.0, 0.0, 0.896, 0.819, 0.664, 0.693, 0.67, 0...</td>\n",
       "      <td>[0.16738, 0.44887, 0.73036, 1.09072, 1.44407, ...</td>\n",
       "      <td>107.053</td>\n",
       "      <td>4</td>\n",
       "      <td>0.657</td>\n",
       "      <td>Jody</td>\n",
       "      <td>2555900</td>\n",
       "      <td>TRAHHUN128F4227029</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22050</td>\n",
       "      <td>7173</td>\n",
       "      <td>0.392710</td>\n",
       "      <td>0.311789</td>\n",
       "      <td>ARO6WZY1187FB3A86E</td>\n",
       "      <td>NaN</td>\n",
       "      <td></td>\n",
       "      <td>NaN</td>\n",
       "      <td>23f7ad3f-a189-4a1c-9991-4763ded495a7</td>\n",
       "      <td>[]</td>\n",
       "      <td>...</td>\n",
       "      <td>321.300</td>\n",
       "      <td>[0.451, 0.426, 0.396, 0.32, 0.255, 0.204, 0.15...</td>\n",
       "      <td>[0.05024, 0.25641, 0.46357, 0.66974, 0.87691, ...</td>\n",
       "      <td>149.853</td>\n",
       "      <td>3</td>\n",
       "      <td>1.000</td>\n",
       "      <td>Turntable Terrorist</td>\n",
       "      <td>5591259</td>\n",
       "      <td>TRAHHMM128F932D5D9</td>\n",
       "      <td>1995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>22050</td>\n",
       "      <td>2759</td>\n",
       "      <td>0.602767</td>\n",
       "      <td>0.463193</td>\n",
       "      <td>ARH1LE01187B98D68D</td>\n",
       "      <td>NaN</td>\n",
       "      <td></td>\n",
       "      <td>NaN</td>\n",
       "      <td>3df3a779-a7b1-4362-a8b4-9ae6c7eb623d</td>\n",
       "      <td>[b'american', b'soundtrack']</td>\n",
       "      <td>...</td>\n",
       "      <td>67.895</td>\n",
       "      <td>[0.056, 0.058, 0.056, 0.059, 0.097, 0.093, 0.0...</td>\n",
       "      <td>[0.54095, 0.86496, 1.20205, 1.52933, 1.85662, ...</td>\n",
       "      <td>91.249</td>\n",
       "      <td>4</td>\n",
       "      <td>0.568</td>\n",
       "      <td>Porcelain Man</td>\n",
       "      <td>7341937</td>\n",
       "      <td>TRAHHJY12903CA73BD</td>\n",
       "      <td>1999</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows Γ— 55 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   analysis_sample_rate  artist_7digitalid  artist_familiarity  \\\n",
       "0                 22050             174717            0.450743   \n",
       "1                 22050               7173            0.392710   \n",
       "2                 22050               2759            0.602767   \n",
       "\n",
       "   artist_hotttnesss           artist_id  artist_latitude artist_location  \\\n",
       "0           0.331215  AR1DGSO1187FB59B15              NaN                   \n",
       "1           0.311789  ARO6WZY1187FB3A86E              NaN                   \n",
       "2           0.463193  ARH1LE01187B98D68D              NaN                   \n",
       "\n",
       "   artist_longitude                           artist_mbid  \\\n",
       "0               NaN  fe4e71a9-ddb9-47b5-9e2e-ec53862a91c6   \n",
       "1               NaN  23f7ad3f-a189-4a1c-9991-4763ded495a7   \n",
       "2               NaN  3df3a779-a7b1-4362-a8b4-9ae6c7eb623d   \n",
       "\n",
       "                  artist_mbtags  ... start_of_fade_out  \\\n",
       "0                            []  ...           266.879   \n",
       "1                            []  ...           321.300   \n",
       "2  [b'american', b'soundtrack']  ...            67.895   \n",
       "\n",
       "                                   tatums_confidence  \\\n",
       "0  [0.0, 0.0, 0.896, 0.819, 0.664, 0.693, 0.67, 0...   \n",
       "1  [0.451, 0.426, 0.396, 0.32, 0.255, 0.204, 0.15...   \n",
       "2  [0.056, 0.058, 0.056, 0.059, 0.097, 0.093, 0.0...   \n",
       "\n",
       "                                        tatums_start    tempo time_signature  \\\n",
       "0  [0.16738, 0.44887, 0.73036, 1.09072, 1.44407, ...  107.053              4   \n",
       "1  [0.05024, 0.25641, 0.46357, 0.66974, 0.87691, ...  149.853              3   \n",
       "2  [0.54095, 0.86496, 1.20205, 1.52933, 1.85662, ...   91.249              4   \n",
       "\n",
       "  time_signature_confidence                title track_7digitalid  \\\n",
       "0                     0.657                 Jody          2555900   \n",
       "1                     1.000  Turntable Terrorist          5591259   \n",
       "2                     0.568        Porcelain Man          7341937   \n",
       "\n",
       "             track_id  year  \n",
       "0  TRAHHUN128F4227029     0  \n",
       "1  TRAHHMM128F932D5D9  1995  \n",
       "2  TRAHHJY12903CA73BD  1999  \n",
       "\n",
       "[3 rows x 55 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ub.peeks(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[37m2025-02-19 14:01:45 [INFO] HuggingFaceDatasetsBackend.data_to_hub: Uploading dataset to HF repo trojblue/million-song-subset\u001b[0m\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c6e47a2259e54cb19dc37e6762883cbc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/5 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f69f061bad16497ca9f9bac1ab4500c2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/20 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1d998490521645edb6d4e462de6045f4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/20 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "389e657da20c4a3c8e18899e57642b96",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/20 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "767913f1d6134a888edea631657fce58",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/20 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b59fd321ef98408880ec22deb55498c6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/20 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[37m2025-02-19 14:02:47 [INFO] saves: DataFrame saved (HF dataset) to \"hf://trojblue/million-song-subset\" in 62.75s\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "ub.saves(df, \"hf://trojblue/million-song-subset\", private=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}