/
file_io.py
163 lines (128 loc) · 6.18 KB
/
file_io.py
1
2
3
4
5
6
7
8
9
10
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
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright (c) 2020-2023 by Fraunhofer Institute for Energy Economics
# and Energy System Technology (IEE), Kassel, and University of Kassel. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
import json
import os
import pickle
from functools import partial
from pandapower.io_utils import PPJSONEncoder, to_dict_with_coord_transform, \
get_raw_data_from_pickle, transform_net_with_df_and_geo, PPJSONDecoder
from pandapower.io_utils import pp_hook, encrypt_string, decrypt_string
from pandapipes.io.convert_format import convert_format
from pandapipes.io.io_utils import isinstance_partial, FromSerializableRegistryPpipe
from pandapipes.pandapipes_net import pandapipesNet
from pandapipes.multinet import MultiNet
from pandapower import pandapowerNet
from pandapower.convert_format import convert_format as convert_format_pandapower
def to_pickle(net, filename):
"""
Saves a pandapipes Network with the pickle library.
:param net: The pandapipes Network to save.
:type net: pandapipesNet
:param filename: The absolute or relative path to the output file or a writable file-like object
:type filename: str, file-object
:return: No output.
:Example:
>>> pandapipes.to_pickle(net, os.path.join("C:", "example_folder", "example1.p")) # absolute path
>>> pandapipes.to_pickle(net, "example2.p") # relative path
"""
if hasattr(filename, 'write'):
pickle.dump(dict(net), filename, protocol=2)
return
if not filename.endswith(".p"):
raise Exception("Please use .p to save pandapipes networks!")
save_net = to_dict_with_coord_transform(net, ["junction_geodata"], ["pipe_geodata"])
with open(filename, "wb") as f:
pickle.dump(save_net, f, protocol=2) # use protocol 2 for py2 / py3 compatibility
def to_json(net, filename=None, encryption_key=None):
"""
Saves a pandapipes Network in JSON format. The index columns of all pandas DataFrames will be
saved in ascending order. net elements which name begins with "_" (internal elements) will not
be saved. Std types will also not be saved.
:param net: The pandapipes Network to save.
:type net: pandapipesNet
:param filename: The absolute or relative path to the output file or a writable file-like \
object. If None, a JSON string is returned.
:type filename: str, file-object, default None
:param encryption_key: If given, the pandapipes network is stored as an encrypted json string
:type encryption_key: str, default None
:return: JSON string of the Network (only if filename is None)
:Example:
>>> pandapipes.to_json(net, "example.json")
"""
json_string = json.dumps(net, cls=PPJSONEncoder, indent=2, isinstance_func=isinstance_partial)
if encryption_key is not None:
json_string = encrypt_string(json_string, encryption_key)
if filename is None:
return json_string
if hasattr(filename, 'write'):
filename.write(json_string)
else:
with open(filename, "w") as fp:
fp.write(json_string)
def from_pickle(filename):
"""
Load a pandapipes format Network from pickle file.
:param filename: The absolute or relative path to the input file or file-like object
:type filename: str, file-object
:return: net - The pandapipes Network which was saved as pickle
:rtype: pandapipesNet
:Example:
>>> net1 = pandapipes.from_pickle(os.path.join("C:", "example_folder", "example1.p"))
>>> net2 = pandapipes.from_pickle("example2.p") #relative path
"""
net = pandapipesNet(get_raw_data_from_pickle(filename))
transform_net_with_df_and_geo(net, ["junction_geodata"], ["pipe_geodata"])
return net
def from_json(filename, convert=True, encryption_key=None):
"""
Load a pandapipes network from a JSON file or string.
The index of the returned network is not necessarily in the same order as the original network.
Index columns of all pandas DataFrames are sorted in ascending order.
:param filename: The absolute or relative path to the input file or file-like object
:type filename: str, file-object
:param convert: whether or not to convert the format from earlier versions
:type convert: bool
:param encryption_key: if given, key to decrypt an encrypted pandapower network
:type encryption_key: str
:return: net - The pandapipes network that was saved as JSON
:rtype: pandapipesNet
:Example:
>>> net = pandapipes.from_json("example.json")
"""
if hasattr(filename, 'read'):
json_string = filename.read()
elif not os.path.isfile(filename):
raise UserWarning("File {} does not exist!!".format(filename))
else:
with open(filename) as fp:
json_string = fp.read()
return from_json_string(json_string, convert=convert, encryption_key=encryption_key)
def from_json_string(json_string, convert=False, encryption_key=None):
"""
Load a pandapipes network from a JSON string.
The index of the returned network is not necessarily in the same order as the original network.
Index columns of all pandas DataFrames are sorted in ascending order.
:param json_string: The JSON string representation of the network
:type json_string: str
:param convert: whether to convert the format from earlier versions
:type convert: bool
:param encryption_key: if given, key to decrypt an encrypted pandapower network
:type encryption_key: str
:return: net - The pandapipes network that was contained in the JSON string
:rtype: pandapipesNet
:Example:
>>> net = pandapipes.from_json_string(json_str)
"""
if encryption_key is not None:
json_string = decrypt_string(json_string, encryption_key)
net = json.loads(json_string, cls=PPJSONDecoder, registry_class=FromSerializableRegistryPpipe)
if convert and isinstance(net, pandapipesNet):
convert_format(net)
elif convert and isinstance(net, MultiNet):
for n in net['nets']:
if isinstance(n, pandapipesNet):
convert_format(net)
elif isinstance(n, pandapowerNet):
convert_format_pandapower(net)
return net