Skip to content

Save

This module provides functions for saving network data to files.

save_edge_params

Saves edge parameters for the network to a CSV file.

Extracts and saves branch parameters including admittance matrices and rate limits.

Parameters:

Name Type Description Default
net pandapowerNet

The power network.

required
path str

Path where the edge parameters CSV file should be saved.

required
Source code in gridfm_datakit/save.py
11
12
13
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
def save_edge_params(net: pandapowerNet, path: str):
    """Saves edge parameters for the network to a CSV file.

    Extracts and saves branch parameters including admittance matrices and rate limits.

    Args:
        net: The power network.
        path: Path where the edge parameters CSV file should be saved.
    """
    pp.rundcpp(net)  # need to run dcpp to create the ppc structure
    ppc = net._ppc
    to_bus = np.real(ppc["branch"][:, T_BUS])
    from_bus = np.real(ppc["branch"][:, F_BUS])
    Ytt, Yff, Yft, Ytf = branch_vectors(ppc["branch"], ppc["branch"].shape[0])
    Ytt_r = np.real(Ytt)
    Ytt_i = np.imag(Ytt)
    Yff_r = np.real(Yff)
    Yff_i = np.imag(Yff)
    Yft_r = np.real(Yft)
    Yft_i = np.imag(Yft)
    Ytf_r = np.real(Ytf)
    Ytf_i = np.imag(Ytf)

    rate_a = np.real(ppc["branch"][:, RATE_A])
    edge_params = pd.DataFrame(
        np.column_stack(
            (
                from_bus,
                to_bus,
                Yff_r,
                Yff_i,
                Yft_r,
                Yft_i,
                Ytf_r,
                Ytf_i,
                Ytt_r,
                Ytt_i,
                rate_a,
            ),
        ),
        columns=[
            "from_bus",
            "to_bus",
            "Yff_r",
            "Yff_i",
            "Yft_r",
            "Yft_i",
            "Ytf_r",
            "Ytf_i",
            "Ytt_r",
            "Ytt_i",
            "rate_a",
        ],
    )
    # comvert everything to float32
    edge_params = edge_params.astype(np.float32)
    edge_params.to_csv(path, index=False)

save_bus_params

Saves bus parameters for the network to a CSV file.

Extracts and saves bus parameters including voltage limits and base values.

Parameters:

Name Type Description Default
net pandapowerNet

The power network.

required
path str

Path where the bus parameters CSV file should be saved.

required
Source code in gridfm_datakit/save.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def save_bus_params(net: pandapowerNet, path: str):
    """Saves bus parameters for the network to a CSV file.

    Extracts and saves bus parameters including voltage limits and base values.

    Args:
        net: The power network.
        path: Path where the bus parameters CSV file should be saved.
    """
    idx = net.bus.index
    base_kv = net.bus.vn_kv
    bus_type = net.bus.type
    vmin = net.bus.min_vm_pu
    vmax = net.bus.max_vm_pu

    bus_params = pd.DataFrame(
        np.column_stack((idx, bus_type, vmin, vmax, base_kv)),
        columns=["bus", "type", "vmin", "vmax", "baseKV"],
    )
    bus_params.to_csv(path, index=False)

save_branch_idx_removed

Saves indices of removed branches for each scenario.

Appends the removed branch indices to an existing CSV file or creates a new one.

Parameters:

Name Type Description Default
branch_idx_removed List[List[int]]

List of removed branch indices for each scenario.

required
path str

Path where the branch indices CSV file should be saved.

required
Source code in gridfm_datakit/save.py
 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
