-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsaveForBRAINtrinsic.m
More file actions
89 lines (83 loc) · 3.02 KB
/
saveForBRAINtrinsic.m
File metadata and controls
89 lines (83 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function saveForBRAINtrinsic(folder, subjectid, labels, network, topologies, clusters)
%%
% folder output foler
% subjectid subject identification
% labels labels keymap, should be a column
% network connectome
% topologies structure of different topologies: isomap, tsne, mds
% should be a 3 columns array [x y z] (optional)
% clusters structure containing clustering data like PLACE or PACE
% should be a column (optional)
% This function generates BRAINtrinsic compatible csv files from input. Two
% files are generated in the pointed folder: a network file and a topology
% file. The network file contains the adjacency matrix. The topology file
% contains either topology data represented in 3 consective columns or
% clustering data represented in one column.
%% checks
if ~isempty(topologies) && ~isstruct(topologies)
error('Topologies must be a structure with each field refer to a 3 columns array');
end
labels = labels(:);
nLabels = length(labels);
%% write network data
filename = [folder, '\NW', subjectid, '.csv'];
fprintf('Writing %s\n', filename)
csvwrite(filename, network);
%% write topology data
filename = [folder, '\topology', subjectid, '.csv'];
header = 'label';
data = labels;
if ~isempty(topologies)
topologiesNames = fieldnames(topologies);
for i = 1:length(topologiesNames)
topology = topologiesNames{i};
header = [header, ',', topology, ',,'];
if size(topologies.(topology),2) ~= 3
error('A topology should contain labels centroids in 3D, it must be a 3 columns array');
end
try
data = [data, topologies.(topology)];
catch
error('Labels and Topologies should have the same number of rows');
end
end
end
if exist('clusters', 'var')
clustersNames = fieldnames(clusters);
for i = 1:length(clustersNames)
header = [header, ',', clustersNames{i}];
cluster = clusters.(clustersNames{i});
if iscell(cluster)
cluster = treeToCluster(cluster);
end
cluster = cluster(:);
try
data = [data, cluster];
catch
error('Labels and Clusters should have the same number of rows');
end
end
end
fprintf('Writing %s\n', filename)
fid = fopen(filename, 'w');
fprintf(fid, [header,'\n']);
fclose(fid)
dlmwrite(filename, data, '-append', 'precision', '%.2f', 'delimiter', ',');
end
function cluster = treeToCluster(tree)
%% This function returns an array of communities from a cell array
% of a cell array community structure input . The input should be
% a cell array containing vectors, each represent the indeces of
% the nodes in the same group, the output is a row vector containing
% the group number of the corresponding index.
%%
IdxOrd = tree(end,:);
nNodes = sum(cellfun(@numel,tree(end,:)));
cluster = zeros(1,nNodes);
CommN = 1;
for j = 1:length(IdxOrd)
if isempty(IdxOrd{j}); continue; end
cluster(IdxOrd{j}) = CommN;
CommN = CommN+1;
end
end