1
1
import json
2
2
import yaml
3
+ import bson
3
4
import sys
4
5
5
6
import numpy as np
23
24
24
25
from cattr .gen import make_dict_unstructure_fn , override
25
26
from cattr .preconf .pyyaml import make_converter as make_yaml_converter
27
+ from cattr .preconf .bson import make_converter as make_bson_converter
26
28
27
29
from tabulate import tabulate
28
30
@@ -72,7 +74,7 @@ def print_v(text: str):
72
74
# A setup an additional converter to apply when we are serializing using PyYAML.
73
75
# This handles things like tuples as lists.
74
76
yaml_converter = make_yaml_converter ()
75
-
77
+ bson_converter = make_bson_converter ()
76
78
77
79
# A simple converter that handles only value expressions.
78
80
value_expr_converter = cattr .Converter ()
@@ -104,6 +106,12 @@ def to_json(self) -> str:
104
106
"""
105
107
return json .dumps (self .to_dict (), indent = 4 )
106
108
109
+ def to_bson (self ) -> str :
110
+ """
111
+ Convert the Base object to a BSON string representation.
112
+ """
113
+ return bson .encode (self .to_dict ())
114
+
107
115
@classmethod
108
116
def from_dict (cls , d : Dict [str , Any ]) -> "Base" :
109
117
"""Instantiate an Base object from a dictionary"""
@@ -130,6 +138,11 @@ def from_json(cls, json_str: str) -> "Base":
130
138
"""Instantiate an modelspec object from a JSON string"""
131
139
return cls .from_dict (json .loads (json_str ))
132
140
141
+ @classmethod
142
+ def from_bson (cls , bson_str : str ) -> "Base" :
143
+ """Instantiate an modelspec object from a BSON string"""
144
+ return cls .from_dict (bson .decode (bson_str ))
145
+
133
146
def to_json_file (
134
147
self , filename : Optional [str ] = None , include_metadata : bool = True
135
148
) -> str :
@@ -155,6 +168,28 @@ def to_json_file(
155
168
156
169
return filename
157
170
171
+ def to_bson_file (self , filename : str , include_metadata : bool = True ) -> str :
172
+ """Convert modelspec format to bson format
173
+
174
+ Args:
175
+ filename: File in modelspec format (Filename extension: .bson )
176
+ include_metadata: Contains contact information, citations, acknowledgements, pointers to sample data,
177
+ benchmark results, and environments in which the specified model was originally implemented
178
+ Returns:
179
+ The name of the generated bson file
180
+ """
181
+
182
+ if filename is None :
183
+ filename = f"{ self .id } .bson"
184
+
185
+ with open (filename , "wb" ) as outfile :
186
+ bson_data = bson .encode (
187
+ bson_converter .unstructure (self .to_dict ()),
188
+ )
189
+ outfile .write (bson_data )
190
+
191
+ return filename
192
+
158
193
def to_yaml (self , include_metadata : bool = True ) -> str :
159
194
"""
160
195
Convert the Base object to a YAML dictionary representation.
@@ -211,6 +246,8 @@ def from_file(cls, filename: str) -> "Base":
211
246
return cls .from_yaml_file (filename )
212
247
elif filename .endswith (".json" ):
213
248
return cls .from_json_file (filename )
249
+ elif filename .endswith (".bson" ):
250
+ return cls .from_bson_file (filename )
214
251
else :
215
252
raise ValueError (
216
253
f"Cannot auto-detect modelspec serialization format from filename ({ filename } ). The filename "
@@ -232,6 +269,22 @@ def from_json_file(cls, filename: str) -> "Base":
232
269
d = json .load (infile )
233
270
return cls .from_dict (d )
234
271
272
+ @classmethod
273
+ def from_bson_file (cls , filename : str ) -> "Base" :
274
+ """
275
+ Create a :class:`.Base` from its BSON representation stored in a file.
276
+
277
+ Args:
278
+ filename: The file from which to load the BSON data.
279
+
280
+ Returns:
281
+ An modelspec :class:`.Base` for this BSON
282
+ """
283
+ with open ("document.bson" ,'rb' ) as infile :
284
+ data_encoded = infile .read ()
285
+ d = bson .decode (data_encoded )
286
+ return cls .from_dict (d )
287
+
235
288
@classmethod
236
289
def from_yaml_file (cls , filename : str ) -> "Base" :
237
290
"""
0 commit comments