Skip to content

Commit

Permalink
Merge pull request #79 from HicServices/dependabot/nuget/develop/HIC.…
Browse files Browse the repository at this point in the history
…DicomTypeTranslation-4.0.0
  • Loading branch information
dependabot[bot] authored Mar 21, 2022
2 parents 06ff194 + 9446f2e commit 7e2f461
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 225 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/testpack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ jobs:
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.x'
- uses: actions/cache@v2
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
restore-keys: |
${{ runner.os }}-nuget-
- name: Start MySQL for testing
run: sudo systemctl start mysql.service
- name: Test
Expand Down
2 changes: 1 addition & 1 deletion BadDicom/BadDicom.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="HIC.DicomTypeTranslation" Version="3.0.0" />
<PackageReference Include="HIC.DicomTypeTranslation" Version="4.0.0" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>
<ItemGroup>
Expand Down
109 changes: 52 additions & 57 deletions BadDicom/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Linq;
using System.Threading.Tasks;
using BadDicom.Configuration;
using Dicom;
using FellowOakDicom;
using DicomTypeTranslation;
using DicomTypeTranslation.TableCreation;
using FAnsi.Discovery;
Expand Down Expand Up @@ -92,12 +92,10 @@ private static void RunOptionsAndReturnExitCode(ProgramOptions opts)
try
{
IPersonCollection identifiers = GetPeople(opts, out Random r);
using(var dicomGenerator = GetDataGenerator(opts, identifiers,r, out DirectoryInfo dir))
{
Console.WriteLine($"{DateTime.Now} Starting file generation (to {dir.FullName})" );
var targetFile = new FileInfo(Path.Combine(dir.FullName, "DicomFiles.csv"));
dicomGenerator.GenerateTestDataFile(identifiers,targetFile,opts.NumberOfStudies);
}
using var dicomGenerator = GetDataGenerator(opts, identifiers,r, out DirectoryInfo dir);
Console.WriteLine($"{DateTime.Now} Starting file generation (to {dir.FullName})" );
var targetFile = new FileInfo(Path.Combine(dir.FullName, "DicomFiles.csv"));
dicomGenerator.GenerateTestDataFile(identifiers,targetFile,opts.NumberOfStudies);
}
catch (Exception e)
{
Expand All @@ -116,9 +114,9 @@ private static DicomDataGenerator GetDataGenerator(ProgramOptions opts, IPersonC
dir = Directory.CreateDirectory(opts.OutputDirectory);

//Generate the dicom files (of the modalities that the user requested)
string[] modalities = !string.IsNullOrWhiteSpace(opts.Modalities)? opts.Modalities.Split(",") :new string[0];
string[] modalities = !string.IsNullOrWhiteSpace(opts.Modalities)? opts.Modalities.Split(",") :Array.Empty<string>();

return new DicomDataGenerator(r, dir, modalities)
return new(r, dir, modalities)
{
NoPixels = opts.NoPixels,
Anonymise = opts.Anonymise,
Expand All @@ -130,7 +128,7 @@ private static DicomDataGenerator GetDataGenerator(ProgramOptions opts, IPersonC

private static IPersonCollection GetPeople(ProgramOptions opts, out Random r)
{
r = opts.Seed == -1 ? new Random() : new Random(opts.Seed);
r = opts.Seed == -1 ? new() : new Random(opts.Seed);

//create a cohort of people
IPersonCollection identifiers = new PersonCollection();
Expand All @@ -148,7 +146,7 @@ private static int RunDatabaseTarget(TargetDatabase configDatabase, ProgramOptio
opts.NoPixels = true;


Stopwatch swTotal = new Stopwatch();
Stopwatch swTotal = new();

swTotal.Start();

Expand Down Expand Up @@ -293,7 +291,7 @@ private static int RunDatabaseTarget(TargetDatabase configDatabase, ProgramOptio
for (int i = 0; i < batchSize; i++)
{
var batch = i;
tasks[i] = new Task(() => // lgtm[cs/local-not-disposed]
tasks[i] = new(() => // lgtm[cs/local-not-disposed]
{
RunBatch(identifiers,opts,r,batches[batch],uploaders[batch]);

Expand Down Expand Up @@ -323,70 +321,67 @@ private static int RunDatabaseTarget(TargetDatabase configDatabase, ProgramOptio
foreach (DiscoveredTable t in tables)
Console.WriteLine($"{t.GetFullyQualifiedName()}: {t.GetRowCount():0,0}");

Console.WriteLine("Total Running Time:" + swTotal.Elapsed);
Console.WriteLine($"Total Running Time:{swTotal.Elapsed}");
return 0;
}

private static void RunBatch(IPersonCollection identifiers, ProgramOptions opts, Random r,DataTable[] batches, IBulkCopy[] uploaders)
{
Stopwatch swGeneration = new Stopwatch();
Stopwatch swReading = new Stopwatch();
Stopwatch swUploading = new Stopwatch();
Stopwatch swGeneration = new();
Stopwatch swReading = new();
Stopwatch swUploading = new();

try
{
using(var dicomGenerator = GetDataGenerator(opts, identifiers,r, out _))
using var dicomGenerator = GetDataGenerator(opts, identifiers,r, out _);
for (int i = 0; i < opts.NumberOfStudies; i++)
{

for (int i = 0; i < opts.NumberOfStudies; i++)
{
swGeneration.Start();
swGeneration.Start();

var p = identifiers.People[r.Next(identifiers.People.Length)];
var ds = dicomGenerator.GenerateStudyImages(p,out Study s);
var p = identifiers.People[r.Next(identifiers.People.Length)];
var ds = dicomGenerator.GenerateStudyImages(p,out Study s);

swGeneration.Stop();
swGeneration.Stop();

foreach (DicomDataset dataset in ds)
{
var rows = new DataRow[batches.Length];
foreach (DicomDataset dataset in ds)
{
var rows = new DataRow[batches.Length];

for (int j = 0; j < batches.Length; j++)
rows[j] = batches[j].NewRow();
for (int j = 0; j < batches.Length; j++)
rows[j] = batches[j].NewRow();

swReading.Start();
foreach (DicomItem item in dataset)
{
var column = DicomTypeTranslaterReader.GetColumnNameForTag(item.Tag, false);
var value = DicomTypeTranslater.Flatten(DicomTypeTranslaterReader.GetCSharpValue(dataset, item));

swReading.Start();
foreach (DicomItem item in dataset)
foreach (DataRow row in rows)
{
var column = DicomTypeTranslaterReader.GetColumnNameForTag(item.Tag, false);
var value = DicomTypeTranslater.Flatten(DicomTypeTranslaterReader.GetCSharpValue(dataset, item));

foreach (DataRow row in rows)
{
if (row.Table.Columns.Contains(column))
row[column] = value ?? DBNull.Value;
}
if (row.Table.Columns.Contains(column))
row[column] = value ?? DBNull.Value;
}
}

for (int j = 0; j < batches.Length; j++)
batches[j].Rows.Add(rows[j]);
for (int j = 0; j < batches.Length; j++)
batches[j].Rows.Add(rows[j]);

swReading.Stop();
}
swReading.Stop();
}

//every 100 and last batch
if (i % 100 == 0 || i == opts.NumberOfStudies - 1)
//every 100 and last batch
if (i % 100 == 0 || i == opts.NumberOfStudies - 1)
{
swUploading.Start();
for (var j = 0; j < uploaders.Length; j++)
{
swUploading.Start();
for (var j = 0; j < uploaders.Length; j++)
{
uploaders[j].Upload(batches[j]);
batches[j].Rows.Clear();
}
swUploading.Stop();
Console.WriteLine($"{DateTime.Now} Done {i} studies");
uploaders[j].Upload(batches[j]);
batches[j].Rows.Clear();
}

swUploading.Stop();
Console.WriteLine($"{DateTime.Now} Done {i} studies");
}

}
}
finally
Expand All @@ -398,9 +393,9 @@ private static void RunBatch(IPersonCollection identifiers, ProgramOptions opts,
}
}

Console.WriteLine("Total time Generating Dicoms:" + swGeneration.Elapsed);
Console.WriteLine("Total time Reading Dicoms:" + swReading.Elapsed);
Console.WriteLine("Total time Uploading Records:" + swUploading.Elapsed);
Console.WriteLine($"Total time Generating Dicoms:{swGeneration.Elapsed}");
Console.WriteLine($"Total time Reading Dicoms:{swReading.Elapsed}");
Console.WriteLine($"Total time Uploading Records:{swUploading.Elapsed}");

}
}
Expand Down
59 changes: 27 additions & 32 deletions BadMedicine.Dicom.Tests/DicomDataGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Dicom;
using FellowOakDicom;
using NUnit.Framework;
using System;
using System.Globalization;
Expand Down Expand Up @@ -39,7 +39,7 @@ public void Test_CreatingOnDisk_OneFile()

Assert.IsNotEmpty(datasetCreated.Dataset.GetSingleValue<String>(DicomTag.AccessionNumber));

Console.WriteLine("Created file "+ f.FullName);
Console.WriteLine($"Created file {f.FullName}");

generator.Dispose();
}
Expand All @@ -53,20 +53,18 @@ public void ExampleUsage()
var person = new Person(r);

//create a generator
using (var generator = new DicomDataGenerator(r, null, "CT"))
{
//create a dataset in memory
DicomDataset dataset = generator.GenerateTestDataset(person, r);

//values should match the patient details
Assert.AreEqual(person.CHI,dataset.GetValue<string>(DicomTag.PatientID,0));
Assert.GreaterOrEqual(dataset.GetValue<DateTime>(DicomTag.StudyDate,0),person.DateOfBirth);

//should have a study description
Assert.IsNotNull(dataset.GetValue<string>(DicomTag.StudyDescription,0));
//should have a study description
Assert.IsNotNull(dataset.GetSingleValue<DateTime>(DicomTag.StudyTime).TimeOfDay);
}
using var generator = new DicomDataGenerator(r, null, "CT");
//create a dataset in memory
DicomDataset dataset = generator.GenerateTestDataset(person, r);

//values should match the patient details
Assert.AreEqual(person.CHI,dataset.GetValue<string>(DicomTag.PatientID,0));
Assert.GreaterOrEqual(dataset.GetValue<DateTime>(DicomTag.StudyDate,0),person.DateOfBirth);

//should have a study description
Assert.IsNotNull(dataset.GetValue<string>(DicomTag.StudyDescription,0));
//should have a study description
Assert.IsNotNull(dataset.GetSingleValue<DateTime>(DicomTag.StudyTime).TimeOfDay);
}

[Test]
Expand All @@ -75,7 +73,7 @@ public void Test_CreatingInMemory_ModalityCT()
var r = new Random(23);
var person = new Person(r);

var generator = new DicomDataGenerator(r,new DirectoryInfo(TestContext.CurrentContext.WorkDirectory),"CT");
var generator = new DicomDataGenerator(r,new(TestContext.CurrentContext.WorkDirectory),"CT");

//generate 100 images
for(int i = 0 ; i < 100 ; i++)
Expand All @@ -94,7 +92,7 @@ public void Test_Anonymise()
var r = new Random(23);
var person = new Person(r);

var generator = new DicomDataGenerator(r,new DirectoryInfo(TestContext.CurrentContext.WorkDirectory),"CT");
var generator = new DicomDataGenerator(r,new(TestContext.CurrentContext.WorkDirectory),"CT");

// without anonymisation (default) we get the normal patient ID
var ds = generator.GenerateTestDataset(person, r);
Expand All @@ -120,7 +118,7 @@ public void Test_CreatingInMemory_Modality_CTAndMR()
var r = new Random(23);
var person = new Person(r);

var generator = new DicomDataGenerator(r,new DirectoryInfo(TestContext.CurrentContext.WorkDirectory),"CT","MR");
var generator = new DicomDataGenerator(r,new(TestContext.CurrentContext.WorkDirectory),"CT","MR");

//generate 100 images
for(int i = 0 ; i < 100 ; i++)
Expand All @@ -139,7 +137,7 @@ public void Test_CreatingInMemory_Modality_CTAndMR()
public void TestFail_CreatingInMemory_Modality_Unknown()
{
var r = new Random(23);
Assert.Throws<ArgumentException>(()=>new DicomDataGenerator(r,new DirectoryInfo(TestContext.CurrentContext.WorkDirectory),"LOLZ"));
Assert.Throws<ArgumentException>(()=>new DicomDataGenerator(r,new(TestContext.CurrentContext.WorkDirectory),"LOLZ"));

}

Expand All @@ -160,27 +158,24 @@ public void Test_CsvOption()
generator.NoPixels = true;
generator.MaximumImages = 500;

generator.GenerateTestDataFile(people,new FileInfo(Path.Combine(outputDir.FullName,"index.csv")),500);
generator.GenerateTestDataFile(people,new(Path.Combine(outputDir.FullName,"index.csv")),500);
}

//3 csv files + index.csv (the default one
Assert.AreEqual(4,outputDir.GetFiles().Length);

foreach (FileInfo f in outputDir.GetFiles())
{
using(var reader = new CsvReader(new StreamReader(f.FullName),CultureInfo.CurrentCulture))
{
int rowcount = 0;

//confirms that the CSV is intact (no dodgy commas, unquoted newlines etc)
while (reader.Read())
rowcount++;
using var reader = new CsvReader(new StreamReader(f.FullName),CultureInfo.CurrentCulture);
int rowcount = 0;

//should be 1 row per image + 1 for header
if(f.Name == DicomDataGenerator.ImageCsvFilename)
Assert.AreEqual(501,rowcount);
}
//confirms that the CSV is intact (no dodgy commas, unquoted newlines etc)
while (reader.Read())
rowcount++;

//should be 1 row per image + 1 for header
if(f.Name == DicomDataGenerator.ImageCsvFilename)
Assert.AreEqual(501,rowcount);
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions BadMedicine.Dicom.Tests/NuspecIsCorrectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public void TestDependencyCorrect(string csproj, string nuspec, string packagesM
Assert.Fail("Could not find file {0}", packagesMarkdown);

//<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
Regex rPackageRef = new Regex(@"<PackageReference\s+Include=""(.*)""\s+Version=""([^""]*)""", RegexOptions.IgnoreCase);
Regex rPackageRef = new(@"<PackageReference\s+Include=""(.*)""\s+Version=""([^""]*)""", RegexOptions.IgnoreCase);

//<dependency id="CsvHelper" version="12.1.2" />
Regex rDependencyRef = new Regex(@"<dependency\s+id=""(.*)""\s+version=""([^""]*)""", RegexOptions.IgnoreCase);
Regex rDependencyRef = new(@"<dependency\s+id=""(.*)""\s+version=""([^""]*)""", RegexOptions.IgnoreCase);

//For each dependency listed in the csproj
foreach (Match p in rPackageRef.Matches(File.ReadAllText(csproj)))
Expand Down Expand Up @@ -81,7 +81,7 @@ public void TestDependencyCorrect(string csproj, string nuspec, string packagesM
found = false;
foreach (string line in File.ReadAllLines(packagesMarkdown))
{
if (Regex.IsMatch(line, @"[\s[]" + Regex.Escape(package) + @"[\s\]]", RegexOptions.IgnoreCase))
if (Regex.IsMatch(line, $@"[\s[]{Regex.Escape(package)}[\s\]]", RegexOptions.IgnoreCase))
{
int count = new Regex(Regex.Escape(version)).Matches(line).Count;

Expand All @@ -97,14 +97,15 @@ public void TestDependencyCorrect(string csproj, string nuspec, string packagesM
}
}

private object BuildRecommendedDependencyLine(string package, string version)
private static object BuildRecommendedDependencyLine(string package, string version)
{
return $"<dependency id=\"{package}\" version=\"{version}\" />";
}

private object BuildRecommendedMarkdownLine(string package, string version)
private static object BuildRecommendedMarkdownLine(string package, string version)
{
return string.Format("| {0} | [GitHub]() | [{1}](https://www.nuget.org/packages/{0}/{1}) | | | |", package, version);
return
$"| {package} | [GitHub]() | [{version}](https://www.nuget.org/packages/{package}/{version}) | | | |";
}
}
}
4 changes: 2 additions & 2 deletions BadMedicine.Dicom.Tests/StudyTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Dicom;
using FellowOakDicom;
using NUnit.Framework;
using System;

Expand All @@ -16,7 +16,7 @@ public void Test_CreatingNewStudy_HasSomeImages()

var p = new Person(r);

Study study = new Study(generator,p,new ModalityStats("MR",2,0,50,0,r),r);
Study study = new(generator,p,new("MR",2,0,50,0,r),r);

Assert.AreEqual(2,study.Series.Count);
Assert.AreEqual(50,study.Series[0].Datasets.Count);
Expand Down
Loading

0 comments on commit 7e2f461

Please sign in to comment.