/
exceptions.py
151 lines (116 loc) · 4.88 KB
/
exceptions.py
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
"""
Every custom ScalarStop exception.
"""
from scalarstop.dataclasses import fields
class ScalarStopException(Exception):
"""
Base class for all ScalarStop exceptions.
"""
class YouForgotTheHyperparams(ValueError, ScalarStopException):
"""Raised when the user creates a class, but forgets to add a nested Hyperparams class."""
def __init__(self, obj):
super().__init__(
"Please make sure to define a dataclass containing "
f"hyperparameters at `{obj.__class__.__name__}.Hyperparams`. "
"Your dataclass should subclass `sp.HyperparamsType` "
"and is decorated with a `@sp.dataclass` decorator."
)
class WrongHyperparamsType(TypeError, ScalarStopException):
"""Raised when the user passes an object with the wrong type as hyperparams."""
def __init__(self, *, hyperparams, obj):
super().__init__(
f"You passed an object with the type {type(hyperparams)} as hyperparams. "
"Check with the surrounding class. You should either pass a dictionary "
"or other mapping-like object or directly pass "
f"a {obj.__class.__name__}.Hyperparams object."
)
class WrongHyperparamsKeys(TypeError, ScalarStopException):
"""Raised when the user has passed extra or missing keys for constructing hyperparams."""
def __init__(self, hyperparams, hyperparams_class):
hyperparams_class_fields = [field.name for field in fields(hyperparams_class)]
super().__init__(
"Wrong keys passed to create hyperparams. "
f"Valid keys include {hyperparams_class_fields}. "
f"The object you passed is: {hyperparams}"
)
class DataBlobNotFound(FileNotFoundError, ScalarStopException):
"""
Raised when we cannot load a :py:class:`DataBlob`
from the filesystem.
"""
def __init__(self, this_dataset_directory: str):
"""
Args:
this_dataset_directory: The directory that we went
looking for the :py:class:`DataBlob` when we
were not able to find it.
"""
super().__init__(
"Could not load a DataBlob from the filesystem at " + this_dataset_directory
)
class ModelNotFoundError(FileNotFoundError, ScalarStopException):
"""Raised when we cannot find a saved model."""
def __init__(self, directory: str):
super().__init__(f"Could not find a saved model at {directory}.")
class FileExists(FileExistsError, ScalarStopException):
"""
Raise when we want to save a :py:class:`DataBlob` but a
file exists at the save path
"""
class FileExistsDuringDataBlobCreation(FileExists):
"""
Raised when a file exists at the save path, but we think the
file was created *while* we were saving the :py:class:`DataBlob`,
which suggests that the user's code has a race conditon.
"""
class TensorFlowDatasetNotFound(FileNotFoundError, ScalarStopException):
"""
Raised when we canno load a :py:class:`tf.data.Dataset`
from the filesystem.
"""
def __init__(self, directory: str):
super().__init__(f"Could not load a tf.data.Dataset from {directory}")
class ElementSpecNotFound(FileNotFoundError, ScalarStopException):
"""
Raised when we try to guess the location of a serialized
``element_spec`` for a :py:class:`tf.data.Dataset`.
"""
def __init__(self, directory: str):
"""
Args:
directory: The directory where we were looking for
the ``element_spec`` for a :py:class:`tf.data.Dataaset`.
The TensorFlow API assumes that you have the
``element_spec`` on hand, but ScalarStop likes to
save it to disk.
"""
super().__init__(
"Could not find an element_spec for the tf.data.Dataset "
f"at {directory}. Either check your path or provide an "
"element_spec."
)
class IsNotImplemented(NotImplementedError, ScalarStopException):
"""
Raised when someone calls a function that isn't yet implemented.
"""
def __init__(self, name: str):
"""
Args:
name: The name of the function, method, or property that
was not implemented. This is useful in case the stack
trace is hard to read for some reason.
"""
super().__init__(f"Make sure to implement `{name}` in a subclass.")
class SQLite_JSON_ModeDisabled(RuntimeError, ScalarStopException):
"""
Raised when the user is running the
:py:class:`~scalarstop.TrainStore` backed by
a SQLite database, but the SQLite JSON1 extension is
unavailable.
"""
def __init__(self):
super().__init__(
"You want to start the TrainStore with a SQLite "
"database, but we could not find the SQLite JSON1 "
"extension in your Python installation."
)