forked from Mirkes/ElMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawMap.m
225 lines (212 loc) · 7.92 KB
/
drawMap.m
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
function drawMap(map, data, varargin)
%drawMap create figure and draw data and maps. If data dimension (number of
%columns) is less than 3 then original data coordinate system is used. If
%data dimension is greater than 3 then projection into space of the first
%three PCs is used for drowing.
%
%Usage
% drawMap(map, data);
% drawMap(__, Name, Value);
%
%Inputs:
% map is object of class MapGeometry (descendant of this class).
% data is n-by-dim matrix of data points. Each row is one point.
% There are several possible Name, value pairs:
% 'classes' is n-by-1 vector of class labels for points. Each label
% is positive integer number which is the index of cell array
% with marker descriptions. Marker descriptions can be specified
% by user in the arguments markColour, markShape, markSize.
% Otherwise standard marker is used for all points.
% 'markColour' is K-by-1 vector of standard Matlab colours ('r', 'g',
% 'b', 'y', 'm', 'c', 'w', 'k'). Value K is number of defined
% markers. If markColour is omitted then 'b' (blue) is used for
% all markers.
% 'markShape' is K-by-1 vector of standard Matlab marker shapes ('o',
% '+', '*', '.', 'x', 's', 'd', '^', 'v', '<', '>', 'p', 'h').
% Value K is number of defined markers. If markShape is omitted
% then 's' (square) is used for all markers.
% 'markSize' is K-by-1 vector of positive numbers. Value K is number
% of defined markers. If markSize is omitted then 6 is used for
% all markers.
% 'nodeMarker' is one of the possible marker shape symbol ('o', '+',
% '*', '.', 'x', 's', 'd', '^', 'v', '<', '>', 'p', 'h') or
% 'none'. Default value is 'o'.
% 'nodeMarkerSize' is positive number which is size of node marker.
% Default value is 6;
% 'nodeColour' is one of the possible Matlab colours ('r', 'g', 'b',
% 'y', 'm', 'c', 'w', 'k'). Default value is 'r';
% 'lineWidth' is non-negative number for map line width. Zero means
% absense of map at all (widht of line is zero and 'nodeMarker'
% is 'none'. Default value is 2.
% Data preprocessing
data = map.preprocessData(data);
%Get data dimension
[N, dim] = size(data);
% Parse varargin
classes = [];
markColour = [];
markShape = [];
markSize = [];
nodeMarker = 'o';
nodeMarkerSize = 6;
nodeColour = 'r';
lineWidth = 2;
for i=1:2:length(varargin)
switch lower(varargin{i})
case 'classes'
classes = varargin{i + 1};
case 'markcolour'
markColour = varargin{i + 1};
case 'markshape'
markShape = varargin{i + 1};
case 'marksize'
markSize = varargin{i + 1};
case 'nodemarker'
nodeMarker = varargin{i + 1};
case 'nodemarkersize'
nodeMarkerSize = varargin{i + 1};
case 'nodecolour'
nodeColour = varargin{i + 1};
case 'linewidth'
lineWidth = varargin{i + 1};
otherwise
error(['Unknown argument at position "', num2str(i + 2)]);
end
end
% Sanity check of arguments.
if isempty(classes)
classes = ones(N, 1);
cls = 1;
else
cls = unique(classes);
end
nCls = length(cls);
if isempty(markColour)
markColour = repmat('b', nCls, 1);
end
if isempty(markShape)
markShape = repmat('s', nCls, 1);
end
if isempty(markSize)
markSize = repmat(6, nCls, 1);
end
if lineWidth == 0
mapDraw = 0;
else
mapDraw = 1;
end
%Create figure
figure;
%Get map coordinates
maps = map.getMappedCoordinates;
%get map links
links = map.getLinks;
if dim > 3 % Dimension is greater than 3 and we need to use PCs
% Calculate three first PCs and project data onto PCs
if isempty(map.PCs)
means = mean(data);
dat = bsxfun(@minus, data, means);
[~, D, V] = svds(dat, 3);
D = diag(D);
[~, ind] = sort(D,'descend');
V = V(:,ind);
data = dat * V;
maps = bsxfun(@minus, maps, means) * V;
end
%Draw data
for k = 1:nCls
ind = classes == cls(k);
plot3(data(ind, 1), data(ind, 2), data(ind, 3),...
[markColour(k), markShape(k)],...
'MarkerFaceColor', markColour(k),...
'MarkerSize', markSize(k));
hold on
end
if mapDraw
%Draw edges
%Prepare arrays
X=[maps(links(:,1), 1)';maps(links(:,2), 1)'];
Y=[maps(links(:,1), 2)';maps(links(:,2), 2)'];
Z=[maps(links(:,1), 3)';maps(links(:,2), 3)'];
%Draw edges
plot3(X, Y, Z, nodeColour, 'LineWidth', lineWidth);
%Draw map nodes
plot3(maps(:, 1), maps(:, 2), maps(:, 3), 'Marker', nodeMarker,...
'MarkerFaceColor', nodeColour, 'MarkerEdgeColor', nodeColour,...
'MarkerSize', nodeMarkerSize, 'LineStyle', 'none');
end
axis equal;
elseif dim == 3
%3d data
%Draw data
for k = 1:nCls
ind = classes == cls(k);
plot3(data(ind, 1), data(ind, 2), data(ind, 3),...
[markColour(k), markShape(k)],...
'MarkerFaceColor', markColour(k),...
'MarkerSize', markSize(k));
hold on
end
if mapDraw
%Draw edges
%Prepare arrays
X=[maps(links(:,1), 1)';maps(links(:,2), 1)'];
Y=[maps(links(:,1), 2)';maps(links(:,2), 2)'];
Z=[maps(links(:,1), 3)';maps(links(:,2), 3)'];
%Draw edges
plot3(X, Y, Z, nodeColour, 'LineWidth', lineWidth);
%Draw map nodes
plot3(maps(:, 1), maps(:, 2), maps(:, 3), 'Marker', nodeMarker,...
'MarkerFaceColor', nodeColour, 'MarkerEdgeColor', nodeColour,...
'MarkerSize', nodeMarkerSize, 'LineStyle', 'none');
end
axis equal;
elseif dim == 2
%two dimensional data
%Draw data
for k = 1:nCls
ind = classes == cls(k);
plot(data(ind, 1), data(ind, 2),...
[markColour(k), markShape(k)],...
'MarkerFaceColor', markColour(k),...
'MarkerSize', markSize(k));
hold on
end
if mapDraw
%Draw edges
%Prepare arrays
X=[maps(links(:,1),1)';maps(links(:,2),1)'];
Y=[maps(links(:,1),2)';maps(links(:,2),2)'];
%Draw edges
plot(X, Y, nodeColour, 'LineWidth', lineWidth);
%Draw map nodes
plot(maps(:,1),maps(:,2), 'Marker', nodeMarker,...
'MarkerFaceColor', nodeColour, 'MarkerEdgeColor', nodeColour,...
'MarkerSize', nodeMarkerSize, 'LineStyle', 'none');
end
axis equal;
else
%one dimensional data
%Draw data
for k = 1:nCls
ind = classes == cls(k);
plot(data(ind, 1), 0,...
[markColour(k), markShape(k)],...
'MarkerFaceColor', markColour(k),...
'MarkerSize', markSize(k));
hold on
end
if mapDraw
%Draw edges
%Prepare arrays
X=[maps(links(:,1),1)'; maps(links(:,2),1)'];
Y=zeros(2,size(links,1));
%Draw edges
plot(X, Y, nodeColour, 'LineWidth', lineWidth);
%Draw map nodes
plot(maps(:,1),0, 'Marker', nodeMarker,...
'MarkerFaceColor', nodeColour, 'MarkerEdgeColor', nodeColour,...
'MarkerSize', nodeMarkerSize, 'LineStyle', 'none');
end
end
end