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

Feature/112 reconfigure the detectors to use global code for the binary serializers 2 #129

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
689e6eb
Update detector serialization to use the new BinaryArraySerializer code
Oct 31, 2023
86841b7
Modified int[] to var in BinaryArraySerializer. Added BinaryArraySer…
hayakawa16 Nov 1, 2023
0d24225
BuildTestRelease.ps1 outed some errors in implementation of extra bin…
hayakawa16 Nov 1, 2023
d496f39
More detector BinarySerialization changes.
hayakawa16 Nov 1, 2023
0fc47ce
More BinarySerialization modifications.
hayakawa16 Nov 1, 2023
f5cf1e6
Added Obsolete attribute to DataArray in BinaryArraySerializers.
hayakawa16 Nov 2, 2023
ff06fe4
Additional code that referenced DataArray in BinaryArraySerializer.
hayakawa16 Nov 2, 2023
a77e491
Found two typos that were not caught by a unit test.
hayakawa16 Nov 2, 2023
237d0f8
Added Obsolete attribute to Dimensions in BinaryArraySerializer.
hayakawa16 Nov 3, 2023
30dbfbb
Updated DetectorIOTests to test GetBinarySerializers single layer of …
hayakawa16 Nov 6, 2023
c2f7a0b
In GetBinarySerializers, added back in check on if (TallySecondMoment…
hayakawa16 Nov 6, 2023
82f333f
Added more Detector GetBinarySerializer tests.
hayakawa16 Nov 7, 2023
ffaa48f
Added xml comments to DetectorBinarySerializationHelper. Added more …
hayakawa16 Nov 7, 2023
7a078de
Renamed new tests to DetectorBinarySerializationTests (for now, will …
hayakawa16 Nov 8, 2023
94d3f9a
Added more DetectorBinarySerializer tests and rearranged file so that…
hayakawa16 Nov 8, 2023
d70e81b
Added more unit tests. Up to 47 now.
hayakawa16 Nov 8, 2023
ada97e3
Modified single value detectors to return an empty array rather than …
hayakawa16 Nov 9, 2023
bce36f5
Added unit tests for BinaryArraySerializerFactory
Nov 9, 2023
3e0e74f
Merge pull request #128 from VirtualPhotonics/feature/add-tests-112-r…
Nov 9, 2023
1a7ba7e
Added more unit tests. Up to 61 now.
hayakawa16 Nov 10, 2023
cf3e0c3
Added more documentation to the tests so that reader can tell dimensi…
hayakawa16 Nov 10, 2023
a7f6fc8
Started first 5D detector unit test.
hayakawa16 Nov 10, 2023
82e0f0f
Found that several detectors GetBinarySerialization code did not chec…
hayakawa16 Nov 10, 2023
ef12a2d
All 0D, 1D, 2D, 3D and 5D detectors complete (current count = 69).
hayakawa16 Nov 10, 2023
9b11460
Finished unit tests for DetectorBinarySerialization in all detectors.…
hayakawa16 Nov 13, 2023
6ca00c6
Added ability to test with TallySecondMoment toggle between true and …
hayakawa16 Nov 13, 2023
1ebf77f
Added missing unit test for pMCTOfRhoDetector.
hayakawa16 Nov 15, 2023
575a9d2
Single value detectors moved to separate files and placed in Vts.Test…
hayakawa16 Nov 15, 2023
9eb6ad9
Added OneTimeTearDown attribute to Clear_previously_generated_files m…
hayakawa16 Nov 16, 2023
94cfcd4
Separated the 1D detectors into separate unit tests.
hayakawa16 Nov 17, 2023
6a8335a
Finished separating 2D detector unit tests into their own files. Thi…
hayakawa16 Nov 17, 2023
553ab02
3D detectors separated into their own unit test class.
hayakawa16 Nov 17, 2023
6b92e9e
Finished separating 4D and 5D detectors and removed original Detector…
hayakawa16 Nov 17, 2023
1b1ddbe
Merge branch 'master' into feature/112-reconfigure-the-detectors-to-u…
hayakawa16 Nov 17, 2023
5f4f183
Fixed some easy code smells.
hayakawa16 Nov 22, 2023
c9e3ab4
More code smell fixes
hayakawa16 Nov 22, 2023
2624f37
Added an explanation to another obsolete attribute.
hayakawa16 Nov 27, 2023
24e0b13
Code modifications made after PR review by reviewer lmalenfant.
hayakawa16 Nov 29, 2023
1e66b63
One unit test dMCdRORhodMusDetectorTests had wrong detector specified…
hayakawa16 Nov 29, 2023
452398a
More extra lines deleted. And in one case, two statements on one line.
hayakawa16 Nov 30, 2023
cb71fd7
Modified unit test name to be more accurate in its description.
hayakawa16 Dec 1, 2023
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
495 changes: 495 additions & 0 deletions src/Vts.Test/IO/BinaryArraySerializerFactoryTests.cs

Large diffs are not rendered by default.

48 changes: 11 additions & 37 deletions src/Vts.Test/MonteCarlo/Factories/DetectorFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,43 +299,17 @@ public void Normalize(long numPhotons)
// this is to allow saving of large arrays separately as a binary file
public BinaryArraySerializer[] GetBinarySerializers()
{
return new[] {
new BinaryArraySerializer {
DataArray = Mean,
Name = "Mean",
FileTag = "",
WriteData = binaryWriter => {
for (var i = 0; i < X.Count - 1; i++) {
binaryWriter.Write(Mean[i]);
}
},
ReadData = binaryReader => {
Mean ??= new double[ X.Count - 1];
for (var i = 0; i < X.Count - 1; i++) {
Mean[i] = binaryReader.ReadDouble();
}
}
},
// return a null serializer, if we're not serializing the second moment
!TallySecondMoment ? null : new BinaryArraySerializer {
DataArray = SecondMoment,
Name = "SecondMoment",
FileTag = "_2",
WriteData = binaryWriter => {
if (!TallySecondMoment || SecondMoment == null) return;
for (var i = 0; i < X.Count - 1; i++) {
binaryWriter.Write(SecondMoment[i]);
}
},
ReadData = binaryReader => {
if (!TallySecondMoment || SecondMoment == null) return;
SecondMoment = new double[ X.Count - 1];
for (var i = 0; i < X.Count - 1; i++) {
SecondMoment[i] = binaryReader.ReadDouble();
}
},
},
};
Mean ??= new double[X.Count - 1];
SecondMoment ??= new double[X.Count - 1];
var allSerializers = new List<BinaryArraySerializer>
{
BinaryArraySerializerFactory.GetSerializer(
Mean, "Mean", ""),
TallySecondMoment ? BinaryArraySerializerFactory.GetSerializer(
SecondMoment, "SecondMoment", "_2") : null,
};
return allSerializers.Where(s => s is not null).ToArray();

}

/// <summary>
Expand Down
223 changes: 112 additions & 111 deletions src/Vts.Test/MonteCarlo/IO/DetectorIOTests.cs

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions src/Vts.Test/Unit/MonteCarlo/Detectors/AOfRhoAndZDetectorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using NUnit.Framework;
using System.IO;
using System.Reflection;
using Vts.Common;
using Vts.IO;
using Vts.MonteCarlo.Detectors;

namespace Vts.Test.Unit.MonteCarlo.Detectors;

[TestFixture]
public class AOfRhoAndZDetectorTests
{
/// <summary>
/// clear all test generated files
/// </summary>
[OneTimeSetUp]
[OneTimeTearDown]
public void Clear_previously_generated_files()
{
var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
FolderCleanup.DeleteFileContaining(currentPath, "testaofrhoandz");
}

/// <summary>
/// Test to verify that GetBinarySerializers are working correctly for this detector.
/// </summary>
[Test]
[TestCase(true)]
[TestCase(false)]
public void Validate_deserialized_binary_arrays_are_correct_when_using_GetBinarySerializers(bool tallySecondMoment)
{
const string detectorName = "testaofrhoandz";
var detector = new AOfRhoAndZDetector
{
Rho = new DoubleRange(0, 10, 3),
Z = new DoubleRange(0, 1, 4),
TallySecondMoment = tallySecondMoment,
Name = detectorName,
Mean = new double[,] { { 1, 2, 3 }, { 4, 5, 6 } },
SecondMoment = new double[,] { { 7, 8, 9 }, { 10, 11, 12 } }
};

DetectorBinarySerializationHelper.WriteClearAndReReadArrays(detector, detector.Mean, detector.SecondMoment);

Assert.AreEqual(1, detector.Mean[0, 0]);
Assert.AreEqual(2, detector.Mean[0, 1]);
Assert.AreEqual(3, detector.Mean[0, 2]);
Assert.AreEqual(4, detector.Mean[1, 0]);
Assert.AreEqual(5, detector.Mean[1, 1]);
Assert.AreEqual(6, detector.Mean[1, 2]);
if (!tallySecondMoment) return;
Assert.AreEqual(7, detector.SecondMoment[0, 0]);
Assert.AreEqual(8, detector.SecondMoment[0, 1]);
Assert.AreEqual(9, detector.SecondMoment[0, 2]);
Assert.AreEqual(10, detector.SecondMoment[1, 0]);
Assert.AreEqual(11, detector.SecondMoment[1, 1]);
Assert.AreEqual(12, detector.SecondMoment[1, 2]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using NUnit.Framework;
using System.IO;
using System.Reflection;
using Vts.Common;
using Vts.IO;
using Vts.MonteCarlo.Detectors;

namespace Vts.Test.Unit.MonteCarlo.Detectors;

[TestFixture]
public class AOfXAndYAndZDetectorTests
{
/// <summary>
/// clear all test generated files
/// </summary>
[OneTimeSetUp]
[OneTimeTearDown]
public void Clear_previously_generated_files()
{
var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
FolderCleanup.DeleteFileContaining(currentPath, "testaofxandyandz");
}

/// <summary>
/// Test to verify that GetBinarySerializers are working correctly for this detector.
/// </summary>
[Test]
[TestCase(true)]
[TestCase(false)]
public void Validate_deserialized_binary_arrays_are_correct_when_using_GetBinarySerializers(bool tallySecondMoment)
{
const string detectorName = "testaofxandyandz";
var detector = new AOfXAndYAndZDetector
{
X = new DoubleRange(-10, 10, 3),
Y = new DoubleRange(-10, 10, 3),
Z = new DoubleRange(0, 1, 4),
TallySecondMoment = tallySecondMoment,
Name = detectorName,
Mean = new double[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } },
SecondMoment = new double[,,] { { { 13, 14, 15 }, { 16, 17, 18 } }, { { 19, 20, 21 }, { 22, 23, 24 } } }
};

DetectorBinarySerializationHelper.WriteClearAndReReadArrays(detector, detector.Mean, detector.SecondMoment);

Assert.AreEqual(1, detector.Mean[0, 0, 0]);
Assert.AreEqual(2, detector.Mean[0, 0, 1]);
Assert.AreEqual(3, detector.Mean[0, 0, 2]);
Assert.AreEqual(4, detector.Mean[0, 1, 0]);
Assert.AreEqual(5, detector.Mean[0, 1, 1]);
Assert.AreEqual(6, detector.Mean[0, 1, 2]);
Assert.AreEqual(7, detector.Mean[1, 0, 0]);
Assert.AreEqual(8, detector.Mean[1, 0, 1]);
Assert.AreEqual(9, detector.Mean[1, 0, 2]);
Assert.AreEqual(10, detector.Mean[1, 1, 0]);
Assert.AreEqual(11, detector.Mean[1, 1, 1]);
Assert.AreEqual(12, detector.Mean[1, 1, 2]);
if (!tallySecondMoment) return;
Assert.AreEqual(13, detector.SecondMoment[0, 0, 0]);
Assert.AreEqual(14, detector.SecondMoment[0, 0, 1]);
Assert.AreEqual(15, detector.SecondMoment[0, 0, 2]);
Assert.AreEqual(16, detector.SecondMoment[0, 1, 0]);
Assert.AreEqual(17, detector.SecondMoment[0, 1, 1]);
Assert.AreEqual(18, detector.SecondMoment[0, 1, 2]);
Assert.AreEqual(19, detector.SecondMoment[1, 0, 0]);
Assert.AreEqual(20, detector.SecondMoment[1, 0, 1]);
Assert.AreEqual(21, detector.SecondMoment[1, 0, 2]);
Assert.AreEqual(22, detector.SecondMoment[1, 1, 0]);
Assert.AreEqual(23, detector.SecondMoment[1, 1, 1]);
Assert.AreEqual(24, detector.SecondMoment[1, 1, 2]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using NUnit.Framework;
using System;
using System.IO;
using System.Reflection;
using Vts.IO;
using Vts.MonteCarlo.Detectors;

namespace Vts.Test.Unit.MonteCarlo.Detectors;

[TestFixture]
public class ATotalBoundingVolumeDetectorTests
{
/// <summary>
/// clear all test generated files
/// </summary>
[OneTimeSetUp]
[OneTimeTearDown]
public void Clear_previously_generated_files()
{
var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
FolderCleanup.DeleteFileContaining(currentPath, "testatotalboundingvolume");
}

/// <summary>
/// Test to verify that GetBinarySerializers are working correctly for this detector.
/// </summary>
[Test]
[TestCase(true)]
[TestCase(false)]
public void Validate_deserialized_binary_arrays_are_correct_when_using_GetBinarySerializers(bool tallySecondMoment)
{
const string detectorName = "testatotalboundingvolume";
var detector = new ATotalBoundingVolumeDetector
{
TallySecondMoment = tallySecondMoment,
Name = detectorName,
Mean = new double(),
SecondMoment = new double()
};

var serializers = detector.GetBinarySerializers();
Assert.AreEqual(Array.Empty<BinaryArraySerializer>(), serializers);
}
}
44 changes: 44 additions & 0 deletions src/Vts.Test/Unit/MonteCarlo/Detectors/ATotalDetectorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using NUnit.Framework;
using System;
using System.IO;
using System.Reflection;
using Vts.IO;
using Vts.MonteCarlo.Detectors;

namespace Vts.Test.Unit.MonteCarlo.Detectors;

[TestFixture]
public class ATotalDetectorTests
{
/// <summary>
/// clear all test generated files
/// </summary>
[OneTimeSetUp]
[OneTimeTearDown]
public void Clear_previously_generated_files()
{
var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
FolderCleanup.DeleteFileContaining(currentPath, "testatotal");
}

/// <summary>
/// Test to verify that GetBinarySerializers are working correctly for this detector.
/// </summary>
[Test]
[TestCase(true)]
[TestCase(false)]
public void Validate_deserialized_binary_arrays_are_correct_when_using_GetBinarySerializers(bool tallySecondMoment)
{
const string detectorName = "testatotal";
var detector = new ATotalDetector
{
TallySecondMoment = tallySecondMoment,
Name = detectorName,
Mean = new double(),
SecondMoment = new double()
};

var serializers = detector.GetBinarySerializers();
Assert.AreEqual(Array.Empty<BinaryArraySerializer>(), serializers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.IO;
using Vts.MonteCarlo;

namespace Vts.Test.Unit.MonteCarlo.Detectors
{
internal static class DetectorBinarySerializationHelper
{
/// <summary>
/// detector input abstract class
/// </summary>
/// <param name="detector">IDetector being serialized</param>
/// <param name="arrays">arrays to be serialized (e.g. Mean, SecondMoment, etc.)</param>
internal static void WriteClearAndReReadArrays(IDetector detector, params Array[] arrays)
{
var serializers = detector.GetBinarySerializers();
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream);
foreach (var serializer in serializers)
{
serializer.WriteData(writer);
}

foreach (var array in arrays)
{
Array.Clear(array);
}

stream.Seek(0, SeekOrigin.Begin);

using var reader = new BinaryReader(stream);
foreach (var serializer in serializers)
{
serializer.ReadData(reader);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using NUnit.Framework;
using System.IO;
using System.Numerics;
using System.Reflection;
using Vts.Common;
using Vts.IO;
using Vts.MonteCarlo.Detectors;

namespace Vts.Test.Unit.MonteCarlo.Detectors;

[TestFixture]
public class FluenceOfFxAndZDetectorTests
{
/// <summary>
/// clear all test generated files
/// </summary>
[OneTimeSetUp]
[OneTimeTearDown]
public void Clear_previously_generated_files()
{
var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
FolderCleanup.DeleteFileContaining(currentPath, "testfluenceoffxandz");
}

/// <summary>
/// Test to verify that GetBinarySerializers are working correctly for this detector.
/// </summary>
[Test]
[TestCase(true)]
[TestCase(false)]
public void Validate_deserialized_binary_arrays_are_correct_when_using_GetBinarySerializers(bool tallySecondMoment)
{
const string detectorName = "testfluenceoffxandz";
var detector = new FluenceOfFxAndZDetector
{
Fx = new DoubleRange(0, 10, 3),
Z = new DoubleRange(0, 1, 4),
TallySecondMoment = tallySecondMoment,
Name = detectorName,
Mean = new[,]
{
{ 1 + Complex.ImaginaryOne, 2 + 2 * Complex.ImaginaryOne, 3 + 3 * Complex.ImaginaryOne },
{ 4 + 4 * Complex.ImaginaryOne, 5 + 5 * Complex.ImaginaryOne, 6 + 6 * Complex.ImaginaryOne }
},
SecondMoment = new[,]
{
{ 7 + 7 * Complex.ImaginaryOne, 8 + 8 * Complex.ImaginaryOne, 9 + 9 * Complex.ImaginaryOne },
{ 10 + 10 * Complex.ImaginaryOne, 11 + 11 * Complex.ImaginaryOne, 12 + 12 * Complex.ImaginaryOne }
}
};

DetectorBinarySerializationHelper.WriteClearAndReReadArrays(detector, detector.Mean, detector.SecondMoment);

Assert.AreEqual(1 + Complex.ImaginaryOne, detector.Mean[0, 0]);
Assert.AreEqual(2 + 2 * Complex.ImaginaryOne, detector.Mean[0, 1]);
Assert.AreEqual(3 + 3 * Complex.ImaginaryOne, detector.Mean[0, 2]);
Assert.AreEqual(4 + 4 * Complex.ImaginaryOne, detector.Mean[1, 0]);
Assert.AreEqual(5 + 5 * Complex.ImaginaryOne, detector.Mean[1, 1]);
Assert.AreEqual(6 + 6 * Complex.ImaginaryOne, detector.Mean[1, 2]);
if (!tallySecondMoment) return;
Assert.AreEqual(7 + 7 * Complex.ImaginaryOne, detector.SecondMoment[0, 0]);
Assert.AreEqual(8 + 8 * Complex.ImaginaryOne, detector.SecondMoment[0, 1]);
Assert.AreEqual(9 + 9 * Complex.ImaginaryOne, detector.SecondMoment[0, 2]);
Assert.AreEqual(10 + 10 * Complex.ImaginaryOne, detector.SecondMoment[1, 0]);
Assert.AreEqual(11 + 11 * Complex.ImaginaryOne, detector.SecondMoment[1, 1]);
Assert.AreEqual(12 + 12 * Complex.ImaginaryOne, detector.SecondMoment[1, 2]);
}
}
Loading
Loading