Skip to content

Commit

Permalink
#315 Use functions that have already been created
Browse files Browse the repository at this point in the history
- remove repetitive functions
- use `pathlib` functions vs `os`
  • Loading branch information
Dana Singh authored and Dana Singh committed Jan 29, 2025
1 parent 1720666 commit 1747cd9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 49 deletions.
32 changes: 8 additions & 24 deletions fre/list_/list_experiments_script.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
"""
Script combines the model yaml with exp, platform, and target to list experiment information.
"""
import os
from pathlib import Path
import yaml
import fre.yamltools.combine_yamls as cy

def join_constructor(loader, node):
"""
Allows FRE properties defined
in main yaml to be concatenated.
"""
seq = loader.construct_sequence(node)
return ''.join([str(i) for i in seq])

# To look into: ignore undefined alias error msg for listing?
# Found this somewhere but don't fully understand yet
# Found this somewhere but don't fully understand yet
#class NoAliasDumper(yaml.SafeDumper):
# def ignore_aliases(self, data):
# return True

def yaml_load(yamlfile):
"""
Load the yamlfile
"""
with open(yamlfile, 'r') as yf:
y = yaml.load(yf,Loader=yaml.Loader)

return y

def quick_combine(yml, exp, platform, target):
"""
Create intermediate combined model and exp. yaml
Expand All @@ -38,20 +20,20 @@ def quick_combine(yml, exp, platform, target):
comb = cy.init_pp_yaml(yml,exp,platform,target)
comb.combine_model()

def clean(combined):
def remove(combined):
"""
Remove intermediate combined yaml.
"""
if Path(combined).exists():
os.remove(combined)
Path(combined).unlink()
print(f"{combined} removed.")

def list_experiments_subtool(yamlfile):
"""
List the post-processing experiments available
"""
# Regsiter tag handler
yaml.add_constructor('!join', join_constructor)
yaml.add_constructor('!join', cy.join_constructor)

e = None
p = None
Expand All @@ -62,10 +44,12 @@ def list_experiments_subtool(yamlfile):
quick_combine(yamlfile,e,p,t)

# Print experiment names
c = yaml_load(combined)
c = cy.yaml_load(combined)

print("\nPost-processing experiments available:")
for i in c.get("experiments"):
print(f' - {i.get("name")}')
print("\n")
clean(combined)

# Clean intermediate combined yaml
remove(combined)
34 changes: 9 additions & 25 deletions fre/list_/list_platforms_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,16 @@
Script combines the model yaml with exp, platform, and target to list experiment information.
"""

import os
from pathlib import Path
import yaml
import fre.yamltools.combine_yamls as cy

def join_constructor(loader, node):
"""
Allows FRE properties defined
in main yaml to be concatenated.
"""
seq = loader.construct_sequence(node)
return ''.join([str(i) for i in seq])

# To look into: ignore undefined alias error msg for listing?
# Found this somewhere but don't fully understand yet
#class NoAliasDumper(yaml.SafeDumper):
# def ignore_aliases(self, data):
# return True

def yaml_load(yamlfile):
"""
Load the yamlfile
"""
with open(yamlfile, 'r') as yf:
y = yaml.load(yf,Loader=yaml.Loader)

return y

def quick_combine(yml, platform, target):
"""
Combine the intermediate model and platforms yaml.
Expand All @@ -40,36 +22,38 @@ def quick_combine(yml, platform, target):
comb.combine_model()
comb.combine_platforms()

def clean(combined):
def remove(combined):
"""
Remove intermediate combined yaml.
"""
if Path(combined).exists():
os.remove(combined)
Path(combined).unlink()
print(f"{combined} removed.")

def list_platforms_subtool(yamlfile):
"""
List the platforms available
"""
# Regsiter tag handler
yaml.add_constructor('!join', join_constructor)
yaml.add_constructor('!join', cy.join_constructor)

e = yamlfile.split("/")[-1].split(".")[0]
e = yamlfile.split("/")[-1].split(".")[0]
p = None
t = None

combined=f"combined-{e}.yaml"
yamlpath = os.path.dirname(yamlfile)
yamlpath = Path(yamlfile).parent

# Combine model / experiment
quick_combine(yamlfile,p,t)

# Print experiment names
c = yaml_load(os.path.join(yamlpath,combined))
c = cy.yaml_load(f"{yamlpath}/{combined}")

print("\nPlatforms available:")
for i in c.get("platforms"):
print(f' - {i.get("name")}')
print("\n")
clean(combined)

# Clean the intermediate combined yaml
remove(combined)

0 comments on commit 1747cd9

Please sign in to comment.