e2nIEE / pandapower
1
# -*- coding: utf-8 -*-
2

3
# Copyright (c) 2016-2021 by University of Kassel and Fraunhofer Institute for Energy Economics
4
# and Energy System Technology (IEE), Kassel. All rights reserved.
5

6

7 1
import json
8 1
import os
9 1
import pickle
10 1
from warnings import warn
11

12 1
import numpy
13 1
import pandas as pd
14 1
from packaging import version
15

16 1
import pandapower.io_utils as io_utils
17 1
from pandapower.auxiliary import pandapowerNet
18 1
from pandapower.convert_format import convert_format
19 1
from pandapower.create import create_empty_network
20

21

22 1
def to_pickle(net, filename):
23
    """
24
    Saves a pandapower Network with the pickle library.
25

26
    INPUT:
27
        **net** (dict) - The pandapower format network
28

29
        **filename** (string) - The absolute or relative path to the output file or an writable file-like objectxs
30

31
    EXAMPLE:
32

33
        >>> pp.to_pickle(net, os.path.join("C:", "example_folder", "example1.p"))  # absolute path
34
        >>> pp.to_pickle(net, "example2.p")  # relative path
35

36
    """
37 1
    if hasattr(filename, 'write'):
38 0
        pickle.dump(dict(net), filename, protocol=2)
39 0
        return
40 1
    if not filename.endswith(".p"):
41 0
        raise Exception("Please use .p to save pandapower networks!")
42 1
    save_net = io_utils.to_dict_with_coord_transform(net, ["bus_geodata"], ["line_geodata"])
43

44 1
    with open(filename, "wb") as f:
45 1
        pickle.dump(save_net, f, protocol=2)  # use protocol 2 for py2 / py3 compatibility
46

47

48 1
def to_excel(net, filename, include_empty_tables=False, include_results=True):
49
    """
50
    Saves a pandapower Network to an excel file.
51

52
    INPUT:
53
        **net** (dict) - The pandapower format network
54

55
        **filename** (string) - The absolute or relative path to the output file
56

57
    OPTIONAL:
58
        **include_empty_tables** (bool, False) - empty element tables are saved as excel sheet
59

60
        **include_results** (bool, True) - results are included in the excel sheet
61

62
    EXAMPLE:
63

64
        >>> pp.to_excel(net, os.path.join("C:", "example_folder", "example1.xlsx"))  # absolute path
65
        >>> pp.to_excel(net, "example2.xlsx")  # relative path
66

67
    """
68 1
    writer = pd.ExcelWriter(filename, engine='xlsxwriter')
69 1
    dict_net = io_utils.to_dict_of_dfs(net, include_results=include_results,
70
                                       include_empty_tables=include_empty_tables)
71 1
    for item, table in dict_net.items():
72 1
        table.to_excel(writer, sheet_name=item)
73 1
    writer.save()
74

75

76 1
def to_json(net, filename=None, encryption_key=None):
77
    """
78
        Saves a pandapower Network in JSON format. The index columns of all pandas DataFrames will
79
        be saved in ascending order. net elements which name begins with "_" (internal elements)
80
        will not be saved. Std types will also not be saved.
81

82
        INPUT:
83
            **net** (dict) - The pandapower format network
84

85
            **filename** (string or file, None) - The absolute or relative path to the output file
86
                                                  or a file-like object,
87
                                                  if 'None' the function returns a json string
88

89
            **encrytion_key** (string, None) - If given, the pandapower network is stored as an
90
                                               encrypted json string
91

92

93
        EXAMPLE:
94

95
             >>> pp.to_json(net, "example.json")
96

97
    """
98 1
    json_string = json.dumps(net, cls=io_utils.PPJSONEncoder, indent=2)
99 1
    if encryption_key is not None:
100 1
        json_string = io_utils.encrypt_string(json_string, encryption_key)
101

102 1
    if filename is None:
103 1
        return json_string
104

105 1
    if hasattr(filename, 'write'):
106 0
        filename.write(json_string)
107
    else:
108 1
        with open(filename, "w") as fp:
109 1
            fp.write(json_string)
110

111

112 1
def to_sql(net, con, include_results=True):
113 1
    dodfs = io_utils.to_dict_of_dfs(net, include_results=include_results)
114 1
    for name, data in dodfs.items():
115 1
        data.to_sql(name, con, if_exists="replace")
116

117

