Skip to content

ConfigManager class with update_config functionality#78

Merged
btobers merged 30 commits intodevfrom
72_update_config
Mar 3, 2025
Merged

ConfigManager class with update_config functionality#78
btobers merged 30 commits intodevfrom
72_update_config

Conversation

@btobers
Copy link
Collaborator

@btobers btobers commented Feb 27, 2025

Closes #72.

ConfigManager class added to config.py, which now includes an update_config() function. This will allow for the advanced_test_tw notebook to be run as another test, as the include_frontalablation setting in ~/PyGEM/config.yaml can now be updated programmatically. Need to update all PyGEM-Community/PyGEM-notebooks to match. the new structure.

@btobers btobers requested a review from ddundo February 27, 2025 20:09
@btobers
Copy link
Collaborator Author

btobers commented Feb 27, 2025

@ddundo Let me know what you think of this. I suspect that the current tests would fail right now, since the way the config.yaml file is read in in the test notebooks now needs updating. If I update the notebooks in PyGEM-notebooks and submit a related PR to the dev branch, would we be able to run the tests with those updated notebooks?

@btobers
Copy link
Collaborator Author

btobers commented Feb 27, 2025

Closes #77

@btobers
Copy link
Collaborator Author

btobers commented Feb 27, 2025

PyGEM-notebooks PR #12 updates all notebooks to work with the new ConfigManager class reading.

@btobers btobers mentioned this pull request Feb 27, 2025
Copy link
Contributor

@ddundo ddundo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @btobers! I made some initial comments but I think more changes will be necessary. I will also think about how to nicely test notebooks on non-main branches. I'll try to do that tomorrow.

Also, we should add tests whenever we add a new functionality. So for example, you could add a new file in pygem/tests called test_config.py which would have tests like this one:

def test_update_config():
    updates = {
        "sim.nsims": "5",
        "user.email": "updated@example.com",
        "constants.density_ice": "850"
    }
    
    config_manager = ConfigManager()  # or whatever needs to go here
    config_manager.update_config(updates)
    config = config_manager.read_config()
    
    assert config["sim"]["nsims"] == 5
    assert config["user"]["email"] == "updated@example.com"
    assert config["constants"]["density_ice"] == 850

so this tests that the update_config method does what it's supposed to do. And similarly for other methods. Then these will automatically be tested in the CI.

Let me know if I can help with this :)

@btobers
Copy link
Collaborator Author

btobers commented Feb 27, 2025

Cleaned up config.py to remove some redundancy and add some rasie errors if certain conditions are met (e.g. don't allow user to update a dictionary key in the config file directly - subkeys must be specified). This should be all set. I like the idea of making a test for this. Perhaps it's also be good to add a method in the ConfigManager class that checks to make sure that the necessary keys exist. But this could get quite messy since there's so much in there.

@btobers
Copy link
Collaborator Author

btobers commented Feb 28, 2025

@ddundo if you want to take another look at this, I cleaned up the ConfigManager class a little bit, adding error raising in the update_config function, and also created a simple config test function as you had mentioned. test_config. test_update_config () will test all the methods in the ConfigManager class (.__init__(), .ensure_config(), .crate_config(), .update_config(), .read_config()).

@btobers btobers requested a review from ddundo February 28, 2025 13:50
@btobers btobers marked this pull request as draft February 28, 2025 14:16
btobers and others added 2 commits February 28, 2025 09:28
Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>
Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>
Copy link
Contributor

@ddundo ddundo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! There are still some kinks to work out, but already looks better :) And yeah I'd encourage you to write tests first since that will help you identify bugs too.

@btobers btobers self-assigned this Feb 28, 2025
@btobers
Copy link
Collaborator Author

btobers commented Feb 28, 2025

The expanded tests look nice - although I'm not sure the TypeErrors will be flexible enough given the current format of the configuration file. This is why I only had a few type checks in the update_config().

@ddundo
Copy link
Contributor

ddundo commented Mar 2, 2025

If I update the notebooks in PyGEM-notebooks and submit a related PR to the dev branch, would we be able to run the tests with those updated notebooks?

#63 is now ready for review. When that gets merged, you could temporarily change test_suite.yml in this branch to do git clone -b 11_config_update --depth 1 https://github.com/pygem-community/PyGEM-notebooks.git. That will checkout the 11_config_update branch of PyGEM-notebooks repo and test the updates in the CI. Then before merging into dev just revert that back to how it was.

It's not the best solution, but I think it's fine for now. In the future we can discuss how to best handle this. For example, maybe we could add PyGEM-notebooks as a git submodule (see https://git-scm.com/book/en/v2/Git-Tools-Submodules).

@btobers
Copy link
Collaborator Author

btobers commented Mar 2, 2025

If I update the notebooks in PyGEM-notebooks and submit a related PR to the dev branch, would we be able to run the tests with those updated notebooks?

#63 is now ready for review. When that gets merged, you could temporarily change test_suite.yml in this branch to do git clone -b 11_config_update --depth 1 https://github.com/pygem-community/PyGEM-notebooks.git. That will checkout the 11_config_update branch of PyGEM-notebooks repo and test the updates in the CI. Then before merging into dev just revert that back to how it was.

It's not the best solution, but I think it's fine for now. In the future we can discuss how to best handle this. For example, maybe we could add PyGEM-notebooks as a git submodule (see https://git-scm.com/book/en/v2/Git-Tools-Submodules).

Great, I'll do this after we Merge branch 72_config_tests with this branch.

Copy link
Contributor

@ddundo ddundo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left comments, after which I think it will be good to go :) There are a few comments about making some methods private. Private methods are essentially methods that we don't want users to use themselves.

Before addressing the comments, I'd suggest you merge dev into this branch to pick up the latest changes and to resolve the conflict it reports.

btobers and others added 10 commits March 2, 2025 16:39
Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>
Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>
Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>
Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>
@btobers btobers requested a review from ddundo March 2, 2025 23:16
@btobers
Copy link
Collaborator Author

btobers commented Mar 2, 2025

@ddundo all issues resolved. All tests passed, but then once I switched the git clone of PyGEM-notebooks in test_suite.yml back to the main branch

git clone -b 11_config_update --depth 1 https://github.com/pygem-community/PyGEM-notebooks.git

the notebook tests all fail. Any thoughts on how we should handle this? As of now, until we merge both repo's dev branches into main I think we'll fail these tests. Perhaps there's a way for the dev branch of PyGEM to clone the notebooks fom PyGEM-notebook's dev branch, and set PyGEM main to clone from the respective main branch of PyGEM-notebooks?

So the following for PyGEM main:
git clone --depth 1 https://github.com/pygem-community/PyGEM-notebooks.git

And then for PyGEM dev:
git clone -b dev --depth 1 https://github.com/pygem-community/PyGEM-notebooks.git

I don't know if there's a way to logic the test_suite.yml in a way such that the appropriate PyGEM-notebook repo is cloned.

Copy link
Contributor

@ddundo ddundo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good :)

@btobers btobers merged commit 4c5b70b into dev Mar 3, 2025
1 check passed
@btobers btobers deleted the 72_update_config branch March 3, 2025 12:48
btobers added a commit that referenced this pull request Mar 13, 2025
* config.py now object oriented and contains update_config() function

* all prior calls to config updated for compatability with new config.ConfigManager class

* raise errors, and ensure that dictionary keys are not overwritten

* create_config() function created and _prompt_overwrite() function removed to clean up the class and remove redundancy

* config test created

* update source_config_path

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* remove self.package_dir definition

* update source path

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* rename ruamel.yaml.YAML() as ryaml

* call ensure_config() upon __init__

* user config validation, make sure necessary keys exist

* ConfigManager.ensure_config() calls removed, now handled on __init__

* bug fix with commit:4fd5493

* more tests added to test_config

* more tests and TypeError checks in update_config

* test ensure config, no overwrite

* remove CLI update_config funcitonality

* Handle datatypes in `config.yaml` and expand tests (#79)

* clean up imports

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* add `advanced_test_tw.ipynb` test

* typo fix

* add __all__ variable

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* cleanup commenting

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* cleanup commenting

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* more descriptive variable in validate_config

* private methods and expanded docstrings

* swap PyGEM-Notebooks clone repo back to main branch

* clone appropriate PyGEM-notebook brach

* comment added

---------

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>
btobers added a commit to btobers/PyGEM that referenced this pull request Mar 16, 2025
…nity#78)

* config.py now object oriented and contains update_config() function

* all prior calls to config updated for compatability with new config.ConfigManager class

* raise errors, and ensure that dictionary keys are not overwritten

* create_config() function created and _prompt_overwrite() function removed to clean up the class and remove redundancy

* config test created

* update source_config_path

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* remove self.package_dir definition

* update source path

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* rename ruamel.yaml.YAML() as ryaml

* call ensure_config() upon __init__

* user config validation, make sure necessary keys exist

* ConfigManager.ensure_config() calls removed, now handled on __init__

* bug fix with commit:4fd5493

* more tests added to test_config

* more tests and TypeError checks in update_config

* test ensure config, no overwrite

* remove CLI update_config funcitonality

* Handle datatypes in `config.yaml` and expand tests (PyGEM-Community#79)

* clean up imports

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* add `advanced_test_tw.ipynb` test

* typo fix

* add __all__ variable

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* cleanup commenting

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* cleanup commenting

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>

* more descriptive variable in validate_config

* private methods and expanded docstrings

* swap PyGEM-Notebooks clone repo back to main branch

* clone appropriate PyGEM-notebook brach

* comment added

---------

Co-authored-by: Davor Dundovic <33790330+ddundo@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants