Skip to content

Commit 5a51dd6

Browse files
authored
Merge pull request #748 from CosmoStat/refactor/number-list-replaces-exclusive
Replace -e/--exclusive flag with NUMBER_LIST (#746)
2 parents 6b30fd4 + e9473a0 commit 5a51dd6

9 files changed

Lines changed: 186 additions & 56 deletions

File tree

docs/source/configuration.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ The following options can be added to the `[FILE]` section of the config file
8484
(*e.g.* `.`, `-`, `:`, *etc.*). *optional*ly a regular expression can also be
8585
passed if it is preceded by `RE:` (*e.g.* `RE:-\d{9}`).
8686
- `NUMBER_LIST` : (`str` or `list`, *optional*) A list of number strings
87-
matching the numbering scheme or a file name.
87+
matching the numbering scheme or a file name. Restricts the run to these
88+
numbers; every entry must match an input file found on disk, otherwise the
89+
run fails at start-up. This is also how a single image is processed per
90+
job (formerly the `-e`/`--exclusive` command-line flag).
8891
- `CORRECT_FILE_PATTERN` : (`bool`, *optional*) Option to allow substring file
8992
patterns. Default value is `True`.
9093

scripts/sh/init_run_exclusive_canfar.sh

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,29 @@ function message() {
144144
fi
145145
}
146146

147+
# Write an updated copy of a shapepipe config with NUMBER_LIST set to the
148+
# given image ID, expressed in the numbering scheme (leading dash, dots ->
149+
# dashes). Replaces the retired shapepipe_run -e/--exclusive flag (#746).
150+
function set_config_number_list() {
151+
local config_orig=$1
152+
local config_upd=$2
153+
local _id=$3
154+
155+
local number="-$(echo $_id | tr '.' '-')"
156+
local config_tmp="${config_upd}.tmp"
157+
158+
if grep -q "^NUMBER_LIST" "$config_orig"; then
159+
perl -pe 's/^NUMBER_LIST\s*=.*/NUMBER_LIST = '$number'/' "$config_orig" > "$config_tmp"
160+
else
161+
perl -pe 's/^\[FILE\][ \t]*$/[FILE]\nNUMBER_LIST = '$number'/' "$config_orig" > "$config_tmp"
162+
fi
163+
if ! grep -q "^NUMBER_LIST = $number$" "$config_tmp"; then
164+
echo "set_config_number_list: failed to set NUMBER_LIST in $config_orig" >&2
165+
exit 1
166+
fi
167+
mv "$config_tmp" "$config_upd"
168+
}
169+
147170

148171
# Init message
149172
message "test=$test_only" $debug_out -1
@@ -165,7 +188,7 @@ if [ "$job" == "-1" ]; then
165188
message "No job indicated, use option -j" $debug_out 2
166189
fi
167190

168-
if [ "$exclusive" == "-1" ]; then
191+
if [ "$ID" == "-1" ]; then
169192
message "No image ID indicated, use option -e" $debug_out 3
170193
fi
171194

@@ -267,7 +290,8 @@ if [ "$fix" == "1" ]; then
267290
message "Unzip weight ($dry_run)" $debug_out -1
268291
command "cd tile_runs/$ID" $dry_run
269292
export SP_RUN=`pwd`
270-
command "shapepipe_run -c cfis/config_tile_Uz.ini -e $ID" $dry_run
293+
command "set_config_number_list cfis/config_tile_Uz.ini config_tile_Uz_upd.ini $ID" $dry_run
294+
command "shapepipe_run -c config_tile_Uz_upd.ini" $dry_run
271295

272296
cd $dir
273297
else
@@ -384,7 +408,8 @@ if [ $do_job != 0 ] && [ "$sp_local" == "1" ]; then
384408
fi
385409
command "update_runs_log_file.py" $dry_run
386410
export SP_RUN=`pwd`
387-
command "shapepipe_run -c cfis/config_exp_Sp.ini -e $exp_ID" $dry_run
411+
command "set_config_number_list cfis/config_exp_Sp.ini config_exp_Sp_upd.ini $exp_ID" $dry_run
412+
command "shapepipe_run -c config_exp_Sp_upd.ini" $dry_run
388413

389414
# Only keep CCD of this ID
390415
command "mkdir -p output/run_sp_exp_Sp_shdu/split_exp_runner/output" $dry_run

scripts/sh/job_sp_canfar.bash

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,19 @@ function command_sp() {
261261
function command_cfg_shapepipe() {
262262
local config_name=$1
263263
local str=$2
264-
local _n_smp=$3
264+
local _n_smp=$3
265265
local _exclusive=$4
266266

267-
if [ "$exclusive" != "" ]; then
268-
exclusive_flag="-e $_exclusive"
269-
else
270-
exclusive_flag=""
267+
config_upd=$(set_config_n_smp $config_name $_n_smp)
268+
269+
# Run a single image ID via NUMBER_LIST in an updated config copy;
270+
# replaces the retired shapepipe_run -e/--exclusive flag (#746)
271+
if [ "$_exclusive" != "" ]; then
272+
set_config_number_list "$config_upd" "$SP_CONFIG_MOD/$config_name" "$_exclusive"
273+
config_upd="$SP_CONFIG_MOD/$config_name"
271274
fi
272275

273-
config_upd=$(set_config_n_smp $config_name $_n_smp)
274-
#local cmd="/arc/home/kilbinger/.conda/envs/shapepipe/bin/shapepipe_run -c $config_upd $exclusive_flag"
275-
local cmd="shapepipe_run -c $config_upd $exclusive_flag"
276+
local cmd="shapepipe_run -c $config_upd"
276277
command_sp "$cmd" "$str"
277278
}
278279

@@ -337,6 +338,29 @@ function update_config() {
337338
| perl -ane 's/'$key'\s+=.+/'$key' = '$val_upd'/; print' > $config_upd
338339
}
339340

341+
# Write an updated copy of a shapepipe config with NUMBER_LIST set to the
342+
# given image ID, expressed in the numbering scheme (leading dash, dots ->
343+
# dashes). Replaces the retired shapepipe_run -e/--exclusive flag (#746).
344+
function set_config_number_list() {
345+
local config_orig=$1
346+
local config_upd=$2
347+
local _id=$3
348+
349+
local number="-$(echo $_id | tr '.' '-')"
350+
local config_tmp="${config_upd}.tmp"
351+
352+
if grep -q "^NUMBER_LIST" "$config_orig"; then
353+
perl -pe 's/^NUMBER_LIST\s*=.*/NUMBER_LIST = '$number'/' "$config_orig" > "$config_tmp"
354+
else
355+
perl -pe 's/^\[FILE\][ \t]*$/[FILE]\nNUMBER_LIST = '$number'/' "$config_orig" > "$config_tmp"
356+
fi
357+
if ! grep -q "^NUMBER_LIST = $number$" "$config_tmp"; then
358+
echo "set_config_number_list: failed to set NUMBER_LIST in $config_orig" >&2
359+
exit 1
360+
fi
361+
mv "$config_tmp" "$config_upd"
362+
}
363+
340364
### Start ###
341365

342366
echo "Start processing"

scripts/sh/job_sp_canfar_v2.0.bash

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ export SP_RUN=`pwd`
165165
# Config file path
166166
export SP_CONFIG=$SP_RUN/cfis
167167

168+
# Path for updated (per-job) config file copies
169+
export SP_CONFIG_MOD=$SP_RUN/cfis_mod
170+
168171
# Root directory for per-exposure work directories.
169172
# Set SP_EXP in the environment to override; otherwise falls back to the
170173
# conventional layout (SP_RUN = .../v2.0/tiles/IDra/ID, three levels up + exp).
@@ -243,27 +246,52 @@ function command () {
243246
fi
244247
}
245248

249+
# Write an updated copy of a shapepipe config with NUMBER_LIST set to the
250+
# given image ID, expressed in the numbering scheme (leading dash, dots ->
251+
# dashes). Replaces the retired shapepipe_run -e/--exclusive flag (#746).
252+
function set_config_number_list() {
253+
local config_orig=$1
254+
local config_upd=$2
255+
local _id=$3
256+
257+
local number="-$(echo $_id | tr '.' '-')"
258+
local config_tmp="${config_upd}.tmp"
259+
260+
if grep -q "^NUMBER_LIST" "$config_orig"; then
261+
perl -pe 's/^NUMBER_LIST\s*=.*/NUMBER_LIST = '$number'/' "$config_orig" > "$config_tmp"
262+
else
263+
perl -pe 's/^\[FILE\][ \t]*$/[FILE]\nNUMBER_LIST = '$number'/' "$config_orig" > "$config_tmp"
264+
fi
265+
if ! grep -q "^NUMBER_LIST = $number$" "$config_tmp"; then
266+
echo "set_config_number_list: failed to set NUMBER_LIST in $config_orig" >&2
267+
exit 1
268+
fi
269+
mv "$config_tmp" "$config_upd"
270+
}
271+
246272
# Set up config file and call shapepipe_run.
247-
# Batch size is passed via --batch_size flag; no config editing needed.
273+
# Batch size is passed via --batch_size flag.
248274
function command_cfg_shapepipe() {
249275
local config_name=$1
250276
local str=$2
251277
local _n_smp=$3
252278
local _exclusive=$4
253279

254-
if [ "$exclusive" != "" ]; then
255-
exclusive_flag="-e $_exclusive"
256-
else
257-
exclusive_flag=""
280+
local config="$SP_CONFIG/$config_name"
281+
282+
# Run a single image ID via NUMBER_LIST in an updated config copy;
283+
# replaces the retired shapepipe_run -e/--exclusive flag (#746)
284+
if [ "$_exclusive" != "" ]; then
285+
set_config_number_list "$config" "$SP_CONFIG_MOD/$config_name" "$_exclusive"
286+
config="$SP_CONFIG_MOD/$config_name"
258287
fi
259288

260289
local batch_flag=""
261290
if [[ $_n_smp != -1 ]]; then
262291
batch_flag="--batch_size $_n_smp"
263292
fi
264293

265-
local config="$SP_CONFIG/$config_name"
266-
local cmd="shapepipe_run.py -c $config $exclusive_flag $batch_flag"
294+
local cmd="shapepipe_run.py -c $config $batch_flag"
267295
command "$cmd" "$str"
268296
}
269297

@@ -275,6 +303,7 @@ echo "Start processing"
275303
mkdir -p $SP_RUN
276304
cd $SP_RUN
277305
mkdir -p $OUTPUT
306+
mkdir -p $SP_CONFIG_MOD
278307

279308
# Processing
280309

src/shapepipe/pipeline/args.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,6 @@ def create_arg_parser():
135135
help="configuration file name",
136136
)
137137

138-
optional.add_argument(
139-
"-e",
140-
"--exclusive",
141-
help="exclusive input file number string",
142-
)
143-
144138
optional.add_argument(
145139
"-b",
146140
"--batch_size",

src/shapepipe/pipeline/file_handler.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@ class FileHandler(object):
3232
List of modules to be run
3333
config : CustomParser
3434
Configuaration parser instance
35-
exclusive : str, optional
36-
Run this file number string exclusively if given, the default is None
3735
verbose : bool, optional
3836
Verbose setting, default is True
3937
4038
"""
4139

42-
def __init__(self, run_name, modules, config, exclusive=None, verbose=True):
40+
def __init__(self, run_name, modules, config, verbose=True):
4341

4442
self._run_name = run_name
4543

@@ -48,7 +46,6 @@ def __init__(self, run_name, modules, config, exclusive=None, verbose=True):
4846
raise ValueError("Invalid module list, check for a trailing comma")
4947

5048
self._config = config
51-
self._exclusive = exclusive
5249
self._verbose = verbose
5350

5451
self.module_runners = get_module_runners(self._module_list)
@@ -1089,7 +1086,20 @@ def _format_process_list(
10891086
if isinstance(self._number_list, type(None)):
10901087
number_list = np.load(memory_map, mmap_mode="r")
10911088
else:
1089+
# NUMBER_LIST comes from the config on faith; check every
1090+
# entry against the numbers actually found on disk so that a
1091+
# wrong ID fails here, at start-up, rather than when a module
1092+
# first tries to open the (non-existent) files (#746).
10921093
number_list = self._number_list
1094+
scanned = set(np.load(memory_map, mmap_mode="r"))
1095+
missing = [num for num in number_list if num not in scanned]
1096+
if missing:
1097+
raise ValueError(
1098+
f"No input file found matching NUMBER_LIST "
1099+
f"entr{'ies' if len(missing) > 1 else 'y'} "
1100+
f"{missing}; {len(scanned)} input file number(s) "
1101+
f"found on disk."
1102+
)
10931103

10941104
if len(number_list) == 0:
10951105
msg = "Empty number list"
@@ -1107,20 +1117,6 @@ def _format_process_list(
11071117
+ f'numbering scheme "{num_scheme}".'
11081118
)
11091119

1110-
# If "exclusive" options is set: discard all non-matching IDs
1111-
if self._exclusive is not None:
1112-
id_to_test = f"-{self._exclusive.replace('.', '-')}"
1113-
if number == id_to_test:
1114-
if self._verbose:
1115-
print(
1116-
f"-- Using exclusive number {self._exclusive} ({id_to_test})"
1117-
)
1118-
else:
1119-
if self._verbose:
1120-
# print(f"Skipping {number}, not equal to {self._exclusive} ({id_to_test})")
1121-
pass
1122-
continue
1123-
11241120
if run_method == "serial":
11251121
process_items = []
11261122
else:
@@ -1134,11 +1130,7 @@ def _format_process_list(
11341130
process_list.append(process_items)
11351131

11361132
if len(process_list) == 0:
1137-
msg = "Empty process list"
1138-
if self._exclusive is not None:
1139-
if len(number_list) > 0:
1140-
msg = f"{msg}. No input file found matching exclusive ID"
1141-
raise ValueError(msg)
1133+
raise ValueError("Empty process list")
11421134

11431135
return process_list
11441136

src/shapepipe/pipeline/job_handler.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ class JobHandler(object):
4242
Joblib backend, the default is None (which corresponds to 'loky')
4343
timeout : int, optional
4444
Timeout limit for a given job in seconds, the default is None
45-
exclusive : str, optional
46-
Run this file number string exclusively if given, the default is None
4745
verbose : bool, optional
4846
Verbose setting, default is True
4947
@@ -60,7 +58,6 @@ def __init__(
6058
batch_size=None,
6159
backend=None,
6260
timeout=None,
63-
exclusive=None,
6461
verbose=True,
6562
):
6663

@@ -75,7 +72,6 @@ def __init__(
7572
self._module = module
7673
self._module_runner = self.filehd.module_runners[self._module]
7774
self.error_count = 0
78-
self.exclusive = exclusive
7975
self._verbose = verbose
8076

8177
# Add the job parameters to the log

src/shapepipe/run.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ def set_up(self):
6868
self._set_run_name()
6969
self.modules = self.config.getlist("EXECUTION", "MODULE")
7070
self.mode = self.config.get("EXECUTION", "MODE").lower()
71-
self.exclusive = self._args.exclusive
7271
self.verbose = self.config.getboolean("DEFAULT", "VERBOSE")
7372
self.filehd = FileHandler(
7473
self._run_name,
7574
self.modules,
7675
self.config,
77-
exclusive=self._args.exclusive,
7876
verbose=self.verbose,
7977
)
8078
self.error_count = 0
@@ -355,7 +353,6 @@ def run_smp(pipe):
355353
config=pipe.config,
356354
log=pipe.log,
357355
job_type=pipe.run_method[module],
358-
exclusive=pipe.exclusive,
359356
verbose=pipe.verbose,
360357
batch_size=pipe._args.batch_size,
361358
)
@@ -415,7 +412,6 @@ def run_mpi(pipe, comm):
415412
log=pipe.log,
416413
job_type=pipe.run_method[module],
417414
parallel_mode="mpi",
418-
exclusive=pipe.exclusive,
419415
verbose=verbose,
420416
batch_size=pipe._args.batch_size,
421417
)

0 commit comments

Comments
 (0)