Skip to content

Commit

Permalink
Make begin_year argument always compulsory, use list of Rainfall mode…
Browse files Browse the repository at this point in the history
…ls directly instead of using dicts & remove useless type hints
  • Loading branch information
paul-florentin-charles committed Aug 12, 2024
1 parent 8cd8416 commit dd724ed
Show file tree
Hide file tree
Showing 16 changed files with 245 additions and 227 deletions.
8 changes: 7 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ def average_rainfall() -> Response:
to_return.update(
{
"name": "average rainfall (mm)",
"value": all_rainfall.get_average_rainfall(*params),
"value": all_rainfall.get_average_rainfall(
params[0],
begin_year=params[1],
end_year=params[2],
month=params[3],
season=params[4],
),
"begin_year": params[1],
"end_year": params[2] or all_rainfall.get_last_year(),
"time_mode": TimeMode[params[0]],
Expand Down
33 changes: 22 additions & 11 deletions src/core/models/all_rainfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def export_as_csv(
def get_average_rainfall(
self,
time_mode: str,
begin_year: int | None = None,
*,
begin_year: int,
end_year: int | None = None,
month: str | None = None,
season: str | None = None,
Expand All @@ -131,7 +132,7 @@ def get_average_rainfall(
:param time_mode: A string setting the time period ['YEARLY', 'MONTHLY', 'SEASONAL']
:param begin_year: An integer representing the year
to start getting our rainfall values (optional).
to start getting our rainfall values.
:param end_year: An integer representing the year
to end getting our rainfall values (optional).
:param month: A string corresponding to the month name.
Expand Down Expand Up @@ -322,29 +323,35 @@ def get_last_year(self) -> int:

def bar_rainfall_averages(
self,
monthly=True,
begin_year: int | None = None,
begin_year: int,
end_year: int | None = None,
monthly=True,
) -> 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. Defaults to True (optional).
:param begin_year: An integer representing the year
to start getting our rainfall values. (optional).
to start getting our rainfall values.
:param end_year: An integer representing the year
to end getting our rainfall values (optional).
:param monthly: If True, plots monthly rainfall averages.
If False, plots seasonal rainfall averages. Defaults to True (optional).
:return: A list of the Rainfall averages for each month or season.
"""
label = 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, begin_year, end_year
list(self.monthly_rainfalls.values()),
begin_year=begin_year,
end_year=end_year,
label=label,
)

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

def bar_rainfall_linreg_slopes(self, monthly=True) -> list:
Expand All @@ -356,9 +363,13 @@ def bar_rainfall_linreg_slopes(self, monthly=True) -> list:
:return: A list of the Rainfall LinReg slopes for each month or season.
"""
if monthly:
return plotting.bar_monthly_rainfall_linreg_slopes(self.monthly_rainfalls)
return plotting.bar_monthly_rainfall_linreg_slopes(
list(self.monthly_rainfalls.values())
)

return plotting.bar_seasonal_rainfall_linreg_slopes(self.seasonal_rainfalls)
return plotting.bar_seasonal_rainfall_linreg_slopes(
list(self.seasonal_rainfalls.values())
)

def get_entity_for_time_mode(
self, time_mode: str, month: str | None = None, season: str | None = None
Expand Down
12 changes: 7 additions & 5 deletions src/core/models/yearly_rainfall.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,22 @@ def load_rainfall(
)

def get_yearly_rainfall(
self, begin_year: int | None = None, end_year: int | None = None
self, begin_year: int, end_year: int | None = None
) -> pd.DataFrame:
"""
Retrieves Yearly Rainfall within a specific year range.
:param begin_year: An integer representing the year
to start getting our rainfall values (optional).
to start getting our rainfall values.
:param end_year: An integer representing the year
to end getting our rainfall values (optional).
:return: A pandas DataFrame displaying rainfall data (in mm)
for instance month according to year.
"""

return df_opr.get_rainfall_within_year_interval(self.data, begin_year, end_year)
return df_opr.get_rainfall_within_year_interval(
self.data, begin_year=begin_year, end_year=end_year
)

def export_as_csv(self, path: str | Path | None = None) -> str | None:
"""
Expand All @@ -107,13 +109,13 @@ def export_as_csv(self, path: str | Path | None = None) -> str | None:
return self.data.to_csv(path_or_buf=path, index=False)

def get_average_yearly_rainfall(
self, begin_year: int | None = None, end_year: int | None = None
self, begin_year: int, end_year: int | None = None
) -> float:
"""
Computes Rainfall average for a specific year range.
:param begin_year: An integer representing the year
to start getting our rainfall values (optional).
to start getting our rainfall values.
:param end_year: An integer representing the year
to end getting our rainfall values (optional).
:return: A float representing the average Rainfall.
Expand Down
12 changes: 4 additions & 8 deletions src/core/utils/functions/dataframe_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,25 @@

def get_rainfall_within_year_interval(
yearly_rainfall: pd.DataFrame,
begin_year: int | None = None,
*,
begin_year: int,
end_year: int | None = None,
) -> pd.DataFrame:
"""
Retrieves Yearly Rainfall within a specific year range.
:param yearly_rainfall: A pandas DataFrame displaying rainfall data (in mm) according to year.
:param begin_year: An integer representing the year
to start getting our rainfall values (optional).
to start getting our rainfall values.
:param end_year: An integer representing the year
to end getting our rainfall values (optional).
:return: A pandas DataFrame displaying rainfall data (in mm)
according to year.
"""
if begin_year is not None:
yearly_rainfall = yearly_rainfall[
yearly_rainfall[Label.YEAR.value] >= begin_year
]

if end_year is not None:
yearly_rainfall = yearly_rainfall[yearly_rainfall[Label.YEAR.value] <= end_year]

return yearly_rainfall
return yearly_rainfall[yearly_rainfall[Label.YEAR.value] >= begin_year]


def remove_column(yearly_rainfall: pd.DataFrame, label: Label) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/functions/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_normal(yearly_rainfall: pd.DataFrame, begin_year, round_precision=2) ->

return get_average_rainfall(
df_opr.get_rainfall_within_year_interval(
yearly_rainfall, begin_year, begin_year + 29
yearly_rainfall, begin_year=begin_year, end_year=begin_year + 29
),
round_precision,
)
38 changes: 20 additions & 18 deletions src/core/utils/functions/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,27 @@ def bar_column_according_to_year(yearly_rainfall: pd.DataFrame, label: Label) ->


def bar_monthly_rainfall_averages(
monthly_rainfalls: dict,
label="Average rainfall (mm)",
begin_year: int | None = None,
monthly_rainfalls: list,
*,
begin_year: int,
end_year: int | None = None,
label="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)
Defaults to "Average rainfall (mm)".
:param begin_year: An integer representing the year
to start getting our rainfall values (optional).
to start getting our rainfall values.
:param end_year: An integer representing the year
to end getting our rainfall values (optional).
:param label: A string to use as a label for bar graphic. (optional)
Defaults to "Average rainfall (mm)".
:return: A list of the Rainfall averages for each month.
"""
month_labels, averages = [], []
for monthly_rainfall in monthly_rainfalls.values():
for monthly_rainfall in monthly_rainfalls:
month_labels.append(monthly_rainfall.month.name[:3])
averages.append(
monthly_rainfall.get_average_yearly_rainfall(
Expand All @@ -119,7 +120,7 @@ def bar_monthly_rainfall_averages(
return averages


def bar_monthly_rainfall_linreg_slopes(monthly_rainfalls: dict) -> list:
def bar_monthly_rainfall_linreg_slopes(monthly_rainfalls: list) -> list:
"""
Plots a bar graphic displaying linear regression slope for each month passed through the dict.
Expand All @@ -128,7 +129,7 @@ def bar_monthly_rainfall_linreg_slopes(monthly_rainfalls: dict) -> list:
:return: A list of the Rainfall LinReg slopes for each month.
"""
month_labels, slopes = [], []
for monthly_rainfall in monthly_rainfalls.values():
for monthly_rainfall in monthly_rainfalls:
month_labels.append(monthly_rainfall.month.name[:3])
slopes.append(monthly_rainfall.add_linear_regression()[1])

Expand All @@ -139,26 +140,27 @@ def bar_monthly_rainfall_linreg_slopes(monthly_rainfalls: dict) -> list:


def bar_seasonal_rainfall_averages(
seasonal_rainfalls: dict,
label="Average rainfall (mm)",
begin_year: int | None = None,
seasonal_rainfalls: list,
*,
begin_year: int,
end_year: int | None = None,
label="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)
Defaults to "Average rainfall (mm)".
:param begin_year: An integer representing the year
to start getting our rainfall values (optional).
to start getting our rainfall values.
:param end_year: An integer representing the year
to end getting our rainfall values (optional).
:param label: A string to use as a label for bar graphic. (optional)
Defaults to "Average rainfall (mm)".
:return: A list of the Rainfall averages for each season.
"""
season_labels, averages = [], []
for seasonal_rainfall in seasonal_rainfalls.values():
for seasonal_rainfall in seasonal_rainfalls:
season_labels.append(seasonal_rainfall.season.name)
averages.append(
seasonal_rainfall.get_average_yearly_rainfall(
Expand All @@ -172,7 +174,7 @@ def bar_seasonal_rainfall_averages(
return averages


def bar_seasonal_rainfall_linreg_slopes(seasonal_rainfalls: dict) -> list:
def bar_seasonal_rainfall_linreg_slopes(seasonal_rainfalls: list) -> list:
"""
Plots a bar graphic displaying linear regression slope for each season passed through the dict.
Expand All @@ -181,7 +183,7 @@ def bar_seasonal_rainfall_linreg_slopes(seasonal_rainfalls: dict) -> list:
:return: A list of the Rainfall LinReg slopes for each season.
"""
season_labels, slopes = [], []
for seasonal_rainfall in seasonal_rainfalls.values():
for seasonal_rainfall in seasonal_rainfalls:
season_labels.append(seasonal_rainfall.season.name)
slopes.append(seasonal_rainfall.add_linear_regression()[1])

Expand Down
Loading

0 comments on commit dd724ed

Please sign in to comment.