From dcda13297f02a5a9a366889ebce44e0be93068a4 Mon Sep 17 00:00:00 2001 From: Pietro Date: Wed, 14 Apr 2021 18:36:30 +0200 Subject: [PATCH] Bugfix, Out JSON --- .gitignore | 1 + .../structure_graph/trajectory_generator.py | 28 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 2e3feea..79df440 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ __pycache__ **/build test.py test_1.py +test1.json diff --git a/PyCTBN/PyCTBN/structure_graph/trajectory_generator.py b/PyCTBN/PyCTBN/structure_graph/trajectory_generator.py index 03aa634..63bba79 100644 --- a/PyCTBN/PyCTBN/structure_graph/trajectory_generator.py +++ b/PyCTBN/PyCTBN/structure_graph/trajectory_generator.py @@ -5,6 +5,8 @@ from .trajectory import Trajectory import numpy as np import pandas as pd import re +import os +import json from numpy import random class TrajectoryGenerator(object): @@ -32,6 +34,8 @@ class TrajectoryGenerator(object): node_states_number = self._importer._df_variables.where(self._importer._df_variables["Name"] == v)["Value"], p_combs = p_combs, cims = v_cims) self._cims[v] = sof + self._generated_trajectory = None + def CTBN_Sample(self, t_end = -1, max_tr = -1): t = 0 sigma = pd.DataFrame(columns = (["Time"] + self._vnames)) @@ -40,10 +44,10 @@ class TrajectoryGenerator(object): n_tr = 0 while True: + current_values = sigma.loc[len(sigma) - 1] + for i in range(0, time.size): if np.isnan(time[i]): - # Probability to transition from current state v_values[i] to (1 - v_values[i]) - current_values = sigma.loc[len(sigma) - 1] cim = self._cims[self._vnames[i]].filter_cims_with_mask(np.array([True for p in self._parents[self._vnames[i]]]), [current_values.at[p] for p in self._parents[self._vnames[i]]])[0].cim param = -1 * cim[current_values.at[self._vnames[i]]][current_values.at[self._vnames[i]]] @@ -55,11 +59,11 @@ class TrajectoryGenerator(object): t = time[next] if (max_tr != -1 and n_tr == max_tr) or (t_end != -1 and t >= t_end): - """ columns = self._importer.build_list_of_samples_array(sigma) - columns[0] = pd.to_numeric(columns[0]) - return Trajectory(columns, len(self._vnames) + 1) """ + self._generated_trajectory = sigma return sigma else: + cim = self._cims[self._vnames[next]].filter_cims_with_mask(np.array([True for p in self._parents[self._vnames[next]]]), + [current_values.at[p] for p in self._parents[self._vnames[next]]])[0].cim cim_row = np.array(cim[current_values.at[self._vnames[next]]]) cim_row[current_values.at[self._vnames[next]]] = 0 cim_row /= sum(cim_row) @@ -73,4 +77,16 @@ class TrajectoryGenerator(object): n_tr += 1 # undefine variable time - time[next] = np.NaN \ No newline at end of file + 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