-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathutils.py
More file actions
50 lines (37 loc) · 1.47 KB
/
Copy pathutils.py
File metadata and controls
50 lines (37 loc) · 1.47 KB
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
import os
import dill as pickle
from pathlib import Path
BASE_DIR = Path(__file__).parent.parent / "cache"
os.environ["TRANSFORMERS_CACHE"] = f"{(Path(BASE_DIR) / '.cache').absolute()}/"
def setup_notebook():
try:
from IPython import get_ipython
ipython = get_ipython()
ipython.magic("load_ext autoreload")
ipython.magic("autoreload 2")
except:
pass
def expand_descriptions(compressed_descriptions):
expand_descriptions = []
for start_index, end_index, description in compressed_descriptions:
expand_descriptions.extend([description] * (end_index - start_index))
return expand_descriptions
def get_lambdas_and_descriptions(layer, explanation_name, task, token):
file_prefix = (
f"figs/{task.name}/regression/token_{token}/layer_{layer}/{explanation_name}"
)
lambdas = pickle.load(open(f"{file_prefix}_lambdas.pkl", "rb"))
descriptions = pickle.load(open(f"{file_prefix}_descriptions.pkl", "rb"))
return lambdas, descriptions
def is_notebook() -> bool:
try:
from IPython import get_ipython
shell = get_ipython().__class__.__name__
if shell == "ZMQInteractiveShell":
return True # Jupyter notebook or qtconsole
elif shell == "TerminalInteractiveShell":
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter