1
0
Fork 0

Refactor StructureEstimator save_results and adjacency_matrix methods

master
Filippo Martini 4 years ago
parent 6ba5762d02
commit 028e6f6b22
  1. 3
      .idea/.gitignore
  2. 8
      .idea/PyCTBN.iml
  3. 6
      .idea/inspectionProfiles/profiles_settings.xml
  4. 4
      .idea/misc.xml
  5. 8
      .idea/modules.xml
  6. 6
      .idea/vcs.xml
  7. 124
      .idea/workspace.xml
  8. BIN
      CTBN_project_dominio.pdf
  9. 18
      PyCTBN/PyCTBN/estimators/structure_estimator.py
  10. 1
      PyCTBN/PyCTBN/structure_graph/sample_path.py
  11. 1
      PyCTBN/PyCTBN/utility/json_importer.py
  12. 19
      PyCTBN/tests/estimators/test_structure_estimator.py
  13. 41
      basic_main.py

3
.idea/.gitignore vendored

@ -1,3 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.8" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -1,6 +0,0 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
</project>

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/PyCTBN.iml" filepath="$PROJECT_DIR$/.idea/PyCTBN.iml" />
</modules>
</component>
</project>

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="4adfa503-a9d9-4b31-a5d8-3bd9cd9214ea" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/.gitignore" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/PyCTBN.iml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/inspectionProfiles/profiles_settings.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/CTBN_project_dominio.pdf" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/PyCTBN/PyCTBN/estimators/structure_estimator.py" beforeDir="false" afterPath="$PROJECT_DIR$/PyCTBN/PyCTBN/estimators/structure_estimator.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/PyCTBN/PyCTBN/structure_graph/sample_path.py" beforeDir="false" afterPath="$PROJECT_DIR$/PyCTBN/PyCTBN/structure_graph/sample_path.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/PyCTBN/PyCTBN/utility/json_importer.py" beforeDir="false" afterPath="$PROJECT_DIR$/PyCTBN/PyCTBN/utility/json_importer.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/PyCTBN/tests/estimators/test_structure_estimator.py" beforeDir="false" afterPath="$PROJECT_DIR$/PyCTBN/tests/estimators/test_structure_estimator.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/basic_main.py" beforeDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="1pR9CzNoVmzUM8b1VxIS85Q2acE" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="ASKED_ADD_EXTERNAL_FILES" value="true" />
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
</component>
<component name="RunManager" selected="Python tests.Unittests in test_structure_estimator.py">
<configuration name="Unittests for PyCTBN.tests.estimators.test_structure_estimator.TestStructureEstimator.test_adjacency_matrix" type="tests" factoryName="Unittests" temporary="true" nameIsGenerated="true">
<module name="PyCTBN" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="_new_additionalArguments" value="&quot;&quot;" />
<option name="_new_target" value="&quot;PyCTBN.tests.estimators.test_structure_estimator.TestStructureEstimator.test_adjacency_matrix&quot;" />
<option name="_new_targetType" value="&quot;PYTHON&quot;" />
<method v="2" />
</configuration>
<configuration name="Unittests for PyCTBN.tests.estimators.test_structure_estimator.TestStructureEstimator.test_save_plot_estimated_graph" type="tests" factoryName="Unittests" temporary="true" nameIsGenerated="true">
<module name="PyCTBN" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="_new_additionalArguments" value="&quot;&quot;" />
<option name="_new_target" value="&quot;PyCTBN.tests.estimators.test_structure_estimator.TestStructureEstimator.test_save_plot_estimated_graph&quot;" />
<option name="_new_targetType" value="&quot;PYTHON&quot;" />
<method v="2" />
</configuration>
<configuration name="Unittests for PyCTBN.tests.estimators.test_structure_estimator.TestStructureEstimator.test_save_results" type="tests" factoryName="Unittests" temporary="true" nameIsGenerated="true">
<module name="PyCTBN" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="_new_additionalArguments" value="&quot;&quot;" />
<option name="_new_target" value="&quot;PyCTBN.tests.estimators.test_structure_estimator.TestStructureEstimator.test_save_results&quot;" />
<option name="_new_targetType" value="&quot;PYTHON&quot;" />
<method v="2" />
</configuration>
<configuration name="Unittests in test_structure_estimator.py" type="tests" factoryName="Unittests" temporary="true" nameIsGenerated="true">
<module name="PyCTBN" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="_new_additionalArguments" value="&quot;&quot;" />
<option name="_new_target" value="&quot;$PROJECT_DIR$/PyCTBN/tests/estimators/test_structure_estimator.py&quot;" />
<option name="_new_targetType" value="&quot;PATH&quot;" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python tests.Unittests in test_structure_estimator.py" />
<item itemvalue="Python tests.Unittests for PyCTBN.tests.estimators.test_structure_estimator.TestStructureEstimator.test_save_plot_estimated_graph" />
<item itemvalue="Python tests.Unittests for PyCTBN.tests.estimators.test_structure_estimator.TestStructureEstimator.test_save_results" />
<item itemvalue="Python tests.Unittests for PyCTBN.tests.estimators.test_structure_estimator.TestStructureEstimator.test_adjacency_matrix" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="4adfa503-a9d9-4b31-a5d8-3bd9cd9214ea" name="Default Changelist" comment="" />
<created>1615134950460</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1615134950460</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>

Binary file not shown.

