get_runs_test.m 16.4 KB
Newer Older
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
% Figure out a way to identify runs longer than some specified length for
% the purposes of calculating the volume of water which is subjected to a
% change in a parameter of some value. 

%% 1D

% close all

clear testChange negIdx dn startEndIdx gotTo inc longOnesIdx

testIdx=88;

dtJump=plotOPTS.change_type;
lengthThreshold=12;
changeThreshold=-0.5;

% Model results
testData=(squeeze(phChange(testIdx,1,:)));
testTime=plotOPTS.Time_record(1:end-dtJump-1);
% Synthetic data
% testData=[10,10,9,8,7,6,4,2,0,-2,-4,-6,-8,-10,-20,-30,-40,-50,-60,-70,-80,-90,-100,-110,-109,-108,-107,-106,-105,-104,-103,-102,-101,-100,-99,-97,-95,-96,-97,-98,-99,-99,-99,-99,-99,-100,-101,-102,-103,-105,-107,-109,-111,-113,-115,-117,-119,-129,-130,-130,-129,-128,-127];
% testTime=1:length(testData)-1;

testChange=diff(testData);

allIdx=1:length(testData); % array of index positions
negIdx=testChange<0; % where the negative values are. 
dn=diff(negIdx); % the boundaries of those negative data

% Find locations at which dn changes from zero to non-zero
dnIdxStart=allIdx(dn==1)+1; % add one to move start along to correct index
dnIdxEnd=allIdx(dn==-1);

% Check if the start and end are identical (i.e. we have a spike).
if (numel(dnIdxStart)==1 || numel(dnIdxEnd)==1)
    if dnIdxStart==dnIdxEnd
        warning('Single spike in time series, so carry on.')
        continue
    end
end

if sum(dnIdxStart)==0 || sum(dnIdxEnd)==0
    % No appropriate values here, so carry on.
    continue
end

% If arrays are different lengths, lop off the last one from the longer
% array.
if length(dnIdxEnd)>length(dnIdxStart)
    timeIndices=(dnIdxEnd(1:end-1)-dnIdxStart)>=lengthThreshold;
elseif length(dnIdxEnd)<length(dnIdxStart)
    timeIndices=(dnIdxEnd-dnIdxStart(1:end-1))>=lengthThreshold;
elseif length(dnIdxEnd)==length(dnIdxStart)
    if dnIdxStart(1)==1
        timeIndices=(dnIdxEnd(1:end-1)-dnIdxStart(2:end))>=lengthThreshold;
    elseif dnIdxEnd(1)==1
        timeIndices=(dnIdxEnd(2:end)-dnIdxStart(1:end-1))>=lengthThreshold;
    else
        timeIndices=(dnIdxEnd-dnIdxStart)>=lengthThreshold;
    end
else
    return
end

if sum(timeIndices~=0)
    longOnesIdx(:,1)=dnIdxStart(timeIndices);
    longOnesIdx(:,2)=dnIdxEnd(timeIndices);
else
    return
end

% Check the change magnitude for those time steps. Here we have to decide
% if we're doing average change over the n-hour time period, or the maximum
% instantaneous change, or ... For a first go, we'll do the average change
% and use that for the threshold comparison.
checkInc=1;
totalVolume=0;
for jj=1:size(longOnesIdx,2)
    averageChange=mean(testData(longOnesIdx(jj,1):longOnesIdx(jj,2)));
    if averageChange<changeThreshold
        totalVolume=totalVolume+cellVolume(longOnesIdx(jj,1):longOnesIdx(jj,2));
        checkInc=checkInc+1;
    end
end

close all

figure(2)
plot(testTime,testChange)
hold on
plot(testTime(testChange<0),testChange(testChange<0),'r.')
% plot(testTime(4:end)-((testTime(2)-testTime(1))/2),dn*0.01,'gp')
for i=1:size(longOnesIdx,1)
    plot(testTime(longOnesIdx(i,:)),zeros(1,length(longOnesIdx(i,:))),'g-x')
    text(mean(testTime(longOnesIdx(i,:))),0.01,num2str((testTime(longOnesIdx(i,2))-testTime(longOnesIdx(i,1)))*24))
end

%% 1Dv2

close all

clear testChange negIdx dn startEndIdx gotTo inc longOnesIdx

