From 37862091714fa7e1bee03248b4da527865c9689c Mon Sep 17 00:00:00 2001 From: Sean Freeman Date: Wed, 13 Sep 2023 13:12:04 -0500 Subject: [PATCH 1/9] adding internal utils changes --- tobac/utils/internal/__init__.py | 1 + .../general_internal.py} | 203 ++++++------------ tobac/utils/internal/internal_consts.py | 6 + tobac/utils/internal/iris_utils.py | 156 ++++++++++++++ tobac/utils/internal/xarray_utils.py | 50 +++++ 5 files changed, 283 insertions(+), 133 deletions(-) create mode 100644 tobac/utils/internal/__init__.py rename tobac/utils/{internal.py => internal/general_internal.py} (78%) create mode 100644 tobac/utils/internal/internal_consts.py create mode 100644 tobac/utils/internal/iris_utils.py create mode 100644 tobac/utils/internal/xarray_utils.py diff --git a/tobac/utils/internal/__init__.py b/tobac/utils/internal/__init__.py new file mode 100644 index 00000000..463d824c --- /dev/null +++ b/tobac/utils/internal/__init__.py @@ -0,0 +1 @@ +from .general_internal import * diff --git a/tobac/utils/internal.py b/tobac/utils/internal/general_internal.py similarity index 78% rename from tobac/utils/internal.py rename to tobac/utils/internal/general_internal.py index 1354504f..271cccaa 100644 --- a/tobac/utils/internal.py +++ b/tobac/utils/internal/general_internal.py @@ -1,10 +1,25 @@ """Internal tobac utilities """ +from __future__ import annotations + import numpy as np import skimage.measure import xarray as xr import iris +import iris.cube +import pandas as pd import warnings +from . import iris_utils +from . import xarray_utils as xr_utils +from typing import Union, Callable + +# list of common vertical coordinates to search for in various functions +COMMON_VERT_COORDS: list[str] = [ + "z", + "model_level_number", + "altitude", + "geopotential_height", +] def _warn_auto_coordinate(): @@ -17,7 +32,7 @@ def _warn_auto_coordinate(): ) -def get_label_props_in_dict(labels): +def get_label_props_in_dict(labels: np.array) -> dict: """Function to get the label properties into a dictionary format. Parameters @@ -40,7 +55,7 @@ def get_label_props_in_dict(labels): return region_properties_dict -def get_indices_of_labels_from_reg_prop_dict(region_property_dict): +def get_indices_of_labels_from_reg_prop_dict(region_property_dict: dict) -> tuple[dict]: """Function to get the x, y, and z indices (as well as point count) of all labeled regions. Parameters ---------- @@ -94,7 +109,7 @@ def get_indices_of_labels_from_reg_prop_dict(region_property_dict): return [curr_loc_indices, y_indices, x_indices] -def iris_to_xarray(func): +def iris_to_xarray(func: Callable) -> Callable: """Decorator that converts all input of a function that is in the form of Iris cubes into xarray DataArrays and converts all outputs with type xarray DataArrays back into Iris cubes. @@ -160,7 +175,7 @@ def wrapper(*args, **kwargs): return wrapper -def xarray_to_iris(func): +def xarray_to_iris(func: Callable) -> Callable: """Decorator that converts all input of a function that is in the form of xarray DataArrays into Iris cubes and converts all outputs with type Iris cubes back into xarray DataArrays. @@ -244,7 +259,7 @@ def wrapper(*args, **kwargs): return wrapper -def irispandas_to_xarray(func): +def irispandas_to_xarray(func: Callable) -> Callable: """Decorator that converts all input of a function that is in the form of Iris cubes/pandas Dataframes into xarray DataArrays/xarray Datasets and converts all outputs with the type xarray DataArray/xarray Dataset @@ -324,7 +339,7 @@ def wrapper(*args, **kwargs): return wrapper -def xarray_to_irispandas(func): +def xarray_to_irispandas(func: Callable) -> Callable: """Decorator that converts all input of a function that is in the form of DataArrays/xarray Datasets into xarray Iris cubes/pandas Dataframes and converts all outputs with the type Iris cubes/pandas Dataframes back into @@ -427,7 +442,7 @@ def wrapper(*args, **kwargs): return wrapper -def njit_if_available(func, **kwargs): +def njit_if_available(func: Callable, **kwargs) -> Callable: """Decorator to wrap a function with numba.njit if available. If numba isn't available, it just returns the function. @@ -446,12 +461,15 @@ def njit_if_available(func, **kwargs): return func -def find_vertical_axis_from_coord(variable_cube, vertical_coord=None): +def find_vertical_axis_from_coord( + variable_cube: Union[iris.cube.Cube, xr.DataArray], + vertical_coord: Union[str, None] = None, +) -> str: """Function to find the vertical coordinate in the iris cube Parameters ---------- - variable_cube: iris.cube + variable_cube: iris.cube.Cube or xarray.DataArray Input variable cube, containing a vertical coordinate. vertical_coord: str Vertical coordinate name. If None, this function tries to auto-detect. @@ -466,69 +484,21 @@ def find_vertical_axis_from_coord(variable_cube, vertical_coord=None): ValueError Raised if the vertical coordinate isn't found in the cube. """ - list_vertical = [ - "z", - "model_level_number", - "altitude", - "geopotential_height", - ] if vertical_coord == "auto": _warn_auto_coordinate() if isinstance(variable_cube, iris.cube.Cube): - list_coord_names = [coord.name() for coord in variable_cube.coords()] - elif isinstance(variable_cube, xr.Dataset) or isinstance( - variable_cube, xr.DataArray - ): - list_coord_names = variable_cube.coords - - if vertical_coord is None or vertical_coord == "auto": - # find the intersection - all_vertical_axes = list(set(list_coord_names) & set(list_vertical)) - if len(all_vertical_axes) >= 1: - return all_vertical_axes[0] - else: - raise ValueError( - "Cube lacks suitable automatic vertical coordinate (z, model_level_number, altitude, or geopotential_height)" - ) - elif vertical_coord in list_coord_names: - return vertical_coord - else: - raise ValueError("Please specify vertical coordinate found in cube") - - -def find_axis_from_coord(variable_cube, coord_name): - """Finds the axis number in an iris cube given a coordinate name. - - Parameters - ---------- - variable_cube: iris.cube - Input variable cube - coord_name: str - coordinate to look for + return iris_utils.find_vertical_axis_from_coord(variable_cube, vertical_coord) + if isinstance(variable_cube, xr.Dataset) or isinstance(variable_cube, xr.DataArray): + return xr_utils.find_vertical_axis_from_coord(variable_cube, vertical_coord) - Returns - ------- - axis_number: int - the number of the axis of the given coordinate, or None if the coordinate - is not found in the cube or not a dimensional coordinate - """ - - list_coord_names = [coord.name() for coord in variable_cube.coords()] - all_matching_axes = list(set(list_coord_names) & set((coord_name,))) - if ( - len(all_matching_axes) == 1 - and len(variable_cube.coord_dims(all_matching_axes[0])) > 0 - ): - return variable_cube.coord_dims(all_matching_axes[0])[0] - elif len(all_matching_axes) > 1: - raise ValueError("Too many axes matched.") - else: - return None + raise ValueError("variable_cube must be xr.DataArray or iris.cube.Cube") -def find_dataframe_vertical_coord(variable_dataframe, vertical_coord=None): +def find_dataframe_vertical_coord( + variable_dataframe: pd.DataFrame, vertical_coord: Union[str, None] = None +) -> str: """Function to find the vertical coordinate in the iris cube Parameters @@ -553,8 +523,9 @@ def find_dataframe_vertical_coord(variable_dataframe, vertical_coord=None): _warn_auto_coordinate() if vertical_coord is None or vertical_coord == "auto": - list_vertical = ["z", "model_level_number", "altitude", "geopotential_height"] - all_vertical_axes = list(set(variable_dataframe.columns) & set(list_vertical)) + all_vertical_axes = list( + set(variable_dataframe.columns) & set(COMMON_VERT_COORDS) + ) if len(all_vertical_axes) == 1: return all_vertical_axes[0] else: @@ -568,7 +539,7 @@ def find_dataframe_vertical_coord(variable_dataframe, vertical_coord=None): @njit_if_available -def calc_distance_coords(coords_1, coords_2): +def calc_distance_coords(coords_1: np.array, coords_2: np.array) -> float: """Function to calculate the distance between cartesian coordinate set 1 and coordinate set 2. Parameters @@ -595,13 +566,17 @@ def calc_distance_coords(coords_1, coords_2): return np.sqrt(np.sum(deltas**2)) -def find_hdim_axes_3D(field_in, vertical_coord=None, vertical_axis=None): +def find_hdim_axes_3D( + field_in: Union[iris.cube.Cube, xr.DataArray], + vertical_coord: Union[str, None] = None, + vertical_axis: Union[int, None] = None, +) -> tuple[int]: """Finds what the hdim axes are given a 3D (including z) or 4D (including z and time) dataset. Parameters ---------- - field_in: iris cube or xarray dataset + field_in: iris cube or xarray dataarray Input field, can be 3D or 4D vertical_coord: str The name of the vertical coord, or None, which will attempt to find @@ -616,7 +591,6 @@ def find_hdim_axes_3D(field_in, vertical_coord=None, vertical_axis=None): The axes for hdim_1 and hdim_2 """ - from iris import cube as iris_cube if vertical_coord == "auto": _warn_auto_coordinate() @@ -625,91 +599,54 @@ def find_hdim_axes_3D(field_in, vertical_coord=None, vertical_axis=None): if vertical_coord != "auto": raise ValueError("Cannot set both vertical_coord and vertical_axis.") - if type(field_in) is iris_cube.Cube: - return find_hdim_axes_3D_iris(field_in, vertical_coord, vertical_axis) + if type(field_in) is iris.cube.Cube: + return iris_utils.find_hdim_axes_3d(field_in, vertical_coord, vertical_axis) elif type(field_in) is xr.DataArray: raise NotImplementedError("Xarray find_hdim_axes_3D not implemented") else: raise ValueError("Unknown data type: " + type(field_in).__name__) -def find_hdim_axes_3D_iris(field_in, vertical_coord=None, vertical_axis=None): - """Finds what the hdim axes are given a 3D (including z) or - 4D (including z and time) dataset. +def find_axis_from_coord( + variable_arr: Union[iris.cube.Cube, xr.DataArray], coord_name: str +) -> int: + """Finds the axis number in an xarray or iris cube given a coordinate or dimension name. Parameters ---------- - field_in: iris cube - Input field, can be 3D or 4D - vertical_coord: str or None - The name of the vertical coord, or None, which will attempt to find - the vertical coordinate name - vertical_axis: int or None - The axis number of the vertical coordinate, or None. Note - that only one of vertical_axis or vertical_coord can be set. + variable_arr: iris.cube.Cube or xarray.DataArray + Input variable cube + coord_name: str + coordinate or dimension to look for Returns ------- - (hdim_1_axis, hdim_2_axis): (int, int) - The axes for hdim_1 and hdim_2 + axis_number: int + the number of the axis of the given coordinate, or None if the coordinate + is not found in the variable or not a dimensional coordinate """ - if vertical_coord == "auto": - _warn_auto_coordinate() - - if vertical_coord is not None and vertical_axis is not None: - if vertical_coord != "auto": - raise ValueError("Cannot set both vertical_coord and vertical_axis.") - - time_axis = find_axis_from_coord(field_in, "time") - if vertical_axis is not None: - vertical_coord_axis = vertical_axis - vert_coord_found = True + if isinstance(variable_arr, iris.cube.Cube): + return iris_utils.find_axis_from_coord(variable_arr, coord_name) + elif isinstance(variable_arr, xr.DataArray): + raise NotImplementedError( + "xarray version of find_axis_from_coord not implemented." + ) else: - try: - vertical_axis = find_vertical_axis_from_coord( - field_in, vertical_coord=vertical_coord - ) - except ValueError: - vert_coord_found = False - else: - vert_coord_found = True - ndim_vertical = field_in.coord_dims(vertical_axis) - if len(ndim_vertical) > 1: - raise ValueError( - "please specify 1 dimensional vertical coordinate." - " Current vertical coordinates: {0}".format(ndim_vertical) - ) - if len(ndim_vertical) != 0: - vertical_coord_axis = ndim_vertical[0] - else: - # this means the vertical coordinate is an auxiliary coordinate of some kind. - vert_coord_found = False - - if not vert_coord_found: - # if we don't have a vertical coordinate, and we are 3D or lower - # that is okay. - if (field_in.ndim == 3 and time_axis is not None) or field_in.ndim < 3: - vertical_coord_axis = None - else: - raise ValueError("No suitable vertical coordinate found") - # Once we know the vertical coordinate, we can resolve the - # horizontal coordinates - - all_axes = np.arange(0, field_in.ndim) - output_vals = tuple( - all_axes[np.logical_not(np.isin(all_axes, [time_axis, vertical_coord_axis]))] - ) - return output_vals + raise ValueError("variable_arr must be Iris Cube or Xarray DataArray") @irispandas_to_xarray -def detect_latlon_coord_name(in_dataset, latitude_name=None, longitude_name=None): +def detect_latlon_coord_name( + in_dataset: Union[xr.DataArray, iris.cube.Cube], + latitude_name: Union[str, None] = None, + longitude_name: Union[str, None] = None, +) -> tuple[str]: """Function to detect the name of latitude/longitude coordinates Parameters ---------- - in_dataset: iris.cube.Cube, xarray.Dataset, or xarray.Dataarray + in_dataset: iris.cube.Cube or xarray.DataArray Input dataset to detect names from latitude_name: str The name of the latitude coordinate. If None, tries to auto-detect. diff --git a/tobac/utils/internal/internal_consts.py b/tobac/utils/internal/internal_consts.py new file mode 100644 index 00000000..a3ec44f5 --- /dev/null +++ b/tobac/utils/internal/internal_consts.py @@ -0,0 +1,6 @@ +COMMON_VERT_COORDS: list[str] = [ + "z", + "model_level_number", + "altitude", + "geopotential_height", +] diff --git a/tobac/utils/internal/iris_utils.py b/tobac/utils/internal/iris_utils.py new file mode 100644 index 00000000..029061cb --- /dev/null +++ b/tobac/utils/internal/iris_utils.py @@ -0,0 +1,156 @@ +"""Internal tobac utilities for iris cubes +The goal will be to, ultimately, remove these when we sunset iris +""" +from __future__ import annotations + +from typing import Union + +import iris +import iris.cube +import numpy as np + +from . import general_internal as tb_utils_gi + + +def find_axis_from_coord( + variable_cube: iris.cube.Cube, coord_name: str +) -> Union[int, None]: + """Finds the axis number in an iris cube given a coordinate name. + + Parameters + ---------- + variable_cube: iris.cube + Input variable cube + coord_name: str + coordinate to look for + + Returns + ------- + axis_number: int + the number of the axis of the given coordinate, or None if the coordinate + is not found in the cube or not a dimensional coordinate + """ + + list_coord_names = [coord.name() for coord in variable_cube.coords()] + all_matching_axes = list(set(list_coord_names) & {coord_name}) + if ( + len(all_matching_axes) == 1 + and len(variable_cube.coord_dims(all_matching_axes[0])) > 0 + ): + return variable_cube.coord_dims(all_matching_axes[0])[0] + if len(all_matching_axes) > 1: + raise ValueError("Too many axes matched.") + + return None + + +def find_vertical_axis_from_coord( + variable_cube: iris.cube.Cube, vertical_coord: Union[str, None] = None +) -> str: + """Function to find the vertical coordinate in the iris cube + + Parameters + ---------- + variable_cube: iris.cube + Input variable cube, containing a vertical coordinate. + vertical_coord: str + Vertical coordinate name. If None, this function tries to auto-detect. + + Returns + ------- + str + the vertical coordinate name + + Raises + ------ + ValueError + Raised if the vertical coordinate isn't found in the cube. + """ + + list_coord_names = [coord.name() for coord in variable_cube.coords()] + + if vertical_coord is None or vertical_coord == "auto": + # find the intersection + all_vertical_axes = list( + set(list_coord_names) & set(tb_utils_gi.COMMON_VERT_COORDS) + ) + if len(all_vertical_axes) >= 1: + return all_vertical_axes[0] + raise ValueError( + "Cube lacks suitable automatic vertical coordinate (z, model_level_number, altitude, " + "or geopotential_height)" + ) + if vertical_coord in list_coord_names: + return vertical_coord + raise ValueError("Please specify vertical coordinate found in cube") + + +def find_hdim_axes_3d( + field_in: iris.cube.Cube, + vertical_coord: Union[str, None] = None, + vertical_axis: Union[int, None] = None, +) -> tuple[int]: + """Finds what the hdim axes are given a 3D (including z) or + 4D (including z and time) dataset. + + Parameters + ---------- + field_in: iris cube + Input field, can be 3D or 4D + vertical_coord: str or None + The name of the vertical coord, or None, which will attempt to find + the vertical coordinate name + vertical_axis: int or None + The axis number of the vertical coordinate, or None. Note + that only one of vertical_axis or vertical_coord can be set. + + Returns + ------- + (hdim_1_axis, hdim_2_axis): (int, int) + The axes for hdim_1 and hdim_2 + """ + + if vertical_coord is not None and vertical_axis is not None: + if vertical_coord != "auto": + raise ValueError("Cannot set both vertical_coord and vertical_axis.") + + time_axis = find_axis_from_coord(field_in, "time") + if vertical_axis is not None: + vertical_coord_axis = vertical_axis + vert_coord_found = True + else: + try: + vertical_axis = find_vertical_axis_from_coord( + field_in, vertical_coord=vertical_coord + ) + except ValueError: + vert_coord_found = False + else: + vert_coord_found = True + ndim_vertical = field_in.coord_dims(vertical_axis) + if len(ndim_vertical) > 1: + raise ValueError( + "please specify 1 dimensional vertical coordinate." + f" Current vertical coordinates: {ndim_vertical}" + ) + if len(ndim_vertical) != 0: + vertical_coord_axis = ndim_vertical[0] + else: + # this means the vertical coordinate is an auxiliary coordinate of some kind. + vert_coord_found = False + + if not vert_coord_found: + # if we don't have a vertical coordinate, and we are 3D or lower + # that is okay. + if (field_in.ndim == 3 and time_axis is not None) or field_in.ndim < 3: + vertical_coord_axis = None + else: + raise ValueError("No suitable vertical coordinate found") + # Once we know the vertical coordinate, we can resolve the + # horizontal coordinates + + all_axes = np.arange(0, field_in.ndim) + output_vals = tuple( + all_axes[np.logical_not(np.isin(all_axes, [time_axis, vertical_coord_axis]))] + ) + return output_vals diff --git a/tobac/utils/internal/xarray_utils.py b/tobac/utils/internal/xarray_utils.py new file mode 100644 index 00000000..641be123 --- /dev/null +++ b/tobac/utils/internal/xarray_utils.py @@ -0,0 +1,50 @@ +"""Internal tobac utilities for xarray datasets/dataarrays +""" +from __future__ import annotations + + +from typing import Union +import xarray as xr +from . import general_internal as tb_utils_gi + + +def find_vertical_axis_from_coord( + variable_cube: xr.DataArray, + vertical_coord: Union[str, None] = None, +) -> str: + """Function to find the vertical coordinate in the iris cube + + Parameters + ---------- + variable_cube: iris.cube.Cube or xarray.DataArray + Input variable cube, containing a vertical coordinate. + vertical_coord: str + Vertical coordinate name. If None, this function tries to auto-detect. + + Returns + ------- + str + the vertical coordinate name + + Raises + ------ + ValueError + Raised if the vertical coordinate isn't found in the cube. + """ + + list_coord_names = variable_cube.coords + + if vertical_coord is None or vertical_coord == "auto": + # find the intersection + all_vertical_axes = list( + set(list_coord_names) & set(tb_utils_gi.COMMON_VERT_COORDS) + ) + if len(all_vertical_axes) >= 1: + return all_vertical_axes[0] + raise ValueError( + "Cube lacks suitable automatic vertical coordinate (z, model_level_number, " + "altitude, or geopotential_height)" + ) + if vertical_coord in list_coord_names: + return vertical_coord + raise ValueError("Please specify vertical coordinate found in cube") From e179280e02c1a5a4417fa0b4c6d47ef98d030699 Mon Sep 17 00:00:00 2001 From: Sean Freeman Date: Tue, 19 Sep 2023 12:04:46 -0700 Subject: [PATCH 2/9] fixing packaging problems --- setup.py | 2 +- tobac/utils/__init__.py | 5 ++++- tobac/utils/general.py | 1 - 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index e01547e0..68b13c4d 100644 --- a/setup.py +++ b/setup.py @@ -86,7 +86,7 @@ def get_requirements(requirements_filename): "peter.marinescu@colostate.edu", ], license="BSD-3-Clause License", - packages=[PACKAGE_NAME, PACKAGE_NAME + ".utils"], + packages=[PACKAGE_NAME, PACKAGE_NAME + ".utils", PACKAGE_NAME + ".utils.internal"], install_requires=get_requirements("requirements.txt"), test_requires=["pytest"], zip_safe=False, diff --git a/tobac/utils/__init__.py b/tobac/utils/__init__.py index e8c077a2..52a1222a 100644 --- a/tobac/utils/__init__.py +++ b/tobac/utils/__init__.py @@ -18,4 +18,7 @@ mask_features_surface, mask_cube_features, ) -from .internal import get_label_props_in_dict, get_indices_of_labels_from_reg_prop_dict +from tobac.utils.internal import ( + get_label_props_in_dict, + get_indices_of_labels_from_reg_prop_dict, +) diff --git a/tobac/utils/general.py b/tobac/utils/general.py index 1146136d..2d2c5fd7 100644 --- a/tobac/utils/general.py +++ b/tobac/utils/general.py @@ -5,7 +5,6 @@ import logging import pandas as pd - from . import internal as internal_utils import numpy as np import sklearn From 292a122edd3d9a6f80095ebbfedbd0db681c7b3a Mon Sep 17 00:00:00 2001 From: Sean Freeman Date: Tue, 19 Sep 2023 13:22:39 -0700 Subject: [PATCH 3/9] cleaning up --- tobac/utils/__init__.py | 5 +---- tobac/utils/general.py | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tobac/utils/__init__.py b/tobac/utils/__init__.py index b5fd2f5b..14e5773f 100644 --- a/tobac/utils/__init__.py +++ b/tobac/utils/__init__.py @@ -19,7 +19,4 @@ mask_features_surface, mask_cube_features, ) -from tobac.utils.internal import ( - get_label_props_in_dict, - get_indices_of_labels_from_reg_prop_dict, -) +from .internal import get_label_props_in_dict, get_indices_of_labels_from_reg_prop_dict diff --git a/tobac/utils/general.py b/tobac/utils/general.py index c08f2d98..7836a9c6 100644 --- a/tobac/utils/general.py +++ b/tobac/utils/general.py @@ -5,6 +5,7 @@ import logging import pandas as pd + from . import internal as internal_utils import numpy as np import sklearn From f574ed7c1a79a3b90dfd3a73418dbc9185a183e5 Mon Sep 17 00:00:00 2001 From: Sean Freeman Date: Tue, 19 Sep 2023 13:36:30 -0700 Subject: [PATCH 4/9] deleting non-used file --- tobac/utils/internal/internal_consts.py | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 tobac/utils/internal/internal_consts.py diff --git a/tobac/utils/internal/internal_consts.py b/tobac/utils/internal/internal_consts.py deleted file mode 100644 index a3ec44f5..00000000 --- a/tobac/utils/internal/internal_consts.py +++ /dev/null @@ -1,6 +0,0 @@ -COMMON_VERT_COORDS: list[str] = [ - "z", - "model_level_number", - "altitude", - "geopotential_height", -] From bf2335e5f9144dbf0980642f5e456cf0bb0ed390 Mon Sep 17 00:00:00 2001 From: Sean Freeman Date: Mon, 2 Oct 2023 10:14:17 -0500 Subject: [PATCH 5/9] adding build.os to rtd config --- .readthedocs.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.readthedocs.yml b/.readthedocs.yml index 1fcc341c..adecddb6 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -1,5 +1,9 @@ version: 2 formats: all +build: + os: ubuntu-22.04 + tools: + python: "3.11" python: version: 3 install: From 3c60e441e6d4c170032388cf111910b5b45ba684 Mon Sep 17 00:00:00 2001 From: Sean Freeman Date: Tue, 3 Oct 2023 11:44:10 -0500 Subject: [PATCH 6/9] update readthedocs config --- .readthedocs.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.readthedocs.yml b/.readthedocs.yml index adecddb6..676445e1 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -5,6 +5,5 @@ build: tools: python: "3.11" python: - version: 3 install: - requirements: doc/requirements.txt \ No newline at end of file From 18f322f5f155ca825c8440f99752e619524afc4b Mon Sep 17 00:00:00 2001 From: Sean Freeman Date: Tue, 3 Oct 2023 12:25:38 -0500 Subject: [PATCH 7/9] switch to using typing.callable explicitly --- tobac/utils/internal/general_internal.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tobac/utils/internal/general_internal.py b/tobac/utils/internal/general_internal.py index a9d3df3c..1da83ffd 100644 --- a/tobac/utils/internal/general_internal.py +++ b/tobac/utils/internal/general_internal.py @@ -11,7 +11,8 @@ import warnings from . import iris_utils from . import xarray_utils as xr_utils -from typing import Union, Callable +from typing import Union +import typing # list of common vertical coordinates to search for in various functions COMMON_VERT_COORDS: list[str] = [ @@ -109,7 +110,7 @@ def get_indices_of_labels_from_reg_prop_dict(region_property_dict: dict) -> tupl return [curr_loc_indices, y_indices, x_indices] -def iris_to_xarray(func: Callable) -> Callable: +def iris_to_xarray(func: typing.Callable) -> typing.Callable: """Decorator that converts all input of a function that is in the form of Iris cubes into xarray DataArrays and converts all outputs with type xarray DataArrays back into Iris cubes. @@ -175,7 +176,7 @@ def wrapper(*args, **kwargs): return wrapper -def xarray_to_iris(func: Callable) -> Callable: +def xarray_to_iris(func: typing.Callable) -> typing.Callable: """Decorator that converts all input of a function that is in the form of xarray DataArrays into Iris cubes and converts all outputs with type Iris cubes back into xarray DataArrays. @@ -259,7 +260,7 @@ def wrapper(*args, **kwargs): return wrapper -def irispandas_to_xarray(func: Callable) -> Callable: +def irispandas_to_xarray(func: typing.Callable) -> typing.Callable: """Decorator that converts all input of a function that is in the form of Iris cubes/pandas Dataframes into xarray DataArrays/xarray Datasets and converts all outputs with the type xarray DataArray/xarray Dataset @@ -339,7 +340,7 @@ def wrapper(*args, **kwargs): return wrapper -def xarray_to_irispandas(func: Callable) -> Callable: +def xarray_to_irispandas(func: typing.Callable) -> typing.Callable: """Decorator that converts all input of a function that is in the form of DataArrays/xarray Datasets into xarray Iris cubes/pandas Dataframes and converts all outputs with the type Iris cubes/pandas Dataframes back into @@ -442,7 +443,7 @@ def wrapper(*args, **kwargs): return wrapper -def njit_if_available(func: Callable, **kwargs) -> Callable: +def njit_if_available(func: typing.Callable, **kwargs) -> typing.Callable: """Decorator to wrap a function with numba.njit if available. If numba isn't available, it just returns the function. From cf0e34313f0a0c7baefe7ae9ae073bc202481138 Mon Sep 17 00:00:00 2001 From: Sean Freeman Date: Tue, 3 Oct 2023 12:33:28 -0500 Subject: [PATCH 8/9] revert typing changes, switch to 3.9 building of python --- .readthedocs.yml | 2 +- tobac/utils/internal/general_internal.py | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.readthedocs.yml b/.readthedocs.yml index 676445e1..96837528 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -3,7 +3,7 @@ formats: all build: os: ubuntu-22.04 tools: - python: "3.11" + python: "3.9" python: install: - requirements: doc/requirements.txt \ No newline at end of file diff --git a/tobac/utils/internal/general_internal.py b/tobac/utils/internal/general_internal.py index 1da83ffd..a9d3df3c 100644 --- a/tobac/utils/internal/general_internal.py +++ b/tobac/utils/internal/general_internal.py @@ -11,8 +11,7 @@ import warnings from . import iris_utils from . import xarray_utils as xr_utils -from typing import Union -import typing +from typing import Union, Callable # list of common vertical coordinates to search for in various functions COMMON_VERT_COORDS: list[str] = [ @@ -110,7 +109,7 @@ def get_indices_of_labels_from_reg_prop_dict(region_property_dict: dict) -> tupl return [curr_loc_indices, y_indices, x_indices] -def iris_to_xarray(func: typing.Callable) -> typing.Callable: +def iris_to_xarray(func: Callable) -> Callable: """Decorator that converts all input of a function that is in the form of Iris cubes into xarray DataArrays and converts all outputs with type xarray DataArrays back into Iris cubes. @@ -176,7 +175,7 @@ def wrapper(*args, **kwargs): return wrapper -def xarray_to_iris(func: typing.Callable) -> typing.Callable: +def xarray_to_iris(func: Callable) -> Callable: """Decorator that converts all input of a function that is in the form of xarray DataArrays into Iris cubes and converts all outputs with type Iris cubes back into xarray DataArrays. @@ -260,7 +259,7 @@ def wrapper(*args, **kwargs): return wrapper -def irispandas_to_xarray(func: typing.Callable) -> typing.Callable: +def irispandas_to_xarray(func: Callable) -> Callable: """Decorator that converts all input of a function that is in the form of Iris cubes/pandas Dataframes into xarray DataArrays/xarray Datasets and converts all outputs with the type xarray DataArray/xarray Dataset @@ -340,7 +339,7 @@ def wrapper(*args, **kwargs): return wrapper -def xarray_to_irispandas(func: typing.Callable) -> typing.Callable: +def xarray_to_irispandas(func: Callable) -> Callable: """Decorator that converts all input of a function that is in the form of DataArrays/xarray Datasets into xarray Iris cubes/pandas Dataframes and converts all outputs with the type Iris cubes/pandas Dataframes back into @@ -443,7 +442,7 @@ def wrapper(*args, **kwargs): return wrapper -def njit_if_available(func: typing.Callable, **kwargs) -> typing.Callable: +def njit_if_available(func: Callable, **kwargs) -> Callable: """Decorator to wrap a function with numba.njit if available. If numba isn't available, it just returns the function. From 689e8622b793578fcad4a72f358f3dc9edd97783 Mon Sep 17 00:00:00 2001 From: Sean Freeman Date: Wed, 8 Nov 2023 10:10:43 -0600 Subject: [PATCH 9/9] rename general_internal to basic --- tobac/utils/internal/__init__.py | 2 +- tobac/utils/internal/{general_internal.py => basic.py} | 0 tobac/utils/internal/iris_utils.py | 2 +- tobac/utils/internal/xarray_utils.py | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename tobac/utils/internal/{general_internal.py => basic.py} (100%) diff --git a/tobac/utils/internal/__init__.py b/tobac/utils/internal/__init__.py index 463d824c..da0f2b60 100644 --- a/tobac/utils/internal/__init__.py +++ b/tobac/utils/internal/__init__.py @@ -1 +1 @@ -from .general_internal import * +from .basic import * diff --git a/tobac/utils/internal/general_internal.py b/tobac/utils/internal/basic.py similarity index 100% rename from tobac/utils/internal/general_internal.py rename to tobac/utils/internal/basic.py diff --git a/tobac/utils/internal/iris_utils.py b/tobac/utils/internal/iris_utils.py index 029061cb..36561799 100644 --- a/tobac/utils/internal/iris_utils.py +++ b/tobac/utils/internal/iris_utils.py @@ -9,7 +9,7 @@ import iris.cube import numpy as np -from . import general_internal as tb_utils_gi +from . import basic as tb_utils_gi def find_axis_from_coord( diff --git a/tobac/utils/internal/xarray_utils.py b/tobac/utils/internal/xarray_utils.py index 641be123..6d37dacc 100644 --- a/tobac/utils/internal/xarray_utils.py +++ b/tobac/utils/internal/xarray_utils.py @@ -5,7 +5,7 @@ from typing import Union import xarray as xr -from . import general_internal as tb_utils_gi +from . import basic as tb_utils_gi def find_vertical_axis_from_coord(