-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatplotlib_sample.py
More file actions
106 lines (91 loc) · 3.73 KB
/
matplotlib_sample.py
File metadata and controls
106 lines (91 loc) · 3.73 KB
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
import matplotlib.pyplot as plt
import psr.graf
if __name__ == "__main__":
"""
Instantiate a `SddpBinaryReader` object or use the context managed function
`open_bin`.
Usage:
- class SddpBinaryReader.open(file_path, kwargs)
- open_bin(file_path, kwargs)
file_path can be a path without extension (hdr and bin have common base names),
with .hdr or .bin extensions.
kwargs:
- print_metadata: boolean, prints HDR description
Data can be read using `class SddpBinaryReader.read(stage, scenario, block)`
or class `SddpBinaryReader.read_blocks(stage, scenario)`.
`stage`, `scenario`, and `block` are 1-based indexes. Their range can be
retrieved by `.stages`, `.scenarios`, and `.blocks(scenario)` respectively.
"""
print("Example using open(...), close()")
filename1 = "sample_data/gerter.hdr"
graf_file = psr.graf.BinReader()
graf_file.open(filename1, print_metadata=True)
print("Stages:", graf_file.stages)
print("Agents:", graf_file.agents)
# 1-base indexing
stage = 1
scenario = 1
block = 1
print("Specific values (per agent):", graf_file.read(stage,
scenario, block))
graf_file.close()
# Hourly demand data plot example
with psr.graf.open_bin("sample_data/demand.hdr") as graf_file:
print()
print(graf_file.name)
print("Stages:", graf_file.stages)
print("Scenarios:", graf_file.scenarios)
print("Agents:", graf_file.agents)
stage = 1
# Number of hours vary by stage on monthly studies.
hours = list(range(1, graf_file.blocks(stage) + 1))
scenario = 1
hour_data_per_agent = []
for hour in hours:
data = graf_file.read(stage, scenario, hour)
hour_data_per_agent.append(data)
# Transpose stage_data_per_agent
agent_data_per_hour = list(map(list, zip(*hour_data_per_agent)))
plt.figure()
plt.title("hourly {}, stage {}, scenario {}".format(graf_file.name,
stage, scenario))
legend_entries = []
for iagent, agent_hour_values in enumerate(agent_data_per_hour):
agent = graf_file.agents[iagent]
legend_entries.append(agent)
plt.plot(hours, agent_hour_values)
plt.legend(legend_entries)
plt.grid(True)
plt.ylabel(graf_file.units)
plt.xlabel("Hours")
with psr.graf.open_bin("sample_data/gerter.hdr") as graf_file:
print()
print(graf_file.name)
print("Stages:", graf_file.stages)
print("Scenarios:", graf_file.scenarios)
print("Agents:", graf_file.agents)
stage_start = graf_file.min_stage
stage_end = graf_file.max_stage + 1
stages = list(range(stage_start, stage_end))
scenario = 1
block = 1
stage_data_per_agent = []
for stage in range(stage_start, stage_end):
data = graf_file.read(stage, scenario, block)
stage_data_per_agent.append(data)
# Transpose stage_data_per_agent
agent_data_per_stage = list(map(list, zip(*stage_data_per_agent)))
plt.figure()
plt.title("{}, scenario {}, block {}".format(graf_file.name,
scenario, block))
legend_entries = []
for iagent, agent_stage_values in enumerate(agent_data_per_stage):
agent = graf_file.agents[iagent]
legend_entries.append(agent)
plt.plot(stages, agent_stage_values)
plt.legend(legend_entries)
plt.grid(True)
plt.ylabel(graf_file.units)
plt.xlabel("Stage")
plt.xticks(stages)
plt.show()