Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions adj2gephilab/adj2gephilab.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
function EdgeL=adj2gephilab(filename,ADJ,parameters)
% Convert ana adjacency matrix of a graph to 2 spreadhseets csv files
% one for the edge table and the other for node table.
% The files _node.csv and _edge.csv have to be open
%in Gephi via Data Laboratory.
% INPUTS:
% filename: string for the prefix name of the two files .csv
% ADJ: the adjacency matrix
% parameters: vector as properties of the node to use as
% attributes of them.
% OUTPUTS:
% two csv spreadsheet files:
% filename_node.csv
% filename_edge.csv
% EdgeL = it returns the edge list corresponing to the
% adjacency matrix. (it can be saved to be open in Gephi too)
%
% The two files must be open in Gephi via the Data Laboratory

nodecsv=[filename,'_node.csv'];
edgecsv=[filename,'_edge.csv'];
n=size(ADJ,1); % square adjacency matrix

if nargin<3
parameters=ones(n,1);% all nodes have the same attributes
end
ps=parameters;
%% Node Table:
% header for node csv
fidN = fopen(nodecsv,'w','native','UTF-8'); % best format for gephi
fprintf(fidN,'%s\n','Id;Label;Attribute');
%
for i=2:n+1,
fprintf(fidN,'%s\n',[ num2str(i-1) ';"Node ' num2str(i-1) '"'...
';' num2str(ps(i-1))]);
end
fclose(fidN);

%% Edge Table
EdgeL=conv_EdgeList(ADJ);

S=EdgeL(:,1); % sources
T=EdgeL(:,2); % targets
W = EdgeL(:,3); % weights

fidE = fopen(edgecsv,'w','native','UTF-8');
% header for edge csv
fprintf(fidE,'%s\n','Source;Target;Label;Weight');

for i=2:length(S)+1,
fprintf(fidN,'%s\n',[ num2str(S(i-1)) ';' num2str(T(i-1)) ';'...
'"Edge from ' num2str(S(i-1)) ' to ' num2str(T(i-1)) '"' ';'...
num2str(W(i-1))]);
end
fclose(fidE);

%% Aux function
function EdgeL=conv_EdgeList(adj)
% convert adj matrix to edge list
n=size(adj,1); % number of nodes
edges=find(adj>0); % indices of all edges
n_e=length(edges);
EdgeL=zeros(n_e,3);
for e=1:n_e
[i,j]=ind2sub([n,n],edges(e)); % node indices of edge e
EdgeL(e,:)=[i j adj(i,j)];
end
67 changes: 67 additions & 0 deletions adj2gephilab/adj2gephilab2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
function EdgeL=adj2gephilab2(filename,ADJ,parameters,idx)
% Convert ana adjacency matrix of a graph to 2 spreadhseets csv files
% one for the edge table and the other for node table.
% The files _node.csv and _edge.csv have to be open
%in Gephi via Data Laboratory.
% INPUTS:
% filename: string for the prefix name of the two files .csv
% ADJ: the adjacency matrix
% parameters: vector as properties of the node to use as
% attributes of them.
% OUTPUTS:
% two csv spreadsheet files:
% filename_node.csv
% filename_edge.csv
% EdgeL = it returns the edge list corresponing to the
% adjacency matrix. (it can be saved to be open in Gephi too)
%
% The two files must be open in Gephi via the Data Laboratory

nodecsv=[filename,'_node.csv'];
edgecsv=[filename,'_edge.csv'];
n=size(ADJ,1); % square adjacency matrix

if nargin<3
parameters=ones(n,1);% all nodes have the same attributes
end
ps=parameters;
%% Node Table:
% header for node csv
fidN = fopen(nodecsv,'w','native','UTF-8'); % best format for gephi
fprintf(fidN,'%s\n','Id;Label;Attribute');
%
for i=2:n+1,
fprintf(fidN,'%s\n',[ num2str(idx(i-1)) ';"Node ' num2str(idx(i-1)) '"'...
';' num2str(ps(i-1))]);
end
fclose(fidN);

%% Edge Table
EdgeL=conv_EdgeList(ADJ,idx);

S=EdgeL(:,1); % sources
T=EdgeL(:,2); % targets
W = EdgeL(:,3); % weights

fidE = fopen(edgecsv,'w','native','UTF-8');
% header for edge csv
fprintf(fidE,'%s\n','Source;Target;Label;Weight');

