importigraphimportsys
+################### Start of Internal Conversions ###################
+
+
+[docs]
+defnodenum(edgepairs):
+"""
+ Returns the number of nodes in the graph
+
+ :param edgepairs: list of edge pairs
+ :type edgepairs: list
+
+ :returns: number of nodes in the graph
+ :rtype: integer
+ """
+ nodes=0
+ foreinedgepairs:
+ nodes=np.max([nodes,int(e[0]),int(e[1])])
+ returnnodes
-[docs]
-defgraph2dot(g,filename):
-"""
- Save the graph structure of `g` to a graphviz format dot file with the name `filename`
-
- :param g: ``gunfolds`` graph
- :type g: dictionary (``gunfolds`` graphs)
-
- :param filename: name of the file
- :type filename: string
- """
- G=graph2nx(g)
- nx.drawing.nx_pydot.write_dot(G,filename)
+[docs]
+defGlag2CG(results):
+"""Converts lag graph format to gunfolds graph format,
+ and A and B matrices representing directed and bidirected edges weights.
+
+ Args:
+ results (dict): A dictionary containing:
+ - 'graph': A 3D NumPy array of shape [N, N, 2] representing the graph structure.
+ - 'val_matrix': A NumPy array of shape [N, N, 2] storing edge weights.
+
+ Returns:
+ tuple: (graph_dict, A_matrix, B_matrix)
+ """
+
+ graph_array=results['graph']
+ bidirected_edges=np.where(graph_array=='o-o',1,0).astype(int)
+ directed_edges=np.where(graph_array=='-->',1,0).astype(int)
+
+ graph_dict=adjs2graph(np.transpose(directed_edges[:,:,1]),np.transpose((bidirected_edges[:,:,0])))
+ A_matrix=results['val_matrix'][:,:,1]
+ B_matrix=results['val_matrix'][:,:,0]
+
+ returngraph_dict,A_matrix,B_matrix
+
+
+
+################### Add new functions to internal conversions above #############
+################### End of Internal Conversions ########################
+
+# Dont remove this fake function for automating sphinx build.
+defsphinx_automation_fake():
+ return
+
+################### Start of Clingo Conversions ###################
-[docs]
-defnodenum(edgepairs):
-"""
- Returns the number of nodes in the graph
-
- :param edgepairs: list of edge pairs
- :type edgepairs: list
-
- :returns: number of nodes in the graph
- :rtype: integer
- """
- nodes=0
- foreinedgepairs:
- nodes=np.max([nodes,int(e[0]),int(e[1])])
- returnnodes
-
-
-
-
-[docs]
-defedgepairs2g(edgepairs):
-"""
- Converts edge pairs to a ``gunfolds`` graph
-
- :param edgepairs: list of edge pairs
- :type edgepairs: list
-
- :returns: ``gunfolds`` graph
- :rtype: dictionary (``gunfolds`` graph)
- """
- n=nodenum(edgepairs)
- g={x+1:{}forxinrange(n)}
- foreinedgepairs:
- g[int(e[0])][int(e[1])]=1
- returng
-defGlag2CG(results):
-"""Converts lag graph format to gunfolds graph format,
- and A and B matrices representing directed and bidirected edges weights.
- Args:
- results (dict): A dictionary containing:
- - 'graph': A 3D NumPy array of shape [N, N, 2] representing the graph structure.
- - 'val_matrix': A NumPy array of shape [N, N, 2] storing edge weights.
+################### Add only new functions to clingo conversions above #############
+################### End of Clingo Conversions ########################
- Returns:
- tuple: (graph_dict, A_matrix, B_matrix)
- """
+# Dont remove this fake function for automating sphinx build.
+defsphinx_automation_fake():
+ return
- graph_array=results['graph']
- bidirected_edges=np.where(graph_array=='o-o',1,0).astype(int)
- directed_edges=np.where(graph_array=='-->',1,0).astype(int)
+################### Start of External Conversions ###################
+
+
+[docs]
+defgraph2dot(g,filename):
+"""
+ Save the graph structure of `g` to a graphviz format dot file with the name `filename`
+
+ :param g: ``gunfolds`` graph
+ :type g: dictionary (``gunfolds`` graphs)
+
+ :param filename: name of the file
+ :type filename: string
+ """
+ G=graph2nx(g)
+ nx.drawing.nx_pydot.write_dot(G,filename)
- graph_dict=adjs2graph(np.transpose(directed_edges[:,:,1]),np.transpose((bidirected_edges[:,:,0])))
- A_matrix=results['val_matrix'][:,:,1]
- B_matrix=results['val_matrix'][:,:,0]
- returngraph_dict,A_matrix,B_matrix
+################### Add only new functions to external conversions above #############
+################### End of External Conversions ########################
-[docs]
-defstableVAR(n,density=0.1,dist='beta'):
-"""
- This function keeps trying to create a random graph and a random
- corresponding transition matrix until it succeeds.
-
- :param n: number of nodes in the graph
- :type n: (guess)integer
-
- :param density: ratio of total nodes to n^2 possible nodes
- :type density: (guess)float
-
- :param dist: distribution from which to sample the weights. Available
- options are flat, flatsigned, beta, normal, uniform
- :type dist: (guess)string
-
- :returns:
- :rtype:
- """
- np.random.seed()
- sst=0.9
- r=None
- whilenotr:
- r=getAring(n,density,sst,False,dist=dist)
- ifsst<0.03:
- sst-=0.001
- else:
- sst-=0.01
- ifsst<0:
- sst=0.02
- returnr['graph'],r['transition']
-
-
-
-
-[docs]
-defgenData(n,rate=2,density=0.1,burnin=100,ssize=2000,noise=0.1,dist='beta'):
-"""
- Given a number of nodes this function randomly generates a ring
- SCC and the corresponding stable transition matrix. It tries until
- succeeds and for some graph densities and parameters of the
- distribution of transition matrix values it may take
- forever. Please play with the dist parameter to stableVAR. Then
- using this transition matrix it generates `ssize` samples of data
- and undersamples them by `rate` discarding the `burnin` number of
- samples at the beginning.
-
- :param n: number of nodes in the desired graph
- :type n: (guess)integer
-
- :param rate: undersampling rate (1 - no undersampling)
- :type rate: integer
-
- :param density: density of the graph to be generted
- :type density: (guess) float
-
- :param burnin: number of samples to discard since the beginning of VAR sampling
- :type burnin: integer
-
- :param ssize: how many samples to keep at the causal sampling rate
- :type ssize: (guess)integer
-
- :param noise: noise standard deviation for the VAR model
- :type noise: (guess)float
-
- :param dist: (GUESS)distribution from which to sample the weights. Available
- options are flat, flatsigned, beta, normal, uniform
- :type dist: (guess)string
-
- :returns:
- :rtype:
- """
- g,Agt=stableVAR(n,density=density,dist=dist)
- data=drawsamplesMA(Agt,samples=burnin+ssize*2,nstd=noise)
- data=data[:,burnin:]
- returng,Agt,data[:,::rate]
-
-
-
-
-[docs]
-defestimateSVAR(data,th=0.09):
-"""
- :param data:
- :type data:
-
- :param th: (GUESS)threshold for discarding edges in A and B
- :type th: (guess)float
-
- :returns:
- :rtype:
- """
- A,B=data2AB(data)
- A,B=AB2intAB(A,B,th=th)
- returnA,B
-
-
-
-# option #1
-
-[docs]
-defrandomSVAR(n,rate=2,density=0.1,th=0.09,burnin=100,
- ssize=2000,noise=0.1,dist='beta'):
-"""
- Given a number of nodes this function randomly generates a ring
- SCC and the corresponding stable transition matrix. It tries until
- succeeds and for some graph densities and parameters of the
- distribution of transition matrix values it may take
- forever. Please play with the dist parameter to stableVAR. Then
- using this transition matrix it generates `ssize` samples of data
- and undersamples them by `rate` discarding the `burnin` number of
- samples at the beginning. For these data the funcion solves the
- SVAR estimation maximizing log likelihood and returns the A and B
- matrices.
-
- :param n: number of nodes in the desired graph
- :type n: (guess)integer
-
- :param rate: undersampling rate (1 - no undersampling)
- :type rate: integer
-
- :param density: density of the graph to be generted
- :type density: (guess)float
-
- :param th: threshold for discarding edges in A and B
- :type th: (guess)float
-
- :param burnin: number of samples to discard since the beginning of VAR sampling
- :type burnin: (guess)integer
-
- :param ssize: how many samples to keep at the causal sampling rate
- :type ssize: (guess)integer
-
- :param noise: noise standard deviation for the VAR model
- :type noise: (guess)float
-
- :param dist: (GUESS)distribution from which to sample the weights. Available
- options are flat, flatsigned, beta, normal, uniform
- :type dist: (guess)string
-
- :returns:
- :rtype:
- """
- g,Agt,data=genData(n,rate=rate,density=density,
- burnin=burnin,ssize=ssize,noise=noise,dist=dist)
- A,B=estimateSVAR(data,th=th)
- return{'graph':g,
- 'rate':rate,
- 'graph@rate':bfu.undersample(g,rate-1),
- 'transition':Agt,
- 'estimate':adjs2graph(A,B),
- 'directed':A,
- 'bidirected':B
- }
-
-
-
-# option #2
-
-[docs]
-defnoiseData(data,noise=0.1):
-"""
- :param data:
- :type data:
-
- :param noise: (GUESS)noise standard deviation for the VAR model
- :type noise: (guess)float
-
- :returns:
- :rtype:
- """
- h,w=data.shape
- returndata+np.random.randn(h,w)*noise
-
-
-
-
-[docs]
-defdecide_absences(As):
-"""
- Given a list of binary matrices returns a binary mask for absence
- and presence of edges
-
- :param As: a list of binary matrices
- :type As:
-
- :returns:
- :rtype:
- """
- M=np.zeros(As[0].shape).astype('int')
- M[np.where(np.sum(As,axis=0)>len(As)/2.0)]=1
- returnM
-
-
-
-
-[docs]
-defpresence_probs(As):
-"""
- Given a list of binary matrices returns a frequency of edge
- presence
-
- :param As: a list of binary matrices
- :type As:
-
- :returns:
- :rtype:
- """
- n=len(As)
- M=np.sum([np.zeros(As[0].shape),np.ones(As[0].shape)]+As,axis=0)
- returnM/(n+2.0)
-
-
-
-
-[docs]
-defweight_and_mask(As):
-"""
- Given a list o fbinary matrices returns a weight matrix for
- presences and absences and a mask to identify which are which
-
- :param As: a list of binary matrices
- :type As:
-
- :returns:
- :rtype:
- """
- M=decide_absences(As)
- W=presence_probs(As)
- A=np.ones(M.shape)-W# ansence probs
- A[np.where(M==1)]=W[np.where(M==1)]
- return(1000*(np.log(A)-np.log(1-A))).astype('int'),M
-
-
-
-
-[docs]
-defrandomSVARs(n,repeats=100,rate=2,density=0.1,th=0.09,
- burnin=100,ssize=2000,noise=0.1,strap_noise=0.1):
-"""
- does what requested - help is on the way
-
- :param n: number of nodes in the desired graph
- :type n: integer
-
- :param repeats: how many times to add noise and re-estiamte
- :type repeats: integer
-
- :param rate: undersampling rate (1 - no undersampling)
- :type rate: integer
-
- :param density: density of the graph to be generted
- :type density: (guess)float
-
- :param th: threshold for discarding edges in A and B
- :type th: (guess)float
-
- :param burnin: number of samples to discard since the beginning of
- VAR sampling
- :type burnin: integer
-
- :param ssize: how many samples to keep at the causal sampling rate
- :type ssize: integer
-
- :param noise: noise standard deviation for the VAR model
- :type noise: float
-
- :param strap_noise: amount of noise for bootstrapping
- :type strap_noise: float
-
- :returns:
- :rtype:
- """
- g,Agt,data=genData(n,rate=rate,density=density,
- burnin=burnin,ssize=ssize,noise=noise)
-
- As=[]
- Bs=[]
- A,B=estimateSVAR(data,th=th)
- As.append(A)
- Bs.append(B)
-
- foriinrange(repeats-1):
- A,B=estimateSVAR(noiseData(data,noise=strap_noise),th=th)
- As.append(A)
- Bs.append(B)
-
- A=weight_and_mask(As)
- B=weight_and_mask(Bs)
-
- return{'graph':g,
- 'rate':rate,
- 'graph@rate':bfu.undersample(g,rate-1),
- 'transition':Agt,
- 'directed':A,
- 'bidirected':B
- }
-
-
-# option #3
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/docs/_modules/index.html b/docs/_modules/index.html
index 2c5bf5a6..816df0f1 100644
--- a/docs/_modules/index.html
+++ b/docs/_modules/index.html
@@ -256,7 +256,6 @@
Converts lag graph format to gunfolds graph format,
+and A and B matrices representing directed and bidirected edges weights.
+
+
Parameters:
+
results (dict) – A dictionary containing:
+- ‘graph’: A 3D NumPy array of shape [N, N, 2] representing the graph structure.
+- ‘val_matrix’: A NumPy array of shape [N, N, 2] storing edge weights.
Given a number of nodes this function randomly generates a ring
-SCC and the corresponding stable transition matrix. It tries until
-succeeds and for some graph densities and parameters of the
-distribution of transition matrix values it may take
-forever. Please play with the dist parameter to stableVAR. Then
-using this transition matrix it generates ssize samples of data
-and undersamples them by rate discarding the burnin number of
-samples at the beginning.
-
-
Parameters:
-
-
n ((guess)integer) – number of nodes in the desired graph
-
rate (integer) – undersampling rate (1 - no undersampling)
-
density ((guess) float) – density of the graph to be generted
-
burnin (integer) – number of samples to discard since the beginning of VAR sampling
-
ssize ((guess)integer) – how many samples to keep at the causal sampling rate
-
noise ((guess)float) – noise standard deviation for the VAR model
-
dist ((guess)string) – (GUESS)distribution from which to sample the weights. Available
-options are flat, flatsigned, beta, normal, uniform
Given a number of nodes this function randomly generates a ring
-SCC and the corresponding stable transition matrix. It tries until
-succeeds and for some graph densities and parameters of the
-distribution of transition matrix values it may take
-forever. Please play with the dist parameter to stableVAR. Then
-using this transition matrix it generates ssize samples of data
-and undersamples them by rate discarding the burnin number of
-samples at the beginning. For these data the funcion solves the
-SVAR estimation maximizing log likelihood and returns the A and B
-matrices.
-
-
Parameters:
-
-
n ((guess)integer) – number of nodes in the desired graph
-
rate (integer) – undersampling rate (1 - no undersampling)
-
density ((guess)float) – density of the graph to be generted
-
th ((guess)float) – threshold for discarding edges in A and B
-
burnin ((guess)integer) – number of samples to discard since the beginning of VAR sampling
-
ssize ((guess)integer) – how many samples to keep at the causal sampling rate
-
noise ((guess)float) – noise standard deviation for the VAR model
-
dist ((guess)string) – (GUESS)distribution from which to sample the weights. Available
-options are flat, flatsigned, beta, normal, uniform
Gstar is the undersampled reference graph, while G is the starting
-graph. The code searches over all undersampled version of G to
-find all matches with Gstar
-
-
Parameters:
-
-
Gstar –
-
G (dictionary (gunfolds graphs)) – gunfolds format graph
Gstar is the undersampled reference graph, while G is the starting
+graph. The code searches over all undersampled version of G to
+find all matches with Gstar
+
+
Parameters:
+
+
Gstar –
+
G (dictionary (gunfolds graphs)) – gunfolds format graph