Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python 

2 

3""" 

4camcops_server/tasks/sfmpq2.py 

5 

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

7 

8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com). 

9 

10 This file is part of CamCOPS. 

11 

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

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

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

15 (at your option) any later version. 

16 

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

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

19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

20 GNU General Public License for more details. 

21 

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

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

24 

25=============================================================================== 

26 

27**Short-Form McGill Pain Questionnaire (SF-MPQ2) task.** 

28 

29""" 

30 

31from camcops_server.cc_modules.cc_constants import CssClass 

32from camcops_server.cc_modules.cc_html import tr_qa, tr, answer 

33from camcops_server.cc_modules.cc_request import CamcopsRequest 

34from camcops_server.cc_modules.cc_sqla_coltypes import ( 

35 CamcopsColumn, 

36 ZERO_TO_10_CHECKER, 

37) 

38 

39from camcops_server.cc_modules.cc_summaryelement import SummaryElement 

40from camcops_server.cc_modules.cc_task import TaskHasPatientMixin, Task 

41import cardinal_pythonlib.rnc_web as ws 

42from cardinal_pythonlib.stringfunc import strseq 

43from sqlalchemy import Float, Integer 

44from sqlalchemy.ext.declarative import DeclarativeMeta 

45from typing import List, Type, Tuple, Dict, Any 

46 

47 

48class Sfmpq2Metaclass(DeclarativeMeta): 

49 # noinspection PyInitNewSignature 

50 def __init__(cls: Type['Sfmpq2'], 

51 name: str, 

52 bases: Tuple[Type, ...], 

53 classdict: Dict[str, Any]) -> None: 

54 

55 # Field descriptions are open access, as per: 

56 # https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5221718/ 

57 # https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3225325/ 

58 comment_strings = [ 

59 "throbbing", 

60 "shooting", 

61 "stabbing", 

62 "sharp", 

63 "cramping", 

64 "gnawing", 

65 "hot-burning", 

66 "aching", 

67 "heavy", 

68 "tender", 

69 "splitting", 

70 "tiring–exhausting", 

71 "sickening", 

72 "fearful", 

73 "punishing–cruel", 

74 "electric-shock", 

75 "cold-freezing", 

76 "piercing", 

77 "light touch", 

78 "itching", 

79 "tingling", 

80 "numbness", 

81 ] 

82 score_comment = "(0 none - 10 worst)" 

83 

84 for q_index in range(0, cls.N_QUESTIONS): 

85 q_num = q_index + 1 

86 q_field = "q{}".format(q_num) 

87 

88 setattr(cls, q_field, CamcopsColumn( 

89 q_field, Integer, 

90 permitted_value_checker=ZERO_TO_10_CHECKER, 

91 comment="Q{} ({}) {}".format( 

92 q_num, 

93 comment_strings[q_index], 

94 score_comment) 

95 )) 

96 

97 super().__init__(name, bases, classdict) 

98 

99 

100class Sfmpq2(TaskHasPatientMixin, 

101 Task, 

102 metaclass=Sfmpq2Metaclass): 

103 __tablename__ = "sfmpq2" 

104 shortname = "SF-MPQ2" 

105 

106 N_QUESTIONS = 22 

107 MAX_SCORE_PER_Q = 10 

108 ALL_QUESTIONS = strseq("q", 1, N_QUESTIONS) 

109 

110 CONTINUOUS_PAIN_QUESTIONS = Task.fieldnames_from_list( 

111 "q", {1, 5, 6, 8, 9, 10}) 

112 INTERMITTENT_PAIN_QUESTIONS = Task.fieldnames_from_list( 

113 "q", {2, 3, 4, 11, 16, 18}) 

114 NEUROPATHIC_PAIN_QUESTIONS = Task.fieldnames_from_list( 

115 "q", {7, 17, 19, 20, 21, 22}) 

116 AFFECTIVE_PAIN_QUESTIONS = Task.fieldnames_from_list( 

117 "q", {12, 13, 14, 15}) 

118 

119 @staticmethod 

120 def longname(req: CamcopsRequest) -> str: 

121 _ = req.gettext 

122 return _("Short-Form McGill Pain Questionnaire 2") 

123 

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

125 return self.standard_task_summary_fields() + [ 

126 SummaryElement( 

127 name="total_pain", coltype=Float(), 

128 value=self.total_pain(), 

129 comment=f"Total pain (/{self.MAX_SCORE_PER_Q})"), 

130 SummaryElement( 

131 name="continuous_pain", coltype=Float(), 

132 value=self.continuous_pain(), 

133 comment=f"Continuous pain (/{self.MAX_SCORE_PER_Q})"), 

134 SummaryElement( 

135 name="intermittent_pain", coltype=Float(), 

136 value=self.intermittent_pain(), 

137 comment=f"Intermittent pain (/{self.MAX_SCORE_PER_Q})"), 

138 SummaryElement( 

139 name="neuropathic_pain", coltype=Float(), 

140 value=self.neuropathic_pain(), 

141 comment=f"Neuropathic pain (/{self.MAX_SCORE_PER_Q})"), 

142 SummaryElement( 

143 name="affective_pain", coltype=Float(), 

144 value=self.affective_pain(), 

145 comment=f"Affective pain (/{self.MAX_SCORE_PER_Q})"), 

146 ] 

147 

148 def is_complete(self) -> bool: 

149 if self.any_fields_none(self.ALL_QUESTIONS): 

150 return False 

151 if not self.field_contents_valid(): 

152 return False 

153 return True 

154 

155 def total_pain(self) -> float: 

156 return self.mean_fields(self.ALL_QUESTIONS) 

157 

158 def continuous_pain(self) -> float: 

159 return self.mean_fields(self.CONTINUOUS_PAIN_QUESTIONS) 

160 

161 def intermittent_pain(self) -> float: 

162 return self.mean_fields(self.INTERMITTENT_PAIN_QUESTIONS) 

163 

164 def neuropathic_pain(self) -> float: 

165 return self.mean_fields(self.NEUROPATHIC_PAIN_QUESTIONS) 

166 

167 def affective_pain(self) -> float: 

168 return self.mean_fields(self.AFFECTIVE_PAIN_QUESTIONS) 

169 

170 def format_average(self, value) -> str: 

171 return "{} / {}".format( 

172 answer(ws.number_to_dp(value, 3, default="?")), 

173 self.MAX_SCORE_PER_Q 

174 ) 

175 

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

177 rows = "" 

178 for q_num in range(1, self.N_QUESTIONS + 1): 

179 q_field = "q" + str(q_num) 

180 question_cell = "{}. {}".format(q_num, self.wxstring(req, q_field)) 

181 

182 score = getattr(self, q_field) 

183 

184 rows += tr_qa(question_cell, score) 

185 

186 html = """ 

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

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

189 {tr_is_complete} 

190 {total_pain} 

191 {continuous_pain} 

192 {intermittent_pain} 

193 {neuropathic_pain} 

194 {affective_pain} 

195 </table> 

196 </div> 

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

198 <tr> 

199 <th width="60%">Question</th> 

200 <th width="40%">Answer <sup>[6]</sup></th> 

201 </tr> 

202 {rows} 

203 </table> 

204 <div class="{CssClass.FOOTNOTES}"> 

205 [1] Average of items 1–22. 

206 [2] Average of items 1, 5, 6, 8, 9, 10. 

207 [3] Average of items 2, 3, 4, 11, 16, 18. 

208 [4] Average of items 7, 17, 19, 20, 21, 22. 

209 [5] Average of items 12, 13, 14, 15. 

210 [6] All items are rated from “0 – none” to 

211 “10 – worst possible”. 

212 </div> 

213 """.format( 

214 CssClass=CssClass, 

215 tr_is_complete=self.get_is_complete_tr(req), 

216 total_pain=tr( 

217 self.wxstring(req, "total_pain") + " <sup>[1]</sup>", 

218 self.format_average(self.total_pain()) 

219 ), 

220 continuous_pain=tr( 

221 self.wxstring(req, "continuous_pain") + " <sup>[2]</sup>", 

222 self.format_average(self.continuous_pain()) 

223 ), 

224 intermittent_pain=tr( 

225 self.wxstring(req, "intermittent_pain") + " <sup>[3]</sup>", 

226 self.format_average(self.intermittent_pain()) 

227 ), 

228 neuropathic_pain=tr( 

229 self.wxstring(req, "neuropathic_pain") + " <sup>[4]</sup>", 

230 self.format_average(self.neuropathic_pain()) 

231 ), 

232 affective_pain=tr( 

233 self.wxstring(req, "affective_pain") + " <sup>[5]</sup>", 

234 self.format_average(self.affective_pain()) 

235 ), 

236 rows=rows, 

237 ) 

238 return html