Serialization
Serialization of PhenEx classes allows for easy data transfer and storage. PhenEx classes can easily be serialized with the to_dict() method on the given class. However, PhenEx also duplicates the interface of the built-in python JSON library with the dump, dumps, load and loads methods found here.
dump(obj, fp, **kwargs)
Serialize PhenEx objects as a JSON formatted stream to fp
(a .write()
-supporting file-like object).
Example:
from phenex.codelists import Codelist
from phenex.phenotypes import CodelistPhenotype
import phenex.util.serialization.json as pxjson
# Initialize with a list
cl = Codelist(
['x', 'y', 'z'],
'mycodelist'
)
entry = CodelistPhenotype(
return_date="first",
codelist=cl,
domain="DRUG_EXPOSURE",
date_range=study_period,
)
cohort = Cohort(
entry=entry
)
PATH = ''
# serialize the phenex object to file
with open(PATH, "w") as f:
pxjson.dump(cohort, f, indent=4)
Source code in phenex/util/serialization/json.py
dumps(obj, **kwargs)
load(fp, **kwargs)
Deserialize fp
(a .read()
-supporting file-like object containing a JSON document) to a PhenEx object.
Example:
Source code in phenex/util/serialization/json.py
loads(s, **kwargs)
Deserialize s
(a str
, bytes
or bytearray
instance containing a JSON document) to a PhenEx object.