Skip to content

Commit

Permalink
rdrf #2453 fixed names in BC all patients
Browse files Browse the repository at this point in the history
  • Loading branch information
id2359 committed Apr 17, 2023
1 parent 477c218 commit 407d266
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
3 changes: 2 additions & 1 deletion rdrf/dashboards/components/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ def __init__(
static_followups={},
):
self.config_model = config_model
self.registry = config_model.registry
if self.config_model is not None:
self.config = config_model.config
self.registry = config_model.registry
else:
self.config = None
self.registry = None
self.data = data
self.title = title
self.patient = patient # none if all patients
Expand Down
30 changes: 29 additions & 1 deletion rdrf/dashboards/components/cpr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .common import BaseGraphic
from dash import dcc, html
from ..data import lookup_cde_value
from ..data import has_static_followups
from ..data import get_static_followups_handler
from ..utils import get_colour_map
from ..utils import add_seq_name
import plotly.express as px
Expand Down Expand Up @@ -36,6 +38,15 @@ class ChangesInPatientResponses(BaseGraphic):
"""

def get_graphic(self):
if self.registry is not None:
if has_static_followups(self.registry):
logger.debug("cpr has static")
self.sfuh = get_static_followups_handler(self.registry)
self.data = self.sfuh.fix_ordering_of_static_followups(self.data)
from dashboards.utils import dump

dump(f"cpr-{self.title}.csv", self.data)

field_dicts = self.config["fields"]
items = []
for field_dict in field_dicts:
Expand Down Expand Up @@ -74,6 +85,7 @@ def _create_stacked_bar_px(self, df, field, label, colour_map):

df = add_seq_name(df)
df = df.round(1)

df["text"] = df.apply(
lambda row: "<b>"
+ str(row["counts"])
Expand Down Expand Up @@ -116,6 +128,9 @@ def fix_xaxis(self, fig, data):
# this replaces the SEQ numbers on the x-axis
# with names

logger.debug("fixing x axis for cpr")
logger.debug(data)

seq_totals = data.groupby(["SEQ"])["counts"].sum()

def get_ticktext(row):
Expand All @@ -124,7 +139,20 @@ def get_ticktext(row):
seq_name = row["SEQ_NAME"]
return seq_name + " (" + str(seq_total) + ")"

data["ticktext"] = data.apply(get_ticktext, axis=1)
def get_ticktext2(row):
logger.debug(row)
seq = row["SEQ"]
seq_total = seq_totals[seq]
old_seq_name = row["SEQ_NAME"]
seq_name = self.sfuh.get_static_form_name_from_seq_name(old_seq_name)
return seq_name + " (" + str(seq_total) + ")"

if hasattr(self, "sfuh"):
func = get_ticktext2
else:
func = get_ticktext

data["ticktext"] = data.apply(func, axis=1)

fig.update_layout(
xaxis=dict(
Expand Down
43 changes: 23 additions & 20 deletions rdrf/dashboards/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rdrf.models.definition.models import ClinicalData
from rdrf.models.definition.models import RDRFContext
from rdrf.models.definition.models import CommonDataElement
from rdrf.models.definition.models import RegistryForm

from registry.patients.models import Patient

Expand Down Expand Up @@ -113,13 +114,10 @@ def _parse_config(self, config_dict: dict):

if self.followup_forms:
self.multiform = True
logger.debug("is multiform")
else:
self.multiform = False
logger.debug("is NOT multiform")

def _order_by_collection_date(self, df: pd.DataFrame):
logger.debug("ordering dataframe by collection date")
# it's more likely collection date on baseline form
# will be null hence the na_position of first
self.df.sort_values(by=[cdf], inplace=True, na_position="first")
Expand All @@ -142,19 +140,14 @@ def update_seq(row):
return row["SEQ"]

if not self.multiform:
logger.debug("applying update seq")
self.df["SEQ"] = self.df.apply(update_seq, axis=1)
else:
logger.debug("multiform!")
self.df = self._custom_ordering(self.df)

# must do this if we re-sequence:
self.df = self._assign_seq_names(self.df)

# self._sanity_check(self.df)

logger.debug("finished ordering")

def _sanity_check(self, df):
for index, row in df.iterrows():
if row["TYPE"] == "baseline":
Expand All @@ -166,8 +159,6 @@ def _reseq(self, df):
pass

def _custom_ordering(self, df):
logger.debug("applying custom ordering")

def seq_from_form(form):
if form == self.baseline_form:
return 0
Expand Down Expand Up @@ -203,8 +194,6 @@ def _reload_dataframe(self):
self.df = self._assign_seq_names(self.df)

def _assign_seq_names(self, df):
logger.debug("assigning sequence names")

def get_aus_date(row):
try:
d = row["COLLECTIONDATE"].date()
Expand Down Expand Up @@ -531,7 +520,6 @@ def combine_data(

combined_data = indiv_data.merge(avg_data, how="left", on="SEQ")
combined_data = combined_data.merge(count_data, how="left", on="SEQ")
logger.debug(f"combined data including counts = {combined_data}")
return combined_data


Expand Down Expand Up @@ -565,16 +553,18 @@ def __init__(self, registry, baseline_form, static_followups):
x["name"] for x in self.static_followups if x["seq"] != "+"
]

self.multiform = [x["name"] for x in self.static_followups if x["seq"] == "+"][
0
]

def fix_ordering_of_static_followups(self, df: pd.DataFrame) -> pd.DataFrame:
changed = False
for index, row in df.iterrows():
if row["FORM"] == self.baseline_form:
old_seq = row["SEQ"]
df.at[index, "SEQ"] = 0
logger.debug(f"""static baseline fix: {row["FORM"]} {old_seq} -> 0""")
changed = True
elif row["FORM"] in self.static_form_names:
logger.debug(f"""fixing row for static followup {row["FORM"]}""")
self.fixup_static_followup(df, index, row)
changed = True

Expand All @@ -600,23 +590,36 @@ def fixup_static_followup(self, df: pd.DataFrame, index, row):
static_seq = static_form_dict["seq"]
old_seq = row["SEQ"]
df.at[index, "SEQ"] = static_seq
logger.debug(
f"""static fu fix: {row["FORM"]} {old_seq} -> {static_seq}"""
)

def get_static_form_name(self, seq, form):
from rdrf.models.definition.models import RegistryForm

if form == self.baseline_form:
form_model = RegistryForm.objects.get(name=form)
return form_model.display_name
name = form_model.display_name
return name
else:
for form_dict in self.static_followups:
if form_dict["name"] == form:
form_model = RegistryForm.objects.get(name=form)
return form_model.display_name
name = form_model.display_name
return name

return form

def get_static_form_name_from_seq_name(self, seq_name):
if seq_name == "Baseline":
return RegistryForm.objects.get(name=self.baseline_form).display_name
else:
from dashboards.utils import seq_names as names_dict

for seq, name in names_dict.items():
if name == seq_name:
for d in self.static_followups:
if d["seq"] == seq:
return RegistryForm.objects.get(name=d["name"]).display_name
return RegistryForm.objects.get(name=self.multiform).display_name


def get_static_followups_handler(registry):
try:
Expand Down

0 comments on commit 407d266

Please sign in to comment.