Skip to content

Commit 6068e7e

Browse files
committed
Make the RESET algorithm reproducible and independent of thread count
- The pseudo-target assignment used std::for_each, which GCC's parallel mode runs on several threads. The threads then drew from the global random number generator concurrently, and the labels differed between runs. A plain loop draws the numbers in input order. - With a single training fold (RESET), mergeCpCnPairs divided the number of positives by numFolds - 1 = 0 and cast the infinite result to int, which is undefined behaviour. The value feeds the stopping rule. The divisor is now at least 1. - Input files can contain rows with the same id, scan, mass and label (percolatorTab has several). These tie in every sort order, so the parallel sort, used with more than one thread, could place them in another order than the sequential sort, and RESET then kept another PSM per peptide. PSMDescription stores the input line number in what was padding (the object stays 88 bytes), and all ScoreHolder orders use it as the last tie-break after the id. CompositionSorter sorts with the total ScoreHolder order instead of the score alone. RESET output on percolatorTab is now identical for 1, 3 and 4 threads and between runs. Default outputs are byte-identical to the previous commit. Interface: PSMDescription::setLineNr/getLineNr and the helper compareIdAndLine in ScoreHolder.h are new. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017yywhfE3wwkLwPUfoERjp6
1 parent 798e2d5 commit 6068e7e

6 files changed

Lines changed: 32 additions & 14 deletions

File tree

