Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Averaging slight adjustement for ensured thread safety #810

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 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 @@ -40,7 +41,7 @@ public static double[][] AverageSpectra(double[][] xArrays, double[][] yArrays,
/// <param name="parameters">how to perform the averaging</param>
/// <returns></returns>
private static double[][] MzBinning(double[][] xArrays, double[][] yArrays,
SpectralAveragingParameters parameters)
SpectralAveragingParameters parameters)
{
// get tics
var tics = yArrays.Select(p => p.Sum()).ToArray();
Expand All @@ -56,22 +57,18 @@ 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) =>
var averagedPeaks = new ConcurrentBag<(double mz, double intensity)>();
var binIncidences = bins.Keys.ToArray();

Parallel.ForEach(binIncidences, new ParallelOptions { MaxDegreeOfParallelism = parameters.MaxThreadsToUsePerFile }, binIndex =>
{
// each bin index that contains peaks
var binIncidences = bins.Keys.ToList();

// iterate through each bin index which contains peaks
for (; iterationIndex < binIncidences.Count; iterationIndex += parameters.MaxThreadsToUsePerFile)
{
var peaksFromBin = bins[binIncidences[iterationIndex]];

peaksFromBin = OutlierRejection.RejectOutliers(peaksFromBin, parameters);
if (!peaksFromBin.Any()) continue;
lock (averagedPeaks)
averagedPeaks.Add(AverageBin(peaksFromBin, weights));
}
var peaksFromBin = bins[binIndex];
peaksFromBin = OutlierRejection.RejectOutliers(peaksFromBin, parameters);

if (peaksFromBin.Count == 0)
return;

averagedPeaks.Add(AverageBin(peaksFromBin, weights));
});

// return averaged
Expand Down
Loading