Coverage for tasks/sfmpq2.py: 66%
64 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2022-11-08 23:14 +0000
1#!/usr/bin/env python
3"""
4camcops_server/tasks/sfmpq2.py
6===============================================================================
8 Copyright (C) 2012, University of Cambridge, Department of Psychiatry.
9 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
11 This file is part of CamCOPS.
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.
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.
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/>.
26===============================================================================
28**Short-Form McGill Pain Questionnaire (SF-MPQ2) task.**
30"""
32from camcops_server.cc_modules.cc_constants import CssClass
33from camcops_server.cc_modules.cc_html import tr_qa, tr, answer
34from camcops_server.cc_modules.cc_request import CamcopsRequest
35from camcops_server.cc_modules.cc_sqla_coltypes import (
36 CamcopsColumn,
37 ZERO_TO_10_CHECKER,
38)
40from camcops_server.cc_modules.cc_summaryelement import SummaryElement
41from camcops_server.cc_modules.cc_task import TaskHasPatientMixin, Task
42import cardinal_pythonlib.rnc_web as ws
43from cardinal_pythonlib.stringfunc import strseq
44from sqlalchemy import Float, Integer
45from sqlalchemy.ext.declarative import DeclarativeMeta
46from typing import List, Type, Tuple, Dict, Any
49class Sfmpq2Metaclass(DeclarativeMeta):
50 # noinspection PyInitNewSignature
51 def __init__(
52 cls: Type["Sfmpq2"],
53 name: str,
54 bases: Tuple[Type, ...],
55 classdict: Dict[str, Any],
56 ) -> None:
58 # Field descriptions are open access, as per:
59 # https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5221718/
60 # https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3225325/
61 comment_strings = [
62 "throbbing",
63 "shooting",
64 "stabbing",
65 "sharp",
66 "cramping",
67 "gnawing",
68 "hot-burning",
69 "aching",
70 "heavy",
71 "tender",
72 "splitting",
73 "tiring–exhausting",
74 "sickening",
75 "fearful",
76 "punishing–cruel",
77 "electric-shock",
78 "cold-freezing",
79 "piercing",
80 "light touch",
81 "itching",
82 "tingling",
83 "numbness",
84 ]
85 score_comment = "(0 none - 10 worst)"
87 for q_index in range(0, cls.N_QUESTIONS):
88 q_num = q_index + 1
89 q_field = "q{}".format(q_num)
91 setattr(
92 cls,
93 q_field,
94 CamcopsColumn(
95 q_field,
96 Integer,
97 permitted_value_checker=ZERO_TO_10_CHECKER,
98 comment="Q{} ({}) {}".format(
99 q_num, comment_strings[q_index], score_comment
100 ),
101 ),
102 )
104 super().__init__(name, bases, classdict)
107class Sfmpq2(TaskHasPatientMixin, Task, metaclass=Sfmpq2Metaclass):
108 __tablename__ = "sfmpq2"
109 shortname = "SF-MPQ2"
111 N_QUESTIONS = 22
112 MAX_SCORE_PER_Q = 10
113 ALL_QUESTIONS = strseq("q", 1, N_QUESTIONS)
115 CONTINUOUS_PAIN_QUESTIONS = Task.fieldnames_from_list(
116 "q", {1, 5, 6, 8, 9, 10}
117 )
118 INTERMITTENT_PAIN_QUESTIONS = Task.fieldnames_from_list(
119 "q", {2, 3, 4, 11, 16, 18}
120 )
121 NEUROPATHIC_PAIN_QUESTIONS = Task.fieldnames_from_list(
122 "q", {7, 17, 19, 20, 21, 22}
123 )
124 AFFECTIVE_PAIN_QUESTIONS = Task.fieldnames_from_list("q", {12, 13, 14, 15})
126 @staticmethod
127 def longname(req: CamcopsRequest) -> str:
128 _ = req.gettext
129 return _("Short-Form McGill Pain Questionnaire 2")
131 def get_summaries(self, req: CamcopsRequest) -> List[SummaryElement]:
132 return self.standard_task_summary_fields() + [
133 SummaryElement(
134 name="total_pain",
135 coltype=Float(),
136 value=self.total_pain(),
137 comment=f"Total pain (/{self.MAX_SCORE_PER_Q})",
138 ),
139 SummaryElement(
140 name="continuous_pain",
141 coltype=Float(),
142 value=self.continuous_pain(),
143 comment=f"Continuous pain (/{self.MAX_SCORE_PER_Q})",
144 ),
145 SummaryElement(
146 name="intermittent_pain",
147 coltype=Float(),
148 value=self.intermittent_pain(),
149 comment=f"Intermittent pain (/{self.MAX_SCORE_PER_Q})",
150 ),
151 SummaryElement(
152 name="neuropathic_pain",
153 coltype=Float(),
154 value=self.neuropathic_pain(),
155 comment=f"Neuropathic pain (/{self.MAX_SCORE_PER_Q})",
156 ),
157 SummaryElement(
158 name="affective_pain",
159 coltype=Float(),
160 value=self.affective_pain(),
161 comment=f"Affective pain (/{self.MAX_SCORE_PER_Q})",
162 ),
163 ]
165 def is_complete(self) -> bool:
166 if self.any_fields_none(self.ALL_QUESTIONS):
167 return False
168 if not self.field_contents_valid():
169 return False
170 return True
172 def total_pain(self) -> float:
173 return self.mean_fields(self.ALL_QUESTIONS)
175 def continuous_pain(self) -> float:
176 return self.mean_fields(self.CONTINUOUS_PAIN_QUESTIONS)
178 def intermittent_pain(self) -> float:
179 return self.mean_fields(self.INTERMITTENT_PAIN_QUESTIONS)
181 def neuropathic_pain(self) -> float:
182 return self.mean_fields(self.NEUROPATHIC_PAIN_QUESTIONS)
184 def affective_pain(self) -> float:
185 return self.mean_fields(self.AFFECTIVE_PAIN_QUESTIONS)
187 def format_average(self, value) -> str:
188 return "{} / {}".format(
189 answer(ws.number_to_dp(value, 3, default="?")),
190 self.MAX_SCORE_PER_Q,
191 )
193 def get_task_html(self, req: CamcopsRequest) -> str:
194 rows = ""
195 for q_num in range(1, self.N_QUESTIONS + 1):
196 q_field = "q" + str(q_num)
197 question_cell = "{}. {}".format(q_num, self.wxstring(req, q_field))
199 score = getattr(self, q_field)
201 rows += tr_qa(question_cell, score)
203 html = """
204 <div class="{CssClass.SUMMARY}">
205 <table class="{CssClass.SUMMARY}">
206 {tr_is_complete}
207 {total_pain}
208 {continuous_pain}
209 {intermittent_pain}
210 {neuropathic_pain}
211 {affective_pain}
212 </table>
213 </div>
214 <table class="{CssClass.TASKDETAIL}">
215 <tr>
216 <th width="60%">Question</th>
217 <th width="40%">Answer <sup>[6]</sup></th>
218 </tr>
219 {rows}
220 </table>
221 <div class="{CssClass.FOOTNOTES}">
222 [1] Average of items 1–22.
223 [2] Average of items 1, 5, 6, 8, 9, 10.
224 [3] Average of items 2, 3, 4, 11, 16, 18.
225 [4] Average of items 7, 17, 19, 20, 21, 22.
226 [5] Average of items 12, 13, 14, 15.
227 [6] All items are rated from “0 – none” to
228 “10 – worst possible”.
229 </div>
230 """.format(
231 CssClass=CssClass,
232 tr_is_complete=self.get_is_complete_tr(req),
233 total_pain=tr(
234 self.wxstring(req, "total_pain") + " <sup>[1]</sup>",
235 self.format_average(self.total_pain()),
236 ),
237 continuous_pain=tr(
238 self.wxstring(req, "continuous_pain") + " <sup>[2]</sup>",
239 self.format_average(self.continuous_pain()),
240 ),
241 intermittent_pain=tr(
242 self.wxstring(req, "intermittent_pain") + " <sup>[3]</sup>",
243 self.format_average(self.intermittent_pain()),
244 ),
245 neuropathic_pain=tr(
246 self.wxstring(req, "neuropathic_pain") + " <sup>[4]</sup>",
247 self.format_average(self.neuropathic_pain()),
248 ),
249 affective_pain=tr(
250 self.wxstring(req, "affective_pain") + " <sup>[5]</sup>",
251 self.format_average(self.affective_pain()),
252 ),
253 rows=rows,
254 )
255 return html