Skip to content

Hospital saver

load_hospitals_from_hdf5(file_path, chunk_size=50000, domain_super_areas=None, super_areas_to_domain_dict=None, config_filename=None)

Loads hospitals from an hdf5 file located at file_path. Note that this object will not be ready to use, as the links to object instances of other classes need to be restored first. This function should rarely be called outside world.py.

Parameters:

Name Type Description Default
file_path str
required
chunk_size

(Default value = 50000)

50000
domain_super_areas

(Default value = None)

None
super_areas_to_domain_dict dict

(Default value = None)

None
config_filename

(Default value = None)

None
Source code in june/hdf5_savers/hospital_saver.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def load_hospitals_from_hdf5(
    file_path: str,
    chunk_size=50000,
    domain_super_areas=None,
    super_areas_to_domain_dict: dict = None,
    config_filename=None,
):
    """Loads hospitals from an hdf5 file located at ``file_path``.
    Note that this object will not be ready to use, as the links to
    object instances of other classes need to be restored first.
    This function should rarely be called outside world.py.

    Args:
        file_path (str): 
        chunk_size: (Default value = 50000)
        domain_super_areas: (Default value = None)
        super_areas_to_domain_dict (dict, optional): (Default value = None)
        config_filename: (Default value = None)

    """
    Hospital_Class = Hospital
    disease_config = GlobalContext.get_disease_config()
    Hospital_Class.subgroup_params = SubgroupParams.from_disease_config(disease_config)
    ExternalHospital_Class = ExternalHospital

    with h5py.File(file_path, "r", libver="latest", swmr=True) as f:
        hospitals = f["hospitals"]
        hospitals_list = []
        n_hospitals = hospitals.attrs["n_hospitals"]
        n_chunks = int(np.ceil(n_hospitals / chunk_size))

        for chunk in range(n_chunks):
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_hospitals)
            ids = read_dataset(hospitals["id"], idx1, idx2)
            n_beds_list = read_dataset(hospitals["n_beds"], idx1, idx2)
            n_icu_beds_list = read_dataset(hospitals["n_icu_beds"], idx1, idx2)
            trust_codes = read_dataset(hospitals["trust_code"], idx1, idx2)
            coordinates = read_dataset(hospitals["coordinates"], idx1, idx2)
            super_areas = read_dataset(hospitals["super_area"], idx1, idx2)
            region_name = read_dataset(hospitals["region_name"], idx1, idx2)

            for k in range(idx2 - idx1):
                super_area = super_areas[k]
                if super_area == nan_integer:
                    raise ValueError(
                        "If ``domain_super_areas`` is True, I expect non-None super areas."
                    )

                trust_code = trust_codes[k]
                trust_code = trust_code.decode() if trust_code.decode() != " " else None

                if (
                    domain_super_areas is not None
                    and super_area not in domain_super_areas
                ):
                    hospital = ExternalHospital_Class(
                        id=ids[k],
                        spec="hospital",
                        domain_id=super_areas_to_domain_dict[super_area],
                        region_name=region_name[k].decode(),
                    )
                else:
                    hospital = Hospital_Class(
                        n_beds=n_beds_list[k],
                        n_icu_beds=n_icu_beds_list[k],
                        coordinates=coordinates[k],
                        trust_code=trust_code
                    )
                    hospital.id = ids[k]
                hospitals_list.append(hospital)

    return Hospitals(hospitals_list, ball_tree=False)

restore_hospital_properties_from_hdf5(world, file_path, chunk_size=50000, domain_super_areas=None, domain_areas=None, super_areas_to_domain_dict=None)

Parameters:

Name Type Description Default
world World
required
file_path str
required
chunk_size

(Default value = 50000)

50000
domain_super_areas

(Default value = None)

None
domain_areas

(Default value = None)

None
super_areas_to_domain_dict dict

(Default value = None)

None
Source code in june/hdf5_savers/hospital_saver.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def restore_hospital_properties_from_hdf5(
    world: World,
    file_path: str,
    chunk_size=50000,
    domain_super_areas=None,
    domain_areas=None,
    super_areas_to_domain_dict: dict = None,
):
    """

    Args:
        world (World): 
        file_path (str): 
        chunk_size: (Default value = 50000)
        domain_super_areas: (Default value = None)
        domain_areas: (Default value = None)
        super_areas_to_domain_dict (dict, optional): (Default value = None)

    """
    with h5py.File(file_path, "r", libver="latest", swmr=True) as f:
        hospitals = f["hospitals"]
        n_hospitals = hospitals.attrs["n_hospitals"]
        n_chunks = int(np.ceil(n_hospitals / chunk_size))
        for chunk in range(n_chunks):
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_hospitals)
            length = idx2 - idx1
            ids = np.empty(length, dtype=int)
            hospitals["id"].read_direct(ids, np.s_[idx1:idx2], np.s_[0:length])
            areas = np.empty(length, dtype=int)
            hospitals["area"].read_direct(areas, np.s_[idx1:idx2], np.s_[0:length])
            for k in range(length):
                if domain_areas is not None:
                    area = areas[k]
                    if area == nan_integer:
                        raise ValueError(
                            "if ``domain_areas`` is True, I expect not Nones areas."
                        )
                    if area not in domain_areas:
                        continue
                hospital = world.hospitals.get_from_id(ids[k])
                area = areas[k]
                if area == nan_integer:
                    area = None
                else:
                    area = world.areas.get_from_id(area)
                hospital.area = area

        # super areas
        geography = f["geography"]
        n_super_areas = geography.attrs["n_super_areas"]
        n_chunks = int(np.ceil(n_super_areas / chunk_size))
        for chunk in range(n_chunks):
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_super_areas)
            length = idx2 - idx1
            super_areas_ids = read_dataset(geography["super_area_id"], idx1, idx2)
            closest_hospitals_ids = read_dataset(
                geography["closest_hospitals_ids"], idx1, idx2
            )
            closest_hospitals_super_areas = read_dataset(
                geography["closest_hospitals_super_areas"], idx1, idx2
            )
            for k in range(length):
                if domain_super_areas is not None:
                    super_area_id = super_areas_ids[k]
                    if super_area_id == nan_integer:
                        raise ValueError(
                            "if ``domain_super_areas`` is True, I expect not Nones super_areas."
                        )
                    if super_area_id not in domain_super_areas:
                        continue
                super_area = world.super_areas.get_from_id(super_areas_ids[k])
                # load closest hospitals
                hospitals = []
                for hospital_id, hospital_super_area_id in zip(
                    closest_hospitals_ids[k], closest_hospitals_super_areas[k]
                ):
                    hospital = world.hospitals.get_from_id(hospital_id)
                    hospitals.append(hospital)
                super_area.closest_hospitals = hospitals

