tensorneko-util 0.3.17__py3-none-any.whl → 0.3.19__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tensorneko_util/io/_path_conversion.py +25 -0
- tensorneko_util/io/audio/audio_reader.py +7 -4
- tensorneko_util/io/audio/audio_writer.py +9 -5
- tensorneko_util/io/hdf5/hdf5_reader.py +9 -3
- tensorneko_util/io/hdf5/hdf5_writer.py +4 -1
- tensorneko_util/io/image/image_reader.py +11 -5
- tensorneko_util/io/image/image_writer.py +13 -8
- tensorneko_util/io/json/json_reader.py +12 -7
- tensorneko_util/io/json/json_writer.py +8 -3
- tensorneko_util/io/matlab/mat_reader.py +8 -4
- tensorneko_util/io/matlab/mat_writer.py +8 -4
- tensorneko_util/io/npy/npy_reader.py +14 -8
- tensorneko_util/io/npy/npy_writer.py +19 -10
- tensorneko_util/io/pickle/pickle_reader.py +9 -3
- tensorneko_util/io/pickle/pickle_writer.py +9 -3
- tensorneko_util/io/reader.py +6 -2
- tensorneko_util/io/text/text_reader.py +11 -3
- tensorneko_util/io/text/text_writer.py +11 -3
- tensorneko_util/io/toml/toml_reader.py +9 -3
- tensorneko_util/io/toml/toml_writer.py +9 -3
- tensorneko_util/io/video/video_reader.py +15 -8
- tensorneko_util/io/video/video_writer.py +11 -6
- tensorneko_util/io/writer.py +6 -2
- tensorneko_util/io/yaml/yaml_reader.py +8 -4
- tensorneko_util/io/yaml/yaml_writer.py +9 -6
- tensorneko_util/msg/gotify.py +2 -2
- tensorneko_util/notebook/__init__.py +2 -1
- tensorneko_util/notebook/animation.py +20 -0
- tensorneko_util/notebook/display.py +6 -1
- tensorneko_util/util/__init__.py +3 -1
- tensorneko_util/util/multi_layer_indexer.py +113 -0
- tensorneko_util/version.txt +1 -1
- tensorneko_util/visualization/watcher/web/dist/assets/{index.53207489.js → index.901ba3f5.js} +47 -47
- tensorneko_util/visualization/watcher/web/dist/index.html +1 -1
- {tensorneko_util-0.3.17.dist-info → tensorneko_util-0.3.19.dist-info}/METADATA +1 -1
- {tensorneko_util-0.3.17.dist-info → tensorneko_util-0.3.19.dist-info}/RECORD +39 -36
- {tensorneko_util-0.3.17.dist-info → tensorneko_util-0.3.19.dist-info}/LICENSE +0 -0
- {tensorneko_util-0.3.17.dist-info → tensorneko_util-0.3.19.dist-info}/WHEEL +0 -0
- {tensorneko_util-0.3.17.dist-info → tensorneko_util-0.3.19.dist-info}/top_level.txt +0 -0
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import pickle
|
|
2
|
+
from typing import Union, Any
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from .._path_conversion import _path2str
|
|
2
6
|
|
|
3
7
|
|
|
4
8
|
class PickleReader:
|
|
5
9
|
|
|
6
10
|
@classmethod
|
|
7
|
-
def of(cls, path: str) ->
|
|
11
|
+
def of(cls, path: Union[str, Path]) -> Any:
|
|
8
12
|
"""
|
|
9
13
|
Save the object to a file.
|
|
10
14
|
|
|
11
15
|
Args:
|
|
12
|
-
path (``str``): The path to the file.
|
|
16
|
+
path (``str`` | ``pathlib.Path``): The path to the file.
|
|
13
17
|
"""
|
|
18
|
+
path = _path2str(path)
|
|
14
19
|
with open(path, 'rb') as f:
|
|
15
20
|
return pickle.load(f)
|
|
16
21
|
|
|
17
|
-
def __new__(cls, path: str) ->
|
|
22
|
+
def __new__(cls, path: Union[str, Path]) -> Any:
|
|
18
23
|
"""Alias to :meth:`~tensorneko_util.io.pickle.PickleReader.of`."""
|
|
24
|
+
path = _path2str(path)
|
|
19
25
|
return cls.of(path)
|
|
@@ -1,20 +1,26 @@
|
|
|
1
1
|
import pickle
|
|
2
|
+
from typing import Union
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from .._path_conversion import _path2str
|
|
2
6
|
|
|
3
7
|
|
|
4
8
|
class PickleWriter:
|
|
5
9
|
|
|
6
10
|
@classmethod
|
|
7
|
-
def to(cls, path: str, obj: object) -> None:
|
|
11
|
+
def to(cls, path: Union[str, Path], obj: object) -> None:
|
|
8
12
|
"""
|
|
9
13
|
Save the object to a file.
|
|
10
14
|
|
|
11
15
|
Args:
|
|
12
|
-
path (``str``): The path to the file.
|
|
16
|
+
path (``str`` | ``pathlib.Path``): The path to the file.
|
|
13
17
|
obj (``object``): The object to save.
|
|
14
18
|
"""
|
|
19
|
+
path = _path2str(path)
|
|
15
20
|
with open(path, 'wb') as f:
|
|
16
21
|
pickle.dump(obj, f)
|
|
17
22
|
|
|
18
|
-
def __new__(cls, path: str, obj: object) -> None:
|
|
23
|
+
def __new__(cls, path: Union[str, Path], obj: object) -> None:
|
|
19
24
|
"""Alias to :meth:`~tensorneko_util.io.pickle.PickleWriter.to`."""
|
|
25
|
+
path = _path2str(path)
|
|
20
26
|
return cls.to(path, obj)
|
tensorneko_util/io/reader.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
from typing import Type
|
|
1
|
+
from typing import Type, Union, Any
|
|
2
|
+
from pathlib import Path
|
|
2
3
|
|
|
4
|
+
from ._path_conversion import _path2str
|
|
3
5
|
from .audio import AudioReader
|
|
4
6
|
from .image import ImageReader
|
|
5
7
|
from .json import JsonReader
|
|
@@ -88,8 +90,10 @@ class Reader:
|
|
|
88
90
|
else:
|
|
89
91
|
raise ImportError("To use the toml reader, please install toml.")
|
|
90
92
|
|
|
91
|
-
def __call__(self, path: str, *args, **kwargs):
|
|
93
|
+
def __call__(self, path: Union[str, Path], *args, **kwargs) -> Any:
|
|
92
94
|
"""Automatically infer the file type and return the corresponding result. """
|
|
95
|
+
path = _path2str(path)
|
|
96
|
+
|
|
93
97
|
if path.endswith(".jpg") or path.endswith(".jpeg") or path.endswith(".png") or path.endswith(".bmp"):
|
|
94
98
|
return self.image(path, *args, **kwargs)
|
|
95
99
|
elif path.endswith(".txt"):
|
|
@@ -1,24 +1,32 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from .._path_conversion import _path2str
|
|
5
|
+
|
|
6
|
+
|
|
1
7
|
class TextReader:
|
|
2
8
|
"""TextReader for reading text file"""
|
|
3
9
|
|
|
4
10
|
@staticmethod
|
|
5
|
-
def of_plain(path: str, encoding: str = "UTF-8") -> str:
|
|
11
|
+
def of_plain(path: Union[str, Path], encoding: str = "UTF-8") -> str:
|
|
6
12
|
"""
|
|
7
13
|
Read texts of a file.
|
|
8
14
|
|
|
9
15
|
Args:
|
|
10
|
-
path (``str``): Text file path.
|
|
16
|
+
path (``str`` | ``pathlib.Path``): Text file path.
|
|
11
17
|
encoding (``str``, optional): File encoding. Default "UTF-8".
|
|
12
18
|
|
|
13
19
|
Returns:
|
|
14
20
|
``str``: The texts of given file.
|
|
15
21
|
"""
|
|
22
|
+
path = _path2str(path)
|
|
16
23
|
with open(path, "r", encoding=encoding) as file:
|
|
17
24
|
text = file.read()
|
|
18
25
|
return text
|
|
19
26
|
|
|
20
27
|
of = of_plain
|
|
21
28
|
|
|
22
|
-
def __new__(cls, path: str, encoding: str = "UTF-8") -> str:
|
|
29
|
+
def __new__(cls, path: Union[str, Path], encoding: str = "UTF-8") -> str:
|
|
23
30
|
"""Alias of :meth:`~TextReader.of"""
|
|
31
|
+
path = _path2str(path)
|
|
24
32
|
return cls.of(path, encoding)
|
|
@@ -1,21 +1,29 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from .._path_conversion import _path2str
|
|
5
|
+
|
|
6
|
+
|
|
1
7
|
class TextWriter:
|
|
2
8
|
"""TextWriter for writing files for text"""
|
|
3
9
|
|
|
4
10
|
@staticmethod
|
|
5
|
-
def to_plain(path: str, text: str, encoding: str = "UTF-8") -> None:
|
|
11
|
+
def to_plain(path: Union[str, Path], text: str, encoding: str = "UTF-8") -> None:
|
|
6
12
|
"""
|
|
7
13
|
Save as a plain text file.
|
|
8
14
|
|
|
9
15
|
Args:
|
|
10
|
-
path (``str``): The path of output file.
|
|
16
|
+
path (``str`` | ``pathlib.Path``): The path of output file.
|
|
11
17
|
text (``str``): The content for output.
|
|
12
18
|
encoding (``str``, optional): Python file IO encoding parameter. Default: "UTF-8".
|
|
13
19
|
"""
|
|
20
|
+
path = _path2str(path)
|
|
14
21
|
with open(path, "w", encoding=encoding) as file:
|
|
15
22
|
file.write(text)
|
|
16
23
|
|
|
17
24
|
to = to_plain
|
|
18
25
|
|
|
19
|
-
def __new__(cls, path: str, text: str, encoding: str = "UTF-8") -> None:
|
|
26
|
+
def __new__(cls, path: Union[str, Path], text: str, encoding: str = "UTF-8") -> None:
|
|
20
27
|
"""Alias of :meth:`~TextWriter.to`"""
|
|
28
|
+
path = _path2str(path)
|
|
21
29
|
cls.to(path, text, encoding)
|
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
import toml
|
|
2
|
+
from typing import Union
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from .._path_conversion import _path2str
|
|
2
6
|
|
|
3
7
|
|
|
4
8
|
class TomlReader:
|
|
5
9
|
|
|
6
10
|
@classmethod
|
|
7
|
-
def of(cls, path: str) -> dict:
|
|
11
|
+
def of(cls, path: Union[str, Path]) -> dict:
|
|
8
12
|
"""
|
|
9
13
|
Open a toml file.
|
|
10
14
|
|
|
11
15
|
Args:
|
|
12
|
-
path (``str``): Path to the toml file.
|
|
16
|
+
path (``str`` | ``pathlib.Path``): Path to the toml file.
|
|
13
17
|
|
|
14
18
|
Returns:
|
|
15
19
|
``dict``: The opened toml file.
|
|
16
20
|
|
|
17
21
|
"""
|
|
22
|
+
path = _path2str(path)
|
|
18
23
|
return toml.load(path)
|
|
19
24
|
|
|
20
|
-
def __new__(cls, path: str):
|
|
25
|
+
def __new__(cls, path: Union[str, Path]):
|
|
26
|
+
path = _path2str(path)
|
|
21
27
|
return cls.of(path)
|
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
import toml
|
|
2
|
+
from typing import Union
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from .._path_conversion import _path2str
|
|
2
6
|
|
|
3
7
|
|
|
4
8
|
class TomlWriter:
|
|
5
9
|
|
|
6
10
|
@classmethod
|
|
7
|
-
def to(cls, path: str, obj: dict):
|
|
11
|
+
def to(cls, path: Union[str, Path], obj: dict):
|
|
8
12
|
"""
|
|
9
13
|
Save as Toml file from a dictionary.
|
|
10
14
|
|
|
11
15
|
Args:
|
|
12
|
-
path (``str``): The path of output file.
|
|
16
|
+
path (``str`` | ``pathlib.Path``): The path of output file.
|
|
13
17
|
obj (``dict``): The toml data which need to be used for output.
|
|
14
18
|
"""
|
|
19
|
+
path = _path2str(path)
|
|
15
20
|
with open(path, "w", encoding="UTF-8") as f:
|
|
16
21
|
toml.dump(obj, f)
|
|
17
22
|
|
|
18
23
|
@classmethod
|
|
19
|
-
def __new__(cls, path: str, obj: dict):
|
|
24
|
+
def __new__(cls, path: Union[str, Path], obj: dict):
|
|
20
25
|
"""Alias of :meth:`~tensorneko.io.toml.toml_writer.TomlWriter.to`."""
|
|
26
|
+
path = _path2str(path)
|
|
21
27
|
cls.to(path, obj)
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
from itertools import islice, takewhile
|
|
2
|
-
from typing import Optional
|
|
2
|
+
from typing import Optional, Union
|
|
3
|
+
from pathlib import Path
|
|
3
4
|
|
|
4
5
|
import numpy as np
|
|
5
6
|
from einops import rearrange
|
|
6
7
|
|
|
7
8
|
from .video_data import VideoData
|
|
8
9
|
from .._default_backends import _default_video_io_backend
|
|
10
|
+
from .._path_conversion import _path2str
|
|
9
11
|
from ...backend.visual_lib import VisualLib
|
|
10
12
|
from ...util import dispatch
|
|
11
13
|
|
|
@@ -13,12 +15,12 @@ from ...util import dispatch
|
|
|
13
15
|
class VideoReader:
|
|
14
16
|
|
|
15
17
|
@classmethod
|
|
16
|
-
def of(cls, path: str, channel_first: bool = True, backend: Optional[VisualLib] = None) -> VideoData:
|
|
18
|
+
def of(cls, path: Union[str, Path], channel_first: bool = True, backend: Optional[VisualLib] = None) -> VideoData:
|
|
17
19
|
"""
|
|
18
20
|
Read video array from given file.
|
|
19
21
|
|
|
20
22
|
Args:
|
|
21
|
-
path (``str``): Path to the video file.
|
|
23
|
+
path (``str`` | ``pathlib.Path``): Path to the video file.
|
|
22
24
|
channel_first (``bool``, optional): Get image dimension (T, H, W, C) if False or (T, C, H, W) if True.
|
|
23
25
|
Default: True.
|
|
24
26
|
backend (:class:`~tensorneko.backend.visual_lib.VisualLib`, optional): VisualLib backend.
|
|
@@ -30,6 +32,7 @@ class VideoReader:
|
|
|
30
32
|
an audio tensor of (T, C) and a :class:`~tensorneko.io.video.video_data.VideoInfo` contains fps info.
|
|
31
33
|
"""
|
|
32
34
|
backend = backend or _default_video_io_backend()
|
|
35
|
+
path = _path2str(path)
|
|
33
36
|
if backend == VisualLib.OPENCV:
|
|
34
37
|
if not VisualLib.opencv_available():
|
|
35
38
|
raise ValueError("OpenCV is not installed.")
|
|
@@ -83,13 +86,13 @@ class VideoReader:
|
|
|
83
86
|
raise ValueError("Unknown backend: {}".format(backend))
|
|
84
87
|
|
|
85
88
|
@classmethod
|
|
86
|
-
def with_indexes(cls, path: str, indexes: np.ndarray,
|
|
89
|
+
def with_indexes(cls, path: Union[str, Path], indexes: np.ndarray,
|
|
87
90
|
channel_first: bool = True, backend: Optional[VisualLib] = None
|
|
88
91
|
) -> VideoData:
|
|
89
92
|
"""
|
|
90
93
|
Get a video frames with indexes. The audio will be ignored.
|
|
91
94
|
Args:
|
|
92
|
-
path (``str``): Path to the video file.
|
|
95
|
+
path (``str`` | ``pathlib.Path``): Path to the video file.
|
|
93
96
|
indexes (``np.ndarray``): Indexes of the video.
|
|
94
97
|
channel_first (``bool``, optional): Get image dimension (T, H, W, C) if False or (T, C, H, W) if True.
|
|
95
98
|
Default: True.
|
|
@@ -102,6 +105,7 @@ class VideoReader:
|
|
|
102
105
|
an audio tensor of (T, C) and a :class:`~tensorneko.io.video.video_data.VideoInfo` contains fps info.
|
|
103
106
|
"""
|
|
104
107
|
backend = backend or _default_video_io_backend()
|
|
108
|
+
path = _path2str(path)
|
|
105
109
|
if backend == VisualLib.OPENCV:
|
|
106
110
|
if not VisualLib.opencv_available():
|
|
107
111
|
raise ValueError("OpenCV is not installed.")
|
|
@@ -176,10 +180,11 @@ class VideoReader:
|
|
|
176
180
|
|
|
177
181
|
@classmethod
|
|
178
182
|
@dispatch
|
|
179
|
-
def with_range(cls, path: str, start: int, end: int, step: int, channel_first: bool = True,
|
|
183
|
+
def with_range(cls, path: Union[str, Path], start: int, end: int, step: int, channel_first: bool = True,
|
|
180
184
|
backend: Optional[VisualLib] = None
|
|
181
185
|
) -> VideoData:
|
|
182
186
|
backend = backend or _default_video_io_backend()
|
|
187
|
+
path = _path2str(path)
|
|
183
188
|
if backend == VisualLib.PYTORCH:
|
|
184
189
|
if not VisualLib.pytorch_available():
|
|
185
190
|
raise ValueError("Torchvision is not installed.")
|
|
@@ -202,11 +207,13 @@ class VideoReader:
|
|
|
202
207
|
|
|
203
208
|
@classmethod
|
|
204
209
|
@dispatch
|
|
205
|
-
def with_range(cls, path: str, end: int, channel_first: bool = True,
|
|
210
|
+
def with_range(cls, path: Union[str, Path], end: int, channel_first: bool = True,
|
|
206
211
|
backend: Optional[VisualLib] = None
|
|
207
212
|
) -> VideoData:
|
|
213
|
+
path = _path2str(path)
|
|
208
214
|
return cls.with_range(path, 0, end, 1, channel_first, backend)
|
|
209
215
|
|
|
210
|
-
def __new__(cls, path: str, channel_first: bool = False, backend: Optional[VisualLib] = None) -> VideoData:
|
|
216
|
+
def __new__(cls, path: Union[str, Path], channel_first: bool = False, backend: Optional[VisualLib] = None) -> VideoData:
|
|
211
217
|
"""Alias of :meth:`~.VideoReader.of`"""
|
|
218
|
+
path = _path2str(path)
|
|
212
219
|
return cls.of(path, channel_first, backend)
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import pathlib
|
|
2
2
|
import warnings
|
|
3
3
|
from subprocess import Popen
|
|
4
|
-
from typing import
|
|
4
|
+
from typing import Union
|
|
5
|
+
from pathlib import Path
|
|
5
6
|
|
|
6
7
|
import numpy as np
|
|
7
8
|
from einops import rearrange
|
|
8
9
|
|
|
9
10
|
from .video_data import VideoData
|
|
10
11
|
from .._default_backends import _default_video_io_backend
|
|
12
|
+
from .._path_conversion import _path2str
|
|
11
13
|
from ...backend.visual_lib import VisualLib
|
|
12
14
|
from ...util import dispatch
|
|
13
15
|
from ...util.type import T_ARRAY
|
|
@@ -18,14 +20,14 @@ class VideoWriter:
|
|
|
18
20
|
|
|
19
21
|
@classmethod
|
|
20
22
|
@dispatch
|
|
21
|
-
def to(cls, path: str, video: VideoData, audio_codec: str = None, channel_first: bool = False,
|
|
23
|
+
def to(cls, path: Union[str, Path], video: VideoData, audio_codec: str = None, channel_first: bool = False,
|
|
22
24
|
backend: VisualLib = None
|
|
23
25
|
) -> None:
|
|
24
26
|
"""
|
|
25
27
|
Write to video file from :class:`~tensorneko.io.video.video_data.VideoData`.
|
|
26
28
|
|
|
27
29
|
Args:
|
|
28
|
-
path (``str``): The path of output file.
|
|
30
|
+
path (``str`` | ``pathlib.Path``): The path of output file.
|
|
29
31
|
video (:class:`~tensorneko.io.video.video_data.VideoData`): The VideoData object for output.
|
|
30
32
|
audio_codec (``str``, optional): The audio codec if audio is required. Default: None.
|
|
31
33
|
channel_first (``bool``, optional): Get image dimension (T, H, W, C) if False or (T, C, H, W) if True.
|
|
@@ -33,11 +35,12 @@ class VideoWriter:
|
|
|
33
35
|
backend (:class:`~tensorneko.backend.visual_lib.VisualLib`, optional): VisualLib backend.
|
|
34
36
|
Default: opencv if installed, then torchvision if installed, then ffmpeg if available.
|
|
35
37
|
"""
|
|
38
|
+
path = _path2str(path)
|
|
36
39
|
cls.to(path, video.video, video.info.video_fps, video.audio, video.info.audio_fps, audio_codec, channel_first, backend)
|
|
37
40
|
|
|
38
41
|
@classmethod
|
|
39
42
|
@dispatch
|
|
40
|
-
def to(cls, path: str, video: T_ARRAY, video_fps: float, audio: T_ARRAY = None,
|
|
43
|
+
def to(cls, path: Union[str, Path], video: T_ARRAY, video_fps: float, audio: T_ARRAY = None,
|
|
41
44
|
audio_fps: int = None, audio_codec: str = None, channel_first: bool = False,
|
|
42
45
|
backend: VisualLib = None
|
|
43
46
|
) -> None:
|
|
@@ -45,7 +48,7 @@ class VideoWriter:
|
|
|
45
48
|
Write to video file from :class:`~torch.Tensor` or :class:`~numpy.ndarray` with (T, C, H, W).
|
|
46
49
|
TODO: Buggy when the argument is too much.
|
|
47
50
|
Args:
|
|
48
|
-
path (``str``): The path of output file.
|
|
51
|
+
path (``str`` | ``pathlib.Path``): The path of output file.
|
|
49
52
|
video (:class:`~torch.Tensor` | :class:`~numpy.ndarray`): The video tensor or array with (T, C, H, W) for
|
|
50
53
|
output.
|
|
51
54
|
video_fps (``float``): The video fps.
|
|
@@ -62,6 +65,7 @@ class VideoWriter:
|
|
|
62
65
|
if audio_codec is None:
|
|
63
66
|
raise ValueError("audio_codec is required if audio is required.")
|
|
64
67
|
backend = backend or _default_video_io_backend()
|
|
68
|
+
path = _path2str(path)
|
|
65
69
|
if channel_first:
|
|
66
70
|
video = rearrange(video, "t c h w -> t h w c")
|
|
67
71
|
|
|
@@ -130,6 +134,7 @@ class VideoWriter:
|
|
|
130
134
|
else:
|
|
131
135
|
raise ValueError("Unknown backend. Should be one of VisualLib.OPENCV, VisualLib.PYTORCH, VisualLib.FFMPEG.")
|
|
132
136
|
|
|
133
|
-
def __new__(cls, path: str, video: Union[T_ARRAY, VideoData], *args, **kwargs):
|
|
137
|
+
def __new__(cls, path: Union[str, Path], video: Union[T_ARRAY, VideoData], *args, **kwargs):
|
|
134
138
|
"""Alias of :func:`~tensorneko.io.video.video_io.to`."""
|
|
139
|
+
path = _path2str(path)
|
|
135
140
|
return cls.to(path, video, *args, **kwargs)
|
tensorneko_util/io/writer.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
from typing import Type
|
|
1
|
+
from typing import Type, Union
|
|
2
|
+
from pathlib import Path
|
|
2
3
|
|
|
4
|
+
from ._path_conversion import _path2str
|
|
3
5
|
from .audio import AudioWriter
|
|
4
6
|
from .image import ImageWriter
|
|
5
7
|
from .json import JsonWriter
|
|
@@ -88,8 +90,10 @@ class Writer:
|
|
|
88
90
|
else:
|
|
89
91
|
raise ImportError("To use the toml writer, please install toml.")
|
|
90
92
|
|
|
91
|
-
def __call__(self, path: str, obj, *args, **kwargs):
|
|
93
|
+
def __call__(self, path: Union[str, Path], obj, *args, **kwargs):
|
|
92
94
|
"""Automatically infer the file type and return the corresponding result. """
|
|
95
|
+
path = _path2str(path)
|
|
96
|
+
|
|
93
97
|
if path.endswith(".jpg") or path.endswith(".jpeg") or path.endswith(".png") or path.endswith(".bmp"):
|
|
94
98
|
self.image(path, obj, *args, **kwargs)
|
|
95
99
|
elif path.endswith(".txt"):
|
|
@@ -1,27 +1,31 @@
|
|
|
1
1
|
from typing import Type, Dict, Any, Union
|
|
2
|
-
|
|
2
|
+
from pathlib import Path
|
|
3
3
|
import yaml
|
|
4
4
|
from yaml import Loader as Loader_, BaseLoader, FullLoader, SafeLoader, UnsafeLoader
|
|
5
5
|
|
|
6
|
+
from .._path_conversion import _path2str
|
|
7
|
+
|
|
6
8
|
L = Type[Union[Loader_, BaseLoader, FullLoader, SafeLoader, UnsafeLoader]]
|
|
7
9
|
|
|
8
10
|
|
|
9
11
|
class YamlReader:
|
|
10
12
|
|
|
11
13
|
@classmethod
|
|
12
|
-
def of(cls, path: str, Loader: L = Loader_) -> Dict[str, Any]:
|
|
14
|
+
def of(cls, path: Union[str, Path], Loader: L = Loader_) -> Dict[str, Any]:
|
|
13
15
|
"""
|
|
14
16
|
Load a YAML file and return a Python dict.
|
|
15
17
|
Args:
|
|
16
|
-
path (``str``): YAML file path.
|
|
18
|
+
path (``str`` | ``pathlib.Path``): YAML file path.
|
|
17
19
|
Loader: (``Loader_ | BaseLoader | FullLoader | SafeLoader | UnsafeLoader``, optional): The loader for yaml. Default: :class:`yaml.Loader`
|
|
18
20
|
|
|
19
21
|
Returns:
|
|
20
22
|
``dict``: The Python dict of given YAML file.
|
|
21
23
|
"""
|
|
24
|
+
path = _path2str(path)
|
|
22
25
|
with open(path, "r") as file:
|
|
23
26
|
return yaml.load(file, Loader=Loader)
|
|
24
27
|
|
|
25
|
-
def __new__(cls, path: str, Loader: L = Loader_):
|
|
28
|
+
def __new__(cls, path: Union[str, Path], Loader: L = Loader_):
|
|
26
29
|
"""Alias of :meth:`~tensorneko_util.io.yaml.yaml_reader.YamlReader.of`."""
|
|
30
|
+
path = _path2str(path)
|
|
27
31
|
return cls.of(path, Loader)
|
|
@@ -1,23 +1,26 @@
|
|
|
1
|
-
from typing import Any
|
|
2
|
-
|
|
1
|
+
from typing import Any, Union
|
|
2
|
+
from pathlib import Path
|
|
3
3
|
import yaml
|
|
4
4
|
|
|
5
|
+
from .._path_conversion import _path2str
|
|
6
|
+
|
|
5
7
|
|
|
6
8
|
class YamlWriter:
|
|
7
9
|
|
|
8
10
|
@classmethod
|
|
9
|
-
def to(cls, path: str, obj: Any) -> None:
|
|
11
|
+
def to(cls, path: Union[str, Path], obj: Any) -> None:
|
|
10
12
|
"""
|
|
11
13
|
Write a Python object to a YAML file.
|
|
12
14
|
|
|
13
15
|
Args:
|
|
14
|
-
path (``str``): YAML file path.
|
|
16
|
+
path (``str`` | ``pathlib.Path``): YAML file path.
|
|
15
17
|
obj (``Any``): The Python object to write.
|
|
16
18
|
"""
|
|
19
|
+
path = _path2str(path)
|
|
17
20
|
with open(path, "w") as f:
|
|
18
21
|
yaml.dump(obj, f)
|
|
19
22
|
|
|
20
|
-
def __new__(cls, path: str, obj: Any) -> None:
|
|
23
|
+
def __new__(cls, path: Union[str, Path], obj: Any) -> None:
|
|
21
24
|
"""Alias of :meth:`~tensorneko_util.io.yaml.yaml_writer.YamlWriter.to`."""
|
|
25
|
+
path = _path2str(path)
|
|
22
26
|
cls.to(path, obj)
|
|
23
|
-
return None
|
tensorneko_util/msg/gotify.py
CHANGED
|
@@ -16,7 +16,7 @@ def push_gotify(message: str, url: Optional[str] = None, token: Optional[str] =
|
|
|
16
16
|
message (``str``): The message to be sent.
|
|
17
17
|
url (``str``, optional): The URL of the Gotify server. If not provided, it will be read from the environment variable GOTIFY_URL.
|
|
18
18
|
token (``str``, optional): The token for the Gotify server. If not provided, it will be read from the environment variable GOTIFY_TOKEN.
|
|
19
|
-
title (``str``, optional): The title of the message. Default is the hostname of the machine.
|
|
19
|
+
title (``str``, optional): The title of the message. Default is GOTIFY_TITLE then the hostname of the machine.
|
|
20
20
|
priority (``int``, optional): The priority of the message. Default is 0.
|
|
21
21
|
|
|
22
22
|
Examples::
|
|
@@ -26,7 +26,7 @@ def push_gotify(message: str, url: Optional[str] = None, token: Optional[str] =
|
|
|
26
26
|
# title = "<HOST_NAME>", message = "This is a test message", priority = 0
|
|
27
27
|
|
|
28
28
|
"""
|
|
29
|
-
title = title or socket.gethostname()
|
|
29
|
+
title = title or os.environ.get("GOTIFY_TITLE") or socket.gethostname()
|
|
30
30
|
url = url or os.environ.get("GOTIFY_URL")
|
|
31
31
|
if url is None:
|
|
32
32
|
raise ValueError("URL is not provided. Input url argument or set the environment variable GOTIFY_URL.")
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from typing import Sequence, Union
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from matplotlib import pyplot as plt
|
|
5
|
+
from matplotlib.animation import FuncAnimation
|
|
6
|
+
from IPython.display import HTML
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def to_animation_html(frames: Union[Sequence[np.ndarray], np.ndarray], interval: int = 100) -> str:
|
|
10
|
+
fig, ax = plt.subplots()
|
|
11
|
+
im = ax.imshow(frames[0])
|
|
12
|
+
def update_frame(i):
|
|
13
|
+
im.set_array(frames[i])
|
|
14
|
+
return [im]
|
|
15
|
+
|
|
16
|
+
return FuncAnimation(fig, update_frame, frames=len(frames), interval=interval).to_jshtml()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def show_animation(frames: Union[Sequence[np.ndarray], np.ndarray], interval: int = 100):
|
|
20
|
+
return HTML(to_animation_html(frames, interval))
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import IPython
|
|
2
|
-
from IPython.display import Audio, Video, YouTubeVideo, Code, Image
|
|
2
|
+
from IPython.display import Audio, Video, YouTubeVideo, Code, Image, HTML
|
|
3
3
|
import sys
|
|
4
4
|
|
|
5
5
|
|
|
@@ -33,6 +33,10 @@ def code(path: str, language: str = None):
|
|
|
33
33
|
IPython.display.Code._repr_html_ = _jupyterlab_repr_html_
|
|
34
34
|
return IPython.display.display(Code(path, language=language))
|
|
35
35
|
|
|
36
|
+
def html(source: str):
|
|
37
|
+
# render HTML in Jupyter
|
|
38
|
+
return IPython.display.HTML(source)
|
|
39
|
+
|
|
36
40
|
|
|
37
41
|
class Display:
|
|
38
42
|
audio = staticmethod(audio)
|
|
@@ -40,6 +44,7 @@ class Display:
|
|
|
40
44
|
image = staticmethod(image)
|
|
41
45
|
youtube_video = staticmethod(youtube_video)
|
|
42
46
|
code = staticmethod(code)
|
|
47
|
+
html = staticmethod(html)
|
|
43
48
|
|
|
44
49
|
def __call__(self, path: str, *args, **kwargs):
|
|
45
50
|
ext = path.split(".")[-1]
|
tensorneko_util/util/__init__.py
CHANGED
|
@@ -12,6 +12,7 @@ from .eventbus import Event, EventBus, EventHandler, subscribe
|
|
|
12
12
|
from .singleton import Singleton
|
|
13
13
|
from .downloader import download_file, download_file_thread, download_files_thread
|
|
14
14
|
from .window_merger import WindowMerger
|
|
15
|
+
from .multi_layer_indexer import MultiLayerIndexer
|
|
15
16
|
|
|
16
17
|
tensorneko_util_path = get_tensorneko_util_path()
|
|
17
18
|
|
|
@@ -56,5 +57,6 @@ __all__ = [
|
|
|
56
57
|
"download_file_thread",
|
|
57
58
|
"download_files_thread",
|
|
58
59
|
"WindowMerger",
|
|
59
|
-
"Registry"
|
|
60
|
+
"Registry",
|
|
61
|
+
"MultiLayerIndexer"
|
|
60
62
|
]
|