Skip to content

Commit f5de1a5

Browse files
committed
Inject SST
1 parent 28055e3 commit f5de1a5

10 files changed

Lines changed: 52 additions & 13 deletions

File tree

docs/source/advanced.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ For this reason, usage of :class:`podium.datasets.BucketIterator` is recommended
417417
>>> label = LabelField(name='label')
418418
>>> fields = {'text': text, 'label': label}
419419
>>>
420-
>>> sst_train, sst_valid, sst_test = SST.get_dataset_splits(fields=fields)
421-
>>> sst_train.finalize_fields()
420+
>>> train, valid, test = SST.get_dataset_splits(fields=fields)
421+
>>> train.finalize_fields()
422422
>>>
423423
>>> # Define the iterators and our sort key
424424
>>> from podium import Iterator, BucketIterator

docs/source/notebooks/advanced.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,8 @@
901901
"label = LabelField(name='label')\n",
902902
"fields = {'text': text, 'label': label}\n",
903903
"\n",
904-
"sst_train, sst_valid, sst_test = SST.get_dataset_splits(fields=fields)\n",
905-
"sst_train.finalize_fields()\n",
904+
"train, valid, test = SST.get_dataset_splits(fields=fields)\n",
905+
"train.finalize_fields()\n",
906906
"\n",
907907
"# Define the iterators and our sort key\n",
908908
"from podium import Iterator, BucketIterator\n",

docs/source/scripts/check_notebooks.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import argparse
22
import copy
33
import multiprocess
4+
import os
5+
import shutil
46
import string
7+
import subprocess
58
import textwrap
69
from functools import partial
710
from pathlib import Path
@@ -13,9 +16,30 @@
1316
NOTEBOOKS_PATH = "../notebooks"
1417
INSTALL_RELEASE_VERSION_COMMAND = "! pip install podium-nlp"
1518
INSTALL_SOURCE_VERSION_COMMAND = "# ! pip install git+https://github.com/TakeLab/podium.git"
19+
INSTALL_SST_COMMAND = "python -c \"from podium.datasets import SST; SST.get_dataset_splits()\""
1620
TRANS_TABLE = str.maketrans(dict.fromkeys(string.whitespace))
1721

1822

23+
def inject_sst():
24+
delim = "&" if os.name == "nt" else ";"
25+
subprocess.call(
26+
delim.join([INSTALL_SOURCE_VERSION_COMMAND[4:], INSTALL_SST_COMMAND]),
27+
shell=True,
28+
cwd=Path(NOTEBOOKS_PATH).absolute(),
29+
stdout=subprocess.DEVNULL,
30+
stderr=subprocess.STDOUT,
31+
)
32+
33+
34+
def cleanup(snap_before_exec, snap_after_exec):
35+
created_paths = set(snap_after_exec) - set(snap_before_exec)
36+
for path in created_paths:
37+
if path.is_dir():
38+
shutil.rmtree(path)
39+
else:
40+
path.unlink()
41+
42+
1943
def print_notebook_name_with_error(func):
2044
def wrapper(*args, **kwargs):
2145
if args:
@@ -101,6 +125,7 @@ def check_notebook_output(notebook_path, env="python3", ignore_whitespace=False)
101125
parser.add_argument("--env", default="python3", help="kernel that executes the notebook")
102126
parser.add_argument("--num_proc", help="number of processes for parallel execution")
103127
parser.add_argument("--ignore_whitespace", action="store_true", help="ignore whitespace when comparing cell outputs")
128+
parser.add_argument("--keep_artifacts", action="store_true", help="save files/directories created during execution")
104129
args = parser.parse_args()
105130

106131
if args.num_proc is None:
@@ -116,16 +141,24 @@ def check_notebook_output(notebook_path, env="python3", ignore_whitespace=False)
116141
if not notebook_path.name.endswith("-checkpoint.ipynb")
117142
]
118143

144+
snap_before_exec = list(Path(NOTEBOOKS_PATH).iterdir())
145+
119146
num_proc = min(min(num_proc, multiprocess.cpu_count()), len(notebook_paths))
120147
if num_proc == 1:
121148
reports = []
122149
for notebook_path in notebook_paths:
123150
report = check_notebook_output(notebook_path, env=args.env, ignore_whitespace=args.ignore_whitespace)
124151
reports.append(report)
125152
else:
153+
# inject the SST dataset to prevent parallel download
154+
inject_sst()
126155
with multiprocess.Pool(num_proc) as pool:
127156
reports = pool.map(partial(check_notebook_output, env=args.env, ignore_whitespace=args.ignore_whitespace), notebook_paths)
128157

