[422372]: / functions / @mmo / subsasgn.m

Download this file

281 lines (256 with data), 10.8 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
 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
% SUBSASGN - define index assignment for eegdata objects
%
% Author: Arnaud Delorme, SCCN, INC, UCSD, Nov. 2008
% Copyright (C) 2008 Arnaud Delorme, SCCN, INC, UCSD
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
function obj = subsasgn(obj,ss,val)
% check empty assignment
% -----------------------
for index = 1:length(ss(1).subs)
if isempty(ss(1).subs{index}), return; end
end
% remove useless ":"
% ------------------
while length(obj.dimensions) < length(ss(1).subs)
if isequal(ss(1).subs{end}, ':')
ss(1).subs(end) = [];
else
break;
end
end
% deal with transposed data
% -------------------------
if obj.transposed, ss = transposeindices(obj, ss); end
% check stack and local variables
% ---------------------
ncopies = checkworkspace(obj);
if ncopies < 2
s = evalin('caller', 'whos');
for index = 1:length(s)
if strcmpi(s(index).class, 'struct') || strcmpi(s(index).class, 'cell')
tmpVar = evalin('caller', s(index).name);
ncopies = ncopies + checkcopies_local(obj, tmpVar);
elseif strcmpi(s(index).class, 'mmo')
if s(index).persistent || s(index).global
disp('Warning: mmo objects should not be made persistent or global. Behavior is unpredictable.');
end
tmpVar = evalin('caller', s(index).name);
if isequal(tmpVar, obj), ncopies = ncopies + 1; end
if ncopies > 1, break; end
end
end
end
% removing some entries
% ---------------------
if isempty(val)
newdim1 = obj.dimensions;
newdim2 = obj.dimensions;
% create new array of new size
% ----------------------------
tmpMMO = memmapfile(obj.dataFile, 'writable', obj.writable, 'format', { 'single' obj.dimensions 'x' });
newFileName = mmo.getnewfilename;
% find non singleton dimension
% ----------------------------
nonSingleton = [];
ss2 = ss;
for index = 1:length(ss(1).subs)
if ~ischar(ss(1).subs{index}) % can only be ":"
nonSingleton(end+1) = index;
if islogical( ss(1).subs{index}), ss(1).subs{index} = find( ss(1).subs{index}); end
ss2(1).subs{index} = setdiff_bc([1:newdim1(index)], ss(1).subs{index}); % invert selection
end
end
if length(nonSingleton) > 1, error('A null assignment can have only one non-colon index'); end
if isempty(nonSingleton), obj = []; return; end
% compute new final dimension and copy data
% -----------------------------------------
if length(ss(1).subs) == 1
fid = fopen(newFileName, 'w');
newdim2 = [ prod(newdim2)-length(ss(1).subs{1}) ];
if ~(newdim1(1) > 1 && all(newdim1(2:end) == 1)), newdim2 = [1 newdim2];
else newdim2 = [newdim2 1]; end
newindices = setdiff_bc([1:prod(newdim1)], ss(1).subs{1});
for index = newindices
fwrite(fid, tmpMMO.Data.x(index), 'float');
end
fclose(fid);
fprintf('Warning: memory mapped object writing might not be up to date in cache on network drive');
else
if length(ss(1).subs) < length(newdim2)
newdim2(length(ss(1).subs)) = prod(newdim2(length(ss(1).subs):end));
newdim2(length(ss(1).subs)+1:end) = [];
if nonSingleton == length(ss(1).subs)
ss2(1).subs{end} = setdiff_bc([1:newdim2(end)], ss(1).subs{end});
end
end
newdim2(nonSingleton) = newdim2(nonSingleton)-length(ss(1).subs{nonSingleton});
if ischar(ss2.subs{end})
ss2.subs{end} = [1:prod(newdim1(length(ss2.subs):end))];
end
ss3 = ss2;
fid = fopen(newFileName, 'w');
% copy large blocks
alllen = cellfun(@length, ss2.subs);
inc = ceil(250000/prod(alllen(1:end-1))); % 1Mb block
for index = 1:inc:length(ss2.subs{end})
ss3.subs{end} = ss2.subs{end}(index:min(index+inc, length(ss2.subs{end})));
tmpdata = subsref(tmpMMO.Data.x, ss3);
fwrite(fid, tmpdata, 'float');
end
fclose(fid);
fprintf('Warning: memory mapped object writing might not be up to date in cache on network drive');
end
% delete file if necessary
if ncopies == 1 && obj.writable
delete(obj.dataFile);
end
if obj.debug, disp('new copy created, old deleted (length 1)'); end
obj.dimensions = newdim2;
obj.dataFile = newFileName;
obj.writable = true;
obj = updateWorkspace(obj);
clear tmpMMO;
return;
else
% check size to see if it increases
% ---------------------------------
newdim1 = obj.dimensions;
newdim2 = newdim1;
if length(ss(1).subs) == 1
if ~ischar(ss(1).subs{1}) && max(ss(1).subs{1}) > prod(newdim1)
if length(newdim1) > 2
error('Attempt to grow array along ambiguous dimension.');
end
end
if max(ss(1).subs{1}) > prod(newdim2)
notOneDim = find(newdim2 > 1);
if length(notOneDim) == 1
newdim2(notOneDim) = max(ss(1).subs{1});
end
end
else
if length(newdim1) == 3 && newdim1(3) == 1, newdim1(end) = []; end
if length(ss(1).subs) == 2 && length(newdim1) == 3
if ~ischar(ss(1).subs{2}) && max(ss(1).subs{2}) > prod(newdim1(2:end))
error('Attempt to grow array along ambiguous dimension.');
end
if isnumeric(ss(1).subs{1}), newdim2(1) = max(max(ss(1).subs{1}), newdim2(1)); end
else
for index = 1:length(ss(1).subs)
if isnumeric(ss(1).subs{index})
if index > length(newdim2)
newdim2(index) = max(ss(1).subs{index});
else newdim2(index) = max(max(ss(1).subs{index}), newdim2(index));
end
end
end
end
end
% create new array of new size if necessary
% -----------------------------------------
if ~isequal(newdim1, newdim2)
tmpMMO = memmapfile(obj.dataFile, 'writable', obj.writable, 'format', { 'single' obj.dimensions 'x' });
newFileName = mmo.getnewfilename;
% copy file row by row
% --------------------
fid = fopen(newFileName, 'w');
dim1 = [ newdim1 1 1 1 1 ];
dim2 = [ newdim2 1 1 1 1 ];
tmpdata1 = zeros(prod(dim2(1:1)) - prod(dim1(1:1)), 1, 'single');
tmpdata2 = zeros((dim2(2) - dim1(2))*dim2(1), 1, 'single');
tmpdata3 = zeros((dim2(3) - dim1(3))*prod(dim2(1:2)), 1, 'single');
% copy new data (copy first array)
% -------------
for index3 = 1:dim1(3)
if dim1(1) == dim2(1) && dim1(2) == dim2(2)
fwrite(fid, tmpMMO.Data.x(:,:,index3), 'float');
else
for index2 = 1:dim1(2)
if dim1(1) == dim2(1)
fwrite(fid, tmpMMO.Data.x(:,index2,index3), 'float');
else
for index1 = 1:dim1(1)
fwrite(fid, tmpMMO.Data.x(index1,index2,index3), 'float');
end
end
fwrite(fid, tmpdata1, 'float');
end
end
fwrite(fid, tmpdata2, 'float');
end
fwrite(fid, tmpdata3, 'float');
fclose(fid);
fprintf('Warning: memory mapped object writing might not be up to date in cache on network drive');
% delete file if necessary
if ncopies == 1 && obj.writable
delete(obj.dataFile);
end
if obj.debug, disp('new copy created, old deleted'); end
obj.dimensions = newdim2;
obj.dataFile = newFileName;
obj.writable = true;
clear tmpMMO;
% create copy if necessary
% ------------------------
elseif ncopies > 1 || ~obj.writable
newFileName = mmo.getnewfilename;
copyfile(obj.dataFile, newFileName);
obj.dataFile = newFileName;
obj.writable = true;
if obj.debug, disp('new copy created'); end
else
if obj.debug, disp('using same copy'); end
end
% copy new data
tmpMMO = memmapfile(obj.dataFile, 'writable', obj.writable, 'format', { 'single' obj.dimensions 'x' });
if ~isa(val, 'mmo')
tmpMMO.Data.x = builtin('subsasgn', tmpMMO.Data.x, ss, val);
else
% copy memory mapped array
if ndims(val) == 2 && (size(val,1) == 1 || size(val,2) == 1)
% vector direct copy
ss2.type = '()';
ss2.subs = { ':' ':' ':' };
tmpMMO.Data.x = builtin('subsasgn', tmpMMO.Data.x, ss, subsref(val,ss2));
else
ss2.type = '()';
ss2.subs = { ':' ':' ':' };
ss3 = ss;
% array, copy each channel
for index1 = 1:size(val,1)
ss2(1).subs{1} = index1;
if ischar(ss(1).subs{1})
ss3(1).subs{1} = index1;
else
ss3(1).subs{1} = ss(1).subs{1}(index1);
end
tmpMMO.Data.x = builtin('subsasgn', tmpMMO.Data.x, ss3, subsref(val,ss2));
end
end
end
obj = updateWorkspace(obj);
end