for i=2:length(S)+1,
fprintf(fidN,'%s\n',[ num2str(S(i-1)) ';' num2str(T(i-1)) ';'...
'"Edge from ' num2str(S(i-1)) ' to ' num2str(T(i-1)) '"' ';'...
num2str(W(i-1))]);
end
fclose(fidE);

%% Aux function
function EdgeL=conv_EdgeList(adj,idx)
% convert adj matrix to edge list
n=size(adj,1); % number of nodes
edges=find(adj>0); % indices of all edges
n_e=length(edges);
EdgeL=zeros(n_e,3);
for e=1:n_e
[i,j]=ind2sub([n,n],edges(e)); % node indices of edge e
EdgeL(e,:)=[idx(i) idx(j) adj(i,j)];
end
24 changes: 20 additions & 4 deletions test/test_poly_plot.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

% Apply a filter
N = matrix_normalize(A);
c = filter_jackson(moments_delta(0.5, 500));
c = filter_jackson(moments_delta(-0.5, 500));
c(1) = c(1)/2;
pN = mfunc_cheb_poly(c,N);

Expand All @@ -18,22 +18,37 @@
[lambda,I] = sort(diag(D), 'descend');
V = V(:,I);
if length(D) == nprobe
warning('Did not converge to desired threshold\n');
warning('Did not converge to desired threshold\n');
end


% Sort out and select top group by leverage on the subspace
nmode = sum(lambda > 0.95*lambda(1));
score = sum(V(:,1:nmode).^2, 2);
[sscore,I] = sort(score, 'descend');
I = I(1:400);

% Extract top + one-hop neighborhood
Nhi = 40;
I = I(1:Nhi);
for hop=1:2
z = sum(A(:,I),2);
z(I) = 1;
I = find(z > 0);
end

%I = I(1:800);
sscore=sscore(I);
As = A(I,I);
EdgeL=adj2gephilab('test',As,sscore);

%{
% Plot top group
figure;
As = A(I,I);
gplot_shatter(As,4);
%}


%{
% Alternative: Heuristic sparsification
marks = zeros(length(N),1);
nmode = sum(lambda > 0.95*lambda(1));
Expand All @@ -43,3 +58,4 @@
I = I(1:400);
figure;
gplot_shatter(A(I,I),4);
%}
69 changes: 69 additions & 0 deletions test/test_poly_plot.m~
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
% Load one of the Gleich examples
A = load_graph('gleich', 'pgp-cc');

% Apply a filter
N = matrix_normalize(A);
c = filter_jackson(moments_delta(-0.5, 500));
c(1) = c(1)/2;
pN = mfunc_cheb_poly(c,N);

% Pull out representative vectors (should revisit)
nprobe = 200;
thresh = 1e-3;
Z = randn(length(N),nprobe);
AZ = pN(Z);

% Version 0: Eigenvector computation
[V,D] = eig_rand1(Z, AZ, thresh);
[lambda,I] = sort(diag(D), 'descend');
V = V(:,I);
if length(D) == nprobe
warning('Did not converge to desired threshold\n');
end


% Sort out and select top group by leverage on the subspace
nmode = sum(lambda > 0.95*lambda(1));
score = sum(V(:,1:nmode).^2, 2);
[sscore,I] = sort(score, 'descend');

% Extract top + one-hop neighborhood
Nhi = 40;
I = I(1:Nhi);
for hop=1:2
z = sum(A(:,I),2);
z(I) = 1;
I = find(z > 0);
end

%I = I(1:800);
sscore=sscore(I);

%Get it to gephi
As = A(I,I);
EdgeL=adj2gephilab('test',As,sscore);
EdgeL2=adj2gephilab2('test2',As,sscore,I);


%Get it back from gephi
Q=csvread('export_text.csv',1,0);
Ns=full(N(Q,Q));

%{
% Plot top group
figure;
As = A(I,I);
gplot_shatter(As,4);
%}

%{
% Alternative: Heuristic sparsification
marks = zeros(length(N),1);
nmode = sum(lambda > 0.95*lambda(1));
[Q,R,E] = qr(V',0);
VQ = V*Q';
[~,I] = sort(max(abs(VQ(:,j)),[],2), 'descend');
I = I(1:400);
figure;
gplot_shatter(A(I,I),4);
%}