1
0
Fork 0

Added tests for SampleImporter

master
Luca Moretti 4 years ago
parent ee02b415dd
commit 78923d4768
  1. 2
      main_package/classes/utility/sample_importer.py
  2. 2
      main_package/tests/optimizers/test_tabu_search.py
  3. 81
      main_package/tests/utility/test_sample_importer.py

@ -43,7 +43,7 @@ class SampleImporter(ai.AbstractImporter):
def import_data(self, header_column = None):
if header_column is None:
if header_column is not None:
self._sorter = header_column
else:
self._sorter = self.build_sorter(self._df_samples_list[0])

@ -46,7 +46,7 @@ class TestTabuSearch(unittest.TestCase):
prior_net_structure=prior_net_structure
)
cls.importer.import_data(0)
cls.importer.import_data()
cls.s1 = sp.SamplePath(cls.importer)
#cls.traj = cls.s1.concatenated_samples

@ -0,0 +1,81 @@
import sys
sys.path.append("../../classes/")
import unittest
import os
import glob
import numpy as np
import pandas as pd
import utility.sample_importer as si
import structure_graph.sample_path as sp
import json
class TestSampleImporter(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
with open("../../data/networks_and_trajectories_binary_data_01_3.json") as f:
raw_data = json.load(f)
trajectory_list_raw= raw_data[0]["samples"]
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):
sample_importer = si.SampleImporter(
trajectory_list=self.trajectory_list,
variables=self.variables,
prior_net_structure=self.prior_net_structure
)
sample_importer.import_data()
s1 = sp.SamplePath(sample_importer)
s1.build_trajectories()
s1.build_structure()
s1.clear_memory()
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):
sample_importer = si.SampleImporter(
trajectory_list=self.trajectory_list,
variables=self.variables,
prior_net_structure=self.prior_net_structure
)
sample_importer.import_data()
s1 = sp.SamplePath(sample_importer)
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()