-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
129 additions
and
16 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
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 |
---|---|---|
@@ -1,13 +1,104 @@ | ||
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_rscriptbridge.ipynb. | ||
|
||
# %% auto 0 | ||
__all__ = [] | ||
__all__ = ['install_R_package', 'calc_hash_from_flowobject', 'check_script_inputs', 'check_script_output', 'run_script'] | ||
|
||
# %% ../nbs/02_rscriptbridge.ipynb 3 | ||
# %% ../nbs/02_rscriptbridge.ipynb 4 | ||
import json, os | ||
import subprocess | ||
import warnings | ||
import hashlib | ||
|
||
from functools import reduce | ||
|
||
# %% ../nbs/02_rscriptbridge.ipynb 30 | ||
def install_R_package(pkg:str|list): | ||
""" | ||
Checks and if neccesary installs an R package | ||
Parameters | ||
---------- | ||
pkg : str|list | ||
name(s) of the package(s) | ||
""" | ||
|
||
if isinstance(pkg, str): | ||
pkg = [pkg] | ||
|
||
for pkg_i in pkg: | ||
run_script_result = subprocess.run(['Rscript','-e', f"library({pkg_i})"], capture_output=True) | ||
if run_script_result.returncode != 0: | ||
print(f"Installing {pkg_i}") | ||
run_script_result = subprocess.run(['Rscript','-e', f"install.packages({pkg_i}, repos='https://cloud.r-project.org')"], capture_output=True) | ||
else: | ||
print(f"Library {pkg_i} already installed") | ||
|
||
print(run_script_result.stderr.decode('UTF-8')) | ||
|
||
|
||
|
||
# %% ../nbs/02_rscriptbridge.ipynb 45 | ||
def calc_hash_from_flowobject(flow_object:dict)->str: | ||
return hashlib.md5(repr(flow_object).encode('UTF-8')).hexdigest() | ||
|
||
# %% ../nbs/02_rscriptbridge.ipynb 48 | ||
def check_script_inputs(flow_object:dict)->bool: | ||
""" | ||
Check if the input files for a script are up-to-date, returns True if up-to-date. | ||
""" | ||
|
||
checksum_file = get_save_path(f"input-checksum-{calc_hash_from_flowobject(flow_object)}") | ||
md5_check_result = subprocess.run( | ||
['md5sum', '-c', checksum_file], | ||
cwd=save_dir, | ||
capture_output=True) | ||
|
||
return int(md5_check_result.returncode) == 0 | ||
|
||
# %% ../nbs/02_rscriptbridge.ipynb 51 | ||
def check_script_output(flow_object:dict)->bool: | ||
""" | ||
Check if the output files for a script exist, returns True if they all exist. | ||
""" | ||
|
||
return all([ | ||
os.path.isfile(get_save_path(F)) | ||
for F in flow_object['out'] | ||
]) | ||
|
||
# %% ../nbs/02_rscriptbridge.ipynb 54 | ||
def run_script(flow_object): | ||
""" Run a script in R | ||
args: | ||
flow_object: dict of flow object | ||
returns: | ||
bool: False if nothing has changed, or an update failed, | ||
and True if a follow-up script might need to be run | ||
""" | ||
|
||
# Check if output exists and inputs have not changed and return False if | ||
# output exists and inputs have not changed | ||
if check_script_output(flow_object) and check_script_inputs(flow_object): | ||
return False | ||
|
||
# Run script | ||
run_script_result = subprocess.run( | ||
['Rscript', '--vanilla', get_asset_path(flow_object['name'])], | ||
cwd=save_dir, | ||
capture_output=True | ||
) | ||
|
||
# check the return code | ||
if run_script_result.returncode: | ||
print(f"Run returned code {run_script_result.returncode}") | ||
print('STDOUT------------\n', run_script_result.stdout.decode('UTF-8')) | ||
print('STDERR------------\n', run_script_result.stderr.decode('UTF-8')) | ||
return False | ||
|
||
# check the output | ||
if not check_script_output(flow_object): | ||
print(f"Output not found for {flow_object['name']}") | ||
return False | ||
|
||
return check_script_output(flow_object) | ||
|