‎src/Caller.cpp‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,12 +1096,15 @@ int Caller::run() {
10961096
// From Algorithm S3 of the percolator-RESET supplementary material
10971097
// decoyFractionTraining - the probability of assigning a decoy to the
10981098
// training set
1099-
std::for_each(allScores.begin(), allScores.end(), [&](ScoreHolder& sh) {
1099+
// A plain loop: GCC's parallel mode would run std::for_each on
1100+
// several threads, which then draw from the random number generator
1101+
// concurrently and make the result differ between runs.
1102+
for (ScoreHolder& sh : allScores) {
11001103
if (sh.isDecoy() &&
11011104
PseudoRandom::lcg_uniform_rand() > decoyFractionTraining) {
11021105
sh.label = LabelType::PSEUDO_TARGET;
11031106
}
1104-
});
1107+
}
11051108

11061109
// we do not necessarily need multiple folds with RESET because the pseudo
11071110
// targets take care of the overfitting.

‎src/CompositionSorter.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ bool compareByScanThenScore(const ScoreHolder* lhs, const ScoreHolder* rhs) {
2121
if (lhs->pPSM->expMass != rhs->pPSM->expMass) {
2222
return lhs->pPSM->expMass < rhs->pPSM->expMass;
2323
}
24-
// Then sort by score in descending order
25-
return lhs->score > rhs->score;
24+
// Then sort by score in descending order, ties in the total order of
25+
// ScoreHolder so that the result does not depend on the sort algorithm
26+
return *lhs > *rhs;
2627
}
2728

2829
void targetDecoyCompetition(std::vector<ScoreHolder*>& scoreHolders) {
@@ -110,7 +111,7 @@ int CompositionSorter::addPSMs(const Scores& scores, bool useTDC) {
110111
int CompositionSorter::sortScorePerPeptide() {
111112
// Comparator for sorting ScoreHolder references
112113
auto compareScoreHolder = [](const ScoreHolder* lhs, const ScoreHolder* rhs) {
113-
return lhs->score > rhs->score; // For descending order
114+
return *lhs > *rhs; // descending score, ties in a total order
114115
};
115116

116117
// Iterating and sorting
@@ -220,8 +221,7 @@ int CompositionSorter::inCompositionCompetition(Scores& bestScoreHolders,
220221
continue;
221222
std::sort(group.begin(), group.end(),
222223
[](const ScoreHolder* a, const ScoreHolder* b) -> bool {
223-
return a->score >
224-
b->score; // Sort in descending order of score
224+
return *a > *b; // descending score, ties in a total order
225225
});
226226
bestScoreHolders.addScoreHolder(
227227
*(group.front())); // Add the highest-scoring ScoreHolder

‎src/CrossValidation.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ int CrossValidation::mergeCpCnPairs(
560560
trainScores_[set].calcScoresAndQvals(weights_[set], testFdr_);
561561
}
562562
trainScoresCurrent_ = true;
563-
return static_cast<int>(bestTruePos / (numFolds_ - 1));
563+
// each PSM is in numFolds_ - 1 training sets, or in the only one
564+
return static_cast<int>(bestTruePos / std::max(1u, numFolds_ - 1));
564565
}
565566

566567
void CrossValidation::postIterationProcessing(Scores& fullset,

‎src/DataSet.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ LabelType DataSet::readPsm(const std::string& line, const unsigned int lineNr,
143143
}
144144
}
145145
if (!hasScannr) psm.scan = lineNr;
146+
psm.setLineNr(lineNr);
146147

147148
unsigned int numFeatures = static_cast<unsigned int>(FeatureNames::getNumFeatures());
148149
double* featureRow = featurePool.allocate();

‎src/PSMDescription.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ class PSMDescription {
147147
}
148148
inline double getRetentionTime() const { return retentionTime_; }
149149

150+
// line of the PSM in the input, used as the last tie-break when sorting
151+
inline void setLineNr(unsigned int lineNr) { lineNr_ = lineNr; }
152+
inline unsigned int getLineNr() const { return lineNr_; }
153+
150154
double* features = NULL; // owned by a FeatureMemoryPool instance, no need to delete
151155
double expMass = 0.0, calcMass = 0.0;
152156
double retentionTime_ = std::numeric_limits<double>::quiet_NaN();
@@ -158,6 +162,7 @@ class PSMDescription {
158162
std::string_view peptide;
159163
const std::string* const* proteins_ = nullptr;
160164
unsigned int numProteins_ = 0u;
165+
unsigned int lineNr_ = 0u; // fills the padding after numProteins_
161166
static std::string proteinNameSeparator_;
162167
static vector<std::string> spectraFileNames_;
163168
static size_t lastSpectrumFileIndex_;

‎src/ScoreHolder.h‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ class ScoreHolder {
6060
does not depend on the sorting algorithm, which with GCC's parallel mode
6161
depends on the number of threads. Inlined because they are the
6262
comparators of the large sorts. */
63+
// Last tie-break of the orders below: the PSM id, then the input line, so
64+
// that duplicated input rows also sort the same way on any number of threads.
65+
inline int compareIdAndLine(const PSMDescription& x, const PSMDescription& y) {
66+
int idCmp = x.getId().compare(y.getId());
67+
if (idCmp != 0) return idCmp;
68+
return (x.getLineNr() > y.getLineNr()) - (x.getLineNr() < y.getLineNr());
69+
}
70+
6371
inline bool operator>(const ScoreHolder& one, const ScoreHolder& other) {
6472
if (one.score != other.score) return one.score > other.score;
6573
if (one.pPSM->scan != other.pPSM->scan)
@@ -69,7 +77,7 @@ inline bool operator>(const ScoreHolder& one, const ScoreHolder& other) {
6977
if (one.label != other.label) return one.label > other.label;
7078
if (one.pPSM->specFileNr != other.pPSM->specFileNr)
7179
return one.pPSM->specFileNr > other.pPSM->specFileNr;
72-
return one.pPSM->getId() > other.pPSM->getId();
80+
return compareIdAndLine(*one.pPSM, *other.pPSM) > 0;
7381
}
7482

7583
inline bool operator<(const ScoreHolder& one, const ScoreHolder& other) {
@@ -81,7 +89,7 @@ inline bool operator<(const ScoreHolder& one, const ScoreHolder& other) {
8189
if (one.label != other.label) return one.label < other.label;
8290
if (one.pPSM->specFileNr != other.pPSM->specFileNr)
8391
return one.pPSM->specFileNr < other.pPSM->specFileNr;
84-
return one.pPSM->getId() < other.pPSM->getId();
92+
return compareIdAndLine(*one.pPSM, *other.pPSM) < 0;
8593
}
8694

8795
struct lexicOrderProb {
@@ -108,7 +116,7 @@ struct lexicOrderProb {
108116
if (peptCmp != 0) return peptCmp == 1;
109117
if (x.label != y.label) return x.label > y.label;
110118
if (x.score != y.score) return x.score > y.score;
111-
return x.pPSM->getId() < y.pPSM->getId();
119+
return compareIdAndLine(*x.pPSM, *y.pPSM) < 0;
112120
}
113121
};
114122

@@ -141,7 +149,7 @@ struct OrderScanHash {
141149
return x.pPSM->specFileNr < y.pPSM->specFileNr;
142150
if (x.pPSM->scan != y.pPSM->scan) return x.pPSM->scan < y.pPSM->scan;
143151
if (x.label != y.label) return x.label > y.label;
144-
return x.pPSM->getId() < y.pPSM->getId();
152+
return compareIdAndLine(*x.pPSM, *y.pPSM) < 0;
145153
}
146154
};
147155

@@ -156,7 +164,7 @@ struct OrderScanMassCharge {
156164
if (x.score != y.score) return x.score > y.score;
157165
// a decoy wins a tie in target-decoy competition
158166
if (x.label != y.label) return x.label < y.label;
159-
return x.pPSM->getId() < y.pPSM->getId();
167+
return compareIdAndLine(*x.pPSM, *y.pPSM) < 0;
160168
}
161169
};
162170

@@ -169,7 +177,7 @@ struct OrderScanLabel {
169177
if (x.label != y.label) return x.label > y.label;
170178
// the best-scoring PSM of a spectrum first
171179
if (x.score != y.score) return x.score > y.score;
172-
return x.pPSM->getId() < y.pPSM->getId();
180+
return compareIdAndLine(*x.pPSM, *y.pPSM) < 0;
173181
}
174182
};
175183

0 commit comments

Comments
 (0)