Skip to content

Commit

Permalink
Changed rounding method (smith-chem-wisc#826)
Browse files Browse the repository at this point in the history
Co-authored-by: Nic Bollis <nbollis@comcast.net>
  • Loading branch information
Alexander-Sol and nbollis authored Jan 22, 2025
1 parent de68239 commit 5d2b774
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion mzLib/MassSpectrometry/MzSpectra/MzSpectrum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ public byte[] Get64BitYarray()

public byte[] Get64BitXarray()
{
return Get64Bitarray(XArray.Select(x => Math.Round(x, 4, MidpointRounding.AwayFromZero)));
return Get64Bitarray(XArray.Select(x => Math.Round(x, 4)));
}

public override string ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void TestWriting(string filePath, string outfile, int loop)
Assert.That(readInScan.MsnOrder.Equals(writtenScan.MsnOrder));
Assert.That(readInScan.IsCentroid.Equals(writtenScan.IsCentroid));
Assert.That(readInScan.MassSpectrum.YArray, Is.EquivalentTo(writtenScan.MassSpectrum.YArray));
Assert.That(readInScan.MassSpectrum.XArray, Is.EquivalentTo(writtenScan.MassSpectrum.XArray.Select(x => Math.Round(x, 4, MidpointRounding.AwayFromZero))));
Assert.That(readInScan.MassSpectrum.XArray, Is.EquivalentTo(writtenScan.MassSpectrum.XArray.Select(x => Math.Round(x, 4))));
}

File.Delete(outfile);
Expand Down
8 changes: 4 additions & 4 deletions mzLib/Test/FileReadingTests/TestMzML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public void WriteMzmlTest()

var newFirstValue = reader.GetOneBasedScan(1).MassSpectrum.FirstX;
Assert.AreNotEqual(oldFirstValue.Value, newFirstValue.Value);
Assert.AreEqual(Math.Round((double)oldFirstValue, 4, MidpointRounding.AwayFromZero), newFirstValue.Value, 1e-6);
Assert.AreEqual(Math.Round((double)oldFirstValue, 4), newFirstValue.Value, 1e-6);

var secondScan2 = reader.GetOneBasedScan(2);

Expand Down Expand Up @@ -1459,7 +1459,7 @@ public void TestMzmlWriterRounding()
MsDataScan[] scans = new MsDataScan[1];

double[] intensities0 = new double[] { 1, 1, 1, 1 };
double[] mz0 = new double[] { 50.00004, 50.00005, 50.0004, 50.0005 };
double[] mz0 = new double[] { 50.00014, 50.00015, 50.0004, 50.0005 };
MzSpectrum massSpec0 = new MzSpectrum(mz0, intensities0, false);
scans[0] = new MsDataScan(massSpec0, 1, 1, true, Polarity.Positive, 1, new MzRange(1, 100), "f", MZAnalyzerType.Orbitrap, massSpec0.SumOfAllY, null, null, "1");

Expand All @@ -1472,8 +1472,8 @@ public void TestMzmlWriterRounding()
var readSpectrum = fakeMzml.Scans[0].MassSpectrum;

// Ensure that the spectrum was rounded to the fourth decimal place on write
Assert.That(readSpectrum.XArray[0], Is.EqualTo(50).Within(0.000001));
Assert.That(readSpectrum.XArray[1], Is.EqualTo(50.0001).Within(0.000001));
Assert.That(readSpectrum.XArray[0], Is.EqualTo(50.0001).Within(0.000001));
Assert.That(readSpectrum.XArray[1], Is.EqualTo(50.0002).Within(0.000001));
Assert.That(readSpectrum.XArray[2], Is.EqualTo(50.0004).Within(0.000001));
Assert.That(readSpectrum.XArray[3], Is.EqualTo(50.0005).Within(0.000001));
}
Expand Down
6 changes: 3 additions & 3 deletions mzLib/Test/FileReadingTests/TestRawFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ public static void TestPeakFilteringRawFileReader(string infile)

for (int j = 0; j < mzmlScan.MassSpectrum.XArray.Length; j++)
{
double roundedRawMz = Math.Round(rawScan.MassSpectrum.XArray[j], 4, MidpointRounding.AwayFromZero);
double roundedRawMz = Math.Round(rawScan.MassSpectrum.XArray[j], 4);

// XArray is rounded to the 4th digit during CreateAndWrite
Assert.AreEqual(mzmlScan.MassSpectrum.XArray[j], roundedRawMz);

double roundedMzmlIntensity = Math.Round(mzmlScan.MassSpectrum.XArray[j], 0, MidpointRounding.AwayFromZero);
double roundedRawIntensity = Math.Round(rawScan.MassSpectrum.XArray[j], 0, MidpointRounding.AwayFromZero);
double roundedMzmlIntensity = Math.Round(mzmlScan.MassSpectrum.XArray[j], 0);
double roundedRawIntensity = Math.Round(rawScan.MassSpectrum.XArray[j], 0);

Assert.AreEqual(roundedMzmlIntensity, roundedRawIntensity);
}
Expand Down

0 comments on commit 5d2b774

Please sign in to comment.