Skip to content

Commit

Permalink
upgrade to python 3.11+ syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mcflugen committed Dec 12, 2024
1 parent 76ee126 commit a72f006
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 49 deletions.
20 changes: 10 additions & 10 deletions deltametrics/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ def register_planform(self, name, PlanformInstance, return_planform=False):
if not issubclass(type(PlanformInstance), BasePlanform):
raise TypeError(
"`PlanformInstance` was not a `Planform`. "
"Instead, was: {0}".format(type(PlanformInstance))
"Instead, was: {}".format(type(PlanformInstance))
)
if not isinstance(name, str):
raise TypeError(
"`name` was not a string. " "Instead, was: {0}".format(type(name))
"`name` was not a string. " "Instead, was: {}".format(type(name))
)
PlanformInstance.connect(self, name=name) # attach cube
self._planform_set[name] = PlanformInstance
Expand Down Expand Up @@ -311,11 +311,11 @@ def register_section(self, name, SectionInstance, return_section=False):
if not issubclass(type(SectionInstance), BaseSection):
raise TypeError(
"`SectionInstance` was not a `Section`. "
"Instead, was: {0}".format(type(SectionInstance))
"Instead, was: {}".format(type(SectionInstance))
)
if not isinstance(name, str):
raise TypeError(
"`name` was not a string. " "Instead, was: {0}".format(type(name))
"`name` was not a string. " "Instead, was: {}".format(type(name))
)
SectionInstance.connect(self, name=name) # attach cube
self._section_set[name] = SectionInstance
Expand Down Expand Up @@ -468,7 +468,7 @@ def quick_show(self, var, idx=-1, axis=0, **kwargs):
# this is a Dip section
_obj = DipSection(self, distance_idx=idx)
else:
raise ValueError("Invalid `axis` specified: {0}".format(axis))
raise ValueError(f"Invalid `axis` specified: {axis}")

# use the object to handle the showing
_obj.show(var, **kwargs)
Expand Down Expand Up @@ -557,7 +557,7 @@ def show_cube(self, var, style="mesh", ve=200, ax=None):
p.add_mesh(slices, cmap=self.varset[var].cmap)

else:
raise ValueError("Bad value for style: {0}".format(style))
raise ValueError(f"Bad value for style: {style}")

p.show()

Expand Down Expand Up @@ -609,7 +609,7 @@ def show_planform(self, name, variable, **kwargs):
if isinstance(name, str):
self._planform_set[name].show(variable, **kwargs)
else:
raise TypeError("`name` was not a string, " "was {0}".format(type(name)))
raise TypeError("`name` was not a string, " "was {}".format(type(name)))

