Skip to content

Conversation

israelmcmc
Copy link
Contributor

@israelmcmc israelmcmc commented May 21, 2025

Python's builtin logging library prints by default to stdout, not stderr. This is the convention since it allows you to easily separate the applications output from the logs. This is, for example, useful when working on larger pipelines, where the logs and output are usually redirected to different places.

This is related to this other issue in Rich, which threeML uses:
Textualize/rich#2022

While the maintainer hasn't changed the default to stderr, the thread has multiple arguments from various developers of why this should be the default. ThreeML uses a custom Console when calling RichHandler, so the Rich default doesn't matter, but I thought it was still related.

In principle, just adding the flag stderr=True during the Console instantiation should had been enough, but the log were still being outputted to stdout.

The problem was that the main __init__ is importing everything from astromodels, including the astromodel's setup_logger, which was shadowing theeML's setup_logger.

Fixing the order of the imports revealed a circular import, which was preventing importing threeML at all. The io/logging moduled needed the config module to configure the logger, and the config module needed io/logging to create its own logs. I patched this by moving the setup_logger import in config to local import within the functions where it was needed.

While this patch worked for now to achieve the initial goal of using logging to stderr by default, a circular import is usually a symptom telling you that there's something in the code design that needs to changed. In this case, I think the way to go to solve this circular import is to not configure the logging within 3ML at all. This is also the recommended way to use the logging library. I opened the issue #647 about this, which I can address in a separate PR.

The equivalent PR for astromodels is: threeML/astromodels#217

…ng library.

- Changing order of astromodels import in init. It was using astromodels' setup_logger, not threeml's setup_logger
- This revealed a circular import. I patched it with a local import and added a FIXME not about it.
@israelmcmc
Copy link
Contributor Author

🤔 I think the failing tests are not related to my changes, are they? The dev branch shows all tests passed, but the job that is failing here was skipped during the dev tests: https://github.com/threeML/threeML/actions/runs/15124602790 . "Build and verify with conde" was skipped in favor of "Publish with conda". I'm not sure what's exactly the difference.

Or at least that's what it seems to me, I'm not very familiar with the tests suite. Any ideas? I at least verified that all tests passed in my machine.

@ndilalla
Copy link
Contributor

🤔 I think the failing tests are not related to my changes, are they? The dev branch shows all tests passed, but the job that is failing here was skipped during the dev tests: https://github.com/threeML/threeML/actions/runs/15124602790 . "Build and verify with conde" was skipped in favor of "Publish with conda". I'm not sure what's exactly the difference.

Or at least that's what it seems to me, I'm not very familiar with the tests suite. Any ideas? I at least verified that all tests passed in my machine.

This looks more like a temporary glitch of the internet connection as it fails to download the catalog files. I roll them back to see if this solves the issue. They don't look related to this PR.

@israelmcmc
Copy link
Contributor Author

Thanks @ndilalla! Most are passing now, except for "test_dynesty_nested" on (macos13, python 3.9). It's passing in both (macos13, python 3.11) and (macos-latest, python 3.9). It also doesn't seem related to the changes here.

Is is possible that the results of that test are random and that the test values tolerance is too small to account for the spread?

@ndilalla
Copy link
Contributor

Thanks @ndilalla! Most are passing now, except for "test_dynesty_nested" on (macos13, python 3.9). It's passing in both (macos13, python 3.11) and (macos-latest, python 3.9). It also doesn't seem related to the changes here.

Is is possible that the results of that test are random and that the test values tolerance is too small to account for the spread?

It shouldn't be random, but you never know! By looking at the fit results in the log, it looks like the fit didn't converge properly:
bn090217206.spectrum.main.Powerlaw.K 2.9392650642395557 -0.8 +0
I restarted again this job, I am positive this will solve this issue.

@israelmcmc
Copy link
Contributor Author

Thanks @ndilalla, that fixed it indeed.

@ndilalla
Copy link
Contributor

ndilalla commented Jul 8, 2025

I forgot this was still open. @israelmcmc shall we go ahead and merge this PR?

@israelmcmc
Copy link
Contributor Author

Thanks, @ndilalla. Yeah, I think so. At least I'm not planning to add anything else to this PR. I can work on #647 on a separate PR, if that OK with you and @omodei.

I'd recommend merging this one together with threeML/astromodels#217

@PreisTo
Copy link

PreisTo commented Jul 21, 2025

I am currently dealing with the circular import issue in the __initi__.py that would be fixed by this PR - is the plan still to merge this one soon, @ndilalla?

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

Successfully merging this pull request may close these issues.

3 participants