Skip to content

Commit

Permalink
Merge pull request #223 from SMI/feature/no-csv
Browse files Browse the repository at this point in the history
Embed data tables, bug fix
  • Loading branch information
JFriel authored May 22, 2024
2 parents 9c18d17 + 624a82c commit 9efbdf3
Show file tree
Hide file tree
Showing 7 changed files with 2,150 additions and 216 deletions.
22 changes: 4 additions & 18 deletions BadMedicine.Dicom/BadMedicine.Dicom.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,20 @@
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<ItemGroup>
<PackageReference Include="fo-dicom" Version="5.1.2" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="HIC.SynthEHR" Version="2.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.3" />
</ItemGroup>


<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\"/>
<None Remove="DicomDataGeneratorDescBodyPart.csv" />
<None Remove="DicomDataGeneratorModalities.csv" />
<None Remove="DicomDataGeneratorTags.csv" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\SharedAssemblyInfo.cs" Link="SharedAssemblyInfo.cs" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="DicomDataGeneratorDescBodyPart.csv" />
<EmbeddedResource Include="DicomDataGeneratorModalities.csv" />
<EmbeddedResource Include="DicomDataGeneratorTags.csv" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="HIC.SynthEHR" Version="2.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.3" />
</ItemGroup>

</Project>
2,055 changes: 2,035 additions & 20 deletions BadMedicine.Dicom/DescBodyPart.cs

Large diffs are not rendered by default.

36 changes: 17 additions & 19 deletions BadMedicine.Dicom/DicomDataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public FileSystemLayout Layout{

private FileSystemLayoutProvider _pathProvider = new(FileSystemLayout.StudyYearMonthDay);

private readonly int[] _modalities;
private readonly int[]? _modalities;

private static readonly List<DicomTag> StudyTags = new()
{
Expand Down Expand Up @@ -154,24 +154,20 @@ public FileSystemLayout Layout{
/// <param name="outputDir"></param>
/// <param name="modalities">List of modalities to generate from e.g. CT,MR. The frequency of images generated is based on
/// the popularity of that modality in a clinical PACS. Passing nothing results in all supported modalities being generated</param>
public DicomDataGenerator(Random r, string? outputDir, params string[] modalities):base(r)
public DicomDataGenerator(Random r, string? outputDir, params string[] modalities) : base(r)
{
DevNull = outputDir?.Equals("/dev/null", StringComparison.InvariantCulture)!=false;
DevNull = outputDir?.Equals("/dev/null", StringComparison.InvariantCulture) != false;
OutputDir = DevNull ? null : Directory.CreateDirectory(outputDir!);

var stats = DicomDataGeneratorStats.GetInstance();

if(modalities.Length == 0)
{
_modalities = stats.ModalityIndexes.Values.ToArray();
}
else
{
if (modalities.Any(m => !stats.ModalityIndexes.ContainsKey(m)))
throw new ArgumentException(
$"Modality '{string.Join(',',modalities.Except(stats.ModalityIndexes.Keys))}' not supported, supported modalities are:{string.Join(",", stats.ModalityIndexes.Keys)}");
_modalities = modalities.Select(m=>stats.ModalityIndexes[m]).ToArray();
}
var modalityList = new HashSet<string>(modalities);
// Iterate through known modalities, listing their offsets within the BucketList
_modalities = stats.ModalityFrequency.Select(static i => i.item.Modality).Select(static (m, i) => (m, i))
.Where(i => modalityList.Count == 0 || modalityList.Contains(i.m)).Select(static i => i.i).ToArray();

if (modalityList.Count != 0 && modalityList.Count != _modalities.Length)
throw new ArgumentException($"Modality list '{string.Join(' ',modalities)}' not supported, valid values are '{string.Join(' ',stats.ModalityFrequency.Select(i=>i.item.Modality))}'");
}

/// <summary>
Expand Down Expand Up @@ -263,10 +259,12 @@ public DicomDataset GenerateTestDataset(Person p,Random _r)
return GenerateTestDataset(p,new Study(this,p,modality,_r).Series[0]);
}

private ModalityStats GetRandomModality(Random _r)
{
return DicomDataGeneratorStats.GetInstance().ModalityFrequency.GetRandom(_modalities,_r);
}
private ModalityStats GetRandomModality(Random _r) =>
_modalities is null
? DicomDataGeneratorStats.GetInstance().ModalityFrequency.GetRandom(_r)
: _modalities.Length == 1
? DicomDataGeneratorStats.GetInstance().ModalityFrequency.Skip(_modalities[0]).First().item
: DicomDataGeneratorStats.GetInstance().ModalityFrequency.GetRandom(_modalities, _r);

/// <summary>
/// Returns a new random dicom image for the <paramref name="p"/> with tag values that make sense for that person
Expand Down Expand Up @@ -307,7 +305,7 @@ public DicomDataset GenerateTestDataset(Person p,Series series)
ds.AddOrUpdate(new DicomTime(DicomTag.SeriesTime, DateTime.Today + series.SeriesTime));

ds.AddOrUpdate(DicomTag.Modality,series.Modality);
ds.AddOrUpdate(DicomTag.AccessionNumber, series.Study.AccessionNumber?? "");
ds.AddOrUpdate(DicomTag.AccessionNumber, series.Study.AccessionNumber ?? "");

if(series.Study.StudyDescription != null)
ds.AddOrUpdate(DicomTag.StudyDescription,series.Study.StudyDescription);
Expand Down
Loading

0 comments on commit 9efbdf3

Please sign in to comment.