def show_section(self, name, variable, **kwargs):
"""Show a registered section by name and variable.
Expand All @@ -632,7 +632,7 @@ def show_section(self, name, variable, **kwargs):
if isinstance(name, str):
self._section_set[name].show(variable, **kwargs)
else:
raise TypeError("`name` was not a string, " "was {0}".format(type(name)))
raise TypeError("`name` was not a string, " "was {}".format(type(name)))


class DataCube(BaseCube):
Expand Down Expand Up @@ -754,7 +754,7 @@ def __getitem__(self, var):

else:
raise AttributeError(
"No variable of {cube} named {var}".format(cube=str(self), var=var)
f"No variable of {str(self)} named {var}"
)

# make _obj xarray if it not already
Expand Down Expand Up @@ -990,7 +990,7 @@ def __getitem__(self, var):
_var = self.dataio[var]
else:
raise AttributeError(
"No variable of {cube} named {var}".format(cube=str(self), var=var)
f"No variable of {str(self)} named {var}"
)

# the following lines apply the data to stratigraphy mapping
Expand Down
12 changes: 6 additions & 6 deletions deltametrics/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def connect(self):
_engine = "h5netcdf"
else:
TypeError(
"File format is not supported " "by DeltaMetrics: {0}".format(_ext)
"File format is not supported " "by DeltaMetrics: {}".format(_ext)
)

try:
Expand All @@ -232,7 +232,7 @@ def connect(self):
self.dataset = _dataset.set_coords(_coords_list)
self.coords = list(self.dataset.coords)
self.dims = copy.deepcopy(self.coords)
elif set(["total_time", "length", "width"]).issubset(_dims_set):
elif {"total_time", "length", "width"}.issubset(_dims_set):
# the coordinates are not set, but there are matching arrays
# this is a legacy option, so issue a warning here
self.dataset = _dataset.set_coords(["x", "y", "time"])
Expand Down Expand Up @@ -388,7 +388,7 @@ def get_known_coords(self, dimensions):
if not isinstance(dimensions, dict):
raise TypeError(
"Input type for `dimensions` must be "
"`dict` but was {0}".format(type(dimensions))
"`dict` but was {}".format(type(dimensions))
)
# there should be exactly 3 keys
if not (len(dimensions.keys()) == 3):
Expand All @@ -398,9 +398,9 @@ def get_known_coords(self, dimensions):
for i, (k, v) in enumerate(dimensions.items()):
if not (len(dimensions[k]) == under_shp[i]):
raise ValueError(
"Shape of `dimensions` at position {0} was {1}, "
"Shape of `dimensions` at position {} was {}, "
"which does not match the variables dimensions "
"{2}.".format(i, len(dimensions[k]), under_shp)
"{}.".format(i, len(dimensions[k]), under_shp)
)
# make the assignment
self.dims = list(dimensions.keys())
Expand Down Expand Up @@ -457,7 +457,7 @@ def __getitem__(self, var):
elif var in self.known_coords:
return self.dimensions[var]
else:
raise ValueError("No variable named {0} found.".format(var))
raise ValueError(f"No variable named {var} found.")

@property
def keys(self):
Expand Down
16 changes: 8 additions & 8 deletions deltametrics/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, mask_type, *args, **kwargs):
)
self._set_shape_mask(args[0])
else:
raise TypeError("Unexpected type was input: {0}".format(type(args[0])))
raise TypeError(f"Unexpected type was input: {type(args[0])}")

def _set_shape_mask(self, array):
"""Set the mask shape.
Expand All @@ -97,7 +97,7 @@ def _set_shape_mask(self, array):
if issubclass(type(array), BaseMask):
raise TypeError(
"Input must be array-like, but was a `Mask` type: "
"{0}".format(type(array))
"{}".format(type(array))
)

# check that the input is not 3D
Expand All @@ -113,7 +113,7 @@ def _set_shape_mask(self, array):
# this will use meshgrid to fill out with dx=1 in shape of array
self._mask = xr.DataArray(data=np.zeros(_shape, dtype=bool))
else:
raise TypeError("Invalid type {0}".format(type(array)))
raise TypeError(f"Invalid type {type(array)}")

# set the shape attribute
self._shape = self._mask.shape
Expand Down Expand Up @@ -831,7 +831,7 @@ def from_mask(*args, **kwargs):
raise TypeError(
"Double `Mask` input types must be `ElevationMask` "
"and `LandMask`, but received argument of type "
"`{0}`.".format(type(UnknownMask))
"`{}`.".format(type(UnknownMask))
)
elif len(args) == 1:
UnknownMask = args[0]
Expand All @@ -842,12 +842,12 @@ def from_mask(*args, **kwargs):
else:
raise TypeError(
"Single `Mask` input was expected to be type "
"`ElevationMask`, but was `{0}`".format(type(UnknownMask))
"`ElevationMask`, but was `{}`".format(type(UnknownMask))
)
else:
raise ValueError(
"Must pass either one or two Masks to static method "
"`from_mask` for `WetMask`, but received {0} args".format(len(args))
"`from_mask` for `WetMask`, but received {} args".format(len(args))
)

# set up the empty shoreline mask
Expand Down Expand Up @@ -1697,12 +1697,12 @@ def from_mask(*args, **kwargs):
raise TypeError(
"Paired `Mask` input types must be `WetMask` "
"and `LandMask`, but received argument of type "
"`{0}`.".format(type(UnknownMask))
"`{}`.".format(type(UnknownMask))
)
else:
raise ValueError(
"Must pass either one or two Masks to static method "
"`from_mask` for `WetMask`, but received {0} args".format(len(args))
"`from_mask` for `WetMask`, but received {} args".format(len(args))
)

