pydicom / deid

best effort anonymization for medical images using python
https://pydicom.github.io/deid/
MIT License
140 stars 43 forks source link

bot.level set to 3 when importing DeidRecipe #158

Closed skarrea closed 3 years ago

skarrea commented 3 years ago

When trying to suppress the error message mentioned in #157 I noticed that bot.level = 3 is assigned in line 38 of deid/deid/config/__init__.py. Thus regardless of the specified MESSAGELEVEL in the environment variables which is used in the DeidMessage class the bot will change to level 3 when importing DeidRecipe.

Reproducing the behavior:

This code reproduces the aforementioned behavior:

import contextlib
import os

@contextlib.contextmanager
def set_env(**environ):
    """
    Temporarily set the process environment variables.

    >>> with set_env(PLUGINS_DIR=u'test/plugins'):
    ...   "PLUGINS_DIR" in os.environ
    True

    >>> "PLUGINS_DIR" in os.environ
    False

    :type environ: dict[str, unicode]
    :param environ: Environment variables to set

    from:
    https://stackoverflow.com/questions/2059482/python-temporarily-modify-the-current-processs-environment
    """
    old_environ = dict(os.environ)
    os.environ.update(environ)
    try:
        yield
    finally:
        os.environ.clear()
        os.environ.update(old_environ)

with set_env(MESSAGELEVEL="5"):
    from deid.logger import bot
    print(bot.level)
    from deid.config import DeidRecipe
    print(bot.level)

Output

bot.level before import: 5
bot.level after import: 3
vsoch commented 3 years ago

@skarrea would you like to just remove that line? That would respect the user-set level. And for #157 - another idea is to remove that line all-together, or put it at a higher level where it's needed (perhaps get/set identifiers). Would that work for a fix?

vsoch commented 3 years ago

Please see https://github.com/pydicom/deid/pull/159 as a suggested solution.

skarrea commented 3 years ago

Yes, that is perfect!