thouska / spotpy

A Statistical Parameter Optimization Tool
https://spotpy.readthedocs.io/en/latest/
MIT License
248 stars 150 forks source link

Feature logging #244

Closed cpwnd closed 1 year ago

cpwnd commented 4 years ago

Hey I followed your discussion in #239 and went ahead to implement the standard logging library. If you wan't to use the code, I'm happy to contribute.

What is already done

All print calls are replaced with a logging.info call. I don't know if the info level is always appropriate. I changed your logging code in analyser.py (EDIT i mean objectivefunctions.py) to use the new main logger. There is one print call in the database package. I don't know if its really necessary.

What has to be done

https://github.com/cpwnd/spotpy/blob/15e219692f6d7d8a9553264a5830a92b481cde82/spotpy/spotpylogging.py#L16

Not implemented in this pr

In my opinion would be possible next steps, that can be adressed with future pull requests:

  1. The logging messages are now all at level info. They can be more nuanced, to have different outputs for different use cases. E.g. the individuals steps of an algorithm can be on the debug level. (optional) @thouska remove/change logging calls with \n they prevent a nice reading flow (just my opinion)
  2. At the moment, the standard file name for the logging output is spotpy.log. This is used for subsequent calls and every run is appended in this file. Of course this can be changed, maybe with a /log directory and a timestamp-suffixed filename.
  3. Maybe you want to be able to use a file config, e.g. to control the log level or the output file. see https://docs.python.org/3/howto/logging.html#configuring-logging

At the moment I tested only with different tutorials from the examples folder.

Hope to hear from you soon :)

thouska commented 4 years ago

Hi @cpwnd , super nice, thank you for this cool contribution! I tested it and it runs smothely on my PC.

Three minor things:

I will work on you PR and implement different levels of the logging. Indeed some of the stuff from the algorithms can be seen as --debug

@philippkraft what do you think about the cli option, logging format and having a logging file?

cpwnd commented 3 years ago

Hey @thouska, you can now set logfile, logdir and verbosity as "quiet" via cli parameters. I tested it via your suggested way:

from spotpy.cli import main
from spotpy.examples.spot_setup_rosenbrock import spot_setup

setup = spot_setup()
main(setup)

And then start it via command line e.g. $ python script.py --logfile test.log --quiet

The path for the logfile should be possible to set (I assume this can/will be done when working on the cli option).

I think we implemented now all remaining points.

EDIT: See the help message for an explanation on the cli options

  -q, --quiet                     Suppress any logging messages to stdout
                                  during computation

  --logfile FILE                  Logging messages will be written to this
                                  file. If already exists, contents are
                                  appended. A default filename is created with
                                  a current timestamp

  --logdir DIRECTORY              Logging files will be written to this
                                  directory. Default is "."
cpwnd commented 3 years ago

Is this line important? Thats the last print statement remaining. https://github.com/thouska/spotpy/blob/6be2dcfa17e34e65dd43bf9f369da39c597d5760/spotpy/database/__init__.py#L21

Maybe it can be replaced by the logger. If yes, then the database/__init__ also needs a logger object.

coveralls commented 3 years ago

Pull Request Test Coverage Report for Build 921


Changes Missing Coverage Covered Lines Changed/Added Lines %
spotpy/algorithms/mle.py 3 4 75.0%
spotpy/algorithms/sa.py 1 2 50.0%
spotpy/database/hdf5.py 0 1 0.0%
spotpy/objectivefunctions.py 19 20 95.0%
spotpy/algorithms/_algorithm.py 26 28 92.86%
spotpy/algorithms/demcz.py 9 11 81.82%
spotpy/algorithms/fast.py 5 7 71.43%
spotpy/algorithms/rope.py 6 8 75.0%
spotpy/analyser.py 31 33 93.94%
spotpy/algorithms/abc.py 10 13 76.92%
<!-- Total: 216 254 85.04% -->
Files with Coverage Reduction New Missed Lines %
spotpy/algorithms/abc.py 1 84.87%
<!-- Total: 1 -->
Totals Coverage Status
Change from base Build 910: 0.2%
Covered Lines: 4155
Relevant Lines: 4901

💛 - Coveralls
cpwnd commented 3 years ago

With the last two commits, at least for me, this is finished. Can be merged into master now.

thouska commented 3 years ago

Hi @cpwnd,

thx for pushing this new feature. I tested it on my device and it works almost perfect. However, there is one strange error: Everytime it is started for the frst time in an interactive console, any of the example errors with:

Traceback (most recent call last): File "...\spotpy\examples\tutorial_parallel_computing_hymod.py", line 44, in sampler=spotpy.algorithms.mc(spot_setup, dbname='Parallel_hymod', dbformat='csv', File "...\spotpy\algorithms\mc.py", line 53, in init super(mc, self).init(*args, **kwargs) File "...\spotpy\algorithms_algorithm.py", line 221, in init self.logger = spotpylogging.instantiate_logger(self.class.name, quiet, logfile, logdir) File "...\spotpy\spotpylogging.py", line 49, in instantiate_logger handler_file = logging.FileHandler(path_to_logfile) File "...\anaconda3\lib\logging__init.py", line 1143, in init StreamHandler.init__(self, self._open()) File "...\anaconda3\lib\logging__init__.py", line 1172, in _open return open(self.baseFilename, self.mode, encoding=self.encoding)

OSError: [Errno 22] Invalid argument: '...\spotpy\examples\2021-06-15-spotpy.log'`

If I restated the code from there, it works without any error. Do you have any idea what could cause that behavior?

cpwnd commented 3 years ago

I don't have an anaconda installation at the moment. But when I start it in an interactive console (ipython) it works. Also I'm using Ubuntu instead of Windows.

How do you start the scripts in anaconda?

Am 15.06.21 um 16:07 schrieb thouska:

Hi @cpwnd https://github.com/cpwnd,

thx for pushing this new feature. I tested it on my device and it works almost perfect. However, there is one strange error: Everytime it is started for the frst time in an interactive console, any of the example errors with:

Traceback (most recent call last):
File "...\spotpy\examples\tutorial_parallel_computing_hymod.py",
line 44, in
sampler=spotpy.algorithms.mc(spot_setup, dbname='Parallel_hymod',
dbformat='csv',
File "...\spotpy\algorithms\mc.py", line 53, in *init*
super(mc, self).*init*(*args, **kwargs)
File "...\spotpy\algorithms_algorithm.py", line 221, in *init*
self.logger =
spotpylogging.instantiate_logger(self.*class*.*name*, quiet,
logfile, logdir)
File "...\spotpy\spotpylogging.py", line 49, in instantiate_logger
handler_file = logging.FileHandler(path_to_logfile)
File "...\anaconda3\lib\logging_/init/_.py", line 1143, in *init*
StreamHandler.*init*(self, self./open())
File "...\anaconda3\lib\logging_/init//.py", line 1172, in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)

OSError: [Errno 22] Invalid argument:
'...\spotpy\examples\2021-06-15-spotpy.log'`

If I restated the code from there, it works without any error. Do you have any idea what could cause that behavior?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/thouska/spotpy/pull/244#issuecomment-861530178, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMTH7ISCMPDF6RVPATK3P3TS5NDDANCNFSM4L2LP2MA.

thouska commented 3 years ago

I start the scripts in Anaconda's spyder editor. I will try, how it works outside of spyer and keep you posted.

cpwnd commented 3 years ago

@thouska please check the branch again for your Windwos Anaconda setup. I replaced the logging paths now with pathlib.PurePaths, which are OS agnostic and should work everywhere.

cpwnd commented 1 year ago

Closing this in favor of #293