Skip to content

Commit 79d58a7

Browse files
authored
Merge pull request #5 from tyhowee/feature/verbose
Added verbose flag to both checkerboard functions.
2 parents 8525af6 + 717ad20 commit 79d58a7

File tree

9 files changed

+115
-51
lines changed

9 files changed

+115
-51
lines changed

build/lib/CheckmateSample/generator.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def make_checkerboard(
77
square_size: tuple[int, int],
88
separation_size: int = None,
99
validation: bool = False,
10+
verbose: bool = False,
1011
) -> np.ndarray:
1112
"""
1213
Create a checkerboard pattern.
@@ -33,7 +34,7 @@ def make_checkerboard(
3334
raise ValueError("Separation size must be a non-negative, non-zero integer")
3435

3536
# Add warning if inputs are not the same
36-
if rows != cols or sq_rows != sq_cols:
37+
if verbose and (rows != cols or sq_rows != sq_cols):
3738
print(
3839
"Warning: The inputs for board_size or square_size are not the same. This may result in a non-square checkerboard."
3940
)
@@ -71,6 +72,7 @@ def make_checkerboard_xr(
7172
keep_pattern: int = 1,
7273
validation: bool = False,
7374
dim_names: dict = None,
75+
verbose: bool = False,
7476
) -> xr.DataArray:
7577
"""
7678
Apply a checkerboard pattern to an existing xarray DataArray.
@@ -95,9 +97,13 @@ def make_checkerboard_xr(
9597
if sq_y <= 0 or sq_x <= 0:
9698
raise ValueError("Square size dimensions must be positive integers.")
9799
if validation and keep_pattern not in [0, 1, 2]:
98-
raise ValueError("For validation (ternary pattern), keep_pattern must be 0, 1, or 2.")
100+
raise ValueError(
101+
"For validation (ternary pattern), keep_pattern must be 0, 1, or 2."
102+
)
99103
elif not validation and keep_pattern not in [0, 1]:
100-
raise ValueError("For non-validation (binary pattern), keep_pattern must be 0 or 1.")
104+
raise ValueError(
105+
"For non-validation (binary pattern), keep_pattern must be 0 or 1."
106+
)
101107
if sep and sep < 0:
102108
raise ValueError("Separation size must be a non-negative, non-zero integer")
103109

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

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

122130
if y_dim not in da.dims or x_dim not in da.dims:
123-
raise ValueError(f"Specified dimensions {y_dim} and {x_dim} not found in DataArray.")
131+
raise ValueError(
132+
f"Specified dimensions {y_dim} and {x_dim} not found in DataArray."
133+
)
124134

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

127137
# Add warning if inputs are not the same
128-
if y_size != x_size or sq_y != sq_x:
138+
if verbose and (y_size != x_size or sq_y != sq_x):
129139
print(
130140
"Warning: The inputs for board_size or square_size are not the same. This may result in a non-square checkerboard."
131141
)

example_notebooks/example_notebook.ipynb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
},
9494
{
9595
"cell_type": "code",
96-
"execution_count": 4,
96+
"execution_count": null,
9797
"metadata": {},
9898
"outputs": [
9999
{
@@ -109,13 +109,13 @@
109109
],
110110
"source": [
111111
"# Plot original 0-1 pattern\n",
112-
"checkerboard_01 = make_checkerboard(board_size=(74,74), square_size=(10,10), separation_size=2)\n",
112+
"checkerboard_01 = make_checkerboard(board_size=(74,74), square_size=(10,10), separation_size=2, verbose=True)\n",
113113
"plot_checkerboard(checkerboard_01, \"Checkerboard Pattern (0-1)\")"
114114
]
115115
},
116116
{
117117
"cell_type": "code",
118-
"execution_count": 5,
118+
"execution_count": null,
119119
"metadata": {},
120120
"outputs": [
121121
{
@@ -131,7 +131,7 @@
131131
],
132132
"source": [
133133
"# Plot new 0-1-2 pattern\n",
134-
"checkerboard_012 = make_checkerboard(board_size=(71,71), square_size=(10,10), separation_size=5, validation=True)\n",
134+
"checkerboard_012 = make_checkerboard(board_size=(71,71), square_size=(10,10), separation_size=5, validation=True, verbose=True)\n",
135135
"plot_checkerboard(checkerboard_012, \"Checkerboard Pattern (0-1-2)\")"
136136
]
137137
},
@@ -173,7 +173,7 @@
173173
},
174174
{
175175
"cell_type": "code",
176-
"execution_count": 8,
176+
"execution_count": null,
177177
"metadata": {},
178178
"outputs": [
179179
{
@@ -204,6 +204,7 @@
204204
" square_size=(50, 50), # adjust this size to make pattern more/less visible\n",
205205
" separation_size=10,\n",
206206
" validation=False,\n",
207+
" verbose=True\n",
207208
")\n",
208209
"\n",
209210
"# Create figure with three subplots\n",
@@ -246,7 +247,7 @@
246247
},
247248
{
248249
"cell_type": "code",
249-
"execution_count": 9,
250+
"execution_count": null,
250251
"metadata": {},
251252
"outputs": [
252253
{
@@ -306,9 +307,9 @@
306307
"\n",
307308
"# Apply the checkerboard pattern for all three parts of the ternary pattern\n",
308309
"square_size = (5, 5) # 5x5 pixel squares\n",
309-
"checkerboard_temp_0 = make_checkerboard_xr(air_temp, square_size, keep_pattern=0, validation=True)\n",
310-
"checkerboard_temp_1 = make_checkerboard_xr(air_temp, square_size, keep_pattern=1, validation=True)\n",
311-
"checkerboard_temp_2 = make_checkerboard_xr(air_temp, square_size, keep_pattern=2, validation=True)\n",
310+
"checkerboard_temp_0 = make_checkerboard_xr(air_temp, square_size, keep_pattern=0, validation=True, verbose=True)\n",
311+
"checkerboard_temp_1 = make_checkerboard_xr(air_temp, square_size, keep_pattern=1, validation=True, verbose=True))\n",
312+
"checkerboard_temp_2 = make_checkerboard_xr(air_temp, square_size, keep_pattern=2, validation=True, verbose=True))\n",
312313
"\n",
313314
"# Plotting\n",
314315
"fig, axs = plt.subplots(2, 2, figsize=(15, 15))\n",
@@ -352,7 +353,7 @@
352353
},
353354
{
354355
"cell_type": "code",
355-
"execution_count": 10,
356+
"execution_count": null,
356357
"metadata": {},
357358
"outputs": [
358359
{
@@ -413,8 +414,8 @@
413414
"\n",
414415
"# Apply the checkerboard pattern for both parts of the binary pattern\n",
415416
"square_size = (7, 5) # 7x5 pixel squares\n",
416-
"checkerboard_temp_0 = make_checkerboard_xr(air_temp, square_size, separation_size=3, keep_pattern=0, validation=False)\n",
417-
"checkerboard_temp_1 = make_checkerboard_xr(air_temp, square_size, separation_size=3, keep_pattern=1, validation=False)\n",
417+
"checkerboard_temp_0 = make_checkerboard_xr(air_temp, square_size, separation_size=3, keep_pattern=0, validation=False, verbose=True))\n",
418+
"checkerboard_temp_1 = make_checkerboard_xr(air_temp, square_size, separation_size=3, keep_pattern=1, validation=False, verbose=True))\n",
418419
"\n",
419420
"# Plotting\n",
420421
"fig, axs = plt.subplots(1, 3, figsize=(20, 6))\n",

src/CheckmateSample.egg-info/PKG-INFO

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Metadata-Version: 2.1
1+
Metadata-Version: 2.4
22
Name: CheckmateSample
3-
Version: 0.0.post1
3+
Version: 0.0.post154
44
Summary: A sample project with checkerboard functionality
55
Author-email: Thomas Martin <tmartin@ucar.edu>
66
Project-URL: Homepage, https://github.com/ThomasMGeo/CheckmateSample
@@ -13,6 +13,7 @@ Provides-Extra: dev
1313
Requires-Dist: pytest>=7.4.0; extra == "dev"
1414
Requires-Dist: ruff>=0.1.3; extra == "dev"
1515
Requires-Dist: setuptools_scm>=8.1; extra == "dev"
16+
Dynamic: license-file
1617

1718
# CheckmateSample
1819

257 Bytes
Binary file not shown.
1.42 KB
Binary file not shown.
7.37 KB
Binary file not shown.

src/CheckmateSample/generator.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def make_checkerboard(
77
square_size: tuple[int, int],
88
separation_size: int = None,
99
validation: bool = False,
10+
verbose: bool = False,
1011
) -> np.ndarray:
1112
"""
1213
Create a checkerboard pattern.
@@ -33,7 +34,7 @@ def make_checkerboard(
3334
raise ValueError("Separation size must be a non-negative, non-zero integer")
3435

3536
# Add warning if inputs are not the same
36-
if rows != cols or sq_rows != sq_cols:
37+
if verbose and (rows != cols or sq_rows != sq_cols):
3738
print(
3839
"Warning: The inputs for board_size or square_size are not the same. This may result in a non-square checkerboard."
3940
)
@@ -71,6 +72,7 @@ def make_checkerboard_xr(
7172
keep_pattern: int = 1,
7273
validation: bool = False,
7374
dim_names: dict = None,
75+
verbose: bool = False,
7476
) -> xr.DataArray:
7577
"""
7678
Apply a checkerboard pattern to an existing xarray DataArray.
@@ -95,9 +97,13 @@ def make_checkerboard_xr(
9597
if sq_y <= 0 or sq_x <= 0:
9698
raise ValueError("Square size dimensions must be positive integers.")
9799
if validation and keep_pattern not in [0, 1, 2]:
98-
raise ValueError("For validation (ternary pattern), keep_pattern must be 0, 1, or 2.")
100+
raise ValueError(
101+
"For validation (ternary pattern), keep_pattern must be 0, 1, or 2."
102+
)
99103
elif not validation and keep_pattern not in [0, 1]:
100-
raise ValueError("For non-validation (binary pattern), keep_pattern must be 0 or 1.")
104+
raise ValueError(
105+
"For non-validation (binary pattern), keep_pattern must be 0 or 1."
106+
)
101107
if sep and sep < 0:
102108
raise ValueError("Separation size must be a non-negative, non-zero integer")
103109

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

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

122130
if y_dim not in da.dims or x_dim not in da.dims:
123-
raise ValueError(f"Specified dimensions {y_dim} and {x_dim} not found in DataArray.")
131+
raise ValueError(
132+
f"Specified dimensions {y_dim} and {x_dim} not found in DataArray."
133+
)
124134

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

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

0 commit comments

Comments
 (0)