|
|
|
@ -5,6 +5,21 @@ from .set_of_cims import SetOfCims |
|
|
|
|
import numpy as np |
|
|
|
|
|
|
|
|
|
class NetworkGenerator(object): |
|
|
|
|
"""Provides the methods to generate a network graph and the CIMs related to it |
|
|
|
|
Items in _labels, _vals and _indxs are related, and therefore respect the same order |
|
|
|
|
|
|
|
|
|
:param _labels: List of variables labels that will be part of the network |
|
|
|
|
:type _labels: List |
|
|
|
|
:param _vals: List of cardinalities of the variables in network (defined in the same order as _labels) |
|
|
|
|
:type _vals: List |
|
|
|
|
:param _indxs: List of the nodes indexes |
|
|
|
|
:type _indxs: List |
|
|
|
|
:param _cims: It contains, for each variable label (the key), the SetOfCims object related to it |
|
|
|
|
:type _cims: Dict |
|
|
|
|
:param _graph: The NetworkGraph object representing the generated structure |
|
|
|
|
:type _graph: NetworkGraph |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
def __init__(self, labels, vals): |
|
|
|
|
self._labels = labels |
|
|
|
|
self._vals = vals |
|
|
|
@ -13,14 +28,30 @@ class NetworkGenerator(object): |
|
|
|
|
self._cims = None |
|
|
|
|
|
|
|
|
|
def generate_graph(self, density): |
|
|
|
|
"""Generate the edges according to specified density, and then instantiate the NetworkGraph object |
|
|
|
|
to represent the network |
|
|
|
|
|
|
|
|
|
:param density: Probability of an edge between two nodes to exist |
|
|
|
|
:type density: float |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
edges = [(i, j) for i in self._labels for j in self._labels if np.random.binomial(1, density) == 1 and i != j] |
|
|
|
|
indxs = np.array([i for i, l in enumerate(self._labels)]) |
|
|
|
|
s = Structure(self._labels, self._indxs, self._vals, edges, len(self._labels)) |
|
|
|
|
self._graph = NetworkGraph(s) |
|
|
|
|
self._graph.add_nodes(s.nodes_labels) |
|
|
|
|
self._graph.add_edges(s.edges) |
|
|
|
|
|
|
|
|
|
def generate_cims(self, min_val, max_val): |
|
|
|
|
"""For each node, generate the corresponding SetOfCims. The objective is to group the CIMs |
|
|
|
|
(actually generated by private method __generate_cim) according to parents possibles states of every node. |
|
|
|
|
This method must obviously be executed after the graph has been generated. |
|
|
|
|
|
|
|
|
|
:param min_val: Minimum value allowed for the coefficients in the CIMs |
|
|
|
|
:type min_val: float |
|
|
|
|
:param max_val: Maximum value allowed for the coefficients in the CIMs |
|
|
|
|
:type max_val: float |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
if self._graph is None: |
|
|
|
|
return |
|
|
|
|
|
|
|
|
@ -44,6 +75,16 @@ class NetworkGenerator(object): |
|
|
|
|
cims = np.array(node_cims)) |
|
|
|
|
|
|
|
|
|
def __generate_cim(self, min_val, max_val, shape): |
|
|
|
|
"""Generate a valid CIM matrix, with coefficients in the range [min_val, max_val] and with dimension [shape, shape] |
|
|
|
|
|
|
|
|
|
:param min_val: Minimum value allowed for the coefficients in the CIMs |
|
|
|
|
:type min_val: float |
|
|
|
|
:param max_val: Maximum value allowed for the coefficients in the CIMs |
|
|
|
|
:type max_val: float |
|
|
|
|
:param shape: Number of elements in each dimension of the matrix (this actually is the cardinality of the node) |
|
|
|
|
:type shape: int |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
cim = np.empty(shape=(shape, shape)) |
|
|
|
|
cim[:] = np.nan |
|
|
|
|
|
|
|
|
@ -58,6 +99,13 @@ class NetworkGenerator(object): |
|
|
|
|
return cim |
|
|
|
|
|
|
|
|
|
def out_json(self, filename): |
|
|
|
|
"""Create a file in current directory and write on it the generated network and the CIMs, |
|
|
|
|
after having restructured the objects in order to respect the standard JSON file structure. |
|
|
|
|
|
|
|
|
|
:param filename: Name of the output file (it must include json extension) |
|
|
|
|
:type filename: string |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
dyn_str = [{"From": edge[0], "To": edge[1]} for edge in self._graph.edges] |
|
|
|
|
variables = [{"Name": l, "Value": self._vals[i]} for i, l in enumerate(self._labels)] |
|
|
|
|
dyn_cims = {} |
|
|
|
@ -89,11 +137,9 @@ class NetworkGenerator(object): |
|
|
|
|
"samples": [] |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
print(data) |
|
|
|
|
|
|
|
|
|
""" path = os.getcwd() |
|
|
|
|
with open(path + "/" + filename, "w") as json_file: |
|
|
|
|
json.dump(data, json_file) """ |
|
|
|
|
path = os.getcwd() |
|
|
|
|
with open(path + "/" + filename, "w") as json_file: |
|
|
|
|
json.dump(data, json_file) |
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
def graph(self) -> NetworkGraph: |
|
|
|
|