generated from ACCESS-NRI/template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from CABLE-LSM/45-automatically-attach-uploade…
…d-files-to-model-output 45 automatically attach uploaded files to model output
- Loading branch information
Showing
9 changed files
with
172 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,6 @@ dependencies: | |
- pytest | ||
- black | ||
- ruff | ||
- pandas>=2.2.2 | ||
- pip: | ||
- -r mkdocs-requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ requirements: | |
- requests >=2.31.0 | ||
- click >=8.1.7 | ||
- PyYAML >=6.0.1 | ||
- pandas >=2.2.2 | ||
|
||
test: | ||
imports: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Methods for parallel execution.""" | ||
|
||
import pandas as pd | ||
import multiprocessing as mp | ||
|
||
|
||
def _execute(mp_args: tuple): | ||
"""Execute an instance of the parallel function. | ||
Parameters | ||
---------- | ||
mp_args : tuple | ||
2-tuple consisting of a callable and an arguments dictionary. | ||
Returns | ||
------- | ||
mixed | ||
Returning value of the callable. | ||
""" | ||
return mp_args[0](**mp_args[1]) | ||
|
||
|
||
def _convert_kwargs(**kwargs): | ||
"""Convert a dict of lists and scalars into even lists for parallel execution. | ||
Returns | ||
------- | ||
dict | ||
A dictionary of lists of arguments. | ||
""" | ||
return pd.DataFrame(kwargs).to_dict("records") | ||
|
||
|
||
def parallelise(func: callable, num_threads: int, **kwargs): | ||
"""Execute `func` in parallel over `num_threads`. | ||
Parameters | ||
---------- | ||
func : callable | ||
Function to parallelise. | ||
num_threads : int | ||
Number of threads. | ||
**kwargs : | ||
Keyword arguments for `func` all lists must have equal length, scalars will be converted to lists. | ||
Returns | ||
------- | ||
mixed | ||
Returning value of `func`. | ||
""" | ||
|
||
# Convert the kwargs to argument list of dicts | ||
mp_args = _convert_kwargs(**kwargs) | ||
|
||
# Attach the function pointer as the first argument | ||
mp_args = [[func, mp_arg] for mp_arg in mp_args] | ||
|
||
# Start with empty results | ||
results = None | ||
|
||
# Establish a pool of workers (blocking) | ||
with mp.Pool(processes=num_threads) as pool: | ||
results = pool.map(_execute, mp_args) | ||
|
||
# Return the results | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters