erdogant / clustimage

clustimage is a python package for unsupervised clustering of images.
https://erdogant.github.io/clustimage
Other
92 stars 8 forks source link

Logging #22

Open lliendo opened 1 year ago

lliendo commented 1 year ago

Hi there,

I'm getting logging messages on the console and interferes with messages that I'm already printing to the console. Moreover I can't see why a module should automatically do logging on the console if it wasn't asked to do so in first place. I can see the following block present in the clustimage.py file just after imports:

# Configure the logger
logger = logging.getLogger('')
[logger.removeHandler(handler) for handler in logger.handlers[:]]
console = logging.StreamHandler()
formatter = logging.Formatter('[clustimage] >%(levelname)s> %(message)s')
console.setFormatter(formatter)
logger.addHandler(console)
logger = logging.getLogger()

It seems that the logger is being configured without giving the user the opportunity to turn if off completely. Do I need to monkeypatch clustimage in order to get rid of it?

Also consider what would happen if every other Python module out there takes a similar approach to logging, we would get the console poluted with messages that we might not want to see at all.

Thanks for your time, Lucas.

erdogant commented 1 year ago

Hi Lucas, is the verbosity setting during the initialization not helping? If not, can you suggest an alternative manner how to turn it off more elegantly?

lliendo commented 1 year ago

Ok, I do see that indeed Clustimage has a verbose keyword argument and that setting it to 60 should suppress all logging. However if I do so it still reports warnings, here's an IPython session example:

In [1]: from clustimage import Clustimage
   ...: 
   ...: Clustimage(method='pca-hog', params_hog={
   ...:     'orientations': 8,
   ...:     'pixels_per_cell': (32, 32),
   ...:     'cells_per_block': (1, 1)
   ...: }, verbose=60)
[clustimage] >WARNING> Parameter grayscale is set to True coz you are using method="pca-hog"
Out[1]: <clustimage.clustimage.Clustimage at 0x7ff3a687a100>

In [2]:

So how should I turn it off? Is the verbose kwarg suppose to silence those messages? May I ask your rationale to do console logging by default? If I had the choice, I'd set logging off by default and mention in the docs how to turn it on just in case is needed.

Note that I initially opened this issue because when I fire Flask I do see:

[clustimage] >INFO> WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://192.168.43.19:5000
[clustimage] >INFO> Press CTRL+C to quit
[clustimage] >INFO>  * Restarting with stat
[clustimage] >WARNING>  * Debugger is active!
[clustimage] >INFO>  * Debugger PIN: 934-626-415

I can't clearly see from the docs how to completely turn off logging. I've also tried:

from clustimage import clustimage, Clustimage

clustimage.disable_tqdm()
...

but it has no effect. As a side note I've checked the code for the disable_tqdm function and you can simply define it as:

def disable_tqdm():
    return logger.getEffectiveLevel() >= 30

as the comparison already returns a True or False value.

Thanks for your time, Lucas.

erdogant commented 1 year ago

The logger is something that I later incorporated. I apparently have missed some routines to correctly suppress the print statement. Reasoning for using the logger is to see whether it runs or hangs because some processes can take forever.

For tqdm I created some routines to turn it off but this requires setting the logger first with set_logger. Within the disable_tqdm function it checks the current logger state. However, the idea is that this is done automatically when setting verbosity to 60. I may have missed suppressing this tqdm too.

ps I will update this for a next release but feel free to make the fix and push it!


 # Set the logger to warning
    > set_logger(verbose='warning')
    > # Test with different messages
    > logger.debug("Hello debug")
    > logger.info("Hello info")
    > logger.warning("Hello warning")
    > logger.critical("Hello critical")
erdogant commented 1 year ago

I have to say, this was quite a deep issue. I needed to resolve the handling of the logging of a few more packages. But it should be fixed now. If you set it to silent during initialization, it must be silent now.

update to the latest version with: pip install -U clustimage