118 1
def to_sqlite(net, filename, include_results=True):
119 1
    import sqlite3
120 1
    conn = sqlite3.connect(filename)
121 1
    to_sql(net, conn, include_results)
122 1
    conn.close()
123

124

125 1
def from_pickle(filename, convert=True):
126
    """
127
    Load a pandapower format Network from pickle file
128

129
    INPUT:
130
        **filename** (string or file) - The absolute or relative path to the input file or file-like object
131

132
        **convert** (bool, True) - If True, converts the format of the net loaded from pickle from the older
133
            version of pandapower to the newer version format
134

135
    OUTPUT:
136
        **net** (dict) - The pandapower format network
137

138
    EXAMPLE:
139

140
        >>> net1 = pp.from_pickle(os.path.join("C:", "example_folder", "example1.p")) #absolute path
141
        >>> net2 = pp.from_pickle("example2.p") #relative path
142

143
    """
144

145 1
    net = pandapowerNet(io_utils.get_raw_data_from_pickle(filename))
146 1
    io_utils.transform_net_with_df_and_geo(net, ["bus_geodata"], ["line_geodata"])
147

148 1
    if convert:
149 1
        convert_format(net)
150 1
    return net
151

152

153 1
def from_excel(filename, convert=True):
154
    """
155
    Load a pandapower network from an excel file
156

157
    INPUT:
158
        **filename** (string) - The absolute or relative path to the input file.
159

160
        **convert** (bool, True) - If True, converts the format of the net loaded from excel from
161
            the older version of pandapower to the newer version format
162

163
    OUTPUT:
164
        **net** (dict) - The pandapower format network
165

166
    EXAMPLE:
167

168
        >>> net1 = pp.from_excel(os.path.join("C:", "example_folder", "example1.xlsx")) #absolute path
169
        >>> net2 = pp.from_excel("example2.xlsx") #relative path
170

171
    """
172

173 1
    if not os.path.isfile(filename):
174 0
        raise UserWarning("File %s does not exist!" % filename)
175 1
    pd_version = version.parse(pd.__version__)
176 1
    if pd_version < version.parse("0.21"):
177 0
        xls = pd.ExcelFile(filename).parse(sheetname=None)
178 1
    elif pd_version < version.parse("0.24"):
179 0
        xls = pd.ExcelFile(filename).parse(sheet_name=None)
180
    else:
181 1
        xls = pd.read_excel(filename, sheet_name=None, index_col=0, engine="openpyxl")
182
        
183 1
    try:
184 1
        net = io_utils.from_dict_of_dfs(xls)
185 0
    except:
186 0
        net = _from_excel_old(xls)
187 1
    if convert:
188 1
        convert_format(net)
189 1
    return net
190

191

192 1
def _from_excel_old(xls):
193 0
    par = xls["parameters"]["parameter"]
194 0
    name = None if pd.isnull(par.at["name"]) else par.at["name"]
195 0
    net = create_empty_network(name=name, f_hz=par.at["f_hz"])
196 0
    net.update(par)
197 0
    for item, table in xls.items():
198 0
        if item == "parameters":
199 0
            continue
200 0
        elif item.endswith("std_types"):
201 0
            item = item.split("_")[0]
202 0
            for std_type, tab in table.iterrows():
203 0
                net.std_types[item][std_type] = dict(tab)
204 0
        elif item == "line_geodata":
205 0
            points = int(len(table.columns) / 2)
206 0
            for i, coords in table.iterrows():
207 0
                coord = [(coords["x%u" % nr], coords["y%u" % nr]) for nr in range(points)
208
                         if pd.notnull(coords["x%u" % nr])]
209 0
                net.line_geodata.loc[i, "coords"] = coord
210
        else:
211 0
            net[item] = table
212 0
    return net
213

214

215 1
def from_json(filename, convert=True, encryption_key=None):
216
    """
217
    Load a pandapower network from a JSON file.
218
    The index of the returned network is not necessarily in the same order as the original network.
219
    Index columns of all pandas DataFrames are sorted in ascending order.
220

221
    INPUT:
222
        **filename** (string or file) - The absolute or relative path to the input file or file-like object
223

224
        **convert** (bool, True) - If True, converts the format of the net loaded from json from the older
225
            version of pandapower to the newer version format
226

227
        **encrytion_key** (string, "") - If given, key to decrypt an encrypted pandapower network
228

229
    OUTPUT:
230
        **net** (dict) - The pandapower format network
231

232
    EXAMPLE:
233

234
        >>> net = pp.from_json("example.json")
235

236
    """