@ -90,17 +90,14 @@ class StructureEstimator(object):
list_without_test_parent.remove(parent_label)
return map(list, itertools.combinations(list_without_test_parent, size))
def save_results(self) -> None:
"""Save the estimated Structure to a .json file in the path where the data are loaded from.
The file is named as the input dataset but the `results_` word is appended to the results file.
def save_results(self, file_path: str) -> None:
"""Save the estimated Structure to a .json file in file_path.
:param file_path: the path including the file name with .json extension
:type file_path: string
"""
res = json_graph.node_link_data(self._complete_graph)
name = self._sample_path._importer.file_path.rsplit('/', 1)[-1]
name = name.split('.', 1)[0]
name += '_' + str(self._sample_path._importer.dataset_id())
name += '.json'
file_name = 'results_' + name
with open(file_name, 'w') as f:
with open(file_path, 'w') as f:
json.dump(res, f)
@ -120,14 +117,13 @@ class StructureEstimator(object):
"""
pass
def adjacency_matrix(self) -> np.ndarray:
"""Converts the estimated structure ``_complete_graph`` to a boolean adjacency matrix representation.
:return: The adjacency matrix of the graph ``_complete_graph``
:rtype: numpy.ndArray
"""
return nx.adj_matrix(self._complete_graph).toarray().astype(bool)
return nx.adj_matrix(self._complete_graph, self._nodes).toarray().astype(bool)
def spurious_edges(self) -> typing.List:
"""Return the spurious edges present in the estimated structure, if a prior net structure is present in

@ -10,6 +10,7 @@ from ..utility.abstract_importer import AbstractImporter
MESSAGE_HAS_TO_CONTAIN_EXCEPTION = 'The importer object has to contain the all processed data!'
class SamplePath(object):
"""Aggregates all the informations about the trajectories, the real structure of the sampled net and variables
cardinalites. Has the task of creating the objects ``Trajectory`` and ``Structure`` that will

@ -131,7 +131,6 @@ class JsonImporter(AbstractImporter):
:rtype: List
"""
dataframe = pd.DataFrame
#print(raw_data)
smps = raw_data[indx][trajectories_key]
df_samples_list = [dataframe(sample) for sample in smps]
return df_samples_list

@ -24,6 +24,7 @@ class TestStructureEstimator(unittest.TestCase):
cls.s1 = SamplePath(cls.importer)
cls.s1.build_trajectories()
cls.s1.build_structure()
cls.real_net_structure = nx.DiGraph(cls.s1.structure.edges)
def test_init(self):
exp_alfa = 0.1
@ -50,7 +51,6 @@ class TestStructureEstimator(unittest.TestCase):
no_self_loops.remove(node)
for n2 in no_self_loops:
self.assertIn((node, n2), cg.edges)
#se1.save_plot_estimated_structure_graph()
def test_build_removable_edges_matrix(self):
exp_alfa = 0.1
@ -84,18 +84,12 @@ class TestStructureEstimator(unittest.TestCase):
print("Execution Time: ", exec_time)
for ed in self.s1.structure.edges:
self.assertIn(tuple(ed), se1._complete_graph.edges)
#print("Spurious Edges:", se1.spurious_edges())
#se1.save_plot_estimated_structure_graph()
def test_save_results(self):
se1 = StructureConstraintBasedEstimator(self.s1, 0.1, 0.1)
se1.ctpc_algorithm()
se1.save_results()
name = self.s1._importer.file_path.rsplit('/', 1)[-1]
name = name.split('.', 1)[0]
name += '_' + str(self.s1._importer.dataset_id())
name += '.json'
file_name = 'results_' + name
file_name = './PyCTBN/tests/estimators/test_save.json'
se1.save_results(file_name)
with open(file_name) as f:
js_graph = json.load(f)
result_graph = nx.json_graph.node_link_graph(js_graph)
@ -105,13 +99,16 @@ class TestStructureEstimator(unittest.TestCase):
def test_adjacency_matrix(self):
se1 = StructureConstraintBasedEstimator(self.s1, 0.1, 0.1)
se1.ctpc_algorithm()
adj_matrix = nx.adj_matrix(se1._complete_graph).toarray().astype(bool)
adj_matrix = nx.adj_matrix(self.real_net_structure, self.s1.structure.nodes_labels).toarray().astype(bool)
self.assertTrue(np.array_equal(adj_matrix, se1.adjacency_matrix()))
def test_save_plot_estimated_graph(self):
se1 = StructureConstraintBasedEstimator(self.s1, 0.1, 0.1)
edges = se1.estimate_structure(disable_multiprocessing=True)
se1.save_plot_estimated_structure_graph('./networks_and_trajectories_ternary_data_3.png')
file_name = './PyCTBN/tests/estimators/test_plot.png'
se1.save_plot_estimated_structure_graph(file_name)
os.remove(file_name)
if __name__ == '__main__':

@ -1,41 +0,0 @@
import glob
import os
import sys
sys.path.append("./PyCTBN/")
import structure_graph.network_graph as ng
import structure_graph.sample_path as sp
import structure_graph.set_of_cims as sofc
import estimators.parameters_estimator as pe
import utility.json_importer as ji
def main():
read_files = glob.glob(os.path.join('./data', "*.json")) #Take all json files in this dir
#import data
importer = ji.JsonImporter(read_files[0], 'samples', 'dyn.str', 'variables', 'Time', 'Name')
#Create a SamplePath Obj
s1 = sp.SamplePath(importer)
#Build The trajectries and the structural infos
s1.build_trajectories()
s1.build_structure()
#From The Structure Object build the Graph
g = ng.NetworkGraph(s1.structure)
#Select a node you want to estimate the parameters
node = g.nodes[1]
#Init the graph specifically for THIS node
g.fast_init(node)
#Use SamplePath and Grpah to create a ParametersEstimator Object
p1 = pe.ParametersEstimator(s1, g)
#Init the peEst specifically for THIS node
p1.fast_init(node)
#Compute the parameters
sofc1 = p1.compute_parameters_for_node(node)
#The est CIMS are inside the resultant SetOfCIms Obj
print(sofc1.actual_cims)
if __name__ == "__main__":
main()