xplain.Xsession Examples

import xplain

"""
instantiate a new xplain session instance, establish connection to server with
default url, user and password
"""
xsession = xplain.Xsession()

"""
load an xplain session with startup configuration
"""
xsession.startup('Patients')

"""
load an xplain session from an existing session, the session id could be
retrieved from other xplain client like Xplain Object Explorer
"""
xsession.load_from_session_id("30FA4025AC5AE68852D803838FAA503D")

'''
print current xplain session id, which can be used by login in a separate
client session, say XOE, just enter the session id into 'config file' field of
XOE login dialog
'''
print(xsession.get_session_id())


"""
take a glance of object tree structure
"""
xsession.show_tree()

"""
count the number of person grouped by their gender
"""
print(xsession.open_attribute(object_name="Patients", dimension_name="Gender",
                              attribute_name="Gender"))

"""
How heigh are the Arzneimittelkosten grouped by gender
"""
cost_query = {
    "groupBys": [
        {
            "groupByLevelNumber": 1,
            "groupByStatesFormHierarchyWithRoot": "Gender",
            "includeUpperLevels": False,
            "attribute": {
                "name": "Gender",
                "attribute": "Gender",
                "dimension": "Gender",
                "object": "Patients"
            }
        }
    ],
    "aggregations": [
        {
            "aggregationType": "SUM",
            "measure": "MedKosten",
            "aggregationName": "SUM(Costs)",
            "complementMissingBranches": False,
            "type": "SUM",
            "dimension": "Costs",
            "object": "Prescriptions"
        }
    ]
}

print(xsession.execute_query(cost_query))

"""
factory the query configuration using Query_config class

"""
query_config = xplain.Query_config()
query_config.add_aggregation(
    object_name="Diagnose",
    dimension_name="Diagnose", type="COUNT")
query_config.add_groupby(object_name="Diagnose",
                         dimension_name="Diagnose",
                         attribute_name="ICD")
query_config.add_selection(object_name="Diagnose",
                           dimension_name="Date_from",
                           attribute_name="Date_from",
                           selected_states=["2020-12"])
df = xsession.execute_query(query_config)
print(df)

"""
Count number of Person grouped by number ranges of prescriptions of Dermatika
"""
xsession.run({
    "method": "select",
    "selections": [
        {
            "attribute": {
                "object": "Prescription",
                "dimension": "ATC",
                "attribute": "ATC"
            },
            "selectedStates": ["D-Dermatika"]
        }
    ]
})

xsession.run({
    "method": "addAggregationDimension",
    "targetObject": "Patient",
    "dimensionName": "Patient Dermatika",
    "aggregation": {
        "aggregationType": "COUNT",
        "object": "Prescription"
    },
    "floatingSemantics": False
})

print(xsession.open_attribute("Patient", "Patient Dermatika",
                              "Patient Dermatika - Log-Ranges"))

## option 2
xsession.get_xobject("Patient").add_aggregation_dimension(
    dimension_name="#Dermatika",
    aggregation={
        "aggregationType": "COUNT",
        "object": "Prescription"
    },
    floating_semantics=False)
print(
    xsession.open_attribute("Patient", "#Dermatika", "#Dermatika - Log-Ranges"))

"""
print the person details who have 50 - 100 prescriptions of Dermatika
"""
xsession.run({
    "method": "select",
    "selections": [
        {
            "attribute": {
                "object": "Patient",
                "dimension": "Patient Dermatika",
                "attribute": "Patient Dermatika - Log-Ranges"
            },
            "selectedStates": ["]50,100]"]
        }
    ]
})
df = xsession.get_selected_instances("Patient", 20)
print(df)
# save instance details to csv file
df.to_csv('out.csv', index=False)




"""
prediction model example
"""

import_model_query = {
        'method': 'importModel',
        'fileName': 'Depression - pre-final.xmodelresult'
    }
get_independent_var = {
    "method": "getIndependentVariableCounts",
    "model": "Depression"
}

xsession.run(import_model_query)
print(xsession.get_predictive_model("Depression"))

result = xsession.perform(get_independent_var)
print(result.get('variableCounts'))



"""
relative time axis

How many patients died after their first psycho diagnose after 50 weeks
"""

select_psyc = {
    "method": "select",
    "selections": [ {
                    "attribute": {
                                 "object": "Diagnose",
                                "dimension": "Diagnose",
                                "attribute": "ICD"
                                 },
                    "selectedStates": ["05-Psych"]
    }]
}
xsession.run(select_psyc)


rel_time_query = {
            "method": "addRelativeTimeDimensions",
            "baseObject": "Patient",
            "floatingSemantics": True,
            "names": ["Dead_psy"],
            "referenceTimeDimension":
                {
                   "object": "Diagnose",
                  "dimension": "Date_from"
                  },
            "relativeTimeType": "TO_FIRST",
            "timeDimensions": [
                {
                    "object": "Patient",
                    "dimension": "Death_Date"
                }
            ]
}
xsession.run(rel_time_query)
print(xsession.open_attribute(object_name="Patient", dimension_name="Dead_psy",attribute_name="Dead_psy"))