pyhealth.data.Patient#
Another basic data structure in the package. A Patient is a collection of Visit for the current patients. It contains all necessary attributes of a patient, such as ethnicity, mortality status, gender, etc. It can support various healthcare tasks.
- class pyhealth.data.Patient(patient_id, birth_datetime=None, death_datetime=None, gender=None, ethnicity=None, **attr)[source]#
Bases:
object
Contains information about a single patient.
A patient is a person who is admitted at least once to a hospital or a specific department. Each patient is associated with a list of visits.
- Parameters:
patient_id (
str
) – unique identifier of the patient.birth_datetime (
Optional
[datetime
]) – timestamp of patient’s birth. Default is None.death_datetime (
Optional
[datetime
]) – timestamp of patient’s death. Default is None.gender – gender of the patient. Default is None.
ethnicity – ethnicity of the patient. Default is None.
**attr – optional attributes to add to the patient as key=value pairs.
- attr_dict#
Dict, dictionary of patient attributes. Each key is an attribute name and each value is the attribute’s value.
- visits#
OrderedDict[str, Visit], an ordered dictionary of visits. Each key is a visit_id and each value is a visit.
- index_to_visit_id#
Dict[int, str], dictionary that maps the index of a visit in the visits list to the corresponding visit_id.
Examples
>>> from pyhealth.data import Event, Visit, Patient >>> event = Event( ... code="00069153041", ... table="PRESCRIPTIONS", ... vocabulary="NDC", ... visit_id="v001", ... patient_id="p001", ... dosage="250mg", ... ) >>> visit = Visit( ... visit_id="v001", ... patient_id="p001", ... ) >>> visit.add_event(event) >>> patient = Patient( ... patient_id="p001", ... ) >>> patient.add_visit(visit) >>> patient Patient p001 with 1 visits
- add_visit(visit)[source]#
Adds a visit to the patient.
If the visit’s visit_id is already in the patient’s visits dictionary, it will be overwritten by the new visit.
- Parameters:
visit (
Visit
) – visit to add.
Note
- As for now, there is no check on the order of the visits. The new visit
is simply added to the end of the ordered dictionary of visits.
- Return type:
- add_event(event)[source]#
Adds an event to the patient.
If the event’s visit_id is not in the patient’s visits dictionary, this function will raise KeyError.
- Parameters:
event (
Event
) – event to add.
Note
- As for now, there is no check on the order of the events. The new event
is simply appended to the end of the list of events of the corresponding visit.
- Return type: