post:labelthresholdevents.m

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
post:labelthresholdevents.m [2020/05/27 18:17] – pseudomoanerpost:labelthresholdevents.m [2020/05/27 21:02] (current) – pseudomoaner
Line 3: Line 3:
 **Class:** Function **Class:** Function
  
-**Description:** Takes in the [[usage:tracking#outputs|procTracks]] data structure and detects events .+**Description:** Takes in the [[usage:tracking#outputs|procTracks]] data structure and detects events based on the times at which the values of a user-specified field rise above (or drops below) a user-specified threshold. Applies a refractory period, preventing new events from being detected before the given quantity drops below (or rises above) a second user-specified threshold. This can help to suppress spurious events that result from noisy data.
  
 **Author:** Oliver J. Meacock **Author:** Oliver J. Meacock
Line 20: Line 20:
 %       restrict event detection to. Can be left empty if you wish to apply %       restrict event detection to. Can be left empty if you wish to apply
 %       it to all tracks. %       it to all tracks.
-%       -trtField: String defining the name of the field of procTracks you+%       -tgtField: String defining the name of the field of procTracks you
 %       wish to apply threshold detection to. Can refer to any field, %       wish to apply threshold detection to. Can refer to any field,
 %       provided it is indexed by time and is one-dimensional (so 'x' and  %       provided it is indexed by time and is one-dimensional (so 'x' and 
Line 34: Line 34:
 %       rises above endThresh. If endThresh < startThresh, the opposite is %       rises above endThresh. If endThresh < startThresh, the opposite is
 %       true. %       true.
-%       -eventLable: The integer you wish to use to label this set of+%       -eventLabel: The integer you wish to use to label this set of
 %       events. %       events.
 % %
Line 40: Line 40:
 %       -procTracks: Equal to the input procTracks, but either with the %       -procTracks: Equal to the input procTracks, but either with the
 %       'event' field updated, or (if not already present) added, with the %       'event' field updated, or (if not already present) added, with the
-%       appropriate events added.+%       appropriate events marked.
 % %
 %   Author: Oliver J. Meacock, (c) 2020 %   Author: Oliver J. Meacock, (c) 2020
Line 50: Line 50:
 else else
     error('Thresholds do not appear to be numerical.')     error('Thresholds do not appear to be numerical.')
 +end
 +
 +%Needed to ensure you don't overwrite pre-existing event data in procTracks
 +%later
 +if isfield(procTracks,'event')
 +    preFlag = true;
 +else
 +    preFlag = false;
 end end
  
 for i = 1:size(procTracks,2) for i = 1:size(procTracks,2)
     currList = procTracks(i).(tgtField);     currList = procTracks(i).(tgtField);
-    if isfield(procTracks,'event')+     
 +    if preFlag
         currEvt = procTracks(i).event; %Get pre-existing event list for this track (if it exists)         currEvt = procTracks(i).event; %Get pre-existing event list for this track (if it exists)
     else     else
Line 91: Line 100:
         evtList = [];         evtList = [];
         while proceed %I.e. while there are still events to test         while proceed %I.e. while there are still events to test
-            if ~refState %If event is permitted, detect next and switch to refractory state +            futureEvts = [0,evtSwitch' > currInd]; 
-                futureEvts = [0,evtSwitch > currInd]; +            futureRefs = [0,refSwitch' > currInd]; 
-                nextInd = evtSwitch(diff(futureEvts) == 1); +             
-                currInd = nextInd; +            if ~refState && sum(futureEvts) > 0 %If event is permitted, detect next and switch to refractory state 
-                evtList = [evtList;nextInd];+                currInd = evtSwitch(diff(futureEvts) == 1); 
 +                evtList = [evtList;currInd];
                 refState = true;                 refState = true;
                                  
-                if sum(refSwitch > currInd) == 0+                if sum(refSwitch' > currInd) == 0
                     proceed = false;                     proceed = false;
                 end                 end
-            else %If event is not permitted, detect next refractory period end and switch to 'excitable' state +            elseif refState && sum(futureRefs) > 0 %If event is not permitted, detect next refractory period end and switch to 'excitable' state 
-                futureRefs = [0,refSwitch > currInd]; +                currInd = refSwitch(diff(futureRefs) == 1);
-                nextInd = refSwitch(diff(futureRefs) == 1); +
-                currInd = nextInd;+
                 refState = false;                 refState = false;
                                  
-                if sum(evtSwitch > currInd) == 0+                if sum(evtSwitch' > currInd) == 0
                     proceed = false;                     proceed = false;
                 end                 end
 +            else
 +                proceed = false;
             end             end
         end         end
                  
-        currEvt(evtList) = eventLabel;+        currEvt(evtList+1) = eventLabel;
     end     end
-     
     procTracks(i).event = currEvt;     procTracks(i).event = currEvt;
 end end
Line 144: Line 153:
 hold on hold on
  
 +%Red dots indicate events of class 1
 plot(procTracks.times(procTracks(1).event == 1)*dt,y(procTracks(1).event == 1),'r.','MarkerSize',12) plot(procTracks.times(procTracks(1).event == 1)*dt,y(procTracks(1).event == 1),'r.','MarkerSize',12)
 +
 +%Black dots indicate events of class 2
 plot(procTracks.times(procTracks(1).event == 2)*dt,y(procTracks(1).event == 2),'k.','MarkerSize',12) plot(procTracks.times(procTracks(1).event == 2)*dt,y(procTracks(1).event == 2),'k.','MarkerSize',12)
-<\code>+</code>
  
-**Example output:** Plot generated in the [[usage:plotting]] module, showing how the two fluorescence channels are split into two separately labelled clusters:+**Example output:**
  
-{{ :post:fluosplitexample.png?nolink&400 |}}+{{ :post:eventlabelexample.png?nolink&400 |}}
  • post/labelthresholdevents.m.1590599836.txt.gz
  • Last modified: 2020/05/27 18:17
  • by pseudomoaner