Skip to content

Commit

Permalink
Changed slog to saver (histroically made sense but not with current d…
Browse files Browse the repository at this point in the history
…esign)
  • Loading branch information
h-0-0 committed Dec 2, 2023
1 parent 481f939 commit 22ca289
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- run: pip install pdoc
# ADJUST THIS: build your documentation into docs/.
# We use a custom build script for pdoc itself, ideally you just run `pdoc -o docs/ ...` here.
- run: pdoc --docformat google -o docs/.html src
- run: pdoc --docformat google --logo "https://github.com/h-0-0/slune/slune.jpeg" -o docs/.html src

- uses: actions/upload-pages-artifact@v2
with:
Expand Down
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Contributing
If you would like to contribute to slune please first familiarize yourself with the package by taking a look at the docs. In particular please read about the class design, the base classes and take a look at the code for the helper functions in the slune module.

To contribute to the package please either submit a pull request for an open issue or open a new issue. If you are unsure about whether to open a new issue or in general have any problems please open a discussion in the discussions tab.

Checklist for contributing:
- [ ] Make sure that your code is well documented and that the documentation is clear and concise, use google style docstrings.
- [ ] Make sure that your code is well tested and that the tests are clear and concise, want to keep coverage as high as possible! (minimum 90%) and keep tests as simple as possible!
- [ ] Make sure that you only solve the issue that you are attempting to close, if you find other issues please open a new issue for them.
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ if __name__ == "__main__":
test_y = [7, 8, 9]
test_predictions = linear_regression_regularized(X, y, test_X, alpha)

# First let's load in a function that we can use to get a saver object that uses the default method of logging (we call this object a slog = saver + logger). The saving will be coordinated by a csv saver object which saves and reads results from csv files stored in a hierarchy of directories.
from slune import get_csv_slog
csv_slog = get_csv_slog(params = args)
# First let's load in a function that we can use to get a saver object that uses the default method of logging. The saving will be coordinated by a csv saver object which saves and reads results from csv files stored in a hierarchy of directories.
from slune import get_csv_saver
csv_saver = get_csv_saver(params = args)

# Let's now calculate the mean squared error of our predictions and log it!
mse = mean((test_y[i] - test_predictions[i])**2 for i in range(len(test_y)))
csv_slog.log({'mse': mse})
csv_saver.log({'mse': mse})

# Let's now save our logged results!
slog.save_collated()
csv_saver.save_collated()
```
Now let's write some code that will submit some jobs to train our model using different hyperparameters!!
```python
Expand All @@ -92,29 +92,28 @@ from slune.searchers import SearcherGrid
grid_searcher = SearcherGrid({'alpha' : [0.25, 0.5, 0.75]}, runs = 1)

