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

Merged
Merged
Show file tree
Hide file tree
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
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
Loading