• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SPF-OST / pytrnsys_process / 13658315606

04 Mar 2025 04:27PM UTC coverage: 98.126%. Remained the same
13658315606

push

github

ahobeost
Adjusted tutorials to have Users get the example data and to make use of its folder structure.

1204 of 1227 relevant lines covered (98.13%)

1.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.67
/examples/ready_to_run/processing_example.py
1
import pathlib as _pl
2✔
2

3
import matplotlib.pyplot as _plt
2✔
4

5
from pytrnsys_process import api
2✔
6

7

8
# Your processing steps are writen inside functions.
9
# Which need to be defined before your scripts entry point.
10
# Define your processing steps at the top of the script.
11
def processing_of_monthly_data(simulation: api.Simulation):
2✔
12
    # create a bar chart using monthly data
13
    monthly_df = simulation.monthly
1✔
14
    columns_to_plot = ["QSnk60P", "QSnk60PauxCondSwitch_kW"]
1✔
15
    fig, ax = api.bar_chart(
1✔
16
        monthly_df,
17
        columns_to_plot,
18
    )
19

20
    # modify label for the y axis
21
    ax.set_ylabel("Power [kW]")
1✔
22

23
    # This is also done by api.save_plot, but if you want to see your plot before saving it.
24
    # For example with _plt.show(). Calling this function is required, to make the plot look as expected.
25
    # _plt.tight_layout()
26

27
    # create plots folder inside simulation directory and save plot in configured formats
28
    api.export_plots_in_configured_formats(
1✔
29
        fig, simulation.path, "monthly-bar-chart"
30
    )
31

32

33
def processing_of_hourly_data(simulation: api.Simulation):
2✔
34
    # create line plot using hourly data
35
    fig, ax = api.line_plot(simulation.hourly, ["QSrc1TIn", "QSrc1TOut"])
1✔
36

37
    # Here you can modify anything to do with your plot, according to the matplotlib.pyplot standard
38
    ax.set_ylabel("Temperature [°C]")
1✔
39
    # make label fit inside your plot
40
    # _plt.tight_layout()
41

42
    # create plots folder inside simulation directory and save plot in configured formats
43
    api.export_plots_in_configured_formats(
1✔
44
        fig, simulation.path, "hourly-line-plot"
45
    )
46

47

48
def processing_for_histogram(simulation: api.Simulation):
2✔
49
    # create histogram using hourly data
50
    api.histogram(simulation.hourly, ["QSrc1TIn"])
2✔
51

52
    # if you don't want to export your plot, you can also just show it by calling this function
53
    _plt.show()
2✔
54

55

56
def calc_total_yearly_demand_per_sim(
2✔
57
        simulations_data: api.SimulationsData,
58
) -> api.SimulationsData:
59
    # Columns to sum
60
    demand_columns = [
2✔
61
        "qSysOut_QSnk131Demand",
62
        "qSysOut_QSnk183Demand",
63
        "qSysOut_QSnk191Demand",
64
        "qSysOut_QSnk225Demand",
65
        "qSysOut_QSnk243Demand",
66
        "qSysOut_QSnk266Demand",
67
        "qSysOut_QSnk322Demand",
68
        "qSysOut_QSnk335Demand",
69
        "qSysOut_QSnk358Demand",
70
        "qSysOut_QSnk417Demand",
71
        "qSysOut_QSnk448Demand",
72
        "qSysOut_QSnk469Demand",
73
        "qSysOut_QSnk488Demand",
74
        "qSysOut_QSnk524Demand",
75
        "qSysOut_QSnk539Demand",
76
        "qSysOut_QSnk558Demand",
77
        "qSysOut_QSnk579Demand",
78
        "qSysOut_QSnk60Demand",
79
        "qSysOut_QSnk85Demand",
80
    ]
81
    # Conversion to gwh
82
    kwh_to_gwh = 1e-6
2✔
83
    # Iterate through each simulation
84
    for sim_name, sim in simulations_data.simulations.items():
2✔
85
        # Access the monthly data
86
        monthly_df = sim.monthly
