def generate_model(p=30, p_zero=0.7, type=None): DEBUG = True ''' this function creates a model in order to generate data according to it's distribution. and in order to compare the approximated model to the original distribution ''' if not type: adjmat = random_graph(p, p_zero) if type == 'grid': adjmat = grid_graph(p) adjmat.nodes() adjmat.nodes_iter() gen = generate(adj_mat=adjmat) return gen
def random_graph(graph=None, size=10, cluster=True, layout=None): """Return a random graph (models.py Graph class). This was my original test graph function. It makes a 2D non-periodic lattice, adds some random noise to it, and returns it. """ if graph is None: g = grid_graph(dim=[size, size]) g = nx_relabel_nodes(g, dict((n, i) for i, n in enumerate(g.nodes()))) if layout: layout = nx.drawing.nx_pydot.pydot_layout(g) # layout = nx.drawing.layout.shell_layout(g) if cluster: clusterGraph(g, p=0.05) # Set weights and make the Graph object. for a, b, d in g.edges(data=True): d["weight"] = -10 from .old.models import Graph G = Graph.fromNetworkX(g, coords=layout, defaultweight=1) return G
def random_graph(graph=None, size=10, cluster=True, layout=None): """Return a random graph (models.py Graph class). This was my original test graph function. It makes a 2D non-periodic lattice, adds some random noise to it, and returns it. """ if graph is None: g = grid_graph(dim=[size, size]) g = nx_relabel_nodes(g, dict((n, i) for i, n in enumerate(g.nodes()))) if layout: layout = nx.drawing.nx_pydot.pydot_layout(g) # layout = nx.drawing.layout.shell_layout(g) if cluster: clusterGraph(g, p=.05) # Set weights and make the Graph object. for a, b, d in g.edges(data=True): d['weight'] = -10 from .old.models import Graph G = Graph.fromNetworkX(g, coords=layout, defaultweight=1) return G
def __init__(self, lattice=ngc.grid_graph( dim=[N,N] ), data=zeros((N,N)), tau_x = 1, tau_y = 1, phi = 0.1): # sanity test if lattice.number_of_nodes() != data.size: raise Exception('data and lattice sizes do not match', '%d vs %d' % (data.size, lattice.number_of_nodes()) ); self.num_nodes = lattice.number_of_nodes(); self.phi = phi; self.tau_x = 1; self.tau_y = 1; #just in case the input decides to give us weights for e in lattice.edges_iter(): if not lattice.get_edge_data(e[0],e[1]) : #setting lattice lattice.edge[e[0]][e[1]] = {'weight':phi}; lattice.edge[e[1]][e[0]] = {'weight':phi}; else: #keep the data pass; self.lattice , self.data = lattice, data; # convert the lattice into a GMRF precision matrix self.Lambda = zeros(self.num_nodes,self.num_nodes); #set up the grid and the data #@stochastic(dtype=float) #@def X # this v is a tuple index of the grid self.Y = [ Normal('Y_'+str(v), mu=0.5, tau=Sigma_Y**-1, value=data[v], observed = True ) for v in lattice.nodes_iter() ]; MCMC.__init__(self, [self.Y])
# Richard Darst, August 2011 import numpy import networkx from networkx.generators.classic import grid_graph import util import pcd g = grid_graph([20,20], periodic=True) for a,b,d in g.edges(data=True): d['weight'] = -1 G = pcd.Graph.fromNetworkX(g, defaultweight=1) print (numpy.sum(G.imatrix) - numpy.sum(G.imatrix.diagonal()))/(400**2-400.) #exit() assert numpy.all(G.imatrix - G.imatrix.T == 0) MR = pcd.MultiResolution() MR.run([G]*5, gammas=dict(low=.1, high=100, density=10)) MR.write('tmp-lattice.txt') #MR.viz()
# Richard Darst, August 2011 import numpy import networkx from networkx.generators.classic import grid_graph import util import pcd g = grid_graph([20, 20], periodic=True) for a, b, d in g.edges(data=True): d['weight'] = -1 G = pcd.Graph.fromNetworkX(g, defaultweight=1) print(numpy.sum(G.imatrix) - numpy.sum(G.imatrix.diagonal())) / (400**2 - 400.) #exit() assert numpy.all(G.imatrix - G.imatrix.T == 0) MR = pcd.MultiResolution() MR.run([G] * 5, gammas=dict(low=.1, high=100, density=10)) MR.write('tmp-lattice.txt') #MR.viz()