Skip to content

Commit

Permalink
Copied from Microstate1.0
Browse files Browse the repository at this point in the history
Renamed to MST (Microstate Analysis Toolbox)
This is the version that is uploaded to EEGlab's extention page.
  • Loading branch information
atpoulsen committed May 3, 2018
1 parent cb59ed0 commit 67a781d
Show file tree
Hide file tree
Showing 20 changed files with 6,269 additions and 0 deletions.
104 changes: 104 additions & 0 deletions MST1.0/MicroFit.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
%MICROFIT Backfitting microstate prototype maps to data.
%
% Usage:
% >> L = MicroFit(X,A)
% >> L = MicroFit(X,A,polarity)
%
% Please cite this toolbox as:
% Poulsen, A. T., Pedroni, A., & Hansen, L. K. (unpublished manuscript).
% Microstate EEGlab toolbox: An introductionary guide.
%
% Inputs:
% X - EEG (channels x samples (x trials)).
% A - Spatial distribution of microstate prototypes (channels x K).
%
% Optional input:
% polarity - Account for polarity when fitting. Typically off for
% spontaneous EEG and on for ERP data (default = 0).
%
% Output:
% L - Label of the most active microstate at each timepoint (trials x
% time).
%
% Authors:
%
% Andreas Pedroni, [email protected]
% University of Zürich, Psychologisches Institut, Methoden der
% Plastizitätsforschung.
%
% Andreas Trier Poulsen, [email protected]
% Technical University of Denmark, DTU Compute, Cognitive systems.
%
% September 2017.

% Copyright (C) 2017 Andreas Pedroni, [email protected].
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

function L = MicroFit(X,A,polarity)
%% Error check and initialisation
if nargin < 2
help MicroFit;
return;
end
if ~exist('polarity','var')
polarity = 0;
end

[C,N,T] = size(X);
K = size(A,2);

% force average reference
X = X - repmat(mean(X,1),[C,1,1]);


%% Check if the data has more than one trial or not and reshape if necessary
if T > 1 % for epoched data
X = squeeze(reshape(X, C, N*T));
end

%% Calculate the Global map dissimilarity for each Microstates
% Normalise EEG and maps (average reference and gfp = 1 for EEG)
X = X ./ repmat(std(X,1), C, 1); % already have average reference
A = (A - repmat(mean(A,1), C, 1)) ./ repmat(std(A,1), C, 1);

% Global map dissilarity
GMD = nan(K,N*T);
for k = 1:K
GMD(k,:) = sqrt(mean( (X - repmat(A(:,k),1,N*T)).^2 ));
end

% Account for polarity? (recommended 0 for spontaneous EEG)
if polarity == 0
% Polarity invariance
GMDinvpol = nan(K,N*T);
for k = 1:K
GMDinvpol(k,:) = sqrt(mean( (X - repmat(-A(:,k),1,size(X,2))).^2));
end
idx = GMDinvpol < GMD;
GMD(idx) = GMDinvpol(idx);
end

% Sort the GMD to get the labels
[~ ,labels] = sort(GMD,1);


%% Export best labels only
L = labels(1,:);

if T > 1 % for epoched data
L = squeeze(reshape(L, 1, N, T))';
end
end
44 changes: 44 additions & 0 deletions MST1.0/MicroPlotFitmeas.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function MicroPlotFitmeas(Res, Measures, Nrange, plot_idx)
%% Plots measures of fit defined in Measures (as a cell) and contained in Res.
% If more than one measure is given, they are normalised to [0 1] and
% plotted on same plot.
%
% Andreas Trier Poulsen, [email protected]
% Technical University of Denmark, DTU Compute, Cognitive systems.
%
% July 2017.

Nmeasures = length(Measures);
Measure_legends = Measures;
hold all
for m = 1:Nmeasures
measure = Res.(Measures{m})(plot_idx);

if Nmeasures > 1
% Normalise to [0 1]
measure = measure - min(measure);
measure = measure / max(measure);
end

% Plot
plot(Nrange, measure, 'linewidth', 2)
xlim([min(Nrange) max(Nrange)])

if strcmp(Measures{m},'KL_nrm')
Measure_legends{m} = 'KL_{nrm}'; % latex style legend for KL_nrm
end
end

xlabel('Number of Microstates')

if Nmeasures > 1
ylabel('Normalised measure of fit (arbitrary units)')
legend(Measure_legends)
else
ylabel(Measure_legends)
end



end

Loading

0 comments on commit 67a781d

Please sign in to comment.