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

Collection was modified Exception on AnonymizationQueue.Anonymize(dcmFile) #78

Open
Lama96103 opened this issue Aug 5, 2020 · 3 comments

Comments

@Lama96103
Copy link

My function is pretty similar to the example from the wiki.
Wiki - Anonymization Operations

    private static void Anonymize(string source, string name = "Sanitized", string id = "99999")
    {

        var toAnonymize = Directory.GetFiles(source);

        var settings = AnonymizationSettings.Default;

        //Change mapping but keep connections
        settings.DoAnonymizeUIDs = false;
        settings.FirstName = name;
        settings.LastName = " ";
        settings.Id = id;

        //Gets a current list of UIDs so it can create new ones 
        var queue = EvilDICOM.Anonymization.AnonymizationQueue.BuildQueue(settings, toAnonymize);

        for (int i = 0; i < toAnonymize.Length; i++)
        {
            Console.WriteLine("Anonymizing {0}", toAnonymize[i]);
            var dcm = DICOMObject.Read(toAnonymize[i]);
            queue.Anonymize(dcm);
            //Write back to initial location - though this can be a different place
            dcm.Write(toAnonymize[i]);
        }
    }

When I now execute the function it throws the following execption on queue.Anonymize(dcm);

Unhandled exception. System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List1.Enumerator.MoveNext() at System.Linq.Enumerable.WhereListIterator1.MoveNext()
at EvilDICOM.Anonymization.Anonymizers.PrivateTagAnonymizer.Anonymize(DICOMObject d)
at EvilDICOM.Anonymization.AnonymizationQueue.Anonymize(DICOMObject dcm)
at DicomSanitzer.Program.Anonymize(String source, String name, String id) in C:\Users[WINDOWSUSER]\source\repos\Toolbox\DicomSanitzer\Program.cs:line 49

@ochicaud
Copy link

Hello.
Same problem
Did you find a solution ?

@Lama96103
Copy link
Author

Hello.
Same problem
Did you find a solution ?

Hi, no unfortunately not. Did change the method to manually set the tags

@clairestreb
Copy link

I found the problem (for me anyway) and fixed it (I think) by updating EvilDICOM\Anonymization\Anonymizers\PrivateTagAnonymizer.cs as follows:

` //customized - fixed
//foreach (var priv in d.AllElements.Where(e => e.Tag.IsPrivate()))
// d.Remove(priv.Tag);
d.AllElements.RemoveAll(x => x.Tag.IsPrivate());``

Sorry, I do not have time to contribute with pull request etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants