diff --git a/build/lib/CheckmateSample/generator.py b/build/lib/CheckmateSample/generator.py index 09ea085..e76ba53 100644 --- a/build/lib/CheckmateSample/generator.py +++ b/build/lib/CheckmateSample/generator.py @@ -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. @@ -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." ) @@ -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. @@ -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") @@ -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") @@ -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." ) diff --git a/example_notebooks/example_notebook.ipynb b/example_notebooks/example_notebook.ipynb index b1fe84c..cf760c5 100644 --- a/example_notebooks/example_notebook.ipynb +++ b/example_notebooks/example_notebook.ipynb @@ -93,7 +93,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -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": [ { @@ -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)\")" ] }, @@ -173,7 +173,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -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", @@ -246,7 +247,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -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", @@ -352,7 +353,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -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", diff --git a/src/CheckmateSample.egg-info/PKG-INFO b/src/CheckmateSample.egg-info/PKG-INFO index 769c603..1d2a93e 100644 --- a/src/CheckmateSample.egg-info/PKG-INFO +++ b/src/CheckmateSample.egg-info/PKG-INFO @@ -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 Project-URL: Homepage, https://github.com/ThomasMGeo/CheckmateSample @@ -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 diff --git a/src/CheckmateSample/__pycache__/__init__.cpython-312.pyc b/src/CheckmateSample/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..ee8f3be Binary files /dev/null and b/src/CheckmateSample/__pycache__/__init__.cpython-312.pyc differ diff --git a/src/CheckmateSample/__pycache__/_version.cpython-312.pyc b/src/CheckmateSample/__pycache__/_version.cpython-312.pyc new file mode 100644 index 0000000..0a1173d Binary files /dev/null and b/src/CheckmateSample/__pycache__/_version.cpython-312.pyc differ diff --git a/src/CheckmateSample/__pycache__/generator.cpython-312.pyc b/src/CheckmateSample/__pycache__/generator.cpython-312.pyc new file mode 100644 index 0000000..f1d77aa Binary files /dev/null and b/src/CheckmateSample/__pycache__/generator.cpython-312.pyc differ diff --git a/src/CheckmateSample/generator.py b/src/CheckmateSample/generator.py index 09ea085..e76ba53 100644 --- a/src/CheckmateSample/generator.py +++ b/src/CheckmateSample/generator.py @@ -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. @@ -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." ) @@ -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. @@ -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") @@ -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") @@ -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." ) diff --git a/tests/__pycache__/test_make_checkerboard.cpython-312-pytest-7.4.4.pyc b/tests/__pycache__/test_make_checkerboard.cpython-312-pytest-7.4.4.pyc new file mode 100644 index 0000000..008bf6e Binary files /dev/null and b/tests/__pycache__/test_make_checkerboard.cpython-312-pytest-7.4.4.pyc differ diff --git a/tests/test_make_checkerboard.py b/tests/test_make_checkerboard.py index ea5c6df..703ba0f 100644 --- a/tests/test_make_checkerboard.py +++ b/tests/test_make_checkerboard.py @@ -6,32 +6,40 @@ def test_checkerboard_values_zero_one(): board_size = (10, 10) square_size = (2, 2) - checkerboard = make_checkerboard(board_size, square_size) - assert np.all(np.logical_or(checkerboard == 0, checkerboard == 1)), "Checkerboard should only contain 0 and 1" + checkerboard = make_checkerboard(board_size, square_size, verbose=True) + assert np.all( + np.logical_or(checkerboard == 0, checkerboard == 1) + ), "Checkerboard should only contain 0 and 1" def test_masked_checkerboard_values_minusone_zero_one(): board_size = (10, 10) square_size = (2, 2) separation_size = 1 - checkerboard = make_checkerboard(board_size, square_size, separation_size) + checkerboard = make_checkerboard( + board_size, square_size, separation_size, verbose=True + ) # Use logical_or.reduce for multiple conditions - valid_values = np.logical_or.reduce([checkerboard == -1, checkerboard == 0, checkerboard == 1]) + valid_values = np.logical_or.reduce( + [checkerboard == -1, checkerboard == 0, checkerboard == 1] + ) assert np.all(valid_values), "Checkerboard should only contain -1, 0 and 1" def test_checkerboard_shape(): board_size = (8, 8) square_size = (1, 1) - checkerboard = make_checkerboard(board_size, square_size) + checkerboard = make_checkerboard(board_size, square_size, verbose=True) assert checkerboard.shape == board_size def test_checkerboard_values(): board_size = (4, 4) square_size = (1, 1) - checkerboard = make_checkerboard(board_size, square_size) - expected = np.array([[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0]], dtype="float32") + checkerboard = make_checkerboard(board_size, square_size, verbose=True) + expected = np.array( + [[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0]], dtype="float32" + ) np.testing.assert_array_equal(checkerboard, expected) @@ -39,15 +47,20 @@ def test_masked_checkerboard_values(): board_size = (4, 4) square_size = (1, 1) setparation_size = 1 - checkerboard = make_checkerboard(board_size, square_size, setparation_size) - expected = np.array([[0, -1, 1, -1], [-1, -1, -1, -1], [1, -1, 0, -1], [-1, -1, -1, -1]], dtype="float32") + checkerboard = make_checkerboard( + board_size, square_size, setparation_size, verbose=True + ) + expected = np.array( + [[0, -1, 1, -1], [-1, -1, -1, -1], [1, -1, 0, -1], [-1, -1, -1, -1]], + dtype="float32", + ) np.testing.assert_array_equal(checkerboard, expected) def test_checkerboard_larger_squares(): board_size = (6, 6) square_size = (2, 2) - checkerboard = make_checkerboard(board_size, square_size) + checkerboard = make_checkerboard(board_size, square_size, verbose=True) expected = np.array( [ [0, 0, 1, 1, 0, 0], @@ -66,7 +79,9 @@ def test_masked_checkerboard_larger_squares(): board_size = (6, 6) square_size = (2, 2) separation_size = 1 - checkerboard = make_checkerboard(board_size, square_size, separation_size) + checkerboard = make_checkerboard( + board_size, square_size, separation_size, verbose=True + ) # Update expected to match the actual implementation pattern expected = np.array( [ @@ -85,9 +100,17 @@ def test_masked_checkerboard_larger_squares(): def test_checkerboard_rectangular(): board_size = (6, 4) square_size = (2, 1) - checkerboard = make_checkerboard(board_size, square_size) + checkerboard = make_checkerboard(board_size, square_size, verbose=True) expected = np.array( - [[0, 1, 0, 1], [0, 1, 0, 1], [1, 0, 1, 0], [1, 0, 1, 0], [0, 1, 0, 1], [0, 1, 0, 1]], dtype="float32" + [ + [0, 1, 0, 1], + [0, 1, 0, 1], + [1, 0, 1, 0], + [1, 0, 1, 0], + [0, 1, 0, 1], + [0, 1, 0, 1], + ], + dtype="float32", ) np.testing.assert_array_equal(checkerboard, expected) @@ -96,9 +119,18 @@ def test_masked_checkerboard_rectangular(): board_size = (6, 4) square_size = (2, 1) setparation_size = 1 - checkerboard = make_checkerboard(board_size, square_size, setparation_size) + checkerboard = make_checkerboard( + board_size, square_size, setparation_size, verbose=True + ) expected = np.array( - [[0, -1, 1, -1], [0, -1, 1, -1], [-1, -1, -1, -1], [1, -1, 0, -1], [1, -1, 0, -1], [-1, -1, -1, -1]], + [ + [0, -1, 1, -1], + [0, -1, 1, -1], + [-1, -1, -1, -1], + [1, -1, 0, -1], + [1, -1, 0, -1], + [-1, -1, -1, -1], + ], dtype="float32", ) np.testing.assert_array_equal(checkerboard, expected) @@ -107,22 +139,23 @@ def test_masked_checkerboard_rectangular(): def test_checkerboard_dtype(): board_size = (4, 4) square_size = (1, 1) - checkerboard = make_checkerboard(board_size, square_size) + checkerboard = make_checkerboard(board_size, square_size, verbose=True) assert checkerboard.dtype == np.float32 @pytest.mark.parametrize( - "board_size,square_size", [((0, 0), (1, 1)), ((5, 5), (0, 0)), ((-1, 5), (1, 1)), ((5, 5), (-1, 1))] + "board_size,square_size", + [((0, 0), (1, 1)), ((5, 5), (0, 0)), ((-1, 5), (1, 1)), ((5, 5), (-1, 1))], ) def test_invalid_inputs(board_size, square_size): with pytest.raises((ValueError, ZeroDivisionError)): - make_checkerboard(board_size, square_size) + make_checkerboard(board_size, square_size, verbose=True) def test_make_checkerboard_validation_true(): board_size = (6, 6) square_size = (2, 2) - result = make_checkerboard(board_size, square_size, validation=True) + result = make_checkerboard(board_size, square_size, validation=True, verbose=True) # Check if the result contains only 0, 1, and 2 assert np.all(np.isin(result, [0, 1, 2])) @@ -147,18 +180,27 @@ def test_make_checkerboard_validation_true(): def test_make_checkerboard_warning_non_square_board(capsys): - make_checkerboard((5, 6), (2, 2)) + make_checkerboard((5, 6), (2, 2), verbose=True) captured = capsys.readouterr() - assert "Warning: The inputs for board_size or square_size are not the same" in captured.out + assert ( + "Warning: The inputs for board_size or square_size are not the same" + in captured.out + ) def test_make_checkerboard_warning_non_square_squares(capsys): - make_checkerboard((6, 6), (2, 3)) + make_checkerboard((6, 6), (2, 3), verbose=True) captured = capsys.readouterr() - assert "Warning: The inputs for board_size or square_size are not the same" in captured.out + assert ( + "Warning: The inputs for board_size or square_size are not the same" + in captured.out + ) def test_make_checkerboard_warning_both_non_square(capsys): - make_checkerboard((5, 6), (2, 3)) + make_checkerboard((5, 6), (2, 3), verbose=True) captured = capsys.readouterr() - assert "Warning: The inputs for board_size or square_size are not the same" in captured.out + assert ( + "Warning: The inputs for board_size or square_size are not the same" + in captured.out + )