Esempio n. 1
0
        /// <summary>
        /// Normalizes the data to a Z-Value
        /// </summary>
        /// <param name="input"></param>
        /// <param name="backgroundThreshold"></param>
        /// <returns></returns>
        public static INifti <float> ZNormalize(INifti <float> input, float backgroundThreshold = 10)
        {
            dynamic output = input.DeepCopy();
            // We take the mean and standard deviation ignoring background.
            var currentMean   = input.Voxels.Where(val => val > backgroundThreshold).Mean();
            var currentStdDev = input.Voxels.Where(val => val > backgroundThreshold).StandardDeviation();

            for (var i = 0; i < output.Voxels.Length; i++)
            {
                output.Voxels[i] = (float)((output.Voxels[i] - currentMean) / currentStdDev);
            }

            output.RecalcHeaderMinMax(); //update display range

            return(output);
        }
Esempio n. 2
0
        public static INifti <float> ANTSRegistration(INifti <float> floating, INifti <float> reference, DataReceivedEventHandler updates = null)
        {
            // Setup our temp file names.
            string niftiInPath  = Path.GetFullPath(Tools.TEMPDIR + floating.GetHashCode() + ".antsrego.in.nii");
            string niftiRefPath = Path.GetFullPath(Tools.TEMPDIR + floating.GetHashCode() + ".antsrego.ref.nii");

            floating.WriteNifti(niftiInPath);
            reference.WriteNifti(niftiRefPath);

            string niftiOutPath = Path.GetFullPath(ANTSRegistration(niftiInPath, niftiRefPath, updates));

            var output = floating.DeepCopy();

            output = output.ReadNifti(niftiOutPath);

            return(output);
        }
Esempio n. 3
0
        /// <summary>
        /// Shifts the ditribution to be within the given range. Default is 0-1.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="rangeStart"></param>
        /// <param name="rangeEnd"></param>
        /// <returns></returns>
        public static INifti <float> RangeNormalize(INifti <float> input, float rangeStart = 0, float rangeEnd = 1)
        {
            if (rangeEnd <= rangeStart)
            {
                throw new ArgumentException("Start of range cannot be greater than end of range.");
            }

            var min   = input.Voxels.Min();
            var range = input.Voxels.Max() - input.Voxels.Min();

            var output = input.DeepCopy();

            for (int i = 0; i < output.Voxels.Length; ++i)
            {
                output.Voxels[i] = ((output.Voxels[i] - min) / range) * (rangeEnd - rangeStart) + rangeStart;
            }

            return(output);
        }
Esempio n. 4
0
        /// <summary>
        /// Uses the BrainSuite BSE tool to extract the brain from a given INifti.
        /// </summary>
        /// <param name="input">Nifti which contains the brain to be extracted</param>
        /// <param name="updates">Data handler for updates from the BSE tool.</param>
        /// <returns>The INifti containing the extracted brain.</returns>
        public static INifti <float> BrainSuiteBSE(INifti <float> input, DataReceivedEventHandler updates = null)
        {
            // Setup our temp file names.
            string niftiInPath  = Path.GetFullPath(Tools.TEMPDIR + input.GetHashCode() + ".bse.in.nii");
            string niftiOutPath = Path.GetFullPath(Tools.TEMPDIR + input.GetHashCode() + ".bse.out.nii");

            // Write nifti to temp directory.
            input.WriteNifti(niftiInPath);

            var args = $"--auto --trim -i \"{niftiInPath}\" -o \"{niftiOutPath}\"";

            ProcessBuilder.CallExecutableFile(CapiConfig.GetConfig().Binaries.bse, args, outputDataReceived: updates);

            var output = input.DeepCopy(); // Sometimes this messes with the header and gives us a 4-up???

            output.ReadNifti(niftiOutPath);

            return(output);
        }
