The IntegrationRoiManager weighs pixel values according to a mask and sums the weighted values to form the integration value. This integration value is displayed without further analysis. However, it might be desired to perform additional computation on the integration values (e.g. to calculate dF/F). This can be done by defining a post processing function.
The IntegrationRoiManager has a property 'postProcessFcn'. This property defines a function handle points to a function, which, by default, passes the integrationValues through without any processing. By changing this function handle, a different functionality can be achieved.

hSI.hIntegrationRoiManager.postProcessFcn = @myPostProcessingFunction; % overwrite the default value of 'postProcessFcn'

 

To get started with your own postProcessingFcn, inspect the default post processing function

Default function
integrationValues = integrationPostProcessingFcn(rois,integrationDone,arrayIndices,integrationValueHistory,integrationTimestampHistory,integrationFrameNumberHistory)
    % This function is used to post process integration values
    % standard behavior: pass the integration values through without changing the calculated values
    integrationValues = integrationValueHistory(arrayIndices);
end

 

This function can be changed to use the integration history for generating values:

Example: Average last three values
integrationValues = integrationPostProcessingFcn(rois,integrationDone,arrayIndices,integrationValueHistory,integrationTimestampHistory,integrationFrameNumberHistory)
   historyLength = size(integrationValueHistory,1);
   numRois = size(integrationValueHistory,2);

   [is,js] = ind2sub(size(integrationValueHistory),arrayIndices); % get the line and row indices of the current integration values

   is = [is-2;is-1;is]; % % get row indices for last three values
   is(is<1) = historyLength - is(is<1); % integrationValueHistory is a rolling buffer. roll over rowindex over if i < 1
   js = [js;js;js];

   idx = sub2ind(is,js); % get 3xn index matrix
   vals = integrationValueHistory(idx); % get last 3 values for each integration roi

 
   integrationValues = mean(vals,1); % average last 3 values, return post processed value
end
Icon

The post processing function will be called several times during a volume. Makes sure to optimize the function for performance, so that the the processing pipeline is not slowed down.

 

Note: only values that are flaged by 'integrationDone' actually were updated when the post processing function was called. To optimize for performance, only these values need to be post processed

Performance optimization
integrationValues = integrationPostProcessingFcn(rois,integrationDone,arrayIndices,integrationValueHistory,integrationTimestampHistory,integrationFrameNumberHistory)
   historyLength = size(integrationValueHistory,1);
   numRois = size(integrationValueHistory,2);


   arrayIndices_ = arrayIndices(integrationDone); % get arrayIndices for values that changed
   [is,js] = ind2sub(size(integrationValueHistory),arrayIndices_); % get the line and row indices of the new values

   is = [is-2;is-1;is]; % % get row indices for last three values
   is(is<1) = historyLength - is(is<1); % integrationValueHistory is a rolling buffer. roll over rowindex over if i < 1
   js = [js;js;js];

   idx = sub2ind(is,js); % get 3xn index matrix
   vals = integrationValueHistory(idx); % get last 3 values for each integration roi

   integrationValues = nan(1,numRois);
   integrationValues(integrationDone) = mean(vals,1); % only update relevant values
end

 

Application idea: overwrite the integrationValue of the last roi to show a peak every 30th frame

Application example
integrationValues = integrationPostProcessingFcn(rois,integrationDone,arrayIndices,integrationValueHistory,integrationTimestampHistory,integrationFrameNumberHistory)
    integrationValues = integrationValueHistory(arrayIndices);
    % overwrite value for last integration roi
    frameNumber = integrationFrameNumber(arrayIndices(end));
    tfmultiple30 = mod(frameNumber,30)==0;
    if tfmultiple30
        integrationValues(end) = Inf;
    else
        integrationValues(end) = 0;
    end
end