Spaces:
Sleeping
Sleeping
Update src/emission_calculator/calculator.py
Browse files
src/emission_calculator/calculator.py
CHANGED
@@ -1,13 +1,15 @@
|
|
1 |
from pandas import DataFrame
|
2 |
|
3 |
from plotly.subplots import make_subplots
|
4 |
-
from plotly.graph_objects import Figure, Pie, Bar, Scatter
|
5 |
|
6 |
|
7 |
-
def draw_report_figure(
|
|
|
|
|
8 |
figure_specs = [
|
|
|
9 |
[{"type": "xy"}, {"type": "xy"}],
|
10 |
-
[{"type": "domain"}, None],
|
11 |
]
|
12 |
|
13 |
fig = make_subplots(
|
@@ -16,7 +18,6 @@ def draw_report_figure(df: DataFrame) -> Figure:
|
|
16 |
specs=figure_specs,
|
17 |
subplot_titles=(
|
18 |
"Carbon Emission by Category",
|
19 |
-
"Cumulative Emission %",
|
20 |
"Emission Distribution",
|
21 |
),
|
22 |
)
|
@@ -32,8 +33,8 @@ def draw_report_figure(df: DataFrame) -> Figure:
|
|
32 |
name="Emission Distribution",
|
33 |
marker={"colors": ["#6DA34D", "#81C3D7", "#FFC857"]},
|
34 |
),
|
35 |
-
row=
|
36 |
-
col=
|
37 |
)
|
38 |
|
39 |
# Bar chart for emissions by category
|
@@ -61,20 +62,6 @@ def draw_report_figure(df: DataFrame) -> Figure:
|
|
61 |
col=1,
|
62 |
)
|
63 |
|
64 |
-
# Cumulative line chart
|
65 |
-
cumulative_percentage = (df["Value"].cumsum() / df["Value"].sum()) * 100
|
66 |
-
fig.add_trace(
|
67 |
-
Scatter(
|
68 |
-
x=df["Category"],
|
69 |
-
y=cumulative_percentage,
|
70 |
-
name="Cumulative %",
|
71 |
-
mode="lines+markers",
|
72 |
-
line=dict(color="#333333", dash="dash"),
|
73 |
-
),
|
74 |
-
row=1,
|
75 |
-
col=2,
|
76 |
-
)
|
77 |
-
|
78 |
# Update layout for axes and overall layout
|
79 |
fig.update_layout(
|
80 |
title_text=f"Carbon Footprint of {df['Name'][0]}",
|
@@ -89,17 +76,12 @@ def draw_report_figure(df: DataFrame) -> Figure:
|
|
89 |
mirror=True,
|
90 |
gridcolor="lightgrey",
|
91 |
),
|
92 |
-
yaxis2=dict(
|
93 |
-
title="Cumulative Percentage",
|
94 |
-
side="right",
|
95 |
-
showgrid=False,
|
96 |
-
),
|
97 |
legend=dict(
|
98 |
-
x=1,
|
99 |
-
y=0,
|
100 |
xanchor="right",
|
101 |
yanchor="bottom",
|
102 |
-
orientation="h",
|
103 |
),
|
104 |
)
|
105 |
|
@@ -117,106 +99,132 @@ def draw_report_figure(df: DataFrame) -> Figure:
|
|
117 |
gridcolor="lightgrey",
|
118 |
)
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
return fig
|
121 |
|
122 |
|
123 |
def draw_historic_figure(df: DataFrame) -> Figure:
|
124 |
-
# Create subplots with 2 rows and
|
125 |
fig = make_subplots(
|
126 |
rows=2,
|
127 |
-
cols=
|
|
|
128 |
subplot_titles=(
|
129 |
-
"Energy Usage by Company",
|
130 |
-
"Waste Generated by Company",
|
131 |
-
"Business Travel by Company",
|
132 |
"Total Carbon Footprint by Company",
|
|
|
133 |
),
|
|
|
134 |
)
|
135 |
|
136 |
-
#
|
137 |
-
|
138 |
-
|
139 |
-
x=df["Name"],
|
140 |
-
y=df["Energy Usage"],
|
141 |
-
mode="lines",
|
142 |
-
fill="tozeroy",
|
143 |
-
line=dict(color="blue"),
|
144 |
-
fillcolor="rgba(31, 119, 180, 0.5)", # Gradient fill for blue
|
145 |
-
name="Energy Usage",
|
146 |
-
),
|
147 |
-
row=1,
|
148 |
-
col=1,
|
149 |
)
|
150 |
|
|
|
151 |
fig.add_trace(
|
152 |
Scatter(
|
153 |
x=df["Name"],
|
154 |
-
y=df["
|
155 |
mode="lines",
|
156 |
fill="tozeroy",
|
157 |
-
line=dict(color="
|
158 |
-
fillcolor="rgba(
|
159 |
-
name="
|
160 |
),
|
161 |
row=1,
|
162 |
-
col=2,
|
163 |
-
)
|
164 |
-
|
165 |
-
fig.add_trace(
|
166 |
-
Scatter(
|
167 |
-
x=df["Name"],
|
168 |
-
y=df["Business Travel"],
|
169 |
-
mode="lines",
|
170 |
-
fill="tozeroy",
|
171 |
-
line=dict(color="green"),
|
172 |
-
fillcolor="rgba(44, 160, 44, 0.5)", # Gradient fill for green
|
173 |
-
name="Business Travel",
|
174 |
-
),
|
175 |
-
row=2,
|
176 |
col=1,
|
177 |
)
|
178 |
|
179 |
-
#
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
196 |
|
197 |
-
# Update layout for
|
198 |
fig.update_layout(
|
199 |
-
title="Company Metrics
|
200 |
-
barmode="group", # Group bars by category
|
201 |
template="plotly_white",
|
202 |
-
|
203 |
-
height=600,
|
204 |
width=1000,
|
205 |
-
|
|
|
206 |
)
|
207 |
|
208 |
-
#
|
209 |
fig.update_xaxes(title_text="Company", row=1, col=1)
|
210 |
-
fig.update_yaxes(title_text="
|
211 |
|
212 |
-
|
213 |
-
fig.
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
|
|
|
|
220 |
|
221 |
return fig
|
222 |
|
|
|
1 |
from pandas import DataFrame
|
2 |
|
3 |
from plotly.subplots import make_subplots
|
4 |
+
from plotly.graph_objects import Figure, Pie, Bar, Scatter, Scatterpolar
|
5 |
|
6 |
|
7 |
+
def draw_report_figure(
|
8 |
+
df: DataFrame, threshold: tuple[float] = [30_000, 5_000, 15_000]
|
9 |
+
) -> Figure:
|
10 |
figure_specs = [
|
11 |
+
[{"type": "xy"}, {"type": "domain"}],
|
12 |
[{"type": "xy"}, {"type": "xy"}],
|
|
|
13 |
]
|
14 |
|
15 |
fig = make_subplots(
|
|
|
18 |
specs=figure_specs,
|
19 |
subplot_titles=(
|
20 |
"Carbon Emission by Category",
|
|
|
21 |
"Emission Distribution",
|
22 |
),
|
23 |
)
|
|
|
33 |
name="Emission Distribution",
|
34 |
marker={"colors": ["#6DA34D", "#81C3D7", "#FFC857"]},
|
35 |
),
|
36 |
+
row=1,
|
37 |
+
col=2,
|
38 |
)
|
39 |
|
40 |
# Bar chart for emissions by category
|
|
|
62 |
col=1,
|
63 |
)
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
# Update layout for axes and overall layout
|
66 |
fig.update_layout(
|
67 |
title_text=f"Carbon Footprint of {df['Name'][0]}",
|
|
|
76 |
mirror=True,
|
77 |
gridcolor="lightgrey",
|
78 |
),
|
|
|
|
|
|
|
|
|
|
|
79 |
legend=dict(
|
80 |
+
x=1,
|
81 |
+
y=0,
|
82 |
xanchor="right",
|
83 |
yanchor="bottom",
|
84 |
+
orientation="h",
|
85 |
),
|
86 |
)
|
87 |
|
|
|
99 |
gridcolor="lightgrey",
|
100 |
)
|
101 |
|
102 |
+
# Add a single general recommendation text box
|
103 |
+
e, w, b = df["Value"]
|
104 |
+
threshold_values = [e >= threshold[0], w >= threshold[1], b >= threshold[2]]
|
105 |
+
recommendations = []
|
106 |
+
texts = [
|
107 |
+
"- Reduce energy usage by adopting energy-efficient practices.\n",
|
108 |
+
"- Minimize waste by recycling and using sustainable materials.\n",
|
109 |
+
"- Limit business travel and opt for virtual meetings where possible."
|
110 |
+
]
|
111 |
+
if any(threshold_values):
|
112 |
+
fig.add_annotation(
|
113 |
+
text=("Recommendations to reduce carbon footprint:\n"),
|
114 |
+
xref="paper",
|
115 |
+
yref="paper",
|
116 |
+
x=0,
|
117 |
+
y=0.2, # Positioning inside the plot area, just below center
|
118 |
+
showarrow=False,
|
119 |
+
font=dict(size=12),
|
120 |
+
align="center",
|
121 |
+
bordercolor="black",
|
122 |
+
borderwidth=1,
|
123 |
+
borderpad=10,
|
124 |
+
bgcolor="lightyellow",
|
125 |
+
)
|
126 |
+
if threshold_values[0]:
|
127 |
+
recommendations.append(texts[0])
|
128 |
+
if threshold_values[1]:
|
129 |
+
recommendations.append(texts[1])
|
130 |
+
if threshold_values[2]:
|
131 |
+
recommendations.append(texts[2])
|
132 |
+
|
133 |
+
for i, text in enumerate(recommendations):
|
134 |
+
fig.add_annotation(
|
135 |
+
text=(text),
|
136 |
+
xref="paper",
|
137 |
+
yref="paper",
|
138 |
+
x=0,
|
139 |
+
y=(i + 1) * 0.05, # Positioning inside the plot area, just below center
|
140 |
+
showarrow=False,
|
141 |
+
font=dict(size=12),
|
142 |
+
align="center",
|
143 |
+
bordercolor="black",
|
144 |
+
borderwidth=1,
|
145 |
+
borderpad=10,
|
146 |
+
bgcolor="lightyellow",
|
147 |
+
)
|
148 |
+
|
149 |
return fig
|
150 |
|
151 |
|
152 |
def draw_historic_figure(df: DataFrame) -> Figure:
|
153 |
+
# Create subplots with 2 rows and 1 column
|
154 |
fig = make_subplots(
|
155 |
rows=2,
|
156 |
+
cols=1,
|
157 |
+
specs=[[{"type": "xy"}], [{"type": "polar"}]],
|
158 |
subplot_titles=(
|
|
|
|
|
|
|
159 |
"Total Carbon Footprint by Company",
|
160 |
+
"Company Metrics Radar Chart",
|
161 |
),
|
162 |
+
row_heights=[0.6, 0.4],
|
163 |
)
|
164 |
|
165 |
+
# Calculate each company's total carbon footprint as the sum of the three metrics
|
166 |
+
df["Carbon Footprint"] = (
|
167 |
+
df["Energy Usage"] + df["Waste Generated"] + df["Business Travel"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
)
|
169 |
|
170 |
+
# Add gradient-filled area trace for the Carbon Footprint
|
171 |
fig.add_trace(
|
172 |
Scatter(
|
173 |
x=df["Name"],
|
174 |
+
y=df["Carbon Footprint"],
|
175 |
mode="lines",
|
176 |
fill="tozeroy",
|
177 |
+
line=dict(color="blue"),
|
178 |
+
fillcolor="rgba(31, 119, 180, 0.5)", # Gradient fill for blue
|
179 |
+
name="Carbon Footprint",
|
180 |
),
|
181 |
row=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
col=1,
|
183 |
)
|
184 |
|
185 |
+
# Prepare data for radar chart (normalizing values for better comparability)
|
186 |
+
categories = ["Energy Usage", "Waste Generated", "Business Travel"]
|
187 |
+
for _, company in df.iterrows():
|
188 |
+
fig.add_trace(
|
189 |
+
Scatterpolar(
|
190 |
+
r=[
|
191 |
+
company["Energy Usage"],
|
192 |
+
company["Waste Generated"],
|
193 |
+
company["Business Travel"],
|
194 |
+
company["Energy Usage"], # Closing the loop
|
195 |
+
],
|
196 |
+
theta=categories + [categories[0]], # Circular radar plot
|
197 |
+
fill="toself",
|
198 |
+
name=company["Name"],
|
199 |
+
),
|
200 |
+
row=2,
|
201 |
+
col=1,
|
202 |
+
)
|
203 |
|
204 |
+
# Update layout for the figure
|
205 |
fig.update_layout(
|
206 |
+
title="Company Metrics Visualization",
|
|
|
207 |
template="plotly_white",
|
208 |
+
height=800,
|
|
|
209 |
width=1000,
|
210 |
+
showlegend=True,
|
211 |
+
legend=dict(x=1.05, y=1), # Adjust legend position
|
212 |
)
|
213 |
|
214 |
+
# Update x and y-axis titles for the first plot
|
215 |
fig.update_xaxes(title_text="Company", row=1, col=1)
|
216 |
+
fig.update_yaxes(title_text="Carbon Footprint (total)", row=1, col=1)
|
217 |
|
218 |
+
# Customize polar (radar) chart layout
|
219 |
+
fig.update_polars(
|
220 |
+
radialaxis=dict(
|
221 |
+
visible=True,
|
222 |
+
range=[
|
223 |
+
df[categories].values.min(),
|
224 |
+
df[categories].values.max(),
|
225 |
+
],
|
226 |
+
)
|
227 |
+
)
|
228 |
|
229 |
return fig
|
230 |
|