Esempio n. 5
0
        /// <summary>
        /// Uses the ANTS implementation of the N4 bias correction algorithm.
        /// </summary>
        /// <param name="input">The input nifti to be corrected</param>
        /// <param name="updates">Event handler for updates from the process</param>
        /// <returns>New, corrected nifti</returns>
        public static INifti <float> AntsN4(INifti <float> input, DataReceivedEventHandler updates = null)
        {
            // Setup our temp file names.
            string niftiInPath  = Path.GetFullPath(Tools.TEMPDIR + input.GetHashCode() + ".antsN4.in.nii");
            string niftiOutPath = Path.GetFullPath(Tools.TEMPDIR + input.GetHashCode() + ".antsN4.out.nii");

            // Write nifti to temp directory.
            input.WriteNifti(niftiInPath);

            var args = $"-i \"{niftiInPath}\" -o \"{niftiOutPath}\"";

            ProcessBuilder.CallExecutableFile(CapiConfig.GetConfig().Binaries.N4BiasFieldCorrection, args, outputDataReceived: updates);

            var output = input.DeepCopy();

            output.ReadNifti(niftiOutPath);
            output.RecalcHeaderMinMax();

            return(output);
        }
Esempio n. 6
0
        private void CheckBrainExtractionMatch(
            INifti <float> currentNifti, INifti <float> priorNifti,
            INifti <float> currentNiftiWithSkull, INifti <float> priorNiftiWithSkull, MSMetrics qaResults)
        {
            var volCurrent       = 0d;
            var volPrior         = 0d;
            var volWSkullCurrent = 0;
            var volWSkullPrior   = 0;

            for (int i = 0; i < currentNifti.Voxels.Length; ++i)
            {
                if (currentNifti.Voxels[i] > 0)
                {
                    volCurrent++;
                }
                if (currentNiftiWithSkull.Voxels[i] > 30)
                {
                    volWSkullCurrent++;
                }
                if (priorNifti.Voxels[i] > 0)
                {
                    volPrior++;
                }
                if (priorNiftiWithSkull.Voxels[i] > 30)
                {
                    volWSkullPrior++;
                }
            }
            var match = Math.Min(volPrior, volCurrent) / Math.Max(volPrior, volCurrent);

            // Add results to QA list
            qaResults.VoxelVolPrior   = volPrior;
            qaResults.VoxelVolCurrent = volCurrent;
            qaResults.BrainMatch      = match;

            _log.Info($@"Percentage of current volume that's brain: {(int)(volCurrent / volWSkullCurrent * 100d)}%");
            _log.Info($@"Percentage of prior volume that's brain: {(int)(volPrior / volWSkullPrior * 100d)}%");
            _log.Info($@"Brain extraction match: {(int)(match * 100)}%");

            // If the match is sub-80% it's probably because one of the brains didn't extract so
            // the compare operation will automatically use the intersection of the two. On the
            // other hand if we're above 80% but less than 95% one of the extractions probably cut
            // out a chunk of brain. So we'll make a mask of the union and apply it to both sides.
            if (match > 0.7 && match < 0.95)
            {
                _log.Info($@"Brain extraction match not good enough, taking the union...");
                // Let's try to make the brain mask an OR of the two.
                var mask = currentNifti.DeepCopy();
                for (int i = 0; i < mask.Voxels.Length; ++i)
                {
                    if (currentNifti.Voxels[i] != 0 || priorNifti.Voxels[i] != 0)
                    {
                        mask.Voxels[i] = 1;
                    }
                    else
                    {
                        mask.Voxels[i] = 0;
                    }
                }

                currentNifti = currentNiftiWithSkull.DeepCopy();
                priorNifti   = priorNiftiWithSkull.DeepCopy();

                for (int i = 0; i < currentNifti.Voxels.Length; ++i)
                {
                    currentNifti.Voxels[i] = currentNifti.Voxels[i] * mask.Voxels[i];
                    priorNifti.Voxels[i]   = priorNifti.Voxels[i] * mask.Voxels[i];
                }

                currentNifti.RecalcHeaderMinMax();
                priorNifti.RecalcHeaderMinMax();

                // Check brain extraction match again...
                volCurrent = 0d;
                volPrior   = 0d;
                for (int i = 0; i < currentNifti.Voxels.Length; ++i)
                {
                    if (currentNifti.Voxels[i] > 0)
                    {
                        volCurrent++;
                    }
                    if (priorNifti.Voxels[i] > 0)
                    {
                        volPrior++;
                    }
                }
                match = Math.Min(volPrior, volCurrent) / Math.Max(volPrior, volCurrent);
                _log.Info($@"Brain extraction match after mask: {(int)(match * 100)}%");
            }
            else if (match < 0.7)
            {
                qaResults.Passed = false;
            }

            // In theory this should stop the skull highlights darkening the output images...
            priorNifti.RecalcHeaderMinMax();
            currentNifti.RecalcHeaderMinMax();
            priorNiftiWithSkull.Header.cal_max   = priorNifti.Header.cal_max;
            currentNiftiWithSkull.Header.cal_max = currentNifti.Header.cal_max;
        }