testIdx=124;

dtJump=plotOPTS.change_type;
lengthThreshold=4;
changeThreshold=-1.5;

% Model results
testData=(squeeze(phChange(testIdx,1,:)));
testTime=plotOPTS.Time_record(1:end-dtJump-1);
% Synthetic data
% testData=[10,10,9,8,7,6,4,2,0,-2,-4,-6,-8,-10,-20,-30,-40,-50,-60,-70,-80,-90,-100,-110,-109,-108,-107,-106,-105,-104,-103,-102,-101,-100,-99,-97,-95,-96,-97,-98,-99,-99,-99,-99,-99,-100,-101,-102,-103,-105,-107,-109,-111,-113,-115,-117,-119,-129,-130,-130,-129,-128,-127];
% testTime=1:length(testData)-1;

testChange=diff(testData);

allIdx=1:length(testData); % array of index positions
negIdx=testChange<0; % where the negative values are. 
dn=diff(negIdx); % the boundaries of those negative data

% Find locations at which dn changes from zero to non-zero
dnIdxStart=allIdx(dn==1)+1; % add one to move start along to correct index
dnIdxEnd=allIdx(dn==-1);

% Check if the start and end are identical (i.e. we have a spike).
if (numel(dnIdxStart)==1 || numel(dnIdxEnd)==1)
    if dnIdxStart==dnIdxEnd
        warning('Single spike in time series, so carry on.')
    end
end
% Check for no values
if sum(dnIdxStart)==0 || sum(dnIdxEnd)==0
    % No appropriate values here, so carry on.
    warning('No appropriate values here, so carry on.')
end
% Check the first index in dnIdxEnd is larger than the dnIdxStart. 
if dnIdxEnd(1)<dnIdxStart(1)
    % Strip it out and adjust the dnIdxStart index accordingly.
    dnIdxEnd=dnIdxEnd(2:end);
    dnIdxStart=dnIdxStart(1:end-1);
end


% If arrays are different lengths, lop off the last one from the longer
% array.
if size(dnIdxEnd,2)>size(dnIdxStart,2)
    longOnesIdx(:,1)=dnIdxStart((dnIdxEnd(1:end-1)-dnIdxStart)>=lengthThreshold);
    longOnesIdx(:,2)=dnIdxEnd((dnIdxEnd(1:end-1)-dnIdxStart)>=lengthThreshold);
elseif size(dnIdxEnd,2)<size(dnIdxStart,2)
    longOnesIdx(:,1)=dnIdxStart((dnIdxEnd-dnIdxStart(1:end-1))>=lengthThreshold);
    longOnesIdx(:,2)=dnIdxEnd((dnIdxEnd-dnIdxStart(1:end-1))>=lengthThreshold);
else
    if dnIdxStart(1)==1
        longOnesIdx(:,1)=dnIdxStart((dnIdxEnd(1:end-1)-dnIdxStart(2:end))>=lengthThreshold);
        longOnesIdx(:,2)=dnIdxEnd((dnIdxEnd(1:end-1)-dnIdxStart(2:end))>=lengthThreshold);
    elseif dnIdxEnd(1)==1
        longOnesIdx(:,1)=dnIdxStart((dnIdxEnd(2:end)-dnIdxStart(1:end-1))>=lengthThreshold);
        longOnesIdx(:,2)=dnIdxEnd((dnIdxEnd(2:end)-dnIdxStart(1:end-1))>=lengthThreshold);
    else
        longOnesIdx(:,1)=dnIdxStart((dnIdxEnd-dnIdxStart)>=lengthThreshold);
        longOnesIdx(:,2)=dnIdxEnd((dnIdxEnd-dnIdxStart)>=lengthThreshold);
    end        
end

close all

plot(testTime,testChange)
hold on
plot(testTime(testChange<0),testChange(testChange<0),'r.')
% plot(testTime(4:end)-((testTime(2)-testTime(1))/2),dn*0.01,'gp')
for i=1:size(longOnesIdx,1)
    plot(testTime(longOnesIdx(i,:)),zeros(1,length(longOnesIdx(i,:))),'g-x')
    text(mean(testTime(longOnesIdx(i,:))),0.01,num2str((testTime(longOnesIdx(i,2))-testTime(longOnesIdx(i,1)))*24))