158+
if args.keep_artefacts is False:
159+
snap_after_exec = list(Path(NOTEBOOKS_PATH).iterdir())
160+
cleanup(snap_before_exec, snap_after_exec)
161+
129162
if any(report for _, report in reports):
130163
reports_str = "\n\n".join([
131164
f"In notebook {notebook}:\n" + textwrap.indent(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ipykernel
2+
ipywidgets
23
nbformat
34
nbconvert
45
multiprocess

podium/datasets/arrow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def from_tabular_file(
299299
format = format.lower()
300300
csv_reader_params = {} if csv_reader_params is None else csv_reader_params
301301

302-
with open(os.path.expanduser(path), encoding="utf8") as f:
302+
with open(os.path.expanduser(path), encoding="utf-8") as f:
303303
if format in {"csv", "tsv"}:
304304
delimiter = "," if format == "csv" else "\t"
305305
reader = csv.reader(f, delimiter=delimiter, **csv_reader_params)
@@ -542,7 +542,7 @@ def load_cache(cache_path) -> "DiskBackedDataset":
542542
"""
543543
# load fields
544544
fields_file_path = os.path.join(cache_path, CACHE_FIELDS_FILENAME)
545-
with open(fields_file_path, "rb") as fields_cache_file:
545+
with open(os.path.expanduser(fields_file_path), "rb") as fields_cache_file:
546546
fields = pickle.load(fields_cache_file)
547547

548548
# load dataset as memory mapped arrow table
@@ -587,7 +587,7 @@ def dump_cache(self, cache_path: Optional[str] = None) -> str:
587587

588588
# pickle fields
589589
cache_fields_path = os.path.join(cache_path, CACHE_FIELDS_FILENAME)
590-
with open(cache_fields_path, "wb") as fields_cache_file:
590+
with open(os.path.expanduser(cache_fields_path), "wb") as fields_cache_file:
591591
pickle.dump(self.fields, fields_cache_file)
592592

593593
# dump table

podium/datasets/impl/conllu_dataset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Module contains the CoNLL-U dataset.
33
"""
44
import collections
5+
import os
56

67
from podium.datasets import Dataset
78
from podium.datasets.example_factory import ExampleFactory
@@ -87,7 +88,7 @@ def safe_conllu_parse(in_file):
8788
example_factory = ExampleFactory(fields)
8889

8990
examples = []
90-
with open(file_path, encoding="utf-8") as in_file:
91+
with open(os.path.expanduser(file_path), encoding="utf-8") as in_file:
9192
for tokenlist in safe_conllu_parse(in_file):
9293
example_dict = collections.defaultdict(lambda: [])
9394
for token in tokenlist:

podium/datasets/impl/imdb.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ def _create_labeled_examples(dir_path, label, fields):
153153
]
154154
examples = []
155155
for file_path in files_list:
156-
with open(file=os.path.join(dir_path, file_path), encoding="utf8") as fpr:
156+
with open(
157+
os.path.expanduser(os.path.join(dir_path, file_path)), encoding="utf-8"
158+
) as fpr:
157159
data = {IMDB.TEXT_FIELD_NAME: fpr.read(), IMDB.LABEL_FIELD_NAME: label}
158160
examples.append(example_factory.from_dict(data))
159161
return examples

podium/datasets/impl/snli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def _create_examples(file_path, fields):
9999
example_factory = ExampleFactory(fields)
100100
examples = []
101101

102-
with open(file=file_path, encoding="utf8") as in_file:
102+
with open(os.path.expanduser(file_path), encoding="utf-8") as in_file:
103103
for line in in_file:
104104
examples.append(example_factory.from_json(line))
105105
return examples

podium/datasets/impl/sst.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def label_trf(label):
123123
return label_to_string_map[label]
124124

125125
examples = []
126-
with open(file=file_path, encoding="utf8") as fpr:
126+
with open(os.path.expanduser(file_path), encoding="utf-8") as fpr:
127127
for line in fpr:
128128

129129
example = example_factory.from_fields_tree(

podium/vectorizers/vectorizer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def _cache_vectors(self):
298298
"""
299299
Method for caching loaded vectors to cache_dir.
300300
"""
301-
with open(self._cache_path, "wb") as cache_file:
301+
with open(os.path.expanduser(self._cache_path), "wb") as cache_file:
302302
for word in self._vectors:
303303
vector_values_string = " ".join(map(str, self._vectors[word]))
304304
cache_file.write(f"{word} {vector_values_string}\n".encode("utf-8"))
@@ -362,7 +362,9 @@ def _load_vectors(self, vocab=None):
362362
vocab = set(vocab)
363363

364364
open_mode, split_delimiter = ("rb", b" ") if self._binary else ("r", " ")
365-
with open(curr_path, open_mode, encoding=self._encoding) as vector_file:
365+
with open(
366+
os.path.expanduser(curr_path), open_mode, encoding=self._encoding
367+
) as vector_file:
366368

367369
vectors_loaded = 0
368370
header_lines = 0

0 commit comments

Comments
 (0)