Source code for NEMtropy.ensemble_generator

import multiprocessing as mp
import numpy as np
# import warnings
# import sys


[docs]def ensemble_sampler_cm_graph(outfile_name, x, cpu_n=2, seed=None): """Produce a single undirected binary graph after UBCM. The graph is saved as an edges list on a file. :param outfile_name: Name of the file where to save the graph on. :type outfile_name: str :param x: UBCM solution :type x: numpy.ndarray :param cpu_n: Number of cpus to use, defaults to 2 :type cpu_n: int, optional :param seed: Random seed, defaults to None :type seed: int, optional :return: The `outfile_name` input parameter :rtype: str """ if seed is not None: np.random.seed(seed) inds = np.arange(len(x)) # put together inputs for pool iter_ = iter( ((i, xi), (j, xj), np.random.randint(0, 1000000)) for i, xi in zip(inds, x) for j, xj in zip(inds, x) if i < j) # compute existing edges with mp.Pool(processes=cpu_n) as pool: edges_list = pool.starmap(is_a_link_cm, iter_) # removing None edges_list[:] = (value for value in edges_list if value is not None) # edgelist writing with open(outfile_name, "w") as outfile: outfile.write( "".join( "{} {}\n".format(str(i), str(j)) for (i, j) in edges_list) ) return outfile_name
[docs]def ensemble_sampler_ecm_graph(outfile_name, x, y, cpu_n=2, seed=None): """Produce a single undirected weighted graph after ECM. The graph is saved as an edges list on a file. The function is parallelyzed. :param outfile_name: Name of the file where to save the graph on. :type outfile_name: str :param x: ECM degrees solution :type x: numpy.ndarray :param y: ECM strengths solution :type y: numpy.ndarray :param cpu_n: Number of cpus to use, defaults to 2 :type cpu_n: int, optional :param seed: Random seed, defaults to None :type seed: int, optional :return: The `outfile_name` input parameter :rtype: str """ if seed is not None: np.random.seed(seed) inds = np.arange(len(x)) # put together inputs for pool iter_ = iter( ((i, xi, yi), (j, xj, yj), np.random.randint(0, 1000000)) for i, xi, yi in zip(inds, x, y) for j, xj, yj in zip(inds, x, y) if i < j) # compute existing edges with mp.Pool(processes=cpu_n) as pool: edges_list = pool.starmap(is_a_link_ecm, iter_) # removing None edges_list[:] = (value for value in edges_list if value is not None) # edgelist writing with open(outfile_name, "w") as outfile: outfile.write( "".join( "{} {} {}\n".format(str(i), str(j), str(w)) for (i, j, w) in edges_list) ) return outfile_name
[docs]def ensemble_sampler_dcm_graph(outfile_name, x, y, cpu_n=2, seed=None): """Produce a single directed binary graph after DBCM. The graph is saved as an edges list on a file. The function is parallelyzed. :param outfile_name: Name of the file where to save the graph on. :type outfile_name: str :param x: DBCM out-degrees parameters solution :type x: numpy.ndarray :param y: DBCM in-degrees parameters solution :type y: numpy.ndarray :param cpu_n: Number of cpus to use, defaults to 2 :type cpu_n: int, optional :param seed: Random seed, defaults to None :type seed: int, optional :return: The `outfile_name` input parameter :rtype: str """ if seed is not None: np.random.seed(seed) inds = np.arange(len(x)) # put together inputs for pool iter_ = iter(((i, xi), (j, yj), np.random.randint(0, 1000000)) for i, xi in zip(inds, x) for j, yj in zip(inds, y) if i != j) # compute existing edges with mp.Pool(processes=cpu_n) as pool: edges_list = pool.starmap(is_a_link_dcm, iter_) # removing None edges_list[:] = (value for value in edges_list if value is not None) # edgelist writing with open(outfile_name, "w") as outfile: outfile.write( "".join( "{} {}\n".format(str(i), str(j)) for (i, j) in edges_list) ) return outfile_name
[docs]def ensemble_sampler_decm_graph( outfile_name, a_out, a_in, b_out, b_in, cpu_n=2, seed=None): """Produce a single directed weighted graph after DECM. The graph is saved as an edges list on a file. The function is parallelyzed. :param outfile_name: Name of the file where to save the graph on. :type outfile_name: str :param a_out: DECM out-degrees parameters solution :type a_out: numpy.ndarray :param a_in: DECM in-degrees parameters solution :type a_in: numpy.ndarray :param b_out: DECM out-strengths parameters solution :type b_out: numpy.ndarray :param b_in: DECM in-strengths parameters solution :type b_in: numpy.ndarray :param cpu_n: Number of cpus to use, defaults to 2 :type cpu_n: int, optional :param seed: Random seed, defaults to None :type seed: int, optional :return: The `outfile_name` input parameter :rtype: str """ if seed is not None: np.random.seed(seed) inds = np.arange(len(a_out)) # put together inputs for pool iter_ = iter(( (i, a_out_i, b_out_i), (j, a_in_j, b_in_j), np.random.randint(0, 1000000)) for i, a_out_i, b_out_i in zip(inds, a_out, b_out) for j, a_in_j, b_in_j in zip(inds, a_in, b_in) if i != j) # compute existing edges with mp.Pool(processes=cpu_n) as pool: edges_list = pool.starmap(is_a_link_decm, iter_) # removing None edges_list[:] = (value for value in edges_list if value is not None) # edgelist writing with open(outfile_name, "w") as outfile: outfile.write( "".join( "{} {} {}\n".format(str(i), str(j), str(w)) for (i, j, w) in edges_list) ) return outfile_name
[docs]def ensemble_sampler_crema_ecm_det_graph( outfile_name, beta, adj, cpu_n=2, seed=None): """Produce a single undirected weighted graph after CReMa, given a fixed, complete network topology. The graph is saved as an edges list on a file. The function is parallelyzed. :param outfile_name: Name of the file where to save the graph on. :type outfile_name: str :param beta: CReMa solution :type beta: numpy.ndarray :param adj: Edges list: out-nodes array, in-nodes array and links weights. :type adj: (numpy.ndarray, numpy.ndarray, numpy.ndarray) :param cpu_n: Number of cpus to use, defaults to 2 :type cpu_n: int, optional :param seed: Random seed, defaults to None :type seed: int, optional :return: The `outfile_name` input parameter :rtype: str """ if seed is not None: np.random.seed(seed) (row_inds, col_inds, weigths_value) = adj del weigths_value # put together inputs for pool iter_ = iter( ((i, beta[i]), (j, beta[j]), np.random.randint(0, 1000000)) for i, j in zip(row_inds, col_inds) ) # compute existing edges with mp.Pool(processes=cpu_n) as pool: edges_list = pool.starmap(is_a_link_crema_ecm_det, iter_) # edgelist writing with open(outfile_name, "w") as outfile: outfile.write( "".join( "{} {} {}\n".format(str(i), str(j), str(w)) for (i, j, w) in edges_list) ) return outfile_name
[docs]def ensemble_sampler_crema_ecm_prob_graph( outfile_name, beta, adj, cpu_n=2, seed=None): """Produce a single undirected weighted graph after CReMa, given a probabilistic network topology. The graph is saved as an edges list on a file. The function is parallelyzed. :param outfile_name: Name of the file where to save the graph on. :type outfile_name: str :param beta: CReMa solution :type beta: numpy.ndarray :param adj: Edges list: out-nodes array, in-nodes array and links probabilities. :type adj: (numpy.ndarray, numpy.ndarray, numpy.ndarray) :param cpu_n: Number of cpus to use, defaults to 2 :type cpu_n: int, optional :param seed: Random seed, defaults to None :type seed: int, optional :return: The `outfile_name` input parameter :rtype: str """ if seed is not None: np.random.seed(seed) (row_inds, col_inds, weigths_value) = adj # put together inputs for pool iter_ = iter( ((i, beta[i]), (j, beta[j]), w_prob, np.random.randint(0, 1000000)) for i, j, w_prob in zip(row_inds, col_inds, weigths_value) ) # compute existing edges with mp.Pool(processes=cpu_n) as pool: edges_list = pool.starmap(is_a_link_crema_ecm_prob, iter_) # removing None edges_list[:] = (value for value in edges_list if value is not None) # edgelist writing with open(outfile_name, "w") as outfile: outfile.write( "".join( "{} {} {}\n".format(str(i), str(j), str(w)) for (i, j, w) in edges_list) ) return outfile_name
[docs]def ensemble_sampler_crema_sparse_ecm_prob_graph( outfile_name, beta, adj, cpu_n=2, seed=None): """Produce a single undirected weighted graph after CReMa, given a probabilistic network topology. The graph is saved as an edges list on a file. The function is parallelyzed. :param outfile_name: Name of the file where to save the graph on. :type outfile_name: str :param beta: CReMa solution :type beta: numpy.ndarray :param adj: Edges list: out-nodes array, in-nodes array and links probabilities. :type adj: (numpy.ndarray, numpy.ndarray, numpy.ndarray) :param cpu_n: Number of cpus to use, defaults to 2 :type cpu_n: int, optional :param seed: Random seed, defaults to None :type seed: int, optional :return: The `outfile_name` input parameter :rtype: str """ if seed is not None: np.random.seed(seed) x = adj[0] n = len(x) # put together inputs for pool iter_ = iter( ((i, beta[i], x[i]), (j, beta[j], x[j]), np.random.randint(0, 1000000)) for i in range(n) for j in range(n) if i != j ) # compute existing edges with mp.Pool(processes=cpu_n) as pool: edges_list = pool.starmap(is_a_link_crema_sparse_ecm_prob, iter_) # removing None edges_list[:] = (value for value in edges_list if value is not None) # debug # print(edges_list) # edgelist writing with open(outfile_name, "w") as outfile: outfile.write( "".join( "{} {} {}\n".format(str(i), str(j), str(w)) for (i, j, w) in edges_list) ) return outfile_name
[docs]def ensemble_sampler_crema_decm_prob_graph( outfile_name, beta, adj, cpu_n=2, seed=None): """Produce a single directed weighted graph after CReMa, given a probabilistic network topology. The graph is saved as an edges list on a file. The function is parallelyzed. :param outfile_name: Name of the file where to save the graph on. :type outfile_name: str :param beta: CReMa solution :type beta: numpy.ndarray :param adj: Edges list: out-nodes array, in-nodes array and links probabilities. :type adj: (numpy.ndarray, numpy.ndarray, numpy.ndarray) :param cpu_n: Number of cpus to use, defaults to 2 :type cpu_n: int, optional :param seed: Random seed, defaults to None :type seed: int, optional :return: The `outfile_name` input parameter :rtype: str """ if seed is not None: np.random.seed(seed) b_out, b_in = beta (row_inds, col_inds, weigths_value) = adj # put together inputs for pool iter_ = iter( ((i, b_out[i]), (j, b_in[j]), w_prob, np.random.randint(0, 1000000)) for i, j, w_prob in zip(row_inds, col_inds, weigths_value) ) # compute existing edges with mp.Pool(processes=cpu_n) as pool: edges_list = pool.starmap(is_a_link_crema_decm_prob, iter_) # debug # print(edges_list) # removing None edges_list[:] = (value for value in edges_list if value is not None) # edgelist writing with open(outfile_name, "w") as outfile: outfile.write( "".join( "{} {} {}\n".format(str(i), str(j), str(w)) for (i, j, w) in edges_list) ) return outfile_name
[docs]def ensemble_sampler_crema_decm_det_graph( outfile_name, beta, adj, cpu_n=2, seed=None): """Produce a single directed weighted graph after CReMa, given a fixed, complete network topology. The graph is saved as an edges list on a file. The function is parallelyzed. :param outfile_name: Name of the file where to save the graph on. :type outfile_name: str :param beta: CReMa solution :type beta: numpy.ndarray :param adj: Edges list: out-nodes array, in-nodes array and links weights. :type adj: (numpy.ndarray, numpy.ndarray, numpy.ndarray) :param cpu_n: Number of cpus to use, defaults to 2 :type cpu_n: int, optional :param seed: Random seed, defaults to None :type seed: int, optional :return: The `outfile_name` input parameter :rtype: str """ if seed is not None: np.random.seed(seed) b_out, b_in = beta (row_inds, col_inds, weigths_value) = adj del weigths_value # put together inputs for pool iter_ = iter( ((i, b_out[i]), (j, b_in[j]), np.random.randint(0, 1000000)) for i, j in zip(row_inds, col_inds) ) # compute existing edges with mp.Pool(processes=cpu_n) as pool: edges_list = pool.starmap(is_a_link_crema_decm_det, iter_) # debug # print(edges_list) # edgelist writing with open(outfile_name, "w") as outfile: outfile.write( "".join( "{} {} {}\n".format(str(i), str(j), str(w)) for (i, j, w) in edges_list) ) return outfile_name
[docs]def ensemble_sampler_crema_sparse_decm_prob_graph( outfile_name, beta, adj, cpu_n=2, seed=None): """Produce a single directed weighted graph after the sparse version of CReMa, given a probabilistic network topology. The graph is saved as an edges list on a file. The function is parallelyzed. :param outfile_name: Name of the file where to save the graph on. :type outfile_name: str :param beta: CReMa solution :type beta: numpy.ndarray :param adj: Tuple of DBCM out-degrees parameters and in-degrees parameters solution. :type adj: (numpy.ndarray, numpy.ndarray) :param cpu_n: Number of cpus to use, defaults to 2 :type cpu_n: int, optional :param seed: Random seed, defaults to None :type seed: int, optional :return: The `outfile_name` input parameter :rtype: str """ if seed is not None: np.random.seed(seed) b_out, b_in = beta x, y = adj n = len(x) # put together inputs for pool iter_ = iter( ( (i, b_out[i], x[i]), (j, b_in[j], y[j]), np.random.randint(0, 1000000)) for i in range(n) for j in range(n) if i != j ) # compute existing edges with mp.Pool(processes=cpu_n) as pool: edges_list = pool.starmap(is_a_link_crema_sparse_decm_prob, iter_) # removing None edges_list[:] = (value for value in edges_list if value is not None) # edgelist writing with open(outfile_name, "w") as outfile: outfile.write( "".join( "{} {} {}\n".format(str(i), str(j), str(w)) for (i, j, w) in edges_list) ) return outfile_name