Coverage for sfkit/utils/pca_protocol.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-08-07 15:11 -0400

1""" 

2Run the PCA protocol 

3""" 

4import os 

5import shutil 

6 

7import tomlkit 

8 

9from sfkit.utils import constants 

10from sfkit.utils.sfgwas_protocol import ( 

11 build_sfgwas, 

12 generate_shared_keys, 

13 install_sfgwas, 

14 start_sfgwas, 

15 update_config_global, 

16) 

17 

18 

19def run_pca_protocol(role: str, demo: bool = False) -> None: 

20 if not (constants.IS_DOCKER or constants.IS_INSTALLED_VIA_SCRIPT): 

21 install_sfgwas() 

22 if not demo: 

23 generate_shared_keys(int(role)) 

24 print("Begin updating config files") 

25 update_config_local(role) 

26 update_config_global(protocol="pca") 

27 if not (constants.IS_DOCKER or constants.IS_INSTALLED_VIA_SCRIPT): 

28 build_sfgwas() 

29 start_sfgwas(role, demo, protocol="PCA") 

30 

31 

32def update_config_local(role: str) -> None: 

33 """ 

34 Update configLocal.Party{role}.toml 

35 :param role: 0, 1, 2, ... 

36 """ 

37 config_file_path = f"{constants.EXECUTABLES_PREFIX}sfgwas/config/pca/configLocal.Party{role}.toml" 

38 

39 try: 

40 with open(config_file_path, "r") as f: 

41 data = tomlkit.parse(f.read()) 

42 except FileNotFoundError: 

43 print(f"File {config_file_path} not found.") 

44 print("Creating it...") 

45 shutil.copyfile(f"{constants.EXECUTABLES_PREFIX}sfgwas/config/pca/configLocal.Party2.toml", config_file_path) 

46 with open(config_file_path, "r") as f: 

47 data = tomlkit.parse(f.read()) 

48 

49 if role != "0": 

50 with open(os.path.join(constants.SFKIT_DIR, "data_path.txt"), "r") as f: 

51 data_path = f.readline().rstrip() 

52 

53 data["input_file"] = f"{data_path}/data.txt" 

54 

55 data["shared_keys_path"] = constants.SFKIT_DIR 

56 data["output_dir"] = f"out/party{role}" 

57 data["cache_dir"] = f"cache/party{role}" 

58 

59 with open(config_file_path, "w") as f: 

60 f.write(tomlkit.dumps(data))