From c71699d267f6c705f4d57bedf19b0ec83e926971 Mon Sep 17 00:00:00 2001 From: Pietro Date: Mon, 19 Apr 2021 11:19:26 +0200 Subject: [PATCH] JsonExporter --- .gitignore | 1 + .../structure_graph/trajectory_generator.py | 14 ++--------- PyCTBN/PyCTBN/utility/json_exporter.py | 25 +++++++++++++++++++ 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 PyCTBN/PyCTBN/utility/json_exporter.py diff --git a/.gitignore b/.gitignore index f0c0376..9607588 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ test.py test_1.py test1.json test2.json +test3.json diff --git a/PyCTBN/PyCTBN/structure_graph/trajectory_generator.py b/PyCTBN/PyCTBN/structure_graph/trajectory_generator.py index f00ce29..769f4a7 100644 --- a/PyCTBN/PyCTBN/structure_graph/trajectory_generator.py +++ b/PyCTBN/PyCTBN/structure_graph/trajectory_generator.py @@ -5,7 +5,6 @@ from .trajectory import Trajectory import numpy as np import pandas as pd import re -import os import json from numpy import random @@ -94,14 +93,5 @@ class TrajectoryGenerator(object): # undefine variable time time[next] = np.NaN - def out_json(self, filename): - data = { - "dyn.str": self._importer._raw_data[0]["dyn.str"], - "variables": self._importer._raw_data[0]["variables"], - "dyn.cims": self._importer._raw_data[0]["dyn.cims"], - "samples": [json.loads(self._generated_trajectory.to_json(orient="records"))] - } - - path = os.getcwd() - with open(path + "/" + filename, "w") as json_file: - json.dump(data, json_file) \ No newline at end of file + def to_json(self): + return json.loads(self._generated_trajectory.to_json(orient="records")) \ No newline at end of file diff --git a/PyCTBN/PyCTBN/utility/json_exporter.py b/PyCTBN/PyCTBN/utility/json_exporter.py new file mode 100644 index 0000000..a264675 --- /dev/null +++ b/PyCTBN/PyCTBN/utility/json_exporter.py @@ -0,0 +1,25 @@ +import json +import pandas as pd +import os + +class JsonExporter(object): + def __init__(self, variables, dyn_str, dyn_cims): + self._variables = variables + self._dyn_str = dyn_str + self._dyn_cims = dyn_cims + self._trajectories = [] + + def add_trajectory(self, trajectory: list): + self._trajectories.append(trajectory) + + def out_json(self, filename): + data = { + "dyn.str": self._dyn_str, + "variables": self._variables, + "dyn.cims": self._dyn_cims, + "samples": self._trajectories + } + + path = os.getcwd() + with open(path + "/" + filename, "w") as json_file: + json.dump(data, json_file) \ No newline at end of file