Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions build/lib/CheckmateSample/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def make_checkerboard(
square_size: tuple[int, int],
separation_size: int = None,
validation: bool = False,
verbose: bool = False,
) -> np.ndarray:
"""
Create a checkerboard pattern.
Expand All @@ -33,7 +34,7 @@ def make_checkerboard(
raise ValueError("Separation size must be a non-negative, non-zero integer")

# Add warning if inputs are not the same
if rows != cols or sq_rows != sq_cols:
if verbose and (rows != cols or sq_rows != sq_cols):
print(
"Warning: The inputs for board_size or square_size are not the same. This may result in a non-square checkerboard."
)
Expand Down Expand Up @@ -71,6 +72,7 @@ def make_checkerboard_xr(
keep_pattern: int = 1,
validation: bool = False,
dim_names: dict = None,
verbose: bool = False,
) -> xr.DataArray:
"""
Apply a checkerboard pattern to an existing xarray DataArray.
Expand All @@ -95,9 +97,13 @@ def make_checkerboard_xr(
if sq_y <= 0 or sq_x <= 0:
raise ValueError("Square size dimensions must be positive integers.")
if validation and keep_pattern not in [0, 1, 2]:
raise ValueError("For validation (ternary pattern), keep_pattern must be 0, 1, or 2.")
raise ValueError(
"For validation (ternary pattern), keep_pattern must be 0, 1, or 2."
)
elif not validation and keep_pattern not in [0, 1]:
raise ValueError("For non-validation (binary pattern), keep_pattern must be 0 or 1.")
raise ValueError(
"For non-validation (binary pattern), keep_pattern must be 0 or 1."
)
if sep and sep < 0:
raise ValueError("Separation size must be a non-negative, non-zero integer")

Expand All @@ -111,7 +117,9 @@ def make_checkerboard_xr(
x_dim = next((dim for dim in da.dims if dim in possible_x_dims), None)

if y_dim is None or x_dim is None:
raise ValueError("Could not automatically detect x and y dimensions. Please specify using dim_names.")
raise ValueError(
"Could not automatically detect x and y dimensions. Please specify using dim_names."
)
else:
y_dim = dim_names.get("y")
x_dim = dim_names.get("x")
Expand All @@ -120,12 +128,14 @@ def make_checkerboard_xr(
raise ValueError("Both 'x' and 'y' must be specified in dim_names.")

if y_dim not in da.dims or x_dim not in da.dims:
raise ValueError(f"Specified dimensions {y_dim} and {x_dim} not found in DataArray.")
raise ValueError(
f"Specified dimensions {y_dim} and {x_dim} not found in DataArray."
)

y_size, x_size = da.sizes[y_dim], da.sizes[x_dim]

# Add warning if inputs are not the same
if y_size != x_size or sq_y != sq_x:
if verbose and (y_size != x_size or sq_y != sq_x):
print(
"Warning: The inputs for board_size or square_size are not the same. This may result in a non-square checkerboard."
)
Expand Down
25 changes: 13 additions & 12 deletions example_notebooks/example_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -109,13 +109,13 @@
],
"source": [
"# Plot original 0-1 pattern\n",
"checkerboard_01 = make_checkerboard(board_size=(74,74), square_size=(10,10), separation_size=2)\n",
"checkerboard_01 = make_checkerboard(board_size=(74,74), square_size=(10,10), separation_size=2, verbose=True)\n",
"plot_checkerboard(checkerboard_01, \"Checkerboard Pattern (0-1)\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -131,7 +131,7 @@
],
"source": [
"# Plot new 0-1-2 pattern\n",
"checkerboard_012 = make_checkerboard(board_size=(71,71), square_size=(10,10), separation_size=5, validation=True)\n",
"checkerboard_012 = make_checkerboard(board_size=(71,71), square_size=(10,10), separation_size=5, validation=True, verbose=True)\n",
"plot_checkerboard(checkerboard_012, \"Checkerboard Pattern (0-1-2)\")"
]
},
Expand Down Expand Up @@ -173,7 +173,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -204,6 +204,7 @@
" square_size=(50, 50), # adjust this size to make pattern more/less visible\n",
" separation_size=10,\n",
" validation=False,\n",
" verbose=True\n",
")\n",
"\n",
"# Create figure with three subplots\n",
Expand Down Expand Up @@ -246,7 +247,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -306,9 +307,9 @@
"\n",
"# Apply the checkerboard pattern for all three parts of the ternary pattern\n",
"square_size = (5, 5) # 5x5 pixel squares\n",
"checkerboard_temp_0 = make_checkerboard_xr(air_temp, square_size, keep_pattern=0, validation=True)\n",
"checkerboard_temp_1 = make_checkerboard_xr(air_temp, square_size, keep_pattern=1, validation=True)\n",
"checkerboard_temp_2 = make_checkerboard_xr(air_temp, square_size, keep_pattern=2, validation=True)\n",
"checkerboard_temp_0 = make_checkerboard_xr(air_temp, square_size, keep_pattern=0, validation=True, verbose=True)\n",
"checkerboard_temp_1 = make_checkerboard_xr(air_temp, square_size, keep_pattern=1, validation=True, verbose=True))\n",
"checkerboard_temp_2 = make_checkerboard_xr(air_temp, square_size, keep_pattern=2, validation=True, verbose=True))\n",
"\n",
"# Plotting\n",
"fig, axs = plt.subplots(2, 2, figsize=(15, 15))\n",
Expand Down Expand Up @@ -352,7 +353,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -413,8 +414,8 @@
"\n",
"# Apply the checkerboard pattern for both parts of the binary pattern\n",
"square_size = (7, 5) # 7x5 pixel squares\n",
"checkerboard_temp_0 = make_checkerboard_xr(air_temp, square_size, separation_size=3, keep_pattern=0, validation=False)\n",
"checkerboard_temp_1 = make_checkerboard_xr(air_temp, square_size, separation_size=3, keep_pattern=1, validation=False)\n",
"checkerboard_temp_0 = make_checkerboard_xr(air_temp, square_size, separation_size=3, keep_pattern=0, validation=False, verbose=True))\n",
"checkerboard_temp_1 = make_checkerboard_xr(air_temp, square_size, separation_size=3, keep_pattern=1, validation=False, verbose=True))\n",
"\n",
"# Plotting\n",
"fig, axs = plt.subplots(1, 3, figsize=(20, 6))\n",
Expand Down
5 changes: 3 additions & 2 deletions src/CheckmateSample.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Metadata-Version: 2.4
Name: CheckmateSample
Version: 0.0.post1
Version: 0.0.post154
Summary: A sample project with checkerboard functionality
Author-email: Thomas Martin <tmartin@ucar.edu>
Project-URL: Homepage, https://github.com/ThomasMGeo/CheckmateSample
Expand All @@ -13,6 +13,7 @@ Provides-Extra: dev
Requires-Dist: pytest>=7.4.0; extra == "dev"
Requires-Dist: ruff>=0.1.3; extra == "dev"
Requires-Dist: setuptools_scm>=8.1; extra == "dev"
Dynamic: license-file

# CheckmateSample

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
22 changes: 16 additions & 6 deletions src/CheckmateSample/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def make_checkerboard(
square_size: tuple[int, int],
separation_size: int = None,
validation: bool = False,
verbose: bool = False,
) -> np.ndarray:
"""
Create a checkerboard pattern.
Expand All @@ -33,7 +34,7 @@ def make_checkerboard(
raise ValueError("Separation size must be a non-negative, non-zero integer")

# Add warning if inputs are not the same
if rows != cols or sq_rows != sq_cols:
if verbose and (rows != cols or sq_rows != sq_cols):
print(
"Warning: The inputs for board_size or square_size are not the same. This may result in a non-square checkerboard."
)
Expand Down Expand Up @@ -71,6 +72,7 @@ def make_checkerboard_xr(
keep_pattern: int = 1,
validation: bool = False,
dim_names: dict = None,
verbose: bool = False,
) -> xr.DataArray:
"""
Apply a checkerboard pattern to an existing xarray DataArray.
Expand All @@ -95,9 +97,13 @@ def make_checkerboard_xr(
if sq_y <= 0 or sq_x <= 0:
raise ValueError("Square size dimensions must be positive integers.")
if validation and keep_pattern not in [0, 1, 2]:
raise ValueError("For validation (ternary pattern), keep_pattern must be 0, 1, or 2.")
raise ValueError(
"For validation (ternary pattern), keep_pattern must be 0, 1, or 2."
)
elif not validation and keep_pattern not in [0, 1]:
raise ValueError("For non-validation (binary pattern), keep_pattern must be 0 or 1.")
raise ValueError(
"For non-validation (binary pattern), keep_pattern must be 0 or 1."
)
if sep and sep < 0:
raise ValueError("Separation size must be a non-negative, non-zero integer")

Expand All @@ -111,7 +117,9 @@ def make_checkerboard_xr(
x_dim = next((dim for dim in da.dims if dim in possible_x_dims), None)

if y_dim is None or x_dim is None:
raise ValueError("Could not automatically detect x and y dimensions. Please specify using dim_names.")
raise ValueError(
"Could not automatically detect x and y dimensions. Please specify using dim_names."
)
else:
y_dim = dim_names.get("y")
x_dim = dim_names.get("x")
Expand All @@ -120,12 +128,14 @@ def make_checkerboard_xr(
raise ValueError("Both 'x' and 'y' must be specified in dim_names.")

if y_dim not in da.dims or x_dim not in da.dims:
raise ValueError(f"Specified dimensions {y_dim} and {x_dim} not found in DataArray.")
raise ValueError(
f"Specified dimensions {y_dim} and {x_dim} not found in DataArray."
)

y_size, x_size = da.sizes[y_dim], da.sizes[x_dim]

# Add warning if inputs are not the same
if y_size != x_size or sq_y != sq_x:
if verbose and (y_size != x_size or sq_y != sq_x):
print(
"Warning: The inputs for board_size or square_size are not the same. This may result in a non-square checkerboard."
)
Expand Down
Binary file not shown.
Loading
Loading