save_hospitals_to_hdf5(hospitals, file_path, chunk_size=50000)

Saves the Hospitals object to hdf5 format file file_path. Currently for each person, the following values are stored: - id, n_beds, n_icu_beds, area, coordinates

Parameters:

Name Type Description Default
hospitals Hospitals
required
file_path str

path of the saved hdf5 file

required
chunk_size int

number of people to save at a time. Note that they have to be copied to be saved,

50000

so keep the number below 1e6. (Default value = 50000)

Source code in june/hdf5_savers/hospital_saver.py
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def save_hospitals_to_hdf5(
    hospitals: Hospitals, file_path: str, chunk_size: int = 50000
):
    """Saves the Hospitals object to hdf5 format file ``file_path``. Currently for each person,
    the following values are stored:
    - id, n_beds, n_icu_beds, area, coordinates

    Args:
        hospitals (Hospitals): 
        file_path (str): path of the saved hdf5 file
        chunk_size (int, optional): number of people to save at a time. Note that they have to be copied to be saved,
    so keep the number below 1e6. (Default value = 50000)

    """
    n_hospitals = len(hospitals)
    n_chunks = int(np.ceil(n_hospitals / chunk_size))
    with h5py.File(file_path, "a") as f:
        hospitals_dset = f.create_group("hospitals")
        for chunk in range(n_chunks):
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_hospitals)
            ids = []
            n_beds = []
            n_icu_beds = []
            areas = []
            super_areas = []
            region_names = []
            coordinates = []
            trust_code = []
            for hospital in hospitals[idx1:idx2]:
                ids.append(hospital.id)
                if hospital.area is None:
                    areas.append(nan_integer)
                    super_areas.append(nan_integer)
                    region_names.append(nan_integer)
                else:
                    areas.append(hospital.area.id)
                    super_areas.append(hospital.super_area.id)
                    region_names.append(hospital.region_name)
                n_beds.append(hospital.n_beds)
                n_icu_beds.append(hospital.n_icu_beds)
                coordinates.append(np.array(hospital.coordinates))
                trust_code.append(hospital.trust_code)

            ids = np.array(ids, dtype=np.int64)
            areas = np.array(areas, dtype=np.int64)
            super_areas = np.array(super_areas, dtype=np.int64)
            region_names = np.array(region_names, dtype="S50")
            trust_code = np.array(trust_code, dtype="S10")
            n_beds = np.array(n_beds, dtype=np.int64)
            n_icu_beds = np.array(n_icu_beds, dtype=np.int64)
            coordinates = np.array(coordinates, dtype=np.float64)
            if chunk == 0:
                hospitals_dset.attrs["n_hospitals"] = n_hospitals
                hospitals_dset.create_dataset("id", data=ids, maxshape=(None,))
                hospitals_dset.create_dataset("area", data=areas, maxshape=(None,))
                hospitals_dset.create_dataset(
                    "super_area", data=super_areas, maxshape=(None,)
                )
                hospitals_dset.create_dataset(
                    "region_name", data=region_names, maxshape=(None,)
                )
                hospitals_dset.create_dataset(
                    "trust_code", data=trust_code, maxshape=(None,)
                )
                hospitals_dset.create_dataset("n_beds", data=n_beds, maxshape=(None,))
                hospitals_dset.create_dataset(
                    "n_icu_beds", data=n_icu_beds, maxshape=(None,)
                )
                hospitals_dset.create_dataset(
                    "coordinates",
                    data=coordinates,
                    maxshape=(None, coordinates.shape[1]),
                )
            else:
                newshape = (hospitals_dset["id"].shape[0] + ids.shape[0],)
                hospitals_dset["id"].resize(newshape)
                hospitals_dset["id"][idx1:idx2] = ids
                hospitals_dset["area"].resize(newshape)
                hospitals_dset["area"][idx1:idx2] = areas
                hospitals_dset["super_area"].resize(newshape)
                hospitals_dset["super_area"][idx1:idx2] = super_areas
                hospitals_dset["region_name"].resize(newshape)
                hospitals_dset["region_name"][idx1:idx2] = region_names
                hospitals_dset["trust_code"].resize(newshape)
                hospitals_dset["trust_code"][idx1:idx2] = trust_code
                hospitals_dset["n_beds"].resize(newshape)
                hospitals_dset["n_beds"][idx1:idx2] = n_beds
                hospitals_dset["n_icu_beds"].resize(newshape)
                hospitals_dset["n_icu_beds"][idx1:idx2] = n_icu_beds
                hospitals_dset["coordinates"].resize(newshape[0], axis=0)
                hospitals_dset["coordinates"][idx1:idx2] = coordinates