File size: 5,400 Bytes
5cbe276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369d8de
5cbe276
 
 
7665f16
 
5cbe276
 
 
 
 
 
 
 
 
 
 
 
369d8de
5cbe276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369d8de
5cbe276
369d8de
5cbe276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7665f16
 
 
 
 
 
 
5cbe276
7665f16
5cbe276
 
 
 
 
 
 
 
 
 
 
 
 
 
7665f16
5cbe276
 
7665f16
 
 
 
 
 
5cbe276
 
 
 
 
 
 
 
 
 
 
 
 
369d8de
5cbe276
369d8de
5cbe276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369d8de
5cbe276
369d8de
5cbe276
 
 
 
 
 
 
7665f16
5cbe276
 
 
 
 
 
 
 
 
 
 
 
 
7665f16
5cbe276
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Accessing the Data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to be able to access the data on Hugging Face Hub, we must import the\n",
    "necessary libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import the load_dataset function from the datasets module\n",
    "from datasets import load_dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After importing the modules, we set a few variables that will be used throughout\n",
    "this demo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# path to the dataset repository on the Hugging Face Hub\n",
    "path = \"molssiai-hub/pubchemqc-b3lyp\"\n",
    "\n",
    "# set the dataset configuration/subset name\n",
    "name = \"b3lyp_pm6\"\n",
    "\n",
    "# set the dataset split\n",
    "split = \"train\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this tutorial, we are going to work with the\n",
    "[PubChemQC-B3LYP/6-31G*//PM6\n",
    "Dataset](https://huggingface.co/datasets/molssiai-hub/pubchemqc-b3lyp)\n",
    "(PubChemQC-B3LYP for short) from the [PubChemQC dataset\n",
    "collection](https://huggingface.co/collections/molssiai-hub/pubchemqc-datasets-669e5482260861ba7cce3d1c).\n",
    "Let us load the dataset as shown below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the dataset\n",
    "hub_dataset = load_dataset(path=path,\n",
    "                           name=name,\n",
    "                           split=split,\n",
    "                           streaming=True,\n",
    "                           trust_remote_code=True)\n",
    "\n",
    "hub_dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PubChemQC datasets are very large in size and downloading them to your local\n",
    "machine can be a heavy lift for your internet network and disk storage.\n",
    "Therefore, we set the `streaming` parameter to `True` to avoid downloading the\n",
    "dataset on disk and ensure streaming the data from the hub. In the `streaming`\n",
    "mode, the `load_dataset` function returns an `IterableDataset` object which\n",
    "allows iterative access to the data. The `trust_remote_code` argument is also\n",
    "set to `True` to allow the usage of a custom [load\n",
    "script](https://huggingface.co/datasets/molssiai-hub/pubchemqc-b3lyp/blob/main/pubchemqc-b3lyp.py)\n",
    "which is in charge of preprocessing the data.\n",
    "\n",
    "The PubChemQC-B3LYP dataset is made of several files called `shards` that enable\n",
    "multiprocessing and parallelization of the data loading process. Multiprocessing\n",
    "can speed up the loading process significantly if you are absolutely sure that\n",
    "you have enough CPU cores to handle the load and enough memory and storage space\n",
    "to download and store the data.\n",
    "\n",
    "You can choose the number of processes to use for loading the data by setting\n",
    "the `num_proc` parameter in the `load_dataset` function.\n",
    "\n",
    "```python\n",
    "\n",
    "    >>> dataset = load_dataset(path=path,\n",
    "                               split=split,\n",
    "                               streaming=False,\n",
    "                               trust_remote_code=True,\n",
    "                               num_proc=4)\n",
    "```\n",
    "\n",
    "Note that we have set the `streaming` parameter to `False` in the code snippet\n",
    "above. This allows the `load_dataset` function to download the dataset on disk\n",
    "and load it into memory as a `Dataset` object which can access the data using\n",
    "fancy indexing and slicing."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once we create our `Dataset` or `IterableDataset` instance, we can access its\n",
    "features or column names using the `features` or `column_names` attributes of\n",
    "the dataset object.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print the column names\n",
    "hub_dataset.column_names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use `take(n)` to fetch the first $n$ examples from the dataset.\n",
    "For demonstration purposes, we set $n$ to 2 which yields a list of\n",
    "two dictionaries, each containing the features of the corresponding\n",
    "data point."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(hub_dataset.take(2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "hface",
   "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}