Esempio n. 7
0
        /// <summary>
        /// Compares the meaningful change in value between the reference Nifti (prior) and the input Nifti (current).
        /// This function also allows significant fine-tuning of the cut-off values.
        /// </summary>
        /// <param name="input">Current Nifti</param>
        /// <param name="reference">Prior Nifti</param>
        /// <param name="backgroundThreshold">Absolute value of background threashold. Any voxels with a value less than this are considered background and ignored.</param>
        /// <param name="minRelevantStd">Minimum relevant value in number of standard deviations from the mean. e.g. a value of -1 will mean that the minimum relevant value will be the mean - 1 standard deviation. Voxels below this threashold are ignored.</param>
        /// <param name="maxRelevantStd">Maximum relevant value in number of standard deviations from the mean. e.g. a value of 3 will mean that the maximum relevant value will be the mean + 3 standard deviations. Voxels above this threashold are ignored.</param>
        /// <param name="minChange">Minimum difference to be considered significant (e.g. noise threshold). Value is given in multiples of the standard deviation for the input voxels (ignoring background).</param>
        /// <param name="maxChange">Maximum difference to be considered significant. Value is given in multiples of the standard deviation for the input voxels (ignoring background).</param>
        /// <returns>INifti object which contains the relevant difference between the reference nifti and the input nifti.</returns>
        public static INifti <float> GatedSubract(INifti <float> input, INifti <float> reference, float backgroundThreshold = 10, float minRelevantStd = -1, float maxRelevantStd = 5, float minChange = 0.8f, float maxChange = 5)
        {
            INifti <float> output = input.DeepCopy();

            //var mean = (float)input.Voxels.Where(val => val > backgroundThreshold).MeanStandardDeviation();
            var meanstddev = input.Voxels.Where(val => val > backgroundThreshold).MeanStandardDeviation();
            var mean       = meanstddev.Item1;
            var stdDev     = meanstddev.Item2; //(Not sure why decompose stopped working here).
            //float range = input.voxels.Max() - input.voxels.Min();
            // Values from trial and error....
            float minRelevantValue = (float)(mean + (minRelevantStd * stdDev));
            float maxRelevantValue = (float)(mean + (maxRelevantStd * stdDev));

            if (input.Voxels.Length != reference.Voxels.Length)
            {
                throw new Exception("Input and reference don't match size");
            }

            for (int i = 0; i < input.Voxels.Length; ++i)
            {
                output.Voxels[i] = input.Voxels[i] - reference.Voxels[i];


                // We want to ignore changes below the minimum relevant value.
                if (input.Voxels[i] < minRelevantValue)
                {
                    output.Voxels[i] = 0;
                }
                if (reference.Voxels[i] < minRelevantValue)
                {
                    output.Voxels[i] = 0;
                }

                // And above the maximum relevant value.
                if (input.Voxels[i] > maxRelevantValue)
                {
                    output.Voxels[i] = 0;
                }
                if (reference.Voxels[i] > maxRelevantValue)
                {
                    output.Voxels[i] = 0;
                }

                // If we haven't changed by at least 1 stdDev we're not significant
                if (Math.Abs(output.Voxels[i]) < Math.Abs(minChange * stdDev))
                {
                    output.Voxels[i] = 0;
                }
                if (Math.Abs(output.Voxels[i]) > Math.Abs(maxChange * stdDev))
                {
                    output.Voxels[i] = 0;
                }
                if (reference.Voxels[i] < backgroundThreshold)
                {
                    output.Voxels[i] = 0;
                }
                if (input.Voxels[i] < backgroundThreshold)
                {
                    output.Voxels[i] = 0;
                }
            }

            for (int i = 1; i < output.Voxels.Length - 1; ++i)
            {
                if (output.Voxels[i - 1] == 0 && output.Voxels[i + 1] == 0)
                {
                    output.Voxels[i] = 0;
                }
            }

            output.RecalcHeaderMinMax(); // Update header range.

            var stdDv = output.Voxels.StandardDeviation();
            var mean2 = output.Voxels.Where(val => val > 0).Mean();

            System.Console.WriteLine($"Compared. Mean={mean2}, stdDv={stdDv}, size={output.Voxels.Where(val => val > 0).Count()}");

            return(output);
        }