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.
120 lines
3.7 KiB
120 lines
3.7 KiB
4 years ago
|
|
||
3 years ago
|
# License: MIT License
|
||
|
|
||
|
|
||
4 years ago
|
import glob
|
||
|
import math
|
||
|
import os
|
||
|
import unittest
|
||
|
|
||
|
import networkx as nx
|
||
|
import numpy as np
|
||
|
import psutil
|
||
4 years ago
|
|
||
4 years ago
|
import copy
|
||
4 years ago
|
import json
|
||
|
import pandas as pd
|
||
4 years ago
|
|
||
4 years ago
|
|
||
3 years ago
|
from pyctbn.legacy.structure_graph.sample_path import SamplePath
|
||
|
from pyctbn.legacy.estimators.structure_score_based_estimator import StructureScoreBasedEstimator
|
||
|
from pyctbn.legacy.utility.json_importer import JsonImporter
|
||
|
from pyctbn.legacy.utility.sample_importer import SampleImporter
|
||
4 years ago
|
|
||
|
|
||
|
|
||
|
class TestHillClimbingSearch(unittest.TestCase):
|
||
|
|
||
|
@classmethod
|
||
|
def setUpClass(cls):
|
||
|
#cls.read_files = glob.glob(os.path.join('../../data', "*.json"))
|
||
4 years ago
|
|
||
|
|
||
3 years ago
|
cls.importer = JsonImporter("./tests/data/networks_and_trajectories_binary_data_01_3.json", 'samples', 'dyn.str', 'variables', 'Time', 'Name')
|
||
4 years ago
|
cls.importer.import_data(0)
|
||
|
cls.s1 = SamplePath(cls.importer)
|
||
4 years ago
|
cls.s1.build_trajectories()
|
||
|
cls.s1.build_structure()
|
||
|
|
||
|
|
||
|
|
||
|
def test_structure(self):
|
||
|
true_edges = copy.deepcopy(self.s1.structure.edges)
|
||
|
true_edges = set(map(tuple, true_edges))
|
||
|
|
||
4 years ago
|
se1 = StructureScoreBasedEstimator(self.s1)
|
||
4 years ago
|
edges = se1.estimate_structure(
|
||
4 years ago
|
max_parents = 2,
|
||
4 years ago
|
iterations_number = 40,
|
||
|
patience = None,
|
||
4 years ago
|
optimizer = 'hill',
|
||
|
disable_multiprocessing=True
|
||
4 years ago
|
)
|
||
|
|
||
|
|
||
|
self.assertEqual(edges, true_edges)
|
||
|
|
||
4 years ago
|
def test_structure_3(self):
|
||
3 years ago
|
with open("./tests/data/networks_and_trajectories_ternary_data_01_6_1.json") as f:
|
||
4 years ago
|
raw_data = json.load(f)
|
||
4 years ago
|
|
||
4 years ago
|
trajectory_list_raw= raw_data["samples"]
|
||
4 years ago
|
# Convert to DataFrame
|
||
4 years ago
|
trajectory_list = [pd.DataFrame(sample) for sample in trajectory_list_raw]
|
||
|
|
||
|
variables= raw_data["variables"]
|
||
|
prior_net_structure = raw_data["dyn.str"]
|
||
|
|
||
|
|
||
|
self.importer = SampleImporter(
|
||
|
trajectory_list=trajectory_list,
|
||
|
variables=variables,
|
||
|
prior_net_structure=prior_net_structure
|
||
|
)
|
||
|
|
||
|
self.importer.import_data()
|
||
|
#cls.s1 = sp.SamplePath(cls.importer)
|
||
|
|
||
|
#cls.traj = cls.s1.concatenated_samples
|
||
|
|
||
|
# print(len(cls.traj))
|
||
|
self.s1 = SamplePath(self.importer)
|
||
|
self.s1.build_trajectories()
|
||
|
self.s1.build_structure()
|
||
|
|
||
|
true_edges = copy.deepcopy(self.s1.structure.edges)
|
||
|
true_edges = set(map(tuple, true_edges))
|
||
|
|
||
|
known_edges = self.s1.structure.edges[0:2]
|
||
|
|
||
|
se1 = StructureScoreBasedEstimator(self.s1,known_edges=known_edges)
|
||
|
edges = se1.estimate_structure(
|
||
|
max_parents = 3,
|
||
|
iterations_number = 100,
|
||
|
patience = 40,
|
||
|
optimizer = 'hill',
|
||
|
disable_multiprocessing=True
|
||
|
)
|
||
|
|
||
|
'calculate precision and recall'
|
||
|
n_missing_edges = 0
|
||
|
n_added_fake_edges = 0
|
||
|
|
||
|
|
||
|
n_added_fake_edges = len(edges.difference(true_edges))
|
||
|
|
||
|
n_missing_edges = len(true_edges.difference(edges))
|
||
|
|
||
|
n_true_positive = len(true_edges) - n_missing_edges
|
||
|
|
||
|
precision = n_true_positive / (n_true_positive + n_added_fake_edges)
|
||
|
|
||
|
recall = n_true_positive / (n_true_positive + n_missing_edges)
|
||
|
|
||
|
self.assertGreaterEqual(precision,0.75)
|
||
|
self.assertGreaterEqual(recall,0.75)
|
||
4 years ago
|
|
||
|
if __name__ == '__main__':
|
||
|
unittest.main()
|
||
|
|