Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] AttributeError: 'ExperimentWriter' object has no attribute 'add_figure' #1694

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ewth
Copy link
Contributor

@ewth ewth commented Oct 9, 2024

Description

This PR is a bugfix for #1256.

At present, the AttributeError is raised (and the script exits) if the logger used does not have the add_figure method.

This fix adds a simple check prior to calling add_figure.

Checklist

  • Linked issues (if existing)
  • Amended changelog for large changes (and added myself there as contributor)
  • Added/modified tests
  • Used pre-commit hooks when committing to ensure that code is compliant with hooks. Install hooks with pre-commit install.
    To run hooks independent of commit, execute pre-commit run --all-files

Make sure to have fun coding!

…_figure'

This fixes the AttributeError failure by checking that `add_figure` exists before attempting to call it.

See sktime#1256
@ewth
Copy link
Contributor Author

ewth commented Oct 9, 2024

Note that I've added an additional check for add_embedding, which causes a similar AttributeError if the logger doesn't have the add_embedding method.

Although related, it's not specifically about the linked issue. If preferred, I can create a new issue and split that off into it.

@ewth
Copy link
Contributor Author

ewth commented Oct 9, 2024

I have not run pre-commit on these commits -- I was having trouble running the toolchain at all. To "test" the fix I just instantiated the model from the modified Python and ran training on it. Happy to follow any advice/suggestions on this.

$ pre-commit run --all-files
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: AssertionError: BUG: expected environment for python to be healthy() immediately after install, please open an issue describing your environment
Check the log at /.../.cache/pre-commit/pre-commit.log

pre-commit.log:

version information

pre-commit version: 2.17.0
git --version: git version 2.34.1
sys.version:
    3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0]
sys.executable: /usr/bin/python3
os.name: posix
sys.platform: linux

error information

An unexpected error has occurred: AssertionError: BUG: expected environment for python to be healthy() immediately after install, please open an issue describing your environment
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pre_commit/error_handler.py", line 70, in error_handler
    yield
  File "/usr/lib/python3/dist-packages/pre_commit/main.py", line 396, in main
    return run(args.config, store, args)
  File "/usr/lib/python3/dist-packages/pre_commit/commands/run.py", line 416, in run
    install_hook_envs(to_install, store)
  File "/usr/lib/python3/dist-packages/pre_commit/repository.py", line 224, in install_hook_envs
    _hook_install(hook)
  File "/usr/lib/python3/dist-packages/pre_commit/repository.py", line 86, in _hook_install
    raise AssertionError(
AssertionError: BUG: expected environment for python to be healthy() immediately after install, please open an issue describing your environment

@ewth
Copy link
Contributor Author

ewth commented Oct 9, 2024

Ah!

I just realised pre-commit was installed at a system level, not in the environment I was using. I can now run it successfully.

Running it on this branch produces the following:


$ pre-commit run --all-files
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check yaml...............................................................Passed
check python ast.........................................................Passed
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

examples/ar.py:88:56: E226 missing whitespace around arithmetic operator
print(f"Number of parameters in network: {deepar.size()/1e3:.1f}k")
                                                       ^
examples/nbeats.py:68:53: E226 missing whitespace around arithmetic operator
print(f"Number of parameters in network: {net.size()/1e3:.1f}k")
                                                    ^
examples/stallion.py:122:53: E226 missing whitespace around arithmetic operator
print(f"Number of parameters in network: {tft.size()/1e3:.1f}k")
                                                    ^
pytorch_forecasting/data/timeseries.py:106:28: E226 missing whitespace around arithmetic operator
                f"{na} ({na/tensor.size(0):.2%}) of {name} "
                           ^

isort....................................................................Passed
black....................................................................Passed
nbqa-black...............................................................Passed
nbqa-isort...............................................................Passed
nbqa-flake8..............................................................Failed
- hook id: nbqa-flake8
- exit code: 1

docs/source/tutorials/stallion.ipynb:cell_7:25:53: E226 missing whitespace around arithmetic operator
print(f"Number of parameters in network: {tft.size()/1e3:.1f}k")
                                                    ^
docs/source/tutorials/stallion.ipynb:cell_9:29:53: E226 missing whitespace around arithmetic operator
print(f"Number of parameters in network: {tft.size()/1e3:.1f}k")
                                                    ^

nbqa-check-ast...........................................................Passed

Which is the same result as running it on main:


trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check yaml...............................................................Passed
check python ast.........................................................Passed
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

examples/ar.py:88:56: E226 missing whitespace around arithmetic operator
print(f"Number of parameters in network: {deepar.size()/1e3:.1f}k")
                                                       ^
examples/nbeats.py:68:53: E226 missing whitespace around arithmetic operator
print(f"Number of parameters in network: {net.size()/1e3:.1f}k")
                                                    ^
examples/stallion.py:122:53: E226 missing whitespace around arithmetic operator
print(f"Number of parameters in network: {tft.size()/1e3:.1f}k")
                                                    ^
pytorch_forecasting/data/timeseries.py:106:28: E226 missing whitespace around arithmetic operator
                f"{na} ({na/tensor.size(0):.2%}) of {name} "
                           ^

isort....................................................................Passed
black....................................................................Passed
nbqa-black...............................................................Passed
nbqa-isort...............................................................Passed
nbqa-flake8..............................................................Failed
- hook id: nbqa-flake8
- exit code: 1

docs/source/tutorials/stallion.ipynb:cell_7:25:53: E226 missing whitespace around arithmetic operator
print(f"Number of parameters in network: {tft.size()/1e3:.1f}k")
                                                    ^
docs/source/tutorials/stallion.ipynb:cell_9:29:53: E226 missing whitespace around arithmetic operator
print(f"Number of parameters in network: {tft.size()/1e3:.1f}k")
                                                    ^

nbqa-check-ast...........................................................Passed

@ewth
Copy link
Contributor Author

ewth commented Oct 9, 2024

I've created #1695 and #1696 as a result of the above.

@fkiraly
Copy link
Collaborator

fkiraly commented Oct 9, 2024

Hm, question - is this an issue if lightning >=2, or only on lightning <2?

Because currently, pyproject.toml does not officially support lightning <2.

Nothing wrong with more defensive programming, but it feels like there is a larger issue to discuss about lightning <2 support if this is the case.

@ewth
Copy link
Contributor Author

ewth commented Oct 9, 2024

It impacts me with lightning==2.4.0.

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.

2 participants