-
Notifications
You must be signed in to change notification settings - Fork 0
/
Step2_addAMICAresults.asv
164 lines (132 loc) · 5.53 KB
/
Step2_addAMICAresults.asv
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
% Add AMICA results from Hipergator and save to new dataset in local folder
% Inputs:
% mydir (string) main directory where output folder will be stored
% subjects (array) numeric array of subject numbers
% downsample flag (int) [0|1] option to downsample data
% newFs (num) new sampling rate. Only works if flag is set ON
%
% Authors: HNL Lab, University of Florida
%set parameters
global mydir Exo_Gait_Events
mydir = 'R:\Ferris-Lab\jacobsen.noelle\Exo Adaptation';
downsample_flag = 1;
newFs = 256;
blueDrive = '\\exasmb.rc.ufl.edu\blue\dferris\jacobsen.noelle\';
AMICA15_path = [blueDrive,'AMICA\AMICA_15\']; %folder containing loadmodout15()
subjects = []; %enter subjets or leave emtpy to auto load all subjects in folder
autoloadAllFiles =1;
outputFolder = [mydir,'\Data\processed_data\',datestr(now, 'yyyy-mm-dd'),'-Step2-AMICA'];
TITLE = 'Load main EEG folder from HPG containing subject subfolders';
[inputfolder] = uigetdir([blueDrive,'AMICA'], TITLE);
if autoloadAllFiles || isempty(subjects)
folderList = dir(inputfolder);
keepidx = contains({folderList.name},'S');
folderList = folderList(keepidx);
else
folderList ={};
x=0;
for subject_num = [subjects]
subject_num = num2str(subject_num);
x= x+1;
folderList{x} = strcat(inputfolder,'\S',subject_num);
end
folderList = cellstr(folderList);
end
if ~exist(outputFolder, 'dir') %check to see if output folder exists, if not make new one
mkdir(outputFolder);
end
addpath(AMICA15_path)
% If you want to run DIPFIT on HPG, you need to copy the component data
% using eeglab2fieldtrip. Create a folder for this data to be sent
% folderList ={};
% i=1;
% for subject_num = [42]
% folderList{i} = strcat('Z:\jacobsen.noelle\AMICA\13-May-2021\S',num2str(subject_num),'_splitbelt\');
% i=i+1;
% end
%% loop
for X = 1:length(folderList)
%load EEG file
if iscell(folderList) %array of chars
folder = folderList {X};
elseif ischar(folderList) %one char
folder = folderList(X,:);
elseif isstruct(folderList)
folder = folderList(X).name;
else
disp('Error in file type selected');
end
amicaFolder = convertCharsToStrings(folder);
%amicaFolder = strcat('M:\dferris\jacobsen.noelle\AMICA\',subject_num,'.',num);
%amicaFolder = strcat('Z:\jacobsen.noelle\AMICA\51221\',folder);
temp = dir(fullfile(amicaFolder,'*.set'));
if length(temp)==1
fileName = temp.name;
disp(fileName);
else
disp('figure this out later'); %ask person to click on file if file doesn't exist or more than one exists
end
%% Open EEGlab (if not already open) and load EEG files
if ~exist('ALLCOM')
addpath('C:\Users\jacobsen.noelle\Desktop\eeglab2021.0');
[ALLEEG EEG CURRENTSET ALLCOM] = eeglab;
end
EEG = pop_loadset('filename',fileName,'filepath',temp.folder);
%check downsample param
if downsample_flag
if newFs> EEG.srate
error('new sampling rate is larger than current sampling rate. Cannot downsample')
end
end
%get rid of old ICA results from infomax
EEG = rmfield(EEG,'icaweights');
EEG.icaweights = [];
EEG = rmfield(EEG,'icawinv');
EEG.icawinv = [];
EEG = rmfield(EEG,'icaact');
EEG.icaact = [];
EEG = rmfield(EEG,'icasphere');
EEG.icasphere = [];
%load up AMICA results
EEG.etc.amicaResultStructure = loadmodout15(temp.folder);
%Insert AMICA results into EEGLAB
EEG.icaweights = EEG.etc.amicaResultStructure.W;
EEG.icawinv = EEG.etc.amicaResultStructure.A;
num_pcs = EEG.etc.amicaResultStructure.num_pcs; %this is to account for doing PCA reduction
EEG.icasphere = EEG.etc.amicaResultStructure.S(1:num_pcs,:); %if you do PCA reduction and send the full S matrix, you will get an error. Someone online said you should take the first X rows where X = your PCA reduction
num_components = num2str(size(EEG.icaweights,1));
%make sure everything is ok and calculate the activation matrix
pop_editoptions('option_computeica',1); %randomly needed for some people to run even if you manually selected this checkbox with the GUI
EEG = eeg_checkset(EEG, 'ica'); %Note: I have no idea why it says "LLt not set" or what LLt is
%store updated EEG structure
AMICA = size(EEG.icaact,1);
AMICA = num2str(AMICA);
EEG.filename = strcat(extractBefore(EEG.filename,'.set'),'_AMICA',AMICA);
[ALLEEG, EEG, CURRENTSET] = eeg_store( ALLEEG, EEG, CURRENTSET );
eeglab redraw
%downsample
if downsample_flag
if EEG.srate > newFs
EEG = pop_resample(EEG,newFs);
EEG.comments = pop_comments(EEG.comments,'',strcat('Downsampled to ',newFs,' Hz'),1);
EEG.setname = strcat([EEG.subject,'','AMICA- downsamp']);
[ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
EEG = eeg_checkset(EEG);
EEG = pop_saveset( EEG, 'filename',strcat(extractBefore(EEG.filename,'.set'),'_DS','.set'),'filepath',outputFolderDS,'version','7.3');
eeglab redraw;
fprintf('Downsampled to %i',newFs);
end
[ALLEEG, EEG, CURRENTSET] = eeg_store( ALLEEG, EEG, CURRENTSET );
eeglab redraw
%save comp data in fieldtrip format for use in custom head model later
comp = eeglab2fieldtrip(EEG, 'componentanalysis');
outputFolder_compdat = [blueDrive,'CustomHeadModel\Data\',datestr(now, 'yyyy-mm-dd'),'-Exo\',EEG.subject];
if ~exist(outputFolder_compdat, 'dir') %check to see if output folder exists, if not make new one
mkdir(outputFolder_compdat);
end
save([outputFolder_compdat,'\comp.mat'],'comp'); clear comp
%save dataset
EEG = pop_saveset( EEG, 'filename',EEG.filename,'filepath',outputFolder, 'version', '7.3');
%update GUI
%eeglab redraw
end