Asteri2themoon commited on
Commit
e40b6e3
1 Parent(s): fa6ee2d

can be used from materials-toolkit

Browse files
Files changed (2) hide show
  1. download-and-process.py +31 -91
  2. materials-project.tar.gz +2 -2
download-and-process.py CHANGED
@@ -11,10 +11,17 @@ import multiprocessing as mp
11
  import json
12
  import re
13
  import tarfile
 
 
 
 
14
 
15
  from ase.io import read
16
  import numpy as np
 
17
  import h5py
 
 
18
 
19
  zip_file = "mp.2019.04.01.json.zip"
20
  url = "https://figshare.com/ndownloader/articles/8097992/versions/2"
@@ -97,108 +104,41 @@ def gen_structure_from_json(filename: str, chunksize: Optional[int] = 1 << 20):
97
  yield "{" + stack + "}"
98
 
99
 
100
- def parse_structure(json_str: str) -> tuple:
101
  data = json.loads(json_str)
102
  struct = read(io.StringIO(data["structure"]), format="cif")
103
 
104
- cell = struct.cell.array
105
- natoms = len(struct)
106
- x = struct.get_scaled_positions()
107
- formula = struct.get_chemical_formula()
108
- z = struct.get_atomic_numbers()
109
-
110
- material_id = data["material_id"]
111
- energy_pa = data["formation_energy_per_atom"]
112
- return material_id, natoms, formula, cell, x, z, energy_pa
113
-
114
 
115
- def process_job(input_queue, output_queue):
116
- while True:
117
- job = input_queue.get()
118
 
119
- if job is None:
120
- break
121
 
122
- output_queue.put(parse_structure(job))
 
123
 
 
124
 
125
- def process(path: Optional[str] = ".", workers: Optional[int] = max(4, os.cpu_count())):
126
- if os.path.exists(os.path.join(path, "index.json")) and os.path.exists(
127
- os.path.join(path, "data.hdf5")
128
  ):
129
  return
130
 
131
- input_queue = mp.Queue()
132
- output_queue = mp.Queue()
133
- results = []
134
-
135
- processes = []
136
- for _ in range(workers):
137
- p = mp.Process(target=process_job, args=(input_queue, output_queue))
138
- p.start()
139
- processes.append(p)
140
-
141
- for elem in gen_structure_from_json(
142
- os.path.join(path, "unzipped/mp.2019.04.01.json")
143
- ):
144
- input_queue.put(elem)
145
-
146
- if input_queue.qsize() > 2 * workers:
147
- results.append(output_queue.get())
148
- while not output_queue.empty():
149
- results.append(output_queue.get())
150
-
151
- for _ in range(workers):
152
- input_queue.put(None)
153
-
154
- for process in processes:
155
- process.join()
156
-
157
- while not output_queue.empty():
158
- results.append(output_queue.get())
159
-
160
- material_id = [material_id for material_id, _, _, _, _, _, _ in results]
161
- formula = [formula for _, _, formula, _, _, _, _ in results]
162
-
163
- natoms = np.array([natoms for _, natoms, _, _, _, _, _ in results], dtype=np.int64)
164
- atoms_ptr = np.pad(natoms.cumsum(0), (1, 0)).astype(np.int64)
165
- idx = np.arange(len(results), dtype=np.int64)
166
-
167
- cell = np.stack(
168
- [cell for _, _, _, cell, _, _, _ in results], axis=0, dtype=np.float32
169
- )
170
- x = np.concatenate([x for _, _, _, _, x, _, _ in results], axis=0, dtype=np.float32)
171
- z = np.concatenate([z for _, _, _, _, _, z, _ in results], axis=0, dtype=np.int64)
172
- energy_pa = np.array(
173
- [energy_pa for _, _, _, _, _, _, energy_pa in results], dtype=np.float32
174
- )
175
-
176
- index = [
177
- {
178
- "index": int(i),
179
- "id": str(m_id),
180
- "formula": str(f),
181
- "natoms": int(n),
182
- "energy_pa": float(e),
183
- }
184
- for i, m_id, f, n, e in zip(idx, material_id, formula, natoms, energy_pa)
185
  ]
186
- with open(os.path.join(path, "index.json"), "w") as fp:
187
- json.dump(index, fp)
188
-
189
- f = h5py.File("data.hdf5", "w")
190
-
191
- structures = f.create_group("structures")
192
- structures.create_dataset("cell", data=cell, dtype=np.float32)
193
- structures.create_dataset("natoms", data=natoms, dtype=np.int32)
194
- structures.create_dataset("energy_pa", data=energy_pa, dtype=np.float32)
195
- structures.create_dataset("atoms_ptr", data=atoms_ptr, dtype=np.int64)
196
-
197
- atoms = f.create_group("atoms")
198
- atoms.create_dataset("positions", data=x, dtype=np.float32)
199
- atoms.create_dataset("atomic_number", data=z, dtype=np.uint8)
200
 
201
- f.close()
202
 
203
 
204
  def compress(path: Optional[str] = "."):
@@ -209,8 +149,8 @@ def compress(path: Optional[str] = "."):
209
 
210
  print("compress into materials-project.tar.gz")
211
  with tarfile.open(output_file, "w:gz") as tar:
212
- tar.add(os.path.join(path, "index.json"))
213
- tar.add(os.path.join(path, "data.hdf5"))
214
 
215
 
216
  download_raw_mp()
 
11
  import json
12
  import re
13
  import tarfile
14
+ import resource
15
+
16
+ rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
17
+ resource.setrlimit(resource.RLIMIT_NOFILE, (1 << 16, rlimit[1]))
18
 
19
  from ase.io import read
20
  import numpy as np
21
+ import torch
22
  import h5py
23
+ from materials_toolkit.data import HDF5Dataset, StructureData
24
+ from materials_toolkit.data.datasets import MaterialsProjectData
25
 
26
  zip_file = "mp.2019.04.01.json.zip"
27
  url = "https://figshare.com/ndownloader/articles/8097992/versions/2"
 
104
  yield "{" + stack + "}"
105
 
106
 
107
+ def parse_structure(json_str: str) -> MaterialsProjectData:
108
  data = json.loads(json_str)
109
  struct = read(io.StringIO(data["structure"]), format="cif")
110
 
111
+ cell = torch.from_numpy(struct.cell.array).unsqueeze(0).float()
112
+ x = torch.from_numpy(struct.get_scaled_positions()).float()
113
+ z = torch.from_numpy(struct.get_atomic_numbers()).int()
114
+ material_id = torch.tensor(
115
+ [int(data["material_id"].split("-")[1])], dtype=torch.long
116
+ )
117
+ energy_pa = torch.tensor([data["formation_energy_per_atom"]], dtype=torch.float)
 
 
 
118
 
119
+ return MaterialsProjectData(
120
+ pos=x, z=z, cell=cell, material_id=material_id, energy_pa=energy_pa
121
+ )
122
 
 
 
123
 
124
+ def process(path: Optional[str] = "."):
125
+ mp_dir = os.path.join(path, "materials-project")
126
 
127
+ os.makedirs(mp_dir, exist_ok=True)
128
 
129
+ if os.path.exists(os.path.join(mp_dir, "batching.json")) and os.path.exists(
130
+ os.path.join(mp_dir, "data.hdf5")
 
131
  ):
132
  return
133
 
134
+ results = [
135
+ parse_structure(elem)
136
+ for elem in gen_structure_from_json(
137
+ os.path.join(path, "unzipped/mp.2019.04.01.json")
138
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
+ HDF5Dataset.create_dataset(mp_dir, results)
142
 
143
 
144
  def compress(path: Optional[str] = "."):
 
149
 
150
  print("compress into materials-project.tar.gz")
151
  with tarfile.open(output_file, "w:gz") as tar:
152
+ tar.add(os.path.join(path, "materials-project/batching.json"))
153
+ tar.add(os.path.join(path, "materials-project/data.hdf5"))
154
 
155
 
156
  download_raw_mp()
materials-project.tar.gz CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:bcf27bb6a544f3cb28ab686ca98ba0977b8ef0d4445e14a63990c8cb91a4158b
3
- size 40789247
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d27d6d4570823fff5a2482ff1e33df223dcffe33a550a0facbd7a4878ad2d37e
3
+ size 38793411