Coverage for mlos_bench/mlos_bench/environments/status.py: 98%

54 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-14 00:55 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Enum for the status of the benchmark/environment Trial or Experiment.""" 

6 

7import enum 

8import logging 

9from typing import Any 

10 

11_LOG = logging.getLogger(__name__) 

12 

13 

14class Status(enum.Enum): 

15 """Enum for the status of the benchmark/environment Trial or Experiment.""" 

16 

17 UNKNOWN = 0 

18 PENDING = 1 

19 READY = 2 

20 RUNNING = 3 

21 SUCCEEDED = 4 

22 CANCELED = 5 

23 FAILED = 6 

24 TIMED_OUT = 7 

25 

26 @staticmethod 

27 def parse(status: Any) -> "Status": 

28 """ 

29 Convert the input to a Status enum. 

30 

31 Parameters 

32 ---------- 

33 status : Any 

34 The status to parse. This can be a string (or string convertible), 

35 int, or Status enum. 

36 

37 Returns 

38 ------- 

39 Status 

40 The corresponding Status enum value or else UNKNOWN if the input is not 

41 recognized. 

42 """ 

43 if isinstance(status, Status): 

44 return status 

45 if not isinstance(status, str): 

46 _LOG.warning("Expected type %s for status: %s", type(status), status) 

47 status = str(status) 

48 if status.isdigit(): 

49 try: 

50 return Status(int(status)) 

51 except ValueError: 

52 _LOG.warning("Unknown status: %d", int(status)) 

53 try: 

54 status = status.upper().strip() 

55 return Status[status] 

56 except KeyError: 

57 _LOG.warning("Unknown status: %s", status) 

58 return Status.UNKNOWN 

59 

60 def is_good(self) -> bool: 

61 """Check if the status of the benchmark/environment is good.""" 

62 return self in { 

63 Status.PENDING, 

64 Status.READY, 

65 Status.RUNNING, 

66 Status.SUCCEEDED, 

67 } 

68 

69 # Class based accessor method to avoid circular import 

70 @staticmethod 

71 def completed_statuses() -> frozenset["Status"]: 

72 """Get the set of :py:data:`.COMPLETED_STATUSES`.""" 

73 return COMPLETED_STATUSES 

74 

75 def is_completed(self) -> bool: 

76 """Check if the status of the benchmark/environment Trial or Experiment is one 

77 of :py:data:`.COMPLETED_STATUSES`. 

78 """ 

79 return self in COMPLETED_STATUSES 

80 

81 def is_pending(self) -> bool: 

82 """Check if the status of the benchmark/environment Trial or Experiment is 

83 PENDING. 

84 """ 

85 return self == Status.PENDING 

86 

87 def is_ready(self) -> bool: 

88 """Check if the status of the benchmark/environment Trial or Experiment is 

89 READY. 

90 """ 

91 return self == Status.READY 

92 

93 def is_running(self) -> bool: 

94 """Check if the status of the benchmark/environment Trial or Experiment is 

95 RUNNING. 

96 """ 

97 return self == Status.RUNNING 

98 

99 def is_succeeded(self) -> bool: 

100 """Check if the status of the benchmark/environment Trial or Experiment is 

101 SUCCEEDED. 

102 """ 

103 return self == Status.SUCCEEDED 

104 

105 def is_failed(self) -> bool: 

106 """Check if the status of the benchmark/environment Trial or Experiment is 

107 FAILED. 

108 """ 

109 return self == Status.FAILED 

110 

111 def is_canceled(self) -> bool: 

112 """Check if the status of the benchmark/environment Trial or Experiment is 

113 CANCELED. 

114 """ 

115 return self == Status.CANCELED 

116 

117 def is_timed_out(self) -> bool: 

118 """Check if the status of the benchmark/environment Trial or Experiment is 

119 TIMED_OUT. 

120 """ 

121 return self == Status.TIMED_OUT 

122 

123 

124COMPLETED_STATUSES = frozenset( 

125 { 

126 Status.SUCCEEDED, 

127 Status.CANCELED, 

128 Status.FAILED, 

129 Status.TIMED_OUT, 

130 } 

131) 

132""" 

133The set of completed statuses. 

134 

135Includes all statuses that indicate the trial or experiment has finished, either 

136successfully or not. 

137This set is used to determine if a trial or experiment has reached a final state. 

138This includes: 

139- :py:attr:`.Status.SUCCEEDED`: The trial or experiment completed successfully. 

140- :py:attr:`.Status.CANCELED`: The trial or experiment was canceled. 

141- :py:attr:`.Status.FAILED`: The trial or experiment failed. 

142- :py:attr:`.Status.TIMED_OUT`: The trial or experiment timed out. 

143"""