Skip to content

Commit

Permalink
Merge pull request #61 from paul-florentin-charles/dev
Browse files Browse the repository at this point in the history
Add begin/end year to average plots
  • Loading branch information
paul-florentin-charles authored Feb 10, 2024
2 parents 1643ede + d2bda14 commit a62fa10
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
10 changes: 7 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,11 @@ def minimal_csv() -> Response:
@app.route(f"{base_path}/graph/monthly_averages")
@swag_from(monthly_averages_specs.route_specs)
def monthly_averages() -> Response:
params: tuple = parse_args(request.args, param.file_name)
params: tuple = parse_args(
request.args, param.file_name, param.begin_year, param.end_year
)

all_rainfall.bar_rainfall_averages()
all_rainfall.bar_rainfall_averages(begin_year=params[1], end_year=params[2])
plt.savefig(params[0], format="svg")
plt.close()

Expand All @@ -274,7 +276,9 @@ def monthly_averages() -> Response:
def seasonal_averages() -> Response:
params: tuple = parse_args(request.args, param.file_name)

all_rainfall.bar_rainfall_averages(monthly=False)
all_rainfall.bar_rainfall_averages(
monthly=False, begin_year=params[1], end_year=params[2]
)
plt.savefig(params[0], format="svg")
plt.close()

Expand Down
6 changes: 5 additions & 1 deletion src/api/swagger/graph/monthly_averages_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"content": MediaType.IMG_SVG,
},
},
"parameters": [param.file_name],
"parameters": [
param.file_name,
param.begin_year,
param.end_year,
],
"produces": MediaType.IMG_SVG,
}
6 changes: 5 additions & 1 deletion src/api/swagger/graph/seasonal_averages_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"content": MediaType.IMG_SVG,
},
},
"parameters": [param.file_name],
"parameters": [
param.file_name,
param.begin_year,
param.end_year,
],
"produces": MediaType.IMG_SVG,
}
17 changes: 13 additions & 4 deletions src/core/models/all_rainfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,19 +320,28 @@ def get_last_year(self) -> int:

return self.yearly_rainfall.get_last_year()

def bar_rainfall_averages(self, monthly: bool | None = True) -> list:
def bar_rainfall_averages(
self,
monthly: bool | None = True,
begin_year: int | None = None,
end_year: int | None = None,
) -> list:
"""
Plots a bar graphic displaying average rainfall for each month or each season.
:param monthly: if True, plots monthly rainfall averages.
if False, plots seasonal rainfall averages.
:return: A list of the Rainfall averages for each month or season.
"""
label: str = f"Average rainfall (mm) between {self.starting_year} and {self.get_last_year()}"
label: str = f"Average rainfall (mm) between {begin_year or self.starting_year} and {self.get_last_year()}"
if monthly:
return plotting.bar_monthly_rainfall_averages(self.monthly_rainfalls, label)
return plotting.bar_monthly_rainfall_averages(
self.monthly_rainfalls, label, begin_year, end_year
)

return plotting.bar_seasonal_rainfall_averages(self.seasonal_rainfalls, label)
return plotting.bar_seasonal_rainfall_averages(
self.seasonal_rainfalls, label, begin_year, end_year
)

def bar_rainfall_linreg_slopes(self, monthly: bool | None = True) -> list:
"""
Expand Down
30 changes: 26 additions & 4 deletions src/core/utils/functions/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,31 @@ def bar_column_according_to_year(yearly_rainfall: pd.DataFrame, label: Label) ->


def bar_monthly_rainfall_averages(
monthly_rainfalls: dict, label: str | None = "Average rainfall (mm)"
monthly_rainfalls: dict,
label: str | None = "Average rainfall (mm)",
begin_year: int | None = None,
end_year: int | None = None,
) -> list:
"""
Plots a bar graphic displaying average rainfall for each month passed through the dict.
:param monthly_rainfalls: A list of instances of MonthlyRainfall.
To be purposeful, all instances should have the same time frame in years.
:param label: A string to use as a label for bar graphic. (optional)
:param begin_year: An integer representing the year
to start getting our rainfall values (optional).
:param end_year: An integer representing the year
to end getting our rainfall values (optional).
:return: A list of the Rainfall averages for each month.
"""
month_labels, averages = [], []
for monthly_rainfall in monthly_rainfalls.values():
month_labels.append(monthly_rainfall.month.name[:3])
averages.append(monthly_rainfall.get_average_yearly_rainfall())
averages.append(
monthly_rainfall.get_average_yearly_rainfall(
begin_year=begin_year, end_year=end_year
)
)

plt.bar(month_labels, averages, label=label)
plt.legend()
Expand Down Expand Up @@ -128,20 +139,31 @@ def bar_monthly_rainfall_linreg_slopes(monthly_rainfalls: dict) -> list:


def bar_seasonal_rainfall_averages(
seasonal_rainfalls: dict, label: str | None = "Average rainfall (mm)"
seasonal_rainfalls: dict,
label: str | None = "Average rainfall (mm)",
begin_year: int | None = None,
end_year: int | None = None,
) -> list:
"""
Plots a bar graphic displaying average rainfall for each season passed through the dict.
:param seasonal_rainfalls: A list of instances of SeasonalRainfall.
To be purposeful, all instances should have the same time frame in years.
:param label: A string to use as a label for bar graphic. (optional)
:param begin_year: An integer representing the year
to start getting our rainfall values (optional).
:param end_year: An integer representing the year
to end getting our rainfall values (optional).
:return: A list of the Rainfall averages for each season.
"""
season_labels, averages = [], []
for seasonal_rainfall in seasonal_rainfalls.values():
season_labels.append(seasonal_rainfall.season.name)
averages.append(seasonal_rainfall.get_average_yearly_rainfall())
averages.append(
seasonal_rainfall.get_average_yearly_rainfall(
begin_year=begin_year, end_year=end_year
)
)

plt.bar(season_labels, averages, label=label)
plt.legend()
Expand Down

0 comments on commit a62fa10

Please sign in to comment.