end


%% 2D

clear testChange negIdx dn startEndIdx gotTo inc allInc longOnesIdx

dtJump=plotOPTS.change_type;
lengthThreshold=7;
changeThreshold=-0.1;

testData=squeeze(phChange(:,1,:));
testTime=plotOPTS.Time_record(1:end-dtJump-1);

inc=1;

% Easiest way I can think of doing this is to just loop through each
% element, doing each timestep as a 1D analysis.
for i=1:size(testData,1);
    disp(i)
    testChange=diff(testData(i,:),1,2);
    allIdx=1:length(testChange);
    negIdx=testChange<0; % where the negative values are. 
    dn=diff(negIdx,1,2);

    % Add one to move start along to correct index. Since the diff
    % describes the relationship between the first and second value in
    % testData at the first value of dn. So, if you have a change between
    % the first and second testData values, then the start of the run is
    % necessarily at the second index in testData. For the end though, the
    % index in dn will describe the change from the run to a non-run part,
    % so we don't need to adjust that in the same way. We need also to
    % adjust the values of testChange when comparine them with 
    %
    % allIdx:   |  1  |  2  |  3  |  4  |  5  |  6  |  7  |
    % testData: |  P  |     |     |     |     |  P  |     |
    %           |     |  N  |  N  |  N  |  N  |     |  N  | 
    % negIdx:   |  0  |  1  |  1  |  1  |  1  |  0  |  1  | 
    % dn:       |  1  |  0  |  0  |  0  | -1  |  1  |  ~  |
    % 
    % So you can see that dn is positive before the start of the N run
    % whilst dn is negative at the last element of the run. So we need to
    % increase the idxStart value by one. The idxEnd doesn't need to be
    % modified this way.
    idxStart_dn=allIdx(dn==1)+1;
%     idxStart_tc=allIdx(testChange(1:end-1)<changeThreshold);
%     dnIdxStart=intersect(idxStart_dn,idxStart_tc);
    dnIdxStart=idxStart_dn;
    idxEnd_dn=allIdx(dn==-1);
%     idxEnd_tc=allIdx(testChange(1:end-1)<changeThreshold);
%     dnIdxEnd=intersect(idxEnd_dn,idxEnd_tc);
    dnIdxEnd=idxEnd_dn;

    % QC the picked results.

    % Check if the start and end are identical (i.e. we have a spike).
    if (numel(dnIdxStart)==1 || numel(dnIdxEnd)==1)
        if dnIdxStart==dnIdxEnd
            warning('Single spike in time series, so carry on.')
            continue
        end
    end
    % Check for no values
    if sum(dnIdxStart)==0 || sum(dnIdxEnd)==0
        % No appropriate values here, so carry on.
        warning('No appropriate values here, so carry on.')
        continue
    end
    % Check the first index in dnIdxEnd is larger than the dnIdxStart. 
    if dnIdxEnd(1)<dnIdxStart(1)
        % Strip it out and adjust the dnIdxStart index accordingly.
        dnIdxEnd=dnIdxEnd(2:end);
        dnIdxStart=dnIdxStart(1:end-1);
    end

    % If arrays are different lengths, lop off the last one from the longer
    % array.
    if size(dnIdxEnd,2)>size(dnIdxStart,2)
        % Not ideal with the try's, but it seems to work...
        try
            timeRange(:,1)=dnIdxStart((dnIdxEnd(1:end-1)-dnIdxStart)>=lengthThreshold);
            timeRange(:,2)=dnIdxEnd((dnIdxEnd(1:end-1)-dnIdxStart)>=lengthThreshold);
        catch %#ok<CTCH>
            continue
        end
    elseif size(dnIdxEnd,2)<size(dnIdxStart,2)
        try
            timeRange(:,1)=dnIdxStart((dnIdxEnd-dnIdxStart(1:end-1))>=lengthThreshold);
            timeRange(:,2)=dnIdxEnd((dnIdxEnd-dnIdxStart(1:end-1))>=lengthThreshold);
        catch %#ok<CTCH>
            continue
        end
    else
        try
            timeRange(:,1)=dnIdxStart((dnIdxEnd-dnIdxStart)>=lengthThreshold);
            timeRange(:,2)=dnIdxEnd((dnIdxEnd-dnIdxStart)>=lengthThreshold);
        catch %#ok<CTCH>
            continue
        end
    end
    
    % Now do the magnitude check now.
    timeRange=timeRange(testChange(timeRange(:,1))<changeThreshold & testChange(timeRange(:,2))<changeThreshold,:);
    % If the start and end ranges are non-zero in length, add them to the
    % longOnesIdx cell array.
    if numel(timeRange)>=2
        longOnesIdx{inc}=timeRange;
        allInc(inc)=i;
        inc=inc+1;
    end
end

%% 2Dv2

clear testChange negIdx dn startEndIdx gotTo inc allInc longOnesIdx timeRange

dtJump=plotOPTS.change_type;
lengthThreshold=2;
changeThreshold=-1;

testData=squeeze(phChange(:,1,:));
testTime=plotOPTS.Time_record(1:end-dtJump-1);

inc=1;

% Easiest way I can think of doing this is to just loop through each
% element, doing each timestep as a 1D analysis.
for i=222%1:size(testData,1);
    disp(i)
    % Model results
    testData=squeeze(phChange(i,1,:))*10000000000;
    testTime=plotOPTS.Time_record(1:end-dtJump-1);
    % Synthetic data
    % testData=[10,10,9,8,7,6,4,2,0,-2,-4,-6,-8,-10,-20,-30,-40,-50,-60,-70,-80,-90,-100,-110,-109,-108,-107,-106,-105,-104,-103,-102,-101,-100,-99,-97,-95,-96,-97,-98,-99,-99,-99,-99,-99,-100,-101,-102,-103,-105,-107,-109,-111,-113,-115,-117,-119,-129,-130,-130,-129,-128,-127];
    % testTime=1:length(testData)-1;

    testChange=diff(testData);

    allIdx=1:length(testData); % array of index positions
    negIdx=testChange<0; % where the negative values are. 
    dn=diff(negIdx); % the boundaries of those negative data

    % Find indices at which dn changes from zero to non-zero
    dnIdxStart=allIdx(dn==1)+1; % add one to move start along to correct index
    dnIdxEnd=allIdx(dn==-1);

    % Check if the start and end are identical (i.e. we have a spike).
    if (numel(dnIdxStart)==1 || numel(dnIdxEnd)==1)
        if dnIdxStart==dnIdxEnd
            warning('Single spike in time series, so carry on.')
            continue
        end
    end
    % Check for no values
    if sum(dnIdxStart)==0 || sum(dnIdxEnd)==0
        % No appropriate values here, so carry on.
        warning('No appropriate values here, so carry on.')
        continue
    end
    % Check the first index in dnIdxEnd is larger than the dnIdxStart. 
    if dnIdxEnd(1)<dnIdxStart(1)
        % Strip it out and adjust the dnIdxStart index accordingly.
        dnIdxEnd=dnIdxEnd(2:end);
        dnIdxStart=dnIdxStart(1:end-1);
    end

    % If arrays are different lengths, lop off the last one from the longer
    % array.
    clear timeRange
    if size(dnIdxEnd,2)>size(dnIdxStart,2)
        % Not ideal with the try's, but it seems to work...
        try
            timeRange(:,1)=dnIdxStart((dnIdxEnd(1:end-1)-dnIdxStart)>=lengthThreshold);
            timeRange(:,2)=dnIdxEnd((dnIdxEnd(1:end-1)-dnIdxStart)>=lengthThreshold);
        catch %#ok<CTCH>
            continue
        end
    elseif size(dnIdxEnd,2)<size(dnIdxStart,2)
        try
            timeRange(:,1)=dnIdxStart((dnIdxEnd-dnIdxStart(1:end-1))>=lengthThreshold);
            timeRange(:,2)=dnIdxEnd((dnIdxEnd-dnIdxStart(1:end-1))>=lengthThreshold);
        catch %#ok<CTCH>
            continue
        end
    else
        try
            timeRange(:,1)=dnIdxStart((dnIdxEnd-dnIdxStart)>=lengthThreshold);
            timeRange(:,2)=dnIdxEnd((dnIdxEnd-dnIdxStart)>=lengthThreshold);
        catch %#ok<CTCH>
            continue
        end
    end

    % Now do the magnitude check now.
    timeRange=timeRange(testChange(timeRange(:,1))<changeThreshold & testChange(timeRange(:,2))<changeThreshold,:);
    
    % If the start and end ranges are non-zero in length, add them to the
    % longOnesIdx cell array.
    if numel(timeRange)>=2
        longOnesIdx{inc}=timeRange;
        allInc(inc)=i;
        inc=inc+1;
    end

