Skip to content

Commit

Permalink
Merge pull request #211 from HicServices/feature/mathnot
Browse files Browse the repository at this point in the history
Remove MathNet.Numerics, embed the one class we actually used from it
  • Loading branch information
JFriel authored Aug 14, 2024
2 parents 4ba0ce2 + 697f49e commit 324c0b4
Show file tree
Hide file tree
Showing 12 changed files with 505 additions and 191 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/testpack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ on: push

jobs:
package:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'
- name: Test
run: dotnet test --nologo
- name: Package
Expand Down
2 changes: 0 additions & 2 deletions Packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
| ------- | ------------| ------- | ------- | -------------------------- |
| CommandLineParser | [GitHub](https://github.com/commandlineparser/commandline) | [MIT](https://opensource.org/licenses/MIT) | Allows command line arguments for main client application and CLI executables |
| CsvHelper | [GitHub](https://github.com/JoshClose/CsvHelper) | MS-PL / Apache 2.0 | Enables reading/writing CSV files |
| Generator.Equals | [GitHub](https://github.com/diegofrata/Generator.Equals) | [MIT](https://opensource.org/licenses/MIT) | Simplifies object comparators |
| HIC.FAnsiSql | [GitHub](https://github.com/HicServices/FAnsiSql) | [GPL 3.0](https://www.gnu.org/licenses/gpl-3.0.html) | DBMS abstraction layer |
| MathNet.Numerics | [GitHub](https://github.com/mathnet/mathnet-numerics)| [MIT](https://opensource.org/licenses/MIT) | Generate statistical distributions (e.g. Gaussian) for random data | |
| Microsoft.SourceLink.GitHub | [GitHub](https://github.com/dotnet/sourcelink) | [1.1.1](https://www.nuget.org/packages/Microsoft.SourceLink.GitHub/1.1.1) | [Apache License 2.0](https://github.com/dotnet/sourcelink/blob/master/License.txt) | Enables source debugging of project nuget package| |
| YamlDotNet | [GitHub](https://github.com/aaubry/YamlDotNet) | [MIT](https://opensource.org/licenses/MIT) | Loading configuration files |

2 changes: 1 addition & 1 deletion SynthEHR.Core/Datasets/BiochemistryRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Data;
using System.Globalization;
using System.Linq;
using MathNet.Numerics.Distributions;
using Normal = SynthEHR.Statistics.Distributions.Normal;

namespace SynthEHR.Datasets;

Expand Down
2 changes: 1 addition & 1 deletion SynthEHR.Core/Datasets/DataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using System.Text;
using CsvHelper;
using CsvHelper.Configuration;
using MathNet.Numerics.Distributions;
using Normal = SynthEHR.Statistics.Distributions.Normal;

namespace SynthEHR.Datasets;

Expand Down
281 changes: 137 additions & 144 deletions SynthEHR.Core/Datasets/Demography.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion SynthEHR.Core/Datasets/HospitalAdmissionsRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ static HospitalAdmissionsRecord()
rowCount++;
}

var operationsTable = new DataTable();
using var operationsTable = new DataTable();
operationsTable.BeginLoadData();
operationsTable.Columns.Add("CountOfRecords", typeof(int));

Expand Down
57 changes: 19 additions & 38 deletions SynthEHR.Core/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.

#nullable enable
using System;
using System.Text;
using Generator.Equals;
using SynthEHR.Datasets;

namespace SynthEHR;

/// <summary>
/// Randomly generated person for whom datasets can be built
/// </summary>
[Equatable]
public sealed partial class Person
public sealed record Person
{

/// <include file='../Datasets.doc.xml' path='Datasets/Demography/Field[@name="Forename"]'/>
public string Forename { get; set; }
/// <include file='../Datasets.doc.xml' path='Datasets/Demography/Field[@name="Surname"]'/>
Expand All @@ -36,7 +34,7 @@ public sealed partial class Person
/// <include file='../Datasets.doc.xml' path='Datasets/Demography/Field[@name="Address"]'/>
public DemographyAddress Address { get; set; }
/// <include file='../Datasets.doc.xml' path='Datasets/Demography/Field[@name="PreviousAddress"]'/>
public DemographyAddress PreviousAddress { get; set; }
public DemographyAddress? PreviousAddress { get; set; }

/// <summary>
/// Earliest year of birth to generate
Expand All @@ -52,25 +50,19 @@ public sealed partial class Person
/// <summary>
/// The collection to which the patient belongs, may be null
/// </summary>
private readonly PersonCollection _parent;
private readonly PersonCollection? _parent;

/// <summary>
/// Generates a new random person using the seeded random. This overload ensures that the <see cref="Person"/> generated
/// does not already exist in the <paramref name="collection"/> (in terms of CHI / ANOCHI numbers).
/// </summary>
/// <param name="r"></param>
/// <param name="collection"></param>
public Person(Random r,PersonCollection collection):this(r)
public Person(Random? r, PersonCollection? collection = null)
{
_parent = collection;
}
r ??= Random.Shared;

/// <summary>
/// Generates a new random person using the seeded random
/// </summary>
/// <param name="r"></param>
public Person(Random r)
{
Gender = r.Next(2) switch
{
0 => 'F',
Expand All @@ -85,7 +77,7 @@ public Person(Random r)

//1 in 10 patients is dead
if (r.Next(10) == 0)
DateOfDeath = DataGenerator.GetRandomDateAfter(DateOfBirth, r);
DateOfDeath = DataGenerator.GetRandomDateAfter(DateOfBirth,r);
else
DateOfDeath = null;

Expand All @@ -96,7 +88,7 @@ public Person(Random r)
Address = new DemographyAddress(r);

//one in 10 people doesn't have a previous address
if(r.Next(10) != 0)
if (r.Next(10) != 0)
PreviousAddress = new DemographyAddress(r);
}

Expand All @@ -105,45 +97,37 @@ public Person(Random r)
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public string GetRandomForename(Random r)
{
return Gender == 'F' ? CommonGirlForenames[r.Next(100)] : CommonBoyForenames[r.Next(100)];
}
public string GetRandomForename(Random r) => Gender == 'F' ? CommonGirlForenames[r.Next(100)] : CommonBoyForenames[r.Next(100)];

/// <summary>
/// Returns a random date after the patients date of birth (and before their death if they are dead).
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public DateTime GetRandomDateDuringLifetime(Random r)
{
return DateOfDeath == null
public DateTime GetRandomDateDuringLifetime(Random r) =>
DateOfDeath == null
? DataGenerator.GetRandomDateAfter(DateOfBirth, r)
: DataGenerator.GetRandomDate(DateOfBirth, (DateTime)DateOfDeath, r);
}

/// <summary>
/// Returns a random surname from a list of common surnames
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public static string GetRandomSurname(Random r)
{
return CommonSurnames[r.Next(100)];
}
public static string GetRandomSurname(Random r) => CommonSurnames[r.Next(100)];

/// <summary>
/// If the person died before onDate it returns NULL (as of onDate we did not know when the person would die). if onDate is > date of death it
/// returns the date of death (we knew when they died - you cannot predict the future, but you can remember the past)
/// </summary>
/// <param name="onDate"></param>
/// <returns></returns>
public DateTime? GetDateOfDeathOrNullOn(DateTime onDate)
{
return onDate >= DateOfDeath ? DateOfDeath :
public DateTime? GetDateOfDeathOrNullOn(DateTime onDate) =>
onDate >= DateOfDeath
? DateOfDeath
:
//we cannot predict the future, they are dead today, but you are pretending the date is onDate
null;
}

/// <summary>
/// Returns a new random ANOCHI which does not exist in <see cref="_parent"/> (if we have one)
Expand All @@ -154,12 +138,10 @@ private string GetNovelANOCHI(Random r)
{
var anochi = GenerateANOCHI(r);

while(_parent != null && _parent.AlreadyGeneratedANOCHIs.Contains(anochi))
while (_parent?.AlreadyGeneratedANOCHIs.Add(anochi) == false)
anochi = GenerateANOCHI(r);
_parent?.AlreadyGeneratedANOCHIs.Add(anochi);

return anochi;

}

/// <summary>
Expand All @@ -171,16 +153,15 @@ private string GetNovelCHI(Random r)
{
var chi = GetRandomCHI(r);

while(_parent != null && _parent.AlreadyGeneratedCHIs.Contains(chi))
while (_parent?.AlreadyGeneratedCHIs.Add(chi) == false)
chi = GetRandomCHI(r);
_parent?.AlreadyGeneratedCHIs.Add(chi);

return chi;
}

private static string GenerateANOCHI(Random r)
{
var toReturn = new StringBuilder();
var toReturn = new StringBuilder(12);

for (var i = 0; i < 10; i++)
toReturn.Append(r.Next(10));
Expand Down
57 changes: 57 additions & 0 deletions SynthEHR.Core/Statistics/Distributions/IContinuousDistribution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// <copyright file="IContinuousDistribution.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2014 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

namespace SynthEHR.Statistics.Distributions;

/// <summary>
/// Continuous Univariate Probability Distribution.
/// </summary>
internal interface IContinuousDistribution : IUnivariateDistribution
{
/// <summary>
/// Gets the mode of the distribution.
/// </summary>
double Mode { get; }

/// <summary>
/// Gets the smallest element in the domain of the distribution which can be represented by a double.
/// </summary>
double Minimum { get; }

/// <summary>
/// Gets the largest element in the domain of the distribution which can be represented by a double.
/// </summary>
double Maximum { get; }

/// <summary>
/// Draws a random sample from the distribution.
/// </summary>
/// <returns>a sample from the distribution.</returns>
double Sample();
}
41 changes: 41 additions & 0 deletions SynthEHR.Core/Statistics/Distributions/IDistribution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// <copyright file="IUnivariateDistribution.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

namespace SynthEHR.Statistics.Distributions;

/// <summary>
/// Probability Distribution.
/// </summary>
internal interface IDistribution
{
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
System.Random RandomSource { get; set; }
}
66 changes: 66 additions & 0 deletions SynthEHR.Core/Statistics/Distributions/IUnivariateDistribution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// <copyright file="IUnivariateDistribution.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

namespace SynthEHR.Statistics.Distributions;

/// <summary>
/// Univariate Probability Distribution.
/// </summary>
internal interface IUnivariateDistribution:IDistribution
{
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
double Mean { get; }

/// <summary>
/// Gets the variance of the distribution.
/// </summary>
double Variance { get; }

/// <summary>
/// Gets the standard deviation of the distribution.
/// </summary>
double StdDev { get; }

/// <summary>
/// Gets the entropy of the distribution.
/// </summary>
double Entropy { get; }

/// <summary>
/// Gets the skewness of the distribution.
/// </summary>
double Skewness { get; }

/// <summary>
/// Gets the median of the distribution.
/// </summary>
double Median { get; }
}
Loading

0 comments on commit 324c0b4

Please sign in to comment.