-1

I am trying to anonymize accession numbers with a fake custom AN with pydicom:

def an_callback(ds, data_element):
    if data_element.tag == Tag(0x00080050):
        data_element.value = '0000000000'

ds.walk(an_callback)

I would like to pass a custom value instead of '0000000' to the call back function. I suppose I could use a global variable, but I would like to avoid that to reduce unwarranted bugs. Is there a different way that does not use global variables?

edit: I thought walk was a special python function, but it was just a method of ds, here is the code. You can change the code here for callback to also include an optional parameter. callback(self, data_element, replace_value=None)

def walk(self, callback, recursive=True):
    """Iterate through the DataElements and run `callback` on each.

    Visit all DataElements, possibly recursing into sequences and their
    datasets. The callback function is called for each DataElement
    (including SQ element). Can be used to perform an operation on certain
    types of DataElements. E.g., `remove_private_tags`() finds all private
    tags and deletes them. DataElement`s will come back in DICOM order (by
    increasing tag number within their dataset).

    Parameters
    ----------
    callback
        A callable that takes two arguments:
            * a Dataset
            * a DataElement belonging to that Dataset
    recursive : bool
        Flag to indicate whether to recurse into Sequences.
    """
    taglist = sorted(self.keys())
    for tag in taglist:

        with tag_in_exception(tag):
            data_element = self[tag]
            callback(self, data_element)  # self = this Dataset
            # 'tag in self' below needed in case callback deleted
            # data_element
            if recursive and tag in self and data_element.VR == "SQ":
                sequence = data_element.value
                for dataset in sequence:
                    dataset.walk(callback)
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
user252046
  • 399
  • 2
  • 11
  • Why do you not want to use a global variable? Have you tried using one yet? – TylerH Aug 24 '18 at 14:22
  • I generally try to avoid global variables, they can introduce unwanted bugs. – user252046 Aug 24 '18 at 14:28
  • 1
    Thanks. Per a colleague, "the code doesn't help much. We can't tell what's preventing them from just having the closure capture the dynamic value." Can you edit your question to include a bit more code? That may prevent the question from being closed (code questions fare better when they present a clear, reproducible problem). – TylerH Aug 24 '18 at 14:39
  • 1
    yes, sorry. I misunderstood what walk() did, for some reason I thought it was a magic python function __walk__() that behaved like map(), I edited my question to include a possible solution. – user252046 Aug 24 '18 at 14:55

1 Answers1

1

A global variable is the simplest solution; if you want to be more elegant, then you could encapsulate the function in some class. Your problem refers to plain Python, so you may want to read Python Alternatives to Global Variables.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Bartłomiej
  • 1,068
  • 1
  • 14
  • 23