%     if size(dnIdxEnd,2)>size(dnIdxStart,2)
%         try
%             timeRange
%             longOnesIdx{inc}(:,1)=dnIdxStart((dnIdxEnd(1:end-1)-dnIdxStart)>=lengthThreshold);
%             longOnesIdx{inc}(:,2)=dnIdxEnd((dnIdxEnd(1:end-1)-dnIdxStart)>=lengthThreshold);
%             allInc(inc)=i;
%             inc=inc+1;
%         end
%     elseif size(dnIdxEnd,2)<size(dnIdxStart,2)
%         try
%             longOnesIdx{inc}(:,1)=dnIdxStart((dnIdxEnd-dnIdxStart(1:end-1))>=lengthThreshold);
%             longOnesIdx{inc}(:,2)=dnIdxEnd((dnIdxEnd-dnIdxStart(1:end-1))>=lengthThreshold);
%             allInc(inc)=i;
%             inc=inc+1;
%         end
%     else
%         try
%             longOnesIdx{inc}(:,1)=dnIdxStart((dnIdxEnd-dnIdxStart)>=lengthThreshold);
%             longOnesIdx{inc}(:,2)=dnIdxEnd((dnIdxEnd-dnIdxStart)>=lengthThreshold);
%             allInc(inc)=i;
%             inc=inc+1;
%         end
%     end

   
%     close all
% 
%     plot(testTime,testChange)
%     hold on
%     plot(testTime(testChange<0),testChange(testChange<0),'r.')
%     % plot(testTime(4:end)-((testTime(2)-testTime(1))/2),dn*0.01,'gp')
%     for j=1:size(longOnesIdx{inc-1},1)
%         plot(testTime(longOnesIdx{inc}(j,:)),zeros(1,length(longOnesIdx{inc}(j,:))),'g-x')
%         text(mean(testTime(longOnesIdx{inc-1}(j,:))),0.01,num2str((testTime(longOnesIdx{inc-1}(j,2))-testTime(longOnesIdx{inc-1}(j,1)))*24))
%     end

end
%%
close all

[tx,ty]=meshgrid(1:size(testChange,2),1:size(testChange,1));

pcolor(tx,ty,testChange); shading flat; axis tight
colorbar
caxis([-1 1])
hold on
plot(tx(allIdx(dn<0)),ty(allIdx(dn<0)),'r.')
plot(tx(allIdx(dn>0)),ty(allIdx(dn>0)),'g.')
for i=1:size(longOnesIdx,1)
    plot(tx(longOnesIdx(i,1)),ty(longOnesIdx(i,2)),'g-x')
end

%% 1D (old way)

% % Find continuous negatives for more than changeThreshold "hours".
% inc=0;
% gotTo=1;
% for i=1:length(dn)
%     % Check whether we're in a flat big (i.e. the change is constant) and
%     % whether the value of the change exceeds the threshold change
%     % specified.
%     if dn(i)==0 && testChange(i)<0
%         fprintf('dn: %i\ttestChange: %i\tinc: %i\n',dn(i),testChange(i),inc)
%         % Check what the value of inc is:
%         % - If it's zero, we're at the start of a run.
%         % - If it's non-zero, we're part way through a run.
%         % - If it's negative, the last value was the end of a run.
%         if inc==0
%             % Start of a run
%             startEndIdx(gotTo,1)=i;
%             gotTo=gotTo+1;
%             inc=1;
%         elseif inc>0
%             % Midway through a run
%             inc=inc+1;
%         else
%             warning('Uhhh... this shouldn''t ever appear...')
%         end
%     elseif dn(i)~=0 && testChange(i)<0
%         % At the end of a run when diff is non-zero and increment is
%         % greater than zero.
%         if inc>0
%             startEndIdx(gotTo-1,2)=i;
%             inc=0;
%         end
%     end
%     
% end