# Let's now import a function which will submit a job for our model, the script_path specifies the path to the script that contains the model we want to train. The template_path specifies the path to the template script that we want to specify the job with, cargs is a list of constant arguments we want to pass to the script for each tuning.
# We set slog to None as we don't want to not run jobs if we have already run them before.
# We set saver to None as we don't want to not run jobs if we have already run them before.
from slune import sbatchit
script_path = 'model.py'
template_path = 'template.sh'
sbatchit(script_path, template_path, grid_searcher, cargs=[], slog=None)
sbatchit(script_path, template_path, grid_searcher, cargs=[], saver=None)
```
Now we've submitted our jobs we will wait for them to finish 🕛🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛, now that they are finished we can read the results!
```python
from slune import get_csv_slog
csv_slog = get_csv_slog(params = None)
params, value = csv_slog.read(params = [], metric_name = 'mse', select_by ='min')
from slune import get_csv_saver
csv_saver = get_csv_saver(params = None)
params, value = csv_saver.read(params = [], metric_name = 'mse', select_by ='min')
print(f'Best hyperparameters: {params}')
print(f'Their MSE: {value}')
```
Amazing! 🥳 We have successfully used slune to train our model. I hope this gives you a good flavour of how you can use slune and how easy it is to use!

Please check out the examples folder for notebooks detailing in more depth some potential ways you can use slune. The docs are not yet up and running 😢 but they are coming soon!
Please check out the examples folder for notebooks detailing in more depth some potential ways you can use slune and of course please check out the docs!

## Roadmap
- Make package user friendly:
- Go through automation settings.
- Code of conduct.
- Contributing guidelines.
- Add to pypi.
Still in early stages! First thing on the horizon is better integration with SLURM:
- Set-up notifications for job completion, failure, etc.
Expand Down
20 changes: 10 additions & 10 deletions examples/From_README/regularized_linear_regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@
" test_y = [7, 8, 9]\n",
" test_predictions = linear_regression_regularized(X, y, test_X, alpha)\n",
"\n",
" # First let's load in a function that we can use to get a saver object that uses the default method of logging (we call this object a slog = saver + logger). The saving will be coordinated by a csv saver object which saves and reads results from csv files stored in a hierarchy of directories.\n",
" from slune import get_csv_slog\n",
" csv_slog = get_csv_slog(params = args)\n",
" # First let's load in a function that we can use to get a saver object that uses the default method of logging. The saving will be coordinated by a csv saver object which saves and reads results from csv files stored in a hierarchy of directories.\n",
" from slune import get_csv_saver\n",
" csv_saver = get_csv_saver(params = args)\n",
"\n",
" # Let's now calculate the mean squared error of our predictions and log it!\n",
" mse = mean((test_y[i] - test_predictions[i])**2 for i in range(len(test_y)))\n",
" csv_slog.log({'mse': mse})\n",
" csv_saver.log({'mse': mse})\n",
"\n",
" # Let's now save our logged results!\n",
" slog.save_collated()"
" csv_saver.save_collated()"
]
},
{
Expand All @@ -100,11 +100,11 @@
"grid_searcher = SearcherGrid({'alpha' : [0.25, 0.5, 0.75]}, runs = 1)\n",
"\n",
"# Let's now import a function which will submit a job for our model, the script_path specifies the path to the script that contains the model we want to train. The template_path specifies the path to the template script that we want to specify the job with, cargs is a list of constant arguments we want to pass to the script for each tuning. \n",
"# We set slog to None as we don't want to not run jobs if we have already run them before.\n",
"# We set saver to None as we don't want to not run jobs if we have already run them before.\n",
"from slune import sbatchit\n",
"script_path = 'model.py'\n",
"template_path = 'template.sh'\n",
"sbatchit(script_path, template_path, grid_searcher, cargs=[], slog=None)"
"sbatchit(script_path, template_path, grid_searcher, cargs=[], saver=None)"
]
},
{
Expand All @@ -120,9 +120,9 @@
"metadata": {},
"outputs": [],
"source": [
"from slune import get_csv_slog\n",
"csv_slog = get_csv_slog(params = None)\n",
"params, value = csv_slog.read(params = [], metric_name = 'mse', select_by ='min')\n",
"from slune import get_csv_saver\n",
"csv_saver = get_csv_saver(params = None)\n",
"params, value = csv_saver.read(params = [], metric_name = 'mse', select_by ='min')\n",
"print(f'Best hyperparameters: {params}')\n",
"print(f'Their MSE: {value}')"
]
Expand Down
Binary file added slune.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""
.. include:: ../README.md
.. include:: ../CLASSDESIGN.md
.. include:: ../CONTRIBUTING.md
"""
4 changes: 2 additions & 2 deletions src/slune/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from .searchers import *
from .savers import *
from .loggers import *
from .slune import submit_job, sbatchit, lsargs, garg, get_csv_slog
from .slune import submit_job, sbatchit, lsargs, garg, get_csv_saver
from . import base, utils

# __all__ = ['submit_job', 'sbatchit', 'lsargs', 'garg', 'get_csv_slog',
# __all__ = ['submit_job', 'sbatchit', 'lsargs', 'garg', 'get_csv_saver',
# 'base', 'utils', 'default', 'grid', 'csv']

import importlib.metadata
Expand Down
2 changes: 1 addition & 1 deletion src/slune/slune.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def single_garg(arg_name):
else:
return single_garg(arg_names)

def get_csv_slog(params: Optional[dict]= None, root_dir: Optional[str]='slune_results') -> BaseSaver:
def get_csv_saver(params: Optional[dict]= None, root_dir: Optional[str]='slune_results') -> BaseSaver:
""" Returns a SaverCsv object with the given parameters and root directory.
Args:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_slune.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def test_sbatchit(self, mock_run):
searcher = MagicMock()
searcher.__iter__.return_value = [['arg1', 'arg2'], ['arg3', 'arg4']]
cargs = ["carg1", "carg2"]
slog = None
saver = None

# Act
sbatchit(script_path, template_path, searcher, cargs, slog)
sbatchit(script_path, template_path, searcher, cargs, saver)

# Assert
calls = [call(['sbatch', template_path, script_path, 'carg1', 'carg2', 'arg1', 'arg2'], check=True),
Expand Down

0 comments on commit 22ca289

Please sign in to comment.