Fix streaming

#7
by lhoestq HF Staff - opened
Files changed (1) hide show
  1. pubchem_selfies.py +13 -12
pubchem_selfies.py CHANGED
@@ -16,6 +16,7 @@
16
  import json
17
  import glob
18
  import gzip
 
19
 
20
  import datasets
21
 
@@ -472,15 +473,15 @@ class PubchemSelfies(datasets.GeneratorBasedBuilder):
472
  def _generate_examples(self, subdirs, split):
473
  # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
474
  for filepath in subdirs:
475
- with gzip.open(filepath, mode="rt") as f:
476
- rows = f.readlines()
477
- for key, row in enumerate(rows):
478
- data = json.loads(row)
479
- properties = data["molecules"][0]["properties"]
480
- yield key, {
481
- "PUBCHEM_COMPOUND_CID": properties["PUBCHEM_COMPOUND_CID"],
482
- "PUBCHEM_OPENEYE_CAN_SMILES": properties[
483
- "PUBCHEM_OPENEYE_CAN_SMILES"
484
- ],
485
- "CAN_SELFIES": properties["CAN_SELFIES"],
486
- }
 
16
  import json
17
  import glob
18
  import gzip
19
+ import os
20
 
21
  import datasets
22
 
 
473
  def _generate_examples(self, subdirs, split):
474
  # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
475
  for filepath in subdirs:
476
+ with gzip.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
477
+ for row_idx, row in enumerate(f):
478
+ data = json.loads(row)
479
+ properties = data["molecules"][0]["properties"]
480
+ key = f"{os.path.basename(filepath)}_{row_idx}"
481
+ yield key, {
482
+ "PUBCHEM_COMPOUND_CID": properties["PUBCHEM_COMPOUND_CID"],
483
+ "PUBCHEM_OPENEYE_CAN_SMILES": properties[
484
+ "PUBCHEM_OPENEYE_CAN_SMILES"
485
+ ],
486
+ "CAN_SELFIES": properties["CAN_SELFIES"],
487
+ }