def save_branch_idx_removed(branch_idx_removed: List[List[int]], path: str):
    """Saves indices of removed branches for each scenario.

    Appends the removed branch indices to an existing CSV file or creates a new one.

    Args:
        branch_idx_removed: List of removed branch indices for each scenario.
        path: Path where the branch indices CSV file should be saved.
    """
    if os.path.exists(path):
        existing_df = pd.read_csv(path, usecols=["scenario"])
        if not existing_df.empty:
            last_scenario = existing_df["scenario"].iloc[-1]
    else:
        last_scenario = -1

    scenario_idx = np.arange(
        last_scenario + 1,
        last_scenario + 1 + len(branch_idx_removed),
    )
    branch_idx_removed_df = pd.DataFrame(branch_idx_removed)
    branch_idx_removed_df.insert(0, "scenario", scenario_idx)
    branch_idx_removed_df.to_csv(
        path,
        mode="a",
        header=not os.path.exists(path),
        index=False,
    )  # append to existing file or create new one

save_node_edge_data

Saves generated node and edge data to CSV files.

Saves generated data for nodes and edges, appending to existing files if they exist.

Parameters:

Name Type Description Default
net pandapowerNet

The power network.

required
node_path str

Path where node data should be saved.

required
edge_path str

Path where edge data should be saved.

required
csv_data list

List of node-level data for each scenario.

required
adjacency_lists list

List of edge-level adjacency lists for each scenario.

required
mode str

Analysis mode, either 'pf' for power flow or 'contingency' for contingency analysis.

'pf'
Source code in gridfm_datakit/save.py
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
181
182
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
def save_node_edge_data(
    net: pandapowerNet,
    node_path: str,
    edge_path: str,
    csv_data: list,
    adjacency_lists: list,
    mode: str = "pf",
):
    """Saves generated node and edge data to CSV files.

    Saves generated data for nodes and edges,
    appending to existing files if they exist.

    Args:
        net: The power network.
        node_path: Path where node data should be saved.
        edge_path: Path where edge data should be saved.
        csv_data: List of node-level data for each scenario.
        adjacency_lists: List of edge-level adjacency lists for each scenario.
        mode: Analysis mode, either 'pf' for power flow or 'contingency' for contingency analysis.
    """
    n_buses = net.bus.shape[0]

    # Determine last scenario index
    last_scenario = -1
    if os.path.exists(node_path):
        existing_df = pd.read_csv(node_path, usecols=["scenario"])
        if not existing_df.empty:
            last_scenario = existing_df["scenario"].iloc[-1]

    # Create DataFrame for node data
    if mode == "pf":
        df = pd.DataFrame(
            csv_data,
            columns=[
                "bus",
                "Pd",
                "Qd",
                "Pg",
                "Qg",
                "Vm",
                "Va",
                "PQ",
                "PV",
                "REF",
            ],
        )
    elif (
        mode == "contingency"
    ):  # we add the dc voltage to the node data for benchmarking purposes
        df = pd.DataFrame(
            csv_data,
            columns=[
                "bus",
                "Pd",
                "Qd",
                "Pg",
                "Qg",
                "Vm",
                "Va",
                "PQ",
                "PV",
                "REF",
                "Vm_dc",
                "Va_dc",
            ],
        )

    df["bus"] = df["bus"].astype("int64")

    # Shift scenario indices
    scenario_indices = np.repeat(
        range(last_scenario + 1, last_scenario + 1 + (df.shape[0] // n_buses)),
        n_buses,
    )  # repeat each scenario index n_buses times since there are n_buses rows for each scenario
    df.insert(0, "scenario", scenario_indices)

    # Append to CSV
    df.to_csv(node_path, mode="a", header=not os.path.exists(node_path), index=False)

    # Create DataFrame for edge data
    adj_df = pd.DataFrame(
        np.concatenate(adjacency_lists),
        columns=["index1", "index2", "G", "B"],
    )

    adj_df[["index1", "index2"]] = adj_df[["index1", "index2"]].astype("int64")

    # Shift scenario indices
    scenario_indices = np.concatenate(
        [
            np.full(adjacency_lists[i].shape[0], last_scenario + 1 + i, dtype="int64")
            for i in range(len(adjacency_lists))
        ],
    )  # for each scenario, we repeat the scenario index as many times as there are edges in the scenario
    adj_df.insert(0, "scenario", scenario_indices)

    # Append to CSV
    adj_df.to_csv(
        edge_path,
        mode="a",
        header=not os.path.exists(edge_path),
        index=False,
    )