1515- rcs_stats.py: functions to conduct statistical testing
1616"""
1717
18- import os
1918import glob
2019import logging
2120import sys
21+ from pathlib import Path
2222
2323import CIME .test_status
2424import CIME .utils
2525from CIME .status import append_testlog
2626from CIME .SystemTests .system_tests_common import SystemTestsCommon
27- from CIME .case . case_setup import case_setup
27+ from CIME .utils import expect
2828
2929logger = logging .getLogger (__name__ )
3030
@@ -56,37 +56,33 @@ def setup_phase(
5656 disable_git = disable_git ,
5757 )
5858 self ._case .flush ()
59- # and again...?
60- case_setup (self ._case , test_mode = False , reset = True )
6159
6260 # get run directory
6361 run_dir = self ._case .get_value ("RUNDIR" )
6462 # get n_inst
6563 n_inst = int (self ._case .get_value ("NINST_ATM" ))
6664 # return early if n_inst <= 1
6765 # we really don't want people to run this test with n_inst=1
68- if n_inst <= 1 :
69- msg = (
70- f"NINST_ATM = { n_inst } . This test requires NINST_ATM > 1. "
71- "Consider setting NINST_ATM > 1 in your env_run.xml "
72- "or use _C# specifier in test name for a multi-driver "
73- "multi-instance setup (producing # pelayout copies), "
74- "or _N# for a single-driver multi-instance setup "
75- "(dividing specified pelayout among # instances)."
76- )
77- raise ValueError (msg )
66+ expect (
67+ n_inst > 1 ,
68+ f"NINST_ATM = { n_inst } . This test requires NINST_ATM > 1. "
69+ "Consider setting NINST_ATM > 1 in your env_run.xml "
70+ "or use _C# specifier in test name for a multi-driver "
71+ "multi-instance setup (producing # pelayout copies), "
72+ "or _N# for a single-driver multi-instance setup "
73+ "(dividing specified pelayout among # instances)."
74+ )
7875
7976 # get rcs_perts functions
8077 # but first add the directory to sys.path if not already there
81- rcs_perts_path = os .path .join (
82- os .path .dirname (__file__ ), 'rcs_perts.py'
78+ rcs_perts_path = Path (__file__ ).parent / 'rcs_perts.py'
79+ expect (
80+ rcs_perts_path .exists (),
81+ f"Cannot find rcs_perts.py at { rcs_perts_path } "
8382 )
84- if not os .path .exists (rcs_perts_path ):
85- raise ImportError (
86- f"Cannot find rcs_perts.py at { rcs_perts_path } "
87- )
88- if os .path .dirname (__file__ ) not in sys .path :
89- sys .path .insert (0 , os .path .dirname (__file__ ))
83+ script_dir = str (Path (__file__ ).parent )
84+ if script_dir not in sys .path :
85+ sys .path .insert (0 , script_dir )
9086 # pylint: disable=import-outside-toplevel
9187 from rcs_perts import duplicate_yaml_file , update_yaml_file
9288
@@ -98,11 +94,14 @@ def setup_phase(
9894 for i in range (1 , n_inst + 1 ):
9995 yaml_file = f"{ run_dir } /data/scream_input.yaml_{ i :04d} "
10096 out_file = f"{ run_dir } /data/monthly_average.yaml_{ i :04d} "
101- if not os .path .isfile (yaml_file ):
102- raise FileNotFoundError (
103- f"File { yaml_file } does not exist." )
104- if not os .path .isfile (out_file ):
105- raise FileNotFoundError (f"File { out_file } does not exist." )
97+ expect (
98+ Path (yaml_file ).is_file (),
99+ f"File { yaml_file } does not exist."
100+ )
101+ expect (
102+ Path (out_file ).is_file (),
103+ f"File { out_file } does not exist."
104+ )
106105 update_yaml_file (yaml_file , i , "pert" )
107106 update_yaml_file (out_file , i , "out" )
108107
@@ -113,32 +112,29 @@ def _generate_baseline(self):
113112
114113 with CIME .utils .SharedArea ():
115114 # get the baseline and run directories
116- base_gen_dir = os .path .join (
117- self ._case .get_value ("BASELINE_ROOT" ),
118- self ._case .get_value ("BASEGEN_CASE" ),
119- )
120- run_dir = self ._case .get_value ("RUNDIR" )
115+ baseline_root = Path (self ._case .get_value ("BASELINE_ROOT" ))
116+ basegen_case = self ._case .get_value ("BASEGEN_CASE" )
117+ base_gen_dir = baseline_root / basegen_case
118+ run_dir = Path (self ._case .get_value ("RUNDIR" ))
121119
122120 # Get all files that match the ensemble pattern
123- hists = glob .glob (
124- os .path .join (run_dir , self .ENSEMBLE_FILE_PATTERN )
125- )
126- hist_files = [os .path .basename (h ) for h in hists ]
121+ hists = glob .glob (str (run_dir / self .ENSEMBLE_FILE_PATTERN ))
122+ hist_files = [Path (h ).name for h in hists ]
127123
128124 for hist in hist_files :
129- src = os . path . join ( run_dir , hist )
130- tgt = os . path . join ( base_gen_dir , hist )
125+ src = run_dir / hist
126+ tgt = base_gen_dir / hist
131127 # remove baselines if they exist
132128 # this is safe because cime forces users to use -o
133- if os . path . exists (tgt ):
134- os . remove ( tgt )
129+ if tgt . exists ():
130+ tgt . unlink ( )
135131
136132 # log and copy
137133 logger .info (
138134 "Copying ... \n \t %s \n ... to ... \n \t %s \n \n " ,
139135 src , tgt
140136 )
141- CIME .utils .safe_copy (src , tgt , preserve_meta = False )
137+ CIME .utils .safe_copy (str ( src ), str ( tgt ) , preserve_meta = False )
142138
143139 def _compare_baseline (self ):
144140 """compare phase implementation"""
@@ -159,31 +155,29 @@ def _compare_baseline(self):
159155
160156 # get the run and baseline directories
161157 run_dir = self ._case .get_value ("RUNDIR" )
162- base_dir = os .path .join (
163- self ._case .get_value ("BASELINE_ROOT" ),
164- self ._case .get_value ("BASECMP_CASE" ),
165- )
158+ baseline_root = Path (self ._case .get_value ("BASELINE_ROOT" ))
159+ basecmp_case = self ._case .get_value ("BASECMP_CASE" )
160+ base_dir = baseline_root / basecmp_case
166161
167162 # launch the statistics tests
168163 # first, import rcs_stats funcs from the other file
169- rcs_stats_path = os .path .join (
170- os .path .dirname (__file__ ), 'rcs_stats.py'
164+ rcs_stats_path = Path (__file__ ).parent / 'rcs_stats.py'
165+ expect (
166+ rcs_stats_path .exists (),
167+ f"Cannot find rcs_stats.py at { rcs_stats_path } "
171168 )
172- if not os .path .exists (rcs_stats_path ):
173- raise ImportError (
174- f"Cannot find rcs_stats.py at { rcs_stats_path } "
175- )
176169 # Add the directory to sys.path if not already there
177- if os .path .dirname (__file__ ) not in sys .path :
178- sys .path .insert (0 , os .path .dirname (__file__ ))
170+ script_dir = str (Path (__file__ ).parent )
171+ if script_dir not in sys .path :
172+ sys .path .insert (0 , script_dir )
179173 # note be extra safe and import whole file
180174 # because we want to avoid import errors of needed pkgs
181175 # pylint: disable=import-outside-toplevel
182176 import rcs_stats as rcss
183177 # now, launch
184178 comments , new_ts = rcss .run_stats_comparison (
185179 run_dir ,
186- base_dir ,
180+ str ( base_dir ) ,
187181 analysis_type = "spatiotemporal" ,
188182 test_type = "ks" ,
189183 alpha = 0.01 ,
0 commit comments