Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple recipe for survey results (Observation, QuestionnaireResponse, Questionnaire) #142

Open
4 tasks
rcdilorenzo opened this issue Mar 24, 2021 · 1 comment
Assignees

Comments

@rcdilorenzo
Copy link
Contributor

Right now, we don't have Questionnaire or QuestionnaireResponse. Each of these have to be merged with the appropriate Observation resources in order to have a nice frame of the results with the survey names and versions.

  • phc.Questionnaire
  • phc.Survey
  • phc.QuestionnaireResponse
  • phc.SurveyObservation (or SurveyResults, SurveyResponse, etc)

Example usage:

phc.SurveyObservation.get_data_frame() # => Returns observations that have the survey code

phc.Survey.get_data_frame() # => Returns surveys (questionnaires) and then you pick an ID
phc.SurveyObservation.get_data_frame(questionnaire_id="...", join_survey=True) # => (Allow multiple ids)

Here's some rough code of what's needed right now to accomplish this.

import pandas as pd

questionnaires_raw = phc.Query.execute_fhir_dsl({
    "type": "select",
    "columns": [
        {"expr": {"type": "column_ref", "column": c}}
        for c in ["id", "title", "meta", "version", "status", "date",
                  "subjectType", "identifier", "description", "contained"]
    ],
    "from": [{"table": "questionnaire"}],
}, page_size=100)

questionnaires = (
    phc.Frame
    .expand(pd.DataFrame([r["_source"] for r in questionnaires_raw]))
    .sort_values("version", ascending=False)
)

questionnaire_responses_raw = phc.Query.execute_fhir_dsl({
    "type": "select",
    "columns": [
        {"expr": {"type": "column_ref", "column": c}}
        for c in ["id", "status", "questionnaire"]
    ],
    "from": [{"table": "questionnaire_response"}],
}, all_results=True)

questionnaire_responses = phc.Frame.expand(
    pd.DataFrame([r["_source"] for r in questionnaire_responses_raw]),
    code_columns=["questionnaire"]
)

responses = phc.Observation.get_data_frame(
    code="...",
    system="http://lifeomic.com/fhir/primary-survey-id",
#     all_results=True
)

def join_observation_responses(
    observation_df: pd.DataFrame,
    questionnaire_response_df: pd.DataFrame,
    questionnaire_df: pd.DataFrame
):
    return observation_df.assign(**{
        "related.target_reference": observation_df["related.target_reference"].str.replace("QuestionnaireResponse/", "")
    }).join(
        questionnaire_response_df.set_index("id"),
        on="related.target_reference",
        rsuffix=".questionnaire_response"
    ).pipe(
        lambda df: df.assign(**{
            "questionnaire.reference": df["questionnaire.reference"].str.replace("Questionnaire/", "")
        })
    ).join(
        questionnaire_df.set_index("id"),
        on="questionnaire.reference",
        rsuffix=".questionnaire"
    )


join_observation_responses(responses, questionnaire_responses, questionnaires)[[
    "id",
    "subject.display",
    "status", # ?
    "title",
    "version",
    "status.questionnaire",
    "description",
    "status.questionnaire_response",
    "code.coding_system__lifeomic.com/fhir/questionnaire/item__code",
    "code.coding_system__lifeomic.com/fhir/questionnaire/item__display",
    "code.coding_system__lifeomic.com/fhir__code",
    "code.coding_system__lifeomic.com/fhir__display",
    "code.coding_system__lifeomic.com/fhir__userSelected",
    "valueCodeableConcept.coding_display",
    "valueCodeableConcept.coding_code",
    "valueString",
    "valueQuantity.value",
    "effectiveDateTime.local"
]].rename(columns={
    "code.coding_system__lifeomic.com/fhir/questionnaire/item__code": "item_code",
    "code.coding_system__lifeomic.com/fhir/questionnaire/item__display": "item", 
})
@jairav
Copy link
Contributor

jairav commented Jan 19, 2022

@alexccl I am going to take a crack at this with the assistance of Python lord @murrayln

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants