Coverage for tasks/qolbasic.py : 60%

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
3"""
4camcops_server/tasks/qolbasic.py
6===============================================================================
8 Copyright (C) 2012-2020 Rudolf Cardinal (rudolf@pobox.com).
10 This file is part of CamCOPS.
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.
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.
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/>.
25===============================================================================
27"""
29from typing import List, Optional
31from cardinal_pythonlib.maths_py import mean
32import cardinal_pythonlib.rnc_web as ws
33from sqlalchemy.sql.sqltypes import Float
35from camcops_server.cc_modules.cc_constants import CssClass
36from camcops_server.cc_modules.cc_ctvinfo import CTV_INCOMPLETE, CtvInfo
37from camcops_server.cc_modules.cc_html import answer, identity, tr
38from camcops_server.cc_modules.cc_request import CamcopsRequest
39from camcops_server.cc_modules.cc_snomed import SnomedExpression, SnomedLookup
40from camcops_server.cc_modules.cc_sqla_coltypes import (
41 CamcopsColumn,
42 PermittedValueChecker,
43)
44from camcops_server.cc_modules.cc_summaryelement import SummaryElement
45from camcops_server.cc_modules.cc_task import Task, TaskHasPatientMixin
46from camcops_server.cc_modules.cc_trackerhelpers import TrackerInfo
49# =============================================================================
50# QoL-Basic
51# =============================================================================
53DP = 3
56class QolBasic(TaskHasPatientMixin, Task):
57 """
58 Server implementation of the QoL-Basic task.
59 """
60 __tablename__ = "qolbasic"
61 shortname = "QoL-Basic"
62 provides_trackers = True
64 tto = CamcopsColumn(
65 "tto", Float,
66 permitted_value_checker=PermittedValueChecker(minimum=0, maximum=10),
67 comment="Time trade-off (QoL * 10). Prompt: ... Indicate... the "
68 "number of years in full health [0-10] that you think is "
69 "of equal value to 10 years in your current health state."
70 )
71 rs = CamcopsColumn(
72 "rs", Float,
73 permitted_value_checker=PermittedValueChecker(minimum=0, maximum=100),
74 comment="Rating scale (QoL * 100). Prompt: Mark the point on the "
75 "scale [0-100] that you feel best illustrates your current "
76 "quality of life."
77 )
79 TASK_FIELDS = ["tto", "rs"]
81 @staticmethod
82 def longname(req: "CamcopsRequest") -> str:
83 _ = req.gettext
84 return _("Quality of Life: basic assessment")
86 def get_trackers(self, req: CamcopsRequest) -> List[TrackerInfo]:
87 return [
88 TrackerInfo(
89 value=self.get_tto_qol(),
90 plot_label="Quality of life: time trade-off",
91 axis_label="TTO QoL (0-1)",
92 axis_min=0,
93 axis_max=1
94 ),
95 TrackerInfo(
96 value=self.get_rs_qol(),
97 plot_label="Quality of life: rating scale",
98 axis_label="RS QoL (0-1)",
99 axis_min=0,
100 axis_max=1
101 ),
102 ]
104 def get_clinical_text(self, req: CamcopsRequest) -> List[CtvInfo]:
105 if not self.is_complete():
106 return CTV_INCOMPLETE
107 tto_qol = self.get_tto_qol()
108 rs_qol = self.get_rs_qol()
109 mean_qol = mean([tto_qol, rs_qol])
110 return [CtvInfo(
111 content=(
112 f"Quality of life: time trade-off "
113 f"{ws.number_to_dp(tto_qol, DP)}, "
114 f"rating scale {ws.number_to_dp(rs_qol, DP)}, "
115 f"mean {ws.number_to_dp(mean_qol, DP)}."
116 )
117 )]
119 def get_summaries(self, req: CamcopsRequest) -> List[SummaryElement]:
120 return self.standard_task_summary_fields() + [
121 SummaryElement(
122 name="tto_qol", coltype=Float(),
123 value=self.get_tto_qol(),
124 comment="Quality of life (0-1), from time trade-off method"),
125 SummaryElement(
126 name="rs_qol", coltype=Float(),
127 value=self.get_rs_qol(),
128 comment="Quality of life (0-1), from rating scale method"),
129 ]
131 def is_complete(self) -> bool:
132 return (
133 self.all_fields_not_none(QolBasic.TASK_FIELDS) and
134 self.field_contents_valid()
135 )
137 def get_tto_qol(self) -> Optional[float]:
138 return self.tto / 10 if self.tto is not None else None
140 def get_rs_qol(self) -> Optional[float]:
141 return self.rs / 100 if self.rs is not None else None
143 def get_task_html(self, req: CamcopsRequest) -> str:
144 tto_qol = self.get_tto_qol()
145 rs_qol = self.get_rs_qol()
146 mean_qol = mean([tto_qol, rs_qol])
147 h = """
148 <div class="{CssClass.SUMMARY}">
149 <table class="{CssClass.SUMMARY}">
150 {tr_is_complete}
151 {mean_qol}
152 </table>
153 </div>
154 <div class="{CssClass.EXPLANATION}">
155 Quality of life (QoL) has anchor values of 0 (none) and 1
156 (perfect health), and can be asked about in several ways.
157 </div>
158 <table class="{CssClass.TASKDETAIL}">
159 <tr>
160 <th width="33%">Scale</th>
161 <th width="33%">Answer</th>
162 <td width="33%">Implied QoL</th>
163 </tr>
164 {tto}
165 {rs}
166 </table>
167 """.format(
168 CssClass=CssClass,
169 tr_is_complete=self.get_is_complete_tr(req),
170 mean_qol=tr(
171 "Mean QoL",
172 answer(ws.number_to_dp(mean_qol, DP, default=None),
173 formatter_answer=identity)
174 ),
175 tto=tr(
176 self.wxstring(req, "tto_q_s"),
177 answer(ws.number_to_dp(self.tto, DP, default=None)),
178 answer(ws.number_to_dp(tto_qol, DP, default=None),
179 formatter_answer=identity)
180 ),
181 rs=tr(
182 self.wxstring(req, "rs_q_s"),
183 answer(ws.number_to_dp(self.rs, DP, default=None)),
184 answer(ws.number_to_dp(rs_qol, DP, default=None),
185 formatter_answer=identity)
186 ),
187 )
188 return h
190 def get_snomed_codes(self, req: CamcopsRequest) -> List[SnomedExpression]:
191 if not self.is_complete():
192 return []
193 return [SnomedExpression(req.snomed(SnomedLookup.QOL_SCALE))]