Skip to content

Commit

Permalink
Merge pull request #51 from paul-florentin-charles/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
paul-florentin-charles committed Dec 6, 2023
2 parents aa823e4 + 67cfd73 commit 7ba1fbb
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
```commandline
git clone https://github.com/paul-florentin-charles/bcn-rainfall-models.git
cd bcn-rainfall-models
pip3 install -r requirements.txt
pip install -r requirements.txt
python3 app.py
```

Expand Down
28 changes: 27 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from typing import Union

import matplotlib.pyplot as plt
from flasgger import Swagger, swag_from
from flask import Flask, jsonify, request, Response, send_file

Expand All @@ -18,6 +19,7 @@
YearsAboveOrBelowNormalSchema,
)
from src.api.swagger.csv import minimal_csv_specs
from src.api.swagger.graph import monthly_averages_specs, seasonal_averages_specs
from src.api.swagger.media_types import MediaType
from src.api.swagger.rainfall import (
average_specs,
Expand Down Expand Up @@ -242,7 +244,7 @@ def years_above_normal() -> Response:
@swag_from(minimal_csv_specs.route_specs)
def minimal_csv() -> Response:
params: tuple = parse_args(
request.args, param.time_mode, param.month, param.season, param.csv_path
request.args, param.time_mode, param.month, param.season, param.file_name
)

error: Union[Response, dict] = manage_time_mode_errors(
Expand All @@ -256,5 +258,29 @@ def minimal_csv() -> Response:
return send_file(params[-1], mimetype=MediaType.TXT_CSV, as_attachment=True)


@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)

all_rainfall.bar_rainfall_averages()
plt.savefig(params[0])
plt.close()

return send_file(params[0], mimetype=MediaType.IMG_SVG, as_attachment=True)


@app.route(f"{base_path}/graph/seasonal_averages")
@swag_from(seasonal_averages_specs.route_specs)
def seasonal_averages() -> Response:
params: tuple = parse_args(request.args, param.file_name)

all_rainfall.bar_rainfall_averages(monthly=False)
plt.savefig(params[0])
plt.close()

return send_file(params[0], mimetype=MediaType.IMG_SVG, as_attachment=True)


if __name__ == "__main__":
app.run(debug=True)
4 changes: 2 additions & 2 deletions coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/api/swagger/csv/minimal_csv_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
},
"400": error.bad_request_specs,
},
"parameters": [param.csv_path, *param.time_params],
"parameters": [param.file_name, *param.time_params],
"produces": MediaType.TXT_CSV,
}
Empty file.
22 changes: 22 additions & 0 deletions src/api/swagger/graph/monthly_averages_specs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
/graph/monthly_averages
Retrieve monthly averages of data as an SVG.
"""

import src.api.swagger.parameters_specs as param
from src.api.swagger.media_types import MediaType

route_specs: dict = {
"operationId": "getMonthlyAverages",
"summary": "Retrieve monthly averages of data as an SVG.",
"tags": ["Graph"],
"responses": {
"200": {
"description": "Monthly averages as an SVG",
"content": MediaType.IMG_SVG,
},
},
"parameters": [param.file_name],
"produces": MediaType.IMG_SVG,
}
22 changes: 22 additions & 0 deletions src/api/swagger/graph/seasonal_averages_specs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
/graph/seasonal_averages
Retrieve seasonal averages of data as an SVG.
"""

import src.api.swagger.parameters_specs as param
from src.api.swagger.media_types import MediaType

route_specs: dict = {
"operationId": "getSeasonalAverages",
"summary": "Retrieve seasonal averages of data as an SVG.",
"tags": ["Graph"],
"responses": {
"200": {
"description": "Seasonal averages as an SVG",
"content": MediaType.IMG_SVG,
},
},
"parameters": [param.file_name],
"produces": MediaType.IMG_SVG,
}
1 change: 1 addition & 0 deletions src/api/swagger/media_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ class MediaType(str, BaseEnum):

APP_JSON: str = mimetypes.types_map[".json"]
TXT_CSV: str = mimetypes.types_map[".csv"]
IMG_SVG: str = mimetypes.types_map[".svg"]
7 changes: 4 additions & 3 deletions src/api/swagger/parameters_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@

time_params: tuple = (time_mode, month, season)

csv_path: dict = {
"default": "data.csv",
file_name: dict = {
"default": None,
"required": True,
"type": "string",
"name": "csv_path",
"name": "file_name",
"description": "Name of the file to be retrieved",
"in": "query",
}
5 changes: 3 additions & 2 deletions src/core/models/all_rainfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,11 @@ def bar_rainfall_averages(self, monthly: Optional[bool] = True) -> list:
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()}"
if monthly:
return plotting.bar_monthly_rainfall_averages(self.monthly_rainfalls)
return plotting.bar_monthly_rainfall_averages(self.monthly_rainfalls, label)

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

def bar_rainfall_linreg_slopes(self, monthly: Optional[bool] = True) -> list:
"""
Expand Down
14 changes: 10 additions & 4 deletions src/core/utils/functions/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,23 @@ def bar_column_according_to_year(yearly_rainfall: pd.DataFrame, label: Label) ->
return True


def bar_monthly_rainfall_averages(monthly_rainfalls: dict) -> list:
def bar_monthly_rainfall_averages(
monthly_rainfalls: dict, label: Optional[str] = "Average rainfall (mm)"
) -> 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)
: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())

plt.bar(month_labels, averages, label="Average rainfall (mm)")
plt.bar(month_labels, averages, label=label)
plt.legend()

return averages
Expand All @@ -124,20 +127,23 @@ def bar_monthly_rainfall_linreg_slopes(monthly_rainfalls: dict) -> list:
return slopes


def bar_seasonal_rainfall_averages(seasonal_rainfalls: dict) -> list:
def bar_seasonal_rainfall_averages(
seasonal_rainfalls: dict, label: Optional[str] = "Average rainfall (mm)"
) -> 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)
: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())

plt.bar(season_labels, averages, label="Average rainfall (mm)")
plt.bar(season_labels, averages, label=label)
plt.legend()

return averages
Expand Down

0 comments on commit 7ba1fbb

Please sign in to comment.