You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: xASL_im_MaskPeakVascularSignal.m
    path of file: ./repos/ExploreASL/Functions/ImageProcessing
    
    the code of the file until where you have to start completion: function [MaskIM, TreatedCBF] = xASL_im_MaskPeakVascularSignal(PathPWI, Path_M0, bClip, ClipThresholdValue, CompressionRate)
%xASL_im_MaskPeakVascularSignal Detect voxels with high intra-vascular peak
%ASL signal
%
% FORMAT: [MaskIM, CBF] = xASL_quant_VascularContrast(PathPWI, Path_M0, CompressionRate, ClipThresholdValue, bClip)
%
% INPUT:
%   PathPWI             - path to PWI image or image matrix (REQUIRED)
%   Path_M0             - path to M0 image or image matrix (OPTIONAL)
%   bClip               - true for applying compression/clipping of the
%                         intra-vascular detected voxels (OPTIONAL, DEFAULT=false)
%   ClipThresholdValue  - how many Mean Absolute Difference (MAD) above the
%                         median we define as clipping/compression threshold (OPTIONAL, DEFAULT=3)
%   CompressionRate     - Compression slope of signal above
%                         ClipThresholdValue, or how "fast" the high values are compressed. Should be a
%                         value between 0 (hard clipping), 0.5 (taking root) to 1 (not affected) (OPTIONAL, DEFAULT=0.675)
%
% OUTPUT:
%   MaskIM              - Binary mask with 1 for negative signal
%   TreatedCBF          - CBF image, with extremely high vascular
%                         signal compressed
% -----------------------------------------------------------------------------------------------------------------------------------------------------
% DESCRIPTION: This function searches for an acceptable high
%              threshold as definition of high intra-vascular ASL signal.
%              It also allows to compress the values here (when
%              bClip==true). Compression retains some variability, but limits their outlying influence
%              on statistics.
%              The procedure works as follows:
%
%              1. Segment {{GM}} on {{ASL}} image at 80th percentile of {{CBF}} image distribution
%              2. For PWI & CBF images, select voxels higher than median + ClipThresholdValue {{MAD}}
%                 Vascular artifacts will have a high intensity in both images, whereas errors by division by M0 will only have a high
%                 intensity on the M0 image, and high values due to a biasfield will only be noticeable on the PWI image
%              3. Combine the two created masks
%              4. Obtain average clipping value from selected voxels from the combined masks
%              5. Apply compression if requested. If not, output image will
%              have NaNs for intra-vascular voxels.
%
%              Note that the definition of the threshold is obtained within
%              the GM only, but that this threshold is applied to the full
%              image to also remove extracranial extreme values.
%
%              Note that the performance may change when using this script
%              with or without M0, as this will change the distribution
%              that determines where the threshold for extremes lies.
% -----------------------------------------------------------------------------------------------------------------------------------------------------
% EXAMPLE: MaskIM = xASL_im_MaskPeakVascularSignal('/analysis/Sub-001/ASL_1/CBF.nii','/analysis/Sub-001/ASL_1/rM0.nii');
% __________________________________

    
function [MaskIM, TreatedCBF] = xASL_im_MaskPeakVascularSignal(PathPWI, Path_M0, bClip, ClipThresholdValue, CompressionRate)
%xASL_im_MaskPeakVascularSignal Detect voxels with high intra-vascular peak
%ASL signal
%
% FORMAT: [MaskIM, CBF] = xASL_quant_VascularContrast(PathPWI, Path_M0, CompressionRate, ClipThresholdValue, bClip)
%
% INPUT:
%   PathPWI             - path to PWI image or image matrix (REQUIRED)
%   Path_M0             - path to M0 image or image matrix (OPTIONAL)
%   bClip               - true for applying compression/clipping of the
%                         intra-vascular detected voxels (OPTIONAL, DEFAULT=false)
%   ClipThresholdValue  - how many Mean Absolute Difference (MAD) above the
%                         median we define as clipping/compression threshold (OPTIONAL, DEFAULT=3)
%   CompressionRate     - Compression slope of signal above
%                         ClipThresholdValue, or how "fast" the high values are compressed. Should be a
%                         value between 0 (hard clipping), 0.5 (taking root) to 1 (not affected) (OPTIONAL, DEFAULT=0.675)
%
% OUTPUT:
%   MaskIM              - Binary mask with 1 for negative signal
%   TreatedCBF          - CBF image, with extremely high vascular
%                         signal compressed
% -----------------------------------------------------------------------------------------------------------------------------------------------------
% DESCRIPTION: This function searches for an acceptable high
%              threshold as definition of high intra-vascular ASL signal.
%              It also allows to compress the values here (when
%              bClip==true). Compression retains some variability, but limits their outlying influence
%              on statistics.
%              The procedure works as follows:
%
%              1. Segment {{GM}} on {{ASL}} image at 80th percentile of {{CBF}} image distribution
%              2. For PWI & CBF images, select voxels higher than median + ClipThresholdValue {{MAD}}
%                 Vascular artifacts will have a high intensity in both images, whereas errors by division by M0 will only have a high
%                 intensity on the M0 image, and high values due to a biasfield will only be noticeable on the PWI image
%              3. Combine the two created masks
%              4. Obtain average clipping value from selected voxels from the combined masks
%              5. Apply compression if requested. If not, output image will
%              have NaNs for intra-vascular voxels.
%
%              Note that the definition of the threshold is obtained within
%              the GM only, but that this threshold is applied to the full
%              image to also remove extracranial extreme values.
%
%              Note that the performance may change when using this script
%              with or without M0, as this will change the distribution
%              that determines where the threshold for extremes lies.
% -----------------------------------------------------------------------------------------------------------------------------------------------------
% EXAMPLE: MaskIM = xASL_im_MaskPeakVascularSignal('/analysis/Sub-001/ASL_1/CBF.nii','/analysis/Sub-001/ASL_1/rM0.nii');
% __________________________________
% Copyright 2015-2020 ExploreASL




%% ===================================================================
% Admin

if nargin<1 || isempty(PathPWI)
    warning('No CBF image found, skipping...');
    return;
else
    PWI = xASL_io_Nifti2Im(PathPWI); % allow both a path or image matrix
    if sum(isfinite(PWI(:)))==0
        warning('Empty CBF image, skipping...');
        MaskIM = ones(size(PWI));
        TreatedCBF = PWI;
        return;
    end