easyvvuq.decoders.json

A basic JSON format decoder.

Will read a JSON file and will output select values. Values have to be either numeric or lists. In case of lists it will treat those as vector-valued quantities of interest.

 1"""A basic JSON format decoder.
 2
 3Will read a JSON file and will output select values. Values have to be either
 4numeric or lists. In case of lists it will treat those as vector-valued quantities
 5of interest.
 6"""
 7
 8import os
 9import logging
10from easyvvuq import OutputType
11import json
12
13__copyright__ = """
14
15    Copyright 2018 Robin A. Richardson, David W. Wright
16
17    This file is part of EasyVVUQ
18
19    EasyVVUQ is free software: you can redistribute it and/or modify
20    it under the terms of the Lesser GNU General Public License as published by
21    the Free Software Foundation, either version 3 of the License, or
22    (at your option) any later version.
23
24    EasyVVUQ is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27    Lesser GNU General Public License for more details.
28
29    You should have received a copy of the Lesser GNU General Public License
30    along with this program.  If not, see <https://www.gnu.org/licenses/>.
31
32"""
33__license__ = "LGPL"
34
35
36logger = logging.Logger(__name__)
37
38
39class JSONDecoder:
40    def __init__(self, target_filename, output_columns):
41        if len(output_columns) == 0:
42            msg = "output_columns cannot be empty."
43            logger.error(msg)
44            raise RuntimeError(msg)
45        self.target_filename = target_filename
46        self.output_columns = output_columns
47        self.output_type = OutputType('sample')
48
49    @staticmethod
50    def _get_output_path(run_info=None, outfile=None):
51        run_path = run_info['run_dir']
52        if not os.path.isdir(run_path):
53            raise RuntimeError(f"Run directory does not exist: {run_path}")
54        return os.path.join(run_path, outfile)
55
56    def parse_sim_output(self, run_info={}):
57        def get_value(data, path):
58            for node in path:
59                data = data[node]
60            return data
61        out_path = self._get_output_path(run_info, self.target_filename)
62        raw_data = self._get_raw_data(out_path)
63        data = []
64        for col in self.output_columns:
65            try:
66                if isinstance(col, str):
67                    data.append((col, raw_data[col]))
68                elif isinstance(col, list):
69                    data.append(('.'.join(col), get_value(raw_data, col)))
70            except KeyError:
71                raise RuntimeError("no such field: {} in this json file".format(col))
72        return dict(data)
73
74    def _get_raw_data(self, out_path):
75        with open(out_path) as fd:
76            return json.load(fd)
logger = <Logger easyvvuq.decoders.json (NOTSET)>
class JSONDecoder:
40class JSONDecoder:
41    def __init__(self, target_filename, output_columns):
42        if len(output_columns) == 0:
43            msg = "output_columns cannot be empty."
44            logger.error(msg)
45            raise RuntimeError(msg)
46        self.target_filename = target_filename
47        self.output_columns = output_columns
48        self.output_type = OutputType('sample')
49
50    @staticmethod
51    def _get_output_path(run_info=None, outfile=None):
52        run_path = run_info['run_dir']
53        if not os.path.isdir(run_path):
54            raise RuntimeError(f"Run directory does not exist: {run_path}")
55        return os.path.join(run_path, outfile)
56
57    def parse_sim_output(self, run_info={}):
58        def get_value(data, path):
59            for node in path:
60                data = data[node]
61            return data
62        out_path = self._get_output_path(run_info, self.target_filename)
63        raw_data = self._get_raw_data(out_path)
64        data = []
65        for col in self.output_columns:
66            try:
67                if isinstance(col, str):
68                    data.append((col, raw_data[col]))
69                elif isinstance(col, list):
70                    data.append(('.'.join(col), get_value(raw_data, col)))
71            except KeyError:
72                raise RuntimeError("no such field: {} in this json file".format(col))
73        return dict(data)
74
75    def _get_raw_data(self, out_path):
76        with open(out_path) as fd:
77            return json.load(fd)
JSONDecoder(target_filename, output_columns)
41    def __init__(self, target_filename, output_columns):
42        if len(output_columns) == 0:
43            msg = "output_columns cannot be empty."
44            logger.error(msg)
45            raise RuntimeError(msg)
46        self.target_filename = target_filename
47        self.output_columns = output_columns
48        self.output_type = OutputType('sample')
target_filename
output_columns
output_type
def parse_sim_output(self, run_info={}):
57    def parse_sim_output(self, run_info={}):
58        def get_value(data, path):
59            for node in path:
60                data = data[node]
61            return data
62        out_path = self._get_output_path(run_info, self.target_filename)
63        raw_data = self._get_raw_data(out_path)
64        data = []
65        for col in self.output_columns:
66            try:
67                if isinstance(col, str):
68                    data.append((col, raw_data[col]))
69                elif isinstance(col, list):
70                    data.append(('.'.join(col), get_value(raw_data, col)))
71            except KeyError:
72                raise RuntimeError("no such field: {} in this json file".format(col))
73        return dict(data)