-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
157 lines (126 loc) · 4.14 KB
/
util.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
import pandas as pd
from pathlib import Path
import json
import cloudclient.datamodel.defaults
from cloudclient.datamodel.toplevel import Payload
ASSET_CATEGORIES = ["CONVERSION", "CONSUMPTION", "PRODUCTION", "STORAGE"]
def sort_columns(df: pd.DataFrame) -> pd.DataFrame:
to_front = ["category", "type", "dataclassname"]
return df.reindex(
[*to_front, *[col for col in df.columns if col not in to_front]], axis=1
)
def generate_excel(
payload: Payload,
file_id: str = None,
path: Path = None,
) -> None:
"""Generates excel sheets for the given payload (model configuration).
Args:
payload (Payload): Model configuration object.
file_id (str, optional): File ID to prepend to the generated excel sheet file names. Defaults to None.
path (Path, optional): Folder to export the excel sheets to. Defaults to None.
"""
if file_id is None:
file_id = ""
else:
file_id = f"{file_id}_"
if path is None:
FOLDER = Path("").resolve().absolute()
else:
FOLDER = path
## excel 01: ASSETS
assets = [
pd.DataFrame.from_dict(
{**json.loads(asset.json()), **{"dataclassname": asset.__class__.__name__}},
orient="index",
).T
for gridconnection in payload.gridconnections
for asset in gridconnection.assets
]
df = sort_columns(pd.concat(assets))
with pd.ExcelWriter(FOLDER / (file_id + "assets.xlsx")) as writer:
for category in ASSET_CATEGORIES:
df.query(f"category == '{category}'").dropna(axis=1).to_excel(
excel_writer=writer, sheet_name=category, index=False
)
## excel 02: CONFIG
gridconnections = [
pd.DataFrame.from_dict(
{
**json.loads(gridconnection.json()),
**{"dataclassname": gridconnection.__class__.__name__},
},
orient="index",
).T
for gridconnection in payload.gridconnections
]
gc_df = sort_columns(pd.concat(gridconnections))
# unpack assets to default asset name
gc_df["assets"] = gc_df["assets"].apply(
lambda assets: [asset["name"] for asset in assets]
)
gridnodes = [
pd.DataFrame.from_dict(
{
**json.loads(gridnode.json()),
**{"dataclassname": gridnode.__class__.__name__},
},
orient="index",
).T
for gridnode in payload.gridnodes
]
gn_df = sort_columns(pd.concat(gridnodes))
actors = [
pd.DataFrame.from_dict(
{
**json.loads(actor.json()),
**{"dataclassname": actor.__class__.__name__},
},
orient="index",
).T.drop("contracts", axis=1)
for actor in payload.actors
]
actor_df = sort_columns(pd.concat(actors))
policies = [
pd.DataFrame.from_dict(
{
**json.loads(policy.json()),
**{"dataclassname": policy.__class__.__name__},
},
orient="index",
).T
for policy in payload.policies
]
pol_df = sort_columns(pd.concat(policies))
contracts = [
pd.DataFrame.from_dict(
{
**json.loads(contract.json()),
**{
"actor_id": actor.id,
"dataclassname": contract.__class__.__name__,
"category": actor.category.value,
},
},
orient="index",
).T
for actor in payload.actors
if actor.contracts is not None
for contract in actor.contracts
]
contract_df = sort_columns(pd.concat(contracts))
dfs = {
"gridconnections": gc_df,
"gridnodes": gn_df,
"actors": actor_df,
"policies": pol_df,
"contracts": contract_df,
}
with pd.ExcelWriter(FOLDER / (file_id + "config.xlsx")) as writer:
for category, df in dfs.items():
df.to_excel(excel_writer=writer, sheet_name=category, index=False)
pass
def read_excel():
pass
def parse_defaults():
cloudclient.datamodel.defaults.__dict__