Skip to content

Commit

Permalink
PEP8
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuel-schmid committed Dec 6, 2023
1 parent 2880bcc commit 8cc88e7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
1 change: 0 additions & 1 deletion climada/engine/test/test_cost_benefit.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from climada.test import get_test_file


HAZ_TEST_MAT = get_test_file('atl_prob_no_name')
ENT_TEST_MAT = get_test_file('demo_today', file_format='MAT-file')
HAZ_TEST_TC :Path = get_test_file('test_tc_florida')

Expand Down
52 changes: 26 additions & 26 deletions climada/hazard/centroids/centr.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import copy
import logging
from pathlib import Path
from typing import Optional, Dict, Any
from typing import Optional
import warnings

import h5py
Expand All @@ -32,16 +32,14 @@
import numpy as np
import pandas as pd
from pyproj.crs.crs import CRS
from rasterio.warp import Resampling
import rasterio
from shapely.geometry.point import Point

from climada.util.constants import (DEF_CRS,
ONE_LAT_KM,
NATEARTH_CENTROIDS)
from climada.util.constants import DEF_CRS
import climada.util.coordinates as u_coord
import climada.util.hdf5_handler as u_hdf5
import climada.util.plot as u_plot


__all__ = ['Centroids']

PROJ_CEA = CRS.from_user_input({'proj': 'cea'})
Expand Down Expand Up @@ -253,7 +251,8 @@ def from_geodataframe(cls, gdf):
longitude=gdf.geometry.x.values,
latitude=gdf.geometry.y.values,
crs=gdf.crs,
**gdf.drop(columns=['geometry', 'latitude', 'longitude'], errors='ignore').to_dict(orient='list')
**gdf.drop(columns=['geometry', 'latitude', 'longitude'],
errors='ignore').to_dict(orient='list')
)

@classmethod
Expand Down Expand Up @@ -344,7 +343,7 @@ def union(self, *others):
# remove duplicate points
return Centroids.remove_duplicate_points(centroids)

def get_closest_point(self, x_lon, y_lat, scheduler=None):
def get_closest_point(self, x_lon, y_lat):
"""Returns closest centroid and its index to a given point.
Parameters
Expand All @@ -353,8 +352,6 @@ def get_closest_point(self, x_lon, y_lat, scheduler=None):
x coord (lon)
y_lat : float
y coord (lat)
scheduler : str
used for dask map_partitions. “threads”, “synchronous” or “processes”
Returns
-------
Expand Down Expand Up @@ -526,7 +523,7 @@ def select_mask(self, sel_cen=None, reg_id=None, extent=None):
return sel_cen

#TODO replace with nice Geodataframe util plot method.

Check warning on line 525 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

fixme

NORMAL: TODO replace with nice Geodataframe util plot method.
Raw output
no description found
def plot(self, ax=None, figsize=(9, 13), **kwargs):
def plot(self, ax=None, figsize=(9, 13), latlon_bounds_buffer=0.0, **kwargs):

Check warning on line 526 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Argument name "ax" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
Raw output
Used when the name doesn't match the regular expression associated to its type(constant, variable, class...).
"""Plot centroids scatter points over earth.
Parameters
Expand All @@ -536,6 +533,8 @@ def plot(self, ax=None, figsize=(9, 13), **kwargs):
figsize: (float, float), optional
figure size for plt.subplots
The default is (9, 13)
latlon_bounds_buffer : float, optional
Buffer to add to all sides of the bounding box. Default: 0.0.
kwargs : optional
arguments for scatter matplotlib function
Expand All @@ -547,7 +546,8 @@ def plot(self, ax=None, figsize=(9, 13), **kwargs):
proj_plot = proj_data
if isinstance(proj_data, ccrs.PlateCarree):
# use different projections for plot and data to shift the central lon in the plot
xmin, ymin, xmax, ymax = u_coord.latlon_bounds(self.lat, self.lon, buffer=pad)
xmin, _ymin, xmax, _ymax = u_coord.latlon_bounds(self.lat, self.lon,
buffer=latlon_bounds_buffer)
proj_plot = ccrs.PlateCarree(central_longitude=0.5 * (xmin + xmax))

if ax is None:
Expand Down Expand Up @@ -592,9 +592,9 @@ def get_area_pixel(self, min_resol=1.0e-8):
'''

@classmethod
def from_raster_file(cls, file_name, src_crs=None, window=None,
geometry=None, dst_crs=None, transform=None, width=None,
height=None, resampling=Resampling.nearest, return_meta=False):
def from_raster_file(cls, file_name, src_crs=None, window=None, geometry=None,

Check warning on line 595 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

too-many-arguments

LOW: Too many arguments (11/7)
Raw output
Used when a function or method takes too many arguments.
dst_crs=None, transform=None, width=None, height=None,
resampling=rasterio.warp.Resampling.nearest, return_meta=False):
"""Create a new Centroids object from a raster file
Select region using window or geometry. Reproject input by providing
Expand All @@ -618,8 +618,9 @@ def from_raster_file(cls, file_name, src_crs=None, window=None,
number of lons for transform
height : float
number of lats for transform
resampling : rasterio.warp,.Resampling optional
resampling function used for reprojection to dst_crs
resampling : rasterio.warp.Resampling optional
resampling function used for reprojection to dst_crs,
default: nearest
Returns
-------
Expand Down Expand Up @@ -736,16 +737,12 @@ def from_excel(cls, file_name, crs=None, var_names=None):

try:
df = pd.read_excel(file_name, var_names['sheet_name'])

Check warning on line 739 in climada/hazard/centroids/centr.py

View check run for this annotation

Jenkins - WCR / Pylint

invalid-name

LOW: Variable name "df" doesn't conform to '(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$' pattern
Raw output
Used when the name doesn't match the regular expression associated to its type(constant, variable, class...).
try:
region_id = df[var_names['col_name']['region_id']]
except KeyError:
region_id = None
pass

except KeyError as err:
raise KeyError("Not existing variable: %s" % str(err)) from err
raise KeyError(f"Not existing variable: {err}") from err

geometry = gpd.points_from_xy(df[var_names['col_name']['lon']], df[var_names['col_name']['lat']])
geometry = gpd.points_from_xy(df[var_names['col_name']['lon']],
df[var_names['col_name']['lat']])
gdf = gpd.GeoDataFrame(df, crs=crs, geometry=geometry)
return cls.from_geodataframe(gdf)

Expand All @@ -770,7 +767,9 @@ def write_hdf5(self, file_name, mode='w'):
# Write dataframe
store.put('centroids', pandas_df)

store.get_storer('centroids').attrs.metadata = {'crs': CRS.from_user_input(self.crs).to_wkt()}
store.get_storer('centroids').attrs.metadata = {
'crs': CRS.from_user_input(self.crs).to_wkt()
}

store.close()

Expand All @@ -794,7 +793,8 @@ def from_hdf5(cls, file_name):
try:
with pd.HDFStore(file_name, mode='r') as store:
metadata = store.get_storer('centroids').attrs.metadata
# in previous versions of CLIMADA and/or geopandas, the CRS was stored in '_crs'/'crs'
# in previous versions of CLIMADA and/or geopandas,
# the CRS was stored in '_crs'/'crs'
crs = metadata.get('crs')
gdf = gpd.GeoDataFrame(store['centroids'], crs=crs)
except TypeError:
Expand Down

0 comments on commit 8cc88e7

Please sign in to comment.