-
Notifications
You must be signed in to change notification settings - Fork 61
/
file.py
286 lines (225 loc) 路 10.1 KB
/
file.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#
# Copyright (c) 2022, Neptune Labs Sp. z o.o.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
from io import IOBase
from typing import TYPE_CHECKING, Optional, TypeVar, Union
from neptune.new.internal.utils import get_stream_content, limits, verify_type
from neptune.new.internal.utils.images import (
get_html_content,
get_image_content,
get_pickle_content,
is_altair_chart,
is_bokeh_figure,
is_matplotlib_figure,
is_numpy_array,
is_pandas_dataframe,
is_pil_image,
is_plotly_figure,
)
from neptune.new.types.atoms.atom import Atom
if TYPE_CHECKING:
from neptune.new.types.value_visitor import ValueVisitor
Ret = TypeVar("Ret")
class File(Atom):
def __init__(
self,
path: Optional[str] = None,
content: Optional[bytes] = None,
extension: Optional[str] = None,
):
verify_type("path", path, (str, type(None)))
verify_type("content", content, (bytes, type(None)))
verify_type("extension", extension, (str, type(None)))
if path is not None and content is not None:
raise ValueError("path and content are mutually exclusive")
if path is None and content is None:
raise ValueError("path or content is required")
self.path = path
self.content = content
if extension is None and path is not None:
try:
ext = os.path.splitext(path)[1]
self.extension = ext[1:] if ext else ""
except ValueError:
self.extension = ""
else:
self.extension = extension or ""
def accept(self, visitor: "ValueVisitor[Ret]") -> Ret:
return visitor.visit_file(self)
def __str__(self):
if self.path is not None:
return "File(path={})".format(str(self.path))
else:
return "File(content=...)"
@staticmethod
def from_content(content: Union[str, bytes], extension: Optional[str] = None) -> "File":
"""Factory method for creating File value objects directly from binary and text content.
In the case of text content, UTF-8 encoding will be used.
Args:
content (str or bytes): Text or binary content to stored in the `File` value object.
extension (str, optional, default is None): Extension of the created file.
File will be used for interpreting the type of content for visualization.
If `None` it will be bin for binary content and txt for text content.
Defaults to `None`.
Returns:
``File``: value object created from the content
You may also want to check `from_content docs page`_.
.. _from_content docs page:
https://docs.neptune.ai/api-reference/field-types#.from_content
"""
if isinstance(content, str):
ext = "txt"
content = content.encode("utf-8")
else:
ext = "bin"
if limits.file_size_exceeds_limit(len(content)):
content = b""
return File(content=content, extension=extension or ext)
@staticmethod
def from_stream(
stream: IOBase, seek: Optional[int] = 0, extension: Optional[str] = None
) -> "File":
"""Factory method for creating File value objects directly from binary and text streams.
In the case of text stream, UTF-8 encoding will be used.
Args:
stream (IOBase): Stream to be converted.
seek (int, optional): See IOBase documentation.
Defaults to `0`.
extension (str, optional): Extension of the file created that will be used for interpreting the type
of content for visualization.
If `None` it will be bin for binary stream and txt for text stream.
Defaults to `None`.
Returns:
``File``: value object created from the stream.
You may also want to check `from_stream docs page`_ and `IOBase documentation`_.
.. _from_stream docs page:
https://docs.neptune.ai/api-reference/field-types#.from_stream
.. _IOBase documentation:
https://docs.python.org/3/library/io.html#io.IOBase
"""
verify_type("stream", stream, IOBase)
content, stream_default_ext = get_stream_content(stream, seek)
return File(content=content, extension=extension or stream_default_ext)
@staticmethod
def as_image(image) -> "File":
"""Static method for converting image objects or image-like objects to an image File value object.
This way you can upload `Matplotlib` figures, `PIL` images, `NumPy` arrays, as static images.
Args:
image: Image-like object to be converted.
Supported are `PyTorch` tensors, `TensorFlow/Keras` tensors, `NumPy` arrays, `PIL` images
and `Matplotlib` figures.
Returns:
``File``: value object with converted image
Examples:
>>> import neptune.new as neptune
>>> from neptune.new.types import File
>>> run = neptune.init()
Convert NumPy array to File value object and upload it
>>> run["train/prediction_example"].upload(File.as_image(numpy_array))
Convert PIL image to File value object and upload it
>>> pil_file = File.as_image(pil_image)
>>> run["dataset/data_sample/img1"].upload(pil_file)
You can upload PIL image without explicit conversion
>>> run["dataset/data_sample/img2"].upload(pil_image)
You may also want to check `as_image docs page`_.
.. _as_image docs page:
https://docs.neptune.ai/api-reference/field-types#.as_image
"""
content_bytes = get_image_content(image)
return File.from_content(
content_bytes if content_bytes is not None else b"", extension="png"
)
@staticmethod
def as_html(chart) -> "File":
"""Converts an object to an HTML File value object.
This way you can upload `Altair`, `Bokeh`, `Plotly`, `Matplotlib` interactive charts
or upload directly `Pandas` `DataFrame` objects to explore them in Neptune UI.
Args:
chart: An object to be converted.
Supported are `Altair`, `Bokeh`, `Plotly`, `Matplotlib` interactive charts,
and `Pandas` `DataFrame` objects.
Returns:
``File``: value object with converted object.
Examples:
>>> import neptune.new as neptune
>>> from neptune.new.types import File
>>> run = neptune.init()
Convert Pandas DataFrame to File value object and upload it
>>> run["train/results"].upload(File.as_html(df_predictions))
Convert Altair interactive chart to File value object and upload it
>>> altair_file = File.as_html(altair_chart)
>>> run["dataset/data_sample/img1"].upload(altair_file)
You can upload Altair interactive chart without explicit conversion
>>> run["dataset/data_sample/img2"].upload(altair_chart)
You may also want to check `as_html docs page`_.
.. _as_html docs page:
https://docs.neptune.ai/api-reference/field-types#.as_html
"""
content = get_html_content(chart)
return File.from_content(content if content is not None else "", extension="html")
@staticmethod
def as_pickle(obj) -> "File":
"""Pickles a Python object and stores it in `File` value object.
This way you can upload any Python object for future use.
Args:
obj: An object to be converted.
Supported are `Altair`, `Bokeh`, `Plotly`, `Matplotlib` interactive charts,
and `Pandas` `DataFrame` objects.
Returns:
``File``: value object with pickled object.
Examples:
>>> import neptune.new as neptune
>>> from neptune.new.types import File
>>> run = neptune.init()
Pickle model object and upload it
>>> run["results/pickled_model"].upload(File.as_pickle(trained_model))
You may also want to check `as_pickle docs page`_.
.. _as_pickle docs page:
https://docs.neptune.ai/api-reference/field-types#.as_pickle
"""
content = get_pickle_content(obj)
return File.from_content(content if content is not None else b"", extension="pkl")
@staticmethod
def create_from(value) -> "File":
if isinstance(value, str):
return File(path=value)
elif is_pil_image(value) or is_matplotlib_figure(value):
return File.as_image(value)
elif is_plotly_figure(value) or is_altair_chart(value) or is_bokeh_figure(value):
return File.as_html(value)
elif is_numpy_array(value):
raise TypeError(
"Value of type {} is not supported. Please use File.as_image().".format(type(value))
)
elif is_pandas_dataframe(value):
raise TypeError(
"Value of type {} is not supported. Please use File.as_html().".format(type(value))
)
elif isinstance(value, File):
return value
raise TypeError("Value of type {} is not supported.".format(type(value)))
@staticmethod
def is_convertable(value):
return (
is_pil_image(value)
or is_matplotlib_figure(value)
or is_plotly_figure(value)
or is_altair_chart(value)
or is_bokeh_figure(value)
or is_numpy_array(value)
or is_pandas_dataframe(value)
or isinstance(value, File)
)