Skip to content

Commit 0ea0b4f

Browse files
authored
Merge pull request #45 from coderefinery/yu/update-example-script
update the iterative solution
2 parents 720af56 + 478d907 commit 0ea0b4f

17 files changed

+9476
-9067
lines changed

CITATION.cff

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ authors:
66
given-names: "Radovan"
77
- family-names: "Darst"
88
given-names: "Richard"
9+
- family-names: "Tian"
10+
given-names: "Yu"
911
- family-names: "Juselius"
1012
given-names: "Jonas"
1113
- family-names: "Di Remigio Eikås"

content/code/abstracting-plot.py

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,66 @@
11
import pandas as pd
2-
from matplotlib import pyplot as plt
2+
import matplotlib.pyplot as plt
33

4-
plt.xlabel("measurements")
5-
plt.ylabel("air temperature (deg C)")
64

5+
def plot(column, label, location, color, compute_mean):
6+
fig, ax = plt.subplots()
77

8-
def plot_temperatures(temperatures):
9-
plt.plot(temperatures, "r-")
10-
plt.axhline(y=mean, color="b", linestyle="--")
11-
plt.show()
12-
plt.savefig(f"{num_measurements}.png")
13-
plt.clf()
8+
# time series
9+
ax.plot(
10+
data_month.index,
11+
data_month[column],
12+
label=label,
13+
color=color,
14+
)
1415

16+
if compute_mean:
17+
values = data_month[column].values
18+
mean_value = sum(values) / len(values)
1519

16-
for num_measurements in [25, 100, 500]:
20+
# mean (as horizontal dashed line)
21+
ax.axhline(
22+
y=mean_value,
23+
label=f"mean {label}: {mean_value:.1f}",
24+
color=color,
25+
linestyle="--",
26+
)
1727

18-
# read data from file
19-
data = pd.read_csv("temperatures.csv", nrows=num_measurements)
20-
temperatures = data["Air temperature (degC)"]
28+
ax.set_title(f"{label} at {location}")
29+
ax.set_xlabel("date and time")
30+
ax.set_ylabel(label)
31+
ax.legend()
32+
ax.grid(True)
2133

22-
# compute statistics
23-
mean = sum(temperatures) / num_measurements
34+
# format x-axis for better date display
35+
fig.autofmt_xdate()
2436

25-
# plot results
26-
# plt.plot(temperatures, 'r-')
27-
# plt.axhline(y=mean, color='b', linestyle='--')
28-
# plt.show()
29-
# plt.savefig(f'{num_measurements}.png')
30-
# plt.clf()
31-
plot_temperatures(temperatures)
37+
fig.savefig(f"{month}-{column}.png")
38+
39+
40+
# read data
41+
data = pd.read_csv("weather_data.csv")
42+
43+
# combine 'date' and 'time' into a single datetime column
44+
data["datetime"] = pd.to_datetime(data["date"] + " " + data["time"])
45+
46+
# set datetime as index for convenience
47+
data = data.set_index("datetime")
48+
49+
50+
for month in ["2024-01", "2024-02", "2024-03"]:
51+
data_month = data.loc[month]
52+
53+
plot(
54+
"air_temperature_celsius",
55+
"air temperature (C)",
56+
"Helsinki airport",
57+
"red",
58+
compute_mean=True,
59+
)
60+
plot(
61+
"precipitation_mm",
62+
"precipitation (mm)",
63+
"Helsinki airport",
64+
"blue",
65+
compute_mean=False,
66+
)

content/code/add-iteration.py

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,69 @@
11
import pandas as pd
2-
from matplotlib import pyplot as plt
2+
import matplotlib.pyplot as plt
33

4-
plt.xlabel("measurements")
5-
plt.ylabel("air temperature (deg C)")
64

7-
for num_measurements in [25, 100, 500]:
5+
# read data
6+
data = pd.read_csv("weather_data.csv")
87

9-
# read data from file
10-
data = pd.read_csv("temperatures.csv", nrows=num_measurements)
11-
temperatures = data["Air temperature (degC)"]
8+
# combine 'date' and 'time' into a single datetime column
9+
data["datetime"] = pd.to_datetime(data["date"] + " " + data["time"])
1210

