Skip to content

Commit

Permalink
Apply ruff format
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Barrachina Civera <[email protected]>
  • Loading branch information
xbarra committed Aug 2, 2024
1 parent 61089d0 commit 580bf84
Show file tree
Hide file tree
Showing 59 changed files with 2,917 additions and 801 deletions.
6 changes: 5 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@
autosummary_generate = True

# Docstrings of private methods
autodoc_default_options = {"members": True, "undoc-members": True, "private-members": False}
autodoc_default_options = {
"members": True,
"undoc-members": True,
"private-members": False,
}

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
Expand Down
45 changes: 33 additions & 12 deletions src/physrisk/api/v1/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ class Asset(BaseModel, extra="allow"):
)
latitude: float = Field(description="Latitude in degrees")
longitude: float = Field(description="Longitude in degrees")
type: Optional[str] = Field(None, description="Type of the asset <level_1>/<level_2>/<level_3>")
type: Optional[str] = Field(
None, description="Type of the asset <level_1>/<level_2>/<level_3>"
)
location: Optional[str] = Field(
None, description="Location (e.g. Africa, Asia, Europe, Global, Oceania, North America, South America)"
None,
description="Location (e.g. Africa, Asia, Europe, Global, Oceania, North America, South America)",
)
capacity: Optional[float] = Field(None, description="Power generation capacity")
attributes: Optional[Dict[str, str]] = Field(
None, description="Bespoke attributes (e.g. number of storeys, structure type, occupancy type)"
None,
description="Bespoke attributes (e.g. number of storeys, structure type, occupancy type)",
)


Expand Down Expand Up @@ -81,7 +85,8 @@ class IntensityCurve(BaseModel):

