Coverage for sfkit/protocol/run_protocol.py: 100%

48 statements  

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

1import time 

2 

3from sfkit.api import get_doc_ref_dict, update_firestore 

4from sfkit.utils.gwas_protocol import run_gwas_protocol 

5from sfkit.utils.pca_protocol import run_pca_protocol 

6from sfkit.utils.sfgwas_protocol import run_sfgwas_protocol 

7from sfkit.api import create_cp0, get_username 

8from sfkit.utils.helper_functions import authenticate_user 

9 

10 

11def run_protocol( 

12 phase: str = "", demo: bool = False, send_results: str = "", results_path: str = "", retry: bool = False 

13) -> None: 

14 authenticate_user() 

15 

16 if phase and phase not in ["1", "2", "3"]: 

17 raise ValueError("phase must be 1, 2, or 3") 

18 

19 doc_ref_dict: dict = get_doc_ref_dict() 

20 username = get_username() 

21 

22 if doc_ref_dict["demo"]: 

23 demo = True 

24 

25 if send_results: 

26 update_firestore(f"update_firestore::SEND_RESULTS={send_results}") 

27 if results_path: 

28 update_firestore(f"update_firestore::RESULTS_PATH={results_path}") 

29 

30 role: str = str(doc_ref_dict["participants"].index(username)) 

31 study_type: str = doc_ref_dict["study_type"] 

32 statuses: dict = doc_ref_dict["status"] 

33 

34 if ( 

35 statuses[username] in ["validated data", "running1", "running2"] 

36 or (role == "0" and statuses[username] == "ready to begin sfkit") 

37 or retry 

38 ): 

39 statuses[username] = "ready to begin protocol" 

40 update_firestore("update_firestore::status=ready to begin protocol") 

41 if statuses[username] == "ready to begin protocol": 

42 while not demo and other_participant_not_ready(list(statuses.values())): 

43 print("Other participant(s) not yet ready. Waiting... (press CTRL-C to cancel)") 

44 update_firestore("update_firestore::task=Waiting for other participant(s) to be ready") 

45 time.sleep(5) 

46 doc_ref_dict = get_doc_ref_dict() 

47 statuses = doc_ref_dict["status"] 

48 

49 if not demo and role == "1": 

50 create_cp0() 

51 

52 if phase: 

53 update_firestore(f"update_firestore::status=running phase {phase} of {study_type} protocol") 

54 else: 

55 update_firestore(f"update_firestore::status=running {study_type} protocol") 

56 

57 if study_type == "MPC-GWAS": 

58 run_gwas_protocol(role, demo) 

59 elif study_type == "SF-GWAS": 

60 run_sfgwas_protocol(role, phase, demo) 

61 elif study_type == "PCA": 

62 run_pca_protocol(role, demo) 

63 else: 

64 raise ValueError(f"Unknown study type: {study_type}") 

65 else: 

66 print("Your status is not ready. Exiting now.") 

67 exit(1) 

68 

69 

70def other_participant_not_ready(status_list: list) -> bool: 

71 return ( 

72 "" in status_list 

73 or "FAILED" in str(status_list) 

74 or "ready to begin sfkit" in str(status_list) 

75 or "setting up your vm instance" in str(status_list) 

76 )