/// <summary> /// Compares the decrease in values from the reference Nifti (prior) to the input Nifti (current). /// The inputs should be pre-registered and normalised. /// </summary> /// <param name="input">Current example</param> /// <param name="reference">Prior example</param> /// <returns>Nifti who's values are the meaningful decrease (less than 0) between prior and current.</returns> public static INifti <float> CompareMSLesionDecrease(INifti <float> input, INifti <float> reference) { INifti <float> output = Compare.GatedSubract(input, reference, backgroundThreshold: 10, minRelevantStd: -1, maxRelevantStd: 5, minChange: 0.8f, maxChange: 5); for (int i = 0; i < output.Voxels.Length; ++i) { if (output.Voxels[i] > 0) { output.Voxels[i] = 0; } } output.RecalcHeaderMinMax(); // This will update the header range. output.ColorMap = ColorMaps.ReverseGreenScale(); return(output); }
private Histogram DoCompare(INifti <float> currentnii, INifti <float> priornii) { _log.Info("Starting normalization..."); currentnii = Normalization.ZNormalize(currentnii, priornii); _log.Info($@"..done."); currentnii.RecalcHeaderMinMax(); priornii.RecalcHeaderMinMax(); var tasks = new List <Task>(); var histogram = new Histogram { Prior = priornii, Current = currentnii }; var cs = _recipe.CompareSettings; if (cs.CompareIncrease) { var t = Task.Run(() => { _log.Info("Comparing increased signal..."); var increase = Compare.GatedSubract(currentnii, priornii, cs.BackgroundThreshold, cs.MinRelevantStd, cs.MaxRelevantStd, cs.MinChange, cs.MaxChange); for (int i = 0; i < increase.Voxels.Length; ++i) { increase.Voxels[i] = increase.Voxels[i] > 0 ? increase.Voxels[i] : 0; } increase.RecalcHeaderMinMax(); increase.ColorMap = ColorMaps.RedScale(); histogram.Increase = increase; var increaseOut = currentnii.AddOverlay(increase); var outpath = _currentPath + ".increase.nii"; increaseOut.WriteNifti(outpath); Metrics.ResultFiles.Add(new ResultFile() { FilePath = outpath, Description = "Increased Signal", Type = ResultType.CURRENT_PROCESSED }); }); tasks.Add(t); } if (cs.CompareDecrease) { _log.Info("Comparing decreased signal..."); // I know the code in these two branches looks similar but there's too many inputs to make a function that much simpler... var t = Task.Run(() => { var decrease = Compare.GatedSubract(currentnii, priornii, cs.BackgroundThreshold, cs.MinRelevantStd, cs.MaxRelevantStd, cs.MinChange, cs.MaxChange); for (int i = 0; i < decrease.Voxels.Length; ++i) { decrease.Voxels[i] = decrease.Voxels[i] < 0 ? decrease.Voxels[i] : 0; } decrease.RecalcHeaderMinMax(); decrease.ColorMap = ColorMaps.ReverseGreenScale(); histogram.Decrease = decrease; var decreaseOut = currentnii.AddOverlay(decrease); var outpath = _currentPath + ".decrease.nii"; decreaseOut.WriteNifti(outpath); Metrics.ResultFiles.Add(new ResultFile() { FilePath = outpath, Description = "Decreased Signal", Type = ResultType.CURRENT_PROCESSED }); }); tasks.Add(t); } Task.WaitAll(tasks.ToArray()); _log.Info("...done."); return(histogram); }
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; }
/// <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); }