237 1
    if hasattr(filename, 'read'):
238 0
        json_string = filename.read()
239 1
    elif not os.path.isfile(filename):
240 0
        raise UserWarning("File {} does not exist!!".format(filename))
241
    else:
242 1
        with open(filename, "r") as fp:
243 1
            json_string = fp.read()
244

245 1
    return from_json_string(json_string, convert=convert, encryption_key=encryption_key)
246

247

248 1
def from_json_string(json_string, convert=False, encryption_key=None):
249
    """
250
    Load a pandapower network from a JSON string.
251
    The index of the returned network is not necessarily in the same order as the original network.
252
    Index columns of all pandas DataFrames are sorted in ascending order.
253

254
    INPUT:
255
        **json_string** (string) - The json string representation of the network
256

257
        **convert** (bool, False) - If True, converts the format of the net loaded from json_string from the
258
            older version of pandapower to the newer version format
259

260
        **encrytion_key** (string, "") - If given, key to decrypt an encrypted json_string
261

262
    OUTPUT:
263
        **net** (dict) - The pandapower format network
264

265
    EXAMPLE:
266

267
        >>> net = pp.from_json_string(json_str)
268

269
    """
270 1
    if encryption_key is not None:
271 1
        json_string = io_utils.decrypt_string(json_string, encryption_key)
272

273 1
    net = json.loads(json_string, cls=io_utils.PPJSONDecoder)
274
    # this can be removed in the future
275
    # now net is saved with "_module", "_class", "_object"..., so json.load already returns
276
    # pandapowerNet. Older files don't have it yet, and are loaded as dict.
277
    # After some time, this part can be removed.
278 1
    if not isinstance(net, pandapowerNet):
279 1
        warn("This net is saved in older format, which will not be supported in future.\r\n"
280
             "Please resave your grid using the current pandapower version.",
281
             DeprecationWarning)
282 1
        net = from_json_dict(net)
283

284 1
    if convert:
285 1
        convert_format(net)
286 1
    return net
287

288

289 1
def from_json_dict(json_dict):
290
    """
291
    Load a pandapower network from a JSON string.
292
    The index of the returned network is not necessarily in the same order as the original network.
293
    Index columns of all pandas DataFrames are sorted in ascending order.
294

295
    INPUT:
296
        **json_dict** (json) - The json object representation of the network
297

298
    OUTPUT:
299
        **net** (dict) - The pandapower format network
300

301
    EXAMPLE:
302

303
        >>> net = pp.pp.from_json_dict(json.loads(json_str))
304

305
    """
306 1
    name = json_dict["name"] if "name" in json_dict else None
307 1
    f_hz = json_dict["f_hz"] if "f_hz" in json_dict else 50
308 1
    net = create_empty_network(name=name, f_hz=f_hz)
309 1
    if "parameters" in json_dict:
310 1
        for par, value in json_dict["parameters"]["parameter"].items():
311 1
            net[par] = value
312

313 1
    for key in sorted(json_dict.keys()):
314 1
        if key == 'dtypes':
315 1
            continue
316 1
        if key in net and isinstance(net[key], pd.DataFrame) and isinstance(json_dict[key], dict) \
317
                or key == "piecewise_linear_cost" or key == "polynomial_cost":
318 1
            net[key] = pd.DataFrame.from_dict(json_dict[key], orient="columns")
319 1
            net[key].set_index(net[key].index.astype(numpy.int64), inplace=True)
320
        else:
321 1
            net[key] = json_dict[key]
322 1
    return net
323

324

325 1
def from_sql(con):
326 1
    cursor = con.cursor()
327 1
    cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
328 1
    dodfs = dict()
329 1
    for t, in cursor.fetchall():
330 1
        table = pd.read_sql_query("SELECT * FROM %s" % t, con, index_col="index")
331 1
        table.index.name = None
332 1
        dodfs[t] = table
333 1
    net = io_utils.from_dict_of_dfs(dodfs)
334 1
    return net
335

336

337 1
def from_sqlite(filename, netname=""):
338 1
    import sqlite3
339 1
    con = sqlite3.connect(filename)
340 1
    net = from_sql(con)
341 1
    con.close()
342 1
    return net

Read our documentation on viewing source code .

Loading