Coverage for tasks/qolbasic.py: 61%

54 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-08 23:14 +0000

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/tasks/qolbasic.py 

5 

6=============================================================================== 

7 

8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry. 

9 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

10 

11 This file is part of CamCOPS. 

12 

13 CamCOPS is free software: you can redistribute it and/or modify 

14 it under the terms of the GNU General Public License as published by 

15 the Free Software Foundation, either version 3 of the License, or 

16 (at your option) any later version. 

17 

18 CamCOPS is distributed in the hope that it will be useful, 

19 but WITHOUT ANY WARRANTY; without even the implied warranty of 

20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

21 GNU General Public License for more details. 

22 

23 You should have received a copy of the GNU General Public License 

24 along with CamCOPS. If not, see <https://www.gnu.org/licenses/>. 

25 

26=============================================================================== 

27 

28""" 

29 

30from typing import List, Optional 

31 

32from cardinal_pythonlib.maths_py import mean 

33import cardinal_pythonlib.rnc_web as ws 

34from sqlalchemy.sql.sqltypes import Float 

35 

36from camcops_server.cc_modules.cc_constants import CssClass 

37from camcops_server.cc_modules.cc_ctvinfo import CTV_INCOMPLETE, CtvInfo 

38from camcops_server.cc_modules.cc_html import answer, identity, tr 

39from camcops_server.cc_modules.cc_request import CamcopsRequest 

40from camcops_server.cc_modules.cc_snomed import SnomedExpression, SnomedLookup 

41from camcops_server.cc_modules.cc_sqla_coltypes import ( 

42 CamcopsColumn, 

43 PermittedValueChecker, 

44) 

45from camcops_server.cc_modules.cc_summaryelement import SummaryElement 

46from camcops_server.cc_modules.cc_task import Task, TaskHasPatientMixin 

47from camcops_server.cc_modules.cc_trackerhelpers import TrackerInfo 

48 

49 

50# ============================================================================= 

51# QoL-Basic 

52# ============================================================================= 

53 

54DP = 3 

55 

56 

57class QolBasic(TaskHasPatientMixin, Task): 

58 """ 

59 Server implementation of the QoL-Basic task. 

60 """ 

61 

62 __tablename__ = "qolbasic" 

63 shortname = "QoL-Basic" 

64 info_filename_stem = "qol" 

65 provides_trackers = True 

66 

67 tto = CamcopsColumn( 

68 "tto", 

69 Float, 

70 permitted_value_checker=PermittedValueChecker(minimum=0, maximum=10), 

71 comment="Time trade-off (QoL * 10). Prompt: ... Indicate... the " 

72 "number of years in full health [0-10] that you think is " 

73 "of equal value to 10 years in your current health state.", 

74 ) 

75 rs = CamcopsColumn( 

76 "rs", 

77 Float, 

78 permitted_value_checker=PermittedValueChecker(minimum=0, maximum=100), 

79 comment="Rating scale (QoL * 100). Prompt: Mark the point on the " 

80 "scale [0-100] that you feel best illustrates your current " 

81 "quality of life.", 

82 ) 

83 

84 TASK_FIELDS = ["tto", "rs"] 

85 

86 @staticmethod 

87 def longname(req: "CamcopsRequest") -> str: 

88 _ = req.gettext 

89 return _("Quality of Life: basic assessment") 

90 

91 def get_trackers(self, req: CamcopsRequest) -> List[TrackerInfo]: 

92 return [ 

93 TrackerInfo( 

94 value=self.get_tto_qol(), 

95 plot_label="Quality of life: time trade-off", 

96 axis_label="TTO QoL (0-1)", 

97 axis_min=0, 

98 axis_max=1, 

99 ), 

100 TrackerInfo( 

101 value=self.get_rs_qol(), 

102 plot_label="Quality of life: rating scale", 

103 axis_label="RS QoL (0-1)", 

104 axis_min=0, 

105 axis_max=1, 

106 ), 

107 ] 

108 

109 def get_clinical_text(self, req: CamcopsRequest) -> List[CtvInfo]: 

110 if not self.is_complete(): 

111 return CTV_INCOMPLETE 

112 tto_qol = self.get_tto_qol() 

113 rs_qol = self.get_rs_qol() 

114 mean_qol = mean([tto_qol, rs_qol]) 

115 return [ 

116 CtvInfo( 

117 content=( 

118 f"Quality of life: time trade-off " 

119 f"{ws.number_to_dp(tto_qol, DP)}, " 

120 f"rating scale {ws.number_to_dp(rs_qol, DP)}, " 

121 f"mean {ws.number_to_dp(mean_qol, DP)}." 

122 ) 

123 ) 

124 ] 

125 

126 def get_summaries(self, req: CamcopsRequest) -> List[SummaryElement]: 

127 return self.standard_task_summary_fields() + [ 

128 SummaryElement( 

129 name="tto_qol", 

130 coltype=Float(), 

131 value=self.get_tto_qol(), 

132 comment="Quality of life (0-1), from time trade-off method", 

133 ), 

134 SummaryElement( 

135 name="rs_qol", 

136 coltype=Float(), 

137 value=self.get_rs_qol(), 

138 comment="Quality of life (0-1), from rating scale method", 

139 ), 

140 ] 

141 

142 def is_complete(self) -> bool: 

143 return ( 

144 self.all_fields_not_none(QolBasic.TASK_FIELDS) 

145 and self.field_contents_valid() 

146 ) 

147 

148 def get_tto_qol(self) -> Optional[float]: 

149 return self.tto / 10 if self.tto is not None else None 

150 

151 def get_rs_qol(self) -> Optional[float]: 

152 return self.rs / 100 if self.rs is not None else None 

153 

154 def get_task_html(self, req: CamcopsRequest) -> str: 

155 tto_qol = self.get_tto_qol() 

156 rs_qol = self.get_rs_qol() 

157 mean_qol = mean([tto_qol, rs_qol]) 

158 h = """ 

159 <div class="{CssClass.SUMMARY}"> 

160 <table class="{CssClass.SUMMARY}"> 

161 {tr_is_complete} 

162 {mean_qol} 

163 </table> 

164 </div> 

165 <div class="{CssClass.EXPLANATION}"> 

166 Quality of life (QoL) has anchor values of 0 (none) and 1 

167 (perfect health), and can be asked about in several ways. 

168 </div> 

169 <table class="{CssClass.TASKDETAIL}"> 

170 <tr> 

171 <th width="33%">Scale</th> 

172 <th width="33%">Answer</th> 

173 <td width="33%">Implied QoL</th> 

174 </tr> 

175 {tto} 

176 {rs} 

177 </table> 

178 """.format( 

179 CssClass=CssClass, 

180 tr_is_complete=self.get_is_complete_tr(req), 

181 mean_qol=tr( 

182 "Mean QoL", 

183 answer( 

184 ws.number_to_dp(mean_qol, DP, default=None), 

185 formatter_answer=identity, 

186 ), 

187 ), 

188 tto=tr( 

189 self.wxstring(req, "tto_q_s"), 

190 answer(ws.number_to_dp(self.tto, DP, default=None)), 

191 answer( 

192 ws.number_to_dp(tto_qol, DP, default=None), 

193 formatter_answer=identity, 

194 ), 

195 ), 

196 rs=tr( 

197 self.wxstring(req, "rs_q_s"), 

198 answer(ws.number_to_dp(self.rs, DP, default=None)), 

199 answer( 

200 ws.number_to_dp(rs_qol, DP, default=None), 

201 formatter_answer=identity, 

202 ), 

203 ), 

204 ) 

205 return h 

206 

207 def get_snomed_codes(self, req: CamcopsRequest) -> List[SnomedExpression]: 

208 if not self.is_complete(): 

209 return [] 

210 return [SnomedExpression(req.snomed(SnomedLookup.QOL_SCALE))]