intensities: List[float] = Field([], description="Hazard indicator intensities.")
return_periods: Optional[List[float]] = Field(
[], description="[Deprecated] Return period in years in the case of an acute hazard."
[],
description="[Deprecated] Return period in years in the case of an acute hazard.",
)
index_values: Optional[Union[List[float], List[str]]] = Field(
[],
Expand All @@ -100,7 +105,9 @@ class ExceedanceCurve(BaseModel):
"""General exceedance curve (e.g. hazazrd, impact)."""

values: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
exceed_probabilities: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
exceed_probabilities: np.ndarray = Field(
default_factory=lambda: np.zeros(10), description=""
)

class Config:
arbitrary_types_allowed = True
Expand All @@ -110,7 +117,9 @@ class Distribution(BaseModel):
"""General exceedance curve (e.g. hazazrd, impact)."""

bin_edges: np.ndarray = Field(default_factory=lambda: np.zeros(11), description="")
probabilities: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
probabilities: np.ndarray = Field(
default_factory=lambda: np.zeros(10), description=""
)

class Config:
arbitrary_types_allowed = True
Expand All @@ -119,8 +128,12 @@ class Config:
class HazardEventDistrib(BaseModel):
"""Intensity curve of an acute hazard."""

intensity_bin_edges: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
probabilities: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
intensity_bin_edges: np.ndarray = Field(
default_factory=lambda: np.zeros(10), description=""
)
probabilities: np.ndarray = Field(
default_factory=lambda: np.zeros(10), description=""
)
path: List[str] = Field([], description="Path to the hazard indicator data source.")

class Config:
Expand All @@ -139,7 +152,9 @@ class VulnerabilityCurve(BaseModel):
intensity: List[float] = Field(...)
intensity_units: str = Field(description="units of the intensity")
impact_mean: List[float] = Field(description="mean impact (damage or disruption)")
impact_std: List[float] = Field(description="standard deviation of impact (damage or disruption)")
impact_std: List[float] = Field(
description="standard deviation of impact (damage or disruption)"
)

class Config:
arbitrary_types_allowed = True
Expand All @@ -154,9 +169,15 @@ class VulnerabilityCurves(BaseModel):
class VulnerabilityDistrib(BaseModel):
"""Defines a vulnerability matrix."""

intensity_bin_edges: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
impact_bin_edges: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
prob_matrix: np.ndarray = Field(default_factory=lambda: np.zeros(10), description="")
intensity_bin_edges: np.ndarray = Field(
default_factory=lambda: np.zeros(10), description=""
)
impact_bin_edges: np.ndarray = Field(
default_factory=lambda: np.zeros(10), description=""
)
prob_matrix: np.ndarray = Field(
default_factory=lambda: np.zeros(10), description=""
)

class Config:
arbitrary_types_allowed = True
11 changes: 8 additions & 3 deletions src/physrisk/api/v1/exposure_req_resp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class AssetExposureRequest(BaseModel):

assets: Assets
calc_settings: CalcSettings = Field(
default_factory=CalcSettings, description="Interpolation method." # type:ignore
default_factory=CalcSettings,
description="Interpolation method.", # type:ignore
)
scenario: str = Field("rcp8p5", description="Name of scenario ('rcp8p5')")
year: int = Field(
Expand All @@ -29,7 +30,9 @@ class AssetExposureRequest(BaseModel):
class Exposure(BaseModel):
category: str
value: Optional[float]
path: str = Field("unknown", description="Path to the hazard indicator data source.")
path: str = Field(
"unknown", description="Path to the hazard indicator data source."
)


class AssetExposure(BaseModel):
Expand All @@ -40,7 +43,9 @@ class AssetExposure(BaseModel):
description="""Asset identifier; will appear if provided in the request
otherwise order of assets in response is identical to order of assets in request.""",
)
exposures: Dict[str, Exposure] = Field({}, description="Category (value) for each hazard type (key).")
exposures: Dict[str, Exposure] = Field(
{}, description="Category (value) for each hazard type (key)."
)


class AssetExposureResponse(BaseModel):
Expand Down
55 changes: 42 additions & 13 deletions src/physrisk/api/v1/hazard_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ class Colormap(BaseModel):
"""Provides details of colormap."""

min_index: Optional[int] = Field(
1, description="Value of colormap minimum. Constant min for a group of maps can facilitate comparison."
1,
description="Value of colormap minimum. Constant min for a group of maps can facilitate comparison.",
)
min_value: float = Field(
description="Value of colormap minimum. Constant min for a group of maps can facilitate comparison."
)
max_index: Optional[int] = Field(
255, description="Value of colormap maximum. Constant max for a group of maps can facilitate comparison."
255,
description="Value of colormap maximum. Constant max for a group of maps can facilitate comparison.",
)
max_value: float = Field(
description="Value of colormap maximum. Constant max for a group of maps can facilitate comparison."
Expand Down Expand Up @@ -51,7 +53,9 @@ class Period(BaseModel):
"""Provides information about a period, which currently corresponds to a year, belonging to a scenario."""

year: int
map_id: str = Field(description="If present, identifier to be used for looking up map tiles from server.")
map_id: str = Field(
description="If present, identifier to be used for looking up map tiles from server."
)


class Scenario(BaseModel):
Expand All @@ -70,7 +74,10 @@ class HazardResource(BaseModel):
"""Provides information about a set of hazard indicators, including available scenarios and years."""

hazard_type: str = Field(description="Type of hazard.")
group_id: Optional[str] = Field("public", description="Identifier of the resource group (used for authentication).")
group_id: Optional[str] = Field(
"public",
description="Identifier of the resource group (used for authentication).",
)
path: str = Field(description="Full path to the indicator array.")
indicator_id: str = Field(
description="Identifier of the hazard indicator (i.e. the modelled quantity), e.g. 'flood_depth'."
Expand All @@ -83,14 +90,23 @@ class HazardResource(BaseModel):
indicator_model_gcm: str = Field(
description="Identifier of general circulation model(s) used in the derivation of the indicator."
)
params: Dict[str, List[str]] = Field({}, description="Parameters used to expand wild-carded fields.")
params: Dict[str, List[str]] = Field(
{}, description="Parameters used to expand wild-carded fields."
)
display_name: str = Field(description="Text used to display indicator.")
display_groups: List[str] = Field([], description="Text used to group the (expanded) indicators for display.")
display_groups: List[str] = Field(
[], description="Text used to group the (expanded) indicators for display."
)
description: str = Field(
description="Brief description in mark down of the indicator and model that generated the indicator."
)
map: Optional[MapInfo] = Field(None, description="Optional information used for display of the indicator in a map.")
scenarios: List[Scenario] = Field(description="Climate change scenarios for which the indicator is available.")
map: Optional[MapInfo] = Field(
None,
description="Optional information used for display of the indicator in a map.",
)
scenarios: List[Scenario] = Field(
description="Climate change scenarios for which the indicator is available."
)
units: str = Field(description="Units of the hazard indicator.")

def expand(self):
Expand Down Expand Up @@ -122,7 +138,9 @@ def expand_resource(
deep=True,
update={
"indicator_id": expand(item.indicator_id, key, param),
"indicator_model_gcm": expand(item.indicator_model_gcm, key, param),
"indicator_model_gcm": expand(
item.indicator_model_gcm, key, param
),
"display_name": expand(item.display_name, key, param),
"path": expand(item.path, key, param),
"map": (
Expand All @@ -132,7 +150,13 @@ def expand_resource(
item.map.model_copy(
deep=True,
update={
"path": expand(item.map.path if item.map.path is not None else "", key, param)
"path": expand(
item.map.path
if item.map.path is not None
else "",
key,
param,
)
},
)
)
Expand All @@ -154,7 +178,8 @@ class InventorySource(Flag):
class HazardAvailabilityRequest(BaseModel):
types: Optional[List[str]] = [] # e.g. ["RiverineInundation"]
sources: Optional[List[str]] = Field(
None, description="Sources of inventory, can be 'embedded', 'hazard' or 'hazard_test'."
None,
description="Sources of inventory, can be 'embedded', 'hazard' or 'hazard_test'.",
)


Expand All @@ -168,15 +193,19 @@ class HazardDescriptionRequest(BaseModel):


class HazardDescriptionResponse(BaseModel):
descriptions: Dict[str, str] = Field(description="For each path (key), the description markdown (value).")
descriptions: Dict[str, str] = Field(
description="For each path (key), the description markdown (value)."
)


class HazardDataRequestItem(BaseModel):
longitudes: List[float]
latitudes: List[float]
request_item_id: str
hazard_type: Optional[str] = None # e.g. RiverineInundation
event_type: Optional[str] = None # e.g. RiverineInundation; deprecated: use hazard_type
event_type: Optional[str] = (
None # e.g. RiverineInundation; deprecated: use hazard_type
)
indicator_id: str
indicator_model_gcm: Optional[str] = ""
path: Optional[str] = None
Expand Down
4 changes: 3 additions & 1 deletion src/physrisk/api/v1/hazard_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class Tile(NamedTuple):


class HazardImageRequest(BaseHazardRequest):
resource: str = Field(description="Full path to the array; formed by '{path}/{id}'.")
resource: str = Field(
description="Full path to the array; formed by '{path}/{id}'."
)
scenario_id: str
year: int
colormap: Optional[str] = Field("heating")
Expand Down
Loading

0 comments on commit 580bf84

Please sign in to comment.