# set up the empty shoreline mask
Expand Down
24 changes: 12 additions & 12 deletions deltametrics/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def show(
else:
_varinfo = self._default_varinfo
else:
raise TypeError("Bad value for `var`: {0}".format(var))
raise TypeError(f"Bad value for `var`: {var}")

self._show(
_field,
Expand Down Expand Up @@ -760,7 +760,7 @@ def __init__(self, *args, **kwargs):
data=np.zeros(_below_mask.shape, dtype=float), name="opening_angles"
)
else:
raise TypeError("Invalid type {0}".format(type(_below_mask)))
raise TypeError(f"Invalid type {type(_below_mask)}")

elif issubclass(type(args[0]), BaseCube):
raise NotImplementedError(
Expand Down Expand Up @@ -1038,7 +1038,7 @@ def __init__(self, *args, **kwargs):
name="mean_image",
)
else:
raise TypeError("Invalid type {0}".format(type(self._elevation_mask)))
raise TypeError(f"Invalid type {type(self._elevation_mask)}")

# see if the inlet width is provided, if not see if cube is avail
if len(args) > 1:
Expand All @@ -1047,7 +1047,7 @@ def __init__(self, *args, **kwargs):
else:
raise TypeError(
"Expected single number to set max inlet size, got: "
"{0}".format(args[1])
"{}".format(args[1])
)
elif isinstance(self.cube, BaseCube):
try:
Expand Down Expand Up @@ -1179,7 +1179,7 @@ def compute_land_area(land_mask):
_lm = land_mask
_dx = 1
else:
raise TypeError("Invalid type {0}".format(type(land_mask)))
raise TypeError(f"Invalid type {type(land_mask)}")

return np.sum(_lm) * _dx * _dx

Expand Down Expand Up @@ -1304,7 +1304,7 @@ def compute_shoreline_roughness(shore_mask, land_mask, **kwargs):
_lm = land_mask
_dx = 1
else:
raise TypeError("Invalid type {0}".format(type(land_mask)))
raise TypeError(f"Invalid type {type(land_mask)}")

_ = kwargs.pop("return_line", None) # trash this variable if passed
shorelength = compute_shoreline_length(shore_mask, return_line=False, **kwargs)
Expand Down Expand Up @@ -1422,7 +1422,7 @@ def compute_shoreline_length(shore_mask, origin=[0, 0], return_line=False):
_dx = 1
# should we have a warning that no dx was found here?
else:
raise TypeError("Invalid type {0}".format(type(shore_mask)))
raise TypeError(f"Invalid type {type(shore_mask)}")

if not (np.sum(_sm) > 0):
raise ValueError("No pixels in shoreline mask.")
Expand Down Expand Up @@ -1639,7 +1639,7 @@ def compute_shoreline_distance(shore_mask, origin=[0, 0], return_distances=False
_sm = shore_mask
_dx = 1
else:
raise TypeError("Invalid type {0}".format(type(shore_mask)))
raise TypeError(f"Invalid type {type(shore_mask)}")

if not (np.sum(_sm) > 0):
raise ValueError("No pixels in shoreline mask.")
Expand Down Expand Up @@ -2170,7 +2170,7 @@ def compute_channel_width(channelmask, section=None, return_widths=False):
channelmask = np.array(channelmask)
else:
raise TypeError(
"Input for `channelmask` was wrong type: {}.".format(type(channelmask))
f"Input for `channelmask` was wrong type: {type(channelmask)}."
)

# get channel starts and ends
Expand Down Expand Up @@ -2292,7 +2292,7 @@ def compute_channel_depth(
channelmask = np.array(channelmask.mask)
else:
raise TypeError(
"Input for `channelmask` was wrong type: {}.".format(type(channelmask))
f"Input for `channelmask` was wrong type: {type(channelmask)}."
)

# get channel starts and ends
Expand Down Expand Up @@ -2326,7 +2326,7 @@ def compute_channel_depth(
elif depth_type == "mean":
_channel_depth_list = _channel_depth_means
else:
raise ValueError("Invalid argument to `depth_type` {}".format(str(depth_type)))
raise ValueError(f"Invalid argument to `depth_type` {str(depth_type)}")

_m, _s = np.mean(_channel_depth_list), np.std(_channel_depth_list)
if return_depths:
Expand Down Expand Up @@ -2411,7 +2411,7 @@ def compute_surface_deposit_time(data, surface_idx=-1, **kwargs):
etas = np.array(data[:surface_idx, :, :])
else:
# implement other options...
raise TypeError("Unexpected data type input: {0}".format(type(data)))
raise TypeError(f"Unexpected data type input: {type(data)}")

sfc_date = _compute_surface_deposit_time_from_etas(etas, **kwargs)

Expand Down
12 changes: 6 additions & 6 deletions deltametrics/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# plotting utilities


class VariableInfo(object):
class VariableInfo:
"""Variable styling and information.
This class holds information for a specific underlying data variable
Expand Down Expand Up @@ -166,7 +166,7 @@ def vmax(self, var):
self._vmax = var


class VariableSet(object):
class VariableSet:
"""A default set of properties for variables.
This makes it easy to have consistent plots.
Expand Down Expand Up @@ -876,7 +876,7 @@ def get_display_arrays(VarInst, data=None):
_z = VarInst[VarInst.dims[0]]

# prepend the data with the lowest depth recorded to fill out image
_p = np.min(_arr_Y) * np.ones((_arr_Y.shape[1])) # prepend base
_p = np.min(_arr_Y) * np.ones(_arr_Y.shape[1]) # prepend base
_arr_Y = np.vstack((_p, _arr_Y))
_arr_X = np.pad(_arr_X, ((0, 1), (0, 1)), mode="edge")
return _den, _arr_X, _arr_Y
Expand Down Expand Up @@ -1382,7 +1382,7 @@ def show_histograms(*args, sets=None, ax=None, **kwargs):

for i in range(n_sets):
CN = "C%d" % (i)
match = np.where((sets == i))[0]
match = np.where(sets == i)[0]
scales = np.linspace(0.8, 1.2, num=len(match))
CNs = [_scale_lightness(colors.to_rgb(CN), sc) for s, sc in enumerate(scales)]
for n in range(len(match)):
Expand Down Expand Up @@ -1603,15 +1603,15 @@ def overlay_sparse_array(
else: # if it is a tuple, check the length
raise TypeError(
"`alpha_clip` must be type `tuple`, "
"but was type {0}.".format(type(alpha_clip))
"but was type {}.".format(type(alpha_clip))
)

# check the clip_type flag
clip_type_allow = ["percentile", "value"]
if clip_type not in clip_type_allow:
raise ValueError(
"Bad value given for `clip_type` argument. Input argument must "
"be one of `{0}`, but was `{1}`".format(clip_type_allow, clip_type)
"be one of `{}`, but was `{}`".format(clip_type_allow, clip_type)
)

# pull the cmap out
Expand Down
4 changes: 2 additions & 2 deletions deltametrics/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ def _compute_section_coords(self):
if len(self._input_length) != 2:
raise ValueError(
"Input `length` must be two element tuple or list, "
"but was {0}".format(str(self._input_length))
"but was {}".format(str(self._input_length))
)
_length = self._input_length

Expand Down Expand Up @@ -1387,7 +1387,7 @@ def _compute_section_coords(self):
if len(self._input_length) != 2:
raise ValueError(
"Input `length` must be two element tuple or list, "
"but was {0}".format(str(self._input_length))
"but was {}".format(str(self._input_length))
)
_length = self._input_length

Expand Down
4 changes: 2 additions & 2 deletions deltametrics/strat.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def compute_boxy_stratigraphy_coordinates(elev, sigma_dist=None,
return strata_coords, data_coords


class BaseStratigraphyAttributes(object):
class BaseStratigraphyAttributes:

def __init__(self, style):
self._style = style
Expand Down Expand Up @@ -680,7 +680,7 @@ def preserved_voxel_count(self):
return ...


class BoxyStratigraphyAttributes(object):
class BoxyStratigraphyAttributes:
"""Attribute set for boxy stratigraphy information, emebdded into a DataCube.
"""

Expand Down
Loading

0 comments on commit a72f006

Please sign in to comment.