MCPcopy Create free account
hub / github.com/Derive-Risk/pyfair / read_json

Method read_json

pyfair/model/meta_model.py:94–146  ·  view source on GitHub ↗

Static method to create a metamodel from a JSON string Parameters ---------- json_data : str a UTF-8 encoded JSON string containing model data Returns ------- pyfair.model.FairMetaModel A meta model instance using the JSON par

(json_data)

Source from the content-addressed store, hash-verified

92
93 @staticmethod
94 def read_json(json_data):
95 """Static method to create a metamodel from a JSON string
96
97 Parameters
98 ----------
99 json_data : str
100 a UTF-8 encoded JSON string containing model data
101
102 Returns
103 -------
104 pyfair.model.FairMetaModel
105 A meta model instance using the JSON parameters supplied
106
107 Raises
108 ------
109 pyfair.fair_exception.FairException
110 If model JSON is supplied erroneously
111
112 Example
113 -------
114 >>> with open('serialized_metamodel.json') as f:
115 ... json_text = f.read()
116 ...
117 >>> metamodel = pyfair.model.FairMetaModel.read_json(json_text)
118
119 """
120 data = json.loads(json_data)
121 # Check type of JSON
122 if data['type'] != 'FairMetaModel':
123 raise FairException('Failed JSON parse attempt. This is not a FairMetaModel.')
124 # Get model params
125 model_params = {
126 key: value
127 for key, value
128 in data.items()
129 if key not in ['name', 'model_uuid', 'type', 'creation_date']
130 }
131 # Instantiate models (there is no need to cover)
132 # metamodels here because metamodel json only
133 # has models in it, not metamodels.
134 models = [
135 FairModel.read_json(json.dumps(value))
136 for value
137 in model_params.values()
138 ]
139 # Create metamodel
140 meta_model = FairMetaModel(
141 name=data['name'],
142 models=models,
143 model_uuid=data['model_uuid'],
144 creation_date=data['creation_date']
145 )
146 return meta_model
147
148 def _load_model(self, model):
149 """Loads an individual model into the metamodel"""

Callers 1

_load_meta_modelMethod · 0.45

Calls 2

FairExceptionClass · 0.85
FairMetaModelClass · 0.85

Tested by

no test coverage detected