#!/usr/bin/env python
# camcops_server/tasks/bprse.py
"""
===============================================================================
Copyright (C) 2012-2018 Rudolf Cardinal (rudolf@pobox.com).
This file is part of CamCOPS.
CamCOPS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CamCOPS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CamCOPS. If not, see <http://www.gnu.org/licenses/>.
===============================================================================
"""
from typing import Any, Dict, List, Tuple, Type
from cardinal_pythonlib.stringfunc import strseq
from sqlalchemy.ext.declarative import DeclarativeMeta
from sqlalchemy.sql.sqltypes import Integer
from camcops_server.cc_modules.cc_ctvinfo import CtvInfo, CTV_INCOMPLETE
from camcops_server.cc_modules.cc_db import add_multiple_columns
from camcops_server.cc_modules.cc_html import answer, tr, tr_qa
from camcops_server.cc_modules.cc_request import CamcopsRequest
from camcops_server.cc_modules.cc_summaryelement import SummaryElement
from camcops_server.cc_modules.cc_task import (
get_from_dict,
Task,
TaskHasClinicianMixin,
TaskHasPatientMixin,
)
from camcops_server.cc_modules.cc_trackerhelpers import TrackerInfo
# =============================================================================
# BPRS-E
# =============================================================================
class BprseMetaclass(DeclarativeMeta):
# noinspection PyInitNewSignature
def __init__(cls: Type['Bprse'],
name: str,
bases: Tuple[Type, ...],
classdict: Dict[str, Any]) -> None:
add_multiple_columns(
cls, "q", 1, cls.NQUESTIONS,
minimum=0, maximum=7,
comment_fmt="Q{n}, {s} (1-7, higher worse, or 0 for not assessed)",
comment_strings=[
"somatic concern", "anxiety", "depression", "suicidality",
"guilt", "hostility", "elevated mood", "grandiosity",
"suspiciousness", "hallucinations", "unusual thought content",
"bizarre behaviour", "self-neglect", "disorientation",
"conceptual disorganisation", "blunted affect",
"emotional withdrawal", "motor retardation", "tension",
"uncooperativeness", "excitement", "distractibility",
"motor hyperactivity", "mannerisms and posturing"]
)
super().__init__(name, bases, classdict)
[docs]class Bprse(TaskHasPatientMixin, TaskHasClinicianMixin, Task,
metaclass=BprseMetaclass):
__tablename__ = "bprse"
shortname = "BPRS-E"
longname = "Brief Psychiatric Rating Scale, Expanded"
provides_trackers = True
NQUESTIONS = 24
TASK_FIELDS = strseq("q", 1, NQUESTIONS)
MAX_SCORE = 168
[docs] def get_trackers(self, req: CamcopsRequest) -> List[TrackerInfo]:
return [TrackerInfo(
value=self.total_score(),
plot_label="BPRS-E total score",
axis_label="Total score (out of {})".format(self.MAX_SCORE),
axis_min=-0.5,
axis_max=self.MAX_SCORE + 0.5,
)]
[docs] def get_clinical_text(self, req: CamcopsRequest) -> List[CtvInfo]:
if not self.is_complete():
return CTV_INCOMPLETE
return [CtvInfo(
content="BPRS-E total score {}/{}".format(self.total_score(),
self.MAX_SCORE)
)]
[docs] def get_summaries(self, req: CamcopsRequest) -> List[SummaryElement]:
return self.standard_task_summary_fields() + [
SummaryElement(name="total",
coltype=Integer(),
value=self.total_score(),
comment="Total score (/{})".format(self.MAX_SCORE)),
]
[docs] def is_complete(self) -> bool:
return (
self.are_all_fields_complete(self.TASK_FIELDS) and
self.field_contents_valid()
)
def total_score(self) -> int:
return self.sum_fields(self.TASK_FIELDS)
[docs] def get_task_html(self, req: CamcopsRequest) -> str:
def bprs_string(x: str) -> str:
return req.wxstring("bprs", x)
main_dict = {
None: None,
0: "0 — " + bprs_string("old_option0"),
1: "1 — " + bprs_string("old_option1"),
2: "2 — " + bprs_string("old_option2"),
3: "3 — " + bprs_string("old_option3"),
4: "4 — " + bprs_string("old_option4"),
5: "5 — " + bprs_string("old_option5"),
6: "6 — " + bprs_string("old_option6"),
7: "7 — " + bprs_string("old_option7")
}
h = """
<div class="summary">
<table class="summary">
""" + self.get_is_complete_tr(req)
h += tr(req.wappstring("total_score") +
" (0–{maxscore}; 24–{maxscore} if all "
"rated)".format(maxscore=self.MAX_SCORE),
answer(self.total_score()))
h += """
</table>
</div>
<div class="explanation">
Each question has specific answer definitions (see e.g. tablet
app).
</div>
<table class="taskdetail">
<tr>
<th width="60%">Question</th>
<th width="40%">Answer <sup>[1]</sup></th>
</tr>
"""
for i in range(1, self.NQUESTIONS + 1):
h += tr_qa(
self.wxstring(req, "q" + str(i) + "_s"),
get_from_dict(main_dict, getattr(self, "q" + str(i)))
)
h += """
</table>
<div class="footnotes">
[1] All answers are in the range 1–7, or 0 (not assessed, for
some).
</div>
"""
return h