From 08ca469092e414292e5c280fd6519c39a55dc091 Mon Sep 17 00:00:00 2001 From: Yuhong Wen Date: Thu, 5 Sep 2024 20:05:11 -0400 Subject: [PATCH] trim the whitespace of the clients and gpu from the job simulator_run (#2912) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * trim the whitespace of the clients and gpu from the job simulator_run. * Added unit test. --------- Co-authored-by: Yuan-Ting Hsieh (謝沅廷) --- nvflare/job_config/fed_job_config.py | 8 ++++++++ tests/unit_test/job_config/fed_job_config_test.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/nvflare/job_config/fed_job_config.py b/nvflare/job_config/fed_job_config.py index ea5f30f639..5163f7223d 100644 --- a/nvflare/job_config/fed_job_config.py +++ b/nvflare/job_config/fed_job_config.py @@ -148,12 +148,14 @@ def simulator_run(self, workspace, clients=None, n_clients=None, threads=None, g + workspace ) if clients: + clients = self._trim_whitespace(clients) command += " -c " + str(clients) if n_clients: command += " -n " + str(n_clients) if threads: command += " -t " + str(threads) if gpu: + gpu = self._trim_whitespace(gpu) command += " -gpu " + str(gpu) new_env = os.environ.copy() @@ -385,3 +387,9 @@ def _get_deploy_map(self): deploy_map[app_name] = deploy_map.get(app_name, []) deploy_map[app_name].append(site) return deploy_map + + def _trim_whitespace(self, string: str): + strings = string.split(",") + for i in range(len(strings)): + strings[i] = strings[i].strip() + return ",".join(strings) diff --git a/tests/unit_test/job_config/fed_job_config_test.py b/tests/unit_test/job_config/fed_job_config_test.py index 8e86fd519f..a430ee2c81 100644 --- a/tests/unit_test/job_config/fed_job_config_test.py +++ b/tests/unit_test/job_config/fed_job_config_test.py @@ -32,3 +32,11 @@ def test_locate_imports(self): with tempfile.NamedTemporaryFile(dir=cwd, suffix=".py") as dest_file: imports = list(job_config.locate_imports(sf, dest_file=dest_file.name)) assert imports == expected + + def test_trim_whitespace(self): + job_config = FedJobConfig(job_name="job_name", min_clients=1) + expected = "site-0,site-1" + assert expected == job_config._trim_whitespace("site-0,site-1") + assert expected == job_config._trim_whitespace("site-0, site-1") + assert expected == job_config._trim_whitespace(" site-0,site-1 ") + assert expected == job_config._trim_whitespace(" site-0, site-1 ")