Skip to content

Private room saver

load_private_rooms_from_hdf5(file_path, chunk_size=50000, domain_super_areas=None)

Loads private rooms from an HDF5 file.

Parameters:

Name Type Description Default
file_path str
required
chunk_size

(Default value = 50000)

50000
domain_super_areas

(Default value = None)

None
Source code in june/hdf5_savers/private_room_saver.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
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
def load_private_rooms_from_hdf5(file_path: str, chunk_size=50000, domain_super_areas=None):
    """Loads private rooms from an HDF5 file.

    Args:
        file_path (str): 
        chunk_size: (Default value = 50000)
        domain_super_areas: (Default value = None)

    """
    private_rooms = []
    logger.info("Loading private rooms...")

    with h5py.File(file_path, "r", libver="latest", swmr=True) as f:
        try:
            private_rooms_dset = f["private_rooms"]
        except KeyError:
            logger.info("No private rooms found in file")
            return PrivateRooms([])

        n_private_rooms = private_rooms_dset.attrs["n_private_rooms"]
        n_chunks = int(np.ceil(n_private_rooms / chunk_size))

        for chunk in range(n_chunks):
            logger.info(f"Loading private rooms chunk {chunk+1} of {n_chunks}")
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_private_rooms)

            ids = read_dataset(private_rooms_dset["id"], idx1, idx2)
            owner_ids = read_dataset(private_rooms_dset["owner_id"], idx1, idx2)
            max_occupancies = read_dataset(private_rooms_dset["max_occupancy"], idx1, idx2)
            area_ids = read_dataset(private_rooms_dset["area_id"], idx1, idx2)
            super_area_ids = read_dataset(private_rooms_dset["super_area_id"], idx1, idx2)

            for k, (
                id_,
                owner_id,
                max_occupancy,
                area_id,
                super_area_id
            ) in enumerate(zip(
                ids,
                owner_ids,
                max_occupancies,
                area_ids,
                super_area_ids
            )):
                # Filter by domain if specified
                if domain_super_areas is not None:
                    if super_area_id == nan_integer:
                        continue  # Skip private rooms without super_area
                    if super_area_id not in domain_super_areas:
                        continue  # Skip private rooms not in this domain

                private_room = PrivateRoom(
                    owner_id=owner_id,
                    max_occupancy=max_occupancy
                )
                private_room.id = id_
                # Store area and super_area IDs for later restoration
                private_room._area_id = area_id if area_id != nan_integer else None
                private_room._super_area_id = super_area_id if super_area_id != nan_integer else None
                private_rooms.append(private_room)

    logger.info(f"Loaded {len(private_rooms)} private rooms")
    return PrivateRooms(private_rooms)

restore_private_rooms_properties_from_hdf5(world, file_path, chunk_size=50000)

Restores private room properties from HDF5. This function restores area and super_area references for private rooms.

Parameters:

Name Type Description Default
world World
required
file_path str
required
chunk_size

(Default value = 50000)

50000
Source code in june/hdf5_savers/private_room_saver.py
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
181
182
183
184
185
186
187
def restore_private_rooms_properties_from_hdf5(
    world: World, file_path: str, chunk_size=50000
):
    """Restores private room properties from HDF5.
    This function restores area and super_area references for private rooms.

    Args:
        world (World): 
        file_path (str): 
        chunk_size: (Default value = 50000)

    """
    logger.info("Restoring private rooms properties...")

    if not hasattr(world, 'private_rooms') or world.private_rooms is None:
        logger.info("No private rooms to restore")
        return

    # Create area and super_area lookup dictionaries
    area_dict = {area.id: area for area in world.areas}
    super_area_dict = {super_area.id: super_area for super_area in world.super_areas}

    for private_room in world.private_rooms:
        # Restore area reference
        if hasattr(private_room, '_area_id') and private_room._area_id is not None:
            private_room.area = area_dict.get(private_room._area_id)
            if private_room.area is None:
                logger.warning(f"Could not find area {private_room._area_id} for private room {private_room.id}")

        # Restore super_area reference  
        if hasattr(private_room, '_super_area_id') and private_room._super_area_id is not None:
            private_room.super_area = super_area_dict.get(private_room._super_area_id)
            if private_room.super_area is None:
                logger.warning(f"Could not find super_area {private_room._super_area_id} for private room {private_room.id}")

    logger.info("Private rooms properties restored successfully.")

save_private_rooms_to_hdf5(private_rooms, file_path, chunk_size=50000)

Saves the private_rooms object to hdf5 format file file_path.

Parameters:

Name Type Description Default
private_rooms PrivateRooms

PrivateRooms object

required
file_path str

path of the saved hdf5 file

required
chunk_size int

number of private rooms to save at a time (Default value = 50000)

50000
Source code in june/hdf5_savers/private_room_saver.py
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
def save_private_rooms_to_hdf5(
    private_rooms: PrivateRooms, file_path: str, chunk_size: int = 50000
):
    """Saves the private_rooms object to hdf5 format file ``file_path``.

    Args:
        private_rooms (PrivateRooms): PrivateRooms object
        file_path (str): path of the saved hdf5 file
        chunk_size (int, optional): number of private rooms to save at a time (Default value = 50000)

    """
    n_private_rooms = len(private_rooms)
    n_chunks = int(np.ceil(n_private_rooms / chunk_size))
    with h5py.File(file_path, "a") as f:
        private_rooms_dset = f.create_group("private_rooms")
        for chunk in range(n_chunks):
            idx1 = chunk * chunk_size
            idx2 = min((chunk + 1) * chunk_size, n_private_rooms)
            ids = []
            owner_ids = []
            max_occupancies = []
            area_ids = []
            super_area_ids = []

            for private_room in private_rooms.members[idx1:idx2]:
                ids.append(private_room.id)
                owner_ids.append(private_room.owner_id)
                max_occupancies.append(private_room.max_occupancy)
                area_ids.append(private_room.area.id if private_room.area else nan_integer)
                super_area_ids.append(private_room.super_area.id if private_room.super_area else nan_integer)

            if chunk == 0:
                private_rooms_dset.attrs["n_private_rooms"] = n_private_rooms
                private_rooms_dset.create_dataset("id", data=np.array(ids, dtype=np.int64), maxshape=(None,), chunks=True)
                private_rooms_dset.create_dataset("owner_id", data=np.array(owner_ids, dtype=np.int64), maxshape=(None,), chunks=True)
                private_rooms_dset.create_dataset("max_occupancy", data=np.array(max_occupancies, dtype=np.int64), maxshape=(None,), chunks=True)
                private_rooms_dset.create_dataset("area_id", data=np.array(area_ids, dtype=np.int64), maxshape=(None,), chunks=True)
                private_rooms_dset.create_dataset("super_area_id", data=np.array(super_area_ids, dtype=np.int64), maxshape=(None,), chunks=True)

            else:
                current_len = private_rooms_dset["id"].shape[0]
                new_len = current_len + len(ids)

                ds = private_rooms_dset["id"]
                ds.resize((new_len,))
                ds[current_len:new_len] = np.array(ids, dtype=np.int64)

                ds = private_rooms_dset["owner_id"]
                ds.resize((new_len,))
                ds[current_len:new_len] = np.array(owner_ids, dtype=np.int64)

                ds = private_rooms_dset["max_occupancy"]
                ds.resize((new_len,))
                ds[current_len:new_len] = np.array(max_occupancies, dtype=np.int64)

                ds = private_rooms_dset["area_id"]
                ds.resize((new_len,))
                ds[current_len:new_len] = np.array(area_ids, dtype=np.int64)

                ds = private_rooms_dset["super_area_id"]
                ds.resize((new_len,))
                ds[current_len:new_len] = np.array(super_area_ids, dtype=np.int64)

    logger.info("Private rooms saved successfully.")