function pat = tangram_pattern (log_fn, lo)
%TANGRAM_PATTERN  Most "popular" binary patterns in boosting.ECC.
%
%   TANGRAM_PATTERN('run/tangram/ecoc_1_14', [3 2]);
%   The second argument, LAYOUT, is optional.
%    [3 2] means using subplot, 3 rows, 2 columns.
%    6 means using 6 separate figures.
%
% See also TANGRAM_BINARY, tangram_pattern.sh

if nargin < 2, lo = [2 2]; end

[c,o] = system(['./tangram_pattern.sh ' log_fn]);
p = eval(['[' o ']']);
[runs,k] = size(p);
k = k - 1; % k should be 7

% find out unique rows
pat = []; cur = 0;
for i = 1:runs
    if i == 1 || ~all(p(i,1:k) == pat(cur,1:k))
        cur = cur + 1;
        pat(cur,:) = [p(i,1:k) 0 0];
    end
    pat(cur,k+1:k+2) = pat(cur,k+1:k+2) + [1 p(i,k+1)];
end

pat = sortrows(pat, k+2);

if length(lo) > 1, figure; end
for i = 1:prod(lo)
    p = pat(end-i+1, :);
    if length(lo) > 1
        subplot(lo(1), lo(2), i);
        title(['\Sigma\alpha: ' num2str(p(k+2)) ', times: ', int2str(p(k+1))]);
    else
        figure
    end
    tangram_binary(p(1:k));
end
