Skip to content

Commit

Permalink
Averaging slight adjustement for ensured thread safety (#810)
Browse files Browse the repository at this point in the history
* Refactor for thread safety and performance

- Import `System.Collections.Concurrent` for concurrent collections.
- Change `averagedPeaks` from `List` to `ConcurrentBag` for thread safety.
- Move `binIncidences` array creation outside parallel loop to avoid redundancy.
- Modify `Parallel.ForEach` loop to use array length instead of list count.
- Remove `lock` around `averagedPeaks.Add` as `ConcurrentBag` is thread-safe.

* Test Project cleanup

* used a partitioner

* Oopsie

* Removed bag as it was an unnecessry structure
  • Loading branch information
nbollis authored Dec 12, 2024
1 parent 6411360 commit fed869f
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 21 deletions.
24 changes: 11 additions & 13 deletions mzLib/SpectralAveraging/Algorithms/SpectraAveraging.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -39,8 +40,7 @@ public static double[][] AverageSpectra(double[][] xArrays, double[][] yArrays,
/// <param name="yArrays">yArrays of spectra to be averaged</param>
/// <param name="parameters">how to perform the averaging</param>
/// <returns></returns>
private static double[][] MzBinning(double[][] xArrays, double[][] yArrays,
SpectralAveragingParameters parameters)
private static double[][] MzBinning(double[][] xArrays, double[][] yArrays, SpectralAveragingParameters parameters)
{
// get tics
var tics = yArrays.Select(p => p.Sum()).ToArray();
Expand All @@ -56,21 +56,20 @@ private static double[][] MzBinning(double[][] xArrays, double[][] yArrays,
var weights = SpectralWeighting.CalculateSpectraWeights(xArrays, yArrays, parameters.SpectralWeightingType);

// reject outliers and average bins
List<(double mz, double intensity)> averagedPeaks = new();
Parallel.ForEach(Enumerable.Range(0, parameters.MaxThreadsToUsePerFile), (iterationIndex) =>
{
// each bin index that contains peaks
var binIncidences = bins.Keys.ToList();
var binIncidences = bins.Keys.ToList();
(double mz, double intensity)[] averagedPeaks = new (double, double)[binIncidences.Count];
var partitioner = Partitioner.Create(0, binIncidences.Count);

// iterate through each bin index which contains peaks
for (; iterationIndex < binIncidences.Count; iterationIndex += parameters.MaxThreadsToUsePerFile)
Parallel.ForEach(partitioner, new ParallelOptions { MaxDegreeOfParallelism = parameters.MaxThreadsToUsePerFile }, (range, state) =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
var peaksFromBin = bins[binIncidences[iterationIndex]];
var peaksFromBin = bins[binIncidences[i]];

peaksFromBin = OutlierRejection.RejectOutliers(peaksFromBin, parameters);
if (!peaksFromBin.Any()) continue;
lock (averagedPeaks)
averagedPeaks.Add(AverageBin(peaksFromBin, weights));

averagedPeaks[i] = AverageBin(peaksFromBin, weights);
}
});

Expand All @@ -86,7 +85,6 @@ private static double[][] MzBinning(double[][] xArrays, double[][] yArrays,
};
}


#region Helpers

/// <summary>
Expand Down
Loading

0 comments on commit fed869f

Please sign in to comment.