Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling multiple weatherforecastlocations selection #146

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

maxi-fort
Copy link
Collaborator

The evolution enables multiple weatherforecastlocations selection.

Default is one weatherforecastlocation (the nearest) so it does not impact the original behaviour of the code.

@maxi-fort maxi-fort linked an issue Apr 26, 2024 that may be closed by this pull request
Signed-off-by: fortinmax <maxime.fortin@rte-france.com>
@maxi-fort maxi-fort force-pushed the 145-extract-multiple-nearest-weather-locations-instead-of-only-1 branch from e9a2c72 to b91a37e Compare April 26, 2024 15:37
maxi-fort and others added 4 commits April 30, 2024 11:03
Signed-off-by: fortinmax <maxime.fortin@rte-france.com>
Signed-off-by: black <action@github.com>
Signed-off-by: fortinmax <maxime.fortin@rte-france.com>
Signed-off-by: black <action@github.com>
@@ -55,6 +55,7 @@ def _get_nearest_weather_location(
location: Union[Tuple[float, float], str],
country: str = "NL",
threshold: float = 150.0,
k: int = 1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this parameter name more descriptive? E.g. number_locations?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, il will change it to number_locations

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @clara-de-smet,
the parameter k has been renamed to number_locations :

def _get_nearest_weather_locations(
self,
location: Union[Tuple[float, float], str],
country: str = "NL",
threshold: float = 150.0,
number_locations: int = 1,
) -> pd.Series:

Copy link
Collaborator

@JanMaartenvanDoorn JanMaartenvanDoorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this feature! See the comments for things that I found. In general it is probably worth to change the output from _get_nearest_weather_locations to a list of strings instead of supporting both a single string and a list of strings. Contact me via teams or email if you have any questions or want to discuss a little.


# Find closest weather location
if distances.index.min() < threshold:
if isinstance(nearest_location, pd.Series):
return nearest_location.reset_index(drop=True)[0]
return nearest_location.reset_index(drop=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output of this function should be string (name of the weather location).

I see in line 108 nearest weather location is created as a list, the if statement in line 112 checks if nearest location is a pd.Series which will never be the case. Probably you do not want to convert the pd.Series to a list in line 108.

If you want to return mulitple weather locations,I think it is good to update the output type hints and documentation that a list of strings is returned instead of just a string. Maybe also change the name "nearest_location" into "nearest_locations"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.
I will apply modifications so that the output is a list of str

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @JanMaartenvanDoorn,

I changed the class of the object returned :
It is now a dataframe with one column "input_city" containing the name of the nearest(s) weather location(s), and the distance (in km) as index.

Such information my be of interest for someone who desire to compute spatial weighted averaging of weather indices (which is RTE's case).

Therefore, the 'list' of input_city is extracted from this dataframe just before db query :

# Initialise strings for the querying influx, it is not possible to parameterize this string
weather_params_str = '" or r._field == "'.join(weatherparams)
weather_models_str = '" or r.source == "'.join(source)
weather_location_name_str = '" or r.input_city == "'.join(
location_name.to_list()
)

@@ -215,6 +217,7 @@ def get_weather_data(
source: Union[List[str], str] = "optimum",
resolution: str = "15min",
country: str = "NL",
k: int = 1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as @clara-de-smet mentioned this should be more descriptive e.g. max_number_of_weather_locations

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will use @clara-de-smet suggestion

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JanMaartenvanDoorn , k has been renamed to number_locations


# Make a list of the source and weatherparams.
# Like this, it also works if source is a string instead of multiple values
if isinstance(source, str):
source = [source]
if isinstance(weatherparams, str):
weatherparams = [weatherparams]
if isinstance(location_name, str):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we decide we only return a list of strings in _get_nearest_weather_location, we do not have to do this conversion and check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing this

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to add unit tests!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added a 2 unit tests in test_weather.py :

def test_get_multiple_location_weather_data(self, MockDataInterface):

def test_get_single_location_weather_data(self, MockDataInterface):

result = result.loc[datetime_start_original:]
result = result[result.index >= datetime_start_original]

if k == 1:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is another k which is not so descriptive

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This have been changed.
Plus, I added a merge with location_name dataframe to add "distiance" column to the returned dataframe if number_locations > 1

if number_locations == 1:
result = result.drop(columns="input_city")
else:
# Adding distance information en km
result = (
result.reset_index()
.merge(location_name.reset_index(), how="left", on="input_city")
.set_index("datetime")
)
return result

openstef_dbc/services/weather.py Outdated Show resolved Hide resolved
openstef_dbc/services/weather.py Outdated Show resolved Hide resolved
maxi-fort and others added 4 commits May 24, 2024 11:02
Signed-off-by: fortinmax <maxime.fortin@rte-france.com>
Signed-off-by: fortinmax <maxime.fortin@rte-france.com>
Signed-off-by: fortinmax <maxime.fortin@rte-france.com>
Signed-off-by: black <action@github.com>
Copy link
Collaborator

@JanMaartenvanDoorn JanMaartenvanDoorn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks for implementing these changes!

maxi-fort and others added 4 commits July 2, 2024 18:59
Signed-off-by: fortinmax <maxime.fortin@rte-france.com>
…of-only-1' of https://github.com/maxi-fort/openstef-dbc into 145-extract-multiple-nearest-weather-locations-instead-of-only-1
Signed-off-by: fortinmax <maxime.fortin@rte-france.com>
Signed-off-by: black <action@github.com>
@maxi-fort
Copy link
Collaborator Author

maxi-fort commented Jul 5, 2024

@JanMaartenvanDoorn and @clara-de-smet ,
Your comment has been taken into account but I modified a few things since, like :

  • keeping distance in get_nearest_weather_locations
  • adding additionnal columns in get_weather_data output
    Could you review this PR once again and mark conversations as "resolved" when it is ok ?
    Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extract multiple nearest weather locationS instead of only 1
4 participants