Old engine for Continuous Time Bayesian Networks. Superseded by reCTBN. 🐍
https://github.com/madlabunimib/PyCTBN
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.8 KiB
90 lines
2.8 KiB
4 years ago
|
|
||
3 years ago
|
# License: MIT License
|
||
|
|
||
|
|
||
4 years ago
|
import unittest
|
||
|
import os
|
||
|
import glob
|
||
3 years ago
|
import json
|
||
4 years ago
|
import numpy as np
|
||
|
import pandas as pd
|
||
3 years ago
|
|
||
|
|
||
3 years ago
|
from pyctbn.legacy.utility.sample_importer import SampleImporter
|
||
|
from pyctbn.legacy.structure_graph.sample_path import SamplePath
|
||
4 years ago
|
|
||
3 years ago
|
|
||
4 years ago
|
|
||
|
|
||
|
|
||
|
class TestSampleImporter(unittest.TestCase):
|
||
|
|
||
|
@classmethod
|
||
|
def setUpClass(cls) -> None:
|
||
3 years ago
|
with open("./tests/data/networks_and_trajectories_binary_data_01_3.json") as f:
|
||
4 years ago
|
raw_data = json.load(f)
|
||
4 years ago
|
|
||
|
#read the samples
|
||
4 years ago
|
trajectory_list_raw= raw_data[0]["samples"]
|
||
4 years ago
|
|
||
4 years ago
|
cls.trajectory_list = [pd.DataFrame(sample) for sample in trajectory_list_raw]
|
||
|
|
||
|
cls.variables= pd.DataFrame(raw_data[0]["variables"])
|
||
|
cls.prior_net_structure = pd.DataFrame(raw_data[0]["dyn.str"])
|
||
|
|
||
|
|
||
|
def test_init(self):
|
||
4 years ago
|
sample_importer = SampleImporter(
|
||
4 years ago
|
trajectory_list=self.trajectory_list,
|
||
|
variables=self.variables,
|
||
|
prior_net_structure=self.prior_net_structure
|
||
|
)
|
||
|
|
||
4 years ago
|
sample_importer.import_data(['X','Y','Z'])
|
||
4 years ago
|
|
||
4 years ago
|
s1 = SamplePath(sample_importer)
|
||
4 years ago
|
s1.build_trajectories()
|
||
|
s1.build_structure()
|
||
|
s1.clear_memory()
|
||
4 years ago
|
data_id= sample_importer.dataset_id()
|
||
4 years ago
|
|
||
4 years ago
|
self.assertEqual(data_id,"")
|
||
4 years ago
|
self.assertEqual(len(s1._importer._df_samples_list), 300)
|
||
|
self.assertIsInstance(s1._importer._df_samples_list,list)
|
||
|
self.assertIsInstance(s1._importer._df_samples_list[0],pd.DataFrame)
|
||
|
self.assertEqual(len(s1._importer._df_variables), 3)
|
||
|
self.assertIsInstance(s1._importer._df_variables,pd.DataFrame)
|
||
|
self.assertEqual(len(s1._importer._df_structure), 2)
|
||
|
self.assertIsInstance(s1._importer._df_structure,pd.DataFrame)
|
||
|
|
||
|
def test_order(self):
|
||
4 years ago
|
sample_importer = SampleImporter(
|
||
4 years ago
|
trajectory_list=self.trajectory_list,
|
||
|
variables=self.variables,
|
||
|
prior_net_structure=self.prior_net_structure
|
||
|
)
|
||
|
|
||
|
sample_importer.import_data()
|
||
|
|
||
4 years ago
|
s1 = SamplePath(sample_importer)
|
||
4 years ago
|
s1.build_trajectories()
|
||
|
s1.build_structure()
|
||
|
s1.clear_memory()
|
||
|
|
||
|
for count,var in enumerate(s1._importer._df_samples_list[0].columns[1:]):
|
||
|
self.assertEqual(s1._importer._sorter[count],var)
|
||
|
|
||
|
|
||
|
|
||
|
def ordered(self, obj):
|
||
|
if isinstance(obj, dict):
|
||
|
return sorted((k, self.ordered(v)) for k, v in obj.items())
|
||
|
if isinstance(obj, list):
|
||
|
return sorted(self.ordered(x) for x in obj)
|
||
|
else:
|
||
|
return obj
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
unittest.main()
|