-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathio.py
More file actions
285 lines (229 loc) · 9.11 KB
/
io.py
File metadata and controls
285 lines (229 loc) · 9.11 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2017-2024, Cabral, Juan
# Copyright (c) 2025, QuatroPe; Clariá, Felipe
# License: MIT
# Full Text:
# https://github.com/quatrope/feets/blob/master/LICENSE
# This code was ripped of from scikit-neuromsi on 06-nov-2024.
# https://github.com/renatoparedes/scikit-neuromsi/blob/f197a3c/skneuromsi/utils/custom_json.py
# Copyright (c) 2021-2022, Renato Paredes; Cabral, Juan
# License: BSD 3-Clause (https://tldrlegal.com/license/bsd-3-clause-license-(revised))
# All rights reserved.
# =============================================================================
# DOCS
# =============================================================================
"""Serialize and deserialize `feets.FeatureSpace` objects."""
# =============================================================================
# IMPORTS
# =============================================================================
import contextlib
import datetime as dt
import io
import json
import pathlib
import numpy as np
import yaml
from .core import FeatureSpace
# =============================================================================
# CUSTOM JSON ENCODER
# =============================================================================
class CustomJSONEncoder(json.JSONEncoder):
"""Custom JSON <https://json.org> encoder for `feets.FeatureSpace` objects.
This class extends the `json.JSONEncoder` to add support for the following
objects and types:
+----------------------------------------------------+---------------+
| Python | JSON |
+====================================================+===============+
| tuple, set, frozenset, np.ndarray | array |
+----------------------------------------------------+---------------+
| datetime | string |
+----------------------------------------------------+---------------+
| np.integer, np.floating, np.complexfloating | number |
+----------------------------------------------------+---------------+
| np.true | true |
+----------------------------------------------------+---------------+
| np.false | false |
+----------------------------------------------------+---------------+
Attributes
----------
CONVERTERS : dict
A dictionary mapping data types to their corresponding converter
functions.
See Also
--------
json.JSONEncoder :
Extensible JSON https://json.org encoder for Python data structures.
"""
CONVERTERS = (
(tuple, list),
(set, list),
(frozenset, list),
(dt.datetime, dt.datetime.isoformat),
(np.integer, int),
(np.floating, float),
(np.complexfloating, complex),
(np.bool_, bool),
(np.ndarray, np.ndarray.tolist),
)
def default(self, obj):
"""Serialize an object to a JSON-serializable format.
This method overrides the default method of the `json.JSONEncoder`
class to provide custom serialization for the data structures defined
in the `CONVERTERS` attribute, or calls the base implementation for
any other object.
Returns
-------
object
The JSON-serializable representation of the object.
Raises
------
TypeError
If the object does not match any of the types in `CONVERTERS`.
"""
for cls, converter in self.CONVERTERS:
if isinstance(obj, cls):
return converter(obj)
return super(CustomJSONEncoder, self).default(obj)
# =============================================================================
# API
# =============================================================================
@contextlib.contextmanager
def none_open_or_buffer(path_or_buffer, mode):
"""Context manager to handle file paths or buffers as file-like objects.
This function provides a unified way to handle file paths, buffers, or
in-memory buffers, and yields a file-like object for reading or writing.
Parameters
----------
path_or_buffer : str, pathlib.Path, file-like object or None
- If `str` or `pathlib.Path`, the file at this given path is opened
with the specified `mode`.
- If a file-like object, it is yielded directly.
- If `None`, an `io.StringIO` in-memory buffer is created and yielded.
mode : str
The mode in which to open the file (e.g., 'r', 'w'). This is ignored
if `path_or_buffer` is not a path.
Yields
------
file-like object
An open, ready-to-use file-like object.
"""
if path_or_buffer is None:
yield io.StringIO()
elif isinstance(path_or_buffer, (str, pathlib.Path)):
with open(path_or_buffer, mode) as fp:
yield fp
else:
yield path_or_buffer
def store_json(fspace, path_or_buffer=None, **kwargs):
"""Serialize a `feets.FeatureSpace` to a JSON formatted string or file.
Parameters
----------
fspace : feets.FeatureSpace
The `feets.FeatureSpace` object to serialize. This object must
implement a `to_dict` method that returns a serializable
representation.
path_or_buffer : str, pathlib.Path, file-like object or None, default=None
The file path, buffer, or stream to write the JSON data to.
If `None`, the JSON data is returned as a string.
**kwargs
Additional keyword arguments passed to `json.dump` when serializing
the feature space.
Returns
-------
str or None
If `path_or_buffer` is `None`, returns a JSON formatted string
representing the feature space. Otherwise, writes the JSON data to the
specified file or buffer and returns `None`.
Raises
------
TypeError
If the provided feature space contains non-serializable objects.
See Also
--------
feets.FeatureSpace :
Class to select and extract features from a time series.
read_json
json.dump
"""
data = fspace.to_dict()
kwargs.setdefault("indent", 2)
with none_open_or_buffer(path_or_buffer, "w") as fp:
json.dump(data, fp=fp, cls=CustomJSONEncoder, **kwargs)
if path_or_buffer is None:
return fp.getvalue()
def store_yaml(fspace, path_or_buffer=None, **kwargs):
"""Serialize a `feets.FeatureSpace` to a YAML formatted string or file.
Parameters
----------
fspace : feets.FeatureSpace
The `feets.FeatureSpace` object to serialize. This object must
implement a `to_dict` method that returns a serializable
representation.
path_or_buffer : str, pathlib.Path, file-like object or None, default=None
The file path, buffer, or stream to write the YAML data to.
If `None`, the YAML data is returned as a string.
**kwargs
Additional keyword arguments passed to `yaml.safe_dump` when serializing
the feature space.
Returns
-------
str or None
If `path_or_buffer` is `None`, returns a YAML formatted string
representing the feature space. Otherwise, writes the YAML data to the
specified file or buffer and returns `None`.
Raises
------
TypeError
If the provided feature space contains non-serializable objects.
See Also
--------
feets.FeatureSpace :
Class to select and extract features from a time series.
read_yaml
yaml.safe_dump
"""
json_str = store_json(fspace, path_or_buffer=None, indent=None)
data = json.loads(json_str)
with none_open_or_buffer(path_or_buffer, "w") as fp:
yaml.safe_dump(data, stream=fp, **kwargs)
if path_or_buffer is None:
return fp.getvalue()
def read_json(path_or_buffer):
"""Deserialize a JSON formatted string or file to `feets.FeatureSpace`.
Parameters
----------
path_or_buffer : str, pathlib.Path or file-like object
The file path, buffer, or stream to read the JSON data from.
Returns
-------
feets.FeatureSpace
A `feets.FeatureSpace` object containing the deserialized data.
See Also
--------
feets.FeatureSpace :
Class to select and extract features from a time series.
store_json
"""
with none_open_or_buffer(path_or_buffer, "r") as fp:
data = json.load(fp)
return FeatureSpace.from_dict(data)
def read_yaml(path_or_buffer):
"""Deserialize a YAML formatted string or file to `feets.FeatureSpace`.
Parameters
----------
path_or_buffer : str, pathlib.Path or file-like object
The file path, buffer, or stream to read the YAML data from.
Returns
-------
feets.FeatureSpace
A `feets.FeatureSpace` object containing the deserialized data.
See Also
--------
feets.FeatureSpace :
Class to select and extract features from a time series.
store_yaml
"""
with none_open_or_buffer(path_or_buffer, "r") as fp:
data = yaml.safe_load(fp)
return FeatureSpace.from_dict(data)