13-
# compute statistics
14-
mean = sum(temperatures) / num_measurements
11+
# set datetime as index for convenience
12+
data = data.set_index("datetime")
1513

16-
# plot results
17-
plt.plot(temperatures, "r-")
18-
plt.axhline(y=mean, color="b", linestyle="--")
19-
plt.show()
20-
plt.savefig(f"{num_measurements}.png")
21-
plt.clf()
14+
15+
for month in ["2024-01", "2024-02", "2024-03"]:
16+
data_month = data.loc[month]
17+
18+
fig, ax = plt.subplots()
19+
20+
# temperature time series
21+
ax.plot(
22+
data_month.index,
23+
data_month["air_temperature_celsius"],
24+
label="air temperature (C)",
25+
color="red",
26+
)
27+
28+
values = data_month["air_temperature_celsius"].values
29+
mean_temp = sum(values) / len(values)
30+
31+
# mean temperature (as horizontal dashed line)
32+
ax.axhline(
33+
y=mean_temp,
34+
label=f"mean air temperature (C): {mean_temp:.1f}",
35+
color="red",
36+
linestyle="--",
37+
)
38+
39+
ax.set_title("air temperature (C) at Helsinki airport")
40+
ax.set_xlabel("date and time")
41+
ax.set_ylabel("air temperature (C)")
42+
ax.legend()
43+
ax.grid(True)
44+
45+
# format x-axis for better date display
46+
fig.autofmt_xdate()
47+
48+
fig.savefig(f"{month}-temperature.png")
49+
50+
fig, ax = plt.subplots()
51+
52+
# precipitation time series
53+
ax.plot(
54+
data_month.index,
55+
data_month["precipitation_mm"],
56+
label="precipitation (mm)",
57+
color="blue",
58+
)
59+
60+
ax.set_title("precipitation (mm) at Helsinki airport")
61+
ax.set_xlabel("date and time")
62+
ax.set_ylabel("precipitation (mm)")
63+
ax.legend()
64+
ax.grid(True)
65+
66+
# format x-axis for better date display
67+
fig.autofmt_xdate()
68+
69+
fig.savefig(f"{month}-precipitation.png")

content/code/cli.py

Lines changed: 87 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,109 @@
1+
from pathlib import Path
2+
3+
14
import pandas as pd
2-
from matplotlib import pyplot as plt
5+
import matplotlib.pyplot as plt
36
import pytest
47
import click
58

69

7-
def plot_data(data, mean, xlabel, ylabel, file_name):
8-
plt.plot(data, "r-")
9-
plt.xlabel(xlabel)
10-
plt.ylabel(ylabel)
11-
plt.axhline(y=mean, color="b", linestyle="--")
12-
plt.savefig(file_name)
13-
plt.clf()
10+
def read_data(file_name):
11+
data = pd.read_csv(file_name)
12+
13+
# combine 'date' and 'time' into a single datetime column
14+
data["datetime"] = pd.to_datetime(data["date"] + " " + data["time"])
15+
16+
# set datetime as index for convenience
17+
data = data.set_index("datetime")
1418

19+
return data
1520

16-
def compute_mean(data):
17-
mean = sum(data) / len(data)
18-
return mean
1921

22+
def arithmetic_mean(values):
23+
mean_value = sum(values) / len(values)
24+
return mean_value
2025

21-
def test_compute_mean():
22-
result = compute_mean([1.0, 2.0, 3.0, 4.0])
26+
27+
def test_arithmetic_mean():
28+
result = arithmetic_mean([1.0, 2.0, 3.0, 4.0])
2329
assert result == pytest.approx(2.5)
2430

2531