2✔
87
        # Calculate total monthly demand in GWh (sum columns and convert from kWh)
88
        monthly_df["total_demand_GWh"] = (
2✔
89
                monthly_df[demand_columns].sum(axis=1) * kwh_to_gwh
90
        )
91

92
        # Sum Jan-December values (skip first 2 months - typically Nov/Dec)
93
        # Wrap in int to get rid of decimal places
94
        yearly_total = int(monthly_df["total_demand_GWh"].iloc[2::].sum())
2✔
95

96
        # Store result in scalar data for comparison
97
        simulations_data.scalar.loc[sim_name, "yearly_demand_GWh"] = (
2✔
98
            yearly_total
99
        )
100
    return simulations_data
2✔
101

102

103
def calc_v_ice_ratio_max_per_sim(
2✔
104
        simulations_data: api.SimulationsData,
105
) -> api.SimulationsData:
106
    # Iterate through each simulation's hourly data
107
    for sim_name, sim in simulations_data.simulations.items():
2✔
108
        # Access hourly data
109
        hourly_data = sim.hourly
2✔
110
        # Get maximum VIceRatio value from hourly timeseries
111
        max_ice_ratio = hourly_data["VIceRatio"].max()
2✔
112
        # Store result in scalar data table using simulation name as index
113
        simulations_data.scalar.loc[sim_name, "VIceRatioMax"] = max_ice_ratio
2✔
114
    return simulations_data
2✔
115

116

117
def comparison_of_scalar_data(
2✔
118
        simulations_data: api.SimulationsData,
119
):
120
    # Calculate and add two new metrics to the results:
121
    # 1. Total yearly energy demand in GWh
122
    # 2. Maximum ice storage ratio from hourly data
123

124
    # Grouping steps will ensure that the failure of a dependent step will cause the whole process to fail.
125
    simulations_data = calc_total_yearly_demand_per_sim(simulations_data)
2✔
126
    simulations_data = calc_v_ice_ratio_max_per_sim(simulations_data)
2✔
127

128
    # Create a scatter plot for comparison comparing:
129
    fig, _ = api.scatter_plot(
2✔
130
        simulations_data.scalar,
131
        "VIceSscaled",
132
        "VIceRatioMax",
133
        group_by_color="yearly_demand_GWh",
134
        group_by_marker="ratioDHWtoSH_allSinks",
135
    )
136

137
    # save plots in the results folder in the configured formats
138
    api.export_plots_in_configured_formats(
2✔
139
        fig,
140
        simulations_data.path_to_simulations,
141
        "compare-plot",
142
        plots_folder_name="",
143
    )
144

145

146
def main():
2✔
147
    # This is the entry point to your script.
148
    # Here, you can decide how you would like to run your processing steps.
149
    # In the example below we run both processing steps on a whole result set.
150
    # Or a single processing step on a single simulation.
151
    # Make sure to remove the lines you do not want to run.
152

153
    # bundle the steps into a list
154
    processing_scenarios = [
2✔
155
        processing_of_monthly_data,
156
        processing_of_hourly_data,
157
    ]
158

159
    # run the scenarios on a whole result using multiple processes
160
    simulations_data = api.process_whole_result_set_parallel(
2✔
161
        _pl.Path(api.REPO_ROOT / "examples/ready_to_run/data/results"),
162
        processing_scenarios,
163
    )
164

165
    # using the data from processing multiple simulations, run your comparisons steps
166
    api.do_comparison(comparison_of_scalar_data, simulations_data)
2✔
167

168
    # run the single scenario on a single simulation
169
    (
2✔
170
        api.process_single_simulation(
171
            _pl.Path(
172
                api.REPO_ROOT
173
                / "examples/ready_to_run/data/results/complete-0-SnkScale0.8000-StoreScale10"
174
            ),
175
            # ===============================================================
176
            processing_for_histogram,
177
            # do not add round brackets when linking your processing step
178
            # processing_for_histogram AND NOT processing_for_histogram()
179
            # ===============================================================
180
        )
181
    )
182

183

184
# This is required since we would like to run the script directly
185
if __name__ == "__main__":
2✔
186
    main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc