How to Create a Custom DICOM Anonymization Profile from CSV, JSON or XML

How to Create a Custom DICOM Anonymization Profile from CSV, JSON or XML

This tutorial demonstrates how to create custom DICOM anonymization profiles using CSV, JSON, or XML files in C#. Custom profiles allow you to define precisely which tags should be removed, replaced, or retained according to your institution’s specific privacy policies.

Why Create Custom Profiles?

Predefined DICOM PS3.15 profiles may not meet all institutional requirements. Custom profiles enable you to:

  • Retain specific institutional identifiers for internal traceability
  • Apply organization-specific privacy rules
  • Meet unique regulatory requirements in your jurisdiction

Tag Actions Explained

The following actions can be applied to DICOM tags:

ActionCodeDescription
DeleteDRemove the tag entirely from the dataset
ZeroZReplace the value with an empty or zero value
RemoveXRemove if present (similar to Delete)
KeepKPreserve the original value unchanged
CleanCClean the value by removing identifying information
Replace with UIDUReplace with a new generated UID

Prerequisites: Preparing the Environment

  1. Set up Visual Studio or any compatible .NET IDE.
  2. Create a new .NET 8 console application project.
  3. Install Aspose.Medical from the NuGet Package Manager.
  4. Prepare your custom profile definition file.

Step-by-Step Guide to Create Custom Profiles

Step 1: Install Aspose.Medical

Add the Aspose.Medical library to your project using NuGet.

Install-Package Aspose.Medical

Step 2: Create a CSV Profile Definition

Create a CSV file with tag patterns and actions. Each line specifies a tag and its action.

profile.csv:

TagPattern;Action
(0010,0010);Z
(0010,0020);Z
(0010,0030);X
(0010,0040);K
(0020,000D);U
(0020,000E);U
(0008,0018);U
(0008,0080);D
(0008,0081);D
(0008,1030);C

<button class=“hextra-code-copy-btn hx-group/copybtn hx-transition-all active:hx-opacity-50 hx-bg-primary-700/5 hx-border hx-border-black/5 hx-text-gray-600 hover:hx-text-gray-900 hx-rounded-md hx-p-1.5 dark:hx-bg-primary-300/10 dark:hx-border-white/10 dark:hx-text-gray-400 dark:hover:hx-text-gray-50” title=“Copy code”

<div class="copy-icon group-[.copied]/copybtn:hx-hidden hx-pointer-events-none hx-h-4 hx-w-4"></div>
<div class="success-icon hx-hidden group-[.copied]/copybtn:hx-block hx-pointer-events-none hx-h-4 hx-w-4"></div>

This profile:

  • Zeros patient name and ID
  • Removes birth date
  • Keeps patient sex
  • Replaces Study, Series, and SOP Instance UIDs
  • Deletes institution information
  • Cleans study description

Step 3: Create a JSON Profile Definition (Alternative)

Create a JSON file with the same tag definitions.

profile.json:

[
  { "TagPattern": "(0010,0010)", "Action": "Z" },
  { "TagPattern": "(0010,0020)", "Action": "Z" },
  { "TagPattern": "(0010,0030)", "Action": "X" },
  { "TagPattern": "(0010,0040)", "Action": "K" },
  { "TagPattern": "(0020,000D)", "Action": "U" },
  { "TagPattern": "(0020,000E)", "Action": "U" },
  { "TagPattern": "(0008,0018)", "Action": "U" },
  { "TagPattern": "(0008,0080)", "Action": "D" },
  { "TagPattern": "(0008,0081)", "Action": "D" },
  { "TagPattern": "(0008,1030)", "Action": "C" }
]

Step 4: Create an XML Profile Definition (Alternative)

Create an XML file with the same tag definitions.

profile.xml:

<?xml version="1.0" encoding="utf-8"?>
<ConfidentialityProfile>
  <TagAction TagPattern="(0010,0010)" Action="Z" />
  <TagAction TagPattern="(0010,0020)" Action="Z" />
  <TagAction TagPattern="(0010,0030)" Action="X" />
  <TagAction TagPattern="(0010,0040)" Action="K" />
  <TagAction TagPattern="(0020,000D)" Action="U" />
  <TagAction TagPattern="(0020,000E)" Action="U" />
  <TagAction TagPattern="(0008,0018)" Action="U" />
  <TagAction TagPattern="(0008,0080)" Action="D" />
  <TagAction TagPattern="(0008,0081)" Action="D" />
  <TagAction TagPattern="(0008,1030)" Action="C" />
</ConfidentialityProfile>

Step 5: Load the Custom Profile

Load the custom profile using the appropriate method for your file format.

using Aspose.Medical.Dicom.Anonymization;

// Load from CSV
ConfidentialityProfile profileFromCsv = ConfidentialityProfile.LoadFromCsvFile(
    "profile.csv", 
    ConfidentialityProfileOptions.All
);