26-
def read_data(file_name, nrows, column):
27-
data = pd.read_csv(file_name, nrows=nrows)
28-
return data[column]
32+
def plot(date_range, values, label, location, color, compute_mean, file_name):
33+
fig, ax = plt.subplots()
34+
35+
# time series
36+
ax.plot(
37+
date_range,
38+
values,
39+
label=label,
40+
color=color,
41+
)
42+
43+
if compute_mean:
44+
mean_value = arithmetic_mean(values)
45+
46+
# mean (as horizontal dashed line)
47+
ax.axhline(
48+
y=mean_value,
49+
label=f"mean {label}: {mean_value:.1f}",
50+
color=color,
51+
linestyle="--",
52+
)
53+
54+
ax.set_title(f"{label} at {location}")
55+
ax.set_xlabel("date and time")
56+
ax.set_ylabel(label)
57+
ax.legend()
58+
ax.grid(True)
59+
60+
# format x-axis for better date display
61+
fig.autofmt_xdate()
62+
63+
fig.savefig(file_name)
2964

3065

3166
@click.command()
67+
@click.option("--month", required=True, type=str, help="Which month (YYYY-MM)?")
3268
@click.option(
33-
"--num-measurements", required=True, type=int, help="Number of measurements."
69+
"--data-file",
70+
required=True,
71+
type=click.Path(exists=True, path_type=Path),
72+
help="Data is read from this file.",
3473
)
35-
@click.option("--in-file", required=True, help="File name where we read from.")
36-
@click.option("--out-file", required=True, help="File name where we write to.")
37-
def main(num_measurements, in_file, out_file):
38-
39-
temperatures = read_data(
40-
file_name=in_file,
41-
nrows=num_measurements,
42-
column="Air temperature (degC)",
43-
)
74+
@click.option(
75+
"--output-directory",
76+
required=True,
77+
type=click.Path(exists=True, path_type=Path),
78+
help="Figures are written to this directory.",
79+
)
80+
def main(
81+
month,
82+
data_file,
83+
output_directory,
84+
):
85+
data = read_data(data_file)
4486

45-
mean = compute_mean(temperatures)
87+
data_month = data.loc[month]
88+
date_range = data_month.index
4689

47-
plot_data(
48-
data=temperatures,
49-
mean=mean,
50-
xlabel="measurements",
51-
ylabel="air temperature (deg C)",
52-
file_name=out_file,
90+
plot(
91+
date_range,
92+
data_month["air_temperature_celsius"].values,
93+
"air temperature (C)",
94+
"Helsinki airport",
95+
"red",
96+
compute_mean=True,
97+
file_name=output_directory / f"{month}-temperature.png",
98+
)
99+
plot(
100+
date_range,
101+
data_month["precipitation_mm"].values,
102+
"precipitation (mm)",
103+
"Helsinki airport",
104+
"blue",
105+
compute_mean=False,
106+
file_name=output_directory / f"{month}-precipitation.png",
53107
)
54108

55109

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pandas as pd
2+
import matplotlib.pyplot as plt
3+
4+
5+
# read data
6+
data = pd.read_csv("weather_data.csv")
7+
8+
# combine 'date' and 'time' into a single datetime column
9+
data["datetime"] = pd.to_datetime(data["date"] + " " + data["time"])
10+
11+
# set datetime as index for convenience
12+
data = data.set_index("datetime")
13+
14+
# keep only january data
15+
january = data.loc["2024-01"]
16+
17+
fig, ax = plt.subplots()
18+
19+
# temperature time series
20+
ax.plot(
21+
january.index,
22+
january["air_temperature_celsius"],
23+
label="air temperature (C)",
24+
color="red",
25+
)
26+
27+
values = january["air_temperature_celsius"].values
28+
mean_temp = sum(values) / len(values)
29+
30+
# mean temperature (as horizontal dashed line)
31+
ax.axhline(
32+
y=mean_temp,
33+
label=f"mean air temperature (C): {mean_temp:.1f}",
34+
color="red",
35+
linestyle="--",
36+
)
37+
38+
ax.set_title("air temperature (C) at Helsinki airport")
39+
ax.set_xlabel("date and time")
40+
ax.set_ylabel("air temperature (C)")
41+
ax.legend()
42+
ax.grid(True)
43+
44+
# format x-axis for better date display
45+
fig.autofmt_xdate()
46+
47+
fig.savefig("2024-01-temperature.png")

0 commit comments

Comments
 (0)