// Or load from JSON
ConfidentialityProfile profileFromJson = ConfidentialityProfile.LoadFromJsonFile(
    "profile.json", 
    ConfidentialityProfileOptions.All
);

// Or load from XML
ConfidentialityProfile profileFromXml = ConfidentialityProfile.LoadFromXmlFile(
    "profile.xml", 
    ConfidentialityProfileOptions.All
);

Step 6: Apply the Custom Profile

Create an Anonymizer with the loaded profile and apply it to DICOM files.

using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Anonymization;

// Load custom profile
ConfidentialityProfile profile = ConfidentialityProfile.LoadFromCsvFile(
    "profile.csv", 
    ConfidentialityProfileOptions.All
);

// Create anonymizer with custom profile
Anonymizer anonymizer = new(profile);

// Load and anonymize DICOM file
DicomFile dcm = DicomFile.Open("patient_study.dcm");
DicomFile anonymizedDcm = anonymizer.Anonymize(dcm);
anonymizedDcm.Save("anonymized_study.dcm");

Complete Code Example with CSV Profile

Here is a complete example using a CSV-based custom profile:

using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Anonymization;

// Load custom confidentiality profile from CSV
ConfidentialityProfile profile = ConfidentialityProfile.LoadFromCsvFile(
    "hospital_privacy_profile.csv",
    ConfidentialityProfileOptions.All
);

// Create anonymizer with the custom profile
Anonymizer anonymizer = new(profile);

// Load DICOM file
DicomFile dcm = DicomFile.Open("patient_scan.dcm");

// Anonymize using custom profile
DicomFile anonymizedDcm = anonymizer.Anonymize(dcm);

// Save result
anonymizedDcm.Save("anonymized_scan.dcm");

Console.WriteLine("Anonymization completed with custom profile!");

Practical Scenario Examples

Scenario 1: Retain Institution ID for Internal Tracking

TagPattern;Action
(0010,0010);Z
(0010,0020);Z
(0008,0080);K
(0008,0081);K

Scenario 2: Replace All UIDs While Preserving Relationships

TagPattern;Action
(0020,000D);U
(0020,000E);U
(0008,0018);U
(0008,0016);K

Scenario 3: Maximum Privacy for External Sharing

TagPattern;Action
(0010,0010);X
(0010,0020);X
(0010,0030);X
(0010,0040);X
(0008,0080);X
(0008,0081);X
(0008,1030);X
(0008,103E);X

Validation: Testing Your Custom Profile

Always validate custom profiles before production use:

using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Anonymization;

// Load custom profile
ConfidentialityProfile profile = ConfidentialityProfile.LoadFromCsvFile(
    "test_profile.csv",
    ConfidentialityProfileOptions.All
);

Anonymizer anonymizer = new(profile);

// Test with sample files
string[] testFiles = Directory.GetFiles("test_samples", "*.dcm");

foreach (string filePath in testFiles)
{
    try
    {
        DicomFile dcm = DicomFile.Open(filePath);
        DicomFile anonymized = anonymizer.Anonymize(dcm);
        
        string outputPath = Path.Combine("test_output", Path.GetFileName(filePath));
        anonymized.Save(outputPath);
        
        Console.WriteLine($"✓ Processed: {Path.GetFileName(filePath)}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"✗ Failed: {Path.GetFileName(filePath)} - {ex.Message}");
    }
}

Console.WriteLine("\nValidation complete. Review output files in DICOM viewer.");

Troubleshooting

Invalid File Path

If the profile file cannot be found:

string profilePath = "profile.csv";
if (!File.Exists(profilePath))
{
    Console.WriteLine($"Profile file not found: {profilePath}");
    return;
}

Invalid Tag Pattern or Action

Ensure tag patterns follow the format (GGGG,EEEE) where GGGG is the group and EEEE is the element in hexadecimal. Valid actions are: D, Z, X, K, C, U.

Best Practices for Custom Profiles

  1. Version Control: Store profile files in version control to track changes over time.
  2. Documentation: Add comments explaining why specific actions were chosen for each tag.
  3. Testing: Validate profiles with test data before applying to production DICOM files.
  4. Backup: Always keep backups of original profile files.
  5. Review: Periodically review profiles to ensure they still meet regulatory requirements.

Additional Information

  • Consider creating multiple profiles for different use cases (internal sharing, external research, etc.).
  • Document which profile was used when anonymizing files for audit purposes.
  • JSON and XML formats offer better structure for complex profiles with nested definitions.

Conclusion

This tutorial has demonstrated how to create custom DICOM anonymization profiles using CSV, JSON, or XML files in C# with Aspose.Medical. Custom profiles provide the flexibility to implement institution-specific privacy policies while maintaining compliance with healthcare regulations.

 English